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 2009 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.42"}; 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 /* Default write cache setting for SATA ATAPI disk */ 424 int sata_atapidisk_write_cache = 1; /* enabled */ 425 426 /* 427 * Linked list of HBA instances 428 */ 429 static sata_hba_inst_t *sata_hba_list = NULL; 430 static sata_hba_inst_t *sata_hba_list_tail = NULL; 431 /* 432 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 433 * structure and in sata soft state. 434 */ 435 436 /* 437 * Event daemon related variables 438 */ 439 static kmutex_t sata_event_mutex; 440 static kcondvar_t sata_event_cv; 441 static kthread_t *sata_event_thread = NULL; 442 static int sata_event_thread_terminate = 0; 443 static int sata_event_pending = 0; 444 static int sata_event_thread_active = 0; 445 extern pri_t minclsyspri; 446 447 /* 448 * NCQ error recovery command 449 */ 450 static const sata_cmd_t sata_rle_cmd = { 451 SATA_CMD_REV, 452 NULL, 453 { 454 SATA_DIR_READ 455 }, 456 ATA_ADDR_LBA48, 457 0, 458 0, 459 0, 460 0, 461 0, 462 1, 463 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 464 0, 465 0, 466 0, 467 SATAC_READ_LOG_EXT, 468 0, 469 0, 470 0, 471 }; 472 473 /* 474 * ATAPI error recovery CDB 475 */ 476 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 477 SCMD_REQUEST_SENSE, 478 0, /* Only fixed RQ format is supported */ 479 0, 480 0, 481 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 482 0 483 }; 484 485 486 /* Warlock directives */ 487 488 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 489 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 491 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 492 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 494 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 495 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 496 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 497 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 498 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 499 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 500 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 502 sata_hba_inst::satahba_scsi_tran)) 503 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 504 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 505 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 506 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 507 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 508 sata_hba_inst::satahba_event_flags)) 509 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 510 sata_cport_info::cport_devp)) 511 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 512 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 513 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 514 sata_cport_info::cport_dev_type)) 515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 516 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 517 sata_cport_info::cport_state)) 518 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 519 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 520 sata_pmport_info::pmport_state)) 521 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 522 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 523 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 524 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 525 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 526 #ifdef SATA_DEBUG 527 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 528 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 529 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 530 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 531 #endif 532 533 /* End of warlock directives */ 534 535 /* ************** loadable module configuration functions ************** */ 536 537 int 538 _init() 539 { 540 int rval; 541 542 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 543 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 544 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 545 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 546 if ((rval = mod_install(&modlinkage)) != 0) { 547 #ifdef SATA_DEBUG 548 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 549 #endif 550 mutex_destroy(&sata_log_mutex); 551 cv_destroy(&sata_event_cv); 552 mutex_destroy(&sata_event_mutex); 553 mutex_destroy(&sata_mutex); 554 } 555 return (rval); 556 } 557 558 int 559 _fini() 560 { 561 int rval; 562 563 if ((rval = mod_remove(&modlinkage)) != 0) 564 return (rval); 565 566 mutex_destroy(&sata_log_mutex); 567 cv_destroy(&sata_event_cv); 568 mutex_destroy(&sata_event_mutex); 569 mutex_destroy(&sata_mutex); 570 return (rval); 571 } 572 573 int 574 _info(struct modinfo *modinfop) 575 { 576 return (mod_info(&modlinkage, modinfop)); 577 } 578 579 580 581 /* ********************* SATA HBA entry points ********************* */ 582 583 584 /* 585 * Called by SATA HBA from _init(). 586 * Registers HBA driver instance/sata framework pair with scsi framework, by 587 * calling scsi_hba_init(). 588 * 589 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 590 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 591 * cb_ops pointer in SATA HBA driver dev_ops structure. 592 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 593 * 594 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 595 * driver. 596 */ 597 int 598 sata_hba_init(struct modlinkage *modlp) 599 { 600 int rval; 601 struct dev_ops *hba_ops; 602 603 SATADBG1(SATA_DBG_HBA_IF, NULL, 604 "sata_hba_init: name %s \n", 605 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 606 /* 607 * Fill-up cb_ops and dev_ops when necessary 608 */ 609 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 610 /* 611 * Provide pointer to SATA dev_ops 612 */ 613 hba_ops->devo_cb_ops = &sata_cb_ops; 614 615 /* 616 * Register SATA HBA with SCSI framework 617 */ 618 if ((rval = scsi_hba_init(modlp)) != 0) { 619 SATADBG1(SATA_DBG_HBA_IF, NULL, 620 "sata_hba_init: scsi hba init failed\n", NULL); 621 return (rval); 622 } 623 624 return (0); 625 } 626 627 628 /* HBA attach stages */ 629 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 630 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 631 #define HBA_ATTACH_STAGE_SETUP 4 632 #define HBA_ATTACH_STAGE_LINKED 8 633 634 635 /* 636 * 637 * Called from SATA HBA driver's attach routine to attach an instance of 638 * the HBA. 639 * 640 * For DDI_ATTACH command: 641 * sata_hba_inst structure is allocated here and initialized with pointers to 642 * SATA framework implementation of required scsi tran functions. 643 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 644 * to the soft structure (sata_hba_inst) allocated by SATA framework for 645 * SATA HBA instance related data. 646 * The scsi_tran's tran_hba_private field is used by SATA framework to 647 * store a pointer to per-HBA-instance of sata_hba_inst structure. 648 * The sata_hba_inst structure is cross-linked to scsi tran structure. 649 * Among other info, a pointer to sata_hba_tran structure is stored in 650 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 651 * linked together into the list, pointed to by sata_hba_list. 652 * On the first HBA instance attach the sata event thread is initialized. 653 * Attachment points are created for all SATA ports of the HBA being attached. 654 * All HBA instance's SATA ports are probed and type of plugged devices is 655 * determined. For each device of a supported type, a target node is created. 656 * 657 * DDI_SUCCESS is returned when attachment process is successful, 658 * DDI_FAILURE is returned otherwise. 659 * 660 * For DDI_RESUME command: 661 * Not implemented at this time (postponed until phase 2 of the development). 662 */ 663 int 664 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 665 ddi_attach_cmd_t cmd) 666 { 667 sata_hba_inst_t *sata_hba_inst; 668 scsi_hba_tran_t *scsi_tran = NULL; 669 int hba_attach_state = 0; 670 char taskq_name[MAXPATHLEN]; 671 672 SATADBG3(SATA_DBG_HBA_IF, NULL, 673 "sata_hba_attach: node %s (%s%d)\n", 674 ddi_node_name(dip), ddi_driver_name(dip), 675 ddi_get_instance(dip)); 676 677 if (cmd == DDI_RESUME) { 678 /* 679 * Postponed until phase 2 of the development 680 */ 681 return (DDI_FAILURE); 682 } 683 684 if (cmd != DDI_ATTACH) { 685 return (DDI_FAILURE); 686 } 687 688 /* cmd == DDI_ATTACH */ 689 690 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 691 SATA_LOG_D((NULL, CE_WARN, 692 "sata_hba_attach: invalid sata_hba_tran")); 693 return (DDI_FAILURE); 694 } 695 /* 696 * Allocate and initialize SCSI tran structure. 697 * SATA copy of tran_bus_config is provided to create port nodes. 698 */ 699 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 700 if (scsi_tran == NULL) 701 return (DDI_FAILURE); 702 /* 703 * Allocate soft structure for SATA HBA instance. 704 * There is a separate softstate for each HBA instance. 705 */ 706 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 707 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 708 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 709 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 710 711 /* 712 * scsi_trans's tran_hba_private is used by SATA Framework to point to 713 * soft structure allocated by SATA framework for 714 * SATA HBA instance related data. 715 */ 716 scsi_tran->tran_hba_private = sata_hba_inst; 717 scsi_tran->tran_tgt_private = NULL; 718 719 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 720 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 721 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 722 723 scsi_tran->tran_start = sata_scsi_start; 724 scsi_tran->tran_reset = sata_scsi_reset; 725 scsi_tran->tran_abort = sata_scsi_abort; 726 scsi_tran->tran_getcap = sata_scsi_getcap; 727 scsi_tran->tran_setcap = sata_scsi_setcap; 728 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 729 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 730 731 scsi_tran->tran_dmafree = sata_scsi_dmafree; 732 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 733 734 scsi_tran->tran_reset_notify = NULL; 735 scsi_tran->tran_get_bus_addr = NULL; 736 scsi_tran->tran_quiesce = NULL; 737 scsi_tran->tran_unquiesce = NULL; 738 scsi_tran->tran_bus_reset = NULL; 739 740 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 741 scsi_tran, 0) != DDI_SUCCESS) { 742 #ifdef SATA_DEBUG 743 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 744 ddi_driver_name(dip), ddi_get_instance(dip)); 745 #endif 746 goto fail; 747 } 748 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 749 750 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 751 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 752 "sata", 1) != DDI_PROP_SUCCESS) { 753 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 754 "failed to create hba sata prop")); 755 goto fail; 756 } 757 } 758 759 /* 760 * Save pointers in hba instance soft state. 761 */ 762 sata_hba_inst->satahba_scsi_tran = scsi_tran; 763 sata_hba_inst->satahba_tran = sata_tran; 764 sata_hba_inst->satahba_dip = dip; 765 766 /* 767 * Create a task queue to handle emulated commands completion 768 * Use node name, dash, instance number as the queue name. 769 */ 770 taskq_name[0] = '\0'; 771 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 772 sizeof (taskq_name)); 773 (void) snprintf(taskq_name + strlen(taskq_name), 774 sizeof (taskq_name) - strlen(taskq_name), 775 "-%d", DEVI(dip)->devi_instance); 776 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 777 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports, 778 TASKQ_DYNAMIC); 779 780 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 781 782 /* 783 * Create events thread if not created yet. 784 */ 785 sata_event_thread_control(1); 786 787 /* 788 * Link this hba instance into the list. 789 */ 790 mutex_enter(&sata_mutex); 791 792 if (sata_hba_list == NULL) { 793 /* 794 * The first instance of HBA is attached. 795 * Set current/active default maximum NCQ/TCQ queue depth for 796 * all SATA devices. It is done here and now, to eliminate the 797 * possibility of the dynamic, programatic modification of the 798 * queue depth via global (and public) sata_max_queue_depth 799 * variable (this would require special handling in HBA drivers) 800 */ 801 sata_current_max_qdepth = sata_max_queue_depth; 802 if (sata_current_max_qdepth > 32) 803 sata_current_max_qdepth = 32; 804 else if (sata_current_max_qdepth < 1) 805 sata_current_max_qdepth = 1; 806 } 807 808 sata_hba_inst->satahba_next = NULL; 809 sata_hba_inst->satahba_prev = sata_hba_list_tail; 810 if (sata_hba_list == NULL) { 811 sata_hba_list = sata_hba_inst; 812 } 813 if (sata_hba_list_tail != NULL) { 814 sata_hba_list_tail->satahba_next = sata_hba_inst; 815 } 816 sata_hba_list_tail = sata_hba_inst; 817 mutex_exit(&sata_mutex); 818 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 819 820 /* 821 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 822 * SATA HBA driver should not use its own open/close entry points. 823 * 824 * Make sure that instance number doesn't overflow 825 * when forming minor numbers. 826 */ 827 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 828 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 829 INST2DEVCTL(ddi_get_instance(dip)), 830 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 831 #ifdef SATA_DEBUG 832 cmn_err(CE_WARN, "sata_hba_attach: " 833 "cannot create devctl minor node"); 834 #endif 835 goto fail; 836 } 837 838 839 /* 840 * Set-up kstats here, if necessary. 841 * (postponed until future phase of the development). 842 */ 843 844 /* 845 * Indicate that HBA is attached. This will enable events processing 846 * for this HBA. 847 */ 848 sata_hba_inst->satahba_attached = 1; 849 /* 850 * Probe controller ports. This operation will describe a current 851 * controller/port/multipliers/device configuration and will create 852 * attachment points. 853 * We may end-up with just a controller with no devices attached. 854 * For the ports with a supported device attached, device target nodes 855 * are created and devices are initialized. 856 */ 857 sata_probe_ports(sata_hba_inst); 858 859 return (DDI_SUCCESS); 860 861 fail: 862 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 863 (void) sata_remove_hba_instance(dip); 864 if (sata_hba_list == NULL) 865 sata_event_thread_control(0); 866 } 867 868 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 869 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 870 taskq_destroy(sata_hba_inst->satahba_taskq); 871 } 872 873 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 874 (void) scsi_hba_detach(dip); 875 876 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 877 mutex_destroy(&sata_hba_inst->satahba_mutex); 878 kmem_free((void *)sata_hba_inst, 879 sizeof (struct sata_hba_inst)); 880 scsi_hba_tran_free(scsi_tran); 881 } 882 883 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 884 ddi_driver_name(dip), ddi_get_instance(dip)); 885 886 return (DDI_FAILURE); 887 } 888 889 890 /* 891 * Called by SATA HBA from to detach an instance of the driver. 892 * 893 * For DDI_DETACH command: 894 * Free local structures allocated for SATA HBA instance during 895 * sata_hba_attach processing. 896 * 897 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 898 * 899 * For DDI_SUSPEND command: 900 * Not implemented at this time (postponed until phase 2 of the development) 901 * Returnd DDI_SUCCESS. 902 * 903 * When the last HBA instance is detached, the event daemon is terminated. 904 * 905 * NOTE: cport support only, no port multiplier support. 906 */ 907 int 908 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 909 { 910 dev_info_t *tdip; 911 sata_hba_inst_t *sata_hba_inst; 912 scsi_hba_tran_t *scsi_hba_tran; 913 sata_cport_info_t *cportinfo; 914 sata_drive_info_t *sdinfo; 915 int ncport; 916 917 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 918 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 919 920 switch (cmd) { 921 case DDI_DETACH: 922 923 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 924 return (DDI_FAILURE); 925 926 sata_hba_inst = scsi_hba_tran->tran_hba_private; 927 if (sata_hba_inst == NULL) 928 return (DDI_FAILURE); 929 930 if (scsi_hba_detach(dip) == DDI_FAILURE) { 931 sata_hba_inst->satahba_attached = 1; 932 return (DDI_FAILURE); 933 } 934 935 /* 936 * Free all target nodes - at this point 937 * devices should be at least offlined 938 * otherwise scsi_hba_detach() should not be called. 939 */ 940 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 941 ncport++) { 942 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 943 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 944 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 945 if (sdinfo != NULL) { 946 tdip = sata_get_target_dip(dip, 947 ncport); 948 if (tdip != NULL) { 949 if (ndi_devi_offline(tdip, 950 NDI_DEVI_REMOVE) != 951 NDI_SUCCESS) { 952 SATA_LOG_D(( 953 sata_hba_inst, 954 CE_WARN, 955 "sata_hba_detach: " 956 "Target node not " 957 "removed !")); 958 return (DDI_FAILURE); 959 } 960 } 961 } 962 } 963 } 964 /* 965 * Disable sata event daemon processing for this HBA 966 */ 967 sata_hba_inst->satahba_attached = 0; 968 969 /* 970 * Remove event daemon thread, if it is last HBA instance. 971 */ 972 973 mutex_enter(&sata_mutex); 974 if (sata_hba_list->satahba_next == NULL) { 975 mutex_exit(&sata_mutex); 976 sata_event_thread_control(0); 977 mutex_enter(&sata_mutex); 978 } 979 mutex_exit(&sata_mutex); 980 981 /* Remove this HBA instance from the HBA list */ 982 sata_remove_hba_instance(dip); 983 984 /* 985 * At this point there should be no target nodes attached. 986 * Detach and destroy device and port info structures. 987 */ 988 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 989 ncport++) { 990 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 991 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 992 sdinfo = 993 cportinfo->cport_devp.cport_sata_drive; 994 if (sdinfo != NULL) { 995 /* Release device structure */ 996 kmem_free(sdinfo, 997 sizeof (sata_drive_info_t)); 998 } 999 /* Release cport info */ 1000 mutex_destroy(&cportinfo->cport_mutex); 1001 kmem_free(cportinfo, 1002 sizeof (sata_cport_info_t)); 1003 } 1004 } 1005 1006 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1007 1008 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1009 1010 taskq_destroy(sata_hba_inst->satahba_taskq); 1011 1012 mutex_destroy(&sata_hba_inst->satahba_mutex); 1013 kmem_free((void *)sata_hba_inst, 1014 sizeof (struct sata_hba_inst)); 1015 1016 return (DDI_SUCCESS); 1017 1018 case DDI_SUSPEND: 1019 /* 1020 * Postponed until phase 2 1021 */ 1022 return (DDI_FAILURE); 1023 1024 default: 1025 return (DDI_FAILURE); 1026 } 1027 } 1028 1029 1030 /* 1031 * Called by an HBA drive from _fini() routine. 1032 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1033 */ 1034 void 1035 sata_hba_fini(struct modlinkage *modlp) 1036 { 1037 SATADBG1(SATA_DBG_HBA_IF, NULL, 1038 "sata_hba_fini: name %s\n", 1039 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1040 1041 scsi_hba_fini(modlp); 1042 } 1043 1044 1045 /* 1046 * Default open and close routine for sata_hba framework. 1047 * 1048 */ 1049 /* 1050 * Open devctl node. 1051 * 1052 * Returns: 1053 * 0 if node was open successfully, error code otherwise. 1054 * 1055 * 1056 */ 1057 1058 static int 1059 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1060 { 1061 #ifndef __lock_lint 1062 _NOTE(ARGUNUSED(credp)) 1063 #endif 1064 int rv = 0; 1065 dev_info_t *dip; 1066 scsi_hba_tran_t *scsi_hba_tran; 1067 sata_hba_inst_t *sata_hba_inst; 1068 1069 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1070 1071 if (otyp != OTYP_CHR) 1072 return (EINVAL); 1073 1074 dip = sata_devt_to_devinfo(*devp); 1075 if (dip == NULL) 1076 return (ENXIO); 1077 1078 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1079 return (ENXIO); 1080 1081 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1082 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1083 return (ENXIO); 1084 1085 mutex_enter(&sata_mutex); 1086 if (flags & FEXCL) { 1087 if (sata_hba_inst->satahba_open_flag != 0) { 1088 rv = EBUSY; 1089 } else { 1090 sata_hba_inst->satahba_open_flag = 1091 SATA_DEVCTL_EXOPENED; 1092 } 1093 } else { 1094 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1095 rv = EBUSY; 1096 } else { 1097 sata_hba_inst->satahba_open_flag = 1098 SATA_DEVCTL_SOPENED; 1099 } 1100 } 1101 mutex_exit(&sata_mutex); 1102 1103 return (rv); 1104 } 1105 1106 1107 /* 1108 * Close devctl node. 1109 * Returns: 1110 * 0 if node was closed successfully, error code otherwise. 1111 * 1112 */ 1113 1114 static int 1115 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1116 { 1117 #ifndef __lock_lint 1118 _NOTE(ARGUNUSED(credp)) 1119 _NOTE(ARGUNUSED(flag)) 1120 #endif 1121 dev_info_t *dip; 1122 scsi_hba_tran_t *scsi_hba_tran; 1123 sata_hba_inst_t *sata_hba_inst; 1124 1125 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1126 1127 if (otyp != OTYP_CHR) 1128 return (EINVAL); 1129 1130 dip = sata_devt_to_devinfo(dev); 1131 if (dip == NULL) 1132 return (ENXIO); 1133 1134 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1135 return (ENXIO); 1136 1137 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1138 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1139 return (ENXIO); 1140 1141 mutex_enter(&sata_mutex); 1142 sata_hba_inst->satahba_open_flag = 0; 1143 mutex_exit(&sata_mutex); 1144 return (0); 1145 } 1146 1147 1148 1149 /* 1150 * Standard IOCTL commands for SATA hotplugging. 1151 * Implemented DEVCTL_AP commands: 1152 * DEVCTL_AP_CONNECT 1153 * DEVCTL_AP_DISCONNECT 1154 * DEVCTL_AP_CONFIGURE 1155 * DEVCTL_UNCONFIGURE 1156 * DEVCTL_AP_CONTROL 1157 * 1158 * Commands passed to default ndi ioctl handler: 1159 * DEVCTL_DEVICE_GETSTATE 1160 * DEVCTL_DEVICE_ONLINE 1161 * DEVCTL_DEVICE_OFFLINE 1162 * DEVCTL_DEVICE_REMOVE 1163 * DEVCTL_DEVICE_INSERT 1164 * DEVCTL_BUS_GETSTATE 1165 * 1166 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1167 * if not. 1168 * 1169 * Returns: 1170 * 0 if successful, 1171 * error code if operation failed. 1172 * 1173 * NOTE: Port Multiplier is not supported. 1174 * 1175 */ 1176 1177 static int 1178 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1179 int *rvalp) 1180 { 1181 #ifndef __lock_lint 1182 _NOTE(ARGUNUSED(credp)) 1183 _NOTE(ARGUNUSED(rvalp)) 1184 #endif 1185 int rv = 0; 1186 int32_t comp_port = -1; 1187 dev_info_t *dip; 1188 devctl_ap_state_t ap_state; 1189 struct devctl_iocdata *dcp = NULL; 1190 scsi_hba_tran_t *scsi_hba_tran; 1191 sata_hba_inst_t *sata_hba_inst; 1192 sata_device_t sata_device; 1193 sata_cport_info_t *cportinfo; 1194 int cport, pmport, qual; 1195 int rval = SATA_SUCCESS; 1196 1197 dip = sata_devt_to_devinfo(dev); 1198 if (dip == NULL) 1199 return (ENXIO); 1200 1201 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1202 return (ENXIO); 1203 1204 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1205 if (sata_hba_inst == NULL) 1206 return (ENXIO); 1207 1208 if (sata_hba_inst->satahba_tran == NULL) 1209 return (ENXIO); 1210 1211 switch (cmd) { 1212 1213 case DEVCTL_DEVICE_GETSTATE: 1214 case DEVCTL_DEVICE_ONLINE: 1215 case DEVCTL_DEVICE_OFFLINE: 1216 case DEVCTL_DEVICE_REMOVE: 1217 case DEVCTL_BUS_GETSTATE: 1218 /* 1219 * There may be more cases that we want to pass to default 1220 * handler rather than fail them. 1221 */ 1222 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1223 } 1224 1225 /* read devctl ioctl data */ 1226 if (cmd != DEVCTL_AP_CONTROL) { 1227 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1228 return (EFAULT); 1229 1230 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1231 -1) { 1232 if (dcp) 1233 ndi_dc_freehdl(dcp); 1234 return (EINVAL); 1235 } 1236 1237 cport = SCSI_TO_SATA_CPORT(comp_port); 1238 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1239 /* Only cport is considered now, i.e. SATA_ADDR_CPORT */ 1240 qual = SATA_ADDR_CPORT; 1241 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1242 qual) != 0) { 1243 ndi_dc_freehdl(dcp); 1244 return (EINVAL); 1245 } 1246 1247 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1248 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1249 cport_mutex); 1250 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1251 /* 1252 * Cannot process ioctl request now. Come back later. 1253 */ 1254 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1255 cport_mutex); 1256 ndi_dc_freehdl(dcp); 1257 return (EBUSY); 1258 } 1259 /* Block event processing for this port */ 1260 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1261 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1262 1263 sata_device.satadev_addr.cport = cport; 1264 sata_device.satadev_addr.pmport = pmport; 1265 sata_device.satadev_addr.qual = qual; 1266 sata_device.satadev_rev = SATA_DEVICE_REV; 1267 } 1268 1269 switch (cmd) { 1270 1271 case DEVCTL_AP_DISCONNECT: 1272 1273 /* 1274 * Normally, cfgadm sata plugin will try to offline 1275 * (unconfigure) device before this request. Nevertheless, 1276 * if a device is still configured, we need to 1277 * attempt to offline and unconfigure device first, and we will 1278 * deactivate the port regardless of the unconfigure 1279 * operation results. 1280 * 1281 */ 1282 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1283 1284 break; 1285 1286 case DEVCTL_AP_UNCONFIGURE: 1287 1288 /* 1289 * The unconfigure operation uses generic nexus operation to 1290 * offline a device. It leaves a target device node attached. 1291 * and obviously sata_drive_info attached as well, because 1292 * from the hardware point of view nothing has changed. 1293 */ 1294 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1295 break; 1296 1297 case DEVCTL_AP_CONNECT: 1298 { 1299 /* 1300 * The sata cfgadm pluging will invoke this operation only if 1301 * port was found in the disconnect state (failed state 1302 * is also treated as the disconnected state). 1303 * If port activation is successful and a device is found 1304 * attached to the port, the initialization sequence is 1305 * executed to probe the port and attach 1306 * a device structure to a port structure. The device is not 1307 * set in configured state (system-wise) by this operation. 1308 */ 1309 1310 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1311 1312 break; 1313 } 1314 1315 case DEVCTL_AP_CONFIGURE: 1316 { 1317 /* 1318 * A port may be in an active or shutdown state. 1319 * If port is in a failed state, operation is aborted. 1320 * If a port is in a shutdown state, sata_tran_port_activate() 1321 * is invoked prior to any other operation. 1322 * 1323 * Onlining the device involves creating a new target node. 1324 * If there is an old target node present (belonging to 1325 * previously removed device), the operation is aborted - the 1326 * old node has to be released and removed before configure 1327 * operation is attempted. 1328 */ 1329 1330 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1331 1332 break; 1333 } 1334 1335 case DEVCTL_AP_GETSTATE: 1336 1337 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1338 1339 ap_state.ap_last_change = (time_t)-1; 1340 ap_state.ap_error_code = 0; 1341 ap_state.ap_in_transition = 0; 1342 1343 /* Copy the return AP-state information to the user space */ 1344 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1345 rv = EFAULT; 1346 } 1347 break; 1348 1349 case DEVCTL_AP_CONTROL: 1350 { 1351 /* 1352 * Generic devctl for hardware specific functionality 1353 */ 1354 sata_ioctl_data_t ioc; 1355 1356 ASSERT(dcp == NULL); 1357 1358 /* Copy in user ioctl data first */ 1359 #ifdef _MULTI_DATAMODEL 1360 if (ddi_model_convert_from(mode & FMODELS) == 1361 DDI_MODEL_ILP32) { 1362 1363 sata_ioctl_data_32_t ioc32; 1364 1365 if (ddi_copyin((void *)arg, (void *)&ioc32, 1366 sizeof (ioc32), mode) != 0) { 1367 rv = EFAULT; 1368 break; 1369 } 1370 ioc.cmd = (uint_t)ioc32.cmd; 1371 ioc.port = (uint_t)ioc32.port; 1372 ioc.get_size = (uint_t)ioc32.get_size; 1373 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1374 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1375 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1376 } else 1377 #endif /* _MULTI_DATAMODEL */ 1378 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1379 mode) != 0) { 1380 return (EFAULT); 1381 } 1382 1383 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1384 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1385 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1386 1387 /* 1388 * To avoid BE/LE and 32/64 issues, a get_size always returns 1389 * a 32-bit number. 1390 */ 1391 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1392 return (EINVAL); 1393 } 1394 /* validate address */ 1395 cport = SCSI_TO_SATA_CPORT(ioc.port); 1396 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1397 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1398 1399 /* Override address qualifier - handle cport only for now */ 1400 qual = SATA_ADDR_CPORT; 1401 1402 if (sata_validate_sata_address(sata_hba_inst, cport, 1403 pmport, qual) != 0) 1404 return (EINVAL); 1405 1406 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1407 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1408 cport_mutex); 1409 /* Is the port locked by event processing daemon ? */ 1410 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1411 /* 1412 * Cannot process ioctl request now. Come back later 1413 */ 1414 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1415 cport_mutex); 1416 return (EBUSY); 1417 } 1418 /* Block event processing for this port */ 1419 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1420 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1421 1422 1423 sata_device.satadev_addr.cport = cport; 1424 sata_device.satadev_addr.pmport = pmport; 1425 sata_device.satadev_addr.qual = qual; 1426 sata_device.satadev_rev = SATA_DEVICE_REV; 1427 1428 switch (ioc.cmd) { 1429 1430 case SATA_CFGA_RESET_PORT: 1431 /* 1432 * There is no protection for configured device. 1433 */ 1434 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1435 break; 1436 1437 case SATA_CFGA_RESET_DEVICE: 1438 /* 1439 * There is no protection for configured device. 1440 */ 1441 rv = sata_ioctl_reset_device(sata_hba_inst, 1442 &sata_device); 1443 break; 1444 1445 case SATA_CFGA_RESET_ALL: 1446 /* 1447 * There is no protection for configured devices. 1448 */ 1449 rv = sata_ioctl_reset_all(sata_hba_inst); 1450 /* 1451 * We return here, because common return is for 1452 * a single port operation - we have already unlocked 1453 * all ports and no dc handle was allocated. 1454 */ 1455 return (rv); 1456 1457 case SATA_CFGA_PORT_DEACTIVATE: 1458 /* 1459 * Arbitrarily unconfigure attached device, if any. 1460 * Even if the unconfigure fails, proceed with the 1461 * port deactivation. 1462 */ 1463 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1464 1465 break; 1466 1467 case SATA_CFGA_PORT_ACTIVATE: 1468 1469 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1470 break; 1471 1472 case SATA_CFGA_PORT_SELF_TEST: 1473 1474 rv = sata_ioctl_port_self_test(sata_hba_inst, 1475 &sata_device); 1476 break; 1477 1478 case SATA_CFGA_GET_DEVICE_PATH: 1479 if (qual == SATA_ADDR_CPORT) 1480 sata_device.satadev_addr.qual = 1481 SATA_ADDR_DCPORT; 1482 else 1483 sata_device.satadev_addr.qual = 1484 SATA_ADDR_DPMPORT; 1485 rv = sata_ioctl_get_device_path(sata_hba_inst, 1486 &sata_device, &ioc, mode); 1487 break; 1488 1489 case SATA_CFGA_GET_AP_TYPE: 1490 1491 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1492 &sata_device, &ioc, mode); 1493 break; 1494 1495 case SATA_CFGA_GET_MODEL_INFO: 1496 1497 rv = sata_ioctl_get_model_info(sata_hba_inst, 1498 &sata_device, &ioc, mode); 1499 break; 1500 1501 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1502 1503 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1504 &sata_device, &ioc, mode); 1505 break; 1506 1507 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1508 1509 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1510 &sata_device, &ioc, mode); 1511 break; 1512 1513 default: 1514 rv = EINVAL; 1515 break; 1516 1517 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1518 1519 break; 1520 } 1521 1522 default: 1523 { 1524 /* 1525 * If we got here, we got an IOCTL that SATA HBA Framework 1526 * does not recognize. Pass ioctl to HBA driver, in case 1527 * it could process it. 1528 */ 1529 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1530 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1531 1532 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1533 "IOCTL 0x%2x not supported in SATA framework, " 1534 "passthrough to HBA", cmd); 1535 1536 if (sata_tran->sata_tran_ioctl == NULL) { 1537 rv = EINVAL; 1538 break; 1539 } 1540 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1541 if (rval != 0) { 1542 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1543 "IOCTL 0x%2x failed in HBA", cmd); 1544 rv = rval; 1545 } 1546 break; 1547 } 1548 1549 } /* End of main IOCTL switch */ 1550 1551 if (dcp) { 1552 ndi_dc_freehdl(dcp); 1553 } 1554 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1555 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1556 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1557 1558 return (rv); 1559 } 1560 1561 1562 /* 1563 * Create error retrieval sata packet 1564 * 1565 * A sata packet is allocated and set-up to contain specified error retrieval 1566 * command and appropriate dma-able data buffer. 1567 * No association with any scsi packet is made and no callback routine is 1568 * specified. 1569 * 1570 * Returns a pointer to sata packet upon successfull packet creation. 1571 * Returns NULL, if packet cannot be created. 1572 */ 1573 sata_pkt_t * 1574 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1575 int pkt_type) 1576 { 1577 sata_hba_inst_t *sata_hba_inst; 1578 sata_pkt_txlate_t *spx; 1579 sata_pkt_t *spkt; 1580 sata_drive_info_t *sdinfo; 1581 1582 mutex_enter(&sata_mutex); 1583 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1584 sata_hba_inst = sata_hba_inst->satahba_next) { 1585 if (SATA_DIP(sata_hba_inst) == dip) 1586 break; 1587 } 1588 mutex_exit(&sata_mutex); 1589 ASSERT(sata_hba_inst != NULL); 1590 1591 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1592 if (sdinfo == NULL) { 1593 sata_log(sata_hba_inst, CE_WARN, 1594 "sata: error recovery request for non-attached device at " 1595 "cport %d", sata_device->satadev_addr.cport); 1596 return (NULL); 1597 } 1598 1599 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1600 spx->txlt_sata_hba_inst = sata_hba_inst; 1601 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1602 spkt = sata_pkt_alloc(spx, NULL); 1603 if (spkt == NULL) { 1604 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1605 return (NULL); 1606 } 1607 /* address is needed now */ 1608 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1609 1610 switch (pkt_type) { 1611 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1612 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1613 return (spkt); 1614 break; 1615 1616 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1617 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1618 return (spkt); 1619 break; 1620 1621 default: 1622 break; 1623 } 1624 1625 sata_pkt_free(spx); 1626 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1627 return (NULL); 1628 1629 } 1630 1631 1632 /* 1633 * Free error retrieval sata packet 1634 * 1635 * Free sata packet and any associated resources allocated previously by 1636 * sata_get_error_retrieval_pkt(). 1637 * 1638 * Void return. 1639 */ 1640 void 1641 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1642 { 1643 sata_pkt_txlate_t *spx = 1644 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1645 1646 ASSERT(sata_pkt != NULL); 1647 1648 sata_free_local_buffer(spx); 1649 sata_pkt_free(spx); 1650 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1651 1652 } 1653 1654 /* 1655 * sata_name_child is for composing the name of the node 1656 * the format of the name is "target,0". 1657 */ 1658 static int 1659 sata_name_child(dev_info_t *dip, char *name, int namelen) 1660 { 1661 int target; 1662 1663 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1664 DDI_PROP_DONTPASS, "target", -1); 1665 if (target == -1) 1666 return (DDI_FAILURE); 1667 (void) snprintf(name, namelen, "%x,0", target); 1668 return (DDI_SUCCESS); 1669 } 1670 1671 1672 1673 /* ****************** SCSA required entry points *********************** */ 1674 1675 /* 1676 * Implementation of scsi tran_tgt_init. 1677 * sata_scsi_tgt_init() initializes scsi_device structure 1678 * 1679 * If successful, DDI_SUCCESS is returned. 1680 * DDI_FAILURE is returned if addressed device does not exist 1681 */ 1682 1683 static int 1684 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1685 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1686 { 1687 #ifndef __lock_lint 1688 _NOTE(ARGUNUSED(hba_dip)) 1689 _NOTE(ARGUNUSED(tgt_dip)) 1690 #endif 1691 sata_device_t sata_device; 1692 sata_drive_info_t *sdinfo; 1693 struct sata_id *sid; 1694 sata_hba_inst_t *sata_hba_inst; 1695 char model[SATA_ID_MODEL_LEN + 1]; 1696 char fw[SATA_ID_FW_LEN + 1]; 1697 char *vid, *pid; 1698 int i; 1699 1700 /* 1701 * Fail tran_tgt_init for .conf stub node 1702 */ 1703 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1704 (void) ndi_merge_node(tgt_dip, sata_name_child); 1705 ddi_set_name_addr(tgt_dip, NULL); 1706 return (DDI_FAILURE); 1707 } 1708 1709 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1710 1711 /* Validate scsi device address */ 1712 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1713 &sata_device) != 0) 1714 return (DDI_FAILURE); 1715 1716 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1717 sata_device.satadev_addr.cport))); 1718 1719 /* sata_device now contains a valid sata address */ 1720 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1721 if (sdinfo == NULL) { 1722 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1723 sata_device.satadev_addr.cport))); 1724 return (DDI_FAILURE); 1725 } 1726 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1727 sata_device.satadev_addr.cport))); 1728 1729 /* 1730 * Check if we need to create a legacy devid (i.e cmdk style) for 1731 * the target disks. 1732 * 1733 * HBA devinfo node will have the property "use-cmdk-devid-format" 1734 * if we need to create cmdk-style devid for all the disk devices 1735 * attached to this controller. This property may have been set 1736 * from HBA driver's .conf file or by the HBA driver in its 1737 * attach(9F) function. 1738 */ 1739 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1740 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1741 "use-cmdk-devid-format", 0) == 1)) { 1742 /* register a legacy devid for this target node */ 1743 sata_target_devid_register(tgt_dip, sdinfo); 1744 } 1745 1746 1747 /* 1748 * 'Identify Device Data' does not always fit in standard SCSI 1749 * INQUIRY data, so establish INQUIRY_* properties with full-form 1750 * of information. 1751 */ 1752 sid = &sdinfo->satadrv_id; 1753 #ifdef _LITTLE_ENDIAN 1754 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 1755 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 1756 #else /* _LITTLE_ENDIAN */ 1757 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 1758 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 1759 #endif /* _LITTLE_ENDIAN */ 1760 model[SATA_ID_MODEL_LEN] = 0; 1761 fw[SATA_ID_FW_LEN] = 0; 1762 1763 /* split model into into vid/pid */ 1764 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 1765 if ((*pid == ' ') || (*pid == '\t')) 1766 break; 1767 if (i < SATA_ID_MODEL_LEN) { 1768 vid = model; 1769 *pid++ = 0; /* terminate vid, establish pid */ 1770 } else { 1771 vid = NULL; /* vid will stay "ATA " */ 1772 pid = model; /* model is all pid */ 1773 } 1774 1775 if (vid) 1776 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 1777 vid, strlen(vid)); 1778 if (pid) 1779 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 1780 pid, strlen(pid)); 1781 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 1782 fw, strlen(fw)); 1783 1784 return (DDI_SUCCESS); 1785 } 1786 1787 /* 1788 * Implementation of scsi tran_tgt_probe. 1789 * Probe target, by calling default scsi routine scsi_hba_probe() 1790 */ 1791 static int 1792 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 1793 { 1794 sata_hba_inst_t *sata_hba_inst = 1795 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 1796 int rval; 1797 1798 rval = scsi_hba_probe(sd, callback); 1799 1800 if (rval == SCSIPROBE_EXISTS) { 1801 /* 1802 * Set property "pm-capable" on the target device node, so that 1803 * the target driver will not try to fetch scsi cycle counters 1804 * before enabling device power-management. 1805 */ 1806 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 1807 "pm-capable", 1)) != DDI_PROP_SUCCESS) { 1808 sata_log(sata_hba_inst, CE_WARN, 1809 "SATA device at port %d: " 1810 "will not be power-managed ", 1811 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 1812 SATA_LOG_D((sata_hba_inst, CE_WARN, 1813 "failure updating pm-capable property")); 1814 } 1815 } 1816 return (rval); 1817 } 1818 1819 /* 1820 * Implementation of scsi tran_tgt_free. 1821 * Release all resources allocated for scsi_device 1822 */ 1823 static void 1824 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1825 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1826 { 1827 #ifndef __lock_lint 1828 _NOTE(ARGUNUSED(hba_dip)) 1829 #endif 1830 sata_device_t sata_device; 1831 sata_drive_info_t *sdinfo; 1832 sata_hba_inst_t *sata_hba_inst; 1833 ddi_devid_t devid; 1834 1835 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1836 1837 /* Validate scsi device address */ 1838 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1839 &sata_device) != 0) 1840 return; 1841 1842 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1843 sata_device.satadev_addr.cport))); 1844 1845 /* sata_device now should contain a valid sata address */ 1846 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1847 if (sdinfo == NULL) { 1848 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1849 sata_device.satadev_addr.cport))); 1850 return; 1851 } 1852 /* 1853 * We did not allocate any resources in sata_scsi_tgt_init() 1854 * other than few properties. 1855 * Free them. 1856 */ 1857 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1858 sata_device.satadev_addr.cport))); 1859 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 1860 1861 /* 1862 * If devid was previously created but not freed up from 1863 * sd(7D) driver (i.e during detach(9F)) then do it here. 1864 */ 1865 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1866 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1867 "use-cmdk-devid-format", 0) == 1) && 1868 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 1869 ddi_devid_unregister(tgt_dip); 1870 ddi_devid_free(devid); 1871 } 1872 } 1873 1874 /* 1875 * Implementation of scsi tran_init_pkt 1876 * Upon successful return, scsi pkt buffer has DMA resources allocated. 1877 * 1878 * It seems that we should always allocate pkt, even if the address is 1879 * for non-existing device - just use some default for dma_attr. 1880 * The reason is that there is no way to communicate this to a caller here. 1881 * Subsequent call to sata_scsi_start may fail appropriately. 1882 * Simply returning NULL does not seem to discourage a target driver... 1883 * 1884 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 1885 */ 1886 static struct scsi_pkt * 1887 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 1888 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 1889 int (*callback)(caddr_t), caddr_t arg) 1890 { 1891 sata_hba_inst_t *sata_hba_inst = 1892 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 1893 dev_info_t *dip = SATA_DIP(sata_hba_inst); 1894 sata_device_t sata_device; 1895 sata_drive_info_t *sdinfo; 1896 sata_pkt_txlate_t *spx; 1897 ddi_dma_attr_t cur_dma_attr; 1898 int rval; 1899 boolean_t new_pkt = TRUE; 1900 1901 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 1902 1903 /* 1904 * We need to translate the address, even if it could be 1905 * a bogus one, for a non-existing device 1906 */ 1907 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 1908 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 1909 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 1910 sata_device.satadev_rev = SATA_DEVICE_REV; 1911 1912 if (pkt == NULL) { 1913 /* 1914 * Have to allocate a brand new scsi packet. 1915 * We need to operate with auto request sense enabled. 1916 */ 1917 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 1918 MAX(statuslen, sizeof (struct scsi_arq_status)), 1919 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 1920 1921 if (pkt == NULL) 1922 return (NULL); 1923 1924 /* Fill scsi packet structure */ 1925 pkt->pkt_comp = (void (*)())NULL; 1926 pkt->pkt_time = 0; 1927 pkt->pkt_resid = 0; 1928 pkt->pkt_statistics = 0; 1929 pkt->pkt_reason = 0; 1930 1931 /* 1932 * pkt_hba_private will point to sata pkt txlate structure 1933 */ 1934 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1935 bzero(spx, sizeof (sata_pkt_txlate_t)); 1936 1937 spx->txlt_scsi_pkt = pkt; 1938 spx->txlt_sata_hba_inst = sata_hba_inst; 1939 1940 /* Allocate sata_pkt */ 1941 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 1942 if (spx->txlt_sata_pkt == NULL) { 1943 /* Could not allocate sata pkt */ 1944 scsi_hba_pkt_free(ap, pkt); 1945 return (NULL); 1946 } 1947 /* Set sata address */ 1948 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 1949 sata_device.satadev_addr; 1950 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 1951 sata_device.satadev_rev; 1952 1953 if ((bp == NULL) || (bp->b_bcount == 0)) 1954 return (pkt); 1955 1956 spx->txlt_total_residue = bp->b_bcount; 1957 } else { 1958 new_pkt = FALSE; 1959 /* 1960 * Packet was preallocated/initialized by previous call 1961 */ 1962 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1963 1964 if ((bp == NULL) || (bp->b_bcount == 0)) { 1965 return (pkt); 1966 } 1967 1968 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 1969 } 1970 1971 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 1972 1973 /* 1974 * We use an adjusted version of the dma_attr, to account 1975 * for device addressing limitations. 1976 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 1977 * happen when a device is not yet configured. 1978 */ 1979 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1980 sata_device.satadev_addr.cport))); 1981 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 1982 &spx->txlt_sata_pkt->satapkt_device); 1983 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 1984 sata_adjust_dma_attr(sdinfo, 1985 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 1986 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1987 sata_device.satadev_addr.cport))); 1988 /* 1989 * Allocate necessary DMA resources for the packet's data buffer 1990 * NOTE: 1991 * In case of read/write commands, DMA resource allocation here is 1992 * based on the premise that the transfer length specified in 1993 * the read/write scsi cdb will match exactly DMA resources - 1994 * returning correct packet residue is crucial. 1995 */ 1996 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 1997 &cur_dma_attr)) != DDI_SUCCESS) { 1998 /* 1999 * If a DMA allocation request fails with 2000 * DDI_DMA_NOMAPPING, indicate the error by calling 2001 * bioerror(9F) with bp and an error code of EFAULT. 2002 * If a DMA allocation request fails with 2003 * DDI_DMA_TOOBIG, indicate the error by calling 2004 * bioerror(9F) with bp and an error code of EINVAL. 2005 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2006 * Request may be repeated later - there is no real error. 2007 */ 2008 switch (rval) { 2009 case DDI_DMA_NORESOURCES: 2010 bioerror(bp, 0); 2011 break; 2012 case DDI_DMA_NOMAPPING: 2013 case DDI_DMA_BADATTR: 2014 bioerror(bp, EFAULT); 2015 break; 2016 case DDI_DMA_TOOBIG: 2017 default: 2018 bioerror(bp, EINVAL); 2019 break; 2020 } 2021 if (new_pkt == TRUE) { 2022 /* 2023 * Since this is a new packet, we can clean-up 2024 * everything 2025 */ 2026 sata_scsi_destroy_pkt(ap, pkt); 2027 } else { 2028 /* 2029 * This is a re-used packet. It will be target driver's 2030 * responsibility to eventually destroy it (which 2031 * will free allocated resources). 2032 * Here, we just "complete" the request, leaving 2033 * allocated resources intact, so the request may 2034 * be retried. 2035 */ 2036 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2037 sata_pkt_free(spx); 2038 } 2039 return (NULL); 2040 } 2041 /* Set number of bytes that are not yet accounted for */ 2042 pkt->pkt_resid = spx->txlt_total_residue; 2043 ASSERT(pkt->pkt_resid >= 0); 2044 2045 return (pkt); 2046 } 2047 2048 /* 2049 * Implementation of scsi tran_start. 2050 * Translate scsi cmd into sata operation and return status. 2051 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2052 * are supported. 2053 * For SATA hard disks, supported scsi commands: 2054 * SCMD_INQUIRY 2055 * SCMD_TEST_UNIT_READY 2056 * SCMD_START_STOP 2057 * SCMD_READ_CAPACITY 2058 * SCMD_REQUEST_SENSE 2059 * SCMD_LOG_SENSE_G1 2060 * SCMD_LOG_SELECT_G1 2061 * SCMD_MODE_SENSE (specific pages) 2062 * SCMD_MODE_SENSE_G1 (specific pages) 2063 * SCMD_MODE_SELECT (specific pages) 2064 * SCMD_MODE_SELECT_G1 (specific pages) 2065 * SCMD_SYNCHRONIZE_CACHE 2066 * SCMD_SYNCHRONIZE_CACHE_G1 2067 * SCMD_READ 2068 * SCMD_READ_G1 2069 * SCMD_READ_G4 2070 * SCMD_READ_G5 2071 * SCMD_WRITE 2072 * SCMD_WRITE_BUFFER 2073 * SCMD_WRITE_G1 2074 * SCMD_WRITE_G4 2075 * SCMD_WRITE_G5 2076 * SCMD_SEEK (noop) 2077 * SCMD_SDIAG 2078 * 2079 * All other commands are rejected as unsupported. 2080 * 2081 * Returns: 2082 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2083 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2084 * a callback could be scheduled. 2085 * TRAN_BADPKT if cmd was directed to invalid address. 2086 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2087 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2088 * was removed and there was no callback specified in scsi pkt. 2089 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2090 * framework was busy performing some other operation(s). 2091 * 2092 */ 2093 static int 2094 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2095 { 2096 sata_hba_inst_t *sata_hba_inst = 2097 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2098 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2099 sata_drive_info_t *sdinfo; 2100 struct buf *bp; 2101 int cport; 2102 int rval; 2103 2104 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2105 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2106 2107 ASSERT(spx != NULL && 2108 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2109 2110 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2111 2112 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2113 sdinfo = sata_get_device_info(sata_hba_inst, 2114 &spx->txlt_sata_pkt->satapkt_device); 2115 if (sdinfo == NULL || 2116 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean == 2117 B_FALSE || 2118 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2119 2120 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2121 pkt->pkt_reason = CMD_DEV_GONE; 2122 /* 2123 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2124 * only in callback function (for normal requests) and 2125 * in the dump code path. 2126 * So, if the callback is available, we need to do 2127 * the callback rather than returning TRAN_FATAL_ERROR here. 2128 */ 2129 if (pkt->pkt_comp != NULL) { 2130 /* scsi callback required */ 2131 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2132 (task_func_t *)pkt->pkt_comp, 2133 (void *)pkt, TQ_SLEEP) == NULL) 2134 /* Scheduling the callback failed */ 2135 return (TRAN_BUSY); 2136 return (TRAN_ACCEPT); 2137 } 2138 /* No callback available */ 2139 return (TRAN_FATAL_ERROR); 2140 } 2141 2142 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2143 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2144 rval = sata_txlt_atapi(spx); 2145 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2146 "sata_scsi_start atapi: rval %d\n", rval); 2147 return (rval); 2148 } 2149 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2150 2151 /* ATA Disk commands processing starts here */ 2152 2153 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2154 2155 switch (pkt->pkt_cdbp[0]) { 2156 2157 case SCMD_INQUIRY: 2158 /* Mapped to identify device */ 2159 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2160 bp_mapin(bp); 2161 rval = sata_txlt_inquiry(spx); 2162 break; 2163 2164 case SCMD_TEST_UNIT_READY: 2165 /* 2166 * SAT "SATA to ATA Translation" doc specifies translation 2167 * to ATA CHECK POWER MODE. 2168 */ 2169 rval = sata_txlt_test_unit_ready(spx); 2170 break; 2171 2172 case SCMD_START_STOP: 2173 /* Mapping depends on the command */ 2174 rval = sata_txlt_start_stop_unit(spx); 2175 break; 2176 2177 case SCMD_READ_CAPACITY: 2178 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2179 bp_mapin(bp); 2180 rval = sata_txlt_read_capacity(spx); 2181 break; 2182 2183 case SCMD_REQUEST_SENSE: 2184 /* 2185 * Always No Sense, since we force ARQ 2186 */ 2187 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2188 bp_mapin(bp); 2189 rval = sata_txlt_request_sense(spx); 2190 break; 2191 2192 case SCMD_LOG_SENSE_G1: 2193 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2194 bp_mapin(bp); 2195 rval = sata_txlt_log_sense(spx); 2196 break; 2197 2198 case SCMD_LOG_SELECT_G1: 2199 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2200 bp_mapin(bp); 2201 rval = sata_txlt_log_select(spx); 2202 break; 2203 2204 case SCMD_MODE_SENSE: 2205 case SCMD_MODE_SENSE_G1: 2206 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2207 bp_mapin(bp); 2208 rval = sata_txlt_mode_sense(spx); 2209 break; 2210 2211 2212 case SCMD_MODE_SELECT: 2213 case SCMD_MODE_SELECT_G1: 2214 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2215 bp_mapin(bp); 2216 rval = sata_txlt_mode_select(spx); 2217 break; 2218 2219 case SCMD_SYNCHRONIZE_CACHE: 2220 case SCMD_SYNCHRONIZE_CACHE_G1: 2221 rval = sata_txlt_synchronize_cache(spx); 2222 break; 2223 2224 case SCMD_READ: 2225 case SCMD_READ_G1: 2226 case SCMD_READ_G4: 2227 case SCMD_READ_G5: 2228 rval = sata_txlt_read(spx); 2229 break; 2230 case SCMD_WRITE_BUFFER: 2231 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2232 bp_mapin(bp); 2233 rval = sata_txlt_write_buffer(spx); 2234 break; 2235 2236 case SCMD_WRITE: 2237 case SCMD_WRITE_G1: 2238 case SCMD_WRITE_G4: 2239 case SCMD_WRITE_G5: 2240 rval = sata_txlt_write(spx); 2241 break; 2242 2243 case SCMD_SEEK: 2244 rval = sata_txlt_nodata_cmd_immediate(spx); 2245 break; 2246 2247 /* Other cases will be filed later */ 2248 /* postponed until phase 2 of the development */ 2249 default: 2250 rval = sata_txlt_invalid_command(spx); 2251 break; 2252 } 2253 2254 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2255 "sata_scsi_start: rval %d\n", rval); 2256 2257 return (rval); 2258 } 2259 2260 /* 2261 * Implementation of scsi tran_abort. 2262 * Abort specific pkt or all packets. 2263 * 2264 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2265 * 2266 * May be called from an interrupt level. 2267 */ 2268 static int 2269 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2270 { 2271 sata_hba_inst_t *sata_hba_inst = 2272 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2273 sata_device_t sata_device; 2274 sata_pkt_t *sata_pkt; 2275 2276 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2277 "sata_scsi_abort: %s at target: 0x%x\n", 2278 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2279 2280 /* Validate address */ 2281 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2282 /* Invalid address */ 2283 return (0); 2284 2285 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2286 sata_device.satadev_addr.cport))); 2287 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2288 /* invalid address */ 2289 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2290 sata_device.satadev_addr.cport))); 2291 return (0); 2292 } 2293 if (scsi_pkt == NULL) { 2294 /* 2295 * Abort all packets. 2296 * Although we do not have specific packet, we still need 2297 * dummy packet structure to pass device address to HBA. 2298 * Allocate one, without sleeping. Fail if pkt cannot be 2299 * allocated. 2300 */ 2301 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2302 if (sata_pkt == NULL) { 2303 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2304 sata_device.satadev_addr.cport))); 2305 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2306 "could not allocate sata_pkt")); 2307 return (0); 2308 } 2309 sata_pkt->satapkt_rev = SATA_PKT_REV; 2310 sata_pkt->satapkt_device = sata_device; 2311 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2312 } else { 2313 if (scsi_pkt->pkt_ha_private == NULL) { 2314 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2315 sata_device.satadev_addr.cport))); 2316 return (0); /* Bad scsi pkt */ 2317 } 2318 /* extract pointer to sata pkt */ 2319 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2320 txlt_sata_pkt; 2321 } 2322 2323 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2324 sata_device.satadev_addr.cport))); 2325 /* Send abort request to HBA */ 2326 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2327 (SATA_DIP(sata_hba_inst), sata_pkt, 2328 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2329 SATA_SUCCESS) { 2330 if (scsi_pkt == NULL) 2331 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2332 /* Success */ 2333 return (1); 2334 } 2335 /* Else, something did not go right */ 2336 if (scsi_pkt == NULL) 2337 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2338 /* Failure */ 2339 return (0); 2340 } 2341 2342 2343 /* 2344 * Implementation of scsi tran_reset. 2345 * RESET_ALL request is translated into port reset. 2346 * RESET_TARGET requests is translated into a device reset, 2347 * RESET_LUN request is accepted only for LUN 0 and translated into 2348 * device reset. 2349 * The target reset should cause all HBA active and queued packets to 2350 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2351 * the return. HBA should report reset event for the device. 2352 * 2353 * Returns 1 upon success, 0 upon failure. 2354 */ 2355 static int 2356 sata_scsi_reset(struct scsi_address *ap, int level) 2357 { 2358 sata_hba_inst_t *sata_hba_inst = 2359 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2360 sata_device_t sata_device; 2361 int val; 2362 2363 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2364 "sata_scsi_reset: level %d target: 0x%x\n", 2365 level, ap->a_target); 2366 2367 /* Validate address */ 2368 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2369 if (val == -1) 2370 /* Invalid address */ 2371 return (0); 2372 2373 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2374 sata_device.satadev_addr.cport))); 2375 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2376 /* invalid address */ 2377 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2378 sata_device.satadev_addr.cport))); 2379 return (0); 2380 } 2381 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2382 sata_device.satadev_addr.cport))); 2383 if (level == RESET_ALL) { 2384 /* port reset - cport only */ 2385 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2386 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2387 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2388 return (1); 2389 else 2390 return (0); 2391 2392 } else if (val == 0 && 2393 (level == RESET_TARGET || level == RESET_LUN)) { 2394 /* reset device (device attached) */ 2395 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2396 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2397 return (1); 2398 else 2399 return (0); 2400 } 2401 return (0); 2402 } 2403 2404 2405 /* 2406 * Implementation of scsi tran_getcap (get transport/device capabilities). 2407 * Supported capabilities for SATA hard disks: 2408 * auto-rqsense (always supported) 2409 * tagged-qing (supported if HBA supports it) 2410 * untagged-qing (could be supported if disk supports it, but because 2411 * caching behavior allowing untagged queuing actually 2412 * results in reduced performance. sd tries to throttle 2413 * back to only 3 outstanding commands, which may 2414 * work for real SCSI disks, but with read ahead 2415 * caching, having more than 1 outstanding command 2416 * results in cache thrashing.) 2417 * sector_size 2418 * dma_max 2419 * interconnect-type (INTERCONNECT_SATA) 2420 * 2421 * Supported capabilities for ATAPI CD/DVD devices: 2422 * auto-rqsense (always supported) 2423 * sector_size 2424 * dma_max 2425 * max-cdb-length 2426 * interconnect-type (INTERCONNECT_SATA) 2427 * 2428 * Supported capabilities for ATAPI TAPE devices: 2429 * auto-rqsense (always supported) 2430 * dma_max 2431 * max-cdb-length 2432 * 2433 * Supported capabilities for SATA ATAPI hard disks: 2434 * auto-rqsense (always supported) 2435 * interconnect-type (INTERCONNECT_SATA) 2436 * max-cdb-length 2437 * 2438 * Request for other capabilities is rejected as unsupported. 2439 * 2440 * Returns supported capability value, or -1 if capability is unsuppported or 2441 * the address is invalid - no device. 2442 */ 2443 2444 static int 2445 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2446 { 2447 2448 sata_hba_inst_t *sata_hba_inst = 2449 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2450 sata_device_t sata_device; 2451 sata_drive_info_t *sdinfo; 2452 ddi_dma_attr_t adj_dma_attr; 2453 int rval; 2454 2455 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2456 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2457 ap->a_target, cap); 2458 2459 /* 2460 * We want to process the capabilities on per port granularity. 2461 * So, we are specifically restricting ourselves to whom != 0 2462 * to exclude the controller wide handling. 2463 */ 2464 if (cap == NULL || whom == 0) 2465 return (-1); 2466 2467 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2468 /* Invalid address */ 2469 return (-1); 2470 } 2471 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2472 sata_device.satadev_addr.cport))); 2473 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2474 NULL) { 2475 /* invalid address */ 2476 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2477 sata_device.satadev_addr.cport))); 2478 return (-1); 2479 } 2480 2481 switch (scsi_hba_lookup_capstr(cap)) { 2482 case SCSI_CAP_ARQ: 2483 rval = 1; /* ARQ supported, turned on */ 2484 break; 2485 2486 case SCSI_CAP_SECTOR_SIZE: 2487 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2488 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2489 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2490 rval = SATA_ATAPI_SECTOR_SIZE; 2491 else rval = -1; 2492 break; 2493 2494 /* 2495 * untagged queuing cause a performance inversion because of 2496 * the way sd operates. Because of this reason we do not 2497 * use it when available. 2498 */ 2499 case SCSI_CAP_UNTAGGED_QING: 2500 if (sdinfo->satadrv_features_enabled & 2501 SATA_DEV_F_E_UNTAGGED_QING) 2502 rval = 1; /* Untagged queuing available */ 2503 else 2504 rval = -1; /* Untagged queuing not available */ 2505 break; 2506 2507 case SCSI_CAP_TAGGED_QING: 2508 if ((sdinfo->satadrv_features_enabled & 2509 SATA_DEV_F_E_TAGGED_QING) && 2510 (sdinfo->satadrv_max_queue_depth > 1)) 2511 rval = 1; /* Tagged queuing available */ 2512 else 2513 rval = -1; /* Tagged queuing not available */ 2514 break; 2515 2516 case SCSI_CAP_DMA_MAX: 2517 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2518 &adj_dma_attr); 2519 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2520 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2521 break; 2522 2523 case SCSI_CAP_INTERCONNECT_TYPE: 2524 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2525 break; 2526 2527 case SCSI_CAP_CDB_LEN: 2528 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2529 rval = sdinfo->satadrv_atapi_cdb_len; 2530 else 2531 rval = -1; 2532 break; 2533 2534 default: 2535 rval = -1; 2536 break; 2537 } 2538 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2539 sata_device.satadev_addr.cport))); 2540 return (rval); 2541 } 2542 2543 /* 2544 * Implementation of scsi tran_setcap 2545 * 2546 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2547 * 2548 */ 2549 static int 2550 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2551 { 2552 sata_hba_inst_t *sata_hba_inst = 2553 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2554 sata_device_t sata_device; 2555 sata_drive_info_t *sdinfo; 2556 int rval; 2557 2558 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2559 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2560 2561 /* 2562 * We want to process the capabilities on per port granularity. 2563 * So, we are specifically restricting ourselves to whom != 0 2564 * to exclude the controller wide handling. 2565 */ 2566 if (cap == NULL || whom == 0) { 2567 return (-1); 2568 } 2569 2570 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2571 /* Invalid address */ 2572 return (-1); 2573 } 2574 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2575 sata_device.satadev_addr.cport))); 2576 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2577 &sata_device)) == NULL) { 2578 /* invalid address */ 2579 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2580 sata_device.satadev_addr.cport))); 2581 return (-1); 2582 } 2583 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2584 sata_device.satadev_addr.cport))); 2585 2586 switch (scsi_hba_lookup_capstr(cap)) { 2587 case SCSI_CAP_ARQ: 2588 case SCSI_CAP_SECTOR_SIZE: 2589 case SCSI_CAP_DMA_MAX: 2590 case SCSI_CAP_INTERCONNECT_TYPE: 2591 rval = 0; 2592 break; 2593 case SCSI_CAP_UNTAGGED_QING: 2594 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2595 rval = 1; 2596 if (value == 1) { 2597 sdinfo->satadrv_features_enabled |= 2598 SATA_DEV_F_E_UNTAGGED_QING; 2599 } else if (value == 0) { 2600 sdinfo->satadrv_features_enabled &= 2601 ~SATA_DEV_F_E_UNTAGGED_QING; 2602 } else { 2603 rval = -1; 2604 } 2605 } else { 2606 rval = 0; 2607 } 2608 break; 2609 case SCSI_CAP_TAGGED_QING: 2610 /* This can TCQ or NCQ */ 2611 if (sata_func_enable & SATA_ENABLE_QUEUING && 2612 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2613 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2614 (sata_func_enable & SATA_ENABLE_NCQ && 2615 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2616 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2617 (sdinfo->satadrv_max_queue_depth > 1)) { 2618 rval = 1; 2619 if (value == 1) { 2620 sdinfo->satadrv_features_enabled |= 2621 SATA_DEV_F_E_TAGGED_QING; 2622 } else if (value == 0) { 2623 sdinfo->satadrv_features_enabled &= 2624 ~SATA_DEV_F_E_TAGGED_QING; 2625 } else { 2626 rval = -1; 2627 } 2628 } else { 2629 rval = 0; 2630 } 2631 break; 2632 default: 2633 rval = -1; 2634 break; 2635 } 2636 return (rval); 2637 } 2638 2639 /* 2640 * Implementations of scsi tran_destroy_pkt. 2641 * Free resources allocated by sata_scsi_init_pkt() 2642 */ 2643 static void 2644 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2645 { 2646 sata_pkt_txlate_t *spx; 2647 2648 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2649 2650 sata_common_free_dma_rsrcs(spx); 2651 2652 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2653 sata_pkt_free(spx); 2654 2655 scsi_hba_pkt_free(ap, pkt); 2656 } 2657 2658 /* 2659 * Implementation of scsi tran_dmafree. 2660 * Free DMA resources allocated by sata_scsi_init_pkt() 2661 */ 2662 2663 static void 2664 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2665 { 2666 #ifndef __lock_lint 2667 _NOTE(ARGUNUSED(ap)) 2668 #endif 2669 sata_pkt_txlate_t *spx; 2670 2671 ASSERT(pkt != NULL); 2672 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2673 2674 sata_common_free_dma_rsrcs(spx); 2675 } 2676 2677 /* 2678 * Implementation of scsi tran_sync_pkt. 2679 * 2680 * The assumption below is that pkt is unique - there is no need to check ap 2681 * 2682 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 2683 * into/from the real buffer. 2684 */ 2685 static void 2686 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2687 { 2688 #ifndef __lock_lint 2689 _NOTE(ARGUNUSED(ap)) 2690 #endif 2691 int rval; 2692 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2693 struct buf *bp; 2694 int direction; 2695 2696 ASSERT(spx != NULL); 2697 if (spx->txlt_buf_dma_handle != NULL) { 2698 direction = spx->txlt_sata_pkt-> 2699 satapkt_cmd.satacmd_flags.sata_data_direction; 2700 if (spx->txlt_sata_pkt != NULL && 2701 direction != SATA_DIR_NODATA_XFER) { 2702 if (spx->txlt_tmp_buf != NULL) { 2703 /* Intermediate DMA buffer used */ 2704 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2705 2706 if (direction & SATA_DIR_WRITE) { 2707 bcopy(bp->b_un.b_addr, 2708 spx->txlt_tmp_buf, bp->b_bcount); 2709 } 2710 } 2711 /* Sync the buffer for device or for CPU */ 2712 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 2713 (direction & SATA_DIR_WRITE) ? 2714 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 2715 ASSERT(rval == DDI_SUCCESS); 2716 if (spx->txlt_tmp_buf != NULL && 2717 !(direction & SATA_DIR_WRITE)) { 2718 /* Intermediate DMA buffer used for read */ 2719 bcopy(spx->txlt_tmp_buf, 2720 bp->b_un.b_addr, bp->b_bcount); 2721 } 2722 2723 } 2724 } 2725 } 2726 2727 2728 2729 /* ******************* SATA - SCSI Translation functions **************** */ 2730 /* 2731 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 2732 * translation. 2733 */ 2734 2735 /* 2736 * Checks if a device exists and can be access and translates common 2737 * scsi_pkt data to sata_pkt data. 2738 * 2739 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 2740 * sata_pkt was set-up. 2741 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 2742 * exist and pkt_comp callback was scheduled. 2743 * Returns other TRAN_XXXXX values when error occured and command should be 2744 * rejected with the returned TRAN_XXXXX value. 2745 * 2746 * This function should be called with port mutex held. 2747 */ 2748 static int 2749 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason) 2750 { 2751 sata_drive_info_t *sdinfo; 2752 sata_device_t sata_device; 2753 const struct sata_cmd_flags sata_initial_cmd_flags = { 2754 SATA_DIR_NODATA_XFER, 2755 /* all other values to 0/FALSE */ 2756 }; 2757 /* 2758 * Pkt_reason has to be set if the pkt_comp callback is invoked, 2759 * and that implies TRAN_ACCEPT return value. Any other returned value 2760 * indicates that the scsi packet was not accepted (the reason will not 2761 * be checked by the scsi target driver). 2762 * To make debugging easier, we set pkt_reason to know value here. 2763 * It may be changed later when different completion reason is 2764 * determined. 2765 */ 2766 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 2767 *reason = CMD_TRAN_ERR; 2768 2769 /* Validate address */ 2770 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 2771 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 2772 2773 case -1: 2774 /* Invalid address or invalid device type */ 2775 return (TRAN_BADPKT); 2776 case 1: 2777 /* valid address but no device - it has disappeared ? */ 2778 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 2779 *reason = CMD_DEV_GONE; 2780 /* 2781 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2782 * only in callback function (for normal requests) and 2783 * in the dump code path. 2784 * So, if the callback is available, we need to do 2785 * the callback rather than returning TRAN_FATAL_ERROR here. 2786 */ 2787 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 2788 /* scsi callback required */ 2789 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2790 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2791 (void *)spx->txlt_scsi_pkt, 2792 TQ_SLEEP) == NULL) 2793 /* Scheduling the callback failed */ 2794 return (TRAN_BUSY); 2795 2796 return (TRAN_ACCEPT); 2797 } 2798 return (TRAN_FATAL_ERROR); 2799 default: 2800 /* all OK; pkt reason will be overwritten later */ 2801 break; 2802 } 2803 /* 2804 * If in an interrupt context, reject packet if it is to be 2805 * executed in polling mode 2806 */ 2807 if (servicing_interrupt() && 2808 (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2809 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 2810 "sata_scsi_start: rejecting synchronous command because " 2811 "of interrupt context\n", NULL); 2812 return (TRAN_BUSY); 2813 } 2814 2815 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2816 &spx->txlt_sata_pkt->satapkt_device); 2817 2818 /* 2819 * If device is in reset condition, reject the packet with 2820 * TRAN_BUSY, unless: 2821 * 1. system is panicking (dumping) 2822 * In such case only one thread is running and there is no way to 2823 * process reset. 2824 * 2. cfgadm operation is is progress (internal APCTL lock is set) 2825 * Some cfgadm operations involve drive commands, so reset condition 2826 * needs to be ignored for IOCTL operations. 2827 */ 2828 if ((sdinfo->satadrv_event_flags & 2829 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 2830 2831 if (!ddi_in_panic() && 2832 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 2833 sata_device.satadev_addr.cport) & 2834 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 2835 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 2836 *reason = CMD_INCOMPLETE; 2837 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2838 "sata_scsi_start: rejecting command because " 2839 "of device reset state\n", NULL); 2840 return (TRAN_BUSY); 2841 } 2842 } 2843 2844 /* 2845 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 2846 * sata_scsi_pkt_init() because pkt init had to work also with 2847 * non-existing devices. 2848 * Now we know that the packet was set-up for a real device, so its 2849 * type is known. 2850 */ 2851 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 2852 2853 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 2854 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 2855 sata_device.satadev_addr.cport)->cport_event_flags & 2856 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 2857 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2858 sata_ignore_dev_reset = B_TRUE; 2859 } 2860 /* 2861 * At this point the generic translation routine determined that the 2862 * scsi packet should be accepted. Packet completion reason may be 2863 * changed later when a different completion reason is determined. 2864 */ 2865 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 2866 *reason = CMD_CMPLT; 2867 2868 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2869 /* Synchronous execution */ 2870 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 2871 SATA_OPMODE_POLLING; 2872 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2873 sata_ignore_dev_reset = ddi_in_panic(); 2874 } else { 2875 /* Asynchronous execution */ 2876 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 2877 SATA_OPMODE_INTERRUPTS; 2878 } 2879 /* Convert queuing information */ 2880 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 2881 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 2882 B_TRUE; 2883 else if (spx->txlt_scsi_pkt->pkt_flags & 2884 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 2885 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 2886 B_TRUE; 2887 2888 /* Always limit pkt time */ 2889 if (spx->txlt_scsi_pkt->pkt_time == 0) 2890 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 2891 else 2892 /* Pass on scsi_pkt time */ 2893 spx->txlt_sata_pkt->satapkt_time = 2894 spx->txlt_scsi_pkt->pkt_time; 2895 2896 return (TRAN_ACCEPT); 2897 } 2898 2899 2900 /* 2901 * Translate ATA Identify Device data to SCSI Inquiry data. 2902 * This function may be called only for ATA devices. 2903 * This function should not be called for ATAPI devices - they 2904 * respond directly to SCSI Inquiry command. 2905 * 2906 * SATA Identify Device data has to be valid in sata_rive_info. 2907 * Buffer has to accomodate the inquiry length (36 bytes). 2908 * 2909 * This function should be called with a port mutex held. 2910 */ 2911 static void 2912 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 2913 sata_drive_info_t *sdinfo, uint8_t *buf) 2914 { 2915 2916 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 2917 struct sata_id *sid = &sdinfo->satadrv_id; 2918 2919 /* Start with a nice clean slate */ 2920 bzero((void *)inq, sizeof (struct scsi_inquiry)); 2921 2922 /* 2923 * Rely on the dev_type for setting paripheral qualifier. 2924 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 2925 * It could be that DTYPE_OPTICAL could also qualify in the future. 2926 * ATAPI Inquiry may provide more data to the target driver. 2927 */ 2928 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 2929 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 2930 2931 inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0; 2932 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 2933 inq->inq_iso = 0; /* ISO version */ 2934 inq->inq_ecma = 0; /* ECMA version */ 2935 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 2936 inq->inq_aenc = 0; /* Async event notification cap. */ 2937 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 2938 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 2939 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 2940 inq->inq_len = 31; /* Additional length */ 2941 inq->inq_dualp = 0; /* dual port device - NO */ 2942 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 2943 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 2944 inq->inq_linked = 0; /* Supports linked commands - NO */ 2945 /* 2946 * Queuing support - controller has to 2947 * support some sort of command queuing. 2948 */ 2949 if (SATA_QDEPTH(sata_hba_inst) > 1) 2950 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 2951 else 2952 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 2953 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 2954 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 2955 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 2956 2957 #ifdef _LITTLE_ENDIAN 2958 /* Swap text fields to match SCSI format */ 2959 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2960 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2961 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2962 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2963 else 2964 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2965 #else /* _LITTLE_ENDIAN */ 2966 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2967 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2968 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2969 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2970 else 2971 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2972 #endif /* _LITTLE_ENDIAN */ 2973 } 2974 2975 2976 /* 2977 * Scsi response set up for invalid command (command not supported) 2978 * 2979 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 2980 */ 2981 static int 2982 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 2983 { 2984 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 2985 struct scsi_extended_sense *sense; 2986 2987 scsipkt->pkt_reason = CMD_CMPLT; 2988 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 2989 STATE_SENT_CMD | STATE_GOT_STATUS; 2990 2991 *scsipkt->pkt_scbp = STATUS_CHECK; 2992 2993 sense = sata_arq_sense(spx); 2994 sense->es_key = KEY_ILLEGAL_REQUEST; 2995 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 2996 2997 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2998 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 2999 3000 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3001 scsipkt->pkt_comp != NULL) 3002 /* scsi callback required */ 3003 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3004 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3005 (void *)spx->txlt_scsi_pkt, 3006 TQ_SLEEP) == NULL) 3007 /* Scheduling the callback failed */ 3008 return (TRAN_BUSY); 3009 return (TRAN_ACCEPT); 3010 } 3011 3012 /* 3013 * Scsi response setup for 3014 * emulated non-data command that requires no action/return data 3015 * 3016 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3017 */ 3018 static int 3019 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3020 { 3021 int rval; 3022 int reason; 3023 3024 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3025 3026 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3027 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3028 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3029 return (rval); 3030 } 3031 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3032 3033 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3034 STATE_SENT_CMD | STATE_GOT_STATUS; 3035 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3036 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3037 3038 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3039 "Scsi_pkt completion reason %x\n", 3040 spx->txlt_scsi_pkt->pkt_reason); 3041 3042 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3043 spx->txlt_scsi_pkt->pkt_comp != NULL) 3044 /* scsi callback required */ 3045 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3046 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3047 (void *)spx->txlt_scsi_pkt, 3048 TQ_SLEEP) == NULL) 3049 /* Scheduling the callback failed */ 3050 return (TRAN_BUSY); 3051 return (TRAN_ACCEPT); 3052 } 3053 3054 3055 /* 3056 * SATA translate command: Inquiry / Identify Device 3057 * Use cached Identify Device data for now, rather than issuing actual 3058 * Device Identify cmd request. If device is detached and re-attached, 3059 * asynchromous event processing should fetch and refresh Identify Device 3060 * data. 3061 * Two VPD pages are supported now: 3062 * Vital Product Data page 3063 * Unit Serial Number page 3064 * 3065 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3066 */ 3067 3068 #define EVPD 1 /* Extended Vital Product Data flag */ 3069 #define CMDDT 2 /* Command Support Data - Obsolete */ 3070 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3071 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3072 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3073 3074 static int 3075 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3076 { 3077 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3078 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3079 sata_drive_info_t *sdinfo; 3080 struct scsi_extended_sense *sense; 3081 int count; 3082 uint8_t *p; 3083 int i, j; 3084 uint8_t page_buf[0xff]; /* Max length */ 3085 int rval, reason; 3086 3087 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3088 3089 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3090 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3091 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3092 return (rval); 3093 } 3094 3095 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3096 &spx->txlt_sata_pkt->satapkt_device); 3097 3098 ASSERT(sdinfo != NULL); 3099 3100 scsipkt->pkt_reason = CMD_CMPLT; 3101 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3102 STATE_SENT_CMD | STATE_GOT_STATUS; 3103 3104 /* Reject not supported request */ 3105 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3106 *scsipkt->pkt_scbp = STATUS_CHECK; 3107 sense = sata_arq_sense(spx); 3108 sense->es_key = KEY_ILLEGAL_REQUEST; 3109 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3110 goto done; 3111 } 3112 3113 /* Valid Inquiry request */ 3114 *scsipkt->pkt_scbp = STATUS_GOOD; 3115 3116 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3117 3118 /* 3119 * Because it is fully emulated command storing data 3120 * programatically in the specified buffer, release 3121 * preallocated DMA resources before storing data in the buffer, 3122 * so no unwanted DMA sync would take place. 3123 */ 3124 sata_scsi_dmafree(NULL, scsipkt); 3125 3126 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3127 /* Standard Inquiry Data request */ 3128 struct scsi_inquiry inq; 3129 unsigned int bufsize; 3130 3131 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3132 sdinfo, (uint8_t *)&inq); 3133 /* Copy no more than requested */ 3134 count = MIN(bp->b_bcount, 3135 sizeof (struct scsi_inquiry)); 3136 bufsize = scsipkt->pkt_cdbp[4]; 3137 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3138 count = MIN(count, bufsize); 3139 bcopy(&inq, bp->b_un.b_addr, count); 3140 3141 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3142 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3143 bufsize - count : 0; 3144 } else { 3145 /* 3146 * peripheral_qualifier = 0; 3147 * 3148 * We are dealing only with HD and will be 3149 * dealing with CD/DVD devices soon 3150 */ 3151 uint8_t peripheral_device_type = 3152 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3153 DTYPE_DIRECT : DTYPE_RODIRECT; 3154 3155 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3156 case INQUIRY_SUP_VPD_PAGE: 3157 /* 3158 * Request for suported Vital Product Data 3159 * pages - assuming only 2 page codes 3160 * supported. 3161 */ 3162 page_buf[0] = peripheral_device_type; 3163 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3164 page_buf[2] = 0; 3165 page_buf[3] = 2; /* page length */ 3166 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3167 page_buf[5] = INQUIRY_USN_PAGE; 3168 /* Copy no more than requested */ 3169 count = MIN(bp->b_bcount, 6); 3170 bcopy(page_buf, bp->b_un.b_addr, count); 3171 break; 3172 3173 case INQUIRY_USN_PAGE: 3174 /* 3175 * Request for Unit Serial Number page. 3176 * Set-up the page. 3177 */ 3178 page_buf[0] = peripheral_device_type; 3179 page_buf[1] = INQUIRY_USN_PAGE; 3180 page_buf[2] = 0; 3181 /* remaining page length */ 3182 page_buf[3] = SATA_ID_SERIAL_LEN; 3183 3184 /* 3185 * Copy serial number from Identify Device data 3186 * words into the inquiry page and swap bytes 3187 * when necessary. 3188 */ 3189 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3190 #ifdef _LITTLE_ENDIAN 3191 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3192 #else 3193 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3194 #endif 3195 /* 3196 * Least significant character of the serial 3197 * number shall appear as the last byte, 3198 * according to SBC-3 spec. 3199 * Count trailing spaces to determine the 3200 * necessary shift length. 3201 */ 3202 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3203 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3204 if (*(p - j) != '\0' && 3205 *(p - j) != '\040') 3206 break; 3207 } 3208 3209 /* 3210 * Shift SN string right, so that the last 3211 * non-blank character would appear in last 3212 * byte of SN field in the page. 3213 * 'j' is the shift length. 3214 */ 3215 for (i = 0; 3216 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3217 i++, p--) 3218 *p = *(p - j); 3219 3220 /* 3221 * Add leading spaces - same number as the 3222 * shift size 3223 */ 3224 for (; j > 0; j--) 3225 page_buf[4 + j - 1] = '\040'; 3226 3227 count = MIN(bp->b_bcount, 3228 SATA_ID_SERIAL_LEN + 4); 3229 bcopy(page_buf, bp->b_un.b_addr, count); 3230 break; 3231 3232 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3233 /* 3234 * We may want to implement this page, when 3235 * identifiers are common for SATA devices 3236 * But not now. 3237 */ 3238 /*FALLTHROUGH*/ 3239 3240 default: 3241 /* Request for unsupported VPD page */ 3242 *scsipkt->pkt_scbp = STATUS_CHECK; 3243 sense = sata_arq_sense(spx); 3244 sense->es_key = KEY_ILLEGAL_REQUEST; 3245 sense->es_add_code = 3246 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3247 goto done; 3248 } 3249 } 3250 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3251 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3252 scsipkt->pkt_cdbp[4] - count : 0; 3253 } 3254 done: 3255 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3256 3257 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3258 "Scsi_pkt completion reason %x\n", 3259 scsipkt->pkt_reason); 3260 3261 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3262 scsipkt->pkt_comp != NULL) { 3263 /* scsi callback required */ 3264 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3265 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3266 TQ_SLEEP) == NULL) 3267 /* Scheduling the callback failed */ 3268 return (TRAN_BUSY); 3269 } 3270 return (TRAN_ACCEPT); 3271 } 3272 3273 /* 3274 * SATA translate command: Request Sense. 3275 * Emulated command (ATA version for SATA hard disks) 3276 * Always NO SENSE, because any sense data should be reported by ARQ sense. 3277 * 3278 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3279 * 3280 * Note: There is a mismatch between already implemented Informational 3281 * Exception Mode Select page 0x1C and this function. 3282 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3283 * NO SENSE and set additional sense code to the exception code - this is not 3284 * implemented here. 3285 */ 3286 static int 3287 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3288 { 3289 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3290 struct scsi_extended_sense sense; 3291 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3292 int rval, reason; 3293 3294 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3295 3296 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3297 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3298 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3299 return (rval); 3300 } 3301 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3302 3303 3304 scsipkt->pkt_reason = CMD_CMPLT; 3305 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3306 STATE_SENT_CMD | STATE_GOT_STATUS; 3307 *scsipkt->pkt_scbp = STATUS_GOOD; 3308 3309 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3310 /* 3311 * Because it is fully emulated command storing data 3312 * programatically in the specified buffer, release 3313 * preallocated DMA resources before storing data in the buffer, 3314 * so no unwanted DMA sync would take place. 3315 */ 3316 int count = MIN(bp->b_bcount, 3317 sizeof (struct scsi_extended_sense)); 3318 sata_scsi_dmafree(NULL, scsipkt); 3319 bzero(&sense, sizeof (struct scsi_extended_sense)); 3320 sense.es_valid = 0; /* Valid LBA */ 3321 sense.es_class = 7; /* Response code 0x70 - current err */ 3322 sense.es_key = KEY_NO_SENSE; 3323 sense.es_add_len = 6; /* Additional length */ 3324 /* Copy no more than requested */ 3325 bcopy(&sense, bp->b_un.b_addr, count); 3326 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3327 scsipkt->pkt_resid = 0; 3328 } 3329 3330 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3331 "Scsi_pkt completion reason %x\n", 3332 scsipkt->pkt_reason); 3333 3334 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3335 scsipkt->pkt_comp != NULL) 3336 /* scsi callback required */ 3337 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3338 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3339 TQ_SLEEP) == NULL) 3340 /* Scheduling the callback failed */ 3341 return (TRAN_BUSY); 3342 return (TRAN_ACCEPT); 3343 } 3344 3345 /* 3346 * SATA translate command: Test Unit Ready 3347 * At the moment this is an emulated command (ATA version for SATA hard disks). 3348 * May be translated into Check Power Mode command in the future 3349 * 3350 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3351 */ 3352 static int 3353 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3354 { 3355 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3356 struct scsi_extended_sense *sense; 3357 int power_state; 3358 int rval, reason; 3359 3360 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3361 3362 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3363 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3364 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3365 return (rval); 3366 } 3367 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3368 3369 /* At this moment, emulate it rather than execute anything */ 3370 power_state = SATA_PWRMODE_ACTIVE; 3371 3372 scsipkt->pkt_reason = CMD_CMPLT; 3373 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3374 STATE_SENT_CMD | STATE_GOT_STATUS; 3375 3376 switch (power_state) { 3377 case SATA_PWRMODE_ACTIVE: 3378 case SATA_PWRMODE_IDLE: 3379 *scsipkt->pkt_scbp = STATUS_GOOD; 3380 break; 3381 default: 3382 /* PWR mode standby */ 3383 *scsipkt->pkt_scbp = STATUS_CHECK; 3384 sense = sata_arq_sense(spx); 3385 sense->es_key = KEY_NOT_READY; 3386 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3387 break; 3388 } 3389 3390 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3391 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3392 3393 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3394 scsipkt->pkt_comp != NULL) 3395 /* scsi callback required */ 3396 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3397 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3398 TQ_SLEEP) == NULL) 3399 /* Scheduling the callback failed */ 3400 return (TRAN_BUSY); 3401 3402 return (TRAN_ACCEPT); 3403 } 3404 3405 3406 /* 3407 * SATA translate command: Start Stop Unit 3408 * Translation depends on a command: 3409 * Start Unit translated into Idle Immediate 3410 * Stop Unit translated into Standby Immediate 3411 * Unload Media / NOT SUPPORTED YET 3412 * Load Media / NOT SUPPROTED YET 3413 * Power condition bits are ignored, so is Immediate bit 3414 * Requesting synchronous execution. 3415 * 3416 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3417 * appropriate values in scsi_pkt fields. 3418 */ 3419 static int 3420 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3421 { 3422 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3423 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3424 struct scsi_extended_sense *sense; 3425 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3426 int cport = SATA_TXLT_CPORT(spx); 3427 int rval, reason; 3428 int synch; 3429 3430 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3431 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3432 3433 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3434 3435 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3436 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3437 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3438 return (rval); 3439 } 3440 3441 if (scsipkt->pkt_cdbp[4] & 2) { 3442 /* Load/Unload Media - invalid request */ 3443 *scsipkt->pkt_scbp = STATUS_CHECK; 3444 sense = sata_arq_sense(spx); 3445 sense->es_key = KEY_ILLEGAL_REQUEST; 3446 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3447 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3448 3449 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3450 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3451 3452 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3453 scsipkt->pkt_comp != NULL) 3454 /* scsi callback required */ 3455 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3456 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3457 TQ_SLEEP) == NULL) 3458 /* Scheduling the callback failed */ 3459 return (TRAN_BUSY); 3460 3461 return (TRAN_ACCEPT); 3462 } 3463 scmd->satacmd_addr_type = 0; 3464 scmd->satacmd_sec_count_lsb = 0; 3465 scmd->satacmd_lba_low_lsb = 0; 3466 scmd->satacmd_lba_mid_lsb = 0; 3467 scmd->satacmd_lba_high_lsb = 0; 3468 scmd->satacmd_features_reg = 0; 3469 scmd->satacmd_device_reg = 0; 3470 scmd->satacmd_status_reg = 0; 3471 if (scsipkt->pkt_cdbp[4] & 1) { 3472 /* Start Unit */ 3473 scmd->satacmd_cmd_reg = SATAC_IDLE_IM; 3474 } else { 3475 /* Stop Unit */ 3476 scmd->satacmd_cmd_reg = SATAC_STANDBY_IM; 3477 } 3478 3479 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 3480 /* Need to set-up a callback function */ 3481 spx->txlt_sata_pkt->satapkt_comp = 3482 sata_txlt_nodata_cmd_completion; 3483 synch = FALSE; 3484 } else { 3485 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3486 synch = TRUE; 3487 } 3488 3489 /* Transfer command to HBA */ 3490 if (sata_hba_start(spx, &rval) != 0) { 3491 /* Pkt not accepted for execution */ 3492 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3493 return (rval); 3494 } 3495 3496 /* 3497 * If execution is non-synchronous, 3498 * a callback function will handle potential errors, translate 3499 * the response and will do a callback to a target driver. 3500 * If it was synchronous, check execution status using the same 3501 * framework callback. 3502 */ 3503 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3504 if (synch) { 3505 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3506 "synchronous execution status %x\n", 3507 spx->txlt_sata_pkt->satapkt_reason); 3508 3509 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 3510 } 3511 return (TRAN_ACCEPT); 3512 3513 } 3514 3515 3516 /* 3517 * SATA translate command: Read Capacity. 3518 * Emulated command for SATA disks. 3519 * Capacity is retrieved from cached Idenifty Device data. 3520 * Identify Device data shows effective disk capacity, not the native 3521 * capacity, which may be limitted by Set Max Address command. 3522 * This is ATA version for SATA hard disks. 3523 * 3524 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3525 */ 3526 static int 3527 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 3528 { 3529 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3530 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3531 sata_drive_info_t *sdinfo; 3532 uint64_t val; 3533 uchar_t *rbuf; 3534 int rval, reason; 3535 3536 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3537 "sata_txlt_read_capacity: ", NULL); 3538 3539 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3540 3541 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3542 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3543 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3544 return (rval); 3545 } 3546 3547 scsipkt->pkt_reason = CMD_CMPLT; 3548 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3549 STATE_SENT_CMD | STATE_GOT_STATUS; 3550 *scsipkt->pkt_scbp = STATUS_GOOD; 3551 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3552 /* 3553 * Because it is fully emulated command storing data 3554 * programatically in the specified buffer, release 3555 * preallocated DMA resources before storing data in the buffer, 3556 * so no unwanted DMA sync would take place. 3557 */ 3558 sata_scsi_dmafree(NULL, scsipkt); 3559 3560 sdinfo = sata_get_device_info( 3561 spx->txlt_sata_hba_inst, 3562 &spx->txlt_sata_pkt->satapkt_device); 3563 /* Last logical block address */ 3564 val = sdinfo->satadrv_capacity - 1; 3565 rbuf = (uchar_t *)bp->b_un.b_addr; 3566 /* Need to swap endians to match scsi format */ 3567 rbuf[0] = (val >> 24) & 0xff; 3568 rbuf[1] = (val >> 16) & 0xff; 3569 rbuf[2] = (val >> 8) & 0xff; 3570 rbuf[3] = val & 0xff; 3571 /* block size - always 512 bytes, for now */ 3572 rbuf[4] = 0; 3573 rbuf[5] = 0; 3574 rbuf[6] = 0x02; 3575 rbuf[7] = 0; 3576 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3577 scsipkt->pkt_resid = 0; 3578 3579 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 3580 sdinfo->satadrv_capacity -1); 3581 } 3582 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3583 /* 3584 * If a callback was requested, do it now. 3585 */ 3586 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3587 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3588 3589 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3590 scsipkt->pkt_comp != NULL) 3591 /* scsi callback required */ 3592 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3593 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3594 TQ_SLEEP) == NULL) 3595 /* Scheduling the callback failed */ 3596 return (TRAN_BUSY); 3597 3598 return (TRAN_ACCEPT); 3599 } 3600 3601 /* 3602 * SATA translate command: Mode Sense. 3603 * Translated into appropriate SATA command or emulated. 3604 * Saved Values Page Control (03) are not supported. 3605 * 3606 * NOTE: only caching mode sense page is currently implemented. 3607 * 3608 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3609 */ 3610 3611 static int 3612 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 3613 { 3614 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3615 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3616 sata_drive_info_t *sdinfo; 3617 sata_id_t *sata_id; 3618 struct scsi_extended_sense *sense; 3619 int len, bdlen, count, alc_len; 3620 int pc; /* Page Control code */ 3621 uint8_t *buf; /* mode sense buffer */ 3622 int rval, reason; 3623 3624 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3625 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 3626 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3627 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3628 3629 buf = kmem_zalloc(1024, KM_SLEEP); 3630 3631 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3632 3633 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3634 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3635 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3636 kmem_free(buf, 1024); 3637 return (rval); 3638 } 3639 3640 scsipkt->pkt_reason = CMD_CMPLT; 3641 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3642 STATE_SENT_CMD | STATE_GOT_STATUS; 3643 3644 pc = scsipkt->pkt_cdbp[2] >> 6; 3645 3646 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3647 /* 3648 * Because it is fully emulated command storing data 3649 * programatically in the specified buffer, release 3650 * preallocated DMA resources before storing data in the buffer, 3651 * so no unwanted DMA sync would take place. 3652 */ 3653 sata_scsi_dmafree(NULL, scsipkt); 3654 3655 len = 0; 3656 bdlen = 0; 3657 if (!(scsipkt->pkt_cdbp[1] & 8)) { 3658 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 3659 (scsipkt->pkt_cdbp[0] & 0x10)) 3660 bdlen = 16; 3661 else 3662 bdlen = 8; 3663 } 3664 /* Build mode parameter header */ 3665 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3666 /* 4-byte mode parameter header */ 3667 buf[len++] = 0; /* mode data length */ 3668 buf[len++] = 0; /* medium type */ 3669 buf[len++] = 0; /* dev-specific param */ 3670 buf[len++] = bdlen; /* Block Descriptor length */ 3671 } else { 3672 /* 8-byte mode parameter header */ 3673 buf[len++] = 0; /* mode data length */ 3674 buf[len++] = 0; 3675 buf[len++] = 0; /* medium type */ 3676 buf[len++] = 0; /* dev-specific param */ 3677 if (bdlen == 16) 3678 buf[len++] = 1; /* long lba descriptor */ 3679 else 3680 buf[len++] = 0; 3681 buf[len++] = 0; 3682 buf[len++] = 0; /* Block Descriptor length */ 3683 buf[len++] = bdlen; 3684 } 3685 3686 sdinfo = sata_get_device_info( 3687 spx->txlt_sata_hba_inst, 3688 &spx->txlt_sata_pkt->satapkt_device); 3689 3690 /* Build block descriptor only if not disabled (DBD) */ 3691 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 3692 /* Block descriptor - direct-access device format */ 3693 if (bdlen == 8) { 3694 /* build regular block descriptor */ 3695 buf[len++] = 3696 (sdinfo->satadrv_capacity >> 24) & 0xff; 3697 buf[len++] = 3698 (sdinfo->satadrv_capacity >> 16) & 0xff; 3699 buf[len++] = 3700 (sdinfo->satadrv_capacity >> 8) & 0xff; 3701 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3702 buf[len++] = 0; /* density code */ 3703 buf[len++] = 0; 3704 if (sdinfo->satadrv_type == 3705 SATA_DTYPE_ATADISK) 3706 buf[len++] = 2; 3707 else 3708 /* ATAPI */ 3709 buf[len++] = 8; 3710 buf[len++] = 0; 3711 } else if (bdlen == 16) { 3712 /* Long LBA Accepted */ 3713 /* build long lba block descriptor */ 3714 #ifndef __lock_lint 3715 buf[len++] = 3716 (sdinfo->satadrv_capacity >> 56) & 0xff; 3717 buf[len++] = 3718 (sdinfo->satadrv_capacity >> 48) & 0xff; 3719 buf[len++] = 3720 (sdinfo->satadrv_capacity >> 40) & 0xff; 3721 buf[len++] = 3722 (sdinfo->satadrv_capacity >> 32) & 0xff; 3723 #endif 3724 buf[len++] = 3725 (sdinfo->satadrv_capacity >> 24) & 0xff; 3726 buf[len++] = 3727 (sdinfo->satadrv_capacity >> 16) & 0xff; 3728 buf[len++] = 3729 (sdinfo->satadrv_capacity >> 8) & 0xff; 3730 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3731 buf[len++] = 0; 3732 buf[len++] = 0; /* density code */ 3733 buf[len++] = 0; 3734 buf[len++] = 0; 3735 if (sdinfo->satadrv_type == 3736 SATA_DTYPE_ATADISK) 3737 buf[len++] = 2; 3738 else 3739 /* ATAPI */ 3740 buf[len++] = 8; 3741 buf[len++] = 0; 3742 } 3743 } 3744 3745 sata_id = &sdinfo->satadrv_id; 3746 3747 /* 3748 * Add requested pages. 3749 * Page 3 and 4 are obsolete and we are not supporting them. 3750 * We deal now with: 3751 * caching (read/write cache control). 3752 * We should eventually deal with following mode pages: 3753 * error recovery (0x01), 3754 * power condition (0x1a), 3755 * exception control page (enables SMART) (0x1c), 3756 * enclosure management (ses), 3757 * protocol-specific port mode (port control). 3758 */ 3759 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 3760 case MODEPAGE_RW_ERRRECOV: 3761 /* DAD_MODE_ERR_RECOV */ 3762 /* R/W recovery */ 3763 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3764 break; 3765 case MODEPAGE_CACHING: 3766 /* DAD_MODE_CACHE */ 3767 /* Reject not supported request for saved parameters */ 3768 if (pc == 3) { 3769 *scsipkt->pkt_scbp = STATUS_CHECK; 3770 sense = sata_arq_sense(spx); 3771 sense->es_key = KEY_ILLEGAL_REQUEST; 3772 sense->es_add_code = 3773 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 3774 goto done; 3775 } 3776 3777 /* caching */ 3778 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3779 break; 3780 case MODEPAGE_INFO_EXCPT: 3781 /* exception cntrl */ 3782 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3783 len += sata_build_msense_page_1c(sdinfo, pc, 3784 buf+len); 3785 } 3786 else 3787 goto err; 3788 break; 3789 case MODEPAGE_POWER_COND: 3790 /* DAD_MODE_POWER_COND */ 3791 /* power condition */ 3792 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3793 break; 3794 3795 case MODEPAGE_ACOUSTIC_MANAG: 3796 /* acoustic management */ 3797 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3798 break; 3799 case MODEPAGE_ALLPAGES: 3800 /* all pages */ 3801 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3802 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3803 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3804 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3805 len += sata_build_msense_page_1c(sdinfo, pc, 3806 buf+len); 3807 } 3808 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3809 break; 3810 default: 3811 err: 3812 /* Invalid request */ 3813 *scsipkt->pkt_scbp = STATUS_CHECK; 3814 sense = sata_arq_sense(spx); 3815 sense->es_key = KEY_ILLEGAL_REQUEST; 3816 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3817 goto done; 3818 } 3819 3820 /* fix total mode data length */ 3821 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3822 /* 4-byte mode parameter header */ 3823 buf[0] = len - 1; /* mode data length */ 3824 } else { 3825 buf[0] = (len -2) >> 8; 3826 buf[1] = (len -2) & 0xff; 3827 } 3828 3829 3830 /* Check allocation length */ 3831 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3832 alc_len = scsipkt->pkt_cdbp[4]; 3833 } else { 3834 alc_len = scsipkt->pkt_cdbp[7]; 3835 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 3836 } 3837 /* 3838 * We do not check for possible parameters truncation 3839 * (alc_len < len) assuming that the target driver works 3840 * correctly. Just avoiding overrun. 3841 * Copy no more than requested and possible, buffer-wise. 3842 */ 3843 count = MIN(alc_len, len); 3844 count = MIN(bp->b_bcount, count); 3845 bcopy(buf, bp->b_un.b_addr, count); 3846 3847 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3848 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 3849 } 3850 *scsipkt->pkt_scbp = STATUS_GOOD; 3851 done: 3852 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3853 (void) kmem_free(buf, 1024); 3854 3855 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3856 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3857 3858 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3859 scsipkt->pkt_comp != NULL) 3860 /* scsi callback required */ 3861 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3862 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3863 TQ_SLEEP) == NULL) 3864 /* Scheduling the callback failed */ 3865 return (TRAN_BUSY); 3866 3867 return (TRAN_ACCEPT); 3868 } 3869 3870 3871 /* 3872 * SATA translate command: Mode Select. 3873 * Translated into appropriate SATA command or emulated. 3874 * Saving parameters is not supported. 3875 * Changing device capacity is not supported (although theoretically 3876 * possible by executing SET FEATURES/SET MAX ADDRESS) 3877 * 3878 * Assumption is that the target driver is working correctly. 3879 * 3880 * More than one SATA command may be executed to perform operations specified 3881 * by mode select pages. The first error terminates further execution. 3882 * Operations performed successully are not backed-up in such case. 3883 * 3884 * NOTE: Implemented pages: 3885 * - caching page 3886 * - informational exception page 3887 * - acoustic management page 3888 * Caching setup is remembered so it could be re-stored in case of 3889 * an unexpected device reset. 3890 * 3891 * Returns TRAN_XXXX. 3892 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 3893 */ 3894 3895 static int 3896 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 3897 { 3898 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3899 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3900 struct scsi_extended_sense *sense; 3901 int len, pagelen, count, pllen; 3902 uint8_t *buf; /* mode select buffer */ 3903 int rval, stat, reason; 3904 uint_t nointr_flag; 3905 int dmod = 0; 3906 3907 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3908 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 3909 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3910 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3911 3912 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3913 3914 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3915 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3916 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3917 return (rval); 3918 } 3919 /* 3920 * If in interrupt context, reject this packet because it may result 3921 * in issuing a synchronous command to HBA. 3922 */ 3923 if (servicing_interrupt()) { 3924 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3925 "sata_txlt_mode_select: rejecting command because " 3926 "of interrupt context\n", NULL); 3927 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3928 return (TRAN_BUSY); 3929 } 3930 3931 rval = TRAN_ACCEPT; 3932 3933 scsipkt->pkt_reason = CMD_CMPLT; 3934 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3935 STATE_SENT_CMD | STATE_GOT_STATUS; 3936 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 3937 3938 /* Reject not supported request */ 3939 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 3940 *scsipkt->pkt_scbp = STATUS_CHECK; 3941 sense = sata_arq_sense(spx); 3942 sense->es_key = KEY_ILLEGAL_REQUEST; 3943 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3944 goto done; 3945 } 3946 3947 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3948 pllen = scsipkt->pkt_cdbp[4]; 3949 } else { 3950 pllen = scsipkt->pkt_cdbp[7]; 3951 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 3952 } 3953 3954 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 3955 3956 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 3957 buf = (uint8_t *)bp->b_un.b_addr; 3958 count = MIN(bp->b_bcount, pllen); 3959 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3960 scsipkt->pkt_resid = 0; 3961 pllen = count; 3962 3963 /* 3964 * Check the header to skip the block descriptor(s) - we 3965 * do not support setting device capacity. 3966 * Existing macros do not recognize long LBA dscriptor, 3967 * hence manual calculation. 3968 */ 3969 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3970 /* 6-bytes CMD, 4 bytes header */ 3971 if (count <= 4) 3972 goto done; /* header only */ 3973 len = buf[3] + 4; 3974 } else { 3975 /* 10-bytes CMD, 8 bytes header */ 3976 if (count <= 8) 3977 goto done; /* header only */ 3978 len = buf[6]; 3979 len = (len << 8) + buf[7] + 8; 3980 } 3981 if (len >= count) 3982 goto done; /* header + descriptor(s) only */ 3983 3984 pllen -= len; /* remaining data length */ 3985 3986 /* 3987 * We may be executing SATA command and want to execute it 3988 * in SYNCH mode, regardless of scsi_pkt setting. 3989 * Save scsi_pkt setting and indicate SYNCH mode 3990 */ 3991 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3992 scsipkt->pkt_comp != NULL) { 3993 scsipkt->pkt_flags |= FLAG_NOINTR; 3994 } 3995 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3996 3997 /* 3998 * len is now the offset to a first mode select page 3999 * Process all pages 4000 */ 4001 while (pllen > 0) { 4002 switch ((int)buf[len]) { 4003 case MODEPAGE_CACHING: 4004 /* No support for SP (saving) */ 4005 if (scsipkt->pkt_cdbp[1] & 0x01) { 4006 *scsipkt->pkt_scbp = STATUS_CHECK; 4007 sense = sata_arq_sense(spx); 4008 sense->es_key = KEY_ILLEGAL_REQUEST; 4009 sense->es_add_code = 4010 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4011 goto done; 4012 } 4013 stat = sata_mode_select_page_8(spx, 4014 (struct mode_cache_scsi3 *)&buf[len], 4015 pllen, &pagelen, &rval, &dmod); 4016 /* 4017 * The pagelen value indicates the number of 4018 * parameter bytes already processed. 4019 * The rval is the return value from 4020 * sata_tran_start(). 4021 * The stat indicates the overall status of 4022 * the operation(s). 4023 */ 4024 if (stat != SATA_SUCCESS) 4025 /* 4026 * Page processing did not succeed - 4027 * all error info is already set-up, 4028 * just return 4029 */ 4030 pllen = 0; /* this breaks the loop */ 4031 else { 4032 len += pagelen; 4033 pllen -= pagelen; 4034 } 4035 break; 4036 4037 case MODEPAGE_INFO_EXCPT: 4038 stat = sata_mode_select_page_1c(spx, 4039 (struct mode_info_excpt_page *)&buf[len], 4040 pllen, &pagelen, &rval, &dmod); 4041 /* 4042 * The pagelen value indicates the number of 4043 * parameter bytes already processed. 4044 * The rval is the return value from 4045 * sata_tran_start(). 4046 * The stat indicates the overall status of 4047 * the operation(s). 4048 */ 4049 if (stat != SATA_SUCCESS) 4050 /* 4051 * Page processing did not succeed - 4052 * all error info is already set-up, 4053 * just return 4054 */ 4055 pllen = 0; /* this breaks the loop */ 4056 else { 4057 len += pagelen; 4058 pllen -= pagelen; 4059 } 4060 break; 4061 4062 case MODEPAGE_ACOUSTIC_MANAG: 4063 stat = sata_mode_select_page_30(spx, 4064 (struct mode_acoustic_management *) 4065 &buf[len], pllen, &pagelen, &rval, &dmod); 4066 /* 4067 * The pagelen value indicates the number of 4068 * parameter bytes already processed. 4069 * The rval is the return value from 4070 * sata_tran_start(). 4071 * The stat indicates the overall status of 4072 * the operation(s). 4073 */ 4074 if (stat != SATA_SUCCESS) 4075 /* 4076 * Page processing did not succeed - 4077 * all error info is already set-up, 4078 * just return 4079 */ 4080 pllen = 0; /* this breaks the loop */ 4081 else { 4082 len += pagelen; 4083 pllen -= pagelen; 4084 } 4085 4086 break; 4087 default: 4088 *scsipkt->pkt_scbp = STATUS_CHECK; 4089 sense = sata_arq_sense(spx); 4090 sense->es_key = KEY_ILLEGAL_REQUEST; 4091 sense->es_add_code = 4092 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4093 goto done; 4094 } 4095 } 4096 } 4097 done: 4098 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4099 /* 4100 * If device parameters were modified, fetch and store the new 4101 * Identify Device data. Since port mutex could have been released 4102 * for accessing HBA driver, we need to re-check device existence. 4103 */ 4104 if (dmod != 0) { 4105 sata_drive_info_t new_sdinfo, *sdinfo; 4106 int rv = 0; 4107 4108 /* 4109 * Following statement has to be changed if this function is 4110 * used for devices other than SATA hard disks. 4111 */ 4112 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4113 4114 new_sdinfo.satadrv_addr = 4115 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4116 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4117 &new_sdinfo); 4118 4119 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4120 /* 4121 * Since port mutex could have been released when 4122 * accessing HBA driver, we need to re-check that the 4123 * framework still holds the device info structure. 4124 */ 4125 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4126 &spx->txlt_sata_pkt->satapkt_device); 4127 if (sdinfo != NULL) { 4128 /* 4129 * Device still has info structure in the 4130 * sata framework. Copy newly fetched info 4131 */ 4132 if (rv == 0) { 4133 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4134 sata_save_drive_settings(sdinfo); 4135 } else { 4136 /* 4137 * Could not fetch new data - invalidate 4138 * sata_drive_info. That makes device 4139 * unusable. 4140 */ 4141 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4142 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4143 } 4144 } 4145 if (rv != 0 || sdinfo == NULL) { 4146 /* 4147 * This changes the overall mode select completion 4148 * reason to a failed one !!!!! 4149 */ 4150 *scsipkt->pkt_scbp = STATUS_CHECK; 4151 sense = sata_arq_sense(spx); 4152 scsipkt->pkt_reason = CMD_INCOMPLETE; 4153 rval = TRAN_ACCEPT; 4154 } 4155 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4156 } 4157 /* Restore the scsi pkt flags */ 4158 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4159 scsipkt->pkt_flags |= nointr_flag; 4160 4161 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4162 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4163 4164 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4165 scsipkt->pkt_comp != NULL) 4166 /* scsi callback required */ 4167 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4168 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4169 TQ_SLEEP) == NULL) 4170 /* Scheduling the callback failed */ 4171 return (TRAN_BUSY); 4172 4173 return (rval); 4174 } 4175 4176 4177 4178 /* 4179 * Translate command: Log Sense 4180 */ 4181 static int 4182 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4183 { 4184 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4185 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4186 sata_drive_info_t *sdinfo; 4187 struct scsi_extended_sense *sense; 4188 int len, count, alc_len; 4189 int pc; /* Page Control code */ 4190 int page_code; /* Page code */ 4191 uint8_t *buf; /* log sense buffer */ 4192 int rval, reason; 4193 #define MAX_LOG_SENSE_PAGE_SIZE 512 4194 4195 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4196 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4197 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4198 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4199 4200 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4201 4202 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4203 4204 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4205 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4206 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4207 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4208 return (rval); 4209 } 4210 /* 4211 * If in interrupt context, reject this packet because it may result 4212 * in issuing a synchronous command to HBA. 4213 */ 4214 if (servicing_interrupt()) { 4215 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4216 "sata_log_sense: rejecting command because " 4217 "of interrupt context\n", NULL); 4218 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4219 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4220 return (TRAN_BUSY); 4221 } 4222 4223 scsipkt->pkt_reason = CMD_CMPLT; 4224 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4225 STATE_SENT_CMD | STATE_GOT_STATUS; 4226 4227 pc = scsipkt->pkt_cdbp[2] >> 6; 4228 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4229 4230 /* Reject not supported request for all but cumulative values */ 4231 switch (pc) { 4232 case PC_CUMULATIVE_VALUES: 4233 break; 4234 default: 4235 *scsipkt->pkt_scbp = STATUS_CHECK; 4236 sense = sata_arq_sense(spx); 4237 sense->es_key = KEY_ILLEGAL_REQUEST; 4238 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4239 goto done; 4240 } 4241 4242 switch (page_code) { 4243 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4244 case PAGE_CODE_SELF_TEST_RESULTS: 4245 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4246 case PAGE_CODE_SMART_READ_DATA: 4247 break; 4248 default: 4249 *scsipkt->pkt_scbp = STATUS_CHECK; 4250 sense = sata_arq_sense(spx); 4251 sense->es_key = KEY_ILLEGAL_REQUEST; 4252 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4253 goto done; 4254 } 4255 4256 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4257 /* 4258 * Because log sense uses local buffers for data retrieval from 4259 * the devices and sets the data programatically in the 4260 * original specified buffer, release preallocated DMA 4261 * resources before storing data in the original buffer, 4262 * so no unwanted DMA sync would take place. 4263 */ 4264 sata_id_t *sata_id; 4265 4266 sata_scsi_dmafree(NULL, scsipkt); 4267 4268 len = 0; 4269 4270 /* Build log parameter header */ 4271 buf[len++] = page_code; /* page code as in the CDB */ 4272 buf[len++] = 0; /* reserved */ 4273 buf[len++] = 0; /* Zero out page length for now (MSB) */ 4274 buf[len++] = 0; /* (LSB) */ 4275 4276 sdinfo = sata_get_device_info( 4277 spx->txlt_sata_hba_inst, 4278 &spx->txlt_sata_pkt->satapkt_device); 4279 4280 /* 4281 * Add requested pages. 4282 */ 4283 switch (page_code) { 4284 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4285 len = sata_build_lsense_page_0(sdinfo, buf + len); 4286 break; 4287 case PAGE_CODE_SELF_TEST_RESULTS: 4288 sata_id = &sdinfo->satadrv_id; 4289 if ((! (sata_id->ai_cmdset84 & 4290 SATA_SMART_SELF_TEST_SUPPORTED)) || 4291 (! (sata_id->ai_features87 & 4292 SATA_SMART_SELF_TEST_SUPPORTED))) { 4293 *scsipkt->pkt_scbp = STATUS_CHECK; 4294 sense = sata_arq_sense(spx); 4295 sense->es_key = KEY_ILLEGAL_REQUEST; 4296 sense->es_add_code = 4297 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4298 4299 goto done; 4300 } 4301 len = sata_build_lsense_page_10(sdinfo, buf + len, 4302 spx->txlt_sata_hba_inst); 4303 break; 4304 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4305 sata_id = &sdinfo->satadrv_id; 4306 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4307 *scsipkt->pkt_scbp = STATUS_CHECK; 4308 sense = sata_arq_sense(spx); 4309 sense->es_key = KEY_ILLEGAL_REQUEST; 4310 sense->es_add_code = 4311 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4312 4313 goto done; 4314 } 4315 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4316 *scsipkt->pkt_scbp = STATUS_CHECK; 4317 sense = sata_arq_sense(spx); 4318 sense->es_key = KEY_ABORTED_COMMAND; 4319 sense->es_add_code = 4320 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4321 sense->es_qual_code = 4322 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4323 4324 goto done; 4325 } 4326 4327 len = sata_build_lsense_page_2f(sdinfo, buf + len, 4328 spx->txlt_sata_hba_inst); 4329 break; 4330 case PAGE_CODE_SMART_READ_DATA: 4331 sata_id = &sdinfo->satadrv_id; 4332 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4333 *scsipkt->pkt_scbp = STATUS_CHECK; 4334 sense = sata_arq_sense(spx); 4335 sense->es_key = KEY_ILLEGAL_REQUEST; 4336 sense->es_add_code = 4337 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4338 4339 goto done; 4340 } 4341 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4342 *scsipkt->pkt_scbp = STATUS_CHECK; 4343 sense = sata_arq_sense(spx); 4344 sense->es_key = KEY_ABORTED_COMMAND; 4345 sense->es_add_code = 4346 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4347 sense->es_qual_code = 4348 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4349 4350 goto done; 4351 } 4352 4353 /* This page doesn't include a page header */ 4354 len = sata_build_lsense_page_30(sdinfo, buf, 4355 spx->txlt_sata_hba_inst); 4356 goto no_header; 4357 default: 4358 /* Invalid request */ 4359 *scsipkt->pkt_scbp = STATUS_CHECK; 4360 sense = sata_arq_sense(spx); 4361 sense->es_key = KEY_ILLEGAL_REQUEST; 4362 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4363 goto done; 4364 } 4365 4366 /* set parameter log sense data length */ 4367 buf[2] = len >> 8; /* log sense length (MSB) */ 4368 buf[3] = len & 0xff; /* log sense length (LSB) */ 4369 4370 len += SCSI_LOG_PAGE_HDR_LEN; 4371 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 4372 4373 no_header: 4374 /* Check allocation length */ 4375 alc_len = scsipkt->pkt_cdbp[7]; 4376 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4377 4378 /* 4379 * We do not check for possible parameters truncation 4380 * (alc_len < len) assuming that the target driver works 4381 * correctly. Just avoiding overrun. 4382 * Copy no more than requested and possible, buffer-wise. 4383 */ 4384 count = MIN(alc_len, len); 4385 count = MIN(bp->b_bcount, count); 4386 bcopy(buf, bp->b_un.b_addr, count); 4387 4388 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4389 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4390 } 4391 *scsipkt->pkt_scbp = STATUS_GOOD; 4392 done: 4393 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4394 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4395 4396 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4397 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4398 4399 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4400 scsipkt->pkt_comp != NULL) 4401 /* scsi callback required */ 4402 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4403 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4404 TQ_SLEEP) == NULL) 4405 /* Scheduling the callback failed */ 4406 return (TRAN_BUSY); 4407 4408 return (TRAN_ACCEPT); 4409 } 4410 4411 /* 4412 * Translate command: Log Select 4413 * Not implemented at this time - returns invalid command response. 4414 */ 4415 static int 4416 sata_txlt_log_select(sata_pkt_txlate_t *spx) 4417 { 4418 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4419 "sata_txlt_log_select\n", NULL); 4420 4421 return (sata_txlt_invalid_command(spx)); 4422 } 4423 4424 4425 /* 4426 * Translate command: Read (various types). 4427 * Translated into appropriate type of ATA READ command 4428 * for SATA hard disks. 4429 * Both the device capabilities and requested operation mode are 4430 * considered. 4431 * 4432 * Following scsi cdb fields are ignored: 4433 * rdprotect, dpo, fua, fua_nv, group_number. 4434 * 4435 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4436 * enable variable sata_func_enable), the capability of the controller and 4437 * capability of a device are checked and if both support queueing, read 4438 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 4439 * command rather than plain READ_XXX command. 4440 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4441 * both the controller and device suport such functionality, the read 4442 * request will be translated to READ_FPDMA_QUEUED command. 4443 * In both cases the maximum queue depth is derived as minimum of: 4444 * HBA capability,device capability and sata_max_queue_depth variable setting. 4445 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4446 * used to pass max queue depth value, and the maximum possible queue depth 4447 * is 32. 4448 * 4449 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4450 * appropriate values in scsi_pkt fields. 4451 */ 4452 static int 4453 sata_txlt_read(sata_pkt_txlate_t *spx) 4454 { 4455 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4456 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4457 sata_drive_info_t *sdinfo; 4458 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4459 int cport = SATA_TXLT_CPORT(spx); 4460 uint16_t sec_count; 4461 uint64_t lba; 4462 int rval, reason; 4463 int synch; 4464 4465 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4466 4467 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4468 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4469 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4470 return (rval); 4471 } 4472 4473 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4474 &spx->txlt_sata_pkt->satapkt_device); 4475 4476 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 4477 /* 4478 * Extract LBA and sector count from scsi CDB. 4479 */ 4480 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4481 case SCMD_READ: 4482 /* 6-byte scsi read cmd : 0x08 */ 4483 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4484 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4485 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4486 sec_count = scsipkt->pkt_cdbp[4]; 4487 /* sec_count 0 will be interpreted as 256 by a device */ 4488 break; 4489 case SCMD_READ_G1: 4490 /* 10-bytes scsi read command : 0x28 */ 4491 lba = scsipkt->pkt_cdbp[2]; 4492 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4493 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4494 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4495 sec_count = scsipkt->pkt_cdbp[7]; 4496 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4497 break; 4498 case SCMD_READ_G5: 4499 /* 12-bytes scsi read command : 0xA8 */ 4500 lba = scsipkt->pkt_cdbp[2]; 4501 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4502 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4503 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4504 sec_count = scsipkt->pkt_cdbp[6]; 4505 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4506 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4507 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4508 break; 4509 case SCMD_READ_G4: 4510 /* 16-bytes scsi read command : 0x88 */ 4511 lba = scsipkt->pkt_cdbp[2]; 4512 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4513 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4514 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4515 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4516 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4517 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4518 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4519 sec_count = scsipkt->pkt_cdbp[10]; 4520 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4521 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4522 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4523 break; 4524 default: 4525 /* Unsupported command */ 4526 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4527 return (sata_txlt_invalid_command(spx)); 4528 } 4529 4530 /* 4531 * Check if specified address exceeds device capacity 4532 */ 4533 if ((lba >= sdinfo->satadrv_capacity) || 4534 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4535 /* LBA out of range */ 4536 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4537 return (sata_txlt_lba_out_of_range(spx)); 4538 } 4539 4540 /* 4541 * For zero-length transfer, emulate good completion of the command 4542 * (reasons for rejecting the command were already checked). 4543 * No DMA resources were allocated. 4544 */ 4545 if (spx->txlt_dma_cookie_list == NULL) { 4546 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4547 return (sata_emul_rw_completion(spx)); 4548 } 4549 4550 /* 4551 * Build cmd block depending on the device capability and 4552 * requested operation mode. 4553 * Do not bother with non-dma mode - we are working only with 4554 * devices supporting DMA. 4555 */ 4556 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4557 scmd->satacmd_device_reg = SATA_ADH_LBA; 4558 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 4559 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4560 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4561 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 4562 scmd->satacmd_sec_count_msb = sec_count >> 8; 4563 #ifndef __lock_lint 4564 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4565 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4566 scmd->satacmd_lba_high_msb = lba >> 40; 4567 #endif 4568 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4569 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4570 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4571 } 4572 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4573 scmd->satacmd_lba_low_lsb = lba & 0xff; 4574 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4575 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4576 scmd->satacmd_features_reg = 0; 4577 scmd->satacmd_status_reg = 0; 4578 scmd->satacmd_error_reg = 0; 4579 4580 /* 4581 * Check if queueing commands should be used and switch 4582 * to appropriate command if possible 4583 */ 4584 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4585 boolean_t using_queuing; 4586 4587 /* Queuing supported by controller and device? */ 4588 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4589 (sdinfo->satadrv_features_support & 4590 SATA_DEV_F_NCQ) && 4591 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4592 SATA_CTLF_NCQ)) { 4593 using_queuing = B_TRUE; 4594 4595 /* NCQ supported - use FPDMA READ */ 4596 scmd->satacmd_cmd_reg = 4597 SATAC_READ_FPDMA_QUEUED; 4598 scmd->satacmd_features_reg_ext = 4599 scmd->satacmd_sec_count_msb; 4600 scmd->satacmd_sec_count_msb = 0; 4601 } else if ((sdinfo->satadrv_features_support & 4602 SATA_DEV_F_TCQ) && 4603 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4604 SATA_CTLF_QCMD)) { 4605 using_queuing = B_TRUE; 4606 4607 /* Legacy queueing */ 4608 if (sdinfo->satadrv_features_support & 4609 SATA_DEV_F_LBA48) { 4610 scmd->satacmd_cmd_reg = 4611 SATAC_READ_DMA_QUEUED_EXT; 4612 scmd->satacmd_features_reg_ext = 4613 scmd->satacmd_sec_count_msb; 4614 scmd->satacmd_sec_count_msb = 0; 4615 } else { 4616 scmd->satacmd_cmd_reg = 4617 SATAC_READ_DMA_QUEUED; 4618 } 4619 } else /* NCQ nor legacy queuing not supported */ 4620 using_queuing = B_FALSE; 4621 4622 /* 4623 * If queuing, the sector count goes in the features register 4624 * and the secount count will contain the tag. 4625 */ 4626 if (using_queuing) { 4627 scmd->satacmd_features_reg = 4628 scmd->satacmd_sec_count_lsb; 4629 scmd->satacmd_sec_count_lsb = 0; 4630 scmd->satacmd_flags.sata_queued = B_TRUE; 4631 4632 /* Set-up maximum queue depth */ 4633 scmd->satacmd_flags.sata_max_queue_depth = 4634 sdinfo->satadrv_max_queue_depth - 1; 4635 } else if (sdinfo->satadrv_features_enabled & 4636 SATA_DEV_F_E_UNTAGGED_QING) { 4637 /* 4638 * Although NCQ/TCQ is not enabled, untagged queuing 4639 * may be still used. 4640 * Set-up the maximum untagged queue depth. 4641 * Use controller's queue depth from sata_hba_tran. 4642 * SATA HBA drivers may ignore this value and rely on 4643 * the internal limits.For drivers that do not 4644 * ignore untaged queue depth, limit the value to 4645 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4646 * largest value that can be passed via 4647 * satacmd_flags.sata_max_queue_depth. 4648 */ 4649 scmd->satacmd_flags.sata_max_queue_depth = 4650 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4651 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4652 4653 } else { 4654 scmd->satacmd_flags.sata_max_queue_depth = 0; 4655 } 4656 } else 4657 scmd->satacmd_flags.sata_max_queue_depth = 0; 4658 4659 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 4660 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 4661 scmd->satacmd_cmd_reg, lba, sec_count); 4662 4663 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4664 /* Need callback function */ 4665 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4666 synch = FALSE; 4667 } else 4668 synch = TRUE; 4669 4670 /* Transfer command to HBA */ 4671 if (sata_hba_start(spx, &rval) != 0) { 4672 /* Pkt not accepted for execution */ 4673 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4674 return (rval); 4675 } 4676 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4677 /* 4678 * If execution is non-synchronous, 4679 * a callback function will handle potential errors, translate 4680 * the response and will do a callback to a target driver. 4681 * If it was synchronous, check execution status using the same 4682 * framework callback. 4683 */ 4684 if (synch) { 4685 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4686 "synchronous execution status %x\n", 4687 spx->txlt_sata_pkt->satapkt_reason); 4688 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4689 } 4690 return (TRAN_ACCEPT); 4691 } 4692 4693 4694 /* 4695 * SATA translate command: Write (various types) 4696 * Translated into appropriate type of ATA WRITE command 4697 * for SATA hard disks. 4698 * Both the device capabilities and requested operation mode are 4699 * considered. 4700 * 4701 * Following scsi cdb fields are ignored: 4702 * rwprotect, dpo, fua, fua_nv, group_number. 4703 * 4704 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4705 * enable variable sata_func_enable), the capability of the controller and 4706 * capability of a device are checked and if both support queueing, write 4707 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 4708 * command rather than plain WRITE_XXX command. 4709 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4710 * both the controller and device suport such functionality, the write 4711 * request will be translated to WRITE_FPDMA_QUEUED command. 4712 * In both cases the maximum queue depth is derived as minimum of: 4713 * HBA capability,device capability and sata_max_queue_depth variable setting. 4714 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4715 * used to pass max queue depth value, and the maximum possible queue depth 4716 * is 32. 4717 * 4718 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4719 * appropriate values in scsi_pkt fields. 4720 */ 4721 static int 4722 sata_txlt_write(sata_pkt_txlate_t *spx) 4723 { 4724 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4725 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4726 sata_drive_info_t *sdinfo; 4727 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4728 int cport = SATA_TXLT_CPORT(spx); 4729 uint16_t sec_count; 4730 uint64_t lba; 4731 int rval, reason; 4732 int synch; 4733 4734 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4735 4736 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4737 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4738 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4739 return (rval); 4740 } 4741 4742 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4743 &spx->txlt_sata_pkt->satapkt_device); 4744 4745 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4746 /* 4747 * Extract LBA and sector count from scsi CDB 4748 */ 4749 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4750 case SCMD_WRITE: 4751 /* 6-byte scsi read cmd : 0x0A */ 4752 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4753 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4754 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4755 sec_count = scsipkt->pkt_cdbp[4]; 4756 /* sec_count 0 will be interpreted as 256 by a device */ 4757 break; 4758 case SCMD_WRITE_G1: 4759 /* 10-bytes scsi write command : 0x2A */ 4760 lba = scsipkt->pkt_cdbp[2]; 4761 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4762 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4763 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4764 sec_count = scsipkt->pkt_cdbp[7]; 4765 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4766 break; 4767 case SCMD_WRITE_G5: 4768 /* 12-bytes scsi read command : 0xAA */ 4769 lba = scsipkt->pkt_cdbp[2]; 4770 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4771 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4772 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4773 sec_count = scsipkt->pkt_cdbp[6]; 4774 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4775 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4776 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4777 break; 4778 case SCMD_WRITE_G4: 4779 /* 16-bytes scsi write command : 0x8A */ 4780 lba = scsipkt->pkt_cdbp[2]; 4781 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4782 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4783 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4784 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4785 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4786 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4787 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4788 sec_count = scsipkt->pkt_cdbp[10]; 4789 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4790 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4791 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4792 break; 4793 default: 4794 /* Unsupported command */ 4795 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4796 return (sata_txlt_invalid_command(spx)); 4797 } 4798 4799 /* 4800 * Check if specified address and length exceeds device capacity 4801 */ 4802 if ((lba >= sdinfo->satadrv_capacity) || 4803 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4804 /* LBA out of range */ 4805 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4806 return (sata_txlt_lba_out_of_range(spx)); 4807 } 4808 4809 /* 4810 * For zero-length transfer, emulate good completion of the command 4811 * (reasons for rejecting the command were already checked). 4812 * No DMA resources were allocated. 4813 */ 4814 if (spx->txlt_dma_cookie_list == NULL) { 4815 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4816 return (sata_emul_rw_completion(spx)); 4817 } 4818 4819 /* 4820 * Build cmd block depending on the device capability and 4821 * requested operation mode. 4822 * Do not bother with non-dma mode- we are working only with 4823 * devices supporting DMA. 4824 */ 4825 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4826 scmd->satacmd_device_reg = SATA_ADH_LBA; 4827 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 4828 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4829 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4830 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 4831 scmd->satacmd_sec_count_msb = sec_count >> 8; 4832 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4833 #ifndef __lock_lint 4834 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4835 scmd->satacmd_lba_high_msb = lba >> 40; 4836 #endif 4837 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4838 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4839 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4840 } 4841 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4842 scmd->satacmd_lba_low_lsb = lba & 0xff; 4843 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4844 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4845 scmd->satacmd_features_reg = 0; 4846 scmd->satacmd_status_reg = 0; 4847 scmd->satacmd_error_reg = 0; 4848 4849 /* 4850 * Check if queueing commands should be used and switch 4851 * to appropriate command if possible 4852 */ 4853 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4854 boolean_t using_queuing; 4855 4856 /* Queuing supported by controller and device? */ 4857 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4858 (sdinfo->satadrv_features_support & 4859 SATA_DEV_F_NCQ) && 4860 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4861 SATA_CTLF_NCQ)) { 4862 using_queuing = B_TRUE; 4863 4864 /* NCQ supported - use FPDMA WRITE */ 4865 scmd->satacmd_cmd_reg = 4866 SATAC_WRITE_FPDMA_QUEUED; 4867 scmd->satacmd_features_reg_ext = 4868 scmd->satacmd_sec_count_msb; 4869 scmd->satacmd_sec_count_msb = 0; 4870 } else if ((sdinfo->satadrv_features_support & 4871 SATA_DEV_F_TCQ) && 4872 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4873 SATA_CTLF_QCMD)) { 4874 using_queuing = B_TRUE; 4875 4876 /* Legacy queueing */ 4877 if (sdinfo->satadrv_features_support & 4878 SATA_DEV_F_LBA48) { 4879 scmd->satacmd_cmd_reg = 4880 SATAC_WRITE_DMA_QUEUED_EXT; 4881 scmd->satacmd_features_reg_ext = 4882 scmd->satacmd_sec_count_msb; 4883 scmd->satacmd_sec_count_msb = 0; 4884 } else { 4885 scmd->satacmd_cmd_reg = 4886 SATAC_WRITE_DMA_QUEUED; 4887 } 4888 } else /* NCQ nor legacy queuing not supported */ 4889 using_queuing = B_FALSE; 4890 4891 if (using_queuing) { 4892 scmd->satacmd_features_reg = 4893 scmd->satacmd_sec_count_lsb; 4894 scmd->satacmd_sec_count_lsb = 0; 4895 scmd->satacmd_flags.sata_queued = B_TRUE; 4896 /* Set-up maximum queue depth */ 4897 scmd->satacmd_flags.sata_max_queue_depth = 4898 sdinfo->satadrv_max_queue_depth - 1; 4899 } else if (sdinfo->satadrv_features_enabled & 4900 SATA_DEV_F_E_UNTAGGED_QING) { 4901 /* 4902 * Although NCQ/TCQ is not enabled, untagged queuing 4903 * may be still used. 4904 * Set-up the maximum untagged queue depth. 4905 * Use controller's queue depth from sata_hba_tran. 4906 * SATA HBA drivers may ignore this value and rely on 4907 * the internal limits. For drivera that do not 4908 * ignore untaged queue depth, limit the value to 4909 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4910 * largest value that can be passed via 4911 * satacmd_flags.sata_max_queue_depth. 4912 */ 4913 scmd->satacmd_flags.sata_max_queue_depth = 4914 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4915 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4916 4917 } else { 4918 scmd->satacmd_flags.sata_max_queue_depth = 0; 4919 } 4920 } else 4921 scmd->satacmd_flags.sata_max_queue_depth = 0; 4922 4923 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4924 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 4925 scmd->satacmd_cmd_reg, lba, sec_count); 4926 4927 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4928 /* Need callback function */ 4929 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4930 synch = FALSE; 4931 } else 4932 synch = TRUE; 4933 4934 /* Transfer command to HBA */ 4935 if (sata_hba_start(spx, &rval) != 0) { 4936 /* Pkt not accepted for execution */ 4937 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4938 return (rval); 4939 } 4940 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4941 4942 /* 4943 * If execution is non-synchronous, 4944 * a callback function will handle potential errors, translate 4945 * the response and will do a callback to a target driver. 4946 * If it was synchronous, check execution status using the same 4947 * framework callback. 4948 */ 4949 if (synch) { 4950 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4951 "synchronous execution status %x\n", 4952 spx->txlt_sata_pkt->satapkt_reason); 4953 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4954 } 4955 return (TRAN_ACCEPT); 4956 } 4957 4958 4959 /* 4960 * Implements SCSI SBC WRITE BUFFER command download microcode option 4961 */ 4962 static int 4963 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 4964 { 4965 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 4966 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 4967 4968 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 4969 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4970 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 4971 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4972 4973 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4974 struct scsi_extended_sense *sense; 4975 int rval, mode, sector_count, reason; 4976 int cport = SATA_TXLT_CPORT(spx); 4977 4978 mode = scsipkt->pkt_cdbp[1] & 0x1f; 4979 4980 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4981 "sata_txlt_write_buffer, mode 0x%x\n", mode); 4982 4983 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4984 4985 if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) { 4986 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4987 return (rval); 4988 } 4989 /* 4990 * If in interrupt context, reject this packet because it would issue 4991 * a synchronous command to HBA. 4992 */ 4993 if (servicing_interrupt()) { 4994 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4995 "sata_txlt_write_buffer: rejecting command because " 4996 "of interrupt context\n", NULL); 4997 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4998 return (TRAN_BUSY); 4999 } 5000 5001 /* Use synchronous mode */ 5002 spx->txlt_sata_pkt->satapkt_op_mode 5003 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 5004 5005 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5006 5007 scsipkt->pkt_reason = CMD_CMPLT; 5008 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5009 STATE_SENT_CMD | STATE_GOT_STATUS; 5010 5011 /* 5012 * The SCSI to ATA translation specification only calls 5013 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5014 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5015 * ATA 8 (draft) got rid of download microcode for temp 5016 * and it is even optional for ATA 7, so it may be aborted. 5017 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5018 * it is not specified and the buffer offset for SCSI is a 16-bit 5019 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5020 * sectors. Thus the offset really doesn't buy us anything. 5021 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5022 * is revised, this can be revisisted. 5023 */ 5024 /* Reject not supported request */ 5025 switch (mode) { 5026 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5027 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5028 break; 5029 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5030 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5031 break; 5032 default: 5033 goto bad_param; 5034 } 5035 5036 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5037 5038 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5039 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5040 goto bad_param; 5041 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5042 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5043 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5044 scmd->satacmd_lba_mid_lsb = 0; 5045 scmd->satacmd_lba_high_lsb = 0; 5046 scmd->satacmd_device_reg = 0; 5047 spx->txlt_sata_pkt->satapkt_comp = NULL; 5048 scmd->satacmd_addr_type = 0; 5049 5050 /* Transfer command to HBA */ 5051 if (sata_hba_start(spx, &rval) != 0) { 5052 /* Pkt not accepted for execution */ 5053 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5054 return (rval); 5055 } 5056 5057 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5058 5059 /* Then we need synchronous check the status of the disk */ 5060 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5061 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5062 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5063 scsipkt->pkt_reason = CMD_CMPLT; 5064 5065 /* Download commmand succeed, so probe and identify device */ 5066 sata_reidentify_device(spx); 5067 } else { 5068 /* Something went wrong, microcode download command failed */ 5069 scsipkt->pkt_reason = CMD_INCOMPLETE; 5070 *scsipkt->pkt_scbp = STATUS_CHECK; 5071 sense = sata_arq_sense(spx); 5072 switch (sata_pkt->satapkt_reason) { 5073 case SATA_PKT_PORT_ERROR: 5074 /* 5075 * We have no device data. Assume no data transfered. 5076 */ 5077 sense->es_key = KEY_HARDWARE_ERROR; 5078 break; 5079 5080 case SATA_PKT_DEV_ERROR: 5081 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5082 SATA_STATUS_ERR) { 5083 /* 5084 * determine dev error reason from error 5085 * reg content 5086 */ 5087 sata_decode_device_error(spx, sense); 5088 break; 5089 } 5090 /* No extended sense key - no info available */ 5091 break; 5092 5093 case SATA_PKT_TIMEOUT: 5094 scsipkt->pkt_reason = CMD_TIMEOUT; 5095 scsipkt->pkt_statistics |= 5096 STAT_TIMEOUT | STAT_DEV_RESET; 5097 /* No extended sense key ? */ 5098 break; 5099 5100 case SATA_PKT_ABORTED: 5101 scsipkt->pkt_reason = CMD_ABORTED; 5102 scsipkt->pkt_statistics |= STAT_ABORTED; 5103 /* No extended sense key ? */ 5104 break; 5105 5106 case SATA_PKT_RESET: 5107 /* pkt aborted by an explicit reset from a host */ 5108 scsipkt->pkt_reason = CMD_RESET; 5109 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5110 break; 5111 5112 default: 5113 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5114 "sata_txlt_nodata_cmd_completion: " 5115 "invalid packet completion reason %d", 5116 sata_pkt->satapkt_reason)); 5117 scsipkt->pkt_reason = CMD_TRAN_ERR; 5118 break; 5119 } 5120 5121 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5122 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5123 5124 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5125 scsipkt->pkt_comp != NULL) 5126 /* scsi callback required */ 5127 (*scsipkt->pkt_comp)(scsipkt); 5128 } 5129 return (TRAN_ACCEPT); 5130 5131 bad_param: 5132 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5133 *scsipkt->pkt_scbp = STATUS_CHECK; 5134 sense = sata_arq_sense(spx); 5135 sense->es_key = KEY_ILLEGAL_REQUEST; 5136 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5137 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5138 scsipkt->pkt_comp != NULL) { 5139 /* scsi callback required */ 5140 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5141 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5142 TQ_SLEEP) == 0) { 5143 /* Scheduling the callback failed */ 5144 rval = TRAN_BUSY; 5145 } 5146 } 5147 return (rval); 5148 } 5149 5150 /* 5151 * Re-identify device after doing a firmware download. 5152 */ 5153 static void 5154 sata_reidentify_device(sata_pkt_txlate_t *spx) 5155 { 5156 #define DOWNLOAD_WAIT_TIME_SECS 60 5157 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5158 int rval; 5159 int retry_cnt; 5160 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5161 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5162 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5163 sata_drive_info_t *sdinfo; 5164 5165 /* 5166 * Before returning good status, probe device. 5167 * Device probing will get IDENTIFY DEVICE data, if possible. 5168 * The assumption is that the new microcode is applied by the 5169 * device. It is a caller responsibility to verify this. 5170 */ 5171 for (retry_cnt = 0; 5172 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5173 retry_cnt++) { 5174 rval = sata_probe_device(sata_hba_inst, &sata_device); 5175 5176 if (rval == SATA_SUCCESS) { /* Set default features */ 5177 sdinfo = sata_get_device_info(sata_hba_inst, 5178 &sata_device); 5179 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5180 SATA_SUCCESS) { 5181 /* retry */ 5182 rval = sata_initialize_device(sata_hba_inst, 5183 sdinfo); 5184 if (rval == SATA_RETRY) 5185 sata_log(sata_hba_inst, CE_WARN, 5186 "SATA device at port %d pmport %d -" 5187 " default device features could not" 5188 " be set. Device may not operate " 5189 "as expected.", 5190 sata_device.satadev_addr.cport, 5191 sata_device.satadev_addr.pmport); 5192 } 5193 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5194 scsipkt->pkt_comp != NULL) 5195 (*scsipkt->pkt_comp)(scsipkt); 5196 return; 5197 } else if (rval == SATA_RETRY) { 5198 delay(drv_usectohz(1000000 * 5199 DOWNLOAD_WAIT_INTERVAL_SECS)); 5200 continue; 5201 } else /* failed - no reason to retry */ 5202 break; 5203 } 5204 5205 /* 5206 * Something went wrong, device probing failed. 5207 */ 5208 SATA_LOG_D((sata_hba_inst, CE_WARN, 5209 "Cannot probe device after downloading microcode\n")); 5210 5211 /* Reset device to force retrying the probe. */ 5212 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5213 (SATA_DIP(sata_hba_inst), &sata_device); 5214 5215 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5216 scsipkt->pkt_comp != NULL) 5217 (*scsipkt->pkt_comp)(scsipkt); 5218 } 5219 5220 5221 /* 5222 * Translate command: Synchronize Cache. 5223 * Translates into Flush Cache command for SATA hard disks. 5224 * 5225 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5226 * appropriate values in scsi_pkt fields. 5227 */ 5228 static int 5229 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5230 { 5231 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5232 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5233 int cport = SATA_TXLT_CPORT(spx); 5234 int rval, reason; 5235 int synch; 5236 5237 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5238 5239 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5240 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5241 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5242 return (rval); 5243 } 5244 5245 scmd->satacmd_addr_type = 0; 5246 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 5247 scmd->satacmd_device_reg = 0; 5248 scmd->satacmd_sec_count_lsb = 0; 5249 scmd->satacmd_lba_low_lsb = 0; 5250 scmd->satacmd_lba_mid_lsb = 0; 5251 scmd->satacmd_lba_high_lsb = 0; 5252 scmd->satacmd_features_reg = 0; 5253 scmd->satacmd_status_reg = 0; 5254 scmd->satacmd_error_reg = 0; 5255 5256 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5257 "sata_txlt_synchronize_cache\n", NULL); 5258 5259 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5260 /* Need to set-up a callback function */ 5261 spx->txlt_sata_pkt->satapkt_comp = 5262 sata_txlt_nodata_cmd_completion; 5263 synch = FALSE; 5264 } else 5265 synch = TRUE; 5266 5267 /* Transfer command to HBA */ 5268 if (sata_hba_start(spx, &rval) != 0) { 5269 /* Pkt not accepted for execution */ 5270 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5271 return (rval); 5272 } 5273 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5274 5275 /* 5276 * If execution non-synchronous, it had to be completed 5277 * a callback function will handle potential errors, translate 5278 * the response and will do a callback to a target driver. 5279 * If it was synchronous, check status, using the same 5280 * framework callback. 5281 */ 5282 if (synch) { 5283 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5284 "synchronous execution status %x\n", 5285 spx->txlt_sata_pkt->satapkt_reason); 5286 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 5287 } 5288 return (TRAN_ACCEPT); 5289 } 5290 5291 5292 /* 5293 * Send pkt to SATA HBA driver 5294 * 5295 * This function may be called only if the operation is requested by scsi_pkt, 5296 * i.e. scsi_pkt is not NULL. 5297 * 5298 * This function has to be called with cport mutex held. It does release 5299 * the mutex when it calls HBA driver sata_tran_start function and 5300 * re-acquires it afterwards. 5301 * 5302 * If return value is 0, pkt was accepted, -1 otherwise 5303 * rval is set to appropriate sata_scsi_start return value. 5304 * 5305 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 5306 * have called the sata_pkt callback function for this packet. 5307 * 5308 * The scsi callback has to be performed by the caller of this routine. 5309 * 5310 * Note 2: No port multiplier support for now. 5311 */ 5312 static int 5313 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 5314 { 5315 int stat, cport; 5316 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5317 sata_drive_info_t *sdinfo; 5318 sata_device_t *sata_device; 5319 uint8_t cmd; 5320 struct sata_cmd_flags cmd_flags; 5321 5322 ASSERT(spx->txlt_sata_pkt != NULL); 5323 5324 cport = SATA_TXLT_CPORT(spx); 5325 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5326 5327 sdinfo = sata_get_device_info(sata_hba_inst, 5328 &spx->txlt_sata_pkt->satapkt_device); 5329 ASSERT(sdinfo != NULL); 5330 5331 /* Clear device reset state? */ 5332 if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 5333 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 5334 sata_clear_dev_reset = B_TRUE; 5335 sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET; 5336 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5337 "sata_hba_start: clearing device reset state\n", NULL); 5338 } 5339 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 5340 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 5341 sata_device = &spx->txlt_sata_pkt->satapkt_device; 5342 5343 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5344 5345 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5346 "Sata cmd 0x%2x\n", cmd); 5347 5348 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 5349 spx->txlt_sata_pkt); 5350 5351 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5352 /* 5353 * If sata pkt was accepted and executed in asynchronous mode, i.e. 5354 * with the sata callback, the sata_pkt could be already destroyed 5355 * by the time we check ther return status from the hba_start() 5356 * function, because sata_scsi_destroy_pkt() could have been already 5357 * called (perhaps in the interrupt context). So, in such case, there 5358 * should be no references to it. In other cases, sata_pkt still 5359 * exists. 5360 */ 5361 if (stat == SATA_TRAN_ACCEPTED) { 5362 /* 5363 * pkt accepted for execution. 5364 * If it was executed synchronously, it is already completed 5365 * and pkt completion_reason indicates completion status. 5366 */ 5367 *rval = TRAN_ACCEPT; 5368 return (0); 5369 } 5370 5371 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5372 switch (stat) { 5373 case SATA_TRAN_QUEUE_FULL: 5374 /* 5375 * Controller detected queue full condition. 5376 */ 5377 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 5378 "sata_hba_start: queue full\n", NULL); 5379 5380 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5381 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 5382 5383 *rval = TRAN_BUSY; 5384 break; 5385 5386 case SATA_TRAN_PORT_ERROR: 5387 /* 5388 * Communication/link with device or general port error 5389 * detected before pkt execution begun. 5390 */ 5391 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5392 SATA_ADDR_CPORT || 5393 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5394 SATA_ADDR_DCPORT) 5395 sata_log(sata_hba_inst, CE_CONT, 5396 "SATA port %d error", 5397 sata_device->satadev_addr.cport); 5398 else 5399 sata_log(sata_hba_inst, CE_CONT, 5400 "SATA port %d pmport %d error\n", 5401 sata_device->satadev_addr.cport, 5402 sata_device->satadev_addr.pmport); 5403 5404 /* 5405 * Update the port/device structure. 5406 * sata_pkt should be still valid. Since port error is 5407 * returned, sata_device content should reflect port 5408 * state - it means, that sata address have been changed, 5409 * because original packet's sata address refered to a device 5410 * attached to some port. 5411 */ 5412 sata_update_port_info(sata_hba_inst, sata_device); 5413 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5414 *rval = TRAN_FATAL_ERROR; 5415 break; 5416 5417 case SATA_TRAN_CMD_UNSUPPORTED: 5418 /* 5419 * Command rejected by HBA as unsupported. It was HBA driver 5420 * that rejected the command, command was not sent to 5421 * an attached device. 5422 */ 5423 if ((sdinfo != NULL) && 5424 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 5425 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5426 "sat_hba_start: cmd 0x%2x rejected " 5427 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 5428 5429 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5430 (void) sata_txlt_invalid_command(spx); 5431 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5432 5433 *rval = TRAN_ACCEPT; 5434 break; 5435 5436 case SATA_TRAN_BUSY: 5437 /* 5438 * Command rejected by HBA because other operation prevents 5439 * accepting the packet, or device is in RESET condition. 5440 */ 5441 if (sdinfo != NULL) { 5442 sdinfo->satadrv_state = 5443 spx->txlt_sata_pkt->satapkt_device.satadev_state; 5444 5445 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 5446 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5447 "sata_hba_start: cmd 0x%2x rejected " 5448 "because of device reset condition\n", 5449 cmd); 5450 } else { 5451 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5452 "sata_hba_start: cmd 0x%2x rejected " 5453 "with SATA_TRAN_BUSY status\n", 5454 cmd); 5455 } 5456 } 5457 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5458 *rval = TRAN_BUSY; 5459 break; 5460 5461 default: 5462 /* Unrecognized HBA response */ 5463 SATA_LOG_D((sata_hba_inst, CE_WARN, 5464 "sata_hba_start: unrecognized HBA response " 5465 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 5466 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5467 *rval = TRAN_FATAL_ERROR; 5468 break; 5469 } 5470 5471 /* 5472 * If we got here, the packet was rejected. 5473 * Check if we need to remember reset state clearing request 5474 */ 5475 if (cmd_flags.sata_clear_dev_reset) { 5476 /* 5477 * Check if device is still configured - it may have 5478 * disapeared from the configuration 5479 */ 5480 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5481 if (sdinfo != NULL) { 5482 /* 5483 * Restore the flag that requests clearing of 5484 * the device reset state, 5485 * so the next sata packet may carry it to HBA. 5486 */ 5487 sdinfo->satadrv_event_flags |= 5488 SATA_EVNT_CLEAR_DEVICE_RESET; 5489 } 5490 } 5491 return (-1); 5492 } 5493 5494 /* 5495 * Scsi response setup for invalid LBA 5496 * 5497 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 5498 */ 5499 static int 5500 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 5501 { 5502 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5503 struct scsi_extended_sense *sense; 5504 5505 scsipkt->pkt_reason = CMD_CMPLT; 5506 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5507 STATE_SENT_CMD | STATE_GOT_STATUS; 5508 *scsipkt->pkt_scbp = STATUS_CHECK; 5509 5510 *scsipkt->pkt_scbp = STATUS_CHECK; 5511 sense = sata_arq_sense(spx); 5512 sense->es_key = KEY_ILLEGAL_REQUEST; 5513 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 5514 5515 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5516 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5517 5518 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5519 scsipkt->pkt_comp != NULL) 5520 /* scsi callback required */ 5521 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5522 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5523 TQ_SLEEP) == NULL) 5524 /* Scheduling the callback failed */ 5525 return (TRAN_BUSY); 5526 return (TRAN_ACCEPT); 5527 } 5528 5529 5530 /* 5531 * Analyze device status and error registers and translate them into 5532 * appropriate scsi sense codes. 5533 * NOTE: non-packet commands only for now 5534 */ 5535 static void 5536 sata_decode_device_error(sata_pkt_txlate_t *spx, 5537 struct scsi_extended_sense *sense) 5538 { 5539 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 5540 5541 ASSERT(sense != NULL); 5542 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 5543 SATA_STATUS_ERR); 5544 5545 5546 if (err_reg & SATA_ERROR_ICRC) { 5547 sense->es_key = KEY_ABORTED_COMMAND; 5548 sense->es_add_code = 0x08; /* Communication failure */ 5549 return; 5550 } 5551 5552 if (err_reg & SATA_ERROR_UNC) { 5553 sense->es_key = KEY_MEDIUM_ERROR; 5554 /* Information bytes (LBA) need to be set by a caller */ 5555 return; 5556 } 5557 5558 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 5559 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 5560 sense->es_key = KEY_UNIT_ATTENTION; 5561 sense->es_add_code = 0x3a; /* No media present */ 5562 return; 5563 } 5564 5565 if (err_reg & SATA_ERROR_IDNF) { 5566 if (err_reg & SATA_ERROR_ABORT) { 5567 sense->es_key = KEY_ABORTED_COMMAND; 5568 } else { 5569 sense->es_key = KEY_ILLEGAL_REQUEST; 5570 sense->es_add_code = 0x21; /* LBA out of range */ 5571 } 5572 return; 5573 } 5574 5575 if (err_reg & SATA_ERROR_ABORT) { 5576 ASSERT(spx->txlt_sata_pkt != NULL); 5577 sense->es_key = KEY_ABORTED_COMMAND; 5578 return; 5579 } 5580 } 5581 5582 /* 5583 * Extract error LBA from sata_pkt.satapkt_cmd register fields 5584 */ 5585 static void 5586 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 5587 { 5588 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 5589 5590 *lba = 0; 5591 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 5592 *lba = sata_cmd->satacmd_lba_high_msb; 5593 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 5594 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 5595 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 5596 *lba = sata_cmd->satacmd_device_reg & 0xf; 5597 } 5598 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 5599 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 5600 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 5601 } 5602 5603 /* 5604 * This is fixed sense format - if LBA exceeds the info field size, 5605 * no valid info will be returned (valid bit in extended sense will 5606 * be set to 0). 5607 */ 5608 static struct scsi_extended_sense * 5609 sata_arq_sense(sata_pkt_txlate_t *spx) 5610 { 5611 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5612 struct scsi_arq_status *arqs; 5613 struct scsi_extended_sense *sense; 5614 5615 /* Fill ARQ sense data */ 5616 scsipkt->pkt_state |= STATE_ARQ_DONE; 5617 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 5618 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 5619 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 5620 arqs->sts_rqpkt_reason = CMD_CMPLT; 5621 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5622 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 5623 arqs->sts_rqpkt_resid = 0; 5624 sense = &arqs->sts_sensedata; 5625 bzero(sense, sizeof (struct scsi_extended_sense)); 5626 sata_fixed_sense_data_preset(sense); 5627 return (sense); 5628 } 5629 5630 5631 /* 5632 * Emulated SATA Read/Write command completion for zero-length requests. 5633 * This request always succedes, so in synchronous mode it always returns 5634 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 5635 * callback cannot be scheduled. 5636 */ 5637 static int 5638 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 5639 { 5640 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5641 5642 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5643 STATE_SENT_CMD | STATE_GOT_STATUS; 5644 scsipkt->pkt_reason = CMD_CMPLT; 5645 *scsipkt->pkt_scbp = STATUS_GOOD; 5646 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5647 /* scsi callback required - have to schedule it */ 5648 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5649 (task_func_t *)scsipkt->pkt_comp, 5650 (void *)scsipkt, TQ_SLEEP) == NULL) 5651 /* Scheduling the callback failed */ 5652 return (TRAN_BUSY); 5653 } 5654 return (TRAN_ACCEPT); 5655 } 5656 5657 5658 /* 5659 * Translate completion status of SATA read/write commands into scsi response. 5660 * pkt completion_reason is checked to determine the completion status. 5661 * Do scsi callback if necessary. 5662 * 5663 * Note: this function may be called also for synchronously executed 5664 * commands. 5665 * This function may be used only if scsi_pkt is non-NULL. 5666 */ 5667 static void 5668 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 5669 { 5670 sata_pkt_txlate_t *spx = 5671 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5672 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 5673 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5674 struct scsi_extended_sense *sense; 5675 uint64_t lba; 5676 struct buf *bp; 5677 int rval; 5678 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5679 /* Normal completion */ 5680 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5681 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5682 scsipkt->pkt_reason = CMD_CMPLT; 5683 *scsipkt->pkt_scbp = STATUS_GOOD; 5684 if (spx->txlt_tmp_buf != NULL) { 5685 /* Temporary buffer was used */ 5686 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5687 if (bp->b_flags & B_READ) { 5688 rval = ddi_dma_sync( 5689 spx->txlt_buf_dma_handle, 0, 0, 5690 DDI_DMA_SYNC_FORCPU); 5691 ASSERT(rval == DDI_SUCCESS); 5692 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 5693 bp->b_bcount); 5694 } 5695 } 5696 } else { 5697 /* 5698 * Something went wrong - analyze return 5699 */ 5700 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5701 STATE_SENT_CMD | STATE_GOT_STATUS; 5702 scsipkt->pkt_reason = CMD_INCOMPLETE; 5703 *scsipkt->pkt_scbp = STATUS_CHECK; 5704 sense = sata_arq_sense(spx); 5705 ASSERT(sense != NULL); 5706 5707 /* 5708 * SATA_PKT_DEV_ERROR is the only case where we may be able to 5709 * extract from device registers the failing LBA. 5710 */ 5711 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 5712 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 5713 (scmd->satacmd_lba_mid_msb != 0 || 5714 scmd->satacmd_lba_high_msb != 0)) { 5715 /* 5716 * We have problem reporting this cmd LBA 5717 * in fixed sense data format, because of 5718 * the size of the scsi LBA fields. 5719 */ 5720 sense->es_valid = 0; 5721 } else { 5722 sata_extract_error_lba(spx, &lba); 5723 sense->es_info_1 = (lba & 0xFF000000) >> 24; 5724 sense->es_info_2 = (lba & 0xFF0000) >> 16; 5725 sense->es_info_3 = (lba & 0xFF00) >> 8; 5726 sense->es_info_4 = lba & 0xFF; 5727 } 5728 } else { 5729 /* Invalid extended sense info */ 5730 sense->es_valid = 0; 5731 } 5732 5733 switch (sata_pkt->satapkt_reason) { 5734 case SATA_PKT_PORT_ERROR: 5735 /* We may want to handle DEV GONE state as well */ 5736 /* 5737 * We have no device data. Assume no data transfered. 5738 */ 5739 sense->es_key = KEY_HARDWARE_ERROR; 5740 break; 5741 5742 case SATA_PKT_DEV_ERROR: 5743 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5744 SATA_STATUS_ERR) { 5745 /* 5746 * determine dev error reason from error 5747 * reg content 5748 */ 5749 sata_decode_device_error(spx, sense); 5750 if (sense->es_key == KEY_MEDIUM_ERROR) { 5751 switch (scmd->satacmd_cmd_reg) { 5752 case SATAC_READ_DMA: 5753 case SATAC_READ_DMA_EXT: 5754 case SATAC_READ_DMA_QUEUED: 5755 case SATAC_READ_DMA_QUEUED_EXT: 5756 case SATAC_READ_FPDMA_QUEUED: 5757 /* Unrecovered read error */ 5758 sense->es_add_code = 5759 SD_SCSI_ASC_UNREC_READ_ERR; 5760 break; 5761 case SATAC_WRITE_DMA: 5762 case SATAC_WRITE_DMA_EXT: 5763 case SATAC_WRITE_DMA_QUEUED: 5764 case SATAC_WRITE_DMA_QUEUED_EXT: 5765 case SATAC_WRITE_FPDMA_QUEUED: 5766 /* Write error */ 5767 sense->es_add_code = 5768 SD_SCSI_ASC_WRITE_ERR; 5769 break; 5770 default: 5771 /* Internal error */ 5772 SATA_LOG_D(( 5773 spx->txlt_sata_hba_inst, 5774 CE_WARN, 5775 "sata_txlt_rw_completion :" 5776 "internal error - invalid " 5777 "command 0x%2x", 5778 scmd->satacmd_cmd_reg)); 5779 break; 5780 } 5781 } 5782 break; 5783 } 5784 /* No extended sense key - no info available */ 5785 scsipkt->pkt_reason = CMD_INCOMPLETE; 5786 break; 5787 5788 case SATA_PKT_TIMEOUT: 5789 scsipkt->pkt_reason = CMD_TIMEOUT; 5790 scsipkt->pkt_statistics |= 5791 STAT_TIMEOUT | STAT_DEV_RESET; 5792 sense->es_key = KEY_ABORTED_COMMAND; 5793 break; 5794 5795 case SATA_PKT_ABORTED: 5796 scsipkt->pkt_reason = CMD_ABORTED; 5797 scsipkt->pkt_statistics |= STAT_ABORTED; 5798 sense->es_key = KEY_ABORTED_COMMAND; 5799 break; 5800 5801 case SATA_PKT_RESET: 5802 scsipkt->pkt_reason = CMD_RESET; 5803 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5804 sense->es_key = KEY_ABORTED_COMMAND; 5805 break; 5806 5807 default: 5808 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5809 "sata_txlt_rw_completion: " 5810 "invalid packet completion reason")); 5811 scsipkt->pkt_reason = CMD_TRAN_ERR; 5812 break; 5813 } 5814 } 5815 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5816 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5817 5818 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5819 scsipkt->pkt_comp != NULL) 5820 /* scsi callback required */ 5821 (*scsipkt->pkt_comp)(scsipkt); 5822 } 5823 5824 5825 /* 5826 * Translate completion status of non-data commands (i.e. commands returning 5827 * no data). 5828 * pkt completion_reason is checked to determine the completion status. 5829 * Do scsi callback if necessary (FLAG_NOINTR == 0) 5830 * 5831 * Note: this function may be called also for synchronously executed 5832 * commands. 5833 * This function may be used only if scsi_pkt is non-NULL. 5834 */ 5835 5836 static void 5837 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 5838 { 5839 sata_pkt_txlate_t *spx = 5840 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5841 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5842 struct scsi_extended_sense *sense; 5843 5844 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5845 STATE_SENT_CMD | STATE_GOT_STATUS; 5846 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5847 /* Normal completion */ 5848 scsipkt->pkt_reason = CMD_CMPLT; 5849 *scsipkt->pkt_scbp = STATUS_GOOD; 5850 } else { 5851 /* Something went wrong */ 5852 scsipkt->pkt_reason = CMD_INCOMPLETE; 5853 *scsipkt->pkt_scbp = STATUS_CHECK; 5854 sense = sata_arq_sense(spx); 5855 switch (sata_pkt->satapkt_reason) { 5856 case SATA_PKT_PORT_ERROR: 5857 /* 5858 * We have no device data. Assume no data transfered. 5859 */ 5860 sense->es_key = KEY_HARDWARE_ERROR; 5861 break; 5862 5863 case SATA_PKT_DEV_ERROR: 5864 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5865 SATA_STATUS_ERR) { 5866 /* 5867 * determine dev error reason from error 5868 * reg content 5869 */ 5870 sata_decode_device_error(spx, sense); 5871 break; 5872 } 5873 /* No extended sense key - no info available */ 5874 break; 5875 5876 case SATA_PKT_TIMEOUT: 5877 scsipkt->pkt_reason = CMD_TIMEOUT; 5878 scsipkt->pkt_statistics |= 5879 STAT_TIMEOUT | STAT_DEV_RESET; 5880 /* No extended sense key ? */ 5881 break; 5882 5883 case SATA_PKT_ABORTED: 5884 scsipkt->pkt_reason = CMD_ABORTED; 5885 scsipkt->pkt_statistics |= STAT_ABORTED; 5886 /* No extended sense key ? */ 5887 break; 5888 5889 case SATA_PKT_RESET: 5890 /* pkt aborted by an explicit reset from a host */ 5891 scsipkt->pkt_reason = CMD_RESET; 5892 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5893 break; 5894 5895 default: 5896 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5897 "sata_txlt_nodata_cmd_completion: " 5898 "invalid packet completion reason %d", 5899 sata_pkt->satapkt_reason)); 5900 scsipkt->pkt_reason = CMD_TRAN_ERR; 5901 break; 5902 } 5903 5904 } 5905 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5906 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5907 5908 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5909 scsipkt->pkt_comp != NULL) 5910 /* scsi callback required */ 5911 (*scsipkt->pkt_comp)(scsipkt); 5912 } 5913 5914 5915 /* 5916 * Build Mode sense R/W recovery page 5917 * NOT IMPLEMENTED 5918 */ 5919 5920 static int 5921 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5922 { 5923 #ifndef __lock_lint 5924 _NOTE(ARGUNUSED(sdinfo)) 5925 _NOTE(ARGUNUSED(pcntrl)) 5926 _NOTE(ARGUNUSED(buf)) 5927 #endif 5928 return (0); 5929 } 5930 5931 /* 5932 * Build Mode sense caching page - scsi-3 implementation. 5933 * Page length distinguishes previous format from scsi-3 format. 5934 * buf must have space for 0x12 bytes. 5935 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 5936 * 5937 */ 5938 static int 5939 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5940 { 5941 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 5942 sata_id_t *sata_id = &sdinfo->satadrv_id; 5943 5944 /* 5945 * Most of the fields are set to 0, being not supported and/or disabled 5946 */ 5947 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 5948 5949 /* Saved paramters not supported */ 5950 if (pcntrl == 3) 5951 return (0); 5952 if (pcntrl == 0 || pcntrl == 2) { 5953 /* 5954 * For now treat current and default parameters as same 5955 * That may have to change, if target driver will complain 5956 */ 5957 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 5958 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5959 5960 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 5961 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 5962 page->dra = 1; /* Read Ahead disabled */ 5963 page->rcd = 1; /* Read Cache disabled */ 5964 } 5965 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 5966 SATA_WRITE_CACHE_ENABLED(*sata_id)) 5967 page->wce = 1; /* Write Cache enabled */ 5968 } else { 5969 /* Changeable parameters */ 5970 page->mode_page.code = MODEPAGE_CACHING; 5971 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5972 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 5973 page->dra = 1; 5974 page->rcd = 1; 5975 } 5976 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 5977 page->wce = 1; 5978 } 5979 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 5980 sizeof (struct mode_page)); 5981 } 5982 5983 /* 5984 * Build Mode sense exception cntrl page 5985 */ 5986 static int 5987 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5988 { 5989 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 5990 sata_id_t *sata_id = &sdinfo->satadrv_id; 5991 5992 /* 5993 * Most of the fields are set to 0, being not supported and/or disabled 5994 */ 5995 bzero(buf, PAGELENGTH_INFO_EXCPT); 5996 5997 page->mode_page.code = MODEPAGE_INFO_EXCPT; 5998 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 5999 6000 /* Indicate that this is page is saveable */ 6001 page->mode_page.ps = 1; 6002 6003 /* 6004 * We will return the same data for default, current and saved page. 6005 * The only changeable bit is dexcpt and that bit is required 6006 * by the ATA specification to be preserved across power cycles. 6007 */ 6008 if (pcntrl != 1) { 6009 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6010 page->mrie = MRIE_ONLY_ON_REQUEST; 6011 } 6012 else 6013 page->dexcpt = 1; /* Only changeable parameter */ 6014 6015 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page)); 6016 } 6017 6018 6019 static int 6020 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6021 { 6022 struct mode_acoustic_management *page = 6023 (struct mode_acoustic_management *)buf; 6024 sata_id_t *sata_id = &sdinfo->satadrv_id; 6025 6026 /* 6027 * Most of the fields are set to 0, being not supported and/or disabled 6028 */ 6029 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 6030 6031 switch (pcntrl) { 6032 case P_CNTRL_DEFAULT: 6033 /* default paramters not supported */ 6034 return (0); 6035 6036 case P_CNTRL_CURRENT: 6037 case P_CNTRL_SAVED: 6038 /* Saved and current are supported and are identical */ 6039 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6040 page->mode_page.length = 6041 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6042 page->mode_page.ps = 1; 6043 6044 /* Word 83 indicates if feature is supported */ 6045 /* If feature is not supported */ 6046 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 6047 page->acoustic_manag_enable = 6048 ACOUSTIC_DISABLED; 6049 } else { 6050 page->acoustic_manag_enable = 6051 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 6052 != 0); 6053 /* Word 94 inidicates the value */ 6054 #ifdef _LITTLE_ENDIAN 6055 page->acoustic_manag_level = 6056 (uchar_t)sata_id->ai_acoustic; 6057 page->vendor_recommended_value = 6058 sata_id->ai_acoustic >> 8; 6059 #else 6060 page->acoustic_manag_level = 6061 sata_id->ai_acoustic >> 8; 6062 page->vendor_recommended_value = 6063 (uchar_t)sata_id->ai_acoustic; 6064 #endif 6065 } 6066 break; 6067 6068 case P_CNTRL_CHANGEABLE: 6069 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6070 page->mode_page.length = 6071 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6072 page->mode_page.ps = 1; 6073 6074 /* Word 83 indicates if the feature is supported */ 6075 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 6076 page->acoustic_manag_enable = 6077 ACOUSTIC_ENABLED; 6078 page->acoustic_manag_level = 0xff; 6079 } 6080 break; 6081 } 6082 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6083 sizeof (struct mode_page)); 6084 } 6085 6086 6087 /* 6088 * Build Mode sense power condition page 6089 * NOT IMPLEMENTED. 6090 */ 6091 static int 6092 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6093 { 6094 #ifndef __lock_lint 6095 _NOTE(ARGUNUSED(sdinfo)) 6096 _NOTE(ARGUNUSED(pcntrl)) 6097 _NOTE(ARGUNUSED(buf)) 6098 #endif 6099 return (0); 6100 } 6101 6102 6103 /* 6104 * Process mode select caching page 8 (scsi3 format only). 6105 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6106 * if these features are supported by the device. If these features are not 6107 * supported, quietly ignore them. 6108 * This function fails only if the SET FEATURE command sent to 6109 * the device fails. The page format is not varified, assuming that the 6110 * target driver operates correctly - if parameters length is too short, 6111 * we just drop the page. 6112 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6113 * setting have to be changed. 6114 * SET FEATURE command is executed synchronously, i.e. we wait here until 6115 * it is completed, regardless of the scsi pkt directives. 6116 * 6117 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6118 * changing DRA will change RCD. 6119 * 6120 * More than one SATA command may be executed to perform operations specified 6121 * by mode select pages. The first error terminates further execution. 6122 * Operations performed successully are not backed-up in such case. 6123 * 6124 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6125 * If operation resulted in changing device setup, dmod flag should be set to 6126 * one (1). If parameters were not changed, dmod flag should be set to 0. 6127 * Upon return, if operation required sending command to the device, the rval 6128 * should be set to the value returned by sata_hba_start. If operation 6129 * did not require device access, rval should be set to TRAN_ACCEPT. 6130 * The pagelen should be set to the length of the page. 6131 * 6132 * This function has to be called with a port mutex held. 6133 * 6134 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6135 */ 6136 int 6137 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6138 int parmlen, int *pagelen, int *rval, int *dmod) 6139 { 6140 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6141 sata_drive_info_t *sdinfo; 6142 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6143 sata_id_t *sata_id; 6144 struct scsi_extended_sense *sense; 6145 int wce, dra; /* Current settings */ 6146 6147 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6148 &spx->txlt_sata_pkt->satapkt_device); 6149 sata_id = &sdinfo->satadrv_id; 6150 *dmod = 0; 6151 6152 /* Verify parameters length. If too short, drop it */ 6153 if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6154 sizeof (struct mode_page) < parmlen) { 6155 *scsipkt->pkt_scbp = STATUS_CHECK; 6156 sense = sata_arq_sense(spx); 6157 sense->es_key = KEY_ILLEGAL_REQUEST; 6158 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6159 *pagelen = parmlen; 6160 *rval = TRAN_ACCEPT; 6161 return (SATA_FAILURE); 6162 } 6163 6164 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6165 6166 /* 6167 * We can manipulate only write cache and read ahead 6168 * (read cache) setting. 6169 */ 6170 if (!SATA_READ_AHEAD_SUPPORTED(*sata_id) && 6171 !SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 6172 /* 6173 * None of the features is supported - ignore 6174 */ 6175 *rval = TRAN_ACCEPT; 6176 return (SATA_SUCCESS); 6177 } 6178 6179 /* Current setting of Read Ahead (and Read Cache) */ 6180 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 6181 dra = 0; /* 0 == not disabled */ 6182 else 6183 dra = 1; 6184 /* Current setting of Write Cache */ 6185 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 6186 wce = 1; 6187 else 6188 wce = 0; 6189 6190 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 6191 /* nothing to do */ 6192 *rval = TRAN_ACCEPT; 6193 return (SATA_SUCCESS); 6194 } 6195 6196 /* 6197 * Need to flip some setting 6198 * Set-up Internal SET FEATURES command(s) 6199 */ 6200 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6201 scmd->satacmd_addr_type = 0; 6202 scmd->satacmd_device_reg = 0; 6203 scmd->satacmd_status_reg = 0; 6204 scmd->satacmd_error_reg = 0; 6205 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6206 if (page->dra != dra || page->rcd != dra) { 6207 /* Need to flip read ahead setting */ 6208 if (dra == 0) 6209 /* Disable read ahead / read cache */ 6210 scmd->satacmd_features_reg = 6211 SATAC_SF_DISABLE_READ_AHEAD; 6212 else 6213 /* Enable read ahead / read cache */ 6214 scmd->satacmd_features_reg = 6215 SATAC_SF_ENABLE_READ_AHEAD; 6216 6217 /* Transfer command to HBA */ 6218 if (sata_hba_start(spx, rval) != 0) 6219 /* 6220 * Pkt not accepted for execution. 6221 */ 6222 return (SATA_FAILURE); 6223 6224 *dmod = 1; 6225 6226 /* Now process return */ 6227 if (spx->txlt_sata_pkt->satapkt_reason != 6228 SATA_PKT_COMPLETED) { 6229 goto failure; /* Terminate */ 6230 } 6231 } 6232 6233 /* Note that the packet is not removed, so it could be re-used */ 6234 if (page->wce != wce) { 6235 /* Need to flip Write Cache setting */ 6236 if (page->wce == 1) 6237 /* Enable write cache */ 6238 scmd->satacmd_features_reg = 6239 SATAC_SF_ENABLE_WRITE_CACHE; 6240 else 6241 /* Disable write cache */ 6242 scmd->satacmd_features_reg = 6243 SATAC_SF_DISABLE_WRITE_CACHE; 6244 6245 /* Transfer command to HBA */ 6246 if (sata_hba_start(spx, rval) != 0) 6247 /* 6248 * Pkt not accepted for execution. 6249 */ 6250 return (SATA_FAILURE); 6251 6252 *dmod = 1; 6253 6254 /* Now process return */ 6255 if (spx->txlt_sata_pkt->satapkt_reason != 6256 SATA_PKT_COMPLETED) { 6257 goto failure; 6258 } 6259 } 6260 return (SATA_SUCCESS); 6261 6262 failure: 6263 sata_xlate_errors(spx); 6264 6265 return (SATA_FAILURE); 6266 } 6267 6268 /* 6269 * Process mode select informational exceptions control page 0x1c 6270 * 6271 * The only changeable bit is dexcpt (disable exceptions). 6272 * MRIE (method of reporting informational exceptions) must be 6273 * "only on request". 6274 * This page applies to informational exceptions that report 6275 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 6276 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 6277 * Informational exception conditions occur as the result of background scan 6278 * errors, background self-test errors, or vendor specific events within a 6279 * logical unit. An informational exception condition may occur asynchronous 6280 * to any commands. 6281 * 6282 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6283 * If operation resulted in changing device setup, dmod flag should be set to 6284 * one (1). If parameters were not changed, dmod flag should be set to 0. 6285 * Upon return, if operation required sending command to the device, the rval 6286 * should be set to the value returned by sata_hba_start. If operation 6287 * did not require device access, rval should be set to TRAN_ACCEPT. 6288 * The pagelen should be set to the length of the page. 6289 * 6290 * This function has to be called with a port mutex held. 6291 * 6292 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6293 * 6294 * Cannot be called in the interrupt context. 6295 */ 6296 static int 6297 sata_mode_select_page_1c( 6298 sata_pkt_txlate_t *spx, 6299 struct mode_info_excpt_page *page, 6300 int parmlen, 6301 int *pagelen, 6302 int *rval, 6303 int *dmod) 6304 { 6305 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6306 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6307 sata_drive_info_t *sdinfo; 6308 sata_id_t *sata_id; 6309 struct scsi_extended_sense *sense; 6310 6311 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6312 &spx->txlt_sata_pkt->satapkt_device); 6313 sata_id = &sdinfo->satadrv_id; 6314 6315 *dmod = 0; 6316 6317 /* Verify parameters length. If too short, drop it */ 6318 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) || 6319 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 6320 *scsipkt->pkt_scbp = STATUS_CHECK; 6321 sense = sata_arq_sense(spx); 6322 sense->es_key = KEY_ILLEGAL_REQUEST; 6323 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6324 *pagelen = parmlen; 6325 *rval = TRAN_ACCEPT; 6326 return (SATA_FAILURE); 6327 } 6328 6329 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 6330 6331 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6332 *scsipkt->pkt_scbp = STATUS_CHECK; 6333 sense = sata_arq_sense(spx); 6334 sense->es_key = KEY_ILLEGAL_REQUEST; 6335 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6336 *pagelen = parmlen; 6337 *rval = TRAN_ACCEPT; 6338 return (SATA_FAILURE); 6339 } 6340 6341 /* If already in the state requested, we are done */ 6342 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6343 /* nothing to do */ 6344 *rval = TRAN_ACCEPT; 6345 return (SATA_SUCCESS); 6346 } 6347 6348 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6349 6350 /* Build SMART_ENABLE or SMART_DISABLE command */ 6351 scmd->satacmd_addr_type = 0; /* N/A */ 6352 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 6353 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 6354 scmd->satacmd_features_reg = page->dexcpt ? 6355 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 6356 scmd->satacmd_device_reg = 0; /* Always device 0 */ 6357 scmd->satacmd_cmd_reg = SATAC_SMART; 6358 6359 /* Transfer command to HBA */ 6360 if (sata_hba_start(spx, rval) != 0) 6361 /* 6362 * Pkt not accepted for execution. 6363 */ 6364 return (SATA_FAILURE); 6365 6366 *dmod = 1; /* At least may have been modified */ 6367 6368 /* Now process return */ 6369 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 6370 return (SATA_SUCCESS); 6371 6372 /* Packet did not complete successfully */ 6373 sata_xlate_errors(spx); 6374 6375 return (SATA_FAILURE); 6376 } 6377 6378 /* 6379 * Process mode select acoustic management control page 0x30 6380 * 6381 * 6382 * This function has to be called with a port mutex held. 6383 * 6384 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6385 * 6386 * Cannot be called in the interrupt context. 6387 */ 6388 int 6389 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 6390 mode_acoustic_management *page, int parmlen, int *pagelen, 6391 int *rval, int *dmod) 6392 { 6393 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6394 sata_drive_info_t *sdinfo; 6395 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6396 sata_id_t *sata_id; 6397 struct scsi_extended_sense *sense; 6398 6399 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6400 &spx->txlt_sata_pkt->satapkt_device); 6401 sata_id = &sdinfo->satadrv_id; 6402 *dmod = 0; 6403 6404 /* If parmlen is too short or the feature is not supported, drop it */ 6405 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6406 sizeof (struct mode_page)) < parmlen) || 6407 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 6408 *scsipkt->pkt_scbp = STATUS_CHECK; 6409 sense = sata_arq_sense(spx); 6410 sense->es_key = KEY_ILLEGAL_REQUEST; 6411 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6412 *pagelen = parmlen; 6413 *rval = TRAN_ACCEPT; 6414 return (SATA_FAILURE); 6415 } 6416 6417 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6418 sizeof (struct mode_page); 6419 6420 /* 6421 * We can enable and disable acoustice management and 6422 * set the acoustic management level. 6423 */ 6424 6425 /* 6426 * Set-up Internal SET FEATURES command(s) 6427 */ 6428 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6429 scmd->satacmd_addr_type = 0; 6430 scmd->satacmd_device_reg = 0; 6431 scmd->satacmd_status_reg = 0; 6432 scmd->satacmd_error_reg = 0; 6433 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6434 if (page->acoustic_manag_enable) { 6435 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 6436 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 6437 } else { /* disabling acoustic management */ 6438 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 6439 } 6440 6441 /* Transfer command to HBA */ 6442 if (sata_hba_start(spx, rval) != 0) 6443 /* 6444 * Pkt not accepted for execution. 6445 */ 6446 return (SATA_FAILURE); 6447 6448 /* Now process return */ 6449 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 6450 sata_xlate_errors(spx); 6451 return (SATA_FAILURE); 6452 } 6453 6454 *dmod = 1; 6455 6456 return (SATA_SUCCESS); 6457 } 6458 6459 6460 6461 6462 /* 6463 * sata_build_lsense_page0() is used to create the 6464 * SCSI LOG SENSE page 0 (supported log pages) 6465 * 6466 * Currently supported pages are 0, 0x10, 0x2f and 0x30 6467 * (supported log pages, self-test results, informational exceptions 6468 * and Sun vendor specific ATA SMART data). 6469 * 6470 * Takes a sata_drive_info t * and the address of a buffer 6471 * in which to create the page information. 6472 * 6473 * Returns the number of bytes valid in the buffer. 6474 */ 6475 static int 6476 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 6477 { 6478 struct log_parameter *lpp = (struct log_parameter *)buf; 6479 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 6480 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 6481 sata_id_t *sata_id = &sdinfo->satadrv_id; 6482 6483 lpp->param_code[0] = 0; 6484 lpp->param_code[1] = 0; 6485 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6486 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 6487 6488 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 6489 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 6490 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 6491 ++num_pages_supported; 6492 } 6493 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 6494 ++num_pages_supported; 6495 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 6496 ++num_pages_supported; 6497 } 6498 6499 lpp->param_len = num_pages_supported; 6500 6501 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 6502 num_pages_supported); 6503 } 6504 6505 /* 6506 * sata_build_lsense_page_10() is used to create the 6507 * SCSI LOG SENSE page 0x10 (self-test results) 6508 * 6509 * Takes a sata_drive_info t * and the address of a buffer 6510 * in which to create the page information as well as a sata_hba_inst_t *. 6511 * 6512 * Returns the number of bytes valid in the buffer. 6513 * 6514 * Note: Self test and SMART data is accessible in device log pages. 6515 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 6516 * of data can be transferred by a single command), or by the General Purpose 6517 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 6518 * - approximately 33MB - can be transferred by a single command. 6519 * The SCT Command response (either error or command) is the same for both 6520 * the SMART and GPL methods of issuing commands. 6521 * This function uses READ LOG EXT command when drive supports LBA48, and 6522 * SMART READ command otherwise. 6523 * 6524 * Since above commands are executed in a synchronous mode, this function 6525 * should not be called in an interrupt context. 6526 */ 6527 static int 6528 sata_build_lsense_page_10( 6529 sata_drive_info_t *sdinfo, 6530 uint8_t *buf, 6531 sata_hba_inst_t *sata_hba_inst) 6532 { 6533 struct log_parameter *lpp = (struct log_parameter *)buf; 6534 int rval; 6535 6536 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6537 struct smart_ext_selftest_log *ext_selftest_log; 6538 6539 ext_selftest_log = kmem_zalloc( 6540 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 6541 6542 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 6543 ext_selftest_log, 0); 6544 if (rval == 0) { 6545 int index, start_index; 6546 struct smart_ext_selftest_log_entry *entry; 6547 static const struct smart_ext_selftest_log_entry empty = 6548 {0}; 6549 uint16_t block_num; 6550 int count; 6551 boolean_t only_one_block = B_FALSE; 6552 6553 index = ext_selftest_log-> 6554 smart_ext_selftest_log_index[0]; 6555 index |= ext_selftest_log-> 6556 smart_ext_selftest_log_index[1] << 8; 6557 if (index == 0) 6558 goto out; 6559 6560 --index; /* Correct for 0 origin */ 6561 start_index = index; /* remember where we started */ 6562 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6563 if (block_num != 0) { 6564 rval = sata_ext_smart_selftest_read_log( 6565 sata_hba_inst, sdinfo, ext_selftest_log, 6566 block_num); 6567 if (rval != 0) 6568 goto out; 6569 } 6570 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6571 entry = 6572 &ext_selftest_log-> 6573 smart_ext_selftest_log_entries[index]; 6574 6575 for (count = 1; 6576 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6577 ++count) { 6578 uint8_t status; 6579 uint8_t code; 6580 uint8_t sense_key; 6581 uint8_t add_sense_code; 6582 uint8_t add_sense_code_qual; 6583 6584 /* If this is an unused entry, we are done */ 6585 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 6586 /* Broken firmware on some disks */ 6587 if (index + 1 == 6588 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 6589 --entry; 6590 --index; 6591 if (bcmp(entry, &empty, 6592 sizeof (empty)) == 0) 6593 goto out; 6594 } else 6595 goto out; 6596 } 6597 6598 if (only_one_block && 6599 start_index == index) 6600 goto out; 6601 6602 lpp->param_code[0] = 0; 6603 lpp->param_code[1] = count; 6604 lpp->param_ctrl_flags = 6605 LOG_CTRL_LP | LOG_CTRL_LBIN; 6606 lpp->param_len = 6607 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6608 6609 status = entry->smart_ext_selftest_log_status; 6610 status >>= 4; 6611 switch (status) { 6612 case 0: 6613 default: 6614 sense_key = KEY_NO_SENSE; 6615 add_sense_code = 6616 SD_SCSI_ASC_NO_ADD_SENSE; 6617 add_sense_code_qual = 0; 6618 break; 6619 case 1: 6620 sense_key = KEY_ABORTED_COMMAND; 6621 add_sense_code = 6622 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6623 add_sense_code_qual = SCSI_COMPONENT_81; 6624 break; 6625 case 2: 6626 sense_key = KEY_ABORTED_COMMAND; 6627 add_sense_code = 6628 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6629 add_sense_code_qual = SCSI_COMPONENT_82; 6630 break; 6631 case 3: 6632 sense_key = KEY_ABORTED_COMMAND; 6633 add_sense_code = 6634 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6635 add_sense_code_qual = SCSI_COMPONENT_83; 6636 break; 6637 case 4: 6638 sense_key = KEY_HARDWARE_ERROR; 6639 add_sense_code = 6640 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6641 add_sense_code_qual = SCSI_COMPONENT_84; 6642 break; 6643 case 5: 6644 sense_key = KEY_HARDWARE_ERROR; 6645 add_sense_code = 6646 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6647 add_sense_code_qual = SCSI_COMPONENT_85; 6648 break; 6649 case 6: 6650 sense_key = KEY_HARDWARE_ERROR; 6651 add_sense_code = 6652 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6653 add_sense_code_qual = SCSI_COMPONENT_86; 6654 break; 6655 case 7: 6656 sense_key = KEY_MEDIUM_ERROR; 6657 add_sense_code = 6658 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6659 add_sense_code_qual = SCSI_COMPONENT_87; 6660 break; 6661 case 8: 6662 sense_key = KEY_HARDWARE_ERROR; 6663 add_sense_code = 6664 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6665 add_sense_code_qual = SCSI_COMPONENT_88; 6666 break; 6667 } 6668 code = 0; /* unspecified */ 6669 status |= (code << 4); 6670 lpp->param_values[0] = status; 6671 lpp->param_values[1] = 0; /* unspecified */ 6672 lpp->param_values[2] = entry-> 6673 smart_ext_selftest_log_timestamp[1]; 6674 lpp->param_values[3] = entry-> 6675 smart_ext_selftest_log_timestamp[0]; 6676 if (status != 0) { 6677 lpp->param_values[4] = 0; 6678 lpp->param_values[5] = 0; 6679 lpp->param_values[6] = entry-> 6680 smart_ext_selftest_log_failing_lba 6681 [5]; 6682 lpp->param_values[7] = entry-> 6683 smart_ext_selftest_log_failing_lba 6684 [4]; 6685 lpp->param_values[8] = entry-> 6686 smart_ext_selftest_log_failing_lba 6687 [3]; 6688 lpp->param_values[9] = entry-> 6689 smart_ext_selftest_log_failing_lba 6690 [2]; 6691 lpp->param_values[10] = entry-> 6692 smart_ext_selftest_log_failing_lba 6693 [1]; 6694 lpp->param_values[11] = entry-> 6695 smart_ext_selftest_log_failing_lba 6696 [0]; 6697 } else { /* No bad block address */ 6698 lpp->param_values[4] = 0xff; 6699 lpp->param_values[5] = 0xff; 6700 lpp->param_values[6] = 0xff; 6701 lpp->param_values[7] = 0xff; 6702 lpp->param_values[8] = 0xff; 6703 lpp->param_values[9] = 0xff; 6704 lpp->param_values[10] = 0xff; 6705 lpp->param_values[11] = 0xff; 6706 } 6707 6708 lpp->param_values[12] = sense_key; 6709 lpp->param_values[13] = add_sense_code; 6710 lpp->param_values[14] = add_sense_code_qual; 6711 lpp->param_values[15] = 0; /* undefined */ 6712 6713 lpp = (struct log_parameter *) 6714 (((uint8_t *)lpp) + 6715 SCSI_LOG_PARAM_HDR_LEN + 6716 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6717 6718 --index; /* Back up to previous entry */ 6719 if (index < 0) { 6720 if (block_num > 0) { 6721 --block_num; 6722 } else { 6723 struct read_log_ext_directory 6724 logdir; 6725 6726 rval = 6727 sata_read_log_ext_directory( 6728 sata_hba_inst, sdinfo, 6729 &logdir); 6730 if (rval == -1) 6731 goto out; 6732 if ((logdir.read_log_ext_vers 6733 [0] == 0) && 6734 (logdir.read_log_ext_vers 6735 [1] == 0)) 6736 goto out; 6737 block_num = 6738 logdir.read_log_ext_nblks 6739 [EXT_SMART_SELFTEST_LOG_PAGE 6740 - 1][0]; 6741 block_num |= logdir. 6742 read_log_ext_nblks 6743 [EXT_SMART_SELFTEST_LOG_PAGE 6744 - 1][1] << 8; 6745 --block_num; 6746 only_one_block = 6747 (block_num == 0); 6748 } 6749 rval = sata_ext_smart_selftest_read_log( 6750 sata_hba_inst, sdinfo, 6751 ext_selftest_log, block_num); 6752 if (rval != 0) 6753 goto out; 6754 6755 index = 6756 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 6757 1; 6758 } 6759 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6760 entry = &ext_selftest_log-> 6761 smart_ext_selftest_log_entries[index]; 6762 } 6763 } 6764 out: 6765 kmem_free(ext_selftest_log, 6766 sizeof (struct smart_ext_selftest_log)); 6767 } else { 6768 struct smart_selftest_log *selftest_log; 6769 6770 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 6771 KM_SLEEP); 6772 6773 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 6774 selftest_log); 6775 6776 if (rval == 0) { 6777 int index; 6778 int count; 6779 struct smart_selftest_log_entry *entry; 6780 static const struct smart_selftest_log_entry empty = 6781 { 0 }; 6782 6783 index = selftest_log->smart_selftest_log_index; 6784 if (index == 0) 6785 goto done; 6786 --index; /* Correct for 0 origin */ 6787 entry = &selftest_log-> 6788 smart_selftest_log_entries[index]; 6789 for (count = 1; 6790 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6791 ++count) { 6792 uint8_t status; 6793 uint8_t code; 6794 uint8_t sense_key; 6795 uint8_t add_sense_code; 6796 uint8_t add_sense_code_qual; 6797 6798 if (bcmp(entry, &empty, sizeof (empty)) == 0) 6799 goto done; 6800 6801 lpp->param_code[0] = 0; 6802 lpp->param_code[1] = count; 6803 lpp->param_ctrl_flags = 6804 LOG_CTRL_LP | LOG_CTRL_LBIN; 6805 lpp->param_len = 6806 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6807 6808 status = entry->smart_selftest_log_status; 6809 status >>= 4; 6810 switch (status) { 6811 case 0: 6812 default: 6813 sense_key = KEY_NO_SENSE; 6814 add_sense_code = 6815 SD_SCSI_ASC_NO_ADD_SENSE; 6816 break; 6817 case 1: 6818 sense_key = KEY_ABORTED_COMMAND; 6819 add_sense_code = 6820 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6821 add_sense_code_qual = SCSI_COMPONENT_81; 6822 break; 6823 case 2: 6824 sense_key = KEY_ABORTED_COMMAND; 6825 add_sense_code = 6826 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6827 add_sense_code_qual = SCSI_COMPONENT_82; 6828 break; 6829 case 3: 6830 sense_key = KEY_ABORTED_COMMAND; 6831 add_sense_code = 6832 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6833 add_sense_code_qual = SCSI_COMPONENT_83; 6834 break; 6835 case 4: 6836 sense_key = KEY_HARDWARE_ERROR; 6837 add_sense_code = 6838 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6839 add_sense_code_qual = SCSI_COMPONENT_84; 6840 break; 6841 case 5: 6842 sense_key = KEY_HARDWARE_ERROR; 6843 add_sense_code = 6844 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6845 add_sense_code_qual = SCSI_COMPONENT_85; 6846 break; 6847 case 6: 6848 sense_key = KEY_HARDWARE_ERROR; 6849 add_sense_code = 6850 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6851 add_sense_code_qual = SCSI_COMPONENT_86; 6852 break; 6853 case 7: 6854 sense_key = KEY_MEDIUM_ERROR; 6855 add_sense_code = 6856 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6857 add_sense_code_qual = SCSI_COMPONENT_87; 6858 break; 6859 case 8: 6860 sense_key = KEY_HARDWARE_ERROR; 6861 add_sense_code = 6862 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6863 add_sense_code_qual = SCSI_COMPONENT_88; 6864 break; 6865 } 6866 code = 0; /* unspecified */ 6867 status |= (code << 4); 6868 lpp->param_values[0] = status; 6869 lpp->param_values[1] = 0; /* unspecified */ 6870 lpp->param_values[2] = entry-> 6871 smart_selftest_log_timestamp[1]; 6872 lpp->param_values[3] = entry-> 6873 smart_selftest_log_timestamp[0]; 6874 if (status != 0) { 6875 lpp->param_values[4] = 0; 6876 lpp->param_values[5] = 0; 6877 lpp->param_values[6] = 0; 6878 lpp->param_values[7] = 0; 6879 lpp->param_values[8] = entry-> 6880 smart_selftest_log_failing_lba[3]; 6881 lpp->param_values[9] = entry-> 6882 smart_selftest_log_failing_lba[2]; 6883 lpp->param_values[10] = entry-> 6884 smart_selftest_log_failing_lba[1]; 6885 lpp->param_values[11] = entry-> 6886 smart_selftest_log_failing_lba[0]; 6887 } else { /* No block address */ 6888 lpp->param_values[4] = 0xff; 6889 lpp->param_values[5] = 0xff; 6890 lpp->param_values[6] = 0xff; 6891 lpp->param_values[7] = 0xff; 6892 lpp->param_values[8] = 0xff; 6893 lpp->param_values[9] = 0xff; 6894 lpp->param_values[10] = 0xff; 6895 lpp->param_values[11] = 0xff; 6896 } 6897 lpp->param_values[12] = sense_key; 6898 lpp->param_values[13] = add_sense_code; 6899 lpp->param_values[14] = add_sense_code_qual; 6900 lpp->param_values[15] = 0; /* undefined */ 6901 6902 lpp = (struct log_parameter *) 6903 (((uint8_t *)lpp) + 6904 SCSI_LOG_PARAM_HDR_LEN + 6905 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6906 --index; /* back up to previous entry */ 6907 if (index < 0) { 6908 index = 6909 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 6910 } 6911 entry = &selftest_log-> 6912 smart_selftest_log_entries[index]; 6913 } 6914 } 6915 done: 6916 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 6917 } 6918 6919 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 6920 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 6921 } 6922 6923 /* 6924 * sata_build_lsense_page_2f() is used to create the 6925 * SCSI LOG SENSE page 0x2f (informational exceptions) 6926 * 6927 * Takes a sata_drive_info t * and the address of a buffer 6928 * in which to create the page information as well as a sata_hba_inst_t *. 6929 * 6930 * Returns the number of bytes valid in the buffer. 6931 * 6932 * Because it invokes function(s) that send synchronously executed command 6933 * to the HBA, it cannot be called in the interrupt context. 6934 */ 6935 static int 6936 sata_build_lsense_page_2f( 6937 sata_drive_info_t *sdinfo, 6938 uint8_t *buf, 6939 sata_hba_inst_t *sata_hba_inst) 6940 { 6941 struct log_parameter *lpp = (struct log_parameter *)buf; 6942 int rval; 6943 uint8_t *smart_data; 6944 uint8_t temp; 6945 sata_id_t *sata_id; 6946 #define SMART_NO_TEMP 0xff 6947 6948 lpp->param_code[0] = 0; 6949 lpp->param_code[1] = 0; 6950 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6951 6952 /* Now get the SMART status w.r.t. threshold exceeded */ 6953 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 6954 switch (rval) { 6955 case 1: 6956 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 6957 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 6958 break; 6959 case 0: 6960 case -1: /* failed to get data */ 6961 lpp->param_values[0] = 0; /* No failure predicted */ 6962 lpp->param_values[1] = 0; 6963 break; 6964 #if defined(SATA_DEBUG) 6965 default: 6966 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 6967 /* NOTREACHED */ 6968 #endif 6969 } 6970 6971 sata_id = &sdinfo->satadrv_id; 6972 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 6973 temp = SMART_NO_TEMP; 6974 else { 6975 /* Now get the temperature */ 6976 smart_data = kmem_zalloc(512, KM_SLEEP); 6977 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 6978 SCT_STATUS_LOG_PAGE, 1); 6979 if (rval == -1) 6980 temp = SMART_NO_TEMP; 6981 else { 6982 temp = smart_data[200]; 6983 if (temp & 0x80) { 6984 if (temp & 0x7f) 6985 temp = 0; 6986 else 6987 temp = SMART_NO_TEMP; 6988 } 6989 } 6990 kmem_free(smart_data, 512); 6991 } 6992 6993 lpp->param_values[2] = temp; /* most recent temperature */ 6994 lpp->param_values[3] = 0; /* required vendor specific byte */ 6995 6996 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 6997 6998 6999 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 7000 } 7001 7002 /* 7003 * sata_build_lsense_page_30() is used to create the 7004 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 7005 * 7006 * Takes a sata_drive_info t * and the address of a buffer 7007 * in which to create the page information as well as a sata_hba_inst_t *. 7008 * 7009 * Returns the number of bytes valid in the buffer. 7010 */ 7011 static int 7012 sata_build_lsense_page_30( 7013 sata_drive_info_t *sdinfo, 7014 uint8_t *buf, 7015 sata_hba_inst_t *sata_hba_inst) 7016 { 7017 struct smart_data *smart_data = (struct smart_data *)buf; 7018 int rval; 7019 7020 /* Now do the SMART READ DATA */ 7021 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7022 if (rval == -1) 7023 return (0); 7024 7025 return (sizeof (struct smart_data)); 7026 } 7027 7028 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 7029 7030 /* 7031 * Start command for ATAPI device. 7032 * This function processes scsi_pkt requests. 7033 * Now CD/DVD, tape and ATAPI disk devices are supported. 7034 * Most commands are packet without any translation into Packet Command. 7035 * Some may be trapped and executed as SATA commands (not clear which one). 7036 * 7037 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 7038 * execution). 7039 * Returns other TRAN_XXXX codes if command is not accepted or completed 7040 * (see return values for sata_hba_start()). 7041 * 7042 * Note: 7043 * Inquiry cdb format differs between transport version 2 and 3. 7044 * However, the transport version 3 devices that were checked did not adhere 7045 * to the specification (ignored MSB of the allocation length). Therefore, 7046 * the transport version is not checked, but Inquiry allocation length is 7047 * truncated to 255 bytes if the original allocation length set-up by the 7048 * target driver is greater than 255 bytes. 7049 */ 7050 static int 7051 sata_txlt_atapi(sata_pkt_txlate_t *spx) 7052 { 7053 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7054 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7055 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7056 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 7057 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 7058 &spx->txlt_sata_pkt->satapkt_device); 7059 int cport = SATA_TXLT_CPORT(spx); 7060 int cdblen; 7061 int rval, reason; 7062 int synch; 7063 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 7064 7065 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7066 7067 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 7068 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 7069 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7070 return (rval); 7071 } 7072 7073 /* 7074 * ATAPI device executes some ATA commands in addition to those 7075 * commands sent via PACKET command. These ATA commands may be 7076 * executed by the regular SATA translation functions. None needs 7077 * to be captured now. 7078 * 7079 * Commands sent via PACKET command include: 7080 * MMC command set for ATAPI CD/DVD device 7081 * SSC command set for ATAPI TAPE device 7082 * SBC command set for ATAPI disk device 7083 * 7084 */ 7085 7086 /* Check the size of cdb */ 7087 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 7088 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 7089 sata_log(NULL, CE_WARN, 7090 "sata: invalid ATAPI cdb length %d", 7091 scsipkt->pkt_cdblen); 7092 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7093 return (TRAN_BADPKT); 7094 } 7095 7096 SATAATAPITRACE(spx, cdblen); 7097 7098 /* 7099 * For non-read/write commands we need to 7100 * map buffer 7101 */ 7102 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 7103 case SCMD_READ: 7104 case SCMD_READ_G1: 7105 case SCMD_READ_G5: 7106 case SCMD_READ_G4: 7107 case SCMD_WRITE: 7108 case SCMD_WRITE_G1: 7109 case SCMD_WRITE_G5: 7110 case SCMD_WRITE_G4: 7111 break; 7112 default: 7113 if (bp != NULL) { 7114 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 7115 bp_mapin(bp); 7116 } 7117 break; 7118 } 7119 /* 7120 * scmd->satacmd_flags.sata_data_direction default - 7121 * SATA_DIR_NODATA_XFER - is set by 7122 * sata_txlt_generic_pkt_info(). 7123 */ 7124 if (scmd->satacmd_bp) { 7125 if (scmd->satacmd_bp->b_flags & B_READ) { 7126 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7127 } else { 7128 scmd->satacmd_flags.sata_data_direction = 7129 SATA_DIR_WRITE; 7130 } 7131 } 7132 7133 /* 7134 * Set up ATAPI packet command. 7135 */ 7136 7137 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7138 7139 /* Copy cdb into sata_cmd */ 7140 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7141 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7142 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 7143 7144 /* See note in the command header */ 7145 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 7146 if (scmd->satacmd_acdb[3] != 0) 7147 scmd->satacmd_acdb[4] = 255; 7148 } 7149 7150 #ifdef SATA_DEBUG 7151 if (sata_debug_flags & SATA_DBG_ATAPI) { 7152 uint8_t *p = scmd->satacmd_acdb; 7153 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 7154 7155 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 7156 "%02x %02x %02x %02x %02x %02x %02x %02x " 7157 "%2x %02x %02x %02x %02x %02x %02x %02x", 7158 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7159 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7160 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 7161 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 7162 } 7163 #endif 7164 7165 /* 7166 * Preset request sense data to NO SENSE. 7167 * If there is no way to get error information via Request Sense, 7168 * the packet request sense data would not have to be modified by HBA, 7169 * but it could be returned as is. 7170 */ 7171 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7172 sata_fixed_sense_data_preset( 7173 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7174 7175 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7176 /* Need callback function */ 7177 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 7178 synch = FALSE; 7179 } else 7180 synch = TRUE; 7181 7182 /* Transfer command to HBA */ 7183 if (sata_hba_start(spx, &rval) != 0) { 7184 /* Pkt not accepted for execution */ 7185 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7186 return (rval); 7187 } 7188 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7189 /* 7190 * If execution is non-synchronous, 7191 * a callback function will handle potential errors, translate 7192 * the response and will do a callback to a target driver. 7193 * If it was synchronous, use the same framework callback to check 7194 * an execution status. 7195 */ 7196 if (synch) { 7197 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7198 "synchronous execution status %x\n", 7199 spx->txlt_sata_pkt->satapkt_reason); 7200 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 7201 } 7202 return (TRAN_ACCEPT); 7203 } 7204 7205 7206 /* 7207 * ATAPI Packet command completion. 7208 * 7209 * Failure of the command passed via Packet command are considered device 7210 * error. SATA HBA driver would have to retrieve error data (via Request 7211 * Sense command delivered via error retrieval sata packet) and copy it 7212 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 7213 */ 7214 static void 7215 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 7216 { 7217 sata_pkt_txlate_t *spx = 7218 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7219 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7220 struct scsi_extended_sense *sense; 7221 struct buf *bp; 7222 int rval; 7223 7224 #ifdef SATA_DEBUG 7225 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 7226 #endif 7227 7228 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7229 STATE_SENT_CMD | STATE_GOT_STATUS; 7230 7231 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7232 /* Normal completion */ 7233 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 7234 scsipkt->pkt_state |= STATE_XFERRED_DATA; 7235 scsipkt->pkt_reason = CMD_CMPLT; 7236 *scsipkt->pkt_scbp = STATUS_GOOD; 7237 if (spx->txlt_tmp_buf != NULL) { 7238 /* Temporary buffer was used */ 7239 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7240 if (bp->b_flags & B_READ) { 7241 rval = ddi_dma_sync( 7242 spx->txlt_buf_dma_handle, 0, 0, 7243 DDI_DMA_SYNC_FORCPU); 7244 ASSERT(rval == DDI_SUCCESS); 7245 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7246 bp->b_bcount); 7247 } 7248 } 7249 } else { 7250 /* 7251 * Something went wrong - analyze return 7252 */ 7253 *scsipkt->pkt_scbp = STATUS_CHECK; 7254 sense = sata_arq_sense(spx); 7255 7256 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7257 /* 7258 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 7259 * Under this condition ERR bit is set for ATA command, 7260 * and CHK bit set for ATAPI command. 7261 * 7262 * Please check st_intr & sdintr about how pkt_reason 7263 * is used. 7264 */ 7265 scsipkt->pkt_reason = CMD_CMPLT; 7266 7267 /* 7268 * We may not have ARQ data if there was a double 7269 * error. But sense data in sata packet was pre-set 7270 * with NO SENSE so it is valid even if HBA could 7271 * not retrieve a real sense data. 7272 * Just copy this sense data into scsi pkt sense area. 7273 */ 7274 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 7275 SATA_ATAPI_MIN_RQSENSE_LEN); 7276 #ifdef SATA_DEBUG 7277 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 7278 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7279 "sata_txlt_atapi_completion: %02x\n" 7280 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7281 " %02x %02x %02x %02x %02x %02x " 7282 " %02x %02x %02x %02x %02x %02x\n", 7283 scsipkt->pkt_reason, 7284 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7285 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7286 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7287 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7288 rqsp[16], rqsp[17]); 7289 } 7290 #endif 7291 } else { 7292 switch (sata_pkt->satapkt_reason) { 7293 case SATA_PKT_PORT_ERROR: 7294 /* 7295 * We have no device data. 7296 */ 7297 scsipkt->pkt_reason = CMD_INCOMPLETE; 7298 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7299 STATE_GOT_TARGET | STATE_SENT_CMD | 7300 STATE_GOT_STATUS); 7301 sense->es_key = KEY_HARDWARE_ERROR; 7302 break; 7303 7304 case SATA_PKT_TIMEOUT: 7305 scsipkt->pkt_reason = CMD_TIMEOUT; 7306 scsipkt->pkt_statistics |= 7307 STAT_TIMEOUT | STAT_DEV_RESET; 7308 /* 7309 * Need to check if HARDWARE_ERROR/ 7310 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 7311 * appropriate. 7312 */ 7313 break; 7314 7315 case SATA_PKT_ABORTED: 7316 scsipkt->pkt_reason = CMD_ABORTED; 7317 scsipkt->pkt_statistics |= STAT_ABORTED; 7318 /* Should we set key COMMAND_ABPRTED? */ 7319 break; 7320 7321 case SATA_PKT_RESET: 7322 scsipkt->pkt_reason = CMD_RESET; 7323 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7324 /* 7325 * May be we should set Unit Attention / 7326 * Reset. Perhaps the same should be 7327 * returned for disks.... 7328 */ 7329 sense->es_key = KEY_UNIT_ATTENTION; 7330 sense->es_add_code = SD_SCSI_ASC_RESET; 7331 break; 7332 7333 default: 7334 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7335 "sata_txlt_atapi_completion: " 7336 "invalid packet completion reason")); 7337 scsipkt->pkt_reason = CMD_TRAN_ERR; 7338 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7339 STATE_GOT_TARGET | STATE_SENT_CMD | 7340 STATE_GOT_STATUS); 7341 break; 7342 } 7343 } 7344 } 7345 7346 SATAATAPITRACE(spx, 0); 7347 7348 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7349 scsipkt->pkt_comp != NULL) { 7350 /* scsi callback required */ 7351 (*scsipkt->pkt_comp)(scsipkt); 7352 } 7353 } 7354 7355 /* 7356 * Set up error retrieval sata command for ATAPI Packet Command error data 7357 * recovery. 7358 * 7359 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 7360 * returns SATA_FAILURE otherwise. 7361 */ 7362 7363 static int 7364 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 7365 { 7366 sata_pkt_t *spkt = spx->txlt_sata_pkt; 7367 sata_cmd_t *scmd; 7368 struct buf *bp; 7369 7370 /* 7371 * Allocate dma-able buffer error data. 7372 * Buffer allocation will take care of buffer alignment and other DMA 7373 * attributes. 7374 */ 7375 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 7376 if (bp == NULL) { 7377 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 7378 "sata_get_err_retrieval_pkt: " 7379 "cannot allocate buffer for error data", NULL); 7380 return (SATA_FAILURE); 7381 } 7382 bp_mapin(bp); /* make data buffer accessible */ 7383 7384 /* Operation modes are up to the caller */ 7385 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7386 7387 /* Synchronous mode, no callback - may be changed by the caller */ 7388 spkt->satapkt_comp = NULL; 7389 spkt->satapkt_time = sata_default_pkt_time; 7390 7391 scmd = &spkt->satapkt_cmd; 7392 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7393 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7394 7395 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7396 7397 /* 7398 * Set-up acdb. Request Sense CDB (packet command content) is 7399 * not in DMA-able buffer. Its handling is HBA-specific (how 7400 * it is transfered into packet FIS). 7401 */ 7402 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7403 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 7404 /* Following zeroing of pad bytes may not be necessary */ 7405 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 7406 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 7407 7408 /* 7409 * Set-up pointer to the buffer handle, so HBA can sync buffer 7410 * before accessing it. Handle is in usual place in translate struct. 7411 */ 7412 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 7413 7414 /* 7415 * Preset request sense data to NO SENSE. 7416 * Here it is redundant, only for a symetry with scsi-originated 7417 * packets. It should not be used for anything but debugging. 7418 */ 7419 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7420 sata_fixed_sense_data_preset( 7421 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7422 7423 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7424 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7425 7426 return (SATA_SUCCESS); 7427 } 7428 7429 /* 7430 * Set-up ATAPI packet command. 7431 * Data transfer direction has to be set-up in sata_cmd structure prior to 7432 * calling this function. 7433 * 7434 * Returns void 7435 */ 7436 7437 static void 7438 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 7439 { 7440 scmd->satacmd_addr_type = 0; /* N/A */ 7441 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 7442 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 7443 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 7444 scmd->satacmd_lba_high_lsb = 7445 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 7446 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 7447 7448 /* 7449 * We want all data to be transfered via DMA. 7450 * But specify it only if drive supports DMA and DMA mode is 7451 * selected - some drives are sensitive about it. 7452 * Hopefully it wil work for all drives.... 7453 */ 7454 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 7455 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 7456 7457 /* 7458 * Features register requires special care for devices that use 7459 * Serial ATA bridge - they need an explicit specification of 7460 * the data transfer direction for Packet DMA commands. 7461 * Setting this bit is harmless if DMA is not used. 7462 * 7463 * Many drives do not implement word 80, specifying what ATA/ATAPI 7464 * spec they follow. 7465 * We are arbitrarily following the latest SerialATA 2.6 spec, 7466 * which uses ATA/ATAPI 6 specification for Identify Data, unless 7467 * ATA/ATAPI-7 support is explicitly indicated. 7468 */ 7469 if (sdinfo->satadrv_id.ai_majorversion != 0 && 7470 sdinfo->satadrv_id.ai_majorversion != 0xffff && 7471 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 7472 /* 7473 * Specification of major version is valid and version 7 7474 * is supported. It does automatically imply that all 7475 * spec features are supported. For now, we assume that 7476 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 7477 */ 7478 if ((sdinfo->satadrv_id.ai_dirdma & 7479 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 7480 if (scmd->satacmd_flags.sata_data_direction == 7481 SATA_DIR_READ) 7482 scmd->satacmd_features_reg |= 7483 SATA_ATAPI_F_DATA_DIR_READ; 7484 } 7485 } 7486 } 7487 7488 7489 #ifdef SATA_DEBUG 7490 7491 /* Display 18 bytes of Inquiry data */ 7492 static void 7493 sata_show_inqry_data(uint8_t *buf) 7494 { 7495 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 7496 uint8_t *p; 7497 7498 cmn_err(CE_NOTE, "Inquiry data:"); 7499 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 7500 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 7501 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 7502 cmn_err(CE_NOTE, "ATAPI transport version %d", 7503 SATA_ATAPI_TRANS_VERSION(inq)); 7504 cmn_err(CE_NOTE, "response data format %d, aenc %d", 7505 inq->inq_rdf, inq->inq_aenc); 7506 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 7507 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 7508 p = (uint8_t *)inq->inq_vid; 7509 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 7510 "%02x %02x %02x %02x", 7511 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7512 p = (uint8_t *)inq->inq_vid; 7513 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 7514 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7515 7516 p = (uint8_t *)inq->inq_pid; 7517 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 7518 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 7519 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7520 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7521 p = (uint8_t *)inq->inq_pid; 7522 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 7523 "%c %c %c %c %c %c %c %c", 7524 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7525 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7526 7527 p = (uint8_t *)inq->inq_revision; 7528 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 7529 p[0], p[1], p[2], p[3]); 7530 p = (uint8_t *)inq->inq_revision; 7531 cmn_err(CE_NOTE, "revision: %c %c %c %c", 7532 p[0], p[1], p[2], p[3]); 7533 7534 } 7535 7536 7537 static void 7538 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 7539 { 7540 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 7541 7542 if (scsi_pkt == NULL) 7543 return; 7544 if (count != 0) { 7545 /* saving cdb */ 7546 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 7547 SATA_ATAPI_MAX_CDB_LEN); 7548 bcopy(scsi_pkt->pkt_cdbp, 7549 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 7550 } else { 7551 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 7552 sts_sensedata, 7553 sata_atapi_trace[sata_atapi_trace_index].arqs, 7554 SATA_ATAPI_MIN_RQSENSE_LEN); 7555 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 7556 scsi_pkt->pkt_reason; 7557 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 7558 spx->txlt_sata_pkt->satapkt_reason; 7559 7560 if (++sata_atapi_trace_index >= 64) 7561 sata_atapi_trace_index = 0; 7562 } 7563 } 7564 7565 #endif 7566 7567 /* 7568 * Fetch inquiry data from ATAPI device 7569 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 7570 * 7571 * Note: 7572 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 7573 * where the caller expects to see the inquiry data. 7574 * 7575 */ 7576 7577 static int 7578 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 7579 sata_address_t *saddr, struct scsi_inquiry *inq) 7580 { 7581 sata_pkt_txlate_t *spx; 7582 sata_pkt_t *spkt; 7583 struct buf *bp; 7584 sata_drive_info_t *sdinfo; 7585 sata_cmd_t *scmd; 7586 int rval; 7587 uint8_t *rqsp; 7588 #ifdef SATA_DEBUG 7589 char msg_buf[MAXPATHLEN]; 7590 #endif 7591 7592 ASSERT(sata_hba != NULL); 7593 7594 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7595 spx->txlt_sata_hba_inst = sata_hba; 7596 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7597 spkt = sata_pkt_alloc(spx, NULL); 7598 if (spkt == NULL) { 7599 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7600 return (SATA_FAILURE); 7601 } 7602 /* address is needed now */ 7603 spkt->satapkt_device.satadev_addr = *saddr; 7604 7605 /* scsi_inquiry size buffer */ 7606 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 7607 if (bp == NULL) { 7608 sata_pkt_free(spx); 7609 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7610 SATA_LOG_D((sata_hba, CE_WARN, 7611 "sata_get_atapi_inquiry_data: " 7612 "cannot allocate data buffer")); 7613 return (SATA_FAILURE); 7614 } 7615 bp_mapin(bp); /* make data buffer accessible */ 7616 7617 scmd = &spkt->satapkt_cmd; 7618 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7619 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7620 7621 /* Use synchronous mode */ 7622 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7623 spkt->satapkt_comp = NULL; 7624 spkt->satapkt_time = sata_default_pkt_time; 7625 7626 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7627 7628 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7629 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7630 7631 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7632 sdinfo = sata_get_device_info(sata_hba, 7633 &spx->txlt_sata_pkt->satapkt_device); 7634 if (sdinfo == NULL) { 7635 /* we have to be carefull about the disapearing device */ 7636 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7637 rval = SATA_FAILURE; 7638 goto cleanup; 7639 } 7640 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7641 7642 /* 7643 * Set-up acdb. This works for atapi transport version 2 and later. 7644 */ 7645 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7646 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7647 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7648 scmd->satacmd_acdb[1] = 0x00; 7649 scmd->satacmd_acdb[2] = 0x00; 7650 scmd->satacmd_acdb[3] = 0x00; 7651 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7652 scmd->satacmd_acdb[5] = 0x00; 7653 7654 sata_fixed_sense_data_preset( 7655 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7656 7657 /* Transfer command to HBA */ 7658 if (sata_hba_start(spx, &rval) != 0) { 7659 /* Pkt not accepted for execution */ 7660 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7661 "sata_get_atapi_inquiry_data: " 7662 "Packet not accepted for execution - ret: %02x", rval); 7663 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7664 rval = SATA_FAILURE; 7665 goto cleanup; 7666 } 7667 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7668 7669 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7670 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7671 "sata_get_atapi_inquiry_data: " 7672 "Packet completed successfully - ret: %02x", rval); 7673 if (spx->txlt_buf_dma_handle != NULL) { 7674 /* 7675 * Sync buffer. Handle is in usual place in translate 7676 * struct. 7677 */ 7678 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7679 DDI_DMA_SYNC_FORCPU); 7680 ASSERT(rval == DDI_SUCCESS); 7681 } 7682 /* 7683 * Normal completion - copy data into caller's buffer 7684 */ 7685 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 7686 sizeof (struct scsi_inquiry)); 7687 #ifdef SATA_DEBUG 7688 if (sata_debug_flags & SATA_DBG_ATAPI) { 7689 sata_show_inqry_data((uint8_t *)inq); 7690 } 7691 #endif 7692 rval = SATA_SUCCESS; 7693 } else { 7694 /* 7695 * Something went wrong - analyze return - check rqsense data 7696 */ 7697 rval = SATA_FAILURE; 7698 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7699 /* 7700 * ARQ data hopefull show something other than NO SENSE 7701 */ 7702 rqsp = scmd->satacmd_rqsense; 7703 #ifdef SATA_DEBUG 7704 if (sata_debug_flags & SATA_DBG_ATAPI) { 7705 msg_buf[0] = '\0'; 7706 (void) snprintf(msg_buf, MAXPATHLEN, 7707 "ATAPI packet completion reason: %02x\n" 7708 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 7709 " %02x %02x %02x %02x %02x %02x\n" 7710 " %02x %02x %02x %02x %02x %02x", 7711 spkt->satapkt_reason, 7712 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7713 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7714 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7715 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7716 rqsp[16], rqsp[17]); 7717 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7718 "%s", msg_buf); 7719 } 7720 #endif 7721 } else { 7722 switch (spkt->satapkt_reason) { 7723 case SATA_PKT_PORT_ERROR: 7724 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7725 "sata_get_atapi_inquiry_data: " 7726 "packet reason: port error", NULL); 7727 break; 7728 7729 case SATA_PKT_TIMEOUT: 7730 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7731 "sata_get_atapi_inquiry_data: " 7732 "packet reason: timeout", NULL); 7733 break; 7734 7735 case SATA_PKT_ABORTED: 7736 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7737 "sata_get_atapi_inquiry_data: " 7738 "packet reason: aborted", NULL); 7739 break; 7740 7741 case SATA_PKT_RESET: 7742 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7743 "sata_get_atapi_inquiry_data: " 7744 "packet reason: reset\n", NULL); 7745 break; 7746 default: 7747 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7748 "sata_get_atapi_inquiry_data: " 7749 "invalid packet reason: %02x\n", 7750 spkt->satapkt_reason); 7751 break; 7752 } 7753 } 7754 } 7755 cleanup: 7756 sata_free_local_buffer(spx); 7757 sata_pkt_free(spx); 7758 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7759 return (rval); 7760 } 7761 7762 7763 7764 7765 7766 #if 0 7767 #ifdef SATA_DEBUG 7768 7769 /* 7770 * Test ATAPI packet command. 7771 * Single threaded test: send packet command in synch mode, process completion 7772 * 7773 */ 7774 static void 7775 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 7776 { 7777 sata_pkt_txlate_t *spx; 7778 sata_pkt_t *spkt; 7779 struct buf *bp; 7780 sata_device_t sata_device; 7781 sata_drive_info_t *sdinfo; 7782 sata_cmd_t *scmd; 7783 int rval; 7784 uint8_t *rqsp; 7785 7786 ASSERT(sata_hba_inst != NULL); 7787 sata_device.satadev_addr.cport = cport; 7788 sata_device.satadev_addr.pmport = 0; 7789 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 7790 sata_device.satadev_rev = SATA_DEVICE_REV; 7791 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7792 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 7793 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7794 if (sdinfo == NULL) { 7795 sata_log(sata_hba_inst, CE_WARN, 7796 "sata_test_atapi_packet_command: " 7797 "no device info for cport %d", 7798 sata_device.satadev_addr.cport); 7799 return; 7800 } 7801 7802 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7803 spx->txlt_sata_hba_inst = sata_hba_inst; 7804 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7805 spkt = sata_pkt_alloc(spx, NULL); 7806 if (spkt == NULL) { 7807 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7808 return; 7809 } 7810 /* address is needed now */ 7811 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 7812 7813 /* 1024k buffer */ 7814 bp = sata_alloc_local_buffer(spx, 1024); 7815 if (bp == NULL) { 7816 sata_pkt_free(spx); 7817 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7818 sata_log(sata_hba_inst, CE_WARN, 7819 "sata_test_atapi_packet_command: " 7820 "cannot allocate data buffer"); 7821 return; 7822 } 7823 bp_mapin(bp); /* make data buffer accessible */ 7824 7825 scmd = &spkt->satapkt_cmd; 7826 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7827 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7828 7829 /* Use synchronous mode */ 7830 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7831 7832 /* Synchronous mode, no callback - may be changed by the caller */ 7833 spkt->satapkt_comp = NULL; 7834 spkt->satapkt_time = sata_default_pkt_time; 7835 7836 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7837 7838 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7839 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7840 7841 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7842 7843 /* Set-up acdb. */ 7844 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7845 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7846 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7847 scmd->satacmd_acdb[1] = 0x00; 7848 scmd->satacmd_acdb[2] = 0x00; 7849 scmd->satacmd_acdb[3] = 0x00; 7850 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7851 scmd->satacmd_acdb[5] = 0x00; 7852 7853 sata_fixed_sense_data_preset( 7854 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7855 7856 /* Transfer command to HBA */ 7857 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7858 if (sata_hba_start(spx, &rval) != 0) { 7859 /* Pkt not accepted for execution */ 7860 sata_log(sata_hba_inst, CE_WARN, 7861 "sata_test_atapi_packet_command: " 7862 "Packet not accepted for execution - ret: %02x", rval); 7863 mutex_exit( 7864 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7865 goto cleanup; 7866 } 7867 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7868 7869 if (spx->txlt_buf_dma_handle != NULL) { 7870 /* 7871 * Sync buffer. Handle is in usual place in translate struct. 7872 */ 7873 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7874 DDI_DMA_SYNC_FORCPU); 7875 ASSERT(rval == DDI_SUCCESS); 7876 } 7877 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7878 sata_log(sata_hba_inst, CE_WARN, 7879 "sata_test_atapi_packet_command: " 7880 "Packet completed successfully"); 7881 /* 7882 * Normal completion - show inquiry data 7883 */ 7884 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 7885 } else { 7886 /* 7887 * Something went wrong - analyze return - check rqsense data 7888 */ 7889 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7890 /* 7891 * ARQ data hopefull show something other than NO SENSE 7892 */ 7893 rqsp = scmd->satacmd_rqsense; 7894 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7895 "ATAPI packet completion reason: %02x\n" 7896 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7897 " %02x %02x %02x %02x %02x %02x " 7898 " %02x %02x %02x %02x %02x %02x\n", 7899 spkt->satapkt_reason, 7900 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7901 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7902 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7903 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7904 rqsp[16], rqsp[17]); 7905 } else { 7906 switch (spkt->satapkt_reason) { 7907 case SATA_PKT_PORT_ERROR: 7908 sata_log(sata_hba_inst, CE_WARN, 7909 "sata_test_atapi_packet_command: " 7910 "packet reason: port error\n"); 7911 break; 7912 7913 case SATA_PKT_TIMEOUT: 7914 sata_log(sata_hba_inst, CE_WARN, 7915 "sata_test_atapi_packet_command: " 7916 "packet reason: timeout\n"); 7917 break; 7918 7919 case SATA_PKT_ABORTED: 7920 sata_log(sata_hba_inst, CE_WARN, 7921 "sata_test_atapi_packet_command: " 7922 "packet reason: aborted\n"); 7923 break; 7924 7925 case SATA_PKT_RESET: 7926 sata_log(sata_hba_inst, CE_WARN, 7927 "sata_test_atapi_packet_command: " 7928 "packet reason: reset\n"); 7929 break; 7930 default: 7931 sata_log(sata_hba_inst, CE_WARN, 7932 "sata_test_atapi_packet_command: " 7933 "invalid packet reason: %02x\n", 7934 spkt->satapkt_reason); 7935 break; 7936 } 7937 } 7938 } 7939 cleanup: 7940 sata_free_local_buffer(spx); 7941 sata_pkt_free(spx); 7942 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7943 } 7944 7945 #endif /* SATA_DEBUG */ 7946 #endif /* 1 */ 7947 7948 7949 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 7950 7951 /* 7952 * Validate sata_tran info 7953 * SATA_FAILURE returns if structure is inconsistent or structure revision 7954 * does not match one used by the framework. 7955 * 7956 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 7957 * required function pointers. 7958 * Returns SATA_FAILURE otherwise. 7959 */ 7960 static int 7961 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 7962 { 7963 /* 7964 * SATA_TRAN_HBA_REV is the current (highest) revision number 7965 * of the SATA interface. 7966 */ 7967 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 7968 sata_log(NULL, CE_WARN, 7969 "sata: invalid sata_hba_tran version %d for driver %s", 7970 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 7971 return (SATA_FAILURE); 7972 } 7973 7974 if (dip != sata_tran->sata_tran_hba_dip) { 7975 SATA_LOG_D((NULL, CE_WARN, 7976 "sata: inconsistent sata_tran_hba_dip " 7977 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 7978 return (SATA_FAILURE); 7979 } 7980 7981 if (sata_tran->sata_tran_probe_port == NULL || 7982 sata_tran->sata_tran_start == NULL || 7983 sata_tran->sata_tran_abort == NULL || 7984 sata_tran->sata_tran_reset_dport == NULL || 7985 sata_tran->sata_tran_hotplug_ops == NULL || 7986 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 7987 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 7988 NULL) { 7989 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 7990 "required functions")); 7991 } 7992 return (SATA_SUCCESS); 7993 } 7994 7995 /* 7996 * Remove HBA instance from sata_hba_list. 7997 */ 7998 static void 7999 sata_remove_hba_instance(dev_info_t *dip) 8000 { 8001 sata_hba_inst_t *sata_hba_inst; 8002 8003 mutex_enter(&sata_mutex); 8004 for (sata_hba_inst = sata_hba_list; 8005 sata_hba_inst != (struct sata_hba_inst *)NULL; 8006 sata_hba_inst = sata_hba_inst->satahba_next) { 8007 if (sata_hba_inst->satahba_dip == dip) 8008 break; 8009 } 8010 8011 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 8012 #ifdef SATA_DEBUG 8013 cmn_err(CE_WARN, "sata_remove_hba_instance: " 8014 "unknown HBA instance\n"); 8015 #endif 8016 ASSERT(FALSE); 8017 } 8018 if (sata_hba_inst == sata_hba_list) { 8019 sata_hba_list = sata_hba_inst->satahba_next; 8020 if (sata_hba_list) { 8021 sata_hba_list->satahba_prev = 8022 (struct sata_hba_inst *)NULL; 8023 } 8024 if (sata_hba_inst == sata_hba_list_tail) { 8025 sata_hba_list_tail = NULL; 8026 } 8027 } else if (sata_hba_inst == sata_hba_list_tail) { 8028 sata_hba_list_tail = sata_hba_inst->satahba_prev; 8029 if (sata_hba_list_tail) { 8030 sata_hba_list_tail->satahba_next = 8031 (struct sata_hba_inst *)NULL; 8032 } 8033 } else { 8034 sata_hba_inst->satahba_prev->satahba_next = 8035 sata_hba_inst->satahba_next; 8036 sata_hba_inst->satahba_next->satahba_prev = 8037 sata_hba_inst->satahba_prev; 8038 } 8039 mutex_exit(&sata_mutex); 8040 } 8041 8042 8043 8044 8045 8046 /* 8047 * Probe all SATA ports of the specified HBA instance. 8048 * The assumption is that there are no target and attachment point minor nodes 8049 * created by the boot subsystems, so we do not need to prune device tree. 8050 * 8051 * This function is called only from sata_hba_attach(). It does not have to 8052 * be protected by controller mutex, because the hba_attached flag is not set 8053 * yet and no one would be touching this HBA instance other than this thread. 8054 * Determines if port is active and what type of the device is attached 8055 * (if any). Allocates necessary structures for each port. 8056 * 8057 * An AP (Attachement Point) node is created for each SATA device port even 8058 * when there is no device attached. 8059 */ 8060 8061 static void 8062 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 8063 { 8064 dev_info_t *dip = SATA_DIP(sata_hba_inst); 8065 int ncport, npmport; 8066 sata_cport_info_t *cportinfo; 8067 sata_drive_info_t *drive; 8068 sata_pmult_info_t *pminfo; 8069 sata_pmport_info_t *pmportinfo; 8070 sata_device_t sata_device; 8071 int rval; 8072 dev_t minor_number; 8073 char name[16]; 8074 clock_t start_time, cur_time; 8075 8076 /* 8077 * Probe controller ports first, to find port status and 8078 * any port multiplier attached. 8079 */ 8080 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 8081 /* allocate cport structure */ 8082 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 8083 ASSERT(cportinfo != NULL); 8084 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 8085 8086 mutex_enter(&cportinfo->cport_mutex); 8087 8088 cportinfo->cport_addr.cport = ncport; 8089 cportinfo->cport_addr.pmport = 0; 8090 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 8091 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8092 cportinfo->cport_state |= SATA_STATE_PROBING; 8093 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 8094 8095 /* 8096 * Regardless if a port is usable or not, create 8097 * an attachment point 8098 */ 8099 mutex_exit(&cportinfo->cport_mutex); 8100 minor_number = 8101 SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0); 8102 (void) sprintf(name, "%d", ncport); 8103 if (ddi_create_minor_node(dip, name, S_IFCHR, 8104 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 8105 DDI_SUCCESS) { 8106 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 8107 "cannot create SATA attachment point for port %d", 8108 ncport); 8109 } 8110 8111 /* Probe port */ 8112 start_time = ddi_get_lbolt(); 8113 reprobe_cport: 8114 sata_device.satadev_addr.cport = ncport; 8115 sata_device.satadev_addr.pmport = 0; 8116 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 8117 sata_device.satadev_rev = SATA_DEVICE_REV; 8118 8119 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8120 (dip, &sata_device); 8121 8122 mutex_enter(&cportinfo->cport_mutex); 8123 sata_update_port_scr(&cportinfo->cport_scr, &sata_device); 8124 if (rval != SATA_SUCCESS) { 8125 /* Something went wrong? Fail the port */ 8126 cportinfo->cport_state = SATA_PSTATE_FAILED; 8127 mutex_exit(&cportinfo->cport_mutex); 8128 continue; 8129 } 8130 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8131 cportinfo->cport_state |= SATA_STATE_PROBED; 8132 cportinfo->cport_dev_type = sata_device.satadev_type; 8133 8134 cportinfo->cport_state |= SATA_STATE_READY; 8135 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 8136 mutex_exit(&cportinfo->cport_mutex); 8137 continue; 8138 } 8139 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8140 /* 8141 * There is some device attached. 8142 * Allocate device info structure 8143 */ 8144 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 8145 mutex_exit(&cportinfo->cport_mutex); 8146 SATA_CPORTINFO_DRV_INFO(cportinfo) = 8147 kmem_zalloc(sizeof (sata_drive_info_t), 8148 KM_SLEEP); 8149 mutex_enter(&cportinfo->cport_mutex); 8150 } 8151 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 8152 drive->satadrv_addr = cportinfo->cport_addr; 8153 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 8154 drive->satadrv_type = cportinfo->cport_dev_type; 8155 drive->satadrv_state = SATA_STATE_UNKNOWN; 8156 8157 mutex_exit(&cportinfo->cport_mutex); 8158 if (sata_add_device(dip, sata_hba_inst, ncport, 0) != 8159 SATA_SUCCESS) { 8160 /* 8161 * Plugged device was not correctly identified. 8162 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 8163 */ 8164 cur_time = ddi_get_lbolt(); 8165 if ((cur_time - start_time) < 8166 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 8167 /* sleep for a while */ 8168 delay(drv_usectohz( 8169 SATA_DEV_RETRY_DLY)); 8170 goto reprobe_cport; 8171 } 8172 } 8173 } else { 8174 mutex_exit(&cportinfo->cport_mutex); 8175 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 8176 pminfo = kmem_zalloc(sizeof (sata_pmult_info_t), 8177 KM_SLEEP); 8178 mutex_enter(&cportinfo->cport_mutex); 8179 ASSERT(pminfo != NULL); 8180 SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo; 8181 pminfo->pmult_addr.cport = cportinfo->cport_addr.cport; 8182 pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT; 8183 pminfo->pmult_addr.qual = SATA_ADDR_PMPORT; 8184 pminfo->pmult_num_dev_ports = 8185 sata_device.satadev_add_info; 8186 mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER, 8187 NULL); 8188 pminfo->pmult_state = SATA_STATE_PROBING; 8189 mutex_exit(&cportinfo->cport_mutex); 8190 8191 /* Probe Port Multiplier ports */ 8192 for (npmport = 0; 8193 npmport < pminfo->pmult_num_dev_ports; 8194 npmport++) { 8195 pmportinfo = kmem_zalloc( 8196 sizeof (sata_pmport_info_t), KM_SLEEP); 8197 mutex_enter(&cportinfo->cport_mutex); 8198 ASSERT(pmportinfo != NULL); 8199 pmportinfo->pmport_addr.cport = ncport; 8200 pmportinfo->pmport_addr.pmport = npmport; 8201 pmportinfo->pmport_addr.qual = 8202 SATA_ADDR_PMPORT; 8203 pminfo->pmult_dev_port[npmport] = pmportinfo; 8204 8205 mutex_init(&pmportinfo->pmport_mutex, NULL, 8206 MUTEX_DRIVER, NULL); 8207 8208 mutex_exit(&cportinfo->cport_mutex); 8209 8210 /* Create an attachment point */ 8211 minor_number = SATA_MAKE_AP_MINOR( 8212 ddi_get_instance(dip), ncport, npmport, 1); 8213 (void) sprintf(name, "%d.%d", ncport, npmport); 8214 if (ddi_create_minor_node(dip, name, S_IFCHR, 8215 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 8216 0) != DDI_SUCCESS) { 8217 sata_log(sata_hba_inst, CE_WARN, 8218 "sata_hba_attach: " 8219 "cannot create SATA attachment " 8220 "point for port %d pmult port %d", 8221 ncport, npmport); 8222 } 8223 8224 start_time = ddi_get_lbolt(); 8225 reprobe_pmport: 8226 sata_device.satadev_addr.pmport = npmport; 8227 sata_device.satadev_addr.qual = 8228 SATA_ADDR_PMPORT; 8229 8230 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8231 (dip, &sata_device); 8232 mutex_enter(&cportinfo->cport_mutex); 8233 8234 /* sata_update_port_info() */ 8235 sata_update_port_scr(&pmportinfo->pmport_scr, 8236 &sata_device); 8237 8238 if (rval != SATA_SUCCESS) { 8239 pmportinfo->pmport_state = 8240 SATA_PSTATE_FAILED; 8241 mutex_exit(&cportinfo->cport_mutex); 8242 continue; 8243 } 8244 pmportinfo->pmport_state &= 8245 ~SATA_STATE_PROBING; 8246 pmportinfo->pmport_state |= SATA_STATE_PROBED; 8247 pmportinfo->pmport_dev_type = 8248 sata_device.satadev_type; 8249 8250 pmportinfo->pmport_state |= SATA_STATE_READY; 8251 if (pmportinfo->pmport_dev_type == 8252 SATA_DTYPE_NONE) { 8253 mutex_exit(&cportinfo->cport_mutex); 8254 continue; 8255 } 8256 /* Port multipliers cannot be chained */ 8257 ASSERT(pmportinfo->pmport_dev_type != 8258 SATA_DTYPE_PMULT); 8259 /* 8260 * There is something attached to Port 8261 * Multiplier device port 8262 * Allocate device info structure 8263 */ 8264 if (pmportinfo->pmport_sata_drive == NULL) { 8265 mutex_exit(&cportinfo->cport_mutex); 8266 pmportinfo->pmport_sata_drive = 8267 kmem_zalloc( 8268 sizeof (sata_drive_info_t), 8269 KM_SLEEP); 8270 mutex_enter(&cportinfo->cport_mutex); 8271 } 8272 drive = pmportinfo->pmport_sata_drive; 8273 drive->satadrv_addr.cport = 8274 pmportinfo->pmport_addr.cport; 8275 drive->satadrv_addr.pmport = npmport; 8276 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 8277 drive->satadrv_type = pmportinfo-> 8278 pmport_dev_type; 8279 drive->satadrv_state = SATA_STATE_UNKNOWN; 8280 8281 mutex_exit(&cportinfo->cport_mutex); 8282 if (sata_add_device(dip, sata_hba_inst, ncport, 8283 npmport) != SATA_SUCCESS) { 8284 /* 8285 * Plugged device was not correctly 8286 * identified. Retry, within the 8287 * SATA_DEV_IDENTIFY_TIMEOUT 8288 */ 8289 cur_time = ddi_get_lbolt(); 8290 if ((cur_time - start_time) < 8291 drv_usectohz( 8292 SATA_DEV_IDENTIFY_TIMEOUT)) { 8293 /* sleep for a while */ 8294 delay(drv_usectohz( 8295 SATA_DEV_RETRY_DLY)); 8296 goto reprobe_pmport; 8297 } 8298 } 8299 } 8300 pmportinfo->pmport_state = 8301 SATA_STATE_PROBED | SATA_STATE_READY; 8302 } 8303 } 8304 } 8305 8306 /* 8307 * Add SATA device for specified HBA instance & port (SCSI target 8308 * device nodes). 8309 * This function is called (indirectly) only from sata_hba_attach(). 8310 * A target node is created when there is a supported type device attached, 8311 * but may be removed if it cannot be put online. 8312 * 8313 * This function cannot be called from an interrupt context. 8314 * 8315 * ONLY DISK TARGET NODES ARE CREATED NOW 8316 * 8317 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 8318 * device identification failed - adding a device could be retried. 8319 * 8320 */ 8321 static int 8322 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport, 8323 int pmport) 8324 { 8325 sata_cport_info_t *cportinfo; 8326 sata_pmult_info_t *pminfo; 8327 sata_pmport_info_t *pmportinfo; 8328 dev_info_t *cdip; /* child dip */ 8329 sata_device_t sata_device; 8330 int rval; 8331 8332 8333 8334 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 8335 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 8336 mutex_enter(&cportinfo->cport_mutex); 8337 /* 8338 * Some device is attached to a controller port. 8339 * We rely on controllers distinquishing between no-device, 8340 * attached port multiplier and other kind of attached device. 8341 * We need to get Identify Device data and determine 8342 * positively the dev type before trying to attach 8343 * the target driver. 8344 */ 8345 sata_device.satadev_rev = SATA_DEVICE_REV; 8346 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8347 /* 8348 * Not port multiplier. 8349 */ 8350 sata_device.satadev_addr = cportinfo->cport_addr; 8351 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8352 mutex_exit(&cportinfo->cport_mutex); 8353 8354 rval = sata_probe_device(sata_hba_inst, &sata_device); 8355 if (rval != SATA_SUCCESS || 8356 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) 8357 return (SATA_FAILURE); 8358 8359 mutex_enter(&cportinfo->cport_mutex); 8360 sata_show_drive_info(sata_hba_inst, 8361 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8362 8363 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8364 /* 8365 * Could not determine device type or 8366 * a device is not supported. 8367 * Degrade this device to unknown. 8368 */ 8369 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8370 mutex_exit(&cportinfo->cport_mutex); 8371 return (SATA_SUCCESS); 8372 } 8373 cportinfo->cport_dev_type = sata_device.satadev_type; 8374 cportinfo->cport_tgtnode_clean = B_TRUE; 8375 mutex_exit(&cportinfo->cport_mutex); 8376 8377 /* 8378 * Initialize device to the desired state. Even if it 8379 * fails, the device will still attach but syslog 8380 * will show the warning. 8381 */ 8382 if (sata_initialize_device(sata_hba_inst, 8383 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 8384 /* Retry */ 8385 rval = sata_initialize_device(sata_hba_inst, 8386 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8387 8388 if (rval == SATA_RETRY) 8389 sata_log(sata_hba_inst, CE_WARN, 8390 "SATA device at port %d - " 8391 "default device features could not be set." 8392 " Device may not operate as expected.", 8393 cportinfo->cport_addr.cport); 8394 } 8395 8396 cdip = sata_create_target_node(pdip, sata_hba_inst, 8397 &sata_device.satadev_addr); 8398 mutex_enter(&cportinfo->cport_mutex); 8399 if (cdip == NULL) { 8400 /* 8401 * Attaching target node failed. 8402 * We retain sata_drive_info structure... 8403 */ 8404 mutex_exit(&cportinfo->cport_mutex); 8405 return (SATA_SUCCESS); 8406 } 8407 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8408 satadrv_state = SATA_STATE_READY; 8409 } else { 8410 /* This must be Port Multiplier type */ 8411 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8412 SATA_LOG_D((sata_hba_inst, CE_WARN, 8413 "sata_add_device: " 8414 "unrecognized dev type %x", 8415 cportinfo->cport_dev_type)); 8416 mutex_exit(&cportinfo->cport_mutex); 8417 return (SATA_SUCCESS); 8418 } 8419 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 8420 pmportinfo = pminfo->pmult_dev_port[pmport]; 8421 sata_device.satadev_addr = pmportinfo->pmport_addr; 8422 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 8423 mutex_exit(&cportinfo->cport_mutex); 8424 8425 rval = sata_probe_device(sata_hba_inst, &sata_device); 8426 if (rval != SATA_SUCCESS || 8427 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) { 8428 return (SATA_FAILURE); 8429 } 8430 mutex_enter(&cportinfo->cport_mutex); 8431 sata_show_drive_info(sata_hba_inst, 8432 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8433 8434 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8435 /* 8436 * Could not determine device type. 8437 * Degrade this device to unknown. 8438 */ 8439 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 8440 mutex_exit(&cportinfo->cport_mutex); 8441 return (SATA_SUCCESS); 8442 } 8443 pmportinfo->pmport_dev_type = sata_device.satadev_type; 8444 pmportinfo->pmport_tgtnode_clean = B_TRUE; 8445 mutex_exit(&cportinfo->cport_mutex); 8446 8447 /* 8448 * Initialize device to the desired state. 8449 * Even if it fails, the device will still 8450 * attach but syslog will show the warning. 8451 */ 8452 if (sata_initialize_device(sata_hba_inst, 8453 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 8454 /* Retry */ 8455 rval = sata_initialize_device(sata_hba_inst, 8456 pmportinfo->pmport_sata_drive); 8457 8458 if (rval == SATA_RETRY) 8459 sata_log(sata_hba_inst, CE_WARN, 8460 "SATA device at port %d pmport %d - " 8461 "default device features could not be set." 8462 " Device may not operate as expected.", 8463 pmportinfo->pmport_addr.cport, 8464 pmportinfo->pmport_addr.pmport); 8465 } 8466 cdip = sata_create_target_node(pdip, sata_hba_inst, 8467 &sata_device.satadev_addr); 8468 mutex_enter(&cportinfo->cport_mutex); 8469 if (cdip == NULL) { 8470 /* 8471 * Attaching target node failed. 8472 * We retain sata_drive_info structure... 8473 */ 8474 mutex_exit(&cportinfo->cport_mutex); 8475 return (SATA_SUCCESS); 8476 } 8477 pmportinfo->pmport_sata_drive->satadrv_state |= 8478 SATA_STATE_READY; 8479 } 8480 mutex_exit(&cportinfo->cport_mutex); 8481 return (SATA_SUCCESS); 8482 } 8483 8484 8485 8486 /* 8487 * Create scsi target node for attached device, create node properties and 8488 * attach the node. 8489 * The node could be removed if the device onlining fails. 8490 * 8491 * A dev_info_t pointer is returned if operation is successful, NULL is 8492 * returned otherwise. 8493 * 8494 * No port multiplier support. 8495 */ 8496 8497 static dev_info_t * 8498 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 8499 sata_address_t *sata_addr) 8500 { 8501 dev_info_t *cdip = NULL; 8502 int rval; 8503 char *nname = NULL; 8504 char **compatible = NULL; 8505 int ncompatible; 8506 struct scsi_inquiry inq; 8507 sata_device_t sata_device; 8508 sata_drive_info_t *sdinfo; 8509 int target; 8510 int i; 8511 8512 sata_device.satadev_rev = SATA_DEVICE_REV; 8513 sata_device.satadev_addr = *sata_addr; 8514 8515 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 8516 8517 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8518 8519 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 8520 sata_addr->pmport, sata_addr->qual); 8521 8522 if (sdinfo == NULL) { 8523 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8524 sata_addr->cport))); 8525 SATA_LOG_D((sata_hba_inst, CE_WARN, 8526 "sata_create_target_node: no sdinfo for target %x", 8527 target)); 8528 return (NULL); 8529 } 8530 8531 /* 8532 * create or get scsi inquiry data, expected by 8533 * scsi_hba_nodename_compatible_get() 8534 * SATA hard disks get Identify Data translated into Inguiry Data. 8535 * ATAPI devices respond directly to Inquiry request. 8536 */ 8537 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 8538 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 8539 (uint8_t *)&inq); 8540 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8541 sata_addr->cport))); 8542 } else { /* Assume supported ATAPI device */ 8543 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8544 sata_addr->cport))); 8545 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 8546 &inq) == SATA_FAILURE) 8547 return (NULL); 8548 /* 8549 * Save supported ATAPI transport version 8550 */ 8551 sdinfo->satadrv_atapi_trans_ver = 8552 SATA_ATAPI_TRANS_VERSION(&inq); 8553 } 8554 8555 /* determine the node name and compatible */ 8556 scsi_hba_nodename_compatible_get(&inq, NULL, 8557 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 8558 8559 #ifdef SATA_DEBUG 8560 if (sata_debug_flags & SATA_DBG_NODES) { 8561 if (nname == NULL) { 8562 cmn_err(CE_NOTE, "sata_create_target_node: " 8563 "cannot determine nodename for target %d\n", 8564 target); 8565 } else { 8566 cmn_err(CE_WARN, "sata_create_target_node: " 8567 "target %d nodename: %s\n", target, nname); 8568 } 8569 if (compatible == NULL) { 8570 cmn_err(CE_WARN, 8571 "sata_create_target_node: no compatible name\n"); 8572 } else { 8573 for (i = 0; i < ncompatible; i++) { 8574 cmn_err(CE_WARN, "sata_create_target_node: " 8575 "compatible name: %s\n", compatible[i]); 8576 } 8577 } 8578 } 8579 #endif 8580 8581 /* if nodename can't be determined, log error and exit */ 8582 if (nname == NULL) { 8583 SATA_LOG_D((sata_hba_inst, CE_WARN, 8584 "sata_create_target_node: cannot determine nodename " 8585 "for target %d\n", target)); 8586 scsi_hba_nodename_compatible_free(nname, compatible); 8587 return (NULL); 8588 } 8589 /* 8590 * Create scsi target node 8591 */ 8592 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 8593 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8594 "device-type", "scsi"); 8595 8596 if (rval != DDI_PROP_SUCCESS) { 8597 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8598 "updating device_type prop failed %d", rval)); 8599 goto fail; 8600 } 8601 8602 /* 8603 * Create target node properties: target & lun 8604 */ 8605 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 8606 if (rval != DDI_PROP_SUCCESS) { 8607 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8608 "updating target prop failed %d", rval)); 8609 goto fail; 8610 } 8611 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 8612 if (rval != DDI_PROP_SUCCESS) { 8613 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8614 "updating target prop failed %d", rval)); 8615 goto fail; 8616 } 8617 8618 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 8619 /* 8620 * Add "variant" property 8621 */ 8622 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8623 "variant", "atapi"); 8624 if (rval != DDI_PROP_SUCCESS) { 8625 SATA_LOG_D((sata_hba_inst, CE_WARN, 8626 "sata_create_target_node: variant atapi " 8627 "property could not be created: %d", rval)); 8628 goto fail; 8629 } 8630 } 8631 /* decorate the node with compatible */ 8632 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 8633 compatible, ncompatible) != DDI_PROP_SUCCESS) { 8634 SATA_LOG_D((sata_hba_inst, CE_WARN, 8635 "sata_create_target_node: FAIL compatible props cdip 0x%p", 8636 (void *)cdip)); 8637 goto fail; 8638 } 8639 8640 8641 /* 8642 * Now, try to attach the driver. If probing of the device fails, 8643 * the target node may be removed 8644 */ 8645 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 8646 8647 scsi_hba_nodename_compatible_free(nname, compatible); 8648 8649 if (rval == NDI_SUCCESS) 8650 return (cdip); 8651 8652 /* target node was removed - are we sure? */ 8653 return (NULL); 8654 8655 fail: 8656 scsi_hba_nodename_compatible_free(nname, compatible); 8657 ddi_prop_remove_all(cdip); 8658 rval = ndi_devi_free(cdip); 8659 if (rval != NDI_SUCCESS) { 8660 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8661 "node removal failed %d", rval)); 8662 } 8663 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 8664 "cannot create target node for SATA device at port %d", 8665 sata_addr->cport); 8666 return (NULL); 8667 } 8668 8669 8670 8671 /* 8672 * Re-probe sata port, check for a device and attach info 8673 * structures when necessary. Identify Device data is fetched, if possible. 8674 * Assumption: sata address is already validated. 8675 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 8676 * the presence of a device and its type. 8677 * 8678 * flag arg specifies that the function should try multiple times to identify 8679 * device type and to initialize it, or it should return immediately on failure. 8680 * SATA_DEV_IDENTIFY_RETRY - retry 8681 * SATA_DEV_IDENTIFY_NORETRY - no retry 8682 * 8683 * SATA_FAILURE is returned if one of the operations failed. 8684 * 8685 * This function cannot be called in interrupt context - it may sleep. 8686 * 8687 * NOte: Port multiplier is not supported yet, although there may be some 8688 * pieces of code referencing to it. 8689 */ 8690 static int 8691 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 8692 int flag) 8693 { 8694 sata_cport_info_t *cportinfo; 8695 sata_drive_info_t *sdinfo, *osdinfo; 8696 boolean_t init_device = B_FALSE; 8697 int prev_device_type = SATA_DTYPE_NONE; 8698 int prev_device_settings = 0; 8699 int prev_device_state = 0; 8700 clock_t start_time; 8701 int retry = B_FALSE; 8702 int rval_probe, rval_init; 8703 8704 /* We only care about host sata cport for now */ 8705 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 8706 sata_device->satadev_addr.cport); 8707 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8708 if (osdinfo != NULL) { 8709 /* 8710 * We are re-probing port with a previously attached device. 8711 * Save previous device type and settings. 8712 */ 8713 prev_device_type = cportinfo->cport_dev_type; 8714 prev_device_settings = osdinfo->satadrv_settings; 8715 prev_device_state = osdinfo->satadrv_state; 8716 } 8717 if (flag == SATA_DEV_IDENTIFY_RETRY) { 8718 start_time = ddi_get_lbolt(); 8719 retry = B_TRUE; 8720 } 8721 retry_probe: 8722 8723 /* probe port */ 8724 mutex_enter(&cportinfo->cport_mutex); 8725 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8726 cportinfo->cport_state |= SATA_STATE_PROBING; 8727 mutex_exit(&cportinfo->cport_mutex); 8728 8729 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8730 (SATA_DIP(sata_hba_inst), sata_device); 8731 8732 mutex_enter(&cportinfo->cport_mutex); 8733 if (rval_probe != SATA_SUCCESS) { 8734 cportinfo->cport_state = SATA_PSTATE_FAILED; 8735 mutex_exit(&cportinfo->cport_mutex); 8736 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 8737 "SATA port %d probing failed", 8738 cportinfo->cport_addr.cport)); 8739 return (SATA_FAILURE); 8740 } 8741 8742 /* 8743 * update sata port state and set device type 8744 */ 8745 sata_update_port_info(sata_hba_inst, sata_device); 8746 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8747 8748 /* 8749 * Sanity check - Port is active? Is the link active? 8750 * Is there any device attached? 8751 */ 8752 if ((cportinfo->cport_state & 8753 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 8754 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 8755 SATA_PORT_DEVLINK_UP) { 8756 /* 8757 * Port in non-usable state or no link active/no device. 8758 * Free info structure if necessary (direct attached drive 8759 * only, for now! 8760 */ 8761 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8762 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8763 /* Add here differentiation for device attached or not */ 8764 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8765 mutex_exit(&cportinfo->cport_mutex); 8766 if (sdinfo != NULL) 8767 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8768 return (SATA_SUCCESS); 8769 } 8770 8771 cportinfo->cport_state |= SATA_STATE_READY; 8772 cportinfo->cport_dev_type = sata_device->satadev_type; 8773 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8774 8775 /* 8776 * If we are re-probing the port, there may be 8777 * sata_drive_info structure attached 8778 * (or sata_pm_info, if PMult is supported). 8779 */ 8780 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 8781 /* 8782 * There is no device, so remove device info structure, 8783 * if necessary. 8784 * Only direct attached drive is considered now, until 8785 * port multiplier is supported. If the previously 8786 * attached device was a port multiplier, we would need 8787 * to take care of devices attached beyond the port 8788 * multiplier. 8789 */ 8790 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8791 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8792 if (sdinfo != NULL) { 8793 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8794 sata_log(sata_hba_inst, CE_WARN, 8795 "SATA device detached " 8796 "from port %d", cportinfo->cport_addr.cport); 8797 } 8798 mutex_exit(&cportinfo->cport_mutex); 8799 return (SATA_SUCCESS); 8800 } 8801 8802 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 8803 if (sdinfo == NULL) { 8804 /* 8805 * There is some device attached, but there is 8806 * no sata_drive_info structure - allocate one 8807 */ 8808 mutex_exit(&cportinfo->cport_mutex); 8809 sdinfo = kmem_zalloc( 8810 sizeof (sata_drive_info_t), KM_SLEEP); 8811 mutex_enter(&cportinfo->cport_mutex); 8812 /* 8813 * Recheck, that the port state did not change when we 8814 * released mutex. 8815 */ 8816 if (cportinfo->cport_state & SATA_STATE_READY) { 8817 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 8818 sdinfo->satadrv_addr = cportinfo->cport_addr; 8819 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 8820 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8821 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 8822 } else { 8823 /* 8824 * Port is not in ready state, we 8825 * cannot attach a device. 8826 */ 8827 mutex_exit(&cportinfo->cport_mutex); 8828 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8829 return (SATA_SUCCESS); 8830 } 8831 /* 8832 * Since we are adding device, presumably new one, 8833 * indicate that it should be initalized, 8834 * as well as some internal framework states). 8835 */ 8836 init_device = B_TRUE; 8837 } 8838 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8839 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 8840 } else { 8841 /* 8842 * The device is a port multiplier - not handled now. 8843 */ 8844 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8845 mutex_exit(&cportinfo->cport_mutex); 8846 return (SATA_SUCCESS); 8847 } 8848 mutex_exit(&cportinfo->cport_mutex); 8849 /* 8850 * Figure out what kind of device we are really 8851 * dealing with. Failure of identifying device does not fail this 8852 * function. 8853 */ 8854 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 8855 rval_init = SATA_FAILURE; 8856 mutex_enter(&cportinfo->cport_mutex); 8857 if (rval_probe == SATA_SUCCESS) { 8858 /* 8859 * If we are dealing with the same type of a device as before, 8860 * restore its settings flags. 8861 */ 8862 if (osdinfo != NULL && 8863 sata_device->satadev_type == prev_device_type) 8864 sdinfo->satadrv_settings = prev_device_settings; 8865 8866 mutex_exit(&cportinfo->cport_mutex); 8867 rval_init = SATA_SUCCESS; 8868 /* Set initial device features, if necessary */ 8869 if (init_device == B_TRUE) { 8870 rval_init = sata_initialize_device(sata_hba_inst, 8871 sdinfo); 8872 } 8873 if (rval_init == SATA_SUCCESS) 8874 return (rval_init); 8875 /* else we will retry if retry was asked for */ 8876 8877 } else { 8878 /* 8879 * If there was some device info before we probe the device, 8880 * restore previous device setting, so we can retry from scratch 8881 * later. Providing, of course, that device has not disapear 8882 * during probing process. 8883 */ 8884 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 8885 if (osdinfo != NULL) { 8886 cportinfo->cport_dev_type = prev_device_type; 8887 sdinfo->satadrv_type = prev_device_type; 8888 sdinfo->satadrv_state = prev_device_state; 8889 } 8890 } else { 8891 /* device is gone */ 8892 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8893 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8894 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8895 mutex_exit(&cportinfo->cport_mutex); 8896 return (SATA_SUCCESS); 8897 } 8898 mutex_exit(&cportinfo->cport_mutex); 8899 } 8900 8901 if (retry) { 8902 clock_t cur_time = ddi_get_lbolt(); 8903 /* 8904 * A device was not successfully identified or initialized. 8905 * Track retry time for device identification. 8906 */ 8907 if ((cur_time - start_time) < 8908 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 8909 /* sleep for a while */ 8910 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 8911 goto retry_probe; 8912 } 8913 /* else no more retries */ 8914 mutex_enter(&cportinfo->cport_mutex); 8915 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 8916 if (rval_init == SATA_RETRY) { 8917 /* 8918 * Setting drive features have failed, but 8919 * because the drive is still accessible, 8920 * keep it and emit a warning message. 8921 */ 8922 sata_log(sata_hba_inst, CE_WARN, 8923 "SATA device at port %d - desired " 8924 "drive features could not be set. " 8925 "Device may not operate as expected.", 8926 cportinfo->cport_addr.cport); 8927 } else { 8928 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 8929 satadrv_state = SATA_DSTATE_FAILED; 8930 } 8931 } 8932 mutex_exit(&cportinfo->cport_mutex); 8933 } 8934 return (SATA_SUCCESS); 8935 } 8936 8937 /* 8938 * Initialize device 8939 * Specified device is initialized to a default state. 8940 * 8941 * Returns SATA_SUCCESS if all device features are set successfully, 8942 * SATA_RETRY if device is accessible but device features were not set 8943 * successfully, and SATA_FAILURE otherwise. 8944 */ 8945 static int 8946 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 8947 sata_drive_info_t *sdinfo) 8948 { 8949 int rval; 8950 8951 sata_save_drive_settings(sdinfo); 8952 8953 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 8954 8955 sata_init_write_cache_mode(sdinfo); 8956 8957 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 8958 8959 /* Determine current data transfer mode */ 8960 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 8961 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8962 } else if ((sdinfo->satadrv_id.ai_validinfo & 8963 SATA_VALIDINFO_88) != 0 && 8964 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 8965 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8966 } else if ((sdinfo->satadrv_id.ai_dworddma & 8967 SATA_MDMA_SEL_MASK) != 0) { 8968 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8969 } else 8970 /* DMA supported, not no DMA transfer mode is selected !? */ 8971 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8972 8973 return (rval); 8974 } 8975 8976 8977 /* 8978 * Initialize write cache mode. 8979 * 8980 * The default write cache setting for SATA HDD is provided by sata_write_cache 8981 * static variable. ATAPI CD/DVDs devices have write cache default is 8982 * determined by sata_atapicdvd_write_cache static variable. 8983 * ATAPI tape devices have write cache default is determined by 8984 * sata_atapitape_write_cache static variable. 8985 * ATAPI disk devices have write cache default is determined by 8986 * sata_atapidisk_write_cache static variable. 8987 * 1 - enable 8988 * 0 - disable 8989 * any other value - current drive setting 8990 * 8991 * Although there is not reason to disable write cache on CD/DVD devices, 8992 * tape devices and ATAPI disk devices, the default setting control is provided 8993 * for the maximun flexibility. 8994 * 8995 * In the future, it may be overridden by the 8996 * disk-write-cache-enable property setting, if it is defined. 8997 * Returns SATA_SUCCESS if all device features are set successfully, 8998 * SATA_FAILURE otherwise. 8999 */ 9000 static void 9001 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 9002 { 9003 switch (sdinfo->satadrv_type) { 9004 case SATA_DTYPE_ATADISK: 9005 if (sata_write_cache == 1) 9006 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9007 else if (sata_write_cache == 0) 9008 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9009 /* 9010 * When sata_write_cache value is not 0 or 1, 9011 * a current setting of the drive's write cache is used. 9012 */ 9013 break; 9014 case SATA_DTYPE_ATAPICD: 9015 if (sata_atapicdvd_write_cache == 1) 9016 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9017 else if (sata_atapicdvd_write_cache == 0) 9018 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9019 /* 9020 * When sata_atapicdvd_write_cache value is not 0 or 1, 9021 * a current setting of the drive's write cache is used. 9022 */ 9023 break; 9024 case SATA_DTYPE_ATAPITAPE: 9025 if (sata_atapitape_write_cache == 1) 9026 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9027 else if (sata_atapitape_write_cache == 0) 9028 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9029 /* 9030 * When sata_atapitape_write_cache value is not 0 or 1, 9031 * a current setting of the drive's write cache is used. 9032 */ 9033 break; 9034 case SATA_DTYPE_ATAPIDISK: 9035 if (sata_atapidisk_write_cache == 1) 9036 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9037 else if (sata_atapidisk_write_cache == 0) 9038 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9039 /* 9040 * When sata_atapidisk_write_cache value is not 0 or 1, 9041 * a current setting of the drive's write cache is used. 9042 */ 9043 break; 9044 } 9045 } 9046 9047 9048 /* 9049 * Validate sata address. 9050 * Specified cport, pmport and qualifier has to match 9051 * passed sata_scsi configuration info. 9052 * The presence of an attached device is not verified. 9053 * 9054 * Returns 0 when address is valid, -1 otherwise. 9055 */ 9056 static int 9057 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 9058 int pmport, int qual) 9059 { 9060 if (qual == SATA_ADDR_DCPORT && pmport != 0) 9061 goto invalid_address; 9062 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9063 goto invalid_address; 9064 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 9065 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 9066 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 9067 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 9068 goto invalid_address; 9069 9070 return (0); 9071 9072 invalid_address: 9073 return (-1); 9074 9075 } 9076 9077 /* 9078 * Validate scsi address 9079 * SCSI target address is translated into SATA cport/pmport and compared 9080 * with a controller port/device configuration. LUN has to be 0. 9081 * Returns 0 if a scsi target refers to an attached device, 9082 * returns 1 if address is valid but device is not attached, 9083 * returns -1 if bad address or device is of an unsupported type. 9084 * Upon return sata_device argument is set. 9085 */ 9086 static int 9087 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 9088 struct scsi_address *ap, sata_device_t *sata_device) 9089 { 9090 int cport, pmport, qual, rval; 9091 9092 rval = -1; /* Invalid address */ 9093 if (ap->a_lun != 0) 9094 goto out; 9095 9096 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 9097 cport = SCSI_TO_SATA_CPORT(ap->a_target); 9098 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 9099 9100 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 9101 goto out; 9102 9103 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 9104 0) { 9105 9106 sata_cport_info_t *cportinfo; 9107 sata_pmult_info_t *pmultinfo; 9108 sata_drive_info_t *sdinfo = NULL; 9109 9110 rval = 1; /* Valid sata address */ 9111 9112 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9113 if (qual == SATA_ADDR_DCPORT) { 9114 if (cportinfo == NULL || 9115 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 9116 goto out; 9117 9118 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT || 9119 (cportinfo->cport_dev_type & 9120 SATA_VALID_DEV_TYPE) == 0) { 9121 rval = -1; 9122 goto out; 9123 } 9124 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9125 9126 } else if (qual == SATA_ADDR_DPMPORT) { 9127 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9128 if (pmultinfo == NULL) { 9129 rval = -1; 9130 goto out; 9131 } 9132 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 9133 NULL || 9134 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 9135 pmport) == SATA_DTYPE_NONE) 9136 goto out; 9137 9138 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 9139 pmport); 9140 } else { 9141 rval = -1; 9142 goto out; 9143 } 9144 if ((sdinfo == NULL) || 9145 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 9146 goto out; 9147 9148 sata_device->satadev_type = sdinfo->satadrv_type; 9149 sata_device->satadev_addr.qual = qual; 9150 sata_device->satadev_addr.cport = cport; 9151 sata_device->satadev_addr.pmport = pmport; 9152 sata_device->satadev_rev = SATA_DEVICE_REV_1; 9153 return (0); 9154 } 9155 out: 9156 if (rval == 1) { 9157 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 9158 "sata_validate_scsi_address: no valid target %x lun %x", 9159 ap->a_target, ap->a_lun); 9160 } 9161 return (rval); 9162 } 9163 9164 /* 9165 * Find dip corresponding to passed device number 9166 * 9167 * Returns NULL if invalid device number is passed or device cannot be found, 9168 * Returns dip is device is found. 9169 */ 9170 static dev_info_t * 9171 sata_devt_to_devinfo(dev_t dev) 9172 { 9173 dev_info_t *dip; 9174 #ifndef __lock_lint 9175 struct devnames *dnp; 9176 major_t major = getmajor(dev); 9177 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 9178 9179 if (major >= devcnt) 9180 return (NULL); 9181 9182 dnp = &devnamesp[major]; 9183 LOCK_DEV_OPS(&(dnp->dn_lock)); 9184 dip = dnp->dn_head; 9185 while (dip && (ddi_get_instance(dip) != instance)) { 9186 dip = ddi_get_next(dip); 9187 } 9188 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 9189 #endif 9190 9191 return (dip); 9192 } 9193 9194 9195 /* 9196 * Probe device. 9197 * This function issues Identify Device command and initializes local 9198 * sata_drive_info structure if the device can be identified. 9199 * The device type is determined by examining Identify Device 9200 * command response. 9201 * If the sata_hba_inst has linked drive info structure for this 9202 * device address, the Identify Device data is stored into sata_drive_info 9203 * structure linked to the port info structure. 9204 * 9205 * sata_device has to refer to the valid sata port(s) for HBA described 9206 * by sata_hba_inst structure. 9207 * 9208 * Returns: 9209 * SATA_SUCCESS if device type was successfully probed and port-linked 9210 * drive info structure was updated; 9211 * SATA_FAILURE if there is no device, or device was not probed 9212 * successully; 9213 * SATA_RETRY if device probe can be retried later. 9214 * If a device cannot be identified, sata_device's dev_state and dev_type 9215 * fields are set to unknown. 9216 * There are no retries in this function. Any retries should be managed by 9217 * the caller. 9218 */ 9219 9220 9221 static int 9222 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 9223 { 9224 sata_drive_info_t *sdinfo; 9225 sata_drive_info_t new_sdinfo; /* local drive info struct */ 9226 int rval; 9227 9228 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 9229 sata_device->satadev_addr.cport) & 9230 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 9231 9232 sata_device->satadev_type = SATA_DTYPE_NONE; 9233 9234 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9235 sata_device->satadev_addr.cport))); 9236 9237 /* Get pointer to port-linked sata device info structure */ 9238 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9239 if (sdinfo != NULL) { 9240 sdinfo->satadrv_state &= 9241 ~(SATA_STATE_PROBED | SATA_STATE_READY); 9242 sdinfo->satadrv_state |= SATA_STATE_PROBING; 9243 } else { 9244 /* No device to probe */ 9245 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9246 sata_device->satadev_addr.cport))); 9247 sata_device->satadev_type = SATA_DTYPE_NONE; 9248 sata_device->satadev_state = SATA_STATE_UNKNOWN; 9249 return (SATA_FAILURE); 9250 } 9251 /* 9252 * Need to issue both types of identify device command and 9253 * determine device type by examining retreived data/status. 9254 * First, ATA Identify Device. 9255 */ 9256 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 9257 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 9258 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9259 sata_device->satadev_addr.cport))); 9260 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 9261 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9262 if (rval == SATA_RETRY) { 9263 /* We may try to check for ATAPI device */ 9264 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 9265 /* 9266 * HBA supports ATAPI - try to issue Identify Packet 9267 * Device command. 9268 */ 9269 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 9270 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9271 } 9272 } 9273 if (rval == SATA_SUCCESS) { 9274 /* 9275 * Got something responding positively to ATA Identify Device 9276 * or to Identify Packet Device cmd. 9277 * Save last used device type. 9278 */ 9279 sata_device->satadev_type = new_sdinfo.satadrv_type; 9280 9281 /* save device info, if possible */ 9282 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9283 sata_device->satadev_addr.cport))); 9284 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9285 if (sdinfo == NULL) { 9286 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9287 sata_device->satadev_addr.cport))); 9288 return (SATA_FAILURE); 9289 } 9290 /* 9291 * Copy drive info into the port-linked drive info structure. 9292 */ 9293 *sdinfo = new_sdinfo; 9294 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9295 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9296 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9297 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9298 sata_device->satadev_addr.cport) = 9299 sdinfo->satadrv_type; 9300 else /* SATA_ADDR_DPMPORT */ 9301 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9302 sata_device->satadev_addr.cport, 9303 sata_device->satadev_addr.pmport) = 9304 sdinfo->satadrv_type; 9305 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9306 sata_device->satadev_addr.cport))); 9307 return (SATA_SUCCESS); 9308 } 9309 9310 /* 9311 * It may be SATA_RETRY or SATA_FAILURE return. 9312 * Looks like we cannot determine the device type at this time. 9313 */ 9314 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9315 sata_device->satadev_addr.cport))); 9316 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9317 if (sdinfo != NULL) { 9318 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 9319 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9320 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9321 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9322 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9323 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9324 sata_device->satadev_addr.cport) = 9325 SATA_DTYPE_UNKNOWN; 9326 else { 9327 /* SATA_ADDR_DPMPORT */ 9328 if ((SATA_PMULT_INFO(sata_hba_inst, 9329 sata_device->satadev_addr.cport) != NULL) && 9330 (SATA_PMPORT_INFO(sata_hba_inst, 9331 sata_device->satadev_addr.cport, 9332 sata_device->satadev_addr.pmport) != NULL)) 9333 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9334 sata_device->satadev_addr.cport, 9335 sata_device->satadev_addr.pmport) = 9336 SATA_DTYPE_UNKNOWN; 9337 } 9338 } 9339 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9340 sata_device->satadev_addr.cport))); 9341 return (rval); 9342 } 9343 9344 9345 /* 9346 * Get pointer to sata_drive_info structure. 9347 * 9348 * The sata_device has to contain address (cport, pmport and qualifier) for 9349 * specified sata_scsi structure. 9350 * 9351 * Returns NULL if device address is not valid for this HBA configuration. 9352 * Otherwise, returns a pointer to sata_drive_info structure. 9353 * 9354 * This function should be called with a port mutex held. 9355 */ 9356 static sata_drive_info_t * 9357 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 9358 sata_device_t *sata_device) 9359 { 9360 uint8_t cport = sata_device->satadev_addr.cport; 9361 uint8_t pmport = sata_device->satadev_addr.pmport; 9362 uint8_t qual = sata_device->satadev_addr.qual; 9363 9364 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9365 return (NULL); 9366 9367 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 9368 (SATA_STATE_PROBED | SATA_STATE_READY))) 9369 /* Port not probed yet */ 9370 return (NULL); 9371 9372 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 9373 return (NULL); 9374 9375 if (qual == SATA_ADDR_DCPORT) { 9376 /* Request for a device on a controller port */ 9377 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 9378 SATA_DTYPE_PMULT) 9379 /* Port multiplier attached */ 9380 return (NULL); 9381 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 9382 } 9383 if (qual == SATA_ADDR_DPMPORT) { 9384 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 9385 SATA_DTYPE_PMULT) 9386 return (NULL); 9387 9388 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 9389 return (NULL); 9390 9391 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 9392 } 9393 9394 /* we should not get here */ 9395 return (NULL); 9396 } 9397 9398 9399 /* 9400 * sata_identify_device. 9401 * Send Identify Device command to SATA HBA driver. 9402 * If command executes successfully, update sata_drive_info structure pointed 9403 * to by sdinfo argument, including Identify Device data. 9404 * If command fails, invalidate data in sata_drive_info. 9405 * 9406 * Cannot be called from interrupt level. 9407 * 9408 * Returns: 9409 * SATA_SUCCESS if the device was identified as a supported device, 9410 * SATA_RETRY if the device was not identified but could be retried, 9411 * SATA_FAILURE if the device was not identified and identify attempt 9412 * should not be retried. 9413 */ 9414 static int 9415 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 9416 sata_drive_info_t *sdinfo) 9417 { 9418 uint16_t cfg_word; 9419 int rval; 9420 9421 /* fetch device identify data */ 9422 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 9423 sdinfo)) != SATA_SUCCESS) 9424 goto fail_unknown; 9425 9426 cfg_word = sdinfo->satadrv_id.ai_config; 9427 9428 /* Set the correct device type */ 9429 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 9430 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 9431 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 9432 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 9433 case SATA_ATAPI_CDROM_DEV: 9434 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 9435 break; 9436 case SATA_ATAPI_SQACC_DEV: 9437 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 9438 break; 9439 case SATA_ATAPI_DIRACC_DEV: 9440 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 9441 break; 9442 default: 9443 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9444 } 9445 } else { 9446 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9447 } 9448 9449 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9450 if (sdinfo->satadrv_capacity == 0) { 9451 /* Non-LBA disk. Too bad... */ 9452 sata_log(sata_hba_inst, CE_WARN, 9453 "SATA disk device at port %d does not support LBA", 9454 sdinfo->satadrv_addr.cport); 9455 rval = SATA_FAILURE; 9456 goto fail_unknown; 9457 } 9458 } 9459 #if 0 9460 /* Left for historical reason */ 9461 /* 9462 * Some initial version of SATA spec indicated that at least 9463 * UDMA mode 4 has to be supported. It is not metioned in 9464 * SerialATA 2.6, so this restriction is removed. 9465 */ 9466 /* Check for Ultra DMA modes 6 through 0 being supported */ 9467 for (i = 6; i >= 0; --i) { 9468 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 9469 break; 9470 } 9471 9472 /* 9473 * At least UDMA 4 mode has to be supported. If mode 4 or 9474 * higher are not supported by the device, fail this 9475 * device. 9476 */ 9477 if (i < 4) { 9478 /* No required Ultra DMA mode supported */ 9479 sata_log(sata_hba_inst, CE_WARN, 9480 "SATA disk device at port %d does not support UDMA " 9481 "mode 4 or higher", sdinfo->satadrv_addr.cport); 9482 SATA_LOG_D((sata_hba_inst, CE_WARN, 9483 "mode 4 or higher required, %d supported", i)); 9484 rval = SATA_FAILURE; 9485 goto fail_unknown; 9486 } 9487 #endif 9488 9489 return (SATA_SUCCESS); 9490 9491 fail_unknown: 9492 /* Invalidate sata_drive_info ? */ 9493 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9494 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 9495 return (rval); 9496 } 9497 9498 /* 9499 * Log/display device information 9500 */ 9501 static void 9502 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 9503 sata_drive_info_t *sdinfo) 9504 { 9505 int valid_version; 9506 char msg_buf[MAXPATHLEN]; 9507 int i; 9508 9509 /* Show HBA path */ 9510 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 9511 9512 cmn_err(CE_CONT, "?%s :\n", msg_buf); 9513 9514 switch (sdinfo->satadrv_type) { 9515 case SATA_DTYPE_ATADISK: 9516 (void) sprintf(msg_buf, "SATA disk device at"); 9517 break; 9518 9519 case SATA_DTYPE_ATAPICD: 9520 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 9521 break; 9522 9523 case SATA_DTYPE_ATAPITAPE: 9524 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 9525 break; 9526 9527 case SATA_DTYPE_ATAPIDISK: 9528 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 9529 break; 9530 9531 case SATA_DTYPE_UNKNOWN: 9532 (void) sprintf(msg_buf, 9533 "Unsupported SATA device type (cfg 0x%x) at ", 9534 sdinfo->satadrv_id.ai_config); 9535 break; 9536 } 9537 9538 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 9539 cmn_err(CE_CONT, "?\t%s port %d\n", 9540 msg_buf, sdinfo->satadrv_addr.cport); 9541 else 9542 cmn_err(CE_CONT, "?\t%s port %d pmport %d\n", 9543 msg_buf, sdinfo->satadrv_addr.cport, 9544 sdinfo->satadrv_addr.pmport); 9545 9546 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 9547 sizeof (sdinfo->satadrv_id.ai_model)); 9548 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 9549 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 9550 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 9551 9552 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 9553 sizeof (sdinfo->satadrv_id.ai_fw)); 9554 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 9555 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 9556 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 9557 9558 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 9559 sizeof (sdinfo->satadrv_id.ai_drvser)); 9560 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 9561 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 9562 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9563 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9564 } else { 9565 /* 9566 * Some drives do not implement serial number and may 9567 * violate the spec by providing spaces rather than zeros 9568 * in serial number field. Scan the buffer to detect it. 9569 */ 9570 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 9571 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 9572 break; 9573 } 9574 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 9575 cmn_err(CE_CONT, "?\tserial number - none\n"); 9576 } else { 9577 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9578 } 9579 } 9580 9581 #ifdef SATA_DEBUG 9582 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9583 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 9584 int i; 9585 for (i = 14; i >= 2; i--) { 9586 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 9587 valid_version = i; 9588 break; 9589 } 9590 } 9591 cmn_err(CE_CONT, 9592 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 9593 valid_version, 9594 sdinfo->satadrv_id.ai_majorversion, 9595 sdinfo->satadrv_id.ai_minorversion); 9596 } 9597 #endif 9598 /* Log some info */ 9599 cmn_err(CE_CONT, "?\tsupported features:\n"); 9600 msg_buf[0] = '\0'; 9601 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9602 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 9603 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 9604 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 9605 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 9606 } 9607 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 9608 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 9609 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 9610 (void) strlcat(msg_buf, ", Native Command Queueing", 9611 MAXPATHLEN); 9612 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 9613 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 9614 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 9615 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 9616 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 9617 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 9618 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 9619 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 9620 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 9621 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 9622 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 9623 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 9624 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 9625 if (sdinfo->satadrv_features_support & 9626 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 9627 msg_buf[0] = '\0'; 9628 (void) snprintf(msg_buf, MAXPATHLEN, 9629 "Supported queue depth %d", 9630 sdinfo->satadrv_queue_depth); 9631 if (!(sata_func_enable & 9632 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 9633 (void) strlcat(msg_buf, 9634 " - queueing disabled globally", MAXPATHLEN); 9635 else if (sdinfo->satadrv_queue_depth > 9636 sdinfo->satadrv_max_queue_depth) { 9637 (void) snprintf(&msg_buf[strlen(msg_buf)], 9638 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 9639 (int)sdinfo->satadrv_max_queue_depth); 9640 } 9641 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 9642 } 9643 9644 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9645 #ifdef __i386 9646 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 9647 sdinfo->satadrv_capacity); 9648 #else 9649 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 9650 sdinfo->satadrv_capacity); 9651 #endif 9652 cmn_err(CE_CONT, "?%s", msg_buf); 9653 } 9654 } 9655 9656 9657 /* 9658 * sata_save_drive_settings extracts current setting of the device and stores 9659 * it for future reference, in case the device setup would need to be restored 9660 * after the device reset. 9661 * 9662 * For all devices read ahead and write cache settings are saved, if the 9663 * device supports these features at all. 9664 * For ATAPI devices the Removable Media Status Notification setting is saved. 9665 */ 9666 static void 9667 sata_save_drive_settings(sata_drive_info_t *sdinfo) 9668 { 9669 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 9670 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 9671 9672 /* Current setting of Read Ahead (and Read Cache) */ 9673 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 9674 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 9675 else 9676 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 9677 9678 /* Current setting of Write Cache */ 9679 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 9680 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9681 else 9682 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9683 } 9684 9685 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9686 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 9687 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 9688 else 9689 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 9690 } 9691 } 9692 9693 9694 /* 9695 * sata_check_capacity function determines a disk capacity 9696 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 9697 * 9698 * NOTE: CHS mode is not supported! If a device does not support LBA, 9699 * this function is not called. 9700 * 9701 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 9702 */ 9703 static uint64_t 9704 sata_check_capacity(sata_drive_info_t *sdinfo) 9705 { 9706 uint64_t capacity = 0; 9707 int i; 9708 9709 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 9710 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 9711 /* Capacity valid only for LBA-addressable disk devices */ 9712 return (0); 9713 9714 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 9715 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 9716 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 9717 /* LBA48 mode supported and enabled */ 9718 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 9719 SATA_DEV_F_LBA28; 9720 for (i = 3; i >= 0; --i) { 9721 capacity <<= 16; 9722 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 9723 } 9724 } else { 9725 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 9726 capacity <<= 16; 9727 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 9728 if (capacity >= 0x1000000) 9729 /* LBA28 mode */ 9730 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 9731 } 9732 return (capacity); 9733 } 9734 9735 9736 /* 9737 * Allocate consistent buffer for DMA transfer 9738 * 9739 * Cannot be called from interrupt level or with mutex held - it may sleep. 9740 * 9741 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 9742 */ 9743 static struct buf * 9744 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 9745 { 9746 struct scsi_address ap; 9747 struct buf *bp; 9748 ddi_dma_attr_t cur_dma_attr; 9749 9750 ASSERT(spx->txlt_sata_pkt != NULL); 9751 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 9752 ap.a_target = SATA_TO_SCSI_TARGET( 9753 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 9754 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 9755 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 9756 ap.a_lun = 0; 9757 9758 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 9759 B_READ, SLEEP_FUNC, NULL); 9760 9761 if (bp != NULL) { 9762 /* Allocate DMA resources for this buffer */ 9763 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 9764 /* 9765 * We use a local version of the dma_attr, to account 9766 * for a device addressing limitations. 9767 * sata_adjust_dma_attr() will handle sdinfo == NULL which 9768 * will cause dma attributes to be adjusted to a lowest 9769 * acceptable level. 9770 */ 9771 sata_adjust_dma_attr(NULL, 9772 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 9773 9774 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 9775 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 9776 scsi_free_consistent_buf(bp); 9777 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9778 bp = NULL; 9779 } 9780 } 9781 return (bp); 9782 } 9783 9784 /* 9785 * Release local buffer (consistent buffer for DMA transfer) allocated 9786 * via sata_alloc_local_buffer(). 9787 */ 9788 static void 9789 sata_free_local_buffer(sata_pkt_txlate_t *spx) 9790 { 9791 ASSERT(spx->txlt_sata_pkt != NULL); 9792 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 9793 9794 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 9795 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 9796 9797 if (spx->txlt_buf_dma_handle != NULL) { 9798 /* Free DMA resources */ 9799 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 9800 ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 9801 spx->txlt_buf_dma_handle = 0; 9802 9803 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 9804 kmem_free(spx->txlt_dma_cookie_list, 9805 spx->txlt_dma_cookie_list_len * 9806 sizeof (ddi_dma_cookie_t)); 9807 spx->txlt_dma_cookie_list = NULL; 9808 spx->txlt_dma_cookie_list_len = 0; 9809 } 9810 } 9811 9812 /* Free buffer */ 9813 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 9814 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9815 } 9816 9817 9818 9819 9820 /* 9821 * Allocate sata_pkt 9822 * Pkt structure version and embedded strcutures version are initialized. 9823 * sata_pkt and sata_pkt_txlate structures are cross-linked. 9824 * 9825 * Since this may be called in interrupt context by sata_scsi_init_pkt, 9826 * callback argument determines if it can sleep or not. 9827 * Hence, it should not be called from interrupt context. 9828 * 9829 * If successful, non-NULL pointer to a sata pkt is returned. 9830 * Upon failure, NULL pointer is returned. 9831 */ 9832 static sata_pkt_t * 9833 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 9834 { 9835 sata_pkt_t *spkt; 9836 int kmsflag; 9837 9838 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 9839 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 9840 if (spkt == NULL) { 9841 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9842 "sata_pkt_alloc: failed")); 9843 return (NULL); 9844 } 9845 spkt->satapkt_rev = SATA_PKT_REV; 9846 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 9847 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 9848 spkt->satapkt_framework_private = spx; 9849 spx->txlt_sata_pkt = spkt; 9850 return (spkt); 9851 } 9852 9853 /* 9854 * Free sata pkt allocated via sata_pkt_alloc() 9855 */ 9856 static void 9857 sata_pkt_free(sata_pkt_txlate_t *spx) 9858 { 9859 ASSERT(spx->txlt_sata_pkt != NULL); 9860 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 9861 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 9862 spx->txlt_sata_pkt = NULL; 9863 } 9864 9865 9866 /* 9867 * Adjust DMA attributes. 9868 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 9869 * from 8 bits to 16 bits, depending on a command being used. 9870 * Limiting max block count arbitrarily to 256 for all read/write 9871 * commands may affects performance, so check both the device and 9872 * controller capability before adjusting dma attributes. 9873 */ 9874 void 9875 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 9876 ddi_dma_attr_t *adj_dma_attr) 9877 { 9878 uint32_t count_max; 9879 9880 /* Copy original attributes */ 9881 *adj_dma_attr = *dma_attr; 9882 /* 9883 * Things to consider: device addressing capability, 9884 * "excessive" controller DMA capabilities. 9885 * If a device is being probed/initialized, there are 9886 * no device info - use default limits then. 9887 */ 9888 if (sdinfo == NULL) { 9889 count_max = dma_attr->dma_attr_granular * 0x100; 9890 if (dma_attr->dma_attr_count_max > count_max) 9891 adj_dma_attr->dma_attr_count_max = count_max; 9892 if (dma_attr->dma_attr_maxxfer > count_max) 9893 adj_dma_attr->dma_attr_maxxfer = count_max; 9894 return; 9895 } 9896 9897 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9898 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 9899 /* 9900 * 16-bit sector count may be used - we rely on 9901 * the assumption that only read and write cmds 9902 * will request more than 256 sectors worth of data 9903 */ 9904 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 9905 } else { 9906 /* 9907 * 8-bit sector count will be used - default limits 9908 * for dma attributes 9909 */ 9910 count_max = adj_dma_attr->dma_attr_granular * 0x100; 9911 } 9912 /* 9913 * Adjust controler dma attributes, if necessary 9914 */ 9915 if (dma_attr->dma_attr_count_max > count_max) 9916 adj_dma_attr->dma_attr_count_max = count_max; 9917 if (dma_attr->dma_attr_maxxfer > count_max) 9918 adj_dma_attr->dma_attr_maxxfer = count_max; 9919 } 9920 } 9921 9922 9923 /* 9924 * Allocate DMA resources for the buffer 9925 * This function handles initial DMA resource allocation as well as 9926 * DMA window shift and may be called repeatedly for the same DMA window 9927 * until all DMA cookies in the DMA window are processed. 9928 * To guarantee that there is always a coherent set of cookies to process 9929 * by SATA HBA driver (observing alignment, device granularity, etc.), 9930 * the number of slots for DMA cookies is equal to lesser of a number of 9931 * cookies in a DMA window and a max number of scatter/gather entries. 9932 * 9933 * Returns DDI_SUCCESS upon successful operation. 9934 * Return failure code of a failing command or DDI_FAILURE when 9935 * internal cleanup failed. 9936 */ 9937 static int 9938 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 9939 int (*callback)(caddr_t), caddr_t arg, 9940 ddi_dma_attr_t *cur_dma_attr) 9941 { 9942 int rval; 9943 off_t offset; 9944 size_t size; 9945 int max_sg_len, req_len, i; 9946 uint_t dma_flags; 9947 struct buf *bp; 9948 uint64_t cur_txfer_len; 9949 9950 9951 ASSERT(spx->txlt_sata_pkt != NULL); 9952 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9953 ASSERT(bp != NULL); 9954 9955 9956 if (spx->txlt_buf_dma_handle == NULL) { 9957 /* 9958 * No DMA resources allocated so far - this is a first call 9959 * for this sata pkt. 9960 */ 9961 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 9962 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 9963 9964 if (rval != DDI_SUCCESS) { 9965 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9966 "sata_dma_buf_setup: no buf DMA resources %x", 9967 rval)); 9968 return (rval); 9969 } 9970 9971 if (bp->b_flags & B_READ) 9972 dma_flags = DDI_DMA_READ; 9973 else 9974 dma_flags = DDI_DMA_WRITE; 9975 9976 if (flags & PKT_CONSISTENT) 9977 dma_flags |= DDI_DMA_CONSISTENT; 9978 9979 if (flags & PKT_DMA_PARTIAL) 9980 dma_flags |= DDI_DMA_PARTIAL; 9981 9982 /* 9983 * Check buffer alignment and size against dma attributes 9984 * Consider dma_attr_align only. There may be requests 9985 * with the size lower than device granularity, but they 9986 * will not read/write from/to the device, so no adjustment 9987 * is necessary. The dma_attr_minxfer theoretically should 9988 * be considered, but no HBA driver is checking it. 9989 */ 9990 if (IS_P2ALIGNED(bp->b_un.b_addr, 9991 cur_dma_attr->dma_attr_align)) { 9992 rval = ddi_dma_buf_bind_handle( 9993 spx->txlt_buf_dma_handle, 9994 bp, dma_flags, callback, arg, 9995 &spx->txlt_dma_cookie, 9996 &spx->txlt_curwin_num_dma_cookies); 9997 } else { /* Buffer is not aligned */ 9998 9999 int (*ddicallback)(caddr_t); 10000 size_t bufsz; 10001 10002 /* Check id sleeping is allowed */ 10003 ddicallback = (callback == NULL_FUNC) ? 10004 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 10005 10006 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10007 "mis-aligned buffer: addr=0x%p, cnt=%lu", 10008 (void *)bp->b_un.b_addr, bp->b_bcount); 10009 10010 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 10011 /* 10012 * CPU will need to access data in the buffer 10013 * (for copying) so map it. 10014 */ 10015 bp_mapin(bp); 10016 10017 ASSERT(spx->txlt_tmp_buf == NULL); 10018 10019 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 10020 rval = ddi_dma_mem_alloc( 10021 spx->txlt_buf_dma_handle, 10022 bp->b_bcount, 10023 &sata_acc_attr, 10024 DDI_DMA_STREAMING, 10025 ddicallback, NULL, 10026 &spx->txlt_tmp_buf, 10027 &bufsz, 10028 &spx->txlt_tmp_buf_handle); 10029 10030 if (rval != DDI_SUCCESS) { 10031 /* DMA mapping failed */ 10032 (void) ddi_dma_free_handle( 10033 &spx->txlt_buf_dma_handle); 10034 spx->txlt_buf_dma_handle = NULL; 10035 #ifdef SATA_DEBUG 10036 mbuffail_count++; 10037 #endif 10038 SATADBG1(SATA_DBG_DMA_SETUP, 10039 spx->txlt_sata_hba_inst, 10040 "sata_dma_buf_setup: " 10041 "buf dma mem alloc failed %x\n", rval); 10042 return (rval); 10043 } 10044 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 10045 cur_dma_attr->dma_attr_align)); 10046 10047 #ifdef SATA_DEBUG 10048 mbuf_count++; 10049 10050 if (bp->b_bcount != bufsz) 10051 /* 10052 * This will require special handling, because 10053 * DMA cookies will be based on the temporary 10054 * buffer size, not the original buffer 10055 * b_bcount, so the residue may have to 10056 * be counted differently. 10057 */ 10058 SATADBG2(SATA_DBG_DMA_SETUP, 10059 spx->txlt_sata_hba_inst, 10060 "sata_dma_buf_setup: bp size %x != " 10061 "bufsz %x\n", bp->b_bcount, bufsz); 10062 #endif 10063 if (dma_flags & DDI_DMA_WRITE) { 10064 /* 10065 * Write operation - copy data into 10066 * an aligned temporary buffer. Buffer will be 10067 * synced for device by ddi_dma_addr_bind_handle 10068 */ 10069 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 10070 bp->b_bcount); 10071 } 10072 10073 rval = ddi_dma_addr_bind_handle( 10074 spx->txlt_buf_dma_handle, 10075 NULL, 10076 spx->txlt_tmp_buf, 10077 bufsz, dma_flags, ddicallback, 0, 10078 &spx->txlt_dma_cookie, 10079 &spx->txlt_curwin_num_dma_cookies); 10080 } 10081 10082 switch (rval) { 10083 case DDI_DMA_PARTIAL_MAP: 10084 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10085 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 10086 /* 10087 * Partial DMA mapping. 10088 * Retrieve number of DMA windows for this request. 10089 */ 10090 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 10091 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 10092 if (spx->txlt_tmp_buf != NULL) { 10093 ddi_dma_mem_free( 10094 &spx->txlt_tmp_buf_handle); 10095 spx->txlt_tmp_buf = NULL; 10096 } 10097 (void) ddi_dma_unbind_handle( 10098 spx->txlt_buf_dma_handle); 10099 (void) ddi_dma_free_handle( 10100 &spx->txlt_buf_dma_handle); 10101 spx->txlt_buf_dma_handle = NULL; 10102 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10103 "sata_dma_buf_setup: numwin failed\n")); 10104 return (DDI_FAILURE); 10105 } 10106 SATADBG2(SATA_DBG_DMA_SETUP, 10107 spx->txlt_sata_hba_inst, 10108 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 10109 spx->txlt_num_dma_win, 10110 spx->txlt_curwin_num_dma_cookies); 10111 spx->txlt_cur_dma_win = 0; 10112 break; 10113 10114 case DDI_DMA_MAPPED: 10115 /* DMA fully mapped */ 10116 spx->txlt_num_dma_win = 1; 10117 spx->txlt_cur_dma_win = 0; 10118 SATADBG1(SATA_DBG_DMA_SETUP, 10119 spx->txlt_sata_hba_inst, 10120 "sata_dma_buf_setup: windows: 1 " 10121 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 10122 break; 10123 10124 default: 10125 /* DMA mapping failed */ 10126 if (spx->txlt_tmp_buf != NULL) { 10127 ddi_dma_mem_free( 10128 &spx->txlt_tmp_buf_handle); 10129 spx->txlt_tmp_buf = NULL; 10130 } 10131 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10132 spx->txlt_buf_dma_handle = NULL; 10133 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10134 "sata_dma_buf_setup: buf dma handle binding " 10135 "failed %x\n", rval)); 10136 return (rval); 10137 } 10138 spx->txlt_curwin_processed_dma_cookies = 0; 10139 spx->txlt_dma_cookie_list = NULL; 10140 } else { 10141 /* 10142 * DMA setup is reused. Check if we need to process more 10143 * cookies in current window, or to get next window, if any. 10144 */ 10145 10146 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 10147 spx->txlt_curwin_num_dma_cookies); 10148 10149 if (spx->txlt_curwin_processed_dma_cookies == 10150 spx->txlt_curwin_num_dma_cookies) { 10151 /* 10152 * All cookies from current DMA window were processed. 10153 * Get next DMA window. 10154 */ 10155 spx->txlt_cur_dma_win++; 10156 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 10157 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 10158 spx->txlt_cur_dma_win, &offset, &size, 10159 &spx->txlt_dma_cookie, 10160 &spx->txlt_curwin_num_dma_cookies); 10161 spx->txlt_curwin_processed_dma_cookies = 0; 10162 } else { 10163 /* No more windows! End of request! */ 10164 /* What to do? - panic for now */ 10165 ASSERT(spx->txlt_cur_dma_win >= 10166 spx->txlt_num_dma_win); 10167 10168 spx->txlt_curwin_num_dma_cookies = 0; 10169 spx->txlt_curwin_processed_dma_cookies = 0; 10170 spx->txlt_sata_pkt-> 10171 satapkt_cmd.satacmd_num_dma_cookies = 0; 10172 return (DDI_SUCCESS); 10173 } 10174 } 10175 } 10176 /* There better be at least one DMA cookie outstanding */ 10177 ASSERT((spx->txlt_curwin_num_dma_cookies - 10178 spx->txlt_curwin_processed_dma_cookies) > 0); 10179 10180 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 10181 /* The default cookie slot was used in previous run */ 10182 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 10183 spx->txlt_dma_cookie_list = NULL; 10184 spx->txlt_dma_cookie_list_len = 0; 10185 } 10186 if (spx->txlt_curwin_processed_dma_cookies == 0) { 10187 /* 10188 * Processing a new DMA window - set-up dma cookies list. 10189 * We may reuse previously allocated cookie array if it is 10190 * possible. 10191 */ 10192 if (spx->txlt_dma_cookie_list != NULL && 10193 spx->txlt_dma_cookie_list_len < 10194 spx->txlt_curwin_num_dma_cookies) { 10195 /* 10196 * New DMA window contains more cookies than 10197 * the previous one. We need larger cookie list - free 10198 * the old one. 10199 */ 10200 (void) kmem_free(spx->txlt_dma_cookie_list, 10201 spx->txlt_dma_cookie_list_len * 10202 sizeof (ddi_dma_cookie_t)); 10203 spx->txlt_dma_cookie_list = NULL; 10204 spx->txlt_dma_cookie_list_len = 0; 10205 } 10206 if (spx->txlt_dma_cookie_list == NULL) { 10207 /* 10208 * Calculate lesser of number of cookies in this 10209 * DMA window and number of s/g entries. 10210 */ 10211 max_sg_len = cur_dma_attr->dma_attr_sgllen; 10212 req_len = MIN(max_sg_len, 10213 spx->txlt_curwin_num_dma_cookies); 10214 10215 /* Allocate new dma cookie array if necessary */ 10216 if (req_len == 1) { 10217 /* Only one cookie - no need for a list */ 10218 spx->txlt_dma_cookie_list = 10219 &spx->txlt_dma_cookie; 10220 spx->txlt_dma_cookie_list_len = 1; 10221 } else { 10222 /* 10223 * More than one cookie - try to allocate space. 10224 */ 10225 spx->txlt_dma_cookie_list = kmem_zalloc( 10226 sizeof (ddi_dma_cookie_t) * req_len, 10227 callback == NULL_FUNC ? KM_NOSLEEP : 10228 KM_SLEEP); 10229 if (spx->txlt_dma_cookie_list == NULL) { 10230 SATADBG1(SATA_DBG_DMA_SETUP, 10231 spx->txlt_sata_hba_inst, 10232 "sata_dma_buf_setup: cookie list " 10233 "allocation failed\n", NULL); 10234 /* 10235 * We could not allocate space for 10236 * neccessary number of dma cookies in 10237 * this window, so we fail this request. 10238 * Next invocation would try again to 10239 * allocate space for cookie list. 10240 * Note:Packet residue was not modified. 10241 */ 10242 return (DDI_DMA_NORESOURCES); 10243 } else { 10244 spx->txlt_dma_cookie_list_len = req_len; 10245 } 10246 } 10247 } 10248 /* 10249 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 10250 * First cookie was already fetched. 10251 */ 10252 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 10253 cur_txfer_len = 10254 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 10255 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 10256 spx->txlt_curwin_processed_dma_cookies++; 10257 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 10258 (i < spx->txlt_curwin_num_dma_cookies); i++) { 10259 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10260 &spx->txlt_dma_cookie_list[i]); 10261 cur_txfer_len += 10262 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10263 spx->txlt_curwin_processed_dma_cookies++; 10264 spx->txlt_sata_pkt-> 10265 satapkt_cmd.satacmd_num_dma_cookies += 1; 10266 } 10267 } else { 10268 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10269 "sata_dma_buf_setup: sliding within DMA window, " 10270 "cur cookie %d, total cookies %d\n", 10271 spx->txlt_curwin_processed_dma_cookies, 10272 spx->txlt_curwin_num_dma_cookies); 10273 10274 /* 10275 * Not all cookies from the current dma window were used because 10276 * of s/g limitation. 10277 * There is no need to re-size the list - it was set at 10278 * optimal size, or only default entry is used (s/g = 1). 10279 */ 10280 if (spx->txlt_dma_cookie_list == NULL) { 10281 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 10282 spx->txlt_dma_cookie_list_len = 1; 10283 } 10284 /* 10285 * Since we are processing remaining cookies in a DMA window, 10286 * there may be less of them than the number of entries in the 10287 * current dma cookie list. 10288 */ 10289 req_len = MIN(spx->txlt_dma_cookie_list_len, 10290 (spx->txlt_curwin_num_dma_cookies - 10291 spx->txlt_curwin_processed_dma_cookies)); 10292 10293 /* Fetch the next batch of cookies */ 10294 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 10295 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10296 &spx->txlt_dma_cookie_list[i]); 10297 cur_txfer_len += 10298 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10299 spx->txlt_sata_pkt-> 10300 satapkt_cmd.satacmd_num_dma_cookies++; 10301 spx->txlt_curwin_processed_dma_cookies++; 10302 } 10303 } 10304 10305 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 10306 10307 /* Point sata_cmd to the cookie list */ 10308 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 10309 &spx->txlt_dma_cookie_list[0]; 10310 10311 /* Remember number of DMA cookies passed in sata packet */ 10312 spx->txlt_num_dma_cookies = 10313 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 10314 10315 ASSERT(cur_txfer_len != 0); 10316 if (cur_txfer_len <= bp->b_bcount) 10317 spx->txlt_total_residue -= cur_txfer_len; 10318 else { 10319 /* 10320 * Temporary DMA buffer has been padded by 10321 * ddi_dma_mem_alloc()! 10322 * This requires special handling, because DMA cookies are 10323 * based on the temporary buffer size, not the b_bcount, 10324 * and we have extra bytes to transfer - but the packet 10325 * residue has to stay correct because we will copy only 10326 * the requested number of bytes. 10327 */ 10328 spx->txlt_total_residue -= bp->b_bcount; 10329 } 10330 10331 return (DDI_SUCCESS); 10332 } 10333 10334 /* 10335 * Common routine for releasing DMA resources 10336 */ 10337 static void 10338 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 10339 { 10340 if (spx->txlt_buf_dma_handle != NULL) { 10341 if (spx->txlt_tmp_buf != NULL) { 10342 /* 10343 * Intermediate DMA buffer was allocated. 10344 * Free allocated buffer and associated access handle. 10345 */ 10346 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 10347 spx->txlt_tmp_buf = NULL; 10348 } 10349 /* 10350 * Free DMA resources - cookies and handles 10351 */ 10352 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 10353 if (spx->txlt_dma_cookie_list != NULL) { 10354 if (spx->txlt_dma_cookie_list != 10355 &spx->txlt_dma_cookie) { 10356 (void) kmem_free(spx->txlt_dma_cookie_list, 10357 spx->txlt_dma_cookie_list_len * 10358 sizeof (ddi_dma_cookie_t)); 10359 spx->txlt_dma_cookie_list = NULL; 10360 } 10361 } 10362 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 10363 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10364 spx->txlt_buf_dma_handle = NULL; 10365 } 10366 } 10367 10368 /* 10369 * Free DMA resources 10370 * Used by the HBA driver to release DMA resources that it does not use. 10371 * 10372 * Returns Void 10373 */ 10374 void 10375 sata_free_dma_resources(sata_pkt_t *sata_pkt) 10376 { 10377 sata_pkt_txlate_t *spx; 10378 10379 if (sata_pkt == NULL) 10380 return; 10381 10382 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 10383 10384 sata_common_free_dma_rsrcs(spx); 10385 } 10386 10387 /* 10388 * Fetch Device Identify data. 10389 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 10390 * command to a device and get the device identify data. 10391 * The device_info structure has to be set to device type (for selecting proper 10392 * device identify command). 10393 * 10394 * Returns: 10395 * SATA_SUCCESS if cmd succeeded 10396 * SATA_RETRY if cmd was rejected and could be retried, 10397 * SATA_FAILURE if cmd failed and should not be retried (port error) 10398 * 10399 * Cannot be called in an interrupt context. 10400 */ 10401 10402 static int 10403 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 10404 sata_drive_info_t *sdinfo) 10405 { 10406 struct buf *bp; 10407 sata_pkt_t *spkt; 10408 sata_cmd_t *scmd; 10409 sata_pkt_txlate_t *spx; 10410 int rval; 10411 10412 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10413 spx->txlt_sata_hba_inst = sata_hba_inst; 10414 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10415 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10416 if (spkt == NULL) { 10417 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10418 return (SATA_RETRY); /* may retry later */ 10419 } 10420 /* address is needed now */ 10421 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10422 10423 /* 10424 * Allocate buffer for Identify Data return data 10425 */ 10426 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 10427 if (bp == NULL) { 10428 sata_pkt_free(spx); 10429 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10430 SATA_LOG_D((sata_hba_inst, CE_WARN, 10431 "sata_fetch_device_identify_data: " 10432 "cannot allocate buffer for ID")); 10433 return (SATA_RETRY); /* may retry later */ 10434 } 10435 10436 /* Fill sata_pkt */ 10437 sdinfo->satadrv_state = SATA_STATE_PROBING; 10438 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10439 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10440 /* Synchronous mode, no callback */ 10441 spkt->satapkt_comp = NULL; 10442 /* Timeout 30s */ 10443 spkt->satapkt_time = sata_default_pkt_time; 10444 10445 scmd = &spkt->satapkt_cmd; 10446 scmd->satacmd_bp = bp; 10447 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10448 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10449 10450 /* Build Identify Device cmd in the sata_pkt */ 10451 scmd->satacmd_addr_type = 0; /* N/A */ 10452 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 10453 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 10454 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 10455 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 10456 scmd->satacmd_features_reg = 0; /* N/A */ 10457 scmd->satacmd_device_reg = 0; /* Always device 0 */ 10458 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 10459 /* Identify Packet Device cmd */ 10460 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 10461 } else { 10462 /* Identify Device cmd - mandatory for all other devices */ 10463 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 10464 } 10465 10466 /* Send pkt to SATA HBA driver */ 10467 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 10468 10469 #ifdef SATA_INJECT_FAULTS 10470 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10471 #endif 10472 10473 if (rval == SATA_TRAN_ACCEPTED && 10474 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10475 if (spx->txlt_buf_dma_handle != NULL) { 10476 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10477 DDI_DMA_SYNC_FORKERNEL); 10478 ASSERT(rval == DDI_SUCCESS); 10479 } 10480 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 10481 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 10482 SATA_LOG_D((sata_hba_inst, CE_WARN, 10483 "SATA disk device at port %d - " 10484 "partial Identify Data", 10485 sdinfo->satadrv_addr.cport)); 10486 rval = SATA_RETRY; /* may retry later */ 10487 goto fail; 10488 } 10489 /* Update sata_drive_info */ 10490 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 10491 sizeof (sata_id_t)); 10492 10493 sdinfo->satadrv_features_support = 0; 10494 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10495 /* 10496 * Retrieve capacity (disks only) and addressing mode 10497 */ 10498 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 10499 } else { 10500 /* 10501 * For ATAPI devices one would have to issue 10502 * Get Capacity cmd for media capacity. Not here. 10503 */ 10504 sdinfo->satadrv_capacity = 0; 10505 /* 10506 * Check what cdb length is supported 10507 */ 10508 if ((sdinfo->satadrv_id.ai_config & 10509 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 10510 sdinfo->satadrv_atapi_cdb_len = 16; 10511 else 10512 sdinfo->satadrv_atapi_cdb_len = 12; 10513 } 10514 /* Setup supported features flags */ 10515 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 10516 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 10517 10518 /* Check for SATA GEN and NCQ support */ 10519 if (sdinfo->satadrv_id.ai_satacap != 0 && 10520 sdinfo->satadrv_id.ai_satacap != 0xffff) { 10521 /* SATA compliance */ 10522 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 10523 sdinfo->satadrv_features_support |= 10524 SATA_DEV_F_NCQ; 10525 if (sdinfo->satadrv_id.ai_satacap & 10526 (SATA_1_SPEED | SATA_2_SPEED)) { 10527 if (sdinfo->satadrv_id.ai_satacap & 10528 SATA_2_SPEED) 10529 sdinfo->satadrv_features_support |= 10530 SATA_DEV_F_SATA2; 10531 if (sdinfo->satadrv_id.ai_satacap & 10532 SATA_1_SPEED) 10533 sdinfo->satadrv_features_support |= 10534 SATA_DEV_F_SATA1; 10535 } else { 10536 sdinfo->satadrv_features_support |= 10537 SATA_DEV_F_SATA1; 10538 } 10539 } 10540 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 10541 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 10542 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 10543 10544 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 10545 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 10546 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 10547 ++sdinfo->satadrv_queue_depth; 10548 /* Adjust according to controller capabilities */ 10549 sdinfo->satadrv_max_queue_depth = MIN( 10550 sdinfo->satadrv_queue_depth, 10551 SATA_QDEPTH(sata_hba_inst)); 10552 /* Adjust according to global queue depth limit */ 10553 sdinfo->satadrv_max_queue_depth = MIN( 10554 sdinfo->satadrv_max_queue_depth, 10555 sata_current_max_qdepth); 10556 if (sdinfo->satadrv_max_queue_depth == 0) 10557 sdinfo->satadrv_max_queue_depth = 1; 10558 } else 10559 sdinfo->satadrv_max_queue_depth = 1; 10560 10561 rval = SATA_SUCCESS; 10562 } else { 10563 /* 10564 * Woops, no Identify Data. 10565 */ 10566 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 10567 rval = SATA_RETRY; /* may retry later */ 10568 } else if (rval == SATA_TRAN_ACCEPTED) { 10569 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 10570 spkt->satapkt_reason == SATA_PKT_ABORTED || 10571 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 10572 spkt->satapkt_reason == SATA_PKT_RESET) 10573 rval = SATA_RETRY; /* may retry later */ 10574 else 10575 rval = SATA_FAILURE; 10576 } else { 10577 rval = SATA_FAILURE; 10578 } 10579 } 10580 fail: 10581 /* Free allocated resources */ 10582 sata_free_local_buffer(spx); 10583 sata_pkt_free(spx); 10584 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10585 10586 return (rval); 10587 } 10588 10589 10590 /* 10591 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 10592 * UDMA mode is checked first, followed by MWDMA mode. 10593 * set correctly, so this function is setting it to the highest supported level. 10594 * Older SATA spec required that the device supports at least DMA 4 mode and 10595 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 10596 * restriction has been removed. 10597 * 10598 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 10599 * Returns SATA_FAILURE if proper DMA mode could not be selected. 10600 * 10601 * NOTE: This function should be called only if DMA mode is supported. 10602 */ 10603 static int 10604 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 10605 { 10606 sata_pkt_t *spkt; 10607 sata_cmd_t *scmd; 10608 sata_pkt_txlate_t *spx; 10609 int i, mode; 10610 uint8_t subcmd; 10611 int rval = SATA_SUCCESS; 10612 10613 ASSERT(sdinfo != NULL); 10614 ASSERT(sata_hba_inst != NULL); 10615 10616 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 10617 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 10618 /* Find highest Ultra DMA mode supported */ 10619 for (mode = 6; mode >= 0; --mode) { 10620 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 10621 break; 10622 } 10623 #if 0 10624 /* Left for historical reasons */ 10625 /* 10626 * Some initial version of SATA spec indicated that at least 10627 * UDMA mode 4 has to be supported. It is not mentioned in 10628 * SerialATA 2.6, so this restriction is removed. 10629 */ 10630 if (mode < 4) 10631 return (SATA_FAILURE); 10632 #endif 10633 /* Find UDMA mode currently selected */ 10634 for (i = 6; i >= 0; --i) { 10635 if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8))) 10636 break; 10637 } 10638 if (i >= mode) 10639 /* Nothing to do */ 10640 return (SATA_SUCCESS); 10641 10642 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 10643 10644 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 10645 /* Find highest MultiWord DMA mode supported */ 10646 for (mode = 2; mode >= 0; --mode) { 10647 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 10648 break; 10649 } 10650 /* Find highest MultiWord DMA mode selected */ 10651 for (i = 2; i >= 0; --i) { 10652 if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8))) 10653 break; 10654 } 10655 if (i >= mode) 10656 /* Nothing to do */ 10657 return (SATA_SUCCESS); 10658 10659 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 10660 } else 10661 return (SATA_SUCCESS); 10662 10663 /* 10664 * Set DMA mode via SET FEATURES COMMAND. 10665 * Prepare packet for SET FEATURES COMMAND. 10666 */ 10667 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10668 spx->txlt_sata_hba_inst = sata_hba_inst; 10669 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10670 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10671 if (spkt == NULL) { 10672 SATA_LOG_D((sata_hba_inst, CE_WARN, 10673 "sata_set_dma_mode: could not set DMA mode %", mode)); 10674 rval = SATA_FAILURE; 10675 goto done; 10676 } 10677 /* Fill sata_pkt */ 10678 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10679 /* Timeout 30s */ 10680 spkt->satapkt_time = sata_default_pkt_time; 10681 /* Synchronous mode, no callback, interrupts */ 10682 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10683 spkt->satapkt_comp = NULL; 10684 scmd = &spkt->satapkt_cmd; 10685 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10686 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10687 scmd->satacmd_addr_type = 0; 10688 scmd->satacmd_device_reg = 0; 10689 scmd->satacmd_status_reg = 0; 10690 scmd->satacmd_error_reg = 0; 10691 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10692 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 10693 scmd->satacmd_sec_count_lsb = subcmd | mode; 10694 10695 /* Transfer command to HBA */ 10696 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 10697 spkt) != SATA_TRAN_ACCEPTED || 10698 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 10699 /* Pkt execution failed */ 10700 rval = SATA_FAILURE; 10701 } 10702 done: 10703 10704 /* Free allocated resources */ 10705 if (spkt != NULL) 10706 sata_pkt_free(spx); 10707 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10708 10709 return (rval); 10710 } 10711 10712 10713 /* 10714 * Set device caching mode. 10715 * One of the following operations should be specified: 10716 * SATAC_SF_ENABLE_READ_AHEAD 10717 * SATAC_SF_DISABLE_READ_AHEAD 10718 * SATAC_SF_ENABLE_WRITE_CACHE 10719 * SATAC_SF_DISABLE_WRITE_CACHE 10720 * 10721 * If operation fails, system log messgage is emitted. 10722 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 10723 * command was sent but did not succeed, and SATA_FAILURE otherwise. 10724 */ 10725 10726 static int 10727 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10728 int cache_op) 10729 { 10730 sata_pkt_t *spkt; 10731 sata_cmd_t *scmd; 10732 sata_pkt_txlate_t *spx; 10733 int rval = SATA_SUCCESS; 10734 int hba_rval; 10735 char *infop; 10736 10737 ASSERT(sdinfo != NULL); 10738 ASSERT(sata_hba_inst != NULL); 10739 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 10740 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 10741 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 10742 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 10743 10744 10745 /* Prepare packet for SET FEATURES COMMAND */ 10746 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10747 spx->txlt_sata_hba_inst = sata_hba_inst; 10748 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10749 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10750 if (spkt == NULL) { 10751 rval = SATA_FAILURE; 10752 goto failure; 10753 } 10754 /* Fill sata_pkt */ 10755 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10756 /* Timeout 30s */ 10757 spkt->satapkt_time = sata_default_pkt_time; 10758 /* Synchronous mode, no callback, interrupts */ 10759 spkt->satapkt_op_mode = 10760 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10761 spkt->satapkt_comp = NULL; 10762 scmd = &spkt->satapkt_cmd; 10763 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10764 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10765 scmd->satacmd_addr_type = 0; 10766 scmd->satacmd_device_reg = 0; 10767 scmd->satacmd_status_reg = 0; 10768 scmd->satacmd_error_reg = 0; 10769 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10770 scmd->satacmd_features_reg = cache_op; 10771 10772 /* Transfer command to HBA */ 10773 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 10774 SATA_DIP(sata_hba_inst), spkt); 10775 10776 #ifdef SATA_INJECT_FAULTS 10777 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10778 #endif 10779 10780 if ((hba_rval != SATA_TRAN_ACCEPTED) || 10781 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10782 /* Pkt execution failed */ 10783 switch (cache_op) { 10784 case SATAC_SF_ENABLE_READ_AHEAD: 10785 infop = "enabling read ahead failed"; 10786 break; 10787 case SATAC_SF_DISABLE_READ_AHEAD: 10788 infop = "disabling read ahead failed"; 10789 break; 10790 case SATAC_SF_ENABLE_WRITE_CACHE: 10791 infop = "enabling write cache failed"; 10792 break; 10793 case SATAC_SF_DISABLE_WRITE_CACHE: 10794 infop = "disabling write cache failed"; 10795 break; 10796 } 10797 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10798 rval = SATA_RETRY; 10799 } 10800 failure: 10801 /* Free allocated resources */ 10802 if (spkt != NULL) 10803 sata_pkt_free(spx); 10804 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10805 return (rval); 10806 } 10807 10808 /* 10809 * Set Removable Media Status Notification (enable/disable) 10810 * state == 0 , disable 10811 * state != 0 , enable 10812 * 10813 * If operation fails, system log messgage is emitted. 10814 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 10815 */ 10816 10817 static int 10818 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10819 int state) 10820 { 10821 sata_pkt_t *spkt; 10822 sata_cmd_t *scmd; 10823 sata_pkt_txlate_t *spx; 10824 int rval = SATA_SUCCESS; 10825 char *infop; 10826 10827 ASSERT(sdinfo != NULL); 10828 ASSERT(sata_hba_inst != NULL); 10829 10830 /* Prepare packet for SET FEATURES COMMAND */ 10831 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10832 spx->txlt_sata_hba_inst = sata_hba_inst; 10833 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10834 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10835 if (spkt == NULL) { 10836 rval = SATA_FAILURE; 10837 goto failure; 10838 } 10839 /* Fill sata_pkt */ 10840 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10841 /* Timeout 30s */ 10842 spkt->satapkt_time = sata_default_pkt_time; 10843 /* Synchronous mode, no callback, interrupts */ 10844 spkt->satapkt_op_mode = 10845 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10846 spkt->satapkt_comp = NULL; 10847 scmd = &spkt->satapkt_cmd; 10848 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10849 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10850 scmd->satacmd_addr_type = 0; 10851 scmd->satacmd_device_reg = 0; 10852 scmd->satacmd_status_reg = 0; 10853 scmd->satacmd_error_reg = 0; 10854 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10855 if (state == 0) 10856 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 10857 else 10858 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 10859 10860 /* Transfer command to HBA */ 10861 if (((*SATA_START_FUNC(sata_hba_inst))( 10862 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 10863 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10864 /* Pkt execution failed */ 10865 if (state == 0) 10866 infop = "disabling Removable Media Status " 10867 "Notification failed"; 10868 else 10869 infop = "enabling Removable Media Status " 10870 "Notification failed"; 10871 10872 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10873 rval = SATA_FAILURE; 10874 } 10875 failure: 10876 /* Free allocated resources */ 10877 if (spkt != NULL) 10878 sata_pkt_free(spx); 10879 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10880 return (rval); 10881 } 10882 10883 10884 /* 10885 * Update port SCR block 10886 */ 10887 static void 10888 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device) 10889 { 10890 port_scr->sstatus = device->satadev_scr.sstatus; 10891 port_scr->serror = device->satadev_scr.serror; 10892 port_scr->scontrol = device->satadev_scr.scontrol; 10893 port_scr->sactive = device->satadev_scr.sactive; 10894 port_scr->snotific = device->satadev_scr.snotific; 10895 } 10896 10897 /* 10898 * Update state and copy port ss* values from passed sata_device structure. 10899 * sata_address is validated - if not valid, nothing is changed in sata_scsi 10900 * configuration struct. 10901 * 10902 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 10903 * regardless of the state in device argument. 10904 * 10905 * Port mutex should be held while calling this function. 10906 */ 10907 static void 10908 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 10909 sata_device_t *sata_device) 10910 { 10911 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, 10912 sata_device->satadev_addr.cport))); 10913 10914 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 10915 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 10916 10917 sata_cport_info_t *cportinfo; 10918 10919 if (SATA_NUM_CPORTS(sata_hba_inst) <= 10920 sata_device->satadev_addr.cport) 10921 return; 10922 10923 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 10924 sata_device->satadev_addr.cport); 10925 sata_update_port_scr(&cportinfo->cport_scr, sata_device); 10926 10927 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10928 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 10929 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 10930 cportinfo->cport_state |= 10931 sata_device->satadev_state & SATA_PSTATE_VALID; 10932 } else { 10933 sata_pmport_info_t *pmportinfo; 10934 10935 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) || 10936 (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 10937 SATA_NUM_PMPORTS(sata_hba_inst, 10938 sata_device->satadev_addr.cport) < 10939 sata_device->satadev_addr.pmport) 10940 return; 10941 10942 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 10943 sata_device->satadev_addr.cport, 10944 sata_device->satadev_addr.pmport); 10945 sata_update_port_scr(&pmportinfo->pmport_scr, sata_device); 10946 10947 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10948 pmportinfo->pmport_state &= 10949 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | 10950 SATA_PSTATE_FAILED); 10951 pmportinfo->pmport_state |= 10952 sata_device->satadev_state & SATA_PSTATE_VALID; 10953 } 10954 } 10955 10956 10957 10958 /* 10959 * Extract SATA port specification from an IOCTL argument. 10960 * 10961 * This function return the port the user land send us as is, unless it 10962 * cannot retrieve port spec, then -1 is returned. 10963 * 10964 * Note: Only cport - no port multiplier port. 10965 */ 10966 static int32_t 10967 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 10968 { 10969 int32_t port; 10970 10971 /* Extract port number from nvpair in dca structure */ 10972 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 10973 SATA_LOG_D((sata_hba_inst, CE_NOTE, 10974 "sata_get_port_num: invalid port spec 0x%x in ioctl", 10975 port)); 10976 port = -1; 10977 } 10978 10979 return (port); 10980 } 10981 10982 /* 10983 * Get dev_info_t pointer to the device node pointed to by port argument. 10984 * NOTE: target argument is a value used in ioctls to identify 10985 * the AP - it is not a sata_address. 10986 * It is a combination of cport, pmport and address qualifier, encodded same 10987 * way as a scsi target number. 10988 * At this moment it carries only cport number. 10989 * 10990 * No PMult hotplug support. 10991 * 10992 * Returns dev_info_t pointer if target device was found, NULL otherwise. 10993 */ 10994 10995 static dev_info_t * 10996 sata_get_target_dip(dev_info_t *dip, int32_t port) 10997 { 10998 dev_info_t *cdip = NULL; 10999 int target, tgt; 11000 int ncport; 11001 int circ; 11002 11003 ncport = port & SATA_CFGA_CPORT_MASK; 11004 target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT); 11005 11006 ndi_devi_enter(dip, &circ); 11007 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 11008 dev_info_t *next = ddi_get_next_sibling(cdip); 11009 11010 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 11011 DDI_PROP_DONTPASS, "target", -1); 11012 if (tgt == -1) { 11013 /* 11014 * This is actually an error condition, but not 11015 * a fatal one. Just continue the search. 11016 */ 11017 cdip = next; 11018 continue; 11019 } 11020 11021 if (tgt == target) 11022 break; 11023 11024 cdip = next; 11025 } 11026 ndi_devi_exit(dip, circ); 11027 11028 return (cdip); 11029 } 11030 11031 /* 11032 * Get dev_info_t pointer to the device node pointed to by port argument. 11033 * NOTE: target argument is a value used in ioctls to identify 11034 * the AP - it is not a sata_address. 11035 * It is a combination of cport, pmport and address qualifier, encoded same 11036 * way as a scsi target number. 11037 * At this moment it carries only cport number. 11038 * 11039 * No PMult hotplug support. 11040 * 11041 * Returns dev_info_t pointer if target device was found, NULL otherwise. 11042 */ 11043 11044 static dev_info_t * 11045 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 11046 { 11047 dev_info_t *cdip = NULL; 11048 int target, tgt; 11049 int circ; 11050 11051 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 11052 11053 ndi_devi_enter(dip, &circ); 11054 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 11055 dev_info_t *next = ddi_get_next_sibling(cdip); 11056 11057 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 11058 DDI_PROP_DONTPASS, "target", -1); 11059 if (tgt == -1) { 11060 /* 11061 * This is actually an error condition, but not 11062 * a fatal one. Just continue the search. 11063 */ 11064 cdip = next; 11065 continue; 11066 } 11067 11068 if (tgt == target) 11069 break; 11070 11071 cdip = next; 11072 } 11073 ndi_devi_exit(dip, circ); 11074 11075 return (cdip); 11076 } 11077 11078 /* 11079 * Process sata port disconnect request. 11080 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 11081 * before this request. Nevertheless, if a device is still configured, 11082 * we need to attempt to offline and unconfigure device. 11083 * Regardless of the unconfigure operation results the port is marked as 11084 * deactivated and no access to the attached device is possible. 11085 * If the target node remains because unconfigure operation failed, its state 11086 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 11087 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 11088 * the device and remove old target node. 11089 * 11090 * This function invokes sata_hba_inst->satahba_tran-> 11091 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 11092 * If successful, the device structure (if any) attached to the specified port 11093 * is removed and state of the port marked appropriately. 11094 * Failure of the port_deactivate may keep port in the physically active state, 11095 * or may fail the port. 11096 * 11097 * NOTE: Port multiplier code is not completed nor tested. 11098 */ 11099 11100 static int 11101 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 11102 sata_device_t *sata_device) 11103 { 11104 sata_drive_info_t *sdinfo = NULL; 11105 sata_cport_info_t *cportinfo = NULL; 11106 sata_pmport_info_t *pmportinfo = NULL; 11107 sata_pmult_info_t *pmultinfo = NULL; 11108 dev_info_t *tdip; 11109 int cport, pmport, qual; 11110 int rval = SATA_SUCCESS; 11111 int rv = 0; 11112 11113 cport = sata_device->satadev_addr.cport; 11114 pmport = sata_device->satadev_addr.pmport; 11115 qual = sata_device->satadev_addr.qual; 11116 11117 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11118 11119 /* 11120 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 11121 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 11122 * Do the sanity check. 11123 */ 11124 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 11125 /* No physical port deactivation supported. */ 11126 return (EINVAL); 11127 } 11128 11129 /* Check the current state of the port */ 11130 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11131 (SATA_DIP(sata_hba_inst), sata_device); 11132 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11133 sata_update_port_info(sata_hba_inst, sata_device); 11134 if (rval != SATA_SUCCESS || 11135 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11136 /* Device port status is unknown or it is in failed state */ 11137 if (qual == SATA_ADDR_PMPORT) { 11138 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 11139 SATA_PSTATE_FAILED; 11140 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11141 "sata_hba_ioctl: connect: failed to deactivate " 11142 "SATA port %d", cport); 11143 } else { 11144 SATA_CPORT_STATE(sata_hba_inst, cport) = 11145 SATA_PSTATE_FAILED; 11146 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11147 "sata_hba_ioctl: connect: failed to deactivate " 11148 "SATA port %d:%d", cport, pmport); 11149 } 11150 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11151 cport)->cport_mutex); 11152 return (EIO); 11153 } 11154 /* 11155 * Set port's dev_state to not ready - this will disable 11156 * an access to a potentially attached device. 11157 */ 11158 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11159 if (qual == SATA_ADDR_PMPORT) { 11160 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11161 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 11162 sdinfo = pmportinfo->pmport_sata_drive; 11163 ASSERT(sdinfo != NULL); 11164 } 11165 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11166 } else { 11167 /* Assuming cport */ 11168 11169 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11170 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 11171 pmultinfo = 11172 cportinfo->cport_devp.cport_sata_pmult; 11173 ASSERT(pmultinfo != NULL); 11174 } else { 11175 sdinfo = cportinfo->cport_devp.cport_sata_drive; 11176 } 11177 } 11178 cportinfo->cport_state &= ~SATA_STATE_READY; 11179 } 11180 if (sdinfo != NULL) { 11181 if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) { 11182 /* 11183 * If a target node exists, try to offline 11184 * a device and remove target node. 11185 */ 11186 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11187 cport)->cport_mutex); 11188 /* We are addressing attached device, not a port */ 11189 sata_device->satadev_addr.qual = 11190 sdinfo->satadrv_addr.qual; 11191 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11192 &sata_device->satadev_addr); 11193 if (tdip != NULL && ndi_devi_offline(tdip, 11194 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11195 /* 11196 * Problem 11197 * The target node remained attached. 11198 * This happens when the device file was open 11199 * or a node was waiting for resources. 11200 * Cannot do anything about it. 11201 */ 11202 if (qual == SATA_ADDR_CPORT) { 11203 SATA_LOG_D((sata_hba_inst, CE_WARN, 11204 "sata_hba_ioctl: disconnect: could " 11205 "not unconfigure device before " 11206 "disconnecting the SATA port %d", 11207 cport)); 11208 } else { 11209 SATA_LOG_D((sata_hba_inst, CE_WARN, 11210 "sata_hba_ioctl: disconnect: could " 11211 "not unconfigure device before " 11212 "disconnecting the SATA port %d:%d", 11213 cport, pmport)); 11214 } 11215 /* 11216 * Set DEVICE REMOVED state in the target 11217 * node. It will prevent access to the device 11218 * even when a new device is attached, until 11219 * the old target node is released, removed and 11220 * recreated for a new device. 11221 */ 11222 sata_set_device_removed(tdip); 11223 11224 /* 11225 * Instruct event daemon to try the target 11226 * node cleanup later. 11227 */ 11228 sata_set_target_node_cleanup( 11229 sata_hba_inst, &sata_device->satadev_addr); 11230 } 11231 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11232 cport)->cport_mutex); 11233 } 11234 11235 /* Remove and release sata_drive info structure. */ 11236 if (pmportinfo != NULL) { 11237 SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) = 11238 NULL; 11239 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11240 } else { 11241 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11242 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11243 } 11244 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11245 } 11246 #if 0 11247 else if (pmultinfo != NULL) { 11248 /* 11249 * Port Multiplier itself needs special handling. 11250 * All device ports need to be processed here! 11251 */ 11252 } 11253 #endif 11254 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11255 /* Just ask HBA driver to deactivate port */ 11256 /* sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */ 11257 11258 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11259 (SATA_DIP(sata_hba_inst), sata_device); 11260 11261 /* 11262 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11263 * without the hint (to force listener to investivate the state). 11264 */ 11265 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11266 SE_NO_HINT); 11267 11268 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11269 sata_update_port_info(sata_hba_inst, sata_device); 11270 11271 if (rval != SATA_SUCCESS) { 11272 /* 11273 * Port deactivation failure - do not 11274 * change port state unless the state 11275 * returned by HBA indicates a port failure. 11276 * NOTE: device structures were released, so devices now are 11277 * invisible! Port reset is needed to re-enumerate devices. 11278 */ 11279 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11280 if (pmportinfo != NULL) 11281 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11282 else 11283 cportinfo->cport_state = SATA_PSTATE_FAILED; 11284 rv = EIO; 11285 } 11286 } else { 11287 /* 11288 * Deactivation succeded. From now on the sata framework 11289 * will not care what is happening to the device, until 11290 * the port is activated again. 11291 */ 11292 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11293 } 11294 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11295 return (rv); 11296 } 11297 11298 11299 11300 /* 11301 * Process sata port connect request 11302 * The sata cfgadm pluging will invoke this operation only if port was found 11303 * in the disconnect state (failed state is also treated as the disconnected 11304 * state). 11305 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 11306 * sata_tran_hotplug_ops->sata_tran_port_activate(). 11307 * If successful and a device is found attached to the port, 11308 * the initialization sequence is executed to attach a device structure to 11309 * a port structure. The state of the port and a device would be set 11310 * appropriately. 11311 * The device is not set in configured state (system-wise) by this operation. 11312 * 11313 * Note, that activating the port may generate link events, 11314 * so it is important that following processing and the 11315 * event processing does not interfere with each other! 11316 * 11317 * This operation may remove port failed state and will 11318 * try to make port active and in good standing. 11319 * 11320 * NOTE: Port multiplier code is not completed nor tested. 11321 */ 11322 11323 static int 11324 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 11325 sata_device_t *sata_device) 11326 { 11327 int cport, pmport, qual; 11328 int rv = 0; 11329 11330 cport = sata_device->satadev_addr.cport; 11331 pmport = sata_device->satadev_addr.pmport; 11332 qual = sata_device->satadev_addr.qual; 11333 11334 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11335 11336 /* 11337 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 11338 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 11339 * Perform sanity check now. 11340 */ 11341 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 11342 /* No physical port activation supported. */ 11343 return (EINVAL); 11344 } 11345 11346 /* Just ask HBA driver to activate port */ 11347 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11348 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11349 /* 11350 * Port activation failure. 11351 */ 11352 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11353 cport)->cport_mutex); 11354 sata_update_port_info(sata_hba_inst, sata_device); 11355 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11356 if (qual == SATA_ADDR_DCPORT) { 11357 SATA_CPORT_STATE(sata_hba_inst, cport) = 11358 SATA_PSTATE_FAILED; 11359 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11360 "sata_hba_ioctl: connect: failed to " 11361 "activate SATA port %d", cport); 11362 } else { /* port multiplier device port */ 11363 SATA_PMPORT_STATE(sata_hba_inst, cport, 11364 pmport) = SATA_PSTATE_FAILED; 11365 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11366 "sata_hba_ioctl: connect: failed to " 11367 "activate SATA port %d:%d", cport, pmport); 11368 11369 } 11370 } 11371 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11372 cport)->cport_mutex); 11373 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11374 "sata_hba_ioctl: connect: failed to activate SATA " 11375 "port %d:%d", cport, pmport); 11376 return (EIO); 11377 } 11378 11379 /* Virgin port state - will be updated by the port re-probe. */ 11380 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11381 if (qual == SATA_ADDR_CPORT) 11382 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 11383 else /* port multiplier device port */ 11384 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 11385 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11386 11387 /* 11388 * Probe the port to find its state and attached device. 11389 */ 11390 if (sata_reprobe_port(sata_hba_inst, sata_device, 11391 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 11392 rv = EIO; 11393 11394 /* 11395 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11396 * without the hint 11397 */ 11398 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11399 SE_NO_HINT); 11400 11401 /* 11402 * If there is a device attached to the port, emit 11403 * a message. 11404 */ 11405 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11406 11407 if (qual == SATA_ADDR_CPORT) { 11408 sata_log(sata_hba_inst, CE_WARN, 11409 "SATA device detected at port %d", cport); 11410 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11411 /* 11412 * A device was not successfully identified 11413 */ 11414 sata_log(sata_hba_inst, CE_WARN, 11415 "Could not identify SATA " 11416 "device at port %d", cport); 11417 } 11418 } else { /* port multiplier device port */ 11419 sata_log(sata_hba_inst, CE_WARN, 11420 "SATA device detected at port %d:%d", 11421 cport, pmport); 11422 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11423 /* 11424 * A device was not successfully identified 11425 */ 11426 sata_log(sata_hba_inst, CE_WARN, 11427 "Could not identify SATA " 11428 "device at port %d:%d", cport, pmport); 11429 } 11430 } 11431 } 11432 11433 return (rv); 11434 } 11435 11436 11437 /* 11438 * Process sata device unconfigure request. 11439 * The unconfigure operation uses generic nexus operation to 11440 * offline a device. It leaves a target device node attached. 11441 * and obviously sata_drive_info attached as well, because 11442 * from the hardware point of view nothing has changed. 11443 */ 11444 static int 11445 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 11446 sata_device_t *sata_device) 11447 { 11448 int rv = 0; 11449 dev_info_t *tdip; 11450 11451 /* We are addressing attached device, not a port */ 11452 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 11453 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11454 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 11455 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11456 11457 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11458 &sata_device->satadev_addr)) != NULL) { 11459 11460 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 11461 SATA_LOG_D((sata_hba_inst, CE_WARN, 11462 "sata_hba_ioctl: unconfigure: " 11463 "failed to unconfigure device at SATA port %d:%d", 11464 sata_device->satadev_addr.cport, 11465 sata_device->satadev_addr.pmport)); 11466 rv = EIO; 11467 } 11468 /* 11469 * The target node devi_state should be marked with 11470 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 11471 * This would be the indication for cfgadm that 11472 * the AP node occupant state is 'unconfigured'. 11473 */ 11474 11475 } else { 11476 /* 11477 * This would indicate a failure on the part of cfgadm 11478 * to detect correct state of the node prior to this 11479 * call - one cannot unconfigure non-existing device. 11480 */ 11481 SATA_LOG_D((sata_hba_inst, CE_WARN, 11482 "sata_hba_ioctl: unconfigure: " 11483 "attempt to unconfigure non-existing device " 11484 "at SATA port %d:%d", 11485 sata_device->satadev_addr.cport, 11486 sata_device->satadev_addr.pmport)); 11487 rv = ENXIO; 11488 } 11489 return (rv); 11490 } 11491 11492 /* 11493 * Process sata device configure request 11494 * If port is in a failed state, operation is aborted - one has to use 11495 * an explicit connect or port activate request to try to get a port into 11496 * non-failed mode. Port reset wil also work in such situation. 11497 * If the port is in disconnected (shutdown) state, the connect operation is 11498 * attempted prior to any other action. 11499 * When port is in the active state, there is a device attached and the target 11500 * node exists, a device was most likely offlined. 11501 * If target node does not exist, a new target node is created. In both cases 11502 * an attempt is made to online (configure) the device. 11503 * 11504 * NOTE: Port multiplier code is not completed nor tested. 11505 */ 11506 static int 11507 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 11508 sata_device_t *sata_device) 11509 { 11510 int cport, pmport, qual; 11511 int rval; 11512 boolean_t target = TRUE; 11513 sata_cport_info_t *cportinfo; 11514 sata_pmport_info_t *pmportinfo = NULL; 11515 dev_info_t *tdip; 11516 sata_drive_info_t *sdinfo; 11517 11518 cport = sata_device->satadev_addr.cport; 11519 pmport = sata_device->satadev_addr.pmport; 11520 qual = sata_device->satadev_addr.qual; 11521 11522 /* Get current port state */ 11523 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11524 (SATA_DIP(sata_hba_inst), sata_device); 11525 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11526 sata_update_port_info(sata_hba_inst, sata_device); 11527 11528 if (rval != SATA_SUCCESS || 11529 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11530 /* 11531 * Obviously, device on a failed port is not visible 11532 */ 11533 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11534 return (ENXIO); 11535 } 11536 11537 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11538 if (qual == SATA_ADDR_PMPORT) 11539 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11540 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11541 11542 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 11543 /* need to activate port */ 11544 target = FALSE; 11545 11546 /* Sanity check */ 11547 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11548 return (ENXIO); 11549 11550 /* Just let HBA driver to activate port */ 11551 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11552 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11553 /* 11554 * Port activation failure - do not change port state 11555 * unless the state returned by HBA indicates a port 11556 * failure. 11557 */ 11558 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11559 cport)->cport_mutex); 11560 sata_update_port_info(sata_hba_inst, sata_device); 11561 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11562 if (qual == SATA_ADDR_PMPORT) 11563 pmportinfo->pmport_state = 11564 SATA_PSTATE_FAILED; 11565 else 11566 cportinfo->cport_state = 11567 SATA_PSTATE_FAILED; 11568 } 11569 mutex_exit(&SATA_CPORT_INFO( 11570 sata_hba_inst, cport)->cport_mutex); 11571 SATA_LOG_D((sata_hba_inst, CE_WARN, 11572 "sata_hba_ioctl: configure: " 11573 "failed to activate SATA port %d:%d", 11574 cport, pmport)); 11575 return (EIO); 11576 } 11577 /* 11578 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11579 * without the hint. 11580 */ 11581 sata_gen_sysevent(sata_hba_inst, 11582 &sata_device->satadev_addr, SE_NO_HINT); 11583 11584 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11585 cport_mutex); 11586 /* Virgin port state */ 11587 if (qual == SATA_ADDR_PMPORT) 11588 pmportinfo->pmport_state = 0; 11589 else 11590 cportinfo->cport_state = 0; 11591 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11592 } 11593 /* 11594 * Always reprobe port, to get current device info. 11595 */ 11596 if (sata_reprobe_port(sata_hba_inst, sata_device, 11597 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 11598 return (EIO); 11599 11600 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 11601 if (qual == SATA_ADDR_PMPORT) { 11602 /* 11603 * That's the transition from "inactive" port 11604 * to active one with device attached. 11605 */ 11606 sata_log(sata_hba_inst, CE_WARN, 11607 "SATA device detected at port %d:%d", 11608 cport, pmport); 11609 } else { 11610 /* 11611 * When PM is attached to the cport and cport is 11612 * activated, every PM device port needs to be reprobed. 11613 * We need to emit message for all devices detected 11614 * at port multiplier's device ports. 11615 * Add such code here. 11616 * For now, just inform about device attached to 11617 * cport. 11618 */ 11619 sata_log(sata_hba_inst, CE_WARN, 11620 "SATA device detected at port %d", cport); 11621 } 11622 } 11623 11624 /* 11625 * This is where real configuration operation starts. 11626 * 11627 * When PM is attached to the cport and cport is activated, 11628 * devices attached PM device ports may have to be configured 11629 * explicitly. This may change when port multiplier is supported. 11630 * For now, configure only disks and other valid target devices. 11631 */ 11632 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 11633 if (qual == SATA_ADDR_CPORT) { 11634 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11635 /* 11636 * A device was not successfully identified 11637 */ 11638 sata_log(sata_hba_inst, CE_WARN, 11639 "Could not identify SATA " 11640 "device at port %d", cport); 11641 } 11642 } else { /* port multiplier device port */ 11643 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11644 /* 11645 * A device was not successfully identified 11646 */ 11647 sata_log(sata_hba_inst, CE_WARN, 11648 "Could not identify SATA " 11649 "device at port %d:%d", cport, pmport); 11650 } 11651 } 11652 return (ENXIO); /* No device to configure */ 11653 } 11654 11655 /* 11656 * Here we may have a device in reset condition, 11657 * but because we are just configuring it, there is 11658 * no need to process the reset other than just 11659 * to clear device reset condition in the HBA driver. 11660 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 11661 * cause a first command sent the HBA driver with the request 11662 * to clear device reset condition. 11663 */ 11664 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11665 if (qual == SATA_ADDR_PMPORT) 11666 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11667 else 11668 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11669 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11670 if (sdinfo == NULL) { 11671 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11672 return (ENXIO); 11673 } 11674 if (sdinfo->satadrv_event_flags & 11675 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 11676 sdinfo->satadrv_event_flags = 0; 11677 } 11678 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 11679 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11680 11681 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11682 &sata_device->satadev_addr)) != NULL) { 11683 /* 11684 * Target node exists. Verify, that it belongs 11685 * to existing, attached device and not to 11686 * a removed device. 11687 */ 11688 if (sata_check_device_removed(tdip) == B_TRUE) { 11689 if (qual == SATA_ADDR_DPMPORT) 11690 sata_log(sata_hba_inst, CE_WARN, 11691 "SATA device at port %d cannot be " 11692 "configured. " 11693 "Application(s) accessing " 11694 "previously attached device " 11695 "have to release it before newly " 11696 "inserted device can be made accessible.", 11697 cport); 11698 else 11699 sata_log(sata_hba_inst, CE_WARN, 11700 "SATA device at port %d:%d cannot be" 11701 "configured. " 11702 "Application(s) accessing " 11703 "previously attached device " 11704 "have to release it before newly " 11705 "inserted device can be made accessible.", 11706 cport, pmport); 11707 return (EIO); 11708 } 11709 /* 11710 * Device was not removed and re-inserted. 11711 * Try to online it. 11712 */ 11713 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 11714 SATA_LOG_D((sata_hba_inst, CE_WARN, 11715 "sata_hba_ioctl: configure: " 11716 "onlining device at SATA port " 11717 "%d:%d failed", cport, pmport)); 11718 return (EIO); 11719 } 11720 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11721 cport)->cport_mutex); 11722 11723 if (qual == SATA_ADDR_DPMPORT) 11724 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11725 else 11726 cportinfo-> cport_tgtnode_clean = B_TRUE; 11727 11728 mutex_exit(&SATA_CPORT_INFO( 11729 sata_hba_inst, cport)->cport_mutex); 11730 } else { 11731 /* 11732 * No target node - need to create a new target node. 11733 */ 11734 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11735 cport_mutex); 11736 if (qual == SATA_ADDR_DPMPORT) 11737 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11738 else 11739 cportinfo-> cport_tgtnode_clean = B_TRUE; 11740 11741 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11742 cport_mutex); 11743 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 11744 sata_hba_inst, &sata_device->satadev_addr); 11745 if (tdip == NULL) { 11746 /* Configure operation failed */ 11747 SATA_LOG_D((sata_hba_inst, CE_WARN, 11748 "sata_hba_ioctl: configure: " 11749 "configuring SATA device at port %d:%d " 11750 "failed", cport, pmport)); 11751 return (EIO); 11752 } 11753 } 11754 return (0); 11755 } 11756 11757 11758 /* 11759 * Process ioctl deactivate port request. 11760 * Arbitrarily unconfigure attached device, if any. 11761 * Even if the unconfigure fails, proceed with the 11762 * port deactivation. 11763 * 11764 * NOTE: Port Multiplier code is not completed and tested. 11765 */ 11766 11767 static int 11768 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 11769 sata_device_t *sata_device) 11770 { 11771 int cport, pmport, qual; 11772 int rval, rv = 0; 11773 sata_cport_info_t *cportinfo; 11774 sata_pmport_info_t *pmportinfo = NULL; 11775 dev_info_t *tdip; 11776 sata_drive_info_t *sdinfo = NULL; 11777 11778 /* Sanity check */ 11779 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 11780 return (ENOTSUP); 11781 11782 cport = sata_device->satadev_addr.cport; 11783 pmport = sata_device->satadev_addr.pmport; 11784 qual = sata_device->satadev_addr.qual; 11785 11786 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11787 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11788 if (qual == SATA_ADDR_CPORT) { 11789 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11790 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11791 /* 11792 * For now, assume that port multiplier is not 11793 * supported, i.e. deal only with valid devices 11794 */ 11795 if ((cportinfo->cport_dev_type & 11796 SATA_VALID_DEV_TYPE) != 0) 11797 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11798 /* 11799 * If attached device is a port multiplier, we will 11800 * have to unconfigure all devices attached to the 11801 * port multiplier. Add this code here. 11802 */ 11803 } 11804 cportinfo->cport_state &= ~SATA_STATE_READY; 11805 } else { 11806 /* Port multiplier device port */ 11807 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11808 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11809 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 11810 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 11811 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11812 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11813 } 11814 11815 if (sdinfo != NULL) { 11816 /* 11817 * If a target node exists, try to offline a device and 11818 * to remove a target node. 11819 */ 11820 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11821 cport_mutex); 11822 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11823 &sata_device->satadev_addr); 11824 if (tdip != NULL) { 11825 /* target node exist */ 11826 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11827 "sata_hba_ioctl: port deactivate: " 11828 "target node exists.", NULL); 11829 11830 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 11831 NDI_SUCCESS) { 11832 SATA_LOG_D((sata_hba_inst, CE_WARN, 11833 "sata_hba_ioctl: port deactivate: " 11834 "failed to unconfigure device at port " 11835 "%d:%d before deactivating the port", 11836 cport, pmport)); 11837 /* 11838 * Set DEVICE REMOVED state in the target 11839 * node. It will prevent an access to 11840 * the device even when a new device is 11841 * attached, until the old target node is 11842 * released, removed and recreated for a new 11843 * device. 11844 */ 11845 sata_set_device_removed(tdip); 11846 11847 /* 11848 * Instruct the event daemon to try the 11849 * target node cleanup later. 11850 */ 11851 sata_set_target_node_cleanup(sata_hba_inst, 11852 &sata_device->satadev_addr); 11853 } 11854 } 11855 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11856 cport_mutex); 11857 /* 11858 * In any case, remove and release sata_drive_info 11859 * structure. 11860 */ 11861 if (qual == SATA_ADDR_CPORT) { 11862 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11863 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11864 } else { /* port multiplier device port */ 11865 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11866 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11867 } 11868 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11869 } 11870 if (qual == SATA_ADDR_CPORT) { 11871 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 11872 SATA_STATE_PROBING); 11873 } else { /* port multiplier device port */ 11874 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 11875 SATA_STATE_PROBING); 11876 } 11877 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11878 11879 /* Just let HBA driver to deactivate port */ 11880 sata_device->satadev_addr.qual = qual; 11881 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11882 (SATA_DIP(sata_hba_inst), sata_device); 11883 11884 /* 11885 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11886 * without the hint 11887 */ 11888 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11889 SE_NO_HINT); 11890 11891 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11892 sata_update_port_info(sata_hba_inst, sata_device); 11893 if (qual == SATA_ADDR_CPORT) { 11894 if (rval != SATA_SUCCESS) { 11895 /* 11896 * Port deactivation failure - do not change port state 11897 * unless the state returned by HBA indicates a port 11898 * failure. 11899 */ 11900 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11901 SATA_CPORT_STATE(sata_hba_inst, cport) = 11902 SATA_PSTATE_FAILED; 11903 } 11904 SATA_LOG_D((sata_hba_inst, CE_WARN, 11905 "sata_hba_ioctl: port deactivate: " 11906 "cannot deactivate SATA port %d", cport)); 11907 rv = EIO; 11908 } else { 11909 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11910 } 11911 } else { 11912 if (rval != SATA_SUCCESS) { 11913 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11914 SATA_PMPORT_STATE(sata_hba_inst, cport, 11915 pmport) = SATA_PSTATE_FAILED; 11916 } 11917 SATA_LOG_D((sata_hba_inst, CE_WARN, 11918 "sata_hba_ioctl: port deactivate: " 11919 "cannot deactivate SATA port %d:%d", 11920 cport, pmport)); 11921 rv = EIO; 11922 } else { 11923 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 11924 } 11925 } 11926 11927 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11928 11929 return (rv); 11930 } 11931 11932 /* 11933 * Process ioctl port activate request. 11934 * 11935 * NOTE: Port multiplier code is not completed nor tested. 11936 */ 11937 static int 11938 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 11939 sata_device_t *sata_device) 11940 { 11941 int cport, pmport, qual; 11942 sata_cport_info_t *cportinfo; 11943 sata_pmport_info_t *pmportinfo = NULL; 11944 boolean_t dev_existed = TRUE; 11945 11946 /* Sanity check */ 11947 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11948 return (ENOTSUP); 11949 11950 cport = sata_device->satadev_addr.cport; 11951 pmport = sata_device->satadev_addr.pmport; 11952 qual = sata_device->satadev_addr.qual; 11953 11954 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11955 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11956 if (qual == SATA_ADDR_PMPORT) { 11957 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11958 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 11959 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 11960 dev_existed = FALSE; 11961 } else { /* cport */ 11962 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 11963 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 11964 dev_existed = FALSE; 11965 } 11966 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11967 11968 /* Just let HBA driver to activate port, if necessary */ 11969 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11970 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11971 /* 11972 * Port activation failure - do not change port state unless 11973 * the state returned by HBA indicates a port failure. 11974 */ 11975 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11976 cport)->cport_mutex); 11977 sata_update_port_info(sata_hba_inst, sata_device); 11978 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11979 if (qual == SATA_ADDR_PMPORT) 11980 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11981 else 11982 cportinfo->cport_state = SATA_PSTATE_FAILED; 11983 11984 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11985 cport)->cport_mutex); 11986 SATA_LOG_D((sata_hba_inst, CE_WARN, 11987 "sata_hba_ioctl: port activate: cannot activate " 11988 "SATA port %d:%d", cport, pmport)); 11989 return (EIO); 11990 } 11991 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11992 } 11993 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11994 if (qual == SATA_ADDR_PMPORT) 11995 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 11996 else 11997 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 11998 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11999 12000 /* 12001 * Re-probe port to find its current state and possibly attached device. 12002 * Port re-probing may change the cportinfo device type if device is 12003 * found attached. 12004 * If port probing failed, the device type would be set to 12005 * SATA_DTYPE_NONE. 12006 */ 12007 (void) sata_reprobe_port(sata_hba_inst, sata_device, 12008 SATA_DEV_IDENTIFY_RETRY); 12009 12010 /* 12011 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 12012 * without the hint. 12013 */ 12014 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 12015 SE_NO_HINT); 12016 12017 if (dev_existed == FALSE) { 12018 if (qual == SATA_ADDR_PMPORT && 12019 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 12020 /* 12021 * That's the transition from the "inactive" port state 12022 * or the active port without a device attached to the 12023 * active port state with a device attached. 12024 */ 12025 sata_log(sata_hba_inst, CE_WARN, 12026 "SATA device detected at port %d:%d", 12027 cport, pmport); 12028 } else if (qual == SATA_ADDR_CPORT && 12029 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 12030 /* 12031 * That's the transition from the "inactive" port state 12032 * or the active port without a device attached to the 12033 * active port state with a device attached. 12034 */ 12035 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 12036 sata_log(sata_hba_inst, CE_WARN, 12037 "SATA device detected at port %d", cport); 12038 } else { 12039 sata_log(sata_hba_inst, CE_WARN, 12040 "SATA port multiplier detected at port %d", 12041 cport); 12042 /* 12043 * Because the detected device is a port 12044 * multiplier, we need to reprobe every device 12045 * port on the port multiplier and show every 12046 * device found attached. 12047 * Add this code here. 12048 */ 12049 } 12050 } 12051 } 12052 return (0); 12053 } 12054 12055 12056 12057 /* 12058 * Process ioctl reset port request. 12059 * 12060 * NOTE: Port multiplier code is not completed nor tested. 12061 */ 12062 static int 12063 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 12064 sata_device_t *sata_device) 12065 { 12066 int cport, pmport, qual; 12067 int rv = 0; 12068 12069 cport = sata_device->satadev_addr.cport; 12070 pmport = sata_device->satadev_addr.pmport; 12071 qual = sata_device->satadev_addr.qual; 12072 12073 /* Sanity check */ 12074 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12075 SATA_LOG_D((sata_hba_inst, CE_WARN, 12076 "sata_hba_ioctl: sata_hba_tran missing required " 12077 "function sata_tran_reset_dport")); 12078 return (ENOTSUP); 12079 } 12080 12081 /* Ask HBA to reset port */ 12082 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 12083 sata_device) != SATA_SUCCESS) { 12084 SATA_LOG_D((sata_hba_inst, CE_WARN, 12085 "sata_hba_ioctl: reset port: failed %d:%d", 12086 cport, pmport)); 12087 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12088 cport_mutex); 12089 sata_update_port_info(sata_hba_inst, sata_device); 12090 if (qual == SATA_ADDR_CPORT) 12091 SATA_CPORT_STATE(sata_hba_inst, cport) = 12092 SATA_PSTATE_FAILED; 12093 else 12094 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 12095 SATA_PSTATE_FAILED; 12096 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12097 cport_mutex); 12098 rv = EIO; 12099 } 12100 /* 12101 * Beacuse the port was reset, it should be probed and 12102 * attached device reinitialized. At this point the 12103 * port state is unknown - it's state is HBA-specific. 12104 * Re-probe port to get its state. 12105 */ 12106 if (sata_reprobe_port(sata_hba_inst, sata_device, 12107 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 12108 rv = EIO; 12109 } 12110 return (rv); 12111 } 12112 12113 /* 12114 * Process ioctl reset device request. 12115 * 12116 * NOTE: Port multiplier code is not completed nor tested. 12117 */ 12118 static int 12119 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 12120 sata_device_t *sata_device) 12121 { 12122 sata_drive_info_t *sdinfo; 12123 int cport, pmport; 12124 int rv = 0; 12125 12126 /* Sanity check */ 12127 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12128 SATA_LOG_D((sata_hba_inst, CE_WARN, 12129 "sata_hba_ioctl: sata_hba_tran missing required " 12130 "function sata_tran_reset_dport")); 12131 return (ENOTSUP); 12132 } 12133 12134 cport = sata_device->satadev_addr.cport; 12135 pmport = sata_device->satadev_addr.pmport; 12136 12137 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12138 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) { 12139 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 12140 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12141 sata_device->satadev_addr.cport); 12142 } else { /* port multiplier */ 12143 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 12144 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12145 sata_device->satadev_addr.cport, 12146 sata_device->satadev_addr.pmport); 12147 } 12148 if (sdinfo == NULL) { 12149 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12150 return (EINVAL); 12151 } 12152 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12153 12154 /* Ask HBA to reset device */ 12155 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12156 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12157 SATA_LOG_D((sata_hba_inst, CE_WARN, 12158 "sata_hba_ioctl: reset device: failed at port %d:%d", 12159 cport, pmport)); 12160 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12161 cport_mutex); 12162 sata_update_port_info(sata_hba_inst, sata_device); 12163 /* 12164 * Device info structure remains attached. Another device reset 12165 * or port disconnect/connect and re-probing is 12166 * needed to change it's state 12167 */ 12168 sdinfo->satadrv_state &= ~SATA_STATE_READY; 12169 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 12170 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12171 rv = EIO; 12172 } 12173 /* 12174 * If attached device was a port multiplier, some extra processing 12175 * may be needed, to bring it back (if port re-probing did not handle 12176 * it). Add such code here. 12177 */ 12178 return (rv); 12179 } 12180 12181 12182 /* 12183 * Process ioctl reset all request. 12184 * 12185 * NOTE: Port multiplier code is not completed nor tested. 12186 */ 12187 static int 12188 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 12189 { 12190 sata_device_t sata_device; 12191 int rv = 0; 12192 int tcport; 12193 int tpmport = 0; 12194 12195 sata_device.satadev_rev = SATA_DEVICE_REV; 12196 12197 /* 12198 * There is no protection here for configured devices. 12199 */ 12200 /* Sanity check */ 12201 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12202 SATA_LOG_D((sata_hba_inst, CE_WARN, 12203 "sata_hba_ioctl: sata_hba_tran missing required " 12204 "function sata_tran_reset_dport")); 12205 return (ENOTSUP); 12206 } 12207 12208 /* 12209 * Need to lock all ports, not just one. 12210 * If any port is locked by event processing, fail the whole operation. 12211 * One port is already locked, but for simplicity lock it again. 12212 */ 12213 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12214 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12215 cport_mutex); 12216 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12217 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 12218 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12219 cport_mutex); 12220 rv = EBUSY; 12221 break; 12222 } else { 12223 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12224 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 12225 /* 12226 * If there is a port multiplier attached, we may need 12227 * to lock its port as well. If so, add such code here. 12228 */ 12229 } 12230 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12231 cport_mutex); 12232 } 12233 12234 if (rv == 0) { 12235 /* 12236 * All cports were successfully locked. 12237 * Reset main SATA controller only for now - no PMult. 12238 * Set the device address to port 0, to have a valid device 12239 * address. 12240 */ 12241 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 12242 sata_device.satadev_addr.cport = 0; 12243 sata_device.satadev_addr.pmport = 0; 12244 12245 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12246 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 12247 SATA_LOG_D((sata_hba_inst, CE_WARN, 12248 "sata_hba_ioctl: reset controller failed")); 12249 return (EIO); 12250 } 12251 /* 12252 * Because ports were reset, port states are unknown. 12253 * They should be re-probed to get their state and 12254 * attached devices should be reinitialized. 12255 * Add code here to re-probe port multiplier device ports. 12256 */ 12257 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 12258 tcport++) { 12259 sata_device.satadev_addr.cport = tcport; 12260 sata_device.satadev_addr.pmport = tpmport; 12261 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 12262 12263 if (sata_reprobe_port(sata_hba_inst, &sata_device, 12264 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12265 rv = EIO; 12266 } 12267 } 12268 /* 12269 * Unlock all ports 12270 */ 12271 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12272 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12273 cport_mutex); 12274 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12275 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 12276 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12277 cport_mutex); 12278 } 12279 12280 /* 12281 * This operation returns EFAULT if either reset 12282 * controller failed or a re-probing of any port failed. 12283 */ 12284 return (rv); 12285 } 12286 12287 12288 /* 12289 * Process ioctl port self test request. 12290 * 12291 * NOTE: Port multiplier code is not completed nor tested. 12292 */ 12293 static int 12294 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 12295 sata_device_t *sata_device) 12296 { 12297 int cport, pmport, qual; 12298 int rv = 0; 12299 12300 /* Sanity check */ 12301 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 12302 return (ENOTSUP); 12303 12304 cport = sata_device->satadev_addr.cport; 12305 pmport = sata_device->satadev_addr.pmport; 12306 qual = sata_device->satadev_addr.qual; 12307 12308 /* 12309 * There is no protection here for a configured 12310 * device attached to this port. 12311 */ 12312 12313 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 12314 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12315 SATA_LOG_D((sata_hba_inst, CE_WARN, 12316 "sata_hba_ioctl: port selftest: " 12317 "failed port %d:%d", cport, pmport)); 12318 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12319 cport_mutex); 12320 sata_update_port_info(sata_hba_inst, sata_device); 12321 if (qual == SATA_ADDR_CPORT) 12322 SATA_CPORT_STATE(sata_hba_inst, cport) = 12323 SATA_PSTATE_FAILED; 12324 else /* port ultiplier device port */ 12325 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 12326 SATA_PSTATE_FAILED; 12327 12328 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12329 cport_mutex); 12330 return (EIO); 12331 } 12332 /* 12333 * Beacuse the port was reset in the course of testing, it should be 12334 * re-probed and attached device state should be restored. At this 12335 * point the port state is unknown - it's state is HBA-specific. 12336 * Force port re-probing to get it into a known state. 12337 */ 12338 if (sata_reprobe_port(sata_hba_inst, sata_device, 12339 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12340 rv = EIO; 12341 return (rv); 12342 } 12343 12344 12345 /* 12346 * sata_cfgadm_state: 12347 * Use the sata port state and state of the target node to figure out 12348 * the cfgadm_state. 12349 * 12350 * The port argument is a value with encoded cport, 12351 * pmport and address qualifier, in the same manner as a scsi target number. 12352 * SCSI_TO_SATA_CPORT macro extracts cport number, 12353 * SCSI_TO_SATA_PMPORT extracts pmport number and 12354 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 12355 * 12356 * For now, support is for cports only - no port multiplier device ports. 12357 */ 12358 12359 static void 12360 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 12361 devctl_ap_state_t *ap_state) 12362 { 12363 uint16_t cport; 12364 int port_state; 12365 sata_drive_info_t *sdinfo; 12366 12367 /* Cport only */ 12368 cport = SCSI_TO_SATA_CPORT(port); 12369 12370 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 12371 if (port_state & SATA_PSTATE_SHUTDOWN || 12372 port_state & SATA_PSTATE_FAILED) { 12373 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 12374 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12375 if (port_state & SATA_PSTATE_FAILED) 12376 ap_state->ap_condition = AP_COND_FAILED; 12377 else 12378 ap_state->ap_condition = AP_COND_UNKNOWN; 12379 12380 return; 12381 } 12382 12383 /* Need to check pmult device port here as well, when supported */ 12384 12385 /* Port is enabled and ready */ 12386 12387 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 12388 case SATA_DTYPE_NONE: 12389 { 12390 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12391 ap_state->ap_condition = AP_COND_OK; 12392 /* No device attached */ 12393 ap_state->ap_rstate = AP_RSTATE_EMPTY; 12394 break; 12395 } 12396 case SATA_DTYPE_UNKNOWN: 12397 case SATA_DTYPE_PMULT: /* Until PMult is supported */ 12398 case SATA_DTYPE_ATADISK: 12399 case SATA_DTYPE_ATAPICD: 12400 case SATA_DTYPE_ATAPITAPE: 12401 case SATA_DTYPE_ATAPIDISK: 12402 { 12403 dev_info_t *tdip = NULL; 12404 dev_info_t *dip = NULL; 12405 int circ; 12406 12407 dip = SATA_DIP(sata_hba_inst); 12408 tdip = sata_get_target_dip(dip, port); 12409 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12410 if (tdip != NULL) { 12411 ndi_devi_enter(dip, &circ); 12412 mutex_enter(&(DEVI(tdip)->devi_lock)); 12413 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 12414 /* 12415 * There could be the case where previously 12416 * configured and opened device was removed 12417 * and unknown device was plugged. 12418 * In such case we want to show a device, and 12419 * its configured or unconfigured state but 12420 * indicate unusable condition untill the 12421 * old target node is released and removed. 12422 */ 12423 ap_state->ap_condition = AP_COND_UNUSABLE; 12424 } else { 12425 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 12426 cport)); 12427 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12428 cport); 12429 if (sdinfo != NULL) { 12430 if ((sdinfo->satadrv_state & 12431 SATA_DSTATE_FAILED) != 0) 12432 ap_state->ap_condition = 12433 AP_COND_FAILED; 12434 else 12435 ap_state->ap_condition = 12436 AP_COND_OK; 12437 } else { 12438 ap_state->ap_condition = 12439 AP_COND_UNKNOWN; 12440 } 12441 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 12442 cport)); 12443 } 12444 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 12445 (DEVI_IS_DEVICE_DOWN(tdip))) { 12446 ap_state->ap_ostate = 12447 AP_OSTATE_UNCONFIGURED; 12448 } else { 12449 ap_state->ap_ostate = 12450 AP_OSTATE_CONFIGURED; 12451 } 12452 mutex_exit(&(DEVI(tdip)->devi_lock)); 12453 ndi_devi_exit(dip, circ); 12454 } else { 12455 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12456 ap_state->ap_condition = AP_COND_UNKNOWN; 12457 } 12458 break; 12459 } 12460 default: 12461 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12462 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12463 ap_state->ap_condition = AP_COND_UNKNOWN; 12464 /* 12465 * This is actually internal error condition (non fatal), 12466 * because we have already checked all defined device types. 12467 */ 12468 SATA_LOG_D((sata_hba_inst, CE_WARN, 12469 "sata_cfgadm_state: Internal error: " 12470 "unknown device type")); 12471 break; 12472 } 12473 } 12474 12475 12476 /* 12477 * Process ioctl get device path request. 12478 * 12479 * NOTE: Port multiplier code is not completed nor tested. 12480 */ 12481 static int 12482 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 12483 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12484 { 12485 char path[MAXPATHLEN]; 12486 uint32_t size; 12487 dev_info_t *tdip; 12488 12489 (void) strcpy(path, "/devices"); 12490 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12491 &sata_device->satadev_addr)) == NULL) { 12492 /* 12493 * No such device. If this is a request for a size, do not 12494 * return EINVAL for non-existing target, because cfgadm 12495 * will then indicate a meaningless ioctl failure. 12496 * If this is a request for a path, indicate invalid 12497 * argument. 12498 */ 12499 if (ioc->get_size == 0) 12500 return (EINVAL); 12501 } else { 12502 (void) ddi_pathname(tdip, path + strlen(path)); 12503 } 12504 size = strlen(path) + 1; 12505 12506 if (ioc->get_size != 0) { 12507 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 12508 mode) != 0) 12509 return (EFAULT); 12510 } else { 12511 if (ioc->bufsiz != size) 12512 return (EINVAL); 12513 12514 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 12515 mode) != 0) 12516 return (EFAULT); 12517 } 12518 return (0); 12519 } 12520 12521 /* 12522 * Process ioctl get attachment point type request. 12523 * 12524 * NOTE: Port multiplier code is not completed nor tested. 12525 */ 12526 static int 12527 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 12528 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12529 { 12530 uint32_t type_len; 12531 const char *ap_type; 12532 int dev_type; 12533 12534 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12535 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 12536 sata_device->satadev_addr.cport); 12537 else /* pmport */ 12538 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12539 sata_device->satadev_addr.cport, 12540 sata_device->satadev_addr.pmport); 12541 12542 switch (dev_type) { 12543 case SATA_DTYPE_NONE: 12544 ap_type = "port"; 12545 break; 12546 12547 case SATA_DTYPE_ATADISK: 12548 case SATA_DTYPE_ATAPIDISK: 12549 ap_type = "disk"; 12550 break; 12551 12552 case SATA_DTYPE_ATAPICD: 12553 ap_type = "cd/dvd"; 12554 break; 12555 12556 case SATA_DTYPE_ATAPITAPE: 12557 ap_type = "tape"; 12558 break; 12559 12560 case SATA_DTYPE_PMULT: 12561 ap_type = "pmult"; 12562 break; 12563 12564 case SATA_DTYPE_UNKNOWN: 12565 ap_type = "unknown"; 12566 break; 12567 12568 default: 12569 ap_type = "unsupported"; 12570 break; 12571 12572 } /* end of dev_type switch */ 12573 12574 type_len = strlen(ap_type) + 1; 12575 12576 if (ioc->get_size) { 12577 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 12578 mode) != 0) 12579 return (EFAULT); 12580 } else { 12581 if (ioc->bufsiz != type_len) 12582 return (EINVAL); 12583 12584 if (ddi_copyout((void *)ap_type, ioc->buf, 12585 ioc->bufsiz, mode) != 0) 12586 return (EFAULT); 12587 } 12588 return (0); 12589 12590 } 12591 12592 /* 12593 * Process ioctl get device model info request. 12594 * This operation should return to cfgadm the device model 12595 * information string 12596 * 12597 * NOTE: Port multiplier code is not completed nor tested. 12598 */ 12599 static int 12600 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 12601 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12602 { 12603 sata_drive_info_t *sdinfo; 12604 uint32_t info_len; 12605 char ap_info[SATA_ID_MODEL_LEN + 1]; 12606 12607 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12608 sata_device->satadev_addr.cport)->cport_mutex); 12609 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12610 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12611 sata_device->satadev_addr.cport); 12612 else /* port multiplier */ 12613 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12614 sata_device->satadev_addr.cport, 12615 sata_device->satadev_addr.pmport); 12616 if (sdinfo == NULL) { 12617 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12618 sata_device->satadev_addr.cport)->cport_mutex); 12619 return (EINVAL); 12620 } 12621 12622 #ifdef _LITTLE_ENDIAN 12623 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12624 #else /* _LITTLE_ENDIAN */ 12625 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12626 #endif /* _LITTLE_ENDIAN */ 12627 12628 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12629 sata_device->satadev_addr.cport)->cport_mutex); 12630 12631 ap_info[SATA_ID_MODEL_LEN] = '\0'; 12632 12633 info_len = strlen(ap_info) + 1; 12634 12635 if (ioc->get_size) { 12636 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12637 mode) != 0) 12638 return (EFAULT); 12639 } else { 12640 if (ioc->bufsiz < info_len) 12641 return (EINVAL); 12642 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12643 mode) != 0) 12644 return (EFAULT); 12645 } 12646 return (0); 12647 } 12648 12649 12650 /* 12651 * Process ioctl get device firmware revision info request. 12652 * This operation should return to cfgadm the device firmware revision 12653 * information string 12654 * 12655 * NOTE: Port multiplier code is not completed nor tested. 12656 */ 12657 static int 12658 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 12659 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12660 { 12661 sata_drive_info_t *sdinfo; 12662 uint32_t info_len; 12663 char ap_info[SATA_ID_FW_LEN + 1]; 12664 12665 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12666 sata_device->satadev_addr.cport)->cport_mutex); 12667 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12668 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12669 sata_device->satadev_addr.cport); 12670 else /* port multiplier */ 12671 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12672 sata_device->satadev_addr.cport, 12673 sata_device->satadev_addr.pmport); 12674 if (sdinfo == NULL) { 12675 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12676 sata_device->satadev_addr.cport)->cport_mutex); 12677 return (EINVAL); 12678 } 12679 12680 #ifdef _LITTLE_ENDIAN 12681 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12682 #else /* _LITTLE_ENDIAN */ 12683 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12684 #endif /* _LITTLE_ENDIAN */ 12685 12686 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12687 sata_device->satadev_addr.cport)->cport_mutex); 12688 12689 ap_info[SATA_ID_FW_LEN] = '\0'; 12690 12691 info_len = strlen(ap_info) + 1; 12692 12693 if (ioc->get_size) { 12694 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12695 mode) != 0) 12696 return (EFAULT); 12697 } else { 12698 if (ioc->bufsiz < info_len) 12699 return (EINVAL); 12700 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12701 mode) != 0) 12702 return (EFAULT); 12703 } 12704 return (0); 12705 } 12706 12707 12708 /* 12709 * Process ioctl get device serial number info request. 12710 * This operation should return to cfgadm the device serial number string. 12711 * 12712 * NOTE: Port multiplier code is not completed nor tested. 12713 */ 12714 static int 12715 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 12716 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12717 { 12718 sata_drive_info_t *sdinfo; 12719 uint32_t info_len; 12720 char ap_info[SATA_ID_SERIAL_LEN + 1]; 12721 12722 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12723 sata_device->satadev_addr.cport)->cport_mutex); 12724 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12725 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12726 sata_device->satadev_addr.cport); 12727 else /* port multiplier */ 12728 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12729 sata_device->satadev_addr.cport, 12730 sata_device->satadev_addr.pmport); 12731 if (sdinfo == NULL) { 12732 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12733 sata_device->satadev_addr.cport)->cport_mutex); 12734 return (EINVAL); 12735 } 12736 12737 #ifdef _LITTLE_ENDIAN 12738 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12739 #else /* _LITTLE_ENDIAN */ 12740 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12741 #endif /* _LITTLE_ENDIAN */ 12742 12743 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12744 sata_device->satadev_addr.cport)->cport_mutex); 12745 12746 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 12747 12748 info_len = strlen(ap_info) + 1; 12749 12750 if (ioc->get_size) { 12751 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12752 mode) != 0) 12753 return (EFAULT); 12754 } else { 12755 if (ioc->bufsiz < info_len) 12756 return (EINVAL); 12757 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12758 mode) != 0) 12759 return (EFAULT); 12760 } 12761 return (0); 12762 } 12763 12764 12765 /* 12766 * Preset scsi extended sense data (to NO SENSE) 12767 * First 18 bytes of the sense data are preset to current valid sense 12768 * with a key NO SENSE data. 12769 * 12770 * Returns void 12771 */ 12772 static void 12773 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 12774 { 12775 sense->es_valid = 1; /* Valid sense */ 12776 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 12777 sense->es_key = KEY_NO_SENSE; 12778 sense->es_info_1 = 0; 12779 sense->es_info_2 = 0; 12780 sense->es_info_3 = 0; 12781 sense->es_info_4 = 0; 12782 sense->es_add_len = 10; /* Additional length - replace with a def */ 12783 sense->es_cmd_info[0] = 0; 12784 sense->es_cmd_info[1] = 0; 12785 sense->es_cmd_info[2] = 0; 12786 sense->es_cmd_info[3] = 0; 12787 sense->es_add_code = 0; 12788 sense->es_qual_code = 0; 12789 } 12790 12791 /* 12792 * Register a legacy cmdk-style devid for the target (disk) device. 12793 * 12794 * Note: This function is called only when the HBA devinfo node has the 12795 * property "use-cmdk-devid-format" set. This property indicates that 12796 * devid compatible with old cmdk (target) driver is to be generated 12797 * for any target device attached to this controller. This will take 12798 * precedence over the devid generated by sd (target) driver. 12799 * This function is derived from cmdk_devid_setup() function in cmdk.c. 12800 */ 12801 static void 12802 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 12803 { 12804 char *hwid; 12805 int modlen; 12806 int serlen; 12807 int rval; 12808 ddi_devid_t devid; 12809 12810 /* 12811 * device ID is a concatanation of model number, "=", serial number. 12812 */ 12813 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 12814 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 12815 sizeof (sdinfo->satadrv_id.ai_model)); 12816 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12817 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12818 if (modlen == 0) 12819 goto err; 12820 hwid[modlen++] = '='; 12821 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 12822 sizeof (sdinfo->satadrv_id.ai_drvser)); 12823 swab(&hwid[modlen], &hwid[modlen], 12824 sizeof (sdinfo->satadrv_id.ai_drvser)); 12825 serlen = sata_check_modser(&hwid[modlen], 12826 sizeof (sdinfo->satadrv_id.ai_drvser)); 12827 if (serlen == 0) 12828 goto err; 12829 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 12830 12831 /* initialize/register devid */ 12832 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 12833 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) 12834 rval = ddi_devid_register(dip, devid); 12835 12836 if (rval != DDI_SUCCESS) 12837 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 12838 " on port %d", sdinfo->satadrv_addr.cport); 12839 err: 12840 kmem_free(hwid, LEGACY_HWID_LEN); 12841 } 12842 12843 /* 12844 * valid model/serial string must contain a non-zero non-space characters. 12845 * trim trailing spaces/NULLs. 12846 */ 12847 static int 12848 sata_check_modser(char *buf, int buf_len) 12849 { 12850 boolean_t ret; 12851 char *s; 12852 int i; 12853 int tb; 12854 char ch; 12855 12856 ret = B_FALSE; 12857 s = buf; 12858 for (i = 0; i < buf_len; i++) { 12859 ch = *s++; 12860 if (ch != ' ' && ch != '\0') 12861 tb = i + 1; 12862 if (ch != ' ' && ch != '\0' && ch != '0') 12863 ret = B_TRUE; 12864 } 12865 12866 if (ret == B_FALSE) 12867 return (0); /* invalid string */ 12868 12869 return (tb); /* return length */ 12870 } 12871 12872 /* 12873 * sata_set_drive_features function compares current device features setting 12874 * with the saved device features settings and, if there is a difference, 12875 * it restores device features setting to the previously saved state. 12876 * It also arbitrarily tries to select the highest supported DMA mode. 12877 * Device Identify or Identify Packet Device data has to be current. 12878 * At the moment read ahead and write cache are considered for all devices. 12879 * For atapi devices, Removable Media Status Notification is set in addition 12880 * to common features. 12881 * 12882 * This function cannot be called in the interrupt context (it may sleep). 12883 * 12884 * The input argument sdinfo should point to the drive info structure 12885 * to be updated after features are set. Note, that only 12886 * device (packet) identify data is updated, not the flags indicating the 12887 * supported features. 12888 * 12889 * Returns SATA_SUCCESS if successful or there was nothing to do. 12890 * Device Identify data in the drive info structure pointed to by the sdinfo 12891 * arguments is updated even when no features were set or changed. 12892 * 12893 * Returns SATA_FAILURE if device features could not be set or DMA mode 12894 * for a disk cannot be set and device identify data cannot be fetched. 12895 * 12896 * Returns SATA_RETRY if device features could not be set (other than disk 12897 * DMA mode) but the device identify data was fetched successfully. 12898 * 12899 * Note: This function may fail the port, making it inaccessible. 12900 * In such case the explicit port disconnect/connect or physical device 12901 * detach/attach is required to re-evaluate port state again. 12902 */ 12903 12904 static int 12905 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 12906 sata_drive_info_t *sdinfo, int restore) 12907 { 12908 int rval = SATA_SUCCESS; 12909 int rval_set; 12910 sata_drive_info_t new_sdinfo; 12911 char *finfo = "sata_set_drive_features: cannot"; 12912 char *finfox; 12913 int cache_op; 12914 12915 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12916 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 12917 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 12918 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12919 /* 12920 * Cannot get device identification - caller may retry later 12921 */ 12922 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12923 "%s fetch device identify data\n", finfo); 12924 return (SATA_FAILURE); 12925 } 12926 finfox = (restore != 0) ? " restore device features" : 12927 " initialize device features\n"; 12928 12929 switch (sdinfo->satadrv_type) { 12930 case SATA_DTYPE_ATADISK: 12931 /* Arbitrarily set UDMA mode */ 12932 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12933 SATA_SUCCESS) { 12934 SATA_LOG_D((sata_hba_inst, CE_WARN, 12935 "%s set UDMA mode\n", finfo)); 12936 return (SATA_FAILURE); 12937 } 12938 break; 12939 case SATA_DTYPE_ATAPICD: 12940 case SATA_DTYPE_ATAPITAPE: 12941 case SATA_DTYPE_ATAPIDISK: 12942 /* Set Removable Media Status Notification, if necessary */ 12943 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 12944 restore != 0) { 12945 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 12946 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 12947 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 12948 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 12949 /* Current setting does not match saved one */ 12950 if (sata_set_rmsn(sata_hba_inst, sdinfo, 12951 sdinfo->satadrv_settings & 12952 SATA_DEV_RMSN) != SATA_SUCCESS) 12953 rval = SATA_FAILURE; 12954 } 12955 } 12956 /* 12957 * We have to set Multiword DMA or UDMA, if it is supported, as 12958 * we want to use DMA transfer mode whenever possible. 12959 * Some devices require explicit setting of the DMA mode. 12960 */ 12961 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 12962 /* Set highest supported DMA mode */ 12963 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12964 SATA_SUCCESS) { 12965 SATA_LOG_D((sata_hba_inst, CE_WARN, 12966 "%s set UDMA mode\n", finfo)); 12967 rval = SATA_FAILURE; 12968 } 12969 } 12970 break; 12971 } 12972 12973 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 12974 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 12975 /* 12976 * neither READ AHEAD nor WRITE CACHE is supported 12977 * - do nothing 12978 */ 12979 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12980 "settable features not supported\n", NULL); 12981 goto update_sdinfo; 12982 } 12983 12984 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 12985 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 12986 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 12987 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12988 /* 12989 * both READ AHEAD and WRITE CACHE are enabled 12990 * - Nothing to do 12991 */ 12992 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12993 "no device features to set\n", NULL); 12994 goto update_sdinfo; 12995 } 12996 12997 cache_op = 0; 12998 12999 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 13000 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 13001 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 13002 /* Enable read ahead / read cache */ 13003 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 13004 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 13005 "enabling read cache\n", NULL); 13006 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 13007 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 13008 /* Disable read ahead / read cache */ 13009 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 13010 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 13011 "disabling read cache\n", NULL); 13012 } 13013 13014 if (cache_op != 0) { 13015 /* Try to set read cache mode */ 13016 rval_set = sata_set_cache_mode(sata_hba_inst, 13017 &new_sdinfo, cache_op); 13018 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 13019 rval = rval_set; 13020 } 13021 } 13022 13023 cache_op = 0; 13024 13025 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 13026 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 13027 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 13028 /* Enable write cache */ 13029 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 13030 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 13031 "enabling write cache\n", NULL); 13032 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 13033 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 13034 /* Disable write cache */ 13035 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 13036 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 13037 "disabling write cache\n", NULL); 13038 } 13039 13040 if (cache_op != 0) { 13041 /* Try to set write cache mode */ 13042 rval_set = sata_set_cache_mode(sata_hba_inst, 13043 &new_sdinfo, cache_op); 13044 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 13045 rval = rval_set; 13046 } 13047 } 13048 if (rval != SATA_SUCCESS) 13049 SATA_LOG_D((sata_hba_inst, CE_WARN, 13050 "%s %s", finfo, finfox)); 13051 13052 update_sdinfo: 13053 /* 13054 * We need to fetch Device Identify data again 13055 */ 13056 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 13057 /* 13058 * Cannot get device identification - retry later 13059 */ 13060 SATA_LOG_D((sata_hba_inst, CE_WARN, 13061 "%s re-fetch device identify data\n", finfo)); 13062 rval = SATA_FAILURE; 13063 } 13064 /* Copy device sata info. */ 13065 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 13066 13067 return (rval); 13068 } 13069 13070 13071 /* 13072 * 13073 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 13074 * unable to determine. 13075 * 13076 * Cannot be called in an interrupt context. 13077 * 13078 * Called by sata_build_lsense_page_2f() 13079 */ 13080 13081 static int 13082 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 13083 sata_drive_info_t *sdinfo) 13084 { 13085 sata_pkt_t *spkt; 13086 sata_cmd_t *scmd; 13087 sata_pkt_txlate_t *spx; 13088 int rval; 13089 13090 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13091 spx->txlt_sata_hba_inst = sata_hba_inst; 13092 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13093 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13094 if (spkt == NULL) { 13095 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13096 return (-1); 13097 } 13098 /* address is needed now */ 13099 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13100 13101 13102 /* Fill sata_pkt */ 13103 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13104 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13105 /* Synchronous mode, no callback */ 13106 spkt->satapkt_comp = NULL; 13107 /* Timeout 30s */ 13108 spkt->satapkt_time = sata_default_pkt_time; 13109 13110 scmd = &spkt->satapkt_cmd; 13111 scmd->satacmd_flags.sata_special_regs = B_TRUE; 13112 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 13113 13114 /* Set up which registers need to be returned */ 13115 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 13116 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 13117 13118 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 13119 scmd->satacmd_addr_type = 0; /* N/A */ 13120 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13121 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13122 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13123 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13124 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 13125 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13126 scmd->satacmd_cmd_reg = SATAC_SMART; 13127 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13128 sdinfo->satadrv_addr.cport))); 13129 13130 13131 /* Send pkt to SATA HBA driver */ 13132 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13133 SATA_TRAN_ACCEPTED || 13134 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13135 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13136 sdinfo->satadrv_addr.cport))); 13137 /* 13138 * Whoops, no SMART RETURN STATUS 13139 */ 13140 rval = -1; 13141 } else { 13142 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13143 sdinfo->satadrv_addr.cport))); 13144 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 13145 rval = -1; 13146 goto fail; 13147 } 13148 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 13149 rval = -1; 13150 goto fail; 13151 } 13152 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 13153 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 13154 rval = 0; 13155 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 13156 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 13157 rval = 1; 13158 else { 13159 rval = -1; 13160 goto fail; 13161 } 13162 } 13163 fail: 13164 /* Free allocated resources */ 13165 sata_pkt_free(spx); 13166 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13167 13168 return (rval); 13169 } 13170 13171 /* 13172 * 13173 * Returns 0 if succeeded, -1 otherwise 13174 * 13175 * Cannot be called in an interrupt context. 13176 * 13177 */ 13178 static int 13179 sata_fetch_smart_data( 13180 sata_hba_inst_t *sata_hba_inst, 13181 sata_drive_info_t *sdinfo, 13182 struct smart_data *smart_data) 13183 { 13184 sata_pkt_t *spkt; 13185 sata_cmd_t *scmd; 13186 sata_pkt_txlate_t *spx; 13187 int rval; 13188 13189 #if ! defined(lint) 13190 ASSERT(sizeof (struct smart_data) == 512); 13191 #endif 13192 13193 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13194 spx->txlt_sata_hba_inst = sata_hba_inst; 13195 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13196 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13197 if (spkt == NULL) { 13198 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13199 return (-1); 13200 } 13201 /* address is needed now */ 13202 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13203 13204 13205 /* Fill sata_pkt */ 13206 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13207 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13208 /* Synchronous mode, no callback */ 13209 spkt->satapkt_comp = NULL; 13210 /* Timeout 30s */ 13211 spkt->satapkt_time = sata_default_pkt_time; 13212 13213 scmd = &spkt->satapkt_cmd; 13214 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13215 13216 /* 13217 * Allocate buffer for SMART data 13218 */ 13219 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13220 sizeof (struct smart_data)); 13221 if (scmd->satacmd_bp == NULL) { 13222 sata_pkt_free(spx); 13223 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13224 SATA_LOG_D((sata_hba_inst, CE_WARN, 13225 "sata_fetch_smart_data: " 13226 "cannot allocate buffer")); 13227 return (-1); 13228 } 13229 13230 13231 /* Build SMART_READ_DATA cmd in the sata_pkt */ 13232 scmd->satacmd_addr_type = 0; /* N/A */ 13233 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13234 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13235 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13236 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13237 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 13238 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13239 scmd->satacmd_cmd_reg = SATAC_SMART; 13240 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13241 sdinfo->satadrv_addr.cport))); 13242 13243 /* Send pkt to SATA HBA driver */ 13244 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13245 SATA_TRAN_ACCEPTED || 13246 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13247 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13248 sdinfo->satadrv_addr.cport))); 13249 /* 13250 * Whoops, no SMART DATA available 13251 */ 13252 rval = -1; 13253 goto fail; 13254 } else { 13255 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13256 sdinfo->satadrv_addr.cport))); 13257 if (spx->txlt_buf_dma_handle != NULL) { 13258 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13259 DDI_DMA_SYNC_FORKERNEL); 13260 ASSERT(rval == DDI_SUCCESS); 13261 } 13262 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 13263 sizeof (struct smart_data)); 13264 } 13265 13266 fail: 13267 /* Free allocated resources */ 13268 sata_free_local_buffer(spx); 13269 sata_pkt_free(spx); 13270 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13271 13272 return (rval); 13273 } 13274 13275 /* 13276 * Used by LOG SENSE page 0x10 13277 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 13278 * Note: cannot be called in the interrupt context. 13279 * 13280 * return 0 for success, -1 otherwise 13281 * 13282 */ 13283 static int 13284 sata_ext_smart_selftest_read_log( 13285 sata_hba_inst_t *sata_hba_inst, 13286 sata_drive_info_t *sdinfo, 13287 struct smart_ext_selftest_log *ext_selftest_log, 13288 uint16_t block_num) 13289 { 13290 sata_pkt_txlate_t *spx; 13291 sata_pkt_t *spkt; 13292 sata_cmd_t *scmd; 13293 int rval; 13294 13295 #if ! defined(lint) 13296 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 13297 #endif 13298 13299 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13300 spx->txlt_sata_hba_inst = sata_hba_inst; 13301 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13302 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13303 if (spkt == NULL) { 13304 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13305 return (-1); 13306 } 13307 /* address is needed now */ 13308 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13309 13310 13311 /* Fill sata_pkt */ 13312 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13313 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13314 /* Synchronous mode, no callback */ 13315 spkt->satapkt_comp = NULL; 13316 /* Timeout 30s */ 13317 spkt->satapkt_time = sata_default_pkt_time; 13318 13319 scmd = &spkt->satapkt_cmd; 13320 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13321 13322 /* 13323 * Allocate buffer for SMART extended self-test log 13324 */ 13325 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13326 sizeof (struct smart_ext_selftest_log)); 13327 if (scmd->satacmd_bp == NULL) { 13328 sata_pkt_free(spx); 13329 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13330 SATA_LOG_D((sata_hba_inst, CE_WARN, 13331 "sata_ext_smart_selftest_log: " 13332 "cannot allocate buffer")); 13333 return (-1); 13334 } 13335 13336 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 13337 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13338 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 13339 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 13340 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 13341 scmd->satacmd_lba_low_msb = 0; 13342 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 13343 scmd->satacmd_lba_mid_msb = block_num >> 8; 13344 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13345 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13346 13347 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13348 sdinfo->satadrv_addr.cport))); 13349 13350 /* Send pkt to SATA HBA driver */ 13351 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13352 SATA_TRAN_ACCEPTED || 13353 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13354 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13355 sdinfo->satadrv_addr.cport))); 13356 13357 /* 13358 * Whoops, no SMART selftest log info available 13359 */ 13360 rval = -1; 13361 goto fail; 13362 } else { 13363 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13364 sdinfo->satadrv_addr.cport))); 13365 13366 if (spx->txlt_buf_dma_handle != NULL) { 13367 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13368 DDI_DMA_SYNC_FORKERNEL); 13369 ASSERT(rval == DDI_SUCCESS); 13370 } 13371 bcopy(scmd->satacmd_bp->b_un.b_addr, 13372 (uint8_t *)ext_selftest_log, 13373 sizeof (struct smart_ext_selftest_log)); 13374 rval = 0; 13375 } 13376 13377 fail: 13378 /* Free allocated resources */ 13379 sata_free_local_buffer(spx); 13380 sata_pkt_free(spx); 13381 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13382 13383 return (rval); 13384 } 13385 13386 /* 13387 * Returns 0 for success, -1 otherwise 13388 * 13389 * SMART self-test log data is returned in buffer pointed to by selftest_log 13390 */ 13391 static int 13392 sata_smart_selftest_log( 13393 sata_hba_inst_t *sata_hba_inst, 13394 sata_drive_info_t *sdinfo, 13395 struct smart_selftest_log *selftest_log) 13396 { 13397 sata_pkt_t *spkt; 13398 sata_cmd_t *scmd; 13399 sata_pkt_txlate_t *spx; 13400 int rval; 13401 13402 #if ! defined(lint) 13403 ASSERT(sizeof (struct smart_selftest_log) == 512); 13404 #endif 13405 13406 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13407 spx->txlt_sata_hba_inst = sata_hba_inst; 13408 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13409 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13410 if (spkt == NULL) { 13411 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13412 return (-1); 13413 } 13414 /* address is needed now */ 13415 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13416 13417 13418 /* Fill sata_pkt */ 13419 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13420 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13421 /* Synchronous mode, no callback */ 13422 spkt->satapkt_comp = NULL; 13423 /* Timeout 30s */ 13424 spkt->satapkt_time = sata_default_pkt_time; 13425 13426 scmd = &spkt->satapkt_cmd; 13427 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13428 13429 /* 13430 * Allocate buffer for SMART SELFTEST LOG 13431 */ 13432 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13433 sizeof (struct smart_selftest_log)); 13434 if (scmd->satacmd_bp == NULL) { 13435 sata_pkt_free(spx); 13436 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13437 SATA_LOG_D((sata_hba_inst, CE_WARN, 13438 "sata_smart_selftest_log: " 13439 "cannot allocate buffer")); 13440 return (-1); 13441 } 13442 13443 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13444 scmd->satacmd_addr_type = 0; /* N/A */ 13445 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 13446 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 13447 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13448 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13449 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13450 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13451 scmd->satacmd_cmd_reg = SATAC_SMART; 13452 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13453 sdinfo->satadrv_addr.cport))); 13454 13455 /* Send pkt to SATA HBA driver */ 13456 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13457 SATA_TRAN_ACCEPTED || 13458 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13459 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13460 sdinfo->satadrv_addr.cport))); 13461 /* 13462 * Whoops, no SMART DATA available 13463 */ 13464 rval = -1; 13465 goto fail; 13466 } else { 13467 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13468 sdinfo->satadrv_addr.cport))); 13469 if (spx->txlt_buf_dma_handle != NULL) { 13470 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13471 DDI_DMA_SYNC_FORKERNEL); 13472 ASSERT(rval == DDI_SUCCESS); 13473 } 13474 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 13475 sizeof (struct smart_selftest_log)); 13476 rval = 0; 13477 } 13478 13479 fail: 13480 /* Free allocated resources */ 13481 sata_free_local_buffer(spx); 13482 sata_pkt_free(spx); 13483 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13484 13485 return (rval); 13486 } 13487 13488 13489 /* 13490 * Returns 0 for success, -1 otherwise 13491 * 13492 * SMART READ LOG data is returned in buffer pointed to by smart_log 13493 */ 13494 static int 13495 sata_smart_read_log( 13496 sata_hba_inst_t *sata_hba_inst, 13497 sata_drive_info_t *sdinfo, 13498 uint8_t *smart_log, /* where the data should be returned */ 13499 uint8_t which_log, /* which log should be returned */ 13500 uint8_t log_size) /* # of 512 bytes in log */ 13501 { 13502 sata_pkt_t *spkt; 13503 sata_cmd_t *scmd; 13504 sata_pkt_txlate_t *spx; 13505 int rval; 13506 13507 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13508 spx->txlt_sata_hba_inst = sata_hba_inst; 13509 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13510 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13511 if (spkt == NULL) { 13512 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13513 return (-1); 13514 } 13515 /* address is needed now */ 13516 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13517 13518 13519 /* Fill sata_pkt */ 13520 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13521 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13522 /* Synchronous mode, no callback */ 13523 spkt->satapkt_comp = NULL; 13524 /* Timeout 30s */ 13525 spkt->satapkt_time = sata_default_pkt_time; 13526 13527 scmd = &spkt->satapkt_cmd; 13528 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13529 13530 /* 13531 * Allocate buffer for SMART READ LOG 13532 */ 13533 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 13534 if (scmd->satacmd_bp == NULL) { 13535 sata_pkt_free(spx); 13536 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13537 SATA_LOG_D((sata_hba_inst, CE_WARN, 13538 "sata_smart_read_log: " "cannot allocate buffer")); 13539 return (-1); 13540 } 13541 13542 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13543 scmd->satacmd_addr_type = 0; /* N/A */ 13544 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 13545 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 13546 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13547 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13548 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13549 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13550 scmd->satacmd_cmd_reg = SATAC_SMART; 13551 13552 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13553 sdinfo->satadrv_addr.cport))); 13554 13555 /* Send pkt to SATA HBA driver */ 13556 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13557 SATA_TRAN_ACCEPTED || 13558 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13559 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13560 sdinfo->satadrv_addr.cport))); 13561 13562 /* 13563 * Whoops, no SMART DATA available 13564 */ 13565 rval = -1; 13566 goto fail; 13567 } else { 13568 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13569 sdinfo->satadrv_addr.cport))); 13570 13571 if (spx->txlt_buf_dma_handle != NULL) { 13572 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13573 DDI_DMA_SYNC_FORKERNEL); 13574 ASSERT(rval == DDI_SUCCESS); 13575 } 13576 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 13577 rval = 0; 13578 } 13579 13580 fail: 13581 /* Free allocated resources */ 13582 sata_free_local_buffer(spx); 13583 sata_pkt_free(spx); 13584 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13585 13586 return (rval); 13587 } 13588 13589 /* 13590 * Used by LOG SENSE page 0x10 13591 * 13592 * return 0 for success, -1 otherwise 13593 * 13594 */ 13595 static int 13596 sata_read_log_ext_directory( 13597 sata_hba_inst_t *sata_hba_inst, 13598 sata_drive_info_t *sdinfo, 13599 struct read_log_ext_directory *logdir) 13600 { 13601 sata_pkt_txlate_t *spx; 13602 sata_pkt_t *spkt; 13603 sata_cmd_t *scmd; 13604 int rval; 13605 13606 #if ! defined(lint) 13607 ASSERT(sizeof (struct read_log_ext_directory) == 512); 13608 #endif 13609 13610 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13611 spx->txlt_sata_hba_inst = sata_hba_inst; 13612 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13613 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13614 if (spkt == NULL) { 13615 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13616 return (-1); 13617 } 13618 13619 /* Fill sata_pkt */ 13620 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13621 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13622 /* Synchronous mode, no callback */ 13623 spkt->satapkt_comp = NULL; 13624 /* Timeout 30s */ 13625 spkt->satapkt_time = sata_default_pkt_time; 13626 13627 scmd = &spkt->satapkt_cmd; 13628 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13629 13630 /* 13631 * Allocate buffer for SMART READ LOG EXTENDED command 13632 */ 13633 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13634 sizeof (struct read_log_ext_directory)); 13635 if (scmd->satacmd_bp == NULL) { 13636 sata_pkt_free(spx); 13637 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13638 SATA_LOG_D((sata_hba_inst, CE_WARN, 13639 "sata_read_log_ext_directory: " 13640 "cannot allocate buffer")); 13641 return (-1); 13642 } 13643 13644 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 13645 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13646 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 13647 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 13648 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 13649 scmd->satacmd_lba_low_msb = 0; 13650 scmd->satacmd_lba_mid_lsb = 0; 13651 scmd->satacmd_lba_mid_msb = 0; 13652 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13653 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13654 13655 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13656 sdinfo->satadrv_addr.cport))); 13657 13658 /* Send pkt to SATA HBA driver */ 13659 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13660 SATA_TRAN_ACCEPTED || 13661 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13662 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13663 sdinfo->satadrv_addr.cport))); 13664 /* 13665 * Whoops, no SMART selftest log info available 13666 */ 13667 rval = -1; 13668 goto fail; 13669 } else { 13670 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13671 sdinfo->satadrv_addr.cport))); 13672 if (spx->txlt_buf_dma_handle != NULL) { 13673 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13674 DDI_DMA_SYNC_FORKERNEL); 13675 ASSERT(rval == DDI_SUCCESS); 13676 } 13677 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 13678 sizeof (struct read_log_ext_directory)); 13679 rval = 0; 13680 } 13681 13682 fail: 13683 /* Free allocated resources */ 13684 sata_free_local_buffer(spx); 13685 sata_pkt_free(spx); 13686 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13687 13688 return (rval); 13689 } 13690 13691 /* 13692 * Set up error retrieval sata command for NCQ command error data 13693 * recovery. 13694 * 13695 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 13696 * returns SATA_FAILURE otherwise. 13697 */ 13698 static int 13699 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 13700 { 13701 #ifndef __lock_lint 13702 _NOTE(ARGUNUSED(sdinfo)) 13703 #endif 13704 13705 sata_pkt_t *spkt = spx->txlt_sata_pkt; 13706 sata_cmd_t *scmd; 13707 struct buf *bp; 13708 13709 /* Operation modes are up to the caller */ 13710 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13711 13712 /* Synchronous mode, no callback - may be changed by the caller */ 13713 spkt->satapkt_comp = NULL; 13714 spkt->satapkt_time = sata_default_pkt_time; 13715 13716 scmd = &spkt->satapkt_cmd; 13717 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 13718 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13719 13720 /* 13721 * Allocate dma_able buffer error data. 13722 * Buffer allocation will take care of buffer alignment and other DMA 13723 * attributes. 13724 */ 13725 bp = sata_alloc_local_buffer(spx, 13726 sizeof (struct sata_ncq_error_recovery_page)); 13727 if (bp == NULL) 13728 return (SATA_FAILURE); 13729 13730 bp_mapin(bp); /* make data buffer accessible */ 13731 scmd->satacmd_bp = bp; 13732 13733 /* 13734 * Set-up pointer to the buffer handle, so HBA can sync buffer 13735 * before accessing it. Handle is in usual place in translate struct. 13736 */ 13737 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 13738 13739 ASSERT(scmd->satacmd_num_dma_cookies != 0); 13740 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 13741 13742 return (SATA_SUCCESS); 13743 } 13744 13745 /* 13746 * sata_xlate_errors() is used to translate (S)ATA error 13747 * information to SCSI information returned in the SCSI 13748 * packet. 13749 */ 13750 static void 13751 sata_xlate_errors(sata_pkt_txlate_t *spx) 13752 { 13753 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 13754 struct scsi_extended_sense *sense; 13755 13756 scsipkt->pkt_reason = CMD_INCOMPLETE; 13757 *scsipkt->pkt_scbp = STATUS_CHECK; 13758 sense = sata_arq_sense(spx); 13759 13760 switch (spx->txlt_sata_pkt->satapkt_reason) { 13761 case SATA_PKT_PORT_ERROR: 13762 /* 13763 * We have no device data. Assume no data transfered. 13764 */ 13765 sense->es_key = KEY_HARDWARE_ERROR; 13766 break; 13767 13768 case SATA_PKT_DEV_ERROR: 13769 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 13770 SATA_STATUS_ERR) { 13771 /* 13772 * determine dev error reason from error 13773 * reg content 13774 */ 13775 sata_decode_device_error(spx, sense); 13776 break; 13777 } 13778 /* No extended sense key - no info available */ 13779 break; 13780 13781 case SATA_PKT_TIMEOUT: 13782 scsipkt->pkt_reason = CMD_TIMEOUT; 13783 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 13784 /* No extended sense key */ 13785 break; 13786 13787 case SATA_PKT_ABORTED: 13788 scsipkt->pkt_reason = CMD_ABORTED; 13789 scsipkt->pkt_statistics |= STAT_ABORTED; 13790 /* No extended sense key */ 13791 break; 13792 13793 case SATA_PKT_RESET: 13794 /* 13795 * pkt aborted either by an explicit reset request from 13796 * a host, or due to error recovery 13797 */ 13798 scsipkt->pkt_reason = CMD_RESET; 13799 scsipkt->pkt_statistics |= STAT_DEV_RESET; 13800 break; 13801 13802 default: 13803 scsipkt->pkt_reason = CMD_TRAN_ERR; 13804 break; 13805 } 13806 } 13807 13808 13809 13810 13811 /* 13812 * Log sata message 13813 * dev pathname msg line preceeds the logged message. 13814 */ 13815 13816 static void 13817 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 13818 { 13819 char pathname[128]; 13820 dev_info_t *dip; 13821 va_list ap; 13822 13823 mutex_enter(&sata_log_mutex); 13824 13825 va_start(ap, fmt); 13826 (void) vsprintf(sata_log_buf, fmt, ap); 13827 va_end(ap); 13828 13829 if (sata_hba_inst != NULL) { 13830 dip = SATA_DIP(sata_hba_inst); 13831 (void) ddi_pathname(dip, pathname); 13832 } else { 13833 pathname[0] = 0; 13834 } 13835 if (level == CE_CONT) { 13836 if (sata_debug_flags == 0) 13837 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 13838 else 13839 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 13840 } else { 13841 if (level != CE_NOTE) { 13842 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 13843 } else if (sata_msg) { 13844 cmn_err(level, "%s:\n %s", pathname, 13845 sata_log_buf); 13846 } 13847 } 13848 13849 mutex_exit(&sata_log_mutex); 13850 } 13851 13852 13853 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 13854 13855 /* 13856 * Start or terminate the thread, depending on flag arg and current state 13857 */ 13858 static void 13859 sata_event_thread_control(int startstop) 13860 { 13861 static int sata_event_thread_terminating = 0; 13862 static int sata_event_thread_starting = 0; 13863 int i; 13864 13865 mutex_enter(&sata_event_mutex); 13866 13867 if (startstop == 0 && (sata_event_thread_starting == 1 || 13868 sata_event_thread_terminating == 1)) { 13869 mutex_exit(&sata_event_mutex); 13870 return; 13871 } 13872 if (startstop == 1 && sata_event_thread_starting == 1) { 13873 mutex_exit(&sata_event_mutex); 13874 return; 13875 } 13876 if (startstop == 1 && sata_event_thread_terminating == 1) { 13877 sata_event_thread_starting = 1; 13878 /* wait til terminate operation completes */ 13879 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13880 while (sata_event_thread_terminating == 1) { 13881 if (i-- <= 0) { 13882 sata_event_thread_starting = 0; 13883 mutex_exit(&sata_event_mutex); 13884 #ifdef SATA_DEBUG 13885 cmn_err(CE_WARN, "sata_event_thread_control: " 13886 "timeout waiting for thread to terminate"); 13887 #endif 13888 return; 13889 } 13890 mutex_exit(&sata_event_mutex); 13891 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13892 mutex_enter(&sata_event_mutex); 13893 } 13894 } 13895 if (startstop == 1) { 13896 if (sata_event_thread == NULL) { 13897 sata_event_thread = thread_create(NULL, 0, 13898 (void (*)())sata_event_daemon, 13899 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 13900 } 13901 sata_event_thread_starting = 0; 13902 mutex_exit(&sata_event_mutex); 13903 return; 13904 } 13905 13906 /* 13907 * If we got here, thread may need to be terminated 13908 */ 13909 if (sata_event_thread != NULL) { 13910 int i; 13911 /* Signal event thread to go away */ 13912 sata_event_thread_terminating = 1; 13913 sata_event_thread_terminate = 1; 13914 cv_signal(&sata_event_cv); 13915 /* 13916 * Wait til daemon terminates. 13917 */ 13918 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13919 while (sata_event_thread_terminate == 1) { 13920 mutex_exit(&sata_event_mutex); 13921 if (i-- <= 0) { 13922 /* Daemon did not go away !!! */ 13923 #ifdef SATA_DEBUG 13924 cmn_err(CE_WARN, "sata_event_thread_control: " 13925 "cannot terminate event daemon thread"); 13926 #endif 13927 mutex_enter(&sata_event_mutex); 13928 break; 13929 } 13930 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13931 mutex_enter(&sata_event_mutex); 13932 } 13933 sata_event_thread_terminating = 0; 13934 } 13935 ASSERT(sata_event_thread_terminating == 0); 13936 ASSERT(sata_event_thread_starting == 0); 13937 mutex_exit(&sata_event_mutex); 13938 } 13939 13940 13941 /* 13942 * SATA HBA event notification function. 13943 * Events reported by SATA HBA drivers per HBA instance relate to a change in 13944 * a port and/or device state or a controller itself. 13945 * Events for different addresses/addr types cannot be combined. 13946 * A warning message is generated for each event type. 13947 * Events are not processed by this function, so only the 13948 * event flag(s)is set for an affected entity and the event thread is 13949 * waken up. Event daemon thread processes all events. 13950 * 13951 * NOTE: Since more than one event may be reported at the same time, one 13952 * cannot determine a sequence of events when opposite event are reported, eg. 13953 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 13954 * is taking precedence over reported events, i.e. may cause ignoring some 13955 * events. 13956 */ 13957 #define SATA_EVENT_MAX_MSG_LENGTH 79 13958 13959 void 13960 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 13961 { 13962 sata_hba_inst_t *sata_hba_inst = NULL; 13963 sata_address_t *saddr; 13964 sata_drive_info_t *sdinfo; 13965 sata_port_stats_t *pstats; 13966 sata_cport_info_t *cportinfo; 13967 sata_pmport_info_t *pmportinfo; 13968 int cport, pmport; 13969 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13970 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13971 char *lcp; 13972 static char *err_msg_evnt_1 = 13973 "sata_hba_event_notify: invalid port event 0x%x "; 13974 static char *err_msg_evnt_2 = 13975 "sata_hba_event_notify: invalid device event 0x%x "; 13976 int linkevent; 13977 13978 /* 13979 * There is a possibility that an event will be generated on HBA 13980 * that has not completed attachment or is detaching. We still want 13981 * to process events until HBA is detached. 13982 */ 13983 mutex_enter(&sata_mutex); 13984 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 13985 sata_hba_inst = sata_hba_inst->satahba_next) { 13986 if (SATA_DIP(sata_hba_inst) == dip) 13987 if (sata_hba_inst->satahba_attached == 1) 13988 break; 13989 } 13990 mutex_exit(&sata_mutex); 13991 if (sata_hba_inst == NULL) 13992 /* HBA not attached */ 13993 return; 13994 13995 ASSERT(sata_device != NULL); 13996 13997 /* 13998 * Validate address before - do not proceed with invalid address. 13999 */ 14000 saddr = &sata_device->satadev_addr; 14001 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 14002 return; 14003 if (saddr->qual == SATA_ADDR_PMPORT || 14004 saddr->qual == SATA_ADDR_DPMPORT) 14005 /* Port Multiplier not supported yet */ 14006 return; 14007 14008 cport = saddr->cport; 14009 pmport = saddr->pmport; 14010 14011 buf1[0] = buf2[0] = '\0'; 14012 14013 /* 14014 * If event relates to port or device, check port state. 14015 * Port has to be initialized, or we cannot accept an event. 14016 */ 14017 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 14018 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) { 14019 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) { 14020 mutex_enter(&sata_hba_inst->satahba_mutex); 14021 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14022 mutex_exit(&sata_hba_inst->satahba_mutex); 14023 if (cportinfo == NULL || cportinfo->cport_state == 0) 14024 return; 14025 } else { 14026 mutex_enter(&sata_hba_inst->satahba_mutex); 14027 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 14028 cport, pmport); 14029 mutex_exit(&sata_hba_inst->satahba_mutex); 14030 if (pmportinfo == NULL || pmportinfo->pmport_state == 0) 14031 return; 14032 } 14033 } 14034 14035 /* 14036 * Events refer to devices, ports and controllers - each has 14037 * unique address. Events for different addresses cannot be combined. 14038 */ 14039 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 14040 14041 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14042 14043 /* qualify this event(s) */ 14044 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 14045 /* Invalid event for the device port */ 14046 (void) sprintf(buf2, err_msg_evnt_1, 14047 event & SATA_EVNT_PORT_EVENTS); 14048 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14049 goto event_info; 14050 } 14051 if (saddr->qual == SATA_ADDR_CPORT) { 14052 /* Controller's device port event */ 14053 14054 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 14055 cport_event_flags |= 14056 event & SATA_EVNT_PORT_EVENTS; 14057 pstats = 14058 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 14059 cport_stats; 14060 } else { 14061 /* Port multiplier's device port event */ 14062 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 14063 pmport_event_flags |= 14064 event & SATA_EVNT_PORT_EVENTS; 14065 pstats = 14066 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 14067 pmport_stats; 14068 } 14069 14070 /* 14071 * Add to statistics and log the message. We have to do it 14072 * here rather than in the event daemon, because there may be 14073 * multiple events occuring before they are processed. 14074 */ 14075 linkevent = event & 14076 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 14077 if (linkevent) { 14078 if (linkevent == (SATA_EVNT_LINK_LOST | 14079 SATA_EVNT_LINK_ESTABLISHED)) { 14080 /* This is likely event combination */ 14081 (void) strlcat(buf1, "link lost/established, ", 14082 SATA_EVENT_MAX_MSG_LENGTH); 14083 14084 if (pstats->link_lost < 0xffffffffffffffffULL) 14085 pstats->link_lost++; 14086 if (pstats->link_established < 14087 0xffffffffffffffffULL) 14088 pstats->link_established++; 14089 linkevent = 0; 14090 } else if (linkevent & SATA_EVNT_LINK_LOST) { 14091 (void) strlcat(buf1, "link lost, ", 14092 SATA_EVENT_MAX_MSG_LENGTH); 14093 14094 if (pstats->link_lost < 0xffffffffffffffffULL) 14095 pstats->link_lost++; 14096 } else { 14097 (void) strlcat(buf1, "link established, ", 14098 SATA_EVENT_MAX_MSG_LENGTH); 14099 if (pstats->link_established < 14100 0xffffffffffffffffULL) 14101 pstats->link_established++; 14102 } 14103 } 14104 if (event & SATA_EVNT_DEVICE_ATTACHED) { 14105 (void) strlcat(buf1, "device attached, ", 14106 SATA_EVENT_MAX_MSG_LENGTH); 14107 if (pstats->device_attached < 0xffffffffffffffffULL) 14108 pstats->device_attached++; 14109 } 14110 if (event & SATA_EVNT_DEVICE_DETACHED) { 14111 (void) strlcat(buf1, "device detached, ", 14112 SATA_EVENT_MAX_MSG_LENGTH); 14113 if (pstats->device_detached < 0xffffffffffffffffULL) 14114 pstats->device_detached++; 14115 } 14116 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 14117 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 14118 "port %d power level changed", cport); 14119 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 14120 pstats->port_pwr_changed++; 14121 } 14122 14123 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 14124 /* There should be no other events for this address */ 14125 (void) sprintf(buf2, err_msg_evnt_1, 14126 event & ~SATA_EVNT_PORT_EVENTS); 14127 } 14128 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14129 14130 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 14131 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14132 14133 /* qualify this event */ 14134 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 14135 /* Invalid event for a device */ 14136 (void) sprintf(buf2, err_msg_evnt_2, 14137 event & SATA_EVNT_DEVICE_RESET); 14138 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14139 goto event_info; 14140 } 14141 /* drive event */ 14142 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 14143 if (sdinfo != NULL) { 14144 if (event & SATA_EVNT_DEVICE_RESET) { 14145 (void) strlcat(buf1, "device reset, ", 14146 SATA_EVENT_MAX_MSG_LENGTH); 14147 if (sdinfo->satadrv_stats.drive_reset < 14148 0xffffffffffffffffULL) 14149 sdinfo->satadrv_stats.drive_reset++; 14150 sdinfo->satadrv_event_flags |= 14151 SATA_EVNT_DEVICE_RESET; 14152 } 14153 } 14154 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 14155 /* Invalid event for a device */ 14156 (void) sprintf(buf2, err_msg_evnt_2, 14157 event & ~SATA_EVNT_DRIVE_EVENTS); 14158 } 14159 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14160 } else { 14161 if (saddr->qual != SATA_ADDR_NULL) { 14162 /* Wrong address qualifier */ 14163 SATA_LOG_D((sata_hba_inst, CE_WARN, 14164 "sata_hba_event_notify: invalid address 0x%x", 14165 *(uint32_t *)saddr)); 14166 return; 14167 } 14168 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 14169 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 14170 /* Invalid event for the controller */ 14171 SATA_LOG_D((sata_hba_inst, CE_WARN, 14172 "sata_hba_event_notify: invalid event 0x%x for " 14173 "controller", 14174 event & SATA_EVNT_CONTROLLER_EVENTS)); 14175 return; 14176 } 14177 buf1[0] = '\0'; 14178 /* This may be a frequent and not interesting event */ 14179 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 14180 "controller power level changed\n", NULL); 14181 14182 mutex_enter(&sata_hba_inst->satahba_mutex); 14183 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 14184 0xffffffffffffffffULL) 14185 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 14186 14187 sata_hba_inst->satahba_event_flags |= 14188 SATA_EVNT_PWR_LEVEL_CHANGED; 14189 mutex_exit(&sata_hba_inst->satahba_mutex); 14190 } 14191 /* 14192 * If we got here, there is something to do with this HBA 14193 * instance. 14194 */ 14195 mutex_enter(&sata_hba_inst->satahba_mutex); 14196 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14197 mutex_exit(&sata_hba_inst->satahba_mutex); 14198 mutex_enter(&sata_mutex); 14199 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 14200 mutex_exit(&sata_mutex); 14201 14202 /* Tickle event thread */ 14203 mutex_enter(&sata_event_mutex); 14204 if (sata_event_thread_active == 0) 14205 cv_signal(&sata_event_cv); 14206 mutex_exit(&sata_event_mutex); 14207 14208 event_info: 14209 if (buf1[0] != '\0') { 14210 lcp = strrchr(buf1, ','); 14211 if (lcp != NULL) 14212 *lcp = '\0'; 14213 } 14214 if (saddr->qual == SATA_ADDR_CPORT || 14215 saddr->qual == SATA_ADDR_DCPORT) { 14216 if (buf1[0] != '\0') { 14217 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14218 cport, buf1); 14219 } 14220 if (buf2[0] != '\0') { 14221 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14222 cport, buf2); 14223 } 14224 } else if (saddr->qual == SATA_ADDR_PMPORT || 14225 saddr->qual == SATA_ADDR_DPMPORT) { 14226 if (buf1[0] != '\0') { 14227 sata_log(sata_hba_inst, CE_NOTE, 14228 "port %d pmport %d: %s\n", cport, pmport, buf1); 14229 } 14230 if (buf2[0] != '\0') { 14231 sata_log(sata_hba_inst, CE_NOTE, 14232 "port %d pmport %d: %s\n", cport, pmport, buf2); 14233 } 14234 } 14235 } 14236 14237 14238 /* 14239 * Event processing thread. 14240 * Arg is a pointer to the sata_hba_list pointer. 14241 * It is not really needed, because sata_hba_list is global and static 14242 */ 14243 static void 14244 sata_event_daemon(void *arg) 14245 { 14246 #ifndef __lock_lint 14247 _NOTE(ARGUNUSED(arg)) 14248 #endif 14249 sata_hba_inst_t *sata_hba_inst; 14250 clock_t lbolt; 14251 14252 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14253 "SATA event daemon started\n", NULL); 14254 loop: 14255 /* 14256 * Process events here. Walk through all registered HBAs 14257 */ 14258 mutex_enter(&sata_mutex); 14259 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14260 sata_hba_inst = sata_hba_inst->satahba_next) { 14261 ASSERT(sata_hba_inst != NULL); 14262 mutex_enter(&sata_hba_inst->satahba_mutex); 14263 if (sata_hba_inst->satahba_attached == 0 || 14264 (sata_hba_inst->satahba_event_flags & 14265 SATA_EVNT_SKIP) != 0) { 14266 mutex_exit(&sata_hba_inst->satahba_mutex); 14267 continue; 14268 } 14269 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 14270 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 14271 mutex_exit(&sata_hba_inst->satahba_mutex); 14272 mutex_exit(&sata_mutex); 14273 /* Got the controller with pending event */ 14274 sata_process_controller_events(sata_hba_inst); 14275 /* 14276 * Since global mutex was released, there is a 14277 * possibility that HBA list has changed, so start 14278 * over from the top. Just processed controller 14279 * will be passed-over because of the SKIP flag. 14280 */ 14281 goto loop; 14282 } 14283 mutex_exit(&sata_hba_inst->satahba_mutex); 14284 } 14285 /* Clear SKIP flag in all controllers */ 14286 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14287 sata_hba_inst = sata_hba_inst->satahba_next) { 14288 mutex_enter(&sata_hba_inst->satahba_mutex); 14289 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 14290 mutex_exit(&sata_hba_inst->satahba_mutex); 14291 } 14292 mutex_exit(&sata_mutex); 14293 14294 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14295 "SATA EVENT DAEMON suspending itself", NULL); 14296 14297 #ifdef SATA_DEBUG 14298 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 14299 sata_log(sata_hba_inst, CE_WARN, 14300 "SATA EVENTS PROCESSING DISABLED\n"); 14301 thread_exit(); /* Daemon will not run again */ 14302 } 14303 #endif 14304 mutex_enter(&sata_event_mutex); 14305 sata_event_thread_active = 0; 14306 mutex_exit(&sata_event_mutex); 14307 /* 14308 * Go to sleep/suspend itself and wake up either because new event or 14309 * wait timeout. Exit if there is a termination request (driver 14310 * unload). 14311 */ 14312 do { 14313 lbolt = ddi_get_lbolt(); 14314 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 14315 mutex_enter(&sata_event_mutex); 14316 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 14317 14318 if (sata_event_thread_active != 0) { 14319 mutex_exit(&sata_event_mutex); 14320 continue; 14321 } 14322 14323 /* Check if it is time to go away */ 14324 if (sata_event_thread_terminate == 1) { 14325 /* 14326 * It is up to the thread setting above flag to make 14327 * sure that this thread is not killed prematurely. 14328 */ 14329 sata_event_thread_terminate = 0; 14330 sata_event_thread = NULL; 14331 mutex_exit(&sata_event_mutex); 14332 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14333 "SATA_EVENT_DAEMON_TERMINATING", NULL); 14334 thread_exit(); { _NOTE(NOT_REACHED) } 14335 } 14336 mutex_exit(&sata_event_mutex); 14337 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 14338 14339 mutex_enter(&sata_event_mutex); 14340 sata_event_thread_active = 1; 14341 mutex_exit(&sata_event_mutex); 14342 14343 mutex_enter(&sata_mutex); 14344 sata_event_pending &= ~SATA_EVNT_MAIN; 14345 mutex_exit(&sata_mutex); 14346 14347 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14348 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 14349 14350 goto loop; 14351 } 14352 14353 /* 14354 * Specific HBA instance event processing. 14355 * 14356 * NOTE: At the moment, device event processing is limited to hard disks 14357 * only. 14358 * cports only are supported - no pmports. 14359 */ 14360 static void 14361 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 14362 { 14363 int ncport; 14364 uint32_t event_flags; 14365 sata_address_t *saddr; 14366 sata_cport_info_t *cportinfo; 14367 14368 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 14369 "Processing controller %d event(s)", 14370 ddi_get_instance(SATA_DIP(sata_hba_inst))); 14371 14372 mutex_enter(&sata_hba_inst->satahba_mutex); 14373 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 14374 event_flags = sata_hba_inst->satahba_event_flags; 14375 mutex_exit(&sata_hba_inst->satahba_mutex); 14376 /* 14377 * Process controller power change first 14378 * HERE 14379 */ 14380 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 14381 sata_process_cntrl_pwr_level_change(sata_hba_inst); 14382 14383 /* 14384 * Search through ports/devices to identify affected port/device. 14385 * We may have to process events for more than one port/device. 14386 */ 14387 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 14388 /* 14389 * Not all ports may be processed in attach by the time we 14390 * get an event. Check if port info is initialized. 14391 */ 14392 mutex_enter(&sata_hba_inst->satahba_mutex); 14393 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 14394 mutex_exit(&sata_hba_inst->satahba_mutex); 14395 if (cportinfo == NULL || cportinfo->cport_state == NULL) 14396 continue; 14397 14398 /* We have initialized controller port info */ 14399 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14400 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14401 cport_event_flags; 14402 /* Check if port was locked by IOCTL processing */ 14403 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 14404 /* 14405 * We ignore port events because port is busy 14406 * with AP control processing. Set again 14407 * controller and main event flag, so that 14408 * events may be processed by the next daemon 14409 * run. 14410 */ 14411 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14412 mutex_enter(&sata_hba_inst->satahba_mutex); 14413 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14414 mutex_exit(&sata_hba_inst->satahba_mutex); 14415 mutex_enter(&sata_mutex); 14416 sata_event_pending |= SATA_EVNT_MAIN; 14417 mutex_exit(&sata_mutex); 14418 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 14419 "Event processing postponed until " 14420 "AP control processing completes", 14421 NULL); 14422 /* Check other ports */ 14423 continue; 14424 } else { 14425 /* 14426 * Set BSY flag so that AP control would not 14427 * interfere with events processing for 14428 * this port. 14429 */ 14430 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14431 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 14432 } 14433 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14434 14435 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 14436 14437 if ((event_flags & 14438 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 14439 /* 14440 * Got port event. 14441 * We need some hierarchy of event processing as they 14442 * are affecting each other: 14443 * 1. port failed 14444 * 2. device detached/attached 14445 * 3. link events - link events may trigger device 14446 * detached or device attached events in some 14447 * circumstances. 14448 * 4. port power level changed 14449 */ 14450 if (event_flags & SATA_EVNT_PORT_FAILED) { 14451 sata_process_port_failed_event(sata_hba_inst, 14452 saddr); 14453 } 14454 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 14455 sata_process_device_detached(sata_hba_inst, 14456 saddr); 14457 } 14458 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 14459 sata_process_device_attached(sata_hba_inst, 14460 saddr); 14461 } 14462 if (event_flags & 14463 (SATA_EVNT_LINK_ESTABLISHED | 14464 SATA_EVNT_LINK_LOST)) { 14465 sata_process_port_link_events(sata_hba_inst, 14466 saddr); 14467 } 14468 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 14469 sata_process_port_pwr_change(sata_hba_inst, 14470 saddr); 14471 } 14472 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 14473 sata_process_target_node_cleanup( 14474 sata_hba_inst, saddr); 14475 } 14476 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 14477 sata_process_device_autoonline( 14478 sata_hba_inst, saddr); 14479 } 14480 } 14481 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14482 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 14483 SATA_DTYPE_NONE) && 14484 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 14485 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 14486 satadrv_event_flags & 14487 (SATA_EVNT_DEVICE_RESET | 14488 SATA_EVNT_INPROC_DEVICE_RESET)) { 14489 /* Have device event */ 14490 sata_process_device_reset(sata_hba_inst, 14491 saddr); 14492 } 14493 } 14494 /* Release PORT_BUSY flag */ 14495 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14496 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 14497 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14498 14499 } /* End of loop through the controller SATA ports */ 14500 } 14501 14502 /* 14503 * Process HBA power level change reported by HBA driver. 14504 * Not implemented at this time - event is ignored. 14505 */ 14506 static void 14507 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 14508 { 14509 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14510 "Processing controller power level change", NULL); 14511 14512 /* Ignoring it for now */ 14513 mutex_enter(&sata_hba_inst->satahba_mutex); 14514 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14515 mutex_exit(&sata_hba_inst->satahba_mutex); 14516 } 14517 14518 /* 14519 * Process port power level change reported by HBA driver. 14520 * Not implemented at this time - event is ignored. 14521 */ 14522 static void 14523 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 14524 sata_address_t *saddr) 14525 { 14526 sata_cport_info_t *cportinfo; 14527 14528 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14529 "Processing port power level change", NULL); 14530 14531 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14532 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14533 /* Reset event flag */ 14534 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14535 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14536 } 14537 14538 /* 14539 * Process port failure reported by HBA driver. 14540 * cports support only - no pmports. 14541 */ 14542 static void 14543 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 14544 sata_address_t *saddr) 14545 { 14546 sata_cport_info_t *cportinfo; 14547 14548 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14549 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14550 /* Reset event flag first */ 14551 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 14552 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 14553 if ((cportinfo->cport_state & 14554 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 14555 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14556 cport_mutex); 14557 return; 14558 } 14559 /* Fail the port */ 14560 cportinfo->cport_state = SATA_PSTATE_FAILED; 14561 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14562 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 14563 } 14564 14565 /* 14566 * Device Reset Event processing. 14567 * The seqeunce is managed by 3 stage flags: 14568 * - reset event reported, 14569 * - reset event being processed, 14570 * - request to clear device reset state. 14571 * 14572 * NOTE: This function has to be entered with cport mutex held. It exits with 14573 * mutex held as well, but can release mutex during the processing. 14574 */ 14575 static void 14576 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 14577 sata_address_t *saddr) 14578 { 14579 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 14580 sata_drive_info_t *sdinfo; 14581 sata_cport_info_t *cportinfo; 14582 sata_device_t sata_device; 14583 int rval_probe, rval_set; 14584 14585 /* We only care about host sata cport for now */ 14586 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14587 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14588 /* 14589 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 14590 * state, ignore reset event. 14591 */ 14592 if (((cportinfo->cport_state & 14593 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 14594 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 14595 sdinfo->satadrv_event_flags &= 14596 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 14597 return; 14598 } 14599 14600 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 14601 SATA_VALID_DEV_TYPE) == 0) { 14602 /* 14603 * This should not happen - coding error. 14604 * But we can recover, so do not panic, just clean up 14605 * and if in debug mode, log the message. 14606 */ 14607 #ifdef SATA_DEBUG 14608 sata_log(sata_hba_inst, CE_WARN, 14609 "sata_process_device_reset: " 14610 "Invalid device type with sdinfo!", NULL); 14611 #endif 14612 sdinfo->satadrv_event_flags = 0; 14613 return; 14614 } 14615 14616 #ifdef SATA_DEBUG 14617 if ((sdinfo->satadrv_event_flags & 14618 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 14619 /* Nothing to do */ 14620 /* Something is weird - why we are processing dev reset? */ 14621 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14622 "No device reset event!!!!", NULL); 14623 14624 return; 14625 } 14626 if ((sdinfo->satadrv_event_flags & 14627 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 14628 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 14629 /* Something is weird - new device reset event */ 14630 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14631 "Overlapping device reset events!", NULL); 14632 } 14633 #endif 14634 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14635 "Processing port %d device reset", saddr->cport); 14636 14637 /* Clear event flag */ 14638 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 14639 14640 /* It seems that we always need to check the port state first */ 14641 sata_device.satadev_rev = SATA_DEVICE_REV; 14642 sata_device.satadev_addr = *saddr; 14643 /* 14644 * We have to exit mutex, because the HBA probe port function may 14645 * block on its own mutex. 14646 */ 14647 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14648 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14649 (SATA_DIP(sata_hba_inst), &sata_device); 14650 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14651 sata_update_port_info(sata_hba_inst, &sata_device); 14652 if (rval_probe != SATA_SUCCESS) { 14653 /* Something went wrong? Fail the port */ 14654 cportinfo->cport_state = SATA_PSTATE_FAILED; 14655 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14656 if (sdinfo != NULL) 14657 sdinfo->satadrv_event_flags = 0; 14658 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14659 cport_mutex); 14660 SATA_LOG_D((sata_hba_inst, CE_WARN, 14661 "SATA port %d probing failed", 14662 saddr->cport)); 14663 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14664 saddr->cport)->cport_mutex); 14665 return; 14666 } 14667 if ((sata_device.satadev_scr.sstatus & 14668 SATA_PORT_DEVLINK_UP_MASK) != 14669 SATA_PORT_DEVLINK_UP || 14670 sata_device.satadev_type == SATA_DTYPE_NONE) { 14671 /* 14672 * No device to process, anymore. Some other event processing 14673 * would or have already performed port info cleanup. 14674 * To be safe (HBA may need it), request clearing device 14675 * reset condition. 14676 */ 14677 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14678 if (sdinfo != NULL) { 14679 sdinfo->satadrv_event_flags &= 14680 ~SATA_EVNT_INPROC_DEVICE_RESET; 14681 sdinfo->satadrv_event_flags |= 14682 SATA_EVNT_CLEAR_DEVICE_RESET; 14683 } 14684 return; 14685 } 14686 14687 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14688 if (sdinfo == NULL) { 14689 return; 14690 } 14691 if ((sdinfo->satadrv_event_flags & 14692 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 14693 /* 14694 * Start tracking time for device feature restoration and 14695 * identification. Save current time (lbolt value). 14696 */ 14697 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 14698 } 14699 /* Mark device reset processing as active */ 14700 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 14701 14702 old_sdinfo = *sdinfo; /* local copy of the drive info */ 14703 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14704 14705 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 14706 14707 if (rval_set != SATA_SUCCESS) { 14708 /* 14709 * Restoring drive setting failed. 14710 * Probe the port first, to check if the port state has changed 14711 */ 14712 sata_device.satadev_rev = SATA_DEVICE_REV; 14713 sata_device.satadev_addr = *saddr; 14714 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14715 /* probe port */ 14716 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14717 (SATA_DIP(sata_hba_inst), &sata_device); 14718 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14719 cport_mutex); 14720 if (rval_probe == SATA_SUCCESS && 14721 (sata_device.satadev_state & 14722 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 14723 (sata_device.satadev_scr.sstatus & 14724 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 14725 sata_device.satadev_type != SATA_DTYPE_NONE) { 14726 /* 14727 * We may retry this a bit later - in-process reset 14728 * condition should be already set. 14729 * Track retry time for device identification. 14730 */ 14731 if ((cportinfo->cport_dev_type & 14732 SATA_VALID_DEV_TYPE) != 0 && 14733 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 14734 sdinfo->satadrv_reset_time != 0) { 14735 clock_t cur_time = ddi_get_lbolt(); 14736 /* 14737 * If the retry time limit was not 14738 * exceeded, retry. 14739 */ 14740 if ((cur_time - sdinfo->satadrv_reset_time) < 14741 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 14742 mutex_enter( 14743 &sata_hba_inst->satahba_mutex); 14744 sata_hba_inst->satahba_event_flags |= 14745 SATA_EVNT_MAIN; 14746 mutex_exit( 14747 &sata_hba_inst->satahba_mutex); 14748 mutex_enter(&sata_mutex); 14749 sata_event_pending |= SATA_EVNT_MAIN; 14750 mutex_exit(&sata_mutex); 14751 return; 14752 } 14753 if (rval_set == SATA_RETRY) { 14754 /* 14755 * Setting drive features failed, but 14756 * the drive is still accessible, 14757 * so emit a warning message before 14758 * return. 14759 */ 14760 mutex_exit(&SATA_CPORT_INFO( 14761 sata_hba_inst, 14762 saddr->cport)->cport_mutex); 14763 goto done; 14764 } 14765 } 14766 /* Fail the drive */ 14767 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 14768 14769 sata_log(sata_hba_inst, CE_WARN, 14770 "SATA device at port %d - device failed", 14771 saddr->cport); 14772 } 14773 /* 14774 * No point of retrying - device failed or some other event 14775 * processing or already did or will do port info cleanup. 14776 * To be safe (HBA may need it), 14777 * request clearing device reset condition. 14778 */ 14779 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 14780 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 14781 sdinfo->satadrv_reset_time = 0; 14782 return; 14783 } 14784 done: 14785 /* 14786 * If setting of drive features failed, but the drive is still 14787 * accessible, emit a warning message. 14788 */ 14789 if (rval_set == SATA_RETRY) { 14790 sata_log(sata_hba_inst, CE_WARN, 14791 "SATA device at port %d - desired setting could not be " 14792 "restored after reset. Device may not operate as expected.", 14793 saddr->cport); 14794 } 14795 /* 14796 * Raise the flag indicating that the next sata command could 14797 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 14798 * reset is reported. 14799 */ 14800 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14801 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14802 sdinfo->satadrv_reset_time = 0; 14803 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 14804 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14805 sdinfo->satadrv_event_flags &= 14806 ~SATA_EVNT_INPROC_DEVICE_RESET; 14807 sdinfo->satadrv_event_flags |= 14808 SATA_EVNT_CLEAR_DEVICE_RESET; 14809 } 14810 } 14811 } 14812 14813 14814 /* 14815 * Port Link Events processing. 14816 * Every link established event may involve device reset (due to 14817 * COMRESET signal, equivalent of the hard reset) so arbitrarily 14818 * set device reset event for an attached device (if any). 14819 * If the port is in SHUTDOWN or FAILED state, ignore link events. 14820 * 14821 * The link established event processing varies, depending on the state 14822 * of the target node, HBA hotplugging capabilities, state of the port. 14823 * If the link is not active, the link established event is ignored. 14824 * If HBA cannot detect device attachment and there is no target node, 14825 * the link established event triggers device attach event processing. 14826 * Else, link established event triggers device reset event processing. 14827 * 14828 * The link lost event processing varies, depending on a HBA hotplugging 14829 * capability and the state of the port (link active or not active). 14830 * If the link is active, the lost link event is ignored. 14831 * If HBA cannot detect device removal, the lost link event triggers 14832 * device detached event processing after link lost timeout. 14833 * Else, the event is ignored. 14834 * 14835 * NOTE: Only cports are processed for now, i.e. no port multiplier ports 14836 */ 14837 static void 14838 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 14839 sata_address_t *saddr) 14840 { 14841 sata_device_t sata_device; 14842 sata_cport_info_t *cportinfo; 14843 sata_drive_info_t *sdinfo; 14844 uint32_t event_flags; 14845 int rval; 14846 14847 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14848 "Processing port %d link event(s)", saddr->cport); 14849 14850 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14851 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14852 event_flags = cportinfo->cport_event_flags; 14853 14854 /* Reset event flags first */ 14855 cportinfo->cport_event_flags &= 14856 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 14857 14858 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 14859 if ((cportinfo->cport_state & 14860 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 14861 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14862 cport_mutex); 14863 return; 14864 } 14865 14866 /* 14867 * For the sanity sake get current port state. 14868 * Set device address only. Other sata_device fields should be 14869 * set by HBA driver. 14870 */ 14871 sata_device.satadev_rev = SATA_DEVICE_REV; 14872 sata_device.satadev_addr = *saddr; 14873 /* 14874 * We have to exit mutex, because the HBA probe port function may 14875 * block on its own mutex. 14876 */ 14877 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14878 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14879 (SATA_DIP(sata_hba_inst), &sata_device); 14880 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14881 sata_update_port_info(sata_hba_inst, &sata_device); 14882 if (rval != SATA_SUCCESS) { 14883 /* Something went wrong? Fail the port */ 14884 cportinfo->cport_state = SATA_PSTATE_FAILED; 14885 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14886 cport_mutex); 14887 SATA_LOG_D((sata_hba_inst, CE_WARN, 14888 "SATA port %d probing failed", 14889 saddr->cport)); 14890 /* 14891 * We may want to release device info structure, but 14892 * it is not necessary. 14893 */ 14894 return; 14895 } else { 14896 /* port probed successfully */ 14897 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14898 } 14899 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 14900 14901 if ((sata_device.satadev_scr.sstatus & 14902 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 14903 /* Ignore event */ 14904 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14905 "Ignoring port %d link established event - " 14906 "link down", 14907 saddr->cport); 14908 goto linklost; 14909 } 14910 14911 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14912 "Processing port %d link established event", 14913 saddr->cport); 14914 14915 /* 14916 * For the sanity sake check if a device is attached - check 14917 * return state of a port probing. 14918 */ 14919 if (sata_device.satadev_type != SATA_DTYPE_NONE && 14920 sata_device.satadev_type != SATA_DTYPE_PMULT) { 14921 /* 14922 * HBA port probe indicated that there is a device 14923 * attached. Check if the framework had device info 14924 * structure attached for this device. 14925 */ 14926 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14927 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 14928 NULL); 14929 14930 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14931 if ((sdinfo->satadrv_type & 14932 SATA_VALID_DEV_TYPE) != 0) { 14933 /* 14934 * Dev info structure is present. 14935 * If dev_type is set to known type in 14936 * the framework's drive info struct 14937 * then the device existed before and 14938 * the link was probably lost 14939 * momentarily - in such case 14940 * we may want to check device 14941 * identity. 14942 * Identity check is not supported now. 14943 * 14944 * Link established event 14945 * triggers device reset event. 14946 */ 14947 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 14948 satadrv_event_flags |= 14949 SATA_EVNT_DEVICE_RESET; 14950 } 14951 } else if (cportinfo->cport_dev_type == 14952 SATA_DTYPE_NONE) { 14953 /* 14954 * We got new device attached! If HBA does not 14955 * generate device attached events, trigger it 14956 * here. 14957 */ 14958 if (!(SATA_FEATURES(sata_hba_inst) & 14959 SATA_CTLF_HOTPLUG)) { 14960 cportinfo->cport_event_flags |= 14961 SATA_EVNT_DEVICE_ATTACHED; 14962 } 14963 } 14964 /* Reset link lost timeout */ 14965 cportinfo->cport_link_lost_time = 0; 14966 } 14967 } 14968 linklost: 14969 if (event_flags & SATA_EVNT_LINK_LOST) { 14970 if ((sata_device.satadev_scr.sstatus & 14971 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 14972 /* Ignore event */ 14973 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14974 "Ignoring port %d link lost event - link is up", 14975 saddr->cport); 14976 goto done; 14977 } 14978 #ifdef SATA_DEBUG 14979 if (cportinfo->cport_link_lost_time == 0) { 14980 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14981 "Processing port %d link lost event", 14982 saddr->cport); 14983 } 14984 #endif 14985 /* 14986 * When HBA cannot generate device attached/detached events, 14987 * we need to track link lost time and eventually generate 14988 * device detach event. 14989 */ 14990 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 14991 /* We are tracking link lost time */ 14992 if (cportinfo->cport_link_lost_time == 0) { 14993 /* save current time (lbolt value) */ 14994 cportinfo->cport_link_lost_time = 14995 ddi_get_lbolt(); 14996 /* just keep link lost event */ 14997 cportinfo->cport_event_flags |= 14998 SATA_EVNT_LINK_LOST; 14999 } else { 15000 clock_t cur_time = ddi_get_lbolt(); 15001 if ((cur_time - 15002 cportinfo->cport_link_lost_time) >= 15003 drv_usectohz( 15004 SATA_EVNT_LINK_LOST_TIMEOUT)) { 15005 /* trigger device detach event */ 15006 cportinfo->cport_event_flags |= 15007 SATA_EVNT_DEVICE_DETACHED; 15008 cportinfo->cport_link_lost_time = 0; 15009 SATADBG1(SATA_DBG_EVENTS, 15010 sata_hba_inst, 15011 "Triggering port %d " 15012 "device detached event", 15013 saddr->cport); 15014 } else { 15015 /* keep link lost event */ 15016 cportinfo->cport_event_flags |= 15017 SATA_EVNT_LINK_LOST; 15018 } 15019 } 15020 } 15021 /* 15022 * We could change port state to disable/delay access to 15023 * the attached device until the link is recovered. 15024 */ 15025 } 15026 done: 15027 event_flags = cportinfo->cport_event_flags; 15028 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15029 if (event_flags != 0) { 15030 mutex_enter(&sata_hba_inst->satahba_mutex); 15031 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15032 mutex_exit(&sata_hba_inst->satahba_mutex); 15033 mutex_enter(&sata_mutex); 15034 sata_event_pending |= SATA_EVNT_MAIN; 15035 mutex_exit(&sata_mutex); 15036 } 15037 } 15038 15039 /* 15040 * Device Detached Event processing. 15041 * Port is probed to find if a device is really gone. If so, 15042 * the device info structure is detached from the SATA port info structure 15043 * and released. 15044 * Port status is updated. 15045 * 15046 * NOTE: Process cports event only, no port multiplier ports. 15047 */ 15048 static void 15049 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 15050 sata_address_t *saddr) 15051 { 15052 sata_cport_info_t *cportinfo; 15053 sata_drive_info_t *sdevinfo; 15054 sata_device_t sata_device; 15055 dev_info_t *tdip; 15056 int rval; 15057 15058 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15059 "Processing port %d device detached", saddr->cport); 15060 15061 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15062 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15063 /* Clear event flag */ 15064 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 15065 15066 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 15067 if ((cportinfo->cport_state & 15068 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 15069 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15070 cport_mutex); 15071 return; 15072 } 15073 /* For sanity, re-probe the port */ 15074 sata_device.satadev_rev = SATA_DEVICE_REV; 15075 sata_device.satadev_addr = *saddr; 15076 15077 /* 15078 * We have to exit mutex, because the HBA probe port function may 15079 * block on its own mutex. 15080 */ 15081 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15082 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15083 (SATA_DIP(sata_hba_inst), &sata_device); 15084 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15085 sata_update_port_info(sata_hba_inst, &sata_device); 15086 if (rval != SATA_SUCCESS) { 15087 /* Something went wrong? Fail the port */ 15088 cportinfo->cport_state = SATA_PSTATE_FAILED; 15089 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15090 cport_mutex); 15091 SATA_LOG_D((sata_hba_inst, CE_WARN, 15092 "SATA port %d probing failed", 15093 saddr->cport)); 15094 /* 15095 * We may want to release device info structure, but 15096 * it is not necessary. 15097 */ 15098 return; 15099 } else { 15100 /* port probed successfully */ 15101 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 15102 } 15103 /* 15104 * Check if a device is still attached. For sanity, check also 15105 * link status - if no link, there is no device. 15106 */ 15107 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 15108 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 15109 SATA_DTYPE_NONE) { 15110 /* 15111 * Device is still attached - ignore detach event. 15112 */ 15113 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15114 cport_mutex); 15115 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15116 "Ignoring detach - device still attached to port %d", 15117 sata_device.satadev_addr.cport); 15118 return; 15119 } 15120 /* 15121 * We need to detach and release device info structure here 15122 */ 15123 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15124 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15125 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15126 (void) kmem_free((void *)sdevinfo, 15127 sizeof (sata_drive_info_t)); 15128 } 15129 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15130 /* 15131 * Device cannot be reached anymore, even if the target node may be 15132 * still present. 15133 */ 15134 15135 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15136 sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d", 15137 sata_device.satadev_addr.cport); 15138 15139 /* 15140 * Try to offline a device and remove target node if it still exists 15141 */ 15142 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15143 if (tdip != NULL) { 15144 /* 15145 * Target node exists. Unconfigure device then remove 15146 * the target node (one ndi operation). 15147 */ 15148 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 15149 /* 15150 * PROBLEM - no device, but target node remained 15151 * This happens when the file was open or node was 15152 * waiting for resources. 15153 */ 15154 SATA_LOG_D((sata_hba_inst, CE_WARN, 15155 "sata_process_device_detached: " 15156 "Failed to remove target node for " 15157 "detached SATA device.")); 15158 /* 15159 * Set target node state to DEVI_DEVICE_REMOVED. 15160 * But re-check first that the node still exists. 15161 */ 15162 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15163 saddr->cport); 15164 if (tdip != NULL) { 15165 sata_set_device_removed(tdip); 15166 /* 15167 * Instruct event daemon to retry the 15168 * cleanup later. 15169 */ 15170 sata_set_target_node_cleanup(sata_hba_inst, 15171 &sata_device.satadev_addr); 15172 } 15173 } 15174 } 15175 /* 15176 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15177 * with the hint: SE_HINT_REMOVE 15178 */ 15179 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 15180 } 15181 15182 15183 /* 15184 * Device Attached Event processing. 15185 * Port state is checked to verify that a device is really attached. If so, 15186 * the device info structure is created and attached to the SATA port info 15187 * structure. 15188 * 15189 * If attached device cannot be identified or set-up, the retry for the 15190 * attach processing is set-up. Subsequent daemon run would try again to 15191 * identify the device, until the time limit is reached 15192 * (SATA_DEV_IDENTIFY_TIMEOUT). 15193 * 15194 * This function cannot be called in interrupt context (it may sleep). 15195 * 15196 * NOTE: Process cports event only, no port multiplier ports. 15197 */ 15198 static void 15199 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 15200 sata_address_t *saddr) 15201 { 15202 sata_cport_info_t *cportinfo; 15203 sata_drive_info_t *sdevinfo; 15204 sata_device_t sata_device; 15205 dev_info_t *tdip; 15206 uint32_t event_flags; 15207 int rval; 15208 15209 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15210 "Processing port %d device attached", saddr->cport); 15211 15212 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15213 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15214 15215 /* Clear attach event flag first */ 15216 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 15217 15218 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 15219 if ((cportinfo->cport_state & 15220 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 15221 cportinfo->cport_dev_attach_time = 0; 15222 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15223 cport_mutex); 15224 return; 15225 } 15226 15227 /* 15228 * If the sata_drive_info structure is found attached to the port info, 15229 * despite the fact the device was removed and now it is re-attached, 15230 * the old drive info structure was not removed. 15231 * Arbitrarily release device info structure. 15232 */ 15233 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15234 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15235 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15236 (void) kmem_free((void *)sdevinfo, 15237 sizeof (sata_drive_info_t)); 15238 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15239 "Arbitrarily detaching old device info.", NULL); 15240 } 15241 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15242 15243 /* For sanity, re-probe the port */ 15244 sata_device.satadev_rev = SATA_DEVICE_REV; 15245 sata_device.satadev_addr = *saddr; 15246 15247 /* 15248 * We have to exit mutex, because the HBA probe port function may 15249 * block on its own mutex. 15250 */ 15251 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15252 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15253 (SATA_DIP(sata_hba_inst), &sata_device); 15254 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15255 sata_update_port_info(sata_hba_inst, &sata_device); 15256 if (rval != SATA_SUCCESS) { 15257 /* Something went wrong? Fail the port */ 15258 cportinfo->cport_state = SATA_PSTATE_FAILED; 15259 cportinfo->cport_dev_attach_time = 0; 15260 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15261 cport_mutex); 15262 SATA_LOG_D((sata_hba_inst, CE_WARN, 15263 "SATA port %d probing failed", 15264 saddr->cport)); 15265 return; 15266 } else { 15267 /* port probed successfully */ 15268 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 15269 } 15270 /* 15271 * Check if a device is still attached. For sanity, check also 15272 * link status - if no link, there is no device. 15273 */ 15274 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 15275 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 15276 SATA_DTYPE_NONE) { 15277 /* 15278 * No device - ignore attach event. 15279 */ 15280 cportinfo->cport_dev_attach_time = 0; 15281 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15282 cport_mutex); 15283 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15284 "Ignoring attach - no device connected to port %d", 15285 sata_device.satadev_addr.cport); 15286 return; 15287 } 15288 15289 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15290 /* 15291 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15292 * with the hint: SE_HINT_INSERT 15293 */ 15294 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 15295 15296 /* 15297 * Port reprobing will take care of the creation of the device 15298 * info structure and determination of the device type. 15299 */ 15300 sata_device.satadev_addr = *saddr; 15301 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 15302 SATA_DEV_IDENTIFY_NORETRY); 15303 15304 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15305 cport_mutex); 15306 if ((cportinfo->cport_state & SATA_STATE_READY) && 15307 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 15308 /* Some device is attached to the port */ 15309 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 15310 /* 15311 * A device was not successfully attached. 15312 * Track retry time for device identification. 15313 */ 15314 if (cportinfo->cport_dev_attach_time != 0) { 15315 clock_t cur_time = ddi_get_lbolt(); 15316 /* 15317 * If the retry time limit was not exceeded, 15318 * reinstate attach event. 15319 */ 15320 if ((cur_time - 15321 cportinfo->cport_dev_attach_time) < 15322 drv_usectohz( 15323 SATA_DEV_IDENTIFY_TIMEOUT)) { 15324 /* OK, restore attach event */ 15325 cportinfo->cport_event_flags |= 15326 SATA_EVNT_DEVICE_ATTACHED; 15327 } else { 15328 /* Timeout - cannot identify device */ 15329 cportinfo->cport_dev_attach_time = 0; 15330 sata_log(sata_hba_inst, 15331 CE_WARN, 15332 "Could not identify SATA device " 15333 "at port %d", 15334 saddr->cport); 15335 } 15336 } else { 15337 /* 15338 * Start tracking time for device 15339 * identification. 15340 * Save current time (lbolt value). 15341 */ 15342 cportinfo->cport_dev_attach_time = 15343 ddi_get_lbolt(); 15344 /* Restore attach event */ 15345 cportinfo->cport_event_flags |= 15346 SATA_EVNT_DEVICE_ATTACHED; 15347 } 15348 } else { 15349 /* 15350 * If device was successfully attached, the subsequent 15351 * action depends on a state of the 15352 * sata_auto_online variable. If it is set to zero. 15353 * an explicit 'configure' command will be needed to 15354 * configure it. If its value is non-zero, we will 15355 * attempt to online (configure) the device. 15356 * First, log the message indicating that a device 15357 * was attached. 15358 */ 15359 cportinfo->cport_dev_attach_time = 0; 15360 sata_log(sata_hba_inst, CE_WARN, 15361 "SATA device detected at port %d", saddr->cport); 15362 15363 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15364 sata_drive_info_t new_sdinfo; 15365 15366 /* Log device info data */ 15367 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 15368 cportinfo)); 15369 sata_show_drive_info(sata_hba_inst, 15370 &new_sdinfo); 15371 } 15372 15373 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15374 saddr->cport)->cport_mutex); 15375 15376 /* 15377 * Make sure that there is no target node for that 15378 * device. If so, release it. It should not happen, 15379 * unless we had problem removing the node when 15380 * device was detached. 15381 */ 15382 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15383 saddr->cport); 15384 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15385 saddr->cport)->cport_mutex); 15386 if (tdip != NULL) { 15387 15388 #ifdef SATA_DEBUG 15389 if ((cportinfo->cport_event_flags & 15390 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 15391 sata_log(sata_hba_inst, CE_WARN, 15392 "sata_process_device_attached: " 15393 "old device target node exists!"); 15394 #endif 15395 /* 15396 * target node exists - try to unconfigure 15397 * device and remove the node. 15398 */ 15399 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15400 saddr->cport)->cport_mutex); 15401 rval = ndi_devi_offline(tdip, 15402 NDI_DEVI_REMOVE); 15403 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15404 saddr->cport)->cport_mutex); 15405 15406 if (rval == NDI_SUCCESS) { 15407 cportinfo->cport_event_flags &= 15408 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15409 cportinfo->cport_tgtnode_clean = B_TRUE; 15410 } else { 15411 /* 15412 * PROBLEM - the target node remained 15413 * and it belongs to a previously 15414 * attached device. 15415 * This happens when the file was open 15416 * or the node was waiting for 15417 * resources at the time the 15418 * associated device was removed. 15419 * Instruct event daemon to retry the 15420 * cleanup later. 15421 */ 15422 sata_log(sata_hba_inst, 15423 CE_WARN, 15424 "Application(s) accessing " 15425 "previously attached SATA " 15426 "device have to release " 15427 "it before newly inserted " 15428 "device can be made accessible.", 15429 saddr->cport); 15430 cportinfo->cport_event_flags |= 15431 SATA_EVNT_TARGET_NODE_CLEANUP; 15432 cportinfo->cport_tgtnode_clean = 15433 B_FALSE; 15434 } 15435 } 15436 if (sata_auto_online != 0) { 15437 cportinfo->cport_event_flags |= 15438 SATA_EVNT_AUTOONLINE_DEVICE; 15439 } 15440 15441 } 15442 } else { 15443 cportinfo->cport_dev_attach_time = 0; 15444 } 15445 15446 event_flags = cportinfo->cport_event_flags; 15447 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15448 if (event_flags != 0) { 15449 mutex_enter(&sata_hba_inst->satahba_mutex); 15450 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15451 mutex_exit(&sata_hba_inst->satahba_mutex); 15452 mutex_enter(&sata_mutex); 15453 sata_event_pending |= SATA_EVNT_MAIN; 15454 mutex_exit(&sata_mutex); 15455 } 15456 } 15457 15458 15459 /* 15460 * Device Target Node Cleanup Event processing. 15461 * If the target node associated with a sata port device is in 15462 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 15463 * If the target node cannot be removed, the event flag is left intact, 15464 * so that event daemon may re-run this function later. 15465 * 15466 * This function cannot be called in interrupt context (it may sleep). 15467 * 15468 * NOTE: Processes cport events only, not port multiplier ports. 15469 */ 15470 static void 15471 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15472 sata_address_t *saddr) 15473 { 15474 sata_cport_info_t *cportinfo; 15475 dev_info_t *tdip; 15476 15477 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15478 "Processing port %d device target node cleanup", saddr->cport); 15479 15480 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15481 15482 /* 15483 * Check if there is target node for that device and it is in the 15484 * DEVI_DEVICE_REMOVED state. If so, release it. 15485 */ 15486 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15487 if (tdip != NULL) { 15488 /* 15489 * target node exists - check if it is target node of 15490 * a removed device. 15491 */ 15492 if (sata_check_device_removed(tdip) == B_TRUE) { 15493 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15494 "sata_process_target_node_cleanup: " 15495 "old device target node exists!", NULL); 15496 /* 15497 * Unconfigure and remove the target node 15498 */ 15499 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 15500 NDI_SUCCESS) { 15501 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15502 saddr->cport)->cport_mutex); 15503 cportinfo->cport_event_flags &= 15504 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15505 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15506 saddr->cport)->cport_mutex); 15507 return; 15508 } 15509 /* 15510 * Event daemon will retry the cleanup later. 15511 */ 15512 mutex_enter(&sata_hba_inst->satahba_mutex); 15513 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15514 mutex_exit(&sata_hba_inst->satahba_mutex); 15515 mutex_enter(&sata_mutex); 15516 sata_event_pending |= SATA_EVNT_MAIN; 15517 mutex_exit(&sata_mutex); 15518 } 15519 } else { 15520 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15521 saddr->cport)->cport_mutex); 15522 cportinfo->cport_event_flags &= 15523 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15524 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15525 saddr->cport)->cport_mutex); 15526 } 15527 } 15528 15529 /* 15530 * Device AutoOnline Event processing. 15531 * If attached device is to be onlined, an attempt is made to online this 15532 * device, but only if there is no lingering (old) target node present. 15533 * If the device cannot be onlined, the event flag is left intact, 15534 * so that event daemon may re-run this function later. 15535 * 15536 * This function cannot be called in interrupt context (it may sleep). 15537 * 15538 * NOTE: Processes cport events only, not port multiplier ports. 15539 */ 15540 static void 15541 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 15542 sata_address_t *saddr) 15543 { 15544 sata_cport_info_t *cportinfo; 15545 sata_drive_info_t *sdinfo; 15546 sata_device_t sata_device; 15547 dev_info_t *tdip; 15548 15549 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15550 "Processing port %d attached device auto-onlining", saddr->cport); 15551 15552 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15553 15554 /* 15555 * Check if device is present and recognized. If not, reset event. 15556 */ 15557 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15558 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 15559 /* Nothing to online */ 15560 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15561 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15562 saddr->cport)->cport_mutex); 15563 return; 15564 } 15565 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15566 15567 /* 15568 * Check if there is target node for this device and if it is in the 15569 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 15570 * the event for later processing. 15571 */ 15572 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15573 if (tdip != NULL) { 15574 /* 15575 * target node exists - check if it is target node of 15576 * a removed device. 15577 */ 15578 if (sata_check_device_removed(tdip) == B_TRUE) { 15579 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15580 "sata_process_device_autoonline: " 15581 "old device target node exists!", NULL); 15582 /* 15583 * Event daemon will retry device onlining later. 15584 */ 15585 mutex_enter(&sata_hba_inst->satahba_mutex); 15586 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15587 mutex_exit(&sata_hba_inst->satahba_mutex); 15588 mutex_enter(&sata_mutex); 15589 sata_event_pending |= SATA_EVNT_MAIN; 15590 mutex_exit(&sata_mutex); 15591 return; 15592 } 15593 /* 15594 * If the target node is not in the 'removed" state, assume 15595 * that it belongs to this device. There is nothing more to do, 15596 * but reset the event. 15597 */ 15598 } else { 15599 15600 /* 15601 * Try to online the device 15602 * If there is any reset-related event, remove it. We are 15603 * configuring the device and no state restoring is needed. 15604 */ 15605 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15606 saddr->cport)->cport_mutex); 15607 sata_device.satadev_addr = *saddr; 15608 if (saddr->qual == SATA_ADDR_CPORT) 15609 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 15610 else 15611 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 15612 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 15613 if (sdinfo != NULL) { 15614 if (sdinfo->satadrv_event_flags & 15615 (SATA_EVNT_DEVICE_RESET | 15616 SATA_EVNT_INPROC_DEVICE_RESET)) 15617 sdinfo->satadrv_event_flags = 0; 15618 sdinfo->satadrv_event_flags |= 15619 SATA_EVNT_CLEAR_DEVICE_RESET; 15620 15621 /* Need to create a new target node. */ 15622 cportinfo->cport_tgtnode_clean = B_TRUE; 15623 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15624 saddr->cport)->cport_mutex); 15625 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15626 sata_hba_inst, &sata_device.satadev_addr); 15627 if (tdip == NULL) { 15628 /* 15629 * Configure (onlining) failed. 15630 * We will NOT retry 15631 */ 15632 SATA_LOG_D((sata_hba_inst, CE_WARN, 15633 "sata_process_device_autoonline: " 15634 "configuring SATA device at port %d failed", 15635 saddr->cport)); 15636 } 15637 } else { 15638 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15639 saddr->cport)->cport_mutex); 15640 } 15641 15642 } 15643 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15644 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15645 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15646 saddr->cport)->cport_mutex); 15647 } 15648 15649 15650 static void 15651 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 15652 int hint) 15653 { 15654 char ap[MAXPATHLEN]; 15655 nvlist_t *ev_attr_list = NULL; 15656 int err; 15657 15658 /* Allocate and build sysevent attribute list */ 15659 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 15660 if (err != 0) { 15661 SATA_LOG_D((sata_hba_inst, CE_WARN, 15662 "sata_gen_sysevent: " 15663 "cannot allocate memory for sysevent attributes\n")); 15664 return; 15665 } 15666 /* Add hint attribute */ 15667 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 15668 if (err != 0) { 15669 SATA_LOG_D((sata_hba_inst, CE_WARN, 15670 "sata_gen_sysevent: " 15671 "failed to add DR_HINT attr for sysevent")); 15672 nvlist_free(ev_attr_list); 15673 return; 15674 } 15675 /* 15676 * Add AP attribute. 15677 * Get controller pathname and convert it into AP pathname by adding 15678 * a target number. 15679 */ 15680 (void) snprintf(ap, MAXPATHLEN, "/devices"); 15681 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 15682 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 15683 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 15684 15685 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 15686 if (err != 0) { 15687 SATA_LOG_D((sata_hba_inst, CE_WARN, 15688 "sata_gen_sysevent: " 15689 "failed to add DR_AP_ID attr for sysevent")); 15690 nvlist_free(ev_attr_list); 15691 return; 15692 } 15693 15694 /* Generate/log sysevent */ 15695 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 15696 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 15697 if (err != DDI_SUCCESS) { 15698 SATA_LOG_D((sata_hba_inst, CE_WARN, 15699 "sata_gen_sysevent: " 15700 "cannot log sysevent, err code %x\n", err)); 15701 } 15702 15703 nvlist_free(ev_attr_list); 15704 } 15705 15706 15707 15708 15709 /* 15710 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 15711 */ 15712 static void 15713 sata_set_device_removed(dev_info_t *tdip) 15714 { 15715 int circ; 15716 15717 ASSERT(tdip != NULL); 15718 15719 ndi_devi_enter(tdip, &circ); 15720 mutex_enter(&DEVI(tdip)->devi_lock); 15721 DEVI_SET_DEVICE_REMOVED(tdip); 15722 mutex_exit(&DEVI(tdip)->devi_lock); 15723 ndi_devi_exit(tdip, circ); 15724 } 15725 15726 15727 /* 15728 * Set internal event instructing event daemon to try 15729 * to perform the target node cleanup. 15730 */ 15731 static void 15732 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15733 sata_address_t *saddr) 15734 { 15735 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15736 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 15737 SATA_EVNT_TARGET_NODE_CLEANUP; 15738 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean = 15739 B_FALSE; 15740 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15741 mutex_enter(&sata_hba_inst->satahba_mutex); 15742 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15743 mutex_exit(&sata_hba_inst->satahba_mutex); 15744 mutex_enter(&sata_mutex); 15745 sata_event_pending |= SATA_EVNT_MAIN; 15746 mutex_exit(&sata_mutex); 15747 } 15748 15749 15750 /* 15751 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 15752 * i.e. check if the target node state indicates that it belongs to a removed 15753 * device. 15754 * 15755 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 15756 * B_FALSE otherwise. 15757 * 15758 * NOTE: No port multiplier support. 15759 */ 15760 static boolean_t 15761 sata_check_device_removed(dev_info_t *tdip) 15762 { 15763 ASSERT(tdip != NULL); 15764 15765 if (DEVI_IS_DEVICE_REMOVED(tdip)) 15766 return (B_TRUE); 15767 else 15768 return (B_FALSE); 15769 } 15770 15771 /* ************************ FAULT INJECTTION **************************** */ 15772 15773 #ifdef SATA_INJECT_FAULTS 15774 15775 static uint32_t sata_fault_count = 0; 15776 static uint32_t sata_fault_suspend_count = 0; 15777 15778 /* 15779 * Inject sata pkt fault 15780 * It modifies returned values of the sata packet. 15781 * It returns immediately if: 15782 * pkt fault injection is not enabled (via sata_inject_fault, 15783 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 15784 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 15785 * pkt is not directed to specified fault controller/device 15786 * (sata_fault_ctrl_dev and sata_fault_device). 15787 * If fault controller is not specified, fault injection applies to all 15788 * controllers and devices. 15789 * 15790 * First argument is the pointer to the executed sata packet. 15791 * Second argument is a pointer to a value returned by the HBA tran_start 15792 * function. 15793 * Third argument specifies injected error. Injected sata packet faults 15794 * are the satapkt_reason values. 15795 * SATA_PKT_BUSY -1 Not completed, busy 15796 * SATA_PKT_DEV_ERROR 1 Device reported error 15797 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 15798 * SATA_PKT_PORT_ERROR 3 Not completed, port error 15799 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 15800 * SATA_PKT_ABORTED 5 Aborted by request 15801 * SATA_PKT_TIMEOUT 6 Operation timeut 15802 * SATA_PKT_RESET 7 Aborted by reset request 15803 * 15804 * Additional global variables affecting the execution: 15805 * 15806 * sata_inject_fault_count variable specifies number of times in row the 15807 * error is injected. Value of -1 specifies permanent fault, ie. every time 15808 * the fault injection point is reached, the fault is injected and a pause 15809 * between fault injection specified by sata_inject_fault_pause_count is 15810 * ignored). Fault injection routine decrements sata_inject_fault_count 15811 * (if greater than zero) until it reaches 0. No fault is injected when 15812 * sata_inject_fault_count is 0 (zero). 15813 * 15814 * sata_inject_fault_pause_count variable specifies number of times a fault 15815 * injection is bypassed (pause between fault injections). 15816 * If set to 0, a fault is injected only a number of times specified by 15817 * sata_inject_fault_count. 15818 * 15819 * The fault counts are static, so for periodic errors they have to be manually 15820 * reset to start repetition sequence from scratch. 15821 * If the original value returned by the HBA tran_start function is not 15822 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 15823 * is injected (to avoid masking real problems); 15824 * 15825 * NOTE: In its current incarnation, this function should be invoked only for 15826 * commands executed in SYNCHRONOUS mode. 15827 */ 15828 15829 15830 static void 15831 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 15832 { 15833 15834 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 15835 return; 15836 15837 if (sata_inject_fault_count == 0) 15838 return; 15839 15840 if (fault == 0) 15841 return; 15842 15843 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 15844 return; 15845 15846 if (sata_fault_ctrl != NULL) { 15847 sata_pkt_txlate_t *spx = 15848 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 15849 15850 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 15851 spx->txlt_sata_hba_inst->satahba_dip) 15852 return; 15853 15854 if (sata_fault_device.satadev_addr.cport != 15855 spkt->satapkt_device.satadev_addr.cport || 15856 sata_fault_device.satadev_addr.pmport != 15857 spkt->satapkt_device.satadev_addr.pmport || 15858 sata_fault_device.satadev_addr.qual != 15859 spkt->satapkt_device.satadev_addr.qual) 15860 return; 15861 } 15862 15863 /* Modify pkt return parameters */ 15864 if (*rval != SATA_TRAN_ACCEPTED || 15865 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15866 sata_fault_count = 0; 15867 sata_fault_suspend_count = 0; 15868 return; 15869 } 15870 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 15871 /* Pause in the injection */ 15872 sata_fault_suspend_count -= 1; 15873 return; 15874 } 15875 15876 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 15877 /* 15878 * Init inject fault cycle. If fault count is set to -1, 15879 * it is a permanent fault. 15880 */ 15881 if (sata_inject_fault_count != -1) { 15882 sata_fault_count = sata_inject_fault_count; 15883 sata_fault_suspend_count = 15884 sata_inject_fault_pause_count; 15885 if (sata_fault_suspend_count == 0) 15886 sata_inject_fault_count = 0; 15887 } 15888 } 15889 15890 if (sata_fault_count != 0) 15891 sata_fault_count -= 1; 15892 15893 switch (fault) { 15894 case SATA_PKT_BUSY: 15895 *rval = SATA_TRAN_BUSY; 15896 spkt->satapkt_reason = SATA_PKT_BUSY; 15897 break; 15898 15899 case SATA_PKT_QUEUE_FULL: 15900 *rval = SATA_TRAN_QUEUE_FULL; 15901 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 15902 break; 15903 15904 case SATA_PKT_CMD_UNSUPPORTED: 15905 *rval = SATA_TRAN_CMD_UNSUPPORTED; 15906 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 15907 break; 15908 15909 case SATA_PKT_PORT_ERROR: 15910 /* This is "rejected" command */ 15911 *rval = SATA_TRAN_PORT_ERROR; 15912 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 15913 /* Additional error setup could be done here - port state */ 15914 break; 15915 15916 case SATA_PKT_DEV_ERROR: 15917 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 15918 /* 15919 * Additional error setup could be done here 15920 */ 15921 break; 15922 15923 case SATA_PKT_ABORTED: 15924 spkt->satapkt_reason = SATA_PKT_ABORTED; 15925 break; 15926 15927 case SATA_PKT_TIMEOUT: 15928 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 15929 /* Additional error setup could be done here */ 15930 break; 15931 15932 case SATA_PKT_RESET: 15933 spkt->satapkt_reason = SATA_PKT_RESET; 15934 /* 15935 * Additional error setup could be done here - device reset 15936 */ 15937 break; 15938 15939 default: 15940 break; 15941 } 15942 } 15943 15944 #endif 15945