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.41"}; 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 /* Default write cache setting for SATA ATAPI tape */ 421 int sata_atapitape_write_cache = 1; /* enabled */ 422 423 /* 424 * Linked list of HBA instances 425 */ 426 static sata_hba_inst_t *sata_hba_list = NULL; 427 static sata_hba_inst_t *sata_hba_list_tail = NULL; 428 /* 429 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 430 * structure and in sata soft state. 431 */ 432 433 /* 434 * Event daemon related variables 435 */ 436 static kmutex_t sata_event_mutex; 437 static kcondvar_t sata_event_cv; 438 static kthread_t *sata_event_thread = NULL; 439 static int sata_event_thread_terminate = 0; 440 static int sata_event_pending = 0; 441 static int sata_event_thread_active = 0; 442 extern pri_t minclsyspri; 443 444 /* 445 * NCQ error recovery command 446 */ 447 static const sata_cmd_t sata_rle_cmd = { 448 SATA_CMD_REV, 449 NULL, 450 { 451 SATA_DIR_READ 452 }, 453 ATA_ADDR_LBA48, 454 0, 455 0, 456 0, 457 0, 458 0, 459 1, 460 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 461 0, 462 0, 463 0, 464 SATAC_READ_LOG_EXT, 465 0, 466 0, 467 0, 468 }; 469 470 /* 471 * ATAPI error recovery CDB 472 */ 473 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 474 SCMD_REQUEST_SENSE, 475 0, /* Only fixed RQ format is supported */ 476 0, 477 0, 478 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 479 0 480 }; 481 482 483 /* Warlock directives */ 484 485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 487 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 488 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 489 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 491 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 492 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 494 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 496 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 497 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 498 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 499 sata_hba_inst::satahba_scsi_tran)) 500 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 502 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 503 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 504 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 505 sata_hba_inst::satahba_event_flags)) 506 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 507 sata_cport_info::cport_devp)) 508 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 509 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 510 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 511 sata_cport_info::cport_dev_type)) 512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 513 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 514 sata_cport_info::cport_state)) 515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 516 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 517 sata_pmport_info::pmport_state)) 518 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 519 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 520 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 521 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 522 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 523 #ifdef SATA_DEBUG 524 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 525 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 526 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 527 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 528 #endif 529 530 /* End of warlock directives */ 531 532 /* ************** loadable module configuration functions ************** */ 533 534 int 535 _init() 536 { 537 int rval; 538 539 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 540 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 541 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 542 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 543 if ((rval = mod_install(&modlinkage)) != 0) { 544 #ifdef SATA_DEBUG 545 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 546 #endif 547 mutex_destroy(&sata_log_mutex); 548 cv_destroy(&sata_event_cv); 549 mutex_destroy(&sata_event_mutex); 550 mutex_destroy(&sata_mutex); 551 } 552 return (rval); 553 } 554 555 int 556 _fini() 557 { 558 int rval; 559 560 if ((rval = mod_remove(&modlinkage)) != 0) 561 return (rval); 562 563 mutex_destroy(&sata_log_mutex); 564 cv_destroy(&sata_event_cv); 565 mutex_destroy(&sata_event_mutex); 566 mutex_destroy(&sata_mutex); 567 return (rval); 568 } 569 570 int 571 _info(struct modinfo *modinfop) 572 { 573 return (mod_info(&modlinkage, modinfop)); 574 } 575 576 577 578 /* ********************* SATA HBA entry points ********************* */ 579 580 581 /* 582 * Called by SATA HBA from _init(). 583 * Registers HBA driver instance/sata framework pair with scsi framework, by 584 * calling scsi_hba_init(). 585 * 586 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 587 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 588 * cb_ops pointer in SATA HBA driver dev_ops structure. 589 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 590 * 591 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 592 * driver. 593 */ 594 int 595 sata_hba_init(struct modlinkage *modlp) 596 { 597 int rval; 598 struct dev_ops *hba_ops; 599 600 SATADBG1(SATA_DBG_HBA_IF, NULL, 601 "sata_hba_init: name %s \n", 602 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 603 /* 604 * Fill-up cb_ops and dev_ops when necessary 605 */ 606 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 607 /* 608 * Provide pointer to SATA dev_ops 609 */ 610 hba_ops->devo_cb_ops = &sata_cb_ops; 611 612 /* 613 * Register SATA HBA with SCSI framework 614 */ 615 if ((rval = scsi_hba_init(modlp)) != 0) { 616 SATADBG1(SATA_DBG_HBA_IF, NULL, 617 "sata_hba_init: scsi hba init failed\n", NULL); 618 return (rval); 619 } 620 621 return (0); 622 } 623 624 625 /* HBA attach stages */ 626 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 627 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 628 #define HBA_ATTACH_STAGE_SETUP 4 629 #define HBA_ATTACH_STAGE_LINKED 8 630 631 632 /* 633 * 634 * Called from SATA HBA driver's attach routine to attach an instance of 635 * the HBA. 636 * 637 * For DDI_ATTACH command: 638 * sata_hba_inst structure is allocated here and initialized with pointers to 639 * SATA framework implementation of required scsi tran functions. 640 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 641 * to the soft structure (sata_hba_inst) allocated by SATA framework for 642 * SATA HBA instance related data. 643 * The scsi_tran's tran_hba_private field is used by SATA framework to 644 * store a pointer to per-HBA-instance of sata_hba_inst structure. 645 * The sata_hba_inst structure is cross-linked to scsi tran structure. 646 * Among other info, a pointer to sata_hba_tran structure is stored in 647 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 648 * linked together into the list, pointed to by sata_hba_list. 649 * On the first HBA instance attach the sata event thread is initialized. 650 * Attachment points are created for all SATA ports of the HBA being attached. 651 * All HBA instance's SATA ports are probed and type of plugged devices is 652 * determined. For each device of a supported type, a target node is created. 653 * 654 * DDI_SUCCESS is returned when attachment process is successful, 655 * DDI_FAILURE is returned otherwise. 656 * 657 * For DDI_RESUME command: 658 * Not implemented at this time (postponed until phase 2 of the development). 659 */ 660 int 661 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 662 ddi_attach_cmd_t cmd) 663 { 664 sata_hba_inst_t *sata_hba_inst; 665 scsi_hba_tran_t *scsi_tran = NULL; 666 int hba_attach_state = 0; 667 char taskq_name[MAXPATHLEN]; 668 669 SATADBG3(SATA_DBG_HBA_IF, NULL, 670 "sata_hba_attach: node %s (%s%d)\n", 671 ddi_node_name(dip), ddi_driver_name(dip), 672 ddi_get_instance(dip)); 673 674 if (cmd == DDI_RESUME) { 675 /* 676 * Postponed until phase 2 of the development 677 */ 678 return (DDI_FAILURE); 679 } 680 681 if (cmd != DDI_ATTACH) { 682 return (DDI_FAILURE); 683 } 684 685 /* cmd == DDI_ATTACH */ 686 687 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 688 SATA_LOG_D((NULL, CE_WARN, 689 "sata_hba_attach: invalid sata_hba_tran")); 690 return (DDI_FAILURE); 691 } 692 /* 693 * Allocate and initialize SCSI tran structure. 694 * SATA copy of tran_bus_config is provided to create port nodes. 695 */ 696 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 697 if (scsi_tran == NULL) 698 return (DDI_FAILURE); 699 /* 700 * Allocate soft structure for SATA HBA instance. 701 * There is a separate softstate for each HBA instance. 702 */ 703 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 704 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 705 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 706 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 707 708 /* 709 * scsi_trans's tran_hba_private is used by SATA Framework to point to 710 * soft structure allocated by SATA framework for 711 * SATA HBA instance related data. 712 */ 713 scsi_tran->tran_hba_private = sata_hba_inst; 714 scsi_tran->tran_tgt_private = NULL; 715 716 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 717 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 718 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 719 720 scsi_tran->tran_start = sata_scsi_start; 721 scsi_tran->tran_reset = sata_scsi_reset; 722 scsi_tran->tran_abort = sata_scsi_abort; 723 scsi_tran->tran_getcap = sata_scsi_getcap; 724 scsi_tran->tran_setcap = sata_scsi_setcap; 725 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 726 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 727 728 scsi_tran->tran_dmafree = sata_scsi_dmafree; 729 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 730 731 scsi_tran->tran_reset_notify = NULL; 732 scsi_tran->tran_get_bus_addr = NULL; 733 scsi_tran->tran_quiesce = NULL; 734 scsi_tran->tran_unquiesce = NULL; 735 scsi_tran->tran_bus_reset = NULL; 736 737 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 738 scsi_tran, 0) != DDI_SUCCESS) { 739 #ifdef SATA_DEBUG 740 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 741 ddi_driver_name(dip), ddi_get_instance(dip)); 742 #endif 743 goto fail; 744 } 745 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 746 747 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 748 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 749 "sata", 1) != DDI_PROP_SUCCESS) { 750 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 751 "failed to create hba sata prop")); 752 goto fail; 753 } 754 } 755 756 /* 757 * Save pointers in hba instance soft state. 758 */ 759 sata_hba_inst->satahba_scsi_tran = scsi_tran; 760 sata_hba_inst->satahba_tran = sata_tran; 761 sata_hba_inst->satahba_dip = dip; 762 763 /* 764 * Create a task queue to handle emulated commands completion 765 * Use node name, dash, instance number as the queue name. 766 */ 767 taskq_name[0] = '\0'; 768 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 769 sizeof (taskq_name)); 770 (void) snprintf(taskq_name + strlen(taskq_name), 771 sizeof (taskq_name) - strlen(taskq_name), 772 "-%d", DEVI(dip)->devi_instance); 773 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 774 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports, 775 TASKQ_DYNAMIC); 776 777 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 778 779 /* 780 * Create events thread if not created yet. 781 */ 782 sata_event_thread_control(1); 783 784 /* 785 * Link this hba instance into the list. 786 */ 787 mutex_enter(&sata_mutex); 788 789 if (sata_hba_list == NULL) { 790 /* 791 * The first instance of HBA is attached. 792 * Set current/active default maximum NCQ/TCQ queue depth for 793 * all SATA devices. It is done here and now, to eliminate the 794 * possibility of the dynamic, programatic modification of the 795 * queue depth via global (and public) sata_max_queue_depth 796 * variable (this would require special handling in HBA drivers) 797 */ 798 sata_current_max_qdepth = sata_max_queue_depth; 799 if (sata_current_max_qdepth > 32) 800 sata_current_max_qdepth = 32; 801 else if (sata_current_max_qdepth < 1) 802 sata_current_max_qdepth = 1; 803 } 804 805 sata_hba_inst->satahba_next = NULL; 806 sata_hba_inst->satahba_prev = sata_hba_list_tail; 807 if (sata_hba_list == NULL) { 808 sata_hba_list = sata_hba_inst; 809 } 810 if (sata_hba_list_tail != NULL) { 811 sata_hba_list_tail->satahba_next = sata_hba_inst; 812 } 813 sata_hba_list_tail = sata_hba_inst; 814 mutex_exit(&sata_mutex); 815 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 816 817 /* 818 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 819 * SATA HBA driver should not use its own open/close entry points. 820 * 821 * Make sure that instance number doesn't overflow 822 * when forming minor numbers. 823 */ 824 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 825 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 826 INST2DEVCTL(ddi_get_instance(dip)), 827 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 828 #ifdef SATA_DEBUG 829 cmn_err(CE_WARN, "sata_hba_attach: " 830 "cannot create devctl minor node"); 831 #endif 832 goto fail; 833 } 834 835 836 /* 837 * Set-up kstats here, if necessary. 838 * (postponed until future phase of the development). 839 */ 840 841 /* 842 * Indicate that HBA is attached. This will enable events processing 843 * for this HBA. 844 */ 845 sata_hba_inst->satahba_attached = 1; 846 /* 847 * Probe controller ports. This operation will describe a current 848 * controller/port/multipliers/device configuration and will create 849 * attachment points. 850 * We may end-up with just a controller with no devices attached. 851 * For the ports with a supported device attached, device target nodes 852 * are created and devices are initialized. 853 */ 854 sata_probe_ports(sata_hba_inst); 855 856 return (DDI_SUCCESS); 857 858 fail: 859 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 860 (void) sata_remove_hba_instance(dip); 861 if (sata_hba_list == NULL) 862 sata_event_thread_control(0); 863 } 864 865 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 866 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 867 taskq_destroy(sata_hba_inst->satahba_taskq); 868 } 869 870 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 871 (void) scsi_hba_detach(dip); 872 873 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 874 mutex_destroy(&sata_hba_inst->satahba_mutex); 875 kmem_free((void *)sata_hba_inst, 876 sizeof (struct sata_hba_inst)); 877 scsi_hba_tran_free(scsi_tran); 878 } 879 880 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 881 ddi_driver_name(dip), ddi_get_instance(dip)); 882 883 return (DDI_FAILURE); 884 } 885 886 887 /* 888 * Called by SATA HBA from to detach an instance of the driver. 889 * 890 * For DDI_DETACH command: 891 * Free local structures allocated for SATA HBA instance during 892 * sata_hba_attach processing. 893 * 894 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 895 * 896 * For DDI_SUSPEND command: 897 * Not implemented at this time (postponed until phase 2 of the development) 898 * Returnd DDI_SUCCESS. 899 * 900 * When the last HBA instance is detached, the event daemon is terminated. 901 * 902 * NOTE: cport support only, no port multiplier support. 903 */ 904 int 905 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 906 { 907 dev_info_t *tdip; 908 sata_hba_inst_t *sata_hba_inst; 909 scsi_hba_tran_t *scsi_hba_tran; 910 sata_cport_info_t *cportinfo; 911 sata_drive_info_t *sdinfo; 912 int ncport; 913 914 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 915 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 916 917 switch (cmd) { 918 case DDI_DETACH: 919 920 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 921 return (DDI_FAILURE); 922 923 sata_hba_inst = scsi_hba_tran->tran_hba_private; 924 if (sata_hba_inst == NULL) 925 return (DDI_FAILURE); 926 927 if (scsi_hba_detach(dip) == DDI_FAILURE) { 928 sata_hba_inst->satahba_attached = 1; 929 return (DDI_FAILURE); 930 } 931 932 /* 933 * Free all target nodes - at this point 934 * devices should be at least offlined 935 * otherwise scsi_hba_detach() should not be called. 936 */ 937 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 938 ncport++) { 939 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 940 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 941 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 942 if (sdinfo != NULL) { 943 tdip = sata_get_target_dip(dip, 944 ncport); 945 if (tdip != NULL) { 946 if (ndi_devi_offline(tdip, 947 NDI_DEVI_REMOVE) != 948 NDI_SUCCESS) { 949 SATA_LOG_D(( 950 sata_hba_inst, 951 CE_WARN, 952 "sata_hba_detach: " 953 "Target node not " 954 "removed !")); 955 return (DDI_FAILURE); 956 } 957 } 958 } 959 } 960 } 961 /* 962 * Disable sata event daemon processing for this HBA 963 */ 964 sata_hba_inst->satahba_attached = 0; 965 966 /* 967 * Remove event daemon thread, if it is last HBA instance. 968 */ 969 970 mutex_enter(&sata_mutex); 971 if (sata_hba_list->satahba_next == NULL) { 972 mutex_exit(&sata_mutex); 973 sata_event_thread_control(0); 974 mutex_enter(&sata_mutex); 975 } 976 mutex_exit(&sata_mutex); 977 978 /* Remove this HBA instance from the HBA list */ 979 sata_remove_hba_instance(dip); 980 981 /* 982 * At this point there should be no target nodes attached. 983 * Detach and destroy device and port info structures. 984 */ 985 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 986 ncport++) { 987 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 988 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 989 sdinfo = 990 cportinfo->cport_devp.cport_sata_drive; 991 if (sdinfo != NULL) { 992 /* Release device structure */ 993 kmem_free(sdinfo, 994 sizeof (sata_drive_info_t)); 995 } 996 /* Release cport info */ 997 mutex_destroy(&cportinfo->cport_mutex); 998 kmem_free(cportinfo, 999 sizeof (sata_cport_info_t)); 1000 } 1001 } 1002 1003 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1004 1005 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1006 1007 taskq_destroy(sata_hba_inst->satahba_taskq); 1008 1009 mutex_destroy(&sata_hba_inst->satahba_mutex); 1010 kmem_free((void *)sata_hba_inst, 1011 sizeof (struct sata_hba_inst)); 1012 1013 return (DDI_SUCCESS); 1014 1015 case DDI_SUSPEND: 1016 /* 1017 * Postponed until phase 2 1018 */ 1019 return (DDI_FAILURE); 1020 1021 default: 1022 return (DDI_FAILURE); 1023 } 1024 } 1025 1026 1027 /* 1028 * Called by an HBA drive from _fini() routine. 1029 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1030 */ 1031 void 1032 sata_hba_fini(struct modlinkage *modlp) 1033 { 1034 SATADBG1(SATA_DBG_HBA_IF, NULL, 1035 "sata_hba_fini: name %s\n", 1036 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1037 1038 scsi_hba_fini(modlp); 1039 } 1040 1041 1042 /* 1043 * Default open and close routine for sata_hba framework. 1044 * 1045 */ 1046 /* 1047 * Open devctl node. 1048 * 1049 * Returns: 1050 * 0 if node was open successfully, error code otherwise. 1051 * 1052 * 1053 */ 1054 1055 static int 1056 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1057 { 1058 #ifndef __lock_lint 1059 _NOTE(ARGUNUSED(credp)) 1060 #endif 1061 int rv = 0; 1062 dev_info_t *dip; 1063 scsi_hba_tran_t *scsi_hba_tran; 1064 sata_hba_inst_t *sata_hba_inst; 1065 1066 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1067 1068 if (otyp != OTYP_CHR) 1069 return (EINVAL); 1070 1071 dip = sata_devt_to_devinfo(*devp); 1072 if (dip == NULL) 1073 return (ENXIO); 1074 1075 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1076 return (ENXIO); 1077 1078 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1079 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1080 return (ENXIO); 1081 1082 mutex_enter(&sata_mutex); 1083 if (flags & FEXCL) { 1084 if (sata_hba_inst->satahba_open_flag != 0) { 1085 rv = EBUSY; 1086 } else { 1087 sata_hba_inst->satahba_open_flag = 1088 SATA_DEVCTL_EXOPENED; 1089 } 1090 } else { 1091 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1092 rv = EBUSY; 1093 } else { 1094 sata_hba_inst->satahba_open_flag = 1095 SATA_DEVCTL_SOPENED; 1096 } 1097 } 1098 mutex_exit(&sata_mutex); 1099 1100 return (rv); 1101 } 1102 1103 1104 /* 1105 * Close devctl node. 1106 * Returns: 1107 * 0 if node was closed successfully, error code otherwise. 1108 * 1109 */ 1110 1111 static int 1112 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1113 { 1114 #ifndef __lock_lint 1115 _NOTE(ARGUNUSED(credp)) 1116 _NOTE(ARGUNUSED(flag)) 1117 #endif 1118 dev_info_t *dip; 1119 scsi_hba_tran_t *scsi_hba_tran; 1120 sata_hba_inst_t *sata_hba_inst; 1121 1122 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1123 1124 if (otyp != OTYP_CHR) 1125 return (EINVAL); 1126 1127 dip = sata_devt_to_devinfo(dev); 1128 if (dip == NULL) 1129 return (ENXIO); 1130 1131 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1132 return (ENXIO); 1133 1134 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1135 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1136 return (ENXIO); 1137 1138 mutex_enter(&sata_mutex); 1139 sata_hba_inst->satahba_open_flag = 0; 1140 mutex_exit(&sata_mutex); 1141 return (0); 1142 } 1143 1144 1145 1146 /* 1147 * Standard IOCTL commands for SATA hotplugging. 1148 * Implemented DEVCTL_AP commands: 1149 * DEVCTL_AP_CONNECT 1150 * DEVCTL_AP_DISCONNECT 1151 * DEVCTL_AP_CONFIGURE 1152 * DEVCTL_UNCONFIGURE 1153 * DEVCTL_AP_CONTROL 1154 * 1155 * Commands passed to default ndi ioctl handler: 1156 * DEVCTL_DEVICE_GETSTATE 1157 * DEVCTL_DEVICE_ONLINE 1158 * DEVCTL_DEVICE_OFFLINE 1159 * DEVCTL_DEVICE_REMOVE 1160 * DEVCTL_DEVICE_INSERT 1161 * DEVCTL_BUS_GETSTATE 1162 * 1163 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1164 * if not. 1165 * 1166 * Returns: 1167 * 0 if successful, 1168 * error code if operation failed. 1169 * 1170 * NOTE: Port Multiplier is not supported. 1171 * 1172 */ 1173 1174 static int 1175 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1176 int *rvalp) 1177 { 1178 #ifndef __lock_lint 1179 _NOTE(ARGUNUSED(credp)) 1180 _NOTE(ARGUNUSED(rvalp)) 1181 #endif 1182 int rv = 0; 1183 int32_t comp_port = -1; 1184 dev_info_t *dip; 1185 devctl_ap_state_t ap_state; 1186 struct devctl_iocdata *dcp = NULL; 1187 scsi_hba_tran_t *scsi_hba_tran; 1188 sata_hba_inst_t *sata_hba_inst; 1189 sata_device_t sata_device; 1190 sata_cport_info_t *cportinfo; 1191 int cport, pmport, qual; 1192 int rval = SATA_SUCCESS; 1193 1194 dip = sata_devt_to_devinfo(dev); 1195 if (dip == NULL) 1196 return (ENXIO); 1197 1198 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1199 return (ENXIO); 1200 1201 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1202 if (sata_hba_inst == NULL) 1203 return (ENXIO); 1204 1205 if (sata_hba_inst->satahba_tran == NULL) 1206 return (ENXIO); 1207 1208 switch (cmd) { 1209 1210 case DEVCTL_DEVICE_GETSTATE: 1211 case DEVCTL_DEVICE_ONLINE: 1212 case DEVCTL_DEVICE_OFFLINE: 1213 case DEVCTL_DEVICE_REMOVE: 1214 case DEVCTL_BUS_GETSTATE: 1215 /* 1216 * There may be more cases that we want to pass to default 1217 * handler rather than fail them. 1218 */ 1219 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1220 } 1221 1222 /* read devctl ioctl data */ 1223 if (cmd != DEVCTL_AP_CONTROL) { 1224 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1225 return (EFAULT); 1226 1227 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1228 -1) { 1229 if (dcp) 1230 ndi_dc_freehdl(dcp); 1231 return (EINVAL); 1232 } 1233 1234 cport = SCSI_TO_SATA_CPORT(comp_port); 1235 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1236 /* Only cport is considered now, i.e. SATA_ADDR_CPORT */ 1237 qual = SATA_ADDR_CPORT; 1238 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1239 qual) != 0) { 1240 ndi_dc_freehdl(dcp); 1241 return (EINVAL); 1242 } 1243 1244 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1245 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1246 cport_mutex); 1247 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1248 /* 1249 * Cannot process ioctl request now. Come back later. 1250 */ 1251 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1252 cport_mutex); 1253 ndi_dc_freehdl(dcp); 1254 return (EBUSY); 1255 } 1256 /* Block event processing for this port */ 1257 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1258 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1259 1260 sata_device.satadev_addr.cport = cport; 1261 sata_device.satadev_addr.pmport = pmport; 1262 sata_device.satadev_addr.qual = qual; 1263 sata_device.satadev_rev = SATA_DEVICE_REV; 1264 } 1265 1266 switch (cmd) { 1267 1268 case DEVCTL_AP_DISCONNECT: 1269 1270 /* 1271 * Normally, cfgadm sata plugin will try to offline 1272 * (unconfigure) device before this request. Nevertheless, 1273 * if a device is still configured, we need to 1274 * attempt to offline and unconfigure device first, and we will 1275 * deactivate the port regardless of the unconfigure 1276 * operation results. 1277 * 1278 */ 1279 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1280 1281 break; 1282 1283 case DEVCTL_AP_UNCONFIGURE: 1284 1285 /* 1286 * The unconfigure operation uses generic nexus operation to 1287 * offline a device. It leaves a target device node attached. 1288 * and obviously sata_drive_info attached as well, because 1289 * from the hardware point of view nothing has changed. 1290 */ 1291 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1292 break; 1293 1294 case DEVCTL_AP_CONNECT: 1295 { 1296 /* 1297 * The sata cfgadm pluging will invoke this operation only if 1298 * port was found in the disconnect state (failed state 1299 * is also treated as the disconnected state). 1300 * If port activation is successful and a device is found 1301 * attached to the port, the initialization sequence is 1302 * executed to probe the port and attach 1303 * a device structure to a port structure. The device is not 1304 * set in configured state (system-wise) by this operation. 1305 */ 1306 1307 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1308 1309 break; 1310 } 1311 1312 case DEVCTL_AP_CONFIGURE: 1313 { 1314 /* 1315 * A port may be in an active or shutdown state. 1316 * If port is in a failed state, operation is aborted. 1317 * If a port is in a shutdown state, sata_tran_port_activate() 1318 * is invoked prior to any other operation. 1319 * 1320 * Onlining the device involves creating a new target node. 1321 * If there is an old target node present (belonging to 1322 * previously removed device), the operation is aborted - the 1323 * old node has to be released and removed before configure 1324 * operation is attempted. 1325 */ 1326 1327 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1328 1329 break; 1330 } 1331 1332 case DEVCTL_AP_GETSTATE: 1333 1334 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1335 1336 ap_state.ap_last_change = (time_t)-1; 1337 ap_state.ap_error_code = 0; 1338 ap_state.ap_in_transition = 0; 1339 1340 /* Copy the return AP-state information to the user space */ 1341 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1342 rv = EFAULT; 1343 } 1344 break; 1345 1346 case DEVCTL_AP_CONTROL: 1347 { 1348 /* 1349 * Generic devctl for hardware specific functionality 1350 */ 1351 sata_ioctl_data_t ioc; 1352 1353 ASSERT(dcp == NULL); 1354 1355 /* Copy in user ioctl data first */ 1356 #ifdef _MULTI_DATAMODEL 1357 if (ddi_model_convert_from(mode & FMODELS) == 1358 DDI_MODEL_ILP32) { 1359 1360 sata_ioctl_data_32_t ioc32; 1361 1362 if (ddi_copyin((void *)arg, (void *)&ioc32, 1363 sizeof (ioc32), mode) != 0) { 1364 rv = EFAULT; 1365 break; 1366 } 1367 ioc.cmd = (uint_t)ioc32.cmd; 1368 ioc.port = (uint_t)ioc32.port; 1369 ioc.get_size = (uint_t)ioc32.get_size; 1370 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1371 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1372 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1373 } else 1374 #endif /* _MULTI_DATAMODEL */ 1375 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1376 mode) != 0) { 1377 return (EFAULT); 1378 } 1379 1380 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1381 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1382 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1383 1384 /* 1385 * To avoid BE/LE and 32/64 issues, a get_size always returns 1386 * a 32-bit number. 1387 */ 1388 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1389 return (EINVAL); 1390 } 1391 /* validate address */ 1392 cport = SCSI_TO_SATA_CPORT(ioc.port); 1393 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1394 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1395 1396 /* Override address qualifier - handle cport only for now */ 1397 qual = SATA_ADDR_CPORT; 1398 1399 if (sata_validate_sata_address(sata_hba_inst, cport, 1400 pmport, qual) != 0) 1401 return (EINVAL); 1402 1403 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1404 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1405 cport_mutex); 1406 /* Is the port locked by event processing daemon ? */ 1407 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1408 /* 1409 * Cannot process ioctl request now. Come back later 1410 */ 1411 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1412 cport_mutex); 1413 return (EBUSY); 1414 } 1415 /* Block event processing for this port */ 1416 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1417 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1418 1419 1420 sata_device.satadev_addr.cport = cport; 1421 sata_device.satadev_addr.pmport = pmport; 1422 sata_device.satadev_addr.qual = qual; 1423 sata_device.satadev_rev = SATA_DEVICE_REV; 1424 1425 switch (ioc.cmd) { 1426 1427 case SATA_CFGA_RESET_PORT: 1428 /* 1429 * There is no protection for configured device. 1430 */ 1431 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1432 break; 1433 1434 case SATA_CFGA_RESET_DEVICE: 1435 /* 1436 * There is no protection for configured device. 1437 */ 1438 rv = sata_ioctl_reset_device(sata_hba_inst, 1439 &sata_device); 1440 break; 1441 1442 case SATA_CFGA_RESET_ALL: 1443 /* 1444 * There is no protection for configured devices. 1445 */ 1446 rv = sata_ioctl_reset_all(sata_hba_inst); 1447 /* 1448 * We return here, because common return is for 1449 * a single port operation - we have already unlocked 1450 * all ports and no dc handle was allocated. 1451 */ 1452 return (rv); 1453 1454 case SATA_CFGA_PORT_DEACTIVATE: 1455 /* 1456 * Arbitrarily unconfigure attached device, if any. 1457 * Even if the unconfigure fails, proceed with the 1458 * port deactivation. 1459 */ 1460 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1461 1462 break; 1463 1464 case SATA_CFGA_PORT_ACTIVATE: 1465 1466 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1467 break; 1468 1469 case SATA_CFGA_PORT_SELF_TEST: 1470 1471 rv = sata_ioctl_port_self_test(sata_hba_inst, 1472 &sata_device); 1473 break; 1474 1475 case SATA_CFGA_GET_DEVICE_PATH: 1476 if (qual == SATA_ADDR_CPORT) 1477 sata_device.satadev_addr.qual = 1478 SATA_ADDR_DCPORT; 1479 else 1480 sata_device.satadev_addr.qual = 1481 SATA_ADDR_DPMPORT; 1482 rv = sata_ioctl_get_device_path(sata_hba_inst, 1483 &sata_device, &ioc, mode); 1484 break; 1485 1486 case SATA_CFGA_GET_AP_TYPE: 1487 1488 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1489 &sata_device, &ioc, mode); 1490 break; 1491 1492 case SATA_CFGA_GET_MODEL_INFO: 1493 1494 rv = sata_ioctl_get_model_info(sata_hba_inst, 1495 &sata_device, &ioc, mode); 1496 break; 1497 1498 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1499 1500 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1501 &sata_device, &ioc, mode); 1502 break; 1503 1504 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1505 1506 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1507 &sata_device, &ioc, mode); 1508 break; 1509 1510 default: 1511 rv = EINVAL; 1512 break; 1513 1514 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1515 1516 break; 1517 } 1518 1519 default: 1520 { 1521 /* 1522 * If we got here, we got an IOCTL that SATA HBA Framework 1523 * does not recognize. Pass ioctl to HBA driver, in case 1524 * it could process it. 1525 */ 1526 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1527 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1528 1529 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1530 "IOCTL 0x%2x not supported in SATA framework, " 1531 "passthrough to HBA", cmd); 1532 1533 if (sata_tran->sata_tran_ioctl == NULL) { 1534 rv = EINVAL; 1535 break; 1536 } 1537 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1538 if (rval != 0) { 1539 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1540 "IOCTL 0x%2x failed in HBA", cmd); 1541 rv = rval; 1542 } 1543 break; 1544 } 1545 1546 } /* End of main IOCTL switch */ 1547 1548 if (dcp) { 1549 ndi_dc_freehdl(dcp); 1550 } 1551 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1552 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1553 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1554 1555 return (rv); 1556 } 1557 1558 1559 /* 1560 * Create error retrieval sata packet 1561 * 1562 * A sata packet is allocated and set-up to contain specified error retrieval 1563 * command and appropriate dma-able data buffer. 1564 * No association with any scsi packet is made and no callback routine is 1565 * specified. 1566 * 1567 * Returns a pointer to sata packet upon successfull packet creation. 1568 * Returns NULL, if packet cannot be created. 1569 */ 1570 sata_pkt_t * 1571 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1572 int pkt_type) 1573 { 1574 sata_hba_inst_t *sata_hba_inst; 1575 sata_pkt_txlate_t *spx; 1576 sata_pkt_t *spkt; 1577 sata_drive_info_t *sdinfo; 1578 1579 mutex_enter(&sata_mutex); 1580 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1581 sata_hba_inst = sata_hba_inst->satahba_next) { 1582 if (SATA_DIP(sata_hba_inst) == dip) 1583 break; 1584 } 1585 mutex_exit(&sata_mutex); 1586 ASSERT(sata_hba_inst != NULL); 1587 1588 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1589 if (sdinfo == NULL) { 1590 sata_log(sata_hba_inst, CE_WARN, 1591 "sata: error recovery request for non-attached device at " 1592 "cport %d", sata_device->satadev_addr.cport); 1593 return (NULL); 1594 } 1595 1596 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1597 spx->txlt_sata_hba_inst = sata_hba_inst; 1598 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1599 spkt = sata_pkt_alloc(spx, NULL); 1600 if (spkt == NULL) { 1601 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1602 return (NULL); 1603 } 1604 /* address is needed now */ 1605 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1606 1607 switch (pkt_type) { 1608 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1609 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1610 return (spkt); 1611 break; 1612 1613 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1614 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1615 return (spkt); 1616 break; 1617 1618 default: 1619 break; 1620 } 1621 1622 sata_pkt_free(spx); 1623 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1624 return (NULL); 1625 1626 } 1627 1628 1629 /* 1630 * Free error retrieval sata packet 1631 * 1632 * Free sata packet and any associated resources allocated previously by 1633 * sata_get_error_retrieval_pkt(). 1634 * 1635 * Void return. 1636 */ 1637 void 1638 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1639 { 1640 sata_pkt_txlate_t *spx = 1641 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1642 1643 ASSERT(sata_pkt != NULL); 1644 1645 sata_free_local_buffer(spx); 1646 sata_pkt_free(spx); 1647 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1648 1649 } 1650 1651 /* 1652 * sata_name_child is for composing the name of the node 1653 * the format of the name is "target,0". 1654 */ 1655 static int 1656 sata_name_child(dev_info_t *dip, char *name, int namelen) 1657 { 1658 int target; 1659 1660 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1661 DDI_PROP_DONTPASS, "target", -1); 1662 if (target == -1) 1663 return (DDI_FAILURE); 1664 (void) snprintf(name, namelen, "%x,0", target); 1665 return (DDI_SUCCESS); 1666 } 1667 1668 1669 1670 /* ****************** SCSA required entry points *********************** */ 1671 1672 /* 1673 * Implementation of scsi tran_tgt_init. 1674 * sata_scsi_tgt_init() initializes scsi_device structure 1675 * 1676 * If successful, DDI_SUCCESS is returned. 1677 * DDI_FAILURE is returned if addressed device does not exist 1678 */ 1679 1680 static int 1681 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1682 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1683 { 1684 #ifndef __lock_lint 1685 _NOTE(ARGUNUSED(hba_dip)) 1686 _NOTE(ARGUNUSED(tgt_dip)) 1687 #endif 1688 sata_device_t sata_device; 1689 sata_drive_info_t *sdinfo; 1690 struct sata_id *sid; 1691 sata_hba_inst_t *sata_hba_inst; 1692 char model[SATA_ID_MODEL_LEN + 1]; 1693 char fw[SATA_ID_FW_LEN + 1]; 1694 char *vid, *pid; 1695 int i; 1696 1697 /* 1698 * Fail tran_tgt_init for .conf stub node 1699 */ 1700 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1701 (void) ndi_merge_node(tgt_dip, sata_name_child); 1702 ddi_set_name_addr(tgt_dip, NULL); 1703 return (DDI_FAILURE); 1704 } 1705 1706 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1707 1708 /* Validate scsi device address */ 1709 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1710 &sata_device) != 0) 1711 return (DDI_FAILURE); 1712 1713 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1714 sata_device.satadev_addr.cport))); 1715 1716 /* sata_device now contains a valid sata address */ 1717 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1718 if (sdinfo == NULL) { 1719 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1720 sata_device.satadev_addr.cport))); 1721 return (DDI_FAILURE); 1722 } 1723 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1724 sata_device.satadev_addr.cport))); 1725 1726 /* 1727 * Check if we need to create a legacy devid (i.e cmdk style) for 1728 * the target disks. 1729 * 1730 * HBA devinfo node will have the property "use-cmdk-devid-format" 1731 * if we need to create cmdk-style devid for all the disk devices 1732 * attached to this controller. This property may have been set 1733 * from HBA driver's .conf file or by the HBA driver in its 1734 * attach(9F) function. 1735 */ 1736 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1737 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1738 "use-cmdk-devid-format", 0) == 1)) { 1739 /* register a legacy devid for this target node */ 1740 sata_target_devid_register(tgt_dip, sdinfo); 1741 } 1742 1743 1744 /* 1745 * 'Identify Device Data' does not always fit in standard SCSI 1746 * INQUIRY data, so establish INQUIRY_* properties with full-form 1747 * of information. 1748 */ 1749 sid = &sdinfo->satadrv_id; 1750 #ifdef _LITTLE_ENDIAN 1751 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 1752 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 1753 #else /* _LITTLE_ENDIAN */ 1754 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 1755 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 1756 #endif /* _LITTLE_ENDIAN */ 1757 model[SATA_ID_MODEL_LEN] = 0; 1758 fw[SATA_ID_FW_LEN] = 0; 1759 1760 /* split model into into vid/pid */ 1761 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 1762 if ((*pid == ' ') || (*pid == '\t')) 1763 break; 1764 if (i < SATA_ID_MODEL_LEN) { 1765 vid = model; 1766 *pid++ = 0; /* terminate vid, establish pid */ 1767 } else { 1768 vid = NULL; /* vid will stay "ATA " */ 1769 pid = model; /* model is all pid */ 1770 } 1771 1772 if (vid) 1773 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 1774 vid, strlen(vid)); 1775 if (pid) 1776 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 1777 pid, strlen(pid)); 1778 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 1779 fw, strlen(fw)); 1780 1781 return (DDI_SUCCESS); 1782 } 1783 1784 /* 1785 * Implementation of scsi tran_tgt_probe. 1786 * Probe target, by calling default scsi routine scsi_hba_probe() 1787 */ 1788 static int 1789 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 1790 { 1791 sata_hba_inst_t *sata_hba_inst = 1792 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 1793 int rval; 1794 1795 rval = scsi_hba_probe(sd, callback); 1796 1797 if (rval == SCSIPROBE_EXISTS) { 1798 /* 1799 * Set property "pm-capable" on the target device node, so that 1800 * the target driver will not try to fetch scsi cycle counters 1801 * before enabling device power-management. 1802 */ 1803 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 1804 "pm-capable", 1)) != DDI_PROP_SUCCESS) { 1805 sata_log(sata_hba_inst, CE_WARN, 1806 "SATA device at port %d: " 1807 "will not be power-managed ", 1808 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 1809 SATA_LOG_D((sata_hba_inst, CE_WARN, 1810 "failure updating pm-capable property")); 1811 } 1812 } 1813 return (rval); 1814 } 1815 1816 /* 1817 * Implementation of scsi tran_tgt_free. 1818 * Release all resources allocated for scsi_device 1819 */ 1820 static void 1821 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1822 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1823 { 1824 #ifndef __lock_lint 1825 _NOTE(ARGUNUSED(hba_dip)) 1826 #endif 1827 sata_device_t sata_device; 1828 sata_drive_info_t *sdinfo; 1829 sata_hba_inst_t *sata_hba_inst; 1830 ddi_devid_t devid; 1831 1832 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1833 1834 /* Validate scsi device address */ 1835 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1836 &sata_device) != 0) 1837 return; 1838 1839 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1840 sata_device.satadev_addr.cport))); 1841 1842 /* sata_device now should contain a valid sata address */ 1843 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1844 if (sdinfo == NULL) { 1845 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1846 sata_device.satadev_addr.cport))); 1847 return; 1848 } 1849 /* 1850 * We did not allocate any resources in sata_scsi_tgt_init() 1851 * other than few properties. 1852 * Free them. 1853 */ 1854 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1855 sata_device.satadev_addr.cport))); 1856 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 1857 1858 /* 1859 * If devid was previously created but not freed up from 1860 * sd(7D) driver (i.e during detach(9F)) then do it here. 1861 */ 1862 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1863 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1864 "use-cmdk-devid-format", 0) == 1) && 1865 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 1866 ddi_devid_unregister(tgt_dip); 1867 ddi_devid_free(devid); 1868 } 1869 } 1870 1871 /* 1872 * Implementation of scsi tran_init_pkt 1873 * Upon successful return, scsi pkt buffer has DMA resources allocated. 1874 * 1875 * It seems that we should always allocate pkt, even if the address is 1876 * for non-existing device - just use some default for dma_attr. 1877 * The reason is that there is no way to communicate this to a caller here. 1878 * Subsequent call to sata_scsi_start may fail appropriately. 1879 * Simply returning NULL does not seem to discourage a target driver... 1880 * 1881 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 1882 */ 1883 static struct scsi_pkt * 1884 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 1885 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 1886 int (*callback)(caddr_t), caddr_t arg) 1887 { 1888 sata_hba_inst_t *sata_hba_inst = 1889 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 1890 dev_info_t *dip = SATA_DIP(sata_hba_inst); 1891 sata_device_t sata_device; 1892 sata_drive_info_t *sdinfo; 1893 sata_pkt_txlate_t *spx; 1894 ddi_dma_attr_t cur_dma_attr; 1895 int rval; 1896 boolean_t new_pkt = TRUE; 1897 1898 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 1899 1900 /* 1901 * We need to translate the address, even if it could be 1902 * a bogus one, for a non-existing device 1903 */ 1904 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 1905 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 1906 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 1907 sata_device.satadev_rev = SATA_DEVICE_REV; 1908 1909 if (pkt == NULL) { 1910 /* 1911 * Have to allocate a brand new scsi packet. 1912 * We need to operate with auto request sense enabled. 1913 */ 1914 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 1915 MAX(statuslen, sizeof (struct scsi_arq_status)), 1916 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 1917 1918 if (pkt == NULL) 1919 return (NULL); 1920 1921 /* Fill scsi packet structure */ 1922 pkt->pkt_comp = (void (*)())NULL; 1923 pkt->pkt_time = 0; 1924 pkt->pkt_resid = 0; 1925 pkt->pkt_statistics = 0; 1926 pkt->pkt_reason = 0; 1927 1928 /* 1929 * pkt_hba_private will point to sata pkt txlate structure 1930 */ 1931 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1932 bzero(spx, sizeof (sata_pkt_txlate_t)); 1933 1934 spx->txlt_scsi_pkt = pkt; 1935 spx->txlt_sata_hba_inst = sata_hba_inst; 1936 1937 /* Allocate sata_pkt */ 1938 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 1939 if (spx->txlt_sata_pkt == NULL) { 1940 /* Could not allocate sata pkt */ 1941 scsi_hba_pkt_free(ap, pkt); 1942 return (NULL); 1943 } 1944 /* Set sata address */ 1945 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 1946 sata_device.satadev_addr; 1947 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 1948 sata_device.satadev_rev; 1949 1950 if ((bp == NULL) || (bp->b_bcount == 0)) 1951 return (pkt); 1952 1953 spx->txlt_total_residue = bp->b_bcount; 1954 } else { 1955 new_pkt = FALSE; 1956 /* 1957 * Packet was preallocated/initialized by previous call 1958 */ 1959 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1960 1961 if ((bp == NULL) || (bp->b_bcount == 0)) { 1962 return (pkt); 1963 } 1964 1965 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 1966 } 1967 1968 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 1969 1970 /* 1971 * We use an adjusted version of the dma_attr, to account 1972 * for device addressing limitations. 1973 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 1974 * happen when a device is not yet configured. 1975 */ 1976 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1977 sata_device.satadev_addr.cport))); 1978 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 1979 &spx->txlt_sata_pkt->satapkt_device); 1980 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 1981 sata_adjust_dma_attr(sdinfo, 1982 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 1983 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1984 sata_device.satadev_addr.cport))); 1985 /* 1986 * Allocate necessary DMA resources for the packet's data buffer 1987 * NOTE: 1988 * In case of read/write commands, DMA resource allocation here is 1989 * based on the premise that the transfer length specified in 1990 * the read/write scsi cdb will match exactly DMA resources - 1991 * returning correct packet residue is crucial. 1992 */ 1993 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 1994 &cur_dma_attr)) != DDI_SUCCESS) { 1995 /* 1996 * If a DMA allocation request fails with 1997 * DDI_DMA_NOMAPPING, indicate the error by calling 1998 * bioerror(9F) with bp and an error code of EFAULT. 1999 * If a DMA allocation request fails with 2000 * DDI_DMA_TOOBIG, indicate the error by calling 2001 * bioerror(9F) with bp and an error code of EINVAL. 2002 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2003 * Request may be repeated later - there is no real error. 2004 */ 2005 switch (rval) { 2006 case DDI_DMA_NORESOURCES: 2007 bioerror(bp, 0); 2008 break; 2009 case DDI_DMA_NOMAPPING: 2010 case DDI_DMA_BADATTR: 2011 bioerror(bp, EFAULT); 2012 break; 2013 case DDI_DMA_TOOBIG: 2014 default: 2015 bioerror(bp, EINVAL); 2016 break; 2017 } 2018 if (new_pkt == TRUE) { 2019 /* 2020 * Since this is a new packet, we can clean-up 2021 * everything 2022 */ 2023 sata_scsi_destroy_pkt(ap, pkt); 2024 } else { 2025 /* 2026 * This is a re-used packet. It will be target driver's 2027 * responsibility to eventually destroy it (which 2028 * will free allocated resources). 2029 * Here, we just "complete" the request, leaving 2030 * allocated resources intact, so the request may 2031 * be retried. 2032 */ 2033 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2034 sata_pkt_free(spx); 2035 } 2036 return (NULL); 2037 } 2038 /* Set number of bytes that are not yet accounted for */ 2039 pkt->pkt_resid = spx->txlt_total_residue; 2040 ASSERT(pkt->pkt_resid >= 0); 2041 2042 return (pkt); 2043 } 2044 2045 /* 2046 * Implementation of scsi tran_start. 2047 * Translate scsi cmd into sata operation and return status. 2048 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2049 * are supported. 2050 * For SATA hard disks, supported scsi commands: 2051 * SCMD_INQUIRY 2052 * SCMD_TEST_UNIT_READY 2053 * SCMD_START_STOP 2054 * SCMD_READ_CAPACITY 2055 * SCMD_REQUEST_SENSE 2056 * SCMD_LOG_SENSE_G1 2057 * SCMD_LOG_SELECT_G1 2058 * SCMD_MODE_SENSE (specific pages) 2059 * SCMD_MODE_SENSE_G1 (specific pages) 2060 * SCMD_MODE_SELECT (specific pages) 2061 * SCMD_MODE_SELECT_G1 (specific pages) 2062 * SCMD_SYNCHRONIZE_CACHE 2063 * SCMD_SYNCHRONIZE_CACHE_G1 2064 * SCMD_READ 2065 * SCMD_READ_G1 2066 * SCMD_READ_G4 2067 * SCMD_READ_G5 2068 * SCMD_WRITE 2069 * SCMD_WRITE_BUFFER 2070 * SCMD_WRITE_G1 2071 * SCMD_WRITE_G4 2072 * SCMD_WRITE_G5 2073 * SCMD_SEEK (noop) 2074 * SCMD_SDIAG 2075 * 2076 * All other commands are rejected as unsupported. 2077 * 2078 * Returns: 2079 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2080 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2081 * a callback could be scheduled. 2082 * TRAN_BADPKT if cmd was directed to invalid address. 2083 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2084 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2085 * was removed and there was no callback specified in scsi pkt. 2086 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2087 * framework was busy performing some other operation(s). 2088 * 2089 */ 2090 static int 2091 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2092 { 2093 sata_hba_inst_t *sata_hba_inst = 2094 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2095 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2096 sata_drive_info_t *sdinfo; 2097 struct buf *bp; 2098 int cport; 2099 int rval; 2100 2101 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2102 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2103 2104 ASSERT(spx != NULL && 2105 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2106 2107 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2108 2109 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2110 sdinfo = sata_get_device_info(sata_hba_inst, 2111 &spx->txlt_sata_pkt->satapkt_device); 2112 if (sdinfo == NULL || 2113 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean == 2114 B_FALSE || 2115 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2116 2117 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2118 pkt->pkt_reason = CMD_DEV_GONE; 2119 /* 2120 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2121 * only in callback function (for normal requests) and 2122 * in the dump code path. 2123 * So, if the callback is available, we need to do 2124 * the callback rather than returning TRAN_FATAL_ERROR here. 2125 */ 2126 if (pkt->pkt_comp != NULL) { 2127 /* scsi callback required */ 2128 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2129 (task_func_t *)pkt->pkt_comp, 2130 (void *)pkt, TQ_SLEEP) == NULL) 2131 /* Scheduling the callback failed */ 2132 return (TRAN_BUSY); 2133 return (TRAN_ACCEPT); 2134 } 2135 /* No callback available */ 2136 return (TRAN_FATAL_ERROR); 2137 } 2138 2139 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2140 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2141 rval = sata_txlt_atapi(spx); 2142 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2143 "sata_scsi_start atapi: rval %d\n", rval); 2144 return (rval); 2145 } 2146 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2147 2148 /* ATA Disk commands processing starts here */ 2149 2150 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2151 2152 switch (pkt->pkt_cdbp[0]) { 2153 2154 case SCMD_INQUIRY: 2155 /* Mapped to identify device */ 2156 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2157 bp_mapin(bp); 2158 rval = sata_txlt_inquiry(spx); 2159 break; 2160 2161 case SCMD_TEST_UNIT_READY: 2162 /* 2163 * SAT "SATA to ATA Translation" doc specifies translation 2164 * to ATA CHECK POWER MODE. 2165 */ 2166 rval = sata_txlt_test_unit_ready(spx); 2167 break; 2168 2169 case SCMD_START_STOP: 2170 /* Mapping depends on the command */ 2171 rval = sata_txlt_start_stop_unit(spx); 2172 break; 2173 2174 case SCMD_READ_CAPACITY: 2175 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2176 bp_mapin(bp); 2177 rval = sata_txlt_read_capacity(spx); 2178 break; 2179 2180 case SCMD_REQUEST_SENSE: 2181 /* 2182 * Always No Sense, since we force ARQ 2183 */ 2184 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2185 bp_mapin(bp); 2186 rval = sata_txlt_request_sense(spx); 2187 break; 2188 2189 case SCMD_LOG_SENSE_G1: 2190 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2191 bp_mapin(bp); 2192 rval = sata_txlt_log_sense(spx); 2193 break; 2194 2195 case SCMD_LOG_SELECT_G1: 2196 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2197 bp_mapin(bp); 2198 rval = sata_txlt_log_select(spx); 2199 break; 2200 2201 case SCMD_MODE_SENSE: 2202 case SCMD_MODE_SENSE_G1: 2203 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2204 bp_mapin(bp); 2205 rval = sata_txlt_mode_sense(spx); 2206 break; 2207 2208 2209 case SCMD_MODE_SELECT: 2210 case SCMD_MODE_SELECT_G1: 2211 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2212 bp_mapin(bp); 2213 rval = sata_txlt_mode_select(spx); 2214 break; 2215 2216 case SCMD_SYNCHRONIZE_CACHE: 2217 case SCMD_SYNCHRONIZE_CACHE_G1: 2218 rval = sata_txlt_synchronize_cache(spx); 2219 break; 2220 2221 case SCMD_READ: 2222 case SCMD_READ_G1: 2223 case SCMD_READ_G4: 2224 case SCMD_READ_G5: 2225 rval = sata_txlt_read(spx); 2226 break; 2227 case SCMD_WRITE_BUFFER: 2228 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2229 bp_mapin(bp); 2230 rval = sata_txlt_write_buffer(spx); 2231 break; 2232 2233 case SCMD_WRITE: 2234 case SCMD_WRITE_G1: 2235 case SCMD_WRITE_G4: 2236 case SCMD_WRITE_G5: 2237 rval = sata_txlt_write(spx); 2238 break; 2239 2240 case SCMD_SEEK: 2241 rval = sata_txlt_nodata_cmd_immediate(spx); 2242 break; 2243 2244 /* Other cases will be filed later */ 2245 /* postponed until phase 2 of the development */ 2246 default: 2247 rval = sata_txlt_invalid_command(spx); 2248 break; 2249 } 2250 2251 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2252 "sata_scsi_start: rval %d\n", rval); 2253 2254 return (rval); 2255 } 2256 2257 /* 2258 * Implementation of scsi tran_abort. 2259 * Abort specific pkt or all packets. 2260 * 2261 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2262 * 2263 * May be called from an interrupt level. 2264 */ 2265 static int 2266 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2267 { 2268 sata_hba_inst_t *sata_hba_inst = 2269 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2270 sata_device_t sata_device; 2271 sata_pkt_t *sata_pkt; 2272 2273 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2274 "sata_scsi_abort: %s at target: 0x%x\n", 2275 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2276 2277 /* Validate address */ 2278 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2279 /* Invalid address */ 2280 return (0); 2281 2282 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2283 sata_device.satadev_addr.cport))); 2284 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2285 /* invalid address */ 2286 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2287 sata_device.satadev_addr.cport))); 2288 return (0); 2289 } 2290 if (scsi_pkt == NULL) { 2291 /* 2292 * Abort all packets. 2293 * Although we do not have specific packet, we still need 2294 * dummy packet structure to pass device address to HBA. 2295 * Allocate one, without sleeping. Fail if pkt cannot be 2296 * allocated. 2297 */ 2298 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2299 if (sata_pkt == NULL) { 2300 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2301 sata_device.satadev_addr.cport))); 2302 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2303 "could not allocate sata_pkt")); 2304 return (0); 2305 } 2306 sata_pkt->satapkt_rev = SATA_PKT_REV; 2307 sata_pkt->satapkt_device = sata_device; 2308 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2309 } else { 2310 if (scsi_pkt->pkt_ha_private == NULL) { 2311 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2312 sata_device.satadev_addr.cport))); 2313 return (0); /* Bad scsi pkt */ 2314 } 2315 /* extract pointer to sata pkt */ 2316 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2317 txlt_sata_pkt; 2318 } 2319 2320 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2321 sata_device.satadev_addr.cport))); 2322 /* Send abort request to HBA */ 2323 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2324 (SATA_DIP(sata_hba_inst), sata_pkt, 2325 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2326 SATA_SUCCESS) { 2327 if (scsi_pkt == NULL) 2328 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2329 /* Success */ 2330 return (1); 2331 } 2332 /* Else, something did not go right */ 2333 if (scsi_pkt == NULL) 2334 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2335 /* Failure */ 2336 return (0); 2337 } 2338 2339 2340 /* 2341 * Implementation of scsi tran_reset. 2342 * RESET_ALL request is translated into port reset. 2343 * RESET_TARGET requests is translated into a device reset, 2344 * RESET_LUN request is accepted only for LUN 0 and translated into 2345 * device reset. 2346 * The target reset should cause all HBA active and queued packets to 2347 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2348 * the return. HBA should report reset event for the device. 2349 * 2350 * Returns 1 upon success, 0 upon failure. 2351 */ 2352 static int 2353 sata_scsi_reset(struct scsi_address *ap, int level) 2354 { 2355 sata_hba_inst_t *sata_hba_inst = 2356 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2357 sata_device_t sata_device; 2358 int val; 2359 2360 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2361 "sata_scsi_reset: level %d target: 0x%x\n", 2362 level, ap->a_target); 2363 2364 /* Validate address */ 2365 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2366 if (val == -1) 2367 /* Invalid address */ 2368 return (0); 2369 2370 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2371 sata_device.satadev_addr.cport))); 2372 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2373 /* invalid address */ 2374 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2375 sata_device.satadev_addr.cport))); 2376 return (0); 2377 } 2378 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2379 sata_device.satadev_addr.cport))); 2380 if (level == RESET_ALL) { 2381 /* port reset - cport only */ 2382 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2383 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2384 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2385 return (1); 2386 else 2387 return (0); 2388 2389 } else if (val == 0 && 2390 (level == RESET_TARGET || level == RESET_LUN)) { 2391 /* reset device (device attached) */ 2392 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2393 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2394 return (1); 2395 else 2396 return (0); 2397 } 2398 return (0); 2399 } 2400 2401 2402 /* 2403 * Implementation of scsi tran_getcap (get transport/device capabilities). 2404 * Supported capabilities for SATA hard disks: 2405 * auto-rqsense (always supported) 2406 * tagged-qing (supported if HBA supports it) 2407 * untagged-qing (could be supported if disk supports it, but because 2408 * caching behavior allowing untagged queuing actually 2409 * results in reduced performance. sd tries to throttle 2410 * back to only 3 outstanding commands, which may 2411 * work for real SCSI disks, but with read ahead 2412 * caching, having more than 1 outstanding command 2413 * results in cache thrashing.) 2414 * sector_size 2415 * dma_max 2416 * interconnect-type (INTERCONNECT_SATA) 2417 * 2418 * Supported capabilities for ATAPI CD/DVD devices: 2419 * auto-rqsense (always supported) 2420 * sector_size 2421 * dma_max 2422 * max-cdb-length 2423 * interconnect-type (INTERCONNECT_SATA) 2424 * 2425 * Supported capabilities for ATAPI TAPE devices: 2426 * auto-rqsense (always supported) 2427 * dma_max 2428 * max-cdb-length 2429 * 2430 * Request for other capabilities is rejected as unsupported. 2431 * 2432 * Returns supported capability value, or -1 if capability is unsuppported or 2433 * the address is invalid - no device. 2434 */ 2435 2436 static int 2437 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2438 { 2439 2440 sata_hba_inst_t *sata_hba_inst = 2441 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2442 sata_device_t sata_device; 2443 sata_drive_info_t *sdinfo; 2444 ddi_dma_attr_t adj_dma_attr; 2445 int rval; 2446 2447 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2448 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2449 ap->a_target, cap); 2450 2451 /* 2452 * We want to process the capabilities on per port granularity. 2453 * So, we are specifically restricting ourselves to whom != 0 2454 * to exclude the controller wide handling. 2455 */ 2456 if (cap == NULL || whom == 0) 2457 return (-1); 2458 2459 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2460 /* Invalid address */ 2461 return (-1); 2462 } 2463 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2464 sata_device.satadev_addr.cport))); 2465 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2466 NULL) { 2467 /* invalid address */ 2468 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2469 sata_device.satadev_addr.cport))); 2470 return (-1); 2471 } 2472 2473 switch (scsi_hba_lookup_capstr(cap)) { 2474 case SCSI_CAP_ARQ: 2475 rval = 1; /* ARQ supported, turned on */ 2476 break; 2477 2478 case SCSI_CAP_SECTOR_SIZE: 2479 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2480 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2481 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2482 rval = SATA_ATAPI_SECTOR_SIZE; 2483 else rval = -1; 2484 break; 2485 2486 /* 2487 * untagged queuing cause a performance inversion because of 2488 * the way sd operates. Because of this reason we do not 2489 * use it when available. 2490 */ 2491 case SCSI_CAP_UNTAGGED_QING: 2492 if (sdinfo->satadrv_features_enabled & 2493 SATA_DEV_F_E_UNTAGGED_QING) 2494 rval = 1; /* Untagged queuing available */ 2495 else 2496 rval = -1; /* Untagged queuing not available */ 2497 break; 2498 2499 case SCSI_CAP_TAGGED_QING: 2500 if ((sdinfo->satadrv_features_enabled & 2501 SATA_DEV_F_E_TAGGED_QING) && 2502 (sdinfo->satadrv_max_queue_depth > 1)) 2503 rval = 1; /* Tagged queuing available */ 2504 else 2505 rval = -1; /* Tagged queuing not available */ 2506 break; 2507 2508 case SCSI_CAP_DMA_MAX: 2509 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2510 &adj_dma_attr); 2511 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2512 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2513 break; 2514 2515 case SCSI_CAP_INTERCONNECT_TYPE: 2516 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2517 break; 2518 2519 case SCSI_CAP_CDB_LEN: 2520 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2521 rval = sdinfo->satadrv_atapi_cdb_len; 2522 else 2523 rval = -1; 2524 break; 2525 2526 default: 2527 rval = -1; 2528 break; 2529 } 2530 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2531 sata_device.satadev_addr.cport))); 2532 return (rval); 2533 } 2534 2535 /* 2536 * Implementation of scsi tran_setcap 2537 * 2538 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2539 * 2540 */ 2541 static int 2542 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2543 { 2544 sata_hba_inst_t *sata_hba_inst = 2545 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2546 sata_device_t sata_device; 2547 sata_drive_info_t *sdinfo; 2548 int rval; 2549 2550 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2551 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2552 2553 /* 2554 * We want to process the capabilities on per port granularity. 2555 * So, we are specifically restricting ourselves to whom != 0 2556 * to exclude the controller wide handling. 2557 */ 2558 if (cap == NULL || whom == 0) { 2559 return (-1); 2560 } 2561 2562 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2563 /* Invalid address */ 2564 return (-1); 2565 } 2566 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2567 sata_device.satadev_addr.cport))); 2568 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2569 &sata_device)) == NULL) { 2570 /* invalid address */ 2571 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2572 sata_device.satadev_addr.cport))); 2573 return (-1); 2574 } 2575 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2576 sata_device.satadev_addr.cport))); 2577 2578 switch (scsi_hba_lookup_capstr(cap)) { 2579 case SCSI_CAP_ARQ: 2580 case SCSI_CAP_SECTOR_SIZE: 2581 case SCSI_CAP_DMA_MAX: 2582 case SCSI_CAP_INTERCONNECT_TYPE: 2583 rval = 0; 2584 break; 2585 case SCSI_CAP_UNTAGGED_QING: 2586 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2587 rval = 1; 2588 if (value == 1) { 2589 sdinfo->satadrv_features_enabled |= 2590 SATA_DEV_F_E_UNTAGGED_QING; 2591 } else if (value == 0) { 2592 sdinfo->satadrv_features_enabled &= 2593 ~SATA_DEV_F_E_UNTAGGED_QING; 2594 } else { 2595 rval = -1; 2596 } 2597 } else { 2598 rval = 0; 2599 } 2600 break; 2601 case SCSI_CAP_TAGGED_QING: 2602 /* This can TCQ or NCQ */ 2603 if (sata_func_enable & SATA_ENABLE_QUEUING && 2604 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2605 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2606 (sata_func_enable & SATA_ENABLE_NCQ && 2607 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2608 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2609 (sdinfo->satadrv_max_queue_depth > 1)) { 2610 rval = 1; 2611 if (value == 1) { 2612 sdinfo->satadrv_features_enabled |= 2613 SATA_DEV_F_E_TAGGED_QING; 2614 } else if (value == 0) { 2615 sdinfo->satadrv_features_enabled &= 2616 ~SATA_DEV_F_E_TAGGED_QING; 2617 } else { 2618 rval = -1; 2619 } 2620 } else { 2621 rval = 0; 2622 } 2623 break; 2624 default: 2625 rval = -1; 2626 break; 2627 } 2628 return (rval); 2629 } 2630 2631 /* 2632 * Implementations of scsi tran_destroy_pkt. 2633 * Free resources allocated by sata_scsi_init_pkt() 2634 */ 2635 static void 2636 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2637 { 2638 sata_pkt_txlate_t *spx; 2639 2640 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2641 2642 sata_common_free_dma_rsrcs(spx); 2643 2644 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2645 sata_pkt_free(spx); 2646 2647 scsi_hba_pkt_free(ap, pkt); 2648 } 2649 2650 /* 2651 * Implementation of scsi tran_dmafree. 2652 * Free DMA resources allocated by sata_scsi_init_pkt() 2653 */ 2654 2655 static void 2656 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2657 { 2658 #ifndef __lock_lint 2659 _NOTE(ARGUNUSED(ap)) 2660 #endif 2661 sata_pkt_txlate_t *spx; 2662 2663 ASSERT(pkt != NULL); 2664 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2665 2666 sata_common_free_dma_rsrcs(spx); 2667 } 2668 2669 /* 2670 * Implementation of scsi tran_sync_pkt. 2671 * 2672 * The assumption below is that pkt is unique - there is no need to check ap 2673 * 2674 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 2675 * into/from the real buffer. 2676 */ 2677 static void 2678 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2679 { 2680 #ifndef __lock_lint 2681 _NOTE(ARGUNUSED(ap)) 2682 #endif 2683 int rval; 2684 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2685 struct buf *bp; 2686 int direction; 2687 2688 ASSERT(spx != NULL); 2689 if (spx->txlt_buf_dma_handle != NULL) { 2690 direction = spx->txlt_sata_pkt-> 2691 satapkt_cmd.satacmd_flags.sata_data_direction; 2692 if (spx->txlt_sata_pkt != NULL && 2693 direction != SATA_DIR_NODATA_XFER) { 2694 if (spx->txlt_tmp_buf != NULL) { 2695 /* Intermediate DMA buffer used */ 2696 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2697 2698 if (direction & SATA_DIR_WRITE) { 2699 bcopy(bp->b_un.b_addr, 2700 spx->txlt_tmp_buf, bp->b_bcount); 2701 } 2702 } 2703 /* Sync the buffer for device or for CPU */ 2704 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 2705 (direction & SATA_DIR_WRITE) ? 2706 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 2707 ASSERT(rval == DDI_SUCCESS); 2708 if (spx->txlt_tmp_buf != NULL && 2709 !(direction & SATA_DIR_WRITE)) { 2710 /* Intermediate DMA buffer used for read */ 2711 bcopy(spx->txlt_tmp_buf, 2712 bp->b_un.b_addr, bp->b_bcount); 2713 } 2714 2715 } 2716 } 2717 } 2718 2719 2720 2721 /* ******************* SATA - SCSI Translation functions **************** */ 2722 /* 2723 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 2724 * translation. 2725 */ 2726 2727 /* 2728 * Checks if a device exists and can be access and translates common 2729 * scsi_pkt data to sata_pkt data. 2730 * 2731 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 2732 * sata_pkt was set-up. 2733 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 2734 * exist and pkt_comp callback was scheduled. 2735 * Returns other TRAN_XXXXX values when error occured and command should be 2736 * rejected with the returned TRAN_XXXXX value. 2737 * 2738 * This function should be called with port mutex held. 2739 */ 2740 static int 2741 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason) 2742 { 2743 sata_drive_info_t *sdinfo; 2744 sata_device_t sata_device; 2745 const struct sata_cmd_flags sata_initial_cmd_flags = { 2746 SATA_DIR_NODATA_XFER, 2747 /* all other values to 0/FALSE */ 2748 }; 2749 /* 2750 * Pkt_reason has to be set if the pkt_comp callback is invoked, 2751 * and that implies TRAN_ACCEPT return value. Any other returned value 2752 * indicates that the scsi packet was not accepted (the reason will not 2753 * be checked by the scsi target driver). 2754 * To make debugging easier, we set pkt_reason to know value here. 2755 * It may be changed later when different completion reason is 2756 * determined. 2757 */ 2758 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 2759 *reason = CMD_TRAN_ERR; 2760 2761 /* Validate address */ 2762 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 2763 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 2764 2765 case -1: 2766 /* Invalid address or invalid device type */ 2767 return (TRAN_BADPKT); 2768 case 1: 2769 /* valid address but no device - it has disappeared ? */ 2770 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 2771 *reason = CMD_DEV_GONE; 2772 /* 2773 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2774 * only in callback function (for normal requests) and 2775 * in the dump code path. 2776 * So, if the callback is available, we need to do 2777 * the callback rather than returning TRAN_FATAL_ERROR here. 2778 */ 2779 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 2780 /* scsi callback required */ 2781 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2782 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2783 (void *)spx->txlt_scsi_pkt, 2784 TQ_SLEEP) == NULL) 2785 /* Scheduling the callback failed */ 2786 return (TRAN_BUSY); 2787 2788 return (TRAN_ACCEPT); 2789 } 2790 return (TRAN_FATAL_ERROR); 2791 default: 2792 /* all OK; pkt reason will be overwritten later */ 2793 break; 2794 } 2795 /* 2796 * If in an interrupt context, reject packet if it is to be 2797 * executed in polling mode 2798 */ 2799 if (servicing_interrupt() && 2800 (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2801 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 2802 "sata_scsi_start: rejecting synchronous command because " 2803 "of interrupt context\n", NULL); 2804 return (TRAN_BUSY); 2805 } 2806 2807 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2808 &spx->txlt_sata_pkt->satapkt_device); 2809 2810 /* 2811 * If device is in reset condition, reject the packet with 2812 * TRAN_BUSY, unless: 2813 * 1. system is panicking (dumping) 2814 * In such case only one thread is running and there is no way to 2815 * process reset. 2816 * 2. cfgadm operation is is progress (internal APCTL lock is set) 2817 * Some cfgadm operations involve drive commands, so reset condition 2818 * needs to be ignored for IOCTL operations. 2819 */ 2820 if ((sdinfo->satadrv_event_flags & 2821 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 2822 2823 if (!ddi_in_panic() && 2824 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 2825 sata_device.satadev_addr.cport) & 2826 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 2827 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 2828 *reason = CMD_INCOMPLETE; 2829 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2830 "sata_scsi_start: rejecting command because " 2831 "of device reset state\n", NULL); 2832 return (TRAN_BUSY); 2833 } 2834 } 2835 2836 /* 2837 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 2838 * sata_scsi_pkt_init() because pkt init had to work also with 2839 * non-existing devices. 2840 * Now we know that the packet was set-up for a real device, so its 2841 * type is known. 2842 */ 2843 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 2844 2845 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 2846 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 2847 sata_device.satadev_addr.cport)->cport_event_flags & 2848 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 2849 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2850 sata_ignore_dev_reset = B_TRUE; 2851 } 2852 /* 2853 * At this point the generic translation routine determined that the 2854 * scsi packet should be accepted. Packet completion reason may be 2855 * changed later when a different completion reason is determined. 2856 */ 2857 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 2858 *reason = CMD_CMPLT; 2859 2860 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2861 /* Synchronous execution */ 2862 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 2863 SATA_OPMODE_POLLING; 2864 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2865 sata_ignore_dev_reset = ddi_in_panic(); 2866 } else { 2867 /* Asynchronous execution */ 2868 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 2869 SATA_OPMODE_INTERRUPTS; 2870 } 2871 /* Convert queuing information */ 2872 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 2873 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 2874 B_TRUE; 2875 else if (spx->txlt_scsi_pkt->pkt_flags & 2876 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 2877 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 2878 B_TRUE; 2879 2880 /* Always limit pkt time */ 2881 if (spx->txlt_scsi_pkt->pkt_time == 0) 2882 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 2883 else 2884 /* Pass on scsi_pkt time */ 2885 spx->txlt_sata_pkt->satapkt_time = 2886 spx->txlt_scsi_pkt->pkt_time; 2887 2888 return (TRAN_ACCEPT); 2889 } 2890 2891 2892 /* 2893 * Translate ATA Identify Device data to SCSI Inquiry data. 2894 * This function may be called only for ATA devices. 2895 * This function should not be called for ATAPI devices - they 2896 * respond directly to SCSI Inquiry command. 2897 * 2898 * SATA Identify Device data has to be valid in sata_rive_info. 2899 * Buffer has to accomodate the inquiry length (36 bytes). 2900 * 2901 * This function should be called with a port mutex held. 2902 */ 2903 static void 2904 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 2905 sata_drive_info_t *sdinfo, uint8_t *buf) 2906 { 2907 2908 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 2909 struct sata_id *sid = &sdinfo->satadrv_id; 2910 2911 /* Start with a nice clean slate */ 2912 bzero((void *)inq, sizeof (struct scsi_inquiry)); 2913 2914 /* 2915 * Rely on the dev_type for setting paripheral qualifier. 2916 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 2917 * It could be that DTYPE_OPTICAL could also qualify in the future. 2918 * ATAPI Inquiry may provide more data to the target driver. 2919 */ 2920 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 2921 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 2922 2923 inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0; 2924 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 2925 inq->inq_iso = 0; /* ISO version */ 2926 inq->inq_ecma = 0; /* ECMA version */ 2927 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 2928 inq->inq_aenc = 0; /* Async event notification cap. */ 2929 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 2930 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 2931 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 2932 inq->inq_len = 31; /* Additional length */ 2933 inq->inq_dualp = 0; /* dual port device - NO */ 2934 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 2935 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 2936 inq->inq_linked = 0; /* Supports linked commands - NO */ 2937 /* 2938 * Queuing support - controller has to 2939 * support some sort of command queuing. 2940 */ 2941 if (SATA_QDEPTH(sata_hba_inst) > 1) 2942 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 2943 else 2944 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 2945 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 2946 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 2947 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 2948 2949 #ifdef _LITTLE_ENDIAN 2950 /* Swap text fields to match SCSI format */ 2951 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2952 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2953 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2954 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2955 else 2956 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2957 #else /* _LITTLE_ENDIAN */ 2958 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2959 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2960 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2961 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2962 else 2963 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2964 #endif /* _LITTLE_ENDIAN */ 2965 } 2966 2967 2968 /* 2969 * Scsi response set up for invalid command (command not supported) 2970 * 2971 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 2972 */ 2973 static int 2974 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 2975 { 2976 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 2977 struct scsi_extended_sense *sense; 2978 2979 scsipkt->pkt_reason = CMD_CMPLT; 2980 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 2981 STATE_SENT_CMD | STATE_GOT_STATUS; 2982 2983 *scsipkt->pkt_scbp = STATUS_CHECK; 2984 2985 sense = sata_arq_sense(spx); 2986 sense->es_key = KEY_ILLEGAL_REQUEST; 2987 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 2988 2989 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2990 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 2991 2992 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 2993 scsipkt->pkt_comp != NULL) 2994 /* scsi callback required */ 2995 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2996 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2997 (void *)spx->txlt_scsi_pkt, 2998 TQ_SLEEP) == NULL) 2999 /* Scheduling the callback failed */ 3000 return (TRAN_BUSY); 3001 return (TRAN_ACCEPT); 3002 } 3003 3004 /* 3005 * Scsi response setup for 3006 * emulated non-data command that requires no action/return data 3007 * 3008 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3009 */ 3010 static int 3011 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3012 { 3013 int rval; 3014 int reason; 3015 3016 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3017 3018 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3019 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3020 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3021 return (rval); 3022 } 3023 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3024 3025 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3026 STATE_SENT_CMD | STATE_GOT_STATUS; 3027 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3028 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3029 3030 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3031 "Scsi_pkt completion reason %x\n", 3032 spx->txlt_scsi_pkt->pkt_reason); 3033 3034 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3035 spx->txlt_scsi_pkt->pkt_comp != NULL) 3036 /* scsi callback required */ 3037 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3038 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3039 (void *)spx->txlt_scsi_pkt, 3040 TQ_SLEEP) == NULL) 3041 /* Scheduling the callback failed */ 3042 return (TRAN_BUSY); 3043 return (TRAN_ACCEPT); 3044 } 3045 3046 3047 /* 3048 * SATA translate command: Inquiry / Identify Device 3049 * Use cached Identify Device data for now, rather than issuing actual 3050 * Device Identify cmd request. If device is detached and re-attached, 3051 * asynchromous event processing should fetch and refresh Identify Device 3052 * data. 3053 * Two VPD pages are supported now: 3054 * Vital Product Data page 3055 * Unit Serial Number page 3056 * 3057 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3058 */ 3059 3060 #define EVPD 1 /* Extended Vital Product Data flag */ 3061 #define CMDDT 2 /* Command Support Data - Obsolete */ 3062 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3063 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3064 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3065 3066 static int 3067 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3068 { 3069 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3070 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3071 sata_drive_info_t *sdinfo; 3072 struct scsi_extended_sense *sense; 3073 int count; 3074 uint8_t *p; 3075 int i, j; 3076 uint8_t page_buf[0xff]; /* Max length */ 3077 int rval, reason; 3078 3079 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3080 3081 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3082 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3083 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3084 return (rval); 3085 } 3086 3087 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3088 &spx->txlt_sata_pkt->satapkt_device); 3089 3090 ASSERT(sdinfo != NULL); 3091 3092 scsipkt->pkt_reason = CMD_CMPLT; 3093 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3094 STATE_SENT_CMD | STATE_GOT_STATUS; 3095 3096 /* Reject not supported request */ 3097 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3098 *scsipkt->pkt_scbp = STATUS_CHECK; 3099 sense = sata_arq_sense(spx); 3100 sense->es_key = KEY_ILLEGAL_REQUEST; 3101 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3102 goto done; 3103 } 3104 3105 /* Valid Inquiry request */ 3106 *scsipkt->pkt_scbp = STATUS_GOOD; 3107 3108 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3109 3110 /* 3111 * Because it is fully emulated command storing data 3112 * programatically in the specified buffer, release 3113 * preallocated DMA resources before storing data in the buffer, 3114 * so no unwanted DMA sync would take place. 3115 */ 3116 sata_scsi_dmafree(NULL, scsipkt); 3117 3118 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3119 /* Standard Inquiry Data request */ 3120 struct scsi_inquiry inq; 3121 unsigned int bufsize; 3122 3123 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3124 sdinfo, (uint8_t *)&inq); 3125 /* Copy no more than requested */ 3126 count = MIN(bp->b_bcount, 3127 sizeof (struct scsi_inquiry)); 3128 bufsize = scsipkt->pkt_cdbp[4]; 3129 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3130 count = MIN(count, bufsize); 3131 bcopy(&inq, bp->b_un.b_addr, count); 3132 3133 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3134 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3135 bufsize - count : 0; 3136 } else { 3137 /* 3138 * peripheral_qualifier = 0; 3139 * 3140 * We are dealing only with HD and will be 3141 * dealing with CD/DVD devices soon 3142 */ 3143 uint8_t peripheral_device_type = 3144 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3145 DTYPE_DIRECT : DTYPE_RODIRECT; 3146 3147 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3148 case INQUIRY_SUP_VPD_PAGE: 3149 /* 3150 * Request for suported Vital Product Data 3151 * pages - assuming only 2 page codes 3152 * supported. 3153 */ 3154 page_buf[0] = peripheral_device_type; 3155 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3156 page_buf[2] = 0; 3157 page_buf[3] = 2; /* page length */ 3158 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3159 page_buf[5] = INQUIRY_USN_PAGE; 3160 /* Copy no more than requested */ 3161 count = MIN(bp->b_bcount, 6); 3162 bcopy(page_buf, bp->b_un.b_addr, count); 3163 break; 3164 3165 case INQUIRY_USN_PAGE: 3166 /* 3167 * Request for Unit Serial Number page. 3168 * Set-up the page. 3169 */ 3170 page_buf[0] = peripheral_device_type; 3171 page_buf[1] = INQUIRY_USN_PAGE; 3172 page_buf[2] = 0; 3173 /* remaining page length */ 3174 page_buf[3] = SATA_ID_SERIAL_LEN; 3175 3176 /* 3177 * Copy serial number from Identify Device data 3178 * words into the inquiry page and swap bytes 3179 * when necessary. 3180 */ 3181 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3182 #ifdef _LITTLE_ENDIAN 3183 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3184 #else 3185 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3186 #endif 3187 /* 3188 * Least significant character of the serial 3189 * number shall appear as the last byte, 3190 * according to SBC-3 spec. 3191 * Count trailing spaces to determine the 3192 * necessary shift length. 3193 */ 3194 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3195 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3196 if (*(p - j) != '\0' && 3197 *(p - j) != '\040') 3198 break; 3199 } 3200 3201 /* 3202 * Shift SN string right, so that the last 3203 * non-blank character would appear in last 3204 * byte of SN field in the page. 3205 * 'j' is the shift length. 3206 */ 3207 for (i = 0; 3208 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3209 i++, p--) 3210 *p = *(p - j); 3211 3212 /* 3213 * Add leading spaces - same number as the 3214 * shift size 3215 */ 3216 for (; j > 0; j--) 3217 page_buf[4 + j - 1] = '\040'; 3218 3219 count = MIN(bp->b_bcount, 3220 SATA_ID_SERIAL_LEN + 4); 3221 bcopy(page_buf, bp->b_un.b_addr, count); 3222 break; 3223 3224 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3225 /* 3226 * We may want to implement this page, when 3227 * identifiers are common for SATA devices 3228 * But not now. 3229 */ 3230 /*FALLTHROUGH*/ 3231 3232 default: 3233 /* Request for unsupported VPD page */ 3234 *scsipkt->pkt_scbp = STATUS_CHECK; 3235 sense = sata_arq_sense(spx); 3236 sense->es_key = KEY_ILLEGAL_REQUEST; 3237 sense->es_add_code = 3238 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3239 goto done; 3240 } 3241 } 3242 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3243 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3244 scsipkt->pkt_cdbp[4] - count : 0; 3245 } 3246 done: 3247 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3248 3249 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3250 "Scsi_pkt completion reason %x\n", 3251 scsipkt->pkt_reason); 3252 3253 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3254 scsipkt->pkt_comp != NULL) { 3255 /* scsi callback required */ 3256 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3257 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3258 TQ_SLEEP) == NULL) 3259 /* Scheduling the callback failed */ 3260 return (TRAN_BUSY); 3261 } 3262 return (TRAN_ACCEPT); 3263 } 3264 3265 /* 3266 * SATA translate command: Request Sense. 3267 * Emulated command (ATA version for SATA hard disks) 3268 * Always NO SENSE, because any sense data should be reported by ARQ sense. 3269 * 3270 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3271 * 3272 * Note: There is a mismatch between already implemented Informational 3273 * Exception Mode Select page 0x1C and this function. 3274 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3275 * NO SENSE and set additional sense code to the exception code - this is not 3276 * implemented here. 3277 */ 3278 static int 3279 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3280 { 3281 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3282 struct scsi_extended_sense sense; 3283 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3284 int rval, reason; 3285 3286 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3287 3288 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3289 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3290 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3291 return (rval); 3292 } 3293 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3294 3295 3296 scsipkt->pkt_reason = CMD_CMPLT; 3297 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3298 STATE_SENT_CMD | STATE_GOT_STATUS; 3299 *scsipkt->pkt_scbp = STATUS_GOOD; 3300 3301 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3302 /* 3303 * Because it is fully emulated command storing data 3304 * programatically in the specified buffer, release 3305 * preallocated DMA resources before storing data in the buffer, 3306 * so no unwanted DMA sync would take place. 3307 */ 3308 int count = MIN(bp->b_bcount, 3309 sizeof (struct scsi_extended_sense)); 3310 sata_scsi_dmafree(NULL, scsipkt); 3311 bzero(&sense, sizeof (struct scsi_extended_sense)); 3312 sense.es_valid = 0; /* Valid LBA */ 3313 sense.es_class = 7; /* Response code 0x70 - current err */ 3314 sense.es_key = KEY_NO_SENSE; 3315 sense.es_add_len = 6; /* Additional length */ 3316 /* Copy no more than requested */ 3317 bcopy(&sense, bp->b_un.b_addr, count); 3318 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3319 scsipkt->pkt_resid = 0; 3320 } 3321 3322 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3323 "Scsi_pkt completion reason %x\n", 3324 scsipkt->pkt_reason); 3325 3326 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3327 scsipkt->pkt_comp != NULL) 3328 /* scsi callback required */ 3329 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3330 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3331 TQ_SLEEP) == NULL) 3332 /* Scheduling the callback failed */ 3333 return (TRAN_BUSY); 3334 return (TRAN_ACCEPT); 3335 } 3336 3337 /* 3338 * SATA translate command: Test Unit Ready 3339 * At the moment this is an emulated command (ATA version for SATA hard disks). 3340 * May be translated into Check Power Mode command in the future 3341 * 3342 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3343 */ 3344 static int 3345 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3346 { 3347 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3348 struct scsi_extended_sense *sense; 3349 int power_state; 3350 int rval, reason; 3351 3352 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3353 3354 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3355 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3356 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3357 return (rval); 3358 } 3359 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3360 3361 /* At this moment, emulate it rather than execute anything */ 3362 power_state = SATA_PWRMODE_ACTIVE; 3363 3364 scsipkt->pkt_reason = CMD_CMPLT; 3365 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3366 STATE_SENT_CMD | STATE_GOT_STATUS; 3367 3368 switch (power_state) { 3369 case SATA_PWRMODE_ACTIVE: 3370 case SATA_PWRMODE_IDLE: 3371 *scsipkt->pkt_scbp = STATUS_GOOD; 3372 break; 3373 default: 3374 /* PWR mode standby */ 3375 *scsipkt->pkt_scbp = STATUS_CHECK; 3376 sense = sata_arq_sense(spx); 3377 sense->es_key = KEY_NOT_READY; 3378 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3379 break; 3380 } 3381 3382 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3383 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3384 3385 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3386 scsipkt->pkt_comp != NULL) 3387 /* scsi callback required */ 3388 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3389 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3390 TQ_SLEEP) == NULL) 3391 /* Scheduling the callback failed */ 3392 return (TRAN_BUSY); 3393 3394 return (TRAN_ACCEPT); 3395 } 3396 3397 3398 /* 3399 * SATA translate command: Start Stop Unit 3400 * Translation depends on a command: 3401 * Start Unit translated into Idle Immediate 3402 * Stop Unit translated into Standby Immediate 3403 * Unload Media / NOT SUPPORTED YET 3404 * Load Media / NOT SUPPROTED YET 3405 * Power condition bits are ignored, so is Immediate bit 3406 * Requesting synchronous execution. 3407 * 3408 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3409 * appropriate values in scsi_pkt fields. 3410 */ 3411 static int 3412 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3413 { 3414 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3415 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3416 struct scsi_extended_sense *sense; 3417 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3418 int cport = SATA_TXLT_CPORT(spx); 3419 int rval, reason; 3420 int synch; 3421 3422 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3423 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3424 3425 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3426 3427 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3428 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3429 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3430 return (rval); 3431 } 3432 3433 if (scsipkt->pkt_cdbp[4] & 2) { 3434 /* Load/Unload Media - invalid request */ 3435 *scsipkt->pkt_scbp = STATUS_CHECK; 3436 sense = sata_arq_sense(spx); 3437 sense->es_key = KEY_ILLEGAL_REQUEST; 3438 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3439 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3440 3441 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3442 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3443 3444 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3445 scsipkt->pkt_comp != NULL) 3446 /* scsi callback required */ 3447 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3448 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3449 TQ_SLEEP) == NULL) 3450 /* Scheduling the callback failed */ 3451 return (TRAN_BUSY); 3452 3453 return (TRAN_ACCEPT); 3454 } 3455 scmd->satacmd_addr_type = 0; 3456 scmd->satacmd_sec_count_lsb = 0; 3457 scmd->satacmd_lba_low_lsb = 0; 3458 scmd->satacmd_lba_mid_lsb = 0; 3459 scmd->satacmd_lba_high_lsb = 0; 3460 scmd->satacmd_features_reg = 0; 3461 scmd->satacmd_device_reg = 0; 3462 scmd->satacmd_status_reg = 0; 3463 if (scsipkt->pkt_cdbp[4] & 1) { 3464 /* Start Unit */ 3465 scmd->satacmd_cmd_reg = SATAC_IDLE_IM; 3466 } else { 3467 /* Stop Unit */ 3468 scmd->satacmd_cmd_reg = SATAC_STANDBY_IM; 3469 } 3470 3471 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 3472 /* Need to set-up a callback function */ 3473 spx->txlt_sata_pkt->satapkt_comp = 3474 sata_txlt_nodata_cmd_completion; 3475 synch = FALSE; 3476 } else { 3477 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3478 synch = TRUE; 3479 } 3480 3481 /* Transfer command to HBA */ 3482 if (sata_hba_start(spx, &rval) != 0) { 3483 /* Pkt not accepted for execution */ 3484 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3485 return (rval); 3486 } 3487 3488 /* 3489 * If execution is non-synchronous, 3490 * a callback function will handle potential errors, translate 3491 * the response and will do a callback to a target driver. 3492 * If it was synchronous, check execution status using the same 3493 * framework callback. 3494 */ 3495 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3496 if (synch) { 3497 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3498 "synchronous execution status %x\n", 3499 spx->txlt_sata_pkt->satapkt_reason); 3500 3501 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 3502 } 3503 return (TRAN_ACCEPT); 3504 3505 } 3506 3507 3508 /* 3509 * SATA translate command: Read Capacity. 3510 * Emulated command for SATA disks. 3511 * Capacity is retrieved from cached Idenifty Device data. 3512 * Identify Device data shows effective disk capacity, not the native 3513 * capacity, which may be limitted by Set Max Address command. 3514 * This is ATA version for SATA hard disks. 3515 * 3516 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3517 */ 3518 static int 3519 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 3520 { 3521 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3522 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3523 sata_drive_info_t *sdinfo; 3524 uint64_t val; 3525 uchar_t *rbuf; 3526 int rval, reason; 3527 3528 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3529 "sata_txlt_read_capacity: ", NULL); 3530 3531 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3532 3533 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3534 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3535 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3536 return (rval); 3537 } 3538 3539 scsipkt->pkt_reason = CMD_CMPLT; 3540 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3541 STATE_SENT_CMD | STATE_GOT_STATUS; 3542 *scsipkt->pkt_scbp = STATUS_GOOD; 3543 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3544 /* 3545 * Because it is fully emulated command storing data 3546 * programatically in the specified buffer, release 3547 * preallocated DMA resources before storing data in the buffer, 3548 * so no unwanted DMA sync would take place. 3549 */ 3550 sata_scsi_dmafree(NULL, scsipkt); 3551 3552 sdinfo = sata_get_device_info( 3553 spx->txlt_sata_hba_inst, 3554 &spx->txlt_sata_pkt->satapkt_device); 3555 /* Last logical block address */ 3556 val = sdinfo->satadrv_capacity - 1; 3557 rbuf = (uchar_t *)bp->b_un.b_addr; 3558 /* Need to swap endians to match scsi format */ 3559 rbuf[0] = (val >> 24) & 0xff; 3560 rbuf[1] = (val >> 16) & 0xff; 3561 rbuf[2] = (val >> 8) & 0xff; 3562 rbuf[3] = val & 0xff; 3563 /* block size - always 512 bytes, for now */ 3564 rbuf[4] = 0; 3565 rbuf[5] = 0; 3566 rbuf[6] = 0x02; 3567 rbuf[7] = 0; 3568 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3569 scsipkt->pkt_resid = 0; 3570 3571 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 3572 sdinfo->satadrv_capacity -1); 3573 } 3574 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3575 /* 3576 * If a callback was requested, do it now. 3577 */ 3578 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3579 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3580 3581 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3582 scsipkt->pkt_comp != NULL) 3583 /* scsi callback required */ 3584 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3585 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3586 TQ_SLEEP) == NULL) 3587 /* Scheduling the callback failed */ 3588 return (TRAN_BUSY); 3589 3590 return (TRAN_ACCEPT); 3591 } 3592 3593 /* 3594 * SATA translate command: Mode Sense. 3595 * Translated into appropriate SATA command or emulated. 3596 * Saved Values Page Control (03) are not supported. 3597 * 3598 * NOTE: only caching mode sense page is currently implemented. 3599 * 3600 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3601 */ 3602 3603 static int 3604 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 3605 { 3606 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3607 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3608 sata_drive_info_t *sdinfo; 3609 sata_id_t *sata_id; 3610 struct scsi_extended_sense *sense; 3611 int len, bdlen, count, alc_len; 3612 int pc; /* Page Control code */ 3613 uint8_t *buf; /* mode sense buffer */ 3614 int rval, reason; 3615 3616 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3617 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 3618 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3619 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3620 3621 buf = kmem_zalloc(1024, KM_SLEEP); 3622 3623 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3624 3625 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3626 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3627 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3628 kmem_free(buf, 1024); 3629 return (rval); 3630 } 3631 3632 scsipkt->pkt_reason = CMD_CMPLT; 3633 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3634 STATE_SENT_CMD | STATE_GOT_STATUS; 3635 3636 pc = scsipkt->pkt_cdbp[2] >> 6; 3637 3638 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3639 /* 3640 * Because it is fully emulated command storing data 3641 * programatically in the specified buffer, release 3642 * preallocated DMA resources before storing data in the buffer, 3643 * so no unwanted DMA sync would take place. 3644 */ 3645 sata_scsi_dmafree(NULL, scsipkt); 3646 3647 len = 0; 3648 bdlen = 0; 3649 if (!(scsipkt->pkt_cdbp[1] & 8)) { 3650 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 3651 (scsipkt->pkt_cdbp[0] & 0x10)) 3652 bdlen = 16; 3653 else 3654 bdlen = 8; 3655 } 3656 /* Build mode parameter header */ 3657 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3658 /* 4-byte mode parameter header */ 3659 buf[len++] = 0; /* mode data length */ 3660 buf[len++] = 0; /* medium type */ 3661 buf[len++] = 0; /* dev-specific param */ 3662 buf[len++] = bdlen; /* Block Descriptor length */ 3663 } else { 3664 /* 8-byte mode parameter header */ 3665 buf[len++] = 0; /* mode data length */ 3666 buf[len++] = 0; 3667 buf[len++] = 0; /* medium type */ 3668 buf[len++] = 0; /* dev-specific param */ 3669 if (bdlen == 16) 3670 buf[len++] = 1; /* long lba descriptor */ 3671 else 3672 buf[len++] = 0; 3673 buf[len++] = 0; 3674 buf[len++] = 0; /* Block Descriptor length */ 3675 buf[len++] = bdlen; 3676 } 3677 3678 sdinfo = sata_get_device_info( 3679 spx->txlt_sata_hba_inst, 3680 &spx->txlt_sata_pkt->satapkt_device); 3681 3682 /* Build block descriptor only if not disabled (DBD) */ 3683 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 3684 /* Block descriptor - direct-access device format */ 3685 if (bdlen == 8) { 3686 /* build regular block descriptor */ 3687 buf[len++] = 3688 (sdinfo->satadrv_capacity >> 24) & 0xff; 3689 buf[len++] = 3690 (sdinfo->satadrv_capacity >> 16) & 0xff; 3691 buf[len++] = 3692 (sdinfo->satadrv_capacity >> 8) & 0xff; 3693 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3694 buf[len++] = 0; /* density code */ 3695 buf[len++] = 0; 3696 if (sdinfo->satadrv_type == 3697 SATA_DTYPE_ATADISK) 3698 buf[len++] = 2; 3699 else 3700 /* ATAPI */ 3701 buf[len++] = 8; 3702 buf[len++] = 0; 3703 } else if (bdlen == 16) { 3704 /* Long LBA Accepted */ 3705 /* build long lba block descriptor */ 3706 #ifndef __lock_lint 3707 buf[len++] = 3708 (sdinfo->satadrv_capacity >> 56) & 0xff; 3709 buf[len++] = 3710 (sdinfo->satadrv_capacity >> 48) & 0xff; 3711 buf[len++] = 3712 (sdinfo->satadrv_capacity >> 40) & 0xff; 3713 buf[len++] = 3714 (sdinfo->satadrv_capacity >> 32) & 0xff; 3715 #endif 3716 buf[len++] = 3717 (sdinfo->satadrv_capacity >> 24) & 0xff; 3718 buf[len++] = 3719 (sdinfo->satadrv_capacity >> 16) & 0xff; 3720 buf[len++] = 3721 (sdinfo->satadrv_capacity >> 8) & 0xff; 3722 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3723 buf[len++] = 0; 3724 buf[len++] = 0; /* density code */ 3725 buf[len++] = 0; 3726 buf[len++] = 0; 3727 if (sdinfo->satadrv_type == 3728 SATA_DTYPE_ATADISK) 3729 buf[len++] = 2; 3730 else 3731 /* ATAPI */ 3732 buf[len++] = 8; 3733 buf[len++] = 0; 3734 } 3735 } 3736 3737 sata_id = &sdinfo->satadrv_id; 3738 3739 /* 3740 * Add requested pages. 3741 * Page 3 and 4 are obsolete and we are not supporting them. 3742 * We deal now with: 3743 * caching (read/write cache control). 3744 * We should eventually deal with following mode pages: 3745 * error recovery (0x01), 3746 * power condition (0x1a), 3747 * exception control page (enables SMART) (0x1c), 3748 * enclosure management (ses), 3749 * protocol-specific port mode (port control). 3750 */ 3751 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 3752 case MODEPAGE_RW_ERRRECOV: 3753 /* DAD_MODE_ERR_RECOV */ 3754 /* R/W recovery */ 3755 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3756 break; 3757 case MODEPAGE_CACHING: 3758 /* DAD_MODE_CACHE */ 3759 /* Reject not supported request for saved parameters */ 3760 if (pc == 3) { 3761 *scsipkt->pkt_scbp = STATUS_CHECK; 3762 sense = sata_arq_sense(spx); 3763 sense->es_key = KEY_ILLEGAL_REQUEST; 3764 sense->es_add_code = 3765 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 3766 goto done; 3767 } 3768 3769 /* caching */ 3770 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3771 break; 3772 case MODEPAGE_INFO_EXCPT: 3773 /* exception cntrl */ 3774 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3775 len += sata_build_msense_page_1c(sdinfo, pc, 3776 buf+len); 3777 } 3778 else 3779 goto err; 3780 break; 3781 case MODEPAGE_POWER_COND: 3782 /* DAD_MODE_POWER_COND */ 3783 /* power condition */ 3784 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3785 break; 3786 3787 case MODEPAGE_ACOUSTIC_MANAG: 3788 /* acoustic management */ 3789 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3790 break; 3791 case MODEPAGE_ALLPAGES: 3792 /* all pages */ 3793 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3794 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3795 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3796 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3797 len += sata_build_msense_page_1c(sdinfo, pc, 3798 buf+len); 3799 } 3800 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3801 break; 3802 default: 3803 err: 3804 /* Invalid request */ 3805 *scsipkt->pkt_scbp = STATUS_CHECK; 3806 sense = sata_arq_sense(spx); 3807 sense->es_key = KEY_ILLEGAL_REQUEST; 3808 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3809 goto done; 3810 } 3811 3812 /* fix total mode data length */ 3813 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3814 /* 4-byte mode parameter header */ 3815 buf[0] = len - 1; /* mode data length */ 3816 } else { 3817 buf[0] = (len -2) >> 8; 3818 buf[1] = (len -2) & 0xff; 3819 } 3820 3821 3822 /* Check allocation length */ 3823 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3824 alc_len = scsipkt->pkt_cdbp[4]; 3825 } else { 3826 alc_len = scsipkt->pkt_cdbp[7]; 3827 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 3828 } 3829 /* 3830 * We do not check for possible parameters truncation 3831 * (alc_len < len) assuming that the target driver works 3832 * correctly. Just avoiding overrun. 3833 * Copy no more than requested and possible, buffer-wise. 3834 */ 3835 count = MIN(alc_len, len); 3836 count = MIN(bp->b_bcount, count); 3837 bcopy(buf, bp->b_un.b_addr, count); 3838 3839 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3840 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 3841 } 3842 *scsipkt->pkt_scbp = STATUS_GOOD; 3843 done: 3844 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3845 (void) kmem_free(buf, 1024); 3846 3847 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3848 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3849 3850 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3851 scsipkt->pkt_comp != NULL) 3852 /* scsi callback required */ 3853 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3854 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3855 TQ_SLEEP) == NULL) 3856 /* Scheduling the callback failed */ 3857 return (TRAN_BUSY); 3858 3859 return (TRAN_ACCEPT); 3860 } 3861 3862 3863 /* 3864 * SATA translate command: Mode Select. 3865 * Translated into appropriate SATA command or emulated. 3866 * Saving parameters is not supported. 3867 * Changing device capacity is not supported (although theoretically 3868 * possible by executing SET FEATURES/SET MAX ADDRESS) 3869 * 3870 * Assumption is that the target driver is working correctly. 3871 * 3872 * More than one SATA command may be executed to perform operations specified 3873 * by mode select pages. The first error terminates further execution. 3874 * Operations performed successully are not backed-up in such case. 3875 * 3876 * NOTE: Implemented pages: 3877 * - caching page 3878 * - informational exception page 3879 * - acoustic management page 3880 * Caching setup is remembered so it could be re-stored in case of 3881 * an unexpected device reset. 3882 * 3883 * Returns TRAN_XXXX. 3884 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 3885 */ 3886 3887 static int 3888 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 3889 { 3890 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3891 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3892 struct scsi_extended_sense *sense; 3893 int len, pagelen, count, pllen; 3894 uint8_t *buf; /* mode select buffer */ 3895 int rval, stat, reason; 3896 uint_t nointr_flag; 3897 int dmod = 0; 3898 3899 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3900 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 3901 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3902 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3903 3904 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3905 3906 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3907 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3908 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3909 return (rval); 3910 } 3911 /* 3912 * If in interrupt context, reject this packet because it may result 3913 * in issuing a synchronous command to HBA. 3914 */ 3915 if (servicing_interrupt()) { 3916 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3917 "sata_txlt_mode_select: rejecting command because " 3918 "of interrupt context\n", NULL); 3919 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3920 return (TRAN_BUSY); 3921 } 3922 3923 rval = TRAN_ACCEPT; 3924 3925 scsipkt->pkt_reason = CMD_CMPLT; 3926 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3927 STATE_SENT_CMD | STATE_GOT_STATUS; 3928 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 3929 3930 /* Reject not supported request */ 3931 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 3932 *scsipkt->pkt_scbp = STATUS_CHECK; 3933 sense = sata_arq_sense(spx); 3934 sense->es_key = KEY_ILLEGAL_REQUEST; 3935 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3936 goto done; 3937 } 3938 3939 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3940 pllen = scsipkt->pkt_cdbp[4]; 3941 } else { 3942 pllen = scsipkt->pkt_cdbp[7]; 3943 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 3944 } 3945 3946 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 3947 3948 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 3949 buf = (uint8_t *)bp->b_un.b_addr; 3950 count = MIN(bp->b_bcount, pllen); 3951 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3952 scsipkt->pkt_resid = 0; 3953 pllen = count; 3954 3955 /* 3956 * Check the header to skip the block descriptor(s) - we 3957 * do not support setting device capacity. 3958 * Existing macros do not recognize long LBA dscriptor, 3959 * hence manual calculation. 3960 */ 3961 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3962 /* 6-bytes CMD, 4 bytes header */ 3963 if (count <= 4) 3964 goto done; /* header only */ 3965 len = buf[3] + 4; 3966 } else { 3967 /* 10-bytes CMD, 8 bytes header */ 3968 if (count <= 8) 3969 goto done; /* header only */ 3970 len = buf[6]; 3971 len = (len << 8) + buf[7] + 8; 3972 } 3973 if (len >= count) 3974 goto done; /* header + descriptor(s) only */ 3975 3976 pllen -= len; /* remaining data length */ 3977 3978 /* 3979 * We may be executing SATA command and want to execute it 3980 * in SYNCH mode, regardless of scsi_pkt setting. 3981 * Save scsi_pkt setting and indicate SYNCH mode 3982 */ 3983 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3984 scsipkt->pkt_comp != NULL) { 3985 scsipkt->pkt_flags |= FLAG_NOINTR; 3986 } 3987 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3988 3989 /* 3990 * len is now the offset to a first mode select page 3991 * Process all pages 3992 */ 3993 while (pllen > 0) { 3994 switch ((int)buf[len]) { 3995 case MODEPAGE_CACHING: 3996 /* No support for SP (saving) */ 3997 if (scsipkt->pkt_cdbp[1] & 0x01) { 3998 *scsipkt->pkt_scbp = STATUS_CHECK; 3999 sense = sata_arq_sense(spx); 4000 sense->es_key = KEY_ILLEGAL_REQUEST; 4001 sense->es_add_code = 4002 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4003 goto done; 4004 } 4005 stat = sata_mode_select_page_8(spx, 4006 (struct mode_cache_scsi3 *)&buf[len], 4007 pllen, &pagelen, &rval, &dmod); 4008 /* 4009 * The pagelen value indicates the number of 4010 * parameter bytes already processed. 4011 * The rval is the return value from 4012 * sata_tran_start(). 4013 * The stat indicates the overall status of 4014 * the operation(s). 4015 */ 4016 if (stat != SATA_SUCCESS) 4017 /* 4018 * Page processing did not succeed - 4019 * all error info is already set-up, 4020 * just return 4021 */ 4022 pllen = 0; /* this breaks the loop */ 4023 else { 4024 len += pagelen; 4025 pllen -= pagelen; 4026 } 4027 break; 4028 4029 case MODEPAGE_INFO_EXCPT: 4030 stat = sata_mode_select_page_1c(spx, 4031 (struct mode_info_excpt_page *)&buf[len], 4032 pllen, &pagelen, &rval, &dmod); 4033 /* 4034 * The pagelen value indicates the number of 4035 * parameter bytes already processed. 4036 * The rval is the return value from 4037 * sata_tran_start(). 4038 * The stat indicates the overall status of 4039 * the operation(s). 4040 */ 4041 if (stat != SATA_SUCCESS) 4042 /* 4043 * Page processing did not succeed - 4044 * all error info is already set-up, 4045 * just return 4046 */ 4047 pllen = 0; /* this breaks the loop */ 4048 else { 4049 len += pagelen; 4050 pllen -= pagelen; 4051 } 4052 break; 4053 4054 case MODEPAGE_ACOUSTIC_MANAG: 4055 stat = sata_mode_select_page_30(spx, 4056 (struct mode_acoustic_management *) 4057 &buf[len], pllen, &pagelen, &rval, &dmod); 4058 /* 4059 * The pagelen value indicates the number of 4060 * parameter bytes already processed. 4061 * The rval is the return value from 4062 * sata_tran_start(). 4063 * The stat indicates the overall status of 4064 * the operation(s). 4065 */ 4066 if (stat != SATA_SUCCESS) 4067 /* 4068 * Page processing did not succeed - 4069 * all error info is already set-up, 4070 * just return 4071 */ 4072 pllen = 0; /* this breaks the loop */ 4073 else { 4074 len += pagelen; 4075 pllen -= pagelen; 4076 } 4077 4078 break; 4079 default: 4080 *scsipkt->pkt_scbp = STATUS_CHECK; 4081 sense = sata_arq_sense(spx); 4082 sense->es_key = KEY_ILLEGAL_REQUEST; 4083 sense->es_add_code = 4084 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4085 goto done; 4086 } 4087 } 4088 } 4089 done: 4090 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4091 /* 4092 * If device parameters were modified, fetch and store the new 4093 * Identify Device data. Since port mutex could have been released 4094 * for accessing HBA driver, we need to re-check device existence. 4095 */ 4096 if (dmod != 0) { 4097 sata_drive_info_t new_sdinfo, *sdinfo; 4098 int rv = 0; 4099 4100 /* 4101 * Following statement has to be changed if this function is 4102 * used for devices other than SATA hard disks. 4103 */ 4104 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4105 4106 new_sdinfo.satadrv_addr = 4107 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4108 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4109 &new_sdinfo); 4110 4111 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4112 /* 4113 * Since port mutex could have been released when 4114 * accessing HBA driver, we need to re-check that the 4115 * framework still holds the device info structure. 4116 */ 4117 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4118 &spx->txlt_sata_pkt->satapkt_device); 4119 if (sdinfo != NULL) { 4120 /* 4121 * Device still has info structure in the 4122 * sata framework. Copy newly fetched info 4123 */ 4124 if (rv == 0) { 4125 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4126 sata_save_drive_settings(sdinfo); 4127 } else { 4128 /* 4129 * Could not fetch new data - invalidate 4130 * sata_drive_info. That makes device 4131 * unusable. 4132 */ 4133 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4134 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4135 } 4136 } 4137 if (rv != 0 || sdinfo == NULL) { 4138 /* 4139 * This changes the overall mode select completion 4140 * reason to a failed one !!!!! 4141 */ 4142 *scsipkt->pkt_scbp = STATUS_CHECK; 4143 sense = sata_arq_sense(spx); 4144 scsipkt->pkt_reason = CMD_INCOMPLETE; 4145 rval = TRAN_ACCEPT; 4146 } 4147 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4148 } 4149 /* Restore the scsi pkt flags */ 4150 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4151 scsipkt->pkt_flags |= nointr_flag; 4152 4153 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4154 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4155 4156 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4157 scsipkt->pkt_comp != NULL) 4158 /* scsi callback required */ 4159 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4160 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4161 TQ_SLEEP) == NULL) 4162 /* Scheduling the callback failed */ 4163 return (TRAN_BUSY); 4164 4165 return (rval); 4166 } 4167 4168 4169 4170 /* 4171 * Translate command: Log Sense 4172 */ 4173 static int 4174 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4175 { 4176 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4177 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4178 sata_drive_info_t *sdinfo; 4179 struct scsi_extended_sense *sense; 4180 int len, count, alc_len; 4181 int pc; /* Page Control code */ 4182 int page_code; /* Page code */ 4183 uint8_t *buf; /* log sense buffer */ 4184 int rval, reason; 4185 #define MAX_LOG_SENSE_PAGE_SIZE 512 4186 4187 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4188 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4189 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4190 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4191 4192 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4193 4194 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4195 4196 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4197 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4198 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4199 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4200 return (rval); 4201 } 4202 /* 4203 * If in interrupt context, reject this packet because it may result 4204 * in issuing a synchronous command to HBA. 4205 */ 4206 if (servicing_interrupt()) { 4207 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4208 "sata_log_sense: rejecting command because " 4209 "of interrupt context\n", NULL); 4210 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4211 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4212 return (TRAN_BUSY); 4213 } 4214 4215 scsipkt->pkt_reason = CMD_CMPLT; 4216 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4217 STATE_SENT_CMD | STATE_GOT_STATUS; 4218 4219 pc = scsipkt->pkt_cdbp[2] >> 6; 4220 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4221 4222 /* Reject not supported request for all but cumulative values */ 4223 switch (pc) { 4224 case PC_CUMULATIVE_VALUES: 4225 break; 4226 default: 4227 *scsipkt->pkt_scbp = STATUS_CHECK; 4228 sense = sata_arq_sense(spx); 4229 sense->es_key = KEY_ILLEGAL_REQUEST; 4230 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4231 goto done; 4232 } 4233 4234 switch (page_code) { 4235 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4236 case PAGE_CODE_SELF_TEST_RESULTS: 4237 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4238 case PAGE_CODE_SMART_READ_DATA: 4239 break; 4240 default: 4241 *scsipkt->pkt_scbp = STATUS_CHECK; 4242 sense = sata_arq_sense(spx); 4243 sense->es_key = KEY_ILLEGAL_REQUEST; 4244 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4245 goto done; 4246 } 4247 4248 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4249 /* 4250 * Because log sense uses local buffers for data retrieval from 4251 * the devices and sets the data programatically in the 4252 * original specified buffer, release preallocated DMA 4253 * resources before storing data in the original buffer, 4254 * so no unwanted DMA sync would take place. 4255 */ 4256 sata_id_t *sata_id; 4257 4258 sata_scsi_dmafree(NULL, scsipkt); 4259 4260 len = 0; 4261 4262 /* Build log parameter header */ 4263 buf[len++] = page_code; /* page code as in the CDB */ 4264 buf[len++] = 0; /* reserved */ 4265 buf[len++] = 0; /* Zero out page length for now (MSB) */ 4266 buf[len++] = 0; /* (LSB) */ 4267 4268 sdinfo = sata_get_device_info( 4269 spx->txlt_sata_hba_inst, 4270 &spx->txlt_sata_pkt->satapkt_device); 4271 4272 /* 4273 * Add requested pages. 4274 */ 4275 switch (page_code) { 4276 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4277 len = sata_build_lsense_page_0(sdinfo, buf + len); 4278 break; 4279 case PAGE_CODE_SELF_TEST_RESULTS: 4280 sata_id = &sdinfo->satadrv_id; 4281 if ((! (sata_id->ai_cmdset84 & 4282 SATA_SMART_SELF_TEST_SUPPORTED)) || 4283 (! (sata_id->ai_features87 & 4284 SATA_SMART_SELF_TEST_SUPPORTED))) { 4285 *scsipkt->pkt_scbp = STATUS_CHECK; 4286 sense = sata_arq_sense(spx); 4287 sense->es_key = KEY_ILLEGAL_REQUEST; 4288 sense->es_add_code = 4289 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4290 4291 goto done; 4292 } 4293 len = sata_build_lsense_page_10(sdinfo, buf + len, 4294 spx->txlt_sata_hba_inst); 4295 break; 4296 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4297 sata_id = &sdinfo->satadrv_id; 4298 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4299 *scsipkt->pkt_scbp = STATUS_CHECK; 4300 sense = sata_arq_sense(spx); 4301 sense->es_key = KEY_ILLEGAL_REQUEST; 4302 sense->es_add_code = 4303 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4304 4305 goto done; 4306 } 4307 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4308 *scsipkt->pkt_scbp = STATUS_CHECK; 4309 sense = sata_arq_sense(spx); 4310 sense->es_key = KEY_ABORTED_COMMAND; 4311 sense->es_add_code = 4312 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4313 sense->es_qual_code = 4314 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4315 4316 goto done; 4317 } 4318 4319 len = sata_build_lsense_page_2f(sdinfo, buf + len, 4320 spx->txlt_sata_hba_inst); 4321 break; 4322 case PAGE_CODE_SMART_READ_DATA: 4323 sata_id = &sdinfo->satadrv_id; 4324 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4325 *scsipkt->pkt_scbp = STATUS_CHECK; 4326 sense = sata_arq_sense(spx); 4327 sense->es_key = KEY_ILLEGAL_REQUEST; 4328 sense->es_add_code = 4329 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4330 4331 goto done; 4332 } 4333 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4334 *scsipkt->pkt_scbp = STATUS_CHECK; 4335 sense = sata_arq_sense(spx); 4336 sense->es_key = KEY_ABORTED_COMMAND; 4337 sense->es_add_code = 4338 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4339 sense->es_qual_code = 4340 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4341 4342 goto done; 4343 } 4344 4345 /* This page doesn't include a page header */ 4346 len = sata_build_lsense_page_30(sdinfo, buf, 4347 spx->txlt_sata_hba_inst); 4348 goto no_header; 4349 default: 4350 /* Invalid request */ 4351 *scsipkt->pkt_scbp = STATUS_CHECK; 4352 sense = sata_arq_sense(spx); 4353 sense->es_key = KEY_ILLEGAL_REQUEST; 4354 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4355 goto done; 4356 } 4357 4358 /* set parameter log sense data length */ 4359 buf[2] = len >> 8; /* log sense length (MSB) */ 4360 buf[3] = len & 0xff; /* log sense length (LSB) */ 4361 4362 len += SCSI_LOG_PAGE_HDR_LEN; 4363 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 4364 4365 no_header: 4366 /* Check allocation length */ 4367 alc_len = scsipkt->pkt_cdbp[7]; 4368 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4369 4370 /* 4371 * We do not check for possible parameters truncation 4372 * (alc_len < len) assuming that the target driver works 4373 * correctly. Just avoiding overrun. 4374 * Copy no more than requested and possible, buffer-wise. 4375 */ 4376 count = MIN(alc_len, len); 4377 count = MIN(bp->b_bcount, count); 4378 bcopy(buf, bp->b_un.b_addr, count); 4379 4380 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4381 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4382 } 4383 *scsipkt->pkt_scbp = STATUS_GOOD; 4384 done: 4385 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4386 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4387 4388 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4389 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4390 4391 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4392 scsipkt->pkt_comp != NULL) 4393 /* scsi callback required */ 4394 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4395 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4396 TQ_SLEEP) == NULL) 4397 /* Scheduling the callback failed */ 4398 return (TRAN_BUSY); 4399 4400 return (TRAN_ACCEPT); 4401 } 4402 4403 /* 4404 * Translate command: Log Select 4405 * Not implemented at this time - returns invalid command response. 4406 */ 4407 static int 4408 sata_txlt_log_select(sata_pkt_txlate_t *spx) 4409 { 4410 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4411 "sata_txlt_log_select\n", NULL); 4412 4413 return (sata_txlt_invalid_command(spx)); 4414 } 4415 4416 4417 /* 4418 * Translate command: Read (various types). 4419 * Translated into appropriate type of ATA READ command 4420 * for SATA hard disks. 4421 * Both the device capabilities and requested operation mode are 4422 * considered. 4423 * 4424 * Following scsi cdb fields are ignored: 4425 * rdprotect, dpo, fua, fua_nv, group_number. 4426 * 4427 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4428 * enable variable sata_func_enable), the capability of the controller and 4429 * capability of a device are checked and if both support queueing, read 4430 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 4431 * command rather than plain READ_XXX command. 4432 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4433 * both the controller and device suport such functionality, the read 4434 * request will be translated to READ_FPDMA_QUEUED command. 4435 * In both cases the maximum queue depth is derived as minimum of: 4436 * HBA capability,device capability and sata_max_queue_depth variable setting. 4437 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4438 * used to pass max queue depth value, and the maximum possible queue depth 4439 * is 32. 4440 * 4441 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4442 * appropriate values in scsi_pkt fields. 4443 */ 4444 static int 4445 sata_txlt_read(sata_pkt_txlate_t *spx) 4446 { 4447 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4448 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4449 sata_drive_info_t *sdinfo; 4450 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4451 int cport = SATA_TXLT_CPORT(spx); 4452 uint16_t sec_count; 4453 uint64_t lba; 4454 int rval, reason; 4455 int synch; 4456 4457 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4458 4459 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4460 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4461 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4462 return (rval); 4463 } 4464 4465 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4466 &spx->txlt_sata_pkt->satapkt_device); 4467 4468 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 4469 /* 4470 * Extract LBA and sector count from scsi CDB. 4471 */ 4472 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4473 case SCMD_READ: 4474 /* 6-byte scsi read cmd : 0x08 */ 4475 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4476 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4477 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4478 sec_count = scsipkt->pkt_cdbp[4]; 4479 /* sec_count 0 will be interpreted as 256 by a device */ 4480 break; 4481 case SCMD_READ_G1: 4482 /* 10-bytes scsi read command : 0x28 */ 4483 lba = scsipkt->pkt_cdbp[2]; 4484 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4485 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4486 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4487 sec_count = scsipkt->pkt_cdbp[7]; 4488 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4489 break; 4490 case SCMD_READ_G5: 4491 /* 12-bytes scsi read command : 0xA8 */ 4492 lba = scsipkt->pkt_cdbp[2]; 4493 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4494 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4495 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4496 sec_count = scsipkt->pkt_cdbp[6]; 4497 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4498 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4499 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4500 break; 4501 case SCMD_READ_G4: 4502 /* 16-bytes scsi read command : 0x88 */ 4503 lba = scsipkt->pkt_cdbp[2]; 4504 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4505 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4506 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4507 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4508 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4509 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4510 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4511 sec_count = scsipkt->pkt_cdbp[10]; 4512 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4513 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4514 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4515 break; 4516 default: 4517 /* Unsupported command */ 4518 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4519 return (sata_txlt_invalid_command(spx)); 4520 } 4521 4522 /* 4523 * Check if specified address exceeds device capacity 4524 */ 4525 if ((lba >= sdinfo->satadrv_capacity) || 4526 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4527 /* LBA out of range */ 4528 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4529 return (sata_txlt_lba_out_of_range(spx)); 4530 } 4531 4532 /* 4533 * For zero-length transfer, emulate good completion of the command 4534 * (reasons for rejecting the command were already checked). 4535 * No DMA resources were allocated. 4536 */ 4537 if (spx->txlt_dma_cookie_list == NULL) { 4538 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4539 return (sata_emul_rw_completion(spx)); 4540 } 4541 4542 /* 4543 * Build cmd block depending on the device capability and 4544 * requested operation mode. 4545 * Do not bother with non-dma mode - we are working only with 4546 * devices supporting DMA. 4547 */ 4548 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4549 scmd->satacmd_device_reg = SATA_ADH_LBA; 4550 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 4551 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4552 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4553 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 4554 scmd->satacmd_sec_count_msb = sec_count >> 8; 4555 #ifndef __lock_lint 4556 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4557 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4558 scmd->satacmd_lba_high_msb = lba >> 40; 4559 #endif 4560 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4561 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4562 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4563 } 4564 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4565 scmd->satacmd_lba_low_lsb = lba & 0xff; 4566 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4567 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4568 scmd->satacmd_features_reg = 0; 4569 scmd->satacmd_status_reg = 0; 4570 scmd->satacmd_error_reg = 0; 4571 4572 /* 4573 * Check if queueing commands should be used and switch 4574 * to appropriate command if possible 4575 */ 4576 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4577 boolean_t using_queuing; 4578 4579 /* Queuing supported by controller and device? */ 4580 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4581 (sdinfo->satadrv_features_support & 4582 SATA_DEV_F_NCQ) && 4583 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4584 SATA_CTLF_NCQ)) { 4585 using_queuing = B_TRUE; 4586 4587 /* NCQ supported - use FPDMA READ */ 4588 scmd->satacmd_cmd_reg = 4589 SATAC_READ_FPDMA_QUEUED; 4590 scmd->satacmd_features_reg_ext = 4591 scmd->satacmd_sec_count_msb; 4592 scmd->satacmd_sec_count_msb = 0; 4593 } else if ((sdinfo->satadrv_features_support & 4594 SATA_DEV_F_TCQ) && 4595 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4596 SATA_CTLF_QCMD)) { 4597 using_queuing = B_TRUE; 4598 4599 /* Legacy queueing */ 4600 if (sdinfo->satadrv_features_support & 4601 SATA_DEV_F_LBA48) { 4602 scmd->satacmd_cmd_reg = 4603 SATAC_READ_DMA_QUEUED_EXT; 4604 scmd->satacmd_features_reg_ext = 4605 scmd->satacmd_sec_count_msb; 4606 scmd->satacmd_sec_count_msb = 0; 4607 } else { 4608 scmd->satacmd_cmd_reg = 4609 SATAC_READ_DMA_QUEUED; 4610 } 4611 } else /* NCQ nor legacy queuing not supported */ 4612 using_queuing = B_FALSE; 4613 4614 /* 4615 * If queuing, the sector count goes in the features register 4616 * and the secount count will contain the tag. 4617 */ 4618 if (using_queuing) { 4619 scmd->satacmd_features_reg = 4620 scmd->satacmd_sec_count_lsb; 4621 scmd->satacmd_sec_count_lsb = 0; 4622 scmd->satacmd_flags.sata_queued = B_TRUE; 4623 4624 /* Set-up maximum queue depth */ 4625 scmd->satacmd_flags.sata_max_queue_depth = 4626 sdinfo->satadrv_max_queue_depth - 1; 4627 } else if (sdinfo->satadrv_features_enabled & 4628 SATA_DEV_F_E_UNTAGGED_QING) { 4629 /* 4630 * Although NCQ/TCQ is not enabled, untagged queuing 4631 * may be still used. 4632 * Set-up the maximum untagged queue depth. 4633 * Use controller's queue depth from sata_hba_tran. 4634 * SATA HBA drivers may ignore this value and rely on 4635 * the internal limits.For drivers that do not 4636 * ignore untaged queue depth, limit the value to 4637 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4638 * largest value that can be passed via 4639 * satacmd_flags.sata_max_queue_depth. 4640 */ 4641 scmd->satacmd_flags.sata_max_queue_depth = 4642 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4643 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4644 4645 } else { 4646 scmd->satacmd_flags.sata_max_queue_depth = 0; 4647 } 4648 } else 4649 scmd->satacmd_flags.sata_max_queue_depth = 0; 4650 4651 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 4652 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 4653 scmd->satacmd_cmd_reg, lba, sec_count); 4654 4655 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4656 /* Need callback function */ 4657 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4658 synch = FALSE; 4659 } else 4660 synch = TRUE; 4661 4662 /* Transfer command to HBA */ 4663 if (sata_hba_start(spx, &rval) != 0) { 4664 /* Pkt not accepted for execution */ 4665 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4666 return (rval); 4667 } 4668 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4669 /* 4670 * If execution is non-synchronous, 4671 * a callback function will handle potential errors, translate 4672 * the response and will do a callback to a target driver. 4673 * If it was synchronous, check execution status using the same 4674 * framework callback. 4675 */ 4676 if (synch) { 4677 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4678 "synchronous execution status %x\n", 4679 spx->txlt_sata_pkt->satapkt_reason); 4680 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4681 } 4682 return (TRAN_ACCEPT); 4683 } 4684 4685 4686 /* 4687 * SATA translate command: Write (various types) 4688 * Translated into appropriate type of ATA WRITE command 4689 * for SATA hard disks. 4690 * Both the device capabilities and requested operation mode are 4691 * considered. 4692 * 4693 * Following scsi cdb fields are ignored: 4694 * rwprotect, dpo, fua, fua_nv, group_number. 4695 * 4696 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4697 * enable variable sata_func_enable), the capability of the controller and 4698 * capability of a device are checked and if both support queueing, write 4699 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 4700 * command rather than plain WRITE_XXX command. 4701 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4702 * both the controller and device suport such functionality, the write 4703 * request will be translated to WRITE_FPDMA_QUEUED command. 4704 * In both cases the maximum queue depth is derived as minimum of: 4705 * HBA capability,device capability and sata_max_queue_depth variable setting. 4706 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4707 * used to pass max queue depth value, and the maximum possible queue depth 4708 * is 32. 4709 * 4710 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4711 * appropriate values in scsi_pkt fields. 4712 */ 4713 static int 4714 sata_txlt_write(sata_pkt_txlate_t *spx) 4715 { 4716 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4717 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4718 sata_drive_info_t *sdinfo; 4719 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4720 int cport = SATA_TXLT_CPORT(spx); 4721 uint16_t sec_count; 4722 uint64_t lba; 4723 int rval, reason; 4724 int synch; 4725 4726 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4727 4728 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4729 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4730 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4731 return (rval); 4732 } 4733 4734 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4735 &spx->txlt_sata_pkt->satapkt_device); 4736 4737 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4738 /* 4739 * Extract LBA and sector count from scsi CDB 4740 */ 4741 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4742 case SCMD_WRITE: 4743 /* 6-byte scsi read cmd : 0x0A */ 4744 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4745 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4746 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4747 sec_count = scsipkt->pkt_cdbp[4]; 4748 /* sec_count 0 will be interpreted as 256 by a device */ 4749 break; 4750 case SCMD_WRITE_G1: 4751 /* 10-bytes scsi write command : 0x2A */ 4752 lba = scsipkt->pkt_cdbp[2]; 4753 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4754 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4755 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4756 sec_count = scsipkt->pkt_cdbp[7]; 4757 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4758 break; 4759 case SCMD_WRITE_G5: 4760 /* 12-bytes scsi read command : 0xAA */ 4761 lba = scsipkt->pkt_cdbp[2]; 4762 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4763 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4764 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4765 sec_count = scsipkt->pkt_cdbp[6]; 4766 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4767 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4768 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4769 break; 4770 case SCMD_WRITE_G4: 4771 /* 16-bytes scsi write command : 0x8A */ 4772 lba = scsipkt->pkt_cdbp[2]; 4773 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4774 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4775 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4776 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4777 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4778 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4779 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4780 sec_count = scsipkt->pkt_cdbp[10]; 4781 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4782 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4783 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4784 break; 4785 default: 4786 /* Unsupported command */ 4787 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4788 return (sata_txlt_invalid_command(spx)); 4789 } 4790 4791 /* 4792 * Check if specified address and length exceeds device capacity 4793 */ 4794 if ((lba >= sdinfo->satadrv_capacity) || 4795 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4796 /* LBA out of range */ 4797 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4798 return (sata_txlt_lba_out_of_range(spx)); 4799 } 4800 4801 /* 4802 * For zero-length transfer, emulate good completion of the command 4803 * (reasons for rejecting the command were already checked). 4804 * No DMA resources were allocated. 4805 */ 4806 if (spx->txlt_dma_cookie_list == NULL) { 4807 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4808 return (sata_emul_rw_completion(spx)); 4809 } 4810 4811 /* 4812 * Build cmd block depending on the device capability and 4813 * requested operation mode. 4814 * Do not bother with non-dma mode- we are working only with 4815 * devices supporting DMA. 4816 */ 4817 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4818 scmd->satacmd_device_reg = SATA_ADH_LBA; 4819 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 4820 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4821 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4822 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 4823 scmd->satacmd_sec_count_msb = sec_count >> 8; 4824 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4825 #ifndef __lock_lint 4826 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4827 scmd->satacmd_lba_high_msb = lba >> 40; 4828 #endif 4829 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4830 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4831 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4832 } 4833 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4834 scmd->satacmd_lba_low_lsb = lba & 0xff; 4835 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4836 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4837 scmd->satacmd_features_reg = 0; 4838 scmd->satacmd_status_reg = 0; 4839 scmd->satacmd_error_reg = 0; 4840 4841 /* 4842 * Check if queueing commands should be used and switch 4843 * to appropriate command if possible 4844 */ 4845 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4846 boolean_t using_queuing; 4847 4848 /* Queuing supported by controller and device? */ 4849 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4850 (sdinfo->satadrv_features_support & 4851 SATA_DEV_F_NCQ) && 4852 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4853 SATA_CTLF_NCQ)) { 4854 using_queuing = B_TRUE; 4855 4856 /* NCQ supported - use FPDMA WRITE */ 4857 scmd->satacmd_cmd_reg = 4858 SATAC_WRITE_FPDMA_QUEUED; 4859 scmd->satacmd_features_reg_ext = 4860 scmd->satacmd_sec_count_msb; 4861 scmd->satacmd_sec_count_msb = 0; 4862 } else if ((sdinfo->satadrv_features_support & 4863 SATA_DEV_F_TCQ) && 4864 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4865 SATA_CTLF_QCMD)) { 4866 using_queuing = B_TRUE; 4867 4868 /* Legacy queueing */ 4869 if (sdinfo->satadrv_features_support & 4870 SATA_DEV_F_LBA48) { 4871 scmd->satacmd_cmd_reg = 4872 SATAC_WRITE_DMA_QUEUED_EXT; 4873 scmd->satacmd_features_reg_ext = 4874 scmd->satacmd_sec_count_msb; 4875 scmd->satacmd_sec_count_msb = 0; 4876 } else { 4877 scmd->satacmd_cmd_reg = 4878 SATAC_WRITE_DMA_QUEUED; 4879 } 4880 } else /* NCQ nor legacy queuing not supported */ 4881 using_queuing = B_FALSE; 4882 4883 if (using_queuing) { 4884 scmd->satacmd_features_reg = 4885 scmd->satacmd_sec_count_lsb; 4886 scmd->satacmd_sec_count_lsb = 0; 4887 scmd->satacmd_flags.sata_queued = B_TRUE; 4888 /* Set-up maximum queue depth */ 4889 scmd->satacmd_flags.sata_max_queue_depth = 4890 sdinfo->satadrv_max_queue_depth - 1; 4891 } else if (sdinfo->satadrv_features_enabled & 4892 SATA_DEV_F_E_UNTAGGED_QING) { 4893 /* 4894 * Although NCQ/TCQ is not enabled, untagged queuing 4895 * may be still used. 4896 * Set-up the maximum untagged queue depth. 4897 * Use controller's queue depth from sata_hba_tran. 4898 * SATA HBA drivers may ignore this value and rely on 4899 * the internal limits. For drivera that do not 4900 * ignore untaged queue depth, limit the value to 4901 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4902 * largest value that can be passed via 4903 * satacmd_flags.sata_max_queue_depth. 4904 */ 4905 scmd->satacmd_flags.sata_max_queue_depth = 4906 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4907 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4908 4909 } else { 4910 scmd->satacmd_flags.sata_max_queue_depth = 0; 4911 } 4912 } else 4913 scmd->satacmd_flags.sata_max_queue_depth = 0; 4914 4915 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4916 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 4917 scmd->satacmd_cmd_reg, lba, sec_count); 4918 4919 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4920 /* Need callback function */ 4921 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4922 synch = FALSE; 4923 } else 4924 synch = TRUE; 4925 4926 /* Transfer command to HBA */ 4927 if (sata_hba_start(spx, &rval) != 0) { 4928 /* Pkt not accepted for execution */ 4929 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4930 return (rval); 4931 } 4932 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4933 4934 /* 4935 * If execution is non-synchronous, 4936 * a callback function will handle potential errors, translate 4937 * the response and will do a callback to a target driver. 4938 * If it was synchronous, check execution status using the same 4939 * framework callback. 4940 */ 4941 if (synch) { 4942 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4943 "synchronous execution status %x\n", 4944 spx->txlt_sata_pkt->satapkt_reason); 4945 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4946 } 4947 return (TRAN_ACCEPT); 4948 } 4949 4950 4951 /* 4952 * Implements SCSI SBC WRITE BUFFER command download microcode option 4953 */ 4954 static int 4955 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 4956 { 4957 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 4958 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 4959 4960 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 4961 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4962 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 4963 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4964 4965 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4966 struct scsi_extended_sense *sense; 4967 int rval, mode, sector_count, reason; 4968 int cport = SATA_TXLT_CPORT(spx); 4969 4970 mode = scsipkt->pkt_cdbp[1] & 0x1f; 4971 4972 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4973 "sata_txlt_write_buffer, mode 0x%x\n", mode); 4974 4975 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4976 4977 if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) { 4978 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4979 return (rval); 4980 } 4981 /* 4982 * If in interrupt context, reject this packet because it would issue 4983 * a synchronous command to HBA. 4984 */ 4985 if (servicing_interrupt()) { 4986 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4987 "sata_txlt_write_buffer: rejecting command because " 4988 "of interrupt context\n", NULL); 4989 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4990 return (TRAN_BUSY); 4991 } 4992 4993 /* Use synchronous mode */ 4994 spx->txlt_sata_pkt->satapkt_op_mode 4995 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 4996 4997 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4998 4999 scsipkt->pkt_reason = CMD_CMPLT; 5000 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5001 STATE_SENT_CMD | STATE_GOT_STATUS; 5002 5003 /* 5004 * The SCSI to ATA translation specification only calls 5005 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5006 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5007 * ATA 8 (draft) got rid of download microcode for temp 5008 * and it is even optional for ATA 7, so it may be aborted. 5009 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5010 * it is not specified and the buffer offset for SCSI is a 16-bit 5011 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5012 * sectors. Thus the offset really doesn't buy us anything. 5013 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5014 * is revised, this can be revisisted. 5015 */ 5016 /* Reject not supported request */ 5017 switch (mode) { 5018 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5019 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5020 break; 5021 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5022 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5023 break; 5024 default: 5025 goto bad_param; 5026 } 5027 5028 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5029 5030 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5031 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5032 goto bad_param; 5033 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5034 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5035 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5036 scmd->satacmd_lba_mid_lsb = 0; 5037 scmd->satacmd_lba_high_lsb = 0; 5038 scmd->satacmd_device_reg = 0; 5039 spx->txlt_sata_pkt->satapkt_comp = NULL; 5040 scmd->satacmd_addr_type = 0; 5041 5042 /* Transfer command to HBA */ 5043 if (sata_hba_start(spx, &rval) != 0) { 5044 /* Pkt not accepted for execution */ 5045 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5046 return (rval); 5047 } 5048 5049 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5050 5051 /* Then we need synchronous check the status of the disk */ 5052 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5053 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5054 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5055 scsipkt->pkt_reason = CMD_CMPLT; 5056 5057 /* Download commmand succeed, so probe and identify device */ 5058 sata_reidentify_device(spx); 5059 } else { 5060 /* Something went wrong, microcode download command failed */ 5061 scsipkt->pkt_reason = CMD_INCOMPLETE; 5062 *scsipkt->pkt_scbp = STATUS_CHECK; 5063 sense = sata_arq_sense(spx); 5064 switch (sata_pkt->satapkt_reason) { 5065 case SATA_PKT_PORT_ERROR: 5066 /* 5067 * We have no device data. Assume no data transfered. 5068 */ 5069 sense->es_key = KEY_HARDWARE_ERROR; 5070 break; 5071 5072 case SATA_PKT_DEV_ERROR: 5073 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5074 SATA_STATUS_ERR) { 5075 /* 5076 * determine dev error reason from error 5077 * reg content 5078 */ 5079 sata_decode_device_error(spx, sense); 5080 break; 5081 } 5082 /* No extended sense key - no info available */ 5083 break; 5084 5085 case SATA_PKT_TIMEOUT: 5086 scsipkt->pkt_reason = CMD_TIMEOUT; 5087 scsipkt->pkt_statistics |= 5088 STAT_TIMEOUT | STAT_DEV_RESET; 5089 /* No extended sense key ? */ 5090 break; 5091 5092 case SATA_PKT_ABORTED: 5093 scsipkt->pkt_reason = CMD_ABORTED; 5094 scsipkt->pkt_statistics |= STAT_ABORTED; 5095 /* No extended sense key ? */ 5096 break; 5097 5098 case SATA_PKT_RESET: 5099 /* pkt aborted by an explicit reset from a host */ 5100 scsipkt->pkt_reason = CMD_RESET; 5101 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5102 break; 5103 5104 default: 5105 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5106 "sata_txlt_nodata_cmd_completion: " 5107 "invalid packet completion reason %d", 5108 sata_pkt->satapkt_reason)); 5109 scsipkt->pkt_reason = CMD_TRAN_ERR; 5110 break; 5111 } 5112 5113 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5114 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5115 5116 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5117 scsipkt->pkt_comp != NULL) 5118 /* scsi callback required */ 5119 (*scsipkt->pkt_comp)(scsipkt); 5120 } 5121 return (TRAN_ACCEPT); 5122 5123 bad_param: 5124 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5125 *scsipkt->pkt_scbp = STATUS_CHECK; 5126 sense = sata_arq_sense(spx); 5127 sense->es_key = KEY_ILLEGAL_REQUEST; 5128 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5129 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5130 scsipkt->pkt_comp != NULL) { 5131 /* scsi callback required */ 5132 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5133 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5134 TQ_SLEEP) == 0) { 5135 /* Scheduling the callback failed */ 5136 rval = TRAN_BUSY; 5137 } 5138 } 5139 return (rval); 5140 } 5141 5142 /* 5143 * Re-identify device after doing a firmware download. 5144 */ 5145 static void 5146 sata_reidentify_device(sata_pkt_txlate_t *spx) 5147 { 5148 #define DOWNLOAD_WAIT_TIME_SECS 60 5149 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5150 int rval; 5151 int retry_cnt; 5152 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5153 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5154 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5155 sata_drive_info_t *sdinfo; 5156 5157 /* 5158 * Before returning good status, probe device. 5159 * Device probing will get IDENTIFY DEVICE data, if possible. 5160 * The assumption is that the new microcode is applied by the 5161 * device. It is a caller responsibility to verify this. 5162 */ 5163 for (retry_cnt = 0; 5164 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5165 retry_cnt++) { 5166 rval = sata_probe_device(sata_hba_inst, &sata_device); 5167 5168 if (rval == SATA_SUCCESS) { /* Set default features */ 5169 sdinfo = sata_get_device_info(sata_hba_inst, 5170 &sata_device); 5171 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5172 SATA_SUCCESS) { 5173 /* retry */ 5174 rval = sata_initialize_device(sata_hba_inst, 5175 sdinfo); 5176 if (rval == SATA_RETRY) 5177 sata_log(sata_hba_inst, CE_WARN, 5178 "SATA device at port %d pmport %d -" 5179 " default device features could not" 5180 " be set. Device may not operate " 5181 "as expected.", 5182 sata_device.satadev_addr.cport, 5183 sata_device.satadev_addr.pmport); 5184 } 5185 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5186 scsipkt->pkt_comp != NULL) 5187 (*scsipkt->pkt_comp)(scsipkt); 5188 return; 5189 } else if (rval == SATA_RETRY) { 5190 delay(drv_usectohz(1000000 * 5191 DOWNLOAD_WAIT_INTERVAL_SECS)); 5192 continue; 5193 } else /* failed - no reason to retry */ 5194 break; 5195 } 5196 5197 /* 5198 * Something went wrong, device probing failed. 5199 */ 5200 SATA_LOG_D((sata_hba_inst, CE_WARN, 5201 "Cannot probe device after downloading microcode\n")); 5202 5203 /* Reset device to force retrying the probe. */ 5204 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5205 (SATA_DIP(sata_hba_inst), &sata_device); 5206 5207 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5208 scsipkt->pkt_comp != NULL) 5209 (*scsipkt->pkt_comp)(scsipkt); 5210 } 5211 5212 5213 /* 5214 * Translate command: Synchronize Cache. 5215 * Translates into Flush Cache command for SATA hard disks. 5216 * 5217 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5218 * appropriate values in scsi_pkt fields. 5219 */ 5220 static int 5221 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5222 { 5223 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5224 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5225 int cport = SATA_TXLT_CPORT(spx); 5226 int rval, reason; 5227 int synch; 5228 5229 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5230 5231 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5232 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5233 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5234 return (rval); 5235 } 5236 5237 scmd->satacmd_addr_type = 0; 5238 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 5239 scmd->satacmd_device_reg = 0; 5240 scmd->satacmd_sec_count_lsb = 0; 5241 scmd->satacmd_lba_low_lsb = 0; 5242 scmd->satacmd_lba_mid_lsb = 0; 5243 scmd->satacmd_lba_high_lsb = 0; 5244 scmd->satacmd_features_reg = 0; 5245 scmd->satacmd_status_reg = 0; 5246 scmd->satacmd_error_reg = 0; 5247 5248 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5249 "sata_txlt_synchronize_cache\n", NULL); 5250 5251 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5252 /* Need to set-up a callback function */ 5253 spx->txlt_sata_pkt->satapkt_comp = 5254 sata_txlt_nodata_cmd_completion; 5255 synch = FALSE; 5256 } else 5257 synch = TRUE; 5258 5259 /* Transfer command to HBA */ 5260 if (sata_hba_start(spx, &rval) != 0) { 5261 /* Pkt not accepted for execution */ 5262 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5263 return (rval); 5264 } 5265 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5266 5267 /* 5268 * If execution non-synchronous, it had to be completed 5269 * a callback function will handle potential errors, translate 5270 * the response and will do a callback to a target driver. 5271 * If it was synchronous, check status, using the same 5272 * framework callback. 5273 */ 5274 if (synch) { 5275 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5276 "synchronous execution status %x\n", 5277 spx->txlt_sata_pkt->satapkt_reason); 5278 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 5279 } 5280 return (TRAN_ACCEPT); 5281 } 5282 5283 5284 /* 5285 * Send pkt to SATA HBA driver 5286 * 5287 * This function may be called only if the operation is requested by scsi_pkt, 5288 * i.e. scsi_pkt is not NULL. 5289 * 5290 * This function has to be called with cport mutex held. It does release 5291 * the mutex when it calls HBA driver sata_tran_start function and 5292 * re-acquires it afterwards. 5293 * 5294 * If return value is 0, pkt was accepted, -1 otherwise 5295 * rval is set to appropriate sata_scsi_start return value. 5296 * 5297 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 5298 * have called the sata_pkt callback function for this packet. 5299 * 5300 * The scsi callback has to be performed by the caller of this routine. 5301 * 5302 * Note 2: No port multiplier support for now. 5303 */ 5304 static int 5305 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 5306 { 5307 int stat, cport; 5308 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5309 sata_drive_info_t *sdinfo; 5310 sata_device_t *sata_device; 5311 uint8_t cmd; 5312 struct sata_cmd_flags cmd_flags; 5313 5314 ASSERT(spx->txlt_sata_pkt != NULL); 5315 5316 cport = SATA_TXLT_CPORT(spx); 5317 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5318 5319 sdinfo = sata_get_device_info(sata_hba_inst, 5320 &spx->txlt_sata_pkt->satapkt_device); 5321 ASSERT(sdinfo != NULL); 5322 5323 /* Clear device reset state? */ 5324 if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 5325 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 5326 sata_clear_dev_reset = B_TRUE; 5327 sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET; 5328 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5329 "sata_hba_start: clearing device reset state\n", NULL); 5330 } 5331 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 5332 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 5333 sata_device = &spx->txlt_sata_pkt->satapkt_device; 5334 5335 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5336 5337 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5338 "Sata cmd 0x%2x\n", cmd); 5339 5340 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 5341 spx->txlt_sata_pkt); 5342 5343 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5344 /* 5345 * If sata pkt was accepted and executed in asynchronous mode, i.e. 5346 * with the sata callback, the sata_pkt could be already destroyed 5347 * by the time we check ther return status from the hba_start() 5348 * function, because sata_scsi_destroy_pkt() could have been already 5349 * called (perhaps in the interrupt context). So, in such case, there 5350 * should be no references to it. In other cases, sata_pkt still 5351 * exists. 5352 */ 5353 if (stat == SATA_TRAN_ACCEPTED) { 5354 /* 5355 * pkt accepted for execution. 5356 * If it was executed synchronously, it is already completed 5357 * and pkt completion_reason indicates completion status. 5358 */ 5359 *rval = TRAN_ACCEPT; 5360 return (0); 5361 } 5362 5363 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5364 switch (stat) { 5365 case SATA_TRAN_QUEUE_FULL: 5366 /* 5367 * Controller detected queue full condition. 5368 */ 5369 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 5370 "sata_hba_start: queue full\n", NULL); 5371 5372 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5373 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 5374 5375 *rval = TRAN_BUSY; 5376 break; 5377 5378 case SATA_TRAN_PORT_ERROR: 5379 /* 5380 * Communication/link with device or general port error 5381 * detected before pkt execution begun. 5382 */ 5383 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5384 SATA_ADDR_CPORT || 5385 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5386 SATA_ADDR_DCPORT) 5387 sata_log(sata_hba_inst, CE_CONT, 5388 "SATA port %d error", 5389 sata_device->satadev_addr.cport); 5390 else 5391 sata_log(sata_hba_inst, CE_CONT, 5392 "SATA port %d pmport %d error\n", 5393 sata_device->satadev_addr.cport, 5394 sata_device->satadev_addr.pmport); 5395 5396 /* 5397 * Update the port/device structure. 5398 * sata_pkt should be still valid. Since port error is 5399 * returned, sata_device content should reflect port 5400 * state - it means, that sata address have been changed, 5401 * because original packet's sata address refered to a device 5402 * attached to some port. 5403 */ 5404 sata_update_port_info(sata_hba_inst, sata_device); 5405 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5406 *rval = TRAN_FATAL_ERROR; 5407 break; 5408 5409 case SATA_TRAN_CMD_UNSUPPORTED: 5410 /* 5411 * Command rejected by HBA as unsupported. It was HBA driver 5412 * that rejected the command, command was not sent to 5413 * an attached device. 5414 */ 5415 if ((sdinfo != NULL) && 5416 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 5417 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5418 "sat_hba_start: cmd 0x%2x rejected " 5419 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 5420 5421 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5422 (void) sata_txlt_invalid_command(spx); 5423 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5424 5425 *rval = TRAN_ACCEPT; 5426 break; 5427 5428 case SATA_TRAN_BUSY: 5429 /* 5430 * Command rejected by HBA because other operation prevents 5431 * accepting the packet, or device is in RESET condition. 5432 */ 5433 if (sdinfo != NULL) { 5434 sdinfo->satadrv_state = 5435 spx->txlt_sata_pkt->satapkt_device.satadev_state; 5436 5437 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 5438 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5439 "sata_hba_start: cmd 0x%2x rejected " 5440 "because of device reset condition\n", 5441 cmd); 5442 } else { 5443 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5444 "sata_hba_start: cmd 0x%2x rejected " 5445 "with SATA_TRAN_BUSY status\n", 5446 cmd); 5447 } 5448 } 5449 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5450 *rval = TRAN_BUSY; 5451 break; 5452 5453 default: 5454 /* Unrecognized HBA response */ 5455 SATA_LOG_D((sata_hba_inst, CE_WARN, 5456 "sata_hba_start: unrecognized HBA response " 5457 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 5458 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5459 *rval = TRAN_FATAL_ERROR; 5460 break; 5461 } 5462 5463 /* 5464 * If we got here, the packet was rejected. 5465 * Check if we need to remember reset state clearing request 5466 */ 5467 if (cmd_flags.sata_clear_dev_reset) { 5468 /* 5469 * Check if device is still configured - it may have 5470 * disapeared from the configuration 5471 */ 5472 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5473 if (sdinfo != NULL) { 5474 /* 5475 * Restore the flag that requests clearing of 5476 * the device reset state, 5477 * so the next sata packet may carry it to HBA. 5478 */ 5479 sdinfo->satadrv_event_flags |= 5480 SATA_EVNT_CLEAR_DEVICE_RESET; 5481 } 5482 } 5483 return (-1); 5484 } 5485 5486 /* 5487 * Scsi response setup for invalid LBA 5488 * 5489 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 5490 */ 5491 static int 5492 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 5493 { 5494 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5495 struct scsi_extended_sense *sense; 5496 5497 scsipkt->pkt_reason = CMD_CMPLT; 5498 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5499 STATE_SENT_CMD | STATE_GOT_STATUS; 5500 *scsipkt->pkt_scbp = STATUS_CHECK; 5501 5502 *scsipkt->pkt_scbp = STATUS_CHECK; 5503 sense = sata_arq_sense(spx); 5504 sense->es_key = KEY_ILLEGAL_REQUEST; 5505 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 5506 5507 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5508 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5509 5510 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5511 scsipkt->pkt_comp != NULL) 5512 /* scsi callback required */ 5513 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5514 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5515 TQ_SLEEP) == NULL) 5516 /* Scheduling the callback failed */ 5517 return (TRAN_BUSY); 5518 return (TRAN_ACCEPT); 5519 } 5520 5521 5522 /* 5523 * Analyze device status and error registers and translate them into 5524 * appropriate scsi sense codes. 5525 * NOTE: non-packet commands only for now 5526 */ 5527 static void 5528 sata_decode_device_error(sata_pkt_txlate_t *spx, 5529 struct scsi_extended_sense *sense) 5530 { 5531 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 5532 5533 ASSERT(sense != NULL); 5534 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 5535 SATA_STATUS_ERR); 5536 5537 5538 if (err_reg & SATA_ERROR_ICRC) { 5539 sense->es_key = KEY_ABORTED_COMMAND; 5540 sense->es_add_code = 0x08; /* Communication failure */ 5541 return; 5542 } 5543 5544 if (err_reg & SATA_ERROR_UNC) { 5545 sense->es_key = KEY_MEDIUM_ERROR; 5546 /* Information bytes (LBA) need to be set by a caller */ 5547 return; 5548 } 5549 5550 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 5551 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 5552 sense->es_key = KEY_UNIT_ATTENTION; 5553 sense->es_add_code = 0x3a; /* No media present */ 5554 return; 5555 } 5556 5557 if (err_reg & SATA_ERROR_IDNF) { 5558 if (err_reg & SATA_ERROR_ABORT) { 5559 sense->es_key = KEY_ABORTED_COMMAND; 5560 } else { 5561 sense->es_key = KEY_ILLEGAL_REQUEST; 5562 sense->es_add_code = 0x21; /* LBA out of range */ 5563 } 5564 return; 5565 } 5566 5567 if (err_reg & SATA_ERROR_ABORT) { 5568 ASSERT(spx->txlt_sata_pkt != NULL); 5569 sense->es_key = KEY_ABORTED_COMMAND; 5570 return; 5571 } 5572 } 5573 5574 /* 5575 * Extract error LBA from sata_pkt.satapkt_cmd register fields 5576 */ 5577 static void 5578 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 5579 { 5580 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 5581 5582 *lba = 0; 5583 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 5584 *lba = sata_cmd->satacmd_lba_high_msb; 5585 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 5586 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 5587 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 5588 *lba = sata_cmd->satacmd_device_reg & 0xf; 5589 } 5590 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 5591 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 5592 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 5593 } 5594 5595 /* 5596 * This is fixed sense format - if LBA exceeds the info field size, 5597 * no valid info will be returned (valid bit in extended sense will 5598 * be set to 0). 5599 */ 5600 static struct scsi_extended_sense * 5601 sata_arq_sense(sata_pkt_txlate_t *spx) 5602 { 5603 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5604 struct scsi_arq_status *arqs; 5605 struct scsi_extended_sense *sense; 5606 5607 /* Fill ARQ sense data */ 5608 scsipkt->pkt_state |= STATE_ARQ_DONE; 5609 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 5610 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 5611 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 5612 arqs->sts_rqpkt_reason = CMD_CMPLT; 5613 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5614 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 5615 arqs->sts_rqpkt_resid = 0; 5616 sense = &arqs->sts_sensedata; 5617 bzero(sense, sizeof (struct scsi_extended_sense)); 5618 sata_fixed_sense_data_preset(sense); 5619 return (sense); 5620 } 5621 5622 5623 /* 5624 * Emulated SATA Read/Write command completion for zero-length requests. 5625 * This request always succedes, so in synchronous mode it always returns 5626 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 5627 * callback cannot be scheduled. 5628 */ 5629 static int 5630 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 5631 { 5632 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5633 5634 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5635 STATE_SENT_CMD | STATE_GOT_STATUS; 5636 scsipkt->pkt_reason = CMD_CMPLT; 5637 *scsipkt->pkt_scbp = STATUS_GOOD; 5638 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5639 /* scsi callback required - have to schedule it */ 5640 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5641 (task_func_t *)scsipkt->pkt_comp, 5642 (void *)scsipkt, TQ_SLEEP) == NULL) 5643 /* Scheduling the callback failed */ 5644 return (TRAN_BUSY); 5645 } 5646 return (TRAN_ACCEPT); 5647 } 5648 5649 5650 /* 5651 * Translate completion status of SATA read/write commands into scsi response. 5652 * pkt completion_reason is checked to determine the completion status. 5653 * Do scsi callback if necessary. 5654 * 5655 * Note: this function may be called also for synchronously executed 5656 * commands. 5657 * This function may be used only if scsi_pkt is non-NULL. 5658 */ 5659 static void 5660 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 5661 { 5662 sata_pkt_txlate_t *spx = 5663 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5664 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 5665 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5666 struct scsi_extended_sense *sense; 5667 uint64_t lba; 5668 struct buf *bp; 5669 int rval; 5670 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5671 /* Normal completion */ 5672 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5673 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5674 scsipkt->pkt_reason = CMD_CMPLT; 5675 *scsipkt->pkt_scbp = STATUS_GOOD; 5676 if (spx->txlt_tmp_buf != NULL) { 5677 /* Temporary buffer was used */ 5678 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5679 if (bp->b_flags & B_READ) { 5680 rval = ddi_dma_sync( 5681 spx->txlt_buf_dma_handle, 0, 0, 5682 DDI_DMA_SYNC_FORCPU); 5683 ASSERT(rval == DDI_SUCCESS); 5684 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 5685 bp->b_bcount); 5686 } 5687 } 5688 } else { 5689 /* 5690 * Something went wrong - analyze return 5691 */ 5692 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5693 STATE_SENT_CMD | STATE_GOT_STATUS; 5694 scsipkt->pkt_reason = CMD_INCOMPLETE; 5695 *scsipkt->pkt_scbp = STATUS_CHECK; 5696 sense = sata_arq_sense(spx); 5697 ASSERT(sense != NULL); 5698 5699 /* 5700 * SATA_PKT_DEV_ERROR is the only case where we may be able to 5701 * extract from device registers the failing LBA. 5702 */ 5703 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 5704 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 5705 (scmd->satacmd_lba_mid_msb != 0 || 5706 scmd->satacmd_lba_high_msb != 0)) { 5707 /* 5708 * We have problem reporting this cmd LBA 5709 * in fixed sense data format, because of 5710 * the size of the scsi LBA fields. 5711 */ 5712 sense->es_valid = 0; 5713 } else { 5714 sata_extract_error_lba(spx, &lba); 5715 sense->es_info_1 = (lba & 0xFF000000) >> 24; 5716 sense->es_info_2 = (lba & 0xFF0000) >> 16; 5717 sense->es_info_3 = (lba & 0xFF00) >> 8; 5718 sense->es_info_4 = lba & 0xFF; 5719 } 5720 } else { 5721 /* Invalid extended sense info */ 5722 sense->es_valid = 0; 5723 } 5724 5725 switch (sata_pkt->satapkt_reason) { 5726 case SATA_PKT_PORT_ERROR: 5727 /* We may want to handle DEV GONE state as well */ 5728 /* 5729 * We have no device data. Assume no data transfered. 5730 */ 5731 sense->es_key = KEY_HARDWARE_ERROR; 5732 break; 5733 5734 case SATA_PKT_DEV_ERROR: 5735 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5736 SATA_STATUS_ERR) { 5737 /* 5738 * determine dev error reason from error 5739 * reg content 5740 */ 5741 sata_decode_device_error(spx, sense); 5742 if (sense->es_key == KEY_MEDIUM_ERROR) { 5743 switch (scmd->satacmd_cmd_reg) { 5744 case SATAC_READ_DMA: 5745 case SATAC_READ_DMA_EXT: 5746 case SATAC_READ_DMA_QUEUED: 5747 case SATAC_READ_DMA_QUEUED_EXT: 5748 case SATAC_READ_FPDMA_QUEUED: 5749 /* Unrecovered read error */ 5750 sense->es_add_code = 5751 SD_SCSI_ASC_UNREC_READ_ERR; 5752 break; 5753 case SATAC_WRITE_DMA: 5754 case SATAC_WRITE_DMA_EXT: 5755 case SATAC_WRITE_DMA_QUEUED: 5756 case SATAC_WRITE_DMA_QUEUED_EXT: 5757 case SATAC_WRITE_FPDMA_QUEUED: 5758 /* Write error */ 5759 sense->es_add_code = 5760 SD_SCSI_ASC_WRITE_ERR; 5761 break; 5762 default: 5763 /* Internal error */ 5764 SATA_LOG_D(( 5765 spx->txlt_sata_hba_inst, 5766 CE_WARN, 5767 "sata_txlt_rw_completion :" 5768 "internal error - invalid " 5769 "command 0x%2x", 5770 scmd->satacmd_cmd_reg)); 5771 break; 5772 } 5773 } 5774 break; 5775 } 5776 /* No extended sense key - no info available */ 5777 scsipkt->pkt_reason = CMD_INCOMPLETE; 5778 break; 5779 5780 case SATA_PKT_TIMEOUT: 5781 scsipkt->pkt_reason = CMD_TIMEOUT; 5782 scsipkt->pkt_statistics |= 5783 STAT_TIMEOUT | STAT_DEV_RESET; 5784 sense->es_key = KEY_ABORTED_COMMAND; 5785 break; 5786 5787 case SATA_PKT_ABORTED: 5788 scsipkt->pkt_reason = CMD_ABORTED; 5789 scsipkt->pkt_statistics |= STAT_ABORTED; 5790 sense->es_key = KEY_ABORTED_COMMAND; 5791 break; 5792 5793 case SATA_PKT_RESET: 5794 scsipkt->pkt_reason = CMD_RESET; 5795 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5796 sense->es_key = KEY_ABORTED_COMMAND; 5797 break; 5798 5799 default: 5800 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5801 "sata_txlt_rw_completion: " 5802 "invalid packet completion reason")); 5803 scsipkt->pkt_reason = CMD_TRAN_ERR; 5804 break; 5805 } 5806 } 5807 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5808 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5809 5810 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5811 scsipkt->pkt_comp != NULL) 5812 /* scsi callback required */ 5813 (*scsipkt->pkt_comp)(scsipkt); 5814 } 5815 5816 5817 /* 5818 * Translate completion status of non-data commands (i.e. commands returning 5819 * no data). 5820 * pkt completion_reason is checked to determine the completion status. 5821 * Do scsi callback if necessary (FLAG_NOINTR == 0) 5822 * 5823 * Note: this function may be called also for synchronously executed 5824 * commands. 5825 * This function may be used only if scsi_pkt is non-NULL. 5826 */ 5827 5828 static void 5829 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 5830 { 5831 sata_pkt_txlate_t *spx = 5832 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5833 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5834 struct scsi_extended_sense *sense; 5835 5836 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5837 STATE_SENT_CMD | STATE_GOT_STATUS; 5838 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5839 /* Normal completion */ 5840 scsipkt->pkt_reason = CMD_CMPLT; 5841 *scsipkt->pkt_scbp = STATUS_GOOD; 5842 } else { 5843 /* Something went wrong */ 5844 scsipkt->pkt_reason = CMD_INCOMPLETE; 5845 *scsipkt->pkt_scbp = STATUS_CHECK; 5846 sense = sata_arq_sense(spx); 5847 switch (sata_pkt->satapkt_reason) { 5848 case SATA_PKT_PORT_ERROR: 5849 /* 5850 * We have no device data. Assume no data transfered. 5851 */ 5852 sense->es_key = KEY_HARDWARE_ERROR; 5853 break; 5854 5855 case SATA_PKT_DEV_ERROR: 5856 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5857 SATA_STATUS_ERR) { 5858 /* 5859 * determine dev error reason from error 5860 * reg content 5861 */ 5862 sata_decode_device_error(spx, sense); 5863 break; 5864 } 5865 /* No extended sense key - no info available */ 5866 break; 5867 5868 case SATA_PKT_TIMEOUT: 5869 scsipkt->pkt_reason = CMD_TIMEOUT; 5870 scsipkt->pkt_statistics |= 5871 STAT_TIMEOUT | STAT_DEV_RESET; 5872 /* No extended sense key ? */ 5873 break; 5874 5875 case SATA_PKT_ABORTED: 5876 scsipkt->pkt_reason = CMD_ABORTED; 5877 scsipkt->pkt_statistics |= STAT_ABORTED; 5878 /* No extended sense key ? */ 5879 break; 5880 5881 case SATA_PKT_RESET: 5882 /* pkt aborted by an explicit reset from a host */ 5883 scsipkt->pkt_reason = CMD_RESET; 5884 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5885 break; 5886 5887 default: 5888 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5889 "sata_txlt_nodata_cmd_completion: " 5890 "invalid packet completion reason %d", 5891 sata_pkt->satapkt_reason)); 5892 scsipkt->pkt_reason = CMD_TRAN_ERR; 5893 break; 5894 } 5895 5896 } 5897 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5898 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5899 5900 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5901 scsipkt->pkt_comp != NULL) 5902 /* scsi callback required */ 5903 (*scsipkt->pkt_comp)(scsipkt); 5904 } 5905 5906 5907 /* 5908 * Build Mode sense R/W recovery page 5909 * NOT IMPLEMENTED 5910 */ 5911 5912 static int 5913 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5914 { 5915 #ifndef __lock_lint 5916 _NOTE(ARGUNUSED(sdinfo)) 5917 _NOTE(ARGUNUSED(pcntrl)) 5918 _NOTE(ARGUNUSED(buf)) 5919 #endif 5920 return (0); 5921 } 5922 5923 /* 5924 * Build Mode sense caching page - scsi-3 implementation. 5925 * Page length distinguishes previous format from scsi-3 format. 5926 * buf must have space for 0x12 bytes. 5927 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 5928 * 5929 */ 5930 static int 5931 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5932 { 5933 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 5934 sata_id_t *sata_id = &sdinfo->satadrv_id; 5935 5936 /* 5937 * Most of the fields are set to 0, being not supported and/or disabled 5938 */ 5939 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 5940 5941 /* Saved paramters not supported */ 5942 if (pcntrl == 3) 5943 return (0); 5944 if (pcntrl == 0 || pcntrl == 2) { 5945 /* 5946 * For now treat current and default parameters as same 5947 * That may have to change, if target driver will complain 5948 */ 5949 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 5950 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5951 5952 if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 5953 !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) { 5954 page->dra = 1; /* Read Ahead disabled */ 5955 page->rcd = 1; /* Read Cache disabled */ 5956 } 5957 if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) && 5958 (sata_id->ai_features85 & SATA_WRITE_CACHE)) 5959 page->wce = 1; /* Write Cache enabled */ 5960 } else { 5961 /* Changeable parameters */ 5962 page->mode_page.code = MODEPAGE_CACHING; 5963 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5964 if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) { 5965 page->dra = 1; 5966 page->rcd = 1; 5967 } 5968 if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE) 5969 page->wce = 1; 5970 } 5971 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 5972 sizeof (struct mode_page)); 5973 } 5974 5975 /* 5976 * Build Mode sense exception cntrl page 5977 */ 5978 static int 5979 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5980 { 5981 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 5982 sata_id_t *sata_id = &sdinfo->satadrv_id; 5983 5984 /* 5985 * Most of the fields are set to 0, being not supported and/or disabled 5986 */ 5987 bzero(buf, PAGELENGTH_INFO_EXCPT); 5988 5989 page->mode_page.code = MODEPAGE_INFO_EXCPT; 5990 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 5991 5992 /* Indicate that this is page is saveable */ 5993 page->mode_page.ps = 1; 5994 5995 /* 5996 * We will return the same data for default, current and saved page. 5997 * The only changeable bit is dexcpt and that bit is required 5998 * by the ATA specification to be preserved across power cycles. 5999 */ 6000 if (pcntrl != 1) { 6001 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6002 page->mrie = MRIE_ONLY_ON_REQUEST; 6003 } 6004 else 6005 page->dexcpt = 1; /* Only changeable parameter */ 6006 6007 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page)); 6008 } 6009 6010 6011 static int 6012 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6013 { 6014 struct mode_acoustic_management *page = 6015 (struct mode_acoustic_management *)buf; 6016 sata_id_t *sata_id = &sdinfo->satadrv_id; 6017 6018 /* 6019 * Most of the fields are set to 0, being not supported and/or disabled 6020 */ 6021 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 6022 6023 switch (pcntrl) { 6024 case P_CNTRL_DEFAULT: 6025 /* default paramters not supported */ 6026 return (0); 6027 6028 case P_CNTRL_CURRENT: 6029 case P_CNTRL_SAVED: 6030 /* Saved and current are supported and are identical */ 6031 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6032 page->mode_page.length = 6033 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6034 page->mode_page.ps = 1; 6035 6036 /* Word 83 indicates if feature is supported */ 6037 /* If feature is not supported */ 6038 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 6039 page->acoustic_manag_enable = 6040 ACOUSTIC_DISABLED; 6041 } else { 6042 page->acoustic_manag_enable = 6043 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 6044 != 0); 6045 /* Word 94 inidicates the value */ 6046 #ifdef _LITTLE_ENDIAN 6047 page->acoustic_manag_level = 6048 (uchar_t)sata_id->ai_acoustic; 6049 page->vendor_recommended_value = 6050 sata_id->ai_acoustic >> 8; 6051 #else 6052 page->acoustic_manag_level = 6053 sata_id->ai_acoustic >> 8; 6054 page->vendor_recommended_value = 6055 (uchar_t)sata_id->ai_acoustic; 6056 #endif 6057 } 6058 break; 6059 6060 case P_CNTRL_CHANGEABLE: 6061 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6062 page->mode_page.length = 6063 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6064 page->mode_page.ps = 1; 6065 6066 /* Word 83 indicates if the feature is supported */ 6067 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 6068 page->acoustic_manag_enable = 6069 ACOUSTIC_ENABLED; 6070 page->acoustic_manag_level = 0xff; 6071 } 6072 break; 6073 } 6074 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6075 sizeof (struct mode_page)); 6076 } 6077 6078 6079 /* 6080 * Build Mode sense power condition page 6081 * NOT IMPLEMENTED. 6082 */ 6083 static int 6084 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6085 { 6086 #ifndef __lock_lint 6087 _NOTE(ARGUNUSED(sdinfo)) 6088 _NOTE(ARGUNUSED(pcntrl)) 6089 _NOTE(ARGUNUSED(buf)) 6090 #endif 6091 return (0); 6092 } 6093 6094 6095 /* 6096 * Process mode select caching page 8 (scsi3 format only). 6097 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6098 * if these features are supported by the device. If these features are not 6099 * supported, quietly ignore them. 6100 * This function fails only if the SET FEATURE command sent to 6101 * the device fails. The page format is not varified, assuming that the 6102 * target driver operates correctly - if parameters length is too short, 6103 * we just drop the page. 6104 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6105 * setting have to be changed. 6106 * SET FEATURE command is executed synchronously, i.e. we wait here until 6107 * it is completed, regardless of the scsi pkt directives. 6108 * 6109 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6110 * changing DRA will change RCD. 6111 * 6112 * More than one SATA command may be executed to perform operations specified 6113 * by mode select pages. The first error terminates further execution. 6114 * Operations performed successully are not backed-up in such case. 6115 * 6116 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6117 * If operation resulted in changing device setup, dmod flag should be set to 6118 * one (1). If parameters were not changed, dmod flag should be set to 0. 6119 * Upon return, if operation required sending command to the device, the rval 6120 * should be set to the value returned by sata_hba_start. If operation 6121 * did not require device access, rval should be set to TRAN_ACCEPT. 6122 * The pagelen should be set to the length of the page. 6123 * 6124 * This function has to be called with a port mutex held. 6125 * 6126 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6127 */ 6128 int 6129 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6130 int parmlen, int *pagelen, int *rval, int *dmod) 6131 { 6132 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6133 sata_drive_info_t *sdinfo; 6134 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6135 sata_id_t *sata_id; 6136 struct scsi_extended_sense *sense; 6137 int wce, dra; /* Current settings */ 6138 6139 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6140 &spx->txlt_sata_pkt->satapkt_device); 6141 sata_id = &sdinfo->satadrv_id; 6142 *dmod = 0; 6143 6144 /* Verify parameters length. If too short, drop it */ 6145 if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6146 sizeof (struct mode_page) < parmlen) { 6147 *scsipkt->pkt_scbp = STATUS_CHECK; 6148 sense = sata_arq_sense(spx); 6149 sense->es_key = KEY_ILLEGAL_REQUEST; 6150 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6151 *pagelen = parmlen; 6152 *rval = TRAN_ACCEPT; 6153 return (SATA_FAILURE); 6154 } 6155 6156 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6157 6158 /* 6159 * We can manipulate only write cache and read ahead 6160 * (read cache) setting. 6161 */ 6162 if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 6163 !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) { 6164 /* 6165 * None of the features is supported - ignore 6166 */ 6167 *rval = TRAN_ACCEPT; 6168 return (SATA_SUCCESS); 6169 } 6170 6171 /* Current setting of Read Ahead (and Read Cache) */ 6172 if (sata_id->ai_features85 & SATA_LOOK_AHEAD) 6173 dra = 0; /* 0 == not disabled */ 6174 else 6175 dra = 1; 6176 /* Current setting of Write Cache */ 6177 if (sata_id->ai_features85 & SATA_WRITE_CACHE) 6178 wce = 1; 6179 else 6180 wce = 0; 6181 6182 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 6183 /* nothing to do */ 6184 *rval = TRAN_ACCEPT; 6185 return (SATA_SUCCESS); 6186 } 6187 6188 /* 6189 * Need to flip some setting 6190 * Set-up Internal SET FEATURES command(s) 6191 */ 6192 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6193 scmd->satacmd_addr_type = 0; 6194 scmd->satacmd_device_reg = 0; 6195 scmd->satacmd_status_reg = 0; 6196 scmd->satacmd_error_reg = 0; 6197 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6198 if (page->dra != dra || page->rcd != dra) { 6199 /* Need to flip read ahead setting */ 6200 if (dra == 0) 6201 /* Disable read ahead / read cache */ 6202 scmd->satacmd_features_reg = 6203 SATAC_SF_DISABLE_READ_AHEAD; 6204 else 6205 /* Enable read ahead / read cache */ 6206 scmd->satacmd_features_reg = 6207 SATAC_SF_ENABLE_READ_AHEAD; 6208 6209 /* Transfer command to HBA */ 6210 if (sata_hba_start(spx, rval) != 0) 6211 /* 6212 * Pkt not accepted for execution. 6213 */ 6214 return (SATA_FAILURE); 6215 6216 *dmod = 1; 6217 6218 /* Now process return */ 6219 if (spx->txlt_sata_pkt->satapkt_reason != 6220 SATA_PKT_COMPLETED) { 6221 goto failure; /* Terminate */ 6222 } 6223 } 6224 6225 /* Note that the packet is not removed, so it could be re-used */ 6226 if (page->wce != wce) { 6227 /* Need to flip Write Cache setting */ 6228 if (page->wce == 1) 6229 /* Enable write cache */ 6230 scmd->satacmd_features_reg = 6231 SATAC_SF_ENABLE_WRITE_CACHE; 6232 else 6233 /* Disable write cache */ 6234 scmd->satacmd_features_reg = 6235 SATAC_SF_DISABLE_WRITE_CACHE; 6236 6237 /* Transfer command to HBA */ 6238 if (sata_hba_start(spx, rval) != 0) 6239 /* 6240 * Pkt not accepted for execution. 6241 */ 6242 return (SATA_FAILURE); 6243 6244 *dmod = 1; 6245 6246 /* Now process return */ 6247 if (spx->txlt_sata_pkt->satapkt_reason != 6248 SATA_PKT_COMPLETED) { 6249 goto failure; 6250 } 6251 } 6252 return (SATA_SUCCESS); 6253 6254 failure: 6255 sata_xlate_errors(spx); 6256 6257 return (SATA_FAILURE); 6258 } 6259 6260 /* 6261 * Process mode select informational exceptions control page 0x1c 6262 * 6263 * The only changeable bit is dexcpt (disable exceptions). 6264 * MRIE (method of reporting informational exceptions) must be 6265 * "only on request". 6266 * This page applies to informational exceptions that report 6267 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 6268 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 6269 * Informational exception conditions occur as the result of background scan 6270 * errors, background self-test errors, or vendor specific events within a 6271 * logical unit. An informational exception condition may occur asynchronous 6272 * to any commands. 6273 * 6274 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6275 * If operation resulted in changing device setup, dmod flag should be set to 6276 * one (1). If parameters were not changed, dmod flag should be set to 0. 6277 * Upon return, if operation required sending command to the device, the rval 6278 * should be set to the value returned by sata_hba_start. If operation 6279 * did not require device access, rval should be set to TRAN_ACCEPT. 6280 * The pagelen should be set to the length of the page. 6281 * 6282 * This function has to be called with a port mutex held. 6283 * 6284 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6285 * 6286 * Cannot be called in the interrupt context. 6287 */ 6288 static int 6289 sata_mode_select_page_1c( 6290 sata_pkt_txlate_t *spx, 6291 struct mode_info_excpt_page *page, 6292 int parmlen, 6293 int *pagelen, 6294 int *rval, 6295 int *dmod) 6296 { 6297 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6298 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6299 sata_drive_info_t *sdinfo; 6300 sata_id_t *sata_id; 6301 struct scsi_extended_sense *sense; 6302 6303 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6304 &spx->txlt_sata_pkt->satapkt_device); 6305 sata_id = &sdinfo->satadrv_id; 6306 6307 *dmod = 0; 6308 6309 /* Verify parameters length. If too short, drop it */ 6310 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) || 6311 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 6312 *scsipkt->pkt_scbp = STATUS_CHECK; 6313 sense = sata_arq_sense(spx); 6314 sense->es_key = KEY_ILLEGAL_REQUEST; 6315 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6316 *pagelen = parmlen; 6317 *rval = TRAN_ACCEPT; 6318 return (SATA_FAILURE); 6319 } 6320 6321 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 6322 6323 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6324 *scsipkt->pkt_scbp = STATUS_CHECK; 6325 sense = sata_arq_sense(spx); 6326 sense->es_key = KEY_ILLEGAL_REQUEST; 6327 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6328 *pagelen = parmlen; 6329 *rval = TRAN_ACCEPT; 6330 return (SATA_FAILURE); 6331 } 6332 6333 /* If already in the state requested, we are done */ 6334 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6335 /* nothing to do */ 6336 *rval = TRAN_ACCEPT; 6337 return (SATA_SUCCESS); 6338 } 6339 6340 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6341 6342 /* Build SMART_ENABLE or SMART_DISABLE command */ 6343 scmd->satacmd_addr_type = 0; /* N/A */ 6344 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 6345 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 6346 scmd->satacmd_features_reg = page->dexcpt ? 6347 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 6348 scmd->satacmd_device_reg = 0; /* Always device 0 */ 6349 scmd->satacmd_cmd_reg = SATAC_SMART; 6350 6351 /* Transfer command to HBA */ 6352 if (sata_hba_start(spx, rval) != 0) 6353 /* 6354 * Pkt not accepted for execution. 6355 */ 6356 return (SATA_FAILURE); 6357 6358 *dmod = 1; /* At least may have been modified */ 6359 6360 /* Now process return */ 6361 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 6362 return (SATA_SUCCESS); 6363 6364 /* Packet did not complete successfully */ 6365 sata_xlate_errors(spx); 6366 6367 return (SATA_FAILURE); 6368 } 6369 6370 /* 6371 * Process mode select acoustic management control page 0x30 6372 * 6373 * 6374 * This function has to be called with a port mutex held. 6375 * 6376 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6377 * 6378 * Cannot be called in the interrupt context. 6379 */ 6380 int 6381 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 6382 mode_acoustic_management *page, int parmlen, int *pagelen, 6383 int *rval, int *dmod) 6384 { 6385 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6386 sata_drive_info_t *sdinfo; 6387 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6388 sata_id_t *sata_id; 6389 struct scsi_extended_sense *sense; 6390 6391 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6392 &spx->txlt_sata_pkt->satapkt_device); 6393 sata_id = &sdinfo->satadrv_id; 6394 *dmod = 0; 6395 6396 /* If parmlen is too short or the feature is not supported, drop it */ 6397 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6398 sizeof (struct mode_page)) < parmlen) || 6399 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 6400 *scsipkt->pkt_scbp = STATUS_CHECK; 6401 sense = sata_arq_sense(spx); 6402 sense->es_key = KEY_ILLEGAL_REQUEST; 6403 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6404 *pagelen = parmlen; 6405 *rval = TRAN_ACCEPT; 6406 return (SATA_FAILURE); 6407 } 6408 6409 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6410 sizeof (struct mode_page); 6411 6412 /* 6413 * We can enable and disable acoustice management and 6414 * set the acoustic management level. 6415 */ 6416 6417 /* 6418 * Set-up Internal SET FEATURES command(s) 6419 */ 6420 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6421 scmd->satacmd_addr_type = 0; 6422 scmd->satacmd_device_reg = 0; 6423 scmd->satacmd_status_reg = 0; 6424 scmd->satacmd_error_reg = 0; 6425 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6426 if (page->acoustic_manag_enable) { 6427 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 6428 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 6429 } else { /* disabling acoustic management */ 6430 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 6431 } 6432 6433 /* Transfer command to HBA */ 6434 if (sata_hba_start(spx, rval) != 0) 6435 /* 6436 * Pkt not accepted for execution. 6437 */ 6438 return (SATA_FAILURE); 6439 6440 /* Now process return */ 6441 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 6442 sata_xlate_errors(spx); 6443 return (SATA_FAILURE); 6444 } 6445 6446 *dmod = 1; 6447 6448 return (SATA_SUCCESS); 6449 } 6450 6451 6452 6453 6454 /* 6455 * sata_build_lsense_page0() is used to create the 6456 * SCSI LOG SENSE page 0 (supported log pages) 6457 * 6458 * Currently supported pages are 0, 0x10, 0x2f and 0x30 6459 * (supported log pages, self-test results, informational exceptions 6460 * and Sun vendor specific ATA SMART data). 6461 * 6462 * Takes a sata_drive_info t * and the address of a buffer 6463 * in which to create the page information. 6464 * 6465 * Returns the number of bytes valid in the buffer. 6466 */ 6467 static int 6468 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 6469 { 6470 struct log_parameter *lpp = (struct log_parameter *)buf; 6471 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 6472 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 6473 sata_id_t *sata_id = &sdinfo->satadrv_id; 6474 6475 lpp->param_code[0] = 0; 6476 lpp->param_code[1] = 0; 6477 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6478 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 6479 6480 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 6481 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 6482 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 6483 ++num_pages_supported; 6484 } 6485 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 6486 ++num_pages_supported; 6487 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 6488 ++num_pages_supported; 6489 } 6490 6491 lpp->param_len = num_pages_supported; 6492 6493 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 6494 num_pages_supported); 6495 } 6496 6497 /* 6498 * sata_build_lsense_page_10() is used to create the 6499 * SCSI LOG SENSE page 0x10 (self-test results) 6500 * 6501 * Takes a sata_drive_info t * and the address of a buffer 6502 * in which to create the page information as well as a sata_hba_inst_t *. 6503 * 6504 * Returns the number of bytes valid in the buffer. 6505 * 6506 * Note: Self test and SMART data is accessible in device log pages. 6507 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 6508 * of data can be transferred by a single command), or by the General Purpose 6509 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 6510 * - approximately 33MB - can be transferred by a single command. 6511 * The SCT Command response (either error or command) is the same for both 6512 * the SMART and GPL methods of issuing commands. 6513 * This function uses READ LOG EXT command when drive supports LBA48, and 6514 * SMART READ command otherwise. 6515 * 6516 * Since above commands are executed in a synchronous mode, this function 6517 * should not be called in an interrupt context. 6518 */ 6519 static int 6520 sata_build_lsense_page_10( 6521 sata_drive_info_t *sdinfo, 6522 uint8_t *buf, 6523 sata_hba_inst_t *sata_hba_inst) 6524 { 6525 struct log_parameter *lpp = (struct log_parameter *)buf; 6526 int rval; 6527 6528 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6529 struct smart_ext_selftest_log *ext_selftest_log; 6530 6531 ext_selftest_log = kmem_zalloc( 6532 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 6533 6534 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 6535 ext_selftest_log, 0); 6536 if (rval == 0) { 6537 int index, start_index; 6538 struct smart_ext_selftest_log_entry *entry; 6539 static const struct smart_ext_selftest_log_entry empty = 6540 {0}; 6541 uint16_t block_num; 6542 int count; 6543 boolean_t only_one_block = B_FALSE; 6544 6545 index = ext_selftest_log-> 6546 smart_ext_selftest_log_index[0]; 6547 index |= ext_selftest_log-> 6548 smart_ext_selftest_log_index[1] << 8; 6549 if (index == 0) 6550 goto out; 6551 6552 --index; /* Correct for 0 origin */ 6553 start_index = index; /* remember where we started */ 6554 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6555 if (block_num != 0) { 6556 rval = sata_ext_smart_selftest_read_log( 6557 sata_hba_inst, sdinfo, ext_selftest_log, 6558 block_num); 6559 if (rval != 0) 6560 goto out; 6561 } 6562 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6563 entry = 6564 &ext_selftest_log-> 6565 smart_ext_selftest_log_entries[index]; 6566 6567 for (count = 1; 6568 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6569 ++count) { 6570 uint8_t status; 6571 uint8_t code; 6572 uint8_t sense_key; 6573 uint8_t add_sense_code; 6574 uint8_t add_sense_code_qual; 6575 6576 /* If this is an unused entry, we are done */ 6577 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 6578 /* Broken firmware on some disks */ 6579 if (index + 1 == 6580 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 6581 --entry; 6582 --index; 6583 if (bcmp(entry, &empty, 6584 sizeof (empty)) == 0) 6585 goto out; 6586 } else 6587 goto out; 6588 } 6589 6590 if (only_one_block && 6591 start_index == index) 6592 goto out; 6593 6594 lpp->param_code[0] = 0; 6595 lpp->param_code[1] = count; 6596 lpp->param_ctrl_flags = 6597 LOG_CTRL_LP | LOG_CTRL_LBIN; 6598 lpp->param_len = 6599 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6600 6601 status = entry->smart_ext_selftest_log_status; 6602 status >>= 4; 6603 switch (status) { 6604 case 0: 6605 default: 6606 sense_key = KEY_NO_SENSE; 6607 add_sense_code = 6608 SD_SCSI_ASC_NO_ADD_SENSE; 6609 add_sense_code_qual = 0; 6610 break; 6611 case 1: 6612 sense_key = KEY_ABORTED_COMMAND; 6613 add_sense_code = 6614 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6615 add_sense_code_qual = SCSI_COMPONENT_81; 6616 break; 6617 case 2: 6618 sense_key = KEY_ABORTED_COMMAND; 6619 add_sense_code = 6620 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6621 add_sense_code_qual = SCSI_COMPONENT_82; 6622 break; 6623 case 3: 6624 sense_key = KEY_ABORTED_COMMAND; 6625 add_sense_code = 6626 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6627 add_sense_code_qual = SCSI_COMPONENT_83; 6628 break; 6629 case 4: 6630 sense_key = KEY_HARDWARE_ERROR; 6631 add_sense_code = 6632 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6633 add_sense_code_qual = SCSI_COMPONENT_84; 6634 break; 6635 case 5: 6636 sense_key = KEY_HARDWARE_ERROR; 6637 add_sense_code = 6638 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6639 add_sense_code_qual = SCSI_COMPONENT_85; 6640 break; 6641 case 6: 6642 sense_key = KEY_HARDWARE_ERROR; 6643 add_sense_code = 6644 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6645 add_sense_code_qual = SCSI_COMPONENT_86; 6646 break; 6647 case 7: 6648 sense_key = KEY_MEDIUM_ERROR; 6649 add_sense_code = 6650 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6651 add_sense_code_qual = SCSI_COMPONENT_87; 6652 break; 6653 case 8: 6654 sense_key = KEY_HARDWARE_ERROR; 6655 add_sense_code = 6656 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6657 add_sense_code_qual = SCSI_COMPONENT_88; 6658 break; 6659 } 6660 code = 0; /* unspecified */ 6661 status |= (code << 4); 6662 lpp->param_values[0] = status; 6663 lpp->param_values[1] = 0; /* unspecified */ 6664 lpp->param_values[2] = entry-> 6665 smart_ext_selftest_log_timestamp[1]; 6666 lpp->param_values[3] = entry-> 6667 smart_ext_selftest_log_timestamp[0]; 6668 if (status != 0) { 6669 lpp->param_values[4] = 0; 6670 lpp->param_values[5] = 0; 6671 lpp->param_values[6] = entry-> 6672 smart_ext_selftest_log_failing_lba 6673 [5]; 6674 lpp->param_values[7] = entry-> 6675 smart_ext_selftest_log_failing_lba 6676 [4]; 6677 lpp->param_values[8] = entry-> 6678 smart_ext_selftest_log_failing_lba 6679 [3]; 6680 lpp->param_values[9] = entry-> 6681 smart_ext_selftest_log_failing_lba 6682 [2]; 6683 lpp->param_values[10] = entry-> 6684 smart_ext_selftest_log_failing_lba 6685 [1]; 6686 lpp->param_values[11] = entry-> 6687 smart_ext_selftest_log_failing_lba 6688 [0]; 6689 } else { /* No bad block address */ 6690 lpp->param_values[4] = 0xff; 6691 lpp->param_values[5] = 0xff; 6692 lpp->param_values[6] = 0xff; 6693 lpp->param_values[7] = 0xff; 6694 lpp->param_values[8] = 0xff; 6695 lpp->param_values[9] = 0xff; 6696 lpp->param_values[10] = 0xff; 6697 lpp->param_values[11] = 0xff; 6698 } 6699 6700 lpp->param_values[12] = sense_key; 6701 lpp->param_values[13] = add_sense_code; 6702 lpp->param_values[14] = add_sense_code_qual; 6703 lpp->param_values[15] = 0; /* undefined */ 6704 6705 lpp = (struct log_parameter *) 6706 (((uint8_t *)lpp) + 6707 SCSI_LOG_PARAM_HDR_LEN + 6708 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6709 6710 --index; /* Back up to previous entry */ 6711 if (index < 0) { 6712 if (block_num > 0) { 6713 --block_num; 6714 } else { 6715 struct read_log_ext_directory 6716 logdir; 6717 6718 rval = 6719 sata_read_log_ext_directory( 6720 sata_hba_inst, sdinfo, 6721 &logdir); 6722 if (rval == -1) 6723 goto out; 6724 if ((logdir.read_log_ext_vers 6725 [0] == 0) && 6726 (logdir.read_log_ext_vers 6727 [1] == 0)) 6728 goto out; 6729 block_num = 6730 logdir.read_log_ext_nblks 6731 [EXT_SMART_SELFTEST_LOG_PAGE 6732 - 1][0]; 6733 block_num |= logdir. 6734 read_log_ext_nblks 6735 [EXT_SMART_SELFTEST_LOG_PAGE 6736 - 1][1] << 8; 6737 --block_num; 6738 only_one_block = 6739 (block_num == 0); 6740 } 6741 rval = sata_ext_smart_selftest_read_log( 6742 sata_hba_inst, sdinfo, 6743 ext_selftest_log, block_num); 6744 if (rval != 0) 6745 goto out; 6746 6747 index = 6748 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 6749 1; 6750 } 6751 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6752 entry = &ext_selftest_log-> 6753 smart_ext_selftest_log_entries[index]; 6754 } 6755 } 6756 out: 6757 kmem_free(ext_selftest_log, 6758 sizeof (struct smart_ext_selftest_log)); 6759 } else { 6760 struct smart_selftest_log *selftest_log; 6761 6762 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 6763 KM_SLEEP); 6764 6765 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 6766 selftest_log); 6767 6768 if (rval == 0) { 6769 int index; 6770 int count; 6771 struct smart_selftest_log_entry *entry; 6772 static const struct smart_selftest_log_entry empty = 6773 { 0 }; 6774 6775 index = selftest_log->smart_selftest_log_index; 6776 if (index == 0) 6777 goto done; 6778 --index; /* Correct for 0 origin */ 6779 entry = &selftest_log-> 6780 smart_selftest_log_entries[index]; 6781 for (count = 1; 6782 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6783 ++count) { 6784 uint8_t status; 6785 uint8_t code; 6786 uint8_t sense_key; 6787 uint8_t add_sense_code; 6788 uint8_t add_sense_code_qual; 6789 6790 if (bcmp(entry, &empty, sizeof (empty)) == 0) 6791 goto done; 6792 6793 lpp->param_code[0] = 0; 6794 lpp->param_code[1] = count; 6795 lpp->param_ctrl_flags = 6796 LOG_CTRL_LP | LOG_CTRL_LBIN; 6797 lpp->param_len = 6798 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6799 6800 status = entry->smart_selftest_log_status; 6801 status >>= 4; 6802 switch (status) { 6803 case 0: 6804 default: 6805 sense_key = KEY_NO_SENSE; 6806 add_sense_code = 6807 SD_SCSI_ASC_NO_ADD_SENSE; 6808 break; 6809 case 1: 6810 sense_key = KEY_ABORTED_COMMAND; 6811 add_sense_code = 6812 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6813 add_sense_code_qual = SCSI_COMPONENT_81; 6814 break; 6815 case 2: 6816 sense_key = KEY_ABORTED_COMMAND; 6817 add_sense_code = 6818 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6819 add_sense_code_qual = SCSI_COMPONENT_82; 6820 break; 6821 case 3: 6822 sense_key = KEY_ABORTED_COMMAND; 6823 add_sense_code = 6824 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6825 add_sense_code_qual = SCSI_COMPONENT_83; 6826 break; 6827 case 4: 6828 sense_key = KEY_HARDWARE_ERROR; 6829 add_sense_code = 6830 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6831 add_sense_code_qual = SCSI_COMPONENT_84; 6832 break; 6833 case 5: 6834 sense_key = KEY_HARDWARE_ERROR; 6835 add_sense_code = 6836 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6837 add_sense_code_qual = SCSI_COMPONENT_85; 6838 break; 6839 case 6: 6840 sense_key = KEY_HARDWARE_ERROR; 6841 add_sense_code = 6842 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6843 add_sense_code_qual = SCSI_COMPONENT_86; 6844 break; 6845 case 7: 6846 sense_key = KEY_MEDIUM_ERROR; 6847 add_sense_code = 6848 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6849 add_sense_code_qual = SCSI_COMPONENT_87; 6850 break; 6851 case 8: 6852 sense_key = KEY_HARDWARE_ERROR; 6853 add_sense_code = 6854 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6855 add_sense_code_qual = SCSI_COMPONENT_88; 6856 break; 6857 } 6858 code = 0; /* unspecified */ 6859 status |= (code << 4); 6860 lpp->param_values[0] = status; 6861 lpp->param_values[1] = 0; /* unspecified */ 6862 lpp->param_values[2] = entry-> 6863 smart_selftest_log_timestamp[1]; 6864 lpp->param_values[3] = entry-> 6865 smart_selftest_log_timestamp[0]; 6866 if (status != 0) { 6867 lpp->param_values[4] = 0; 6868 lpp->param_values[5] = 0; 6869 lpp->param_values[6] = 0; 6870 lpp->param_values[7] = 0; 6871 lpp->param_values[8] = entry-> 6872 smart_selftest_log_failing_lba[3]; 6873 lpp->param_values[9] = entry-> 6874 smart_selftest_log_failing_lba[2]; 6875 lpp->param_values[10] = entry-> 6876 smart_selftest_log_failing_lba[1]; 6877 lpp->param_values[11] = entry-> 6878 smart_selftest_log_failing_lba[0]; 6879 } else { /* No block address */ 6880 lpp->param_values[4] = 0xff; 6881 lpp->param_values[5] = 0xff; 6882 lpp->param_values[6] = 0xff; 6883 lpp->param_values[7] = 0xff; 6884 lpp->param_values[8] = 0xff; 6885 lpp->param_values[9] = 0xff; 6886 lpp->param_values[10] = 0xff; 6887 lpp->param_values[11] = 0xff; 6888 } 6889 lpp->param_values[12] = sense_key; 6890 lpp->param_values[13] = add_sense_code; 6891 lpp->param_values[14] = add_sense_code_qual; 6892 lpp->param_values[15] = 0; /* undefined */ 6893 6894 lpp = (struct log_parameter *) 6895 (((uint8_t *)lpp) + 6896 SCSI_LOG_PARAM_HDR_LEN + 6897 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6898 --index; /* back up to previous entry */ 6899 if (index < 0) { 6900 index = 6901 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 6902 } 6903 entry = &selftest_log-> 6904 smart_selftest_log_entries[index]; 6905 } 6906 } 6907 done: 6908 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 6909 } 6910 6911 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 6912 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 6913 } 6914 6915 /* 6916 * sata_build_lsense_page_2f() is used to create the 6917 * SCSI LOG SENSE page 0x2f (informational exceptions) 6918 * 6919 * Takes a sata_drive_info t * and the address of a buffer 6920 * in which to create the page information as well as a sata_hba_inst_t *. 6921 * 6922 * Returns the number of bytes valid in the buffer. 6923 * 6924 * Because it invokes function(s) that send synchronously executed command 6925 * to the HBA, it cannot be called in the interrupt context. 6926 */ 6927 static int 6928 sata_build_lsense_page_2f( 6929 sata_drive_info_t *sdinfo, 6930 uint8_t *buf, 6931 sata_hba_inst_t *sata_hba_inst) 6932 { 6933 struct log_parameter *lpp = (struct log_parameter *)buf; 6934 int rval; 6935 uint8_t *smart_data; 6936 uint8_t temp; 6937 sata_id_t *sata_id; 6938 #define SMART_NO_TEMP 0xff 6939 6940 lpp->param_code[0] = 0; 6941 lpp->param_code[1] = 0; 6942 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6943 6944 /* Now get the SMART status w.r.t. threshold exceeded */ 6945 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 6946 switch (rval) { 6947 case 1: 6948 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 6949 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 6950 break; 6951 case 0: 6952 case -1: /* failed to get data */ 6953 lpp->param_values[0] = 0; /* No failure predicted */ 6954 lpp->param_values[1] = 0; 6955 break; 6956 #if defined(SATA_DEBUG) 6957 default: 6958 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 6959 /* NOTREACHED */ 6960 #endif 6961 } 6962 6963 sata_id = &sdinfo->satadrv_id; 6964 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 6965 temp = SMART_NO_TEMP; 6966 else { 6967 /* Now get the temperature */ 6968 smart_data = kmem_zalloc(512, KM_SLEEP); 6969 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 6970 SCT_STATUS_LOG_PAGE, 1); 6971 if (rval == -1) 6972 temp = SMART_NO_TEMP; 6973 else { 6974 temp = smart_data[200]; 6975 if (temp & 0x80) { 6976 if (temp & 0x7f) 6977 temp = 0; 6978 else 6979 temp = SMART_NO_TEMP; 6980 } 6981 } 6982 kmem_free(smart_data, 512); 6983 } 6984 6985 lpp->param_values[2] = temp; /* most recent temperature */ 6986 lpp->param_values[3] = 0; /* required vendor specific byte */ 6987 6988 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 6989 6990 6991 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 6992 } 6993 6994 /* 6995 * sata_build_lsense_page_30() is used to create the 6996 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 6997 * 6998 * Takes a sata_drive_info t * and the address of a buffer 6999 * in which to create the page information as well as a sata_hba_inst_t *. 7000 * 7001 * Returns the number of bytes valid in the buffer. 7002 */ 7003 static int 7004 sata_build_lsense_page_30( 7005 sata_drive_info_t *sdinfo, 7006 uint8_t *buf, 7007 sata_hba_inst_t *sata_hba_inst) 7008 { 7009 struct smart_data *smart_data = (struct smart_data *)buf; 7010 int rval; 7011 7012 /* Now do the SMART READ DATA */ 7013 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7014 if (rval == -1) 7015 return (0); 7016 7017 return (sizeof (struct smart_data)); 7018 } 7019 7020 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 7021 7022 /* 7023 * Start command for ATAPI device. 7024 * This function processes scsi_pkt requests. 7025 * Now only CD/DVD and tape devices are supported. 7026 * Most commands are packet without any translation into Packet Command. 7027 * Some may be trapped and executed as SATA commands (not clear which one). 7028 * 7029 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 7030 * execution). 7031 * Returns other TRAN_XXXX codes if command is not accepted or completed 7032 * (see return values for sata_hba_start()). 7033 * 7034 * Note: 7035 * Inquiry cdb format differs between transport version 2 and 3. 7036 * However, the transport version 3 devices that were checked did not adhere 7037 * to the specification (ignored MSB of the allocation length). Therefore, 7038 * the transport version is not checked, but Inquiry allocation length is 7039 * truncated to 255 bytes if the original allocation length set-up by the 7040 * target driver is greater than 255 bytes. 7041 */ 7042 static int 7043 sata_txlt_atapi(sata_pkt_txlate_t *spx) 7044 { 7045 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7046 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7047 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7048 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 7049 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 7050 &spx->txlt_sata_pkt->satapkt_device); 7051 int cport = SATA_TXLT_CPORT(spx); 7052 int cdblen; 7053 int rval, reason; 7054 int synch; 7055 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 7056 7057 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7058 7059 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 7060 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 7061 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7062 return (rval); 7063 } 7064 7065 /* 7066 * ATAPI device executes some ATA commands in addition to those 7067 * commands sent via PACKET command. These ATA commands may be 7068 * executed by the regular SATA translation functions. None needs 7069 * to be captured now. 7070 * 7071 * Commands sent via PACKET command include: 7072 * MMC command set for ATAPI CD/DVD device 7073 * SSC command set for ATAPI TAPE device 7074 * 7075 */ 7076 7077 /* Check the size of cdb */ 7078 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 7079 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 7080 sata_log(NULL, CE_WARN, 7081 "sata: invalid ATAPI cdb length %d", 7082 scsipkt->pkt_cdblen); 7083 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7084 return (TRAN_BADPKT); 7085 } 7086 7087 SATAATAPITRACE(spx, cdblen); 7088 7089 /* 7090 * For non-read/write commands we need to 7091 * map buffer 7092 */ 7093 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 7094 case SCMD_READ: 7095 case SCMD_READ_G1: 7096 case SCMD_READ_G5: 7097 case SCMD_READ_G4: 7098 case SCMD_WRITE: 7099 case SCMD_WRITE_G1: 7100 case SCMD_WRITE_G5: 7101 case SCMD_WRITE_G4: 7102 break; 7103 default: 7104 if (bp != NULL) { 7105 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 7106 bp_mapin(bp); 7107 } 7108 break; 7109 } 7110 /* 7111 * scmd->satacmd_flags.sata_data_direction default - 7112 * SATA_DIR_NODATA_XFER - is set by 7113 * sata_txlt_generic_pkt_info(). 7114 */ 7115 if (scmd->satacmd_bp) { 7116 if (scmd->satacmd_bp->b_flags & B_READ) { 7117 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7118 } else { 7119 scmd->satacmd_flags.sata_data_direction = 7120 SATA_DIR_WRITE; 7121 } 7122 } 7123 7124 /* 7125 * Set up ATAPI packet command. 7126 */ 7127 7128 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7129 7130 /* Copy cdb into sata_cmd */ 7131 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7132 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7133 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 7134 7135 /* See note in the command header */ 7136 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 7137 if (scmd->satacmd_acdb[3] != 0) 7138 scmd->satacmd_acdb[4] = 255; 7139 } 7140 7141 #ifdef SATA_DEBUG 7142 if (sata_debug_flags & SATA_DBG_ATAPI) { 7143 uint8_t *p = scmd->satacmd_acdb; 7144 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 7145 7146 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 7147 "%02x %02x %02x %02x %02x %02x %02x %02x " 7148 "%2x %02x %02x %02x %02x %02x %02x %02x", 7149 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7150 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7151 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 7152 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 7153 } 7154 #endif 7155 7156 /* 7157 * Preset request sense data to NO SENSE. 7158 * If there is no way to get error information via Request Sense, 7159 * the packet request sense data would not have to be modified by HBA, 7160 * but it could be returned as is. 7161 */ 7162 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7163 sata_fixed_sense_data_preset( 7164 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7165 7166 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7167 /* Need callback function */ 7168 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 7169 synch = FALSE; 7170 } else 7171 synch = TRUE; 7172 7173 /* Transfer command to HBA */ 7174 if (sata_hba_start(spx, &rval) != 0) { 7175 /* Pkt not accepted for execution */ 7176 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7177 return (rval); 7178 } 7179 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7180 /* 7181 * If execution is non-synchronous, 7182 * a callback function will handle potential errors, translate 7183 * the response and will do a callback to a target driver. 7184 * If it was synchronous, use the same framework callback to check 7185 * an execution status. 7186 */ 7187 if (synch) { 7188 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7189 "synchronous execution status %x\n", 7190 spx->txlt_sata_pkt->satapkt_reason); 7191 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 7192 } 7193 return (TRAN_ACCEPT); 7194 } 7195 7196 7197 /* 7198 * ATAPI Packet command completion. 7199 * 7200 * Failure of the command passed via Packet command are considered device 7201 * error. SATA HBA driver would have to retrieve error data (via Request 7202 * Sense command delivered via error retrieval sata packet) and copy it 7203 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 7204 */ 7205 static void 7206 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 7207 { 7208 sata_pkt_txlate_t *spx = 7209 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7210 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7211 struct scsi_extended_sense *sense; 7212 struct buf *bp; 7213 int rval; 7214 7215 #ifdef SATA_DEBUG 7216 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 7217 #endif 7218 7219 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7220 STATE_SENT_CMD | STATE_GOT_STATUS; 7221 7222 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7223 /* Normal completion */ 7224 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 7225 scsipkt->pkt_state |= STATE_XFERRED_DATA; 7226 scsipkt->pkt_reason = CMD_CMPLT; 7227 *scsipkt->pkt_scbp = STATUS_GOOD; 7228 if (spx->txlt_tmp_buf != NULL) { 7229 /* Temporary buffer was used */ 7230 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7231 if (bp->b_flags & B_READ) { 7232 rval = ddi_dma_sync( 7233 spx->txlt_buf_dma_handle, 0, 0, 7234 DDI_DMA_SYNC_FORCPU); 7235 ASSERT(rval == DDI_SUCCESS); 7236 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7237 bp->b_bcount); 7238 } 7239 } 7240 } else { 7241 /* 7242 * Something went wrong - analyze return 7243 */ 7244 *scsipkt->pkt_scbp = STATUS_CHECK; 7245 sense = sata_arq_sense(spx); 7246 7247 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7248 /* 7249 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 7250 * Under this condition ERR bit is set for ATA command, 7251 * and CHK bit set for ATAPI command. 7252 * 7253 * Please check st_intr & sdintr about how pkt_reason 7254 * is used. 7255 */ 7256 scsipkt->pkt_reason = CMD_CMPLT; 7257 7258 /* 7259 * We may not have ARQ data if there was a double 7260 * error. But sense data in sata packet was pre-set 7261 * with NO SENSE so it is valid even if HBA could 7262 * not retrieve a real sense data. 7263 * Just copy this sense data into scsi pkt sense area. 7264 */ 7265 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 7266 SATA_ATAPI_MIN_RQSENSE_LEN); 7267 #ifdef SATA_DEBUG 7268 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 7269 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7270 "sata_txlt_atapi_completion: %02x\n" 7271 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7272 " %02x %02x %02x %02x %02x %02x " 7273 " %02x %02x %02x %02x %02x %02x\n", 7274 scsipkt->pkt_reason, 7275 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7276 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7277 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7278 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7279 rqsp[16], rqsp[17]); 7280 } 7281 #endif 7282 } else { 7283 switch (sata_pkt->satapkt_reason) { 7284 case SATA_PKT_PORT_ERROR: 7285 /* 7286 * We have no device data. 7287 */ 7288 scsipkt->pkt_reason = CMD_INCOMPLETE; 7289 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7290 STATE_GOT_TARGET | STATE_SENT_CMD | 7291 STATE_GOT_STATUS); 7292 sense->es_key = KEY_HARDWARE_ERROR; 7293 break; 7294 7295 case SATA_PKT_TIMEOUT: 7296 scsipkt->pkt_reason = CMD_TIMEOUT; 7297 scsipkt->pkt_statistics |= 7298 STAT_TIMEOUT | STAT_DEV_RESET; 7299 /* 7300 * Need to check if HARDWARE_ERROR/ 7301 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 7302 * appropriate. 7303 */ 7304 break; 7305 7306 case SATA_PKT_ABORTED: 7307 scsipkt->pkt_reason = CMD_ABORTED; 7308 scsipkt->pkt_statistics |= STAT_ABORTED; 7309 /* Should we set key COMMAND_ABPRTED? */ 7310 break; 7311 7312 case SATA_PKT_RESET: 7313 scsipkt->pkt_reason = CMD_RESET; 7314 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7315 /* 7316 * May be we should set Unit Attention / 7317 * Reset. Perhaps the same should be 7318 * returned for disks.... 7319 */ 7320 sense->es_key = KEY_UNIT_ATTENTION; 7321 sense->es_add_code = SD_SCSI_ASC_RESET; 7322 break; 7323 7324 default: 7325 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7326 "sata_txlt_atapi_completion: " 7327 "invalid packet completion reason")); 7328 scsipkt->pkt_reason = CMD_TRAN_ERR; 7329 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7330 STATE_GOT_TARGET | STATE_SENT_CMD | 7331 STATE_GOT_STATUS); 7332 break; 7333 } 7334 } 7335 } 7336 7337 SATAATAPITRACE(spx, 0); 7338 7339 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7340 scsipkt->pkt_comp != NULL) { 7341 /* scsi callback required */ 7342 (*scsipkt->pkt_comp)(scsipkt); 7343 } 7344 } 7345 7346 /* 7347 * Set up error retrieval sata command for ATAPI Packet Command error data 7348 * recovery. 7349 * 7350 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 7351 * returns SATA_FAILURE otherwise. 7352 */ 7353 7354 static int 7355 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 7356 { 7357 sata_pkt_t *spkt = spx->txlt_sata_pkt; 7358 sata_cmd_t *scmd; 7359 struct buf *bp; 7360 7361 /* 7362 * Allocate dma-able buffer error data. 7363 * Buffer allocation will take care of buffer alignment and other DMA 7364 * attributes. 7365 */ 7366 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 7367 if (bp == NULL) { 7368 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 7369 "sata_get_err_retrieval_pkt: " 7370 "cannot allocate buffer for error data", NULL); 7371 return (SATA_FAILURE); 7372 } 7373 bp_mapin(bp); /* make data buffer accessible */ 7374 7375 /* Operation modes are up to the caller */ 7376 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7377 7378 /* Synchronous mode, no callback - may be changed by the caller */ 7379 spkt->satapkt_comp = NULL; 7380 spkt->satapkt_time = sata_default_pkt_time; 7381 7382 scmd = &spkt->satapkt_cmd; 7383 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7384 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7385 7386 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7387 7388 /* 7389 * Set-up acdb. Request Sense CDB (packet command content) is 7390 * not in DMA-able buffer. Its handling is HBA-specific (how 7391 * it is transfered into packet FIS). 7392 */ 7393 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7394 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 7395 /* Following zeroing of pad bytes may not be necessary */ 7396 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 7397 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 7398 7399 /* 7400 * Set-up pointer to the buffer handle, so HBA can sync buffer 7401 * before accessing it. Handle is in usual place in translate struct. 7402 */ 7403 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 7404 7405 /* 7406 * Preset request sense data to NO SENSE. 7407 * Here it is redundant, only for a symetry with scsi-originated 7408 * packets. It should not be used for anything but debugging. 7409 */ 7410 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7411 sata_fixed_sense_data_preset( 7412 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7413 7414 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7415 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7416 7417 return (SATA_SUCCESS); 7418 } 7419 7420 /* 7421 * Set-up ATAPI packet command. 7422 * Data transfer direction has to be set-up in sata_cmd structure prior to 7423 * calling this function. 7424 * 7425 * Returns void 7426 */ 7427 7428 static void 7429 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 7430 { 7431 scmd->satacmd_addr_type = 0; /* N/A */ 7432 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 7433 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 7434 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 7435 scmd->satacmd_lba_high_lsb = 7436 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 7437 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 7438 7439 /* 7440 * We want all data to be transfered via DMA. 7441 * But specify it only if drive supports DMA and DMA mode is 7442 * selected - some drives are sensitive about it. 7443 * Hopefully it wil work for all drives.... 7444 */ 7445 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 7446 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 7447 7448 /* 7449 * Features register requires special care for devices that use 7450 * Serial ATA bridge - they need an explicit specification of 7451 * the data transfer direction for Packet DMA commands. 7452 * Setting this bit is harmless if DMA is not used. 7453 * 7454 * Many drives do not implement word 80, specifying what ATA/ATAPI 7455 * spec they follow. 7456 * We are arbitrarily following the latest SerialATA 2.6 spec, 7457 * which uses ATA/ATAPI 6 specification for Identify Data, unless 7458 * ATA/ATAPI-7 support is explicitly indicated. 7459 */ 7460 if (sdinfo->satadrv_id.ai_majorversion != 0 && 7461 sdinfo->satadrv_id.ai_majorversion != 0xffff && 7462 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 7463 /* 7464 * Specification of major version is valid and version 7 7465 * is supported. It does automatically imply that all 7466 * spec features are supported. For now, we assume that 7467 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 7468 */ 7469 if ((sdinfo->satadrv_id.ai_dirdma & 7470 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 7471 if (scmd->satacmd_flags.sata_data_direction == 7472 SATA_DIR_READ) 7473 scmd->satacmd_features_reg |= 7474 SATA_ATAPI_F_DATA_DIR_READ; 7475 } 7476 } 7477 } 7478 7479 7480 #ifdef SATA_DEBUG 7481 7482 /* Display 18 bytes of Inquiry data */ 7483 static void 7484 sata_show_inqry_data(uint8_t *buf) 7485 { 7486 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 7487 uint8_t *p; 7488 7489 cmn_err(CE_NOTE, "Inquiry data:"); 7490 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 7491 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 7492 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 7493 cmn_err(CE_NOTE, "ATAPI transport version %d", 7494 SATA_ATAPI_TRANS_VERSION(inq)); 7495 cmn_err(CE_NOTE, "response data format %d, aenc %d", 7496 inq->inq_rdf, inq->inq_aenc); 7497 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 7498 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 7499 p = (uint8_t *)inq->inq_vid; 7500 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 7501 "%02x %02x %02x %02x", 7502 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7503 p = (uint8_t *)inq->inq_vid; 7504 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 7505 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7506 7507 p = (uint8_t *)inq->inq_pid; 7508 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 7509 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 7510 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7511 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7512 p = (uint8_t *)inq->inq_pid; 7513 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 7514 "%c %c %c %c %c %c %c %c", 7515 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7516 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7517 7518 p = (uint8_t *)inq->inq_revision; 7519 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 7520 p[0], p[1], p[2], p[3]); 7521 p = (uint8_t *)inq->inq_revision; 7522 cmn_err(CE_NOTE, "revision: %c %c %c %c", 7523 p[0], p[1], p[2], p[3]); 7524 7525 } 7526 7527 7528 static void 7529 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 7530 { 7531 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 7532 7533 if (scsi_pkt == NULL) 7534 return; 7535 if (count != 0) { 7536 /* saving cdb */ 7537 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 7538 SATA_ATAPI_MAX_CDB_LEN); 7539 bcopy(scsi_pkt->pkt_cdbp, 7540 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 7541 } else { 7542 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 7543 sts_sensedata, 7544 sata_atapi_trace[sata_atapi_trace_index].arqs, 7545 SATA_ATAPI_MIN_RQSENSE_LEN); 7546 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 7547 scsi_pkt->pkt_reason; 7548 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 7549 spx->txlt_sata_pkt->satapkt_reason; 7550 7551 if (++sata_atapi_trace_index >= 64) 7552 sata_atapi_trace_index = 0; 7553 } 7554 } 7555 7556 #endif 7557 7558 /* 7559 * Fetch inquiry data from ATAPI device 7560 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 7561 * 7562 * Note: 7563 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 7564 * where the caller expects to see the inquiry data. 7565 * 7566 */ 7567 7568 static int 7569 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 7570 sata_address_t *saddr, struct scsi_inquiry *inq) 7571 { 7572 sata_pkt_txlate_t *spx; 7573 sata_pkt_t *spkt; 7574 struct buf *bp; 7575 sata_drive_info_t *sdinfo; 7576 sata_cmd_t *scmd; 7577 int rval; 7578 uint8_t *rqsp; 7579 #ifdef SATA_DEBUG 7580 char msg_buf[MAXPATHLEN]; 7581 #endif 7582 7583 ASSERT(sata_hba != NULL); 7584 7585 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7586 spx->txlt_sata_hba_inst = sata_hba; 7587 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7588 spkt = sata_pkt_alloc(spx, NULL); 7589 if (spkt == NULL) { 7590 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7591 return (SATA_FAILURE); 7592 } 7593 /* address is needed now */ 7594 spkt->satapkt_device.satadev_addr = *saddr; 7595 7596 /* scsi_inquiry size buffer */ 7597 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 7598 if (bp == NULL) { 7599 sata_pkt_free(spx); 7600 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7601 SATA_LOG_D((sata_hba, CE_WARN, 7602 "sata_get_atapi_inquiry_data: " 7603 "cannot allocate data buffer")); 7604 return (SATA_FAILURE); 7605 } 7606 bp_mapin(bp); /* make data buffer accessible */ 7607 7608 scmd = &spkt->satapkt_cmd; 7609 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7610 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7611 7612 /* Use synchronous mode */ 7613 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7614 spkt->satapkt_comp = NULL; 7615 spkt->satapkt_time = sata_default_pkt_time; 7616 7617 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7618 7619 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7620 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7621 7622 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7623 sdinfo = sata_get_device_info(sata_hba, 7624 &spx->txlt_sata_pkt->satapkt_device); 7625 if (sdinfo == NULL) { 7626 /* we have to be carefull about the disapearing device */ 7627 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7628 rval = SATA_FAILURE; 7629 goto cleanup; 7630 } 7631 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7632 7633 /* 7634 * Set-up acdb. This works for atapi transport version 2 and later. 7635 */ 7636 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7637 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7638 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7639 scmd->satacmd_acdb[1] = 0x00; 7640 scmd->satacmd_acdb[2] = 0x00; 7641 scmd->satacmd_acdb[3] = 0x00; 7642 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7643 scmd->satacmd_acdb[5] = 0x00; 7644 7645 sata_fixed_sense_data_preset( 7646 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7647 7648 /* Transfer command to HBA */ 7649 if (sata_hba_start(spx, &rval) != 0) { 7650 /* Pkt not accepted for execution */ 7651 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7652 "sata_get_atapi_inquiry_data: " 7653 "Packet not accepted for execution - ret: %02x", rval); 7654 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7655 rval = SATA_FAILURE; 7656 goto cleanup; 7657 } 7658 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7659 7660 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7661 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7662 "sata_get_atapi_inquiry_data: " 7663 "Packet completed successfully - ret: %02x", rval); 7664 if (spx->txlt_buf_dma_handle != NULL) { 7665 /* 7666 * Sync buffer. Handle is in usual place in translate 7667 * struct. 7668 */ 7669 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7670 DDI_DMA_SYNC_FORCPU); 7671 ASSERT(rval == DDI_SUCCESS); 7672 } 7673 /* 7674 * Normal completion - copy data into caller's buffer 7675 */ 7676 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 7677 sizeof (struct scsi_inquiry)); 7678 #ifdef SATA_DEBUG 7679 if (sata_debug_flags & SATA_DBG_ATAPI) { 7680 sata_show_inqry_data((uint8_t *)inq); 7681 } 7682 #endif 7683 rval = SATA_SUCCESS; 7684 } else { 7685 /* 7686 * Something went wrong - analyze return - check rqsense data 7687 */ 7688 rval = SATA_FAILURE; 7689 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7690 /* 7691 * ARQ data hopefull show something other than NO SENSE 7692 */ 7693 rqsp = scmd->satacmd_rqsense; 7694 #ifdef SATA_DEBUG 7695 if (sata_debug_flags & SATA_DBG_ATAPI) { 7696 msg_buf[0] = '\0'; 7697 (void) snprintf(msg_buf, MAXPATHLEN, 7698 "ATAPI packet completion reason: %02x\n" 7699 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 7700 " %02x %02x %02x %02x %02x %02x\n" 7701 " %02x %02x %02x %02x %02x %02x", 7702 spkt->satapkt_reason, 7703 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7704 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7705 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7706 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7707 rqsp[16], rqsp[17]); 7708 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7709 "%s", msg_buf); 7710 } 7711 #endif 7712 } else { 7713 switch (spkt->satapkt_reason) { 7714 case SATA_PKT_PORT_ERROR: 7715 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7716 "sata_get_atapi_inquiry_data: " 7717 "packet reason: port error", NULL); 7718 break; 7719 7720 case SATA_PKT_TIMEOUT: 7721 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7722 "sata_get_atapi_inquiry_data: " 7723 "packet reason: timeout", NULL); 7724 break; 7725 7726 case SATA_PKT_ABORTED: 7727 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7728 "sata_get_atapi_inquiry_data: " 7729 "packet reason: aborted", NULL); 7730 break; 7731 7732 case SATA_PKT_RESET: 7733 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7734 "sata_get_atapi_inquiry_data: " 7735 "packet reason: reset\n", NULL); 7736 break; 7737 default: 7738 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7739 "sata_get_atapi_inquiry_data: " 7740 "invalid packet reason: %02x\n", 7741 spkt->satapkt_reason); 7742 break; 7743 } 7744 } 7745 } 7746 cleanup: 7747 sata_free_local_buffer(spx); 7748 sata_pkt_free(spx); 7749 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7750 return (rval); 7751 } 7752 7753 7754 7755 7756 7757 #if 0 7758 #ifdef SATA_DEBUG 7759 7760 /* 7761 * Test ATAPI packet command. 7762 * Single threaded test: send packet command in synch mode, process completion 7763 * 7764 */ 7765 static void 7766 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 7767 { 7768 sata_pkt_txlate_t *spx; 7769 sata_pkt_t *spkt; 7770 struct buf *bp; 7771 sata_device_t sata_device; 7772 sata_drive_info_t *sdinfo; 7773 sata_cmd_t *scmd; 7774 int rval; 7775 uint8_t *rqsp; 7776 7777 ASSERT(sata_hba_inst != NULL); 7778 sata_device.satadev_addr.cport = cport; 7779 sata_device.satadev_addr.pmport = 0; 7780 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 7781 sata_device.satadev_rev = SATA_DEVICE_REV; 7782 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7783 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 7784 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7785 if (sdinfo == NULL) { 7786 sata_log(sata_hba_inst, CE_WARN, 7787 "sata_test_atapi_packet_command: " 7788 "no device info for cport %d", 7789 sata_device.satadev_addr.cport); 7790 return; 7791 } 7792 7793 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7794 spx->txlt_sata_hba_inst = sata_hba_inst; 7795 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7796 spkt = sata_pkt_alloc(spx, NULL); 7797 if (spkt == NULL) { 7798 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7799 return; 7800 } 7801 /* address is needed now */ 7802 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 7803 7804 /* 1024k buffer */ 7805 bp = sata_alloc_local_buffer(spx, 1024); 7806 if (bp == NULL) { 7807 sata_pkt_free(spx); 7808 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7809 sata_log(sata_hba_inst, CE_WARN, 7810 "sata_test_atapi_packet_command: " 7811 "cannot allocate data buffer"); 7812 return; 7813 } 7814 bp_mapin(bp); /* make data buffer accessible */ 7815 7816 scmd = &spkt->satapkt_cmd; 7817 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7818 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7819 7820 /* Use synchronous mode */ 7821 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7822 7823 /* Synchronous mode, no callback - may be changed by the caller */ 7824 spkt->satapkt_comp = NULL; 7825 spkt->satapkt_time = sata_default_pkt_time; 7826 7827 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7828 7829 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7830 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7831 7832 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7833 7834 /* Set-up acdb. */ 7835 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7836 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7837 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7838 scmd->satacmd_acdb[1] = 0x00; 7839 scmd->satacmd_acdb[2] = 0x00; 7840 scmd->satacmd_acdb[3] = 0x00; 7841 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7842 scmd->satacmd_acdb[5] = 0x00; 7843 7844 sata_fixed_sense_data_preset( 7845 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7846 7847 /* Transfer command to HBA */ 7848 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7849 if (sata_hba_start(spx, &rval) != 0) { 7850 /* Pkt not accepted for execution */ 7851 sata_log(sata_hba_inst, CE_WARN, 7852 "sata_test_atapi_packet_command: " 7853 "Packet not accepted for execution - ret: %02x", rval); 7854 mutex_exit( 7855 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7856 goto cleanup; 7857 } 7858 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7859 7860 if (spx->txlt_buf_dma_handle != NULL) { 7861 /* 7862 * Sync buffer. Handle is in usual place in translate struct. 7863 */ 7864 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7865 DDI_DMA_SYNC_FORCPU); 7866 ASSERT(rval == DDI_SUCCESS); 7867 } 7868 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7869 sata_log(sata_hba_inst, CE_WARN, 7870 "sata_test_atapi_packet_command: " 7871 "Packet completed successfully"); 7872 /* 7873 * Normal completion - show inquiry data 7874 */ 7875 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 7876 } else { 7877 /* 7878 * Something went wrong - analyze return - check rqsense data 7879 */ 7880 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7881 /* 7882 * ARQ data hopefull show something other than NO SENSE 7883 */ 7884 rqsp = scmd->satacmd_rqsense; 7885 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7886 "ATAPI packet completion reason: %02x\n" 7887 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7888 " %02x %02x %02x %02x %02x %02x " 7889 " %02x %02x %02x %02x %02x %02x\n", 7890 spkt->satapkt_reason, 7891 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7892 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7893 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7894 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7895 rqsp[16], rqsp[17]); 7896 } else { 7897 switch (spkt->satapkt_reason) { 7898 case SATA_PKT_PORT_ERROR: 7899 sata_log(sata_hba_inst, CE_WARN, 7900 "sata_test_atapi_packet_command: " 7901 "packet reason: port error\n"); 7902 break; 7903 7904 case SATA_PKT_TIMEOUT: 7905 sata_log(sata_hba_inst, CE_WARN, 7906 "sata_test_atapi_packet_command: " 7907 "packet reason: timeout\n"); 7908 break; 7909 7910 case SATA_PKT_ABORTED: 7911 sata_log(sata_hba_inst, CE_WARN, 7912 "sata_test_atapi_packet_command: " 7913 "packet reason: aborted\n"); 7914 break; 7915 7916 case SATA_PKT_RESET: 7917 sata_log(sata_hba_inst, CE_WARN, 7918 "sata_test_atapi_packet_command: " 7919 "packet reason: reset\n"); 7920 break; 7921 default: 7922 sata_log(sata_hba_inst, CE_WARN, 7923 "sata_test_atapi_packet_command: " 7924 "invalid packet reason: %02x\n", 7925 spkt->satapkt_reason); 7926 break; 7927 } 7928 } 7929 } 7930 cleanup: 7931 sata_free_local_buffer(spx); 7932 sata_pkt_free(spx); 7933 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7934 } 7935 7936 #endif /* SATA_DEBUG */ 7937 #endif /* 1 */ 7938 7939 7940 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 7941 7942 /* 7943 * Validate sata_tran info 7944 * SATA_FAILURE returns if structure is inconsistent or structure revision 7945 * does not match one used by the framework. 7946 * 7947 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 7948 * required function pointers. 7949 * Returns SATA_FAILURE otherwise. 7950 */ 7951 static int 7952 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 7953 { 7954 /* 7955 * SATA_TRAN_HBA_REV is the current (highest) revision number 7956 * of the SATA interface. 7957 */ 7958 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 7959 sata_log(NULL, CE_WARN, 7960 "sata: invalid sata_hba_tran version %d for driver %s", 7961 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 7962 return (SATA_FAILURE); 7963 } 7964 7965 if (dip != sata_tran->sata_tran_hba_dip) { 7966 SATA_LOG_D((NULL, CE_WARN, 7967 "sata: inconsistent sata_tran_hba_dip " 7968 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 7969 return (SATA_FAILURE); 7970 } 7971 7972 if (sata_tran->sata_tran_probe_port == NULL || 7973 sata_tran->sata_tran_start == NULL || 7974 sata_tran->sata_tran_abort == NULL || 7975 sata_tran->sata_tran_reset_dport == NULL || 7976 sata_tran->sata_tran_hotplug_ops == NULL || 7977 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 7978 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 7979 NULL) { 7980 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 7981 "required functions")); 7982 } 7983 return (SATA_SUCCESS); 7984 } 7985 7986 /* 7987 * Remove HBA instance from sata_hba_list. 7988 */ 7989 static void 7990 sata_remove_hba_instance(dev_info_t *dip) 7991 { 7992 sata_hba_inst_t *sata_hba_inst; 7993 7994 mutex_enter(&sata_mutex); 7995 for (sata_hba_inst = sata_hba_list; 7996 sata_hba_inst != (struct sata_hba_inst *)NULL; 7997 sata_hba_inst = sata_hba_inst->satahba_next) { 7998 if (sata_hba_inst->satahba_dip == dip) 7999 break; 8000 } 8001 8002 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 8003 #ifdef SATA_DEBUG 8004 cmn_err(CE_WARN, "sata_remove_hba_instance: " 8005 "unknown HBA instance\n"); 8006 #endif 8007 ASSERT(FALSE); 8008 } 8009 if (sata_hba_inst == sata_hba_list) { 8010 sata_hba_list = sata_hba_inst->satahba_next; 8011 if (sata_hba_list) { 8012 sata_hba_list->satahba_prev = 8013 (struct sata_hba_inst *)NULL; 8014 } 8015 if (sata_hba_inst == sata_hba_list_tail) { 8016 sata_hba_list_tail = NULL; 8017 } 8018 } else if (sata_hba_inst == sata_hba_list_tail) { 8019 sata_hba_list_tail = sata_hba_inst->satahba_prev; 8020 if (sata_hba_list_tail) { 8021 sata_hba_list_tail->satahba_next = 8022 (struct sata_hba_inst *)NULL; 8023 } 8024 } else { 8025 sata_hba_inst->satahba_prev->satahba_next = 8026 sata_hba_inst->satahba_next; 8027 sata_hba_inst->satahba_next->satahba_prev = 8028 sata_hba_inst->satahba_prev; 8029 } 8030 mutex_exit(&sata_mutex); 8031 } 8032 8033 8034 8035 8036 8037 /* 8038 * Probe all SATA ports of the specified HBA instance. 8039 * The assumption is that there are no target and attachment point minor nodes 8040 * created by the boot subsystems, so we do not need to prune device tree. 8041 * 8042 * This function is called only from sata_hba_attach(). It does not have to 8043 * be protected by controller mutex, because the hba_attached flag is not set 8044 * yet and no one would be touching this HBA instance other than this thread. 8045 * Determines if port is active and what type of the device is attached 8046 * (if any). Allocates necessary structures for each port. 8047 * 8048 * An AP (Attachement Point) node is created for each SATA device port even 8049 * when there is no device attached. 8050 */ 8051 8052 static void 8053 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 8054 { 8055 dev_info_t *dip = SATA_DIP(sata_hba_inst); 8056 int ncport, npmport; 8057 sata_cport_info_t *cportinfo; 8058 sata_drive_info_t *drive; 8059 sata_pmult_info_t *pminfo; 8060 sata_pmport_info_t *pmportinfo; 8061 sata_device_t sata_device; 8062 int rval; 8063 dev_t minor_number; 8064 char name[16]; 8065 clock_t start_time, cur_time; 8066 8067 /* 8068 * Probe controller ports first, to find port status and 8069 * any port multiplier attached. 8070 */ 8071 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 8072 /* allocate cport structure */ 8073 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 8074 ASSERT(cportinfo != NULL); 8075 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 8076 8077 mutex_enter(&cportinfo->cport_mutex); 8078 8079 cportinfo->cport_addr.cport = ncport; 8080 cportinfo->cport_addr.pmport = 0; 8081 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 8082 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8083 cportinfo->cport_state |= SATA_STATE_PROBING; 8084 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 8085 8086 /* 8087 * Regardless if a port is usable or not, create 8088 * an attachment point 8089 */ 8090 mutex_exit(&cportinfo->cport_mutex); 8091 minor_number = 8092 SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0); 8093 (void) sprintf(name, "%d", ncport); 8094 if (ddi_create_minor_node(dip, name, S_IFCHR, 8095 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 8096 DDI_SUCCESS) { 8097 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 8098 "cannot create SATA attachment point for port %d", 8099 ncport); 8100 } 8101 8102 /* Probe port */ 8103 start_time = ddi_get_lbolt(); 8104 reprobe_cport: 8105 sata_device.satadev_addr.cport = ncport; 8106 sata_device.satadev_addr.pmport = 0; 8107 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 8108 sata_device.satadev_rev = SATA_DEVICE_REV; 8109 8110 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8111 (dip, &sata_device); 8112 8113 mutex_enter(&cportinfo->cport_mutex); 8114 sata_update_port_scr(&cportinfo->cport_scr, &sata_device); 8115 if (rval != SATA_SUCCESS) { 8116 /* Something went wrong? Fail the port */ 8117 cportinfo->cport_state = SATA_PSTATE_FAILED; 8118 mutex_exit(&cportinfo->cport_mutex); 8119 continue; 8120 } 8121 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8122 cportinfo->cport_state |= SATA_STATE_PROBED; 8123 cportinfo->cport_dev_type = sata_device.satadev_type; 8124 8125 cportinfo->cport_state |= SATA_STATE_READY; 8126 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 8127 mutex_exit(&cportinfo->cport_mutex); 8128 continue; 8129 } 8130 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8131 /* 8132 * There is some device attached. 8133 * Allocate device info structure 8134 */ 8135 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 8136 mutex_exit(&cportinfo->cport_mutex); 8137 SATA_CPORTINFO_DRV_INFO(cportinfo) = 8138 kmem_zalloc(sizeof (sata_drive_info_t), 8139 KM_SLEEP); 8140 mutex_enter(&cportinfo->cport_mutex); 8141 } 8142 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 8143 drive->satadrv_addr = cportinfo->cport_addr; 8144 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 8145 drive->satadrv_type = cportinfo->cport_dev_type; 8146 drive->satadrv_state = SATA_STATE_UNKNOWN; 8147 8148 mutex_exit(&cportinfo->cport_mutex); 8149 if (sata_add_device(dip, sata_hba_inst, ncport, 0) != 8150 SATA_SUCCESS) { 8151 /* 8152 * Plugged device was not correctly identified. 8153 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 8154 */ 8155 cur_time = ddi_get_lbolt(); 8156 if ((cur_time - start_time) < 8157 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 8158 /* sleep for a while */ 8159 delay(drv_usectohz( 8160 SATA_DEV_RETRY_DLY)); 8161 goto reprobe_cport; 8162 } 8163 } 8164 } else { 8165 mutex_exit(&cportinfo->cport_mutex); 8166 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 8167 pminfo = kmem_zalloc(sizeof (sata_pmult_info_t), 8168 KM_SLEEP); 8169 mutex_enter(&cportinfo->cport_mutex); 8170 ASSERT(pminfo != NULL); 8171 SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo; 8172 pminfo->pmult_addr.cport = cportinfo->cport_addr.cport; 8173 pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT; 8174 pminfo->pmult_addr.qual = SATA_ADDR_PMPORT; 8175 pminfo->pmult_num_dev_ports = 8176 sata_device.satadev_add_info; 8177 mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER, 8178 NULL); 8179 pminfo->pmult_state = SATA_STATE_PROBING; 8180 mutex_exit(&cportinfo->cport_mutex); 8181 8182 /* Probe Port Multiplier ports */ 8183 for (npmport = 0; 8184 npmport < pminfo->pmult_num_dev_ports; 8185 npmport++) { 8186 pmportinfo = kmem_zalloc( 8187 sizeof (sata_pmport_info_t), KM_SLEEP); 8188 mutex_enter(&cportinfo->cport_mutex); 8189 ASSERT(pmportinfo != NULL); 8190 pmportinfo->pmport_addr.cport = ncport; 8191 pmportinfo->pmport_addr.pmport = npmport; 8192 pmportinfo->pmport_addr.qual = 8193 SATA_ADDR_PMPORT; 8194 pminfo->pmult_dev_port[npmport] = pmportinfo; 8195 8196 mutex_init(&pmportinfo->pmport_mutex, NULL, 8197 MUTEX_DRIVER, NULL); 8198 8199 mutex_exit(&cportinfo->cport_mutex); 8200 8201 /* Create an attachment point */ 8202 minor_number = SATA_MAKE_AP_MINOR( 8203 ddi_get_instance(dip), ncport, npmport, 1); 8204 (void) sprintf(name, "%d.%d", ncport, npmport); 8205 if (ddi_create_minor_node(dip, name, S_IFCHR, 8206 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 8207 0) != DDI_SUCCESS) { 8208 sata_log(sata_hba_inst, CE_WARN, 8209 "sata_hba_attach: " 8210 "cannot create SATA attachment " 8211 "point for port %d pmult port %d", 8212 ncport, npmport); 8213 } 8214 8215 start_time = ddi_get_lbolt(); 8216 reprobe_pmport: 8217 sata_device.satadev_addr.pmport = npmport; 8218 sata_device.satadev_addr.qual = 8219 SATA_ADDR_PMPORT; 8220 8221 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8222 (dip, &sata_device); 8223 mutex_enter(&cportinfo->cport_mutex); 8224 8225 /* sata_update_port_info() */ 8226 sata_update_port_scr(&pmportinfo->pmport_scr, 8227 &sata_device); 8228 8229 if (rval != SATA_SUCCESS) { 8230 pmportinfo->pmport_state = 8231 SATA_PSTATE_FAILED; 8232 mutex_exit(&cportinfo->cport_mutex); 8233 continue; 8234 } 8235 pmportinfo->pmport_state &= 8236 ~SATA_STATE_PROBING; 8237 pmportinfo->pmport_state |= SATA_STATE_PROBED; 8238 pmportinfo->pmport_dev_type = 8239 sata_device.satadev_type; 8240 8241 pmportinfo->pmport_state |= SATA_STATE_READY; 8242 if (pmportinfo->pmport_dev_type == 8243 SATA_DTYPE_NONE) { 8244 mutex_exit(&cportinfo->cport_mutex); 8245 continue; 8246 } 8247 /* Port multipliers cannot be chained */ 8248 ASSERT(pmportinfo->pmport_dev_type != 8249 SATA_DTYPE_PMULT); 8250 /* 8251 * There is something attached to Port 8252 * Multiplier device port 8253 * Allocate device info structure 8254 */ 8255 if (pmportinfo->pmport_sata_drive == NULL) { 8256 mutex_exit(&cportinfo->cport_mutex); 8257 pmportinfo->pmport_sata_drive = 8258 kmem_zalloc( 8259 sizeof (sata_drive_info_t), 8260 KM_SLEEP); 8261 mutex_enter(&cportinfo->cport_mutex); 8262 } 8263 drive = pmportinfo->pmport_sata_drive; 8264 drive->satadrv_addr.cport = 8265 pmportinfo->pmport_addr.cport; 8266 drive->satadrv_addr.pmport = npmport; 8267 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 8268 drive->satadrv_type = pmportinfo-> 8269 pmport_dev_type; 8270 drive->satadrv_state = SATA_STATE_UNKNOWN; 8271 8272 mutex_exit(&cportinfo->cport_mutex); 8273 if (sata_add_device(dip, sata_hba_inst, ncport, 8274 npmport) != SATA_SUCCESS) { 8275 /* 8276 * Plugged device was not correctly 8277 * identified. Retry, within the 8278 * SATA_DEV_IDENTIFY_TIMEOUT 8279 */ 8280 cur_time = ddi_get_lbolt(); 8281 if ((cur_time - start_time) < 8282 drv_usectohz( 8283 SATA_DEV_IDENTIFY_TIMEOUT)) { 8284 /* sleep for a while */ 8285 delay(drv_usectohz( 8286 SATA_DEV_RETRY_DLY)); 8287 goto reprobe_pmport; 8288 } 8289 } 8290 } 8291 pmportinfo->pmport_state = 8292 SATA_STATE_PROBED | SATA_STATE_READY; 8293 } 8294 } 8295 } 8296 8297 /* 8298 * Add SATA device for specified HBA instance & port (SCSI target 8299 * device nodes). 8300 * This function is called (indirectly) only from sata_hba_attach(). 8301 * A target node is created when there is a supported type device attached, 8302 * but may be removed if it cannot be put online. 8303 * 8304 * This function cannot be called from an interrupt context. 8305 * 8306 * ONLY DISK TARGET NODES ARE CREATED NOW 8307 * 8308 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 8309 * device identification failed - adding a device could be retried. 8310 * 8311 */ 8312 static int 8313 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport, 8314 int pmport) 8315 { 8316 sata_cport_info_t *cportinfo; 8317 sata_pmult_info_t *pminfo; 8318 sata_pmport_info_t *pmportinfo; 8319 dev_info_t *cdip; /* child dip */ 8320 sata_device_t sata_device; 8321 int rval; 8322 8323 8324 8325 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 8326 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 8327 mutex_enter(&cportinfo->cport_mutex); 8328 /* 8329 * Some device is attached to a controller port. 8330 * We rely on controllers distinquishing between no-device, 8331 * attached port multiplier and other kind of attached device. 8332 * We need to get Identify Device data and determine 8333 * positively the dev type before trying to attach 8334 * the target driver. 8335 */ 8336 sata_device.satadev_rev = SATA_DEVICE_REV; 8337 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8338 /* 8339 * Not port multiplier. 8340 */ 8341 sata_device.satadev_addr = cportinfo->cport_addr; 8342 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8343 mutex_exit(&cportinfo->cport_mutex); 8344 8345 rval = sata_probe_device(sata_hba_inst, &sata_device); 8346 if (rval != SATA_SUCCESS || 8347 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) 8348 return (SATA_FAILURE); 8349 8350 mutex_enter(&cportinfo->cport_mutex); 8351 sata_show_drive_info(sata_hba_inst, 8352 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8353 8354 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8355 /* 8356 * Could not determine device type or 8357 * a device is not supported. 8358 * Degrade this device to unknown. 8359 */ 8360 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8361 mutex_exit(&cportinfo->cport_mutex); 8362 return (SATA_SUCCESS); 8363 } 8364 cportinfo->cport_dev_type = sata_device.satadev_type; 8365 cportinfo->cport_tgtnode_clean = B_TRUE; 8366 mutex_exit(&cportinfo->cport_mutex); 8367 8368 /* 8369 * Initialize device to the desired state. Even if it 8370 * fails, the device will still attach but syslog 8371 * will show the warning. 8372 */ 8373 if (sata_initialize_device(sata_hba_inst, 8374 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 8375 /* Retry */ 8376 rval = sata_initialize_device(sata_hba_inst, 8377 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8378 8379 if (rval == SATA_RETRY) 8380 sata_log(sata_hba_inst, CE_WARN, 8381 "SATA device at port %d - " 8382 "default device features could not be set." 8383 " Device may not operate as expected.", 8384 cportinfo->cport_addr.cport); 8385 } 8386 8387 cdip = sata_create_target_node(pdip, sata_hba_inst, 8388 &sata_device.satadev_addr); 8389 mutex_enter(&cportinfo->cport_mutex); 8390 if (cdip == NULL) { 8391 /* 8392 * Attaching target node failed. 8393 * We retain sata_drive_info structure... 8394 */ 8395 mutex_exit(&cportinfo->cport_mutex); 8396 return (SATA_SUCCESS); 8397 } 8398 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8399 satadrv_state = SATA_STATE_READY; 8400 } else { 8401 /* This must be Port Multiplier type */ 8402 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8403 SATA_LOG_D((sata_hba_inst, CE_WARN, 8404 "sata_add_device: " 8405 "unrecognized dev type %x", 8406 cportinfo->cport_dev_type)); 8407 mutex_exit(&cportinfo->cport_mutex); 8408 return (SATA_SUCCESS); 8409 } 8410 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 8411 pmportinfo = pminfo->pmult_dev_port[pmport]; 8412 sata_device.satadev_addr = pmportinfo->pmport_addr; 8413 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 8414 mutex_exit(&cportinfo->cport_mutex); 8415 8416 rval = sata_probe_device(sata_hba_inst, &sata_device); 8417 if (rval != SATA_SUCCESS || 8418 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) { 8419 return (SATA_FAILURE); 8420 } 8421 mutex_enter(&cportinfo->cport_mutex); 8422 sata_show_drive_info(sata_hba_inst, 8423 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8424 8425 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8426 /* 8427 * Could not determine device type. 8428 * Degrade this device to unknown. 8429 */ 8430 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 8431 mutex_exit(&cportinfo->cport_mutex); 8432 return (SATA_SUCCESS); 8433 } 8434 pmportinfo->pmport_dev_type = sata_device.satadev_type; 8435 pmportinfo->pmport_tgtnode_clean = B_TRUE; 8436 mutex_exit(&cportinfo->cport_mutex); 8437 8438 /* 8439 * Initialize device to the desired state. 8440 * Even if it fails, the device will still 8441 * attach but syslog will show the warning. 8442 */ 8443 if (sata_initialize_device(sata_hba_inst, 8444 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 8445 /* Retry */ 8446 rval = sata_initialize_device(sata_hba_inst, 8447 pmportinfo->pmport_sata_drive); 8448 8449 if (rval == SATA_RETRY) 8450 sata_log(sata_hba_inst, CE_WARN, 8451 "SATA device at port %d pmport %d - " 8452 "default device features could not be set." 8453 " Device may not operate as expected.", 8454 pmportinfo->pmport_addr.cport, 8455 pmportinfo->pmport_addr.pmport); 8456 } 8457 cdip = sata_create_target_node(pdip, sata_hba_inst, 8458 &sata_device.satadev_addr); 8459 mutex_enter(&cportinfo->cport_mutex); 8460 if (cdip == NULL) { 8461 /* 8462 * Attaching target node failed. 8463 * We retain sata_drive_info structure... 8464 */ 8465 mutex_exit(&cportinfo->cport_mutex); 8466 return (SATA_SUCCESS); 8467 } 8468 pmportinfo->pmport_sata_drive->satadrv_state |= 8469 SATA_STATE_READY; 8470 } 8471 mutex_exit(&cportinfo->cport_mutex); 8472 return (SATA_SUCCESS); 8473 } 8474 8475 8476 8477 /* 8478 * Create scsi target node for attached device, create node properties and 8479 * attach the node. 8480 * The node could be removed if the device onlining fails. 8481 * 8482 * A dev_info_t pointer is returned if operation is successful, NULL is 8483 * returned otherwise. 8484 * 8485 * No port multiplier support. 8486 */ 8487 8488 static dev_info_t * 8489 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 8490 sata_address_t *sata_addr) 8491 { 8492 dev_info_t *cdip = NULL; 8493 int rval; 8494 char *nname = NULL; 8495 char **compatible = NULL; 8496 int ncompatible; 8497 struct scsi_inquiry inq; 8498 sata_device_t sata_device; 8499 sata_drive_info_t *sdinfo; 8500 int target; 8501 int i; 8502 8503 sata_device.satadev_rev = SATA_DEVICE_REV; 8504 sata_device.satadev_addr = *sata_addr; 8505 8506 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 8507 8508 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8509 8510 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 8511 sata_addr->pmport, sata_addr->qual); 8512 8513 if (sdinfo == NULL) { 8514 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8515 sata_addr->cport))); 8516 SATA_LOG_D((sata_hba_inst, CE_WARN, 8517 "sata_create_target_node: no sdinfo for target %x", 8518 target)); 8519 return (NULL); 8520 } 8521 8522 /* 8523 * create or get scsi inquiry data, expected by 8524 * scsi_hba_nodename_compatible_get() 8525 * SATA hard disks get Identify Data translated into Inguiry Data. 8526 * ATAPI devices respond directly to Inquiry request. 8527 */ 8528 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 8529 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 8530 (uint8_t *)&inq); 8531 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8532 sata_addr->cport))); 8533 } else { /* Assume supported ATAPI device */ 8534 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8535 sata_addr->cport))); 8536 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 8537 &inq) == SATA_FAILURE) 8538 return (NULL); 8539 /* 8540 * Save supported ATAPI transport version 8541 */ 8542 sdinfo->satadrv_atapi_trans_ver = 8543 SATA_ATAPI_TRANS_VERSION(&inq); 8544 } 8545 8546 /* determine the node name and compatible */ 8547 scsi_hba_nodename_compatible_get(&inq, NULL, 8548 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 8549 8550 #ifdef SATA_DEBUG 8551 if (sata_debug_flags & SATA_DBG_NODES) { 8552 if (nname == NULL) { 8553 cmn_err(CE_NOTE, "sata_create_target_node: " 8554 "cannot determine nodename for target %d\n", 8555 target); 8556 } else { 8557 cmn_err(CE_WARN, "sata_create_target_node: " 8558 "target %d nodename: %s\n", target, nname); 8559 } 8560 if (compatible == NULL) { 8561 cmn_err(CE_WARN, 8562 "sata_create_target_node: no compatible name\n"); 8563 } else { 8564 for (i = 0; i < ncompatible; i++) { 8565 cmn_err(CE_WARN, "sata_create_target_node: " 8566 "compatible name: %s\n", compatible[i]); 8567 } 8568 } 8569 } 8570 #endif 8571 8572 /* if nodename can't be determined, log error and exit */ 8573 if (nname == NULL) { 8574 SATA_LOG_D((sata_hba_inst, CE_WARN, 8575 "sata_create_target_node: cannot determine nodename " 8576 "for target %d\n", target)); 8577 scsi_hba_nodename_compatible_free(nname, compatible); 8578 return (NULL); 8579 } 8580 /* 8581 * Create scsi target node 8582 */ 8583 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 8584 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8585 "device-type", "scsi"); 8586 8587 if (rval != DDI_PROP_SUCCESS) { 8588 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8589 "updating device_type prop failed %d", rval)); 8590 goto fail; 8591 } 8592 8593 /* 8594 * Create target node properties: target & lun 8595 */ 8596 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 8597 if (rval != DDI_PROP_SUCCESS) { 8598 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8599 "updating target prop failed %d", rval)); 8600 goto fail; 8601 } 8602 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 8603 if (rval != DDI_PROP_SUCCESS) { 8604 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8605 "updating target prop failed %d", rval)); 8606 goto fail; 8607 } 8608 8609 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 8610 /* 8611 * Add "variant" property 8612 */ 8613 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8614 "variant", "atapi"); 8615 if (rval != DDI_PROP_SUCCESS) { 8616 SATA_LOG_D((sata_hba_inst, CE_WARN, 8617 "sata_create_target_node: variant atapi " 8618 "property could not be created: %d", rval)); 8619 goto fail; 8620 } 8621 } 8622 /* decorate the node with compatible */ 8623 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 8624 compatible, ncompatible) != DDI_PROP_SUCCESS) { 8625 SATA_LOG_D((sata_hba_inst, CE_WARN, 8626 "sata_create_target_node: FAIL compatible props cdip 0x%p", 8627 (void *)cdip)); 8628 goto fail; 8629 } 8630 8631 8632 /* 8633 * Now, try to attach the driver. If probing of the device fails, 8634 * the target node may be removed 8635 */ 8636 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 8637 8638 scsi_hba_nodename_compatible_free(nname, compatible); 8639 8640 if (rval == NDI_SUCCESS) 8641 return (cdip); 8642 8643 /* target node was removed - are we sure? */ 8644 return (NULL); 8645 8646 fail: 8647 scsi_hba_nodename_compatible_free(nname, compatible); 8648 ddi_prop_remove_all(cdip); 8649 rval = ndi_devi_free(cdip); 8650 if (rval != NDI_SUCCESS) { 8651 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8652 "node removal failed %d", rval)); 8653 } 8654 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 8655 "cannot create target node for SATA device at port %d", 8656 sata_addr->cport); 8657 return (NULL); 8658 } 8659 8660 8661 8662 /* 8663 * Re-probe sata port, check for a device and attach info 8664 * structures when necessary. Identify Device data is fetched, if possible. 8665 * Assumption: sata address is already validated. 8666 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 8667 * the presence of a device and its type. 8668 * 8669 * flag arg specifies that the function should try multiple times to identify 8670 * device type and to initialize it, or it should return immediately on failure. 8671 * SATA_DEV_IDENTIFY_RETRY - retry 8672 * SATA_DEV_IDENTIFY_NORETRY - no retry 8673 * 8674 * SATA_FAILURE is returned if one of the operations failed. 8675 * 8676 * This function cannot be called in interrupt context - it may sleep. 8677 * 8678 * NOte: Port multiplier is not supported yet, although there may be some 8679 * pieces of code referencing to it. 8680 */ 8681 static int 8682 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 8683 int flag) 8684 { 8685 sata_cport_info_t *cportinfo; 8686 sata_drive_info_t *sdinfo, *osdinfo; 8687 boolean_t init_device = B_FALSE; 8688 int prev_device_type = SATA_DTYPE_NONE; 8689 int prev_device_settings = 0; 8690 int prev_device_state = 0; 8691 clock_t start_time; 8692 int retry = B_FALSE; 8693 int rval_probe, rval_init; 8694 8695 /* We only care about host sata cport for now */ 8696 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 8697 sata_device->satadev_addr.cport); 8698 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8699 if (osdinfo != NULL) { 8700 /* 8701 * We are re-probing port with a previously attached device. 8702 * Save previous device type and settings. 8703 */ 8704 prev_device_type = cportinfo->cport_dev_type; 8705 prev_device_settings = osdinfo->satadrv_settings; 8706 prev_device_state = osdinfo->satadrv_state; 8707 } 8708 if (flag == SATA_DEV_IDENTIFY_RETRY) { 8709 start_time = ddi_get_lbolt(); 8710 retry = B_TRUE; 8711 } 8712 retry_probe: 8713 8714 /* probe port */ 8715 mutex_enter(&cportinfo->cport_mutex); 8716 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8717 cportinfo->cport_state |= SATA_STATE_PROBING; 8718 mutex_exit(&cportinfo->cport_mutex); 8719 8720 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8721 (SATA_DIP(sata_hba_inst), sata_device); 8722 8723 mutex_enter(&cportinfo->cport_mutex); 8724 if (rval_probe != SATA_SUCCESS) { 8725 cportinfo->cport_state = SATA_PSTATE_FAILED; 8726 mutex_exit(&cportinfo->cport_mutex); 8727 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 8728 "SATA port %d probing failed", 8729 cportinfo->cport_addr.cport)); 8730 return (SATA_FAILURE); 8731 } 8732 8733 /* 8734 * update sata port state and set device type 8735 */ 8736 sata_update_port_info(sata_hba_inst, sata_device); 8737 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8738 8739 /* 8740 * Sanity check - Port is active? Is the link active? 8741 * Is there any device attached? 8742 */ 8743 if ((cportinfo->cport_state & 8744 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 8745 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 8746 SATA_PORT_DEVLINK_UP) { 8747 /* 8748 * Port in non-usable state or no link active/no device. 8749 * Free info structure if necessary (direct attached drive 8750 * only, for now! 8751 */ 8752 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8753 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8754 /* Add here differentiation for device attached or not */ 8755 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8756 mutex_exit(&cportinfo->cport_mutex); 8757 if (sdinfo != NULL) 8758 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8759 return (SATA_SUCCESS); 8760 } 8761 8762 cportinfo->cport_state |= SATA_STATE_READY; 8763 cportinfo->cport_dev_type = sata_device->satadev_type; 8764 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8765 8766 /* 8767 * If we are re-probing the port, there may be 8768 * sata_drive_info structure attached 8769 * (or sata_pm_info, if PMult is supported). 8770 */ 8771 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 8772 /* 8773 * There is no device, so remove device info structure, 8774 * if necessary. 8775 * Only direct attached drive is considered now, until 8776 * port multiplier is supported. If the previously 8777 * attached device was a port multiplier, we would need 8778 * to take care of devices attached beyond the port 8779 * multiplier. 8780 */ 8781 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8782 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8783 if (sdinfo != NULL) { 8784 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8785 sata_log(sata_hba_inst, CE_WARN, 8786 "SATA device detached " 8787 "from port %d", cportinfo->cport_addr.cport); 8788 } 8789 mutex_exit(&cportinfo->cport_mutex); 8790 return (SATA_SUCCESS); 8791 } 8792 8793 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 8794 if (sdinfo == NULL) { 8795 /* 8796 * There is some device attached, but there is 8797 * no sata_drive_info structure - allocate one 8798 */ 8799 mutex_exit(&cportinfo->cport_mutex); 8800 sdinfo = kmem_zalloc( 8801 sizeof (sata_drive_info_t), KM_SLEEP); 8802 mutex_enter(&cportinfo->cport_mutex); 8803 /* 8804 * Recheck, that the port state did not change when we 8805 * released mutex. 8806 */ 8807 if (cportinfo->cport_state & SATA_STATE_READY) { 8808 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 8809 sdinfo->satadrv_addr = cportinfo->cport_addr; 8810 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 8811 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8812 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 8813 } else { 8814 /* 8815 * Port is not in ready state, we 8816 * cannot attach a device. 8817 */ 8818 mutex_exit(&cportinfo->cport_mutex); 8819 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8820 return (SATA_SUCCESS); 8821 } 8822 /* 8823 * Since we are adding device, presumably new one, 8824 * indicate that it should be initalized, 8825 * as well as some internal framework states). 8826 */ 8827 init_device = B_TRUE; 8828 } 8829 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8830 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 8831 } else { 8832 /* 8833 * The device is a port multiplier - not handled now. 8834 */ 8835 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8836 mutex_exit(&cportinfo->cport_mutex); 8837 return (SATA_SUCCESS); 8838 } 8839 mutex_exit(&cportinfo->cport_mutex); 8840 /* 8841 * Figure out what kind of device we are really 8842 * dealing with. Failure of identifying device does not fail this 8843 * function. 8844 */ 8845 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 8846 rval_init = SATA_FAILURE; 8847 mutex_enter(&cportinfo->cport_mutex); 8848 if (rval_probe == SATA_SUCCESS) { 8849 /* 8850 * If we are dealing with the same type of a device as before, 8851 * restore its settings flags. 8852 */ 8853 if (osdinfo != NULL && 8854 sata_device->satadev_type == prev_device_type) 8855 sdinfo->satadrv_settings = prev_device_settings; 8856 8857 mutex_exit(&cportinfo->cport_mutex); 8858 rval_init = SATA_SUCCESS; 8859 /* Set initial device features, if necessary */ 8860 if (init_device == B_TRUE) { 8861 rval_init = sata_initialize_device(sata_hba_inst, 8862 sdinfo); 8863 } 8864 if (rval_init == SATA_SUCCESS) 8865 return (rval_init); 8866 /* else we will retry if retry was asked for */ 8867 8868 } else { 8869 /* 8870 * If there was some device info before we probe the device, 8871 * restore previous device setting, so we can retry from scratch 8872 * later. Providing, of course, that device has not disapear 8873 * during probing process. 8874 */ 8875 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 8876 if (osdinfo != NULL) { 8877 cportinfo->cport_dev_type = prev_device_type; 8878 sdinfo->satadrv_type = prev_device_type; 8879 sdinfo->satadrv_state = prev_device_state; 8880 } 8881 } else { 8882 /* device is gone */ 8883 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8884 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8885 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8886 mutex_exit(&cportinfo->cport_mutex); 8887 return (SATA_SUCCESS); 8888 } 8889 mutex_exit(&cportinfo->cport_mutex); 8890 } 8891 8892 if (retry) { 8893 clock_t cur_time = ddi_get_lbolt(); 8894 /* 8895 * A device was not successfully identified or initialized. 8896 * Track retry time for device identification. 8897 */ 8898 if ((cur_time - start_time) < 8899 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 8900 /* sleep for a while */ 8901 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 8902 goto retry_probe; 8903 } 8904 /* else no more retries */ 8905 mutex_enter(&cportinfo->cport_mutex); 8906 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 8907 if (rval_init == SATA_RETRY) { 8908 /* 8909 * Setting drive features have failed, but 8910 * because the drive is still accessible, 8911 * keep it and emit a warning message. 8912 */ 8913 sata_log(sata_hba_inst, CE_WARN, 8914 "SATA device at port %d - desired " 8915 "drive features could not be set. " 8916 "Device may not operate as expected.", 8917 cportinfo->cport_addr.cport); 8918 } else { 8919 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 8920 satadrv_state = SATA_DSTATE_FAILED; 8921 } 8922 } 8923 mutex_exit(&cportinfo->cport_mutex); 8924 } 8925 return (SATA_SUCCESS); 8926 } 8927 8928 /* 8929 * Initialize device 8930 * Specified device is initialized to a default state. 8931 * 8932 * Returns SATA_SUCCESS if all device features are set successfully, 8933 * SATA_RETRY if device is accessible but device features were not set 8934 * successfully, and SATA_FAILURE otherwise. 8935 */ 8936 static int 8937 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 8938 sata_drive_info_t *sdinfo) 8939 { 8940 int rval; 8941 8942 sata_save_drive_settings(sdinfo); 8943 8944 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 8945 8946 sata_init_write_cache_mode(sdinfo); 8947 8948 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 8949 8950 /* Determine current data transfer mode */ 8951 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 8952 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8953 } else if ((sdinfo->satadrv_id.ai_validinfo & 8954 SATA_VALIDINFO_88) != 0 && 8955 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 8956 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8957 } else if ((sdinfo->satadrv_id.ai_dworddma & 8958 SATA_MDMA_SEL_MASK) != 0) { 8959 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8960 } else 8961 /* DMA supported, not no DMA transfer mode is selected !? */ 8962 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8963 8964 return (rval); 8965 } 8966 8967 8968 /* 8969 * Initialize write cache mode. 8970 * 8971 * The default write cache setting for SATA HDD is provided by sata_write_cache 8972 * static variable. ATAPI CD/DVDs devices have write cache default is 8973 * determined by sata_atapicdvd_write_cache static variable. 8974 * ATAPI tape devices have write cache default is determined by 8975 * sata_atapitape_write_cache static variable. 8976 * 1 - enable 8977 * 0 - disable 8978 * any other value - current drive setting 8979 * 8980 * Although there is not reason to disable write cache on CD/DVD devices 8981 * and tape devices, the default setting control is provided for the maximun 8982 * flexibility. 8983 * 8984 * In the future, it may be overridden by the 8985 * disk-write-cache-enable property setting, if it is defined. 8986 * Returns SATA_SUCCESS if all device features are set successfully, 8987 * SATA_FAILURE otherwise. 8988 */ 8989 static void 8990 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 8991 { 8992 switch (sdinfo->satadrv_type) { 8993 case SATA_DTYPE_ATADISK: 8994 if (sata_write_cache == 1) 8995 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 8996 else if (sata_write_cache == 0) 8997 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 8998 /* 8999 * When sata_write_cache value is not 0 or 1, 9000 * a current setting of the drive's write cache is used. 9001 */ 9002 break; 9003 case SATA_DTYPE_ATAPICD: 9004 if (sata_atapicdvd_write_cache == 1) 9005 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9006 else if (sata_atapicdvd_write_cache == 0) 9007 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9008 /* 9009 * When sata_write_cache value is not 0 or 1, 9010 * a current setting of the drive's write cache is used. 9011 */ 9012 break; 9013 case SATA_DTYPE_ATAPITAPE: 9014 if (sata_atapitape_write_cache == 1) 9015 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9016 else if (sata_atapitape_write_cache == 0) 9017 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9018 /* 9019 * When sata_write_cache value is not 0 or 1, 9020 * a current setting of the drive's write cache is used. 9021 */ 9022 break; 9023 } 9024 } 9025 9026 9027 /* 9028 * Validate sata address. 9029 * Specified cport, pmport and qualifier has to match 9030 * passed sata_scsi configuration info. 9031 * The presence of an attached device is not verified. 9032 * 9033 * Returns 0 when address is valid, -1 otherwise. 9034 */ 9035 static int 9036 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 9037 int pmport, int qual) 9038 { 9039 if (qual == SATA_ADDR_DCPORT && pmport != 0) 9040 goto invalid_address; 9041 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9042 goto invalid_address; 9043 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 9044 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 9045 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 9046 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 9047 goto invalid_address; 9048 9049 return (0); 9050 9051 invalid_address: 9052 return (-1); 9053 9054 } 9055 9056 /* 9057 * Validate scsi address 9058 * SCSI target address is translated into SATA cport/pmport and compared 9059 * with a controller port/device configuration. LUN has to be 0. 9060 * Returns 0 if a scsi target refers to an attached device, 9061 * returns 1 if address is valid but device is not attached, 9062 * returns -1 if bad address or device is of an unsupported type. 9063 * Upon return sata_device argument is set. 9064 */ 9065 static int 9066 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 9067 struct scsi_address *ap, sata_device_t *sata_device) 9068 { 9069 int cport, pmport, qual, rval; 9070 9071 rval = -1; /* Invalid address */ 9072 if (ap->a_lun != 0) 9073 goto out; 9074 9075 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 9076 cport = SCSI_TO_SATA_CPORT(ap->a_target); 9077 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 9078 9079 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 9080 goto out; 9081 9082 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 9083 0) { 9084 9085 sata_cport_info_t *cportinfo; 9086 sata_pmult_info_t *pmultinfo; 9087 sata_drive_info_t *sdinfo = NULL; 9088 9089 rval = 1; /* Valid sata address */ 9090 9091 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9092 if (qual == SATA_ADDR_DCPORT) { 9093 if (cportinfo == NULL || 9094 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 9095 goto out; 9096 9097 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT || 9098 (cportinfo->cport_dev_type & 9099 SATA_VALID_DEV_TYPE) == 0) { 9100 rval = -1; 9101 goto out; 9102 } 9103 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9104 9105 } else if (qual == SATA_ADDR_DPMPORT) { 9106 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9107 if (pmultinfo == NULL) { 9108 rval = -1; 9109 goto out; 9110 } 9111 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 9112 NULL || 9113 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 9114 pmport) == SATA_DTYPE_NONE) 9115 goto out; 9116 9117 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 9118 pmport); 9119 } else { 9120 rval = -1; 9121 goto out; 9122 } 9123 if ((sdinfo == NULL) || 9124 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 9125 goto out; 9126 9127 sata_device->satadev_type = sdinfo->satadrv_type; 9128 sata_device->satadev_addr.qual = qual; 9129 sata_device->satadev_addr.cport = cport; 9130 sata_device->satadev_addr.pmport = pmport; 9131 sata_device->satadev_rev = SATA_DEVICE_REV_1; 9132 return (0); 9133 } 9134 out: 9135 if (rval == 1) { 9136 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 9137 "sata_validate_scsi_address: no valid target %x lun %x", 9138 ap->a_target, ap->a_lun); 9139 } 9140 return (rval); 9141 } 9142 9143 /* 9144 * Find dip corresponding to passed device number 9145 * 9146 * Returns NULL if invalid device number is passed or device cannot be found, 9147 * Returns dip is device is found. 9148 */ 9149 static dev_info_t * 9150 sata_devt_to_devinfo(dev_t dev) 9151 { 9152 dev_info_t *dip; 9153 #ifndef __lock_lint 9154 struct devnames *dnp; 9155 major_t major = getmajor(dev); 9156 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 9157 9158 if (major >= devcnt) 9159 return (NULL); 9160 9161 dnp = &devnamesp[major]; 9162 LOCK_DEV_OPS(&(dnp->dn_lock)); 9163 dip = dnp->dn_head; 9164 while (dip && (ddi_get_instance(dip) != instance)) { 9165 dip = ddi_get_next(dip); 9166 } 9167 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 9168 #endif 9169 9170 return (dip); 9171 } 9172 9173 9174 /* 9175 * Probe device. 9176 * This function issues Identify Device command and initializes local 9177 * sata_drive_info structure if the device can be identified. 9178 * The device type is determined by examining Identify Device 9179 * command response. 9180 * If the sata_hba_inst has linked drive info structure for this 9181 * device address, the Identify Device data is stored into sata_drive_info 9182 * structure linked to the port info structure. 9183 * 9184 * sata_device has to refer to the valid sata port(s) for HBA described 9185 * by sata_hba_inst structure. 9186 * 9187 * Returns: 9188 * SATA_SUCCESS if device type was successfully probed and port-linked 9189 * drive info structure was updated; 9190 * SATA_FAILURE if there is no device, or device was not probed 9191 * successully; 9192 * SATA_RETRY if device probe can be retried later. 9193 * If a device cannot be identified, sata_device's dev_state and dev_type 9194 * fields are set to unknown. 9195 * There are no retries in this function. Any retries should be managed by 9196 * the caller. 9197 */ 9198 9199 9200 static int 9201 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 9202 { 9203 sata_drive_info_t *sdinfo; 9204 sata_drive_info_t new_sdinfo; /* local drive info struct */ 9205 int rval; 9206 9207 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 9208 sata_device->satadev_addr.cport) & 9209 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 9210 9211 sata_device->satadev_type = SATA_DTYPE_NONE; 9212 9213 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9214 sata_device->satadev_addr.cport))); 9215 9216 /* Get pointer to port-linked sata device info structure */ 9217 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9218 if (sdinfo != NULL) { 9219 sdinfo->satadrv_state &= 9220 ~(SATA_STATE_PROBED | SATA_STATE_READY); 9221 sdinfo->satadrv_state |= SATA_STATE_PROBING; 9222 } else { 9223 /* No device to probe */ 9224 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9225 sata_device->satadev_addr.cport))); 9226 sata_device->satadev_type = SATA_DTYPE_NONE; 9227 sata_device->satadev_state = SATA_STATE_UNKNOWN; 9228 return (SATA_FAILURE); 9229 } 9230 /* 9231 * Need to issue both types of identify device command and 9232 * determine device type by examining retreived data/status. 9233 * First, ATA Identify Device. 9234 */ 9235 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 9236 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 9237 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9238 sata_device->satadev_addr.cport))); 9239 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 9240 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9241 if (rval == SATA_RETRY) { 9242 /* We may try to check for ATAPI device */ 9243 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 9244 /* 9245 * HBA supports ATAPI - try to issue Identify Packet 9246 * Device command. 9247 */ 9248 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 9249 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9250 } 9251 } 9252 if (rval == SATA_SUCCESS) { 9253 /* 9254 * Got something responding positively to ATA Identify Device 9255 * or to Identify Packet Device cmd. 9256 * Save last used device type. 9257 */ 9258 sata_device->satadev_type = new_sdinfo.satadrv_type; 9259 9260 /* save device info, if possible */ 9261 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9262 sata_device->satadev_addr.cport))); 9263 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9264 if (sdinfo == NULL) { 9265 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9266 sata_device->satadev_addr.cport))); 9267 return (SATA_FAILURE); 9268 } 9269 /* 9270 * Copy drive info into the port-linked drive info structure. 9271 */ 9272 *sdinfo = new_sdinfo; 9273 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9274 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9275 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9276 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9277 sata_device->satadev_addr.cport) = 9278 sdinfo->satadrv_type; 9279 else /* SATA_ADDR_DPMPORT */ 9280 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9281 sata_device->satadev_addr.cport, 9282 sata_device->satadev_addr.pmport) = 9283 sdinfo->satadrv_type; 9284 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9285 sata_device->satadev_addr.cport))); 9286 return (SATA_SUCCESS); 9287 } 9288 9289 /* 9290 * It may be SATA_RETRY or SATA_FAILURE return. 9291 * Looks like we cannot determine the device type at this time. 9292 */ 9293 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9294 sata_device->satadev_addr.cport))); 9295 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9296 if (sdinfo != NULL) { 9297 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 9298 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9299 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9300 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9301 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9302 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9303 sata_device->satadev_addr.cport) = 9304 SATA_DTYPE_UNKNOWN; 9305 else { 9306 /* SATA_ADDR_DPMPORT */ 9307 if ((SATA_PMULT_INFO(sata_hba_inst, 9308 sata_device->satadev_addr.cport) != NULL) && 9309 (SATA_PMPORT_INFO(sata_hba_inst, 9310 sata_device->satadev_addr.cport, 9311 sata_device->satadev_addr.pmport) != NULL)) 9312 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9313 sata_device->satadev_addr.cport, 9314 sata_device->satadev_addr.pmport) = 9315 SATA_DTYPE_UNKNOWN; 9316 } 9317 } 9318 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9319 sata_device->satadev_addr.cport))); 9320 return (rval); 9321 } 9322 9323 9324 /* 9325 * Get pointer to sata_drive_info structure. 9326 * 9327 * The sata_device has to contain address (cport, pmport and qualifier) for 9328 * specified sata_scsi structure. 9329 * 9330 * Returns NULL if device address is not valid for this HBA configuration. 9331 * Otherwise, returns a pointer to sata_drive_info structure. 9332 * 9333 * This function should be called with a port mutex held. 9334 */ 9335 static sata_drive_info_t * 9336 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 9337 sata_device_t *sata_device) 9338 { 9339 uint8_t cport = sata_device->satadev_addr.cport; 9340 uint8_t pmport = sata_device->satadev_addr.pmport; 9341 uint8_t qual = sata_device->satadev_addr.qual; 9342 9343 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9344 return (NULL); 9345 9346 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 9347 (SATA_STATE_PROBED | SATA_STATE_READY))) 9348 /* Port not probed yet */ 9349 return (NULL); 9350 9351 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 9352 return (NULL); 9353 9354 if (qual == SATA_ADDR_DCPORT) { 9355 /* Request for a device on a controller port */ 9356 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 9357 SATA_DTYPE_PMULT) 9358 /* Port multiplier attached */ 9359 return (NULL); 9360 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 9361 } 9362 if (qual == SATA_ADDR_DPMPORT) { 9363 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 9364 SATA_DTYPE_PMULT) 9365 return (NULL); 9366 9367 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 9368 return (NULL); 9369 9370 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 9371 } 9372 9373 /* we should not get here */ 9374 return (NULL); 9375 } 9376 9377 9378 /* 9379 * sata_identify_device. 9380 * Send Identify Device command to SATA HBA driver. 9381 * If command executes successfully, update sata_drive_info structure pointed 9382 * to by sdinfo argument, including Identify Device data. 9383 * If command fails, invalidate data in sata_drive_info. 9384 * 9385 * Cannot be called from interrupt level. 9386 * 9387 * Returns: 9388 * SATA_SUCCESS if the device was identified as a supported device, 9389 * SATA_RETRY if the device was not identified but could be retried, 9390 * SATA_FAILURE if the device was not identified and identify attempt 9391 * should not be retried. 9392 */ 9393 static int 9394 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 9395 sata_drive_info_t *sdinfo) 9396 { 9397 uint16_t cfg_word; 9398 int rval; 9399 9400 /* fetch device identify data */ 9401 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 9402 sdinfo)) != SATA_SUCCESS) 9403 goto fail_unknown; 9404 9405 cfg_word = sdinfo->satadrv_id.ai_config; 9406 9407 /* Set the correct device type */ 9408 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 9409 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 9410 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 9411 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 9412 case SATA_ATAPI_CDROM_DEV: 9413 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 9414 break; 9415 case SATA_ATAPI_SQACC_DEV: 9416 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 9417 break; 9418 default: 9419 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9420 } 9421 } else { 9422 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9423 } 9424 9425 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9426 if (sdinfo->satadrv_capacity == 0) { 9427 /* Non-LBA disk. Too bad... */ 9428 sata_log(sata_hba_inst, CE_WARN, 9429 "SATA disk device at port %d does not support LBA", 9430 sdinfo->satadrv_addr.cport); 9431 rval = SATA_FAILURE; 9432 goto fail_unknown; 9433 } 9434 } 9435 #if 0 9436 /* Left for historical reason */ 9437 /* 9438 * Some initial version of SATA spec indicated that at least 9439 * UDMA mode 4 has to be supported. It is not metioned in 9440 * SerialATA 2.6, so this restriction is removed. 9441 */ 9442 /* Check for Ultra DMA modes 6 through 0 being supported */ 9443 for (i = 6; i >= 0; --i) { 9444 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 9445 break; 9446 } 9447 9448 /* 9449 * At least UDMA 4 mode has to be supported. If mode 4 or 9450 * higher are not supported by the device, fail this 9451 * device. 9452 */ 9453 if (i < 4) { 9454 /* No required Ultra DMA mode supported */ 9455 sata_log(sata_hba_inst, CE_WARN, 9456 "SATA disk device at port %d does not support UDMA " 9457 "mode 4 or higher", sdinfo->satadrv_addr.cport); 9458 SATA_LOG_D((sata_hba_inst, CE_WARN, 9459 "mode 4 or higher required, %d supported", i)); 9460 rval = SATA_FAILURE; 9461 goto fail_unknown; 9462 } 9463 #endif 9464 9465 return (SATA_SUCCESS); 9466 9467 fail_unknown: 9468 /* Invalidate sata_drive_info ? */ 9469 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9470 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 9471 return (rval); 9472 } 9473 9474 /* 9475 * Log/display device information 9476 */ 9477 static void 9478 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 9479 sata_drive_info_t *sdinfo) 9480 { 9481 int valid_version; 9482 char msg_buf[MAXPATHLEN]; 9483 int i; 9484 9485 /* Show HBA path */ 9486 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 9487 9488 cmn_err(CE_CONT, "?%s :\n", msg_buf); 9489 9490 switch (sdinfo->satadrv_type) { 9491 case SATA_DTYPE_ATADISK: 9492 (void) sprintf(msg_buf, "SATA disk device at"); 9493 break; 9494 9495 case SATA_DTYPE_ATAPICD: 9496 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 9497 break; 9498 9499 case SATA_DTYPE_ATAPITAPE: 9500 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 9501 break; 9502 9503 case SATA_DTYPE_UNKNOWN: 9504 (void) sprintf(msg_buf, 9505 "Unsupported SATA device type (cfg 0x%x) at ", 9506 sdinfo->satadrv_id.ai_config); 9507 break; 9508 } 9509 9510 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 9511 cmn_err(CE_CONT, "?\t%s port %d\n", 9512 msg_buf, sdinfo->satadrv_addr.cport); 9513 else 9514 cmn_err(CE_CONT, "?\t%s port %d pmport %d\n", 9515 msg_buf, sdinfo->satadrv_addr.cport, 9516 sdinfo->satadrv_addr.pmport); 9517 9518 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 9519 sizeof (sdinfo->satadrv_id.ai_model)); 9520 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 9521 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 9522 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 9523 9524 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 9525 sizeof (sdinfo->satadrv_id.ai_fw)); 9526 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 9527 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 9528 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 9529 9530 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 9531 sizeof (sdinfo->satadrv_id.ai_drvser)); 9532 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 9533 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 9534 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9535 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9536 } else { 9537 /* 9538 * Some drives do not implement serial number and may 9539 * violate the spec by providing spaces rather than zeros 9540 * in serial number field. Scan the buffer to detect it. 9541 */ 9542 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 9543 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 9544 break; 9545 } 9546 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 9547 cmn_err(CE_CONT, "?\tserial number - none\n"); 9548 } else { 9549 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9550 } 9551 } 9552 9553 #ifdef SATA_DEBUG 9554 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9555 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 9556 int i; 9557 for (i = 14; i >= 2; i--) { 9558 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 9559 valid_version = i; 9560 break; 9561 } 9562 } 9563 cmn_err(CE_CONT, 9564 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 9565 valid_version, 9566 sdinfo->satadrv_id.ai_majorversion, 9567 sdinfo->satadrv_id.ai_minorversion); 9568 } 9569 #endif 9570 /* Log some info */ 9571 cmn_err(CE_CONT, "?\tsupported features:\n"); 9572 msg_buf[0] = '\0'; 9573 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9574 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 9575 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 9576 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 9577 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 9578 } 9579 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 9580 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 9581 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 9582 (void) strlcat(msg_buf, ", Native Command Queueing", 9583 MAXPATHLEN); 9584 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 9585 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 9586 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 9587 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 9588 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 9589 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 9590 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 9591 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 9592 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 9593 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 9594 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 9595 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 9596 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 9597 if (sdinfo->satadrv_features_support & 9598 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 9599 msg_buf[0] = '\0'; 9600 (void) snprintf(msg_buf, MAXPATHLEN, 9601 "Supported queue depth %d", 9602 sdinfo->satadrv_queue_depth); 9603 if (!(sata_func_enable & 9604 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 9605 (void) strlcat(msg_buf, 9606 " - queueing disabled globally", MAXPATHLEN); 9607 else if (sdinfo->satadrv_queue_depth > 9608 sdinfo->satadrv_max_queue_depth) { 9609 (void) snprintf(&msg_buf[strlen(msg_buf)], 9610 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 9611 (int)sdinfo->satadrv_max_queue_depth); 9612 } 9613 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 9614 } 9615 9616 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9617 #ifdef __i386 9618 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 9619 sdinfo->satadrv_capacity); 9620 #else 9621 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 9622 sdinfo->satadrv_capacity); 9623 #endif 9624 cmn_err(CE_CONT, "?%s", msg_buf); 9625 } 9626 } 9627 9628 9629 /* 9630 * sata_save_drive_settings extracts current setting of the device and stores 9631 * it for future reference, in case the device setup would need to be restored 9632 * after the device reset. 9633 * 9634 * For all devices read ahead and write cache settings are saved, if the 9635 * device supports these features at all. 9636 * For ATAPI devices the Removable Media Status Notification setting is saved. 9637 */ 9638 static void 9639 sata_save_drive_settings(sata_drive_info_t *sdinfo) 9640 { 9641 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) || 9642 (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 9643 9644 /* Current setting of Read Ahead (and Read Cache) */ 9645 if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD) 9646 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 9647 else 9648 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 9649 9650 /* Current setting of Write Cache */ 9651 if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE) 9652 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9653 else 9654 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9655 } 9656 9657 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9658 if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC) 9659 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 9660 else 9661 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 9662 } 9663 } 9664 9665 9666 /* 9667 * sata_check_capacity function determines a disk capacity 9668 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 9669 * 9670 * NOTE: CHS mode is not supported! If a device does not support LBA, 9671 * this function is not called. 9672 * 9673 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 9674 */ 9675 static uint64_t 9676 sata_check_capacity(sata_drive_info_t *sdinfo) 9677 { 9678 uint64_t capacity = 0; 9679 int i; 9680 9681 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 9682 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 9683 /* Capacity valid only for LBA-addressable disk devices */ 9684 return (0); 9685 9686 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 9687 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 9688 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 9689 /* LBA48 mode supported and enabled */ 9690 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 9691 SATA_DEV_F_LBA28; 9692 for (i = 3; i >= 0; --i) { 9693 capacity <<= 16; 9694 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 9695 } 9696 } else { 9697 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 9698 capacity <<= 16; 9699 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 9700 if (capacity >= 0x1000000) 9701 /* LBA28 mode */ 9702 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 9703 } 9704 return (capacity); 9705 } 9706 9707 9708 /* 9709 * Allocate consistent buffer for DMA transfer 9710 * 9711 * Cannot be called from interrupt level or with mutex held - it may sleep. 9712 * 9713 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 9714 */ 9715 static struct buf * 9716 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 9717 { 9718 struct scsi_address ap; 9719 struct buf *bp; 9720 ddi_dma_attr_t cur_dma_attr; 9721 9722 ASSERT(spx->txlt_sata_pkt != NULL); 9723 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 9724 ap.a_target = SATA_TO_SCSI_TARGET( 9725 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 9726 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 9727 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 9728 ap.a_lun = 0; 9729 9730 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 9731 B_READ, SLEEP_FUNC, NULL); 9732 9733 if (bp != NULL) { 9734 /* Allocate DMA resources for this buffer */ 9735 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 9736 /* 9737 * We use a local version of the dma_attr, to account 9738 * for a device addressing limitations. 9739 * sata_adjust_dma_attr() will handle sdinfo == NULL which 9740 * will cause dma attributes to be adjusted to a lowest 9741 * acceptable level. 9742 */ 9743 sata_adjust_dma_attr(NULL, 9744 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 9745 9746 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 9747 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 9748 scsi_free_consistent_buf(bp); 9749 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9750 bp = NULL; 9751 } 9752 } 9753 return (bp); 9754 } 9755 9756 /* 9757 * Release local buffer (consistent buffer for DMA transfer) allocated 9758 * via sata_alloc_local_buffer(). 9759 */ 9760 static void 9761 sata_free_local_buffer(sata_pkt_txlate_t *spx) 9762 { 9763 ASSERT(spx->txlt_sata_pkt != NULL); 9764 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 9765 9766 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 9767 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 9768 9769 if (spx->txlt_buf_dma_handle != NULL) { 9770 /* Free DMA resources */ 9771 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 9772 ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 9773 spx->txlt_buf_dma_handle = 0; 9774 9775 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 9776 kmem_free(spx->txlt_dma_cookie_list, 9777 spx->txlt_dma_cookie_list_len * 9778 sizeof (ddi_dma_cookie_t)); 9779 spx->txlt_dma_cookie_list = NULL; 9780 spx->txlt_dma_cookie_list_len = 0; 9781 } 9782 } 9783 9784 /* Free buffer */ 9785 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 9786 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9787 } 9788 9789 9790 9791 9792 /* 9793 * Allocate sata_pkt 9794 * Pkt structure version and embedded strcutures version are initialized. 9795 * sata_pkt and sata_pkt_txlate structures are cross-linked. 9796 * 9797 * Since this may be called in interrupt context by sata_scsi_init_pkt, 9798 * callback argument determines if it can sleep or not. 9799 * Hence, it should not be called from interrupt context. 9800 * 9801 * If successful, non-NULL pointer to a sata pkt is returned. 9802 * Upon failure, NULL pointer is returned. 9803 */ 9804 static sata_pkt_t * 9805 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 9806 { 9807 sata_pkt_t *spkt; 9808 int kmsflag; 9809 9810 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 9811 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 9812 if (spkt == NULL) { 9813 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9814 "sata_pkt_alloc: failed")); 9815 return (NULL); 9816 } 9817 spkt->satapkt_rev = SATA_PKT_REV; 9818 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 9819 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 9820 spkt->satapkt_framework_private = spx; 9821 spx->txlt_sata_pkt = spkt; 9822 return (spkt); 9823 } 9824 9825 /* 9826 * Free sata pkt allocated via sata_pkt_alloc() 9827 */ 9828 static void 9829 sata_pkt_free(sata_pkt_txlate_t *spx) 9830 { 9831 ASSERT(spx->txlt_sata_pkt != NULL); 9832 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 9833 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 9834 spx->txlt_sata_pkt = NULL; 9835 } 9836 9837 9838 /* 9839 * Adjust DMA attributes. 9840 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 9841 * from 8 bits to 16 bits, depending on a command being used. 9842 * Limiting max block count arbitrarily to 256 for all read/write 9843 * commands may affects performance, so check both the device and 9844 * controller capability before adjusting dma attributes. 9845 */ 9846 void 9847 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 9848 ddi_dma_attr_t *adj_dma_attr) 9849 { 9850 uint32_t count_max; 9851 9852 /* Copy original attributes */ 9853 *adj_dma_attr = *dma_attr; 9854 /* 9855 * Things to consider: device addressing capability, 9856 * "excessive" controller DMA capabilities. 9857 * If a device is being probed/initialized, there are 9858 * no device info - use default limits then. 9859 */ 9860 if (sdinfo == NULL) { 9861 count_max = dma_attr->dma_attr_granular * 0x100; 9862 if (dma_attr->dma_attr_count_max > count_max) 9863 adj_dma_attr->dma_attr_count_max = count_max; 9864 if (dma_attr->dma_attr_maxxfer > count_max) 9865 adj_dma_attr->dma_attr_maxxfer = count_max; 9866 return; 9867 } 9868 9869 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9870 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 9871 /* 9872 * 16-bit sector count may be used - we rely on 9873 * the assumption that only read and write cmds 9874 * will request more than 256 sectors worth of data 9875 */ 9876 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 9877 } else { 9878 /* 9879 * 8-bit sector count will be used - default limits 9880 * for dma attributes 9881 */ 9882 count_max = adj_dma_attr->dma_attr_granular * 0x100; 9883 } 9884 /* 9885 * Adjust controler dma attributes, if necessary 9886 */ 9887 if (dma_attr->dma_attr_count_max > count_max) 9888 adj_dma_attr->dma_attr_count_max = count_max; 9889 if (dma_attr->dma_attr_maxxfer > count_max) 9890 adj_dma_attr->dma_attr_maxxfer = count_max; 9891 } 9892 } 9893 9894 9895 /* 9896 * Allocate DMA resources for the buffer 9897 * This function handles initial DMA resource allocation as well as 9898 * DMA window shift and may be called repeatedly for the same DMA window 9899 * until all DMA cookies in the DMA window are processed. 9900 * To guarantee that there is always a coherent set of cookies to process 9901 * by SATA HBA driver (observing alignment, device granularity, etc.), 9902 * the number of slots for DMA cookies is equal to lesser of a number of 9903 * cookies in a DMA window and a max number of scatter/gather entries. 9904 * 9905 * Returns DDI_SUCCESS upon successful operation. 9906 * Return failure code of a failing command or DDI_FAILURE when 9907 * internal cleanup failed. 9908 */ 9909 static int 9910 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 9911 int (*callback)(caddr_t), caddr_t arg, 9912 ddi_dma_attr_t *cur_dma_attr) 9913 { 9914 int rval; 9915 off_t offset; 9916 size_t size; 9917 int max_sg_len, req_len, i; 9918 uint_t dma_flags; 9919 struct buf *bp; 9920 uint64_t cur_txfer_len; 9921 9922 9923 ASSERT(spx->txlt_sata_pkt != NULL); 9924 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9925 ASSERT(bp != NULL); 9926 9927 9928 if (spx->txlt_buf_dma_handle == NULL) { 9929 /* 9930 * No DMA resources allocated so far - this is a first call 9931 * for this sata pkt. 9932 */ 9933 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 9934 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 9935 9936 if (rval != DDI_SUCCESS) { 9937 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9938 "sata_dma_buf_setup: no buf DMA resources %x", 9939 rval)); 9940 return (rval); 9941 } 9942 9943 if (bp->b_flags & B_READ) 9944 dma_flags = DDI_DMA_READ; 9945 else 9946 dma_flags = DDI_DMA_WRITE; 9947 9948 if (flags & PKT_CONSISTENT) 9949 dma_flags |= DDI_DMA_CONSISTENT; 9950 9951 if (flags & PKT_DMA_PARTIAL) 9952 dma_flags |= DDI_DMA_PARTIAL; 9953 9954 /* 9955 * Check buffer alignment and size against dma attributes 9956 * Consider dma_attr_align only. There may be requests 9957 * with the size lower than device granularity, but they 9958 * will not read/write from/to the device, so no adjustment 9959 * is necessary. The dma_attr_minxfer theoretically should 9960 * be considered, but no HBA driver is checking it. 9961 */ 9962 if (IS_P2ALIGNED(bp->b_un.b_addr, 9963 cur_dma_attr->dma_attr_align)) { 9964 rval = ddi_dma_buf_bind_handle( 9965 spx->txlt_buf_dma_handle, 9966 bp, dma_flags, callback, arg, 9967 &spx->txlt_dma_cookie, 9968 &spx->txlt_curwin_num_dma_cookies); 9969 } else { /* Buffer is not aligned */ 9970 9971 int (*ddicallback)(caddr_t); 9972 size_t bufsz; 9973 9974 /* Check id sleeping is allowed */ 9975 ddicallback = (callback == NULL_FUNC) ? 9976 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 9977 9978 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9979 "mis-aligned buffer: addr=0x%p, cnt=%lu", 9980 (void *)bp->b_un.b_addr, bp->b_bcount); 9981 9982 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 9983 /* 9984 * CPU will need to access data in the buffer 9985 * (for copying) so map it. 9986 */ 9987 bp_mapin(bp); 9988 9989 ASSERT(spx->txlt_tmp_buf == NULL); 9990 9991 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 9992 rval = ddi_dma_mem_alloc( 9993 spx->txlt_buf_dma_handle, 9994 bp->b_bcount, 9995 &sata_acc_attr, 9996 DDI_DMA_STREAMING, 9997 ddicallback, NULL, 9998 &spx->txlt_tmp_buf, 9999 &bufsz, 10000 &spx->txlt_tmp_buf_handle); 10001 10002 if (rval != DDI_SUCCESS) { 10003 /* DMA mapping failed */ 10004 (void) ddi_dma_free_handle( 10005 &spx->txlt_buf_dma_handle); 10006 spx->txlt_buf_dma_handle = NULL; 10007 #ifdef SATA_DEBUG 10008 mbuffail_count++; 10009 #endif 10010 SATADBG1(SATA_DBG_DMA_SETUP, 10011 spx->txlt_sata_hba_inst, 10012 "sata_dma_buf_setup: " 10013 "buf dma mem alloc failed %x\n", rval); 10014 return (rval); 10015 } 10016 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 10017 cur_dma_attr->dma_attr_align)); 10018 10019 #ifdef SATA_DEBUG 10020 mbuf_count++; 10021 10022 if (bp->b_bcount != bufsz) 10023 /* 10024 * This will require special handling, because 10025 * DMA cookies will be based on the temporary 10026 * buffer size, not the original buffer 10027 * b_bcount, so the residue may have to 10028 * be counted differently. 10029 */ 10030 SATADBG2(SATA_DBG_DMA_SETUP, 10031 spx->txlt_sata_hba_inst, 10032 "sata_dma_buf_setup: bp size %x != " 10033 "bufsz %x\n", bp->b_bcount, bufsz); 10034 #endif 10035 if (dma_flags & DDI_DMA_WRITE) { 10036 /* 10037 * Write operation - copy data into 10038 * an aligned temporary buffer. Buffer will be 10039 * synced for device by ddi_dma_addr_bind_handle 10040 */ 10041 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 10042 bp->b_bcount); 10043 } 10044 10045 rval = ddi_dma_addr_bind_handle( 10046 spx->txlt_buf_dma_handle, 10047 NULL, 10048 spx->txlt_tmp_buf, 10049 bufsz, dma_flags, ddicallback, 0, 10050 &spx->txlt_dma_cookie, 10051 &spx->txlt_curwin_num_dma_cookies); 10052 } 10053 10054 switch (rval) { 10055 case DDI_DMA_PARTIAL_MAP: 10056 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10057 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 10058 /* 10059 * Partial DMA mapping. 10060 * Retrieve number of DMA windows for this request. 10061 */ 10062 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 10063 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 10064 if (spx->txlt_tmp_buf != NULL) { 10065 ddi_dma_mem_free( 10066 &spx->txlt_tmp_buf_handle); 10067 spx->txlt_tmp_buf = NULL; 10068 } 10069 (void) ddi_dma_unbind_handle( 10070 spx->txlt_buf_dma_handle); 10071 (void) ddi_dma_free_handle( 10072 &spx->txlt_buf_dma_handle); 10073 spx->txlt_buf_dma_handle = NULL; 10074 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10075 "sata_dma_buf_setup: numwin failed\n")); 10076 return (DDI_FAILURE); 10077 } 10078 SATADBG2(SATA_DBG_DMA_SETUP, 10079 spx->txlt_sata_hba_inst, 10080 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 10081 spx->txlt_num_dma_win, 10082 spx->txlt_curwin_num_dma_cookies); 10083 spx->txlt_cur_dma_win = 0; 10084 break; 10085 10086 case DDI_DMA_MAPPED: 10087 /* DMA fully mapped */ 10088 spx->txlt_num_dma_win = 1; 10089 spx->txlt_cur_dma_win = 0; 10090 SATADBG1(SATA_DBG_DMA_SETUP, 10091 spx->txlt_sata_hba_inst, 10092 "sata_dma_buf_setup: windows: 1 " 10093 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 10094 break; 10095 10096 default: 10097 /* DMA mapping failed */ 10098 if (spx->txlt_tmp_buf != NULL) { 10099 ddi_dma_mem_free( 10100 &spx->txlt_tmp_buf_handle); 10101 spx->txlt_tmp_buf = NULL; 10102 } 10103 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10104 spx->txlt_buf_dma_handle = NULL; 10105 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10106 "sata_dma_buf_setup: buf dma handle binding " 10107 "failed %x\n", rval)); 10108 return (rval); 10109 } 10110 spx->txlt_curwin_processed_dma_cookies = 0; 10111 spx->txlt_dma_cookie_list = NULL; 10112 } else { 10113 /* 10114 * DMA setup is reused. Check if we need to process more 10115 * cookies in current window, or to get next window, if any. 10116 */ 10117 10118 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 10119 spx->txlt_curwin_num_dma_cookies); 10120 10121 if (spx->txlt_curwin_processed_dma_cookies == 10122 spx->txlt_curwin_num_dma_cookies) { 10123 /* 10124 * All cookies from current DMA window were processed. 10125 * Get next DMA window. 10126 */ 10127 spx->txlt_cur_dma_win++; 10128 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 10129 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 10130 spx->txlt_cur_dma_win, &offset, &size, 10131 &spx->txlt_dma_cookie, 10132 &spx->txlt_curwin_num_dma_cookies); 10133 spx->txlt_curwin_processed_dma_cookies = 0; 10134 } else { 10135 /* No more windows! End of request! */ 10136 /* What to do? - panic for now */ 10137 ASSERT(spx->txlt_cur_dma_win >= 10138 spx->txlt_num_dma_win); 10139 10140 spx->txlt_curwin_num_dma_cookies = 0; 10141 spx->txlt_curwin_processed_dma_cookies = 0; 10142 spx->txlt_sata_pkt-> 10143 satapkt_cmd.satacmd_num_dma_cookies = 0; 10144 return (DDI_SUCCESS); 10145 } 10146 } 10147 } 10148 /* There better be at least one DMA cookie outstanding */ 10149 ASSERT((spx->txlt_curwin_num_dma_cookies - 10150 spx->txlt_curwin_processed_dma_cookies) > 0); 10151 10152 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 10153 /* The default cookie slot was used in previous run */ 10154 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 10155 spx->txlt_dma_cookie_list = NULL; 10156 spx->txlt_dma_cookie_list_len = 0; 10157 } 10158 if (spx->txlt_curwin_processed_dma_cookies == 0) { 10159 /* 10160 * Processing a new DMA window - set-up dma cookies list. 10161 * We may reuse previously allocated cookie array if it is 10162 * possible. 10163 */ 10164 if (spx->txlt_dma_cookie_list != NULL && 10165 spx->txlt_dma_cookie_list_len < 10166 spx->txlt_curwin_num_dma_cookies) { 10167 /* 10168 * New DMA window contains more cookies than 10169 * the previous one. We need larger cookie list - free 10170 * the old one. 10171 */ 10172 (void) kmem_free(spx->txlt_dma_cookie_list, 10173 spx->txlt_dma_cookie_list_len * 10174 sizeof (ddi_dma_cookie_t)); 10175 spx->txlt_dma_cookie_list = NULL; 10176 spx->txlt_dma_cookie_list_len = 0; 10177 } 10178 if (spx->txlt_dma_cookie_list == NULL) { 10179 /* 10180 * Calculate lesser of number of cookies in this 10181 * DMA window and number of s/g entries. 10182 */ 10183 max_sg_len = cur_dma_attr->dma_attr_sgllen; 10184 req_len = MIN(max_sg_len, 10185 spx->txlt_curwin_num_dma_cookies); 10186 10187 /* Allocate new dma cookie array if necessary */ 10188 if (req_len == 1) { 10189 /* Only one cookie - no need for a list */ 10190 spx->txlt_dma_cookie_list = 10191 &spx->txlt_dma_cookie; 10192 spx->txlt_dma_cookie_list_len = 1; 10193 } else { 10194 /* 10195 * More than one cookie - try to allocate space. 10196 */ 10197 spx->txlt_dma_cookie_list = kmem_zalloc( 10198 sizeof (ddi_dma_cookie_t) * req_len, 10199 callback == NULL_FUNC ? KM_NOSLEEP : 10200 KM_SLEEP); 10201 if (spx->txlt_dma_cookie_list == NULL) { 10202 SATADBG1(SATA_DBG_DMA_SETUP, 10203 spx->txlt_sata_hba_inst, 10204 "sata_dma_buf_setup: cookie list " 10205 "allocation failed\n", NULL); 10206 /* 10207 * We could not allocate space for 10208 * neccessary number of dma cookies in 10209 * this window, so we fail this request. 10210 * Next invocation would try again to 10211 * allocate space for cookie list. 10212 * Note:Packet residue was not modified. 10213 */ 10214 return (DDI_DMA_NORESOURCES); 10215 } else { 10216 spx->txlt_dma_cookie_list_len = req_len; 10217 } 10218 } 10219 } 10220 /* 10221 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 10222 * First cookie was already fetched. 10223 */ 10224 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 10225 cur_txfer_len = 10226 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 10227 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 10228 spx->txlt_curwin_processed_dma_cookies++; 10229 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 10230 (i < spx->txlt_curwin_num_dma_cookies); i++) { 10231 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10232 &spx->txlt_dma_cookie_list[i]); 10233 cur_txfer_len += 10234 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10235 spx->txlt_curwin_processed_dma_cookies++; 10236 spx->txlt_sata_pkt-> 10237 satapkt_cmd.satacmd_num_dma_cookies += 1; 10238 } 10239 } else { 10240 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10241 "sata_dma_buf_setup: sliding within DMA window, " 10242 "cur cookie %d, total cookies %d\n", 10243 spx->txlt_curwin_processed_dma_cookies, 10244 spx->txlt_curwin_num_dma_cookies); 10245 10246 /* 10247 * Not all cookies from the current dma window were used because 10248 * of s/g limitation. 10249 * There is no need to re-size the list - it was set at 10250 * optimal size, or only default entry is used (s/g = 1). 10251 */ 10252 if (spx->txlt_dma_cookie_list == NULL) { 10253 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 10254 spx->txlt_dma_cookie_list_len = 1; 10255 } 10256 /* 10257 * Since we are processing remaining cookies in a DMA window, 10258 * there may be less of them than the number of entries in the 10259 * current dma cookie list. 10260 */ 10261 req_len = MIN(spx->txlt_dma_cookie_list_len, 10262 (spx->txlt_curwin_num_dma_cookies - 10263 spx->txlt_curwin_processed_dma_cookies)); 10264 10265 /* Fetch the next batch of cookies */ 10266 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 10267 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10268 &spx->txlt_dma_cookie_list[i]); 10269 cur_txfer_len += 10270 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10271 spx->txlt_sata_pkt-> 10272 satapkt_cmd.satacmd_num_dma_cookies++; 10273 spx->txlt_curwin_processed_dma_cookies++; 10274 } 10275 } 10276 10277 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 10278 10279 /* Point sata_cmd to the cookie list */ 10280 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 10281 &spx->txlt_dma_cookie_list[0]; 10282 10283 /* Remember number of DMA cookies passed in sata packet */ 10284 spx->txlt_num_dma_cookies = 10285 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 10286 10287 ASSERT(cur_txfer_len != 0); 10288 if (cur_txfer_len <= bp->b_bcount) 10289 spx->txlt_total_residue -= cur_txfer_len; 10290 else { 10291 /* 10292 * Temporary DMA buffer has been padded by 10293 * ddi_dma_mem_alloc()! 10294 * This requires special handling, because DMA cookies are 10295 * based on the temporary buffer size, not the b_bcount, 10296 * and we have extra bytes to transfer - but the packet 10297 * residue has to stay correct because we will copy only 10298 * the requested number of bytes. 10299 */ 10300 spx->txlt_total_residue -= bp->b_bcount; 10301 } 10302 10303 return (DDI_SUCCESS); 10304 } 10305 10306 /* 10307 * Common routine for releasing DMA resources 10308 */ 10309 static void 10310 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 10311 { 10312 if (spx->txlt_buf_dma_handle != NULL) { 10313 if (spx->txlt_tmp_buf != NULL) { 10314 /* 10315 * Intermediate DMA buffer was allocated. 10316 * Free allocated buffer and associated access handle. 10317 */ 10318 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 10319 spx->txlt_tmp_buf = NULL; 10320 } 10321 /* 10322 * Free DMA resources - cookies and handles 10323 */ 10324 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 10325 if (spx->txlt_dma_cookie_list != NULL) { 10326 if (spx->txlt_dma_cookie_list != 10327 &spx->txlt_dma_cookie) { 10328 (void) kmem_free(spx->txlt_dma_cookie_list, 10329 spx->txlt_dma_cookie_list_len * 10330 sizeof (ddi_dma_cookie_t)); 10331 spx->txlt_dma_cookie_list = NULL; 10332 } 10333 } 10334 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 10335 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10336 spx->txlt_buf_dma_handle = NULL; 10337 } 10338 } 10339 10340 /* 10341 * Free DMA resources 10342 * Used by the HBA driver to release DMA resources that it does not use. 10343 * 10344 * Returns Void 10345 */ 10346 void 10347 sata_free_dma_resources(sata_pkt_t *sata_pkt) 10348 { 10349 sata_pkt_txlate_t *spx; 10350 10351 if (sata_pkt == NULL) 10352 return; 10353 10354 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 10355 10356 sata_common_free_dma_rsrcs(spx); 10357 } 10358 10359 /* 10360 * Fetch Device Identify data. 10361 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 10362 * command to a device and get the device identify data. 10363 * The device_info structure has to be set to device type (for selecting proper 10364 * device identify command). 10365 * 10366 * Returns: 10367 * SATA_SUCCESS if cmd succeeded 10368 * SATA_RETRY if cmd was rejected and could be retried, 10369 * SATA_FAILURE if cmd failed and should not be retried (port error) 10370 * 10371 * Cannot be called in an interrupt context. 10372 */ 10373 10374 static int 10375 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 10376 sata_drive_info_t *sdinfo) 10377 { 10378 struct buf *bp; 10379 sata_pkt_t *spkt; 10380 sata_cmd_t *scmd; 10381 sata_pkt_txlate_t *spx; 10382 int rval; 10383 10384 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10385 spx->txlt_sata_hba_inst = sata_hba_inst; 10386 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10387 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10388 if (spkt == NULL) { 10389 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10390 return (SATA_RETRY); /* may retry later */ 10391 } 10392 /* address is needed now */ 10393 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10394 10395 /* 10396 * Allocate buffer for Identify Data return data 10397 */ 10398 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 10399 if (bp == NULL) { 10400 sata_pkt_free(spx); 10401 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10402 SATA_LOG_D((sata_hba_inst, CE_WARN, 10403 "sata_fetch_device_identify_data: " 10404 "cannot allocate buffer for ID")); 10405 return (SATA_RETRY); /* may retry later */ 10406 } 10407 10408 /* Fill sata_pkt */ 10409 sdinfo->satadrv_state = SATA_STATE_PROBING; 10410 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10411 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10412 /* Synchronous mode, no callback */ 10413 spkt->satapkt_comp = NULL; 10414 /* Timeout 30s */ 10415 spkt->satapkt_time = sata_default_pkt_time; 10416 10417 scmd = &spkt->satapkt_cmd; 10418 scmd->satacmd_bp = bp; 10419 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10420 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10421 10422 /* Build Identify Device cmd in the sata_pkt */ 10423 scmd->satacmd_addr_type = 0; /* N/A */ 10424 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 10425 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 10426 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 10427 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 10428 scmd->satacmd_features_reg = 0; /* N/A */ 10429 scmd->satacmd_device_reg = 0; /* Always device 0 */ 10430 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 10431 /* Identify Packet Device cmd */ 10432 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 10433 } else { 10434 /* Identify Device cmd - mandatory for all other devices */ 10435 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 10436 } 10437 10438 /* Send pkt to SATA HBA driver */ 10439 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 10440 10441 #ifdef SATA_INJECT_FAULTS 10442 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10443 #endif 10444 10445 if (rval == SATA_TRAN_ACCEPTED && 10446 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10447 if (spx->txlt_buf_dma_handle != NULL) { 10448 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10449 DDI_DMA_SYNC_FORKERNEL); 10450 ASSERT(rval == DDI_SUCCESS); 10451 } 10452 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 10453 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 10454 SATA_LOG_D((sata_hba_inst, CE_WARN, 10455 "SATA disk device at port %d - " 10456 "partial Identify Data", 10457 sdinfo->satadrv_addr.cport)); 10458 rval = SATA_RETRY; /* may retry later */ 10459 goto fail; 10460 } 10461 /* Update sata_drive_info */ 10462 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 10463 sizeof (sata_id_t)); 10464 10465 sdinfo->satadrv_features_support = 0; 10466 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10467 /* 10468 * Retrieve capacity (disks only) and addressing mode 10469 */ 10470 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 10471 } else { 10472 /* 10473 * For ATAPI devices one would have to issue 10474 * Get Capacity cmd for media capacity. Not here. 10475 */ 10476 sdinfo->satadrv_capacity = 0; 10477 /* 10478 * Check what cdb length is supported 10479 */ 10480 if ((sdinfo->satadrv_id.ai_config & 10481 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 10482 sdinfo->satadrv_atapi_cdb_len = 16; 10483 else 10484 sdinfo->satadrv_atapi_cdb_len = 12; 10485 } 10486 /* Setup supported features flags */ 10487 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 10488 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 10489 10490 /* Check for SATA GEN and NCQ support */ 10491 if (sdinfo->satadrv_id.ai_satacap != 0 && 10492 sdinfo->satadrv_id.ai_satacap != 0xffff) { 10493 /* SATA compliance */ 10494 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 10495 sdinfo->satadrv_features_support |= 10496 SATA_DEV_F_NCQ; 10497 if (sdinfo->satadrv_id.ai_satacap & 10498 (SATA_1_SPEED | SATA_2_SPEED)) { 10499 if (sdinfo->satadrv_id.ai_satacap & 10500 SATA_2_SPEED) 10501 sdinfo->satadrv_features_support |= 10502 SATA_DEV_F_SATA2; 10503 if (sdinfo->satadrv_id.ai_satacap & 10504 SATA_1_SPEED) 10505 sdinfo->satadrv_features_support |= 10506 SATA_DEV_F_SATA1; 10507 } else { 10508 sdinfo->satadrv_features_support |= 10509 SATA_DEV_F_SATA1; 10510 } 10511 } 10512 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 10513 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 10514 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 10515 10516 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 10517 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 10518 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 10519 ++sdinfo->satadrv_queue_depth; 10520 /* Adjust according to controller capabilities */ 10521 sdinfo->satadrv_max_queue_depth = MIN( 10522 sdinfo->satadrv_queue_depth, 10523 SATA_QDEPTH(sata_hba_inst)); 10524 /* Adjust according to global queue depth limit */ 10525 sdinfo->satadrv_max_queue_depth = MIN( 10526 sdinfo->satadrv_max_queue_depth, 10527 sata_current_max_qdepth); 10528 if (sdinfo->satadrv_max_queue_depth == 0) 10529 sdinfo->satadrv_max_queue_depth = 1; 10530 } else 10531 sdinfo->satadrv_max_queue_depth = 1; 10532 10533 rval = SATA_SUCCESS; 10534 } else { 10535 /* 10536 * Woops, no Identify Data. 10537 */ 10538 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 10539 rval = SATA_RETRY; /* may retry later */ 10540 } else if (rval == SATA_TRAN_ACCEPTED) { 10541 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 10542 spkt->satapkt_reason == SATA_PKT_ABORTED || 10543 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 10544 spkt->satapkt_reason == SATA_PKT_RESET) 10545 rval = SATA_RETRY; /* may retry later */ 10546 else 10547 rval = SATA_FAILURE; 10548 } else { 10549 rval = SATA_FAILURE; 10550 } 10551 } 10552 fail: 10553 /* Free allocated resources */ 10554 sata_free_local_buffer(spx); 10555 sata_pkt_free(spx); 10556 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10557 10558 return (rval); 10559 } 10560 10561 10562 /* 10563 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 10564 * UDMA mode is checked first, followed by MWDMA mode. 10565 * set correctly, so this function is setting it to the highest supported level. 10566 * Older SATA spec required that the device supports at least DMA 4 mode and 10567 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 10568 * restriction has been removed. 10569 * 10570 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 10571 * Returns SATA_FAILURE if proper DMA mode could not be selected. 10572 * 10573 * NOTE: This function should be called only if DMA mode is supported. 10574 */ 10575 static int 10576 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 10577 { 10578 sata_pkt_t *spkt; 10579 sata_cmd_t *scmd; 10580 sata_pkt_txlate_t *spx; 10581 int i, mode; 10582 uint8_t subcmd; 10583 int rval = SATA_SUCCESS; 10584 10585 ASSERT(sdinfo != NULL); 10586 ASSERT(sata_hba_inst != NULL); 10587 10588 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 10589 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 10590 /* Find highest Ultra DMA mode supported */ 10591 for (mode = 6; mode >= 0; --mode) { 10592 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 10593 break; 10594 } 10595 #if 0 10596 /* Left for historical reasons */ 10597 /* 10598 * Some initial version of SATA spec indicated that at least 10599 * UDMA mode 4 has to be supported. It is not mentioned in 10600 * SerialATA 2.6, so this restriction is removed. 10601 */ 10602 if (mode < 4) 10603 return (SATA_FAILURE); 10604 #endif 10605 /* Find UDMA mode currently selected */ 10606 for (i = 6; i >= 0; --i) { 10607 if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8))) 10608 break; 10609 } 10610 if (i >= mode) 10611 /* Nothing to do */ 10612 return (SATA_SUCCESS); 10613 10614 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 10615 10616 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 10617 /* Find highest MultiWord DMA mode supported */ 10618 for (mode = 2; mode >= 0; --mode) { 10619 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 10620 break; 10621 } 10622 /* Find highest MultiWord DMA mode selected */ 10623 for (i = 2; i >= 0; --i) { 10624 if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8))) 10625 break; 10626 } 10627 if (i >= mode) 10628 /* Nothing to do */ 10629 return (SATA_SUCCESS); 10630 10631 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 10632 } else 10633 return (SATA_SUCCESS); 10634 10635 /* 10636 * Set DMA mode via SET FEATURES COMMAND. 10637 * Prepare packet for SET FEATURES COMMAND. 10638 */ 10639 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10640 spx->txlt_sata_hba_inst = sata_hba_inst; 10641 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10642 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10643 if (spkt == NULL) { 10644 SATA_LOG_D((sata_hba_inst, CE_WARN, 10645 "sata_set_dma_mode: could not set DMA mode %", mode)); 10646 rval = SATA_FAILURE; 10647 goto done; 10648 } 10649 /* Fill sata_pkt */ 10650 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10651 /* Timeout 30s */ 10652 spkt->satapkt_time = sata_default_pkt_time; 10653 /* Synchronous mode, no callback, interrupts */ 10654 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10655 spkt->satapkt_comp = NULL; 10656 scmd = &spkt->satapkt_cmd; 10657 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10658 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10659 scmd->satacmd_addr_type = 0; 10660 scmd->satacmd_device_reg = 0; 10661 scmd->satacmd_status_reg = 0; 10662 scmd->satacmd_error_reg = 0; 10663 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10664 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 10665 scmd->satacmd_sec_count_lsb = subcmd | mode; 10666 10667 /* Transfer command to HBA */ 10668 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 10669 spkt) != SATA_TRAN_ACCEPTED || 10670 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 10671 /* Pkt execution failed */ 10672 rval = SATA_FAILURE; 10673 } 10674 done: 10675 10676 /* Free allocated resources */ 10677 if (spkt != NULL) 10678 sata_pkt_free(spx); 10679 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10680 10681 return (rval); 10682 } 10683 10684 10685 /* 10686 * Set device caching mode. 10687 * One of the following operations should be specified: 10688 * SATAC_SF_ENABLE_READ_AHEAD 10689 * SATAC_SF_DISABLE_READ_AHEAD 10690 * SATAC_SF_ENABLE_WRITE_CACHE 10691 * SATAC_SF_DISABLE_WRITE_CACHE 10692 * 10693 * If operation fails, system log messgage is emitted. 10694 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 10695 * command was sent but did not succeed, and SATA_FAILURE otherwise. 10696 */ 10697 10698 static int 10699 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10700 int cache_op) 10701 { 10702 sata_pkt_t *spkt; 10703 sata_cmd_t *scmd; 10704 sata_pkt_txlate_t *spx; 10705 int rval = SATA_SUCCESS; 10706 int hba_rval; 10707 char *infop; 10708 10709 ASSERT(sdinfo != NULL); 10710 ASSERT(sata_hba_inst != NULL); 10711 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 10712 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 10713 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 10714 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 10715 10716 10717 /* Prepare packet for SET FEATURES COMMAND */ 10718 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10719 spx->txlt_sata_hba_inst = sata_hba_inst; 10720 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10721 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10722 if (spkt == NULL) { 10723 rval = SATA_FAILURE; 10724 goto failure; 10725 } 10726 /* Fill sata_pkt */ 10727 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10728 /* Timeout 30s */ 10729 spkt->satapkt_time = sata_default_pkt_time; 10730 /* Synchronous mode, no callback, interrupts */ 10731 spkt->satapkt_op_mode = 10732 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10733 spkt->satapkt_comp = NULL; 10734 scmd = &spkt->satapkt_cmd; 10735 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10736 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10737 scmd->satacmd_addr_type = 0; 10738 scmd->satacmd_device_reg = 0; 10739 scmd->satacmd_status_reg = 0; 10740 scmd->satacmd_error_reg = 0; 10741 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10742 scmd->satacmd_features_reg = cache_op; 10743 10744 /* Transfer command to HBA */ 10745 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 10746 SATA_DIP(sata_hba_inst), spkt); 10747 10748 #ifdef SATA_INJECT_FAULTS 10749 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10750 #endif 10751 10752 if ((hba_rval != SATA_TRAN_ACCEPTED) || 10753 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10754 /* Pkt execution failed */ 10755 switch (cache_op) { 10756 case SATAC_SF_ENABLE_READ_AHEAD: 10757 infop = "enabling read ahead failed"; 10758 break; 10759 case SATAC_SF_DISABLE_READ_AHEAD: 10760 infop = "disabling read ahead failed"; 10761 break; 10762 case SATAC_SF_ENABLE_WRITE_CACHE: 10763 infop = "enabling write cache failed"; 10764 break; 10765 case SATAC_SF_DISABLE_WRITE_CACHE: 10766 infop = "disabling write cache failed"; 10767 break; 10768 } 10769 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10770 rval = SATA_RETRY; 10771 } 10772 failure: 10773 /* Free allocated resources */ 10774 if (spkt != NULL) 10775 sata_pkt_free(spx); 10776 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10777 return (rval); 10778 } 10779 10780 /* 10781 * Set Removable Media Status Notification (enable/disable) 10782 * state == 0 , disable 10783 * state != 0 , enable 10784 * 10785 * If operation fails, system log messgage is emitted. 10786 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 10787 */ 10788 10789 static int 10790 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10791 int state) 10792 { 10793 sata_pkt_t *spkt; 10794 sata_cmd_t *scmd; 10795 sata_pkt_txlate_t *spx; 10796 int rval = SATA_SUCCESS; 10797 char *infop; 10798 10799 ASSERT(sdinfo != NULL); 10800 ASSERT(sata_hba_inst != NULL); 10801 10802 /* Prepare packet for SET FEATURES COMMAND */ 10803 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10804 spx->txlt_sata_hba_inst = sata_hba_inst; 10805 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10806 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10807 if (spkt == NULL) { 10808 rval = SATA_FAILURE; 10809 goto failure; 10810 } 10811 /* Fill sata_pkt */ 10812 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10813 /* Timeout 30s */ 10814 spkt->satapkt_time = sata_default_pkt_time; 10815 /* Synchronous mode, no callback, interrupts */ 10816 spkt->satapkt_op_mode = 10817 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10818 spkt->satapkt_comp = NULL; 10819 scmd = &spkt->satapkt_cmd; 10820 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10821 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10822 scmd->satacmd_addr_type = 0; 10823 scmd->satacmd_device_reg = 0; 10824 scmd->satacmd_status_reg = 0; 10825 scmd->satacmd_error_reg = 0; 10826 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10827 if (state == 0) 10828 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 10829 else 10830 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 10831 10832 /* Transfer command to HBA */ 10833 if (((*SATA_START_FUNC(sata_hba_inst))( 10834 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 10835 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10836 /* Pkt execution failed */ 10837 if (state == 0) 10838 infop = "disabling Removable Media Status " 10839 "Notification failed"; 10840 else 10841 infop = "enabling Removable Media Status " 10842 "Notification failed"; 10843 10844 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10845 rval = SATA_FAILURE; 10846 } 10847 failure: 10848 /* Free allocated resources */ 10849 if (spkt != NULL) 10850 sata_pkt_free(spx); 10851 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10852 return (rval); 10853 } 10854 10855 10856 /* 10857 * Update port SCR block 10858 */ 10859 static void 10860 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device) 10861 { 10862 port_scr->sstatus = device->satadev_scr.sstatus; 10863 port_scr->serror = device->satadev_scr.serror; 10864 port_scr->scontrol = device->satadev_scr.scontrol; 10865 port_scr->sactive = device->satadev_scr.sactive; 10866 port_scr->snotific = device->satadev_scr.snotific; 10867 } 10868 10869 /* 10870 * Update state and copy port ss* values from passed sata_device structure. 10871 * sata_address is validated - if not valid, nothing is changed in sata_scsi 10872 * configuration struct. 10873 * 10874 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 10875 * regardless of the state in device argument. 10876 * 10877 * Port mutex should be held while calling this function. 10878 */ 10879 static void 10880 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 10881 sata_device_t *sata_device) 10882 { 10883 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, 10884 sata_device->satadev_addr.cport))); 10885 10886 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 10887 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 10888 10889 sata_cport_info_t *cportinfo; 10890 10891 if (SATA_NUM_CPORTS(sata_hba_inst) <= 10892 sata_device->satadev_addr.cport) 10893 return; 10894 10895 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 10896 sata_device->satadev_addr.cport); 10897 sata_update_port_scr(&cportinfo->cport_scr, sata_device); 10898 10899 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10900 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 10901 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 10902 cportinfo->cport_state |= 10903 sata_device->satadev_state & SATA_PSTATE_VALID; 10904 } else { 10905 sata_pmport_info_t *pmportinfo; 10906 10907 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) || 10908 (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 10909 SATA_NUM_PMPORTS(sata_hba_inst, 10910 sata_device->satadev_addr.cport) < 10911 sata_device->satadev_addr.pmport) 10912 return; 10913 10914 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 10915 sata_device->satadev_addr.cport, 10916 sata_device->satadev_addr.pmport); 10917 sata_update_port_scr(&pmportinfo->pmport_scr, sata_device); 10918 10919 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10920 pmportinfo->pmport_state &= 10921 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | 10922 SATA_PSTATE_FAILED); 10923 pmportinfo->pmport_state |= 10924 sata_device->satadev_state & SATA_PSTATE_VALID; 10925 } 10926 } 10927 10928 10929 10930 /* 10931 * Extract SATA port specification from an IOCTL argument. 10932 * 10933 * This function return the port the user land send us as is, unless it 10934 * cannot retrieve port spec, then -1 is returned. 10935 * 10936 * Note: Only cport - no port multiplier port. 10937 */ 10938 static int32_t 10939 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 10940 { 10941 int32_t port; 10942 10943 /* Extract port number from nvpair in dca structure */ 10944 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 10945 SATA_LOG_D((sata_hba_inst, CE_NOTE, 10946 "sata_get_port_num: invalid port spec 0x%x in ioctl", 10947 port)); 10948 port = -1; 10949 } 10950 10951 return (port); 10952 } 10953 10954 /* 10955 * Get dev_info_t pointer to the device node pointed to by port argument. 10956 * NOTE: target argument is a value used in ioctls to identify 10957 * the AP - it is not a sata_address. 10958 * It is a combination of cport, pmport and address qualifier, encodded same 10959 * way as a scsi target number. 10960 * At this moment it carries only cport number. 10961 * 10962 * No PMult hotplug support. 10963 * 10964 * Returns dev_info_t pointer if target device was found, NULL otherwise. 10965 */ 10966 10967 static dev_info_t * 10968 sata_get_target_dip(dev_info_t *dip, int32_t port) 10969 { 10970 dev_info_t *cdip = NULL; 10971 int target, tgt; 10972 int ncport; 10973 int circ; 10974 10975 ncport = port & SATA_CFGA_CPORT_MASK; 10976 target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT); 10977 10978 ndi_devi_enter(dip, &circ); 10979 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 10980 dev_info_t *next = ddi_get_next_sibling(cdip); 10981 10982 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 10983 DDI_PROP_DONTPASS, "target", -1); 10984 if (tgt == -1) { 10985 /* 10986 * This is actually an error condition, but not 10987 * a fatal one. Just continue the search. 10988 */ 10989 cdip = next; 10990 continue; 10991 } 10992 10993 if (tgt == target) 10994 break; 10995 10996 cdip = next; 10997 } 10998 ndi_devi_exit(dip, circ); 10999 11000 return (cdip); 11001 } 11002 11003 /* 11004 * Get dev_info_t pointer to the device node pointed to by port argument. 11005 * NOTE: target argument is a value used in ioctls to identify 11006 * the AP - it is not a sata_address. 11007 * It is a combination of cport, pmport and address qualifier, encoded same 11008 * way as a scsi target number. 11009 * At this moment it carries only cport number. 11010 * 11011 * No PMult hotplug support. 11012 * 11013 * Returns dev_info_t pointer if target device was found, NULL otherwise. 11014 */ 11015 11016 static dev_info_t * 11017 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 11018 { 11019 dev_info_t *cdip = NULL; 11020 int target, tgt; 11021 int circ; 11022 11023 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 11024 11025 ndi_devi_enter(dip, &circ); 11026 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 11027 dev_info_t *next = ddi_get_next_sibling(cdip); 11028 11029 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 11030 DDI_PROP_DONTPASS, "target", -1); 11031 if (tgt == -1) { 11032 /* 11033 * This is actually an error condition, but not 11034 * a fatal one. Just continue the search. 11035 */ 11036 cdip = next; 11037 continue; 11038 } 11039 11040 if (tgt == target) 11041 break; 11042 11043 cdip = next; 11044 } 11045 ndi_devi_exit(dip, circ); 11046 11047 return (cdip); 11048 } 11049 11050 /* 11051 * Process sata port disconnect request. 11052 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 11053 * before this request. Nevertheless, if a device is still configured, 11054 * we need to attempt to offline and unconfigure device. 11055 * Regardless of the unconfigure operation results the port is marked as 11056 * deactivated and no access to the attached device is possible. 11057 * If the target node remains because unconfigure operation failed, its state 11058 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 11059 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 11060 * the device and remove old target node. 11061 * 11062 * This function invokes sata_hba_inst->satahba_tran-> 11063 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 11064 * If successful, the device structure (if any) attached to the specified port 11065 * is removed and state of the port marked appropriately. 11066 * Failure of the port_deactivate may keep port in the physically active state, 11067 * or may fail the port. 11068 * 11069 * NOTE: Port multiplier code is not completed nor tested. 11070 */ 11071 11072 static int 11073 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 11074 sata_device_t *sata_device) 11075 { 11076 sata_drive_info_t *sdinfo = NULL; 11077 sata_cport_info_t *cportinfo = NULL; 11078 sata_pmport_info_t *pmportinfo = NULL; 11079 sata_pmult_info_t *pmultinfo = NULL; 11080 dev_info_t *tdip; 11081 int cport, pmport, qual; 11082 int rval = SATA_SUCCESS; 11083 int rv = 0; 11084 11085 cport = sata_device->satadev_addr.cport; 11086 pmport = sata_device->satadev_addr.pmport; 11087 qual = sata_device->satadev_addr.qual; 11088 11089 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11090 11091 /* 11092 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 11093 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 11094 * Do the sanity check. 11095 */ 11096 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 11097 /* No physical port deactivation supported. */ 11098 return (EINVAL); 11099 } 11100 11101 /* Check the current state of the port */ 11102 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11103 (SATA_DIP(sata_hba_inst), sata_device); 11104 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11105 sata_update_port_info(sata_hba_inst, sata_device); 11106 if (rval != SATA_SUCCESS || 11107 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11108 /* Device port status is unknown or it is in failed state */ 11109 if (qual == SATA_ADDR_PMPORT) { 11110 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 11111 SATA_PSTATE_FAILED; 11112 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11113 "sata_hba_ioctl: connect: failed to deactivate " 11114 "SATA port %d", cport); 11115 } else { 11116 SATA_CPORT_STATE(sata_hba_inst, cport) = 11117 SATA_PSTATE_FAILED; 11118 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11119 "sata_hba_ioctl: connect: failed to deactivate " 11120 "SATA port %d:%d", cport, pmport); 11121 } 11122 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11123 cport)->cport_mutex); 11124 return (EIO); 11125 } 11126 /* 11127 * Set port's dev_state to not ready - this will disable 11128 * an access to a potentially attached device. 11129 */ 11130 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11131 if (qual == SATA_ADDR_PMPORT) { 11132 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11133 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 11134 sdinfo = pmportinfo->pmport_sata_drive; 11135 ASSERT(sdinfo != NULL); 11136 } 11137 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11138 } else { 11139 /* Assuming cport */ 11140 11141 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11142 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 11143 pmultinfo = 11144 cportinfo->cport_devp.cport_sata_pmult; 11145 ASSERT(pmultinfo != NULL); 11146 } else { 11147 sdinfo = cportinfo->cport_devp.cport_sata_drive; 11148 } 11149 } 11150 cportinfo->cport_state &= ~SATA_STATE_READY; 11151 } 11152 if (sdinfo != NULL) { 11153 if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) { 11154 /* 11155 * If a target node exists, try to offline 11156 * a device and remove target node. 11157 */ 11158 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11159 cport)->cport_mutex); 11160 /* We are addressing attached device, not a port */ 11161 sata_device->satadev_addr.qual = 11162 sdinfo->satadrv_addr.qual; 11163 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11164 &sata_device->satadev_addr); 11165 if (tdip != NULL && ndi_devi_offline(tdip, 11166 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11167 /* 11168 * Problem 11169 * The target node remained attached. 11170 * This happens when the device file was open 11171 * or a node was waiting for resources. 11172 * Cannot do anything about it. 11173 */ 11174 if (qual == SATA_ADDR_CPORT) { 11175 SATA_LOG_D((sata_hba_inst, CE_WARN, 11176 "sata_hba_ioctl: disconnect: could " 11177 "not unconfigure device before " 11178 "disconnecting the SATA port %d", 11179 cport)); 11180 } else { 11181 SATA_LOG_D((sata_hba_inst, CE_WARN, 11182 "sata_hba_ioctl: disconnect: could " 11183 "not unconfigure device before " 11184 "disconnecting the SATA port %d:%d", 11185 cport, pmport)); 11186 } 11187 /* 11188 * Set DEVICE REMOVED state in the target 11189 * node. It will prevent access to the device 11190 * even when a new device is attached, until 11191 * the old target node is released, removed and 11192 * recreated for a new device. 11193 */ 11194 sata_set_device_removed(tdip); 11195 11196 /* 11197 * Instruct event daemon to try the target 11198 * node cleanup later. 11199 */ 11200 sata_set_target_node_cleanup( 11201 sata_hba_inst, &sata_device->satadev_addr); 11202 } 11203 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11204 cport)->cport_mutex); 11205 } 11206 11207 /* Remove and release sata_drive info structure. */ 11208 if (pmportinfo != NULL) { 11209 SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) = 11210 NULL; 11211 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11212 } else { 11213 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11214 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11215 } 11216 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11217 } 11218 #if 0 11219 else if (pmultinfo != NULL) { 11220 /* 11221 * Port Multiplier itself needs special handling. 11222 * All device ports need to be processed here! 11223 */ 11224 } 11225 #endif 11226 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11227 /* Just ask HBA driver to deactivate port */ 11228 /* sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */ 11229 11230 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11231 (SATA_DIP(sata_hba_inst), sata_device); 11232 11233 /* 11234 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11235 * without the hint (to force listener to investivate the state). 11236 */ 11237 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11238 SE_NO_HINT); 11239 11240 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11241 sata_update_port_info(sata_hba_inst, sata_device); 11242 11243 if (rval != SATA_SUCCESS) { 11244 /* 11245 * Port deactivation failure - do not 11246 * change port state unless the state 11247 * returned by HBA indicates a port failure. 11248 * NOTE: device structures were released, so devices now are 11249 * invisible! Port reset is needed to re-enumerate devices. 11250 */ 11251 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11252 if (pmportinfo != NULL) 11253 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11254 else 11255 cportinfo->cport_state = SATA_PSTATE_FAILED; 11256 rv = EIO; 11257 } 11258 } else { 11259 /* 11260 * Deactivation succeded. From now on the sata framework 11261 * will not care what is happening to the device, until 11262 * the port is activated again. 11263 */ 11264 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11265 } 11266 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11267 return (rv); 11268 } 11269 11270 11271 11272 /* 11273 * Process sata port connect request 11274 * The sata cfgadm pluging will invoke this operation only if port was found 11275 * in the disconnect state (failed state is also treated as the disconnected 11276 * state). 11277 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 11278 * sata_tran_hotplug_ops->sata_tran_port_activate(). 11279 * If successful and a device is found attached to the port, 11280 * the initialization sequence is executed to attach a device structure to 11281 * a port structure. The state of the port and a device would be set 11282 * appropriately. 11283 * The device is not set in configured state (system-wise) by this operation. 11284 * 11285 * Note, that activating the port may generate link events, 11286 * so it is important that following processing and the 11287 * event processing does not interfere with each other! 11288 * 11289 * This operation may remove port failed state and will 11290 * try to make port active and in good standing. 11291 * 11292 * NOTE: Port multiplier code is not completed nor tested. 11293 */ 11294 11295 static int 11296 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 11297 sata_device_t *sata_device) 11298 { 11299 int cport, pmport, qual; 11300 int rv = 0; 11301 11302 cport = sata_device->satadev_addr.cport; 11303 pmport = sata_device->satadev_addr.pmport; 11304 qual = sata_device->satadev_addr.qual; 11305 11306 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11307 11308 /* 11309 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 11310 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 11311 * Perform sanity check now. 11312 */ 11313 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 11314 /* No physical port activation supported. */ 11315 return (EINVAL); 11316 } 11317 11318 /* Just ask HBA driver to activate port */ 11319 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11320 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11321 /* 11322 * Port activation failure. 11323 */ 11324 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11325 cport)->cport_mutex); 11326 sata_update_port_info(sata_hba_inst, sata_device); 11327 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11328 if (qual == SATA_ADDR_DCPORT) { 11329 SATA_CPORT_STATE(sata_hba_inst, cport) = 11330 SATA_PSTATE_FAILED; 11331 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11332 "sata_hba_ioctl: connect: failed to " 11333 "activate SATA port %d", cport); 11334 } else { /* port multiplier device port */ 11335 SATA_PMPORT_STATE(sata_hba_inst, cport, 11336 pmport) = SATA_PSTATE_FAILED; 11337 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11338 "sata_hba_ioctl: connect: failed to " 11339 "activate SATA port %d:%d", cport, pmport); 11340 11341 } 11342 } 11343 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11344 cport)->cport_mutex); 11345 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11346 "sata_hba_ioctl: connect: failed to activate SATA " 11347 "port %d:%d", cport, pmport); 11348 return (EIO); 11349 } 11350 11351 /* Virgin port state - will be updated by the port re-probe. */ 11352 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11353 if (qual == SATA_ADDR_CPORT) 11354 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 11355 else /* port multiplier device port */ 11356 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 11357 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11358 11359 /* 11360 * Probe the port to find its state and attached device. 11361 */ 11362 if (sata_reprobe_port(sata_hba_inst, sata_device, 11363 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 11364 rv = EIO; 11365 11366 /* 11367 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11368 * without the hint 11369 */ 11370 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11371 SE_NO_HINT); 11372 11373 /* 11374 * If there is a device attached to the port, emit 11375 * a message. 11376 */ 11377 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11378 11379 if (qual == SATA_ADDR_CPORT) { 11380 sata_log(sata_hba_inst, CE_WARN, 11381 "SATA device detected at port %d", cport); 11382 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11383 /* 11384 * A device was not successfully identified 11385 */ 11386 sata_log(sata_hba_inst, CE_WARN, 11387 "Could not identify SATA " 11388 "device at port %d", cport); 11389 } 11390 } else { /* port multiplier device port */ 11391 sata_log(sata_hba_inst, CE_WARN, 11392 "SATA device detected at port %d:%d", 11393 cport, pmport); 11394 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11395 /* 11396 * A device was not successfully identified 11397 */ 11398 sata_log(sata_hba_inst, CE_WARN, 11399 "Could not identify SATA " 11400 "device at port %d:%d", cport, pmport); 11401 } 11402 } 11403 } 11404 11405 return (rv); 11406 } 11407 11408 11409 /* 11410 * Process sata device unconfigure request. 11411 * The unconfigure operation uses generic nexus operation to 11412 * offline a device. It leaves a target device node attached. 11413 * and obviously sata_drive_info attached as well, because 11414 * from the hardware point of view nothing has changed. 11415 */ 11416 static int 11417 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 11418 sata_device_t *sata_device) 11419 { 11420 int rv = 0; 11421 dev_info_t *tdip; 11422 11423 /* We are addressing attached device, not a port */ 11424 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 11425 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11426 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 11427 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11428 11429 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11430 &sata_device->satadev_addr)) != NULL) { 11431 11432 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 11433 SATA_LOG_D((sata_hba_inst, CE_WARN, 11434 "sata_hba_ioctl: unconfigure: " 11435 "failed to unconfigure device at SATA port %d:%d", 11436 sata_device->satadev_addr.cport, 11437 sata_device->satadev_addr.pmport)); 11438 rv = EIO; 11439 } 11440 /* 11441 * The target node devi_state should be marked with 11442 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 11443 * This would be the indication for cfgadm that 11444 * the AP node occupant state is 'unconfigured'. 11445 */ 11446 11447 } else { 11448 /* 11449 * This would indicate a failure on the part of cfgadm 11450 * to detect correct state of the node prior to this 11451 * call - one cannot unconfigure non-existing device. 11452 */ 11453 SATA_LOG_D((sata_hba_inst, CE_WARN, 11454 "sata_hba_ioctl: unconfigure: " 11455 "attempt to unconfigure non-existing device " 11456 "at SATA port %d:%d", 11457 sata_device->satadev_addr.cport, 11458 sata_device->satadev_addr.pmport)); 11459 rv = ENXIO; 11460 } 11461 return (rv); 11462 } 11463 11464 /* 11465 * Process sata device configure request 11466 * If port is in a failed state, operation is aborted - one has to use 11467 * an explicit connect or port activate request to try to get a port into 11468 * non-failed mode. Port reset wil also work in such situation. 11469 * If the port is in disconnected (shutdown) state, the connect operation is 11470 * attempted prior to any other action. 11471 * When port is in the active state, there is a device attached and the target 11472 * node exists, a device was most likely offlined. 11473 * If target node does not exist, a new target node is created. In both cases 11474 * an attempt is made to online (configure) the device. 11475 * 11476 * NOTE: Port multiplier code is not completed nor tested. 11477 */ 11478 static int 11479 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 11480 sata_device_t *sata_device) 11481 { 11482 int cport, pmport, qual; 11483 int rval; 11484 boolean_t target = TRUE; 11485 sata_cport_info_t *cportinfo; 11486 sata_pmport_info_t *pmportinfo = NULL; 11487 dev_info_t *tdip; 11488 sata_drive_info_t *sdinfo; 11489 11490 cport = sata_device->satadev_addr.cport; 11491 pmport = sata_device->satadev_addr.pmport; 11492 qual = sata_device->satadev_addr.qual; 11493 11494 /* Get current port state */ 11495 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11496 (SATA_DIP(sata_hba_inst), sata_device); 11497 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11498 sata_update_port_info(sata_hba_inst, sata_device); 11499 11500 if (rval != SATA_SUCCESS || 11501 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11502 /* 11503 * Obviously, device on a failed port is not visible 11504 */ 11505 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11506 return (ENXIO); 11507 } 11508 11509 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11510 if (qual == SATA_ADDR_PMPORT) 11511 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11512 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11513 11514 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 11515 /* need to activate port */ 11516 target = FALSE; 11517 11518 /* Sanity check */ 11519 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11520 return (ENXIO); 11521 11522 /* Just let HBA driver to activate port */ 11523 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11524 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11525 /* 11526 * Port activation failure - do not change port state 11527 * unless the state returned by HBA indicates a port 11528 * failure. 11529 */ 11530 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11531 cport)->cport_mutex); 11532 sata_update_port_info(sata_hba_inst, sata_device); 11533 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11534 if (qual == SATA_ADDR_PMPORT) 11535 pmportinfo->pmport_state = 11536 SATA_PSTATE_FAILED; 11537 else 11538 cportinfo->cport_state = 11539 SATA_PSTATE_FAILED; 11540 } 11541 mutex_exit(&SATA_CPORT_INFO( 11542 sata_hba_inst, cport)->cport_mutex); 11543 SATA_LOG_D((sata_hba_inst, CE_WARN, 11544 "sata_hba_ioctl: configure: " 11545 "failed to activate SATA port %d:%d", 11546 cport, pmport)); 11547 return (EIO); 11548 } 11549 /* 11550 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11551 * without the hint. 11552 */ 11553 sata_gen_sysevent(sata_hba_inst, 11554 &sata_device->satadev_addr, SE_NO_HINT); 11555 11556 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11557 cport_mutex); 11558 /* Virgin port state */ 11559 if (qual == SATA_ADDR_PMPORT) 11560 pmportinfo->pmport_state = 0; 11561 else 11562 cportinfo->cport_state = 0; 11563 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11564 } 11565 /* 11566 * Always reprobe port, to get current device info. 11567 */ 11568 if (sata_reprobe_port(sata_hba_inst, sata_device, 11569 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 11570 return (EIO); 11571 11572 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 11573 if (qual == SATA_ADDR_PMPORT) { 11574 /* 11575 * That's the transition from "inactive" port 11576 * to active one with device attached. 11577 */ 11578 sata_log(sata_hba_inst, CE_WARN, 11579 "SATA device detected at port %d:%d", 11580 cport, pmport); 11581 } else { 11582 /* 11583 * When PM is attached to the cport and cport is 11584 * activated, every PM device port needs to be reprobed. 11585 * We need to emit message for all devices detected 11586 * at port multiplier's device ports. 11587 * Add such code here. 11588 * For now, just inform about device attached to 11589 * cport. 11590 */ 11591 sata_log(sata_hba_inst, CE_WARN, 11592 "SATA device detected at port %d", cport); 11593 } 11594 } 11595 11596 /* 11597 * This is where real configuration operation starts. 11598 * 11599 * When PM is attached to the cport and cport is activated, 11600 * devices attached PM device ports may have to be configured 11601 * explicitly. This may change when port multiplier is supported. 11602 * For now, configure only disks and other valid target devices. 11603 */ 11604 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 11605 if (qual == SATA_ADDR_CPORT) { 11606 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11607 /* 11608 * A device was not successfully identified 11609 */ 11610 sata_log(sata_hba_inst, CE_WARN, 11611 "Could not identify SATA " 11612 "device at port %d", cport); 11613 } 11614 } else { /* port multiplier device port */ 11615 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11616 /* 11617 * A device was not successfully identified 11618 */ 11619 sata_log(sata_hba_inst, CE_WARN, 11620 "Could not identify SATA " 11621 "device at port %d:%d", cport, pmport); 11622 } 11623 } 11624 return (ENXIO); /* No device to configure */ 11625 } 11626 11627 /* 11628 * Here we may have a device in reset condition, 11629 * but because we are just configuring it, there is 11630 * no need to process the reset other than just 11631 * to clear device reset condition in the HBA driver. 11632 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 11633 * cause a first command sent the HBA driver with the request 11634 * to clear device reset condition. 11635 */ 11636 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11637 if (qual == SATA_ADDR_PMPORT) 11638 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11639 else 11640 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11641 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11642 if (sdinfo == NULL) { 11643 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11644 return (ENXIO); 11645 } 11646 if (sdinfo->satadrv_event_flags & 11647 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 11648 sdinfo->satadrv_event_flags = 0; 11649 } 11650 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 11651 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11652 11653 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11654 &sata_device->satadev_addr)) != NULL) { 11655 /* 11656 * Target node exists. Verify, that it belongs 11657 * to existing, attached device and not to 11658 * a removed device. 11659 */ 11660 if (sata_check_device_removed(tdip) == B_TRUE) { 11661 if (qual == SATA_ADDR_DPMPORT) 11662 sata_log(sata_hba_inst, CE_WARN, 11663 "SATA device at port %d cannot be " 11664 "configured. " 11665 "Application(s) accessing " 11666 "previously attached device " 11667 "have to release it before newly " 11668 "inserted device can be made accessible.", 11669 cport); 11670 else 11671 sata_log(sata_hba_inst, CE_WARN, 11672 "SATA device at port %d:%d cannot be" 11673 "configured. " 11674 "Application(s) accessing " 11675 "previously attached device " 11676 "have to release it before newly " 11677 "inserted device can be made accessible.", 11678 cport, pmport); 11679 return (EIO); 11680 } 11681 /* 11682 * Device was not removed and re-inserted. 11683 * Try to online it. 11684 */ 11685 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 11686 SATA_LOG_D((sata_hba_inst, CE_WARN, 11687 "sata_hba_ioctl: configure: " 11688 "onlining device at SATA port " 11689 "%d:%d failed", cport, pmport)); 11690 return (EIO); 11691 } 11692 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11693 cport)->cport_mutex); 11694 11695 if (qual == SATA_ADDR_DPMPORT) 11696 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11697 else 11698 cportinfo-> cport_tgtnode_clean = B_TRUE; 11699 11700 mutex_exit(&SATA_CPORT_INFO( 11701 sata_hba_inst, cport)->cport_mutex); 11702 } else { 11703 /* 11704 * No target node - need to create a new target node. 11705 */ 11706 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11707 cport_mutex); 11708 if (qual == SATA_ADDR_DPMPORT) 11709 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11710 else 11711 cportinfo-> cport_tgtnode_clean = B_TRUE; 11712 11713 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11714 cport_mutex); 11715 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 11716 sata_hba_inst, &sata_device->satadev_addr); 11717 if (tdip == NULL) { 11718 /* Configure operation failed */ 11719 SATA_LOG_D((sata_hba_inst, CE_WARN, 11720 "sata_hba_ioctl: configure: " 11721 "configuring SATA device at port %d:%d " 11722 "failed", cport, pmport)); 11723 return (EIO); 11724 } 11725 } 11726 return (0); 11727 } 11728 11729 11730 /* 11731 * Process ioctl deactivate port request. 11732 * Arbitrarily unconfigure attached device, if any. 11733 * Even if the unconfigure fails, proceed with the 11734 * port deactivation. 11735 * 11736 * NOTE: Port Multiplier code is not completed and tested. 11737 */ 11738 11739 static int 11740 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 11741 sata_device_t *sata_device) 11742 { 11743 int cport, pmport, qual; 11744 int rval, rv = 0; 11745 sata_cport_info_t *cportinfo; 11746 sata_pmport_info_t *pmportinfo = NULL; 11747 dev_info_t *tdip; 11748 sata_drive_info_t *sdinfo = NULL; 11749 11750 /* Sanity check */ 11751 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 11752 return (ENOTSUP); 11753 11754 cport = sata_device->satadev_addr.cport; 11755 pmport = sata_device->satadev_addr.pmport; 11756 qual = sata_device->satadev_addr.qual; 11757 11758 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11759 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11760 if (qual == SATA_ADDR_CPORT) { 11761 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11762 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11763 /* 11764 * For now, assume that port multiplier is not 11765 * supported, i.e. deal only with valid devices 11766 */ 11767 if ((cportinfo->cport_dev_type & 11768 SATA_VALID_DEV_TYPE) != 0) 11769 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11770 /* 11771 * If attached device is a port multiplier, we will 11772 * have to unconfigure all devices attached to the 11773 * port multiplier. Add this code here. 11774 */ 11775 } 11776 cportinfo->cport_state &= ~SATA_STATE_READY; 11777 } else { 11778 /* Port multiplier device port */ 11779 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11780 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11781 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 11782 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 11783 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11784 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11785 } 11786 11787 if (sdinfo != NULL) { 11788 /* 11789 * If a target node exists, try to offline a device and 11790 * to remove a target node. 11791 */ 11792 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11793 cport_mutex); 11794 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11795 &sata_device->satadev_addr); 11796 if (tdip != NULL) { 11797 /* target node exist */ 11798 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11799 "sata_hba_ioctl: port deactivate: " 11800 "target node exists.", NULL); 11801 11802 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 11803 NDI_SUCCESS) { 11804 SATA_LOG_D((sata_hba_inst, CE_WARN, 11805 "sata_hba_ioctl: port deactivate: " 11806 "failed to unconfigure device at port " 11807 "%d:%d before deactivating the port", 11808 cport, pmport)); 11809 /* 11810 * Set DEVICE REMOVED state in the target 11811 * node. It will prevent an access to 11812 * the device even when a new device is 11813 * attached, until the old target node is 11814 * released, removed and recreated for a new 11815 * device. 11816 */ 11817 sata_set_device_removed(tdip); 11818 11819 /* 11820 * Instruct the event daemon to try the 11821 * target node cleanup later. 11822 */ 11823 sata_set_target_node_cleanup(sata_hba_inst, 11824 &sata_device->satadev_addr); 11825 } 11826 } 11827 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11828 cport_mutex); 11829 /* 11830 * In any case, remove and release sata_drive_info 11831 * structure. 11832 */ 11833 if (qual == SATA_ADDR_CPORT) { 11834 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11835 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11836 } else { /* port multiplier device port */ 11837 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11838 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11839 } 11840 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11841 } 11842 if (qual == SATA_ADDR_CPORT) { 11843 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 11844 SATA_STATE_PROBING); 11845 } else { /* port multiplier device port */ 11846 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 11847 SATA_STATE_PROBING); 11848 } 11849 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11850 11851 /* Just let HBA driver to deactivate port */ 11852 sata_device->satadev_addr.qual = qual; 11853 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11854 (SATA_DIP(sata_hba_inst), sata_device); 11855 11856 /* 11857 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11858 * without the hint 11859 */ 11860 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11861 SE_NO_HINT); 11862 11863 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11864 sata_update_port_info(sata_hba_inst, sata_device); 11865 if (qual == SATA_ADDR_CPORT) { 11866 if (rval != SATA_SUCCESS) { 11867 /* 11868 * Port deactivation failure - do not change port state 11869 * unless the state returned by HBA indicates a port 11870 * failure. 11871 */ 11872 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11873 SATA_CPORT_STATE(sata_hba_inst, cport) = 11874 SATA_PSTATE_FAILED; 11875 } 11876 SATA_LOG_D((sata_hba_inst, CE_WARN, 11877 "sata_hba_ioctl: port deactivate: " 11878 "cannot deactivate SATA port %d", cport)); 11879 rv = EIO; 11880 } else { 11881 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11882 } 11883 } else { 11884 if (rval != SATA_SUCCESS) { 11885 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11886 SATA_PMPORT_STATE(sata_hba_inst, cport, 11887 pmport) = SATA_PSTATE_FAILED; 11888 } 11889 SATA_LOG_D((sata_hba_inst, CE_WARN, 11890 "sata_hba_ioctl: port deactivate: " 11891 "cannot deactivate SATA port %d:%d", 11892 cport, pmport)); 11893 rv = EIO; 11894 } else { 11895 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 11896 } 11897 } 11898 11899 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11900 11901 return (rv); 11902 } 11903 11904 /* 11905 * Process ioctl port activate request. 11906 * 11907 * NOTE: Port multiplier code is not completed nor tested. 11908 */ 11909 static int 11910 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 11911 sata_device_t *sata_device) 11912 { 11913 int cport, pmport, qual; 11914 sata_cport_info_t *cportinfo; 11915 sata_pmport_info_t *pmportinfo = NULL; 11916 boolean_t dev_existed = TRUE; 11917 11918 /* Sanity check */ 11919 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11920 return (ENOTSUP); 11921 11922 cport = sata_device->satadev_addr.cport; 11923 pmport = sata_device->satadev_addr.pmport; 11924 qual = sata_device->satadev_addr.qual; 11925 11926 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11927 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11928 if (qual == SATA_ADDR_PMPORT) { 11929 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11930 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 11931 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 11932 dev_existed = FALSE; 11933 } else { /* cport */ 11934 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 11935 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 11936 dev_existed = FALSE; 11937 } 11938 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11939 11940 /* Just let HBA driver to activate port, if necessary */ 11941 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11942 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11943 /* 11944 * Port activation failure - do not change port state unless 11945 * the state returned by HBA indicates a port failure. 11946 */ 11947 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11948 cport)->cport_mutex); 11949 sata_update_port_info(sata_hba_inst, sata_device); 11950 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11951 if (qual == SATA_ADDR_PMPORT) 11952 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11953 else 11954 cportinfo->cport_state = SATA_PSTATE_FAILED; 11955 11956 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11957 cport)->cport_mutex); 11958 SATA_LOG_D((sata_hba_inst, CE_WARN, 11959 "sata_hba_ioctl: port activate: cannot activate " 11960 "SATA port %d:%d", cport, pmport)); 11961 return (EIO); 11962 } 11963 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11964 } 11965 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11966 if (qual == SATA_ADDR_PMPORT) 11967 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 11968 else 11969 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 11970 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11971 11972 /* 11973 * Re-probe port to find its current state and possibly attached device. 11974 * Port re-probing may change the cportinfo device type if device is 11975 * found attached. 11976 * If port probing failed, the device type would be set to 11977 * SATA_DTYPE_NONE. 11978 */ 11979 (void) sata_reprobe_port(sata_hba_inst, sata_device, 11980 SATA_DEV_IDENTIFY_RETRY); 11981 11982 /* 11983 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11984 * without the hint. 11985 */ 11986 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11987 SE_NO_HINT); 11988 11989 if (dev_existed == FALSE) { 11990 if (qual == SATA_ADDR_PMPORT && 11991 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 11992 /* 11993 * That's the transition from the "inactive" port state 11994 * or the active port without a device attached to the 11995 * active port state with a device attached. 11996 */ 11997 sata_log(sata_hba_inst, CE_WARN, 11998 "SATA device detected at port %d:%d", 11999 cport, pmport); 12000 } else if (qual == SATA_ADDR_CPORT && 12001 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 12002 /* 12003 * That's the transition from the "inactive" port state 12004 * or the active port without a device attached to the 12005 * active port state with a device attached. 12006 */ 12007 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 12008 sata_log(sata_hba_inst, CE_WARN, 12009 "SATA device detected at port %d", cport); 12010 } else { 12011 sata_log(sata_hba_inst, CE_WARN, 12012 "SATA port multiplier detected at port %d", 12013 cport); 12014 /* 12015 * Because the detected device is a port 12016 * multiplier, we need to reprobe every device 12017 * port on the port multiplier and show every 12018 * device found attached. 12019 * Add this code here. 12020 */ 12021 } 12022 } 12023 } 12024 return (0); 12025 } 12026 12027 12028 12029 /* 12030 * Process ioctl reset port request. 12031 * 12032 * NOTE: Port multiplier code is not completed nor tested. 12033 */ 12034 static int 12035 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 12036 sata_device_t *sata_device) 12037 { 12038 int cport, pmport, qual; 12039 int rv = 0; 12040 12041 cport = sata_device->satadev_addr.cport; 12042 pmport = sata_device->satadev_addr.pmport; 12043 qual = sata_device->satadev_addr.qual; 12044 12045 /* Sanity check */ 12046 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12047 SATA_LOG_D((sata_hba_inst, CE_WARN, 12048 "sata_hba_ioctl: sata_hba_tran missing required " 12049 "function sata_tran_reset_dport")); 12050 return (ENOTSUP); 12051 } 12052 12053 /* Ask HBA to reset port */ 12054 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 12055 sata_device) != SATA_SUCCESS) { 12056 SATA_LOG_D((sata_hba_inst, CE_WARN, 12057 "sata_hba_ioctl: reset port: failed %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 if (qual == SATA_ADDR_CPORT) 12063 SATA_CPORT_STATE(sata_hba_inst, cport) = 12064 SATA_PSTATE_FAILED; 12065 else 12066 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 12067 SATA_PSTATE_FAILED; 12068 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12069 cport_mutex); 12070 rv = EIO; 12071 } 12072 /* 12073 * Beacuse the port was reset, it should be probed and 12074 * attached device reinitialized. At this point the 12075 * port state is unknown - it's state is HBA-specific. 12076 * Re-probe port to get its state. 12077 */ 12078 if (sata_reprobe_port(sata_hba_inst, sata_device, 12079 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 12080 rv = EIO; 12081 } 12082 return (rv); 12083 } 12084 12085 /* 12086 * Process ioctl reset device request. 12087 * 12088 * NOTE: Port multiplier code is not completed nor tested. 12089 */ 12090 static int 12091 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 12092 sata_device_t *sata_device) 12093 { 12094 sata_drive_info_t *sdinfo; 12095 int cport, pmport; 12096 int rv = 0; 12097 12098 /* Sanity check */ 12099 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12100 SATA_LOG_D((sata_hba_inst, CE_WARN, 12101 "sata_hba_ioctl: sata_hba_tran missing required " 12102 "function sata_tran_reset_dport")); 12103 return (ENOTSUP); 12104 } 12105 12106 cport = sata_device->satadev_addr.cport; 12107 pmport = sata_device->satadev_addr.pmport; 12108 12109 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12110 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) { 12111 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 12112 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12113 sata_device->satadev_addr.cport); 12114 } else { /* port multiplier */ 12115 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 12116 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12117 sata_device->satadev_addr.cport, 12118 sata_device->satadev_addr.pmport); 12119 } 12120 if (sdinfo == NULL) { 12121 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12122 return (EINVAL); 12123 } 12124 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12125 12126 /* Ask HBA to reset device */ 12127 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12128 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12129 SATA_LOG_D((sata_hba_inst, CE_WARN, 12130 "sata_hba_ioctl: reset device: failed at port %d:%d", 12131 cport, pmport)); 12132 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12133 cport_mutex); 12134 sata_update_port_info(sata_hba_inst, sata_device); 12135 /* 12136 * Device info structure remains attached. Another device reset 12137 * or port disconnect/connect and re-probing is 12138 * needed to change it's state 12139 */ 12140 sdinfo->satadrv_state &= ~SATA_STATE_READY; 12141 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 12142 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12143 rv = EIO; 12144 } 12145 /* 12146 * If attached device was a port multiplier, some extra processing 12147 * may be needed, to bring it back (if port re-probing did not handle 12148 * it). Add such code here. 12149 */ 12150 return (rv); 12151 } 12152 12153 12154 /* 12155 * Process ioctl reset all request. 12156 * 12157 * NOTE: Port multiplier code is not completed nor tested. 12158 */ 12159 static int 12160 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 12161 { 12162 sata_device_t sata_device; 12163 int rv = 0; 12164 int tcport; 12165 int tpmport = 0; 12166 12167 sata_device.satadev_rev = SATA_DEVICE_REV; 12168 12169 /* 12170 * There is no protection here for configured devices. 12171 */ 12172 /* Sanity check */ 12173 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12174 SATA_LOG_D((sata_hba_inst, CE_WARN, 12175 "sata_hba_ioctl: sata_hba_tran missing required " 12176 "function sata_tran_reset_dport")); 12177 return (ENOTSUP); 12178 } 12179 12180 /* 12181 * Need to lock all ports, not just one. 12182 * If any port is locked by event processing, fail the whole operation. 12183 * One port is already locked, but for simplicity lock it again. 12184 */ 12185 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12186 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12187 cport_mutex); 12188 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12189 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 12190 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12191 cport_mutex); 12192 rv = EBUSY; 12193 break; 12194 } else { 12195 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12196 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 12197 /* 12198 * If there is a port multiplier attached, we may need 12199 * to lock its port as well. If so, add such code here. 12200 */ 12201 } 12202 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12203 cport_mutex); 12204 } 12205 12206 if (rv == 0) { 12207 /* 12208 * All cports were successfully locked. 12209 * Reset main SATA controller only for now - no PMult. 12210 * Set the device address to port 0, to have a valid device 12211 * address. 12212 */ 12213 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 12214 sata_device.satadev_addr.cport = 0; 12215 sata_device.satadev_addr.pmport = 0; 12216 12217 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12218 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 12219 SATA_LOG_D((sata_hba_inst, CE_WARN, 12220 "sata_hba_ioctl: reset controller failed")); 12221 return (EIO); 12222 } 12223 /* 12224 * Because ports were reset, port states are unknown. 12225 * They should be re-probed to get their state and 12226 * attached devices should be reinitialized. 12227 * Add code here to re-probe port multiplier device ports. 12228 */ 12229 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 12230 tcport++) { 12231 sata_device.satadev_addr.cport = tcport; 12232 sata_device.satadev_addr.pmport = tpmport; 12233 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 12234 12235 if (sata_reprobe_port(sata_hba_inst, &sata_device, 12236 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12237 rv = EIO; 12238 } 12239 } 12240 /* 12241 * Unlock all ports 12242 */ 12243 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12244 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12245 cport_mutex); 12246 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12247 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 12248 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12249 cport_mutex); 12250 } 12251 12252 /* 12253 * This operation returns EFAULT if either reset 12254 * controller failed or a re-probing of any port failed. 12255 */ 12256 return (rv); 12257 } 12258 12259 12260 /* 12261 * Process ioctl port self test request. 12262 * 12263 * NOTE: Port multiplier code is not completed nor tested. 12264 */ 12265 static int 12266 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 12267 sata_device_t *sata_device) 12268 { 12269 int cport, pmport, qual; 12270 int rv = 0; 12271 12272 /* Sanity check */ 12273 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 12274 return (ENOTSUP); 12275 12276 cport = sata_device->satadev_addr.cport; 12277 pmport = sata_device->satadev_addr.pmport; 12278 qual = sata_device->satadev_addr.qual; 12279 12280 /* 12281 * There is no protection here for a configured 12282 * device attached to this port. 12283 */ 12284 12285 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 12286 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12287 SATA_LOG_D((sata_hba_inst, CE_WARN, 12288 "sata_hba_ioctl: port selftest: " 12289 "failed port %d:%d", cport, pmport)); 12290 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12291 cport_mutex); 12292 sata_update_port_info(sata_hba_inst, sata_device); 12293 if (qual == SATA_ADDR_CPORT) 12294 SATA_CPORT_STATE(sata_hba_inst, cport) = 12295 SATA_PSTATE_FAILED; 12296 else /* port ultiplier device port */ 12297 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 12298 SATA_PSTATE_FAILED; 12299 12300 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12301 cport_mutex); 12302 return (EIO); 12303 } 12304 /* 12305 * Beacuse the port was reset in the course of testing, it should be 12306 * re-probed and attached device state should be restored. At this 12307 * point the port state is unknown - it's state is HBA-specific. 12308 * Force port re-probing to get it into a known state. 12309 */ 12310 if (sata_reprobe_port(sata_hba_inst, sata_device, 12311 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12312 rv = EIO; 12313 return (rv); 12314 } 12315 12316 12317 /* 12318 * sata_cfgadm_state: 12319 * Use the sata port state and state of the target node to figure out 12320 * the cfgadm_state. 12321 * 12322 * The port argument is a value with encoded cport, 12323 * pmport and address qualifier, in the same manner as a scsi target number. 12324 * SCSI_TO_SATA_CPORT macro extracts cport number, 12325 * SCSI_TO_SATA_PMPORT extracts pmport number and 12326 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 12327 * 12328 * For now, support is for cports only - no port multiplier device ports. 12329 */ 12330 12331 static void 12332 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 12333 devctl_ap_state_t *ap_state) 12334 { 12335 uint16_t cport; 12336 int port_state; 12337 sata_drive_info_t *sdinfo; 12338 12339 /* Cport only */ 12340 cport = SCSI_TO_SATA_CPORT(port); 12341 12342 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 12343 if (port_state & SATA_PSTATE_SHUTDOWN || 12344 port_state & SATA_PSTATE_FAILED) { 12345 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 12346 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12347 if (port_state & SATA_PSTATE_FAILED) 12348 ap_state->ap_condition = AP_COND_FAILED; 12349 else 12350 ap_state->ap_condition = AP_COND_UNKNOWN; 12351 12352 return; 12353 } 12354 12355 /* Need to check pmult device port here as well, when supported */ 12356 12357 /* Port is enabled and ready */ 12358 12359 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 12360 case SATA_DTYPE_NONE: 12361 { 12362 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12363 ap_state->ap_condition = AP_COND_OK; 12364 /* No device attached */ 12365 ap_state->ap_rstate = AP_RSTATE_EMPTY; 12366 break; 12367 } 12368 case SATA_DTYPE_UNKNOWN: 12369 case SATA_DTYPE_PMULT: /* Until PMult is supported */ 12370 case SATA_DTYPE_ATADISK: 12371 case SATA_DTYPE_ATAPICD: 12372 case SATA_DTYPE_ATAPITAPE: 12373 { 12374 dev_info_t *tdip = NULL; 12375 dev_info_t *dip = NULL; 12376 int circ; 12377 12378 dip = SATA_DIP(sata_hba_inst); 12379 tdip = sata_get_target_dip(dip, port); 12380 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12381 if (tdip != NULL) { 12382 ndi_devi_enter(dip, &circ); 12383 mutex_enter(&(DEVI(tdip)->devi_lock)); 12384 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 12385 /* 12386 * There could be the case where previously 12387 * configured and opened device was removed 12388 * and unknown device was plugged. 12389 * In such case we want to show a device, and 12390 * its configured or unconfigured state but 12391 * indicate unusable condition untill the 12392 * old target node is released and removed. 12393 */ 12394 ap_state->ap_condition = AP_COND_UNUSABLE; 12395 } else { 12396 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 12397 cport)); 12398 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12399 cport); 12400 if (sdinfo != NULL) { 12401 if ((sdinfo->satadrv_state & 12402 SATA_DSTATE_FAILED) != 0) 12403 ap_state->ap_condition = 12404 AP_COND_FAILED; 12405 else 12406 ap_state->ap_condition = 12407 AP_COND_OK; 12408 } else { 12409 ap_state->ap_condition = 12410 AP_COND_UNKNOWN; 12411 } 12412 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 12413 cport)); 12414 } 12415 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 12416 (DEVI_IS_DEVICE_DOWN(tdip))) { 12417 ap_state->ap_ostate = 12418 AP_OSTATE_UNCONFIGURED; 12419 } else { 12420 ap_state->ap_ostate = 12421 AP_OSTATE_CONFIGURED; 12422 } 12423 mutex_exit(&(DEVI(tdip)->devi_lock)); 12424 ndi_devi_exit(dip, circ); 12425 } else { 12426 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12427 ap_state->ap_condition = AP_COND_UNKNOWN; 12428 } 12429 break; 12430 } 12431 default: 12432 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12433 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12434 ap_state->ap_condition = AP_COND_UNKNOWN; 12435 /* 12436 * This is actually internal error condition (non fatal), 12437 * because we have already checked all defined device types. 12438 */ 12439 SATA_LOG_D((sata_hba_inst, CE_WARN, 12440 "sata_cfgadm_state: Internal error: " 12441 "unknown device type")); 12442 break; 12443 } 12444 } 12445 12446 12447 /* 12448 * Process ioctl get device path request. 12449 * 12450 * NOTE: Port multiplier code is not completed nor tested. 12451 */ 12452 static int 12453 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 12454 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12455 { 12456 char path[MAXPATHLEN]; 12457 uint32_t size; 12458 dev_info_t *tdip; 12459 12460 (void) strcpy(path, "/devices"); 12461 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12462 &sata_device->satadev_addr)) == NULL) { 12463 /* 12464 * No such device. If this is a request for a size, do not 12465 * return EINVAL for non-existing target, because cfgadm 12466 * will then indicate a meaningless ioctl failure. 12467 * If this is a request for a path, indicate invalid 12468 * argument. 12469 */ 12470 if (ioc->get_size == 0) 12471 return (EINVAL); 12472 } else { 12473 (void) ddi_pathname(tdip, path + strlen(path)); 12474 } 12475 size = strlen(path) + 1; 12476 12477 if (ioc->get_size != 0) { 12478 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 12479 mode) != 0) 12480 return (EFAULT); 12481 } else { 12482 if (ioc->bufsiz != size) 12483 return (EINVAL); 12484 12485 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 12486 mode) != 0) 12487 return (EFAULT); 12488 } 12489 return (0); 12490 } 12491 12492 /* 12493 * Process ioctl get attachment point type request. 12494 * 12495 * NOTE: Port multiplier code is not completed nor tested. 12496 */ 12497 static int 12498 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 12499 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12500 { 12501 uint32_t type_len; 12502 const char *ap_type; 12503 int dev_type; 12504 12505 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12506 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 12507 sata_device->satadev_addr.cport); 12508 else /* pmport */ 12509 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12510 sata_device->satadev_addr.cport, 12511 sata_device->satadev_addr.pmport); 12512 12513 switch (dev_type) { 12514 case SATA_DTYPE_NONE: 12515 ap_type = "port"; 12516 break; 12517 12518 case SATA_DTYPE_ATADISK: 12519 ap_type = "disk"; 12520 break; 12521 12522 case SATA_DTYPE_ATAPICD: 12523 ap_type = "cd/dvd"; 12524 break; 12525 12526 case SATA_DTYPE_ATAPITAPE: 12527 ap_type = "tape"; 12528 break; 12529 12530 case SATA_DTYPE_PMULT: 12531 ap_type = "pmult"; 12532 break; 12533 12534 case SATA_DTYPE_UNKNOWN: 12535 ap_type = "unknown"; 12536 break; 12537 12538 default: 12539 ap_type = "unsupported"; 12540 break; 12541 12542 } /* end of dev_type switch */ 12543 12544 type_len = strlen(ap_type) + 1; 12545 12546 if (ioc->get_size) { 12547 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 12548 mode) != 0) 12549 return (EFAULT); 12550 } else { 12551 if (ioc->bufsiz != type_len) 12552 return (EINVAL); 12553 12554 if (ddi_copyout((void *)ap_type, ioc->buf, 12555 ioc->bufsiz, mode) != 0) 12556 return (EFAULT); 12557 } 12558 return (0); 12559 12560 } 12561 12562 /* 12563 * Process ioctl get device model info request. 12564 * This operation should return to cfgadm the device model 12565 * information string 12566 * 12567 * NOTE: Port multiplier code is not completed nor tested. 12568 */ 12569 static int 12570 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 12571 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12572 { 12573 sata_drive_info_t *sdinfo; 12574 uint32_t info_len; 12575 char ap_info[SATA_ID_MODEL_LEN + 1]; 12576 12577 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12578 sata_device->satadev_addr.cport)->cport_mutex); 12579 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12580 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12581 sata_device->satadev_addr.cport); 12582 else /* port multiplier */ 12583 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12584 sata_device->satadev_addr.cport, 12585 sata_device->satadev_addr.pmport); 12586 if (sdinfo == NULL) { 12587 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12588 sata_device->satadev_addr.cport)->cport_mutex); 12589 return (EINVAL); 12590 } 12591 12592 #ifdef _LITTLE_ENDIAN 12593 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12594 #else /* _LITTLE_ENDIAN */ 12595 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12596 #endif /* _LITTLE_ENDIAN */ 12597 12598 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12599 sata_device->satadev_addr.cport)->cport_mutex); 12600 12601 ap_info[SATA_ID_MODEL_LEN] = '\0'; 12602 12603 info_len = strlen(ap_info) + 1; 12604 12605 if (ioc->get_size) { 12606 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12607 mode) != 0) 12608 return (EFAULT); 12609 } else { 12610 if (ioc->bufsiz < info_len) 12611 return (EINVAL); 12612 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12613 mode) != 0) 12614 return (EFAULT); 12615 } 12616 return (0); 12617 } 12618 12619 12620 /* 12621 * Process ioctl get device firmware revision info request. 12622 * This operation should return to cfgadm the device firmware revision 12623 * information string 12624 * 12625 * NOTE: Port multiplier code is not completed nor tested. 12626 */ 12627 static int 12628 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 12629 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12630 { 12631 sata_drive_info_t *sdinfo; 12632 uint32_t info_len; 12633 char ap_info[SATA_ID_FW_LEN + 1]; 12634 12635 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12636 sata_device->satadev_addr.cport)->cport_mutex); 12637 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12638 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12639 sata_device->satadev_addr.cport); 12640 else /* port multiplier */ 12641 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12642 sata_device->satadev_addr.cport, 12643 sata_device->satadev_addr.pmport); 12644 if (sdinfo == NULL) { 12645 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12646 sata_device->satadev_addr.cport)->cport_mutex); 12647 return (EINVAL); 12648 } 12649 12650 #ifdef _LITTLE_ENDIAN 12651 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12652 #else /* _LITTLE_ENDIAN */ 12653 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12654 #endif /* _LITTLE_ENDIAN */ 12655 12656 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12657 sata_device->satadev_addr.cport)->cport_mutex); 12658 12659 ap_info[SATA_ID_FW_LEN] = '\0'; 12660 12661 info_len = strlen(ap_info) + 1; 12662 12663 if (ioc->get_size) { 12664 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12665 mode) != 0) 12666 return (EFAULT); 12667 } else { 12668 if (ioc->bufsiz < info_len) 12669 return (EINVAL); 12670 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12671 mode) != 0) 12672 return (EFAULT); 12673 } 12674 return (0); 12675 } 12676 12677 12678 /* 12679 * Process ioctl get device serial number info request. 12680 * This operation should return to cfgadm the device serial number string. 12681 * 12682 * NOTE: Port multiplier code is not completed nor tested. 12683 */ 12684 static int 12685 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 12686 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12687 { 12688 sata_drive_info_t *sdinfo; 12689 uint32_t info_len; 12690 char ap_info[SATA_ID_SERIAL_LEN + 1]; 12691 12692 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12693 sata_device->satadev_addr.cport)->cport_mutex); 12694 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12695 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12696 sata_device->satadev_addr.cport); 12697 else /* port multiplier */ 12698 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12699 sata_device->satadev_addr.cport, 12700 sata_device->satadev_addr.pmport); 12701 if (sdinfo == NULL) { 12702 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12703 sata_device->satadev_addr.cport)->cport_mutex); 12704 return (EINVAL); 12705 } 12706 12707 #ifdef _LITTLE_ENDIAN 12708 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12709 #else /* _LITTLE_ENDIAN */ 12710 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12711 #endif /* _LITTLE_ENDIAN */ 12712 12713 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12714 sata_device->satadev_addr.cport)->cport_mutex); 12715 12716 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 12717 12718 info_len = strlen(ap_info) + 1; 12719 12720 if (ioc->get_size) { 12721 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12722 mode) != 0) 12723 return (EFAULT); 12724 } else { 12725 if (ioc->bufsiz < info_len) 12726 return (EINVAL); 12727 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12728 mode) != 0) 12729 return (EFAULT); 12730 } 12731 return (0); 12732 } 12733 12734 12735 /* 12736 * Preset scsi extended sense data (to NO SENSE) 12737 * First 18 bytes of the sense data are preset to current valid sense 12738 * with a key NO SENSE data. 12739 * 12740 * Returns void 12741 */ 12742 static void 12743 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 12744 { 12745 sense->es_valid = 1; /* Valid sense */ 12746 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 12747 sense->es_key = KEY_NO_SENSE; 12748 sense->es_info_1 = 0; 12749 sense->es_info_2 = 0; 12750 sense->es_info_3 = 0; 12751 sense->es_info_4 = 0; 12752 sense->es_add_len = 10; /* Additional length - replace with a def */ 12753 sense->es_cmd_info[0] = 0; 12754 sense->es_cmd_info[1] = 0; 12755 sense->es_cmd_info[2] = 0; 12756 sense->es_cmd_info[3] = 0; 12757 sense->es_add_code = 0; 12758 sense->es_qual_code = 0; 12759 } 12760 12761 /* 12762 * Register a legacy cmdk-style devid for the target (disk) device. 12763 * 12764 * Note: This function is called only when the HBA devinfo node has the 12765 * property "use-cmdk-devid-format" set. This property indicates that 12766 * devid compatible with old cmdk (target) driver is to be generated 12767 * for any target device attached to this controller. This will take 12768 * precedence over the devid generated by sd (target) driver. 12769 * This function is derived from cmdk_devid_setup() function in cmdk.c. 12770 */ 12771 static void 12772 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 12773 { 12774 char *hwid; 12775 int modlen; 12776 int serlen; 12777 int rval; 12778 ddi_devid_t devid; 12779 12780 /* 12781 * device ID is a concatanation of model number, "=", serial number. 12782 */ 12783 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 12784 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 12785 sizeof (sdinfo->satadrv_id.ai_model)); 12786 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12787 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12788 if (modlen == 0) 12789 goto err; 12790 hwid[modlen++] = '='; 12791 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 12792 sizeof (sdinfo->satadrv_id.ai_drvser)); 12793 swab(&hwid[modlen], &hwid[modlen], 12794 sizeof (sdinfo->satadrv_id.ai_drvser)); 12795 serlen = sata_check_modser(&hwid[modlen], 12796 sizeof (sdinfo->satadrv_id.ai_drvser)); 12797 if (serlen == 0) 12798 goto err; 12799 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 12800 12801 /* initialize/register devid */ 12802 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 12803 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) 12804 rval = ddi_devid_register(dip, devid); 12805 12806 if (rval != DDI_SUCCESS) 12807 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 12808 " on port %d", sdinfo->satadrv_addr.cport); 12809 err: 12810 kmem_free(hwid, LEGACY_HWID_LEN); 12811 } 12812 12813 /* 12814 * valid model/serial string must contain a non-zero non-space characters. 12815 * trim trailing spaces/NULLs. 12816 */ 12817 static int 12818 sata_check_modser(char *buf, int buf_len) 12819 { 12820 boolean_t ret; 12821 char *s; 12822 int i; 12823 int tb; 12824 char ch; 12825 12826 ret = B_FALSE; 12827 s = buf; 12828 for (i = 0; i < buf_len; i++) { 12829 ch = *s++; 12830 if (ch != ' ' && ch != '\0') 12831 tb = i + 1; 12832 if (ch != ' ' && ch != '\0' && ch != '0') 12833 ret = B_TRUE; 12834 } 12835 12836 if (ret == B_FALSE) 12837 return (0); /* invalid string */ 12838 12839 return (tb); /* return length */ 12840 } 12841 12842 /* 12843 * sata_set_drive_features function compares current device features setting 12844 * with the saved device features settings and, if there is a difference, 12845 * it restores device features setting to the previously saved state. 12846 * It also arbitrarily tries to select the highest supported DMA mode. 12847 * Device Identify or Identify Packet Device data has to be current. 12848 * At the moment read ahead and write cache are considered for all devices. 12849 * For atapi devices, Removable Media Status Notification is set in addition 12850 * to common features. 12851 * 12852 * This function cannot be called in the interrupt context (it may sleep). 12853 * 12854 * The input argument sdinfo should point to the drive info structure 12855 * to be updated after features are set. Note, that only 12856 * device (packet) identify data is updated, not the flags indicating the 12857 * supported features. 12858 * 12859 * Returns SATA_SUCCESS if successful or there was nothing to do. 12860 * Device Identify data in the drive info structure pointed to by the sdinfo 12861 * arguments is updated even when no features were set or changed. 12862 * 12863 * Returns SATA_FAILURE if device features could not be set or DMA mode 12864 * for a disk cannot be set and device identify data cannot be fetched. 12865 * 12866 * Returns SATA_RETRY if device features could not be set (other than disk 12867 * DMA mode) but the device identify data was fetched successfully. 12868 * 12869 * Note: This function may fail the port, making it inaccessible. 12870 * In such case the explicit port disconnect/connect or physical device 12871 * detach/attach is required to re-evaluate port state again. 12872 */ 12873 12874 static int 12875 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 12876 sata_drive_info_t *sdinfo, int restore) 12877 { 12878 int rval = SATA_SUCCESS; 12879 int rval_set; 12880 sata_drive_info_t new_sdinfo; 12881 char *finfo = "sata_set_drive_features: cannot"; 12882 char *finfox; 12883 int cache_op; 12884 12885 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12886 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 12887 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 12888 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12889 /* 12890 * Cannot get device identification - caller may retry later 12891 */ 12892 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12893 "%s fetch device identify data\n", finfo); 12894 return (SATA_FAILURE); 12895 } 12896 finfox = (restore != 0) ? " restore device features" : 12897 " initialize device features\n"; 12898 12899 switch (sdinfo->satadrv_type) { 12900 case SATA_DTYPE_ATADISK: 12901 /* Arbitrarily set UDMA mode */ 12902 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12903 SATA_SUCCESS) { 12904 SATA_LOG_D((sata_hba_inst, CE_WARN, 12905 "%s set UDMA mode\n", finfo)); 12906 return (SATA_FAILURE); 12907 } 12908 break; 12909 case SATA_DTYPE_ATAPICD: 12910 case SATA_DTYPE_ATAPITAPE: 12911 /* Set Removable Media Status Notification, if necessary */ 12912 if ((new_sdinfo.satadrv_id.ai_cmdset83 & 12913 SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) { 12914 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 12915 (!(new_sdinfo.satadrv_id.ai_features86 & 12916 SATA_RM_STATUS_NOTIFIC))) || 12917 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 12918 (new_sdinfo.satadrv_id.ai_features86 & 12919 SATA_RM_STATUS_NOTIFIC))) { 12920 /* Current setting does not match saved one */ 12921 if (sata_set_rmsn(sata_hba_inst, sdinfo, 12922 sdinfo->satadrv_settings & 12923 SATA_DEV_RMSN) != SATA_SUCCESS) 12924 rval = SATA_FAILURE; 12925 } 12926 } 12927 /* 12928 * We have to set Multiword DMA or UDMA, if it is supported, as 12929 * we want to use DMA transfer mode whenever possible. 12930 * Some devices require explicit setting of the DMA mode. 12931 */ 12932 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 12933 /* Set highest supported DMA mode */ 12934 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12935 SATA_SUCCESS) { 12936 SATA_LOG_D((sata_hba_inst, CE_WARN, 12937 "%s set UDMA mode\n", finfo)); 12938 rval = SATA_FAILURE; 12939 } 12940 } 12941 break; 12942 } 12943 12944 if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) && 12945 !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 12946 /* None of the features is supported - do nothing */ 12947 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12948 "settable features not supported\n", NULL); 12949 goto update_sdinfo; 12950 } 12951 12952 if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12953 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 12954 ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12955 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12956 /* Nothing to do */ 12957 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12958 "no device features to set\n", NULL); 12959 goto update_sdinfo; 12960 } 12961 12962 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12963 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) { 12964 if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) { 12965 /* Enable read ahead / read cache */ 12966 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 12967 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12968 "enabling read cache\n", NULL); 12969 } else { 12970 /* Disable read ahead / read cache */ 12971 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 12972 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12973 "disabling read cache\n", NULL); 12974 } 12975 12976 /* Try to set read cache mode */ 12977 rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12978 cache_op); 12979 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 12980 rval = rval_set; 12981 } 12982 12983 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12984 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12985 if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) { 12986 /* Enable write cache */ 12987 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 12988 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12989 "enabling write cache\n", NULL); 12990 } else { 12991 /* Disable write cache */ 12992 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 12993 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12994 "disabling write cache\n", NULL); 12995 } 12996 /* Try to set write cache mode */ 12997 rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12998 cache_op); 12999 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 13000 rval = rval_set; 13001 } 13002 if (rval != SATA_SUCCESS) 13003 SATA_LOG_D((sata_hba_inst, CE_WARN, 13004 "%s %s", finfo, finfox)); 13005 13006 update_sdinfo: 13007 /* 13008 * We need to fetch Device Identify data again 13009 */ 13010 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 13011 /* 13012 * Cannot get device identification - retry later 13013 */ 13014 SATA_LOG_D((sata_hba_inst, CE_WARN, 13015 "%s re-fetch device identify data\n", finfo)); 13016 rval = SATA_FAILURE; 13017 } 13018 /* Copy device sata info. */ 13019 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 13020 13021 return (rval); 13022 } 13023 13024 13025 /* 13026 * 13027 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 13028 * unable to determine. 13029 * 13030 * Cannot be called in an interrupt context. 13031 * 13032 * Called by sata_build_lsense_page_2f() 13033 */ 13034 13035 static int 13036 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 13037 sata_drive_info_t *sdinfo) 13038 { 13039 sata_pkt_t *spkt; 13040 sata_cmd_t *scmd; 13041 sata_pkt_txlate_t *spx; 13042 int rval; 13043 13044 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13045 spx->txlt_sata_hba_inst = sata_hba_inst; 13046 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13047 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13048 if (spkt == NULL) { 13049 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13050 return (-1); 13051 } 13052 /* address is needed now */ 13053 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13054 13055 13056 /* Fill sata_pkt */ 13057 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13058 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13059 /* Synchronous mode, no callback */ 13060 spkt->satapkt_comp = NULL; 13061 /* Timeout 30s */ 13062 spkt->satapkt_time = sata_default_pkt_time; 13063 13064 scmd = &spkt->satapkt_cmd; 13065 scmd->satacmd_flags.sata_special_regs = B_TRUE; 13066 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 13067 13068 /* Set up which registers need to be returned */ 13069 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 13070 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 13071 13072 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 13073 scmd->satacmd_addr_type = 0; /* N/A */ 13074 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13075 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13076 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13077 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13078 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 13079 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13080 scmd->satacmd_cmd_reg = SATAC_SMART; 13081 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13082 sdinfo->satadrv_addr.cport))); 13083 13084 13085 /* Send pkt to SATA HBA driver */ 13086 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13087 SATA_TRAN_ACCEPTED || 13088 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13089 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13090 sdinfo->satadrv_addr.cport))); 13091 /* 13092 * Whoops, no SMART RETURN STATUS 13093 */ 13094 rval = -1; 13095 } else { 13096 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13097 sdinfo->satadrv_addr.cport))); 13098 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 13099 rval = -1; 13100 goto fail; 13101 } 13102 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 13103 rval = -1; 13104 goto fail; 13105 } 13106 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 13107 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 13108 rval = 0; 13109 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 13110 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 13111 rval = 1; 13112 else { 13113 rval = -1; 13114 goto fail; 13115 } 13116 } 13117 fail: 13118 /* Free allocated resources */ 13119 sata_pkt_free(spx); 13120 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13121 13122 return (rval); 13123 } 13124 13125 /* 13126 * 13127 * Returns 0 if succeeded, -1 otherwise 13128 * 13129 * Cannot be called in an interrupt context. 13130 * 13131 */ 13132 static int 13133 sata_fetch_smart_data( 13134 sata_hba_inst_t *sata_hba_inst, 13135 sata_drive_info_t *sdinfo, 13136 struct smart_data *smart_data) 13137 { 13138 sata_pkt_t *spkt; 13139 sata_cmd_t *scmd; 13140 sata_pkt_txlate_t *spx; 13141 int rval; 13142 13143 #if ! defined(lint) 13144 ASSERT(sizeof (struct smart_data) == 512); 13145 #endif 13146 13147 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13148 spx->txlt_sata_hba_inst = sata_hba_inst; 13149 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13150 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13151 if (spkt == NULL) { 13152 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13153 return (-1); 13154 } 13155 /* address is needed now */ 13156 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13157 13158 13159 /* Fill sata_pkt */ 13160 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13161 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13162 /* Synchronous mode, no callback */ 13163 spkt->satapkt_comp = NULL; 13164 /* Timeout 30s */ 13165 spkt->satapkt_time = sata_default_pkt_time; 13166 13167 scmd = &spkt->satapkt_cmd; 13168 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13169 13170 /* 13171 * Allocate buffer for SMART data 13172 */ 13173 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13174 sizeof (struct smart_data)); 13175 if (scmd->satacmd_bp == NULL) { 13176 sata_pkt_free(spx); 13177 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13178 SATA_LOG_D((sata_hba_inst, CE_WARN, 13179 "sata_fetch_smart_data: " 13180 "cannot allocate buffer")); 13181 return (-1); 13182 } 13183 13184 13185 /* Build SMART_READ_DATA cmd in the sata_pkt */ 13186 scmd->satacmd_addr_type = 0; /* N/A */ 13187 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13188 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13189 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13190 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13191 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 13192 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13193 scmd->satacmd_cmd_reg = SATAC_SMART; 13194 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13195 sdinfo->satadrv_addr.cport))); 13196 13197 /* Send pkt to SATA HBA driver */ 13198 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13199 SATA_TRAN_ACCEPTED || 13200 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13201 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13202 sdinfo->satadrv_addr.cport))); 13203 /* 13204 * Whoops, no SMART DATA available 13205 */ 13206 rval = -1; 13207 goto fail; 13208 } else { 13209 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13210 sdinfo->satadrv_addr.cport))); 13211 if (spx->txlt_buf_dma_handle != NULL) { 13212 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13213 DDI_DMA_SYNC_FORKERNEL); 13214 ASSERT(rval == DDI_SUCCESS); 13215 } 13216 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 13217 sizeof (struct smart_data)); 13218 } 13219 13220 fail: 13221 /* Free allocated resources */ 13222 sata_free_local_buffer(spx); 13223 sata_pkt_free(spx); 13224 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13225 13226 return (rval); 13227 } 13228 13229 /* 13230 * Used by LOG SENSE page 0x10 13231 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 13232 * Note: cannot be called in the interrupt context. 13233 * 13234 * return 0 for success, -1 otherwise 13235 * 13236 */ 13237 static int 13238 sata_ext_smart_selftest_read_log( 13239 sata_hba_inst_t *sata_hba_inst, 13240 sata_drive_info_t *sdinfo, 13241 struct smart_ext_selftest_log *ext_selftest_log, 13242 uint16_t block_num) 13243 { 13244 sata_pkt_txlate_t *spx; 13245 sata_pkt_t *spkt; 13246 sata_cmd_t *scmd; 13247 int rval; 13248 13249 #if ! defined(lint) 13250 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 13251 #endif 13252 13253 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13254 spx->txlt_sata_hba_inst = sata_hba_inst; 13255 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13256 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13257 if (spkt == NULL) { 13258 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13259 return (-1); 13260 } 13261 /* address is needed now */ 13262 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13263 13264 13265 /* Fill sata_pkt */ 13266 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13267 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13268 /* Synchronous mode, no callback */ 13269 spkt->satapkt_comp = NULL; 13270 /* Timeout 30s */ 13271 spkt->satapkt_time = sata_default_pkt_time; 13272 13273 scmd = &spkt->satapkt_cmd; 13274 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13275 13276 /* 13277 * Allocate buffer for SMART extended self-test log 13278 */ 13279 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13280 sizeof (struct smart_ext_selftest_log)); 13281 if (scmd->satacmd_bp == NULL) { 13282 sata_pkt_free(spx); 13283 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13284 SATA_LOG_D((sata_hba_inst, CE_WARN, 13285 "sata_ext_smart_selftest_log: " 13286 "cannot allocate buffer")); 13287 return (-1); 13288 } 13289 13290 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 13291 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13292 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 13293 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 13294 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 13295 scmd->satacmd_lba_low_msb = 0; 13296 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 13297 scmd->satacmd_lba_mid_msb = block_num >> 8; 13298 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13299 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13300 13301 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13302 sdinfo->satadrv_addr.cport))); 13303 13304 /* Send pkt to SATA HBA driver */ 13305 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13306 SATA_TRAN_ACCEPTED || 13307 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13308 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13309 sdinfo->satadrv_addr.cport))); 13310 13311 /* 13312 * Whoops, no SMART selftest log info available 13313 */ 13314 rval = -1; 13315 goto fail; 13316 } else { 13317 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13318 sdinfo->satadrv_addr.cport))); 13319 13320 if (spx->txlt_buf_dma_handle != NULL) { 13321 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13322 DDI_DMA_SYNC_FORKERNEL); 13323 ASSERT(rval == DDI_SUCCESS); 13324 } 13325 bcopy(scmd->satacmd_bp->b_un.b_addr, 13326 (uint8_t *)ext_selftest_log, 13327 sizeof (struct smart_ext_selftest_log)); 13328 rval = 0; 13329 } 13330 13331 fail: 13332 /* Free allocated resources */ 13333 sata_free_local_buffer(spx); 13334 sata_pkt_free(spx); 13335 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13336 13337 return (rval); 13338 } 13339 13340 /* 13341 * Returns 0 for success, -1 otherwise 13342 * 13343 * SMART self-test log data is returned in buffer pointed to by selftest_log 13344 */ 13345 static int 13346 sata_smart_selftest_log( 13347 sata_hba_inst_t *sata_hba_inst, 13348 sata_drive_info_t *sdinfo, 13349 struct smart_selftest_log *selftest_log) 13350 { 13351 sata_pkt_t *spkt; 13352 sata_cmd_t *scmd; 13353 sata_pkt_txlate_t *spx; 13354 int rval; 13355 13356 #if ! defined(lint) 13357 ASSERT(sizeof (struct smart_selftest_log) == 512); 13358 #endif 13359 13360 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13361 spx->txlt_sata_hba_inst = sata_hba_inst; 13362 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13363 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13364 if (spkt == NULL) { 13365 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13366 return (-1); 13367 } 13368 /* address is needed now */ 13369 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13370 13371 13372 /* Fill sata_pkt */ 13373 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13374 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13375 /* Synchronous mode, no callback */ 13376 spkt->satapkt_comp = NULL; 13377 /* Timeout 30s */ 13378 spkt->satapkt_time = sata_default_pkt_time; 13379 13380 scmd = &spkt->satapkt_cmd; 13381 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13382 13383 /* 13384 * Allocate buffer for SMART SELFTEST LOG 13385 */ 13386 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13387 sizeof (struct smart_selftest_log)); 13388 if (scmd->satacmd_bp == NULL) { 13389 sata_pkt_free(spx); 13390 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13391 SATA_LOG_D((sata_hba_inst, CE_WARN, 13392 "sata_smart_selftest_log: " 13393 "cannot allocate buffer")); 13394 return (-1); 13395 } 13396 13397 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13398 scmd->satacmd_addr_type = 0; /* N/A */ 13399 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 13400 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 13401 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13402 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13403 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13404 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13405 scmd->satacmd_cmd_reg = SATAC_SMART; 13406 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13407 sdinfo->satadrv_addr.cport))); 13408 13409 /* Send pkt to SATA HBA driver */ 13410 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13411 SATA_TRAN_ACCEPTED || 13412 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13413 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13414 sdinfo->satadrv_addr.cport))); 13415 /* 13416 * Whoops, no SMART DATA available 13417 */ 13418 rval = -1; 13419 goto fail; 13420 } else { 13421 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13422 sdinfo->satadrv_addr.cport))); 13423 if (spx->txlt_buf_dma_handle != NULL) { 13424 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13425 DDI_DMA_SYNC_FORKERNEL); 13426 ASSERT(rval == DDI_SUCCESS); 13427 } 13428 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 13429 sizeof (struct smart_selftest_log)); 13430 rval = 0; 13431 } 13432 13433 fail: 13434 /* Free allocated resources */ 13435 sata_free_local_buffer(spx); 13436 sata_pkt_free(spx); 13437 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13438 13439 return (rval); 13440 } 13441 13442 13443 /* 13444 * Returns 0 for success, -1 otherwise 13445 * 13446 * SMART READ LOG data is returned in buffer pointed to by smart_log 13447 */ 13448 static int 13449 sata_smart_read_log( 13450 sata_hba_inst_t *sata_hba_inst, 13451 sata_drive_info_t *sdinfo, 13452 uint8_t *smart_log, /* where the data should be returned */ 13453 uint8_t which_log, /* which log should be returned */ 13454 uint8_t log_size) /* # of 512 bytes in log */ 13455 { 13456 sata_pkt_t *spkt; 13457 sata_cmd_t *scmd; 13458 sata_pkt_txlate_t *spx; 13459 int rval; 13460 13461 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13462 spx->txlt_sata_hba_inst = sata_hba_inst; 13463 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13464 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13465 if (spkt == NULL) { 13466 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13467 return (-1); 13468 } 13469 /* address is needed now */ 13470 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13471 13472 13473 /* Fill sata_pkt */ 13474 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13475 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13476 /* Synchronous mode, no callback */ 13477 spkt->satapkt_comp = NULL; 13478 /* Timeout 30s */ 13479 spkt->satapkt_time = sata_default_pkt_time; 13480 13481 scmd = &spkt->satapkt_cmd; 13482 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13483 13484 /* 13485 * Allocate buffer for SMART READ LOG 13486 */ 13487 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 13488 if (scmd->satacmd_bp == NULL) { 13489 sata_pkt_free(spx); 13490 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13491 SATA_LOG_D((sata_hba_inst, CE_WARN, 13492 "sata_smart_read_log: " "cannot allocate buffer")); 13493 return (-1); 13494 } 13495 13496 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13497 scmd->satacmd_addr_type = 0; /* N/A */ 13498 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 13499 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 13500 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13501 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13502 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13503 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13504 scmd->satacmd_cmd_reg = SATAC_SMART; 13505 13506 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13507 sdinfo->satadrv_addr.cport))); 13508 13509 /* Send pkt to SATA HBA driver */ 13510 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13511 SATA_TRAN_ACCEPTED || 13512 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13513 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13514 sdinfo->satadrv_addr.cport))); 13515 13516 /* 13517 * Whoops, no SMART DATA available 13518 */ 13519 rval = -1; 13520 goto fail; 13521 } else { 13522 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13523 sdinfo->satadrv_addr.cport))); 13524 13525 if (spx->txlt_buf_dma_handle != NULL) { 13526 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13527 DDI_DMA_SYNC_FORKERNEL); 13528 ASSERT(rval == DDI_SUCCESS); 13529 } 13530 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 13531 rval = 0; 13532 } 13533 13534 fail: 13535 /* Free allocated resources */ 13536 sata_free_local_buffer(spx); 13537 sata_pkt_free(spx); 13538 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13539 13540 return (rval); 13541 } 13542 13543 /* 13544 * Used by LOG SENSE page 0x10 13545 * 13546 * return 0 for success, -1 otherwise 13547 * 13548 */ 13549 static int 13550 sata_read_log_ext_directory( 13551 sata_hba_inst_t *sata_hba_inst, 13552 sata_drive_info_t *sdinfo, 13553 struct read_log_ext_directory *logdir) 13554 { 13555 sata_pkt_txlate_t *spx; 13556 sata_pkt_t *spkt; 13557 sata_cmd_t *scmd; 13558 int rval; 13559 13560 #if ! defined(lint) 13561 ASSERT(sizeof (struct read_log_ext_directory) == 512); 13562 #endif 13563 13564 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13565 spx->txlt_sata_hba_inst = sata_hba_inst; 13566 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13567 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13568 if (spkt == NULL) { 13569 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13570 return (-1); 13571 } 13572 13573 /* Fill sata_pkt */ 13574 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13575 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13576 /* Synchronous mode, no callback */ 13577 spkt->satapkt_comp = NULL; 13578 /* Timeout 30s */ 13579 spkt->satapkt_time = sata_default_pkt_time; 13580 13581 scmd = &spkt->satapkt_cmd; 13582 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13583 13584 /* 13585 * Allocate buffer for SMART READ LOG EXTENDED command 13586 */ 13587 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13588 sizeof (struct read_log_ext_directory)); 13589 if (scmd->satacmd_bp == NULL) { 13590 sata_pkt_free(spx); 13591 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13592 SATA_LOG_D((sata_hba_inst, CE_WARN, 13593 "sata_read_log_ext_directory: " 13594 "cannot allocate buffer")); 13595 return (-1); 13596 } 13597 13598 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 13599 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13600 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 13601 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 13602 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 13603 scmd->satacmd_lba_low_msb = 0; 13604 scmd->satacmd_lba_mid_lsb = 0; 13605 scmd->satacmd_lba_mid_msb = 0; 13606 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13607 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13608 13609 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13610 sdinfo->satadrv_addr.cport))); 13611 13612 /* Send pkt to SATA HBA driver */ 13613 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13614 SATA_TRAN_ACCEPTED || 13615 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13616 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13617 sdinfo->satadrv_addr.cport))); 13618 /* 13619 * Whoops, no SMART selftest log info available 13620 */ 13621 rval = -1; 13622 goto fail; 13623 } else { 13624 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13625 sdinfo->satadrv_addr.cport))); 13626 if (spx->txlt_buf_dma_handle != NULL) { 13627 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13628 DDI_DMA_SYNC_FORKERNEL); 13629 ASSERT(rval == DDI_SUCCESS); 13630 } 13631 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 13632 sizeof (struct read_log_ext_directory)); 13633 rval = 0; 13634 } 13635 13636 fail: 13637 /* Free allocated resources */ 13638 sata_free_local_buffer(spx); 13639 sata_pkt_free(spx); 13640 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13641 13642 return (rval); 13643 } 13644 13645 /* 13646 * Set up error retrieval sata command for NCQ command error data 13647 * recovery. 13648 * 13649 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 13650 * returns SATA_FAILURE otherwise. 13651 */ 13652 static int 13653 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 13654 { 13655 #ifndef __lock_lint 13656 _NOTE(ARGUNUSED(sdinfo)) 13657 #endif 13658 13659 sata_pkt_t *spkt = spx->txlt_sata_pkt; 13660 sata_cmd_t *scmd; 13661 struct buf *bp; 13662 13663 /* Operation modes are up to the caller */ 13664 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13665 13666 /* Synchronous mode, no callback - may be changed by the caller */ 13667 spkt->satapkt_comp = NULL; 13668 spkt->satapkt_time = sata_default_pkt_time; 13669 13670 scmd = &spkt->satapkt_cmd; 13671 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 13672 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13673 13674 /* 13675 * Allocate dma_able buffer error data. 13676 * Buffer allocation will take care of buffer alignment and other DMA 13677 * attributes. 13678 */ 13679 bp = sata_alloc_local_buffer(spx, 13680 sizeof (struct sata_ncq_error_recovery_page)); 13681 if (bp == NULL) 13682 return (SATA_FAILURE); 13683 13684 bp_mapin(bp); /* make data buffer accessible */ 13685 scmd->satacmd_bp = bp; 13686 13687 /* 13688 * Set-up pointer to the buffer handle, so HBA can sync buffer 13689 * before accessing it. Handle is in usual place in translate struct. 13690 */ 13691 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 13692 13693 ASSERT(scmd->satacmd_num_dma_cookies != 0); 13694 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 13695 13696 return (SATA_SUCCESS); 13697 } 13698 13699 /* 13700 * sata_xlate_errors() is used to translate (S)ATA error 13701 * information to SCSI information returned in the SCSI 13702 * packet. 13703 */ 13704 static void 13705 sata_xlate_errors(sata_pkt_txlate_t *spx) 13706 { 13707 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 13708 struct scsi_extended_sense *sense; 13709 13710 scsipkt->pkt_reason = CMD_INCOMPLETE; 13711 *scsipkt->pkt_scbp = STATUS_CHECK; 13712 sense = sata_arq_sense(spx); 13713 13714 switch (spx->txlt_sata_pkt->satapkt_reason) { 13715 case SATA_PKT_PORT_ERROR: 13716 /* 13717 * We have no device data. Assume no data transfered. 13718 */ 13719 sense->es_key = KEY_HARDWARE_ERROR; 13720 break; 13721 13722 case SATA_PKT_DEV_ERROR: 13723 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 13724 SATA_STATUS_ERR) { 13725 /* 13726 * determine dev error reason from error 13727 * reg content 13728 */ 13729 sata_decode_device_error(spx, sense); 13730 break; 13731 } 13732 /* No extended sense key - no info available */ 13733 break; 13734 13735 case SATA_PKT_TIMEOUT: 13736 scsipkt->pkt_reason = CMD_TIMEOUT; 13737 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 13738 /* No extended sense key */ 13739 break; 13740 13741 case SATA_PKT_ABORTED: 13742 scsipkt->pkt_reason = CMD_ABORTED; 13743 scsipkt->pkt_statistics |= STAT_ABORTED; 13744 /* No extended sense key */ 13745 break; 13746 13747 case SATA_PKT_RESET: 13748 /* 13749 * pkt aborted either by an explicit reset request from 13750 * a host, or due to error recovery 13751 */ 13752 scsipkt->pkt_reason = CMD_RESET; 13753 scsipkt->pkt_statistics |= STAT_DEV_RESET; 13754 break; 13755 13756 default: 13757 scsipkt->pkt_reason = CMD_TRAN_ERR; 13758 break; 13759 } 13760 } 13761 13762 13763 13764 13765 /* 13766 * Log sata message 13767 * dev pathname msg line preceeds the logged message. 13768 */ 13769 13770 static void 13771 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 13772 { 13773 char pathname[128]; 13774 dev_info_t *dip; 13775 va_list ap; 13776 13777 mutex_enter(&sata_log_mutex); 13778 13779 va_start(ap, fmt); 13780 (void) vsprintf(sata_log_buf, fmt, ap); 13781 va_end(ap); 13782 13783 if (sata_hba_inst != NULL) { 13784 dip = SATA_DIP(sata_hba_inst); 13785 (void) ddi_pathname(dip, pathname); 13786 } else { 13787 pathname[0] = 0; 13788 } 13789 if (level == CE_CONT) { 13790 if (sata_debug_flags == 0) 13791 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 13792 else 13793 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 13794 } else { 13795 if (level != CE_NOTE) { 13796 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 13797 } else if (sata_msg) { 13798 cmn_err(level, "%s:\n %s", pathname, 13799 sata_log_buf); 13800 } 13801 } 13802 13803 mutex_exit(&sata_log_mutex); 13804 } 13805 13806 13807 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 13808 13809 /* 13810 * Start or terminate the thread, depending on flag arg and current state 13811 */ 13812 static void 13813 sata_event_thread_control(int startstop) 13814 { 13815 static int sata_event_thread_terminating = 0; 13816 static int sata_event_thread_starting = 0; 13817 int i; 13818 13819 mutex_enter(&sata_event_mutex); 13820 13821 if (startstop == 0 && (sata_event_thread_starting == 1 || 13822 sata_event_thread_terminating == 1)) { 13823 mutex_exit(&sata_event_mutex); 13824 return; 13825 } 13826 if (startstop == 1 && sata_event_thread_starting == 1) { 13827 mutex_exit(&sata_event_mutex); 13828 return; 13829 } 13830 if (startstop == 1 && sata_event_thread_terminating == 1) { 13831 sata_event_thread_starting = 1; 13832 /* wait til terminate operation completes */ 13833 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13834 while (sata_event_thread_terminating == 1) { 13835 if (i-- <= 0) { 13836 sata_event_thread_starting = 0; 13837 mutex_exit(&sata_event_mutex); 13838 #ifdef SATA_DEBUG 13839 cmn_err(CE_WARN, "sata_event_thread_control: " 13840 "timeout waiting for thread to terminate"); 13841 #endif 13842 return; 13843 } 13844 mutex_exit(&sata_event_mutex); 13845 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13846 mutex_enter(&sata_event_mutex); 13847 } 13848 } 13849 if (startstop == 1) { 13850 if (sata_event_thread == NULL) { 13851 sata_event_thread = thread_create(NULL, 0, 13852 (void (*)())sata_event_daemon, 13853 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 13854 } 13855 sata_event_thread_starting = 0; 13856 mutex_exit(&sata_event_mutex); 13857 return; 13858 } 13859 13860 /* 13861 * If we got here, thread may need to be terminated 13862 */ 13863 if (sata_event_thread != NULL) { 13864 int i; 13865 /* Signal event thread to go away */ 13866 sata_event_thread_terminating = 1; 13867 sata_event_thread_terminate = 1; 13868 cv_signal(&sata_event_cv); 13869 /* 13870 * Wait til daemon terminates. 13871 */ 13872 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13873 while (sata_event_thread_terminate == 1) { 13874 mutex_exit(&sata_event_mutex); 13875 if (i-- <= 0) { 13876 /* Daemon did not go away !!! */ 13877 #ifdef SATA_DEBUG 13878 cmn_err(CE_WARN, "sata_event_thread_control: " 13879 "cannot terminate event daemon thread"); 13880 #endif 13881 mutex_enter(&sata_event_mutex); 13882 break; 13883 } 13884 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13885 mutex_enter(&sata_event_mutex); 13886 } 13887 sata_event_thread_terminating = 0; 13888 } 13889 ASSERT(sata_event_thread_terminating == 0); 13890 ASSERT(sata_event_thread_starting == 0); 13891 mutex_exit(&sata_event_mutex); 13892 } 13893 13894 13895 /* 13896 * SATA HBA event notification function. 13897 * Events reported by SATA HBA drivers per HBA instance relate to a change in 13898 * a port and/or device state or a controller itself. 13899 * Events for different addresses/addr types cannot be combined. 13900 * A warning message is generated for each event type. 13901 * Events are not processed by this function, so only the 13902 * event flag(s)is set for an affected entity and the event thread is 13903 * waken up. Event daemon thread processes all events. 13904 * 13905 * NOTE: Since more than one event may be reported at the same time, one 13906 * cannot determine a sequence of events when opposite event are reported, eg. 13907 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 13908 * is taking precedence over reported events, i.e. may cause ignoring some 13909 * events. 13910 */ 13911 #define SATA_EVENT_MAX_MSG_LENGTH 79 13912 13913 void 13914 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 13915 { 13916 sata_hba_inst_t *sata_hba_inst = NULL; 13917 sata_address_t *saddr; 13918 sata_drive_info_t *sdinfo; 13919 sata_port_stats_t *pstats; 13920 sata_cport_info_t *cportinfo; 13921 sata_pmport_info_t *pmportinfo; 13922 int cport, pmport; 13923 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13924 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13925 char *lcp; 13926 static char *err_msg_evnt_1 = 13927 "sata_hba_event_notify: invalid port event 0x%x "; 13928 static char *err_msg_evnt_2 = 13929 "sata_hba_event_notify: invalid device event 0x%x "; 13930 int linkevent; 13931 13932 /* 13933 * There is a possibility that an event will be generated on HBA 13934 * that has not completed attachment or is detaching. We still want 13935 * to process events until HBA is detached. 13936 */ 13937 mutex_enter(&sata_mutex); 13938 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 13939 sata_hba_inst = sata_hba_inst->satahba_next) { 13940 if (SATA_DIP(sata_hba_inst) == dip) 13941 if (sata_hba_inst->satahba_attached == 1) 13942 break; 13943 } 13944 mutex_exit(&sata_mutex); 13945 if (sata_hba_inst == NULL) 13946 /* HBA not attached */ 13947 return; 13948 13949 ASSERT(sata_device != NULL); 13950 13951 /* 13952 * Validate address before - do not proceed with invalid address. 13953 */ 13954 saddr = &sata_device->satadev_addr; 13955 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 13956 return; 13957 if (saddr->qual == SATA_ADDR_PMPORT || 13958 saddr->qual == SATA_ADDR_DPMPORT) 13959 /* Port Multiplier not supported yet */ 13960 return; 13961 13962 cport = saddr->cport; 13963 pmport = saddr->pmport; 13964 13965 buf1[0] = buf2[0] = '\0'; 13966 13967 /* 13968 * If event relates to port or device, check port state. 13969 * Port has to be initialized, or we cannot accept an event. 13970 */ 13971 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 13972 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) { 13973 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) { 13974 mutex_enter(&sata_hba_inst->satahba_mutex); 13975 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13976 mutex_exit(&sata_hba_inst->satahba_mutex); 13977 if (cportinfo == NULL || cportinfo->cport_state == 0) 13978 return; 13979 } else { 13980 mutex_enter(&sata_hba_inst->satahba_mutex); 13981 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 13982 cport, pmport); 13983 mutex_exit(&sata_hba_inst->satahba_mutex); 13984 if (pmportinfo == NULL || pmportinfo->pmport_state == 0) 13985 return; 13986 } 13987 } 13988 13989 /* 13990 * Events refer to devices, ports and controllers - each has 13991 * unique address. Events for different addresses cannot be combined. 13992 */ 13993 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 13994 13995 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13996 13997 /* qualify this event(s) */ 13998 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 13999 /* Invalid event for the device port */ 14000 (void) sprintf(buf2, err_msg_evnt_1, 14001 event & SATA_EVNT_PORT_EVENTS); 14002 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14003 goto event_info; 14004 } 14005 if (saddr->qual == SATA_ADDR_CPORT) { 14006 /* Controller's device port event */ 14007 14008 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 14009 cport_event_flags |= 14010 event & SATA_EVNT_PORT_EVENTS; 14011 pstats = 14012 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 14013 cport_stats; 14014 } else { 14015 /* Port multiplier's device port event */ 14016 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 14017 pmport_event_flags |= 14018 event & SATA_EVNT_PORT_EVENTS; 14019 pstats = 14020 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 14021 pmport_stats; 14022 } 14023 14024 /* 14025 * Add to statistics and log the message. We have to do it 14026 * here rather than in the event daemon, because there may be 14027 * multiple events occuring before they are processed. 14028 */ 14029 linkevent = event & 14030 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 14031 if (linkevent) { 14032 if (linkevent == (SATA_EVNT_LINK_LOST | 14033 SATA_EVNT_LINK_ESTABLISHED)) { 14034 /* This is likely event combination */ 14035 (void) strlcat(buf1, "link lost/established, ", 14036 SATA_EVENT_MAX_MSG_LENGTH); 14037 14038 if (pstats->link_lost < 0xffffffffffffffffULL) 14039 pstats->link_lost++; 14040 if (pstats->link_established < 14041 0xffffffffffffffffULL) 14042 pstats->link_established++; 14043 linkevent = 0; 14044 } else if (linkevent & SATA_EVNT_LINK_LOST) { 14045 (void) strlcat(buf1, "link lost, ", 14046 SATA_EVENT_MAX_MSG_LENGTH); 14047 14048 if (pstats->link_lost < 0xffffffffffffffffULL) 14049 pstats->link_lost++; 14050 } else { 14051 (void) strlcat(buf1, "link established, ", 14052 SATA_EVENT_MAX_MSG_LENGTH); 14053 if (pstats->link_established < 14054 0xffffffffffffffffULL) 14055 pstats->link_established++; 14056 } 14057 } 14058 if (event & SATA_EVNT_DEVICE_ATTACHED) { 14059 (void) strlcat(buf1, "device attached, ", 14060 SATA_EVENT_MAX_MSG_LENGTH); 14061 if (pstats->device_attached < 0xffffffffffffffffULL) 14062 pstats->device_attached++; 14063 } 14064 if (event & SATA_EVNT_DEVICE_DETACHED) { 14065 (void) strlcat(buf1, "device detached, ", 14066 SATA_EVENT_MAX_MSG_LENGTH); 14067 if (pstats->device_detached < 0xffffffffffffffffULL) 14068 pstats->device_detached++; 14069 } 14070 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 14071 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 14072 "port %d power level changed", cport); 14073 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 14074 pstats->port_pwr_changed++; 14075 } 14076 14077 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 14078 /* There should be no other events for this address */ 14079 (void) sprintf(buf2, err_msg_evnt_1, 14080 event & ~SATA_EVNT_PORT_EVENTS); 14081 } 14082 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14083 14084 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 14085 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14086 14087 /* qualify this event */ 14088 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 14089 /* Invalid event for a device */ 14090 (void) sprintf(buf2, err_msg_evnt_2, 14091 event & SATA_EVNT_DEVICE_RESET); 14092 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14093 goto event_info; 14094 } 14095 /* drive event */ 14096 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 14097 if (sdinfo != NULL) { 14098 if (event & SATA_EVNT_DEVICE_RESET) { 14099 (void) strlcat(buf1, "device reset, ", 14100 SATA_EVENT_MAX_MSG_LENGTH); 14101 if (sdinfo->satadrv_stats.drive_reset < 14102 0xffffffffffffffffULL) 14103 sdinfo->satadrv_stats.drive_reset++; 14104 sdinfo->satadrv_event_flags |= 14105 SATA_EVNT_DEVICE_RESET; 14106 } 14107 } 14108 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 14109 /* Invalid event for a device */ 14110 (void) sprintf(buf2, err_msg_evnt_2, 14111 event & ~SATA_EVNT_DRIVE_EVENTS); 14112 } 14113 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14114 } else { 14115 if (saddr->qual != SATA_ADDR_NULL) { 14116 /* Wrong address qualifier */ 14117 SATA_LOG_D((sata_hba_inst, CE_WARN, 14118 "sata_hba_event_notify: invalid address 0x%x", 14119 *(uint32_t *)saddr)); 14120 return; 14121 } 14122 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 14123 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 14124 /* Invalid event for the controller */ 14125 SATA_LOG_D((sata_hba_inst, CE_WARN, 14126 "sata_hba_event_notify: invalid event 0x%x for " 14127 "controller", 14128 event & SATA_EVNT_CONTROLLER_EVENTS)); 14129 return; 14130 } 14131 buf1[0] = '\0'; 14132 /* This may be a frequent and not interesting event */ 14133 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 14134 "controller power level changed\n", NULL); 14135 14136 mutex_enter(&sata_hba_inst->satahba_mutex); 14137 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 14138 0xffffffffffffffffULL) 14139 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 14140 14141 sata_hba_inst->satahba_event_flags |= 14142 SATA_EVNT_PWR_LEVEL_CHANGED; 14143 mutex_exit(&sata_hba_inst->satahba_mutex); 14144 } 14145 /* 14146 * If we got here, there is something to do with this HBA 14147 * instance. 14148 */ 14149 mutex_enter(&sata_hba_inst->satahba_mutex); 14150 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14151 mutex_exit(&sata_hba_inst->satahba_mutex); 14152 mutex_enter(&sata_mutex); 14153 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 14154 mutex_exit(&sata_mutex); 14155 14156 /* Tickle event thread */ 14157 mutex_enter(&sata_event_mutex); 14158 if (sata_event_thread_active == 0) 14159 cv_signal(&sata_event_cv); 14160 mutex_exit(&sata_event_mutex); 14161 14162 event_info: 14163 if (buf1[0] != '\0') { 14164 lcp = strrchr(buf1, ','); 14165 if (lcp != NULL) 14166 *lcp = '\0'; 14167 } 14168 if (saddr->qual == SATA_ADDR_CPORT || 14169 saddr->qual == SATA_ADDR_DCPORT) { 14170 if (buf1[0] != '\0') { 14171 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14172 cport, buf1); 14173 } 14174 if (buf2[0] != '\0') { 14175 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14176 cport, buf2); 14177 } 14178 } else if (saddr->qual == SATA_ADDR_PMPORT || 14179 saddr->qual == SATA_ADDR_DPMPORT) { 14180 if (buf1[0] != '\0') { 14181 sata_log(sata_hba_inst, CE_NOTE, 14182 "port %d pmport %d: %s\n", cport, pmport, buf1); 14183 } 14184 if (buf2[0] != '\0') { 14185 sata_log(sata_hba_inst, CE_NOTE, 14186 "port %d pmport %d: %s\n", cport, pmport, buf2); 14187 } 14188 } 14189 } 14190 14191 14192 /* 14193 * Event processing thread. 14194 * Arg is a pointer to the sata_hba_list pointer. 14195 * It is not really needed, because sata_hba_list is global and static 14196 */ 14197 static void 14198 sata_event_daemon(void *arg) 14199 { 14200 #ifndef __lock_lint 14201 _NOTE(ARGUNUSED(arg)) 14202 #endif 14203 sata_hba_inst_t *sata_hba_inst; 14204 clock_t lbolt; 14205 14206 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14207 "SATA event daemon started\n", NULL); 14208 loop: 14209 /* 14210 * Process events here. Walk through all registered HBAs 14211 */ 14212 mutex_enter(&sata_mutex); 14213 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14214 sata_hba_inst = sata_hba_inst->satahba_next) { 14215 ASSERT(sata_hba_inst != NULL); 14216 mutex_enter(&sata_hba_inst->satahba_mutex); 14217 if (sata_hba_inst->satahba_attached == 0 || 14218 (sata_hba_inst->satahba_event_flags & 14219 SATA_EVNT_SKIP) != 0) { 14220 mutex_exit(&sata_hba_inst->satahba_mutex); 14221 continue; 14222 } 14223 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 14224 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 14225 mutex_exit(&sata_hba_inst->satahba_mutex); 14226 mutex_exit(&sata_mutex); 14227 /* Got the controller with pending event */ 14228 sata_process_controller_events(sata_hba_inst); 14229 /* 14230 * Since global mutex was released, there is a 14231 * possibility that HBA list has changed, so start 14232 * over from the top. Just processed controller 14233 * will be passed-over because of the SKIP flag. 14234 */ 14235 goto loop; 14236 } 14237 mutex_exit(&sata_hba_inst->satahba_mutex); 14238 } 14239 /* Clear SKIP flag in all controllers */ 14240 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14241 sata_hba_inst = sata_hba_inst->satahba_next) { 14242 mutex_enter(&sata_hba_inst->satahba_mutex); 14243 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 14244 mutex_exit(&sata_hba_inst->satahba_mutex); 14245 } 14246 mutex_exit(&sata_mutex); 14247 14248 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14249 "SATA EVENT DAEMON suspending itself", NULL); 14250 14251 #ifdef SATA_DEBUG 14252 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 14253 sata_log(sata_hba_inst, CE_WARN, 14254 "SATA EVENTS PROCESSING DISABLED\n"); 14255 thread_exit(); /* Daemon will not run again */ 14256 } 14257 #endif 14258 mutex_enter(&sata_event_mutex); 14259 sata_event_thread_active = 0; 14260 mutex_exit(&sata_event_mutex); 14261 /* 14262 * Go to sleep/suspend itself and wake up either because new event or 14263 * wait timeout. Exit if there is a termination request (driver 14264 * unload). 14265 */ 14266 do { 14267 lbolt = ddi_get_lbolt(); 14268 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 14269 mutex_enter(&sata_event_mutex); 14270 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 14271 14272 if (sata_event_thread_active != 0) { 14273 mutex_exit(&sata_event_mutex); 14274 continue; 14275 } 14276 14277 /* Check if it is time to go away */ 14278 if (sata_event_thread_terminate == 1) { 14279 /* 14280 * It is up to the thread setting above flag to make 14281 * sure that this thread is not killed prematurely. 14282 */ 14283 sata_event_thread_terminate = 0; 14284 sata_event_thread = NULL; 14285 mutex_exit(&sata_event_mutex); 14286 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14287 "SATA_EVENT_DAEMON_TERMINATING", NULL); 14288 thread_exit(); { _NOTE(NOT_REACHED) } 14289 } 14290 mutex_exit(&sata_event_mutex); 14291 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 14292 14293 mutex_enter(&sata_event_mutex); 14294 sata_event_thread_active = 1; 14295 mutex_exit(&sata_event_mutex); 14296 14297 mutex_enter(&sata_mutex); 14298 sata_event_pending &= ~SATA_EVNT_MAIN; 14299 mutex_exit(&sata_mutex); 14300 14301 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14302 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 14303 14304 goto loop; 14305 } 14306 14307 /* 14308 * Specific HBA instance event processing. 14309 * 14310 * NOTE: At the moment, device event processing is limited to hard disks 14311 * only. 14312 * cports only are supported - no pmports. 14313 */ 14314 static void 14315 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 14316 { 14317 int ncport; 14318 uint32_t event_flags; 14319 sata_address_t *saddr; 14320 sata_cport_info_t *cportinfo; 14321 14322 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 14323 "Processing controller %d event(s)", 14324 ddi_get_instance(SATA_DIP(sata_hba_inst))); 14325 14326 mutex_enter(&sata_hba_inst->satahba_mutex); 14327 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 14328 event_flags = sata_hba_inst->satahba_event_flags; 14329 mutex_exit(&sata_hba_inst->satahba_mutex); 14330 /* 14331 * Process controller power change first 14332 * HERE 14333 */ 14334 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 14335 sata_process_cntrl_pwr_level_change(sata_hba_inst); 14336 14337 /* 14338 * Search through ports/devices to identify affected port/device. 14339 * We may have to process events for more than one port/device. 14340 */ 14341 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 14342 /* 14343 * Not all ports may be processed in attach by the time we 14344 * get an event. Check if port info is initialized. 14345 */ 14346 mutex_enter(&sata_hba_inst->satahba_mutex); 14347 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 14348 mutex_exit(&sata_hba_inst->satahba_mutex); 14349 if (cportinfo == NULL || cportinfo->cport_state == NULL) 14350 continue; 14351 14352 /* We have initialized controller port info */ 14353 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14354 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14355 cport_event_flags; 14356 /* Check if port was locked by IOCTL processing */ 14357 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 14358 /* 14359 * We ignore port events because port is busy 14360 * with AP control processing. Set again 14361 * controller and main event flag, so that 14362 * events may be processed by the next daemon 14363 * run. 14364 */ 14365 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14366 mutex_enter(&sata_hba_inst->satahba_mutex); 14367 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14368 mutex_exit(&sata_hba_inst->satahba_mutex); 14369 mutex_enter(&sata_mutex); 14370 sata_event_pending |= SATA_EVNT_MAIN; 14371 mutex_exit(&sata_mutex); 14372 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 14373 "Event processing postponed until " 14374 "AP control processing completes", 14375 NULL); 14376 /* Check other ports */ 14377 continue; 14378 } else { 14379 /* 14380 * Set BSY flag so that AP control would not 14381 * interfere with events processing for 14382 * this port. 14383 */ 14384 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14385 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 14386 } 14387 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14388 14389 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 14390 14391 if ((event_flags & 14392 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 14393 /* 14394 * Got port event. 14395 * We need some hierarchy of event processing as they 14396 * are affecting each other: 14397 * 1. port failed 14398 * 2. device detached/attached 14399 * 3. link events - link events may trigger device 14400 * detached or device attached events in some 14401 * circumstances. 14402 * 4. port power level changed 14403 */ 14404 if (event_flags & SATA_EVNT_PORT_FAILED) { 14405 sata_process_port_failed_event(sata_hba_inst, 14406 saddr); 14407 } 14408 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 14409 sata_process_device_detached(sata_hba_inst, 14410 saddr); 14411 } 14412 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 14413 sata_process_device_attached(sata_hba_inst, 14414 saddr); 14415 } 14416 if (event_flags & 14417 (SATA_EVNT_LINK_ESTABLISHED | 14418 SATA_EVNT_LINK_LOST)) { 14419 sata_process_port_link_events(sata_hba_inst, 14420 saddr); 14421 } 14422 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 14423 sata_process_port_pwr_change(sata_hba_inst, 14424 saddr); 14425 } 14426 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 14427 sata_process_target_node_cleanup( 14428 sata_hba_inst, saddr); 14429 } 14430 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 14431 sata_process_device_autoonline( 14432 sata_hba_inst, saddr); 14433 } 14434 } 14435 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14436 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 14437 SATA_DTYPE_NONE) && 14438 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 14439 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 14440 satadrv_event_flags & 14441 (SATA_EVNT_DEVICE_RESET | 14442 SATA_EVNT_INPROC_DEVICE_RESET)) { 14443 /* Have device event */ 14444 sata_process_device_reset(sata_hba_inst, 14445 saddr); 14446 } 14447 } 14448 /* Release PORT_BUSY flag */ 14449 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14450 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 14451 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14452 14453 } /* End of loop through the controller SATA ports */ 14454 } 14455 14456 /* 14457 * Process HBA power level change reported by HBA driver. 14458 * Not implemented at this time - event is ignored. 14459 */ 14460 static void 14461 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 14462 { 14463 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14464 "Processing controller power level change", NULL); 14465 14466 /* Ignoring it for now */ 14467 mutex_enter(&sata_hba_inst->satahba_mutex); 14468 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14469 mutex_exit(&sata_hba_inst->satahba_mutex); 14470 } 14471 14472 /* 14473 * Process port power level change reported by HBA driver. 14474 * Not implemented at this time - event is ignored. 14475 */ 14476 static void 14477 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 14478 sata_address_t *saddr) 14479 { 14480 sata_cport_info_t *cportinfo; 14481 14482 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14483 "Processing port power level change", NULL); 14484 14485 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14486 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14487 /* Reset event flag */ 14488 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14489 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14490 } 14491 14492 /* 14493 * Process port failure reported by HBA driver. 14494 * cports support only - no pmports. 14495 */ 14496 static void 14497 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 14498 sata_address_t *saddr) 14499 { 14500 sata_cport_info_t *cportinfo; 14501 14502 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14503 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14504 /* Reset event flag first */ 14505 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 14506 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 14507 if ((cportinfo->cport_state & 14508 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 14509 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14510 cport_mutex); 14511 return; 14512 } 14513 /* Fail the port */ 14514 cportinfo->cport_state = SATA_PSTATE_FAILED; 14515 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14516 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 14517 } 14518 14519 /* 14520 * Device Reset Event processing. 14521 * The seqeunce is managed by 3 stage flags: 14522 * - reset event reported, 14523 * - reset event being processed, 14524 * - request to clear device reset state. 14525 * 14526 * NOTE: This function has to be entered with cport mutex held. It exits with 14527 * mutex held as well, but can release mutex during the processing. 14528 */ 14529 static void 14530 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 14531 sata_address_t *saddr) 14532 { 14533 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 14534 sata_drive_info_t *sdinfo; 14535 sata_cport_info_t *cportinfo; 14536 sata_device_t sata_device; 14537 int rval_probe, rval_set; 14538 14539 /* We only care about host sata cport for now */ 14540 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14541 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14542 /* 14543 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 14544 * state, ignore reset event. 14545 */ 14546 if (((cportinfo->cport_state & 14547 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 14548 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 14549 sdinfo->satadrv_event_flags &= 14550 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 14551 return; 14552 } 14553 14554 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 14555 SATA_VALID_DEV_TYPE) == 0) { 14556 /* 14557 * This should not happen - coding error. 14558 * But we can recover, so do not panic, just clean up 14559 * and if in debug mode, log the message. 14560 */ 14561 #ifdef SATA_DEBUG 14562 sata_log(sata_hba_inst, CE_WARN, 14563 "sata_process_device_reset: " 14564 "Invalid device type with sdinfo!", NULL); 14565 #endif 14566 sdinfo->satadrv_event_flags = 0; 14567 return; 14568 } 14569 14570 #ifdef SATA_DEBUG 14571 if ((sdinfo->satadrv_event_flags & 14572 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 14573 /* Nothing to do */ 14574 /* Something is weird - why we are processing dev reset? */ 14575 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14576 "No device reset event!!!!", NULL); 14577 14578 return; 14579 } 14580 if ((sdinfo->satadrv_event_flags & 14581 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 14582 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 14583 /* Something is weird - new device reset event */ 14584 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14585 "Overlapping device reset events!", NULL); 14586 } 14587 #endif 14588 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14589 "Processing port %d device reset", saddr->cport); 14590 14591 /* Clear event flag */ 14592 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 14593 14594 /* It seems that we always need to check the port state first */ 14595 sata_device.satadev_rev = SATA_DEVICE_REV; 14596 sata_device.satadev_addr = *saddr; 14597 /* 14598 * We have to exit mutex, because the HBA probe port function may 14599 * block on its own mutex. 14600 */ 14601 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14602 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14603 (SATA_DIP(sata_hba_inst), &sata_device); 14604 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14605 sata_update_port_info(sata_hba_inst, &sata_device); 14606 if (rval_probe != SATA_SUCCESS) { 14607 /* Something went wrong? Fail the port */ 14608 cportinfo->cport_state = SATA_PSTATE_FAILED; 14609 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14610 if (sdinfo != NULL) 14611 sdinfo->satadrv_event_flags = 0; 14612 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14613 cport_mutex); 14614 SATA_LOG_D((sata_hba_inst, CE_WARN, 14615 "SATA port %d probing failed", 14616 saddr->cport)); 14617 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14618 saddr->cport)->cport_mutex); 14619 return; 14620 } 14621 if ((sata_device.satadev_scr.sstatus & 14622 SATA_PORT_DEVLINK_UP_MASK) != 14623 SATA_PORT_DEVLINK_UP || 14624 sata_device.satadev_type == SATA_DTYPE_NONE) { 14625 /* 14626 * No device to process, anymore. Some other event processing 14627 * would or have already performed port info cleanup. 14628 * To be safe (HBA may need it), request clearing device 14629 * reset condition. 14630 */ 14631 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14632 if (sdinfo != NULL) { 14633 sdinfo->satadrv_event_flags &= 14634 ~SATA_EVNT_INPROC_DEVICE_RESET; 14635 sdinfo->satadrv_event_flags |= 14636 SATA_EVNT_CLEAR_DEVICE_RESET; 14637 } 14638 return; 14639 } 14640 14641 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14642 if (sdinfo == NULL) { 14643 return; 14644 } 14645 if ((sdinfo->satadrv_event_flags & 14646 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 14647 /* 14648 * Start tracking time for device feature restoration and 14649 * identification. Save current time (lbolt value). 14650 */ 14651 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 14652 } 14653 /* Mark device reset processing as active */ 14654 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 14655 14656 old_sdinfo = *sdinfo; /* local copy of the drive info */ 14657 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14658 14659 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 14660 14661 if (rval_set != SATA_SUCCESS) { 14662 /* 14663 * Restoring drive setting failed. 14664 * Probe the port first, to check if the port state has changed 14665 */ 14666 sata_device.satadev_rev = SATA_DEVICE_REV; 14667 sata_device.satadev_addr = *saddr; 14668 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14669 /* probe port */ 14670 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14671 (SATA_DIP(sata_hba_inst), &sata_device); 14672 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14673 cport_mutex); 14674 if (rval_probe == SATA_SUCCESS && 14675 (sata_device.satadev_state & 14676 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 14677 (sata_device.satadev_scr.sstatus & 14678 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 14679 sata_device.satadev_type != SATA_DTYPE_NONE) { 14680 /* 14681 * We may retry this a bit later - in-process reset 14682 * condition should be already set. 14683 * Track retry time for device identification. 14684 */ 14685 if ((cportinfo->cport_dev_type & 14686 SATA_VALID_DEV_TYPE) != 0 && 14687 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 14688 sdinfo->satadrv_reset_time != 0) { 14689 clock_t cur_time = ddi_get_lbolt(); 14690 /* 14691 * If the retry time limit was not 14692 * exceeded, retry. 14693 */ 14694 if ((cur_time - sdinfo->satadrv_reset_time) < 14695 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 14696 mutex_enter( 14697 &sata_hba_inst->satahba_mutex); 14698 sata_hba_inst->satahba_event_flags |= 14699 SATA_EVNT_MAIN; 14700 mutex_exit( 14701 &sata_hba_inst->satahba_mutex); 14702 mutex_enter(&sata_mutex); 14703 sata_event_pending |= SATA_EVNT_MAIN; 14704 mutex_exit(&sata_mutex); 14705 return; 14706 } 14707 if (rval_set == SATA_RETRY) { 14708 /* 14709 * Setting drive features failed, but 14710 * the drive is still accessible, 14711 * so emit a warning message before 14712 * return. 14713 */ 14714 mutex_exit(&SATA_CPORT_INFO( 14715 sata_hba_inst, 14716 saddr->cport)->cport_mutex); 14717 goto done; 14718 } 14719 } 14720 /* Fail the drive */ 14721 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 14722 14723 sata_log(sata_hba_inst, CE_WARN, 14724 "SATA device at port %d - device failed", 14725 saddr->cport); 14726 } 14727 /* 14728 * No point of retrying - device failed or some other event 14729 * processing or already did or will do port info cleanup. 14730 * To be safe (HBA may need it), 14731 * request clearing device reset condition. 14732 */ 14733 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 14734 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 14735 sdinfo->satadrv_reset_time = 0; 14736 return; 14737 } 14738 done: 14739 /* 14740 * If setting of drive features failed, but the drive is still 14741 * accessible, emit a warning message. 14742 */ 14743 if (rval_set == SATA_RETRY) { 14744 sata_log(sata_hba_inst, CE_WARN, 14745 "SATA device at port %d - desired setting could not be " 14746 "restored after reset. Device may not operate as expected.", 14747 saddr->cport); 14748 } 14749 /* 14750 * Raise the flag indicating that the next sata command could 14751 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 14752 * reset is reported. 14753 */ 14754 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14755 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14756 sdinfo->satadrv_reset_time = 0; 14757 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 14758 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14759 sdinfo->satadrv_event_flags &= 14760 ~SATA_EVNT_INPROC_DEVICE_RESET; 14761 sdinfo->satadrv_event_flags |= 14762 SATA_EVNT_CLEAR_DEVICE_RESET; 14763 } 14764 } 14765 } 14766 14767 14768 /* 14769 * Port Link Events processing. 14770 * Every link established event may involve device reset (due to 14771 * COMRESET signal, equivalent of the hard reset) so arbitrarily 14772 * set device reset event for an attached device (if any). 14773 * If the port is in SHUTDOWN or FAILED state, ignore link events. 14774 * 14775 * The link established event processing varies, depending on the state 14776 * of the target node, HBA hotplugging capabilities, state of the port. 14777 * If the link is not active, the link established event is ignored. 14778 * If HBA cannot detect device attachment and there is no target node, 14779 * the link established event triggers device attach event processing. 14780 * Else, link established event triggers device reset event processing. 14781 * 14782 * The link lost event processing varies, depending on a HBA hotplugging 14783 * capability and the state of the port (link active or not active). 14784 * If the link is active, the lost link event is ignored. 14785 * If HBA cannot detect device removal, the lost link event triggers 14786 * device detached event processing after link lost timeout. 14787 * Else, the event is ignored. 14788 * 14789 * NOTE: Only cports are processed for now, i.e. no port multiplier ports 14790 */ 14791 static void 14792 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 14793 sata_address_t *saddr) 14794 { 14795 sata_device_t sata_device; 14796 sata_cport_info_t *cportinfo; 14797 sata_drive_info_t *sdinfo; 14798 uint32_t event_flags; 14799 int rval; 14800 14801 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14802 "Processing port %d link event(s)", saddr->cport); 14803 14804 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14805 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14806 event_flags = cportinfo->cport_event_flags; 14807 14808 /* Reset event flags first */ 14809 cportinfo->cport_event_flags &= 14810 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 14811 14812 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 14813 if ((cportinfo->cport_state & 14814 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 14815 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14816 cport_mutex); 14817 return; 14818 } 14819 14820 /* 14821 * For the sanity sake get current port state. 14822 * Set device address only. Other sata_device fields should be 14823 * set by HBA driver. 14824 */ 14825 sata_device.satadev_rev = SATA_DEVICE_REV; 14826 sata_device.satadev_addr = *saddr; 14827 /* 14828 * We have to exit mutex, because the HBA probe port function may 14829 * block on its own mutex. 14830 */ 14831 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14832 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14833 (SATA_DIP(sata_hba_inst), &sata_device); 14834 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14835 sata_update_port_info(sata_hba_inst, &sata_device); 14836 if (rval != SATA_SUCCESS) { 14837 /* Something went wrong? Fail the port */ 14838 cportinfo->cport_state = SATA_PSTATE_FAILED; 14839 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14840 cport_mutex); 14841 SATA_LOG_D((sata_hba_inst, CE_WARN, 14842 "SATA port %d probing failed", 14843 saddr->cport)); 14844 /* 14845 * We may want to release device info structure, but 14846 * it is not necessary. 14847 */ 14848 return; 14849 } else { 14850 /* port probed successfully */ 14851 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14852 } 14853 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 14854 14855 if ((sata_device.satadev_scr.sstatus & 14856 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 14857 /* Ignore event */ 14858 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14859 "Ignoring port %d link established event - " 14860 "link down", 14861 saddr->cport); 14862 goto linklost; 14863 } 14864 14865 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14866 "Processing port %d link established event", 14867 saddr->cport); 14868 14869 /* 14870 * For the sanity sake check if a device is attached - check 14871 * return state of a port probing. 14872 */ 14873 if (sata_device.satadev_type != SATA_DTYPE_NONE && 14874 sata_device.satadev_type != SATA_DTYPE_PMULT) { 14875 /* 14876 * HBA port probe indicated that there is a device 14877 * attached. Check if the framework had device info 14878 * structure attached for this device. 14879 */ 14880 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14881 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 14882 NULL); 14883 14884 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14885 if ((sdinfo->satadrv_type & 14886 SATA_VALID_DEV_TYPE) != 0) { 14887 /* 14888 * Dev info structure is present. 14889 * If dev_type is set to known type in 14890 * the framework's drive info struct 14891 * then the device existed before and 14892 * the link was probably lost 14893 * momentarily - in such case 14894 * we may want to check device 14895 * identity. 14896 * Identity check is not supported now. 14897 * 14898 * Link established event 14899 * triggers device reset event. 14900 */ 14901 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 14902 satadrv_event_flags |= 14903 SATA_EVNT_DEVICE_RESET; 14904 } 14905 } else if (cportinfo->cport_dev_type == 14906 SATA_DTYPE_NONE) { 14907 /* 14908 * We got new device attached! If HBA does not 14909 * generate device attached events, trigger it 14910 * here. 14911 */ 14912 if (!(SATA_FEATURES(sata_hba_inst) & 14913 SATA_CTLF_HOTPLUG)) { 14914 cportinfo->cport_event_flags |= 14915 SATA_EVNT_DEVICE_ATTACHED; 14916 } 14917 } 14918 /* Reset link lost timeout */ 14919 cportinfo->cport_link_lost_time = 0; 14920 } 14921 } 14922 linklost: 14923 if (event_flags & SATA_EVNT_LINK_LOST) { 14924 if ((sata_device.satadev_scr.sstatus & 14925 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 14926 /* Ignore event */ 14927 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14928 "Ignoring port %d link lost event - link is up", 14929 saddr->cport); 14930 goto done; 14931 } 14932 #ifdef SATA_DEBUG 14933 if (cportinfo->cport_link_lost_time == 0) { 14934 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14935 "Processing port %d link lost event", 14936 saddr->cport); 14937 } 14938 #endif 14939 /* 14940 * When HBA cannot generate device attached/detached events, 14941 * we need to track link lost time and eventually generate 14942 * device detach event. 14943 */ 14944 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 14945 /* We are tracking link lost time */ 14946 if (cportinfo->cport_link_lost_time == 0) { 14947 /* save current time (lbolt value) */ 14948 cportinfo->cport_link_lost_time = 14949 ddi_get_lbolt(); 14950 /* just keep link lost event */ 14951 cportinfo->cport_event_flags |= 14952 SATA_EVNT_LINK_LOST; 14953 } else { 14954 clock_t cur_time = ddi_get_lbolt(); 14955 if ((cur_time - 14956 cportinfo->cport_link_lost_time) >= 14957 drv_usectohz( 14958 SATA_EVNT_LINK_LOST_TIMEOUT)) { 14959 /* trigger device detach event */ 14960 cportinfo->cport_event_flags |= 14961 SATA_EVNT_DEVICE_DETACHED; 14962 cportinfo->cport_link_lost_time = 0; 14963 SATADBG1(SATA_DBG_EVENTS, 14964 sata_hba_inst, 14965 "Triggering port %d " 14966 "device detached event", 14967 saddr->cport); 14968 } else { 14969 /* keep link lost event */ 14970 cportinfo->cport_event_flags |= 14971 SATA_EVNT_LINK_LOST; 14972 } 14973 } 14974 } 14975 /* 14976 * We could change port state to disable/delay access to 14977 * the attached device until the link is recovered. 14978 */ 14979 } 14980 done: 14981 event_flags = cportinfo->cport_event_flags; 14982 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14983 if (event_flags != 0) { 14984 mutex_enter(&sata_hba_inst->satahba_mutex); 14985 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14986 mutex_exit(&sata_hba_inst->satahba_mutex); 14987 mutex_enter(&sata_mutex); 14988 sata_event_pending |= SATA_EVNT_MAIN; 14989 mutex_exit(&sata_mutex); 14990 } 14991 } 14992 14993 /* 14994 * Device Detached Event processing. 14995 * Port is probed to find if a device is really gone. If so, 14996 * the device info structure is detached from the SATA port info structure 14997 * and released. 14998 * Port status is updated. 14999 * 15000 * NOTE: Process cports event only, no port multiplier ports. 15001 */ 15002 static void 15003 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 15004 sata_address_t *saddr) 15005 { 15006 sata_cport_info_t *cportinfo; 15007 sata_drive_info_t *sdevinfo; 15008 sata_device_t sata_device; 15009 dev_info_t *tdip; 15010 int rval; 15011 15012 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15013 "Processing port %d device detached", saddr->cport); 15014 15015 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15016 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15017 /* Clear event flag */ 15018 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 15019 15020 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 15021 if ((cportinfo->cport_state & 15022 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 15023 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15024 cport_mutex); 15025 return; 15026 } 15027 /* For sanity, re-probe the port */ 15028 sata_device.satadev_rev = SATA_DEVICE_REV; 15029 sata_device.satadev_addr = *saddr; 15030 15031 /* 15032 * We have to exit mutex, because the HBA probe port function may 15033 * block on its own mutex. 15034 */ 15035 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15036 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15037 (SATA_DIP(sata_hba_inst), &sata_device); 15038 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15039 sata_update_port_info(sata_hba_inst, &sata_device); 15040 if (rval != SATA_SUCCESS) { 15041 /* Something went wrong? Fail the port */ 15042 cportinfo->cport_state = SATA_PSTATE_FAILED; 15043 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15044 cport_mutex); 15045 SATA_LOG_D((sata_hba_inst, CE_WARN, 15046 "SATA port %d probing failed", 15047 saddr->cport)); 15048 /* 15049 * We may want to release device info structure, but 15050 * it is not necessary. 15051 */ 15052 return; 15053 } else { 15054 /* port probed successfully */ 15055 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 15056 } 15057 /* 15058 * Check if a device is still attached. For sanity, check also 15059 * link status - if no link, there is no device. 15060 */ 15061 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 15062 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 15063 SATA_DTYPE_NONE) { 15064 /* 15065 * Device is still attached - ignore detach event. 15066 */ 15067 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15068 cport_mutex); 15069 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15070 "Ignoring detach - device still attached to port %d", 15071 sata_device.satadev_addr.cport); 15072 return; 15073 } 15074 /* 15075 * We need to detach and release device info structure here 15076 */ 15077 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15078 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15079 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15080 (void) kmem_free((void *)sdevinfo, 15081 sizeof (sata_drive_info_t)); 15082 } 15083 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15084 /* 15085 * Device cannot be reached anymore, even if the target node may be 15086 * still present. 15087 */ 15088 15089 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15090 sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d", 15091 sata_device.satadev_addr.cport); 15092 15093 /* 15094 * Try to offline a device and remove target node if it still exists 15095 */ 15096 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15097 if (tdip != NULL) { 15098 /* 15099 * Target node exists. Unconfigure device then remove 15100 * the target node (one ndi operation). 15101 */ 15102 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 15103 /* 15104 * PROBLEM - no device, but target node remained 15105 * This happens when the file was open or node was 15106 * waiting for resources. 15107 */ 15108 SATA_LOG_D((sata_hba_inst, CE_WARN, 15109 "sata_process_device_detached: " 15110 "Failed to remove target node for " 15111 "detached SATA device.")); 15112 /* 15113 * Set target node state to DEVI_DEVICE_REMOVED. 15114 * But re-check first that the node still exists. 15115 */ 15116 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15117 saddr->cport); 15118 if (tdip != NULL) { 15119 sata_set_device_removed(tdip); 15120 /* 15121 * Instruct event daemon to retry the 15122 * cleanup later. 15123 */ 15124 sata_set_target_node_cleanup(sata_hba_inst, 15125 &sata_device.satadev_addr); 15126 } 15127 } 15128 } 15129 /* 15130 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15131 * with the hint: SE_HINT_REMOVE 15132 */ 15133 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 15134 } 15135 15136 15137 /* 15138 * Device Attached Event processing. 15139 * Port state is checked to verify that a device is really attached. If so, 15140 * the device info structure is created and attached to the SATA port info 15141 * structure. 15142 * 15143 * If attached device cannot be identified or set-up, the retry for the 15144 * attach processing is set-up. Subsequent daemon run would try again to 15145 * identify the device, until the time limit is reached 15146 * (SATA_DEV_IDENTIFY_TIMEOUT). 15147 * 15148 * This function cannot be called in interrupt context (it may sleep). 15149 * 15150 * NOTE: Process cports event only, no port multiplier ports. 15151 */ 15152 static void 15153 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 15154 sata_address_t *saddr) 15155 { 15156 sata_cport_info_t *cportinfo; 15157 sata_drive_info_t *sdevinfo; 15158 sata_device_t sata_device; 15159 dev_info_t *tdip; 15160 uint32_t event_flags; 15161 int rval; 15162 15163 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15164 "Processing port %d device attached", saddr->cport); 15165 15166 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15167 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15168 15169 /* Clear attach event flag first */ 15170 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 15171 15172 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 15173 if ((cportinfo->cport_state & 15174 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 15175 cportinfo->cport_dev_attach_time = 0; 15176 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15177 cport_mutex); 15178 return; 15179 } 15180 15181 /* 15182 * If the sata_drive_info structure is found attached to the port info, 15183 * despite the fact the device was removed and now it is re-attached, 15184 * the old drive info structure was not removed. 15185 * Arbitrarily release device info structure. 15186 */ 15187 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15188 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15189 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15190 (void) kmem_free((void *)sdevinfo, 15191 sizeof (sata_drive_info_t)); 15192 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15193 "Arbitrarily detaching old device info.", NULL); 15194 } 15195 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15196 15197 /* For sanity, re-probe the port */ 15198 sata_device.satadev_rev = SATA_DEVICE_REV; 15199 sata_device.satadev_addr = *saddr; 15200 15201 /* 15202 * We have to exit mutex, because the HBA probe port function may 15203 * block on its own mutex. 15204 */ 15205 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15206 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15207 (SATA_DIP(sata_hba_inst), &sata_device); 15208 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15209 sata_update_port_info(sata_hba_inst, &sata_device); 15210 if (rval != SATA_SUCCESS) { 15211 /* Something went wrong? Fail the port */ 15212 cportinfo->cport_state = SATA_PSTATE_FAILED; 15213 cportinfo->cport_dev_attach_time = 0; 15214 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15215 cport_mutex); 15216 SATA_LOG_D((sata_hba_inst, CE_WARN, 15217 "SATA port %d probing failed", 15218 saddr->cport)); 15219 return; 15220 } else { 15221 /* port probed successfully */ 15222 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 15223 } 15224 /* 15225 * Check if a device is still attached. For sanity, check also 15226 * link status - if no link, there is no device. 15227 */ 15228 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 15229 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 15230 SATA_DTYPE_NONE) { 15231 /* 15232 * No device - ignore attach event. 15233 */ 15234 cportinfo->cport_dev_attach_time = 0; 15235 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15236 cport_mutex); 15237 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15238 "Ignoring attach - no device connected to port %d", 15239 sata_device.satadev_addr.cport); 15240 return; 15241 } 15242 15243 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15244 /* 15245 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15246 * with the hint: SE_HINT_INSERT 15247 */ 15248 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 15249 15250 /* 15251 * Port reprobing will take care of the creation of the device 15252 * info structure and determination of the device type. 15253 */ 15254 sata_device.satadev_addr = *saddr; 15255 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 15256 SATA_DEV_IDENTIFY_NORETRY); 15257 15258 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15259 cport_mutex); 15260 if ((cportinfo->cport_state & SATA_STATE_READY) && 15261 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 15262 /* Some device is attached to the port */ 15263 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 15264 /* 15265 * A device was not successfully attached. 15266 * Track retry time for device identification. 15267 */ 15268 if (cportinfo->cport_dev_attach_time != 0) { 15269 clock_t cur_time = ddi_get_lbolt(); 15270 /* 15271 * If the retry time limit was not exceeded, 15272 * reinstate attach event. 15273 */ 15274 if ((cur_time - 15275 cportinfo->cport_dev_attach_time) < 15276 drv_usectohz( 15277 SATA_DEV_IDENTIFY_TIMEOUT)) { 15278 /* OK, restore attach event */ 15279 cportinfo->cport_event_flags |= 15280 SATA_EVNT_DEVICE_ATTACHED; 15281 } else { 15282 /* Timeout - cannot identify device */ 15283 cportinfo->cport_dev_attach_time = 0; 15284 sata_log(sata_hba_inst, 15285 CE_WARN, 15286 "Could not identify SATA device " 15287 "at port %d", 15288 saddr->cport); 15289 } 15290 } else { 15291 /* 15292 * Start tracking time for device 15293 * identification. 15294 * Save current time (lbolt value). 15295 */ 15296 cportinfo->cport_dev_attach_time = 15297 ddi_get_lbolt(); 15298 /* Restore attach event */ 15299 cportinfo->cport_event_flags |= 15300 SATA_EVNT_DEVICE_ATTACHED; 15301 } 15302 } else { 15303 /* 15304 * If device was successfully attached, the subsequent 15305 * action depends on a state of the 15306 * sata_auto_online variable. If it is set to zero. 15307 * an explicit 'configure' command will be needed to 15308 * configure it. If its value is non-zero, we will 15309 * attempt to online (configure) the device. 15310 * First, log the message indicating that a device 15311 * was attached. 15312 */ 15313 cportinfo->cport_dev_attach_time = 0; 15314 sata_log(sata_hba_inst, CE_WARN, 15315 "SATA device detected at port %d", saddr->cport); 15316 15317 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15318 sata_drive_info_t new_sdinfo; 15319 15320 /* Log device info data */ 15321 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 15322 cportinfo)); 15323 sata_show_drive_info(sata_hba_inst, 15324 &new_sdinfo); 15325 } 15326 15327 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15328 saddr->cport)->cport_mutex); 15329 15330 /* 15331 * Make sure that there is no target node for that 15332 * device. If so, release it. It should not happen, 15333 * unless we had problem removing the node when 15334 * device was detached. 15335 */ 15336 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15337 saddr->cport); 15338 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15339 saddr->cport)->cport_mutex); 15340 if (tdip != NULL) { 15341 15342 #ifdef SATA_DEBUG 15343 if ((cportinfo->cport_event_flags & 15344 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 15345 sata_log(sata_hba_inst, CE_WARN, 15346 "sata_process_device_attached: " 15347 "old device target node exists!"); 15348 #endif 15349 /* 15350 * target node exists - try to unconfigure 15351 * device and remove the node. 15352 */ 15353 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15354 saddr->cport)->cport_mutex); 15355 rval = ndi_devi_offline(tdip, 15356 NDI_DEVI_REMOVE); 15357 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15358 saddr->cport)->cport_mutex); 15359 15360 if (rval == NDI_SUCCESS) { 15361 cportinfo->cport_event_flags &= 15362 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15363 cportinfo->cport_tgtnode_clean = B_TRUE; 15364 } else { 15365 /* 15366 * PROBLEM - the target node remained 15367 * and it belongs to a previously 15368 * attached device. 15369 * This happens when the file was open 15370 * or the node was waiting for 15371 * resources at the time the 15372 * associated device was removed. 15373 * Instruct event daemon to retry the 15374 * cleanup later. 15375 */ 15376 sata_log(sata_hba_inst, 15377 CE_WARN, 15378 "Application(s) accessing " 15379 "previously attached SATA " 15380 "device have to release " 15381 "it before newly inserted " 15382 "device can be made accessible.", 15383 saddr->cport); 15384 cportinfo->cport_event_flags |= 15385 SATA_EVNT_TARGET_NODE_CLEANUP; 15386 cportinfo->cport_tgtnode_clean = 15387 B_FALSE; 15388 } 15389 } 15390 if (sata_auto_online != 0) { 15391 cportinfo->cport_event_flags |= 15392 SATA_EVNT_AUTOONLINE_DEVICE; 15393 } 15394 15395 } 15396 } else { 15397 cportinfo->cport_dev_attach_time = 0; 15398 } 15399 15400 event_flags = cportinfo->cport_event_flags; 15401 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15402 if (event_flags != 0) { 15403 mutex_enter(&sata_hba_inst->satahba_mutex); 15404 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15405 mutex_exit(&sata_hba_inst->satahba_mutex); 15406 mutex_enter(&sata_mutex); 15407 sata_event_pending |= SATA_EVNT_MAIN; 15408 mutex_exit(&sata_mutex); 15409 } 15410 } 15411 15412 15413 /* 15414 * Device Target Node Cleanup Event processing. 15415 * If the target node associated with a sata port device is in 15416 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 15417 * If the target node cannot be removed, the event flag is left intact, 15418 * so that event daemon may re-run this function later. 15419 * 15420 * This function cannot be called in interrupt context (it may sleep). 15421 * 15422 * NOTE: Processes cport events only, not port multiplier ports. 15423 */ 15424 static void 15425 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15426 sata_address_t *saddr) 15427 { 15428 sata_cport_info_t *cportinfo; 15429 dev_info_t *tdip; 15430 15431 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15432 "Processing port %d device target node cleanup", saddr->cport); 15433 15434 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15435 15436 /* 15437 * Check if there is target node for that device and it is in the 15438 * DEVI_DEVICE_REMOVED state. If so, release it. 15439 */ 15440 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15441 if (tdip != NULL) { 15442 /* 15443 * target node exists - check if it is target node of 15444 * a removed device. 15445 */ 15446 if (sata_check_device_removed(tdip) == B_TRUE) { 15447 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15448 "sata_process_target_node_cleanup: " 15449 "old device target node exists!", NULL); 15450 /* 15451 * Unconfigure and remove the target node 15452 */ 15453 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 15454 NDI_SUCCESS) { 15455 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15456 saddr->cport)->cport_mutex); 15457 cportinfo->cport_event_flags &= 15458 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15459 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15460 saddr->cport)->cport_mutex); 15461 return; 15462 } 15463 /* 15464 * Event daemon will retry the cleanup later. 15465 */ 15466 mutex_enter(&sata_hba_inst->satahba_mutex); 15467 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15468 mutex_exit(&sata_hba_inst->satahba_mutex); 15469 mutex_enter(&sata_mutex); 15470 sata_event_pending |= SATA_EVNT_MAIN; 15471 mutex_exit(&sata_mutex); 15472 } 15473 } else { 15474 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15475 saddr->cport)->cport_mutex); 15476 cportinfo->cport_event_flags &= 15477 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15478 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15479 saddr->cport)->cport_mutex); 15480 } 15481 } 15482 15483 /* 15484 * Device AutoOnline Event processing. 15485 * If attached device is to be onlined, an attempt is made to online this 15486 * device, but only if there is no lingering (old) target node present. 15487 * If the device cannot be onlined, the event flag is left intact, 15488 * so that event daemon may re-run this function later. 15489 * 15490 * This function cannot be called in interrupt context (it may sleep). 15491 * 15492 * NOTE: Processes cport events only, not port multiplier ports. 15493 */ 15494 static void 15495 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 15496 sata_address_t *saddr) 15497 { 15498 sata_cport_info_t *cportinfo; 15499 sata_drive_info_t *sdinfo; 15500 sata_device_t sata_device; 15501 dev_info_t *tdip; 15502 15503 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15504 "Processing port %d attached device auto-onlining", saddr->cport); 15505 15506 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15507 15508 /* 15509 * Check if device is present and recognized. If not, reset event. 15510 */ 15511 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15512 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 15513 /* Nothing to online */ 15514 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15515 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15516 saddr->cport)->cport_mutex); 15517 return; 15518 } 15519 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15520 15521 /* 15522 * Check if there is target node for this device and if it is in the 15523 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 15524 * the event for later processing. 15525 */ 15526 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15527 if (tdip != NULL) { 15528 /* 15529 * target node exists - check if it is target node of 15530 * a removed device. 15531 */ 15532 if (sata_check_device_removed(tdip) == B_TRUE) { 15533 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15534 "sata_process_device_autoonline: " 15535 "old device target node exists!", NULL); 15536 /* 15537 * Event daemon will retry device onlining later. 15538 */ 15539 mutex_enter(&sata_hba_inst->satahba_mutex); 15540 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15541 mutex_exit(&sata_hba_inst->satahba_mutex); 15542 mutex_enter(&sata_mutex); 15543 sata_event_pending |= SATA_EVNT_MAIN; 15544 mutex_exit(&sata_mutex); 15545 return; 15546 } 15547 /* 15548 * If the target node is not in the 'removed" state, assume 15549 * that it belongs to this device. There is nothing more to do, 15550 * but reset the event. 15551 */ 15552 } else { 15553 15554 /* 15555 * Try to online the device 15556 * If there is any reset-related event, remove it. We are 15557 * configuring the device and no state restoring is needed. 15558 */ 15559 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15560 saddr->cport)->cport_mutex); 15561 sata_device.satadev_addr = *saddr; 15562 if (saddr->qual == SATA_ADDR_CPORT) 15563 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 15564 else 15565 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 15566 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 15567 if (sdinfo != NULL) { 15568 if (sdinfo->satadrv_event_flags & 15569 (SATA_EVNT_DEVICE_RESET | 15570 SATA_EVNT_INPROC_DEVICE_RESET)) 15571 sdinfo->satadrv_event_flags = 0; 15572 sdinfo->satadrv_event_flags |= 15573 SATA_EVNT_CLEAR_DEVICE_RESET; 15574 15575 /* Need to create a new target node. */ 15576 cportinfo->cport_tgtnode_clean = B_TRUE; 15577 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15578 saddr->cport)->cport_mutex); 15579 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15580 sata_hba_inst, &sata_device.satadev_addr); 15581 if (tdip == NULL) { 15582 /* 15583 * Configure (onlining) failed. 15584 * We will NOT retry 15585 */ 15586 SATA_LOG_D((sata_hba_inst, CE_WARN, 15587 "sata_process_device_autoonline: " 15588 "configuring SATA device at port %d failed", 15589 saddr->cport)); 15590 } 15591 } else { 15592 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15593 saddr->cport)->cport_mutex); 15594 } 15595 15596 } 15597 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15598 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15599 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15600 saddr->cport)->cport_mutex); 15601 } 15602 15603 15604 static void 15605 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 15606 int hint) 15607 { 15608 char ap[MAXPATHLEN]; 15609 nvlist_t *ev_attr_list = NULL; 15610 int err; 15611 15612 /* Allocate and build sysevent attribute list */ 15613 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 15614 if (err != 0) { 15615 SATA_LOG_D((sata_hba_inst, CE_WARN, 15616 "sata_gen_sysevent: " 15617 "cannot allocate memory for sysevent attributes\n")); 15618 return; 15619 } 15620 /* Add hint attribute */ 15621 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 15622 if (err != 0) { 15623 SATA_LOG_D((sata_hba_inst, CE_WARN, 15624 "sata_gen_sysevent: " 15625 "failed to add DR_HINT attr for sysevent")); 15626 nvlist_free(ev_attr_list); 15627 return; 15628 } 15629 /* 15630 * Add AP attribute. 15631 * Get controller pathname and convert it into AP pathname by adding 15632 * a target number. 15633 */ 15634 (void) snprintf(ap, MAXPATHLEN, "/devices"); 15635 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 15636 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 15637 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 15638 15639 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 15640 if (err != 0) { 15641 SATA_LOG_D((sata_hba_inst, CE_WARN, 15642 "sata_gen_sysevent: " 15643 "failed to add DR_AP_ID attr for sysevent")); 15644 nvlist_free(ev_attr_list); 15645 return; 15646 } 15647 15648 /* Generate/log sysevent */ 15649 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 15650 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 15651 if (err != DDI_SUCCESS) { 15652 SATA_LOG_D((sata_hba_inst, CE_WARN, 15653 "sata_gen_sysevent: " 15654 "cannot log sysevent, err code %x\n", err)); 15655 } 15656 15657 nvlist_free(ev_attr_list); 15658 } 15659 15660 15661 15662 15663 /* 15664 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 15665 */ 15666 static void 15667 sata_set_device_removed(dev_info_t *tdip) 15668 { 15669 int circ; 15670 15671 ASSERT(tdip != NULL); 15672 15673 ndi_devi_enter(tdip, &circ); 15674 mutex_enter(&DEVI(tdip)->devi_lock); 15675 DEVI_SET_DEVICE_REMOVED(tdip); 15676 mutex_exit(&DEVI(tdip)->devi_lock); 15677 ndi_devi_exit(tdip, circ); 15678 } 15679 15680 15681 /* 15682 * Set internal event instructing event daemon to try 15683 * to perform the target node cleanup. 15684 */ 15685 static void 15686 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15687 sata_address_t *saddr) 15688 { 15689 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15690 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 15691 SATA_EVNT_TARGET_NODE_CLEANUP; 15692 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean = 15693 B_FALSE; 15694 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15695 mutex_enter(&sata_hba_inst->satahba_mutex); 15696 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15697 mutex_exit(&sata_hba_inst->satahba_mutex); 15698 mutex_enter(&sata_mutex); 15699 sata_event_pending |= SATA_EVNT_MAIN; 15700 mutex_exit(&sata_mutex); 15701 } 15702 15703 15704 /* 15705 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 15706 * i.e. check if the target node state indicates that it belongs to a removed 15707 * device. 15708 * 15709 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 15710 * B_FALSE otherwise. 15711 * 15712 * NOTE: No port multiplier support. 15713 */ 15714 static boolean_t 15715 sata_check_device_removed(dev_info_t *tdip) 15716 { 15717 ASSERT(tdip != NULL); 15718 15719 if (DEVI_IS_DEVICE_REMOVED(tdip)) 15720 return (B_TRUE); 15721 else 15722 return (B_FALSE); 15723 } 15724 15725 /* ************************ FAULT INJECTTION **************************** */ 15726 15727 #ifdef SATA_INJECT_FAULTS 15728 15729 static uint32_t sata_fault_count = 0; 15730 static uint32_t sata_fault_suspend_count = 0; 15731 15732 /* 15733 * Inject sata pkt fault 15734 * It modifies returned values of the sata packet. 15735 * It returns immediately if: 15736 * pkt fault injection is not enabled (via sata_inject_fault, 15737 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 15738 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 15739 * pkt is not directed to specified fault controller/device 15740 * (sata_fault_ctrl_dev and sata_fault_device). 15741 * If fault controller is not specified, fault injection applies to all 15742 * controllers and devices. 15743 * 15744 * First argument is the pointer to the executed sata packet. 15745 * Second argument is a pointer to a value returned by the HBA tran_start 15746 * function. 15747 * Third argument specifies injected error. Injected sata packet faults 15748 * are the satapkt_reason values. 15749 * SATA_PKT_BUSY -1 Not completed, busy 15750 * SATA_PKT_DEV_ERROR 1 Device reported error 15751 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 15752 * SATA_PKT_PORT_ERROR 3 Not completed, port error 15753 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 15754 * SATA_PKT_ABORTED 5 Aborted by request 15755 * SATA_PKT_TIMEOUT 6 Operation timeut 15756 * SATA_PKT_RESET 7 Aborted by reset request 15757 * 15758 * Additional global variables affecting the execution: 15759 * 15760 * sata_inject_fault_count variable specifies number of times in row the 15761 * error is injected. Value of -1 specifies permanent fault, ie. every time 15762 * the fault injection point is reached, the fault is injected and a pause 15763 * between fault injection specified by sata_inject_fault_pause_count is 15764 * ignored). Fault injection routine decrements sata_inject_fault_count 15765 * (if greater than zero) until it reaches 0. No fault is injected when 15766 * sata_inject_fault_count is 0 (zero). 15767 * 15768 * sata_inject_fault_pause_count variable specifies number of times a fault 15769 * injection is bypassed (pause between fault injections). 15770 * If set to 0, a fault is injected only a number of times specified by 15771 * sata_inject_fault_count. 15772 * 15773 * The fault counts are static, so for periodic errors they have to be manually 15774 * reset to start repetition sequence from scratch. 15775 * If the original value returned by the HBA tran_start function is not 15776 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 15777 * is injected (to avoid masking real problems); 15778 * 15779 * NOTE: In its current incarnation, this function should be invoked only for 15780 * commands executed in SYNCHRONOUS mode. 15781 */ 15782 15783 15784 static void 15785 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 15786 { 15787 15788 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 15789 return; 15790 15791 if (sata_inject_fault_count == 0) 15792 return; 15793 15794 if (fault == 0) 15795 return; 15796 15797 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 15798 return; 15799 15800 if (sata_fault_ctrl != NULL) { 15801 sata_pkt_txlate_t *spx = 15802 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 15803 15804 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 15805 spx->txlt_sata_hba_inst->satahba_dip) 15806 return; 15807 15808 if (sata_fault_device.satadev_addr.cport != 15809 spkt->satapkt_device.satadev_addr.cport || 15810 sata_fault_device.satadev_addr.pmport != 15811 spkt->satapkt_device.satadev_addr.pmport || 15812 sata_fault_device.satadev_addr.qual != 15813 spkt->satapkt_device.satadev_addr.qual) 15814 return; 15815 } 15816 15817 /* Modify pkt return parameters */ 15818 if (*rval != SATA_TRAN_ACCEPTED || 15819 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15820 sata_fault_count = 0; 15821 sata_fault_suspend_count = 0; 15822 return; 15823 } 15824 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 15825 /* Pause in the injection */ 15826 sata_fault_suspend_count -= 1; 15827 return; 15828 } 15829 15830 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 15831 /* 15832 * Init inject fault cycle. If fault count is set to -1, 15833 * it is a permanent fault. 15834 */ 15835 if (sata_inject_fault_count != -1) { 15836 sata_fault_count = sata_inject_fault_count; 15837 sata_fault_suspend_count = 15838 sata_inject_fault_pause_count; 15839 if (sata_fault_suspend_count == 0) 15840 sata_inject_fault_count = 0; 15841 } 15842 } 15843 15844 if (sata_fault_count != 0) 15845 sata_fault_count -= 1; 15846 15847 switch (fault) { 15848 case SATA_PKT_BUSY: 15849 *rval = SATA_TRAN_BUSY; 15850 spkt->satapkt_reason = SATA_PKT_BUSY; 15851 break; 15852 15853 case SATA_PKT_QUEUE_FULL: 15854 *rval = SATA_TRAN_QUEUE_FULL; 15855 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 15856 break; 15857 15858 case SATA_PKT_CMD_UNSUPPORTED: 15859 *rval = SATA_TRAN_CMD_UNSUPPORTED; 15860 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 15861 break; 15862 15863 case SATA_PKT_PORT_ERROR: 15864 /* This is "rejected" command */ 15865 *rval = SATA_TRAN_PORT_ERROR; 15866 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 15867 /* Additional error setup could be done here - port state */ 15868 break; 15869 15870 case SATA_PKT_DEV_ERROR: 15871 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 15872 /* 15873 * Additional error setup could be done here 15874 */ 15875 break; 15876 15877 case SATA_PKT_ABORTED: 15878 spkt->satapkt_reason = SATA_PKT_ABORTED; 15879 break; 15880 15881 case SATA_PKT_TIMEOUT: 15882 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 15883 /* Additional error setup could be done here */ 15884 break; 15885 15886 case SATA_PKT_RESET: 15887 spkt->satapkt_reason = SATA_PKT_RESET; 15888 /* 15889 * Additional error setup could be done here - device reset 15890 */ 15891 break; 15892 15893 default: 15894 break; 15895 } 15896 } 15897 15898 #endif 15899