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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * SATA Framework 31 * Generic SATA Host Adapter Implementation 32 */ 33 34 #include <sys/conf.h> 35 #include <sys/file.h> 36 #include <sys/ddi.h> 37 #include <sys/sunddi.h> 38 #include <sys/modctl.h> 39 #include <sys/cmn_err.h> 40 #include <sys/errno.h> 41 #include <sys/thread.h> 42 #include <sys/kstat.h> 43 #include <sys/note.h> 44 #include <sys/sysevent.h> 45 #include <sys/sysevent/eventdefs.h> 46 #include <sys/sysevent/dr.h> 47 #include <sys/taskq.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 57 /* 58 * Flags enabling selected SATA HBA framework functionality 59 */ 60 #define SATA_ENABLE_QUEUING 1 61 #define SATA_ENABLE_NCQ 2 62 #define SATA_ENABLE_PROCESS_EVENTS 4 63 int sata_func_enable = 64 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 65 66 /* 67 * Global variable setting default maximum queue depth (NCQ or TCQ) 68 * Note:minimum queue depth is 1 69 */ 70 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 71 72 /* 73 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 74 * initialization, using value from sata_max_queue_depth 75 * It is adjusted to minimum supported by the controller and by the device, 76 * if queueing is enabled. 77 */ 78 static int sata_current_max_qdepth; 79 80 #ifdef SATA_DEBUG 81 82 #define SATA_LOG_D(args) sata_log args 83 uint64_t mbuf_count = 0; 84 uint64_t mbuffail_count = 0; 85 86 sata_atapi_cmd_t sata_atapi_trace[64]; 87 uint32_t sata_atapi_trace_index = 0; 88 int sata_atapi_trace_save = 1; 89 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 90 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 91 sata_save_atapi_trace(spx, count); 92 93 #else 94 #define SATA_LOG_D(arg) 95 #define SATAATAPITRACE(spx, count) 96 #endif 97 98 #if 0 99 static void 100 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 101 #endif 102 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 103 104 105 /* 106 * SATA cb_ops functions 107 */ 108 static int sata_hba_open(dev_t *, int, int, cred_t *); 109 static int sata_hba_close(dev_t, int, int, cred_t *); 110 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 111 112 /* 113 * SCSA required entry points 114 */ 115 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 116 scsi_hba_tran_t *, struct scsi_device *); 117 static int sata_scsi_tgt_probe(struct scsi_device *, 118 int (*callback)(void)); 119 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 120 scsi_hba_tran_t *, struct scsi_device *); 121 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 122 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 123 static int sata_scsi_reset(struct scsi_address *, int); 124 static int sata_scsi_getcap(struct scsi_address *, char *, int); 125 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 126 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 127 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 128 caddr_t); 129 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 130 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 131 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 132 133 /* 134 * SATA HBA interface functions are defined in sata_hba.h header file 135 */ 136 137 /* Event processing functions */ 138 static void sata_event_daemon(void *); 139 static void sata_event_thread_control(int); 140 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 141 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 142 static void sata_process_port_failed_event(sata_hba_inst_t *, 143 sata_address_t *); 144 static void sata_process_port_link_events(sata_hba_inst_t *, 145 sata_address_t *); 146 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 147 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 148 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 149 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 150 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 151 sata_address_t *); 152 153 154 /* 155 * Local translation functions 156 */ 157 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 158 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 159 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 160 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 161 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 162 static int sata_txlt_read(sata_pkt_txlate_t *); 163 static int sata_txlt_write(sata_pkt_txlate_t *); 164 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 165 static int sata_txlt_log_select(sata_pkt_txlate_t *); 166 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 167 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 168 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 169 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 170 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 171 172 static int sata_hba_start(sata_pkt_txlate_t *, int *); 173 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 174 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 175 static void sata_txlt_rw_completion(sata_pkt_t *); 176 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 177 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 178 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 179 static struct scsi_extended_sense *sata_immediate_error_response( 180 sata_pkt_txlate_t *, int); 181 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 182 183 static int sata_txlt_atapi(sata_pkt_txlate_t *); 184 static void sata_txlt_atapi_completion(sata_pkt_t *); 185 186 /* 187 * Local functions for ioctl 188 */ 189 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 190 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 191 devctl_ap_state_t *); 192 static dev_info_t *sata_get_target_dip(dev_info_t *, int32_t); 193 static dev_info_t *sata_devt_to_devinfo(dev_t); 194 195 /* 196 * Local functions 197 */ 198 static void sata_remove_hba_instance(dev_info_t *); 199 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 200 static void sata_probe_ports(sata_hba_inst_t *); 201 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 202 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport, 203 int pmport); 204 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 205 sata_address_t *); 206 static int sata_validate_scsi_address(sata_hba_inst_t *, 207 struct scsi_address *, sata_device_t *); 208 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 209 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 210 static void sata_pkt_free(sata_pkt_txlate_t *); 211 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 212 caddr_t, ddi_dma_attr_t *); 213 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 214 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 215 sata_device_t *); 216 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 217 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 218 static void sata_free_local_buffer(sata_pkt_txlate_t *); 219 static uint64_t sata_check_capacity(sata_drive_info_t *); 220 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 221 ddi_dma_attr_t *); 222 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 223 sata_drive_info_t *); 224 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 225 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 226 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 227 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 228 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 229 static int sata_set_drive_features(sata_hba_inst_t *, 230 sata_drive_info_t *, int flag); 231 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 232 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 233 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 234 uint8_t *); 235 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 236 struct scsi_inquiry *); 237 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 238 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 239 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 240 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 241 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 242 struct mode_cache_scsi3 *, int, int *, int *, int *); 243 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 244 struct mode_info_excpt_page *, int, int *, int *, int *); 245 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 246 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 247 struct mode_acoustic_management *, int, int *, int *, int *); 248 249 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 250 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 251 sata_hba_inst_t *); 252 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 253 sata_hba_inst_t *); 254 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 255 sata_hba_inst_t *); 256 static void sata_save_drive_settings(sata_drive_info_t *); 257 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 258 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 259 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 260 sata_drive_info_t *); 261 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 262 struct smart_data *); 263 static int sata_smart_selftest_log(sata_hba_inst_t *, 264 sata_drive_info_t *, 265 struct smart_selftest_log *); 266 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 267 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 268 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 269 uint8_t *, uint8_t, uint8_t); 270 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 271 struct read_log_ext_directory *); 272 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 273 static void sata_xlate_errors(sata_pkt_txlate_t *); 274 static void sata_decode_device_error(sata_pkt_txlate_t *, 275 struct scsi_extended_sense *); 276 static void sata_set_device_removed(dev_info_t *); 277 static boolean_t sata_check_device_removed(dev_info_t *); 278 static void sata_set_target_node_cleanup(sata_hba_inst_t *, int cport); 279 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 280 sata_drive_info_t *); 281 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 282 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 283 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 284 static int sata_check_modser(char *, int); 285 286 287 288 /* 289 * SATA Framework will ignore SATA HBA driver cb_ops structure and 290 * register following one with SCSA framework. 291 * Open & close are provided, so scsi framework will not use its own 292 */ 293 static struct cb_ops sata_cb_ops = { 294 sata_hba_open, /* open */ 295 sata_hba_close, /* close */ 296 nodev, /* strategy */ 297 nodev, /* print */ 298 nodev, /* dump */ 299 nodev, /* read */ 300 nodev, /* write */ 301 sata_hba_ioctl, /* ioctl */ 302 nodev, /* devmap */ 303 nodev, /* mmap */ 304 nodev, /* segmap */ 305 nochpoll, /* chpoll */ 306 ddi_prop_op, /* cb_prop_op */ 307 0, /* streamtab */ 308 D_NEW | D_MP, /* cb_flag */ 309 CB_REV, /* rev */ 310 nodev, /* aread */ 311 nodev /* awrite */ 312 }; 313 314 315 extern struct mod_ops mod_miscops; 316 extern uchar_t scsi_cdb_size[]; 317 318 static struct modlmisc modlmisc = { 319 &mod_miscops, /* Type of module */ 320 "SATA Module v%I%" /* module name */ 321 }; 322 323 324 static struct modlinkage modlinkage = { 325 MODREV_1, 326 (void *)&modlmisc, 327 NULL 328 }; 329 330 /* 331 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 332 * i.e. when scsi_pkt has not timeout specified. 333 */ 334 static int sata_default_pkt_time = 60; /* 60 seconds */ 335 336 /* 337 * Intermediate buffer device access attributes - they are required, 338 * but not necessarily used. 339 */ 340 static ddi_device_acc_attr_t sata_acc_attr = { 341 DDI_DEVICE_ATTR_V0, 342 DDI_STRUCTURE_LE_ACC, 343 DDI_STRICTORDER_ACC 344 }; 345 346 347 /* 348 * Mutexes protecting structures in multithreaded operations. 349 * Because events are relatively rare, a single global mutex protecting 350 * data structures should be sufficient. To increase performance, add 351 * separate mutex per each sata port and use global mutex only to protect 352 * common data structures. 353 */ 354 static kmutex_t sata_mutex; /* protects sata_hba_list */ 355 static kmutex_t sata_log_mutex; /* protects log */ 356 357 static char sata_log_buf[256]; 358 359 /* Default write cache setting for SATA hard disks */ 360 int sata_write_cache = 1; /* enabled */ 361 362 /* Default write cache setting for SATA ATAPI CD/DVD */ 363 int sata_atapicdvd_write_cache = 1; /* enabled */ 364 365 /* 366 * Linked list of HBA instances 367 */ 368 static sata_hba_inst_t *sata_hba_list = NULL; 369 static sata_hba_inst_t *sata_hba_list_tail = NULL; 370 /* 371 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 372 * structure and in sata soft state. 373 */ 374 375 /* 376 * Event daemon related variables 377 */ 378 static kmutex_t sata_event_mutex; 379 static kcondvar_t sata_event_cv; 380 static kthread_t *sata_event_thread = NULL; 381 static int sata_event_thread_terminate = 0; 382 static int sata_event_pending = 0; 383 static int sata_event_thread_active = 0; 384 extern pri_t minclsyspri; 385 386 /* 387 * NCQ error recovery command 388 */ 389 static const sata_cmd_t sata_rle_cmd = { 390 SATA_CMD_REV, 391 NULL, 392 { 393 SATA_DIR_READ 394 }, 395 ATA_ADDR_LBA48, 396 0, 397 0, 398 0, 399 0, 400 0, 401 1, 402 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 403 0, 404 0, 405 0, 406 SATAC_READ_LOG_EXT, 407 0, 408 0, 409 0, 410 }; 411 412 /* 413 * ATAPI error recovery CDB 414 */ 415 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 416 SCMD_REQUEST_SENSE, 417 0, /* Only fixed RQ format is supported */ 418 0, 419 0, 420 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 421 0 422 }; 423 424 425 /* Warlock directives */ 426 427 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 428 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 429 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 430 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 431 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 432 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 433 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 434 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 435 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 436 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 437 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 438 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 439 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 440 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 441 sata_hba_inst::satahba_scsi_tran)) 442 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 443 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 444 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 445 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 446 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 447 sata_hba_inst::satahba_event_flags)) 448 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 449 sata_cport_info::cport_devp)) 450 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 451 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 452 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 453 sata_cport_info::cport_dev_type)) 454 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 455 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 456 sata_cport_info::cport_state)) 457 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 458 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 459 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 460 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 461 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 462 #ifdef SATA_DEBUG 463 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 464 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 465 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 466 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 467 #endif 468 469 /* End of warlock directives */ 470 471 /* ************** loadable module configuration functions ************** */ 472 473 int 474 _init() 475 { 476 int rval; 477 478 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 479 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 480 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 481 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 482 if ((rval = mod_install(&modlinkage)) != 0) { 483 #ifdef SATA_DEBUG 484 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 485 #endif 486 mutex_destroy(&sata_log_mutex); 487 cv_destroy(&sata_event_cv); 488 mutex_destroy(&sata_event_mutex); 489 mutex_destroy(&sata_mutex); 490 } 491 return (rval); 492 } 493 494 int 495 _fini() 496 { 497 int rval; 498 499 if ((rval = mod_remove(&modlinkage)) != 0) 500 return (rval); 501 502 mutex_destroy(&sata_log_mutex); 503 cv_destroy(&sata_event_cv); 504 mutex_destroy(&sata_event_mutex); 505 mutex_destroy(&sata_mutex); 506 return (rval); 507 } 508 509 int 510 _info(struct modinfo *modinfop) 511 { 512 return (mod_info(&modlinkage, modinfop)); 513 } 514 515 516 517 /* ********************* SATA HBA entry points ********************* */ 518 519 520 /* 521 * Called by SATA HBA from _init(). 522 * Registers HBA driver instance/sata framework pair with scsi framework, by 523 * calling scsi_hba_init(). 524 * 525 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 526 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 527 * cb_ops pointer in SATA HBA driver dev_ops structure. 528 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 529 * 530 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 531 * driver. 532 */ 533 int 534 sata_hba_init(struct modlinkage *modlp) 535 { 536 int rval; 537 struct dev_ops *hba_ops; 538 539 SATADBG1(SATA_DBG_HBA_IF, NULL, 540 "sata_hba_init: name %s \n", 541 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 542 /* 543 * Fill-up cb_ops and dev_ops when necessary 544 */ 545 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 546 /* 547 * Provide pointer to SATA dev_ops 548 */ 549 hba_ops->devo_cb_ops = &sata_cb_ops; 550 551 /* 552 * Register SATA HBA with SCSI framework 553 */ 554 if ((rval = scsi_hba_init(modlp)) != 0) { 555 SATADBG1(SATA_DBG_HBA_IF, NULL, 556 "sata_hba_init: scsi hba init failed\n", NULL); 557 return (rval); 558 } 559 560 return (0); 561 } 562 563 564 /* HBA attach stages */ 565 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 566 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 567 #define HBA_ATTACH_STAGE_SETUP 4 568 #define HBA_ATTACH_STAGE_LINKED 8 569 570 571 /* 572 * 573 * Called from SATA HBA driver's attach routine to attach an instance of 574 * the HBA. 575 * 576 * For DDI_ATTACH command: 577 * sata_hba_inst structure is allocated here and initialized with pointers to 578 * SATA framework implementation of required scsi tran functions. 579 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 580 * to the soft structure (sata_hba_inst) allocated by SATA framework for 581 * SATA HBA instance related data. 582 * The scsi_tran's tran_hba_private field is used by SATA framework to 583 * store a pointer to per-HBA-instance of sata_hba_inst structure. 584 * The sata_hba_inst structure is cross-linked to scsi tran structure. 585 * Among other info, a pointer to sata_hba_tran structure is stored in 586 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 587 * linked together into the list, pointed to by sata_hba_list. 588 * On the first HBA instance attach the sata event thread is initialized. 589 * Attachment points are created for all SATA ports of the HBA being attached. 590 * All HBA instance's SATA ports are probed and type of plugged devices is 591 * determined. For each device of a supported type, a target node is created. 592 * 593 * DDI_SUCCESS is returned when attachment process is successful, 594 * DDI_FAILURE is returned otherwise. 595 * 596 * For DDI_RESUME command: 597 * Not implemented at this time (postponed until phase 2 of the development). 598 */ 599 int 600 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 601 ddi_attach_cmd_t cmd) 602 { 603 sata_hba_inst_t *sata_hba_inst; 604 scsi_hba_tran_t *scsi_tran = NULL; 605 int hba_attach_state = 0; 606 char taskq_name[MAXPATHLEN]; 607 608 SATADBG3(SATA_DBG_HBA_IF, NULL, 609 "sata_hba_attach: node %s (%s%d)\n", 610 ddi_node_name(dip), ddi_driver_name(dip), 611 ddi_get_instance(dip)); 612 613 if (cmd == DDI_RESUME) { 614 /* 615 * Postponed until phase 2 of the development 616 */ 617 return (DDI_FAILURE); 618 } 619 620 if (cmd != DDI_ATTACH) { 621 return (DDI_FAILURE); 622 } 623 624 /* cmd == DDI_ATTACH */ 625 626 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 627 SATA_LOG_D((NULL, CE_WARN, 628 "sata_hba_attach: invalid sata_hba_tran")); 629 return (DDI_FAILURE); 630 } 631 /* 632 * Allocate and initialize SCSI tran structure. 633 * SATA copy of tran_bus_config is provided to create port nodes. 634 */ 635 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 636 if (scsi_tran == NULL) 637 return (DDI_FAILURE); 638 /* 639 * Allocate soft structure for SATA HBA instance. 640 * There is a separate softstate for each HBA instance. 641 */ 642 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 643 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 644 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 645 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 646 647 /* 648 * scsi_trans's tran_hba_private is used by SATA Framework to point to 649 * soft structure allocated by SATA framework for 650 * SATA HBA instance related data. 651 */ 652 scsi_tran->tran_hba_private = sata_hba_inst; 653 scsi_tran->tran_tgt_private = NULL; 654 655 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 656 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 657 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 658 659 scsi_tran->tran_start = sata_scsi_start; 660 scsi_tran->tran_reset = sata_scsi_reset; 661 scsi_tran->tran_abort = sata_scsi_abort; 662 scsi_tran->tran_getcap = sata_scsi_getcap; 663 scsi_tran->tran_setcap = sata_scsi_setcap; 664 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 665 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 666 667 scsi_tran->tran_dmafree = sata_scsi_dmafree; 668 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 669 670 scsi_tran->tran_reset_notify = NULL; 671 scsi_tran->tran_get_bus_addr = NULL; 672 scsi_tran->tran_quiesce = NULL; 673 scsi_tran->tran_unquiesce = NULL; 674 scsi_tran->tran_bus_reset = NULL; 675 676 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 677 scsi_tran, 0) != DDI_SUCCESS) { 678 #ifdef SATA_DEBUG 679 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 680 ddi_driver_name(dip), ddi_get_instance(dip)); 681 #endif 682 goto fail; 683 } 684 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 685 686 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 687 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 688 "sata", 1) != DDI_PROP_SUCCESS) { 689 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 690 "failed to create hba sata prop")); 691 goto fail; 692 } 693 } 694 695 /* 696 * Save pointers in hba instance soft state. 697 */ 698 sata_hba_inst->satahba_scsi_tran = scsi_tran; 699 sata_hba_inst->satahba_tran = sata_tran; 700 sata_hba_inst->satahba_dip = dip; 701 702 /* 703 * Create a task queue to handle emulated commands completion 704 * Use node name, dash, instance number as the queue name. 705 */ 706 taskq_name[0] = '\0'; 707 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 708 sizeof (taskq_name)); 709 (void) snprintf(taskq_name + strlen(taskq_name), 710 sizeof (taskq_name) - strlen(taskq_name), 711 "-%d", DEVI(dip)->devi_instance); 712 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 713 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports, 714 TASKQ_DYNAMIC); 715 716 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 717 718 /* 719 * Create events thread if not created yet. 720 */ 721 sata_event_thread_control(1); 722 723 /* 724 * Link this hba instance into the list. 725 */ 726 mutex_enter(&sata_mutex); 727 728 if (sata_hba_list == NULL) { 729 /* 730 * The first instance of HBA is attached. 731 * Set current/active default maximum NCQ/TCQ queue depth for 732 * all SATA devices. It is done here and now, to eliminate the 733 * possibility of the dynamic, programatic modification of the 734 * queue depth via global (and public) sata_max_queue_depth 735 * variable (this would require special handling in HBA drivers) 736 */ 737 sata_current_max_qdepth = sata_max_queue_depth; 738 if (sata_current_max_qdepth > 32) 739 sata_current_max_qdepth = 32; 740 else if (sata_current_max_qdepth < 1) 741 sata_current_max_qdepth = 1; 742 } 743 744 sata_hba_inst->satahba_next = NULL; 745 sata_hba_inst->satahba_prev = sata_hba_list_tail; 746 if (sata_hba_list == NULL) { 747 sata_hba_list = sata_hba_inst; 748 } 749 if (sata_hba_list_tail != NULL) { 750 sata_hba_list_tail->satahba_next = sata_hba_inst; 751 } 752 sata_hba_list_tail = sata_hba_inst; 753 mutex_exit(&sata_mutex); 754 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 755 756 /* 757 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 758 * SATA HBA driver should not use its own open/close entry points. 759 * 760 * Make sure that instance number doesn't overflow 761 * when forming minor numbers. 762 */ 763 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 764 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 765 INST2DEVCTL(ddi_get_instance(dip)), 766 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 767 #ifdef SATA_DEBUG 768 cmn_err(CE_WARN, "sata_hba_attach: " 769 "cannot create devctl minor node"); 770 #endif 771 goto fail; 772 } 773 774 775 /* 776 * Set-up kstats here, if necessary. 777 * (postponed until phase 2 of the development). 778 */ 779 780 781 /* 782 * Probe controller ports. This operation will describe a current 783 * controller/port/multipliers/device configuration and will create 784 * attachment points. 785 * We may end-up with just a controller with no devices attached. 786 * For the ports with a supported device attached, device target nodes 787 * are created and devices are initialized. 788 */ 789 sata_probe_ports(sata_hba_inst); 790 791 sata_hba_inst->satahba_attached = 1; 792 return (DDI_SUCCESS); 793 794 fail: 795 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 796 (void) sata_remove_hba_instance(dip); 797 if (sata_hba_list == NULL) 798 sata_event_thread_control(0); 799 } 800 801 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 802 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 803 taskq_destroy(sata_hba_inst->satahba_taskq); 804 } 805 806 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 807 (void) scsi_hba_detach(dip); 808 809 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 810 mutex_destroy(&sata_hba_inst->satahba_mutex); 811 kmem_free((void *)sata_hba_inst, 812 sizeof (struct sata_hba_inst)); 813 scsi_hba_tran_free(scsi_tran); 814 } 815 816 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 817 ddi_driver_name(dip), ddi_get_instance(dip)); 818 819 return (DDI_FAILURE); 820 } 821 822 823 /* 824 * Called by SATA HBA from to detach an instance of the driver. 825 * 826 * For DDI_DETACH command: 827 * Free local structures allocated for SATA HBA instance during 828 * sata_hba_attach processing. 829 * 830 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 831 * 832 * For DDI_SUSPEND command: 833 * Not implemented at this time (postponed until phase 2 of the development) 834 * Returnd DDI_SUCCESS. 835 * 836 * When the last HBA instance is detached, the event daemon is terminated. 837 * 838 * NOTE: cport support only, no port multiplier support. 839 */ 840 int 841 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 842 { 843 dev_info_t *tdip; 844 sata_hba_inst_t *sata_hba_inst; 845 scsi_hba_tran_t *scsi_hba_tran; 846 sata_cport_info_t *cportinfo; 847 sata_drive_info_t *sdinfo; 848 int ncport; 849 850 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 851 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 852 853 switch (cmd) { 854 case DDI_DETACH: 855 856 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 857 return (DDI_FAILURE); 858 859 sata_hba_inst = scsi_hba_tran->tran_hba_private; 860 if (sata_hba_inst == NULL) 861 return (DDI_FAILURE); 862 863 if (scsi_hba_detach(dip) == DDI_FAILURE) { 864 sata_hba_inst->satahba_attached = 1; 865 return (DDI_FAILURE); 866 } 867 868 /* 869 * Free all target nodes - at this point 870 * devices should be at least offlined 871 * otherwise scsi_hba_detach() should not be called. 872 */ 873 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 874 ncport++) { 875 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 876 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 877 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 878 if (sdinfo != NULL) { 879 tdip = sata_get_target_dip(dip, 880 ncport); 881 if (tdip != NULL) { 882 if (ndi_devi_offline(tdip, 883 NDI_DEVI_REMOVE) != 884 NDI_SUCCESS) { 885 SATA_LOG_D(( 886 sata_hba_inst, 887 CE_WARN, 888 "sata_hba_detach: " 889 "Target node not " 890 "removed !")); 891 return (DDI_FAILURE); 892 } 893 } 894 } 895 } 896 } 897 /* 898 * Disable sata event daemon processing for this HBA 899 */ 900 sata_hba_inst->satahba_attached = 0; 901 902 /* 903 * Remove event daemon thread, if it is last HBA instance. 904 */ 905 906 mutex_enter(&sata_mutex); 907 if (sata_hba_list->satahba_next == NULL) { 908 mutex_exit(&sata_mutex); 909 sata_event_thread_control(0); 910 mutex_enter(&sata_mutex); 911 } 912 mutex_exit(&sata_mutex); 913 914 /* Remove this HBA instance from the HBA list */ 915 sata_remove_hba_instance(dip); 916 917 /* 918 * At this point there should be no target nodes attached. 919 * Detach and destroy device and port info structures. 920 */ 921 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 922 ncport++) { 923 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 924 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 925 sdinfo = 926 cportinfo->cport_devp.cport_sata_drive; 927 if (sdinfo != NULL) { 928 /* Release device structure */ 929 kmem_free(sdinfo, 930 sizeof (sata_drive_info_t)); 931 } 932 /* Release cport info */ 933 mutex_destroy(&cportinfo->cport_mutex); 934 kmem_free(cportinfo, 935 sizeof (sata_cport_info_t)); 936 } 937 } 938 939 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 940 941 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 942 943 taskq_destroy(sata_hba_inst->satahba_taskq); 944 945 mutex_destroy(&sata_hba_inst->satahba_mutex); 946 kmem_free((void *)sata_hba_inst, 947 sizeof (struct sata_hba_inst)); 948 949 return (DDI_SUCCESS); 950 951 case DDI_SUSPEND: 952 /* 953 * Postponed until phase 2 954 */ 955 return (DDI_FAILURE); 956 957 default: 958 return (DDI_FAILURE); 959 } 960 } 961 962 963 /* 964 * Called by an HBA drive from _fini() routine. 965 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 966 */ 967 void 968 sata_hba_fini(struct modlinkage *modlp) 969 { 970 SATADBG1(SATA_DBG_HBA_IF, NULL, 971 "sata_hba_fini: name %s\n", 972 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 973 974 scsi_hba_fini(modlp); 975 } 976 977 978 /* 979 * Default open and close routine for sata_hba framework. 980 * 981 */ 982 /* 983 * Open devctl node. 984 * 985 * Returns: 986 * 0 if node was open successfully, error code otherwise. 987 * 988 * 989 */ 990 991 static int 992 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 993 { 994 #ifndef __lock_lint 995 _NOTE(ARGUNUSED(credp)) 996 #endif 997 int rv = 0; 998 dev_info_t *dip; 999 scsi_hba_tran_t *scsi_hba_tran; 1000 sata_hba_inst_t *sata_hba_inst; 1001 1002 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1003 1004 if (otyp != OTYP_CHR) 1005 return (EINVAL); 1006 1007 dip = sata_devt_to_devinfo(*devp); 1008 if (dip == NULL) 1009 return (ENXIO); 1010 1011 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1012 return (ENXIO); 1013 1014 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1015 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1016 return (ENXIO); 1017 1018 mutex_enter(&sata_mutex); 1019 if (flags & FEXCL) { 1020 if (sata_hba_inst->satahba_open_flag != 0) { 1021 rv = EBUSY; 1022 } else { 1023 sata_hba_inst->satahba_open_flag = 1024 SATA_DEVCTL_EXOPENED; 1025 } 1026 } else { 1027 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1028 rv = EBUSY; 1029 } else { 1030 sata_hba_inst->satahba_open_flag = 1031 SATA_DEVCTL_SOPENED; 1032 } 1033 } 1034 mutex_exit(&sata_mutex); 1035 1036 return (rv); 1037 } 1038 1039 1040 /* 1041 * Close devctl node. 1042 * Returns: 1043 * 0 if node was closed successfully, error code otherwise. 1044 * 1045 */ 1046 1047 static int 1048 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1049 { 1050 #ifndef __lock_lint 1051 _NOTE(ARGUNUSED(credp)) 1052 _NOTE(ARGUNUSED(flag)) 1053 #endif 1054 dev_info_t *dip; 1055 scsi_hba_tran_t *scsi_hba_tran; 1056 sata_hba_inst_t *sata_hba_inst; 1057 1058 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1059 1060 if (otyp != OTYP_CHR) 1061 return (EINVAL); 1062 1063 dip = sata_devt_to_devinfo(dev); 1064 if (dip == NULL) 1065 return (ENXIO); 1066 1067 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1068 return (ENXIO); 1069 1070 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1071 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1072 return (ENXIO); 1073 1074 mutex_enter(&sata_mutex); 1075 sata_hba_inst->satahba_open_flag = 0; 1076 mutex_exit(&sata_mutex); 1077 return (0); 1078 } 1079 1080 1081 1082 /* 1083 * Standard IOCTL commands for SATA hotplugging. 1084 * Implemented DEVCTL_AP commands: 1085 * DEVCTL_AP_CONNECT 1086 * DEVCTL_AP_DISCONNECT 1087 * DEVCTL_AP_CONFIGURE 1088 * DEVCTL_UNCONFIGURE 1089 * DEVCTL_AP_CONTROL 1090 * 1091 * Commands passed to default ndi ioctl handler: 1092 * DEVCTL_DEVICE_GETSTATE 1093 * DEVCTL_DEVICE_ONLINE 1094 * DEVCTL_DEVICE_OFFLINE 1095 * DEVCTL_DEVICE_REMOVE 1096 * DEVCTL_DEVICE_INSERT 1097 * DEVCTL_BUS_GETSTATE 1098 * 1099 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1100 * if not. 1101 * 1102 * Returns: 1103 * 0 if successful, 1104 * error code if operation failed. 1105 * 1106 * NOTE: Port Multiplier is not supported. 1107 * 1108 */ 1109 1110 static int 1111 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1112 int *rvalp) 1113 { 1114 #ifndef __lock_lint 1115 _NOTE(ARGUNUSED(credp)) 1116 _NOTE(ARGUNUSED(rvalp)) 1117 #endif 1118 int rv = 0; 1119 int32_t comp_port = -1; 1120 dev_info_t *dip, *tdip; 1121 devctl_ap_state_t ap_state; 1122 struct devctl_iocdata *dcp = NULL; 1123 scsi_hba_tran_t *scsi_hba_tran; 1124 sata_hba_inst_t *sata_hba_inst; 1125 sata_device_t sata_device; 1126 sata_drive_info_t *sdinfo; 1127 sata_cport_info_t *cportinfo; 1128 int cport, pmport, qual; 1129 int rval = SATA_SUCCESS; 1130 1131 dip = sata_devt_to_devinfo(dev); 1132 if (dip == NULL) 1133 return (ENXIO); 1134 1135 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1136 return (ENXIO); 1137 1138 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1139 if (sata_hba_inst == NULL) 1140 return (ENXIO); 1141 1142 if (sata_hba_inst->satahba_tran == NULL) 1143 return (ENXIO); 1144 1145 switch (cmd) { 1146 1147 case DEVCTL_DEVICE_GETSTATE: 1148 case DEVCTL_DEVICE_ONLINE: 1149 case DEVCTL_DEVICE_OFFLINE: 1150 case DEVCTL_DEVICE_REMOVE: 1151 case DEVCTL_BUS_GETSTATE: 1152 /* 1153 * There may be more cases that we want to pass to default 1154 * handler rather than fail them. 1155 */ 1156 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1157 } 1158 1159 /* read devctl ioctl data */ 1160 if (cmd != DEVCTL_AP_CONTROL) { 1161 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1162 return (EFAULT); 1163 1164 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1165 -1) { 1166 if (dcp) 1167 ndi_dc_freehdl(dcp); 1168 return (EINVAL); 1169 } 1170 1171 cport = SCSI_TO_SATA_CPORT(comp_port); 1172 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1173 /* Only cport is considered now, i.e. SATA_ADDR_CPORT */ 1174 qual = SATA_ADDR_CPORT; 1175 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1176 qual) != 0) { 1177 ndi_dc_freehdl(dcp); 1178 return (EINVAL); 1179 } 1180 1181 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1182 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1183 cport_mutex); 1184 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1185 /* 1186 * Cannot process ioctl request now. Come back later. 1187 */ 1188 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1189 cport_mutex); 1190 ndi_dc_freehdl(dcp); 1191 return (EBUSY); 1192 } 1193 /* Block event processing for this port */ 1194 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1195 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1196 1197 sata_device.satadev_addr.cport = cport; 1198 sata_device.satadev_addr.pmport = pmport; 1199 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1200 sata_device.satadev_rev = SATA_DEVICE_REV; 1201 } 1202 1203 switch (cmd) { 1204 1205 case DEVCTL_AP_DISCONNECT: 1206 /* 1207 * Normally, cfgadm sata plugin will try to offline 1208 * (unconfigure) device before this request. Nevertheless, 1209 * if a device is still configured, we need to 1210 * attempt to offline and unconfigure device first, and we will 1211 * deactivate the port regardless of the unconfigure 1212 * operation results. 1213 * 1214 * DEVCTL_AP_DISCONNECT invokes 1215 * sata_hba_inst->satahba_tran-> 1216 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 1217 * If successful, the device structure (if any) attached 1218 * to a port is removed and state of the port marked 1219 * appropriately. 1220 * Failure of the port_deactivate may keep port in 1221 * the active state, or may fail the port. 1222 */ 1223 1224 /* Check the current state of the port */ 1225 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 1226 (dip, &sata_device); 1227 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1228 cport_mutex); 1229 sata_update_port_info(sata_hba_inst, &sata_device); 1230 if (rval != SATA_SUCCESS || 1231 (sata_device.satadev_state & SATA_PSTATE_FAILED)) { 1232 cportinfo->cport_state = SATA_PSTATE_FAILED; 1233 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1234 cport_mutex); 1235 rv = EIO; 1236 break; 1237 } 1238 /* Sanity check */ 1239 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 1240 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1241 cport_mutex); 1242 /* No physical port deactivation supported. */ 1243 break; 1244 } 1245 1246 /* 1247 * set port's dev_state to not ready - this will disable 1248 * an access to an attached device. 1249 */ 1250 cportinfo->cport_state &= ~SATA_STATE_READY; 1251 1252 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1253 sdinfo = cportinfo->cport_devp.cport_sata_drive; 1254 ASSERT(sdinfo != NULL); 1255 if ((sdinfo->satadrv_type & 1256 (SATA_VALID_DEV_TYPE))) { 1257 /* 1258 * If a target node exists, try to offline 1259 * a device and remove target node. 1260 */ 1261 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1262 cport)->cport_mutex); 1263 tdip = sata_get_target_dip(dip, comp_port); 1264 if (tdip != NULL && ndi_devi_offline(tdip, 1265 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 1266 /* 1267 * Problem 1268 * A target node remained 1269 * attached. This happens when 1270 * the file was open or a node 1271 * was waiting for resources. 1272 * Cannot do anything about it. 1273 */ 1274 SATA_LOG_D((sata_hba_inst, CE_WARN, 1275 "sata_hba_ioctl: " 1276 "disconnect: could not " 1277 "unconfigure device before " 1278 "disconnecting the SATA " 1279 "port %d", cport)); 1280 1281 /* 1282 * Set DEVICE REMOVED state 1283 * in the target node. It 1284 * will prevent access to 1285 * the device even when a 1286 * new device is attached, 1287 * until the old target node 1288 * is released, removed and 1289 * recreated for a new 1290 * device. 1291 */ 1292 sata_set_device_removed(tdip); 1293 /* 1294 * Instruct event daemon to 1295 * try the target node cleanup 1296 * later. 1297 */ 1298 sata_set_target_node_cleanup( 1299 sata_hba_inst, cport); 1300 } 1301 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1302 cport)->cport_mutex); 1303 /* 1304 * Remove and release sata_drive_info 1305 * structure. 1306 */ 1307 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != 1308 NULL) { 1309 SATA_CPORTINFO_DRV_INFO(cportinfo) = 1310 NULL; 1311 (void) kmem_free((void *)sdinfo, 1312 sizeof (sata_drive_info_t)); 1313 cportinfo->cport_dev_type = 1314 SATA_DTYPE_NONE; 1315 } 1316 } 1317 /* 1318 * Note: PMult info requires different handling. 1319 * Put PMult handling code here, when PMult is 1320 * supported. 1321 */ 1322 1323 } 1324 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1325 /* Just ask HBA driver to deactivate port */ 1326 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1327 1328 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 1329 (dip, &sata_device); 1330 1331 /* 1332 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1333 * without the hint. 1334 */ 1335 sata_gen_sysevent(sata_hba_inst, 1336 &sata_device.satadev_addr, SE_NO_HINT); 1337 1338 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1339 cport_mutex); 1340 sata_update_port_info(sata_hba_inst, &sata_device); 1341 1342 if (rval != SATA_SUCCESS) { 1343 /* 1344 * Port deactivation failure - do not 1345 * change port state unless the state 1346 * returned by HBA indicates a port failure. 1347 */ 1348 if (sata_device.satadev_state & SATA_PSTATE_FAILED) 1349 cportinfo->cport_state = SATA_PSTATE_FAILED; 1350 rv = EIO; 1351 } else { 1352 /* 1353 * Deactivation succeded. From now on the framework 1354 * will not know what is happening to the device, until 1355 * the port is activated again. 1356 */ 1357 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 1358 } 1359 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1360 break; 1361 1362 case DEVCTL_AP_UNCONFIGURE: 1363 1364 /* 1365 * The unconfigure operation uses generic nexus operation to 1366 * offline a device. It leaves a target device node attached. 1367 * and obviously sata_drive_info attached as well, because 1368 * from the hardware point of view nothing has changed. 1369 */ 1370 if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) { 1371 1372 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != 1373 NDI_SUCCESS) { 1374 SATA_LOG_D((sata_hba_inst, CE_WARN, 1375 "sata_hba_ioctl: unconfigure: " 1376 "failed to unconfigure " 1377 "device at SATA port %d", cport)); 1378 rv = EIO; 1379 } 1380 /* 1381 * The target node devi_state should be marked with 1382 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 1383 * This would be the indication for cfgadm that 1384 * the AP node occupant state is 'unconfigured'. 1385 */ 1386 1387 } else { 1388 /* 1389 * This would indicate a failure on the part of cfgadm 1390 * to detect correct state of the node prior to this 1391 * call - one cannot unconfigure non-existing device. 1392 */ 1393 SATA_LOG_D((sata_hba_inst, CE_WARN, 1394 "sata_hba_ioctl: unconfigure: " 1395 "attempt to unconfigure non-existing device " 1396 "at SATA port %d", cport)); 1397 rv = ENXIO; 1398 } 1399 1400 break; 1401 1402 case DEVCTL_AP_CONNECT: 1403 { 1404 /* 1405 * The sata cfgadm pluging will invoke this operation only if 1406 * port was found in the disconnect state (failed state 1407 * is also treated as the disconnected state). 1408 * DEVCTL_AP_CONNECT would invoke 1409 * sata_hba_inst->satahba_tran-> 1410 * sata_tran_hotplug_ops->sata_tran_port_activate(). 1411 * If successful and a device is found attached to the port, 1412 * the initialization sequence is executed to attach 1413 * a device structure to a port structure. The device is not 1414 * set in configured state (system-wise) by this operation. 1415 * The state of the port and a device would be set 1416 * appropriately. 1417 * 1418 * Note, that activating the port may generate link events, 1419 * so is is important that following processing and the 1420 * event processing does not interfere with each other! 1421 * 1422 * This operation may remove port failed state and will 1423 * try to make port active and in good standing. 1424 */ 1425 1426 /* We only care about host sata cport for now */ 1427 1428 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) { 1429 /* Just let HBA driver to activate port */ 1430 1431 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 1432 (dip, &sata_device) != SATA_SUCCESS) { 1433 /* 1434 * Port activation failure. 1435 */ 1436 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1437 cport)->cport_mutex); 1438 sata_update_port_info(sata_hba_inst, 1439 &sata_device); 1440 if (sata_device.satadev_state & 1441 SATA_PSTATE_FAILED) { 1442 cportinfo->cport_state = 1443 SATA_PSTATE_FAILED; 1444 } 1445 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1446 cport)->cport_mutex); 1447 SATA_LOG_D((sata_hba_inst, CE_WARN, 1448 "sata_hba_ioctl: connect: " 1449 "failed to activate SATA port %d", 1450 cport)); 1451 rv = EIO; 1452 break; 1453 } 1454 } 1455 /* Virgin port state - will be updated by the port re-probe. */ 1456 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1457 cport)->cport_mutex); 1458 cportinfo->cport_state = 0; 1459 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1460 cport)->cport_mutex); 1461 1462 /* 1463 * Probe the port to find its state and attached device. 1464 */ 1465 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1466 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 1467 rv = EIO; 1468 /* 1469 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1470 * without the hint 1471 */ 1472 sata_gen_sysevent(sata_hba_inst, 1473 &sata_device.satadev_addr, SE_NO_HINT); 1474 /* 1475 * If there is a device attached to the port, emit 1476 * a message. 1477 */ 1478 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1479 sata_log(sata_hba_inst, CE_WARN, 1480 "SATA device detected at port %d", cport); 1481 } 1482 break; 1483 } 1484 1485 case DEVCTL_AP_CONFIGURE: 1486 { 1487 boolean_t target = TRUE; 1488 1489 /* 1490 * A port may be in an active or shutdown state. 1491 * If port is in a failed state, operation is aborted - one 1492 * has to use explicit connect or port activate request 1493 * to try to get a port into non-failed mode. 1494 * 1495 * If a port is in a shutdown state, arbitrarily invoke 1496 * sata_tran_port_activate() prior to any other operation. 1497 * 1498 * Verify that port state is READY and there is a device 1499 * of a supported type attached to this port. 1500 * If target node exists, a device was most likely offlined. 1501 * If target node does not exist, create a target node an 1502 * attempt to online it. 1503 * * 1504 * NO PMult or devices beyond PMult are supported yet. 1505 */ 1506 1507 /* We only care about host controller's sata cport for now. */ 1508 if (cportinfo->cport_state & SATA_PSTATE_FAILED) { 1509 rv = ENXIO; 1510 break; 1511 } 1512 /* Check the current state of the port */ 1513 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1514 1515 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 1516 (dip, &sata_device); 1517 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1518 cport_mutex); 1519 sata_update_port_info(sata_hba_inst, &sata_device); 1520 if (rval != SATA_SUCCESS || 1521 (sata_device.satadev_state & SATA_PSTATE_FAILED)) { 1522 cportinfo->cport_state = SATA_PSTATE_FAILED; 1523 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1524 cport_mutex); 1525 rv = EIO; 1526 break; 1527 } 1528 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN) { 1529 target = FALSE; 1530 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1531 cport_mutex); 1532 1533 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) { 1534 /* Just let HBA driver to activate port */ 1535 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 1536 (dip, &sata_device) != SATA_SUCCESS) { 1537 /* 1538 * Port activation failure - do not 1539 * change port state unless the state 1540 * returned by HBA indicates a port 1541 * failure. 1542 */ 1543 mutex_enter(&SATA_CPORT_INFO( 1544 sata_hba_inst, cport)->cport_mutex); 1545 sata_update_port_info(sata_hba_inst, 1546 &sata_device); 1547 if (sata_device.satadev_state & 1548 SATA_PSTATE_FAILED) { 1549 cportinfo->cport_state = 1550 SATA_PSTATE_FAILED; 1551 } 1552 mutex_exit(&SATA_CPORT_INFO( 1553 sata_hba_inst, cport)->cport_mutex); 1554 SATA_LOG_D((sata_hba_inst, CE_WARN, 1555 "sata_hba_ioctl: configure: " 1556 "failed to activate SATA port %d", 1557 cport)); 1558 rv = EIO; 1559 break; 1560 } 1561 } 1562 /* 1563 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1564 * without the hint. 1565 */ 1566 sata_gen_sysevent(sata_hba_inst, 1567 &sata_device.satadev_addr, SE_NO_HINT); 1568 1569 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1570 cport_mutex); 1571 /* Virgin port state */ 1572 cportinfo->cport_state = 0; 1573 } 1574 /* 1575 * Always reprobe port, to get current device info. 1576 */ 1577 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1578 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1579 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 1580 rv = EIO; 1581 break; 1582 } 1583 if (target == FALSE && 1584 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1585 /* 1586 * That's the transition from "inactive" port 1587 * to active one with device attached. 1588 */ 1589 sata_log(sata_hba_inst, CE_WARN, 1590 "SATA device detected at port %d", 1591 cport); 1592 } 1593 1594 /* 1595 * This is where real configure starts. 1596 * Change following check for PMult support. 1597 */ 1598 if (!(sata_device.satadev_type & SATA_VALID_DEV_TYPE)) { 1599 /* No device to configure */ 1600 rv = ENXIO; /* No device to configure */ 1601 break; 1602 } 1603 1604 /* 1605 * Here we may have a device in reset condition, 1606 * but because we are just configuring it, there is 1607 * no need to process the reset other than just 1608 * to clear device reset condition in the HBA driver. 1609 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 1610 * cause a first command sent the HBA driver with the request 1611 * to clear device reset condition. 1612 */ 1613 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1614 cport_mutex); 1615 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1616 if (sdinfo == NULL) { 1617 rv = ENXIO; 1618 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1619 cport_mutex); 1620 break; 1621 } 1622 if (sdinfo->satadrv_event_flags & 1623 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) 1624 sdinfo->satadrv_event_flags = 0; 1625 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 1626 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1627 1628 if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) { 1629 /* 1630 * Target node exists. Verify, that it belongs 1631 * to existing, attached device and not to 1632 * a removed device. 1633 */ 1634 if (sata_check_device_removed(tdip) == B_FALSE) { 1635 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 1636 SATA_LOG_D((sata_hba_inst, CE_WARN, 1637 "sata_hba_ioctl: configure: " 1638 "onlining device at SATA port %d " 1639 "failed", cport)); 1640 rv = EIO; 1641 break; 1642 } else { 1643 mutex_enter(&SATA_CPORT_INFO( 1644 sata_hba_inst, cport)->cport_mutex); 1645 SATA_CPORT_INFO(sata_hba_inst, cport)-> 1646 cport_tgtnode_clean = B_TRUE; 1647 mutex_exit(&SATA_CPORT_INFO( 1648 sata_hba_inst, cport)->cport_mutex); 1649 } 1650 } else { 1651 sata_log(sata_hba_inst, CE_WARN, 1652 "SATA device at port %d cannot be " 1653 "configured. " 1654 "Application(s) accessing previously " 1655 "attached device " 1656 "have to release it before newly inserted " 1657 "device can be made accessible.", 1658 cport); 1659 break; 1660 } 1661 } else { 1662 /* 1663 * No target node - need to create a new target node. 1664 */ 1665 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1666 cport_mutex); 1667 SATA_CPORT_INFO(sata_hba_inst, cport)-> 1668 cport_tgtnode_clean = B_TRUE; 1669 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1670 cport_mutex); 1671 tdip = sata_create_target_node(dip, sata_hba_inst, 1672 &sata_device.satadev_addr); 1673 if (tdip == NULL) { 1674 /* configure failed */ 1675 SATA_LOG_D((sata_hba_inst, CE_WARN, 1676 "sata_hba_ioctl: configure: " 1677 "configuring SATA device at port %d " 1678 "failed", cport)); 1679 rv = EIO; 1680 break; 1681 } 1682 } 1683 1684 break; 1685 } 1686 1687 case DEVCTL_AP_GETSTATE: 1688 1689 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1690 1691 ap_state.ap_last_change = (time_t)-1; 1692 ap_state.ap_error_code = 0; 1693 ap_state.ap_in_transition = 0; 1694 1695 /* Copy the return AP-state information to the user space */ 1696 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1697 rv = EFAULT; 1698 } 1699 break; 1700 1701 case DEVCTL_AP_CONTROL: 1702 { 1703 /* 1704 * Generic devctl for hardware specific functionality 1705 */ 1706 sata_ioctl_data_t ioc; 1707 1708 ASSERT(dcp == NULL); 1709 1710 /* Copy in user ioctl data first */ 1711 #ifdef _MULTI_DATAMODEL 1712 if (ddi_model_convert_from(mode & FMODELS) == 1713 DDI_MODEL_ILP32) { 1714 1715 sata_ioctl_data_32_t ioc32; 1716 1717 if (ddi_copyin((void *)arg, (void *)&ioc32, 1718 sizeof (ioc32), mode) != 0) { 1719 rv = EFAULT; 1720 break; 1721 } 1722 ioc.cmd = (uint_t)ioc32.cmd; 1723 ioc.port = (uint_t)ioc32.port; 1724 ioc.get_size = (uint_t)ioc32.get_size; 1725 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1726 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1727 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1728 } else 1729 #endif /* _MULTI_DATAMODEL */ 1730 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1731 mode) != 0) { 1732 return (EFAULT); 1733 } 1734 1735 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1736 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1737 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1738 1739 /* 1740 * To avoid BE/LE and 32/64 issues, a get_size always returns 1741 * a 32-bit number. 1742 */ 1743 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1744 return (EINVAL); 1745 } 1746 /* validate address */ 1747 cport = SCSI_TO_SATA_CPORT(ioc.port); 1748 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1749 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1750 1751 /* Override address qualifier - handle cport only for now */ 1752 qual = SATA_ADDR_CPORT; 1753 1754 if (sata_validate_sata_address(sata_hba_inst, cport, 1755 pmport, qual) != 0) 1756 return (EINVAL); 1757 1758 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1759 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1760 cport_mutex); 1761 /* Is the port locked by event processing daemon ? */ 1762 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1763 /* 1764 * Cannot process ioctl request now. Come back later 1765 */ 1766 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1767 cport_mutex); 1768 return (EBUSY); 1769 } 1770 /* Block event processing for this port */ 1771 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1772 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1773 1774 1775 sata_device.satadev_addr.cport = cport; 1776 sata_device.satadev_addr.pmport = pmport; 1777 sata_device.satadev_rev = SATA_DEVICE_REV; 1778 1779 switch (ioc.cmd) { 1780 1781 case SATA_CFGA_RESET_PORT: 1782 /* 1783 * There is no protection here for configured 1784 * device. 1785 */ 1786 1787 /* Sanity check */ 1788 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1789 SATA_LOG_D((sata_hba_inst, CE_WARN, 1790 "sata_hba_ioctl: " 1791 "sata_hba_tran missing required " 1792 "function sata_tran_reset_dport")); 1793 rv = EINVAL; 1794 break; 1795 } 1796 1797 /* handle cport only for now */ 1798 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1799 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1800 (dip, &sata_device) != SATA_SUCCESS) { 1801 SATA_LOG_D((sata_hba_inst, CE_WARN, 1802 "sata_hba_ioctl: reset port: " 1803 "failed cport %d pmport %d", 1804 cport, pmport)); 1805 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1806 cport)->cport_mutex); 1807 sata_update_port_info(sata_hba_inst, 1808 &sata_device); 1809 SATA_CPORT_STATE(sata_hba_inst, cport) = 1810 SATA_PSTATE_FAILED; 1811 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1812 cport)->cport_mutex); 1813 rv = EIO; 1814 } 1815 /* 1816 * Since the port was reset, it should be probed and 1817 * attached device reinitialized. At this point the 1818 * port state is unknown - it's state is HBA-specific. 1819 * Re-probe port to get its state. 1820 */ 1821 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1822 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 1823 rv = EIO; 1824 break; 1825 } 1826 break; 1827 1828 case SATA_CFGA_RESET_DEVICE: 1829 /* 1830 * There is no protection here for configured 1831 * device. 1832 */ 1833 1834 /* Sanity check */ 1835 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1836 SATA_LOG_D((sata_hba_inst, CE_WARN, 1837 "sata_hba_ioctl: " 1838 "sata_hba_tran missing required " 1839 "function sata_tran_reset_dport")); 1840 rv = EINVAL; 1841 break; 1842 } 1843 1844 /* handle only device attached to cports, for now */ 1845 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1846 1847 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1848 cport_mutex); 1849 sdinfo = sata_get_device_info(sata_hba_inst, 1850 &sata_device); 1851 if (sdinfo == NULL) { 1852 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1853 cport)->cport_mutex); 1854 rv = EINVAL; 1855 break; 1856 } 1857 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1858 cport_mutex); 1859 1860 /* only handle cport for now */ 1861 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1862 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1863 (dip, &sata_device) != SATA_SUCCESS) { 1864 SATA_LOG_D((sata_hba_inst, CE_WARN, 1865 "sata_hba_ioctl: reset device: failed " 1866 "cport %d pmport %d", cport, pmport)); 1867 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1868 cport)->cport_mutex); 1869 sata_update_port_info(sata_hba_inst, 1870 &sata_device); 1871 /* 1872 * Device info structure remains 1873 * attached. Another device reset or 1874 * port disconnect/connect and re-probing is 1875 * needed to change it's state 1876 */ 1877 sdinfo->satadrv_state &= ~SATA_STATE_READY; 1878 sdinfo->satadrv_state |= 1879 SATA_DSTATE_FAILED; 1880 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1881 cport)->cport_mutex); 1882 rv = EIO; 1883 } 1884 /* 1885 * Since the device was reset, we expect reset event 1886 * to be reported and processed. 1887 */ 1888 break; 1889 1890 case SATA_CFGA_RESET_ALL: 1891 { 1892 int tcport; 1893 1894 /* 1895 * There is no protection here for configured 1896 * devices. 1897 */ 1898 /* Sanity check */ 1899 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1900 SATA_LOG_D((sata_hba_inst, CE_WARN, 1901 "sata_hba_ioctl: " 1902 "sata_hba_tran missing required " 1903 "function sata_tran_reset_dport")); 1904 rv = EINVAL; 1905 break; 1906 } 1907 1908 /* 1909 * Need to lock all ports, not just one. 1910 * If any port is locked by event processing, fail 1911 * the whole operation. 1912 * One port is already locked, but for simplicity 1913 * lock it again. 1914 */ 1915 for (tcport = 0; 1916 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1917 tcport++) { 1918 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1919 tcport)->cport_mutex); 1920 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 1921 cport_event_flags) & 1922 SATA_EVNT_LOCK_PORT_BUSY) != 0) { 1923 rv = EBUSY; 1924 mutex_exit( 1925 &SATA_CPORT_INFO(sata_hba_inst, 1926 tcport)->cport_mutex); 1927 break; 1928 } else { 1929 SATA_CPORT_INFO(sata_hba_inst, 1930 tcport)->cport_event_flags |= 1931 SATA_APCTL_LOCK_PORT_BUSY; 1932 } 1933 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1934 tcport)->cport_mutex); 1935 } 1936 1937 if (rv == 0) { 1938 /* 1939 * All cports successfully locked. 1940 * Reset main SATA controller only for now - 1941 * no PMult. 1942 */ 1943 sata_device.satadev_addr.qual = 1944 SATA_ADDR_CNTRL; 1945 1946 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1947 (dip, &sata_device) != SATA_SUCCESS) { 1948 SATA_LOG_D((sata_hba_inst, CE_WARN, 1949 "sata_hba_ioctl: reset controller " 1950 "failed")); 1951 rv = EIO; 1952 } 1953 1954 /* 1955 * Since ports were reset, they should be 1956 * re-probed and attached devices 1957 * reinitialized. 1958 * At this point port states are unknown, 1959 * Re-probe ports to get their state - 1960 * cports only for now. 1961 */ 1962 for (tcport = 0; 1963 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1964 tcport++) { 1965 sata_device.satadev_addr.cport = 1966 tcport; 1967 sata_device.satadev_addr.qual = 1968 SATA_ADDR_CPORT; 1969 1970 if (sata_reprobe_port(sata_hba_inst, 1971 &sata_device, 1972 SATA_DEV_IDENTIFY_RETRY) != 1973 SATA_SUCCESS) 1974 rv = EIO; 1975 1976 } 1977 } 1978 /* 1979 * Unlock all ports 1980 */ 1981 for (tcport = 0; 1982 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1983 tcport++) { 1984 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1985 tcport)->cport_mutex); 1986 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 1987 cport_event_flags &= 1988 ~SATA_APCTL_LOCK_PORT_BUSY; 1989 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1990 tcport)->cport_mutex); 1991 } 1992 1993 /* 1994 * This operation returns EFAULT if either reset 1995 * controller failed or a re-probing of any ports 1996 * failed. 1997 * We return here, because common return is for 1998 * a single cport operation. 1999 */ 2000 return (rv); 2001 } 2002 2003 case SATA_CFGA_PORT_DEACTIVATE: 2004 /* Sanity check */ 2005 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 2006 rv = ENOTSUP; 2007 break; 2008 } 2009 /* 2010 * Arbitrarily unconfigure attached device, if any. 2011 * Even if the unconfigure fails, proceed with the 2012 * port deactivation. 2013 */ 2014 2015 /* Handle only device attached to cports, for now */ 2016 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2017 2018 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2019 cport_mutex); 2020 cportinfo->cport_state &= ~SATA_STATE_READY; 2021 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 2022 /* 2023 * Handle only device attached to cports, 2024 * for now 2025 */ 2026 sata_device.satadev_addr.qual = 2027 SATA_ADDR_DCPORT; 2028 sdinfo = sata_get_device_info(sata_hba_inst, 2029 &sata_device); 2030 if (sdinfo != NULL && 2031 (sdinfo->satadrv_type & 2032 SATA_VALID_DEV_TYPE)) { 2033 /* 2034 * If a target node exists, try to 2035 * offline a device and remove target 2036 * node. 2037 */ 2038 mutex_exit(&SATA_CPORT_INFO( 2039 sata_hba_inst, cport)->cport_mutex); 2040 tdip = sata_get_target_dip(dip, cport); 2041 if (tdip != NULL) { 2042 /* target node exist */ 2043 SATADBG1(SATA_DBG_IOCTL_IF, 2044 sata_hba_inst, 2045 "sata_hba_ioctl: " 2046 "port deactivate: " 2047 "target node exists.", 2048 NULL); 2049 2050 if (ndi_devi_offline(tdip, 2051 NDI_DEVI_REMOVE) != 2052 NDI_SUCCESS) { 2053 SATA_LOG_D(( 2054 sata_hba_inst, 2055 CE_WARN, 2056 "sata_hba_ioctl:" 2057 "port deactivate: " 2058 "failed to " 2059 "unconfigure " 2060 "device at port " 2061 "%d before " 2062 "deactivating " 2063 "the port", cport)); 2064 /* 2065 * Set DEVICE REMOVED 2066 * state in the target 2067 * node. It will 2068 * prevent access to 2069 * the device even when 2070 * a new device is 2071 * attached, until the 2072 * old target node is 2073 * released, removed and 2074 * recreated for a new 2075 * device. 2076 */ 2077 sata_set_device_removed 2078 (tdip); 2079 /* 2080 * Instruct event 2081 * daemon to try the 2082 * target node cleanup 2083 * later. 2084 */ 2085 sata_set_target_node_cleanup( 2086 sata_hba_inst, cport); 2087 } 2088 } 2089 mutex_enter(&SATA_CPORT_INFO( 2090 sata_hba_inst, cport)->cport_mutex); 2091 /* 2092 * In any case, 2093 * remove and release sata_drive_info 2094 * structure. 2095 * (cport attached device ony, for now) 2096 */ 2097 SATA_CPORTINFO_DRV_INFO(cportinfo) = 2098 NULL; 2099 (void) kmem_free((void *)sdinfo, 2100 sizeof (sata_drive_info_t)); 2101 cportinfo->cport_dev_type = 2102 SATA_DTYPE_NONE; 2103 } 2104 /* 2105 * Note: PMult info requires different 2106 * handling. This comment is a placeholder for 2107 * a code handling PMult, to be implemented 2108 * in phase 2. 2109 */ 2110 } 2111 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 2112 SATA_STATE_PROBING); 2113 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2114 cport_mutex); 2115 /* handle cport only for now */ 2116 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2117 /* Just let HBA driver to deactivate port */ 2118 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 2119 (dip, &sata_device); 2120 /* 2121 * Generate sysevent - 2122 * EC_DR / ESC_DR_AP_STATE_CHANGE 2123 * without the hint 2124 */ 2125 sata_gen_sysevent(sata_hba_inst, 2126 &sata_device.satadev_addr, SE_NO_HINT); 2127 2128 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2129 cport_mutex); 2130 sata_update_port_info(sata_hba_inst, &sata_device); 2131 if (rval != SATA_SUCCESS) { 2132 /* 2133 * Port deactivation failure - do not 2134 * change port state unless the state 2135 * returned by HBA indicates a port failure. 2136 */ 2137 if (sata_device.satadev_state & 2138 SATA_PSTATE_FAILED) { 2139 SATA_CPORT_STATE(sata_hba_inst, 2140 cport) = SATA_PSTATE_FAILED; 2141 } 2142 SATA_LOG_D((sata_hba_inst, CE_WARN, 2143 "sata_hba_ioctl: port deactivate: " 2144 "cannot deactivate SATA port %d", 2145 cport)); 2146 rv = EIO; 2147 } else { 2148 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 2149 } 2150 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2151 cport_mutex); 2152 2153 break; 2154 2155 case SATA_CFGA_PORT_ACTIVATE: 2156 { 2157 boolean_t dev_existed = TRUE; 2158 2159 /* Sanity check */ 2160 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 2161 rv = ENOTSUP; 2162 break; 2163 } 2164 /* handle cport only for now */ 2165 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 2166 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 2167 dev_existed = FALSE; 2168 2169 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2170 /* Just let HBA driver to activate port */ 2171 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 2172 (dip, &sata_device) != SATA_SUCCESS) { 2173 /* 2174 * Port activation failure - do not 2175 * change port state unless the state 2176 * returned by HBA indicates a port failure. 2177 */ 2178 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 2179 cport)->cport_mutex); 2180 sata_update_port_info(sata_hba_inst, 2181 &sata_device); 2182 if (sata_device.satadev_state & 2183 SATA_PSTATE_FAILED) { 2184 SATA_CPORT_STATE(sata_hba_inst, 2185 cport) = SATA_PSTATE_FAILED; 2186 } 2187 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2188 cport)->cport_mutex); 2189 SATA_LOG_D((sata_hba_inst, CE_WARN, 2190 "sata_hba_ioctl: port activate: " 2191 "cannot activate SATA port %d", 2192 cport)); 2193 rv = EIO; 2194 break; 2195 } 2196 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2197 cport_mutex); 2198 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 2199 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2200 cport_mutex); 2201 2202 /* 2203 * Re-probe port to find its current state and 2204 * possibly attached device. 2205 * Port re-probing may change the cportinfo device 2206 * type if device is found attached. 2207 * If port probing failed, the device type would be 2208 * set to SATA_DTYPE_NONE. 2209 */ 2210 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 2211 SATA_DEV_IDENTIFY_RETRY); 2212 2213 /* 2214 * Generate sysevent - 2215 * EC_DR / ESC_DR_AP_STATE_CHANGE 2216 * without the hint. 2217 */ 2218 sata_gen_sysevent(sata_hba_inst, 2219 &sata_device.satadev_addr, SE_NO_HINT); 2220 2221 if (dev_existed == FALSE && 2222 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 2223 /* 2224 * That's the transition from "inactive" port 2225 * state or active port without a device 2226 * attached to the active port state with 2227 * a device attached. 2228 */ 2229 sata_log(sata_hba_inst, CE_WARN, 2230 "SATA device detected at port %d", cport); 2231 } 2232 2233 break; 2234 } 2235 2236 case SATA_CFGA_PORT_SELF_TEST: 2237 2238 /* Sanity check */ 2239 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) { 2240 rv = ENOTSUP; 2241 break; 2242 } 2243 /* 2244 * There is no protection here for a configured 2245 * device attached to this port. 2246 */ 2247 2248 /* only handle cport for now */ 2249 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2250 2251 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 2252 (dip, &sata_device) != SATA_SUCCESS) { 2253 SATA_LOG_D((sata_hba_inst, CE_WARN, 2254 "sata_hba_ioctl: port selftest: " 2255 "failed cport %d pmport %d", 2256 cport, pmport)); 2257 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 2258 cport)->cport_mutex); 2259 sata_update_port_info(sata_hba_inst, 2260 &sata_device); 2261 SATA_CPORT_STATE(sata_hba_inst, cport) = 2262 SATA_PSTATE_FAILED; 2263 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2264 cport)->cport_mutex); 2265 rv = EIO; 2266 break; 2267 } 2268 /* 2269 * Since the port was reset, it should be probed and 2270 * attached device reinitialized. At this point the 2271 * port state is unknown - it's state is HBA-specific. 2272 * Force port re-probing to get it into a known state. 2273 */ 2274 if (sata_reprobe_port(sata_hba_inst, &sata_device, 2275 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 2276 rv = EIO; 2277 break; 2278 } 2279 break; 2280 2281 case SATA_CFGA_GET_DEVICE_PATH: 2282 { 2283 char path[MAXPATHLEN]; 2284 uint32_t size; 2285 2286 (void) strcpy(path, "/devices"); 2287 if ((tdip = sata_get_target_dip(dip, ioc.port)) == 2288 NULL) { 2289 2290 /* 2291 * No such device. 2292 * If this is a request for a size, do not 2293 * return EINVAL for non-exisiting target, 2294 * because cfgadm will indicate a meaningless 2295 * ioctl failure. 2296 * If this is a real request for a path, 2297 * indicate invalid argument. 2298 */ 2299 if (!ioc.get_size) { 2300 rv = EINVAL; 2301 break; 2302 } 2303 } else { 2304 (void) ddi_pathname(tdip, path + strlen(path)); 2305 } 2306 size = strlen(path) + 1; 2307 2308 if (ioc.get_size) { 2309 if (ddi_copyout((void *)&size, 2310 ioc.buf, ioc.bufsiz, mode) != 0) { 2311 rv = EFAULT; 2312 } 2313 } else { 2314 if (ioc.bufsiz != size) { 2315 rv = EINVAL; 2316 } else if (ddi_copyout((void *)&path, 2317 ioc.buf, ioc.bufsiz, mode) != 0) { 2318 rv = EFAULT; 2319 } 2320 } 2321 break; 2322 } 2323 2324 case SATA_CFGA_GET_AP_TYPE: 2325 { 2326 uint32_t type_len; 2327 const char *ap_type; 2328 2329 /* cport only, no port multiplier support */ 2330 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 2331 case SATA_DTYPE_NONE: 2332 ap_type = "port"; 2333 break; 2334 2335 case SATA_DTYPE_ATADISK: 2336 ap_type = "disk"; 2337 break; 2338 2339 case SATA_DTYPE_ATAPICD: 2340 ap_type = "cd/dvd"; 2341 break; 2342 2343 case SATA_DTYPE_PMULT: 2344 ap_type = "pmult"; 2345 break; 2346 2347 case SATA_DTYPE_UNKNOWN: 2348 ap_type = "unknown"; 2349 break; 2350 2351 default: 2352 ap_type = "unsupported"; 2353 break; 2354 2355 } /* end of dev_type switch */ 2356 2357 type_len = strlen(ap_type) + 1; 2358 2359 if (ioc.get_size) { 2360 if (ddi_copyout((void *)&type_len, 2361 ioc.buf, ioc.bufsiz, mode) != 0) { 2362 rv = EFAULT; 2363 break; 2364 } 2365 } else { 2366 if (ioc.bufsiz != type_len) { 2367 rv = EINVAL; 2368 break; 2369 } 2370 if (ddi_copyout((void *)ap_type, ioc.buf, 2371 ioc.bufsiz, mode) != 0) { 2372 rv = EFAULT; 2373 break; 2374 } 2375 } 2376 2377 break; 2378 } 2379 2380 case SATA_CFGA_GET_MODEL_INFO: 2381 { 2382 uint32_t info_len; 2383 char ap_info[sizeof (sdinfo->satadrv_id.ai_model) + 1]; 2384 2385 /* 2386 * This operation should return to cfgadm the 2387 * device model information string 2388 */ 2389 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2390 cport_mutex); 2391 /* only handle device connected to cport for now */ 2392 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2393 sdinfo = sata_get_device_info(sata_hba_inst, 2394 &sata_device); 2395 if (sdinfo == NULL) { 2396 rv = EINVAL; 2397 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2398 cport)->cport_mutex); 2399 break; 2400 } 2401 bcopy(sdinfo->satadrv_id.ai_model, ap_info, 2402 sizeof (sdinfo->satadrv_id.ai_model)); 2403 swab(ap_info, ap_info, 2404 sizeof (sdinfo->satadrv_id.ai_model)); 2405 ap_info[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 2406 2407 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2408 cport_mutex); 2409 2410 info_len = strlen(ap_info) + 1; 2411 2412 if (ioc.get_size) { 2413 if (ddi_copyout((void *)&info_len, 2414 ioc.buf, ioc.bufsiz, mode) != 0) { 2415 rv = EFAULT; 2416 break; 2417 } 2418 } else { 2419 if (ioc.bufsiz < info_len) { 2420 rv = EINVAL; 2421 break; 2422 } 2423 if (ddi_copyout((void *)ap_info, ioc.buf, 2424 ioc.bufsiz, mode) != 0) { 2425 rv = EFAULT; 2426 break; 2427 } 2428 } 2429 2430 break; 2431 } 2432 2433 case SATA_CFGA_GET_REVFIRMWARE_INFO: 2434 { 2435 uint32_t info_len; 2436 char ap_info[ 2437 sizeof (sdinfo->satadrv_id.ai_fw) + 1]; 2438 2439 /* 2440 * This operation should return to cfgadm the 2441 * device firmware revision information string 2442 */ 2443 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2444 cport_mutex); 2445 /* only handle device connected to cport for now */ 2446 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2447 2448 sdinfo = sata_get_device_info(sata_hba_inst, 2449 &sata_device); 2450 if (sdinfo == NULL) { 2451 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2452 cport)->cport_mutex); 2453 rv = EINVAL; 2454 break; 2455 } 2456 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, 2457 sizeof (sdinfo->satadrv_id.ai_fw)); 2458 swab(ap_info, ap_info, 2459 sizeof (sdinfo->satadrv_id.ai_fw)); 2460 ap_info[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 2461 2462 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2463 cport_mutex); 2464 2465 info_len = strlen(ap_info) + 1; 2466 2467 if (ioc.get_size) { 2468 if (ddi_copyout((void *)&info_len, 2469 ioc.buf, ioc.bufsiz, mode) != 0) { 2470 rv = EFAULT; 2471 break; 2472 } 2473 } else { 2474 if (ioc.bufsiz < info_len) { 2475 rv = EINVAL; 2476 break; 2477 } 2478 if (ddi_copyout((void *)ap_info, ioc.buf, 2479 ioc.bufsiz, mode) != 0) { 2480 rv = EFAULT; 2481 break; 2482 } 2483 } 2484 2485 break; 2486 } 2487 2488 case SATA_CFGA_GET_SERIALNUMBER_INFO: 2489 { 2490 uint32_t info_len; 2491 char ap_info[ 2492 sizeof (sdinfo->satadrv_id.ai_drvser) + 1]; 2493 2494 /* 2495 * This operation should return to cfgadm the 2496 * device serial number information string 2497 */ 2498 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2499 cport_mutex); 2500 /* only handle device connected to cport for now */ 2501 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2502 2503 sdinfo = sata_get_device_info(sata_hba_inst, 2504 &sata_device); 2505 if (sdinfo == NULL) { 2506 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2507 cport)->cport_mutex); 2508 rv = EINVAL; 2509 break; 2510 } 2511 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, 2512 sizeof (sdinfo->satadrv_id.ai_drvser)); 2513 swab(ap_info, ap_info, 2514 sizeof (sdinfo->satadrv_id.ai_drvser)); 2515 ap_info[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 2516 2517 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2518 cport_mutex); 2519 2520 info_len = strlen(ap_info) + 1; 2521 2522 if (ioc.get_size) { 2523 if (ddi_copyout((void *)&info_len, 2524 ioc.buf, ioc.bufsiz, mode) != 0) { 2525 rv = EFAULT; 2526 break; 2527 } 2528 } else { 2529 if (ioc.bufsiz < info_len) { 2530 rv = EINVAL; 2531 break; 2532 } 2533 if (ddi_copyout((void *)ap_info, ioc.buf, 2534 ioc.bufsiz, mode) != 0) { 2535 rv = EFAULT; 2536 break; 2537 } 2538 } 2539 2540 break; 2541 } 2542 2543 default: 2544 rv = EINVAL; 2545 break; 2546 2547 } /* End of DEVCTL_AP_CONTROL cmd switch */ 2548 2549 break; 2550 } 2551 2552 default: 2553 { 2554 /* 2555 * If we got here, we got an IOCTL that SATA HBA Framework 2556 * does not recognize. Pass ioctl to HBA driver, in case 2557 * it could process it. 2558 */ 2559 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 2560 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 2561 2562 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 2563 "IOCTL 0x%2x not supported in SATA framework, " 2564 "passthrough to HBA", cmd); 2565 2566 if (sata_tran->sata_tran_ioctl == NULL) { 2567 rv = EINVAL; 2568 break; 2569 } 2570 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 2571 if (rval != 0) { 2572 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 2573 "IOCTL 0x%2x failed in HBA", cmd); 2574 rv = rval; 2575 } 2576 break; 2577 } 2578 2579 } /* End of main IOCTL switch */ 2580 2581 if (dcp) { 2582 ndi_dc_freehdl(dcp); 2583 } 2584 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 2585 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 2586 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 2587 2588 return (rv); 2589 } 2590 2591 2592 /* 2593 * Create error retrieval sata packet 2594 * 2595 * A sata packet is allocated and set-up to contain specified error retrieval 2596 * command and appropriate dma-able data buffer. 2597 * No association with any scsi packet is made and no callback routine is 2598 * specified. 2599 * 2600 * Returns a pointer to sata packet upon successfull packet creation. 2601 * Returns NULL, if packet cannot be created. 2602 */ 2603 sata_pkt_t * 2604 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 2605 int pkt_type) 2606 { 2607 sata_hba_inst_t *sata_hba_inst; 2608 sata_pkt_txlate_t *spx; 2609 sata_pkt_t *spkt; 2610 sata_drive_info_t *sdinfo; 2611 2612 mutex_enter(&sata_mutex); 2613 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 2614 sata_hba_inst = sata_hba_inst->satahba_next) { 2615 if (SATA_DIP(sata_hba_inst) == dip) 2616 break; 2617 } 2618 mutex_exit(&sata_mutex); 2619 ASSERT(sata_hba_inst != NULL); 2620 2621 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 2622 if (sdinfo == NULL) { 2623 sata_log(sata_hba_inst, CE_WARN, 2624 "sata: error recovery request for non-attached device at " 2625 "cport %d", sata_device->satadev_addr.cport); 2626 return (NULL); 2627 } 2628 2629 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 2630 spx->txlt_sata_hba_inst = sata_hba_inst; 2631 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 2632 spkt = sata_pkt_alloc(spx, NULL); 2633 if (spkt == NULL) { 2634 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 2635 return (NULL); 2636 } 2637 /* address is needed now */ 2638 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 2639 2640 switch (pkt_type) { 2641 case SATA_ERR_RETR_PKT_TYPE_NCQ: 2642 /* Placeholder only */ 2643 break; 2644 2645 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 2646 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 2647 return (spkt); 2648 break; 2649 2650 default: 2651 break; 2652 } 2653 2654 sata_pkt_free(spx); 2655 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 2656 return (NULL); 2657 2658 } 2659 2660 2661 /* 2662 * Free error retrieval sata packet 2663 * 2664 * Free sata packet and any associated resources allocated previously by 2665 * sata_get_error_retrieval_pkt(). 2666 * 2667 * Void return. 2668 */ 2669 void 2670 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 2671 { 2672 sata_pkt_txlate_t *spx = 2673 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 2674 2675 ASSERT(sata_pkt != NULL); 2676 2677 sata_free_local_buffer(spx); 2678 sata_pkt_free(spx); 2679 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 2680 2681 } 2682 2683 2684 /* ****************** SCSA required entry points *********************** */ 2685 2686 /* 2687 * Implementation of scsi tran_tgt_init. 2688 * sata_scsi_tgt_init() initializes scsi_device structure 2689 * 2690 * If successful, DDI_SUCCESS is returned. 2691 * DDI_FAILURE is returned if addressed device does not exist 2692 */ 2693 2694 static int 2695 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2696 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2697 { 2698 #ifndef __lock_lint 2699 _NOTE(ARGUNUSED(hba_dip)) 2700 _NOTE(ARGUNUSED(tgt_dip)) 2701 #endif 2702 sata_device_t sata_device; 2703 sata_drive_info_t *sdinfo; 2704 struct sata_id *sid; 2705 sata_hba_inst_t *sata_hba_inst; 2706 char model[SATA_ID_MODEL_LEN + 1]; 2707 char fw[SATA_ID_FW_LEN + 1]; 2708 char *vid, *pid; 2709 int i; 2710 2711 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2712 2713 /* Validate scsi device address */ 2714 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2715 &sata_device) != 0) 2716 return (DDI_FAILURE); 2717 2718 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2719 sata_device.satadev_addr.cport))); 2720 2721 /* sata_device now contains a valid sata address */ 2722 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2723 if (sdinfo == NULL) { 2724 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2725 sata_device.satadev_addr.cport))); 2726 return (DDI_FAILURE); 2727 } 2728 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2729 sata_device.satadev_addr.cport))); 2730 2731 /* 2732 * Check if we need to create a legacy devid (i.e cmdk style) for 2733 * the target disks. 2734 * 2735 * HBA devinfo node will have the property "use-cmdk-devid-format" 2736 * if we need to create cmdk-style devid for all the disk devices 2737 * attached to this controller. This property may have been set 2738 * from HBA driver's .conf file or by the HBA driver in its 2739 * attach(9F) function. 2740 */ 2741 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2742 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2743 "use-cmdk-devid-format", 0) == 1)) { 2744 /* register a legacy devid for this target node */ 2745 sata_target_devid_register(tgt_dip, sdinfo); 2746 } 2747 2748 2749 /* 2750 * 'Identify Device Data' does not always fit in standard SCSI 2751 * INQUIRY data, so establish INQUIRY_* properties with full-form 2752 * of information. 2753 */ 2754 sid = &sdinfo->satadrv_id; 2755 #ifdef _LITTLE_ENDIAN 2756 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2757 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2758 #else /* _LITTLE_ENDIAN */ 2759 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2760 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2761 #endif /* _LITTLE_ENDIAN */ 2762 model[SATA_ID_MODEL_LEN] = 0; 2763 fw[SATA_ID_FW_LEN] = 0; 2764 2765 /* split model into into vid/pid */ 2766 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 2767 if ((*pid == ' ') || (*pid == '\t')) 2768 break; 2769 if (i < SATA_ID_MODEL_LEN) { 2770 vid = model; 2771 *pid++ = 0; /* terminate vid, establish pid */ 2772 } else { 2773 vid = NULL; /* vid will stay "ATA " */ 2774 pid = model; /* model is all pid */ 2775 } 2776 2777 if (vid) 2778 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2779 vid, strlen(vid)); 2780 if (pid) 2781 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2782 pid, strlen(pid)); 2783 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2784 fw, strlen(fw)); 2785 2786 return (DDI_SUCCESS); 2787 } 2788 2789 /* 2790 * Implementation of scsi tran_tgt_probe. 2791 * Probe target, by calling default scsi routine scsi_hba_probe() 2792 */ 2793 static int 2794 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2795 { 2796 sata_hba_inst_t *sata_hba_inst = 2797 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2798 int rval; 2799 2800 rval = scsi_hba_probe(sd, callback); 2801 2802 if (rval == SCSIPROBE_EXISTS) { 2803 /* 2804 * Set property "pm-capable" on the target device node, so that 2805 * the target driver will not try to fetch scsi cycle counters 2806 * before enabling device power-management. 2807 */ 2808 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2809 "pm-capable", 1)) != DDI_PROP_SUCCESS) { 2810 sata_log(sata_hba_inst, CE_WARN, 2811 "SATA device at port %d: " 2812 "will not be power-managed ", 2813 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2814 SATA_LOG_D((sata_hba_inst, CE_WARN, 2815 "failure updating pm-capable property")); 2816 } 2817 } 2818 return (rval); 2819 } 2820 2821 /* 2822 * Implementation of scsi tran_tgt_free. 2823 * Release all resources allocated for scsi_device 2824 */ 2825 static void 2826 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2827 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2828 { 2829 #ifndef __lock_lint 2830 _NOTE(ARGUNUSED(hba_dip)) 2831 #endif 2832 sata_device_t sata_device; 2833 sata_drive_info_t *sdinfo; 2834 sata_hba_inst_t *sata_hba_inst; 2835 ddi_devid_t devid; 2836 2837 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2838 2839 /* Validate scsi device address */ 2840 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2841 &sata_device) != 0) 2842 return; 2843 2844 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2845 sata_device.satadev_addr.cport))); 2846 2847 /* sata_device now should contain a valid sata address */ 2848 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2849 if (sdinfo == NULL) { 2850 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2851 sata_device.satadev_addr.cport))); 2852 return; 2853 } 2854 /* 2855 * We did not allocate any resources in sata_scsi_tgt_init() 2856 * other than few properties. 2857 * Free them. 2858 */ 2859 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2860 sata_device.satadev_addr.cport))); 2861 if (ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable") != 2862 DDI_PROP_SUCCESS) 2863 SATA_LOG_D((sata_hba_inst, CE_WARN, 2864 "sata_scsi_tgt_free: pm-capable " 2865 "property could not be removed")); 2866 2867 /* 2868 * If devid was previously created but not freed up from 2869 * sd(7D) driver (i.e during detach(9F)) then do it here. 2870 */ 2871 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2872 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2873 "use-cmdk-devid-format", 0) == 1) && 2874 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2875 ddi_devid_unregister(tgt_dip); 2876 ddi_devid_free(devid); 2877 } 2878 } 2879 2880 /* 2881 * Implementation of scsi tran_init_pkt 2882 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2883 * 2884 * It seems that we should always allocate pkt, even if the address is 2885 * for non-existing device - just use some default for dma_attr. 2886 * The reason is that there is no way to communicate this to a caller here. 2887 * Subsequent call to sata_scsi_start may fail appropriately. 2888 * Simply returning NULL does not seem to discourage a target driver... 2889 * 2890 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2891 */ 2892 static struct scsi_pkt * 2893 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2894 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2895 int (*callback)(caddr_t), caddr_t arg) 2896 { 2897 sata_hba_inst_t *sata_hba_inst = 2898 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2899 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2900 sata_device_t sata_device; 2901 sata_drive_info_t *sdinfo; 2902 sata_pkt_txlate_t *spx; 2903 ddi_dma_attr_t cur_dma_attr; 2904 int rval; 2905 boolean_t new_pkt = TRUE; 2906 2907 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2908 2909 /* 2910 * We need to translate the address, even if it could be 2911 * a bogus one, for a non-existing device 2912 */ 2913 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2914 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2915 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2916 sata_device.satadev_rev = SATA_DEVICE_REV; 2917 2918 if (pkt == NULL) { 2919 /* 2920 * Have to allocate a brand new scsi packet. 2921 * We need to operate with auto request sense enabled. 2922 */ 2923 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2924 MAX(statuslen, sizeof (struct scsi_arq_status)), 2925 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2926 2927 if (pkt == NULL) 2928 return (NULL); 2929 2930 /* Fill scsi packet structure */ 2931 pkt->pkt_comp = (void (*)())NULL; 2932 pkt->pkt_time = 0; 2933 pkt->pkt_resid = 0; 2934 pkt->pkt_statistics = 0; 2935 pkt->pkt_reason = 0; 2936 2937 /* 2938 * pkt_hba_private will point to sata pkt txlate structure 2939 */ 2940 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2941 bzero(spx, sizeof (sata_pkt_txlate_t)); 2942 2943 spx->txlt_scsi_pkt = pkt; 2944 spx->txlt_sata_hba_inst = sata_hba_inst; 2945 2946 /* Allocate sata_pkt */ 2947 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2948 if (spx->txlt_sata_pkt == NULL) { 2949 /* Could not allocate sata pkt */ 2950 scsi_hba_pkt_free(ap, pkt); 2951 return (NULL); 2952 } 2953 /* Set sata address */ 2954 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2955 sata_device.satadev_addr; 2956 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2957 sata_device.satadev_rev; 2958 2959 if ((bp == NULL) || (bp->b_bcount == 0)) 2960 return (pkt); 2961 2962 spx->txlt_total_residue = bp->b_bcount; 2963 } else { 2964 new_pkt = FALSE; 2965 /* 2966 * Packet was preallocated/initialized by previous call 2967 */ 2968 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2969 2970 if ((bp == NULL) || (bp->b_bcount == 0)) { 2971 return (pkt); 2972 } 2973 ASSERT(spx->txlt_buf_dma_handle != NULL); 2974 2975 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2976 } 2977 2978 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2979 2980 /* 2981 * We use an adjusted version of the dma_attr, to account 2982 * for device addressing limitations. 2983 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2984 * happen when a device is not yet configured. 2985 */ 2986 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2987 sata_device.satadev_addr.cport))); 2988 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2989 &spx->txlt_sata_pkt->satapkt_device); 2990 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2991 sata_adjust_dma_attr(sdinfo, 2992 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2993 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2994 sata_device.satadev_addr.cport))); 2995 /* 2996 * Allocate necessary DMA resources for the packet's data buffer 2997 * NOTE: 2998 * In case of read/write commands, DMA resource allocation here is 2999 * based on the premise that the transfer length specified in 3000 * the read/write scsi cdb will match exactly DMA resources - 3001 * returning correct packet residue is crucial. 3002 */ 3003 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 3004 &cur_dma_attr)) != DDI_SUCCESS) { 3005 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 3006 sata_pkt_free(spx); 3007 /* 3008 * If a DMA allocation request fails with 3009 * DDI_DMA_NOMAPPING, indicate the error by calling 3010 * bioerror(9F) with bp and an error code of EFAULT. 3011 * If a DMA allocation request fails with 3012 * DDI_DMA_TOOBIG, indicate the error by calling 3013 * bioerror(9F) with bp and an error code of EINVAL. 3014 */ 3015 switch (rval) { 3016 case DDI_DMA_NORESOURCES: 3017 bioerror(bp, 0); 3018 break; 3019 case DDI_DMA_NOMAPPING: 3020 case DDI_DMA_BADATTR: 3021 bioerror(bp, EFAULT); 3022 break; 3023 case DDI_DMA_TOOBIG: 3024 default: 3025 bioerror(bp, EINVAL); 3026 break; 3027 } 3028 if (new_pkt == TRUE) 3029 scsi_hba_pkt_free(ap, pkt); 3030 return (NULL); 3031 } 3032 /* Set number of bytes that are not yet accounted for */ 3033 pkt->pkt_resid = spx->txlt_total_residue; 3034 ASSERT(pkt->pkt_resid >= 0); 3035 3036 return (pkt); 3037 } 3038 3039 /* 3040 * Implementation of scsi tran_start. 3041 * Translate scsi cmd into sata operation and return status. 3042 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 3043 * are supported. 3044 * For SATA hard disks, supported scsi commands: 3045 * SCMD_INQUIRY 3046 * SCMD_TEST_UNIT_READY 3047 * SCMD_START_STOP 3048 * SCMD_READ_CAPACITY 3049 * SCMD_REQUEST_SENSE 3050 * SCMD_LOG_SENSE_G1 3051 * SCMD_LOG_SELECT_G1 3052 * SCMD_MODE_SENSE (specific pages) 3053 * SCMD_MODE_SENSE_G1 (specific pages) 3054 * SCMD_MODE_SELECT (specific pages) 3055 * SCMD_MODE_SELECT_G1 (specific pages) 3056 * SCMD_SYNCHRONIZE_CACHE 3057 * SCMD_SYNCHRONIZE_CACHE_G1 3058 * SCMD_READ 3059 * SCMD_READ_G1 3060 * SCMD_READ_G4 3061 * SCMD_READ_G5 3062 * SCMD_WRITE 3063 * SCMD_WRITE_BUFFER 3064 * SCMD_WRITE_G1 3065 * SCMD_WRITE_G4 3066 * SCMD_WRITE_G5 3067 * SCMD_SEEK (noop) 3068 * SCMD_SDIAG 3069 * 3070 * All other commands are rejected as unsupported. 3071 * 3072 * Returns: 3073 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 3074 * for execution. TRAN_ACCEPT may be returned also if device was removed but 3075 * a callback could be scheduled. 3076 * TRAN_BADPKT if cmd was directed to invalid address. 3077 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 3078 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 3079 * was removed and there was no callback specified in scsi pkt. 3080 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 3081 * framework was busy performing some other operation(s). 3082 * 3083 */ 3084 static int 3085 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 3086 { 3087 sata_hba_inst_t *sata_hba_inst = 3088 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3089 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3090 sata_drive_info_t *sdinfo; 3091 struct buf *bp; 3092 int cport; 3093 int rval; 3094 3095 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 3096 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 3097 3098 ASSERT(spx != NULL && 3099 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 3100 3101 cport = SCSI_TO_SATA_CPORT(ap->a_target); 3102 3103 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 3104 sdinfo = sata_get_device_info(sata_hba_inst, 3105 &spx->txlt_sata_pkt->satapkt_device); 3106 if (sdinfo == NULL || 3107 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean == 3108 B_FALSE) { 3109 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 3110 pkt->pkt_reason = CMD_DEV_GONE; 3111 /* 3112 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3113 * only in callback function (for normal requests) and 3114 * in the dump code path. 3115 * So, if the callback is available, we need to do 3116 * the callback rather than returning TRAN_FATAL_ERROR here. 3117 */ 3118 if (pkt->pkt_comp != NULL) { 3119 /* scsi callback required */ 3120 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3121 (task_func_t *)pkt->pkt_comp, 3122 (void *)pkt, TQ_SLEEP) == NULL) 3123 /* Scheduling the callback failed */ 3124 return (TRAN_BUSY); 3125 return (TRAN_ACCEPT); 3126 } 3127 /* No callback available */ 3128 return (TRAN_FATAL_ERROR); 3129 } 3130 3131 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 3132 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 3133 rval = sata_txlt_atapi(spx); 3134 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 3135 "sata_scsi_start atapi: rval %d\n", rval); 3136 return (rval); 3137 } 3138 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 3139 3140 /* ATA Disk commands processing starts here */ 3141 3142 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3143 3144 switch (pkt->pkt_cdbp[0]) { 3145 3146 case SCMD_INQUIRY: 3147 /* Mapped to identify device */ 3148 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3149 bp_mapin(bp); 3150 rval = sata_txlt_inquiry(spx); 3151 break; 3152 3153 case SCMD_TEST_UNIT_READY: 3154 /* 3155 * SAT "SATA to ATA Translation" doc specifies translation 3156 * to ATA CHECK POWER MODE. 3157 */ 3158 rval = sata_txlt_test_unit_ready(spx); 3159 break; 3160 3161 case SCMD_START_STOP: 3162 /* Mapping depends on the command */ 3163 rval = sata_txlt_start_stop_unit(spx); 3164 break; 3165 3166 case SCMD_READ_CAPACITY: 3167 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3168 bp_mapin(bp); 3169 rval = sata_txlt_read_capacity(spx); 3170 break; 3171 3172 case SCMD_REQUEST_SENSE: 3173 /* 3174 * Always No Sense, since we force ARQ 3175 */ 3176 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3177 bp_mapin(bp); 3178 rval = sata_txlt_request_sense(spx); 3179 break; 3180 3181 case SCMD_LOG_SENSE_G1: 3182 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3183 bp_mapin(bp); 3184 rval = sata_txlt_log_sense(spx); 3185 break; 3186 3187 case SCMD_LOG_SELECT_G1: 3188 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3189 bp_mapin(bp); 3190 rval = sata_txlt_log_select(spx); 3191 break; 3192 3193 case SCMD_MODE_SENSE: 3194 case SCMD_MODE_SENSE_G1: 3195 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3196 bp_mapin(bp); 3197 rval = sata_txlt_mode_sense(spx); 3198 break; 3199 3200 3201 case SCMD_MODE_SELECT: 3202 case SCMD_MODE_SELECT_G1: 3203 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3204 bp_mapin(bp); 3205 rval = sata_txlt_mode_select(spx); 3206 break; 3207 3208 case SCMD_SYNCHRONIZE_CACHE: 3209 case SCMD_SYNCHRONIZE_CACHE_G1: 3210 rval = sata_txlt_synchronize_cache(spx); 3211 break; 3212 3213 case SCMD_READ: 3214 case SCMD_READ_G1: 3215 case SCMD_READ_G4: 3216 case SCMD_READ_G5: 3217 rval = sata_txlt_read(spx); 3218 break; 3219 case SCMD_WRITE_BUFFER: 3220 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3221 bp_mapin(bp); 3222 rval = sata_txlt_write_buffer(spx); 3223 break; 3224 3225 case SCMD_WRITE: 3226 case SCMD_WRITE_G1: 3227 case SCMD_WRITE_G4: 3228 case SCMD_WRITE_G5: 3229 rval = sata_txlt_write(spx); 3230 break; 3231 3232 case SCMD_SEEK: 3233 rval = sata_txlt_nodata_cmd_immediate(spx); 3234 break; 3235 3236 /* Other cases will be filed later */ 3237 /* postponed until phase 2 of the development */ 3238 default: 3239 rval = sata_txlt_invalid_command(spx); 3240 break; 3241 } 3242 3243 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 3244 "sata_scsi_start: rval %d\n", rval); 3245 3246 return (rval); 3247 } 3248 3249 /* 3250 * Implementation of scsi tran_abort. 3251 * Abort specific pkt or all packets. 3252 * 3253 * Returns 1 if one or more packets were aborted, returns 0 otherwise 3254 * 3255 * May be called from an interrupt level. 3256 */ 3257 static int 3258 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 3259 { 3260 sata_hba_inst_t *sata_hba_inst = 3261 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3262 sata_device_t sata_device; 3263 sata_pkt_t *sata_pkt; 3264 3265 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3266 "sata_scsi_abort: %s at target: 0x%x\n", 3267 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 3268 3269 /* Validate address */ 3270 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 3271 /* Invalid address */ 3272 return (0); 3273 3274 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3275 sata_device.satadev_addr.cport))); 3276 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 3277 /* invalid address */ 3278 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3279 sata_device.satadev_addr.cport))); 3280 return (0); 3281 } 3282 if (scsi_pkt == NULL) { 3283 /* 3284 * Abort all packets. 3285 * Although we do not have specific packet, we still need 3286 * dummy packet structure to pass device address to HBA. 3287 * Allocate one, without sleeping. Fail if pkt cannot be 3288 * allocated. 3289 */ 3290 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 3291 if (sata_pkt == NULL) { 3292 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3293 sata_device.satadev_addr.cport))); 3294 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 3295 "could not allocate sata_pkt")); 3296 return (0); 3297 } 3298 sata_pkt->satapkt_rev = SATA_PKT_REV; 3299 sata_pkt->satapkt_device = sata_device; 3300 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 3301 } else { 3302 if (scsi_pkt->pkt_ha_private == NULL) { 3303 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3304 sata_device.satadev_addr.cport))); 3305 return (0); /* Bad scsi pkt */ 3306 } 3307 /* extract pointer to sata pkt */ 3308 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 3309 txlt_sata_pkt; 3310 } 3311 3312 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3313 sata_device.satadev_addr.cport))); 3314 /* Send abort request to HBA */ 3315 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 3316 (SATA_DIP(sata_hba_inst), sata_pkt, 3317 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 3318 SATA_SUCCESS) { 3319 if (scsi_pkt == NULL) 3320 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 3321 /* Success */ 3322 return (1); 3323 } 3324 /* Else, something did not go right */ 3325 if (scsi_pkt == NULL) 3326 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 3327 /* Failure */ 3328 return (0); 3329 } 3330 3331 3332 /* 3333 * Implementation of scsi tran_reset. 3334 * RESET_ALL request is translated into port reset. 3335 * RESET_TARGET requests is translated into a device reset, 3336 * RESET_LUN request is accepted only for LUN 0 and translated into 3337 * device reset. 3338 * The target reset should cause all HBA active and queued packets to 3339 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 3340 * the return. HBA should report reset event for the device. 3341 * 3342 * Returns 1 upon success, 0 upon failure. 3343 */ 3344 static int 3345 sata_scsi_reset(struct scsi_address *ap, int level) 3346 { 3347 sata_hba_inst_t *sata_hba_inst = 3348 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3349 sata_device_t sata_device; 3350 int val; 3351 3352 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3353 "sata_scsi_reset: level %d target: 0x%x\n", 3354 level, ap->a_target); 3355 3356 /* Validate address */ 3357 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 3358 if (val == -1) 3359 /* Invalid address */ 3360 return (0); 3361 3362 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3363 sata_device.satadev_addr.cport))); 3364 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 3365 /* invalid address */ 3366 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3367 sata_device.satadev_addr.cport))); 3368 return (0); 3369 } 3370 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3371 sata_device.satadev_addr.cport))); 3372 if (level == RESET_ALL) { 3373 /* port reset - cport only */ 3374 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 3375 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 3376 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 3377 return (1); 3378 else 3379 return (0); 3380 3381 } else if (val == 0 && 3382 (level == RESET_TARGET || level == RESET_LUN)) { 3383 /* reset device (device attached) */ 3384 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 3385 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 3386 return (1); 3387 else 3388 return (0); 3389 } 3390 return (0); 3391 } 3392 3393 3394 /* 3395 * Implementation of scsi tran_getcap (get transport/device capabilities). 3396 * Supported capabilities for SATA hard disks: 3397 * auto-rqsense (always supported) 3398 * tagged-qing (supported if HBA supports it) 3399 * untagged-qing (could be supported if disk supports it, but because 3400 * caching behavior allowing untagged queuing actually 3401 * results in reduced performance. sd tries to throttle 3402 * back to only 3 outstanding commands, which may 3403 * work for real SCSI disks, but with read ahead 3404 * caching, having more than 1 outstanding command 3405 * results in cache thrashing.) 3406 * sector_size 3407 * dma_max 3408 * interconnect-type (INTERCONNECT_SATA) 3409 * 3410 * Supported capabilities for ATAPI devices (CD/DVD): 3411 * auto-rqsense (always supported) 3412 * sector_size 3413 * dma_max 3414 * interconnect-type (INTERCONNECT_SATA) 3415 * 3416 * Request for other capabilities is rejected as unsupported. 3417 * 3418 * Returns supported capability value, or -1 if capability is unsuppported or 3419 * the address is invalid - no device. 3420 */ 3421 3422 static int 3423 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 3424 { 3425 3426 sata_hba_inst_t *sata_hba_inst = 3427 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3428 sata_device_t sata_device; 3429 sata_drive_info_t *sdinfo; 3430 ddi_dma_attr_t adj_dma_attr; 3431 int rval; 3432 3433 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3434 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 3435 ap->a_target, cap); 3436 3437 /* 3438 * We want to process the capabilities on per port granularity. 3439 * So, we are specifically restricting ourselves to whom != 0 3440 * to exclude the controller wide handling. 3441 */ 3442 if (cap == NULL || whom == 0) 3443 return (-1); 3444 3445 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3446 /* Invalid address */ 3447 return (-1); 3448 } 3449 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3450 sata_device.satadev_addr.cport))); 3451 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 3452 NULL) { 3453 /* invalid address */ 3454 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3455 sata_device.satadev_addr.cport))); 3456 return (-1); 3457 } 3458 3459 switch (scsi_hba_lookup_capstr(cap)) { 3460 case SCSI_CAP_ARQ: 3461 rval = 1; /* ARQ supported, turned on */ 3462 break; 3463 3464 case SCSI_CAP_SECTOR_SIZE: 3465 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 3466 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 3467 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 3468 rval = SATA_ATAPI_SECTOR_SIZE; 3469 else rval = -1; 3470 break; 3471 3472 /* 3473 * untagged queuing cause a performance inversion because of 3474 * the way sd operates. Because of this reason we do not 3475 * use it when available. 3476 */ 3477 case SCSI_CAP_UNTAGGED_QING: 3478 if (sdinfo->satadrv_features_enabled & 3479 SATA_DEV_F_E_UNTAGGED_QING) 3480 rval = 1; /* Untagged queuing available */ 3481 else 3482 rval = -1; /* Untagged queuing not available */ 3483 break; 3484 3485 case SCSI_CAP_TAGGED_QING: 3486 if ((sdinfo->satadrv_features_enabled & 3487 SATA_DEV_F_E_TAGGED_QING) && 3488 (sdinfo->satadrv_max_queue_depth > 1)) 3489 rval = 1; /* Tagged queuing available */ 3490 else 3491 rval = -1; /* Tagged queuing not available */ 3492 break; 3493 3494 case SCSI_CAP_DMA_MAX: 3495 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 3496 &adj_dma_attr); 3497 rval = (int)adj_dma_attr.dma_attr_maxxfer; 3498 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 3499 break; 3500 3501 case SCSI_CAP_INTERCONNECT_TYPE: 3502 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 3503 break; 3504 3505 default: 3506 rval = -1; 3507 break; 3508 } 3509 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3510 sata_device.satadev_addr.cport))); 3511 return (rval); 3512 } 3513 3514 /* 3515 * Implementation of scsi tran_setcap 3516 * 3517 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 3518 * 3519 */ 3520 static int 3521 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 3522 { 3523 sata_hba_inst_t *sata_hba_inst = 3524 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3525 sata_device_t sata_device; 3526 sata_drive_info_t *sdinfo; 3527 int rval; 3528 3529 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3530 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 3531 3532 /* 3533 * We want to process the capabilities on per port granularity. 3534 * So, we are specifically restricting ourselves to whom != 0 3535 * to exclude the controller wide handling. 3536 */ 3537 if (cap == NULL || whom == 0) { 3538 return (-1); 3539 } 3540 3541 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3542 /* Invalid address */ 3543 return (-1); 3544 } 3545 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3546 sata_device.satadev_addr.cport))); 3547 if ((sdinfo = sata_get_device_info(sata_hba_inst, 3548 &sata_device)) == NULL) { 3549 /* invalid address */ 3550 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3551 sata_device.satadev_addr.cport))); 3552 return (-1); 3553 } 3554 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3555 sata_device.satadev_addr.cport))); 3556 3557 switch (scsi_hba_lookup_capstr(cap)) { 3558 case SCSI_CAP_ARQ: 3559 case SCSI_CAP_SECTOR_SIZE: 3560 case SCSI_CAP_DMA_MAX: 3561 case SCSI_CAP_INTERCONNECT_TYPE: 3562 rval = 0; 3563 break; 3564 case SCSI_CAP_UNTAGGED_QING: 3565 if (SATA_QDEPTH(sata_hba_inst) > 1) { 3566 rval = 1; 3567 if (value == 1) { 3568 sdinfo->satadrv_features_enabled |= 3569 SATA_DEV_F_E_UNTAGGED_QING; 3570 } else if (value == 0) { 3571 sdinfo->satadrv_features_enabled &= 3572 ~SATA_DEV_F_E_UNTAGGED_QING; 3573 } else { 3574 rval = -1; 3575 } 3576 } else { 3577 rval = 0; 3578 } 3579 break; 3580 case SCSI_CAP_TAGGED_QING: 3581 /* This can TCQ or NCQ */ 3582 if (sata_func_enable & SATA_ENABLE_QUEUING && 3583 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 3584 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 3585 (sata_func_enable & SATA_ENABLE_NCQ && 3586 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 3587 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 3588 (sdinfo->satadrv_max_queue_depth > 1)) { 3589 rval = 1; 3590 if (value == 1) { 3591 sdinfo->satadrv_features_enabled |= 3592 SATA_DEV_F_E_TAGGED_QING; 3593 } else if (value == 0) { 3594 sdinfo->satadrv_features_enabled &= 3595 ~SATA_DEV_F_E_TAGGED_QING; 3596 } else { 3597 rval = -1; 3598 } 3599 } else { 3600 rval = 0; 3601 } 3602 break; 3603 default: 3604 rval = -1; 3605 break; 3606 } 3607 return (rval); 3608 } 3609 3610 /* 3611 * Implementations of scsi tran_destroy_pkt. 3612 * Free resources allocated by sata_scsi_init_pkt() 3613 */ 3614 static void 3615 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3616 { 3617 sata_pkt_txlate_t *spx; 3618 3619 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3620 3621 if (spx->txlt_buf_dma_handle != NULL) { 3622 if (spx->txlt_tmp_buf != NULL) { 3623 ASSERT(spx->txlt_tmp_buf_handle != 0); 3624 /* 3625 * Intermediate DMA buffer was allocated. 3626 * Free allocated buffer and associated access handle. 3627 */ 3628 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 3629 spx->txlt_tmp_buf = NULL; 3630 } 3631 /* 3632 * Free DMA resources - cookies and handles 3633 */ 3634 if (spx->txlt_dma_cookie_list != NULL) { 3635 if (spx->txlt_dma_cookie_list != 3636 &spx->txlt_dma_cookie) { 3637 (void) kmem_free(spx->txlt_dma_cookie_list, 3638 spx->txlt_dma_cookie_list_len * 3639 sizeof (ddi_dma_cookie_t)); 3640 spx->txlt_dma_cookie_list = NULL; 3641 } 3642 } 3643 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 3644 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 3645 } 3646 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 3647 sata_pkt_free(spx); 3648 3649 scsi_hba_pkt_free(ap, pkt); 3650 } 3651 3652 /* 3653 * Implementation of scsi tran_dmafree. 3654 * Free DMA resources allocated by sata_scsi_init_pkt() 3655 */ 3656 3657 static void 3658 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 3659 { 3660 #ifndef __lock_lint 3661 _NOTE(ARGUNUSED(ap)) 3662 #endif 3663 sata_pkt_txlate_t *spx; 3664 3665 ASSERT(pkt != NULL); 3666 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3667 3668 if (spx->txlt_buf_dma_handle != NULL) { 3669 if (spx->txlt_tmp_buf != NULL) { 3670 /* 3671 * Intermediate DMA buffer was allocated. 3672 * Free allocated buffer and associated access handle. 3673 */ 3674 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 3675 spx->txlt_tmp_buf = NULL; 3676 } 3677 /* 3678 * Free DMA resources - cookies and handles 3679 */ 3680 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 3681 if (spx->txlt_dma_cookie_list != NULL) { 3682 if (spx->txlt_dma_cookie_list != 3683 &spx->txlt_dma_cookie) { 3684 (void) kmem_free(spx->txlt_dma_cookie_list, 3685 spx->txlt_dma_cookie_list_len * 3686 sizeof (ddi_dma_cookie_t)); 3687 spx->txlt_dma_cookie_list = NULL; 3688 } 3689 } 3690 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 3691 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 3692 spx->txlt_buf_dma_handle = NULL; 3693 } 3694 } 3695 3696 /* 3697 * Implementation of scsi tran_sync_pkt. 3698 * 3699 * The assumption below is that pkt is unique - there is no need to check ap 3700 * 3701 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3702 * into/from the real buffer. 3703 */ 3704 static void 3705 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3706 { 3707 #ifndef __lock_lint 3708 _NOTE(ARGUNUSED(ap)) 3709 #endif 3710 int rval; 3711 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3712 struct buf *bp; 3713 int direction; 3714 3715 ASSERT(spx != NULL); 3716 if (spx->txlt_buf_dma_handle != NULL) { 3717 direction = spx->txlt_sata_pkt-> 3718 satapkt_cmd.satacmd_flags.sata_data_direction; 3719 if (spx->txlt_sata_pkt != NULL && 3720 direction != SATA_DIR_NODATA_XFER) { 3721 if (spx->txlt_tmp_buf != NULL) { 3722 /* Intermediate DMA buffer used */ 3723 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3724 3725 if (direction & SATA_DIR_WRITE) { 3726 bcopy(bp->b_un.b_addr, 3727 spx->txlt_tmp_buf, bp->b_bcount); 3728 } 3729 } 3730 /* Sync the buffer for device or for CPU */ 3731 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3732 (direction & SATA_DIR_WRITE) ? 3733 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3734 ASSERT(rval == DDI_SUCCESS); 3735 if (spx->txlt_tmp_buf != NULL && 3736 !(direction & SATA_DIR_WRITE)) { 3737 /* Intermediate DMA buffer used for read */ 3738 bcopy(spx->txlt_tmp_buf, 3739 bp->b_un.b_addr, bp->b_bcount); 3740 } 3741 3742 } 3743 } 3744 } 3745 3746 3747 3748 /* ******************* SATA - SCSI Translation functions **************** */ 3749 /* 3750 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3751 * translation. 3752 */ 3753 3754 /* 3755 * Checks if a device exists and can be access and translates common 3756 * scsi_pkt data to sata_pkt data. 3757 * 3758 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3759 * sata_pkt was set-up. 3760 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3761 * exist and pkt_comp callback was scheduled. 3762 * Returns other TRAN_XXXXX values when error occured and command should be 3763 * rejected with the returned TRAN_XXXXX value. 3764 * 3765 * This function should be called with port mutex held. 3766 */ 3767 static int 3768 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx) 3769 { 3770 sata_drive_info_t *sdinfo; 3771 sata_device_t sata_device; 3772 const struct sata_cmd_flags sata_initial_cmd_flags = { 3773 SATA_DIR_NODATA_XFER, 3774 /* all other values to 0/FALSE */ 3775 }; 3776 /* 3777 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3778 * and that implies TRAN_ACCEPT return value. Any other returned value 3779 * indicates that the scsi packet was not accepted (the reason will not 3780 * be checked by the scsi traget driver). 3781 * To make debugging easier, we set pkt_reason to know value here. 3782 * It may be changed later when different completion reason is 3783 * determined. 3784 */ 3785 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3786 3787 /* Validate address */ 3788 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3789 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3790 3791 case -1: 3792 /* Invalid address or invalid device type */ 3793 return (TRAN_BADPKT); 3794 case 1: 3795 /* valid address but no device - it has disappeared ? */ 3796 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3797 /* 3798 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3799 * only in callback function (for normal requests) and 3800 * in the dump code path. 3801 * So, if the callback is available, we need to do 3802 * the callback rather than returning TRAN_FATAL_ERROR here. 3803 */ 3804 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3805 /* scsi callback required */ 3806 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3807 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3808 (void *)spx->txlt_scsi_pkt, 3809 TQ_SLEEP) == NULL) 3810 /* Scheduling the callback failed */ 3811 return (TRAN_BUSY); 3812 3813 return (TRAN_ACCEPT); 3814 } 3815 return (TRAN_FATAL_ERROR); 3816 default: 3817 /* all OK */ 3818 break; 3819 } 3820 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3821 &spx->txlt_sata_pkt->satapkt_device); 3822 3823 /* 3824 * If device is in reset condition, reject the packet with 3825 * TRAN_BUSY, unless: 3826 * 1. system is panicking (dumping) 3827 * In such case only one thread is running and there is no way to 3828 * process reset. 3829 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3830 * Some cfgadm operations involve drive commands, so reset condition 3831 * needs to be ignored for IOCTL operations. 3832 */ 3833 if ((sdinfo->satadrv_event_flags & 3834 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3835 3836 if (!ddi_in_panic() && 3837 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3838 sata_device.satadev_addr.cport) & 3839 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3840 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3841 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3842 "sata_scsi_start: rejecting command because " 3843 "of device reset state\n", NULL); 3844 return (TRAN_BUSY); 3845 } 3846 } 3847 3848 /* 3849 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3850 * sata_scsi_pkt_init() because pkt init had to work also with 3851 * non-existing devices. 3852 * Now we know that the packet was set-up for a real device, so its 3853 * type is known. 3854 */ 3855 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3856 3857 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3858 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3859 sata_device.satadev_addr.cport)->cport_event_flags & 3860 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3861 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3862 sata_ignore_dev_reset = B_TRUE; 3863 } 3864 /* 3865 * At this point the generic translation routine determined that the 3866 * scsi packet should be accepted. Packet completion reason may be 3867 * changed later when a different completion reason is determined. 3868 */ 3869 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3870 3871 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3872 /* Synchronous execution */ 3873 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3874 SATA_OPMODE_POLLING; 3875 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3876 sata_ignore_dev_reset = ddi_in_panic(); 3877 } else { 3878 /* Asynchronous execution */ 3879 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3880 SATA_OPMODE_INTERRUPTS; 3881 } 3882 /* Convert queuing information */ 3883 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3884 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3885 B_TRUE; 3886 else if (spx->txlt_scsi_pkt->pkt_flags & 3887 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3888 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3889 B_TRUE; 3890 3891 /* Always limit pkt time */ 3892 if (spx->txlt_scsi_pkt->pkt_time == 0) 3893 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3894 else 3895 /* Pass on scsi_pkt time */ 3896 spx->txlt_sata_pkt->satapkt_time = 3897 spx->txlt_scsi_pkt->pkt_time; 3898 3899 return (TRAN_ACCEPT); 3900 } 3901 3902 3903 /* 3904 * Translate ATA Identify Device data to SCSI Inquiry data. 3905 * This function may be called only for ATA devices. 3906 * This function should not be called for ATAPI devices - they 3907 * respond directly to SCSI Inquiry command. 3908 * 3909 * SATA Identify Device data has to be valid in sata_rive_info. 3910 * Buffer has to accomodate the inquiry length (36 bytes). 3911 * 3912 * This function should be called with a port mutex held. 3913 */ 3914 static void 3915 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3916 sata_drive_info_t *sdinfo, uint8_t *buf) 3917 { 3918 3919 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3920 struct sata_id *sid = &sdinfo->satadrv_id; 3921 3922 /* Start with a nice clean slate */ 3923 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3924 3925 /* 3926 * Rely on the dev_type for setting paripheral qualifier. 3927 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3928 * It could be that DTYPE_OPTICAL could also qualify in the future. 3929 * ATAPI Inquiry may provide more data to the target driver. 3930 */ 3931 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3932 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3933 3934 inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0; 3935 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3936 inq->inq_iso = 0; /* ISO version */ 3937 inq->inq_ecma = 0; /* ECMA version */ 3938 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3939 inq->inq_aenc = 0; /* Async event notification cap. */ 3940 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3941 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3942 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3943 inq->inq_len = 31; /* Additional length */ 3944 inq->inq_dualp = 0; /* dual port device - NO */ 3945 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3946 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3947 inq->inq_linked = 0; /* Supports linked commands - NO */ 3948 /* 3949 * Queuing support - controller has to 3950 * support some sort of command queuing. 3951 */ 3952 if (SATA_QDEPTH(sata_hba_inst) > 1) 3953 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3954 else 3955 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3956 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3957 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3958 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3959 3960 #ifdef _LITTLE_ENDIAN 3961 /* Swap text fields to match SCSI format */ 3962 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3963 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3964 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3965 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3966 else 3967 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3968 #else /* _LITTLE_ENDIAN */ 3969 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3970 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3971 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3972 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3973 else 3974 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3975 #endif /* _LITTLE_ENDIAN */ 3976 } 3977 3978 3979 /* 3980 * Scsi response set up for invalid command (command not supported) 3981 * 3982 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3983 */ 3984 static int 3985 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3986 { 3987 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3988 struct scsi_extended_sense *sense; 3989 3990 scsipkt->pkt_reason = CMD_CMPLT; 3991 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3992 STATE_SENT_CMD | STATE_GOT_STATUS; 3993 3994 *scsipkt->pkt_scbp = STATUS_CHECK; 3995 3996 sense = sata_arq_sense(spx); 3997 sense->es_key = KEY_ILLEGAL_REQUEST; 3998 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3999 4000 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4001 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4002 4003 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4004 scsipkt->pkt_comp != NULL) 4005 /* scsi callback required */ 4006 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4007 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4008 (void *)spx->txlt_scsi_pkt, 4009 TQ_SLEEP) == NULL) 4010 /* Scheduling the callback failed */ 4011 return (TRAN_BUSY); 4012 return (TRAN_ACCEPT); 4013 } 4014 4015 /* 4016 * Scsi response setup for 4017 * emulated non-data command that requires no action/return data 4018 * 4019 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4020 */ 4021 static int 4022 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 4023 { 4024 int rval; 4025 4026 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4027 4028 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4029 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4030 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4031 return (rval); 4032 } 4033 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4034 4035 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4036 STATE_SENT_CMD | STATE_GOT_STATUS; 4037 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 4038 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 4039 4040 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4041 "Scsi_pkt completion reason %x\n", 4042 spx->txlt_scsi_pkt->pkt_reason); 4043 4044 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 4045 spx->txlt_scsi_pkt->pkt_comp != NULL) 4046 /* scsi callback required */ 4047 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4048 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4049 (void *)spx->txlt_scsi_pkt, 4050 TQ_SLEEP) == NULL) 4051 /* Scheduling the callback failed */ 4052 return (TRAN_BUSY); 4053 return (TRAN_ACCEPT); 4054 } 4055 4056 4057 /* 4058 * SATA translate command: Inquiry / Identify Device 4059 * Use cached Identify Device data for now, rather than issuing actual 4060 * Device Identify cmd request. If device is detached and re-attached, 4061 * asynchromous event processing should fetch and refresh Identify Device 4062 * data. 4063 * Two VPD pages are supported now: 4064 * Vital Product Data page 4065 * Unit Serial Number page 4066 * 4067 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4068 */ 4069 4070 #define EVPD 1 /* Extended Vital Product Data flag */ 4071 #define CMDDT 2 /* Command Support Data - Obsolete */ 4072 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 4073 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 4074 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 4075 4076 static int 4077 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 4078 { 4079 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4080 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4081 sata_drive_info_t *sdinfo; 4082 struct scsi_extended_sense *sense; 4083 int count; 4084 uint8_t *p; 4085 int i, j; 4086 uint8_t page_buf[0xff]; /* Max length */ 4087 int rval; 4088 4089 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4090 4091 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4092 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4093 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4094 return (rval); 4095 } 4096 4097 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4098 &spx->txlt_sata_pkt->satapkt_device); 4099 4100 ASSERT(sdinfo != NULL); 4101 4102 scsipkt->pkt_reason = CMD_CMPLT; 4103 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4104 STATE_SENT_CMD | STATE_GOT_STATUS; 4105 4106 /* Reject not supported request */ 4107 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 4108 *scsipkt->pkt_scbp = STATUS_CHECK; 4109 sense = sata_arq_sense(spx); 4110 sense->es_key = KEY_ILLEGAL_REQUEST; 4111 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4112 goto done; 4113 } 4114 4115 /* Valid Inquiry request */ 4116 *scsipkt->pkt_scbp = STATUS_GOOD; 4117 4118 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4119 4120 /* 4121 * Because it is fully emulated command storing data 4122 * programatically in the specified buffer, release 4123 * preallocated DMA resources before storing data in the buffer, 4124 * so no unwanted DMA sync would take place. 4125 */ 4126 sata_scsi_dmafree(NULL, scsipkt); 4127 4128 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 4129 /* Standard Inquiry Data request */ 4130 struct scsi_inquiry inq; 4131 unsigned int bufsize; 4132 4133 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 4134 sdinfo, (uint8_t *)&inq); 4135 /* Copy no more than requested */ 4136 count = MIN(bp->b_bcount, 4137 sizeof (struct scsi_inquiry)); 4138 bufsize = scsipkt->pkt_cdbp[4]; 4139 bufsize |= scsipkt->pkt_cdbp[3] << 8; 4140 count = MIN(count, bufsize); 4141 bcopy(&inq, bp->b_un.b_addr, count); 4142 4143 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4144 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 4145 bufsize - count : 0; 4146 } else { 4147 /* 4148 * peripheral_qualifier = 0; 4149 * 4150 * We are dealing only with HD and will be 4151 * dealing with CD/DVD devices soon 4152 */ 4153 uint8_t peripheral_device_type = 4154 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 4155 DTYPE_DIRECT : DTYPE_RODIRECT; 4156 4157 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 4158 case INQUIRY_SUP_VPD_PAGE: 4159 /* 4160 * Request for suported Vital Product Data 4161 * pages - assuming only 2 page codes 4162 * supported 4163 */ 4164 page_buf[0] = peripheral_device_type; 4165 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 4166 page_buf[2] = 0; 4167 page_buf[3] = 2; /* page length */ 4168 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 4169 page_buf[5] = INQUIRY_USN_PAGE; 4170 /* Copy no more than requested */ 4171 count = MIN(bp->b_bcount, 6); 4172 bcopy(page_buf, bp->b_un.b_addr, count); 4173 break; 4174 case INQUIRY_USN_PAGE: 4175 /* 4176 * Request for Unit Serial Number page 4177 */ 4178 page_buf[0] = peripheral_device_type; 4179 page_buf[1] = INQUIRY_USN_PAGE; 4180 page_buf[2] = 0; 4181 page_buf[3] = 20; /* remaining page length */ 4182 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 4183 #ifdef _LITTLE_ENDIAN 4184 swab(p, &page_buf[4], 20); 4185 #else 4186 bcopy(p, &page_buf[4], 20); 4187 #endif 4188 for (i = 0; i < 20; i++) { 4189 if (page_buf[4 + i] == '\0' || 4190 page_buf[4 + i] == '\040') { 4191 break; 4192 } 4193 } 4194 /* 4195 * 'i' contains string length. 4196 * 4197 * Least significant character of the serial 4198 * number shall appear as the last byte, 4199 * according to SBC-3 spec. 4200 */ 4201 p = &page_buf[20 + 4 - 1]; 4202 for (j = i; j > 0; j--, p--) { 4203 *p = *(p - 20 + i); 4204 } 4205 p = &page_buf[4]; 4206 for (j = 20 - i; j > 0; j--) { 4207 *p++ = '\040'; 4208 } 4209 count = MIN(bp->b_bcount, 24); 4210 bcopy(page_buf, bp->b_un.b_addr, count); 4211 break; 4212 4213 case INQUIRY_DEV_IDENTIFICATION_PAGE: 4214 /* 4215 * We may want to implement this page, when 4216 * identifiers are common for SATA devices 4217 * But not now. 4218 */ 4219 /*FALLTHROUGH*/ 4220 4221 default: 4222 /* Request for unsupported VPD page */ 4223 *scsipkt->pkt_scbp = STATUS_CHECK; 4224 sense = sata_arq_sense(spx); 4225 sense->es_key = KEY_ILLEGAL_REQUEST; 4226 sense->es_add_code = 4227 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4228 goto done; 4229 } 4230 } 4231 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4232 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 4233 scsipkt->pkt_cdbp[4] - count : 0; 4234 } 4235 done: 4236 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4237 4238 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4239 "Scsi_pkt completion reason %x\n", 4240 scsipkt->pkt_reason); 4241 4242 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4243 scsipkt->pkt_comp != NULL) { 4244 /* scsi callback required */ 4245 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4246 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4247 TQ_SLEEP) == NULL) 4248 /* Scheduling the callback failed */ 4249 return (TRAN_BUSY); 4250 } 4251 return (TRAN_ACCEPT); 4252 } 4253 4254 /* 4255 * SATA translate command: Request Sense. 4256 * Emulated command (ATA version for SATA hard disks) 4257 * Always NO SENSE, because any sense data should be reported by ARQ sense. 4258 * 4259 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4260 */ 4261 static int 4262 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 4263 { 4264 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4265 struct scsi_extended_sense sense; 4266 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4267 int rval; 4268 4269 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4270 4271 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4272 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4273 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4274 return (rval); 4275 } 4276 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4277 4278 4279 scsipkt->pkt_reason = CMD_CMPLT; 4280 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4281 STATE_SENT_CMD | STATE_GOT_STATUS; 4282 *scsipkt->pkt_scbp = STATUS_GOOD; 4283 4284 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4285 /* 4286 * Because it is fully emulated command storing data 4287 * programatically in the specified buffer, release 4288 * preallocated DMA resources before storing data in the buffer, 4289 * so no unwanted DMA sync would take place. 4290 */ 4291 int count = MIN(bp->b_bcount, 4292 sizeof (struct scsi_extended_sense)); 4293 sata_scsi_dmafree(NULL, scsipkt); 4294 bzero(&sense, sizeof (struct scsi_extended_sense)); 4295 sense.es_valid = 0; /* Valid LBA */ 4296 sense.es_class = 7; /* Response code 0x70 - current err */ 4297 sense.es_key = KEY_NO_SENSE; 4298 sense.es_add_len = 6; /* Additional length */ 4299 /* Copy no more than requested */ 4300 bcopy(&sense, bp->b_un.b_addr, count); 4301 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4302 scsipkt->pkt_resid = 0; 4303 } 4304 4305 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4306 "Scsi_pkt completion reason %x\n", 4307 scsipkt->pkt_reason); 4308 4309 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4310 scsipkt->pkt_comp != NULL) 4311 /* scsi callback required */ 4312 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4313 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4314 TQ_SLEEP) == NULL) 4315 /* Scheduling the callback failed */ 4316 return (TRAN_BUSY); 4317 return (TRAN_ACCEPT); 4318 } 4319 4320 /* 4321 * SATA translate command: Test Unit Ready 4322 * At the moment this is an emulated command (ATA version for SATA hard disks). 4323 * May be translated into Check Power Mode command in the future 4324 * 4325 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4326 */ 4327 static int 4328 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 4329 { 4330 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4331 struct scsi_extended_sense *sense; 4332 int power_state; 4333 int rval; 4334 4335 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4336 4337 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4338 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4339 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4340 return (rval); 4341 } 4342 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4343 4344 /* At this moment, emulate it rather than execute anything */ 4345 power_state = SATA_PWRMODE_ACTIVE; 4346 4347 scsipkt->pkt_reason = CMD_CMPLT; 4348 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4349 STATE_SENT_CMD | STATE_GOT_STATUS; 4350 4351 switch (power_state) { 4352 case SATA_PWRMODE_ACTIVE: 4353 case SATA_PWRMODE_IDLE: 4354 *scsipkt->pkt_scbp = STATUS_GOOD; 4355 break; 4356 default: 4357 /* PWR mode standby */ 4358 *scsipkt->pkt_scbp = STATUS_CHECK; 4359 sense = sata_arq_sense(spx); 4360 sense->es_key = KEY_NOT_READY; 4361 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 4362 break; 4363 } 4364 4365 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4366 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4367 4368 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4369 scsipkt->pkt_comp != NULL) 4370 /* scsi callback required */ 4371 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4372 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4373 TQ_SLEEP) == NULL) 4374 /* Scheduling the callback failed */ 4375 return (TRAN_BUSY); 4376 4377 return (TRAN_ACCEPT); 4378 } 4379 4380 4381 /* 4382 * SATA translate command: Start Stop Unit 4383 * Translation depends on a command: 4384 * Start Unit translated into Idle Immediate 4385 * Stop Unit translated into Standby Immediate 4386 * Unload Media / NOT SUPPORTED YET 4387 * Load Media / NOT SUPPROTED YET 4388 * Power condition bits are ignored, so is Immediate bit 4389 * Requesting synchronous execution. 4390 * 4391 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4392 * appropriate values in scsi_pkt fields. 4393 */ 4394 static int 4395 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 4396 { 4397 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4398 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4399 struct scsi_extended_sense *sense; 4400 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4401 int cport = SATA_TXLT_CPORT(spx); 4402 int rval; 4403 int synch; 4404 4405 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4406 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 4407 4408 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 4409 4410 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4411 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4412 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4413 return (rval); 4414 } 4415 4416 if (scsipkt->pkt_cdbp[4] & 2) { 4417 /* Load/Unload Media - invalid request */ 4418 *scsipkt->pkt_scbp = STATUS_CHECK; 4419 sense = sata_arq_sense(spx); 4420 sense->es_key = KEY_ILLEGAL_REQUEST; 4421 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4422 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4423 4424 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4425 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4426 4427 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4428 scsipkt->pkt_comp != NULL) 4429 /* scsi callback required */ 4430 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4431 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4432 TQ_SLEEP) == NULL) 4433 /* Scheduling the callback failed */ 4434 return (TRAN_BUSY); 4435 4436 return (TRAN_ACCEPT); 4437 } 4438 scmd->satacmd_addr_type = 0; 4439 scmd->satacmd_sec_count_lsb = 0; 4440 scmd->satacmd_lba_low_lsb = 0; 4441 scmd->satacmd_lba_mid_lsb = 0; 4442 scmd->satacmd_lba_high_lsb = 0; 4443 scmd->satacmd_features_reg = 0; 4444 scmd->satacmd_device_reg = 0; 4445 scmd->satacmd_status_reg = 0; 4446 if (scsipkt->pkt_cdbp[4] & 1) { 4447 /* Start Unit */ 4448 scmd->satacmd_cmd_reg = SATAC_IDLE_IM; 4449 } else { 4450 /* Stop Unit */ 4451 scmd->satacmd_cmd_reg = SATAC_STANDBY_IM; 4452 } 4453 4454 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4455 /* Need to set-up a callback function */ 4456 spx->txlt_sata_pkt->satapkt_comp = 4457 sata_txlt_nodata_cmd_completion; 4458 synch = FALSE; 4459 } else { 4460 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4461 synch = TRUE; 4462 } 4463 4464 /* Transfer command to HBA */ 4465 if (sata_hba_start(spx, &rval) != 0) { 4466 /* Pkt not accepted for execution */ 4467 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4468 return (rval); 4469 } 4470 4471 /* 4472 * If execution is non-synchronous, 4473 * a callback function will handle potential errors, translate 4474 * the response and will do a callback to a target driver. 4475 * If it was synchronous, check execution status using the same 4476 * framework callback. 4477 */ 4478 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4479 if (synch) { 4480 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4481 "synchronous execution status %x\n", 4482 spx->txlt_sata_pkt->satapkt_reason); 4483 4484 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4485 } 4486 return (TRAN_ACCEPT); 4487 4488 } 4489 4490 4491 /* 4492 * SATA translate command: Read Capacity. 4493 * Emulated command for SATA disks. 4494 * Capacity is retrieved from cached Idenifty Device data. 4495 * Identify Device data shows effective disk capacity, not the native 4496 * capacity, which may be limitted by Set Max Address command. 4497 * This is ATA version for SATA hard disks. 4498 * 4499 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4500 */ 4501 static int 4502 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4503 { 4504 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4505 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4506 sata_drive_info_t *sdinfo; 4507 uint64_t val; 4508 uchar_t *rbuf; 4509 int rval; 4510 4511 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4512 "sata_txlt_read_capacity: ", NULL); 4513 4514 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4515 4516 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4517 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4518 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4519 return (rval); 4520 } 4521 4522 scsipkt->pkt_reason = CMD_CMPLT; 4523 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4524 STATE_SENT_CMD | STATE_GOT_STATUS; 4525 *scsipkt->pkt_scbp = STATUS_GOOD; 4526 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4527 /* 4528 * Because it is fully emulated command storing data 4529 * programatically in the specified buffer, release 4530 * preallocated DMA resources before storing data in the buffer, 4531 * so no unwanted DMA sync would take place. 4532 */ 4533 sata_scsi_dmafree(NULL, scsipkt); 4534 4535 sdinfo = sata_get_device_info( 4536 spx->txlt_sata_hba_inst, 4537 &spx->txlt_sata_pkt->satapkt_device); 4538 /* Last logical block address */ 4539 val = sdinfo->satadrv_capacity - 1; 4540 rbuf = (uchar_t *)bp->b_un.b_addr; 4541 /* Need to swap endians to match scsi format */ 4542 rbuf[0] = (val >> 24) & 0xff; 4543 rbuf[1] = (val >> 16) & 0xff; 4544 rbuf[2] = (val >> 8) & 0xff; 4545 rbuf[3] = val & 0xff; 4546 /* block size - always 512 bytes, for now */ 4547 rbuf[4] = 0; 4548 rbuf[5] = 0; 4549 rbuf[6] = 0x02; 4550 rbuf[7] = 0; 4551 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4552 scsipkt->pkt_resid = 0; 4553 4554 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4555 sdinfo->satadrv_capacity -1); 4556 } 4557 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4558 /* 4559 * If a callback was requested, do it now. 4560 */ 4561 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4562 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4563 4564 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4565 scsipkt->pkt_comp != NULL) 4566 /* scsi callback required */ 4567 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4568 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4569 TQ_SLEEP) == NULL) 4570 /* Scheduling the callback failed */ 4571 return (TRAN_BUSY); 4572 4573 return (TRAN_ACCEPT); 4574 } 4575 4576 /* 4577 * SATA translate command: Mode Sense. 4578 * Translated into appropriate SATA command or emulated. 4579 * Saved Values Page Control (03) are not supported. 4580 * 4581 * NOTE: only caching mode sense page is currently implemented. 4582 * 4583 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4584 */ 4585 4586 static int 4587 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4588 { 4589 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4590 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4591 sata_drive_info_t *sdinfo; 4592 sata_id_t *sata_id; 4593 struct scsi_extended_sense *sense; 4594 int len, bdlen, count, alc_len; 4595 int pc; /* Page Control code */ 4596 uint8_t *buf; /* mode sense buffer */ 4597 int rval; 4598 4599 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4600 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 4601 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4602 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4603 4604 buf = kmem_zalloc(1024, KM_SLEEP); 4605 4606 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4607 4608 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4609 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4610 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4611 kmem_free(buf, 1024); 4612 return (rval); 4613 } 4614 4615 scsipkt->pkt_reason = CMD_CMPLT; 4616 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4617 STATE_SENT_CMD | STATE_GOT_STATUS; 4618 4619 pc = scsipkt->pkt_cdbp[2] >> 6; 4620 4621 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4622 /* 4623 * Because it is fully emulated command storing data 4624 * programatically in the specified buffer, release 4625 * preallocated DMA resources before storing data in the buffer, 4626 * so no unwanted DMA sync would take place. 4627 */ 4628 sata_scsi_dmafree(NULL, scsipkt); 4629 4630 len = 0; 4631 bdlen = 0; 4632 if (!(scsipkt->pkt_cdbp[1] & 8)) { 4633 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 4634 (scsipkt->pkt_cdbp[0] & 0x10)) 4635 bdlen = 16; 4636 else 4637 bdlen = 8; 4638 } 4639 /* Build mode parameter header */ 4640 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4641 /* 4-byte mode parameter header */ 4642 buf[len++] = 0; /* mode data length */ 4643 buf[len++] = 0; /* medium type */ 4644 buf[len++] = 0; /* dev-specific param */ 4645 buf[len++] = bdlen; /* Block Descriptor length */ 4646 } else { 4647 /* 8-byte mode parameter header */ 4648 buf[len++] = 0; /* mode data length */ 4649 buf[len++] = 0; 4650 buf[len++] = 0; /* medium type */ 4651 buf[len++] = 0; /* dev-specific param */ 4652 if (bdlen == 16) 4653 buf[len++] = 1; /* long lba descriptor */ 4654 else 4655 buf[len++] = 0; 4656 buf[len++] = 0; 4657 buf[len++] = 0; /* Block Descriptor length */ 4658 buf[len++] = bdlen; 4659 } 4660 4661 sdinfo = sata_get_device_info( 4662 spx->txlt_sata_hba_inst, 4663 &spx->txlt_sata_pkt->satapkt_device); 4664 4665 /* Build block descriptor only if not disabled (DBD) */ 4666 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 4667 /* Block descriptor - direct-access device format */ 4668 if (bdlen == 8) { 4669 /* build regular block descriptor */ 4670 buf[len++] = 4671 (sdinfo->satadrv_capacity >> 24) & 0xff; 4672 buf[len++] = 4673 (sdinfo->satadrv_capacity >> 16) & 0xff; 4674 buf[len++] = 4675 (sdinfo->satadrv_capacity >> 8) & 0xff; 4676 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4677 buf[len++] = 0; /* density code */ 4678 buf[len++] = 0; 4679 if (sdinfo->satadrv_type == 4680 SATA_DTYPE_ATADISK) 4681 buf[len++] = 2; 4682 else 4683 /* ATAPI */ 4684 buf[len++] = 8; 4685 buf[len++] = 0; 4686 } else if (bdlen == 16) { 4687 /* Long LBA Accepted */ 4688 /* build long lba block descriptor */ 4689 #ifndef __lock_lint 4690 buf[len++] = 4691 (sdinfo->satadrv_capacity >> 56) & 0xff; 4692 buf[len++] = 4693 (sdinfo->satadrv_capacity >> 48) & 0xff; 4694 buf[len++] = 4695 (sdinfo->satadrv_capacity >> 40) & 0xff; 4696 buf[len++] = 4697 (sdinfo->satadrv_capacity >> 32) & 0xff; 4698 #endif 4699 buf[len++] = 4700 (sdinfo->satadrv_capacity >> 24) & 0xff; 4701 buf[len++] = 4702 (sdinfo->satadrv_capacity >> 16) & 0xff; 4703 buf[len++] = 4704 (sdinfo->satadrv_capacity >> 8) & 0xff; 4705 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4706 buf[len++] = 0; 4707 buf[len++] = 0; /* density code */ 4708 buf[len++] = 0; 4709 buf[len++] = 0; 4710 if (sdinfo->satadrv_type == 4711 SATA_DTYPE_ATADISK) 4712 buf[len++] = 2; 4713 else 4714 /* ATAPI */ 4715 buf[len++] = 8; 4716 buf[len++] = 0; 4717 } 4718 } 4719 4720 sata_id = &sdinfo->satadrv_id; 4721 4722 /* 4723 * Add requested pages. 4724 * Page 3 and 4 are obsolete and we are not supporting them. 4725 * We deal now with: 4726 * caching (read/write cache control). 4727 * We should eventually deal with following mode pages: 4728 * error recovery (0x01), 4729 * power condition (0x1a), 4730 * exception control page (enables SMART) (0x1c), 4731 * enclosure management (ses), 4732 * protocol-specific port mode (port control). 4733 */ 4734 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 4735 case MODEPAGE_RW_ERRRECOV: 4736 /* DAD_MODE_ERR_RECOV */ 4737 /* R/W recovery */ 4738 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4739 break; 4740 case MODEPAGE_CACHING: 4741 /* DAD_MODE_CACHE */ 4742 /* Reject not supported request for saved parameters */ 4743 if (pc == 3) { 4744 *scsipkt->pkt_scbp = STATUS_CHECK; 4745 sense = sata_arq_sense(spx); 4746 sense->es_key = KEY_ILLEGAL_REQUEST; 4747 sense->es_add_code = 4748 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 4749 goto done; 4750 } 4751 4752 /* caching */ 4753 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4754 break; 4755 case MODEPAGE_INFO_EXCPT: 4756 /* exception cntrl */ 4757 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4758 len += sata_build_msense_page_1c(sdinfo, pc, 4759 buf+len); 4760 } 4761 else 4762 goto err; 4763 break; 4764 case MODEPAGE_POWER_COND: 4765 /* DAD_MODE_POWER_COND */ 4766 /* power condition */ 4767 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4768 break; 4769 4770 case MODEPAGE_ACOUSTIC_MANAG: 4771 /* acoustic management */ 4772 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4773 break; 4774 case MODEPAGE_ALLPAGES: 4775 /* all pages */ 4776 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4777 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4778 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4779 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4780 len += sata_build_msense_page_1c(sdinfo, pc, 4781 buf+len); 4782 } 4783 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4784 break; 4785 default: 4786 err: 4787 /* Invalid request */ 4788 *scsipkt->pkt_scbp = STATUS_CHECK; 4789 sense = sata_arq_sense(spx); 4790 sense->es_key = KEY_ILLEGAL_REQUEST; 4791 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4792 goto done; 4793 } 4794 4795 /* fix total mode data length */ 4796 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4797 /* 4-byte mode parameter header */ 4798 buf[0] = len - 1; /* mode data length */ 4799 } else { 4800 buf[0] = (len -2) >> 8; 4801 buf[1] = (len -2) & 0xff; 4802 } 4803 4804 4805 /* Check allocation length */ 4806 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4807 alc_len = scsipkt->pkt_cdbp[4]; 4808 } else { 4809 alc_len = scsipkt->pkt_cdbp[7]; 4810 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4811 } 4812 /* 4813 * We do not check for possible parameters truncation 4814 * (alc_len < len) assuming that the target driver works 4815 * correctly. Just avoiding overrun. 4816 * Copy no more than requested and possible, buffer-wise. 4817 */ 4818 count = MIN(alc_len, len); 4819 count = MIN(bp->b_bcount, count); 4820 bcopy(buf, bp->b_un.b_addr, count); 4821 4822 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4823 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4824 } 4825 *scsipkt->pkt_scbp = STATUS_GOOD; 4826 done: 4827 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4828 (void) kmem_free(buf, 1024); 4829 4830 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4831 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4832 4833 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4834 scsipkt->pkt_comp != NULL) 4835 /* scsi callback required */ 4836 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4837 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4838 TQ_SLEEP) == NULL) 4839 /* Scheduling the callback failed */ 4840 return (TRAN_BUSY); 4841 4842 return (TRAN_ACCEPT); 4843 } 4844 4845 4846 /* 4847 * SATA translate command: Mode Select. 4848 * Translated into appropriate SATA command or emulated. 4849 * Saving parameters is not supported. 4850 * Changing device capacity is not supported (although theoretically 4851 * possible by executing SET FEATURES/SET MAX ADDRESS) 4852 * 4853 * Assumption is that the target driver is working correctly. 4854 * 4855 * More than one SATA command may be executed to perform operations specified 4856 * by mode select pages. The first error terminates further execution. 4857 * Operations performed successully are not backed-up in such case. 4858 * 4859 * NOTE: only caching mode select page is implemented. 4860 * Caching setup is remembered so it could be re-stored in case of 4861 * an unexpected device reset. 4862 * 4863 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4864 */ 4865 4866 static int 4867 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 4868 { 4869 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4870 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4871 struct scsi_extended_sense *sense; 4872 int len, pagelen, count, pllen; 4873 uint8_t *buf; /* mode select buffer */ 4874 int rval, stat; 4875 uint_t nointr_flag; 4876 int dmod = 0; 4877 4878 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4879 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 4880 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4881 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4882 4883 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4884 4885 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4886 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4887 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4888 return (rval); 4889 } 4890 4891 rval = TRAN_ACCEPT; 4892 4893 scsipkt->pkt_reason = CMD_CMPLT; 4894 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4895 STATE_SENT_CMD | STATE_GOT_STATUS; 4896 4897 /* Reject not supported request */ 4898 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 4899 *scsipkt->pkt_scbp = STATUS_CHECK; 4900 sense = sata_arq_sense(spx); 4901 sense->es_key = KEY_ILLEGAL_REQUEST; 4902 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4903 goto done; 4904 } 4905 4906 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4907 pllen = scsipkt->pkt_cdbp[4]; 4908 } else { 4909 pllen = scsipkt->pkt_cdbp[7]; 4910 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 4911 } 4912 4913 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4914 4915 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 4916 buf = (uint8_t *)bp->b_un.b_addr; 4917 count = MIN(bp->b_bcount, pllen); 4918 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4919 scsipkt->pkt_resid = 0; 4920 pllen = count; 4921 4922 /* 4923 * Check the header to skip the block descriptor(s) - we 4924 * do not support setting device capacity. 4925 * Existing macros do not recognize long LBA dscriptor, 4926 * hence manual calculation. 4927 */ 4928 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4929 /* 6-bytes CMD, 4 bytes header */ 4930 if (count <= 4) 4931 goto done; /* header only */ 4932 len = buf[3] + 4; 4933 } else { 4934 /* 10-bytes CMD, 8 bytes header */ 4935 if (count <= 8) 4936 goto done; /* header only */ 4937 len = buf[6]; 4938 len = (len << 8) + buf[7] + 8; 4939 } 4940 if (len >= count) 4941 goto done; /* header + descriptor(s) only */ 4942 4943 pllen -= len; /* remaining data length */ 4944 4945 /* 4946 * We may be executing SATA command and want to execute it 4947 * in SYNCH mode, regardless of scsi_pkt setting. 4948 * Save scsi_pkt setting and indicate SYNCH mode 4949 */ 4950 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 4951 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4952 scsipkt->pkt_comp != NULL) { 4953 scsipkt->pkt_flags |= FLAG_NOINTR; 4954 } 4955 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4956 4957 /* 4958 * len is now the offset to a first mode select page 4959 * Process all pages 4960 */ 4961 while (pllen > 0) { 4962 switch ((int)buf[len]) { 4963 case MODEPAGE_CACHING: 4964 /* No support for SP (saving) */ 4965 if (scsipkt->pkt_cdbp[1] & 0x01) { 4966 *scsipkt->pkt_scbp = STATUS_CHECK; 4967 sense = sata_arq_sense(spx); 4968 sense->es_key = KEY_ILLEGAL_REQUEST; 4969 sense->es_add_code = 4970 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4971 goto done; 4972 } 4973 stat = sata_mode_select_page_8(spx, 4974 (struct mode_cache_scsi3 *)&buf[len], 4975 pllen, &pagelen, &rval, &dmod); 4976 /* 4977 * The pagelen value indicates the number of 4978 * parameter bytes already processed. 4979 * The rval is the return value from 4980 * sata_tran_start(). 4981 * The stat indicates the overall status of 4982 * the operation(s). 4983 */ 4984 if (stat != SATA_SUCCESS) 4985 /* 4986 * Page processing did not succeed - 4987 * all error info is already set-up, 4988 * just return 4989 */ 4990 pllen = 0; /* this breaks the loop */ 4991 else { 4992 len += pagelen; 4993 pllen -= pagelen; 4994 } 4995 break; 4996 4997 case MODEPAGE_INFO_EXCPT: 4998 stat = sata_mode_select_page_1c(spx, 4999 (struct mode_info_excpt_page *)&buf[len], 5000 pllen, &pagelen, &rval, &dmod); 5001 /* 5002 * The pagelen value indicates the number of 5003 * parameter bytes already processed. 5004 * The rval is the return value from 5005 * sata_tran_start(). 5006 * The stat indicates the overall status of 5007 * the operation(s). 5008 */ 5009 if (stat != SATA_SUCCESS) 5010 /* 5011 * Page processing did not succeed - 5012 * all error info is already set-up, 5013 * just return 5014 */ 5015 pllen = 0; /* this breaks the loop */ 5016 else { 5017 len += pagelen; 5018 pllen -= pagelen; 5019 } 5020 break; 5021 5022 case MODEPAGE_ACOUSTIC_MANAG: 5023 stat = sata_mode_select_page_30(spx, 5024 (struct mode_acoustic_management *) 5025 &buf[len], pllen, &pagelen, &rval, &dmod); 5026 /* 5027 * The pagelen value indicates the number of 5028 * parameter bytes already processed. 5029 * The rval is the return value from 5030 * sata_tran_start(). 5031 * The stat indicates the overall status of 5032 * the operation(s). 5033 */ 5034 if (stat != SATA_SUCCESS) 5035 /* 5036 * Page processing did not succeed - 5037 * all error info is already set-up, 5038 * just return 5039 */ 5040 pllen = 0; /* this breaks the loop */ 5041 else { 5042 len += pagelen; 5043 pllen -= pagelen; 5044 } 5045 5046 break; 5047 default: 5048 *scsipkt->pkt_scbp = STATUS_CHECK; 5049 sense = sata_arq_sense(spx); 5050 sense->es_key = KEY_ILLEGAL_REQUEST; 5051 sense->es_add_code = 5052 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 5053 goto done; 5054 } 5055 } 5056 } 5057 done: 5058 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5059 /* 5060 * If device parameters were modified, fetch and store the new 5061 * Identify Device data. Since port mutex could have been released 5062 * for accessing HBA driver, we need to re-check device existence. 5063 */ 5064 if (dmod != 0) { 5065 sata_drive_info_t new_sdinfo, *sdinfo; 5066 int rv; 5067 5068 new_sdinfo.satadrv_addr = 5069 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 5070 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 5071 &new_sdinfo); 5072 5073 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5074 /* 5075 * Since port mutex could have been released when 5076 * accessing HBA driver, we need to re-check that the 5077 * framework still holds the device info structure. 5078 */ 5079 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5080 &spx->txlt_sata_pkt->satapkt_device); 5081 if (sdinfo != NULL) { 5082 /* 5083 * Device still has info structure in the 5084 * sata framework. Copy newly fetched info 5085 */ 5086 if (rv == 0) { 5087 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 5088 sata_save_drive_settings(sdinfo); 5089 } else { 5090 /* 5091 * Could not fetch new data - invalidate 5092 * sata_drive_info. That makes device 5093 * unusable. 5094 */ 5095 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 5096 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 5097 } 5098 } 5099 if (rv != 0 || sdinfo == NULL) { 5100 /* 5101 * This changes the overall mode select completion 5102 * reason to a failed one !!!!! 5103 */ 5104 *scsipkt->pkt_scbp = STATUS_CHECK; 5105 sense = sata_arq_sense(spx); 5106 scsipkt->pkt_reason = CMD_INCOMPLETE; 5107 rval = TRAN_ACCEPT; 5108 } 5109 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5110 } 5111 /* Restore the scsi pkt flags */ 5112 scsipkt->pkt_flags &= ~FLAG_NOINTR; 5113 scsipkt->pkt_flags |= nointr_flag; 5114 5115 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5116 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5117 5118 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5119 scsipkt->pkt_comp != NULL) 5120 /* scsi callback required */ 5121 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5122 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5123 TQ_SLEEP) == NULL) 5124 /* Scheduling the callback failed */ 5125 return (TRAN_BUSY); 5126 5127 return (rval); 5128 } 5129 5130 5131 5132 /* 5133 * Translate command: Log Sense 5134 */ 5135 static int 5136 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 5137 { 5138 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5139 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5140 sata_drive_info_t *sdinfo; 5141 struct scsi_extended_sense *sense; 5142 int len, count, alc_len; 5143 int pc; /* Page Control code */ 5144 int page_code; /* Page code */ 5145 uint8_t *buf; /* log sense buffer */ 5146 int rval; 5147 #define MAX_LOG_SENSE_PAGE_SIZE 512 5148 5149 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5150 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 5151 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5152 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5153 5154 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 5155 5156 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5157 5158 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5159 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5160 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5161 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5162 return (rval); 5163 } 5164 5165 scsipkt->pkt_reason = CMD_CMPLT; 5166 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5167 STATE_SENT_CMD | STATE_GOT_STATUS; 5168 5169 pc = scsipkt->pkt_cdbp[2] >> 6; 5170 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 5171 5172 /* Reject not supported request for all but cumulative values */ 5173 switch (pc) { 5174 case PC_CUMULATIVE_VALUES: 5175 break; 5176 default: 5177 *scsipkt->pkt_scbp = STATUS_CHECK; 5178 sense = sata_arq_sense(spx); 5179 sense->es_key = KEY_ILLEGAL_REQUEST; 5180 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5181 goto done; 5182 } 5183 5184 switch (page_code) { 5185 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5186 case PAGE_CODE_SELF_TEST_RESULTS: 5187 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5188 case PAGE_CODE_SMART_READ_DATA: 5189 break; 5190 default: 5191 *scsipkt->pkt_scbp = STATUS_CHECK; 5192 sense = sata_arq_sense(spx); 5193 sense->es_key = KEY_ILLEGAL_REQUEST; 5194 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5195 goto done; 5196 } 5197 5198 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5199 /* 5200 * Because log sense uses local buffers for data retrieval from 5201 * the devices and sets the data programatically in the 5202 * original specified buffer, release preallocated DMA 5203 * resources before storing data in the original buffer, 5204 * so no unwanted DMA sync would take place. 5205 */ 5206 sata_id_t *sata_id; 5207 5208 sata_scsi_dmafree(NULL, scsipkt); 5209 5210 len = 0; 5211 5212 /* Build log parameter header */ 5213 buf[len++] = page_code; /* page code as in the CDB */ 5214 buf[len++] = 0; /* reserved */ 5215 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5216 buf[len++] = 0; /* (LSB) */ 5217 5218 sdinfo = sata_get_device_info( 5219 spx->txlt_sata_hba_inst, 5220 &spx->txlt_sata_pkt->satapkt_device); 5221 5222 5223 /* 5224 * Add requested pages. 5225 */ 5226 switch (page_code) { 5227 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5228 len = sata_build_lsense_page_0(sdinfo, buf + len); 5229 break; 5230 case PAGE_CODE_SELF_TEST_RESULTS: 5231 sata_id = &sdinfo->satadrv_id; 5232 if ((! (sata_id->ai_cmdset84 & 5233 SATA_SMART_SELF_TEST_SUPPORTED)) || 5234 (! (sata_id->ai_features87 & 5235 SATA_SMART_SELF_TEST_SUPPORTED))) { 5236 *scsipkt->pkt_scbp = STATUS_CHECK; 5237 sense = sata_arq_sense(spx); 5238 sense->es_key = KEY_ILLEGAL_REQUEST; 5239 sense->es_add_code = 5240 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5241 5242 goto done; 5243 } 5244 len = sata_build_lsense_page_10(sdinfo, buf + len, 5245 spx->txlt_sata_hba_inst); 5246 break; 5247 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5248 sata_id = &sdinfo->satadrv_id; 5249 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5250 *scsipkt->pkt_scbp = STATUS_CHECK; 5251 sense = sata_arq_sense(spx); 5252 sense->es_key = KEY_ILLEGAL_REQUEST; 5253 sense->es_add_code = 5254 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5255 5256 goto done; 5257 } 5258 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5259 *scsipkt->pkt_scbp = STATUS_CHECK; 5260 sense = sata_arq_sense(spx); 5261 sense->es_key = KEY_ABORTED_COMMAND; 5262 sense->es_add_code = 5263 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5264 sense->es_qual_code = 5265 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5266 5267 goto done; 5268 } 5269 5270 len = sata_build_lsense_page_2f(sdinfo, buf + len, 5271 spx->txlt_sata_hba_inst); 5272 break; 5273 case PAGE_CODE_SMART_READ_DATA: 5274 sata_id = &sdinfo->satadrv_id; 5275 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5276 *scsipkt->pkt_scbp = STATUS_CHECK; 5277 sense = sata_arq_sense(spx); 5278 sense->es_key = KEY_ILLEGAL_REQUEST; 5279 sense->es_add_code = 5280 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5281 5282 goto done; 5283 } 5284 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5285 *scsipkt->pkt_scbp = STATUS_CHECK; 5286 sense = sata_arq_sense(spx); 5287 sense->es_key = KEY_ABORTED_COMMAND; 5288 sense->es_add_code = 5289 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5290 sense->es_qual_code = 5291 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5292 5293 goto done; 5294 } 5295 5296 /* This page doesn't include a page header */ 5297 len = sata_build_lsense_page_30(sdinfo, buf, 5298 spx->txlt_sata_hba_inst); 5299 goto no_header; 5300 default: 5301 /* Invalid request */ 5302 *scsipkt->pkt_scbp = STATUS_CHECK; 5303 sense = sata_arq_sense(spx); 5304 sense->es_key = KEY_ILLEGAL_REQUEST; 5305 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5306 goto done; 5307 } 5308 5309 /* set parameter log sense data length */ 5310 buf[2] = len >> 8; /* log sense length (MSB) */ 5311 buf[3] = len & 0xff; /* log sense length (LSB) */ 5312 5313 len += SCSI_LOG_PAGE_HDR_LEN; 5314 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 5315 5316 no_header: 5317 /* Check allocation length */ 5318 alc_len = scsipkt->pkt_cdbp[7]; 5319 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5320 5321 /* 5322 * We do not check for possible parameters truncation 5323 * (alc_len < len) assuming that the target driver works 5324 * correctly. Just avoiding overrun. 5325 * Copy no more than requested and possible, buffer-wise. 5326 */ 5327 count = MIN(alc_len, len); 5328 count = MIN(bp->b_bcount, count); 5329 bcopy(buf, bp->b_un.b_addr, count); 5330 5331 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5332 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5333 } 5334 *scsipkt->pkt_scbp = STATUS_GOOD; 5335 done: 5336 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5337 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5338 5339 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5340 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5341 5342 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5343 scsipkt->pkt_comp != NULL) 5344 /* scsi callback required */ 5345 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5346 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5347 TQ_SLEEP) == NULL) 5348 /* Scheduling the callback failed */ 5349 return (TRAN_BUSY); 5350 5351 return (TRAN_ACCEPT); 5352 } 5353 5354 /* 5355 * Translate command: Log Select 5356 * Not implemented at this time - returns invalid command response. 5357 */ 5358 static int 5359 sata_txlt_log_select(sata_pkt_txlate_t *spx) 5360 { 5361 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5362 "sata_txlt_log_select\n", NULL); 5363 5364 return (sata_txlt_invalid_command(spx)); 5365 } 5366 5367 5368 /* 5369 * Translate command: Read (various types). 5370 * Translated into appropriate type of ATA READ command 5371 * for SATA hard disks. 5372 * Both the device capabilities and requested operation mode are 5373 * considered. 5374 * 5375 * Following scsi cdb fields are ignored: 5376 * rdprotect, dpo, fua, fua_nv, group_number. 5377 * 5378 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5379 * enable variable sata_func_enable), the capability of the controller and 5380 * capability of a device are checked and if both support queueing, read 5381 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 5382 * command rather than plain READ_XXX command. 5383 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5384 * both the controller and device suport such functionality, the read 5385 * request will be translated to READ_FPDMA_QUEUED command. 5386 * In both cases the maximum queue depth is derived as minimum of: 5387 * HBA capability,device capability and sata_max_queue_depth variable setting. 5388 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5389 * used to pass max queue depth value, and the maximum possible queue depth 5390 * is 32. 5391 * 5392 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5393 * appropriate values in scsi_pkt fields. 5394 */ 5395 static int 5396 sata_txlt_read(sata_pkt_txlate_t *spx) 5397 { 5398 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5399 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5400 sata_drive_info_t *sdinfo; 5401 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5402 int cport = SATA_TXLT_CPORT(spx); 5403 uint16_t sec_count; 5404 uint64_t lba; 5405 int rval; 5406 int synch; 5407 5408 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5409 5410 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5411 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5412 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5413 return (rval); 5414 } 5415 5416 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5417 &spx->txlt_sata_pkt->satapkt_device); 5418 5419 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5420 /* 5421 * Extract LBA and sector count from scsi CDB. 5422 */ 5423 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5424 case SCMD_READ: 5425 /* 6-byte scsi read cmd : 0x08 */ 5426 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5427 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5428 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5429 sec_count = scsipkt->pkt_cdbp[4]; 5430 /* sec_count 0 will be interpreted as 256 by a device */ 5431 break; 5432 case SCMD_READ_G1: 5433 /* 10-bytes scsi read command : 0x28 */ 5434 lba = scsipkt->pkt_cdbp[2]; 5435 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5436 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5437 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5438 sec_count = scsipkt->pkt_cdbp[7]; 5439 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5440 break; 5441 case SCMD_READ_G5: 5442 /* 12-bytes scsi read command : 0xA8 */ 5443 lba = scsipkt->pkt_cdbp[2]; 5444 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5445 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5446 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5447 sec_count = scsipkt->pkt_cdbp[6]; 5448 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5449 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5450 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5451 break; 5452 case SCMD_READ_G4: 5453 /* 16-bytes scsi read command : 0x88 */ 5454 lba = scsipkt->pkt_cdbp[2]; 5455 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5456 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5457 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5458 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5459 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5460 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5461 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5462 sec_count = scsipkt->pkt_cdbp[10]; 5463 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5464 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5465 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5466 break; 5467 default: 5468 /* Unsupported command */ 5469 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5470 return (sata_txlt_invalid_command(spx)); 5471 } 5472 5473 /* 5474 * Check if specified address exceeds device capacity 5475 */ 5476 if ((lba >= sdinfo->satadrv_capacity) || 5477 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5478 /* LBA out of range */ 5479 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5480 return (sata_txlt_lba_out_of_range(spx)); 5481 } 5482 5483 /* 5484 * For zero-length transfer, emulate good completion of the command 5485 * (reasons for rejecting the command were already checked). 5486 * No DMA resources were allocated. 5487 */ 5488 if (spx->txlt_dma_cookie_list == NULL) { 5489 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5490 return (sata_emul_rw_completion(spx)); 5491 } 5492 5493 /* 5494 * Build cmd block depending on the device capability and 5495 * requested operation mode. 5496 * Do not bother with non-dma mode - we are working only with 5497 * devices supporting DMA. 5498 */ 5499 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5500 scmd->satacmd_device_reg = SATA_ADH_LBA; 5501 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 5502 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5503 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5504 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 5505 scmd->satacmd_sec_count_msb = sec_count >> 8; 5506 #ifndef __lock_lint 5507 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5508 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5509 scmd->satacmd_lba_high_msb = lba >> 40; 5510 #endif 5511 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5512 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5513 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5514 } 5515 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5516 scmd->satacmd_lba_low_lsb = lba & 0xff; 5517 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5518 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5519 scmd->satacmd_features_reg = 0; 5520 scmd->satacmd_status_reg = 0; 5521 scmd->satacmd_error_reg = 0; 5522 5523 /* 5524 * Check if queueing commands should be used and switch 5525 * to appropriate command if possible 5526 */ 5527 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5528 boolean_t using_queuing; 5529 5530 /* Queuing supported by controller and device? */ 5531 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5532 (sdinfo->satadrv_features_support & 5533 SATA_DEV_F_NCQ) && 5534 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5535 SATA_CTLF_NCQ)) { 5536 using_queuing = B_TRUE; 5537 5538 /* NCQ supported - use FPDMA READ */ 5539 scmd->satacmd_cmd_reg = 5540 SATAC_READ_FPDMA_QUEUED; 5541 scmd->satacmd_features_reg_ext = 5542 scmd->satacmd_sec_count_msb; 5543 scmd->satacmd_sec_count_msb = 0; 5544 } else if ((sdinfo->satadrv_features_support & 5545 SATA_DEV_F_TCQ) && 5546 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5547 SATA_CTLF_QCMD)) { 5548 using_queuing = B_TRUE; 5549 5550 /* Legacy queueing */ 5551 if (sdinfo->satadrv_features_support & 5552 SATA_DEV_F_LBA48) { 5553 scmd->satacmd_cmd_reg = 5554 SATAC_READ_DMA_QUEUED_EXT; 5555 scmd->satacmd_features_reg_ext = 5556 scmd->satacmd_sec_count_msb; 5557 scmd->satacmd_sec_count_msb = 0; 5558 } else { 5559 scmd->satacmd_cmd_reg = 5560 SATAC_READ_DMA_QUEUED; 5561 } 5562 } else /* NCQ nor legacy queuing not supported */ 5563 using_queuing = B_FALSE; 5564 5565 /* 5566 * If queuing, the sector count goes in the features register 5567 * and the secount count will contain the tag. 5568 */ 5569 if (using_queuing) { 5570 scmd->satacmd_features_reg = 5571 scmd->satacmd_sec_count_lsb; 5572 scmd->satacmd_sec_count_lsb = 0; 5573 scmd->satacmd_flags.sata_queued = B_TRUE; 5574 5575 /* Set-up maximum queue depth */ 5576 scmd->satacmd_flags.sata_max_queue_depth = 5577 sdinfo->satadrv_max_queue_depth - 1; 5578 } else if (sdinfo->satadrv_features_enabled & 5579 SATA_DEV_F_E_UNTAGGED_QING) { 5580 /* 5581 * Although NCQ/TCQ is not enabled, untagged queuing 5582 * may be still used. 5583 * Set-up the maximum untagged queue depth. 5584 * Use controller's queue depth from sata_hba_tran. 5585 * SATA HBA drivers may ignore this value and rely on 5586 * the internal limits.For drivers that do not 5587 * ignore untaged queue depth, limit the value to 5588 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5589 * largest value that can be passed via 5590 * satacmd_flags.sata_max_queue_depth. 5591 */ 5592 scmd->satacmd_flags.sata_max_queue_depth = 5593 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5594 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5595 5596 } else { 5597 scmd->satacmd_flags.sata_max_queue_depth = 0; 5598 } 5599 } else 5600 scmd->satacmd_flags.sata_max_queue_depth = 0; 5601 5602 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 5603 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 5604 scmd->satacmd_cmd_reg, lba, sec_count); 5605 5606 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5607 /* Need callback function */ 5608 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5609 synch = FALSE; 5610 } else 5611 synch = TRUE; 5612 5613 /* Transfer command to HBA */ 5614 if (sata_hba_start(spx, &rval) != 0) { 5615 /* Pkt not accepted for execution */ 5616 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5617 return (rval); 5618 } 5619 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5620 /* 5621 * If execution is non-synchronous, 5622 * a callback function will handle potential errors, translate 5623 * the response and will do a callback to a target driver. 5624 * If it was synchronous, check execution status using the same 5625 * framework callback. 5626 */ 5627 if (synch) { 5628 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5629 "synchronous execution status %x\n", 5630 spx->txlt_sata_pkt->satapkt_reason); 5631 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5632 } 5633 return (TRAN_ACCEPT); 5634 } 5635 5636 5637 /* 5638 * SATA translate command: Write (various types) 5639 * Translated into appropriate type of ATA WRITE command 5640 * for SATA hard disks. 5641 * Both the device capabilities and requested operation mode are 5642 * considered. 5643 * 5644 * Following scsi cdb fields are ignored: 5645 * rwprotect, dpo, fua, fua_nv, group_number. 5646 * 5647 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5648 * enable variable sata_func_enable), the capability of the controller and 5649 * capability of a device are checked and if both support queueing, write 5650 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 5651 * command rather than plain WRITE_XXX command. 5652 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5653 * both the controller and device suport such functionality, the write 5654 * request will be translated to WRITE_FPDMA_QUEUED command. 5655 * In both cases the maximum queue depth is derived as minimum of: 5656 * HBA capability,device capability and sata_max_queue_depth variable setting. 5657 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5658 * used to pass max queue depth value, and the maximum possible queue depth 5659 * is 32. 5660 * 5661 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5662 * appropriate values in scsi_pkt fields. 5663 */ 5664 static int 5665 sata_txlt_write(sata_pkt_txlate_t *spx) 5666 { 5667 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5668 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5669 sata_drive_info_t *sdinfo; 5670 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5671 int cport = SATA_TXLT_CPORT(spx); 5672 uint16_t sec_count; 5673 uint64_t lba; 5674 int rval; 5675 int synch; 5676 5677 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5678 5679 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5680 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5681 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5682 return (rval); 5683 } 5684 5685 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5686 &spx->txlt_sata_pkt->satapkt_device); 5687 5688 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5689 /* 5690 * Extract LBA and sector count from scsi CDB 5691 */ 5692 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5693 case SCMD_WRITE: 5694 /* 6-byte scsi read cmd : 0x0A */ 5695 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5696 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5697 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5698 sec_count = scsipkt->pkt_cdbp[4]; 5699 /* sec_count 0 will be interpreted as 256 by a device */ 5700 break; 5701 case SCMD_WRITE_G1: 5702 /* 10-bytes scsi write command : 0x2A */ 5703 lba = scsipkt->pkt_cdbp[2]; 5704 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5705 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5706 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5707 sec_count = scsipkt->pkt_cdbp[7]; 5708 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5709 break; 5710 case SCMD_WRITE_G5: 5711 /* 12-bytes scsi read command : 0xAA */ 5712 lba = scsipkt->pkt_cdbp[2]; 5713 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5714 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5715 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5716 sec_count = scsipkt->pkt_cdbp[6]; 5717 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5718 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5719 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5720 break; 5721 case SCMD_WRITE_G4: 5722 /* 16-bytes scsi write command : 0x8A */ 5723 lba = scsipkt->pkt_cdbp[2]; 5724 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5725 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5726 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5727 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5728 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5729 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5730 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5731 sec_count = scsipkt->pkt_cdbp[10]; 5732 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5733 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5734 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5735 break; 5736 default: 5737 /* Unsupported command */ 5738 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5739 return (sata_txlt_invalid_command(spx)); 5740 } 5741 5742 /* 5743 * Check if specified address and length exceeds device capacity 5744 */ 5745 if ((lba >= sdinfo->satadrv_capacity) || 5746 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5747 /* LBA out of range */ 5748 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5749 return (sata_txlt_lba_out_of_range(spx)); 5750 } 5751 5752 /* 5753 * For zero-length transfer, emulate good completion of the command 5754 * (reasons for rejecting the command were already checked). 5755 * No DMA resources were allocated. 5756 */ 5757 if (spx->txlt_dma_cookie_list == NULL) { 5758 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5759 return (sata_emul_rw_completion(spx)); 5760 } 5761 5762 /* 5763 * Build cmd block depending on the device capability and 5764 * requested operation mode. 5765 * Do not bother with non-dma mode- we are working only with 5766 * devices supporting DMA. 5767 */ 5768 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5769 scmd->satacmd_device_reg = SATA_ADH_LBA; 5770 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 5771 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5772 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5773 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 5774 scmd->satacmd_sec_count_msb = sec_count >> 8; 5775 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5776 #ifndef __lock_lint 5777 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5778 scmd->satacmd_lba_high_msb = lba >> 40; 5779 #endif 5780 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5781 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5782 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5783 } 5784 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5785 scmd->satacmd_lba_low_lsb = lba & 0xff; 5786 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5787 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5788 scmd->satacmd_features_reg = 0; 5789 scmd->satacmd_status_reg = 0; 5790 scmd->satacmd_error_reg = 0; 5791 5792 /* 5793 * Check if queueing commands should be used and switch 5794 * to appropriate command if possible 5795 */ 5796 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5797 boolean_t using_queuing; 5798 5799 /* Queuing supported by controller and device? */ 5800 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5801 (sdinfo->satadrv_features_support & 5802 SATA_DEV_F_NCQ) && 5803 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5804 SATA_CTLF_NCQ)) { 5805 using_queuing = B_TRUE; 5806 5807 /* NCQ supported - use FPDMA WRITE */ 5808 scmd->satacmd_cmd_reg = 5809 SATAC_WRITE_FPDMA_QUEUED; 5810 scmd->satacmd_features_reg_ext = 5811 scmd->satacmd_sec_count_msb; 5812 scmd->satacmd_sec_count_msb = 0; 5813 } else if ((sdinfo->satadrv_features_support & 5814 SATA_DEV_F_TCQ) && 5815 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5816 SATA_CTLF_QCMD)) { 5817 using_queuing = B_TRUE; 5818 5819 /* Legacy queueing */ 5820 if (sdinfo->satadrv_features_support & 5821 SATA_DEV_F_LBA48) { 5822 scmd->satacmd_cmd_reg = 5823 SATAC_WRITE_DMA_QUEUED_EXT; 5824 scmd->satacmd_features_reg_ext = 5825 scmd->satacmd_sec_count_msb; 5826 scmd->satacmd_sec_count_msb = 0; 5827 } else { 5828 scmd->satacmd_cmd_reg = 5829 SATAC_WRITE_DMA_QUEUED; 5830 } 5831 } else /* NCQ nor legacy queuing not supported */ 5832 using_queuing = B_FALSE; 5833 5834 if (using_queuing) { 5835 scmd->satacmd_features_reg = 5836 scmd->satacmd_sec_count_lsb; 5837 scmd->satacmd_sec_count_lsb = 0; 5838 scmd->satacmd_flags.sata_queued = B_TRUE; 5839 /* Set-up maximum queue depth */ 5840 scmd->satacmd_flags.sata_max_queue_depth = 5841 sdinfo->satadrv_max_queue_depth - 1; 5842 } else if (sdinfo->satadrv_features_enabled & 5843 SATA_DEV_F_E_UNTAGGED_QING) { 5844 /* 5845 * Although NCQ/TCQ is not enabled, untagged queuing 5846 * may be still used. 5847 * Set-up the maximum untagged queue depth. 5848 * Use controller's queue depth from sata_hba_tran. 5849 * SATA HBA drivers may ignore this value and rely on 5850 * the internal limits. For drivera that do not 5851 * ignore untaged queue depth, limit the value to 5852 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5853 * largest value that can be passed via 5854 * satacmd_flags.sata_max_queue_depth. 5855 */ 5856 scmd->satacmd_flags.sata_max_queue_depth = 5857 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5858 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5859 5860 } else { 5861 scmd->satacmd_flags.sata_max_queue_depth = 0; 5862 } 5863 } else 5864 scmd->satacmd_flags.sata_max_queue_depth = 0; 5865 5866 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5867 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 5868 scmd->satacmd_cmd_reg, lba, sec_count); 5869 5870 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5871 /* Need callback function */ 5872 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5873 synch = FALSE; 5874 } else 5875 synch = TRUE; 5876 5877 /* Transfer command to HBA */ 5878 if (sata_hba_start(spx, &rval) != 0) { 5879 /* Pkt not accepted for execution */ 5880 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5881 return (rval); 5882 } 5883 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5884 5885 /* 5886 * If execution is non-synchronous, 5887 * a callback function will handle potential errors, translate 5888 * the response and will do a callback to a target driver. 5889 * If it was synchronous, check execution status using the same 5890 * framework callback. 5891 */ 5892 if (synch) { 5893 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5894 "synchronous execution status %x\n", 5895 spx->txlt_sata_pkt->satapkt_reason); 5896 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5897 } 5898 return (TRAN_ACCEPT); 5899 } 5900 5901 5902 /* 5903 * Implements SCSI SBC WRITE BUFFER command download microcode option 5904 */ 5905 static int 5906 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 5907 { 5908 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 5909 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 5910 5911 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5912 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5913 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5914 struct scsi_extended_sense *sense; 5915 int rval, mode, sector_count; 5916 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5917 int cport = SATA_TXLT_CPORT(spx); 5918 boolean_t synch; 5919 5920 synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0; 5921 mode = scsipkt->pkt_cdbp[1] & 0x1f; 5922 5923 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5924 "sata_txlt_write_buffer, mode 0x%x\n", mode); 5925 5926 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5927 5928 if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) { 5929 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5930 return (rval); 5931 } 5932 5933 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5934 5935 scsipkt->pkt_reason = CMD_CMPLT; 5936 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5937 STATE_SENT_CMD | STATE_GOT_STATUS; 5938 5939 /* 5940 * The SCSI to ATA translation specification only calls 5941 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5942 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5943 * ATA 8 (draft) got rid of download microcode for temp 5944 * and it is even optional for ATA 7, so it may be aborted. 5945 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5946 * it is not specified and the buffer offset for SCSI is a 16-bit 5947 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5948 * sectors. Thus the offset really doesn't buy us anything. 5949 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5950 * is revised, this can be revisisted. 5951 */ 5952 /* Reject not supported request */ 5953 switch (mode) { 5954 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5955 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5956 break; 5957 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5958 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5959 break; 5960 default: 5961 goto bad_param; 5962 } 5963 5964 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5965 5966 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5967 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5968 goto bad_param; 5969 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5970 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5971 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5972 scmd->satacmd_lba_mid_lsb = 0; 5973 scmd->satacmd_lba_high_lsb = 0; 5974 scmd->satacmd_device_reg = 0; 5975 spx->txlt_sata_pkt->satapkt_comp = 5976 sata_txlt_download_mcode_cmd_completion; 5977 scmd->satacmd_addr_type = 0; 5978 5979 /* Transfer command to HBA */ 5980 if (sata_hba_start(spx, &rval) != 0) { 5981 /* Pkt not accepted for execution */ 5982 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5983 return (rval); 5984 } 5985 5986 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5987 /* 5988 * If execution is non-synchronous, 5989 * a callback function will handle potential errors, translate 5990 * the response and will do a callback to a target driver. 5991 * If it was synchronous, check execution status using the same 5992 * framework callback. 5993 */ 5994 if (synch) { 5995 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5996 "synchronous execution\n", NULL); 5997 /* Calling pre-set completion routine */ 5998 (*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt); 5999 } 6000 return (TRAN_ACCEPT); 6001 6002 bad_param: 6003 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 6004 *scsipkt->pkt_scbp = STATUS_CHECK; 6005 sense = sata_arq_sense(spx); 6006 sense->es_key = KEY_ILLEGAL_REQUEST; 6007 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6008 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6009 scsipkt->pkt_comp != NULL) { 6010 /* scsi callback required */ 6011 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6012 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6013 TQ_SLEEP) == 0) { 6014 /* Scheduling the callback failed */ 6015 rval = TRAN_BUSY; 6016 } 6017 } 6018 return (rval); 6019 } 6020 6021 6022 /* 6023 * Retry identify device when command returns SATA_INCOMPLETE_DATA 6024 * after doing a firmware download. 6025 */ 6026 static void 6027 sata_retry_identify_device(void *arg) 6028 { 6029 #define DOWNLOAD_WAIT_TIME_SECS 60 6030 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 6031 int rval; 6032 int retry_cnt; 6033 sata_pkt_t *sata_pkt = (sata_pkt_t *)arg; 6034 sata_pkt_txlate_t *spx = 6035 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6036 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6037 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6038 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6039 sata_drive_info_t *sdinfo; 6040 6041 /* 6042 * Before returning good status, probe device. 6043 * Device probing will get IDENTIFY DEVICE data, if possible. 6044 * The assumption is that the new microcode is applied by the 6045 * device. It is a caller responsibility to verify this. 6046 */ 6047 for (retry_cnt = 0; 6048 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 6049 retry_cnt++) { 6050 rval = sata_probe_device(sata_hba_inst, &sata_device); 6051 6052 if (rval == SATA_SUCCESS) { /* Set default features */ 6053 sdinfo = sata_get_device_info(sata_hba_inst, 6054 &sata_device); 6055 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6056 SATA_SUCCESS) { 6057 /* retry */ 6058 (void) sata_initialize_device(sata_hba_inst, 6059 sdinfo); 6060 } 6061 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6062 scsipkt->pkt_comp != NULL) 6063 (*scsipkt->pkt_comp)(scsipkt); 6064 return; 6065 } else if (rval == SATA_RETRY) { 6066 delay(drv_usectohz(1000000 * 6067 DOWNLOAD_WAIT_INTERVAL_SECS)); 6068 continue; 6069 } else /* failed - no reason to retry */ 6070 break; 6071 } 6072 6073 /* 6074 * Something went wrong, device probing failed. 6075 */ 6076 SATA_LOG_D((sata_hba_inst, CE_WARN, 6077 "Cannot probe device after downloading microcode\n")); 6078 6079 /* Reset device to force retrying the probe. */ 6080 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 6081 (SATA_DIP(sata_hba_inst), &sata_device); 6082 6083 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6084 scsipkt->pkt_comp != NULL) 6085 (*scsipkt->pkt_comp)(scsipkt); 6086 } 6087 6088 /* 6089 * Translate completion status of download microcode command. 6090 * pkt completion_reason is checked to determine the completion status. 6091 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6092 * 6093 * Note: this function may be called also for synchronously executed 6094 * command. 6095 * This function may be used only if scsi_pkt is non-NULL. 6096 */ 6097 static void 6098 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt) 6099 { 6100 sata_pkt_txlate_t *spx = 6101 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6102 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6103 struct scsi_extended_sense *sense; 6104 sata_drive_info_t *sdinfo; 6105 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6106 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6107 int rval; 6108 6109 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6110 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6111 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6112 scsipkt->pkt_reason = CMD_CMPLT; 6113 6114 rval = sata_probe_device(sata_hba_inst, &sata_device); 6115 6116 if (rval == SATA_SUCCESS) { /* Set default features */ 6117 sdinfo = sata_get_device_info(sata_hba_inst, 6118 &sata_device); 6119 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6120 SATA_SUCCESS) { 6121 /* retry */ 6122 (void) sata_initialize_device(sata_hba_inst, 6123 sdinfo); 6124 } 6125 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6126 scsipkt->pkt_comp != NULL) 6127 (*scsipkt->pkt_comp)(scsipkt); 6128 } else { 6129 (void) ddi_taskq_dispatch( 6130 (ddi_taskq_t *)SATA_TXLT_TASKQ(spx), 6131 sata_retry_identify_device, 6132 (void *)sata_pkt, TQ_NOSLEEP); 6133 } 6134 6135 6136 } else { 6137 /* Something went wrong, microcode download command failed */ 6138 scsipkt->pkt_reason = CMD_INCOMPLETE; 6139 *scsipkt->pkt_scbp = STATUS_CHECK; 6140 sense = sata_arq_sense(spx); 6141 switch (sata_pkt->satapkt_reason) { 6142 case SATA_PKT_PORT_ERROR: 6143 /* 6144 * We have no device data. Assume no data transfered. 6145 */ 6146 sense->es_key = KEY_HARDWARE_ERROR; 6147 break; 6148 6149 case SATA_PKT_DEV_ERROR: 6150 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6151 SATA_STATUS_ERR) { 6152 /* 6153 * determine dev error reason from error 6154 * reg content 6155 */ 6156 sata_decode_device_error(spx, sense); 6157 break; 6158 } 6159 /* No extended sense key - no info available */ 6160 break; 6161 6162 case SATA_PKT_TIMEOUT: 6163 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 6164 scsipkt->pkt_reason = CMD_INCOMPLETE; 6165 /* No extended sense key ? */ 6166 break; 6167 6168 case SATA_PKT_ABORTED: 6169 scsipkt->pkt_reason = CMD_ABORTED; 6170 /* No extended sense key ? */ 6171 break; 6172 6173 case SATA_PKT_RESET: 6174 /* pkt aborted by an explicit reset from a host */ 6175 scsipkt->pkt_reason = CMD_RESET; 6176 break; 6177 6178 default: 6179 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6180 "sata_txlt_nodata_cmd_completion: " 6181 "invalid packet completion reason %d", 6182 sata_pkt->satapkt_reason)); 6183 scsipkt->pkt_reason = CMD_TRAN_ERR; 6184 break; 6185 } 6186 6187 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6188 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6189 6190 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6191 scsipkt->pkt_comp != NULL) 6192 /* scsi callback required */ 6193 (*scsipkt->pkt_comp)(scsipkt); 6194 } 6195 } 6196 6197 6198 6199 6200 /* 6201 * Translate command: Synchronize Cache. 6202 * Translates into Flush Cache command for SATA hard disks. 6203 * 6204 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6205 * appropriate values in scsi_pkt fields. 6206 */ 6207 static int 6208 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 6209 { 6210 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6211 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6212 int cport = SATA_TXLT_CPORT(spx); 6213 int rval; 6214 int synch; 6215 6216 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 6217 6218 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 6219 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 6220 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 6221 return (rval); 6222 } 6223 6224 scmd->satacmd_addr_type = 0; 6225 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6226 scmd->satacmd_device_reg = 0; 6227 scmd->satacmd_sec_count_lsb = 0; 6228 scmd->satacmd_lba_low_lsb = 0; 6229 scmd->satacmd_lba_mid_lsb = 0; 6230 scmd->satacmd_lba_high_lsb = 0; 6231 scmd->satacmd_features_reg = 0; 6232 scmd->satacmd_status_reg = 0; 6233 scmd->satacmd_error_reg = 0; 6234 6235 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6236 "sata_txlt_synchronize_cache\n", NULL); 6237 6238 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6239 /* Need to set-up a callback function */ 6240 spx->txlt_sata_pkt->satapkt_comp = 6241 sata_txlt_nodata_cmd_completion; 6242 synch = FALSE; 6243 } else 6244 synch = TRUE; 6245 6246 /* Transfer command to HBA */ 6247 if (sata_hba_start(spx, &rval) != 0) { 6248 /* Pkt not accepted for execution */ 6249 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6250 return (rval); 6251 } 6252 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6253 6254 /* 6255 * If execution non-synchronous, it had to be completed 6256 * a callback function will handle potential errors, translate 6257 * the response and will do a callback to a target driver. 6258 * If it was synchronous, check status, using the same 6259 * framework callback. 6260 */ 6261 if (synch) { 6262 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6263 "synchronous execution status %x\n", 6264 spx->txlt_sata_pkt->satapkt_reason); 6265 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6266 } 6267 return (TRAN_ACCEPT); 6268 } 6269 6270 6271 /* 6272 * Send pkt to SATA HBA driver 6273 * 6274 * This function may be called only if the operation is requested by scsi_pkt, 6275 * i.e. scsi_pkt is not NULL. 6276 * 6277 * This function has to be called with cport mutex held. It does release 6278 * the mutex when it calls HBA driver sata_tran_start function and 6279 * re-acquires it afterwards. 6280 * 6281 * If return value is 0, pkt was accepted, -1 otherwise 6282 * rval is set to appropriate sata_scsi_start return value. 6283 * 6284 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6285 * have called the sata_pkt callback function for this packet. 6286 * 6287 * The scsi callback has to be performed by the caller of this routine. 6288 * 6289 * Note 2: No port multiplier support for now. 6290 */ 6291 static int 6292 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6293 { 6294 int stat, cport; 6295 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6296 sata_drive_info_t *sdinfo; 6297 sata_device_t *sata_device; 6298 uint8_t cmd; 6299 struct sata_cmd_flags cmd_flags; 6300 6301 ASSERT(spx->txlt_sata_pkt != NULL); 6302 6303 cport = SATA_TXLT_CPORT(spx); 6304 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6305 6306 sdinfo = sata_get_device_info(sata_hba_inst, 6307 &spx->txlt_sata_pkt->satapkt_device); 6308 ASSERT(sdinfo != NULL); 6309 6310 /* Clear device reset state? */ 6311 if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 6312 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6313 sata_clear_dev_reset = B_TRUE; 6314 sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET; 6315 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6316 "sata_hba_start: clearing device reset state\n", NULL); 6317 } 6318 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 6319 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 6320 sata_device = &spx->txlt_sata_pkt->satapkt_device; 6321 6322 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6323 6324 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6325 "Sata cmd 0x%2x\n", cmd); 6326 6327 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 6328 spx->txlt_sata_pkt); 6329 6330 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6331 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6332 /* 6333 * If sata pkt was accepted and executed in asynchronous mode, i.e. 6334 * with the sata callback, the sata_pkt could be already destroyed 6335 * by the time we check ther return status from the hba_start() 6336 * function, because sata_scsi_destroy_pkt() could have been already 6337 * called (perhaps in the interrupt context). So, in such case, there 6338 * should be no references to it. In other cases, sata_pkt still 6339 * exists. 6340 */ 6341 switch (stat) { 6342 case SATA_TRAN_ACCEPTED: 6343 /* 6344 * pkt accepted for execution. 6345 * If it was executed synchronously, it is already completed 6346 * and pkt completion_reason indicates completion status. 6347 */ 6348 *rval = TRAN_ACCEPT; 6349 return (0); 6350 6351 case SATA_TRAN_QUEUE_FULL: 6352 /* 6353 * Controller detected queue full condition. 6354 */ 6355 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 6356 "sata_hba_start: queue full\n", NULL); 6357 6358 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6359 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 6360 6361 *rval = TRAN_BUSY; 6362 break; 6363 6364 case SATA_TRAN_PORT_ERROR: 6365 /* 6366 * Communication/link with device or general port error 6367 * detected before pkt execution begun. 6368 */ 6369 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6370 SATA_ADDR_CPORT || 6371 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6372 SATA_ADDR_DCPORT) 6373 sata_log(sata_hba_inst, CE_CONT, 6374 "SATA port %d error", 6375 sata_device->satadev_addr.cport); 6376 else 6377 sata_log(sata_hba_inst, CE_CONT, 6378 "SATA port %d pmport %d error\n", 6379 sata_device->satadev_addr.cport, 6380 sata_device->satadev_addr.pmport); 6381 6382 /* 6383 * Update the port/device structure. 6384 * sata_pkt should be still valid. Since port error is 6385 * returned, sata_device content should reflect port 6386 * state - it means, that sata address have been changed, 6387 * because original packet's sata address refered to a device 6388 * attached to some port. 6389 */ 6390 sata_update_port_info(sata_hba_inst, sata_device); 6391 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6392 *rval = TRAN_FATAL_ERROR; 6393 break; 6394 6395 case SATA_TRAN_CMD_UNSUPPORTED: 6396 /* 6397 * Command rejected by HBA as unsupported. It was HBA driver 6398 * that rejected the command, command was not sent to 6399 * an attached device. 6400 */ 6401 if ((sdinfo != NULL) && 6402 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 6403 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6404 "sat_hba_start: cmd 0x%2x rejected " 6405 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 6406 6407 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6408 (void) sata_txlt_invalid_command(spx); 6409 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6410 6411 *rval = TRAN_ACCEPT; 6412 break; 6413 6414 case SATA_TRAN_BUSY: 6415 /* 6416 * Command rejected by HBA because other operation prevents 6417 * accepting the packet, or device is in RESET condition. 6418 */ 6419 if (sdinfo != NULL) { 6420 sdinfo->satadrv_state = 6421 spx->txlt_sata_pkt->satapkt_device.satadev_state; 6422 6423 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 6424 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6425 "sata_hba_start: cmd 0x%2x rejected " 6426 "because of device reset condition\n", 6427 cmd); 6428 } else { 6429 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6430 "sata_hba_start: cmd 0x%2x rejected " 6431 "with SATA_TRAN_BUSY status\n", 6432 cmd); 6433 } 6434 } 6435 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6436 *rval = TRAN_BUSY; 6437 break; 6438 6439 default: 6440 /* Unrecognized HBA response */ 6441 SATA_LOG_D((sata_hba_inst, CE_WARN, 6442 "sata_hba_start: unrecognized HBA response " 6443 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 6444 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6445 *rval = TRAN_FATAL_ERROR; 6446 break; 6447 } 6448 6449 /* 6450 * If we got here, the packet was rejected. 6451 * Check if we need to remember reset state clearing request 6452 */ 6453 if (cmd_flags.sata_clear_dev_reset) { 6454 /* 6455 * Check if device is still configured - it may have 6456 * disapeared from the configuration 6457 */ 6458 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6459 if (sdinfo != NULL) { 6460 /* 6461 * Restore the flag that requests clearing of 6462 * the device reset state, 6463 * so the next sata packet may carry it to HBA. 6464 */ 6465 sdinfo->satadrv_event_flags |= 6466 SATA_EVNT_CLEAR_DEVICE_RESET; 6467 } 6468 } 6469 return (-1); 6470 } 6471 6472 /* 6473 * Scsi response setup for invalid LBA 6474 * 6475 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 6476 */ 6477 static int 6478 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 6479 { 6480 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6481 struct scsi_extended_sense *sense; 6482 6483 scsipkt->pkt_reason = CMD_CMPLT; 6484 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6485 STATE_SENT_CMD | STATE_GOT_STATUS; 6486 *scsipkt->pkt_scbp = STATUS_CHECK; 6487 6488 *scsipkt->pkt_scbp = STATUS_CHECK; 6489 sense = sata_arq_sense(spx); 6490 sense->es_key = KEY_ILLEGAL_REQUEST; 6491 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 6492 6493 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6494 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6495 6496 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6497 scsipkt->pkt_comp != NULL) 6498 /* scsi callback required */ 6499 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6500 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6501 TQ_SLEEP) == NULL) 6502 /* Scheduling the callback failed */ 6503 return (TRAN_BUSY); 6504 return (TRAN_ACCEPT); 6505 } 6506 6507 6508 /* 6509 * Analyze device status and error registers and translate them into 6510 * appropriate scsi sense codes. 6511 * NOTE: non-packet commands only for now 6512 */ 6513 static void 6514 sata_decode_device_error(sata_pkt_txlate_t *spx, 6515 struct scsi_extended_sense *sense) 6516 { 6517 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 6518 6519 ASSERT(sense != NULL); 6520 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 6521 SATA_STATUS_ERR); 6522 6523 6524 if (err_reg & SATA_ERROR_ICRC) { 6525 sense->es_key = KEY_ABORTED_COMMAND; 6526 sense->es_add_code = 0x08; /* Communication failure */ 6527 return; 6528 } 6529 6530 if (err_reg & SATA_ERROR_UNC) { 6531 sense->es_key = KEY_MEDIUM_ERROR; 6532 /* Information bytes (LBA) need to be set by a caller */ 6533 return; 6534 } 6535 6536 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 6537 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 6538 sense->es_key = KEY_UNIT_ATTENTION; 6539 sense->es_add_code = 0x3a; /* No media present */ 6540 return; 6541 } 6542 6543 if (err_reg & SATA_ERROR_IDNF) { 6544 if (err_reg & SATA_ERROR_ABORT) { 6545 sense->es_key = KEY_ABORTED_COMMAND; 6546 } else { 6547 sense->es_key = KEY_ILLEGAL_REQUEST; 6548 sense->es_add_code = 0x21; /* LBA out of range */ 6549 } 6550 return; 6551 } 6552 6553 if (err_reg & SATA_ERROR_ABORT) { 6554 ASSERT(spx->txlt_sata_pkt != NULL); 6555 sense->es_key = KEY_ABORTED_COMMAND; 6556 return; 6557 } 6558 } 6559 6560 /* 6561 * Extract error LBA from sata_pkt.satapkt_cmd register fields 6562 */ 6563 static void 6564 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 6565 { 6566 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 6567 6568 *lba = 0; 6569 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 6570 *lba = sata_cmd->satacmd_lba_high_msb; 6571 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 6572 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 6573 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 6574 *lba = sata_cmd->satacmd_device_reg & 0xf; 6575 } 6576 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 6577 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 6578 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 6579 } 6580 6581 /* 6582 * This is fixed sense format - if LBA exceeds the info field size, 6583 * no valid info will be returned (valid bit in extended sense will 6584 * be set to 0). 6585 */ 6586 static struct scsi_extended_sense * 6587 sata_arq_sense(sata_pkt_txlate_t *spx) 6588 { 6589 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6590 struct scsi_arq_status *arqs; 6591 struct scsi_extended_sense *sense; 6592 6593 /* Fill ARQ sense data */ 6594 scsipkt->pkt_state |= STATE_ARQ_DONE; 6595 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6596 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6597 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6598 arqs->sts_rqpkt_reason = CMD_CMPLT; 6599 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6600 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6601 arqs->sts_rqpkt_resid = 0; 6602 sense = &arqs->sts_sensedata; 6603 bzero(sense, sizeof (struct scsi_extended_sense)); 6604 sata_fixed_sense_data_preset(sense); 6605 return (sense); 6606 } 6607 6608 6609 /* 6610 * Emulated SATA Read/Write command completion for zero-length requests. 6611 * This request always succedes, so in synchronous mode it always returns 6612 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 6613 * callback cannot be scheduled. 6614 */ 6615 static int 6616 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 6617 { 6618 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6619 6620 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6621 STATE_SENT_CMD | STATE_GOT_STATUS; 6622 scsipkt->pkt_reason = CMD_CMPLT; 6623 *scsipkt->pkt_scbp = STATUS_GOOD; 6624 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6625 /* scsi callback required - have to schedule it */ 6626 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6627 (task_func_t *)scsipkt->pkt_comp, 6628 (void *)scsipkt, TQ_SLEEP) == NULL) 6629 /* Scheduling the callback failed */ 6630 return (TRAN_BUSY); 6631 } 6632 return (TRAN_ACCEPT); 6633 } 6634 6635 6636 /* 6637 * Translate completion status of SATA read/write commands into scsi response. 6638 * pkt completion_reason is checked to determine the completion status. 6639 * Do scsi callback if necessary. 6640 * 6641 * Note: this function may be called also for synchronously executed 6642 * commands. 6643 * This function may be used only if scsi_pkt is non-NULL. 6644 */ 6645 static void 6646 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 6647 { 6648 sata_pkt_txlate_t *spx = 6649 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6650 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6651 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6652 struct scsi_extended_sense *sense; 6653 uint64_t lba; 6654 struct buf *bp; 6655 int rval; 6656 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6657 /* Normal completion */ 6658 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6659 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6660 scsipkt->pkt_reason = CMD_CMPLT; 6661 *scsipkt->pkt_scbp = STATUS_GOOD; 6662 if (spx->txlt_tmp_buf != NULL) { 6663 /* Temporary buffer was used */ 6664 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6665 if (bp->b_flags & B_READ) { 6666 rval = ddi_dma_sync( 6667 spx->txlt_buf_dma_handle, 0, 0, 6668 DDI_DMA_SYNC_FORCPU); 6669 ASSERT(rval == DDI_SUCCESS); 6670 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6671 bp->b_bcount); 6672 } 6673 } 6674 } else { 6675 /* 6676 * Something went wrong - analyze return 6677 */ 6678 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6679 STATE_SENT_CMD | STATE_GOT_STATUS; 6680 scsipkt->pkt_reason = CMD_INCOMPLETE; 6681 *scsipkt->pkt_scbp = STATUS_CHECK; 6682 sense = sata_arq_sense(spx); 6683 ASSERT(sense != NULL); 6684 6685 /* 6686 * SATA_PKT_DEV_ERROR is the only case where we may be able to 6687 * extract from device registers the failing LBA. 6688 */ 6689 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 6690 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 6691 (scmd->satacmd_lba_mid_msb != 0 || 6692 scmd->satacmd_lba_high_msb != 0)) { 6693 /* 6694 * We have problem reporting this cmd LBA 6695 * in fixed sense data format, because of 6696 * the size of the scsi LBA fields. 6697 */ 6698 sense->es_valid = 0; 6699 } else { 6700 sata_extract_error_lba(spx, &lba); 6701 sense->es_info_1 = (lba & 0xFF000000) >> 24; 6702 sense->es_info_2 = (lba & 0xFF0000) >> 16; 6703 sense->es_info_3 = (lba & 0xFF00) >> 8; 6704 sense->es_info_4 = lba & 0xFF; 6705 } 6706 } else { 6707 /* Invalid extended sense info */ 6708 sense->es_valid = 0; 6709 } 6710 6711 switch (sata_pkt->satapkt_reason) { 6712 case SATA_PKT_PORT_ERROR: 6713 /* We may want to handle DEV GONE state as well */ 6714 /* 6715 * We have no device data. Assume no data transfered. 6716 */ 6717 sense->es_key = KEY_HARDWARE_ERROR; 6718 break; 6719 6720 case SATA_PKT_DEV_ERROR: 6721 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6722 SATA_STATUS_ERR) { 6723 /* 6724 * determine dev error reason from error 6725 * reg content 6726 */ 6727 sata_decode_device_error(spx, sense); 6728 if (sense->es_key == KEY_MEDIUM_ERROR) { 6729 switch (scmd->satacmd_cmd_reg) { 6730 case SATAC_READ_DMA: 6731 case SATAC_READ_DMA_EXT: 6732 case SATAC_READ_DMA_QUEUED: 6733 case SATAC_READ_DMA_QUEUED_EXT: 6734 case SATAC_READ_FPDMA_QUEUED: 6735 /* Unrecovered read error */ 6736 sense->es_add_code = 6737 SD_SCSI_ASC_UNREC_READ_ERROR; 6738 break; 6739 case SATAC_WRITE_DMA: 6740 case SATAC_WRITE_DMA_EXT: 6741 case SATAC_WRITE_DMA_QUEUED: 6742 case SATAC_WRITE_DMA_QUEUED_EXT: 6743 case SATAC_WRITE_FPDMA_QUEUED: 6744 /* Write error */ 6745 sense->es_add_code = 6746 SD_SCSI_ASC_WRITE_ERROR; 6747 break; 6748 default: 6749 /* Internal error */ 6750 SATA_LOG_D(( 6751 spx->txlt_sata_hba_inst, 6752 CE_WARN, 6753 "sata_txlt_rw_completion :" 6754 "internal error - invalid " 6755 "command 0x%2x", 6756 scmd->satacmd_cmd_reg)); 6757 break; 6758 } 6759 } 6760 break; 6761 } 6762 /* No extended sense key - no info available */ 6763 scsipkt->pkt_reason = CMD_INCOMPLETE; 6764 break; 6765 6766 case SATA_PKT_TIMEOUT: 6767 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 6768 scsipkt->pkt_reason = CMD_INCOMPLETE; 6769 /* No extended sense key ? */ 6770 break; 6771 6772 case SATA_PKT_ABORTED: 6773 scsipkt->pkt_reason = CMD_ABORTED; 6774 /* No extended sense key ? */ 6775 break; 6776 6777 case SATA_PKT_RESET: 6778 scsipkt->pkt_reason = CMD_RESET; 6779 break; 6780 6781 default: 6782 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6783 "sata_txlt_rw_completion: " 6784 "invalid packet completion reason")); 6785 scsipkt->pkt_reason = CMD_TRAN_ERR; 6786 break; 6787 } 6788 } 6789 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6790 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6791 6792 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6793 scsipkt->pkt_comp != NULL) 6794 /* scsi callback required */ 6795 (*scsipkt->pkt_comp)(scsipkt); 6796 } 6797 6798 6799 /* 6800 * Translate completion status of non-data commands (i.e. commands returning 6801 * no data). 6802 * pkt completion_reason is checked to determine the completion status. 6803 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6804 * 6805 * Note: this function may be called also for synchronously executed 6806 * commands. 6807 * This function may be used only if scsi_pkt is non-NULL. 6808 */ 6809 6810 static void 6811 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 6812 { 6813 sata_pkt_txlate_t *spx = 6814 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6815 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6816 struct scsi_extended_sense *sense; 6817 6818 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6819 STATE_SENT_CMD | STATE_GOT_STATUS; 6820 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6821 /* Normal completion */ 6822 scsipkt->pkt_reason = CMD_CMPLT; 6823 *scsipkt->pkt_scbp = STATUS_GOOD; 6824 } else { 6825 /* Something went wrong */ 6826 scsipkt->pkt_reason = CMD_INCOMPLETE; 6827 *scsipkt->pkt_scbp = STATUS_CHECK; 6828 sense = sata_arq_sense(spx); 6829 switch (sata_pkt->satapkt_reason) { 6830 case SATA_PKT_PORT_ERROR: 6831 /* 6832 * We have no device data. Assume no data transfered. 6833 */ 6834 sense->es_key = KEY_HARDWARE_ERROR; 6835 break; 6836 6837 case SATA_PKT_DEV_ERROR: 6838 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6839 SATA_STATUS_ERR) { 6840 /* 6841 * determine dev error reason from error 6842 * reg content 6843 */ 6844 sata_decode_device_error(spx, sense); 6845 break; 6846 } 6847 /* No extended sense key - no info available */ 6848 break; 6849 6850 case SATA_PKT_TIMEOUT: 6851 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 6852 scsipkt->pkt_reason = CMD_INCOMPLETE; 6853 /* No extended sense key ? */ 6854 break; 6855 6856 case SATA_PKT_ABORTED: 6857 scsipkt->pkt_reason = CMD_ABORTED; 6858 /* No extended sense key ? */ 6859 break; 6860 6861 case SATA_PKT_RESET: 6862 /* pkt aborted by an explicit reset from a host */ 6863 scsipkt->pkt_reason = CMD_RESET; 6864 break; 6865 6866 default: 6867 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6868 "sata_txlt_nodata_cmd_completion: " 6869 "invalid packet completion reason %d", 6870 sata_pkt->satapkt_reason)); 6871 scsipkt->pkt_reason = CMD_TRAN_ERR; 6872 break; 6873 } 6874 6875 } 6876 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6877 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6878 6879 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6880 scsipkt->pkt_comp != NULL) 6881 /* scsi callback required */ 6882 (*scsipkt->pkt_comp)(scsipkt); 6883 } 6884 6885 6886 /* 6887 * Build Mode sense R/W recovery page 6888 * NOT IMPLEMENTED 6889 */ 6890 6891 static int 6892 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6893 { 6894 #ifndef __lock_lint 6895 _NOTE(ARGUNUSED(sdinfo)) 6896 _NOTE(ARGUNUSED(pcntrl)) 6897 _NOTE(ARGUNUSED(buf)) 6898 #endif 6899 return (0); 6900 } 6901 6902 /* 6903 * Build Mode sense caching page - scsi-3 implementation. 6904 * Page length distinguishes previous format from scsi-3 format. 6905 * buf must have space for 0x12 bytes. 6906 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 6907 * 6908 */ 6909 static int 6910 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6911 { 6912 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 6913 sata_id_t *sata_id = &sdinfo->satadrv_id; 6914 6915 /* 6916 * Most of the fields are set to 0, being not supported and/or disabled 6917 */ 6918 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 6919 6920 /* Saved paramters not supported */ 6921 if (pcntrl == 3) 6922 return (0); 6923 if (pcntrl == 0 || pcntrl == 2) { 6924 /* 6925 * For now treat current and default parameters as same 6926 * That may have to change, if target driver will complain 6927 */ 6928 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 6929 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6930 6931 if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 6932 !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) { 6933 page->dra = 1; /* Read Ahead disabled */ 6934 page->rcd = 1; /* Read Cache disabled */ 6935 } 6936 if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) && 6937 (sata_id->ai_features85 & SATA_WRITE_CACHE)) 6938 page->wce = 1; /* Write Cache enabled */ 6939 } else { 6940 /* Changeable parameters */ 6941 page->mode_page.code = MODEPAGE_CACHING; 6942 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6943 if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) { 6944 page->dra = 1; 6945 page->rcd = 1; 6946 } 6947 if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE) 6948 page->wce = 1; 6949 } 6950 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6951 sizeof (struct mode_page)); 6952 } 6953 6954 /* 6955 * Build Mode sense exception cntrl page 6956 */ 6957 static int 6958 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6959 { 6960 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 6961 sata_id_t *sata_id = &sdinfo->satadrv_id; 6962 6963 /* 6964 * Most of the fields are set to 0, being not supported and/or disabled 6965 */ 6966 bzero(buf, PAGELENGTH_INFO_EXCPT); 6967 6968 page->mode_page.code = MODEPAGE_INFO_EXCPT; 6969 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 6970 6971 /* Indicate that this is page is saveable */ 6972 page->mode_page.ps = 1; 6973 6974 /* 6975 * We will return the same data for default, current and saved page. 6976 * The only changeable bit is dexcpt and that bit is required 6977 * by the ATA specification to be preserved across power cycles. 6978 */ 6979 if (pcntrl != 1) { 6980 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6981 page->mrie = MRIE_ONLY_ON_REQUEST; 6982 } 6983 else 6984 page->dexcpt = 1; /* Only changeable parameter */ 6985 6986 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page)); 6987 } 6988 6989 6990 static int 6991 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6992 { 6993 struct mode_acoustic_management *page = 6994 (struct mode_acoustic_management *)buf; 6995 sata_id_t *sata_id = &sdinfo->satadrv_id; 6996 6997 /* 6998 * Most of the fields are set to 0, being not supported and/or disabled 6999 */ 7000 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 7001 7002 switch (pcntrl) { 7003 case P_CNTRL_DEFAULT: 7004 /* default paramters not supported */ 7005 return (0); 7006 7007 case P_CNTRL_CURRENT: 7008 case P_CNTRL_SAVED: 7009 /* Saved and current are supported and are identical */ 7010 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 7011 page->mode_page.length = 7012 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 7013 page->mode_page.ps = 1; 7014 7015 /* Word 83 indicates if feature is supported */ 7016 /* If feature is not supported */ 7017 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 7018 page->acoustic_manag_enable = 7019 ACOUSTIC_DISABLED; 7020 } else { 7021 page->acoustic_manag_enable = 7022 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 7023 != 0); 7024 /* Word 94 inidicates the value */ 7025 #ifdef _LITTLE_ENDIAN 7026 page->acoustic_manag_level = 7027 (uchar_t)sata_id->ai_acoustic; 7028 page->vendor_recommended_value = 7029 sata_id->ai_acoustic >> 8; 7030 #else 7031 page->acoustic_manag_level = 7032 sata_id->ai_acoustic >> 8; 7033 page->vendor_recommended_value = 7034 (uchar_t)sata_id->ai_acoustic; 7035 #endif 7036 } 7037 break; 7038 7039 case P_CNTRL_CHANGEABLE: 7040 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 7041 page->mode_page.length = 7042 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 7043 page->mode_page.ps = 1; 7044 7045 /* Word 83 indicates if the feature is supported */ 7046 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 7047 page->acoustic_manag_enable = 7048 ACOUSTIC_ENABLED; 7049 page->acoustic_manag_level = 0xff; 7050 } 7051 break; 7052 } 7053 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7054 sizeof (struct mode_page)); 7055 } 7056 7057 7058 /* 7059 * Build Mode sense power condition page 7060 * NOT IMPLEMENTED. 7061 */ 7062 static int 7063 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7064 { 7065 #ifndef __lock_lint 7066 _NOTE(ARGUNUSED(sdinfo)) 7067 _NOTE(ARGUNUSED(pcntrl)) 7068 _NOTE(ARGUNUSED(buf)) 7069 #endif 7070 return (0); 7071 } 7072 7073 7074 /* 7075 * Process mode select caching page 8 (scsi3 format only). 7076 * Read Ahead (same as read cache) and Write Cache may be turned on and off 7077 * if these features are supported by the device. If these features are not 7078 * supported, quietly ignore them. 7079 * This function fails only if the SET FEATURE command sent to 7080 * the device fails. The page format is not varified, assuming that the 7081 * target driver operates correctly - if parameters length is too short, 7082 * we just drop the page. 7083 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 7084 * setting have to be changed. 7085 * SET FEATURE command is executed synchronously, i.e. we wait here until 7086 * it is completed, regardless of the scsi pkt directives. 7087 * 7088 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 7089 * changing DRA will change RCD. 7090 * 7091 * More than one SATA command may be executed to perform operations specified 7092 * by mode select pages. The first error terminates further execution. 7093 * Operations performed successully are not backed-up in such case. 7094 * 7095 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7096 * If operation resulted in changing device setup, dmod flag should be set to 7097 * one (1). If parameters were not changed, dmod flag should be set to 0. 7098 * Upon return, if operation required sending command to the device, the rval 7099 * should be set to the value returned by sata_hba_start. If operation 7100 * did not require device access, rval should be set to TRAN_ACCEPT. 7101 * The pagelen should be set to the length of the page. 7102 * 7103 * This function has to be called with a port mutex held. 7104 * 7105 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7106 */ 7107 int 7108 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 7109 int parmlen, int *pagelen, int *rval, int *dmod) 7110 { 7111 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7112 sata_drive_info_t *sdinfo; 7113 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7114 sata_id_t *sata_id; 7115 struct scsi_extended_sense *sense; 7116 int wce, dra; /* Current settings */ 7117 7118 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7119 &spx->txlt_sata_pkt->satapkt_device); 7120 sata_id = &sdinfo->satadrv_id; 7121 *dmod = 0; 7122 7123 /* Verify parameters length. If too short, drop it */ 7124 if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 7125 sizeof (struct mode_page) < parmlen) { 7126 *scsipkt->pkt_scbp = STATUS_CHECK; 7127 sense = sata_arq_sense(spx); 7128 sense->es_key = KEY_ILLEGAL_REQUEST; 7129 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7130 *pagelen = parmlen; 7131 *rval = TRAN_ACCEPT; 7132 return (SATA_FAILURE); 7133 } 7134 7135 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 7136 7137 /* 7138 * We can manipulate only write cache and read ahead 7139 * (read cache) setting. 7140 */ 7141 if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 7142 !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) { 7143 /* 7144 * None of the features is supported - ignore 7145 */ 7146 *rval = TRAN_ACCEPT; 7147 return (SATA_SUCCESS); 7148 } 7149 7150 /* Current setting of Read Ahead (and Read Cache) */ 7151 if (sata_id->ai_features85 & SATA_LOOK_AHEAD) 7152 dra = 0; /* 0 == not disabled */ 7153 else 7154 dra = 1; 7155 /* Current setting of Write Cache */ 7156 if (sata_id->ai_features85 & SATA_WRITE_CACHE) 7157 wce = 1; 7158 else 7159 wce = 0; 7160 7161 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 7162 /* nothing to do */ 7163 *rval = TRAN_ACCEPT; 7164 return (SATA_SUCCESS); 7165 } 7166 /* 7167 * Need to flip some setting 7168 * Set-up Internal SET FEATURES command(s) 7169 */ 7170 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7171 scmd->satacmd_addr_type = 0; 7172 scmd->satacmd_device_reg = 0; 7173 scmd->satacmd_status_reg = 0; 7174 scmd->satacmd_error_reg = 0; 7175 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7176 if (page->dra != dra || page->rcd != dra) { 7177 /* Need to flip read ahead setting */ 7178 if (dra == 0) 7179 /* Disable read ahead / read cache */ 7180 scmd->satacmd_features_reg = 7181 SATAC_SF_DISABLE_READ_AHEAD; 7182 else 7183 /* Enable read ahead / read cache */ 7184 scmd->satacmd_features_reg = 7185 SATAC_SF_ENABLE_READ_AHEAD; 7186 7187 /* Transfer command to HBA */ 7188 if (sata_hba_start(spx, rval) != 0) 7189 /* 7190 * Pkt not accepted for execution. 7191 */ 7192 return (SATA_FAILURE); 7193 7194 *dmod = 1; 7195 7196 /* Now process return */ 7197 if (spx->txlt_sata_pkt->satapkt_reason != 7198 SATA_PKT_COMPLETED) { 7199 goto failure; /* Terminate */ 7200 } 7201 } 7202 7203 /* Note that the packet is not removed, so it could be re-used */ 7204 if (page->wce != wce) { 7205 /* Need to flip Write Cache setting */ 7206 if (page->wce == 1) 7207 /* Enable write cache */ 7208 scmd->satacmd_features_reg = 7209 SATAC_SF_ENABLE_WRITE_CACHE; 7210 else 7211 /* Disable write cache */ 7212 scmd->satacmd_features_reg = 7213 SATAC_SF_DISABLE_WRITE_CACHE; 7214 7215 /* Transfer command to HBA */ 7216 if (sata_hba_start(spx, rval) != 0) 7217 /* 7218 * Pkt not accepted for execution. 7219 */ 7220 return (SATA_FAILURE); 7221 7222 *dmod = 1; 7223 7224 /* Now process return */ 7225 if (spx->txlt_sata_pkt->satapkt_reason != 7226 SATA_PKT_COMPLETED) { 7227 goto failure; 7228 } 7229 } 7230 return (SATA_SUCCESS); 7231 7232 failure: 7233 sata_xlate_errors(spx); 7234 7235 return (SATA_FAILURE); 7236 } 7237 7238 /* 7239 * Process mode select informational exceptions control page 0x1c 7240 * 7241 * The only changeable bit is dexcpt (disable exceptions). 7242 * MRIE (method of reporting informational exceptions) must be 7243 * "only on request". 7244 * 7245 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7246 * If operation resulted in changing device setup, dmod flag should be set to 7247 * one (1). If parameters were not changed, dmod flag should be set to 0. 7248 * Upon return, if operation required sending command to the device, the rval 7249 * should be set to the value returned by sata_hba_start. If operation 7250 * did not require device access, rval should be set to TRAN_ACCEPT. 7251 * The pagelen should be set to the length of the page. 7252 * 7253 * This function has to be called with a port mutex held. 7254 * 7255 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7256 */ 7257 static int 7258 sata_mode_select_page_1c( 7259 sata_pkt_txlate_t *spx, 7260 struct mode_info_excpt_page *page, 7261 int parmlen, 7262 int *pagelen, 7263 int *rval, 7264 int *dmod) 7265 { 7266 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7267 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7268 sata_drive_info_t *sdinfo; 7269 sata_id_t *sata_id; 7270 struct scsi_extended_sense *sense; 7271 7272 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7273 &spx->txlt_sata_pkt->satapkt_device); 7274 sata_id = &sdinfo->satadrv_id; 7275 7276 *dmod = 0; 7277 7278 /* Verify parameters length. If too short, drop it */ 7279 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) || 7280 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 7281 *scsipkt->pkt_scbp = STATUS_CHECK; 7282 sense = sata_arq_sense(spx); 7283 sense->es_key = KEY_ILLEGAL_REQUEST; 7284 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7285 *pagelen = parmlen; 7286 *rval = TRAN_ACCEPT; 7287 return (SATA_FAILURE); 7288 } 7289 7290 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 7291 7292 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 7293 *scsipkt->pkt_scbp = STATUS_CHECK; 7294 sense = sata_arq_sense(spx); 7295 sense->es_key = KEY_ILLEGAL_REQUEST; 7296 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7297 *pagelen = parmlen; 7298 *rval = TRAN_ACCEPT; 7299 return (SATA_FAILURE); 7300 } 7301 7302 /* If already in the state requested, we are done */ 7303 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 7304 /* nothing to do */ 7305 *rval = TRAN_ACCEPT; 7306 return (SATA_SUCCESS); 7307 } 7308 7309 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7310 7311 /* Build SMART_ENABLE or SMART_DISABLE command */ 7312 scmd->satacmd_addr_type = 0; /* N/A */ 7313 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 7314 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 7315 scmd->satacmd_features_reg = page->dexcpt ? 7316 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 7317 scmd->satacmd_device_reg = 0; /* Always device 0 */ 7318 scmd->satacmd_cmd_reg = SATAC_SMART; 7319 7320 /* Transfer command to HBA */ 7321 if (sata_hba_start(spx, rval) != 0) 7322 /* 7323 * Pkt not accepted for execution. 7324 */ 7325 return (SATA_FAILURE); 7326 7327 *dmod = 1; /* At least may have been modified */ 7328 7329 /* Now process return */ 7330 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 7331 return (SATA_SUCCESS); 7332 7333 /* Packet did not complete successfully */ 7334 sata_xlate_errors(spx); 7335 7336 return (SATA_FAILURE); 7337 } 7338 7339 int 7340 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 7341 mode_acoustic_management *page, int parmlen, int *pagelen, 7342 int *rval, int *dmod) 7343 { 7344 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7345 sata_drive_info_t *sdinfo; 7346 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7347 sata_id_t *sata_id; 7348 struct scsi_extended_sense *sense; 7349 7350 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7351 &spx->txlt_sata_pkt->satapkt_device); 7352 sata_id = &sdinfo->satadrv_id; 7353 *dmod = 0; 7354 7355 /* If parmlen is too short or the feature is not supported, drop it */ 7356 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7357 sizeof (struct mode_page)) < parmlen) || 7358 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 7359 *scsipkt->pkt_scbp = STATUS_CHECK; 7360 sense = sata_arq_sense(spx); 7361 sense->es_key = KEY_ILLEGAL_REQUEST; 7362 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7363 *pagelen = parmlen; 7364 *rval = TRAN_ACCEPT; 7365 return (SATA_FAILURE); 7366 } 7367 7368 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7369 sizeof (struct mode_page); 7370 7371 /* 7372 * We can enable and disable acoustice management and 7373 * set the acoustic management level. 7374 */ 7375 7376 /* 7377 * Set-up Internal SET FEATURES command(s) 7378 */ 7379 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7380 scmd->satacmd_addr_type = 0; 7381 scmd->satacmd_device_reg = 0; 7382 scmd->satacmd_status_reg = 0; 7383 scmd->satacmd_error_reg = 0; 7384 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7385 if (page->acoustic_manag_enable) { 7386 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 7387 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 7388 } else { /* disabling acoustic management */ 7389 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 7390 } 7391 7392 /* Transfer command to HBA */ 7393 if (sata_hba_start(spx, rval) != 0) 7394 /* 7395 * Pkt not accepted for execution. 7396 */ 7397 return (SATA_FAILURE); 7398 7399 /* Now process return */ 7400 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 7401 sata_xlate_errors(spx); 7402 return (SATA_FAILURE); 7403 } 7404 7405 *dmod = 1; 7406 7407 return (SATA_SUCCESS); 7408 } 7409 7410 7411 7412 7413 /* 7414 * sata_build_lsense_page0() is used to create the 7415 * SCSI LOG SENSE page 0 (supported log pages) 7416 * 7417 * Currently supported pages are 0, 0x10, 0x2f and 0x30 7418 * (supported log pages, self-test results, informational exceptions 7419 * and Sun vendor specific ATA SMART data). 7420 * 7421 * Takes a sata_drive_info t * and the address of a buffer 7422 * in which to create the page information. 7423 * 7424 * Returns the number of bytes valid in the buffer. 7425 */ 7426 static int 7427 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 7428 { 7429 struct log_parameter *lpp = (struct log_parameter *)buf; 7430 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 7431 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 7432 sata_id_t *sata_id = &sdinfo->satadrv_id; 7433 7434 lpp->param_code[0] = 0; 7435 lpp->param_code[1] = 0; 7436 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7437 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 7438 7439 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 7440 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 7441 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 7442 ++num_pages_supported; 7443 } 7444 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 7445 ++num_pages_supported; 7446 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 7447 ++num_pages_supported; 7448 } 7449 7450 lpp->param_len = num_pages_supported; 7451 7452 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 7453 num_pages_supported); 7454 } 7455 7456 /* 7457 * sata_build_lsense_page_10() is used to create the 7458 * SCSI LOG SENSE page 0x10 (self-test results) 7459 * 7460 * Takes a sata_drive_info t * and the address of a buffer 7461 * in which to create the page information as well as a sata_hba_inst_t *. 7462 * 7463 * Returns the number of bytes valid in the buffer. 7464 */ 7465 static int 7466 sata_build_lsense_page_10( 7467 sata_drive_info_t *sdinfo, 7468 uint8_t *buf, 7469 sata_hba_inst_t *sata_hba_inst) 7470 { 7471 struct log_parameter *lpp = (struct log_parameter *)buf; 7472 int rval; 7473 7474 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 7475 struct smart_ext_selftest_log *ext_selftest_log; 7476 7477 ext_selftest_log = kmem_zalloc( 7478 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 7479 7480 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 7481 ext_selftest_log, 0); 7482 if (rval == 0) { 7483 int index, start_index; 7484 struct smart_ext_selftest_log_entry *entry; 7485 static const struct smart_ext_selftest_log_entry empty = 7486 {0}; 7487 uint16_t block_num; 7488 int count; 7489 boolean_t only_one_block = B_FALSE; 7490 7491 index = ext_selftest_log-> 7492 smart_ext_selftest_log_index[0]; 7493 index |= ext_selftest_log-> 7494 smart_ext_selftest_log_index[1] << 8; 7495 if (index == 0) 7496 goto out; 7497 7498 --index; /* Correct for 0 origin */ 7499 start_index = index; /* remember where we started */ 7500 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7501 if (block_num != 0) { 7502 rval = sata_ext_smart_selftest_read_log( 7503 sata_hba_inst, sdinfo, ext_selftest_log, 7504 block_num); 7505 if (rval != 0) 7506 goto out; 7507 } 7508 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7509 entry = 7510 &ext_selftest_log-> 7511 smart_ext_selftest_log_entries[index]; 7512 7513 for (count = 1; 7514 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7515 ++count) { 7516 uint8_t status; 7517 uint8_t code; 7518 uint8_t sense_key; 7519 uint8_t add_sense_code; 7520 uint8_t add_sense_code_qual; 7521 7522 /* If this is an unused entry, we are done */ 7523 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 7524 /* Broken firmware on some disks */ 7525 if (index + 1 == 7526 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 7527 --entry; 7528 --index; 7529 if (bcmp(entry, &empty, 7530 sizeof (empty)) == 0) 7531 goto out; 7532 } else 7533 goto out; 7534 } 7535 7536 if (only_one_block && 7537 start_index == index) 7538 goto out; 7539 7540 lpp->param_code[0] = 0; 7541 lpp->param_code[1] = count; 7542 lpp->param_ctrl_flags = 7543 LOG_CTRL_LP | LOG_CTRL_LBIN; 7544 lpp->param_len = 7545 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7546 7547 status = entry->smart_ext_selftest_log_status; 7548 status >>= 4; 7549 switch (status) { 7550 case 0: 7551 default: 7552 sense_key = KEY_NO_SENSE; 7553 add_sense_code = 7554 SD_SCSI_ASC_NO_ADD_SENSE; 7555 add_sense_code_qual = 0; 7556 break; 7557 case 1: 7558 sense_key = KEY_ABORTED_COMMAND; 7559 add_sense_code = 7560 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7561 add_sense_code_qual = SCSI_COMPONENT_81; 7562 break; 7563 case 2: 7564 sense_key = KEY_ABORTED_COMMAND; 7565 add_sense_code = 7566 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7567 add_sense_code_qual = SCSI_COMPONENT_82; 7568 break; 7569 case 3: 7570 sense_key = KEY_ABORTED_COMMAND; 7571 add_sense_code = 7572 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7573 add_sense_code_qual = SCSI_COMPONENT_83; 7574 break; 7575 case 4: 7576 sense_key = KEY_HARDWARE_ERROR; 7577 add_sense_code = 7578 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7579 add_sense_code_qual = SCSI_COMPONENT_84; 7580 break; 7581 case 5: 7582 sense_key = KEY_HARDWARE_ERROR; 7583 add_sense_code = 7584 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7585 add_sense_code_qual = SCSI_COMPONENT_85; 7586 break; 7587 case 6: 7588 sense_key = KEY_HARDWARE_ERROR; 7589 add_sense_code = 7590 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7591 add_sense_code_qual = SCSI_COMPONENT_86; 7592 break; 7593 case 7: 7594 sense_key = KEY_MEDIUM_ERROR; 7595 add_sense_code = 7596 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7597 add_sense_code_qual = SCSI_COMPONENT_87; 7598 break; 7599 case 8: 7600 sense_key = KEY_HARDWARE_ERROR; 7601 add_sense_code = 7602 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7603 add_sense_code_qual = SCSI_COMPONENT_88; 7604 break; 7605 } 7606 code = 0; /* unspecified */ 7607 status |= (code << 4); 7608 lpp->param_values[0] = status; 7609 lpp->param_values[1] = 0; /* unspecified */ 7610 lpp->param_values[2] = entry-> 7611 smart_ext_selftest_log_timestamp[1]; 7612 lpp->param_values[3] = entry-> 7613 smart_ext_selftest_log_timestamp[0]; 7614 if (status != 0) { 7615 lpp->param_values[4] = 0; 7616 lpp->param_values[5] = 0; 7617 lpp->param_values[6] = entry-> 7618 smart_ext_selftest_log_failing_lba 7619 [5]; 7620 lpp->param_values[7] = entry-> 7621 smart_ext_selftest_log_failing_lba 7622 [4]; 7623 lpp->param_values[8] = entry-> 7624 smart_ext_selftest_log_failing_lba 7625 [3]; 7626 lpp->param_values[9] = entry-> 7627 smart_ext_selftest_log_failing_lba 7628 [2]; 7629 lpp->param_values[10] = entry-> 7630 smart_ext_selftest_log_failing_lba 7631 [1]; 7632 lpp->param_values[11] = entry-> 7633 smart_ext_selftest_log_failing_lba 7634 [0]; 7635 } else { /* No bad block address */ 7636 lpp->param_values[4] = 0xff; 7637 lpp->param_values[5] = 0xff; 7638 lpp->param_values[6] = 0xff; 7639 lpp->param_values[7] = 0xff; 7640 lpp->param_values[8] = 0xff; 7641 lpp->param_values[9] = 0xff; 7642 lpp->param_values[10] = 0xff; 7643 lpp->param_values[11] = 0xff; 7644 } 7645 7646 lpp->param_values[12] = sense_key; 7647 lpp->param_values[13] = add_sense_code; 7648 lpp->param_values[14] = add_sense_code_qual; 7649 lpp->param_values[15] = 0; /* undefined */ 7650 7651 lpp = (struct log_parameter *) 7652 (((uint8_t *)lpp) + 7653 SCSI_LOG_PARAM_HDR_LEN + 7654 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7655 7656 --index; /* Back up to previous entry */ 7657 if (index < 0) { 7658 if (block_num > 0) { 7659 --block_num; 7660 } else { 7661 struct read_log_ext_directory 7662 logdir; 7663 7664 rval = 7665 sata_read_log_ext_directory( 7666 sata_hba_inst, sdinfo, 7667 &logdir); 7668 if (rval == -1) 7669 goto out; 7670 if ((logdir.read_log_ext_vers 7671 [0] == 0) && 7672 (logdir.read_log_ext_vers 7673 [1] == 0)) 7674 goto out; 7675 block_num = 7676 logdir.read_log_ext_nblks 7677 [EXT_SMART_SELFTEST_LOG_PAGE 7678 - 1][0]; 7679 block_num |= logdir. 7680 read_log_ext_nblks 7681 [EXT_SMART_SELFTEST_LOG_PAGE 7682 - 1][1] << 8; 7683 --block_num; 7684 only_one_block = 7685 (block_num == 0); 7686 } 7687 rval = sata_ext_smart_selftest_read_log( 7688 sata_hba_inst, sdinfo, 7689 ext_selftest_log, block_num); 7690 if (rval != 0) 7691 goto out; 7692 7693 index = 7694 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 7695 1; 7696 } 7697 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7698 entry = &ext_selftest_log-> 7699 smart_ext_selftest_log_entries[index]; 7700 } 7701 } 7702 out: 7703 kmem_free(ext_selftest_log, 7704 sizeof (struct smart_ext_selftest_log)); 7705 } else { 7706 struct smart_selftest_log *selftest_log; 7707 7708 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 7709 KM_SLEEP); 7710 7711 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 7712 selftest_log); 7713 7714 if (rval == 0) { 7715 int index; 7716 int count; 7717 struct smart_selftest_log_entry *entry; 7718 static const struct smart_selftest_log_entry empty = 7719 { 0 }; 7720 7721 index = selftest_log->smart_selftest_log_index; 7722 if (index == 0) 7723 goto done; 7724 --index; /* Correct for 0 origin */ 7725 entry = &selftest_log-> 7726 smart_selftest_log_entries[index]; 7727 for (count = 1; 7728 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7729 ++count) { 7730 uint8_t status; 7731 uint8_t code; 7732 uint8_t sense_key; 7733 uint8_t add_sense_code; 7734 uint8_t add_sense_code_qual; 7735 7736 if (bcmp(entry, &empty, sizeof (empty)) == 0) 7737 goto done; 7738 7739 lpp->param_code[0] = 0; 7740 lpp->param_code[1] = count; 7741 lpp->param_ctrl_flags = 7742 LOG_CTRL_LP | LOG_CTRL_LBIN; 7743 lpp->param_len = 7744 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7745 7746 status = entry->smart_selftest_log_status; 7747 status >>= 4; 7748 switch (status) { 7749 case 0: 7750 default: 7751 sense_key = KEY_NO_SENSE; 7752 add_sense_code = 7753 SD_SCSI_ASC_NO_ADD_SENSE; 7754 break; 7755 case 1: 7756 sense_key = KEY_ABORTED_COMMAND; 7757 add_sense_code = 7758 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7759 add_sense_code_qual = SCSI_COMPONENT_81; 7760 break; 7761 case 2: 7762 sense_key = KEY_ABORTED_COMMAND; 7763 add_sense_code = 7764 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7765 add_sense_code_qual = SCSI_COMPONENT_82; 7766 break; 7767 case 3: 7768 sense_key = KEY_ABORTED_COMMAND; 7769 add_sense_code = 7770 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7771 add_sense_code_qual = SCSI_COMPONENT_83; 7772 break; 7773 case 4: 7774 sense_key = KEY_HARDWARE_ERROR; 7775 add_sense_code = 7776 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7777 add_sense_code_qual = SCSI_COMPONENT_84; 7778 break; 7779 case 5: 7780 sense_key = KEY_HARDWARE_ERROR; 7781 add_sense_code = 7782 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7783 add_sense_code_qual = SCSI_COMPONENT_85; 7784 break; 7785 case 6: 7786 sense_key = KEY_HARDWARE_ERROR; 7787 add_sense_code = 7788 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7789 add_sense_code_qual = SCSI_COMPONENT_86; 7790 break; 7791 case 7: 7792 sense_key = KEY_MEDIUM_ERROR; 7793 add_sense_code = 7794 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7795 add_sense_code_qual = SCSI_COMPONENT_87; 7796 break; 7797 case 8: 7798 sense_key = KEY_HARDWARE_ERROR; 7799 add_sense_code = 7800 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7801 add_sense_code_qual = SCSI_COMPONENT_88; 7802 break; 7803 } 7804 code = 0; /* unspecified */ 7805 status |= (code << 4); 7806 lpp->param_values[0] = status; 7807 lpp->param_values[1] = 0; /* unspecified */ 7808 lpp->param_values[2] = entry-> 7809 smart_selftest_log_timestamp[1]; 7810 lpp->param_values[3] = entry-> 7811 smart_selftest_log_timestamp[0]; 7812 if (status != 0) { 7813 lpp->param_values[4] = 0; 7814 lpp->param_values[5] = 0; 7815 lpp->param_values[6] = 0; 7816 lpp->param_values[7] = 0; 7817 lpp->param_values[8] = entry-> 7818 smart_selftest_log_failing_lba[3]; 7819 lpp->param_values[9] = entry-> 7820 smart_selftest_log_failing_lba[2]; 7821 lpp->param_values[10] = entry-> 7822 smart_selftest_log_failing_lba[1]; 7823 lpp->param_values[11] = entry-> 7824 smart_selftest_log_failing_lba[0]; 7825 } else { /* No block address */ 7826 lpp->param_values[4] = 0xff; 7827 lpp->param_values[5] = 0xff; 7828 lpp->param_values[6] = 0xff; 7829 lpp->param_values[7] = 0xff; 7830 lpp->param_values[8] = 0xff; 7831 lpp->param_values[9] = 0xff; 7832 lpp->param_values[10] = 0xff; 7833 lpp->param_values[11] = 0xff; 7834 } 7835 lpp->param_values[12] = sense_key; 7836 lpp->param_values[13] = add_sense_code; 7837 lpp->param_values[14] = add_sense_code_qual; 7838 lpp->param_values[15] = 0; /* undefined */ 7839 7840 lpp = (struct log_parameter *) 7841 (((uint8_t *)lpp) + 7842 SCSI_LOG_PARAM_HDR_LEN + 7843 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7844 --index; /* back up to previous entry */ 7845 if (index < 0) { 7846 index = 7847 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 7848 } 7849 entry = &selftest_log-> 7850 smart_selftest_log_entries[index]; 7851 } 7852 } 7853 done: 7854 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 7855 } 7856 7857 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 7858 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 7859 } 7860 7861 /* 7862 * sata_build_lsense_page_2f() is used to create the 7863 * SCSI LOG SENSE page 0x10 (informational exceptions) 7864 * 7865 * Takes a sata_drive_info t * and the address of a buffer 7866 * in which to create the page information as well as a sata_hba_inst_t *. 7867 * 7868 * Returns the number of bytes valid in the buffer. 7869 */ 7870 static int 7871 sata_build_lsense_page_2f( 7872 sata_drive_info_t *sdinfo, 7873 uint8_t *buf, 7874 sata_hba_inst_t *sata_hba_inst) 7875 { 7876 struct log_parameter *lpp = (struct log_parameter *)buf; 7877 int rval; 7878 uint8_t *smart_data; 7879 uint8_t temp; 7880 sata_id_t *sata_id; 7881 #define SMART_NO_TEMP 0xff 7882 7883 lpp->param_code[0] = 0; 7884 lpp->param_code[1] = 0; 7885 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7886 7887 /* Now get the SMART status w.r.t. threshold exceeded */ 7888 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 7889 switch (rval) { 7890 case 1: 7891 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 7892 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 7893 break; 7894 case 0: 7895 case -1: /* failed to get data */ 7896 lpp->param_values[0] = 0; /* No failure predicted */ 7897 lpp->param_values[1] = 0; 7898 break; 7899 #if defined(SATA_DEBUG) 7900 default: 7901 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 7902 /* NOTREACHED */ 7903 #endif 7904 } 7905 7906 sata_id = &sdinfo->satadrv_id; 7907 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 7908 temp = SMART_NO_TEMP; 7909 else { 7910 /* Now get the temperature */ 7911 smart_data = kmem_zalloc(512, KM_SLEEP); 7912 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 7913 SCT_STATUS_LOG_PAGE, 1); 7914 if (rval == -1) 7915 temp = SMART_NO_TEMP; 7916 else { 7917 temp = smart_data[200]; 7918 if (temp & 0x80) { 7919 if (temp & 0x7f) 7920 temp = 0; 7921 else 7922 temp = SMART_NO_TEMP; 7923 } 7924 } 7925 kmem_free(smart_data, 512); 7926 } 7927 7928 lpp->param_values[2] = temp; /* most recent temperature */ 7929 lpp->param_values[3] = 0; /* required vendor specific byte */ 7930 7931 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 7932 7933 7934 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 7935 } 7936 7937 /* 7938 * sata_build_lsense_page_30() is used to create the 7939 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 7940 * 7941 * Takes a sata_drive_info t * and the address of a buffer 7942 * in which to create the page information as well as a sata_hba_inst_t *. 7943 * 7944 * Returns the number of bytes valid in the buffer. 7945 */ 7946 static int 7947 sata_build_lsense_page_30( 7948 sata_drive_info_t *sdinfo, 7949 uint8_t *buf, 7950 sata_hba_inst_t *sata_hba_inst) 7951 { 7952 struct smart_data *smart_data = (struct smart_data *)buf; 7953 int rval; 7954 7955 /* Now do the SMART READ DATA */ 7956 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7957 if (rval == -1) 7958 return (0); 7959 7960 return (sizeof (struct smart_data)); 7961 } 7962 7963 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 7964 7965 /* 7966 * Start command for ATAPI device. 7967 * This function processes scsi_pkt requests. 7968 * Only CD/DVD devices are supported. 7969 * Most commands are packet without any translation into Packet Command. 7970 * Some may be trapped and executed as SATA commands (not clear which one). 7971 * 7972 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 7973 * execution). 7974 * Returns other TRAN_XXXX codes if command is not accepted or completed 7975 * (see return values for sata_hba_start()). 7976 * 7977 * Note: 7978 * Inquiry cdb format differs between transport version 2 and 3. 7979 * However, the transport version 3 devices that were checked did not adhere 7980 * to the specification (ignored MSB of the allocation length). Therefore, 7981 * the transport version is not checked, but Inquiry allocation length is 7982 * truncated to 255 bytes if the original allocation length set-up by the 7983 * target driver is greater than 255 bytes. 7984 */ 7985 static int 7986 sata_txlt_atapi(sata_pkt_txlate_t *spx) 7987 { 7988 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7989 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7990 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7991 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 7992 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 7993 &spx->txlt_sata_pkt->satapkt_device); 7994 int cport = SATA_TXLT_CPORT(spx); 7995 int cdblen; 7996 int rval; 7997 int synch; 7998 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 7999 8000 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8001 8002 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 8003 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 8004 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8005 return (rval); 8006 } 8007 8008 /* 8009 * ATAPI device executes some ATA commands in addition to MMC command 8010 * set. These ATA commands may be executed by the regular SATA 8011 * translation functions. None needs to be captured now. 8012 * Other commands belong to MMC command set and are delivered 8013 * to ATAPI device via Packet Command. 8014 */ 8015 8016 /* Check the size of cdb */ 8017 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 8018 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 8019 sata_log(NULL, CE_WARN, 8020 "sata: invalid ATAPI cdb length %d", 8021 scsipkt->pkt_cdblen); 8022 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8023 return (TRAN_BADPKT); 8024 } 8025 8026 SATAATAPITRACE(spx, cdblen); 8027 8028 /* 8029 * For non-read/write commands we need to 8030 * map buffer 8031 */ 8032 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 8033 case SCMD_READ: 8034 case SCMD_READ_G1: 8035 case SCMD_READ_G5: 8036 case SCMD_READ_G4: 8037 case SCMD_WRITE: 8038 case SCMD_WRITE_G1: 8039 case SCMD_WRITE_G5: 8040 case SCMD_WRITE_G4: 8041 break; 8042 default: 8043 if (bp != NULL) { 8044 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 8045 bp_mapin(bp); 8046 } 8047 break; 8048 } 8049 /* 8050 * scmd->satacmd_flags.sata_data_direction default - 8051 * SATA_DIR_NODATA_XFER - is set by 8052 * sata_txlt_generic_pkt_info(). 8053 */ 8054 if (scmd->satacmd_bp) { 8055 if (scmd->satacmd_bp->b_flags & B_READ) { 8056 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8057 } else { 8058 scmd->satacmd_flags.sata_data_direction = 8059 SATA_DIR_WRITE; 8060 } 8061 } 8062 8063 /* 8064 * Set up ATAPI packet command. 8065 */ 8066 8067 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8068 8069 /* Copy cdb into sata_cmd */ 8070 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8071 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8072 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 8073 8074 /* See note in the command header */ 8075 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 8076 if (scmd->satacmd_acdb[3] != 0) 8077 scmd->satacmd_acdb[4] = 255; 8078 } 8079 8080 #ifdef SATA_DEBUG 8081 if (sata_debug_flags & SATA_DBG_ATAPI) { 8082 uint8_t *p = scmd->satacmd_acdb; 8083 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 8084 8085 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 8086 "%02x %02x %02x %02x %02x %02x %02x %02x " 8087 "%2x %02x %02x %02x %02x %02x %02x %02x", 8088 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8089 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8090 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 8091 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 8092 } 8093 #endif 8094 8095 /* 8096 * Preset request sense data to NO SENSE. 8097 * If there is no way to get error information via Request Sense, 8098 * the packet request sense data would not have to be modified by HBA, 8099 * but it could be returned as is. 8100 */ 8101 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8102 sata_fixed_sense_data_preset( 8103 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8104 8105 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 8106 /* Need callback function */ 8107 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 8108 synch = FALSE; 8109 } else 8110 synch = TRUE; 8111 8112 /* Transfer command to HBA */ 8113 if (sata_hba_start(spx, &rval) != 0) { 8114 /* Pkt not accepted for execution */ 8115 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8116 return (rval); 8117 } 8118 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8119 /* 8120 * If execution is non-synchronous, 8121 * a callback function will handle potential errors, translate 8122 * the response and will do a callback to a target driver. 8123 * If it was synchronous, use the same framework callback to check 8124 * an execution status. 8125 */ 8126 if (synch) { 8127 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8128 "synchronous execution status %x\n", 8129 spx->txlt_sata_pkt->satapkt_reason); 8130 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 8131 } 8132 return (TRAN_ACCEPT); 8133 } 8134 8135 8136 /* 8137 * ATAPI Packet command completion. 8138 * 8139 * Failure of the command passed via Packet command are considered device 8140 * error. SATA HBA driver would have to retrieve error data (via Request 8141 * Sense command delivered via error retrieval sata packet) and copy it 8142 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 8143 */ 8144 static void 8145 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 8146 { 8147 sata_pkt_txlate_t *spx = 8148 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 8149 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8150 struct scsi_extended_sense *sense; 8151 struct buf *bp; 8152 int rval; 8153 8154 #ifdef SATA_DEBUG 8155 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 8156 #endif 8157 8158 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 8159 STATE_SENT_CMD | STATE_GOT_STATUS; 8160 8161 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 8162 /* Normal completion */ 8163 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 8164 scsipkt->pkt_state |= STATE_XFERRED_DATA; 8165 scsipkt->pkt_reason = CMD_CMPLT; 8166 *scsipkt->pkt_scbp = STATUS_GOOD; 8167 if (spx->txlt_tmp_buf != NULL) { 8168 /* Temporary buffer was used */ 8169 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8170 if (bp->b_flags & B_READ) { 8171 rval = ddi_dma_sync( 8172 spx->txlt_buf_dma_handle, 0, 0, 8173 DDI_DMA_SYNC_FORCPU); 8174 ASSERT(rval == DDI_SUCCESS); 8175 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 8176 bp->b_bcount); 8177 } 8178 } 8179 } else { 8180 /* 8181 * Something went wrong - analyze return 8182 */ 8183 *scsipkt->pkt_scbp = STATUS_CHECK; 8184 sense = sata_arq_sense(spx); 8185 8186 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8187 scsipkt->pkt_reason = CMD_INCOMPLETE; 8188 /* 8189 * We may not have ARQ data if there was a double 8190 * error. But sense data in sata packet was pre-set 8191 * with NO SENSE so it is valid even if HBA could 8192 * not retrieve a real sense data. 8193 * Just copy this sense data into scsi pkt sense area. 8194 */ 8195 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 8196 SATA_ATAPI_MIN_RQSENSE_LEN); 8197 #ifdef SATA_DEBUG 8198 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 8199 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8200 "sata_txlt_atapi_completion: %02x\n" 8201 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8202 " %02x %02x %02x %02x %02x %02x " 8203 " %02x %02x %02x %02x %02x %02x\n", 8204 scsipkt->pkt_reason, 8205 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8206 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8207 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8208 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8209 rqsp[16], rqsp[17]); 8210 } 8211 #endif 8212 } else { 8213 switch (sata_pkt->satapkt_reason) { 8214 case SATA_PKT_PORT_ERROR: 8215 /* 8216 * We have no device data. 8217 */ 8218 scsipkt->pkt_reason = CMD_INCOMPLETE; 8219 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8220 STATE_GOT_TARGET | STATE_SENT_CMD | 8221 STATE_GOT_STATUS); 8222 sense->es_key = KEY_HARDWARE_ERROR; 8223 8224 /* No extended sense key - no info available */ 8225 scsipkt->pkt_reason = CMD_INCOMPLETE; 8226 break; 8227 8228 case SATA_PKT_TIMEOUT: 8229 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 8230 /* No extended sense key */ 8231 /* 8232 * Need to check if HARDWARE_ERROR/ 8233 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 8234 * appropriate. 8235 */ 8236 break; 8237 8238 case SATA_PKT_ABORTED: 8239 scsipkt->pkt_reason = CMD_ABORTED; 8240 /* Should we set key COMMAND_ABPRTED? */ 8241 break; 8242 8243 case SATA_PKT_RESET: 8244 scsipkt->pkt_reason = CMD_RESET; 8245 /* 8246 * May be we should set Unit Attention / 8247 * Reset. Perhaps the same should be 8248 * returned for disks.... 8249 */ 8250 sense->es_key = KEY_UNIT_ATTENTION; 8251 sense->es_add_code = SD_SCSI_ASC_RESET; 8252 break; 8253 8254 default: 8255 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8256 "sata_txlt_atapi_completion: " 8257 "invalid packet completion reason")); 8258 scsipkt->pkt_reason = CMD_TRAN_ERR; 8259 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8260 STATE_GOT_TARGET | STATE_SENT_CMD | 8261 STATE_GOT_STATUS); 8262 break; 8263 } 8264 } 8265 } 8266 8267 SATAATAPITRACE(spx, 0); 8268 8269 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 8270 scsipkt->pkt_comp != NULL) { 8271 /* scsi callback required */ 8272 (*scsipkt->pkt_comp)(scsipkt); 8273 } 8274 } 8275 8276 /* 8277 * Set up error retrieval sata command for ATAPI Packet Command error data 8278 * recovery. 8279 * 8280 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 8281 * returns SATA_FAILURE otherwise. 8282 */ 8283 8284 static int 8285 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 8286 { 8287 sata_pkt_t *spkt = spx->txlt_sata_pkt; 8288 sata_cmd_t *scmd; 8289 struct buf *bp; 8290 8291 /* 8292 * Allocate dma-able buffer error data. 8293 * Buffer allocation will take care of buffer alignment and other DMA 8294 * attributes. 8295 */ 8296 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 8297 if (bp == NULL) { 8298 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 8299 "sata_get_err_retrieval_pkt: " 8300 "cannot allocate buffer for error data", NULL); 8301 return (SATA_FAILURE); 8302 } 8303 bp_mapin(bp); /* make data buffer accessible */ 8304 8305 /* Operation modes are up to the caller */ 8306 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8307 8308 /* Synchronous mode, no callback - may be changed by the caller */ 8309 spkt->satapkt_comp = NULL; 8310 spkt->satapkt_time = sata_default_pkt_time; 8311 8312 scmd = &spkt->satapkt_cmd; 8313 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8314 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8315 8316 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8317 8318 /* 8319 * Set-up acdb. Request Sense CDB (packet command content) is 8320 * not in DMA-able buffer. Its handling is HBA-specific (how 8321 * it is transfered into packet FIS). 8322 */ 8323 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8324 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 8325 /* Following zeroing of pad bytes may not be necessary */ 8326 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 8327 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 8328 8329 /* 8330 * Set-up pointer to the buffer handle, so HBA can sync buffer 8331 * before accessing it. Handle is in usual place in translate struct. 8332 */ 8333 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 8334 8335 /* 8336 * Preset request sense data to NO SENSE. 8337 * Here it is redundant, only for a symetry with scsi-originated 8338 * packets. It should not be used for anything but debugging. 8339 */ 8340 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8341 sata_fixed_sense_data_preset( 8342 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8343 8344 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8345 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8346 8347 return (SATA_SUCCESS); 8348 } 8349 8350 /* 8351 * Set-up ATAPI packet command. 8352 * Data transfer direction has to be set-up in sata_cmd structure prior to 8353 * calling this function. 8354 * 8355 * Returns void 8356 */ 8357 8358 static void 8359 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 8360 { 8361 scmd->satacmd_addr_type = 0; /* N/A */ 8362 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 8363 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 8364 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 8365 scmd->satacmd_lba_high_lsb = 8366 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 8367 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 8368 8369 /* 8370 * We want all data to be transfered via DMA. 8371 * But specify it only if drive supports DMA and DMA mode is 8372 * selected - some drives are sensitive about it. 8373 * Hopefully it wil work for all drives.... 8374 */ 8375 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 8376 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 8377 8378 /* 8379 * Features register requires special care for devices that use 8380 * Serial ATA bridge - they need an explicit specification of 8381 * the data transfer direction for Packet DMA commands. 8382 * Setting this bit is harmless if DMA is not used. 8383 * 8384 * Many drives do not implement word 80, specifying what ATA/ATAPI 8385 * spec they follow. 8386 * We are arbitrarily following the latest SerialATA 2.6 spec, 8387 * which uses ATA/ATAPI 6 specification for Identify Data, unless 8388 * ATA/ATAPI-7 support is explicitly indicated. 8389 */ 8390 if (sdinfo->satadrv_id.ai_majorversion != 0 && 8391 sdinfo->satadrv_id.ai_majorversion != 0xffff && 8392 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 8393 /* 8394 * Specification of major version is valid and version 7 8395 * is supported. It does automatically imply that all 8396 * spec features are supported. For now, we assume that 8397 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 8398 */ 8399 if ((sdinfo->satadrv_id.ai_dirdma & 8400 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 8401 if (scmd->satacmd_flags.sata_data_direction == 8402 SATA_DIR_READ) 8403 scmd->satacmd_features_reg |= 8404 SATA_ATAPI_F_DATA_DIR_READ; 8405 } 8406 } 8407 } 8408 8409 8410 #ifdef SATA_DEBUG 8411 8412 /* Display 18 bytes of Inquiry data */ 8413 static void 8414 sata_show_inqry_data(uint8_t *buf) 8415 { 8416 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 8417 uint8_t *p; 8418 8419 cmn_err(CE_NOTE, "Inquiry data:"); 8420 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 8421 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 8422 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 8423 cmn_err(CE_NOTE, "ATAPI transport version %d", 8424 SATA_ATAPI_TRANS_VERSION(inq)); 8425 cmn_err(CE_NOTE, "response data format %d, aenc %d", 8426 inq->inq_rdf, inq->inq_aenc); 8427 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 8428 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 8429 p = (uint8_t *)inq->inq_vid; 8430 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 8431 "%02x %02x %02x %02x", 8432 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8433 p = (uint8_t *)inq->inq_vid; 8434 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 8435 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8436 8437 p = (uint8_t *)inq->inq_pid; 8438 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 8439 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 8440 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8441 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8442 p = (uint8_t *)inq->inq_pid; 8443 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 8444 "%c %c %c %c %c %c %c %c", 8445 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8446 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8447 8448 p = (uint8_t *)inq->inq_revision; 8449 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 8450 p[0], p[1], p[2], p[3]); 8451 p = (uint8_t *)inq->inq_revision; 8452 cmn_err(CE_NOTE, "revision: %c %c %c %c", 8453 p[0], p[1], p[2], p[3]); 8454 8455 } 8456 8457 8458 static void 8459 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 8460 { 8461 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 8462 8463 if (scsi_pkt == NULL) 8464 return; 8465 if (count != 0) { 8466 /* saving cdb */ 8467 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 8468 SATA_ATAPI_MAX_CDB_LEN); 8469 bcopy(scsi_pkt->pkt_cdbp, 8470 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 8471 } else { 8472 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 8473 sts_sensedata, 8474 sata_atapi_trace[sata_atapi_trace_index].arqs, 8475 SATA_ATAPI_MIN_RQSENSE_LEN); 8476 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 8477 scsi_pkt->pkt_reason; 8478 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 8479 spx->txlt_sata_pkt->satapkt_reason; 8480 8481 if (++sata_atapi_trace_index >= 64) 8482 sata_atapi_trace_index = 0; 8483 } 8484 } 8485 8486 #endif 8487 8488 /* 8489 * Fetch inquiry data from ATAPI device 8490 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 8491 * 8492 * Note: 8493 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 8494 * where the caller expects to see the inquiry data. 8495 * 8496 */ 8497 8498 static int 8499 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 8500 sata_address_t *saddr, struct scsi_inquiry *inq) 8501 { 8502 sata_pkt_txlate_t *spx; 8503 sata_pkt_t *spkt; 8504 struct buf *bp; 8505 sata_drive_info_t *sdinfo; 8506 sata_cmd_t *scmd; 8507 int rval; 8508 uint8_t *rqsp; 8509 #ifdef SATA_DEBUG 8510 char msg_buf[MAXPATHLEN]; 8511 #endif 8512 8513 ASSERT(sata_hba != NULL); 8514 8515 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8516 spx->txlt_sata_hba_inst = sata_hba; 8517 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8518 spkt = sata_pkt_alloc(spx, NULL); 8519 if (spkt == NULL) { 8520 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8521 return (SATA_FAILURE); 8522 } 8523 /* address is needed now */ 8524 spkt->satapkt_device.satadev_addr = *saddr; 8525 8526 /* scsi_inquiry size buffer */ 8527 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 8528 if (bp == NULL) { 8529 sata_pkt_free(spx); 8530 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8531 SATA_LOG_D((sata_hba, CE_WARN, 8532 "sata_get_atapi_inquiry_data: " 8533 "cannot allocate data buffer")); 8534 return (SATA_FAILURE); 8535 } 8536 bp_mapin(bp); /* make data buffer accessible */ 8537 8538 scmd = &spkt->satapkt_cmd; 8539 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8540 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8541 8542 /* Use synchronous mode */ 8543 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8544 spkt->satapkt_comp = NULL; 8545 spkt->satapkt_time = sata_default_pkt_time; 8546 8547 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8548 8549 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8550 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8551 8552 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8553 sdinfo = sata_get_device_info(sata_hba, 8554 &spx->txlt_sata_pkt->satapkt_device); 8555 if (sdinfo == NULL) { 8556 /* we have to be carefull about the disapearing device */ 8557 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8558 rval = SATA_FAILURE; 8559 goto cleanup; 8560 } 8561 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8562 8563 /* 8564 * Set-up acdb. This works for atapi transport version 2 and later. 8565 */ 8566 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8567 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8568 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8569 scmd->satacmd_acdb[1] = 0x00; 8570 scmd->satacmd_acdb[2] = 0x00; 8571 scmd->satacmd_acdb[3] = 0x00; 8572 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8573 scmd->satacmd_acdb[5] = 0x00; 8574 8575 sata_fixed_sense_data_preset( 8576 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8577 8578 /* Transfer command to HBA */ 8579 if (sata_hba_start(spx, &rval) != 0) { 8580 /* Pkt not accepted for execution */ 8581 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8582 "sata_get_atapi_inquiry_data: " 8583 "Packet not accepted for execution - ret: %02x", rval); 8584 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8585 rval = SATA_FAILURE; 8586 goto cleanup; 8587 } 8588 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8589 8590 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8591 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8592 "sata_get_atapi_inquiry_data: " 8593 "Packet completed successfully - ret: %02x", rval); 8594 /* 8595 * Sync buffer. Handle is in usual place in translate struct. 8596 * Normal completion - copy data into caller's buffer 8597 */ 8598 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8599 DDI_DMA_SYNC_FORCPU); 8600 ASSERT(rval == DDI_SUCCESS); 8601 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 8602 sizeof (struct scsi_inquiry)); 8603 #ifdef SATA_DEBUG 8604 if (sata_debug_flags & SATA_DBG_ATAPI) { 8605 sata_show_inqry_data((uint8_t *)inq); 8606 } 8607 #endif 8608 rval = SATA_SUCCESS; 8609 } else { 8610 /* 8611 * Something went wrong - analyze return - check rqsense data 8612 */ 8613 rval = SATA_FAILURE; 8614 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8615 /* 8616 * ARQ data hopefull show something other than NO SENSE 8617 */ 8618 rqsp = scmd->satacmd_rqsense; 8619 #ifdef SATA_DEBUG 8620 if (sata_debug_flags & SATA_DBG_ATAPI) { 8621 msg_buf[0] = '\0'; 8622 (void) snprintf(msg_buf, MAXPATHLEN, 8623 "ATAPI packet completion reason: %02x\n" 8624 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 8625 " %02x %02x %02x %02x %02x %02x\n" 8626 " %02x %02x %02x %02x %02x %02x", 8627 spkt->satapkt_reason, 8628 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8629 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8630 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8631 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8632 rqsp[16], rqsp[17]); 8633 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8634 "%s", msg_buf); 8635 } 8636 #endif 8637 } else { 8638 switch (spkt->satapkt_reason) { 8639 case SATA_PKT_PORT_ERROR: 8640 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8641 "sata_get_atapi_inquiry_data: " 8642 "packet reason: port error", NULL); 8643 break; 8644 8645 case SATA_PKT_TIMEOUT: 8646 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8647 "sata_get_atapi_inquiry_data: " 8648 "packet reason: timeout", NULL); 8649 break; 8650 8651 case SATA_PKT_ABORTED: 8652 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8653 "sata_get_atapi_inquiry_data: " 8654 "packet reason: aborted", NULL); 8655 break; 8656 8657 case SATA_PKT_RESET: 8658 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8659 "sata_get_atapi_inquiry_data: " 8660 "packet reason: reset\n", NULL); 8661 break; 8662 default: 8663 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8664 "sata_get_atapi_inquiry_data: " 8665 "invalid packet reason: %02x\n", 8666 spkt->satapkt_reason); 8667 break; 8668 } 8669 } 8670 } 8671 cleanup: 8672 sata_free_local_buffer(spx); 8673 sata_pkt_free(spx); 8674 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8675 return (rval); 8676 } 8677 8678 8679 8680 8681 8682 #if 0 8683 #ifdef SATA_DEBUG 8684 8685 /* 8686 * Test ATAPI packet command. 8687 * Single threaded test: send packet command in synch mode, process completion 8688 * 8689 */ 8690 static void 8691 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 8692 { 8693 sata_pkt_txlate_t *spx; 8694 sata_pkt_t *spkt; 8695 struct buf *bp; 8696 sata_device_t sata_device; 8697 sata_drive_info_t *sdinfo; 8698 sata_cmd_t *scmd; 8699 int rval; 8700 uint8_t *rqsp; 8701 8702 ASSERT(sata_hba_inst != NULL); 8703 sata_device.satadev_addr.cport = cport; 8704 sata_device.satadev_addr.pmport = 0; 8705 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8706 sata_device.satadev_rev = SATA_DEVICE_REV; 8707 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8708 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8709 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8710 if (sdinfo == NULL) { 8711 sata_log(sata_hba_inst, CE_WARN, 8712 "sata_test_atapi_packet_command: " 8713 "no device info for cport %d", 8714 sata_device.satadev_addr.cport); 8715 return; 8716 } 8717 8718 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8719 spx->txlt_sata_hba_inst = sata_hba_inst; 8720 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8721 spkt = sata_pkt_alloc(spx, NULL); 8722 if (spkt == NULL) { 8723 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8724 return; 8725 } 8726 /* address is needed now */ 8727 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 8728 8729 /* 1024k buffer */ 8730 bp = sata_alloc_local_buffer(spx, 1024); 8731 if (bp == NULL) { 8732 sata_pkt_free(spx); 8733 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8734 sata_log(sata_hba_inst, CE_WARN, 8735 "sata_test_atapi_packet_command: " 8736 "cannot allocate data buffer"); 8737 return; 8738 } 8739 bp_mapin(bp); /* make data buffer accessible */ 8740 8741 scmd = &spkt->satapkt_cmd; 8742 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8743 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8744 8745 /* Use synchronous mode */ 8746 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8747 8748 /* Synchronous mode, no callback - may be changed by the caller */ 8749 spkt->satapkt_comp = NULL; 8750 spkt->satapkt_time = sata_default_pkt_time; 8751 8752 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8753 8754 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8755 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8756 8757 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8758 8759 /* Set-up acdb. */ 8760 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8761 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8762 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8763 scmd->satacmd_acdb[1] = 0x00; 8764 scmd->satacmd_acdb[2] = 0x00; 8765 scmd->satacmd_acdb[3] = 0x00; 8766 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8767 scmd->satacmd_acdb[5] = 0x00; 8768 8769 sata_fixed_sense_data_preset( 8770 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8771 8772 /* Transfer command to HBA */ 8773 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8774 if (sata_hba_start(spx, &rval) != 0) { 8775 /* Pkt not accepted for execution */ 8776 sata_log(sata_hba_inst, CE_WARN, 8777 "sata_test_atapi_packet_command: " 8778 "Packet not accepted for execution - ret: %02x", rval); 8779 mutex_exit( 8780 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8781 goto cleanup; 8782 } 8783 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8784 8785 /* 8786 * Sync buffer. Handle is in usual place in translate struct. 8787 */ 8788 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8789 DDI_DMA_SYNC_FORCPU); 8790 ASSERT(rval == DDI_SUCCESS); 8791 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8792 sata_log(sata_hba_inst, CE_WARN, 8793 "sata_test_atapi_packet_command: " 8794 "Packet completed successfully"); 8795 /* 8796 * Normal completion - show inquiry data 8797 */ 8798 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 8799 } else { 8800 /* 8801 * Something went wrong - analyze return - check rqsense data 8802 */ 8803 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8804 /* 8805 * ARQ data hopefull show something other than NO SENSE 8806 */ 8807 rqsp = scmd->satacmd_rqsense; 8808 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8809 "ATAPI packet completion reason: %02x\n" 8810 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8811 " %02x %02x %02x %02x %02x %02x " 8812 " %02x %02x %02x %02x %02x %02x\n", 8813 spkt->satapkt_reason, 8814 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8815 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8816 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8817 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8818 rqsp[16], rqsp[17]); 8819 } else { 8820 switch (spkt->satapkt_reason) { 8821 case SATA_PKT_PORT_ERROR: 8822 sata_log(sata_hba_inst, CE_WARN, 8823 "sata_test_atapi_packet_command: " 8824 "packet reason: port error\n"); 8825 break; 8826 8827 case SATA_PKT_TIMEOUT: 8828 sata_log(sata_hba_inst, CE_WARN, 8829 "sata_test_atapi_packet_command: " 8830 "packet reason: timeout\n"); 8831 break; 8832 8833 case SATA_PKT_ABORTED: 8834 sata_log(sata_hba_inst, CE_WARN, 8835 "sata_test_atapi_packet_command: " 8836 "packet reason: aborted\n"); 8837 break; 8838 8839 case SATA_PKT_RESET: 8840 sata_log(sata_hba_inst, CE_WARN, 8841 "sata_test_atapi_packet_command: " 8842 "packet reason: reset\n"); 8843 break; 8844 default: 8845 sata_log(sata_hba_inst, CE_WARN, 8846 "sata_test_atapi_packet_command: " 8847 "invalid packet reason: %02x\n", 8848 spkt->satapkt_reason); 8849 break; 8850 } 8851 } 8852 } 8853 cleanup: 8854 sata_free_local_buffer(spx); 8855 sata_pkt_free(spx); 8856 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8857 } 8858 8859 #endif /* SATA_DEBUG */ 8860 #endif /* 1 */ 8861 8862 8863 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 8864 8865 /* 8866 * Validate sata_tran info 8867 * SATA_FAILURE returns if structure is inconsistent or structure revision 8868 * does not match one used by the framework. 8869 * 8870 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 8871 * required function pointers. 8872 * Returns SATA_FAILURE otherwise. 8873 */ 8874 static int 8875 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 8876 { 8877 /* 8878 * SATA_TRAN_HBA_REV is the current (highest) revision number 8879 * of the SATA interface. 8880 */ 8881 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 8882 sata_log(NULL, CE_WARN, 8883 "sata: invalid sata_hba_tran version %d for driver %s", 8884 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 8885 return (SATA_FAILURE); 8886 } 8887 8888 if (dip != sata_tran->sata_tran_hba_dip) { 8889 SATA_LOG_D((NULL, CE_WARN, 8890 "sata: inconsistent sata_tran_hba_dip " 8891 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 8892 return (SATA_FAILURE); 8893 } 8894 8895 if (sata_tran->sata_tran_probe_port == NULL || 8896 sata_tran->sata_tran_start == NULL || 8897 sata_tran->sata_tran_abort == NULL || 8898 sata_tran->sata_tran_reset_dport == NULL || 8899 sata_tran->sata_tran_hotplug_ops == NULL || 8900 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 8901 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 8902 NULL) { 8903 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 8904 "required functions")); 8905 } 8906 return (SATA_SUCCESS); 8907 } 8908 8909 /* 8910 * Remove HBA instance from sata_hba_list. 8911 */ 8912 static void 8913 sata_remove_hba_instance(dev_info_t *dip) 8914 { 8915 sata_hba_inst_t *sata_hba_inst; 8916 8917 mutex_enter(&sata_mutex); 8918 for (sata_hba_inst = sata_hba_list; 8919 sata_hba_inst != (struct sata_hba_inst *)NULL; 8920 sata_hba_inst = sata_hba_inst->satahba_next) { 8921 if (sata_hba_inst->satahba_dip == dip) 8922 break; 8923 } 8924 8925 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 8926 #ifdef SATA_DEBUG 8927 cmn_err(CE_WARN, "sata_remove_hba_instance: " 8928 "unknown HBA instance\n"); 8929 #endif 8930 ASSERT(FALSE); 8931 } 8932 if (sata_hba_inst == sata_hba_list) { 8933 sata_hba_list = sata_hba_inst->satahba_next; 8934 if (sata_hba_list) { 8935 sata_hba_list->satahba_prev = 8936 (struct sata_hba_inst *)NULL; 8937 } 8938 if (sata_hba_inst == sata_hba_list_tail) { 8939 sata_hba_list_tail = NULL; 8940 } 8941 } else if (sata_hba_inst == sata_hba_list_tail) { 8942 sata_hba_list_tail = sata_hba_inst->satahba_prev; 8943 if (sata_hba_list_tail) { 8944 sata_hba_list_tail->satahba_next = 8945 (struct sata_hba_inst *)NULL; 8946 } 8947 } else { 8948 sata_hba_inst->satahba_prev->satahba_next = 8949 sata_hba_inst->satahba_next; 8950 sata_hba_inst->satahba_next->satahba_prev = 8951 sata_hba_inst->satahba_prev; 8952 } 8953 mutex_exit(&sata_mutex); 8954 } 8955 8956 8957 8958 8959 8960 /* 8961 * Probe all SATA ports of the specified HBA instance. 8962 * The assumption is that there are no target and attachment point minor nodes 8963 * created by the boot subsystems, so we do not need to prune device tree. 8964 * 8965 * This function is called only from sata_hba_attach(). It does not have to 8966 * be protected by controller mutex, because the hba_attached flag is not set 8967 * yet and no one would be touching this HBA instance other than this thread. 8968 * Determines if port is active and what type of the device is attached 8969 * (if any). Allocates necessary structures for each port. 8970 * 8971 * An AP (Attachement Point) node is created for each SATA device port even 8972 * when there is no device attached. 8973 */ 8974 8975 static void 8976 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 8977 { 8978 dev_info_t *dip = SATA_DIP(sata_hba_inst); 8979 int ncport, npmport; 8980 sata_cport_info_t *cportinfo; 8981 sata_drive_info_t *drive; 8982 sata_pmult_info_t *pminfo; 8983 sata_pmport_info_t *pmportinfo; 8984 sata_device_t sata_device; 8985 int rval; 8986 dev_t minor_number; 8987 char name[16]; 8988 clock_t start_time, cur_time; 8989 8990 /* 8991 * Probe controller ports first, to find port status and 8992 * any port multiplier attached. 8993 */ 8994 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 8995 /* allocate cport structure */ 8996 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 8997 ASSERT(cportinfo != NULL); 8998 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 8999 9000 mutex_enter(&cportinfo->cport_mutex); 9001 9002 cportinfo->cport_addr.cport = ncport; 9003 cportinfo->cport_addr.pmport = 0; 9004 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 9005 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9006 cportinfo->cport_state |= SATA_STATE_PROBING; 9007 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 9008 9009 /* 9010 * Regardless if a port is usable or not, create 9011 * an attachment point 9012 */ 9013 mutex_exit(&cportinfo->cport_mutex); 9014 minor_number = 9015 SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0); 9016 (void) sprintf(name, "%d", ncport); 9017 if (ddi_create_minor_node(dip, name, S_IFCHR, 9018 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 9019 DDI_SUCCESS) { 9020 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 9021 "cannot create SATA attachment point for port %d", 9022 ncport); 9023 } 9024 9025 /* Probe port */ 9026 start_time = ddi_get_lbolt(); 9027 reprobe_cport: 9028 sata_device.satadev_addr.cport = ncport; 9029 sata_device.satadev_addr.pmport = 0; 9030 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 9031 sata_device.satadev_rev = SATA_DEVICE_REV; 9032 9033 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9034 (dip, &sata_device); 9035 9036 mutex_enter(&cportinfo->cport_mutex); 9037 sata_update_port_scr(&cportinfo->cport_scr, &sata_device); 9038 if (rval != SATA_SUCCESS) { 9039 /* Something went wrong? Fail the port */ 9040 cportinfo->cport_state = SATA_PSTATE_FAILED; 9041 mutex_exit(&cportinfo->cport_mutex); 9042 continue; 9043 } 9044 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9045 cportinfo->cport_state |= SATA_STATE_PROBED; 9046 cportinfo->cport_dev_type = sata_device.satadev_type; 9047 9048 cportinfo->cport_state |= SATA_STATE_READY; 9049 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 9050 mutex_exit(&cportinfo->cport_mutex); 9051 continue; 9052 } 9053 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9054 /* 9055 * There is some device attached. 9056 * Allocate device info structure 9057 */ 9058 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 9059 mutex_exit(&cportinfo->cport_mutex); 9060 SATA_CPORTINFO_DRV_INFO(cportinfo) = 9061 kmem_zalloc(sizeof (sata_drive_info_t), 9062 KM_SLEEP); 9063 mutex_enter(&cportinfo->cport_mutex); 9064 } 9065 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 9066 drive->satadrv_addr = cportinfo->cport_addr; 9067 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 9068 drive->satadrv_type = cportinfo->cport_dev_type; 9069 drive->satadrv_state = SATA_STATE_UNKNOWN; 9070 9071 mutex_exit(&cportinfo->cport_mutex); 9072 if (sata_add_device(dip, sata_hba_inst, ncport, 0) != 9073 SATA_SUCCESS) { 9074 /* 9075 * Plugged device was not correctly identified. 9076 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 9077 */ 9078 cur_time = ddi_get_lbolt(); 9079 if ((cur_time - start_time) < 9080 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 9081 /* sleep for a while */ 9082 delay(drv_usectohz( 9083 SATA_DEV_IDENTIFY_RETRY_DELAY)); 9084 goto reprobe_cport; 9085 } 9086 } 9087 } else { 9088 mutex_exit(&cportinfo->cport_mutex); 9089 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 9090 pminfo = kmem_zalloc(sizeof (sata_pmult_info_t), 9091 KM_SLEEP); 9092 mutex_enter(&cportinfo->cport_mutex); 9093 ASSERT(pminfo != NULL); 9094 SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo; 9095 pminfo->pmult_addr.cport = cportinfo->cport_addr.cport; 9096 pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT; 9097 pminfo->pmult_addr.qual = SATA_ADDR_PMPORT; 9098 pminfo->pmult_num_dev_ports = 9099 sata_device.satadev_add_info; 9100 mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER, 9101 NULL); 9102 pminfo->pmult_state = SATA_STATE_PROBING; 9103 mutex_exit(&cportinfo->cport_mutex); 9104 9105 /* Probe Port Multiplier ports */ 9106 for (npmport = 0; 9107 npmport < pminfo->pmult_num_dev_ports; 9108 npmport++) { 9109 pmportinfo = kmem_zalloc( 9110 sizeof (sata_pmport_info_t), KM_SLEEP); 9111 mutex_enter(&cportinfo->cport_mutex); 9112 ASSERT(pmportinfo != NULL); 9113 pmportinfo->pmport_addr.cport = ncport; 9114 pmportinfo->pmport_addr.pmport = npmport; 9115 pmportinfo->pmport_addr.qual = 9116 SATA_ADDR_PMPORT; 9117 pminfo->pmult_dev_port[npmport] = pmportinfo; 9118 9119 mutex_init(&pmportinfo->pmport_mutex, NULL, 9120 MUTEX_DRIVER, NULL); 9121 9122 mutex_exit(&cportinfo->cport_mutex); 9123 9124 /* Create an attachment point */ 9125 minor_number = SATA_MAKE_AP_MINOR( 9126 ddi_get_instance(dip), ncport, npmport, 1); 9127 (void) sprintf(name, "%d.%d", ncport, npmport); 9128 if (ddi_create_minor_node(dip, name, S_IFCHR, 9129 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 9130 0) != DDI_SUCCESS) { 9131 sata_log(sata_hba_inst, CE_WARN, 9132 "sata_hba_attach: " 9133 "cannot create SATA attachment " 9134 "point for port %d pmult port %d", 9135 ncport, npmport); 9136 } 9137 9138 start_time = ddi_get_lbolt(); 9139 reprobe_pmport: 9140 sata_device.satadev_addr.pmport = npmport; 9141 sata_device.satadev_addr.qual = 9142 SATA_ADDR_PMPORT; 9143 9144 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9145 (dip, &sata_device); 9146 mutex_enter(&cportinfo->cport_mutex); 9147 9148 /* sata_update_port_info() */ 9149 sata_update_port_scr(&pmportinfo->pmport_scr, 9150 &sata_device); 9151 9152 if (rval != SATA_SUCCESS) { 9153 pmportinfo->pmport_state = 9154 SATA_PSTATE_FAILED; 9155 mutex_exit(&cportinfo->cport_mutex); 9156 continue; 9157 } 9158 pmportinfo->pmport_state &= 9159 ~SATA_STATE_PROBING; 9160 pmportinfo->pmport_state |= SATA_STATE_PROBED; 9161 pmportinfo->pmport_dev_type = 9162 sata_device.satadev_type; 9163 9164 pmportinfo->pmport_state |= SATA_STATE_READY; 9165 if (pmportinfo->pmport_dev_type == 9166 SATA_DTYPE_NONE) { 9167 mutex_exit(&cportinfo->cport_mutex); 9168 continue; 9169 } 9170 /* Port multipliers cannot be chained */ 9171 ASSERT(pmportinfo->pmport_dev_type != 9172 SATA_DTYPE_PMULT); 9173 /* 9174 * There is something attached to Port 9175 * Multiplier device port 9176 * Allocate device info structure 9177 */ 9178 if (pmportinfo->pmport_sata_drive == NULL) { 9179 mutex_exit(&cportinfo->cport_mutex); 9180 pmportinfo->pmport_sata_drive = 9181 kmem_zalloc( 9182 sizeof (sata_drive_info_t), 9183 KM_SLEEP); 9184 mutex_enter(&cportinfo->cport_mutex); 9185 } 9186 drive = pmportinfo->pmport_sata_drive; 9187 drive->satadrv_addr.cport = 9188 pmportinfo->pmport_addr.cport; 9189 drive->satadrv_addr.pmport = npmport; 9190 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 9191 drive->satadrv_type = pmportinfo-> 9192 pmport_dev_type; 9193 drive->satadrv_state = SATA_STATE_UNKNOWN; 9194 9195 mutex_exit(&cportinfo->cport_mutex); 9196 if (sata_add_device(dip, sata_hba_inst, ncport, 9197 npmport) != SATA_SUCCESS) { 9198 /* 9199 * Plugged device was not correctly 9200 * identified. Retry, within the 9201 * SATA_DEV_IDENTIFY_TIMEOUT 9202 */ 9203 cur_time = ddi_get_lbolt(); 9204 if ((cur_time - start_time) < 9205 drv_usectohz( 9206 SATA_DEV_IDENTIFY_TIMEOUT)) { 9207 /* sleep for a while */ 9208 delay(drv_usectohz( 9209 SATA_DEV_IDENTIFY_RETRY_DELAY)); 9210 goto reprobe_pmport; 9211 } 9212 } 9213 } 9214 pmportinfo->pmport_state = 9215 SATA_STATE_PROBED | SATA_STATE_READY; 9216 } 9217 } 9218 } 9219 9220 /* 9221 * Add SATA device for specified HBA instance & port (SCSI target 9222 * device nodes). 9223 * This function is called (indirectly) only from sata_hba_attach(). 9224 * A target node is created when there is a supported type device attached, 9225 * but may be removed if it cannot be put online. 9226 * 9227 * This function cannot be called from an interrupt context. 9228 * 9229 * ONLY DISK TARGET NODES ARE CREATED NOW 9230 * 9231 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 9232 * device identification failed - adding a device could be retried. 9233 * 9234 */ 9235 static int 9236 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport, 9237 int pmport) 9238 { 9239 sata_cport_info_t *cportinfo; 9240 sata_pmult_info_t *pminfo; 9241 sata_pmport_info_t *pmportinfo; 9242 dev_info_t *cdip; /* child dip */ 9243 sata_device_t sata_device; 9244 int rval; 9245 9246 9247 9248 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9249 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 9250 mutex_enter(&cportinfo->cport_mutex); 9251 /* 9252 * Some device is attached to a controller port. 9253 * We rely on controllers distinquishing between no-device, 9254 * attached port multiplier and other kind of attached device. 9255 * We need to get Identify Device data and determine 9256 * positively the dev type before trying to attach 9257 * the target driver. 9258 */ 9259 sata_device.satadev_rev = SATA_DEVICE_REV; 9260 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9261 /* 9262 * Not port multiplier. 9263 */ 9264 sata_device.satadev_addr = cportinfo->cport_addr; 9265 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 9266 mutex_exit(&cportinfo->cport_mutex); 9267 9268 rval = sata_probe_device(sata_hba_inst, &sata_device); 9269 if (rval != SATA_SUCCESS || 9270 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) 9271 return (SATA_FAILURE); 9272 9273 mutex_enter(&cportinfo->cport_mutex); 9274 sata_show_drive_info(sata_hba_inst, 9275 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9276 9277 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9278 /* 9279 * Could not determine device type or 9280 * a device is not supported. 9281 * Degrade this device to unknown. 9282 */ 9283 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9284 mutex_exit(&cportinfo->cport_mutex); 9285 return (SATA_SUCCESS); 9286 } 9287 cportinfo->cport_dev_type = sata_device.satadev_type; 9288 cportinfo->cport_tgtnode_clean = B_TRUE; 9289 mutex_exit(&cportinfo->cport_mutex); 9290 9291 /* 9292 * Initialize device to the desired state. Even if it 9293 * fails, the device will still attach but syslog 9294 * will show the warning. 9295 */ 9296 if (sata_initialize_device(sata_hba_inst, 9297 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) 9298 /* Retry */ 9299 (void) sata_initialize_device(sata_hba_inst, 9300 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9301 9302 cdip = sata_create_target_node(pdip, sata_hba_inst, 9303 &sata_device.satadev_addr); 9304 mutex_enter(&cportinfo->cport_mutex); 9305 if (cdip == NULL) { 9306 /* 9307 * Attaching target node failed. 9308 * We retain sata_drive_info structure... 9309 */ 9310 mutex_exit(&cportinfo->cport_mutex); 9311 return (SATA_SUCCESS); 9312 } 9313 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 9314 satadrv_state = SATA_STATE_READY; 9315 } else { 9316 /* This must be Port Multiplier type */ 9317 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9318 SATA_LOG_D((sata_hba_inst, CE_WARN, 9319 "sata_add_device: " 9320 "unrecognized dev type %x", 9321 cportinfo->cport_dev_type)); 9322 mutex_exit(&cportinfo->cport_mutex); 9323 return (SATA_SUCCESS); 9324 } 9325 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9326 pmportinfo = pminfo->pmult_dev_port[pmport]; 9327 sata_device.satadev_addr = pmportinfo->pmport_addr; 9328 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 9329 mutex_exit(&cportinfo->cport_mutex); 9330 9331 rval = sata_probe_device(sata_hba_inst, &sata_device); 9332 if (rval != SATA_SUCCESS || 9333 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) { 9334 return (SATA_FAILURE); 9335 } 9336 mutex_enter(&cportinfo->cport_mutex); 9337 sata_show_drive_info(sata_hba_inst, 9338 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9339 9340 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9341 /* 9342 * Could not determine device type. 9343 * Degrade this device to unknown. 9344 */ 9345 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 9346 mutex_exit(&cportinfo->cport_mutex); 9347 return (SATA_SUCCESS); 9348 } 9349 pmportinfo->pmport_dev_type = sata_device.satadev_type; 9350 pmportinfo->pmport_tgtnode_clean = B_TRUE; 9351 mutex_exit(&cportinfo->cport_mutex); 9352 9353 /* 9354 * Initialize device to the desired state. 9355 * Even if it fails, the device will still 9356 * attach but syslog will show the warning. 9357 */ 9358 if (sata_initialize_device(sata_hba_inst, 9359 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) 9360 /* Retry */ 9361 (void) sata_initialize_device(sata_hba_inst, 9362 pmportinfo->pmport_sata_drive); 9363 9364 cdip = sata_create_target_node(pdip, sata_hba_inst, 9365 &sata_device.satadev_addr); 9366 mutex_enter(&cportinfo->cport_mutex); 9367 if (cdip == NULL) { 9368 /* 9369 * Attaching target node failed. 9370 * We retain sata_drive_info structure... 9371 */ 9372 mutex_exit(&cportinfo->cport_mutex); 9373 return (SATA_SUCCESS); 9374 } 9375 pmportinfo->pmport_sata_drive->satadrv_state |= 9376 SATA_STATE_READY; 9377 } 9378 mutex_exit(&cportinfo->cport_mutex); 9379 return (SATA_SUCCESS); 9380 } 9381 9382 9383 9384 /* 9385 * Create scsi target node for attached device, create node properties and 9386 * attach the node. 9387 * The node could be removed if the device onlining fails. 9388 * 9389 * A dev_info_t pointer is returned if operation is successful, NULL is 9390 * returned otherwise. 9391 * 9392 * No port multiplier support. 9393 */ 9394 9395 static dev_info_t * 9396 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 9397 sata_address_t *sata_addr) 9398 { 9399 dev_info_t *cdip = NULL; 9400 int rval; 9401 char *nname = NULL; 9402 char **compatible = NULL; 9403 int ncompatible; 9404 struct scsi_inquiry inq; 9405 sata_device_t sata_device; 9406 sata_drive_info_t *sdinfo; 9407 int target; 9408 int i; 9409 9410 sata_device.satadev_rev = SATA_DEVICE_REV; 9411 sata_device.satadev_addr = *sata_addr; 9412 9413 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 9414 9415 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 9416 9417 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 9418 sata_addr->pmport, sata_addr->qual); 9419 9420 if (sdinfo == NULL) { 9421 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9422 sata_addr->cport))); 9423 SATA_LOG_D((sata_hba_inst, CE_WARN, 9424 "sata_create_target_node: no sdinfo for target %x", 9425 target)); 9426 return (NULL); 9427 } 9428 9429 /* 9430 * create or get scsi inquiry data, expected by 9431 * scsi_hba_nodename_compatible_get() 9432 * SATA hard disks get Identify Data translated into Inguiry Data. 9433 * ATAPI devices respond directly to Inquiry request. 9434 */ 9435 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9436 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 9437 (uint8_t *)&inq); 9438 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9439 sata_addr->cport))); 9440 } else { /* Assume supported ATAPI device */ 9441 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9442 sata_addr->cport))); 9443 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 9444 &inq) == SATA_FAILURE) 9445 return (NULL); 9446 /* 9447 * Save supported ATAPI transport version 9448 */ 9449 sdinfo->satadrv_atapi_trans_ver = 9450 SATA_ATAPI_TRANS_VERSION(&inq); 9451 } 9452 9453 /* determine the node name and compatible */ 9454 scsi_hba_nodename_compatible_get(&inq, NULL, 9455 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 9456 9457 #ifdef SATA_DEBUG 9458 if (sata_debug_flags & SATA_DBG_NODES) { 9459 if (nname == NULL) { 9460 cmn_err(CE_NOTE, "sata_create_target_node: " 9461 "cannot determine nodename for target %d\n", 9462 target); 9463 } else { 9464 cmn_err(CE_WARN, "sata_create_target_node: " 9465 "target %d nodename: %s\n", target, nname); 9466 } 9467 if (compatible == NULL) { 9468 cmn_err(CE_WARN, 9469 "sata_create_target_node: no compatible name\n"); 9470 } else { 9471 for (i = 0; i < ncompatible; i++) { 9472 cmn_err(CE_WARN, "sata_create_target_node: " 9473 "compatible name: %s\n", compatible[i]); 9474 } 9475 } 9476 } 9477 #endif 9478 9479 /* if nodename can't be determined, log error and exit */ 9480 if (nname == NULL) { 9481 SATA_LOG_D((sata_hba_inst, CE_WARN, 9482 "sata_create_target_node: cannot determine nodename " 9483 "for target %d\n", target)); 9484 scsi_hba_nodename_compatible_free(nname, compatible); 9485 return (NULL); 9486 } 9487 /* 9488 * Create scsi target node 9489 */ 9490 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 9491 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9492 "device-type", "scsi"); 9493 9494 if (rval != DDI_PROP_SUCCESS) { 9495 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9496 "updating device_type prop failed %d", rval)); 9497 goto fail; 9498 } 9499 9500 /* 9501 * Create target node properties: target & lun 9502 */ 9503 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 9504 if (rval != DDI_PROP_SUCCESS) { 9505 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9506 "updating target prop failed %d", rval)); 9507 goto fail; 9508 } 9509 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 9510 if (rval != DDI_PROP_SUCCESS) { 9511 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9512 "updating target prop failed %d", rval)); 9513 goto fail; 9514 } 9515 9516 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9517 /* 9518 * Add "variant" property 9519 */ 9520 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9521 "variant", "atapi"); 9522 if (rval != DDI_PROP_SUCCESS) { 9523 SATA_LOG_D((sata_hba_inst, CE_WARN, 9524 "sata_create_target_node: variant atapi " 9525 "property could not be created: %d", rval)); 9526 goto fail; 9527 } 9528 } 9529 /* decorate the node with compatible */ 9530 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 9531 compatible, ncompatible) != DDI_PROP_SUCCESS) { 9532 SATA_LOG_D((sata_hba_inst, CE_WARN, 9533 "sata_create_target_node: FAIL compatible props cdip 0x%p", 9534 (void *)cdip)); 9535 goto fail; 9536 } 9537 9538 9539 /* 9540 * Now, try to attach the driver. If probing of the device fails, 9541 * the target node may be removed 9542 */ 9543 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 9544 9545 scsi_hba_nodename_compatible_free(nname, compatible); 9546 9547 if (rval == NDI_SUCCESS) 9548 return (cdip); 9549 9550 /* target node was removed - are we sure? */ 9551 return (NULL); 9552 9553 fail: 9554 scsi_hba_nodename_compatible_free(nname, compatible); 9555 ddi_prop_remove_all(cdip); 9556 rval = ndi_devi_free(cdip); 9557 if (rval != NDI_SUCCESS) { 9558 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9559 "node removal failed %d", rval)); 9560 } 9561 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 9562 "cannot create target node for SATA device at port %d", 9563 sata_addr->cport); 9564 return (NULL); 9565 } 9566 9567 9568 9569 /* 9570 * Re-probe sata port, check for a device and attach info 9571 * structures when necessary. Identify Device data is fetched, if possible. 9572 * Assumption: sata address is already validated. 9573 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 9574 * the presence of a device and its type. 9575 * 9576 * flag arg specifies that the function should try multiple times to identify 9577 * device type and to initialize it, or it should return immediately on failure. 9578 * SATA_DEV_IDENTIFY_RETRY - retry 9579 * SATA_DEV_IDENTIFY_NORETRY - no retry 9580 * 9581 * SATA_FAILURE is returned if one of the operations failed. 9582 * 9583 * This function cannot be called in interrupt context - it may sleep. 9584 */ 9585 static int 9586 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 9587 int flag) 9588 { 9589 sata_cport_info_t *cportinfo; 9590 sata_drive_info_t *sdinfo; 9591 boolean_t init_device = B_FALSE; 9592 int prev_device_type = SATA_DTYPE_NONE; 9593 int prev_device_settings = 0; 9594 clock_t start_time; 9595 int retry = B_FALSE; 9596 int rval; 9597 9598 /* We only care about host sata cport for now */ 9599 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 9600 sata_device->satadev_addr.cport); 9601 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9602 if (sdinfo != NULL) { 9603 /* 9604 * We are re-probing port with a previously attached device. 9605 * Save previous device type and settings 9606 */ 9607 prev_device_type = cportinfo->cport_dev_type; 9608 prev_device_settings = sdinfo->satadrv_settings; 9609 } 9610 if (flag == SATA_DEV_IDENTIFY_RETRY) { 9611 start_time = ddi_get_lbolt(); 9612 retry = B_TRUE; 9613 } 9614 retry_probe: 9615 9616 /* probe port */ 9617 mutex_enter(&cportinfo->cport_mutex); 9618 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9619 cportinfo->cport_state |= SATA_STATE_PROBING; 9620 mutex_exit(&cportinfo->cport_mutex); 9621 9622 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9623 (SATA_DIP(sata_hba_inst), sata_device); 9624 9625 mutex_enter(&cportinfo->cport_mutex); 9626 if (rval != SATA_SUCCESS) { 9627 cportinfo->cport_state = SATA_PSTATE_FAILED; 9628 mutex_exit(&cportinfo->cport_mutex); 9629 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 9630 "SATA port %d probing failed", 9631 cportinfo->cport_addr.cport)); 9632 return (SATA_FAILURE); 9633 } 9634 9635 /* 9636 * update sata port state and set device type 9637 */ 9638 sata_update_port_info(sata_hba_inst, sata_device); 9639 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9640 9641 /* 9642 * Sanity check - Port is active? Is the link active? 9643 * Is there any device attached? 9644 */ 9645 if ((cportinfo->cport_state & 9646 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 9647 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 9648 SATA_PORT_DEVLINK_UP) { 9649 /* 9650 * Port in non-usable state or no link active/no device. 9651 * Free info structure if necessary (direct attached drive 9652 * only, for now! 9653 */ 9654 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9655 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 9656 /* Add here differentiation for device attached or not */ 9657 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 9658 mutex_exit(&cportinfo->cport_mutex); 9659 if (sdinfo != NULL) 9660 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9661 return (SATA_SUCCESS); 9662 } 9663 9664 cportinfo->cport_state |= SATA_STATE_READY; 9665 cportinfo->cport_dev_type = sata_device->satadev_type; 9666 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9667 9668 /* 9669 * If we are re-probing the port, there may be 9670 * sata_drive_info structure attached 9671 * (or sata_pm_info, if PMult is supported). 9672 */ 9673 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 9674 /* 9675 * There is no device, so remove device info structure, 9676 * if necessary. Direct attached drive only! 9677 */ 9678 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 9679 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 9680 if (sdinfo != NULL) { 9681 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9682 sata_log(sata_hba_inst, CE_WARN, 9683 "SATA device detached " 9684 "from port %d", cportinfo->cport_addr.cport); 9685 } 9686 mutex_exit(&cportinfo->cport_mutex); 9687 return (SATA_SUCCESS); 9688 } 9689 9690 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 9691 if (sdinfo == NULL) { 9692 /* 9693 * There is some device attached, but there is 9694 * no sata_drive_info structure - allocate one 9695 */ 9696 mutex_exit(&cportinfo->cport_mutex); 9697 sdinfo = kmem_zalloc( 9698 sizeof (sata_drive_info_t), KM_SLEEP); 9699 mutex_enter(&cportinfo->cport_mutex); 9700 /* 9701 * Recheck, that the port state did not change when we 9702 * released mutex. 9703 */ 9704 if (cportinfo->cport_state & SATA_STATE_READY) { 9705 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 9706 sdinfo->satadrv_addr = cportinfo->cport_addr; 9707 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 9708 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9709 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 9710 } else { 9711 /* 9712 * Port is not in ready state, we 9713 * cannot attach a device. 9714 */ 9715 mutex_exit(&cportinfo->cport_mutex); 9716 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9717 return (SATA_SUCCESS); 9718 } 9719 /* 9720 * Since we are adding device, presumably new one, 9721 * indicate that it should be initalized, 9722 * as well as some internal framework states). 9723 */ 9724 init_device = B_TRUE; 9725 } 9726 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9727 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 9728 } else { 9729 /* 9730 * The device is a port multiplier - not handled now. 9731 */ 9732 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9733 mutex_exit(&cportinfo->cport_mutex); 9734 return (SATA_SUCCESS); 9735 } 9736 mutex_exit(&cportinfo->cport_mutex); 9737 /* 9738 * Figure out what kind of device we are really 9739 * dealing with. 9740 */ 9741 rval = sata_probe_device(sata_hba_inst, sata_device); 9742 9743 if (rval == SATA_SUCCESS) { 9744 /* 9745 * If we are dealing with the same type of a device as before, 9746 * restore its settings flags. 9747 */ 9748 if (sata_device->satadev_type == prev_device_type) 9749 sdinfo->satadrv_settings = prev_device_settings; 9750 9751 /* Set initial device features, if necessary */ 9752 if (init_device == B_TRUE) { 9753 rval = sata_initialize_device(sata_hba_inst, sdinfo); 9754 } 9755 if (rval == SATA_SUCCESS) 9756 return (rval); 9757 } 9758 9759 if (retry) { 9760 clock_t cur_time = ddi_get_lbolt(); 9761 /* 9762 * A device was not successfully identified or initialized. 9763 * Track retry time for device identification. 9764 */ 9765 if ((cur_time - start_time) < 9766 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 9767 /* sleep for a while */ 9768 delay(drv_usectohz(SATA_DEV_IDENTIFY_RETRY_DELAY)); 9769 goto retry_probe; 9770 } 9771 } 9772 return (rval); 9773 } 9774 9775 /* 9776 * Initialize device 9777 * Specified device is initialized to a default state. 9778 * 9779 * Returns SATA_SUCCESS if all device features are set successfully, 9780 * SATA_FAILURE otherwise 9781 */ 9782 static int 9783 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 9784 sata_drive_info_t *sdinfo) 9785 { 9786 int rval; 9787 9788 sata_save_drive_settings(sdinfo); 9789 9790 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 9791 9792 sata_init_write_cache_mode(sdinfo); 9793 9794 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 9795 9796 /* Determine current data transfer mode */ 9797 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 9798 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 9799 } else if ((sdinfo->satadrv_id.ai_validinfo & 9800 SATA_VALIDINFO_88) != 0 && 9801 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 9802 sdinfo->satadrv_settings |= SATA_DEV_DMA; 9803 } else if ((sdinfo->satadrv_id.ai_dworddma & 9804 SATA_MDMA_SEL_MASK) != 0) { 9805 sdinfo->satadrv_settings |= SATA_DEV_DMA; 9806 } else 9807 /* DMA supported, not no DMA transfer mode is selected !? */ 9808 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 9809 9810 return (rval); 9811 } 9812 9813 9814 /* 9815 * Initialize write cache mode. 9816 * 9817 * The default write cache setting for SATA HDD is provided by sata_write_cache 9818 * static variable. ATAPI CD/DVDs devices have write cache default is 9819 * determined by sata_atapicdvd_write_cache static variable. 9820 * 1 - enable 9821 * 0 - disable 9822 * any other value - current drive setting 9823 * 9824 * Although there is not reason to disable write cache on CD/DVD devices, 9825 * the default setting control is provided for the maximun flexibility. 9826 * 9827 * In the future, it may be overridden by the 9828 * disk-write-cache-enable property setting, if it is defined. 9829 * Returns SATA_SUCCESS if all device features are set successfully, 9830 * SATA_FAILURE otherwise. 9831 */ 9832 static void 9833 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 9834 { 9835 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9836 if (sata_write_cache == 1) 9837 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9838 else if (sata_write_cache == 0) 9839 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9840 /* 9841 * When sata_write_cache value is not 0 or 1, 9842 * a current setting of the drive's write cache is used. 9843 */ 9844 } else { /* Assume ATAPI CD/DVD device */ 9845 if (sata_atapicdvd_write_cache == 1) 9846 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9847 else if (sata_atapicdvd_write_cache == 0) 9848 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9849 /* 9850 * When sata_write_cache value is not 0 or 1, 9851 * a current setting of the drive's write cache is used. 9852 */ 9853 } 9854 } 9855 9856 9857 /* 9858 * Validate sata address. 9859 * Specified cport, pmport and qualifier has to match 9860 * passed sata_scsi configuration info. 9861 * The presence of an attached device is not verified. 9862 * 9863 * Returns 0 when address is valid, -1 otherwise. 9864 */ 9865 static int 9866 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 9867 int pmport, int qual) 9868 { 9869 if (qual == SATA_ADDR_DCPORT && pmport != 0) 9870 goto invalid_address; 9871 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9872 goto invalid_address; 9873 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 9874 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 9875 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 9876 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 9877 goto invalid_address; 9878 9879 return (0); 9880 9881 invalid_address: 9882 return (-1); 9883 9884 } 9885 9886 /* 9887 * Validate scsi address 9888 * SCSI target address is translated into SATA cport/pmport and compared 9889 * with a controller port/device configuration. LUN has to be 0. 9890 * Returns 0 if a scsi target refers to an attached device, 9891 * returns 1 if address is valid but device is not attached, 9892 * returns -1 if bad address or device is of an unsupported type. 9893 * Upon return sata_device argument is set. 9894 */ 9895 static int 9896 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 9897 struct scsi_address *ap, sata_device_t *sata_device) 9898 { 9899 int cport, pmport, qual, rval; 9900 9901 rval = -1; /* Invalid address */ 9902 if (ap->a_lun != 0) 9903 goto out; 9904 9905 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 9906 cport = SCSI_TO_SATA_CPORT(ap->a_target); 9907 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 9908 9909 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 9910 goto out; 9911 9912 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 9913 0) { 9914 9915 sata_cport_info_t *cportinfo; 9916 sata_pmult_info_t *pmultinfo; 9917 sata_drive_info_t *sdinfo = NULL; 9918 9919 rval = 1; /* Valid sata address */ 9920 9921 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9922 if (qual == SATA_ADDR_DCPORT) { 9923 if (cportinfo == NULL || 9924 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 9925 goto out; 9926 9927 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT || 9928 (cportinfo->cport_dev_type & 9929 SATA_VALID_DEV_TYPE) == 0) { 9930 rval = -1; 9931 goto out; 9932 } 9933 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9934 9935 } else if (qual == SATA_ADDR_DPMPORT) { 9936 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9937 if (pmultinfo == NULL) { 9938 rval = -1; 9939 goto out; 9940 } 9941 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 9942 NULL || 9943 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 9944 pmport) == SATA_DTYPE_NONE) 9945 goto out; 9946 9947 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 9948 pmport); 9949 } else { 9950 rval = -1; 9951 goto out; 9952 } 9953 if ((sdinfo == NULL) || 9954 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 9955 goto out; 9956 9957 sata_device->satadev_type = sdinfo->satadrv_type; 9958 sata_device->satadev_addr.qual = qual; 9959 sata_device->satadev_addr.cport = cport; 9960 sata_device->satadev_addr.pmport = pmport; 9961 sata_device->satadev_rev = SATA_DEVICE_REV_1; 9962 return (0); 9963 } 9964 out: 9965 if (rval == 1) { 9966 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 9967 "sata_validate_scsi_address: no valid target %x lun %x", 9968 ap->a_target, ap->a_lun); 9969 } 9970 return (rval); 9971 } 9972 9973 /* 9974 * Find dip corresponding to passed device number 9975 * 9976 * Returns NULL if invalid device number is passed or device cannot be found, 9977 * Returns dip is device is found. 9978 */ 9979 static dev_info_t * 9980 sata_devt_to_devinfo(dev_t dev) 9981 { 9982 dev_info_t *dip; 9983 #ifndef __lock_lint 9984 struct devnames *dnp; 9985 major_t major = getmajor(dev); 9986 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 9987 9988 if (major >= devcnt) 9989 return (NULL); 9990 9991 dnp = &devnamesp[major]; 9992 LOCK_DEV_OPS(&(dnp->dn_lock)); 9993 dip = dnp->dn_head; 9994 while (dip && (ddi_get_instance(dip) != instance)) { 9995 dip = ddi_get_next(dip); 9996 } 9997 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 9998 #endif 9999 10000 return (dip); 10001 } 10002 10003 10004 /* 10005 * Probe device. 10006 * This function issues Identify Device command and initializes local 10007 * sata_drive_info structure if the device can be identified. 10008 * The device type is determined by examining Identify Device 10009 * command response. 10010 * If the sata_hba_inst has linked drive info structure for this 10011 * device address, the Identify Device data is stored into sata_drive_info 10012 * structure linked to the port info structure. 10013 * 10014 * sata_device has to refer to the valid sata port(s) for HBA described 10015 * by sata_hba_inst structure. 10016 * 10017 * Returns: 10018 * SATA_SUCCESS if device type was successfully probed and port-linked 10019 * drive info structure was updated; 10020 * SATA_FAILURE if there is no device, or device was not probed 10021 * successully; 10022 * SATA_RETRY if device probe can be retried later. 10023 * If a device cannot be identified, sata_device's dev_state and dev_type 10024 * fields are set to unknown. 10025 * There are no retries in this function. Any retries should be managed by 10026 * the caller. 10027 */ 10028 10029 10030 static int 10031 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 10032 { 10033 sata_drive_info_t *sdinfo; 10034 sata_drive_info_t new_sdinfo; /* local drive info struct */ 10035 int rval; 10036 10037 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 10038 sata_device->satadev_addr.cport) & 10039 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 10040 10041 sata_device->satadev_type = SATA_DTYPE_NONE; 10042 10043 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 10044 sata_device->satadev_addr.cport))); 10045 10046 /* Get pointer to port-linked sata device info structure */ 10047 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 10048 if (sdinfo != NULL) { 10049 sdinfo->satadrv_state &= 10050 ~(SATA_STATE_PROBED | SATA_STATE_READY); 10051 sdinfo->satadrv_state |= SATA_STATE_PROBING; 10052 } else { 10053 /* No device to probe */ 10054 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10055 sata_device->satadev_addr.cport))); 10056 sata_device->satadev_type = SATA_DTYPE_NONE; 10057 sata_device->satadev_state = SATA_STATE_UNKNOWN; 10058 return (SATA_FAILURE); 10059 } 10060 /* 10061 * Need to issue both types of identify device command and 10062 * determine device type by examining retreived data/status. 10063 * First, ATA Identify Device. 10064 */ 10065 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 10066 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 10067 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10068 sata_device->satadev_addr.cport))); 10069 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 10070 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 10071 if (rval == SATA_RETRY) { 10072 /* We may try to check for ATAPI device */ 10073 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 10074 /* 10075 * HBA supports ATAPI - try to issue Identify Packet 10076 * Device command. 10077 */ 10078 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD; 10079 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 10080 } 10081 } 10082 if (rval == SATA_SUCCESS) { 10083 /* 10084 * Got something responding positively to ATA Identify Device 10085 * or to Identify Packet Device cmd. 10086 * Save last used device type. 10087 */ 10088 sata_device->satadev_type = new_sdinfo.satadrv_type; 10089 10090 /* save device info, if possible */ 10091 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 10092 sata_device->satadev_addr.cport))); 10093 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 10094 if (sdinfo == NULL) { 10095 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10096 sata_device->satadev_addr.cport))); 10097 return (SATA_FAILURE); 10098 } 10099 /* 10100 * Copy drive info into the port-linked drive info structure. 10101 */ 10102 *sdinfo = new_sdinfo; 10103 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 10104 sdinfo->satadrv_state |= SATA_STATE_PROBED; 10105 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 10106 SATA_CPORT_DEV_TYPE(sata_hba_inst, 10107 sata_device->satadev_addr.cport) = 10108 sdinfo->satadrv_type; 10109 else /* SATA_ADDR_DPMPORT */ 10110 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 10111 sata_device->satadev_addr.cport, 10112 sata_device->satadev_addr.pmport) = 10113 sdinfo->satadrv_type; 10114 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10115 sata_device->satadev_addr.cport))); 10116 return (SATA_SUCCESS); 10117 } 10118 10119 /* 10120 * It may be SATA_RETRY or SATA_FAILURE return. 10121 * Looks like we cannot determine the device type at this time. 10122 */ 10123 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 10124 sata_device->satadev_addr.cport))); 10125 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 10126 if (sdinfo != NULL) { 10127 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 10128 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10129 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 10130 sdinfo->satadrv_state = SATA_STATE_PROBED; 10131 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 10132 SATA_CPORT_DEV_TYPE(sata_hba_inst, 10133 sata_device->satadev_addr.cport) = 10134 SATA_DTYPE_UNKNOWN; 10135 else { 10136 /* SATA_ADDR_DPMPORT */ 10137 if ((SATA_PMULT_INFO(sata_hba_inst, 10138 sata_device->satadev_addr.cport) != NULL) && 10139 (SATA_PMPORT_INFO(sata_hba_inst, 10140 sata_device->satadev_addr.cport, 10141 sata_device->satadev_addr.pmport) != NULL)) 10142 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 10143 sata_device->satadev_addr.cport, 10144 sata_device->satadev_addr.pmport) = 10145 SATA_DTYPE_UNKNOWN; 10146 } 10147 } 10148 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10149 sata_device->satadev_addr.cport))); 10150 return (rval); 10151 } 10152 10153 10154 /* 10155 * Get pointer to sata_drive_info structure. 10156 * 10157 * The sata_device has to contain address (cport, pmport and qualifier) for 10158 * specified sata_scsi structure. 10159 * 10160 * Returns NULL if device address is not valid for this HBA configuration. 10161 * Otherwise, returns a pointer to sata_drive_info structure. 10162 * 10163 * This function should be called with a port mutex held. 10164 */ 10165 static sata_drive_info_t * 10166 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 10167 sata_device_t *sata_device) 10168 { 10169 uint8_t cport = sata_device->satadev_addr.cport; 10170 uint8_t pmport = sata_device->satadev_addr.pmport; 10171 uint8_t qual = sata_device->satadev_addr.qual; 10172 10173 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 10174 return (NULL); 10175 10176 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 10177 (SATA_STATE_PROBED | SATA_STATE_READY))) 10178 /* Port not probed yet */ 10179 return (NULL); 10180 10181 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 10182 return (NULL); 10183 10184 if (qual == SATA_ADDR_DCPORT) { 10185 /* Request for a device on a controller port */ 10186 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 10187 SATA_DTYPE_PMULT) 10188 /* Port multiplier attached */ 10189 return (NULL); 10190 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 10191 } 10192 if (qual == SATA_ADDR_DPMPORT) { 10193 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 10194 SATA_DTYPE_PMULT) 10195 return (NULL); 10196 10197 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 10198 return (NULL); 10199 10200 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 10201 } 10202 10203 /* we should not get here */ 10204 return (NULL); 10205 } 10206 10207 10208 /* 10209 * sata_identify_device. 10210 * Send Identify Device command to SATA HBA driver. 10211 * If command executes successfully, update sata_drive_info structure pointed 10212 * to by sdinfo argument, including Identify Device data. 10213 * If command fails, invalidate data in sata_drive_info. 10214 * 10215 * Cannot be called from interrupt level. 10216 * 10217 * Returns: 10218 * SATA_SUCCESS if the device was identified as a supported device, 10219 * SATA_RETRY if the device was not identified but could be retried, 10220 * SATA_FAILURE if the device was not identified and identify attempt 10221 * should not be retried. 10222 */ 10223 static int 10224 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 10225 sata_drive_info_t *sdinfo) 10226 { 10227 uint16_t cfg_word; 10228 int rval; 10229 10230 /* fetch device identify data */ 10231 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 10232 sdinfo)) != 0) 10233 goto fail_unknown; 10234 10235 cfg_word = sdinfo->satadrv_id.ai_config; 10236 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK && 10237 (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) { 10238 /* Change device type to reflect Identify Device data */ 10239 if (((cfg_word & SATA_ATAPI_TYPE_MASK) == 10240 SATA_ATAPI_TYPE) && 10241 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) == 10242 SATA_ATAPI_CDROM_DEV)) { 10243 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 10244 } else { 10245 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10246 } 10247 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD && 10248 (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) || 10249 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) { 10250 /* Change device type to reflect Identify Device data ! */ 10251 if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) == 10252 SATA_ATA_TYPE) { 10253 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 10254 } else { 10255 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10256 } 10257 } 10258 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10259 if (sdinfo->satadrv_capacity == 0) { 10260 /* Non-LBA disk. Too bad... */ 10261 sata_log(sata_hba_inst, CE_WARN, 10262 "SATA disk device at port %d does not support LBA", 10263 sdinfo->satadrv_addr.cport); 10264 rval = SATA_FAILURE; 10265 goto fail_unknown; 10266 } 10267 } 10268 #if 0 10269 /* Left for historical reason */ 10270 /* 10271 * Some initial version of SATA spec indicated that at least 10272 * UDMA mode 4 has to be supported. It is not metioned in 10273 * SerialATA 2.6, so this restriction is removed. 10274 */ 10275 /* Check for Ultra DMA modes 6 through 0 being supported */ 10276 for (i = 6; i >= 0; --i) { 10277 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 10278 break; 10279 } 10280 10281 /* 10282 * At least UDMA 4 mode has to be supported. If mode 4 or 10283 * higher are not supported by the device, fail this 10284 * device. 10285 */ 10286 if (i < 4) { 10287 /* No required Ultra DMA mode supported */ 10288 sata_log(sata_hba_inst, CE_WARN, 10289 "SATA disk device at port %d does not support UDMA " 10290 "mode 4 or higher", sdinfo->satadrv_addr.cport); 10291 SATA_LOG_D((sata_hba_inst, CE_WARN, 10292 "mode 4 or higher required, %d supported", i)); 10293 rval = SATA_FAILURE; 10294 goto fail_unknown; 10295 } 10296 #endif 10297 10298 return (SATA_SUCCESS); 10299 10300 fail_unknown: 10301 /* Invalidate sata_drive_info ? */ 10302 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10303 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10304 return (rval); 10305 } 10306 10307 /* 10308 * Log/display device information 10309 */ 10310 static void 10311 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 10312 sata_drive_info_t *sdinfo) 10313 { 10314 int valid_version; 10315 char msg_buf[MAXPATHLEN]; 10316 int i; 10317 10318 /* Show HBA path */ 10319 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 10320 10321 cmn_err(CE_CONT, "?%s :\n", msg_buf); 10322 10323 if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) { 10324 (void) sprintf(msg_buf, 10325 "Unsupported SATA device type (cfg 0x%x) at ", 10326 sdinfo->satadrv_id.ai_config); 10327 } else { 10328 (void) sprintf(msg_buf, "SATA %s device at", 10329 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 10330 "disk":"CD/DVD (ATAPI)"); 10331 } 10332 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 10333 cmn_err(CE_CONT, "?\t%s port %d\n", 10334 msg_buf, sdinfo->satadrv_addr.cport); 10335 else 10336 cmn_err(CE_CONT, "?\t%s port %d pmport %d\n", 10337 msg_buf, sdinfo->satadrv_addr.cport, 10338 sdinfo->satadrv_addr.pmport); 10339 10340 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 10341 sizeof (sdinfo->satadrv_id.ai_model)); 10342 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 10343 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 10344 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 10345 10346 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 10347 sizeof (sdinfo->satadrv_id.ai_fw)); 10348 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 10349 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 10350 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 10351 10352 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 10353 sizeof (sdinfo->satadrv_id.ai_drvser)); 10354 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 10355 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 10356 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10357 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 10358 } else { 10359 /* Assuming ATAPI CD/DVD */ 10360 /* 10361 * SOme drives do not implement serial number and may 10362 * violate the spec by provinding spaces rather than zeros 10363 * in serial number field. Scan the buffer to detect it. 10364 */ 10365 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 10366 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 10367 break; 10368 } 10369 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 10370 cmn_err(CE_CONT, "?\tserial number - none\n"); 10371 } else { 10372 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 10373 } 10374 } 10375 10376 #ifdef SATA_DEBUG 10377 if (sdinfo->satadrv_id.ai_majorversion != 0 && 10378 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 10379 int i; 10380 for (i = 14; i >= 2; i--) { 10381 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 10382 valid_version = i; 10383 break; 10384 } 10385 } 10386 cmn_err(CE_CONT, 10387 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 10388 valid_version, 10389 sdinfo->satadrv_id.ai_majorversion, 10390 sdinfo->satadrv_id.ai_minorversion); 10391 } 10392 #endif 10393 /* Log some info */ 10394 cmn_err(CE_CONT, "?\tsupported features:\n"); 10395 msg_buf[0] = '\0'; 10396 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10397 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 10398 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 10399 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 10400 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 10401 } 10402 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 10403 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 10404 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 10405 (void) strlcat(msg_buf, ", Native Command Queueing", 10406 MAXPATHLEN); 10407 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 10408 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 10409 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 10410 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 10411 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 10412 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 10413 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 10414 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 10415 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 10416 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 10417 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 10418 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 10419 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 10420 if (sdinfo->satadrv_features_support & 10421 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 10422 msg_buf[0] = '\0'; 10423 (void) snprintf(msg_buf, MAXPATHLEN, 10424 "Supported queue depth %d", 10425 sdinfo->satadrv_queue_depth); 10426 if (!(sata_func_enable & 10427 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 10428 (void) strlcat(msg_buf, 10429 " - queueing disabled globally", MAXPATHLEN); 10430 else if (sdinfo->satadrv_queue_depth > 10431 sdinfo->satadrv_max_queue_depth) { 10432 (void) snprintf(&msg_buf[strlen(msg_buf)], 10433 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 10434 (int)sdinfo->satadrv_max_queue_depth); 10435 } 10436 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 10437 } 10438 10439 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10440 #ifdef __i386 10441 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 10442 sdinfo->satadrv_capacity); 10443 #else 10444 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 10445 sdinfo->satadrv_capacity); 10446 #endif 10447 cmn_err(CE_CONT, "?%s", msg_buf); 10448 } 10449 } 10450 10451 10452 /* 10453 * sata_save_drive_settings extracts current setting of the device and stores 10454 * it for future reference, in case the device setup would need to be restored 10455 * after the device reset. 10456 * 10457 * For all devices read ahead and write cache settings are saved, if the 10458 * device supports these features at all. 10459 * For ATAPI devices the Removable Media Status Notification setting is saved. 10460 */ 10461 static void 10462 sata_save_drive_settings(sata_drive_info_t *sdinfo) 10463 { 10464 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) || 10465 (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 10466 10467 /* Current setting of Read Ahead (and Read Cache) */ 10468 if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD) 10469 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 10470 else 10471 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 10472 10473 /* Current setting of Write Cache */ 10474 if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE) 10475 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10476 else 10477 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10478 } 10479 10480 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 10481 if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC) 10482 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 10483 else 10484 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 10485 } 10486 } 10487 10488 10489 /* 10490 * sata_check_capacity function determines a disk capacity 10491 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 10492 * 10493 * NOTE: CHS mode is not supported! If a device does not support LBA, 10494 * this function is not called. 10495 * 10496 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 10497 */ 10498 static uint64_t 10499 sata_check_capacity(sata_drive_info_t *sdinfo) 10500 { 10501 uint64_t capacity = 0; 10502 int i; 10503 10504 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 10505 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 10506 /* Capacity valid only for LBA-addressable disk devices */ 10507 return (0); 10508 10509 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 10510 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 10511 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 10512 /* LBA48 mode supported and enabled */ 10513 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 10514 SATA_DEV_F_LBA28; 10515 for (i = 3; i >= 0; --i) { 10516 capacity <<= 16; 10517 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 10518 } 10519 } else { 10520 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 10521 capacity <<= 16; 10522 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 10523 if (capacity >= 0x1000000) 10524 /* LBA28 mode */ 10525 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 10526 } 10527 return (capacity); 10528 } 10529 10530 10531 /* 10532 * Allocate consistent buffer for DMA transfer 10533 * 10534 * Cannot be called from interrupt level or with mutex held - it may sleep. 10535 * 10536 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 10537 */ 10538 static struct buf * 10539 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 10540 { 10541 struct scsi_address ap; 10542 struct buf *bp; 10543 ddi_dma_attr_t cur_dma_attr; 10544 10545 ASSERT(spx->txlt_sata_pkt != NULL); 10546 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 10547 ap.a_target = SATA_TO_SCSI_TARGET( 10548 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 10549 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 10550 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 10551 ap.a_lun = 0; 10552 10553 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 10554 B_READ, SLEEP_FUNC, NULL); 10555 10556 if (bp != NULL) { 10557 /* Allocate DMA resources for this buffer */ 10558 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 10559 /* 10560 * We use a local version of the dma_attr, to account 10561 * for a device addressing limitations. 10562 * sata_adjust_dma_attr() will handle sdinfo == NULL which 10563 * will cause dma attributes to be adjusted to a lowest 10564 * acceptable level. 10565 */ 10566 sata_adjust_dma_attr(NULL, 10567 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 10568 10569 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 10570 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 10571 scsi_free_consistent_buf(bp); 10572 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 10573 bp = NULL; 10574 } 10575 } 10576 return (bp); 10577 } 10578 10579 /* 10580 * Release local buffer (consistent buffer for DMA transfer) allocated 10581 * via sata_alloc_local_buffer(). 10582 */ 10583 static void 10584 sata_free_local_buffer(sata_pkt_txlate_t *spx) 10585 { 10586 ASSERT(spx->txlt_sata_pkt != NULL); 10587 ASSERT(spx->txlt_dma_cookie_list != NULL); 10588 ASSERT(spx->txlt_dma_cookie_list_len != 0); 10589 ASSERT(spx->txlt_buf_dma_handle != NULL); 10590 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 10591 10592 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 10593 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 10594 10595 /* Free DMA resources */ 10596 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 10597 ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10598 spx->txlt_buf_dma_handle = 0; 10599 10600 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 10601 kmem_free(spx->txlt_dma_cookie_list, 10602 spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t)); 10603 spx->txlt_dma_cookie_list = NULL; 10604 spx->txlt_dma_cookie_list_len = 0; 10605 } 10606 /* Free buffer */ 10607 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 10608 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 10609 } 10610 10611 10612 10613 10614 /* 10615 * Allocate sata_pkt 10616 * Pkt structure version and embedded strcutures version are initialized. 10617 * sata_pkt and sata_pkt_txlate structures are cross-linked. 10618 * 10619 * Since this may be called in interrupt context by sata_scsi_init_pkt, 10620 * callback argument determines if it can sleep or not. 10621 * Hence, it should not be called from interrupt context. 10622 * 10623 * If successful, non-NULL pointer to a sata pkt is returned. 10624 * Upon failure, NULL pointer is returned. 10625 */ 10626 static sata_pkt_t * 10627 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 10628 { 10629 sata_pkt_t *spkt; 10630 int kmsflag; 10631 10632 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 10633 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 10634 if (spkt == NULL) { 10635 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10636 "sata_pkt_alloc: failed")); 10637 return (NULL); 10638 } 10639 spkt->satapkt_rev = SATA_PKT_REV; 10640 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 10641 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 10642 spkt->satapkt_framework_private = spx; 10643 spx->txlt_sata_pkt = spkt; 10644 return (spkt); 10645 } 10646 10647 /* 10648 * Free sata pkt allocated via sata_pkt_alloc() 10649 */ 10650 static void 10651 sata_pkt_free(sata_pkt_txlate_t *spx) 10652 { 10653 ASSERT(spx->txlt_sata_pkt != NULL); 10654 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 10655 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 10656 spx->txlt_sata_pkt = NULL; 10657 } 10658 10659 10660 /* 10661 * Adjust DMA attributes. 10662 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 10663 * from 8 bits to 16 bits, depending on a command being used. 10664 * Limiting max block count arbitrarily to 256 for all read/write 10665 * commands may affects performance, so check both the device and 10666 * controller capability before adjusting dma attributes. 10667 */ 10668 void 10669 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 10670 ddi_dma_attr_t *adj_dma_attr) 10671 { 10672 uint32_t count_max; 10673 10674 /* Copy original attributes */ 10675 *adj_dma_attr = *dma_attr; 10676 /* 10677 * Things to consider: device addressing capability, 10678 * "excessive" controller DMA capabilities. 10679 * If a device is being probed/initialized, there are 10680 * no device info - use default limits then. 10681 */ 10682 if (sdinfo == NULL) { 10683 count_max = dma_attr->dma_attr_granular * 0x100; 10684 if (dma_attr->dma_attr_count_max > count_max) 10685 adj_dma_attr->dma_attr_count_max = count_max; 10686 if (dma_attr->dma_attr_maxxfer > count_max) 10687 adj_dma_attr->dma_attr_maxxfer = count_max; 10688 return; 10689 } 10690 10691 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10692 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 10693 /* 10694 * 16-bit sector count may be used - we rely on 10695 * the assumption that only read and write cmds 10696 * will request more than 256 sectors worth of data 10697 */ 10698 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 10699 } else { 10700 /* 10701 * 8-bit sector count will be used - default limits 10702 * for dma attributes 10703 */ 10704 count_max = adj_dma_attr->dma_attr_granular * 0x100; 10705 } 10706 /* 10707 * Adjust controler dma attributes, if necessary 10708 */ 10709 if (dma_attr->dma_attr_count_max > count_max) 10710 adj_dma_attr->dma_attr_count_max = count_max; 10711 if (dma_attr->dma_attr_maxxfer > count_max) 10712 adj_dma_attr->dma_attr_maxxfer = count_max; 10713 } 10714 } 10715 10716 10717 /* 10718 * Allocate DMA resources for the buffer 10719 * This function handles initial DMA resource allocation as well as 10720 * DMA window shift and may be called repeatedly for the same DMA window 10721 * until all DMA cookies in the DMA window are processed. 10722 * To guarantee that there is always a coherent set of cookies to process 10723 * by SATA HBA driver (observing alignment, device granularity, etc.), 10724 * the number of slots for DMA cookies is equal to lesser of a number of 10725 * cookies in a DMA window and a max number of scatter/gather entries. 10726 * 10727 * Returns DDI_SUCCESS upon successful operation. 10728 * Return failure code of a failing command or DDI_FAILURE when 10729 * internal cleanup failed. 10730 */ 10731 static int 10732 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 10733 int (*callback)(caddr_t), caddr_t arg, 10734 ddi_dma_attr_t *cur_dma_attr) 10735 { 10736 int rval; 10737 off_t offset; 10738 size_t size; 10739 int max_sg_len, req_len, i; 10740 uint_t dma_flags; 10741 struct buf *bp; 10742 uint64_t cur_txfer_len; 10743 10744 10745 ASSERT(spx->txlt_sata_pkt != NULL); 10746 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 10747 ASSERT(bp != NULL); 10748 10749 10750 if (spx->txlt_buf_dma_handle == NULL) { 10751 /* 10752 * No DMA resources allocated so far - this is a first call 10753 * for this sata pkt. 10754 */ 10755 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 10756 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 10757 10758 if (rval != DDI_SUCCESS) { 10759 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10760 "sata_dma_buf_setup: no buf DMA resources %x", 10761 rval)); 10762 return (rval); 10763 } 10764 10765 if (bp->b_flags & B_READ) 10766 dma_flags = DDI_DMA_READ; 10767 else 10768 dma_flags = DDI_DMA_WRITE; 10769 10770 if (flags & PKT_CONSISTENT) 10771 dma_flags |= DDI_DMA_CONSISTENT; 10772 10773 if (flags & PKT_DMA_PARTIAL) 10774 dma_flags |= DDI_DMA_PARTIAL; 10775 10776 /* 10777 * Check buffer alignment and size against dma attributes 10778 * Consider dma_attr_align only. There may be requests 10779 * with the size lower than device granularity, but they 10780 * will not read/write from/to the device, so no adjustment 10781 * is necessary. The dma_attr_minxfer theoretically should 10782 * be considered, but no HBA driver is checking it. 10783 */ 10784 if (IS_P2ALIGNED(bp->b_un.b_addr, 10785 cur_dma_attr->dma_attr_align)) { 10786 rval = ddi_dma_buf_bind_handle( 10787 spx->txlt_buf_dma_handle, 10788 bp, dma_flags, callback, arg, 10789 &spx->txlt_dma_cookie, 10790 &spx->txlt_curwin_num_dma_cookies); 10791 } else { /* Buffer is not aligned */ 10792 10793 int (*ddicallback)(caddr_t); 10794 size_t bufsz; 10795 10796 /* Check id sleeping is allowed */ 10797 ddicallback = (callback == NULL_FUNC) ? 10798 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 10799 10800 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10801 "mis-aligned buffer: addr=0x%p, cnt=%lu", 10802 (void *)bp->b_un.b_addr, bp->b_bcount); 10803 10804 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 10805 /* 10806 * CPU will need to access data in the buffer 10807 * (for copying) so map it. 10808 */ 10809 bp_mapin(bp); 10810 10811 ASSERT(spx->txlt_tmp_buf == NULL); 10812 10813 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 10814 rval = ddi_dma_mem_alloc( 10815 spx->txlt_buf_dma_handle, 10816 bp->b_bcount, 10817 &sata_acc_attr, 10818 DDI_DMA_STREAMING, 10819 ddicallback, NULL, 10820 &spx->txlt_tmp_buf, 10821 &bufsz, 10822 &spx->txlt_tmp_buf_handle); 10823 10824 if (rval != DDI_SUCCESS) { 10825 /* DMA mapping failed */ 10826 (void) ddi_dma_free_handle( 10827 &spx->txlt_buf_dma_handle); 10828 spx->txlt_buf_dma_handle = NULL; 10829 #ifdef SATA_DEBUG 10830 mbuffail_count++; 10831 #endif 10832 SATADBG1(SATA_DBG_DMA_SETUP, 10833 spx->txlt_sata_hba_inst, 10834 "sata_dma_buf_setup: " 10835 "buf dma mem alloc failed %x\n", rval); 10836 return (rval); 10837 } 10838 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 10839 cur_dma_attr->dma_attr_align)); 10840 10841 #ifdef SATA_DEBUG 10842 mbuf_count++; 10843 10844 if (bp->b_bcount != bufsz) 10845 /* 10846 * This will require special handling, because 10847 * DMA cookies will be based on the temporary 10848 * buffer size, not the original buffer 10849 * b_bcount, so the residue may have to 10850 * be counted differently. 10851 */ 10852 SATADBG2(SATA_DBG_DMA_SETUP, 10853 spx->txlt_sata_hba_inst, 10854 "sata_dma_buf_setup: bp size %x != " 10855 "bufsz %x\n", bp->b_bcount, bufsz); 10856 #endif 10857 if (dma_flags & DDI_DMA_WRITE) { 10858 /* 10859 * Write operation - copy data into 10860 * an aligned temporary buffer. Buffer will be 10861 * synced for device by ddi_dma_addr_bind_handle 10862 */ 10863 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 10864 bp->b_bcount); 10865 } 10866 10867 rval = ddi_dma_addr_bind_handle( 10868 spx->txlt_buf_dma_handle, 10869 NULL, 10870 spx->txlt_tmp_buf, 10871 bufsz, dma_flags, ddicallback, 0, 10872 &spx->txlt_dma_cookie, 10873 &spx->txlt_curwin_num_dma_cookies); 10874 } 10875 10876 switch (rval) { 10877 case DDI_DMA_PARTIAL_MAP: 10878 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10879 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 10880 /* 10881 * Partial DMA mapping. 10882 * Retrieve number of DMA windows for this request. 10883 */ 10884 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 10885 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 10886 if (spx->txlt_tmp_buf != NULL) { 10887 ddi_dma_mem_free( 10888 &spx->txlt_tmp_buf_handle); 10889 spx->txlt_tmp_buf = NULL; 10890 } 10891 (void) ddi_dma_unbind_handle( 10892 spx->txlt_buf_dma_handle); 10893 (void) ddi_dma_free_handle( 10894 &spx->txlt_buf_dma_handle); 10895 spx->txlt_buf_dma_handle = NULL; 10896 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10897 "sata_dma_buf_setup: numwin failed\n")); 10898 return (DDI_FAILURE); 10899 } 10900 SATADBG2(SATA_DBG_DMA_SETUP, 10901 spx->txlt_sata_hba_inst, 10902 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 10903 spx->txlt_num_dma_win, 10904 spx->txlt_curwin_num_dma_cookies); 10905 spx->txlt_cur_dma_win = 0; 10906 break; 10907 10908 case DDI_DMA_MAPPED: 10909 /* DMA fully mapped */ 10910 spx->txlt_num_dma_win = 1; 10911 spx->txlt_cur_dma_win = 0; 10912 SATADBG1(SATA_DBG_DMA_SETUP, 10913 spx->txlt_sata_hba_inst, 10914 "sata_dma_buf_setup: windows: 1 " 10915 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 10916 break; 10917 10918 default: 10919 /* DMA mapping failed */ 10920 if (spx->txlt_tmp_buf != NULL) { 10921 ddi_dma_mem_free( 10922 &spx->txlt_tmp_buf_handle); 10923 spx->txlt_tmp_buf = NULL; 10924 } 10925 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10926 spx->txlt_buf_dma_handle = NULL; 10927 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10928 "sata_dma_buf_setup: buf dma handle binding " 10929 "failed %x\n", rval)); 10930 return (rval); 10931 } 10932 spx->txlt_curwin_processed_dma_cookies = 0; 10933 spx->txlt_dma_cookie_list = NULL; 10934 } else { 10935 /* 10936 * DMA setup is reused. Check if we need to process more 10937 * cookies in current window, or to get next window, if any. 10938 */ 10939 10940 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 10941 spx->txlt_curwin_num_dma_cookies); 10942 10943 if (spx->txlt_curwin_processed_dma_cookies == 10944 spx->txlt_curwin_num_dma_cookies) { 10945 /* 10946 * All cookies from current DMA window were processed. 10947 * Get next DMA window. 10948 */ 10949 spx->txlt_cur_dma_win++; 10950 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 10951 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 10952 spx->txlt_cur_dma_win, &offset, &size, 10953 &spx->txlt_dma_cookie, 10954 &spx->txlt_curwin_num_dma_cookies); 10955 spx->txlt_curwin_processed_dma_cookies = 0; 10956 } else { 10957 /* No more windows! End of request! */ 10958 /* What to do? - panic for now */ 10959 ASSERT(spx->txlt_cur_dma_win >= 10960 spx->txlt_num_dma_win); 10961 10962 spx->txlt_curwin_num_dma_cookies = 0; 10963 spx->txlt_curwin_processed_dma_cookies = 0; 10964 spx->txlt_sata_pkt-> 10965 satapkt_cmd.satacmd_num_dma_cookies = 0; 10966 return (DDI_SUCCESS); 10967 } 10968 } 10969 } 10970 /* There better be at least one DMA cookie outstanding */ 10971 ASSERT((spx->txlt_curwin_num_dma_cookies - 10972 spx->txlt_curwin_processed_dma_cookies) > 0); 10973 10974 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 10975 /* The default cookie slot was used in previous run */ 10976 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 10977 spx->txlt_dma_cookie_list = NULL; 10978 spx->txlt_dma_cookie_list_len = 0; 10979 } 10980 if (spx->txlt_curwin_processed_dma_cookies == 0) { 10981 /* 10982 * Processing a new DMA window - set-up dma cookies list. 10983 * We may reuse previously allocated cookie array if it is 10984 * possible. 10985 */ 10986 if (spx->txlt_dma_cookie_list != NULL && 10987 spx->txlt_dma_cookie_list_len < 10988 spx->txlt_curwin_num_dma_cookies) { 10989 /* 10990 * New DMA window contains more cookies than 10991 * the previous one. We need larger cookie list - free 10992 * the old one. 10993 */ 10994 (void) kmem_free(spx->txlt_dma_cookie_list, 10995 spx->txlt_dma_cookie_list_len * 10996 sizeof (ddi_dma_cookie_t)); 10997 spx->txlt_dma_cookie_list = NULL; 10998 spx->txlt_dma_cookie_list_len = 0; 10999 } 11000 if (spx->txlt_dma_cookie_list == NULL) { 11001 /* 11002 * Calculate lesser of number of cookies in this 11003 * DMA window and number of s/g entries. 11004 */ 11005 max_sg_len = cur_dma_attr->dma_attr_sgllen; 11006 req_len = MIN(max_sg_len, 11007 spx->txlt_curwin_num_dma_cookies); 11008 11009 /* Allocate new dma cookie array if necessary */ 11010 if (req_len == 1) { 11011 /* Only one cookie - no need for a list */ 11012 spx->txlt_dma_cookie_list = 11013 &spx->txlt_dma_cookie; 11014 spx->txlt_dma_cookie_list_len = 1; 11015 } else { 11016 /* 11017 * More than one cookie - try to allocate space. 11018 */ 11019 spx->txlt_dma_cookie_list = kmem_zalloc( 11020 sizeof (ddi_dma_cookie_t) * req_len, 11021 callback == NULL_FUNC ? KM_NOSLEEP : 11022 KM_SLEEP); 11023 if (spx->txlt_dma_cookie_list == NULL) { 11024 SATADBG1(SATA_DBG_DMA_SETUP, 11025 spx->txlt_sata_hba_inst, 11026 "sata_dma_buf_setup: cookie list " 11027 "allocation failed\n", NULL); 11028 /* 11029 * We could not allocate space for 11030 * neccessary number of dma cookies in 11031 * this window, so we fail this request. 11032 * Next invocation would try again to 11033 * allocate space for cookie list. 11034 * Note:Packet residue was not modified. 11035 */ 11036 return (DDI_DMA_NORESOURCES); 11037 } else { 11038 spx->txlt_dma_cookie_list_len = req_len; 11039 } 11040 } 11041 } 11042 /* 11043 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 11044 * First cookie was already fetched. 11045 */ 11046 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 11047 cur_txfer_len = 11048 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 11049 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 11050 spx->txlt_curwin_processed_dma_cookies++; 11051 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 11052 (i < spx->txlt_curwin_num_dma_cookies); i++) { 11053 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 11054 &spx->txlt_dma_cookie_list[i]); 11055 cur_txfer_len += 11056 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 11057 spx->txlt_curwin_processed_dma_cookies++; 11058 spx->txlt_sata_pkt-> 11059 satapkt_cmd.satacmd_num_dma_cookies += 1; 11060 } 11061 } else { 11062 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 11063 "sata_dma_buf_setup: sliding within DMA window, " 11064 "cur cookie %d, total cookies %d\n", 11065 spx->txlt_curwin_processed_dma_cookies, 11066 spx->txlt_curwin_num_dma_cookies); 11067 11068 /* 11069 * Not all cookies from the current dma window were used because 11070 * of s/g limitation. 11071 * There is no need to re-size the list - it was set at 11072 * optimal size, or only default entry is used (s/g = 1). 11073 */ 11074 if (spx->txlt_dma_cookie_list == NULL) { 11075 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 11076 spx->txlt_dma_cookie_list_len = 1; 11077 } 11078 /* 11079 * Since we are processing remaining cookies in a DMA window, 11080 * there may be less of them than the number of entries in the 11081 * current dma cookie list. 11082 */ 11083 req_len = MIN(spx->txlt_dma_cookie_list_len, 11084 (spx->txlt_curwin_num_dma_cookies - 11085 spx->txlt_curwin_processed_dma_cookies)); 11086 11087 /* Fetch the next batch of cookies */ 11088 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 11089 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 11090 &spx->txlt_dma_cookie_list[i]); 11091 cur_txfer_len += 11092 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 11093 spx->txlt_sata_pkt-> 11094 satapkt_cmd.satacmd_num_dma_cookies++; 11095 spx->txlt_curwin_processed_dma_cookies++; 11096 } 11097 } 11098 11099 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 11100 11101 /* Point sata_cmd to the cookie list */ 11102 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 11103 &spx->txlt_dma_cookie_list[0]; 11104 11105 /* Remember number of DMA cookies passed in sata packet */ 11106 spx->txlt_num_dma_cookies = 11107 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 11108 11109 ASSERT(cur_txfer_len != 0); 11110 if (cur_txfer_len <= bp->b_bcount) 11111 spx->txlt_total_residue -= cur_txfer_len; 11112 else { 11113 /* 11114 * Temporary DMA buffer has been padded by 11115 * ddi_dma_mem_alloc()! 11116 * This requires special handling, because DMA cookies are 11117 * based on the temporary buffer size, not the b_bcount, 11118 * and we have extra bytes to transfer - but the packet 11119 * residue has to stay correct because we will copy only 11120 * the requested number of bytes. 11121 */ 11122 spx->txlt_total_residue -= bp->b_bcount; 11123 } 11124 11125 return (DDI_SUCCESS); 11126 } 11127 11128 11129 /* 11130 * Fetch Device Identify data. 11131 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 11132 * command to a device and get the device identify data. 11133 * The device_info structure has to be set to device type (for selecting proper 11134 * device identify command). 11135 * 11136 * Returns: 11137 * SATA_SUCCESS if cmd succeeded 11138 * SATA_RETRY if cmd was rejected and could be retried, 11139 * SATA_FAILURE if cmd failed and should not be retried (port error) 11140 * 11141 * Cannot be called in an interrupt context. 11142 */ 11143 11144 static int 11145 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 11146 sata_drive_info_t *sdinfo) 11147 { 11148 struct buf *bp; 11149 sata_pkt_t *spkt; 11150 sata_cmd_t *scmd; 11151 sata_pkt_txlate_t *spx; 11152 int rval; 11153 11154 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 11155 spx->txlt_sata_hba_inst = sata_hba_inst; 11156 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 11157 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 11158 if (spkt == NULL) { 11159 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11160 return (SATA_RETRY); /* may retry later */ 11161 } 11162 /* address is needed now */ 11163 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 11164 11165 /* 11166 * Allocate buffer for Identify Data return data 11167 */ 11168 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 11169 if (bp == NULL) { 11170 sata_pkt_free(spx); 11171 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11172 SATA_LOG_D((sata_hba_inst, CE_WARN, 11173 "sata_fetch_device_identify_data: " 11174 "cannot allocate buffer for ID")); 11175 return (SATA_RETRY); /* may retry later */ 11176 } 11177 11178 /* Fill sata_pkt */ 11179 sdinfo->satadrv_state = SATA_STATE_PROBING; 11180 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 11181 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 11182 /* Synchronous mode, no callback */ 11183 spkt->satapkt_comp = NULL; 11184 /* Timeout 30s */ 11185 spkt->satapkt_time = sata_default_pkt_time; 11186 11187 scmd = &spkt->satapkt_cmd; 11188 scmd->satacmd_bp = bp; 11189 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 11190 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 11191 11192 /* Build Identify Device cmd in the sata_pkt */ 11193 scmd->satacmd_addr_type = 0; /* N/A */ 11194 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 11195 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 11196 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 11197 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 11198 scmd->satacmd_features_reg = 0; /* N/A */ 11199 scmd->satacmd_device_reg = 0; /* Always device 0 */ 11200 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 11201 /* Identify Packet Device cmd */ 11202 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 11203 } else { 11204 /* Identify Device cmd - mandatory for all other devices */ 11205 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 11206 } 11207 11208 /* Send pkt to SATA HBA driver */ 11209 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 11210 if (rval == SATA_TRAN_ACCEPTED && 11211 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 11212 if ((sdinfo->satadrv_id.ai_config & SATA_INCOMPLETE_DATA) == 11213 SATA_INCOMPLETE_DATA) { 11214 SATA_LOG_D((sata_hba_inst, CE_WARN, 11215 "SATA disk device at port %d - " 11216 "partial Identify Data", 11217 sdinfo->satadrv_addr.cport)); 11218 rval = SATA_RETRY; /* may retry later */ 11219 goto fail; 11220 } 11221 /* Update sata_drive_info */ 11222 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 11223 DDI_DMA_SYNC_FORKERNEL); 11224 ASSERT(rval == DDI_SUCCESS); 11225 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 11226 sizeof (sata_id_t)); 11227 11228 sdinfo->satadrv_features_support = 0; 11229 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11230 /* 11231 * Retrieve capacity (disks only) and addressing mode 11232 */ 11233 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 11234 } else { 11235 /* 11236 * For ATAPI devices one would have to issue 11237 * Get Capacity cmd for media capacity. Not here. 11238 */ 11239 sdinfo->satadrv_capacity = 0; 11240 /* 11241 * Check what cdb length is supported 11242 */ 11243 if ((sdinfo->satadrv_id.ai_config & 11244 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 11245 sdinfo->satadrv_atapi_cdb_len = 16; 11246 else 11247 sdinfo->satadrv_atapi_cdb_len = 12; 11248 } 11249 /* Setup supported features flags */ 11250 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 11251 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 11252 11253 /* Check for SATA GEN and NCQ support */ 11254 if (sdinfo->satadrv_id.ai_satacap != 0 && 11255 sdinfo->satadrv_id.ai_satacap != 0xffff) { 11256 /* SATA compliance */ 11257 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 11258 sdinfo->satadrv_features_support |= 11259 SATA_DEV_F_NCQ; 11260 if (sdinfo->satadrv_id.ai_satacap & 11261 (SATA_1_SPEED | SATA_2_SPEED)) { 11262 if (sdinfo->satadrv_id.ai_satacap & 11263 SATA_2_SPEED) 11264 sdinfo->satadrv_features_support |= 11265 SATA_DEV_F_SATA2; 11266 if (sdinfo->satadrv_id.ai_satacap & 11267 SATA_1_SPEED) 11268 sdinfo->satadrv_features_support |= 11269 SATA_DEV_F_SATA1; 11270 } else { 11271 sdinfo->satadrv_features_support |= 11272 SATA_DEV_F_SATA1; 11273 } 11274 } 11275 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 11276 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 11277 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 11278 11279 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 11280 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 11281 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 11282 ++sdinfo->satadrv_queue_depth; 11283 /* Adjust according to controller capabilities */ 11284 sdinfo->satadrv_max_queue_depth = MIN( 11285 sdinfo->satadrv_queue_depth, 11286 SATA_QDEPTH(sata_hba_inst)); 11287 /* Adjust according to global queue depth limit */ 11288 sdinfo->satadrv_max_queue_depth = MIN( 11289 sdinfo->satadrv_max_queue_depth, 11290 sata_current_max_qdepth); 11291 if (sdinfo->satadrv_max_queue_depth == 0) 11292 sdinfo->satadrv_max_queue_depth = 1; 11293 } else 11294 sdinfo->satadrv_max_queue_depth = 1; 11295 11296 rval = SATA_SUCCESS; 11297 } else { 11298 /* 11299 * Woops, no Identify Data. 11300 */ 11301 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 11302 rval = SATA_RETRY; /* may retry later */ 11303 } else if (rval == SATA_TRAN_ACCEPTED) { 11304 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 11305 spkt->satapkt_reason == SATA_PKT_ABORTED || 11306 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 11307 spkt->satapkt_reason == SATA_PKT_RESET) 11308 rval = SATA_RETRY; /* may retry later */ 11309 else 11310 rval = SATA_FAILURE; 11311 } else { 11312 rval = SATA_FAILURE; 11313 } 11314 } 11315 fail: 11316 /* Free allocated resources */ 11317 sata_free_local_buffer(spx); 11318 sata_pkt_free(spx); 11319 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11320 11321 return (rval); 11322 } 11323 11324 11325 /* 11326 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 11327 * UDMA mode is checked first, followed by MWDMA mode. 11328 * set correctly, so this function is setting it to the highest supported level. 11329 * Older SATA spec required that the device supports at least DMA 4 mode and 11330 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 11331 * restriction has been removed. 11332 * 11333 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 11334 * Returns SATA_FAILURE if proper DMA mode could not be selected. 11335 * 11336 * NOTE: This function should be called only if DMA mode is supported. 11337 */ 11338 static int 11339 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 11340 { 11341 sata_pkt_t *spkt; 11342 sata_cmd_t *scmd; 11343 sata_pkt_txlate_t *spx; 11344 int i, mode; 11345 uint8_t subcmd; 11346 int rval = SATA_SUCCESS; 11347 11348 ASSERT(sdinfo != NULL); 11349 ASSERT(sata_hba_inst != NULL); 11350 11351 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 11352 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 11353 /* Find highest Ultra DMA mode supported */ 11354 for (mode = 6; mode >= 0; --mode) { 11355 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 11356 break; 11357 } 11358 #if 0 11359 /* Left for historical reasons */ 11360 /* 11361 * Some initial version of SATA spec indicated that at least 11362 * UDMA mode 4 has to be supported. It is not mentioned in 11363 * SerialATA 2.6, so this restriction is removed. 11364 */ 11365 if (mode < 4) 11366 return (SATA_FAILURE); 11367 #endif 11368 /* Find UDMA mode currently selected */ 11369 for (i = 6; i >= 0; --i) { 11370 if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8))) 11371 break; 11372 } 11373 if (i >= mode) 11374 /* Nothing to do */ 11375 return (SATA_SUCCESS); 11376 11377 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 11378 11379 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 11380 /* Find highest MultiWord DMA mode supported */ 11381 for (mode = 2; mode >= 0; --mode) { 11382 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 11383 break; 11384 } 11385 /* Find highest MultiWord DMA mode selected */ 11386 for (i = 2; i >= 0; --i) { 11387 if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8))) 11388 break; 11389 } 11390 if (i >= mode) 11391 /* Nothing to do */ 11392 return (SATA_SUCCESS); 11393 11394 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 11395 } else 11396 return (SATA_SUCCESS); 11397 11398 /* 11399 * Set DMA mode via SET FEATURES COMMAND. 11400 * Prepare packet for SET FEATURES COMMAND. 11401 */ 11402 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 11403 spx->txlt_sata_hba_inst = sata_hba_inst; 11404 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 11405 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 11406 if (spkt == NULL) { 11407 SATA_LOG_D((sata_hba_inst, CE_WARN, 11408 "sata_set_dma_mode: could not set DMA mode %", mode)); 11409 rval = SATA_FAILURE; 11410 goto done; 11411 } 11412 /* Fill sata_pkt */ 11413 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 11414 /* Timeout 30s */ 11415 spkt->satapkt_time = sata_default_pkt_time; 11416 /* Synchronous mode, no callback, interrupts */ 11417 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 11418 spkt->satapkt_comp = NULL; 11419 scmd = &spkt->satapkt_cmd; 11420 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 11421 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 11422 scmd->satacmd_addr_type = 0; 11423 scmd->satacmd_device_reg = 0; 11424 scmd->satacmd_status_reg = 0; 11425 scmd->satacmd_error_reg = 0; 11426 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 11427 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 11428 scmd->satacmd_sec_count_lsb = subcmd | mode; 11429 11430 /* Transfer command to HBA */ 11431 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 11432 spkt) != SATA_TRAN_ACCEPTED || 11433 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 11434 /* Pkt execution failed */ 11435 rval = SATA_FAILURE; 11436 } 11437 done: 11438 11439 /* Free allocated resources */ 11440 if (spkt != NULL) 11441 sata_pkt_free(spx); 11442 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11443 11444 return (rval); 11445 } 11446 11447 11448 /* 11449 * Set device caching mode. 11450 * One of the following operations should be specified: 11451 * SATAC_SF_ENABLE_READ_AHEAD 11452 * SATAC_SF_DISABLE_READ_AHEAD 11453 * SATAC_SF_ENABLE_WRITE_CACHE 11454 * SATAC_SF_DISABLE_WRITE_CACHE 11455 * 11456 * If operation fails, system log messgage is emitted. 11457 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 11458 */ 11459 11460 static int 11461 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 11462 int cache_op) 11463 { 11464 sata_pkt_t *spkt; 11465 sata_cmd_t *scmd; 11466 sata_pkt_txlate_t *spx; 11467 int rval = SATA_SUCCESS; 11468 char *infop; 11469 11470 ASSERT(sdinfo != NULL); 11471 ASSERT(sata_hba_inst != NULL); 11472 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 11473 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 11474 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 11475 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 11476 11477 11478 /* Prepare packet for SET FEATURES COMMAND */ 11479 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 11480 spx->txlt_sata_hba_inst = sata_hba_inst; 11481 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 11482 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 11483 if (spkt == NULL) { 11484 rval = SATA_FAILURE; 11485 goto failure; 11486 } 11487 /* Fill sata_pkt */ 11488 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 11489 /* Timeout 30s */ 11490 spkt->satapkt_time = sata_default_pkt_time; 11491 /* Synchronous mode, no callback, interrupts */ 11492 spkt->satapkt_op_mode = 11493 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 11494 spkt->satapkt_comp = NULL; 11495 scmd = &spkt->satapkt_cmd; 11496 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 11497 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 11498 scmd->satacmd_addr_type = 0; 11499 scmd->satacmd_device_reg = 0; 11500 scmd->satacmd_status_reg = 0; 11501 scmd->satacmd_error_reg = 0; 11502 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 11503 scmd->satacmd_features_reg = cache_op; 11504 11505 /* Transfer command to HBA */ 11506 if (((*SATA_START_FUNC(sata_hba_inst))( 11507 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 11508 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 11509 /* Pkt execution failed */ 11510 switch (cache_op) { 11511 case SATAC_SF_ENABLE_READ_AHEAD: 11512 infop = "enabling read ahead failed"; 11513 break; 11514 case SATAC_SF_DISABLE_READ_AHEAD: 11515 infop = "disabling read ahead failed"; 11516 break; 11517 case SATAC_SF_ENABLE_WRITE_CACHE: 11518 infop = "enabling write cache failed"; 11519 break; 11520 case SATAC_SF_DISABLE_WRITE_CACHE: 11521 infop = "disabling write cache failed"; 11522 break; 11523 } 11524 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 11525 rval = SATA_FAILURE; 11526 } 11527 failure: 11528 /* Free allocated resources */ 11529 if (spkt != NULL) 11530 sata_pkt_free(spx); 11531 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11532 return (rval); 11533 } 11534 11535 /* 11536 * Set Removable Media Status Notification (enable/disable) 11537 * state == 0 , disable 11538 * state != 0 , enable 11539 * 11540 * If operation fails, system log messgage is emitted. 11541 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 11542 */ 11543 11544 static int 11545 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 11546 int state) 11547 { 11548 sata_pkt_t *spkt; 11549 sata_cmd_t *scmd; 11550 sata_pkt_txlate_t *spx; 11551 int rval = SATA_SUCCESS; 11552 char *infop; 11553 11554 ASSERT(sdinfo != NULL); 11555 ASSERT(sata_hba_inst != NULL); 11556 11557 /* Prepare packet for SET FEATURES COMMAND */ 11558 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 11559 spx->txlt_sata_hba_inst = sata_hba_inst; 11560 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 11561 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 11562 if (spkt == NULL) { 11563 rval = SATA_FAILURE; 11564 goto failure; 11565 } 11566 /* Fill sata_pkt */ 11567 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 11568 /* Timeout 30s */ 11569 spkt->satapkt_time = sata_default_pkt_time; 11570 /* Synchronous mode, no callback, interrupts */ 11571 spkt->satapkt_op_mode = 11572 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 11573 spkt->satapkt_comp = NULL; 11574 scmd = &spkt->satapkt_cmd; 11575 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 11576 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 11577 scmd->satacmd_addr_type = 0; 11578 scmd->satacmd_device_reg = 0; 11579 scmd->satacmd_status_reg = 0; 11580 scmd->satacmd_error_reg = 0; 11581 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 11582 if (state == 0) 11583 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 11584 else 11585 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 11586 11587 /* Transfer command to HBA */ 11588 if (((*SATA_START_FUNC(sata_hba_inst))( 11589 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 11590 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 11591 /* Pkt execution failed */ 11592 if (state == 0) 11593 infop = "disabling Removable Media Status " 11594 "Notification failed"; 11595 else 11596 infop = "enabling Removable Media Status " 11597 "Notification failed"; 11598 11599 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 11600 rval = SATA_FAILURE; 11601 } 11602 failure: 11603 /* Free allocated resources */ 11604 if (spkt != NULL) 11605 sata_pkt_free(spx); 11606 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 11607 return (rval); 11608 } 11609 11610 11611 /* 11612 * Update port SCR block 11613 */ 11614 static void 11615 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device) 11616 { 11617 port_scr->sstatus = device->satadev_scr.sstatus; 11618 port_scr->serror = device->satadev_scr.serror; 11619 port_scr->scontrol = device->satadev_scr.scontrol; 11620 port_scr->sactive = device->satadev_scr.sactive; 11621 port_scr->snotific = device->satadev_scr.snotific; 11622 } 11623 11624 /* 11625 * Update state and copy port ss* values from passed sata_device structure. 11626 * sata_address is validated - if not valid, nothing is changed in sata_scsi 11627 * configuration struct. 11628 * 11629 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 11630 * regardless of the state in device argument. 11631 * 11632 * Port mutex should be held while calling this function. 11633 */ 11634 static void 11635 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 11636 sata_device_t *sata_device) 11637 { 11638 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, 11639 sata_device->satadev_addr.cport))); 11640 11641 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 11642 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 11643 11644 sata_cport_info_t *cportinfo; 11645 11646 if (SATA_NUM_CPORTS(sata_hba_inst) <= 11647 sata_device->satadev_addr.cport) 11648 return; 11649 11650 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 11651 sata_device->satadev_addr.cport); 11652 sata_update_port_scr(&cportinfo->cport_scr, sata_device); 11653 11654 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 11655 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 11656 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 11657 cportinfo->cport_state |= 11658 sata_device->satadev_state & SATA_PSTATE_VALID; 11659 } else { 11660 sata_pmport_info_t *pmportinfo; 11661 11662 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) || 11663 (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 11664 SATA_NUM_PMPORTS(sata_hba_inst, 11665 sata_device->satadev_addr.cport) < 11666 sata_device->satadev_addr.pmport) 11667 return; 11668 11669 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 11670 sata_device->satadev_addr.cport, 11671 sata_device->satadev_addr.pmport); 11672 sata_update_port_scr(&pmportinfo->pmport_scr, sata_device); 11673 11674 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 11675 pmportinfo->pmport_state &= 11676 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | 11677 SATA_PSTATE_FAILED); 11678 pmportinfo->pmport_state |= 11679 sata_device->satadev_state & SATA_PSTATE_VALID; 11680 } 11681 } 11682 11683 11684 11685 /* 11686 * Extract SATA port specification from an IOCTL argument. 11687 * 11688 * This function return the port the user land send us as is, unless it 11689 * cannot retrieve port spec, then -1 is returned. 11690 * 11691 * Note: Only cport - no port multiplier port. 11692 */ 11693 static int32_t 11694 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 11695 { 11696 int32_t port; 11697 11698 /* Extract port number from nvpair in dca structure */ 11699 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 11700 SATA_LOG_D((sata_hba_inst, CE_NOTE, 11701 "sata_get_port_num: invalid port spec 0x%x in ioctl", 11702 port)); 11703 port = -1; 11704 } 11705 11706 return (port); 11707 } 11708 11709 /* 11710 * Get dev_info_t pointer to the device node pointed to by port argument. 11711 * NOTE: target argument is a value used in ioctls to identify 11712 * the AP - it is not a sata_address. 11713 * It is a combination of cport, pmport and address qualifier, encodded same 11714 * way as a scsi target number. 11715 * At this moment it carries only cport number. 11716 * 11717 * No PMult hotplug support. 11718 * 11719 * Returns dev_info_t pointer if target device was found, NULL otherwise. 11720 */ 11721 11722 static dev_info_t * 11723 sata_get_target_dip(dev_info_t *dip, int32_t port) 11724 { 11725 dev_info_t *cdip = NULL; 11726 int target, tgt; 11727 int ncport; 11728 int circ; 11729 11730 ncport = port & SATA_CFGA_CPORT_MASK; 11731 target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT); 11732 11733 ndi_devi_enter(dip, &circ); 11734 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 11735 dev_info_t *next = ddi_get_next_sibling(cdip); 11736 11737 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 11738 DDI_PROP_DONTPASS, "target", -1); 11739 if (tgt == -1) { 11740 /* 11741 * This is actually an error condition, but not 11742 * a fatal one. Just continue the search. 11743 */ 11744 cdip = next; 11745 continue; 11746 } 11747 11748 if (tgt == target) 11749 break; 11750 11751 cdip = next; 11752 } 11753 ndi_devi_exit(dip, circ); 11754 11755 return (cdip); 11756 } 11757 11758 11759 /* 11760 * sata_cfgadm_state: 11761 * Use the sata port state and state of the target node to figure out 11762 * the cfgadm_state. 11763 * 11764 * The port argument is a value with encoded cport, 11765 * pmport and address qualifier, in the same manner as a scsi target number. 11766 * SCSI_TO_SATA_CPORT macro extracts cport number, 11767 * SCSI_TO_SATA_PMPORT extracts pmport number and 11768 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 11769 * 11770 * For now, support is for cports only - no port multiplier device ports. 11771 */ 11772 11773 static void 11774 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 11775 devctl_ap_state_t *ap_state) 11776 { 11777 uint16_t cport; 11778 int port_state; 11779 11780 /* Cport only */ 11781 cport = SCSI_TO_SATA_CPORT(port); 11782 11783 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 11784 if (port_state & SATA_PSTATE_SHUTDOWN || 11785 port_state & SATA_PSTATE_FAILED) { 11786 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 11787 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 11788 if (port_state & SATA_PSTATE_FAILED) 11789 ap_state->ap_condition = AP_COND_FAILED; 11790 else 11791 ap_state->ap_condition = AP_COND_UNKNOWN; 11792 11793 return; 11794 } 11795 11796 /* Need to check pmult device port here as well, when supported */ 11797 11798 /* Port is enabled and ready */ 11799 11800 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 11801 case SATA_DTYPE_NONE: 11802 { 11803 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 11804 ap_state->ap_condition = AP_COND_OK; 11805 /* No device attached */ 11806 ap_state->ap_rstate = AP_RSTATE_EMPTY; 11807 break; 11808 } 11809 case SATA_DTYPE_UNKNOWN: 11810 case SATA_DTYPE_ATAPINONCD: 11811 case SATA_DTYPE_PMULT: /* Until PMult is supported */ 11812 case SATA_DTYPE_ATADISK: 11813 case SATA_DTYPE_ATAPICD: 11814 { 11815 dev_info_t *tdip = NULL; 11816 dev_info_t *dip = NULL; 11817 int circ; 11818 11819 dip = SATA_DIP(sata_hba_inst); 11820 tdip = sata_get_target_dip(dip, port); 11821 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 11822 if (tdip != NULL) { 11823 ndi_devi_enter(dip, &circ); 11824 mutex_enter(&(DEVI(tdip)->devi_lock)); 11825 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 11826 /* 11827 * There could be the case where previously 11828 * configured and opened device was removed 11829 * and unknown device was plugged. 11830 * In such case we want to show a device, and 11831 * its configured or unconfigured state but 11832 * indicate unusable condition untill the 11833 * old target node is released and removed. 11834 */ 11835 ap_state->ap_condition = AP_COND_UNUSABLE; 11836 } else { 11837 ap_state->ap_condition = AP_COND_OK; 11838 } 11839 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 11840 (DEVI_IS_DEVICE_DOWN(tdip))) { 11841 ap_state->ap_ostate = 11842 AP_OSTATE_UNCONFIGURED; 11843 } else { 11844 ap_state->ap_ostate = 11845 AP_OSTATE_CONFIGURED; 11846 } 11847 mutex_exit(&(DEVI(tdip)->devi_lock)); 11848 ndi_devi_exit(dip, circ); 11849 } else { 11850 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 11851 ap_state->ap_condition = AP_COND_UNKNOWN; 11852 } 11853 break; 11854 } 11855 default: 11856 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 11857 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 11858 ap_state->ap_condition = AP_COND_UNKNOWN; 11859 /* 11860 * This is actually internal error condition (non fatal), 11861 * because we have already checked all defined device types. 11862 */ 11863 SATA_LOG_D((sata_hba_inst, CE_WARN, 11864 "sata_cfgadm_state: Internal error: " 11865 "unknown device type")); 11866 break; 11867 } 11868 } 11869 11870 11871 /* 11872 * Preset scsi extended sense data (to NO SENSE) 11873 * First 18 bytes of the sense data are preset to current valid sense 11874 * with a key NO SENSE data. 11875 * 11876 * Returns void 11877 */ 11878 static void 11879 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 11880 { 11881 sense->es_valid = 1; /* Valid sense */ 11882 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 11883 sense->es_key = KEY_NO_SENSE; 11884 sense->es_info_1 = 0; 11885 sense->es_info_2 = 0; 11886 sense->es_info_3 = 0; 11887 sense->es_info_4 = 0; 11888 sense->es_add_len = 10; /* Additional length - replace with a def */ 11889 sense->es_cmd_info[0] = 0; 11890 sense->es_cmd_info[1] = 0; 11891 sense->es_cmd_info[2] = 0; 11892 sense->es_cmd_info[3] = 0; 11893 sense->es_add_code = 0; 11894 sense->es_qual_code = 0; 11895 } 11896 11897 /* 11898 * Register a legacy cmdk-style devid for the target (disk) device. 11899 * 11900 * Note: This function is called only when the HBA devinfo node has the 11901 * property "use-cmdk-devid-format" set. This property indicates that 11902 * devid compatible with old cmdk (target) driver is to be generated 11903 * for any target device attached to this controller. This will take 11904 * precedence over the devid generated by sd (target) driver. 11905 * This function is derived from cmdk_devid_setup() function in cmdk.c. 11906 */ 11907 static void 11908 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 11909 { 11910 char *hwid; 11911 int modlen; 11912 int serlen; 11913 int rval; 11914 ddi_devid_t devid; 11915 11916 /* 11917 * device ID is a concatanation of model number, "=", serial number. 11918 */ 11919 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 11920 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 11921 sizeof (sdinfo->satadrv_id.ai_model)); 11922 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 11923 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 11924 if (modlen == 0) 11925 goto err; 11926 hwid[modlen++] = '='; 11927 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 11928 sizeof (sdinfo->satadrv_id.ai_drvser)); 11929 swab(&hwid[modlen], &hwid[modlen], 11930 sizeof (sdinfo->satadrv_id.ai_drvser)); 11931 serlen = sata_check_modser(&hwid[modlen], 11932 sizeof (sdinfo->satadrv_id.ai_drvser)); 11933 if (serlen == 0) 11934 goto err; 11935 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 11936 11937 /* initialize/register devid */ 11938 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 11939 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) 11940 rval = ddi_devid_register(dip, devid); 11941 11942 if (rval != DDI_SUCCESS) 11943 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 11944 " on port %d", sdinfo->satadrv_addr.cport); 11945 err: 11946 kmem_free(hwid, LEGACY_HWID_LEN); 11947 } 11948 11949 /* 11950 * valid model/serial string must contain a non-zero non-space characters. 11951 * trim trailing spaces/NULLs. 11952 */ 11953 static int 11954 sata_check_modser(char *buf, int buf_len) 11955 { 11956 boolean_t ret; 11957 char *s; 11958 int i; 11959 int tb; 11960 char ch; 11961 11962 ret = B_FALSE; 11963 s = buf; 11964 for (i = 0; i < buf_len; i++) { 11965 ch = *s++; 11966 if (ch != ' ' && ch != '\0') 11967 tb = i + 1; 11968 if (ch != ' ' && ch != '\0' && ch != '0') 11969 ret = B_TRUE; 11970 } 11971 11972 if (ret == B_FALSE) 11973 return (0); /* invalid string */ 11974 11975 return (tb); /* return length */ 11976 } 11977 11978 /* 11979 * sata_set_drive_features function compares current device features setting 11980 * with the saved device features settings and, if there is a difference, 11981 * it restores device features setting to the previously saved state. 11982 * It also arbitrarily tries to select the highest supported DMA mode. 11983 * Device Identify or Identify Packet Device data has to be current. 11984 * At the moment read ahead and write cache are considered for all devices. 11985 * For atapi devices, Removable Media Status Notification is set in addition 11986 * to common features. 11987 * 11988 * This function cannot be called in the interrupt context (it may sleep). 11989 * 11990 * The input argument sdinfo should point to the drive info structure 11991 * to be updated after features are set. Note, that only 11992 * device (packet) identify data is updated, not the flags indicating the 11993 * supported features. 11994 * 11995 * Returns TRUE if successful or there was nothing to do. Device Identify data 11996 * in the drive info structure pointed to by the sdinfo argumens is updated 11997 * even when no features were set or changed. 11998 * 11999 * Returns FALSE if device features could not be set. 12000 * 12001 * Note: This function may fail the port, making it inaccessible. 12002 * In such case the explicit port disconnect/connect or physical device 12003 * detach/attach is required to re-evaluate port state again. 12004 */ 12005 12006 static int 12007 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 12008 sata_drive_info_t *sdinfo, int restore) 12009 { 12010 int rval = SATA_SUCCESS; 12011 sata_drive_info_t new_sdinfo; 12012 char *finfo = "sata_set_drive_features: cannot"; 12013 char *finfox; 12014 int cache_op; 12015 12016 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12017 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 12018 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 12019 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12020 /* 12021 * Cannot get device identification - retry later 12022 */ 12023 SATA_LOG_D((sata_hba_inst, CE_WARN, 12024 "%s fetch device identify data\n", finfo)); 12025 return (SATA_FAILURE); 12026 } 12027 finfox = (restore != 0) ? " restore device features" : 12028 " initialize device features\n"; 12029 12030 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12031 /* Arbitrarily set UDMA mode */ 12032 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12033 SATA_SUCCESS) { 12034 SATA_LOG_D((sata_hba_inst, CE_WARN, 12035 "%s set UDMA mode\n", finfo)); 12036 return (SATA_FAILURE); 12037 } 12038 } else { /* Assume SATA ATAPI CD/DVD */ 12039 /* Set Removable Media Status Notification, if necessary */ 12040 if ((new_sdinfo.satadrv_id.ai_cmdset83 & 12041 SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) { 12042 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 12043 (!(new_sdinfo.satadrv_id.ai_features86 & 12044 SATA_RM_STATUS_NOTIFIC))) || 12045 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 12046 (new_sdinfo.satadrv_id.ai_features86 & 12047 SATA_RM_STATUS_NOTIFIC))) { 12048 /* Current setting does not match saved one */ 12049 if (sata_set_rmsn(sata_hba_inst, sdinfo, 12050 sdinfo->satadrv_settings & 12051 SATA_DEV_RMSN) != SATA_SUCCESS) 12052 rval = SATA_FAILURE; 12053 } 12054 } 12055 /* 12056 * We have to set Multiword DMA or UDMA, if it is supported, as 12057 * we want to use DMA transfer mode whenever possible. 12058 * Some devices require explicit setting of the DMA mode. 12059 */ 12060 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 12061 /* Set highest supported DMA mode */ 12062 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12063 SATA_SUCCESS) { 12064 SATA_LOG_D((sata_hba_inst, CE_WARN, 12065 "%s set UDMA mode\n", finfo)); 12066 rval = SATA_FAILURE; 12067 } 12068 } 12069 } 12070 12071 if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) && 12072 !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 12073 /* None of the features is supported - do nothing */ 12074 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12075 "settable features not supported\n", NULL); 12076 goto update_sdinfo; 12077 } 12078 12079 if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12080 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 12081 ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12082 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12083 /* Nothing to do */ 12084 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12085 "no device features to set\n", NULL); 12086 goto update_sdinfo; 12087 } 12088 12089 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12090 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) { 12091 if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) { 12092 /* Enable read ahead / read cache */ 12093 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 12094 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12095 "enabling read cache\n", NULL); 12096 } else { 12097 /* Disable read ahead / read cache */ 12098 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 12099 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12100 "disabling read cache\n", NULL); 12101 } 12102 12103 /* Try to set read cache mode */ 12104 if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12105 cache_op) != SATA_SUCCESS) { 12106 /* Pkt execution failed */ 12107 rval = SATA_FAILURE; 12108 } 12109 } 12110 12111 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12112 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12113 if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) { 12114 /* Enable write cache */ 12115 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 12116 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12117 "enabling write cache\n", NULL); 12118 } else { 12119 /* Disable write cache */ 12120 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 12121 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12122 "disabling write cache\n", NULL); 12123 } 12124 /* Try to set write cache mode */ 12125 if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12126 cache_op) != SATA_SUCCESS) { 12127 /* Pkt execution failed */ 12128 rval = SATA_FAILURE; 12129 } 12130 } 12131 12132 if (rval == SATA_FAILURE) 12133 SATA_LOG_D((sata_hba_inst, CE_WARN, 12134 "%s %s", finfo, finfox)); 12135 update_sdinfo: 12136 /* 12137 * We need to fetch Device Identify data again 12138 */ 12139 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12140 /* 12141 * Cannot get device identification - retry later 12142 */ 12143 SATA_LOG_D((sata_hba_inst, CE_WARN, 12144 "%s cannot re-fetch device identify data\n")); 12145 rval = SATA_FAILURE; 12146 } 12147 /* Copy device sata info. */ 12148 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 12149 12150 return (rval); 12151 } 12152 12153 12154 /* 12155 * 12156 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 12157 * unable to determine. 12158 * 12159 * Cannot be called in an interrupt context. 12160 * 12161 * Called by sata_build_lsense_page_2f() 12162 */ 12163 12164 static int 12165 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 12166 sata_drive_info_t *sdinfo) 12167 { 12168 sata_pkt_t *spkt; 12169 sata_cmd_t *scmd; 12170 sata_pkt_txlate_t *spx; 12171 int rval; 12172 12173 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12174 spx->txlt_sata_hba_inst = sata_hba_inst; 12175 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12176 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12177 if (spkt == NULL) { 12178 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12179 return (-1); 12180 } 12181 /* address is needed now */ 12182 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12183 12184 12185 /* Fill sata_pkt */ 12186 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12187 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12188 /* Synchronous mode, no callback */ 12189 spkt->satapkt_comp = NULL; 12190 /* Timeout 30s */ 12191 spkt->satapkt_time = sata_default_pkt_time; 12192 12193 scmd = &spkt->satapkt_cmd; 12194 scmd->satacmd_flags.sata_special_regs = B_TRUE; 12195 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12196 12197 /* Set up which registers need to be returned */ 12198 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 12199 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 12200 12201 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 12202 scmd->satacmd_addr_type = 0; /* N/A */ 12203 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12204 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12205 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12206 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12207 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 12208 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12209 scmd->satacmd_cmd_reg = SATAC_SMART; 12210 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12211 sdinfo->satadrv_addr.cport))); 12212 12213 12214 /* Send pkt to SATA HBA driver */ 12215 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12216 SATA_TRAN_ACCEPTED || 12217 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12218 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12219 sdinfo->satadrv_addr.cport))); 12220 /* 12221 * Whoops, no SMART RETURN STATUS 12222 */ 12223 rval = -1; 12224 } else { 12225 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12226 sdinfo->satadrv_addr.cport))); 12227 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 12228 rval = -1; 12229 goto fail; 12230 } 12231 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 12232 rval = -1; 12233 goto fail; 12234 } 12235 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 12236 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 12237 rval = 0; 12238 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 12239 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 12240 rval = 1; 12241 else { 12242 rval = -1; 12243 goto fail; 12244 } 12245 } 12246 fail: 12247 /* Free allocated resources */ 12248 sata_pkt_free(spx); 12249 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12250 12251 return (rval); 12252 } 12253 12254 /* 12255 * 12256 * Returns 0 if succeeded, -1 otherwise 12257 * 12258 * Cannot be called in an interrupt context. 12259 * 12260 */ 12261 static int 12262 sata_fetch_smart_data( 12263 sata_hba_inst_t *sata_hba_inst, 12264 sata_drive_info_t *sdinfo, 12265 struct smart_data *smart_data) 12266 { 12267 sata_pkt_t *spkt; 12268 sata_cmd_t *scmd; 12269 sata_pkt_txlate_t *spx; 12270 int rval; 12271 12272 #if ! defined(lint) 12273 ASSERT(sizeof (struct smart_data) == 512); 12274 #endif 12275 12276 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12277 spx->txlt_sata_hba_inst = sata_hba_inst; 12278 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12279 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12280 if (spkt == NULL) { 12281 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12282 return (-1); 12283 } 12284 /* address is needed now */ 12285 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12286 12287 12288 /* Fill sata_pkt */ 12289 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12290 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12291 /* Synchronous mode, no callback */ 12292 spkt->satapkt_comp = NULL; 12293 /* Timeout 30s */ 12294 spkt->satapkt_time = sata_default_pkt_time; 12295 12296 scmd = &spkt->satapkt_cmd; 12297 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12298 12299 /* 12300 * Allocate buffer for SMART data 12301 */ 12302 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12303 sizeof (struct smart_data)); 12304 if (scmd->satacmd_bp == NULL) { 12305 sata_pkt_free(spx); 12306 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12307 SATA_LOG_D((sata_hba_inst, CE_WARN, 12308 "sata_fetch_smart_data: " 12309 "cannot allocate buffer")); 12310 return (-1); 12311 } 12312 12313 12314 /* Build SMART_READ_DATA cmd in the sata_pkt */ 12315 scmd->satacmd_addr_type = 0; /* N/A */ 12316 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12317 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12318 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12319 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12320 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 12321 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12322 scmd->satacmd_cmd_reg = SATAC_SMART; 12323 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12324 sdinfo->satadrv_addr.cport))); 12325 12326 /* Send pkt to SATA HBA driver */ 12327 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12328 SATA_TRAN_ACCEPTED || 12329 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12330 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12331 sdinfo->satadrv_addr.cport))); 12332 /* 12333 * Whoops, no SMART DATA available 12334 */ 12335 rval = -1; 12336 goto fail; 12337 } else { 12338 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12339 sdinfo->satadrv_addr.cport))); 12340 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12341 DDI_DMA_SYNC_FORKERNEL); 12342 ASSERT(rval == DDI_SUCCESS); 12343 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 12344 sizeof (struct smart_data)); 12345 } 12346 12347 fail: 12348 /* Free allocated resources */ 12349 sata_free_local_buffer(spx); 12350 sata_pkt_free(spx); 12351 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12352 12353 return (rval); 12354 } 12355 12356 /* 12357 * Used by LOG SENSE page 0x10 12358 * 12359 * return 0 for success, -1 otherwise 12360 * 12361 */ 12362 static int 12363 sata_ext_smart_selftest_read_log( 12364 sata_hba_inst_t *sata_hba_inst, 12365 sata_drive_info_t *sdinfo, 12366 struct smart_ext_selftest_log *ext_selftest_log, 12367 uint16_t block_num) 12368 { 12369 sata_pkt_txlate_t *spx; 12370 sata_pkt_t *spkt; 12371 sata_cmd_t *scmd; 12372 int rval; 12373 12374 #if ! defined(lint) 12375 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 12376 #endif 12377 12378 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12379 spx->txlt_sata_hba_inst = sata_hba_inst; 12380 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12381 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12382 if (spkt == NULL) { 12383 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12384 return (-1); 12385 } 12386 /* address is needed now */ 12387 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12388 12389 12390 /* Fill sata_pkt */ 12391 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12392 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12393 /* Synchronous mode, no callback */ 12394 spkt->satapkt_comp = NULL; 12395 /* Timeout 30s */ 12396 spkt->satapkt_time = sata_default_pkt_time; 12397 12398 scmd = &spkt->satapkt_cmd; 12399 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12400 12401 /* 12402 * Allocate buffer for SMART extended self-test log 12403 */ 12404 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12405 sizeof (struct smart_ext_selftest_log)); 12406 if (scmd->satacmd_bp == NULL) { 12407 sata_pkt_free(spx); 12408 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12409 SATA_LOG_D((sata_hba_inst, CE_WARN, 12410 "sata_ext_smart_selftest_log: " 12411 "cannot allocate buffer")); 12412 return (-1); 12413 } 12414 12415 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 12416 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 12417 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 12418 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 12419 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 12420 scmd->satacmd_lba_low_msb = 0; 12421 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 12422 scmd->satacmd_lba_mid_msb = block_num >> 8; 12423 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12424 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 12425 12426 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12427 sdinfo->satadrv_addr.cport))); 12428 12429 /* Send pkt to SATA HBA driver */ 12430 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12431 SATA_TRAN_ACCEPTED || 12432 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12433 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12434 sdinfo->satadrv_addr.cport))); 12435 12436 /* 12437 * Whoops, no SMART selftest log info available 12438 */ 12439 rval = -1; 12440 goto fail; 12441 } else { 12442 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12443 sdinfo->satadrv_addr.cport))); 12444 12445 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12446 DDI_DMA_SYNC_FORKERNEL); 12447 ASSERT(rval == DDI_SUCCESS); 12448 bcopy(scmd->satacmd_bp->b_un.b_addr, 12449 (uint8_t *)ext_selftest_log, 12450 sizeof (struct smart_ext_selftest_log)); 12451 rval = 0; 12452 } 12453 12454 fail: 12455 /* Free allocated resources */ 12456 sata_free_local_buffer(spx); 12457 sata_pkt_free(spx); 12458 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12459 12460 return (rval); 12461 } 12462 12463 /* 12464 * Returns 0 for success, -1 otherwise 12465 * 12466 * SMART self-test log data is returned in buffer pointed to by selftest_log 12467 */ 12468 static int 12469 sata_smart_selftest_log( 12470 sata_hba_inst_t *sata_hba_inst, 12471 sata_drive_info_t *sdinfo, 12472 struct smart_selftest_log *selftest_log) 12473 { 12474 sata_pkt_t *spkt; 12475 sata_cmd_t *scmd; 12476 sata_pkt_txlate_t *spx; 12477 int rval; 12478 12479 #if ! defined(lint) 12480 ASSERT(sizeof (struct smart_selftest_log) == 512); 12481 #endif 12482 12483 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12484 spx->txlt_sata_hba_inst = sata_hba_inst; 12485 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12486 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12487 if (spkt == NULL) { 12488 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12489 return (-1); 12490 } 12491 /* address is needed now */ 12492 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12493 12494 12495 /* Fill sata_pkt */ 12496 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12497 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12498 /* Synchronous mode, no callback */ 12499 spkt->satapkt_comp = NULL; 12500 /* Timeout 30s */ 12501 spkt->satapkt_time = sata_default_pkt_time; 12502 12503 scmd = &spkt->satapkt_cmd; 12504 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12505 12506 /* 12507 * Allocate buffer for SMART SELFTEST LOG 12508 */ 12509 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12510 sizeof (struct smart_selftest_log)); 12511 if (scmd->satacmd_bp == NULL) { 12512 sata_pkt_free(spx); 12513 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12514 SATA_LOG_D((sata_hba_inst, CE_WARN, 12515 "sata_smart_selftest_log: " 12516 "cannot allocate buffer")); 12517 return (-1); 12518 } 12519 12520 /* Build SMART_READ_LOG cmd in the sata_pkt */ 12521 scmd->satacmd_addr_type = 0; /* N/A */ 12522 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 12523 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 12524 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12525 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12526 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 12527 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12528 scmd->satacmd_cmd_reg = SATAC_SMART; 12529 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12530 sdinfo->satadrv_addr.cport))); 12531 12532 /* Send pkt to SATA HBA driver */ 12533 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12534 SATA_TRAN_ACCEPTED || 12535 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12536 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12537 sdinfo->satadrv_addr.cport))); 12538 /* 12539 * Whoops, no SMART DATA available 12540 */ 12541 rval = -1; 12542 goto fail; 12543 } else { 12544 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12545 sdinfo->satadrv_addr.cport))); 12546 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12547 DDI_DMA_SYNC_FORKERNEL); 12548 ASSERT(rval == DDI_SUCCESS); 12549 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 12550 sizeof (struct smart_selftest_log)); 12551 rval = 0; 12552 } 12553 12554 fail: 12555 /* Free allocated resources */ 12556 sata_free_local_buffer(spx); 12557 sata_pkt_free(spx); 12558 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12559 12560 return (rval); 12561 } 12562 12563 12564 /* 12565 * Returns 0 for success, -1 otherwise 12566 * 12567 * SMART READ LOG data is returned in buffer pointed to by smart_log 12568 */ 12569 static int 12570 sata_smart_read_log( 12571 sata_hba_inst_t *sata_hba_inst, 12572 sata_drive_info_t *sdinfo, 12573 uint8_t *smart_log, /* where the data should be returned */ 12574 uint8_t which_log, /* which log should be returned */ 12575 uint8_t log_size) /* # of 512 bytes in log */ 12576 { 12577 sata_pkt_t *spkt; 12578 sata_cmd_t *scmd; 12579 sata_pkt_txlate_t *spx; 12580 int rval; 12581 12582 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12583 spx->txlt_sata_hba_inst = sata_hba_inst; 12584 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12585 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12586 if (spkt == NULL) { 12587 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12588 return (-1); 12589 } 12590 /* address is needed now */ 12591 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12592 12593 12594 /* Fill sata_pkt */ 12595 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12596 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12597 /* Synchronous mode, no callback */ 12598 spkt->satapkt_comp = NULL; 12599 /* Timeout 30s */ 12600 spkt->satapkt_time = sata_default_pkt_time; 12601 12602 scmd = &spkt->satapkt_cmd; 12603 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12604 12605 /* 12606 * Allocate buffer for SMART READ LOG 12607 */ 12608 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 12609 if (scmd->satacmd_bp == NULL) { 12610 sata_pkt_free(spx); 12611 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12612 SATA_LOG_D((sata_hba_inst, CE_WARN, 12613 "sata_smart_read_log: " "cannot allocate buffer")); 12614 return (-1); 12615 } 12616 12617 /* Build SMART_READ_LOG cmd in the sata_pkt */ 12618 scmd->satacmd_addr_type = 0; /* N/A */ 12619 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 12620 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 12621 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12622 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12623 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 12624 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12625 scmd->satacmd_cmd_reg = SATAC_SMART; 12626 12627 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12628 sdinfo->satadrv_addr.cport))); 12629 12630 /* Send pkt to SATA HBA driver */ 12631 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12632 SATA_TRAN_ACCEPTED || 12633 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12634 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12635 sdinfo->satadrv_addr.cport))); 12636 12637 /* 12638 * Whoops, no SMART DATA available 12639 */ 12640 rval = -1; 12641 goto fail; 12642 } else { 12643 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12644 sdinfo->satadrv_addr.cport))); 12645 12646 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12647 DDI_DMA_SYNC_FORKERNEL); 12648 ASSERT(rval == DDI_SUCCESS); 12649 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 12650 rval = 0; 12651 } 12652 12653 fail: 12654 /* Free allocated resources */ 12655 sata_free_local_buffer(spx); 12656 sata_pkt_free(spx); 12657 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12658 12659 return (rval); 12660 } 12661 12662 /* 12663 * Used by LOG SENSE page 0x10 12664 * 12665 * return 0 for success, -1 otherwise 12666 * 12667 */ 12668 static int 12669 sata_read_log_ext_directory( 12670 sata_hba_inst_t *sata_hba_inst, 12671 sata_drive_info_t *sdinfo, 12672 struct read_log_ext_directory *logdir) 12673 { 12674 sata_pkt_txlate_t *spx; 12675 sata_pkt_t *spkt; 12676 sata_cmd_t *scmd; 12677 int rval; 12678 12679 #if ! defined(lint) 12680 ASSERT(sizeof (struct read_log_ext_directory) == 512); 12681 #endif 12682 12683 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12684 spx->txlt_sata_hba_inst = sata_hba_inst; 12685 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12686 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12687 if (spkt == NULL) { 12688 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12689 return (-1); 12690 } 12691 12692 /* Fill sata_pkt */ 12693 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12694 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12695 /* Synchronous mode, no callback */ 12696 spkt->satapkt_comp = NULL; 12697 /* Timeout 30s */ 12698 spkt->satapkt_time = sata_default_pkt_time; 12699 12700 scmd = &spkt->satapkt_cmd; 12701 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12702 12703 /* 12704 * Allocate buffer for SMART READ LOG EXTENDED command 12705 */ 12706 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12707 sizeof (struct read_log_ext_directory)); 12708 if (scmd->satacmd_bp == NULL) { 12709 sata_pkt_free(spx); 12710 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12711 SATA_LOG_D((sata_hba_inst, CE_WARN, 12712 "sata_read_log_ext_directory: " 12713 "cannot allocate buffer")); 12714 return (-1); 12715 } 12716 12717 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 12718 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 12719 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 12720 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 12721 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 12722 scmd->satacmd_lba_low_msb = 0; 12723 scmd->satacmd_lba_mid_lsb = 0; 12724 scmd->satacmd_lba_mid_msb = 0; 12725 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12726 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 12727 12728 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12729 sdinfo->satadrv_addr.cport))); 12730 12731 /* Send pkt to SATA HBA driver */ 12732 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12733 SATA_TRAN_ACCEPTED || 12734 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12735 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12736 sdinfo->satadrv_addr.cport))); 12737 /* 12738 * Whoops, no SMART selftest log info available 12739 */ 12740 rval = -1; 12741 goto fail; 12742 } else { 12743 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12744 sdinfo->satadrv_addr.cport))); 12745 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12746 DDI_DMA_SYNC_FORKERNEL); 12747 ASSERT(rval == DDI_SUCCESS); 12748 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 12749 sizeof (struct read_log_ext_directory)); 12750 rval = 0; 12751 } 12752 12753 fail: 12754 /* Free allocated resources */ 12755 sata_free_local_buffer(spx); 12756 sata_pkt_free(spx); 12757 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12758 12759 return (rval); 12760 } 12761 12762 12763 /* 12764 * sata_xlate_errors() is used to translate (S)ATA error 12765 * information to SCSI information returned in the SCSI 12766 * packet. 12767 */ 12768 static void 12769 sata_xlate_errors(sata_pkt_txlate_t *spx) 12770 { 12771 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 12772 struct scsi_extended_sense *sense; 12773 12774 scsipkt->pkt_reason = CMD_INCOMPLETE; 12775 *scsipkt->pkt_scbp = STATUS_CHECK; 12776 sense = sata_arq_sense(spx); 12777 12778 switch (spx->txlt_sata_pkt->satapkt_reason) { 12779 case SATA_PKT_PORT_ERROR: 12780 /* 12781 * We have no device data. Assume no data transfered. 12782 */ 12783 sense->es_key = KEY_HARDWARE_ERROR; 12784 break; 12785 12786 case SATA_PKT_DEV_ERROR: 12787 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 12788 SATA_STATUS_ERR) { 12789 /* 12790 * determine dev error reason from error 12791 * reg content 12792 */ 12793 sata_decode_device_error(spx, sense); 12794 break; 12795 } 12796 /* No extended sense key - no info available */ 12797 break; 12798 12799 case SATA_PKT_TIMEOUT: 12800 /* 12801 * scsipkt->pkt_reason = CMD_TIMEOUT; This causes problems. 12802 */ 12803 scsipkt->pkt_reason = CMD_INCOMPLETE; 12804 /* No extended sense key */ 12805 break; 12806 12807 case SATA_PKT_ABORTED: 12808 scsipkt->pkt_reason = CMD_ABORTED; 12809 /* No extended sense key */ 12810 break; 12811 12812 case SATA_PKT_RESET: 12813 /* 12814 * pkt aborted either by an explicit reset request from 12815 * a host, or due to error recovery 12816 */ 12817 scsipkt->pkt_reason = CMD_RESET; 12818 break; 12819 12820 default: 12821 scsipkt->pkt_reason = CMD_TRAN_ERR; 12822 break; 12823 } 12824 } 12825 12826 12827 12828 12829 /* 12830 * Log sata message 12831 * dev pathname msg line preceeds the logged message. 12832 */ 12833 12834 static void 12835 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 12836 { 12837 char pathname[128]; 12838 dev_info_t *dip; 12839 va_list ap; 12840 12841 mutex_enter(&sata_log_mutex); 12842 12843 va_start(ap, fmt); 12844 (void) vsprintf(sata_log_buf, fmt, ap); 12845 va_end(ap); 12846 12847 if (sata_hba_inst != NULL) { 12848 dip = SATA_DIP(sata_hba_inst); 12849 (void) ddi_pathname(dip, pathname); 12850 } else { 12851 pathname[0] = 0; 12852 } 12853 if (level == CE_CONT) { 12854 if (sata_debug_flags == 0) 12855 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 12856 else 12857 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 12858 } else 12859 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 12860 12861 mutex_exit(&sata_log_mutex); 12862 } 12863 12864 12865 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 12866 12867 /* 12868 * Start or terminate the thread, depending on flag arg and current state 12869 */ 12870 static void 12871 sata_event_thread_control(int startstop) 12872 { 12873 static int sata_event_thread_terminating = 0; 12874 static int sata_event_thread_starting = 0; 12875 int i; 12876 12877 mutex_enter(&sata_event_mutex); 12878 12879 if (startstop == 0 && (sata_event_thread_starting == 1 || 12880 sata_event_thread_terminating == 1)) { 12881 mutex_exit(&sata_event_mutex); 12882 return; 12883 } 12884 if (startstop == 1 && sata_event_thread_starting == 1) { 12885 mutex_exit(&sata_event_mutex); 12886 return; 12887 } 12888 if (startstop == 1 && sata_event_thread_terminating == 1) { 12889 sata_event_thread_starting = 1; 12890 /* wait til terminate operation completes */ 12891 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 12892 while (sata_event_thread_terminating == 1) { 12893 if (i-- <= 0) { 12894 sata_event_thread_starting = 0; 12895 mutex_exit(&sata_event_mutex); 12896 #ifdef SATA_DEBUG 12897 cmn_err(CE_WARN, "sata_event_thread_control: " 12898 "timeout waiting for thread to terminate"); 12899 #endif 12900 return; 12901 } 12902 mutex_exit(&sata_event_mutex); 12903 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 12904 mutex_enter(&sata_event_mutex); 12905 } 12906 } 12907 if (startstop == 1) { 12908 if (sata_event_thread == NULL) { 12909 sata_event_thread = thread_create(NULL, 0, 12910 (void (*)())sata_event_daemon, 12911 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 12912 } 12913 sata_event_thread_starting = 0; 12914 mutex_exit(&sata_event_mutex); 12915 return; 12916 } 12917 12918 /* 12919 * If we got here, thread may need to be terminated 12920 */ 12921 if (sata_event_thread != NULL) { 12922 int i; 12923 /* Signal event thread to go away */ 12924 sata_event_thread_terminating = 1; 12925 sata_event_thread_terminate = 1; 12926 cv_signal(&sata_event_cv); 12927 /* 12928 * Wait til daemon terminates. 12929 */ 12930 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 12931 while (sata_event_thread_terminate == 1) { 12932 mutex_exit(&sata_event_mutex); 12933 if (i-- <= 0) { 12934 /* Daemon did not go away !!! */ 12935 #ifdef SATA_DEBUG 12936 cmn_err(CE_WARN, "sata_event_thread_control: " 12937 "cannot terminate event daemon thread"); 12938 #endif 12939 mutex_enter(&sata_event_mutex); 12940 break; 12941 } 12942 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 12943 mutex_enter(&sata_event_mutex); 12944 } 12945 sata_event_thread_terminating = 0; 12946 } 12947 ASSERT(sata_event_thread_terminating == 0); 12948 ASSERT(sata_event_thread_starting == 0); 12949 mutex_exit(&sata_event_mutex); 12950 } 12951 12952 12953 /* 12954 * SATA HBA event notification function. 12955 * Events reported by SATA HBA drivers per HBA instance relate to a change in 12956 * a port and/or device state or a controller itself. 12957 * Events for different addresses/addr types cannot be combined. 12958 * A warning message is generated for each event type. 12959 * Events are not processed by this function, so only the 12960 * event flag(s)is set for an affected entity and the event thread is 12961 * waken up. Event daemon thread processes all events. 12962 * 12963 * NOTE: Since more than one event may be reported at the same time, one 12964 * cannot determine a sequence of events when opposite event are reported, eg. 12965 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 12966 * is taking precedence over reported events, i.e. may cause ignoring some 12967 * events. 12968 */ 12969 #define SATA_EVENT_MAX_MSG_LENGTH 79 12970 12971 void 12972 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 12973 { 12974 sata_hba_inst_t *sata_hba_inst = NULL; 12975 sata_address_t *saddr; 12976 sata_drive_info_t *sdinfo; 12977 sata_port_stats_t *pstats; 12978 int cport, pmport; 12979 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 12980 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 12981 char *lcp; 12982 static char *err_msg_evnt_1 = 12983 "sata_hba_event_notify: invalid port event 0x%x "; 12984 static char *err_msg_evnt_2 = 12985 "sata_hba_event_notify: invalid device event 0x%x "; 12986 int linkevent; 12987 12988 /* 12989 * There is a possibility that an event will be generated on HBA 12990 * that has not completed attachment or is detaching. 12991 * HBA driver should prevent this, but just in case it does not, 12992 * we need to ignore events for such HBA. 12993 */ 12994 mutex_enter(&sata_mutex); 12995 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 12996 sata_hba_inst = sata_hba_inst->satahba_next) { 12997 if (SATA_DIP(sata_hba_inst) == dip) 12998 if (sata_hba_inst->satahba_attached == 1) 12999 break; 13000 } 13001 mutex_exit(&sata_mutex); 13002 if (sata_hba_inst == NULL) 13003 /* HBA not attached */ 13004 return; 13005 13006 ASSERT(sata_device != NULL); 13007 13008 /* 13009 * Validate address before - do not proceed with invalid address. 13010 */ 13011 saddr = &sata_device->satadev_addr; 13012 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 13013 return; 13014 if (saddr->qual == SATA_ADDR_PMPORT || 13015 saddr->qual == SATA_ADDR_DPMPORT) 13016 /* Port Multiplier not supported yet */ 13017 return; 13018 13019 cport = saddr->cport; 13020 pmport = saddr->pmport; 13021 13022 buf1[0] = buf2[0] = '\0'; 13023 13024 /* 13025 * Events refer to devices, ports and controllers - each has 13026 * unique address. Events for different addresses cannot be combined. 13027 */ 13028 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 13029 13030 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13031 13032 /* qualify this event(s) */ 13033 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 13034 /* Invalid event for the device port */ 13035 (void) sprintf(buf2, err_msg_evnt_1, 13036 event & SATA_EVNT_PORT_EVENTS); 13037 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13038 goto event_info; 13039 } 13040 if (saddr->qual == SATA_ADDR_CPORT) { 13041 /* Controller's device port event */ 13042 13043 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 13044 cport_event_flags |= 13045 event & SATA_EVNT_PORT_EVENTS; 13046 pstats = 13047 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 13048 cport_stats; 13049 } else { 13050 /* Port multiplier's device port event */ 13051 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 13052 pmport_event_flags |= 13053 event & SATA_EVNT_PORT_EVENTS; 13054 pstats = 13055 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 13056 pmport_stats; 13057 } 13058 13059 /* 13060 * Add to statistics and log the message. We have to do it 13061 * here rather than in the event daemon, because there may be 13062 * multiple events occuring before they are processed. 13063 */ 13064 linkevent = event & 13065 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 13066 if (linkevent) { 13067 if (linkevent == (SATA_EVNT_LINK_LOST | 13068 SATA_EVNT_LINK_ESTABLISHED)) { 13069 /* This is likely event combination */ 13070 (void) strlcat(buf1, "link lost/established, ", 13071 SATA_EVENT_MAX_MSG_LENGTH); 13072 13073 if (pstats->link_lost < 0xffffffffffffffffULL) 13074 pstats->link_lost++; 13075 if (pstats->link_established < 13076 0xffffffffffffffffULL) 13077 pstats->link_established++; 13078 linkevent = 0; 13079 } else if (linkevent & SATA_EVNT_LINK_LOST) { 13080 (void) strlcat(buf1, "link lost, ", 13081 SATA_EVENT_MAX_MSG_LENGTH); 13082 13083 if (pstats->link_lost < 0xffffffffffffffffULL) 13084 pstats->link_lost++; 13085 } else { 13086 (void) strlcat(buf1, "link established, ", 13087 SATA_EVENT_MAX_MSG_LENGTH); 13088 if (pstats->link_established < 13089 0xffffffffffffffffULL) 13090 pstats->link_established++; 13091 } 13092 } 13093 if (event & SATA_EVNT_DEVICE_ATTACHED) { 13094 (void) strlcat(buf1, "device attached, ", 13095 SATA_EVENT_MAX_MSG_LENGTH); 13096 if (pstats->device_attached < 0xffffffffffffffffULL) 13097 pstats->device_attached++; 13098 } 13099 if (event & SATA_EVNT_DEVICE_DETACHED) { 13100 (void) strlcat(buf1, "device detached, ", 13101 SATA_EVENT_MAX_MSG_LENGTH); 13102 if (pstats->device_detached < 0xffffffffffffffffULL) 13103 pstats->device_detached++; 13104 } 13105 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 13106 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 13107 "port %d power level changed", cport); 13108 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 13109 pstats->port_pwr_changed++; 13110 } 13111 13112 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 13113 /* There should be no other events for this address */ 13114 (void) sprintf(buf2, err_msg_evnt_1, 13115 event & ~SATA_EVNT_PORT_EVENTS); 13116 } 13117 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13118 13119 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 13120 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13121 13122 /* qualify this event */ 13123 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 13124 /* Invalid event for a device */ 13125 (void) sprintf(buf2, err_msg_evnt_2, 13126 event & SATA_EVNT_DEVICE_RESET); 13127 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13128 goto event_info; 13129 } 13130 /* drive event */ 13131 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 13132 if (sdinfo != NULL) { 13133 if (event & SATA_EVNT_DEVICE_RESET) { 13134 (void) strlcat(buf1, "device reset, ", 13135 SATA_EVENT_MAX_MSG_LENGTH); 13136 if (sdinfo->satadrv_stats.drive_reset < 13137 0xffffffffffffffffULL) 13138 sdinfo->satadrv_stats.drive_reset++; 13139 sdinfo->satadrv_event_flags |= 13140 SATA_EVNT_DEVICE_RESET; 13141 } 13142 } 13143 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 13144 /* Invalid event for a device */ 13145 (void) sprintf(buf2, err_msg_evnt_2, 13146 event & ~SATA_EVNT_DRIVE_EVENTS); 13147 } 13148 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13149 } else { 13150 if (saddr->qual != SATA_ADDR_NULL) { 13151 /* Wrong address qualifier */ 13152 SATA_LOG_D((sata_hba_inst, CE_WARN, 13153 "sata_hba_event_notify: invalid address 0x%x", 13154 *(uint32_t *)saddr)); 13155 return; 13156 } 13157 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 13158 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 13159 /* Invalid event for the controller */ 13160 SATA_LOG_D((sata_hba_inst, CE_WARN, 13161 "sata_hba_event_notify: invalid event 0x%x for " 13162 "controller", 13163 event & SATA_EVNT_CONTROLLER_EVENTS)); 13164 return; 13165 } 13166 buf1[0] = '\0'; 13167 /* This may be a frequent and not interesting event */ 13168 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 13169 "controller power level changed\n", NULL); 13170 13171 mutex_enter(&sata_hba_inst->satahba_mutex); 13172 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 13173 0xffffffffffffffffULL) 13174 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 13175 13176 sata_hba_inst->satahba_event_flags |= 13177 SATA_EVNT_PWR_LEVEL_CHANGED; 13178 mutex_exit(&sata_hba_inst->satahba_mutex); 13179 } 13180 /* 13181 * If we got here, there is something to do with this HBA 13182 * instance. 13183 */ 13184 mutex_enter(&sata_hba_inst->satahba_mutex); 13185 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 13186 mutex_exit(&sata_hba_inst->satahba_mutex); 13187 mutex_enter(&sata_mutex); 13188 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 13189 mutex_exit(&sata_mutex); 13190 13191 /* Tickle event thread */ 13192 mutex_enter(&sata_event_mutex); 13193 if (sata_event_thread_active == 0) 13194 cv_signal(&sata_event_cv); 13195 mutex_exit(&sata_event_mutex); 13196 13197 event_info: 13198 if (buf1[0] != '\0') { 13199 lcp = strrchr(buf1, ','); 13200 if (lcp != NULL) 13201 *lcp = '\0'; 13202 } 13203 if (saddr->qual == SATA_ADDR_CPORT || 13204 saddr->qual == SATA_ADDR_DCPORT) { 13205 if (buf1[0] != '\0') { 13206 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 13207 cport, buf1); 13208 } 13209 if (buf2[0] != '\0') { 13210 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 13211 cport, buf2); 13212 } 13213 } else if (saddr->qual == SATA_ADDR_PMPORT || 13214 saddr->qual == SATA_ADDR_DPMPORT) { 13215 if (buf1[0] != '\0') { 13216 sata_log(sata_hba_inst, CE_NOTE, 13217 "port %d pmport %d: %s\n", cport, pmport, buf1); 13218 } 13219 if (buf2[0] != '\0') { 13220 sata_log(sata_hba_inst, CE_NOTE, 13221 "port %d pmport %d: %s\n", cport, pmport, buf2); 13222 } 13223 } 13224 } 13225 13226 13227 /* 13228 * Event processing thread. 13229 * Arg is a pointer to the sata_hba_list pointer. 13230 * It is not really needed, because sata_hba_list is global and static 13231 */ 13232 static void 13233 sata_event_daemon(void *arg) 13234 { 13235 #ifndef __lock_lint 13236 _NOTE(ARGUNUSED(arg)) 13237 #endif 13238 sata_hba_inst_t *sata_hba_inst; 13239 clock_t lbolt; 13240 13241 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 13242 "SATA event daemon started\n", NULL); 13243 loop: 13244 /* 13245 * Process events here. Walk through all registered HBAs 13246 */ 13247 mutex_enter(&sata_mutex); 13248 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 13249 sata_hba_inst = sata_hba_inst->satahba_next) { 13250 ASSERT(sata_hba_inst != NULL); 13251 mutex_enter(&sata_hba_inst->satahba_mutex); 13252 if (sata_hba_inst->satahba_attached != 1 || 13253 (sata_hba_inst->satahba_event_flags & 13254 SATA_EVNT_SKIP) != 0) { 13255 mutex_exit(&sata_hba_inst->satahba_mutex); 13256 continue; 13257 } 13258 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 13259 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 13260 mutex_exit(&sata_hba_inst->satahba_mutex); 13261 mutex_exit(&sata_mutex); 13262 /* Got the controller with pending event */ 13263 sata_process_controller_events(sata_hba_inst); 13264 /* 13265 * Since global mutex was released, there is a 13266 * possibility that HBA list has changed, so start 13267 * over from the top. Just processed controller 13268 * will be passed-over because of the SKIP flag. 13269 */ 13270 goto loop; 13271 } 13272 mutex_exit(&sata_hba_inst->satahba_mutex); 13273 } 13274 /* Clear SKIP flag in all controllers */ 13275 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 13276 sata_hba_inst = sata_hba_inst->satahba_next) { 13277 mutex_enter(&sata_hba_inst->satahba_mutex); 13278 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 13279 mutex_exit(&sata_hba_inst->satahba_mutex); 13280 } 13281 mutex_exit(&sata_mutex); 13282 13283 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 13284 "SATA EVENT DAEMON suspending itself", NULL); 13285 13286 #ifdef SATA_DEBUG 13287 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 13288 sata_log(sata_hba_inst, CE_WARN, 13289 "SATA EVENTS PROCESSING DISABLED\n"); 13290 thread_exit(); /* Daemon will not run again */ 13291 } 13292 #endif 13293 mutex_enter(&sata_event_mutex); 13294 sata_event_thread_active = 0; 13295 mutex_exit(&sata_event_mutex); 13296 /* 13297 * Go to sleep/suspend itself and wake up either because new event or 13298 * wait timeout. Exit if there is a termination request (driver 13299 * unload). 13300 */ 13301 do { 13302 lbolt = ddi_get_lbolt(); 13303 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 13304 mutex_enter(&sata_event_mutex); 13305 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 13306 13307 if (sata_event_thread_active != 0) { 13308 mutex_exit(&sata_event_mutex); 13309 continue; 13310 } 13311 13312 /* Check if it is time to go away */ 13313 if (sata_event_thread_terminate == 1) { 13314 /* 13315 * It is up to the thread setting above flag to make 13316 * sure that this thread is not killed prematurely. 13317 */ 13318 sata_event_thread_terminate = 0; 13319 sata_event_thread = NULL; 13320 mutex_exit(&sata_event_mutex); 13321 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 13322 "SATA_EVENT_DAEMON_TERMINATING", NULL); 13323 thread_exit(); { _NOTE(NOT_REACHED) } 13324 } 13325 mutex_exit(&sata_event_mutex); 13326 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 13327 13328 mutex_enter(&sata_event_mutex); 13329 sata_event_thread_active = 1; 13330 mutex_exit(&sata_event_mutex); 13331 13332 mutex_enter(&sata_mutex); 13333 sata_event_pending &= ~SATA_EVNT_MAIN; 13334 mutex_exit(&sata_mutex); 13335 13336 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 13337 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 13338 13339 goto loop; 13340 } 13341 13342 /* 13343 * Specific HBA instance event processing. 13344 * 13345 * NOTE: At the moment, device event processing is limited to hard disks 13346 * only. 13347 * cports only are supported - no pmports. 13348 */ 13349 static void 13350 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 13351 { 13352 int ncport; 13353 uint32_t event_flags; 13354 sata_address_t *saddr; 13355 13356 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 13357 "Processing controller %d event(s)", 13358 ddi_get_instance(SATA_DIP(sata_hba_inst))); 13359 13360 mutex_enter(&sata_hba_inst->satahba_mutex); 13361 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 13362 event_flags = sata_hba_inst->satahba_event_flags; 13363 mutex_exit(&sata_hba_inst->satahba_mutex); 13364 /* 13365 * Process controller power change first 13366 * HERE 13367 */ 13368 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 13369 sata_process_cntrl_pwr_level_change(sata_hba_inst); 13370 13371 /* 13372 * Search through ports/devices to identify affected port/device. 13373 * We may have to process events for more than one port/device. 13374 */ 13375 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 13376 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 13377 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 13378 cport_event_flags; 13379 /* Check if port was locked by IOCTL processing */ 13380 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 13381 /* 13382 * We ignore port events because port is busy 13383 * with AP control processing. Set again 13384 * controller and main event flag, so that 13385 * events may be processed by the next daemon 13386 * run. 13387 */ 13388 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 13389 mutex_enter(&sata_hba_inst->satahba_mutex); 13390 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 13391 mutex_exit(&sata_hba_inst->satahba_mutex); 13392 mutex_enter(&sata_mutex); 13393 sata_event_pending |= SATA_EVNT_MAIN; 13394 mutex_exit(&sata_mutex); 13395 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 13396 "Event processing postponed until " 13397 "AP control processing completes", 13398 NULL); 13399 /* Check other ports */ 13400 continue; 13401 } else { 13402 /* 13403 * Set BSY flag so that AP control would not 13404 * interfere with events processing for 13405 * this port. 13406 */ 13407 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 13408 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 13409 } 13410 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 13411 13412 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 13413 13414 if ((event_flags & 13415 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 13416 /* 13417 * Got port event. 13418 * We need some hierarchy of event processing as they 13419 * are affecting each other: 13420 * 1. port failed 13421 * 2. device detached/attached 13422 * 3. link events - link events may trigger device 13423 * detached or device attached events in some 13424 * circumstances. 13425 * 4. port power level changed 13426 */ 13427 if (event_flags & SATA_EVNT_PORT_FAILED) { 13428 sata_process_port_failed_event(sata_hba_inst, 13429 saddr); 13430 } 13431 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 13432 sata_process_device_detached(sata_hba_inst, 13433 saddr); 13434 } 13435 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 13436 sata_process_device_attached(sata_hba_inst, 13437 saddr); 13438 } 13439 if (event_flags & 13440 (SATA_EVNT_LINK_ESTABLISHED | 13441 SATA_EVNT_LINK_LOST)) { 13442 sata_process_port_link_events(sata_hba_inst, 13443 saddr); 13444 } 13445 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 13446 sata_process_port_pwr_change(sata_hba_inst, 13447 saddr); 13448 } 13449 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 13450 sata_process_target_node_cleanup( 13451 sata_hba_inst, saddr); 13452 } 13453 } 13454 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 13455 SATA_DTYPE_NONE) { 13456 /* May have device event */ 13457 sata_process_device_reset(sata_hba_inst, saddr); 13458 } 13459 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 13460 /* Release PORT_BUSY flag */ 13461 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 13462 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 13463 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 13464 13465 } /* End of loop through the controller SATA ports */ 13466 } 13467 13468 /* 13469 * Process HBA power level change reported by HBA driver. 13470 * Not implemented at this time - event is ignored. 13471 */ 13472 static void 13473 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 13474 { 13475 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13476 "Processing controller power level change", NULL); 13477 13478 /* Ignoring it for now */ 13479 mutex_enter(&sata_hba_inst->satahba_mutex); 13480 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 13481 mutex_exit(&sata_hba_inst->satahba_mutex); 13482 } 13483 13484 /* 13485 * Process port power level change reported by HBA driver. 13486 * Not implemented at this time - event is ignored. 13487 */ 13488 static void 13489 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 13490 sata_address_t *saddr) 13491 { 13492 sata_cport_info_t *cportinfo; 13493 13494 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13495 "Processing port power level change", NULL); 13496 13497 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 13498 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13499 /* Reset event flag */ 13500 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 13501 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13502 } 13503 13504 /* 13505 * Process port failure reported by HBA driver. 13506 * cports support only - no pmports. 13507 */ 13508 static void 13509 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 13510 sata_address_t *saddr) 13511 { 13512 sata_cport_info_t *cportinfo; 13513 13514 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 13515 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13516 /* Reset event flag first */ 13517 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 13518 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 13519 if ((cportinfo->cport_state & 13520 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 13521 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13522 cport_mutex); 13523 return; 13524 } 13525 /* Fail the port */ 13526 cportinfo->cport_state = SATA_PSTATE_FAILED; 13527 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13528 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 13529 } 13530 13531 /* 13532 * Device Reset Event processing. 13533 * The seqeunce is managed by 3 stage flags: 13534 * - reset event reported, 13535 * - reset event being processed, 13536 * - request to clear device reset state. 13537 */ 13538 static void 13539 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 13540 sata_address_t *saddr) 13541 { 13542 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 13543 sata_drive_info_t *sdinfo; 13544 sata_cport_info_t *cportinfo; 13545 sata_device_t sata_device; 13546 int rval; 13547 13548 /* We only care about host sata cport for now */ 13549 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 13550 13551 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13552 13553 /* If the port is in SHUTDOWN or FAILED state, ignore reset event. */ 13554 if ((cportinfo->cport_state & 13555 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 13556 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13557 cport_mutex); 13558 return; 13559 } 13560 13561 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 13562 SATA_VALID_DEV_TYPE) == 0) { 13563 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13564 cport_mutex); 13565 return; 13566 } 13567 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 13568 if (sdinfo == NULL) { 13569 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13570 cport_mutex); 13571 return; 13572 } 13573 13574 if ((sdinfo->satadrv_event_flags & 13575 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 13576 /* Nothing to do */ 13577 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13578 cport_mutex); 13579 return; 13580 } 13581 #ifdef SATA_DEBUG 13582 if ((sdinfo->satadrv_event_flags & 13583 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 13584 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 13585 /* Something is weird - new device reset event */ 13586 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13587 "Overlapping device reset events!", NULL); 13588 } 13589 #endif 13590 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13591 "Processing port %d device reset", saddr->cport); 13592 13593 /* Clear event flag */ 13594 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 13595 13596 /* It seems that we always need to check the port state first */ 13597 sata_device.satadev_rev = SATA_DEVICE_REV; 13598 sata_device.satadev_addr = *saddr; 13599 /* 13600 * We have to exit mutex, because the HBA probe port function may 13601 * block on its own mutex. 13602 */ 13603 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13604 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13605 (SATA_DIP(sata_hba_inst), &sata_device); 13606 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13607 sata_update_port_info(sata_hba_inst, &sata_device); 13608 if (rval != SATA_SUCCESS) { 13609 /* Something went wrong? Fail the port */ 13610 cportinfo->cport_state = SATA_PSTATE_FAILED; 13611 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13612 cport_mutex); 13613 SATA_LOG_D((sata_hba_inst, CE_WARN, 13614 "SATA port %d probing failed", 13615 saddr->cport)); 13616 return; 13617 } 13618 if ((sata_device.satadev_scr.sstatus & 13619 SATA_PORT_DEVLINK_UP_MASK) != 13620 SATA_PORT_DEVLINK_UP || 13621 sata_device.satadev_type == SATA_DTYPE_NONE) { 13622 /* 13623 * No device to process, anymore. Some other event processing 13624 * would or have already performed port info cleanup. 13625 * To be safe (HBA may need it), request clearing device 13626 * reset condition. 13627 */ 13628 sdinfo->satadrv_event_flags = 0; 13629 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 13630 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13631 cport_mutex); 13632 return; 13633 } 13634 13635 /* Mark device reset processing as active */ 13636 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 13637 13638 old_sdinfo = *sdinfo; /* local copy of the drive info */ 13639 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13640 13641 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 13642 SATA_FAILURE) { 13643 /* 13644 * Restoring drive setting failed. 13645 * Probe the port first, to check if the port state has changed 13646 */ 13647 sata_device.satadev_rev = SATA_DEVICE_REV; 13648 sata_device.satadev_addr = *saddr; 13649 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 13650 /* probe port */ 13651 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13652 (SATA_DIP(sata_hba_inst), &sata_device); 13653 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13654 cport_mutex); 13655 if (rval == SATA_SUCCESS && 13656 (sata_device.satadev_state & 13657 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 13658 (sata_device.satadev_scr.sstatus & 13659 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 13660 sata_device.satadev_type != SATA_DTYPE_NONE) { 13661 /* 13662 * We may retry this a bit later - in-process reset 13663 * condition should be already set. 13664 */ 13665 if ((cportinfo->cport_dev_type & 13666 SATA_VALID_DEV_TYPE) != 0 && 13667 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 13668 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13669 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13670 saddr->cport)->cport_mutex); 13671 mutex_enter(&sata_hba_inst->satahba_mutex); 13672 sata_hba_inst->satahba_event_flags |= 13673 SATA_EVNT_MAIN; 13674 mutex_exit(&sata_hba_inst->satahba_mutex); 13675 mutex_enter(&sata_mutex); 13676 sata_event_pending |= SATA_EVNT_MAIN; 13677 mutex_exit(&sata_mutex); 13678 return; 13679 } 13680 } else { 13681 /* 13682 * No point of retrying - some other event processing 13683 * would or already did port info cleanup. 13684 * To be safe (HBA may need it), 13685 * request clearing device reset condition. 13686 */ 13687 sdinfo->satadrv_event_flags = 0; 13688 sdinfo->satadrv_event_flags |= 13689 SATA_EVNT_CLEAR_DEVICE_RESET; 13690 } 13691 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13692 cport_mutex); 13693 return; 13694 } 13695 13696 /* 13697 * Raise the flag indicating that the next sata command could 13698 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 13699 * reset is reported. 13700 */ 13701 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13702 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 && 13703 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 13704 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13705 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 13706 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 13707 } 13708 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13709 } 13710 13711 13712 /* 13713 * Port Link Events processing. 13714 * Every link established event may involve device reset (due to 13715 * COMRESET signal, equivalent of the hard reset) so arbitrarily 13716 * set device reset event for an attached device (if any). 13717 * If the port is in SHUTDOWN or FAILED state, ignore link events. 13718 * 13719 * The link established event processing varies, depending on the state 13720 * of the target node, HBA hotplugging capabilities, state of the port. 13721 * If the link is not active, the link established event is ignored. 13722 * If HBA cannot detect device attachment and there is no target node, 13723 * the link established event triggers device attach event processing. 13724 * Else, link established event triggers device reset event processing. 13725 * 13726 * The link lost event processing varies, depending on a HBA hotplugging 13727 * capability and the state of the port (link active or not active). 13728 * If the link is active, the lost link event is ignored. 13729 * If HBA cannot detect device removal, the lost link event triggers 13730 * device detached event processing after link lost timeout. 13731 * Else, the event is ignored. 13732 * 13733 * NOTE: Only cports are processed for now, i.e. no port multiplier ports 13734 */ 13735 static void 13736 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 13737 sata_address_t *saddr) 13738 { 13739 sata_device_t sata_device; 13740 sata_cport_info_t *cportinfo; 13741 sata_drive_info_t *sdinfo; 13742 uint32_t event_flags; 13743 int rval; 13744 13745 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13746 "Processing port %d link event(s)", saddr->cport); 13747 13748 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 13749 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13750 event_flags = cportinfo->cport_event_flags; 13751 13752 /* Reset event flags first */ 13753 cportinfo->cport_event_flags &= 13754 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 13755 13756 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 13757 if ((cportinfo->cport_state & 13758 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 13759 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13760 cport_mutex); 13761 return; 13762 } 13763 13764 /* 13765 * For the sanity sake get current port state. 13766 * Set device address only. Other sata_device fields should be 13767 * set by HBA driver. 13768 */ 13769 sata_device.satadev_rev = SATA_DEVICE_REV; 13770 sata_device.satadev_addr = *saddr; 13771 /* 13772 * We have to exit mutex, because the HBA probe port function may 13773 * block on its own mutex. 13774 */ 13775 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13776 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13777 (SATA_DIP(sata_hba_inst), &sata_device); 13778 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13779 sata_update_port_info(sata_hba_inst, &sata_device); 13780 if (rval != SATA_SUCCESS) { 13781 /* Something went wrong? Fail the port */ 13782 cportinfo->cport_state = SATA_PSTATE_FAILED; 13783 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13784 cport_mutex); 13785 SATA_LOG_D((sata_hba_inst, CE_WARN, 13786 "SATA port %d probing failed", 13787 saddr->cport)); 13788 /* 13789 * We may want to release device info structure, but 13790 * it is not necessary. 13791 */ 13792 return; 13793 } else { 13794 /* port probed successfully */ 13795 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 13796 } 13797 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 13798 13799 if ((sata_device.satadev_scr.sstatus & 13800 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 13801 /* Ignore event */ 13802 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13803 "Ignoring port %d link established event - " 13804 "link down", 13805 saddr->cport); 13806 goto linklost; 13807 } 13808 13809 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13810 "Processing port %d link established event", 13811 saddr->cport); 13812 13813 /* 13814 * For the sanity sake check if a device is attached - check 13815 * return state of a port probing. 13816 */ 13817 if (sata_device.satadev_type != SATA_DTYPE_NONE && 13818 sata_device.satadev_type != SATA_DTYPE_PMULT) { 13819 /* 13820 * HBA port probe indicated that there is a device 13821 * attached. Check if the framework had device info 13822 * structure attached for this device. 13823 */ 13824 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 13825 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 13826 NULL); 13827 13828 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13829 if ((sdinfo->satadrv_type & 13830 SATA_VALID_DEV_TYPE) != 0) { 13831 /* 13832 * Dev info structure is present. 13833 * If dev_type is set to known type in 13834 * the framework's drive info struct 13835 * then the device existed before and 13836 * the link was probably lost 13837 * momentarily - in such case 13838 * we may want to check device 13839 * identity. 13840 * Identity check is not supported now. 13841 * 13842 * Link established event 13843 * triggers device reset event. 13844 */ 13845 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 13846 satadrv_event_flags |= 13847 SATA_EVNT_DEVICE_RESET; 13848 } 13849 } else if (cportinfo->cport_dev_type == 13850 SATA_DTYPE_NONE) { 13851 /* 13852 * We got new device attached! If HBA does not 13853 * generate device attached events, trigger it 13854 * here. 13855 */ 13856 if (!(SATA_FEATURES(sata_hba_inst) & 13857 SATA_CTLF_HOTPLUG)) { 13858 cportinfo->cport_event_flags |= 13859 SATA_EVNT_DEVICE_ATTACHED; 13860 } 13861 } 13862 /* Reset link lost timeout */ 13863 cportinfo->cport_link_lost_time = 0; 13864 } 13865 } 13866 linklost: 13867 if (event_flags & SATA_EVNT_LINK_LOST) { 13868 if ((sata_device.satadev_scr.sstatus & 13869 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 13870 /* Ignore event */ 13871 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13872 "Ignoring port %d link lost event - link is up", 13873 saddr->cport); 13874 goto done; 13875 } 13876 #ifdef SATA_DEBUG 13877 if (cportinfo->cport_link_lost_time == 0) { 13878 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13879 "Processing port %d link lost event", 13880 saddr->cport); 13881 } 13882 #endif 13883 /* 13884 * When HBA cannot generate device attached/detached events, 13885 * we need to track link lost time and eventually generate 13886 * device detach event. 13887 */ 13888 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 13889 /* We are tracking link lost time */ 13890 if (cportinfo->cport_link_lost_time == 0) { 13891 /* save current time (lbolt value) */ 13892 cportinfo->cport_link_lost_time = 13893 ddi_get_lbolt(); 13894 /* just keep link lost event */ 13895 cportinfo->cport_event_flags |= 13896 SATA_EVNT_LINK_LOST; 13897 } else { 13898 clock_t cur_time = ddi_get_lbolt(); 13899 if ((cur_time - 13900 cportinfo->cport_link_lost_time) >= 13901 drv_usectohz( 13902 SATA_EVNT_LINK_LOST_TIMEOUT)) { 13903 /* trigger device detach event */ 13904 cportinfo->cport_event_flags |= 13905 SATA_EVNT_DEVICE_DETACHED; 13906 cportinfo->cport_link_lost_time = 0; 13907 SATADBG1(SATA_DBG_EVENTS, 13908 sata_hba_inst, 13909 "Triggering port %d " 13910 "device detached event", 13911 saddr->cport); 13912 } else { 13913 /* keep link lost event */ 13914 cportinfo->cport_event_flags |= 13915 SATA_EVNT_LINK_LOST; 13916 } 13917 } 13918 } 13919 /* 13920 * We could change port state to disable/delay access to 13921 * the attached device until the link is recovered. 13922 */ 13923 } 13924 done: 13925 event_flags = cportinfo->cport_event_flags; 13926 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13927 if (event_flags != 0) { 13928 mutex_enter(&sata_hba_inst->satahba_mutex); 13929 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 13930 mutex_exit(&sata_hba_inst->satahba_mutex); 13931 mutex_enter(&sata_mutex); 13932 sata_event_pending |= SATA_EVNT_MAIN; 13933 mutex_exit(&sata_mutex); 13934 } 13935 } 13936 13937 /* 13938 * Device Detached Event processing. 13939 * Port is probed to find if a device is really gone. If so, 13940 * the device info structure is detached from the SATA port info structure 13941 * and released. 13942 * Port status is updated. 13943 * 13944 * NOTE: Process cports event only, no port multiplier ports. 13945 */ 13946 static void 13947 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 13948 sata_address_t *saddr) 13949 { 13950 sata_cport_info_t *cportinfo; 13951 sata_drive_info_t *sdevinfo; 13952 sata_device_t sata_device; 13953 dev_info_t *tdip; 13954 int rval; 13955 13956 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 13957 "Processing port %d device detached", saddr->cport); 13958 13959 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 13960 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13961 /* Clear event flag */ 13962 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 13963 13964 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 13965 if ((cportinfo->cport_state & 13966 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 13967 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13968 cport_mutex); 13969 return; 13970 } 13971 /* For sanity, re-probe the port */ 13972 sata_device.satadev_rev = SATA_DEVICE_REV; 13973 sata_device.satadev_addr = *saddr; 13974 13975 /* 13976 * We have to exit mutex, because the HBA probe port function may 13977 * block on its own mutex. 13978 */ 13979 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13980 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13981 (SATA_DIP(sata_hba_inst), &sata_device); 13982 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 13983 sata_update_port_info(sata_hba_inst, &sata_device); 13984 if (rval != SATA_SUCCESS) { 13985 /* Something went wrong? Fail the port */ 13986 cportinfo->cport_state = SATA_PSTATE_FAILED; 13987 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 13988 cport_mutex); 13989 SATA_LOG_D((sata_hba_inst, CE_WARN, 13990 "SATA port %d probing failed", 13991 saddr->cport)); 13992 /* 13993 * We may want to release device info structure, but 13994 * it is not necessary. 13995 */ 13996 return; 13997 } else { 13998 /* port probed successfully */ 13999 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14000 } 14001 /* 14002 * Check if a device is still attached. For sanity, check also 14003 * link status - if no link, there is no device. 14004 */ 14005 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 14006 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 14007 SATA_DTYPE_NONE) { 14008 /* 14009 * Device is still attached - ignore detach event. 14010 */ 14011 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14012 cport_mutex); 14013 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14014 "Ignoring detach - device still attached to port %d", 14015 sata_device.satadev_addr.cport); 14016 return; 14017 } 14018 /* 14019 * We need to detach and release device info structure here 14020 */ 14021 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14022 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14023 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14024 (void) kmem_free((void *)sdevinfo, 14025 sizeof (sata_drive_info_t)); 14026 } 14027 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14028 /* 14029 * Device cannot be reached anymore, even if the target node may be 14030 * still present. 14031 */ 14032 14033 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14034 sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d", 14035 sata_device.satadev_addr.cport); 14036 14037 /* 14038 * Try to offline a device and remove target node if it still exists 14039 */ 14040 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 14041 if (tdip != NULL) { 14042 /* 14043 * Target node exists. Unconfigure device then remove 14044 * the target node (one ndi operation). 14045 */ 14046 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 14047 /* 14048 * PROBLEM - no device, but target node remained 14049 * This happens when the file was open or node was 14050 * waiting for resources. 14051 */ 14052 SATA_LOG_D((sata_hba_inst, CE_WARN, 14053 "sata_process_device_detached: " 14054 "Failed to remove target node for " 14055 "detached SATA device.")); 14056 /* 14057 * Set target node state to DEVI_DEVICE_REMOVED. 14058 * But re-check first that the node still exists. 14059 */ 14060 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 14061 saddr->cport); 14062 if (tdip != NULL) { 14063 sata_set_device_removed(tdip); 14064 /* 14065 * Instruct event daemon to retry the 14066 * cleanup later. 14067 */ 14068 sata_set_target_node_cleanup(sata_hba_inst, 14069 saddr->cport); 14070 } 14071 } 14072 } 14073 /* 14074 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14075 * with the hint: SE_HINT_REMOVE 14076 */ 14077 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 14078 } 14079 14080 14081 /* 14082 * Device Attached Event processing. 14083 * Port state is checked to verify that a device is really attached. If so, 14084 * the device info structure is created and attached to the SATA port info 14085 * structure. 14086 * 14087 * If attached device cannot be identified or set-up, the retry for the 14088 * attach processing is set-up. Subsequent daemon run would try again to 14089 * identify the device, until the time limit is reached 14090 * (SATA_DEV_IDENTIFY_TIMEOUT). 14091 * 14092 * This function cannot be called in interrupt context (it may sleep). 14093 * 14094 * NOTE: Process cports event only, no port multiplier ports. 14095 */ 14096 static void 14097 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 14098 sata_address_t *saddr) 14099 { 14100 sata_cport_info_t *cportinfo; 14101 sata_drive_info_t *sdevinfo; 14102 sata_device_t sata_device; 14103 dev_info_t *tdip; 14104 uint32_t event_flags; 14105 int rval; 14106 14107 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14108 "Processing port %d device attached", saddr->cport); 14109 14110 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14111 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14112 14113 /* Clear attach event flag first */ 14114 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 14115 14116 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 14117 if ((cportinfo->cport_state & 14118 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 14119 cportinfo->cport_dev_attach_time = 0; 14120 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14121 cport_mutex); 14122 return; 14123 } 14124 14125 /* 14126 * If the sata_drive_info structure is found attached to the port info, 14127 * despite the fact the device was removed and now it is re-attached, 14128 * the old drive info structure was not removed. 14129 * Arbitrarily release device info structure. 14130 */ 14131 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14132 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14133 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14134 (void) kmem_free((void *)sdevinfo, 14135 sizeof (sata_drive_info_t)); 14136 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14137 "Arbitrarily detaching old device info.", NULL); 14138 } 14139 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14140 14141 /* For sanity, re-probe the port */ 14142 sata_device.satadev_rev = SATA_DEVICE_REV; 14143 sata_device.satadev_addr = *saddr; 14144 14145 /* 14146 * We have to exit mutex, because the HBA probe port function may 14147 * block on its own mutex. 14148 */ 14149 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14150 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14151 (SATA_DIP(sata_hba_inst), &sata_device); 14152 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14153 sata_update_port_info(sata_hba_inst, &sata_device); 14154 if (rval != SATA_SUCCESS) { 14155 /* Something went wrong? Fail the port */ 14156 cportinfo->cport_state = SATA_PSTATE_FAILED; 14157 cportinfo->cport_dev_attach_time = 0; 14158 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14159 cport_mutex); 14160 SATA_LOG_D((sata_hba_inst, CE_WARN, 14161 "SATA port %d probing failed", 14162 saddr->cport)); 14163 return; 14164 } else { 14165 /* port probed successfully */ 14166 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14167 } 14168 /* 14169 * Check if a device is still attached. For sanity, check also 14170 * link status - if no link, there is no device. 14171 */ 14172 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 14173 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 14174 SATA_DTYPE_NONE) { 14175 /* 14176 * No device - ignore attach event. 14177 */ 14178 cportinfo->cport_dev_attach_time = 0; 14179 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14180 cport_mutex); 14181 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14182 "Ignoring attach - no device connected to port %d", 14183 sata_device.satadev_addr.cport); 14184 return; 14185 } 14186 14187 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14188 /* 14189 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14190 * with the hint: SE_HINT_INSERT 14191 */ 14192 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 14193 14194 /* 14195 * Port reprobing will take care of the creation of the device 14196 * info structure and determination of the device type. 14197 */ 14198 sata_device.satadev_addr = *saddr; 14199 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 14200 SATA_DEV_IDENTIFY_NORETRY); 14201 14202 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14203 cport_mutex); 14204 if ((cportinfo->cport_state & SATA_STATE_READY) && 14205 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 14206 /* Some device is attached to the port */ 14207 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 14208 /* 14209 * A device was not successfully attached. 14210 * Track retry time for device identification. 14211 */ 14212 if (cportinfo->cport_dev_attach_time != 0) { 14213 clock_t cur_time = ddi_get_lbolt(); 14214 /* 14215 * If the retry time limit was not exceeded, 14216 * reinstate attach event. 14217 */ 14218 if ((cur_time - 14219 cportinfo->cport_dev_attach_time) < 14220 drv_usectohz( 14221 SATA_DEV_IDENTIFY_TIMEOUT)) { 14222 /* OK, restore attach event */ 14223 cportinfo->cport_event_flags |= 14224 SATA_EVNT_DEVICE_ATTACHED; 14225 } else { 14226 /* Timeout - cannot identify device */ 14227 cportinfo->cport_dev_attach_time = 0; 14228 sata_log(sata_hba_inst, 14229 CE_WARN, 14230 "Cannot identify SATA device " 14231 "at port %d - device will not be " 14232 "attached.", 14233 saddr->cport); 14234 } 14235 } else { 14236 /* 14237 * Start tracking time for device 14238 * identification. 14239 * Save current time (lbolt value). 14240 */ 14241 cportinfo->cport_dev_attach_time = 14242 ddi_get_lbolt(); 14243 /* Restore attach event */ 14244 cportinfo->cport_event_flags |= 14245 SATA_EVNT_DEVICE_ATTACHED; 14246 } 14247 } else { 14248 /* 14249 * If device was successfully attached, an explicit 14250 * 'configure' command will be needed to configure it. 14251 * Log the message indicating that a device 14252 * was attached. 14253 */ 14254 cportinfo->cport_dev_attach_time = 0; 14255 sata_log(sata_hba_inst, CE_WARN, 14256 "SATA device detected at port %d", saddr->cport); 14257 14258 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14259 sata_drive_info_t new_sdinfo; 14260 14261 /* Log device info data */ 14262 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 14263 cportinfo)); 14264 sata_show_drive_info(sata_hba_inst, 14265 &new_sdinfo); 14266 } 14267 14268 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14269 saddr->cport)->cport_mutex); 14270 14271 /* 14272 * Make sure that there is no target node for that 14273 * device. If so, release it. It should not happen, 14274 * unless we had problem removing the node when 14275 * device was detached. 14276 */ 14277 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 14278 saddr->cport); 14279 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14280 saddr->cport)->cport_mutex); 14281 if (tdip != NULL) { 14282 14283 #ifdef SATA_DEBUG 14284 if ((cportinfo->cport_event_flags & 14285 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 14286 sata_log(sata_hba_inst, CE_WARN, 14287 "sata_process_device_attached: " 14288 "old device target node exists!"); 14289 #endif 14290 /* 14291 * target node exists - try to unconfigure 14292 * device and remove the node. 14293 */ 14294 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14295 saddr->cport)->cport_mutex); 14296 rval = ndi_devi_offline(tdip, 14297 NDI_DEVI_REMOVE); 14298 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14299 saddr->cport)->cport_mutex); 14300 14301 if (rval == NDI_SUCCESS) { 14302 cportinfo->cport_event_flags &= 14303 ~SATA_EVNT_TARGET_NODE_CLEANUP; 14304 cportinfo->cport_tgtnode_clean = B_TRUE; 14305 } else { 14306 /* 14307 * PROBLEM - the target node remained 14308 * and it belongs to a previously 14309 * attached device. 14310 * This happens when the file was open 14311 * or the node was waiting for 14312 * resources at the time the 14313 * associated device was removed. 14314 * Instruct event daemon to retry the 14315 * cleanup later. 14316 */ 14317 sata_log(sata_hba_inst, 14318 CE_WARN, 14319 "Application(s) accessing " 14320 "previously attached SATA " 14321 "device have to release " 14322 "it before newly inserted " 14323 "device can be made accessible.", 14324 saddr->cport); 14325 cportinfo->cport_event_flags |= 14326 SATA_EVNT_TARGET_NODE_CLEANUP; 14327 cportinfo->cport_tgtnode_clean = 14328 B_FALSE; 14329 } 14330 } 14331 14332 } 14333 } else { 14334 cportinfo->cport_dev_attach_time = 0; 14335 } 14336 14337 event_flags = cportinfo->cport_event_flags; 14338 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14339 if (event_flags != 0) { 14340 mutex_enter(&sata_hba_inst->satahba_mutex); 14341 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14342 mutex_exit(&sata_hba_inst->satahba_mutex); 14343 mutex_enter(&sata_mutex); 14344 sata_event_pending |= SATA_EVNT_MAIN; 14345 mutex_exit(&sata_mutex); 14346 } 14347 } 14348 14349 14350 /* 14351 * Device Target Node Cleanup Event processing. 14352 * If the target node associated with a sata port device is in 14353 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 14354 * If the target node cannot be removed, the event flag is left intact, 14355 * so that event daemon may re-run this function later. 14356 * 14357 * This function cannot be called in interrupt context (it may sleep). 14358 * 14359 * NOTE: Processes cport events only, not port multiplier ports. 14360 */ 14361 static void 14362 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 14363 sata_address_t *saddr) 14364 { 14365 sata_cport_info_t *cportinfo; 14366 dev_info_t *tdip; 14367 14368 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14369 "Processing port %d device target node cleanup", saddr->cport); 14370 14371 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14372 14373 /* 14374 * Check if there is target node for that device and it is in the 14375 * DEVI_DEVICE_REMOVED state. If so, release it. 14376 */ 14377 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 14378 if (tdip != NULL) { 14379 /* 14380 * target node exists - check if it is target node of 14381 * a removed device. 14382 */ 14383 if (sata_check_device_removed(tdip) == B_TRUE) { 14384 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14385 "sata_process_target_node_cleanup: " 14386 "old device target node exists!", NULL); 14387 /* 14388 * Unconfigure and remove the target node 14389 */ 14390 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 14391 NDI_SUCCESS) { 14392 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14393 saddr->cport)->cport_mutex); 14394 cportinfo->cport_event_flags &= 14395 ~SATA_EVNT_TARGET_NODE_CLEANUP; 14396 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14397 saddr->cport)->cport_mutex); 14398 return; 14399 } 14400 /* 14401 * Event daemon will retry the cleanup later. 14402 */ 14403 mutex_enter(&sata_hba_inst->satahba_mutex); 14404 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14405 mutex_exit(&sata_hba_inst->satahba_mutex); 14406 mutex_enter(&sata_mutex); 14407 sata_event_pending |= SATA_EVNT_MAIN; 14408 mutex_exit(&sata_mutex); 14409 } 14410 } else { 14411 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14412 saddr->cport)->cport_mutex); 14413 cportinfo->cport_event_flags &= 14414 ~SATA_EVNT_TARGET_NODE_CLEANUP; 14415 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14416 saddr->cport)->cport_mutex); 14417 } 14418 } 14419 14420 static void 14421 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 14422 int hint) 14423 { 14424 char ap[MAXPATHLEN]; 14425 nvlist_t *ev_attr_list = NULL; 14426 int err; 14427 14428 /* Allocate and build sysevent attribute list */ 14429 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 14430 if (err != 0) { 14431 SATA_LOG_D((sata_hba_inst, CE_WARN, 14432 "sata_gen_sysevent: " 14433 "cannot allocate memory for sysevent attributes\n")); 14434 return; 14435 } 14436 /* Add hint attribute */ 14437 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 14438 if (err != 0) { 14439 SATA_LOG_D((sata_hba_inst, CE_WARN, 14440 "sata_gen_sysevent: " 14441 "failed to add DR_HINT attr for sysevent")); 14442 nvlist_free(ev_attr_list); 14443 return; 14444 } 14445 /* 14446 * Add AP attribute. 14447 * Get controller pathname and convert it into AP pathname by adding 14448 * a target number. 14449 */ 14450 (void) snprintf(ap, MAXPATHLEN, "/devices"); 14451 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 14452 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 14453 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 14454 14455 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 14456 if (err != 0) { 14457 SATA_LOG_D((sata_hba_inst, CE_WARN, 14458 "sata_gen_sysevent: " 14459 "failed to add DR_AP_ID attr for sysevent")); 14460 nvlist_free(ev_attr_list); 14461 return; 14462 } 14463 14464 /* Generate/log sysevent */ 14465 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 14466 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 14467 if (err != DDI_SUCCESS) { 14468 SATA_LOG_D((sata_hba_inst, CE_WARN, 14469 "sata_gen_sysevent: " 14470 "cannot log sysevent, err code %x\n", err)); 14471 } 14472 14473 nvlist_free(ev_attr_list); 14474 } 14475 14476 14477 14478 14479 /* 14480 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 14481 */ 14482 static void 14483 sata_set_device_removed(dev_info_t *tdip) 14484 { 14485 int circ; 14486 14487 ASSERT(tdip != NULL); 14488 14489 ndi_devi_enter(tdip, &circ); 14490 mutex_enter(&DEVI(tdip)->devi_lock); 14491 DEVI_SET_DEVICE_REMOVED(tdip); 14492 mutex_exit(&DEVI(tdip)->devi_lock); 14493 ndi_devi_exit(tdip, circ); 14494 } 14495 14496 14497 /* 14498 * Set internal event instructing event daemon to try 14499 * to perform the target node cleanup. 14500 */ 14501 static void 14502 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, int cport) 14503 { 14504 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14505 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) |= 14506 SATA_EVNT_TARGET_NODE_CLEANUP; 14507 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean = B_FALSE; 14508 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14509 mutex_enter(&sata_hba_inst->satahba_mutex); 14510 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14511 mutex_exit(&sata_hba_inst->satahba_mutex); 14512 mutex_enter(&sata_mutex); 14513 sata_event_pending |= SATA_EVNT_MAIN; 14514 mutex_exit(&sata_mutex); 14515 } 14516 14517 14518 /* 14519 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 14520 * i.e. check if the target node state indicates that it belongs to a removed 14521 * device. 14522 * 14523 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 14524 * B_FALSE otherwise. 14525 * 14526 * NOTE: No port multiplier support. 14527 */ 14528 static boolean_t 14529 sata_check_device_removed(dev_info_t *tdip) 14530 { 14531 ASSERT(tdip != NULL); 14532 14533 if (DEVI_IS_DEVICE_REMOVED(tdip)) 14534 return (B_TRUE); 14535 else 14536 return (B_FALSE); 14537 } 14538