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 * NOTE: THIS VERSION DOES NOT SUPPORT ATAPI DEVICES, 34 * although there is some code related to these devices. 35 * 36 */ 37 #include <sys/conf.h> 38 #include <sys/file.h> 39 #include <sys/ddi.h> 40 #include <sys/sunddi.h> 41 #include <sys/modctl.h> 42 #include <sys/cmn_err.h> 43 #include <sys/errno.h> 44 #include <sys/thread.h> 45 #include <sys/kstat.h> 46 #include <sys/note.h> 47 #include <sys/sysevent.h> 48 #include <sys/sysevent/eventdefs.h> 49 #include <sys/sysevent/dr.h> 50 #include <sys/taskq.h> 51 52 #include <sys/sata/impl/sata.h> 53 #include <sys/sata/sata_hba.h> 54 #include <sys/sata/sata_defs.h> 55 #include <sys/sata/sata_cfgadm.h> 56 57 58 /* Debug flags - defined in sata.h */ 59 int sata_debug_flags = 0; 60 /* 61 * Flags enabling selected SATA HBA framework functionality 62 */ 63 #define SATA_ENABLE_QUEUING 1 64 #define SATA_ENABLE_NCQ 2 65 #define SATA_ENABLE_PROCESS_EVENTS 4 66 int sata_func_enable = 67 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 68 69 #ifdef SATA_DEBUG 70 #define SATA_LOG_D(args) sata_log args 71 uint64_t mbuf_count = 0; 72 uint64_t mbuffail_count = 0; 73 #else 74 #define SATA_LOG_D(arg) 75 #endif 76 77 78 /* 79 * SATA cb_ops functions 80 */ 81 static int sata_hba_open(dev_t *, int, int, cred_t *); 82 static int sata_hba_close(dev_t, int, int, cred_t *); 83 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 84 85 /* 86 * SCSA required entry points 87 */ 88 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 89 scsi_hba_tran_t *, struct scsi_device *); 90 static int sata_scsi_tgt_probe(struct scsi_device *, 91 int (*callback)(void)); 92 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 93 scsi_hba_tran_t *, struct scsi_device *); 94 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 95 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 96 static int sata_scsi_reset(struct scsi_address *, int); 97 static int sata_scsi_getcap(struct scsi_address *, char *, int); 98 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 99 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 100 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 101 caddr_t); 102 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 103 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 104 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 105 106 107 /* 108 * Local functions 109 */ 110 static void sata_remove_hba_instance(dev_info_t *); 111 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 112 static void sata_probe_ports(sata_hba_inst_t *); 113 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 114 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport, 115 int pmport); 116 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 117 sata_address_t *); 118 static int sata_validate_scsi_address(sata_hba_inst_t *, 119 struct scsi_address *, sata_device_t *); 120 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 121 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 122 static void sata_pkt_free(sata_pkt_txlate_t *); 123 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 124 caddr_t, ddi_dma_attr_t *); 125 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 126 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 127 sata_device_t *); 128 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 129 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 130 static void sata_free_local_buffer(sata_pkt_txlate_t *); 131 static uint64_t sata_check_capacity(sata_drive_info_t *); 132 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 133 ddi_dma_attr_t *); 134 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 135 sata_drive_info_t *); 136 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 137 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 138 static int sata_set_udma_mode(sata_hba_inst_t *, sata_drive_info_t *); 139 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 140 static int sata_set_drive_features(sata_hba_inst_t *, 141 sata_drive_info_t *, int flag); 142 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 143 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 144 145 /* Event processing functions */ 146 static void sata_event_daemon(void *); 147 static void sata_event_thread_control(int); 148 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 149 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 150 static void sata_process_port_failed_event(sata_hba_inst_t *, 151 sata_address_t *); 152 static void sata_process_port_link_events(sata_hba_inst_t *, 153 sata_address_t *); 154 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 155 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 156 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 157 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 158 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 159 sata_address_t *); 160 161 /* Local functions for ioctl */ 162 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 163 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 164 devctl_ap_state_t *); 165 static dev_info_t *sata_get_target_dip(dev_info_t *, int32_t); 166 static dev_info_t *sata_devt_to_devinfo(dev_t); 167 168 /* Local translation functions */ 169 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 170 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 171 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 172 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 173 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 174 static int sata_txlt_read(sata_pkt_txlate_t *); 175 static int sata_txlt_write(sata_pkt_txlate_t *); 176 static int sata_txlt_atapi(sata_pkt_txlate_t *); 177 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 178 static int sata_txlt_log_select(sata_pkt_txlate_t *); 179 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 180 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 181 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 182 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 183 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 184 185 static int sata_hba_start(sata_pkt_txlate_t *, int *); 186 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 187 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 188 static void sata_txlt_rw_completion(sata_pkt_t *); 189 static void sata_txlt_atapi_completion(sata_pkt_t *); 190 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 191 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 192 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 193 194 static struct scsi_extended_sense *sata_immediate_error_response( 195 sata_pkt_txlate_t *, int); 196 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 197 198 /* Local functions */ 199 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 200 uint8_t *); 201 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 202 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 203 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 204 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 205 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 206 struct mode_cache_scsi3 *, int, int *, int *, int *); 207 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 208 struct mode_info_excpt_page *, int, int *, int *, int *); 209 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 210 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 211 sata_hba_inst_t *); 212 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 213 sata_hba_inst_t *); 214 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 215 sata_hba_inst_t *); 216 static void sata_save_drive_settings(sata_drive_info_t *); 217 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 218 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 219 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 220 sata_drive_info_t *); 221 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 222 struct smart_data *); 223 static int sata_smart_selftest_log(sata_hba_inst_t *, 224 sata_drive_info_t *, 225 struct smart_selftest_log *); 226 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 227 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 228 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 229 uint8_t *, uint8_t, uint8_t); 230 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 231 struct read_log_ext_directory *); 232 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 233 static void sata_xlate_errors(sata_pkt_txlate_t *); 234 static void sata_decode_device_error(sata_pkt_txlate_t *, 235 struct scsi_extended_sense *); 236 static void sata_set_device_removed(dev_info_t *); 237 static boolean_t sata_check_device_removed(dev_info_t *); 238 static void sata_set_target_node_cleanup(sata_hba_inst_t *, int cport); 239 240 241 /* 242 * SATA Framework will ignore SATA HBA driver cb_ops structure and 243 * register following one with SCSA framework. 244 * Open & close are provided, so scsi framework will not use its own 245 */ 246 static struct cb_ops sata_cb_ops = { 247 sata_hba_open, /* open */ 248 sata_hba_close, /* close */ 249 nodev, /* strategy */ 250 nodev, /* print */ 251 nodev, /* dump */ 252 nodev, /* read */ 253 nodev, /* write */ 254 sata_hba_ioctl, /* ioctl */ 255 nodev, /* devmap */ 256 nodev, /* mmap */ 257 nodev, /* segmap */ 258 nochpoll, /* chpoll */ 259 ddi_prop_op, /* cb_prop_op */ 260 0, /* streamtab */ 261 D_NEW | D_MP, /* cb_flag */ 262 CB_REV, /* rev */ 263 nodev, /* aread */ 264 nodev /* awrite */ 265 }; 266 267 268 extern struct mod_ops mod_miscops; 269 extern uchar_t scsi_cdb_size[]; 270 271 static struct modlmisc modlmisc = { 272 &mod_miscops, /* Type of module */ 273 "SATA Module v%I%" /* module name */ 274 }; 275 276 277 static struct modlinkage modlinkage = { 278 MODREV_1, 279 (void *)&modlmisc, 280 NULL 281 }; 282 283 /* 284 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 285 * i.e. when scsi_pkt has not timeout specified. 286 */ 287 static int sata_default_pkt_time = 60; /* 60 seconds */ 288 289 /* 290 * Intermediate buffer device access attributes - they are required, 291 * but not necessarily used. 292 */ 293 static ddi_device_acc_attr_t sata_acc_attr = { 294 DDI_DEVICE_ATTR_V0, 295 DDI_STRUCTURE_LE_ACC, 296 DDI_STRICTORDER_ACC 297 }; 298 299 300 /* 301 * Mutexes protecting structures in multithreaded operations. 302 * Because events are relatively rare, a single global mutex protecting 303 * data structures should be sufficient. To increase performance, add 304 * separate mutex per each sata port and use global mutex only to protect 305 * common data structures. 306 */ 307 static kmutex_t sata_mutex; /* protects sata_hba_list */ 308 static kmutex_t sata_log_mutex; /* protects log */ 309 310 static char sata_log_buf[256]; 311 312 /* Default write cache setting */ 313 int sata_write_cache = 1; 314 315 /* 316 * Linked list of HBA instances 317 */ 318 static sata_hba_inst_t *sata_hba_list = NULL; 319 static sata_hba_inst_t *sata_hba_list_tail = NULL; 320 /* 321 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 322 * structure and in sata soft state. 323 */ 324 325 /* 326 * Event daemon related variables 327 */ 328 static kmutex_t sata_event_mutex; 329 static kcondvar_t sata_event_cv; 330 static kthread_t *sata_event_thread = NULL; 331 static int sata_event_thread_terminate = 0; 332 static int sata_event_pending = 0; 333 static int sata_event_thread_active = 0; 334 extern pri_t minclsyspri; 335 336 /* 337 * NCQ specific data 338 */ 339 static const sata_cmd_t sata_rle_cmd = { 340 SATA_CMD_REV, 341 NULL, 342 { 343 SATA_DIR_READ 344 }, 345 ATA_ADDR_LBA48, 346 0, 347 0, 348 0, 349 0, 350 0, 351 1, 352 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 353 0, 354 0, 355 0, 356 SATAC_READ_LOG_EXT, 357 0, 358 0, 359 0, 360 }; 361 362 /* Warlock directives */ 363 364 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 365 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 366 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 367 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 368 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 369 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 370 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 371 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 372 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 373 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 374 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 375 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 376 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 377 sata_hba_inst::satahba_scsi_tran)) 378 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 379 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 380 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 381 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 382 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 383 sata_hba_inst::satahba_event_flags)) 384 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 385 sata_cport_info::cport_devp)) 386 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 387 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 388 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 389 sata_cport_info::cport_dev_type)) 390 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 391 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 392 sata_cport_info::cport_state)) 393 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 394 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 395 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 396 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 397 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 398 #ifdef SATA_DEBUG 399 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 400 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 401 #endif 402 403 /* End of warlock directives */ 404 405 /* ************** loadable module configuration functions ************** */ 406 407 int 408 _init() 409 { 410 int rval; 411 412 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 413 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 414 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 415 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 416 if ((rval = mod_install(&modlinkage)) != 0) { 417 #ifdef SATA_DEBUG 418 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 419 #endif 420 mutex_destroy(&sata_log_mutex); 421 cv_destroy(&sata_event_cv); 422 mutex_destroy(&sata_event_mutex); 423 mutex_destroy(&sata_mutex); 424 } 425 return (rval); 426 } 427 428 int 429 _fini() 430 { 431 int rval; 432 433 if ((rval = mod_remove(&modlinkage)) != 0) 434 return (rval); 435 436 mutex_destroy(&sata_log_mutex); 437 cv_destroy(&sata_event_cv); 438 mutex_destroy(&sata_event_mutex); 439 mutex_destroy(&sata_mutex); 440 return (rval); 441 } 442 443 int 444 _info(struct modinfo *modinfop) 445 { 446 return (mod_info(&modlinkage, modinfop)); 447 } 448 449 450 451 /* ********************* SATA HBA entry points ********************* */ 452 453 454 /* 455 * Called by SATA HBA from _init(). 456 * Registers HBA driver instance/sata framework pair with scsi framework, by 457 * calling scsi_hba_init(). 458 * 459 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 460 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 461 * cb_ops pointer in SATA HBA driver dev_ops structure. 462 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 463 * 464 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 465 * driver. 466 */ 467 int 468 sata_hba_init(struct modlinkage *modlp) 469 { 470 int rval; 471 struct dev_ops *hba_ops; 472 473 SATADBG1(SATA_DBG_HBA_IF, NULL, 474 "sata_hba_init: name %s \n", 475 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 476 /* 477 * Fill-up cb_ops and dev_ops when necessary 478 */ 479 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 480 /* 481 * Provide pointer to SATA dev_ops 482 */ 483 hba_ops->devo_cb_ops = &sata_cb_ops; 484 485 /* 486 * Register SATA HBA with SCSI framework 487 */ 488 if ((rval = scsi_hba_init(modlp)) != 0) { 489 SATADBG1(SATA_DBG_HBA_IF, NULL, 490 "sata_hba_init: scsi hba init failed\n", NULL); 491 return (rval); 492 } 493 494 return (0); 495 } 496 497 498 /* HBA attach stages */ 499 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 500 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 501 #define HBA_ATTACH_STAGE_SETUP 4 502 #define HBA_ATTACH_STAGE_LINKED 8 503 504 505 /* 506 * 507 * Called from SATA HBA driver's attach routine to attach an instance of 508 * the HBA. 509 * 510 * For DDI_ATTACH command: 511 * sata_hba_inst structure is allocated here and initialized with pointers to 512 * SATA framework implementation of required scsi tran functions. 513 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 514 * to the soft structure (sata_hba_inst) allocated by SATA framework for 515 * SATA HBA instance related data. 516 * The scsi_tran's tran_hba_private field is used by SATA framework to 517 * store a pointer to per-HBA-instance of sata_hba_inst structure. 518 * The sata_hba_inst structure is cross-linked to scsi tran structure. 519 * Among other info, a pointer to sata_hba_tran structure is stored in 520 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 521 * linked together into the list, pointed to by sata_hba_list. 522 * On the first HBA instance attach the sata event thread is initialized. 523 * Attachment points are created for all SATA ports of the HBA being attached. 524 * All HBA instance's SATA ports are probed and type of plugged devices is 525 * determined. For each device of a supported type, a target node is created. 526 * 527 * DDI_SUCCESS is returned when attachment process is successful, 528 * DDI_FAILURE is returned otherwise. 529 * 530 * For DDI_RESUME command: 531 * Not implemented at this time (postponed until phase 2 of the development). 532 */ 533 int 534 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 535 ddi_attach_cmd_t cmd) 536 { 537 sata_hba_inst_t *sata_hba_inst; 538 scsi_hba_tran_t *scsi_tran = NULL; 539 int hba_attach_state = 0; 540 char taskq_name[MAXPATHLEN]; 541 542 SATADBG3(SATA_DBG_HBA_IF, NULL, 543 "sata_hba_attach: node %s (%s%d)\n", 544 ddi_node_name(dip), ddi_driver_name(dip), 545 ddi_get_instance(dip)); 546 547 if (cmd == DDI_RESUME) { 548 /* 549 * Postponed until phase 2 of the development 550 */ 551 return (DDI_FAILURE); 552 } 553 554 if (cmd != DDI_ATTACH) { 555 return (DDI_FAILURE); 556 } 557 558 /* cmd == DDI_ATTACH */ 559 560 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 561 SATA_LOG_D((NULL, CE_WARN, 562 "sata_hba_attach: invalid sata_hba_tran")); 563 return (DDI_FAILURE); 564 } 565 /* 566 * Allocate and initialize SCSI tran structure. 567 * SATA copy of tran_bus_config is provided to create port nodes. 568 */ 569 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 570 if (scsi_tran == NULL) 571 return (DDI_FAILURE); 572 /* 573 * Allocate soft structure for SATA HBA instance. 574 * There is a separate softstate for each HBA instance. 575 */ 576 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 577 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 578 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 579 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 580 581 /* 582 * scsi_trans's tran_hba_private is used by SATA Framework to point to 583 * soft structure allocated by SATA framework for 584 * SATA HBA instance related data. 585 */ 586 scsi_tran->tran_hba_private = sata_hba_inst; 587 scsi_tran->tran_tgt_private = NULL; 588 589 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 590 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 591 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 592 593 scsi_tran->tran_start = sata_scsi_start; 594 scsi_tran->tran_reset = sata_scsi_reset; 595 scsi_tran->tran_abort = sata_scsi_abort; 596 scsi_tran->tran_getcap = sata_scsi_getcap; 597 scsi_tran->tran_setcap = sata_scsi_setcap; 598 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 599 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 600 601 scsi_tran->tran_dmafree = sata_scsi_dmafree; 602 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 603 604 scsi_tran->tran_reset_notify = NULL; 605 scsi_tran->tran_get_bus_addr = NULL; 606 scsi_tran->tran_quiesce = NULL; 607 scsi_tran->tran_unquiesce = NULL; 608 scsi_tran->tran_bus_reset = NULL; 609 610 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 611 scsi_tran, 0) != DDI_SUCCESS) { 612 #ifdef SATA_DEBUG 613 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 614 ddi_driver_name(dip), ddi_get_instance(dip)); 615 #endif 616 goto fail; 617 } 618 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 619 620 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 621 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 622 "sata", 1) != DDI_PROP_SUCCESS) { 623 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 624 "failed to create hba sata prop")); 625 goto fail; 626 } 627 } 628 629 /* 630 * Save pointers in hba instance soft state. 631 */ 632 sata_hba_inst->satahba_scsi_tran = scsi_tran; 633 sata_hba_inst->satahba_tran = sata_tran; 634 sata_hba_inst->satahba_dip = dip; 635 636 /* 637 * Create a task queue to handle emulated commands completion 638 * Use node name, dash, instance number as the queue name. 639 */ 640 taskq_name[0] = '\0'; 641 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 642 sizeof (taskq_name)); 643 (void) snprintf(taskq_name + strlen(taskq_name), 644 sizeof (taskq_name) - strlen(taskq_name), 645 "-%d", DEVI(dip)->devi_instance); 646 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 647 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports, 648 TASKQ_DYNAMIC); 649 650 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 651 652 /* 653 * Create events thread if not created yet. 654 */ 655 sata_event_thread_control(1); 656 657 /* 658 * Link this hba instance into the list. 659 */ 660 mutex_enter(&sata_mutex); 661 662 663 sata_hba_inst->satahba_next = NULL; 664 sata_hba_inst->satahba_prev = sata_hba_list_tail; 665 if (sata_hba_list == NULL) { 666 sata_hba_list = sata_hba_inst; 667 } 668 if (sata_hba_list_tail != NULL) { 669 sata_hba_list_tail->satahba_next = sata_hba_inst; 670 } 671 sata_hba_list_tail = sata_hba_inst; 672 mutex_exit(&sata_mutex); 673 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 674 675 /* 676 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 677 * SATA HBA driver should not use its own open/close entry points. 678 * 679 * Make sure that instance number doesn't overflow 680 * when forming minor numbers. 681 */ 682 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 683 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 684 INST2DEVCTL(ddi_get_instance(dip)), 685 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 686 #ifdef SATA_DEBUG 687 cmn_err(CE_WARN, "sata_hba_attach: " 688 "cannot create devctl minor node"); 689 #endif 690 goto fail; 691 } 692 693 694 /* 695 * Set-up kstats here, if necessary. 696 * (postponed until phase 2 of the development). 697 */ 698 699 700 /* 701 * Probe controller ports. This operation will describe a current 702 * controller/port/multipliers/device configuration and will create 703 * attachment points. 704 * We may end-up with just a controller with no devices attached. 705 * For the ports with a supported device attached, device target nodes 706 * are created and devices are initialized. 707 */ 708 sata_probe_ports(sata_hba_inst); 709 710 sata_hba_inst->satahba_attached = 1; 711 return (DDI_SUCCESS); 712 713 fail: 714 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 715 (void) sata_remove_hba_instance(dip); 716 if (sata_hba_list == NULL) 717 sata_event_thread_control(0); 718 } 719 720 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 721 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 722 taskq_destroy(sata_hba_inst->satahba_taskq); 723 } 724 725 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 726 (void) scsi_hba_detach(dip); 727 728 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 729 mutex_destroy(&sata_hba_inst->satahba_mutex); 730 kmem_free((void *)sata_hba_inst, 731 sizeof (struct sata_hba_inst)); 732 scsi_hba_tran_free(scsi_tran); 733 } 734 735 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 736 ddi_driver_name(dip), ddi_get_instance(dip)); 737 738 return (DDI_FAILURE); 739 } 740 741 742 /* 743 * Called by SATA HBA from to detach an instance of the driver. 744 * 745 * For DDI_DETACH command: 746 * Free local structures allocated for SATA HBA instance during 747 * sata_hba_attach processing. 748 * 749 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 750 * 751 * For DDI_SUSPEND command: 752 * Not implemented at this time (postponed until phase 2 of the development) 753 * Returnd DDI_SUCCESS. 754 * 755 * When the last HBA instance is detached, the event daemon is terminated. 756 * 757 * NOTE: cport support only, no port multiplier support. 758 */ 759 int 760 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 761 { 762 dev_info_t *tdip; 763 sata_hba_inst_t *sata_hba_inst; 764 scsi_hba_tran_t *scsi_hba_tran; 765 sata_cport_info_t *cportinfo; 766 sata_drive_info_t *sdinfo; 767 int ncport; 768 769 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 770 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 771 772 switch (cmd) { 773 case DDI_DETACH: 774 775 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 776 return (DDI_FAILURE); 777 778 sata_hba_inst = scsi_hba_tran->tran_hba_private; 779 if (sata_hba_inst == NULL) 780 return (DDI_FAILURE); 781 782 if (scsi_hba_detach(dip) == DDI_FAILURE) { 783 sata_hba_inst->satahba_attached = 1; 784 return (DDI_FAILURE); 785 } 786 787 /* 788 * Free all target nodes - at this point 789 * devices should be at least offlined 790 * otherwise scsi_hba_detach() should not be called. 791 */ 792 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 793 ncport++) { 794 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 795 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 796 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 797 if (sdinfo != NULL) { 798 tdip = sata_get_target_dip(dip, 799 ncport); 800 if (tdip != NULL) { 801 if (ndi_devi_offline(tdip, 802 NDI_DEVI_REMOVE) != 803 NDI_SUCCESS) { 804 SATA_LOG_D(( 805 sata_hba_inst, 806 CE_WARN, 807 "sata_hba_detach: " 808 "Target node not " 809 "removed !")); 810 return (DDI_FAILURE); 811 } 812 } 813 } 814 } 815 } 816 /* 817 * Disable sata event daemon processing for this HBA 818 */ 819 sata_hba_inst->satahba_attached = 0; 820 821 /* 822 * Remove event daemon thread, if it is last HBA instance. 823 */ 824 825 mutex_enter(&sata_mutex); 826 if (sata_hba_list->satahba_next == NULL) { 827 mutex_exit(&sata_mutex); 828 sata_event_thread_control(0); 829 mutex_enter(&sata_mutex); 830 } 831 mutex_exit(&sata_mutex); 832 833 /* Remove this HBA instance from the HBA list */ 834 sata_remove_hba_instance(dip); 835 836 /* 837 * At this point there should be no target nodes attached. 838 * Detach and destroy device and port info structures. 839 */ 840 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 841 ncport++) { 842 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 843 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 844 sdinfo = 845 cportinfo->cport_devp.cport_sata_drive; 846 if (sdinfo != NULL) { 847 /* Release device structure */ 848 kmem_free(sdinfo, 849 sizeof (sata_drive_info_t)); 850 } 851 /* Release cport info */ 852 mutex_destroy(&cportinfo->cport_mutex); 853 kmem_free(cportinfo, 854 sizeof (sata_cport_info_t)); 855 } 856 } 857 858 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 859 860 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 861 862 taskq_destroy(sata_hba_inst->satahba_taskq); 863 864 mutex_destroy(&sata_hba_inst->satahba_mutex); 865 kmem_free((void *)sata_hba_inst, 866 sizeof (struct sata_hba_inst)); 867 868 return (DDI_SUCCESS); 869 870 case DDI_SUSPEND: 871 /* 872 * Postponed until phase 2 873 */ 874 return (DDI_FAILURE); 875 876 default: 877 return (DDI_FAILURE); 878 } 879 } 880 881 882 /* 883 * Called by an HBA drive from _fini() routine. 884 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 885 */ 886 void 887 sata_hba_fini(struct modlinkage *modlp) 888 { 889 SATADBG1(SATA_DBG_HBA_IF, NULL, 890 "sata_hba_fini: name %s\n", 891 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 892 893 scsi_hba_fini(modlp); 894 } 895 896 897 /* 898 * Default open and close routine for sata_hba framework. 899 * 900 */ 901 /* 902 * Open devctl node. 903 * 904 * Returns: 905 * 0 if node was open successfully, error code otherwise. 906 * 907 * 908 */ 909 910 static int 911 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 912 { 913 #ifndef __lock_lint 914 _NOTE(ARGUNUSED(credp)) 915 #endif 916 int rv = 0; 917 dev_info_t *dip; 918 scsi_hba_tran_t *scsi_hba_tran; 919 sata_hba_inst_t *sata_hba_inst; 920 921 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 922 923 if (otyp != OTYP_CHR) 924 return (EINVAL); 925 926 dip = sata_devt_to_devinfo(*devp); 927 if (dip == NULL) 928 return (ENXIO); 929 930 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 931 return (ENXIO); 932 933 sata_hba_inst = scsi_hba_tran->tran_hba_private; 934 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 935 return (ENXIO); 936 937 mutex_enter(&sata_mutex); 938 if (flags & FEXCL) { 939 if (sata_hba_inst->satahba_open_flag != 0) { 940 rv = EBUSY; 941 } else { 942 sata_hba_inst->satahba_open_flag = 943 SATA_DEVCTL_EXOPENED; 944 } 945 } else { 946 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 947 rv = EBUSY; 948 } else { 949 sata_hba_inst->satahba_open_flag = 950 SATA_DEVCTL_SOPENED; 951 } 952 } 953 mutex_exit(&sata_mutex); 954 955 return (rv); 956 } 957 958 959 /* 960 * Close devctl node. 961 * Returns: 962 * 0 if node was closed successfully, error code otherwise. 963 * 964 */ 965 966 static int 967 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 968 { 969 #ifndef __lock_lint 970 _NOTE(ARGUNUSED(credp)) 971 _NOTE(ARGUNUSED(flag)) 972 #endif 973 dev_info_t *dip; 974 scsi_hba_tran_t *scsi_hba_tran; 975 sata_hba_inst_t *sata_hba_inst; 976 977 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 978 979 if (otyp != OTYP_CHR) 980 return (EINVAL); 981 982 dip = sata_devt_to_devinfo(dev); 983 if (dip == NULL) 984 return (ENXIO); 985 986 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 987 return (ENXIO); 988 989 sata_hba_inst = scsi_hba_tran->tran_hba_private; 990 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 991 return (ENXIO); 992 993 mutex_enter(&sata_mutex); 994 sata_hba_inst->satahba_open_flag = 0; 995 mutex_exit(&sata_mutex); 996 return (0); 997 } 998 999 1000 1001 /* 1002 * Standard IOCTL commands for SATA hotplugging. 1003 * Implemented DEVCTL_AP commands: 1004 * DEVCTL_AP_CONNECT 1005 * DEVCTL_AP_DISCONNECT 1006 * DEVCTL_AP_CONFIGURE 1007 * DEVCTL_UNCONFIGURE 1008 * DEVCTL_AP_CONTROL 1009 * 1010 * Commands passed to default ndi ioctl handler: 1011 * DEVCTL_DEVICE_GETSTATE 1012 * DEVCTL_DEVICE_ONLINE 1013 * DEVCTL_DEVICE_OFFLINE 1014 * DEVCTL_DEVICE_REMOVE 1015 * DEVCTL_DEVICE_INSERT 1016 * DEVCTL_BUS_GETSTATE 1017 * 1018 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1019 * if not. 1020 * 1021 * Returns: 1022 * 0 if successful, 1023 * error code if operation failed. 1024 * 1025 * NOTE: Port Multiplier is not supported. 1026 * 1027 */ 1028 1029 static int 1030 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1031 int *rvalp) 1032 { 1033 #ifndef __lock_lint 1034 _NOTE(ARGUNUSED(credp)) 1035 _NOTE(ARGUNUSED(rvalp)) 1036 #endif 1037 int rv = 0; 1038 int32_t comp_port = -1; 1039 dev_info_t *dip, *tdip; 1040 devctl_ap_state_t ap_state; 1041 struct devctl_iocdata *dcp = NULL; 1042 scsi_hba_tran_t *scsi_hba_tran; 1043 sata_hba_inst_t *sata_hba_inst; 1044 sata_device_t sata_device; 1045 sata_drive_info_t *sdinfo; 1046 sata_cport_info_t *cportinfo; 1047 int cport, pmport, qual; 1048 int rval = SATA_SUCCESS; 1049 1050 dip = sata_devt_to_devinfo(dev); 1051 if (dip == NULL) 1052 return (ENXIO); 1053 1054 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1055 return (ENXIO); 1056 1057 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1058 if (sata_hba_inst == NULL) 1059 return (ENXIO); 1060 1061 if (sata_hba_inst->satahba_tran == NULL) 1062 return (ENXIO); 1063 1064 switch (cmd) { 1065 1066 case DEVCTL_DEVICE_GETSTATE: 1067 case DEVCTL_DEVICE_ONLINE: 1068 case DEVCTL_DEVICE_OFFLINE: 1069 case DEVCTL_DEVICE_REMOVE: 1070 case DEVCTL_BUS_GETSTATE: 1071 /* 1072 * There may be more cases that we want to pass to default 1073 * handler rather then fail them. 1074 */ 1075 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1076 } 1077 1078 /* read devctl ioctl data */ 1079 if (cmd != DEVCTL_AP_CONTROL) { 1080 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1081 return (EFAULT); 1082 1083 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1084 -1) { 1085 if (dcp) 1086 ndi_dc_freehdl(dcp); 1087 return (EINVAL); 1088 } 1089 1090 cport = SCSI_TO_SATA_CPORT(comp_port); 1091 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1092 /* Only cport is considered now, i.e. SATA_ADDR_CPORT */ 1093 qual = SATA_ADDR_CPORT; 1094 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1095 qual) != 0) { 1096 ndi_dc_freehdl(dcp); 1097 return (EINVAL); 1098 } 1099 1100 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1101 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1102 cport_mutex); 1103 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1104 /* 1105 * Cannot process ioctl request now. Come back later. 1106 */ 1107 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1108 cport_mutex); 1109 ndi_dc_freehdl(dcp); 1110 return (EBUSY); 1111 } 1112 /* Block event processing for this port */ 1113 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1114 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1115 1116 sata_device.satadev_addr.cport = cport; 1117 sata_device.satadev_addr.pmport = pmport; 1118 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1119 sata_device.satadev_rev = SATA_DEVICE_REV; 1120 } 1121 1122 switch (cmd) { 1123 1124 case DEVCTL_AP_DISCONNECT: 1125 /* 1126 * Normally, cfgadm sata plugin will try to offline 1127 * (unconfigure) device before this request. Nevertheless, 1128 * if a device is still configured, we need to 1129 * attempt to offline and unconfigure device first, and we will 1130 * deactivate the port regardless of the unconfigure 1131 * operation results. 1132 * 1133 * DEVCTL_AP_DISCONNECT invokes 1134 * sata_hba_inst->satahba_tran-> 1135 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 1136 * If successful, the device structure (if any) attached 1137 * to a port is removed and state of the port marked 1138 * appropriately. 1139 * Failure of the port_deactivate may keep port in 1140 * the active state, or may fail the port. 1141 */ 1142 1143 /* Check the current state of the port */ 1144 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 1145 (dip, &sata_device); 1146 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1147 cport_mutex); 1148 sata_update_port_info(sata_hba_inst, &sata_device); 1149 if (rval != SATA_SUCCESS || 1150 (sata_device.satadev_state & SATA_PSTATE_FAILED)) { 1151 cportinfo->cport_state = SATA_PSTATE_FAILED; 1152 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1153 cport_mutex); 1154 rv = EIO; 1155 break; 1156 } 1157 /* Sanity check */ 1158 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 1159 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1160 cport_mutex); 1161 /* No physical port deactivation supported. */ 1162 break; 1163 } 1164 1165 /* 1166 * set port's dev_state to not ready - this will disable 1167 * an access to an attached device. 1168 */ 1169 cportinfo->cport_state &= ~SATA_STATE_READY; 1170 1171 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1172 sdinfo = cportinfo->cport_devp.cport_sata_drive; 1173 ASSERT(sdinfo != NULL); 1174 if ((sdinfo->satadrv_type & 1175 (SATA_VALID_DEV_TYPE))) { 1176 /* 1177 * If a target node exists, try to offline 1178 * a device and remove target node. 1179 */ 1180 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1181 cport)->cport_mutex); 1182 tdip = sata_get_target_dip(dip, comp_port); 1183 if (tdip != NULL && ndi_devi_offline(tdip, 1184 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 1185 /* 1186 * Problem 1187 * A target node remained 1188 * attached. This happens when 1189 * the file was open or a node 1190 * was waiting for resources. 1191 * Cannot do anything about it. 1192 */ 1193 SATA_LOG_D((sata_hba_inst, CE_WARN, 1194 "sata_hba_ioctl: " 1195 "disconnect: could not " 1196 "unconfigure device before " 1197 "disconnecting the SATA " 1198 "port %d", cport)); 1199 1200 /* 1201 * Set DEVICE REMOVED state 1202 * in the target node. It 1203 * will prevent access to 1204 * the device even when a 1205 * new device is attached, 1206 * until the old target node 1207 * is released, removed and 1208 * recreated for a new 1209 * device. 1210 */ 1211 sata_set_device_removed(tdip); 1212 /* 1213 * Instruct event daemon to 1214 * try the target node cleanup 1215 * later. 1216 */ 1217 sata_set_target_node_cleanup( 1218 sata_hba_inst, cport); 1219 } 1220 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1221 cport)->cport_mutex); 1222 /* 1223 * Remove and release sata_drive_info 1224 * structure. 1225 */ 1226 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != 1227 NULL) { 1228 SATA_CPORTINFO_DRV_INFO(cportinfo) = 1229 NULL; 1230 (void) kmem_free((void *)sdinfo, 1231 sizeof (sata_drive_info_t)); 1232 cportinfo->cport_dev_type = 1233 SATA_DTYPE_NONE; 1234 } 1235 } 1236 /* 1237 * Note: PMult info requires different handling. 1238 * Put PMult handling code here, when PMult is 1239 * supported. 1240 */ 1241 1242 } 1243 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1244 /* Just ask HBA driver to deactivate port */ 1245 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1246 1247 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 1248 (dip, &sata_device); 1249 1250 /* 1251 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1252 * without the hint. 1253 */ 1254 sata_gen_sysevent(sata_hba_inst, 1255 &sata_device.satadev_addr, SE_NO_HINT); 1256 1257 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1258 cport_mutex); 1259 sata_update_port_info(sata_hba_inst, &sata_device); 1260 1261 if (rval != SATA_SUCCESS) { 1262 /* 1263 * Port deactivation failure - do not 1264 * change port state unless the state 1265 * returned by HBA indicates a port failure. 1266 */ 1267 if (sata_device.satadev_state & SATA_PSTATE_FAILED) 1268 cportinfo->cport_state = SATA_PSTATE_FAILED; 1269 rv = EIO; 1270 } else { 1271 /* 1272 * Deactivation succeded. From now on the framework 1273 * will not know what is happening to the device, until 1274 * the port is activated again. 1275 */ 1276 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 1277 } 1278 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1279 break; 1280 1281 case DEVCTL_AP_UNCONFIGURE: 1282 1283 /* 1284 * The unconfigure operation uses generic nexus operation to 1285 * offline a device. It leaves a target device node attached. 1286 * and obviously sata_drive_info attached as well, because 1287 * from the hardware point of view nothing has changed. 1288 */ 1289 if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) { 1290 1291 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != 1292 NDI_SUCCESS) { 1293 SATA_LOG_D((sata_hba_inst, CE_WARN, 1294 "sata_hba_ioctl: unconfigure: " 1295 "failed to unconfigure " 1296 "device at SATA port %d", cport)); 1297 rv = EIO; 1298 } 1299 /* 1300 * The target node devi_state should be marked with 1301 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 1302 * This would be the indication for cfgadm that 1303 * the AP node occupant state is 'unconfigured'. 1304 */ 1305 1306 } else { 1307 /* 1308 * This would indicate a failure on the part of cfgadm 1309 * to detect correct state of the node prior to this 1310 * call - one cannot unconfigure non-existing device. 1311 */ 1312 SATA_LOG_D((sata_hba_inst, CE_WARN, 1313 "sata_hba_ioctl: unconfigure: " 1314 "attempt to unconfigure non-existing device " 1315 "at SATA port %d", cport)); 1316 rv = ENXIO; 1317 } 1318 1319 break; 1320 1321 case DEVCTL_AP_CONNECT: 1322 { 1323 /* 1324 * The sata cfgadm pluging will invoke this operation only if 1325 * port was found in the disconnect state (failed state 1326 * is also treated as the disconnected state). 1327 * DEVCTL_AP_CONNECT would invoke 1328 * sata_hba_inst->satahba_tran-> 1329 * sata_tran_hotplug_ops->sata_tran_port_activate(). 1330 * If successful and a device is found attached to the port, 1331 * the initialization sequence is executed to attach 1332 * a device structure to a port structure. The device is not 1333 * set in configured state (system-wise) by this operation. 1334 * The state of the port and a device would be set 1335 * appropriately. 1336 * 1337 * Note, that activating the port may generate link events, 1338 * so is is important that following processing and the 1339 * event processing does not interfere with each other! 1340 * 1341 * This operation may remove port failed state and will 1342 * try to make port active and in good standing. 1343 */ 1344 1345 /* We only care about host sata cport for now */ 1346 1347 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) { 1348 /* Just let HBA driver to activate port */ 1349 1350 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 1351 (dip, &sata_device) != SATA_SUCCESS) { 1352 /* 1353 * Port activation failure. 1354 */ 1355 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1356 cport)->cport_mutex); 1357 sata_update_port_info(sata_hba_inst, 1358 &sata_device); 1359 if (sata_device.satadev_state & 1360 SATA_PSTATE_FAILED) { 1361 cportinfo->cport_state = 1362 SATA_PSTATE_FAILED; 1363 } 1364 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1365 cport)->cport_mutex); 1366 SATA_LOG_D((sata_hba_inst, CE_WARN, 1367 "sata_hba_ioctl: connect: " 1368 "failed to activate SATA port %d", 1369 cport)); 1370 rv = EIO; 1371 break; 1372 } 1373 } 1374 /* Virgin port state - will be updated by the port re-probe. */ 1375 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1376 cport)->cport_mutex); 1377 cportinfo->cport_state = 0; 1378 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1379 cport)->cport_mutex); 1380 1381 /* 1382 * Probe the port to find its state and attached device. 1383 */ 1384 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1385 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 1386 rv = EIO; 1387 /* 1388 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1389 * without the hint 1390 */ 1391 sata_gen_sysevent(sata_hba_inst, 1392 &sata_device.satadev_addr, SE_NO_HINT); 1393 /* 1394 * If there is a device attached to the port, emit 1395 * a message. 1396 */ 1397 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1398 sata_log(sata_hba_inst, CE_WARN, 1399 "SATA device detected at port %d", cport); 1400 } 1401 break; 1402 } 1403 1404 case DEVCTL_AP_CONFIGURE: 1405 { 1406 boolean_t target = TRUE; 1407 1408 /* 1409 * A port may be in an active or shutdown state. 1410 * If port is in a failed state, operation is aborted - one 1411 * has to use explicit connect or port activate request 1412 * to try to get a port into non-failed mode. 1413 * 1414 * If a port is in a shutdown state, arbitrarily invoke 1415 * sata_tran_port_activate() prior to any other operation. 1416 * 1417 * Verify that port state is READY and there is a device 1418 * of a supported type attached to this port. 1419 * If target node exists, a device was most likely offlined. 1420 * If target node does not exist, create a target node an 1421 * attempt to online it. 1422 * * 1423 * NO PMult or devices beyond PMult are supported yet. 1424 */ 1425 1426 /* We only care about host controller's sata cport for now. */ 1427 if (cportinfo->cport_state & SATA_PSTATE_FAILED) { 1428 rv = ENXIO; 1429 break; 1430 } 1431 /* Check the current state of the port */ 1432 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1433 1434 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 1435 (dip, &sata_device); 1436 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1437 cport_mutex); 1438 sata_update_port_info(sata_hba_inst, &sata_device); 1439 if (rval != SATA_SUCCESS || 1440 (sata_device.satadev_state & SATA_PSTATE_FAILED)) { 1441 cportinfo->cport_state = SATA_PSTATE_FAILED; 1442 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1443 cport_mutex); 1444 rv = EIO; 1445 break; 1446 } 1447 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN) { 1448 target = FALSE; 1449 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1450 cport_mutex); 1451 1452 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) { 1453 /* Just let HBA driver to activate port */ 1454 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 1455 (dip, &sata_device) != SATA_SUCCESS) { 1456 /* 1457 * Port activation failure - do not 1458 * change port state unless the state 1459 * returned by HBA indicates a port 1460 * failure. 1461 */ 1462 mutex_enter(&SATA_CPORT_INFO( 1463 sata_hba_inst, cport)->cport_mutex); 1464 sata_update_port_info(sata_hba_inst, 1465 &sata_device); 1466 if (sata_device.satadev_state & 1467 SATA_PSTATE_FAILED) { 1468 cportinfo->cport_state = 1469 SATA_PSTATE_FAILED; 1470 } 1471 mutex_exit(&SATA_CPORT_INFO( 1472 sata_hba_inst, cport)->cport_mutex); 1473 SATA_LOG_D((sata_hba_inst, CE_WARN, 1474 "sata_hba_ioctl: configure: " 1475 "failed to activate SATA port %d", 1476 cport)); 1477 rv = EIO; 1478 break; 1479 } 1480 } 1481 /* 1482 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 1483 * without the hint. 1484 */ 1485 sata_gen_sysevent(sata_hba_inst, 1486 &sata_device.satadev_addr, SE_NO_HINT); 1487 1488 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1489 cport_mutex); 1490 /* Virgin port state */ 1491 cportinfo->cport_state = 0; 1492 } 1493 /* 1494 * Always reprobe port, to get current device info. 1495 */ 1496 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1497 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1498 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 1499 rv = EIO; 1500 break; 1501 } 1502 if (target == FALSE && 1503 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1504 /* 1505 * That's the transition from "inactive" port 1506 * to active one with device attached. 1507 */ 1508 sata_log(sata_hba_inst, CE_WARN, 1509 "SATA device detected at port %d", 1510 cport); 1511 } 1512 1513 /* 1514 * This is where real configure starts. 1515 * Change following check for PMult support. 1516 */ 1517 if (!(sata_device.satadev_type & SATA_VALID_DEV_TYPE)) { 1518 /* No device to configure */ 1519 rv = ENXIO; /* No device to configure */ 1520 break; 1521 } 1522 1523 /* 1524 * Here we may have a device in reset condition, 1525 * but because we are just configuring it, there is 1526 * no need to process the reset other than just 1527 * to clear device reset condition in the HBA driver. 1528 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 1529 * cause a first command sent the HBA driver with the request 1530 * to clear device reset condition. 1531 */ 1532 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1533 cport_mutex); 1534 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1535 if (sdinfo == NULL) { 1536 rv = ENXIO; 1537 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1538 cport_mutex); 1539 break; 1540 } 1541 if (sdinfo->satadrv_event_flags & 1542 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) 1543 sdinfo->satadrv_event_flags = 0; 1544 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 1545 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1546 1547 if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) { 1548 /* 1549 * Target node exists. Verify, that it belongs 1550 * to existing, attached device and not to 1551 * a removed device. 1552 */ 1553 if (sata_check_device_removed(tdip) == B_FALSE) { 1554 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 1555 SATA_LOG_D((sata_hba_inst, CE_WARN, 1556 "sata_hba_ioctl: configure: " 1557 "onlining device at SATA port %d " 1558 "failed", cport)); 1559 rv = EIO; 1560 break; 1561 } else { 1562 mutex_enter(&SATA_CPORT_INFO( 1563 sata_hba_inst, cport)->cport_mutex); 1564 SATA_CPORT_INFO(sata_hba_inst, cport)-> 1565 cport_tgtnode_clean = B_TRUE; 1566 mutex_exit(&SATA_CPORT_INFO( 1567 sata_hba_inst, cport)->cport_mutex); 1568 } 1569 } else { 1570 sata_log(sata_hba_inst, CE_WARN, 1571 "SATA device at port %d cannot be " 1572 "configured. " 1573 "Application(s) accessing previously " 1574 "attached device " 1575 "have to release it before newly inserted " 1576 "device can be made accessible.", 1577 cport); 1578 break; 1579 } 1580 } else { 1581 /* 1582 * No target node - need to create a new target node. 1583 */ 1584 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1585 cport_mutex); 1586 SATA_CPORT_INFO(sata_hba_inst, cport)-> 1587 cport_tgtnode_clean = B_TRUE; 1588 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1589 cport_mutex); 1590 tdip = sata_create_target_node(dip, sata_hba_inst, 1591 &sata_device.satadev_addr); 1592 if (tdip == NULL) { 1593 /* configure failed */ 1594 SATA_LOG_D((sata_hba_inst, CE_WARN, 1595 "sata_hba_ioctl: configure: " 1596 "configuring SATA device at port %d " 1597 "failed", cport)); 1598 rv = EIO; 1599 break; 1600 } 1601 } 1602 1603 break; 1604 } 1605 1606 case DEVCTL_AP_GETSTATE: 1607 1608 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1609 1610 ap_state.ap_last_change = (time_t)-1; 1611 ap_state.ap_error_code = 0; 1612 ap_state.ap_in_transition = 0; 1613 1614 /* Copy the return AP-state information to the user space */ 1615 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1616 rv = EFAULT; 1617 } 1618 break; 1619 1620 case DEVCTL_AP_CONTROL: 1621 { 1622 /* 1623 * Generic devctl for hardware specific functionality 1624 */ 1625 sata_ioctl_data_t ioc; 1626 1627 ASSERT(dcp == NULL); 1628 1629 /* Copy in user ioctl data first */ 1630 #ifdef _MULTI_DATAMODEL 1631 if (ddi_model_convert_from(mode & FMODELS) == 1632 DDI_MODEL_ILP32) { 1633 1634 sata_ioctl_data_32_t ioc32; 1635 1636 if (ddi_copyin((void *)arg, (void *)&ioc32, 1637 sizeof (ioc32), mode) != 0) { 1638 rv = EFAULT; 1639 break; 1640 } 1641 ioc.cmd = (uint_t)ioc32.cmd; 1642 ioc.port = (uint_t)ioc32.port; 1643 ioc.get_size = (uint_t)ioc32.get_size; 1644 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1645 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1646 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1647 } else 1648 #endif /* _MULTI_DATAMODEL */ 1649 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1650 mode) != 0) { 1651 return (EFAULT); 1652 } 1653 1654 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1655 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1656 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1657 1658 /* 1659 * To avoid BE/LE and 32/64 issues, a get_size always returns 1660 * a 32-bit number. 1661 */ 1662 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1663 return (EINVAL); 1664 } 1665 /* validate address */ 1666 cport = SCSI_TO_SATA_CPORT(ioc.port); 1667 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1668 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1669 1670 /* Override address qualifier - handle cport only for now */ 1671 qual = SATA_ADDR_CPORT; 1672 1673 if (sata_validate_sata_address(sata_hba_inst, cport, 1674 pmport, qual) != 0) 1675 return (EINVAL); 1676 1677 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1678 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1679 cport_mutex); 1680 /* Is the port locked by event processing daemon ? */ 1681 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1682 /* 1683 * Cannot process ioctl request now. Come back later 1684 */ 1685 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1686 cport_mutex); 1687 return (EBUSY); 1688 } 1689 /* Block event processing for this port */ 1690 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1691 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1692 1693 1694 sata_device.satadev_addr.cport = cport; 1695 sata_device.satadev_addr.pmport = pmport; 1696 sata_device.satadev_rev = SATA_DEVICE_REV; 1697 1698 switch (ioc.cmd) { 1699 1700 case SATA_CFGA_RESET_PORT: 1701 /* 1702 * There is no protection here for configured 1703 * device. 1704 */ 1705 1706 /* Sanity check */ 1707 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1708 SATA_LOG_D((sata_hba_inst, CE_WARN, 1709 "sata_hba_ioctl: " 1710 "sata_hba_tran missing required " 1711 "function sata_tran_reset_dport")); 1712 rv = EINVAL; 1713 break; 1714 } 1715 1716 /* handle cport only for now */ 1717 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 1718 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1719 (dip, &sata_device) != SATA_SUCCESS) { 1720 SATA_LOG_D((sata_hba_inst, CE_WARN, 1721 "sata_hba_ioctl: reset port: " 1722 "failed cport %d pmport %d", 1723 cport, pmport)); 1724 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1725 cport)->cport_mutex); 1726 sata_update_port_info(sata_hba_inst, 1727 &sata_device); 1728 SATA_CPORT_STATE(sata_hba_inst, cport) = 1729 SATA_PSTATE_FAILED; 1730 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1731 cport)->cport_mutex); 1732 rv = EIO; 1733 } 1734 /* 1735 * Since the port was reset, it should be probed and 1736 * attached device reinitialized. At this point the 1737 * port state is unknown - it's state is HBA-specific. 1738 * Re-probe port to get its state. 1739 */ 1740 if (sata_reprobe_port(sata_hba_inst, &sata_device, 1741 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 1742 rv = EIO; 1743 break; 1744 } 1745 break; 1746 1747 case SATA_CFGA_RESET_DEVICE: 1748 /* 1749 * There is no protection here for configured 1750 * device. 1751 */ 1752 1753 /* Sanity check */ 1754 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1755 SATA_LOG_D((sata_hba_inst, CE_WARN, 1756 "sata_hba_ioctl: " 1757 "sata_hba_tran missing required " 1758 "function sata_tran_reset_dport")); 1759 rv = EINVAL; 1760 break; 1761 } 1762 1763 /* handle only device attached to cports, for now */ 1764 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1765 1766 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1767 cport_mutex); 1768 sdinfo = sata_get_device_info(sata_hba_inst, 1769 &sata_device); 1770 if (sdinfo == NULL) { 1771 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1772 cport)->cport_mutex); 1773 rv = EINVAL; 1774 break; 1775 } 1776 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1777 cport_mutex); 1778 1779 /* only handle cport for now */ 1780 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1781 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1782 (dip, &sata_device) != SATA_SUCCESS) { 1783 SATA_LOG_D((sata_hba_inst, CE_WARN, 1784 "sata_hba_ioctl: reset device: failed " 1785 "cport %d pmport %d", cport, pmport)); 1786 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1787 cport)->cport_mutex); 1788 sata_update_port_info(sata_hba_inst, 1789 &sata_device); 1790 /* 1791 * Device info structure remains 1792 * attached. Another device reset or 1793 * port disconnect/connect and re-probing is 1794 * needed to change it's state 1795 */ 1796 sdinfo->satadrv_state &= ~SATA_STATE_READY; 1797 sdinfo->satadrv_state |= 1798 SATA_DSTATE_FAILED; 1799 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1800 cport)->cport_mutex); 1801 rv = EIO; 1802 } 1803 /* 1804 * Since the device was reset, we expect reset event 1805 * to be reported and processed. 1806 */ 1807 break; 1808 1809 case SATA_CFGA_RESET_ALL: 1810 { 1811 int tcport; 1812 1813 /* 1814 * There is no protection here for configured 1815 * devices. 1816 */ 1817 /* Sanity check */ 1818 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 1819 SATA_LOG_D((sata_hba_inst, CE_WARN, 1820 "sata_hba_ioctl: " 1821 "sata_hba_tran missing required " 1822 "function sata_tran_reset_dport")); 1823 rv = EINVAL; 1824 break; 1825 } 1826 1827 /* 1828 * Need to lock all ports, not just one. 1829 * If any port is locked by event processing, fail 1830 * the whole operation. 1831 * One port is already locked, but for simplicity 1832 * lock it again. 1833 */ 1834 for (tcport = 0; 1835 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1836 tcport++) { 1837 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1838 tcport)->cport_mutex); 1839 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 1840 cport_event_flags) & 1841 SATA_EVNT_LOCK_PORT_BUSY) != 0) { 1842 rv = EBUSY; 1843 mutex_exit( 1844 &SATA_CPORT_INFO(sata_hba_inst, 1845 tcport)->cport_mutex); 1846 break; 1847 } else { 1848 SATA_CPORT_INFO(sata_hba_inst, 1849 tcport)->cport_event_flags |= 1850 SATA_APCTL_LOCK_PORT_BUSY; 1851 } 1852 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1853 tcport)->cport_mutex); 1854 } 1855 1856 if (rv == 0) { 1857 /* 1858 * All cports successfully locked. 1859 * Reset main SATA controller only for now - 1860 * no PMult. 1861 */ 1862 sata_device.satadev_addr.qual = 1863 SATA_ADDR_CNTRL; 1864 1865 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 1866 (dip, &sata_device) != SATA_SUCCESS) { 1867 SATA_LOG_D((sata_hba_inst, CE_WARN, 1868 "sata_hba_ioctl: reset controller " 1869 "failed")); 1870 rv = EIO; 1871 } 1872 1873 /* 1874 * Since ports were reset, they should be 1875 * re-probed and attached devices 1876 * reinitialized. 1877 * At this point port states are unknown, 1878 * Re-probe ports to get their state - 1879 * cports only for now. 1880 */ 1881 for (tcport = 0; 1882 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1883 tcport++) { 1884 sata_device.satadev_addr.cport = 1885 tcport; 1886 sata_device.satadev_addr.qual = 1887 SATA_ADDR_CPORT; 1888 1889 if (sata_reprobe_port(sata_hba_inst, 1890 &sata_device, 1891 SATA_DEV_IDENTIFY_RETRY) != 1892 SATA_SUCCESS) 1893 rv = EIO; 1894 1895 } 1896 } 1897 /* 1898 * Unlock all ports 1899 */ 1900 for (tcport = 0; 1901 tcport < SATA_NUM_CPORTS(sata_hba_inst); 1902 tcport++) { 1903 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1904 tcport)->cport_mutex); 1905 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 1906 cport_event_flags &= 1907 ~SATA_APCTL_LOCK_PORT_BUSY; 1908 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 1909 tcport)->cport_mutex); 1910 } 1911 1912 /* 1913 * This operation returns EFAULT if either reset 1914 * controller failed or a re-probing of any ports 1915 * failed. 1916 * We return here, because common return is for 1917 * a single cport operation. 1918 */ 1919 return (rv); 1920 } 1921 1922 case SATA_CFGA_PORT_DEACTIVATE: 1923 /* Sanity check */ 1924 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 1925 rv = ENOTSUP; 1926 break; 1927 } 1928 /* 1929 * Arbitrarily unconfigure attached device, if any. 1930 * Even if the unconfigure fails, proceed with the 1931 * port deactivation. 1932 */ 1933 1934 /* Handle only device attached to cports, for now */ 1935 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 1936 1937 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1938 cport_mutex); 1939 cportinfo->cport_state &= ~SATA_STATE_READY; 1940 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 1941 /* 1942 * Handle only device attached to cports, 1943 * for now 1944 */ 1945 sata_device.satadev_addr.qual = 1946 SATA_ADDR_DCPORT; 1947 sdinfo = sata_get_device_info(sata_hba_inst, 1948 &sata_device); 1949 if (sdinfo != NULL && 1950 (sdinfo->satadrv_type & 1951 SATA_VALID_DEV_TYPE)) { 1952 /* 1953 * If a target node exists, try to 1954 * offline a device and remove target 1955 * node. 1956 */ 1957 mutex_exit(&SATA_CPORT_INFO( 1958 sata_hba_inst, cport)->cport_mutex); 1959 tdip = sata_get_target_dip(dip, cport); 1960 if (tdip != NULL) { 1961 /* target node exist */ 1962 SATADBG1(SATA_DBG_IOCTL_IF, 1963 sata_hba_inst, 1964 "sata_hba_ioctl: " 1965 "port deactivate: " 1966 "target node exists.", 1967 NULL); 1968 1969 if (ndi_devi_offline(tdip, 1970 NDI_DEVI_REMOVE) != 1971 NDI_SUCCESS) { 1972 SATA_LOG_D(( 1973 sata_hba_inst, 1974 CE_WARN, 1975 "sata_hba_ioctl:" 1976 "port deactivate: " 1977 "failed to " 1978 "unconfigure " 1979 "device at port " 1980 "%d before " 1981 "deactivating " 1982 "the port", cport)); 1983 1984 1985 /* 1986 * Set DEVICE REMOVED 1987 * state in the target 1988 * node. It will 1989 * prevent access to 1990 * the device even when 1991 * a new device is 1992 * attached, until the 1993 * old target node is 1994 * released, removed and 1995 * recreated for a new 1996 * device. 1997 */ 1998 sata_set_device_removed 1999 (tdip); 2000 /* 2001 * Instruct event 2002 * daemon to try the 2003 * target node cleanup 2004 * later. 2005 */ 2006 sata_set_target_node_cleanup( 2007 sata_hba_inst, cport); 2008 } 2009 } 2010 mutex_enter(&SATA_CPORT_INFO( 2011 sata_hba_inst, cport)->cport_mutex); 2012 /* 2013 * In any case, 2014 * remove and release sata_drive_info 2015 * structure. 2016 * (cport attached device ony, for now) 2017 */ 2018 SATA_CPORTINFO_DRV_INFO(cportinfo) = 2019 NULL; 2020 (void) kmem_free((void *)sdinfo, 2021 sizeof (sata_drive_info_t)); 2022 cportinfo->cport_dev_type = 2023 SATA_DTYPE_NONE; 2024 } 2025 /* 2026 * Note: PMult info requires different 2027 * handling. This comment is a placeholder for 2028 * a code handling PMult, to be implemented 2029 * in phase 2. 2030 */ 2031 } 2032 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 2033 SATA_STATE_PROBING); 2034 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2035 cport_mutex); 2036 /* handle cport only for now */ 2037 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2038 /* Just let HBA driver to deactivate port */ 2039 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 2040 (dip, &sata_device); 2041 /* 2042 * Generate sysevent - 2043 * EC_DR / ESC_DR_AP_STATE_CHANGE 2044 * without the hint 2045 */ 2046 sata_gen_sysevent(sata_hba_inst, 2047 &sata_device.satadev_addr, SE_NO_HINT); 2048 2049 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2050 cport_mutex); 2051 sata_update_port_info(sata_hba_inst, &sata_device); 2052 if (rval != SATA_SUCCESS) { 2053 /* 2054 * Port deactivation failure - do not 2055 * change port state unless the state 2056 * returned by HBA indicates a port failure. 2057 */ 2058 if (sata_device.satadev_state & 2059 SATA_PSTATE_FAILED) { 2060 SATA_CPORT_STATE(sata_hba_inst, 2061 cport) = SATA_PSTATE_FAILED; 2062 } 2063 SATA_LOG_D((sata_hba_inst, CE_WARN, 2064 "sata_hba_ioctl: port deactivate: " 2065 "cannot deactivate SATA port %d", 2066 cport)); 2067 rv = EIO; 2068 } else { 2069 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 2070 } 2071 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2072 cport_mutex); 2073 2074 break; 2075 2076 case SATA_CFGA_PORT_ACTIVATE: 2077 { 2078 boolean_t dev_existed = TRUE; 2079 2080 /* Sanity check */ 2081 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 2082 rv = ENOTSUP; 2083 break; 2084 } 2085 /* handle cport only for now */ 2086 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 2087 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 2088 dev_existed = FALSE; 2089 2090 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2091 /* Just let HBA driver to activate port */ 2092 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 2093 (dip, &sata_device) != SATA_SUCCESS) { 2094 /* 2095 * Port activation failure - do not 2096 * change port state unless the state 2097 * returned by HBA indicates a port failure. 2098 */ 2099 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 2100 cport)->cport_mutex); 2101 sata_update_port_info(sata_hba_inst, 2102 &sata_device); 2103 if (sata_device.satadev_state & 2104 SATA_PSTATE_FAILED) { 2105 SATA_CPORT_STATE(sata_hba_inst, 2106 cport) = SATA_PSTATE_FAILED; 2107 } 2108 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2109 cport)->cport_mutex); 2110 SATA_LOG_D((sata_hba_inst, CE_WARN, 2111 "sata_hba_ioctl: port activate: " 2112 "cannot activate SATA port %d", 2113 cport)); 2114 rv = EIO; 2115 break; 2116 } 2117 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2118 cport_mutex); 2119 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 2120 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2121 cport_mutex); 2122 2123 /* 2124 * Re-probe port to find its current state and 2125 * possibly attached device. 2126 * Port re-probing may change the cportinfo device 2127 * type if device is found attached. 2128 * If port probing failed, the device type would be 2129 * set to SATA_DTYPE_NONE. 2130 */ 2131 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 2132 SATA_DEV_IDENTIFY_RETRY); 2133 2134 /* 2135 * Generate sysevent - 2136 * EC_DR / ESC_DR_AP_STATE_CHANGE 2137 * without the hint. 2138 */ 2139 sata_gen_sysevent(sata_hba_inst, 2140 &sata_device.satadev_addr, SE_NO_HINT); 2141 2142 if (dev_existed == FALSE && 2143 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 2144 /* 2145 * That's the transition from "inactive" port 2146 * state or active port without a device 2147 * attached to the active port state with 2148 * a device attached. 2149 */ 2150 sata_log(sata_hba_inst, CE_WARN, 2151 "SATA device detected at port %d", cport); 2152 } 2153 2154 break; 2155 } 2156 2157 case SATA_CFGA_PORT_SELF_TEST: 2158 2159 /* Sanity check */ 2160 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) { 2161 rv = ENOTSUP; 2162 break; 2163 } 2164 /* 2165 * There is no protection here for a configured 2166 * device attached to this port. 2167 */ 2168 2169 /* only handle cport for now */ 2170 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2171 2172 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 2173 (dip, &sata_device) != SATA_SUCCESS) { 2174 SATA_LOG_D((sata_hba_inst, CE_WARN, 2175 "sata_hba_ioctl: port selftest: " 2176 "failed cport %d pmport %d", 2177 cport, pmport)); 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 SATA_CPORT_STATE(sata_hba_inst, cport) = 2183 SATA_PSTATE_FAILED; 2184 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2185 cport)->cport_mutex); 2186 rv = EIO; 2187 break; 2188 } 2189 /* 2190 * Since the port was reset, it should be probed and 2191 * attached device reinitialized. At this point the 2192 * port state is unknown - it's state is HBA-specific. 2193 * Force port re-probing to get it into a known state. 2194 */ 2195 if (sata_reprobe_port(sata_hba_inst, &sata_device, 2196 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 2197 rv = EIO; 2198 break; 2199 } 2200 break; 2201 2202 case SATA_CFGA_GET_DEVICE_PATH: 2203 { 2204 char path[MAXPATHLEN]; 2205 uint32_t size; 2206 2207 (void) strcpy(path, "/devices"); 2208 if ((tdip = sata_get_target_dip(dip, ioc.port)) == 2209 NULL) { 2210 2211 /* 2212 * No such device. 2213 * If this is a request for a size, do not 2214 * return EINVAL for non-exisiting target, 2215 * because cfgadm will indicate a meaningless 2216 * ioctl failure. 2217 * If this is a real request for a path, 2218 * indicate invalid argument. 2219 */ 2220 if (!ioc.get_size) { 2221 rv = EINVAL; 2222 break; 2223 } 2224 } else { 2225 (void) ddi_pathname(tdip, path + strlen(path)); 2226 } 2227 size = strlen(path) + 1; 2228 2229 if (ioc.get_size) { 2230 if (ddi_copyout((void *)&size, 2231 ioc.buf, ioc.bufsiz, mode) != 0) { 2232 rv = EFAULT; 2233 } 2234 } else { 2235 if (ioc.bufsiz != size) { 2236 rv = EINVAL; 2237 } else if (ddi_copyout((void *)&path, 2238 ioc.buf, ioc.bufsiz, mode) != 0) { 2239 rv = EFAULT; 2240 } 2241 } 2242 break; 2243 } 2244 2245 case SATA_CFGA_GET_AP_TYPE: 2246 { 2247 uint32_t type_len; 2248 const char *ap_type; 2249 2250 /* cport only, no port multiplier support */ 2251 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 2252 case SATA_DTYPE_NONE: 2253 ap_type = "port"; 2254 break; 2255 2256 case SATA_DTYPE_ATADISK: 2257 ap_type = "disk"; 2258 break; 2259 2260 case SATA_DTYPE_ATAPICD: 2261 ap_type = "cd/dvd"; 2262 break; 2263 2264 case SATA_DTYPE_PMULT: 2265 ap_type = "pmult"; 2266 break; 2267 2268 case SATA_DTYPE_UNKNOWN: 2269 ap_type = "unknown"; 2270 break; 2271 2272 default: 2273 ap_type = "unsupported"; 2274 break; 2275 2276 } /* end of dev_type switch */ 2277 2278 type_len = strlen(ap_type) + 1; 2279 2280 if (ioc.get_size) { 2281 if (ddi_copyout((void *)&type_len, 2282 ioc.buf, ioc.bufsiz, mode) != 0) { 2283 rv = EFAULT; 2284 break; 2285 } 2286 } else { 2287 if (ioc.bufsiz != type_len) { 2288 rv = EINVAL; 2289 break; 2290 } 2291 if (ddi_copyout((void *)ap_type, ioc.buf, 2292 ioc.bufsiz, mode) != 0) { 2293 rv = EFAULT; 2294 break; 2295 } 2296 } 2297 2298 break; 2299 } 2300 2301 case SATA_CFGA_GET_MODEL_INFO: 2302 { 2303 uint32_t info_len; 2304 char ap_info[sizeof (sdinfo->satadrv_id.ai_model) + 1]; 2305 2306 /* 2307 * This operation should return to cfgadm the 2308 * device model information string 2309 */ 2310 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2311 cport_mutex); 2312 /* only handle device connected to cport for now */ 2313 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2314 sdinfo = sata_get_device_info(sata_hba_inst, 2315 &sata_device); 2316 if (sdinfo == NULL) { 2317 rv = EINVAL; 2318 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2319 cport)->cport_mutex); 2320 break; 2321 } 2322 bcopy(sdinfo->satadrv_id.ai_model, ap_info, 2323 sizeof (sdinfo->satadrv_id.ai_model)); 2324 swab(ap_info, ap_info, 2325 sizeof (sdinfo->satadrv_id.ai_model)); 2326 ap_info[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 2327 2328 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2329 cport_mutex); 2330 2331 info_len = strlen(ap_info) + 1; 2332 2333 if (ioc.get_size) { 2334 if (ddi_copyout((void *)&info_len, 2335 ioc.buf, ioc.bufsiz, mode) != 0) { 2336 rv = EFAULT; 2337 break; 2338 } 2339 } else { 2340 if (ioc.bufsiz < info_len) { 2341 rv = EINVAL; 2342 break; 2343 } 2344 if (ddi_copyout((void *)ap_info, ioc.buf, 2345 ioc.bufsiz, mode) != 0) { 2346 rv = EFAULT; 2347 break; 2348 } 2349 } 2350 2351 break; 2352 } 2353 2354 case SATA_CFGA_GET_REVFIRMWARE_INFO: 2355 { 2356 uint32_t info_len; 2357 char ap_info[ 2358 sizeof (sdinfo->satadrv_id.ai_fw) + 1]; 2359 2360 /* 2361 * This operation should return to cfgadm the 2362 * device firmware revision information string 2363 */ 2364 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2365 cport_mutex); 2366 /* only handle device connected to cport for now */ 2367 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2368 2369 sdinfo = sata_get_device_info(sata_hba_inst, 2370 &sata_device); 2371 if (sdinfo == NULL) { 2372 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2373 cport)->cport_mutex); 2374 rv = EINVAL; 2375 break; 2376 } 2377 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, 2378 sizeof (sdinfo->satadrv_id.ai_fw)); 2379 swab(ap_info, ap_info, 2380 sizeof (sdinfo->satadrv_id.ai_fw)); 2381 ap_info[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 2382 2383 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2384 cport_mutex); 2385 2386 info_len = strlen(ap_info) + 1; 2387 2388 if (ioc.get_size) { 2389 if (ddi_copyout((void *)&info_len, 2390 ioc.buf, ioc.bufsiz, mode) != 0) { 2391 rv = EFAULT; 2392 break; 2393 } 2394 } else { 2395 if (ioc.bufsiz < info_len) { 2396 rv = EINVAL; 2397 break; 2398 } 2399 if (ddi_copyout((void *)ap_info, ioc.buf, 2400 ioc.bufsiz, mode) != 0) { 2401 rv = EFAULT; 2402 break; 2403 } 2404 } 2405 2406 break; 2407 } 2408 2409 case SATA_CFGA_GET_SERIALNUMBER_INFO: 2410 { 2411 uint32_t info_len; 2412 char ap_info[ 2413 sizeof (sdinfo->satadrv_id.ai_drvser) + 1]; 2414 2415 /* 2416 * This operation should return to cfgadm the 2417 * device serial number information string 2418 */ 2419 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2420 cport_mutex); 2421 /* only handle device connected to cport for now */ 2422 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 2423 2424 sdinfo = sata_get_device_info(sata_hba_inst, 2425 &sata_device); 2426 if (sdinfo == NULL) { 2427 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 2428 cport)->cport_mutex); 2429 rv = EINVAL; 2430 break; 2431 } 2432 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, 2433 sizeof (sdinfo->satadrv_id.ai_drvser)); 2434 swab(ap_info, ap_info, 2435 sizeof (sdinfo->satadrv_id.ai_drvser)); 2436 ap_info[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 2437 2438 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 2439 cport_mutex); 2440 2441 info_len = strlen(ap_info) + 1; 2442 2443 if (ioc.get_size) { 2444 if (ddi_copyout((void *)&info_len, 2445 ioc.buf, ioc.bufsiz, mode) != 0) { 2446 rv = EFAULT; 2447 break; 2448 } 2449 } else { 2450 if (ioc.bufsiz < info_len) { 2451 rv = EINVAL; 2452 break; 2453 } 2454 if (ddi_copyout((void *)ap_info, ioc.buf, 2455 ioc.bufsiz, mode) != 0) { 2456 rv = EFAULT; 2457 break; 2458 } 2459 } 2460 2461 break; 2462 } 2463 2464 default: 2465 rv = EINVAL; 2466 break; 2467 2468 } /* End of DEVCTL_AP_CONTROL cmd switch */ 2469 2470 break; 2471 } 2472 2473 default: 2474 { 2475 /* 2476 * If we got here, we got an IOCTL that SATA HBA Framework 2477 * does not recognize. Pass ioctl to HBA driver, in case 2478 * it could process it. 2479 */ 2480 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 2481 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 2482 2483 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 2484 "IOCTL 0x%2x not supported in SATA framework, " 2485 "passthrough to HBA", cmd); 2486 2487 if (sata_tran->sata_tran_ioctl == NULL) { 2488 rv = EINVAL; 2489 break; 2490 } 2491 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 2492 if (rval != 0) { 2493 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 2494 "IOCTL 0x%2x failed in HBA", cmd); 2495 rv = rval; 2496 } 2497 break; 2498 } 2499 2500 } /* End of main IOCTL switch */ 2501 2502 if (dcp) { 2503 ndi_dc_freehdl(dcp); 2504 } 2505 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 2506 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 2507 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 2508 2509 return (rv); 2510 } 2511 2512 2513 2514 2515 /* ****************** SCSA required entry points *********************** */ 2516 2517 /* 2518 * Implementation of scsi tran_tgt_init. 2519 * sata_scsi_tgt_init() initializes scsi_device structure 2520 * 2521 * If successful, DDI_SUCCESS is returned. 2522 * DDI_FAILURE is returned if addressed device does not exist 2523 */ 2524 2525 static int 2526 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2527 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2528 { 2529 #ifndef __lock_lint 2530 _NOTE(ARGUNUSED(hba_dip)) 2531 #endif 2532 sata_device_t sata_device; 2533 sata_drive_info_t *sdinfo; 2534 struct sata_id *sid; 2535 sata_hba_inst_t *sata_hba_inst; 2536 char model[SATA_ID_MODEL_LEN + 1]; 2537 char fw[SATA_ID_FW_LEN + 1]; 2538 char *vid, *pid; 2539 int i; 2540 2541 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2542 2543 /* Validate scsi device address */ 2544 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2545 &sata_device) != 0) 2546 return (DDI_FAILURE); 2547 2548 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2549 sata_device.satadev_addr.cport))); 2550 2551 /* sata_device now contains a valid sata address */ 2552 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2553 if (sdinfo == NULL) { 2554 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2555 sata_device.satadev_addr.cport))); 2556 return (DDI_FAILURE); 2557 } 2558 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2559 sata_device.satadev_addr.cport))); 2560 2561 if (sata_device.satadev_type == SATA_DTYPE_ATAPICD) { 2562 if (ndi_prop_update_string(DDI_DEV_T_NONE, tgt_dip, 2563 "variant", "atapi") != DDI_PROP_SUCCESS) { 2564 SATA_LOG_D((sata_hba_inst, CE_WARN, 2565 "sata_scsi_tgt_init: variant atapi " 2566 "property could not be created")); 2567 return (DDI_FAILURE); 2568 } 2569 } 2570 2571 /* 2572 * 'Identify Device Data' does not always fit in standard SCSI 2573 * INQUIRY data, so establish INQUIRY_* properties with full-form 2574 * of information. 2575 */ 2576 sid = &sdinfo->satadrv_id; 2577 #ifdef _LITTLE_ENDIAN 2578 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2579 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2580 #else /* _LITTLE_ENDIAN */ 2581 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2582 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2583 #endif /* _LITTLE_ENDIAN */ 2584 model[SATA_ID_MODEL_LEN] = 0; 2585 fw[SATA_ID_FW_LEN] = 0; 2586 2587 /* split model into into vid/pid */ 2588 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 2589 if ((*pid == ' ') || (*pid == '\t')) 2590 break; 2591 if (i < SATA_ID_MODEL_LEN) { 2592 vid = model; 2593 *pid++ = 0; /* terminate vid, establish pid */ 2594 } else { 2595 vid = NULL; /* vid will stay "ATA " */ 2596 pid = model; /* model is all pid */ 2597 } 2598 2599 if (vid) 2600 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2601 vid, strlen(vid)); 2602 if (pid) 2603 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2604 pid, strlen(pid)); 2605 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2606 fw, strlen(fw)); 2607 2608 return (DDI_SUCCESS); 2609 } 2610 2611 /* 2612 * Implementation of scsi tran_tgt_probe. 2613 * Probe target, by calling default scsi routine scsi_hba_probe() 2614 */ 2615 static int 2616 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2617 { 2618 sata_hba_inst_t *sata_hba_inst = 2619 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2620 int rval; 2621 2622 rval = scsi_hba_probe(sd, callback); 2623 2624 if (rval == SCSIPROBE_EXISTS) { 2625 /* 2626 * Set property "pm-capable" on the target device node, so that 2627 * the target driver will not try to fetch scsi cycle counters 2628 * before enabling device power-management. 2629 */ 2630 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2631 "pm-capable", 1)) != DDI_PROP_SUCCESS) { 2632 sata_log(sata_hba_inst, CE_WARN, 2633 "SATA device at port %d: " 2634 "will not be power-managed ", 2635 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2636 SATA_LOG_D((sata_hba_inst, CE_WARN, 2637 "failure updating pm-capable property")); 2638 } 2639 } 2640 return (rval); 2641 } 2642 2643 /* 2644 * Implementation of scsi tran_tgt_free. 2645 * Release all resources allocated for scsi_device 2646 */ 2647 static void 2648 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2649 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2650 { 2651 #ifndef __lock_lint 2652 _NOTE(ARGUNUSED(hba_dip)) 2653 #endif 2654 sata_device_t sata_device; 2655 sata_drive_info_t *sdinfo; 2656 sata_hba_inst_t *sata_hba_inst; 2657 2658 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2659 2660 /* Validate scsi device address */ 2661 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2662 &sata_device) != 0) 2663 return; 2664 2665 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2666 sata_device.satadev_addr.cport))); 2667 2668 /* sata_device now should contain a valid sata address */ 2669 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2670 if (sdinfo == NULL) { 2671 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2672 sata_device.satadev_addr.cport))); 2673 return; 2674 } 2675 /* 2676 * We did not allocate any resources in sata_scsi_tgt_init() 2677 * other than property for ATAPI device, if any 2678 */ 2679 if (sata_device.satadev_type == SATA_DTYPE_ATAPICD) { 2680 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2681 sata_device.satadev_addr.cport))); 2682 if (ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "variant") != 2683 DDI_PROP_SUCCESS) 2684 SATA_LOG_D((sata_hba_inst, CE_WARN, 2685 "sata_scsi_tgt_free: variant atapi " 2686 "property could not be removed")); 2687 } else { 2688 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2689 sata_device.satadev_addr.cport))); 2690 } 2691 } 2692 2693 /* 2694 * Implementation of scsi tran_init_pkt 2695 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2696 * 2697 * It seems that we should always allocate pkt, even if the address is 2698 * for non-existing device - just use some default for dma_attr. 2699 * The reason is that there is no way to communicate this to a caller here. 2700 * Subsequent call to sata_scsi_start may fail appropriately. 2701 * Simply returning NULL does not seem to discourage a target driver... 2702 * 2703 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2704 */ 2705 static struct scsi_pkt * 2706 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2707 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2708 int (*callback)(caddr_t), caddr_t arg) 2709 { 2710 sata_hba_inst_t *sata_hba_inst = 2711 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2712 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2713 sata_device_t sata_device; 2714 sata_drive_info_t *sdinfo; 2715 sata_pkt_txlate_t *spx; 2716 ddi_dma_attr_t cur_dma_attr; 2717 int rval; 2718 boolean_t new_pkt = TRUE; 2719 2720 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2721 2722 /* 2723 * We need to translate the address, even if it could be 2724 * a bogus one, for a non-existing device 2725 */ 2726 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2727 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2728 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2729 sata_device.satadev_rev = SATA_DEVICE_REV; 2730 2731 if (pkt == NULL) { 2732 /* 2733 * Have to allocate a brand new scsi packet. 2734 * We need to operate with auto request sense enabled. 2735 */ 2736 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2737 MAX(statuslen, sizeof (struct scsi_arq_status)), 2738 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2739 2740 if (pkt == NULL) 2741 return (NULL); 2742 2743 /* Fill scsi packet structure */ 2744 pkt->pkt_comp = (void (*)())NULL; 2745 pkt->pkt_time = 0; 2746 pkt->pkt_resid = 0; 2747 pkt->pkt_statistics = 0; 2748 pkt->pkt_reason = 0; 2749 2750 /* 2751 * pkt_hba_private will point to sata pkt txlate structure 2752 */ 2753 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2754 bzero(spx, sizeof (sata_pkt_txlate_t)); 2755 2756 spx->txlt_scsi_pkt = pkt; 2757 spx->txlt_sata_hba_inst = sata_hba_inst; 2758 2759 /* Allocate sata_pkt */ 2760 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2761 if (spx->txlt_sata_pkt == NULL) { 2762 /* Could not allocate sata pkt */ 2763 scsi_hba_pkt_free(ap, pkt); 2764 return (NULL); 2765 } 2766 /* Set sata address */ 2767 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2768 sata_device.satadev_addr; 2769 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2770 sata_device.satadev_rev; 2771 2772 if ((bp == NULL) || (bp->b_bcount == 0)) 2773 return (pkt); 2774 2775 spx->txlt_total_residue = bp->b_bcount; 2776 } else { 2777 new_pkt = FALSE; 2778 /* 2779 * Packet was preallocated/initialized by previous call 2780 */ 2781 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2782 2783 if ((bp == NULL) || (bp->b_bcount == 0)) { 2784 return (pkt); 2785 } 2786 ASSERT(spx->txlt_buf_dma_handle != NULL); 2787 2788 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2789 } 2790 2791 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2792 2793 /* 2794 * We use an adjusted version of the dma_attr, to account 2795 * for device addressing limitations. 2796 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2797 * happen when a device is not yet configured. 2798 */ 2799 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2800 sata_device.satadev_addr.cport))); 2801 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2802 &spx->txlt_sata_pkt->satapkt_device); 2803 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2804 sata_adjust_dma_attr(sdinfo, 2805 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2806 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2807 sata_device.satadev_addr.cport))); 2808 /* 2809 * Allocate necessary DMA resources for the packet's data buffer 2810 * NOTE: 2811 * In case of read/write commands, DMA resource allocation here is 2812 * based on the premise that the transfer length specified in 2813 * the read/write scsi cdb will match exactly DMA resources - 2814 * returning correct packet residue is crucial. 2815 */ 2816 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2817 &cur_dma_attr)) != DDI_SUCCESS) { 2818 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2819 sata_pkt_free(spx); 2820 /* 2821 * If a DMA allocation request fails with 2822 * DDI_DMA_NOMAPPING, indicate the error by calling 2823 * bioerror(9F) with bp and an error code of EFAULT. 2824 * If a DMA allocation request fails with 2825 * DDI_DMA_TOOBIG, indicate the error by calling 2826 * bioerror(9F) with bp and an error code of EINVAL. 2827 */ 2828 switch (rval) { 2829 case DDI_DMA_NORESOURCES: 2830 bioerror(bp, 0); 2831 break; 2832 case DDI_DMA_NOMAPPING: 2833 case DDI_DMA_BADATTR: 2834 bioerror(bp, EFAULT); 2835 break; 2836 case DDI_DMA_TOOBIG: 2837 default: 2838 bioerror(bp, EINVAL); 2839 break; 2840 } 2841 if (new_pkt == TRUE) 2842 scsi_hba_pkt_free(ap, pkt); 2843 return (NULL); 2844 } 2845 /* Set number of bytes that are not yet accounted for */ 2846 pkt->pkt_resid = spx->txlt_total_residue; 2847 ASSERT(pkt->pkt_resid >= 0); 2848 2849 return (pkt); 2850 } 2851 2852 /* 2853 * Implementation of scsi tran_start. 2854 * Translate scsi cmd into sata operation and return status. 2855 * Supported scsi commands: 2856 * SCMD_INQUIRY 2857 * SCMD_TEST_UNIT_READY 2858 * SCMD_START_STOP 2859 * SCMD_READ_CAPACITY 2860 * SCMD_REQUEST_SENSE 2861 * SCMD_LOG_SENSE_G1 2862 * SCMD_LOG_SELECT_G1 2863 * SCMD_MODE_SENSE (specific pages) 2864 * SCMD_MODE_SENSE_G1 (specific pages) 2865 * SCMD_MODE_SELECT (specific pages) 2866 * SCMD_MODE_SELECT_G1 (specific pages) 2867 * SCMD_SYNCHRONIZE_CACHE 2868 * SCMD_SYNCHRONIZE_CACHE_G1 2869 * SCMD_READ 2870 * SCMD_READ_G1 2871 * SCMD_READ_G4 2872 * SCMD_READ_G5 2873 * SCMD_WRITE 2874 * SCMD_WRITE_BUFFER 2875 * SCMD_WRITE_G1 2876 * SCMD_WRITE_G4 2877 * SCMD_WRITE_G5 2878 * SCMD_SEEK (noop) 2879 * SCMD_SDIAG 2880 * 2881 * All other commands are rejected as unsupported. 2882 * 2883 * Returns: 2884 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2885 * for execution. 2886 * TRAN_BADPKT if cmd was directed to invalid address. 2887 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2888 * unexpected removal of a device or some other unspecified error. 2889 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2890 * framework was busy performing some other operation(s). 2891 * 2892 */ 2893 static int 2894 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2895 { 2896 sata_hba_inst_t *sata_hba_inst = 2897 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2898 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2899 sata_drive_info_t *sdinfo; 2900 struct buf *bp; 2901 int cport; 2902 int rval; 2903 2904 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2905 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2906 2907 ASSERT(spx != NULL && 2908 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2909 2910 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2911 2912 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2913 sdinfo = sata_get_device_info(sata_hba_inst, 2914 &spx->txlt_sata_pkt->satapkt_device); 2915 if (sdinfo == NULL || 2916 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean == 2917 B_FALSE) { 2918 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2919 pkt->pkt_reason = CMD_DEV_GONE; 2920 /* 2921 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2922 * only in callback function (for normal requests) and 2923 * in the dump code path. 2924 * So, if the callback is available, we need to do 2925 * the callback rather than returning TRAN_FATAL_ERROR here. 2926 */ 2927 if (pkt->pkt_comp != NULL) { 2928 /* scsi callback required */ 2929 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2930 (task_func_t *)pkt->pkt_comp, 2931 (void *)pkt, TQ_SLEEP) == NULL) 2932 /* Scheduling the callback failed */ 2933 return (TRAN_BUSY); 2934 return (TRAN_ACCEPT); 2935 } 2936 /* No callback available */ 2937 return (TRAN_FATAL_ERROR); 2938 } 2939 2940 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 2941 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2942 rval = sata_txlt_atapi(spx); 2943 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2944 "sata_scsi_start atapi: rval %d\n", rval); 2945 return (rval); 2946 } 2947 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2948 2949 /* ATA Disk commands processing starts here */ 2950 2951 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2952 2953 switch (pkt->pkt_cdbp[0]) { 2954 2955 case SCMD_INQUIRY: 2956 /* Mapped to identify device */ 2957 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2958 bp_mapin(bp); 2959 rval = sata_txlt_inquiry(spx); 2960 break; 2961 2962 case SCMD_TEST_UNIT_READY: 2963 /* 2964 * SAT "SATA to ATA Translation" doc specifies translation 2965 * to ATA CHECK POWER MODE. 2966 */ 2967 rval = sata_txlt_test_unit_ready(spx); 2968 break; 2969 2970 case SCMD_START_STOP: 2971 /* Mapping depends on the command */ 2972 rval = sata_txlt_start_stop_unit(spx); 2973 break; 2974 2975 case SCMD_READ_CAPACITY: 2976 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2977 bp_mapin(bp); 2978 rval = sata_txlt_read_capacity(spx); 2979 break; 2980 2981 case SCMD_REQUEST_SENSE: 2982 /* 2983 * Always No Sense, since we force ARQ 2984 */ 2985 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2986 bp_mapin(bp); 2987 rval = sata_txlt_request_sense(spx); 2988 break; 2989 2990 case SCMD_LOG_SENSE_G1: 2991 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2992 bp_mapin(bp); 2993 rval = sata_txlt_log_sense(spx); 2994 break; 2995 2996 case SCMD_LOG_SELECT_G1: 2997 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2998 bp_mapin(bp); 2999 rval = sata_txlt_log_select(spx); 3000 break; 3001 3002 case SCMD_MODE_SENSE: 3003 case SCMD_MODE_SENSE_G1: 3004 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3005 bp_mapin(bp); 3006 rval = sata_txlt_mode_sense(spx); 3007 break; 3008 3009 3010 case SCMD_MODE_SELECT: 3011 case SCMD_MODE_SELECT_G1: 3012 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3013 bp_mapin(bp); 3014 rval = sata_txlt_mode_select(spx); 3015 break; 3016 3017 case SCMD_SYNCHRONIZE_CACHE: 3018 case SCMD_SYNCHRONIZE_CACHE_G1: 3019 rval = sata_txlt_synchronize_cache(spx); 3020 break; 3021 3022 case SCMD_READ: 3023 case SCMD_READ_G1: 3024 case SCMD_READ_G4: 3025 case SCMD_READ_G5: 3026 rval = sata_txlt_read(spx); 3027 break; 3028 case SCMD_WRITE_BUFFER: 3029 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 3030 bp_mapin(bp); 3031 rval = sata_txlt_write_buffer(spx); 3032 break; 3033 3034 case SCMD_WRITE: 3035 case SCMD_WRITE_G1: 3036 case SCMD_WRITE_G4: 3037 case SCMD_WRITE_G5: 3038 rval = sata_txlt_write(spx); 3039 break; 3040 3041 case SCMD_SEEK: 3042 rval = sata_txlt_nodata_cmd_immediate(spx); 3043 break; 3044 3045 /* Other cases will be filed later */ 3046 /* postponed until phase 2 of the development */ 3047 default: 3048 rval = sata_txlt_invalid_command(spx); 3049 break; 3050 } 3051 3052 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 3053 "sata_scsi_start: rval %d\n", rval); 3054 3055 return (rval); 3056 } 3057 3058 /* 3059 * Implementation of scsi tran_abort. 3060 * Abort specific pkt or all packets. 3061 * 3062 * Returns 1 if one or more packets were aborted, returns 0 otherwise 3063 * 3064 * May be called from an interrupt level. 3065 */ 3066 static int 3067 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 3068 { 3069 sata_hba_inst_t *sata_hba_inst = 3070 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3071 sata_device_t sata_device; 3072 sata_pkt_t *sata_pkt; 3073 3074 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3075 "sata_scsi_abort: %s at target: 0x%x\n", 3076 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 3077 3078 /* Validate address */ 3079 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 3080 /* Invalid address */ 3081 return (0); 3082 3083 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3084 sata_device.satadev_addr.cport))); 3085 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 3086 /* invalid address */ 3087 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3088 sata_device.satadev_addr.cport))); 3089 return (0); 3090 } 3091 if (scsi_pkt == NULL) { 3092 /* 3093 * Abort all packets. 3094 * Although we do not have specific packet, we still need 3095 * dummy packet structure to pass device address to HBA. 3096 * Allocate one, without sleeping. Fail if pkt cannot be 3097 * allocated. 3098 */ 3099 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 3100 if (sata_pkt == NULL) { 3101 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3102 sata_device.satadev_addr.cport))); 3103 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 3104 "could not allocate sata_pkt")); 3105 return (0); 3106 } 3107 sata_pkt->satapkt_rev = SATA_PKT_REV; 3108 sata_pkt->satapkt_device = sata_device; 3109 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 3110 } else { 3111 if (scsi_pkt->pkt_ha_private == NULL) { 3112 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3113 sata_device.satadev_addr.cport))); 3114 return (0); /* Bad scsi pkt */ 3115 } 3116 /* extract pointer to sata pkt */ 3117 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 3118 txlt_sata_pkt; 3119 } 3120 3121 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3122 sata_device.satadev_addr.cport))); 3123 /* Send abort request to HBA */ 3124 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 3125 (SATA_DIP(sata_hba_inst), sata_pkt, 3126 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 3127 SATA_SUCCESS) { 3128 if (scsi_pkt == NULL) 3129 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 3130 /* Success */ 3131 return (1); 3132 } 3133 /* Else, something did not go right */ 3134 if (scsi_pkt == NULL) 3135 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 3136 /* Failure */ 3137 return (0); 3138 } 3139 3140 3141 /* 3142 * Implementation os scsi tran_reset. 3143 * RESET_ALL request is translated into port reset. 3144 * RESET_TARGET requests is translated into a device reset, 3145 * RESET_LUN request is accepted only for LUN 0 and translated into 3146 * device reset. 3147 * The target reset should cause all HBA active and queued packets to 3148 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 3149 * the return. HBA should report reset event for the device. 3150 * 3151 * Returns 1 upon success, 0 upon failure. 3152 */ 3153 static int 3154 sata_scsi_reset(struct scsi_address *ap, int level) 3155 { 3156 sata_hba_inst_t *sata_hba_inst = 3157 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3158 sata_device_t sata_device; 3159 int val; 3160 3161 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3162 "sata_scsi_reset: level %d target: 0x%x\n", 3163 level, ap->a_target); 3164 3165 /* Validate address */ 3166 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 3167 if (val == -1) 3168 /* Invalid address */ 3169 return (0); 3170 3171 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3172 sata_device.satadev_addr.cport))); 3173 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 3174 /* invalid address */ 3175 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3176 sata_device.satadev_addr.cport))); 3177 return (0); 3178 } 3179 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3180 sata_device.satadev_addr.cport))); 3181 if (level == RESET_ALL) { 3182 /* port reset - cport only */ 3183 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 3184 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 3185 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 3186 return (1); 3187 else 3188 return (0); 3189 3190 } else if (val == 0 && 3191 (level == RESET_TARGET || level == RESET_LUN)) { 3192 /* reset device (device attached) */ 3193 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 3194 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 3195 return (1); 3196 else 3197 return (0); 3198 } 3199 return (0); 3200 } 3201 3202 3203 /* 3204 * Implementation of scsi tran_getcap (get transport/device capabilities). 3205 * Supported capabilities: 3206 * auto-rqsense (always supported) 3207 * tagged-qing (supported if HBA supports it) 3208 * untagged-qing (could be supported if disk supports it, but because 3209 * caching behavior allowing untagged queuing actually 3210 * results in reduced performance. sd tries to throttle 3211 * back to only 3 outstanding commands, which may 3212 * work for real SCSI disks, but with read ahead 3213 * caching, having more than 1 outstanding command 3214 * results in cache thrashing.) 3215 * dma_max 3216 * interconnect-type (INTERCONNECT_SATA) 3217 * 3218 * Request for other capabilities is rejected as unsupported. 3219 * 3220 * Returns supported capability value, or -1 if capability is unsuppported or 3221 * the address is invalid (no device). 3222 */ 3223 3224 static int 3225 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 3226 { 3227 3228 sata_hba_inst_t *sata_hba_inst = 3229 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3230 sata_device_t sata_device; 3231 sata_drive_info_t *sdinfo; 3232 ddi_dma_attr_t adj_dma_attr; 3233 int rval; 3234 3235 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3236 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 3237 ap->a_target, cap); 3238 3239 /* 3240 * We want to process the capabilities on per port granularity. 3241 * So, we are specifically restricting ourselves to whom != 0 3242 * to exclude the controller wide handling. 3243 */ 3244 if (cap == NULL || whom == 0) 3245 return (-1); 3246 3247 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3248 /* Invalid address */ 3249 return (-1); 3250 } 3251 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3252 sata_device.satadev_addr.cport))); 3253 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 3254 NULL) { 3255 /* invalid address */ 3256 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3257 sata_device.satadev_addr.cport))); 3258 return (-1); 3259 } 3260 3261 switch (scsi_hba_lookup_capstr(cap)) { 3262 case SCSI_CAP_ARQ: 3263 rval = 1; /* ARQ supported, turned on */ 3264 break; 3265 3266 case SCSI_CAP_SECTOR_SIZE: 3267 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 3268 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 3269 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 3270 rval = SATA_ATAPI_SECTOR_SIZE; 3271 else rval = -1; 3272 break; 3273 3274 /* 3275 * untagged queuing cause a performance inversion because of 3276 * the way sd operates. Because of this reason we do not 3277 * use it when available. 3278 */ 3279 case SCSI_CAP_UNTAGGED_QING: 3280 if (sdinfo->satadrv_features_enabled & 3281 SATA_DEV_F_E_UNTAGGED_QING) 3282 rval = 1; /* Untagged queuing available */ 3283 else 3284 rval = -1; /* Untagged queuing not available */ 3285 break; 3286 3287 case SCSI_CAP_TAGGED_QING: 3288 if (sdinfo->satadrv_features_enabled & SATA_DEV_F_E_TAGGED_QING) 3289 rval = 1; /* Tagged queuing available */ 3290 else 3291 rval = -1; /* Tagged queuing not available */ 3292 break; 3293 3294 case SCSI_CAP_DMA_MAX: 3295 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 3296 &adj_dma_attr); 3297 rval = (int)adj_dma_attr.dma_attr_maxxfer; 3298 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 3299 break; 3300 3301 case SCSI_CAP_INTERCONNECT_TYPE: 3302 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 3303 break; 3304 3305 default: 3306 rval = -1; 3307 break; 3308 } 3309 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3310 sata_device.satadev_addr.cport))); 3311 return (rval); 3312 } 3313 3314 /* 3315 * Implementation of scsi tran_setcap 3316 */ 3317 static int 3318 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 3319 { 3320 sata_hba_inst_t *sata_hba_inst = 3321 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3322 sata_device_t sata_device; 3323 sata_drive_info_t *sdinfo; 3324 int rval; 3325 3326 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3327 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 3328 3329 /* 3330 * We want to process the capabilities on per port granularity. 3331 * So, we are specifically restricting ourselves to whom != 0 3332 * to exclude the controller wide handling. 3333 */ 3334 if (cap == NULL || whom == 0) { 3335 return (-1); 3336 } 3337 3338 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3339 /* Invalid address */ 3340 return (-1); 3341 } 3342 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3343 sata_device.satadev_addr.cport))); 3344 if ((sdinfo = sata_get_device_info(sata_hba_inst, 3345 &sata_device)) == NULL) { 3346 /* invalid address */ 3347 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3348 sata_device.satadev_addr.cport))); 3349 return (-1); 3350 } 3351 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3352 sata_device.satadev_addr.cport))); 3353 3354 switch (scsi_hba_lookup_capstr(cap)) { 3355 case SCSI_CAP_ARQ: 3356 case SCSI_CAP_SECTOR_SIZE: 3357 case SCSI_CAP_DMA_MAX: 3358 case SCSI_CAP_INTERCONNECT_TYPE: 3359 rval = 0; 3360 break; 3361 case SCSI_CAP_UNTAGGED_QING: 3362 if (SATA_QDEPTH(sata_hba_inst) > 1) { 3363 rval = 1; 3364 if (value == 1) { 3365 sdinfo->satadrv_features_enabled |= 3366 SATA_DEV_F_E_UNTAGGED_QING; 3367 } else if (value == 0) { 3368 sdinfo->satadrv_features_enabled &= 3369 ~SATA_DEV_F_E_UNTAGGED_QING; 3370 } else { 3371 rval = -1; 3372 } 3373 } else { 3374 rval = 0; 3375 } 3376 break; 3377 case SCSI_CAP_TAGGED_QING: 3378 /* This can TCQ or NCQ */ 3379 if (sata_func_enable & SATA_ENABLE_QUEUING && 3380 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 3381 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 3382 (sata_func_enable & SATA_ENABLE_NCQ && 3383 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 3384 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ))) { 3385 rval = 1; 3386 if (value == 1) { 3387 sdinfo->satadrv_features_enabled |= 3388 SATA_DEV_F_E_TAGGED_QING; 3389 } else if (value == 0) { 3390 sdinfo->satadrv_features_enabled &= 3391 ~SATA_DEV_F_E_TAGGED_QING; 3392 } else { 3393 rval = -1; 3394 } 3395 } else { 3396 rval = 0; 3397 } 3398 break; 3399 default: 3400 rval = -1; 3401 break; 3402 } 3403 return (rval); 3404 } 3405 3406 /* 3407 * Implementations of scsi tran_destroy_pkt. 3408 * Free resources allocated by sata_scsi_init_pkt() 3409 */ 3410 static void 3411 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3412 { 3413 sata_pkt_txlate_t *spx; 3414 3415 ASSERT(pkt != NULL); 3416 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3417 3418 if (spx->txlt_buf_dma_handle != NULL) { 3419 if (spx->txlt_tmp_buf != NULL) { 3420 ASSERT(spx->txlt_tmp_buf_handle != 0); 3421 /* 3422 * Intermediate DMA buffer was allocated. 3423 * Free allocated buffer and associated access handle. 3424 */ 3425 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 3426 spx->txlt_tmp_buf = NULL; 3427 } 3428 /* 3429 * Free DMA resources - cookies and handles 3430 */ 3431 ASSERT(spx->txlt_dma_cookie_list != NULL); 3432 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 3433 (void) kmem_free(spx->txlt_dma_cookie_list, 3434 spx->txlt_dma_cookie_list_len * 3435 sizeof (ddi_dma_cookie_t)); 3436 spx->txlt_dma_cookie_list = NULL; 3437 } 3438 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 3439 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 3440 } 3441 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 3442 sata_pkt_free(spx); 3443 3444 scsi_hba_pkt_free(ap, pkt); 3445 } 3446 3447 /* 3448 * Implementation of scsi tran_dmafree. 3449 * Free DMA resources allocated by sata_scsi_init_pkt() 3450 */ 3451 3452 static void 3453 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 3454 { 3455 #ifndef __lock_lint 3456 _NOTE(ARGUNUSED(ap)) 3457 #endif 3458 sata_pkt_txlate_t *spx; 3459 3460 ASSERT(pkt != NULL); 3461 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3462 3463 if (spx->txlt_buf_dma_handle != NULL) { 3464 /* 3465 * Free DMA resources - cookies and handles 3466 */ 3467 ASSERT(spx->txlt_dma_cookie_list != NULL); 3468 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 3469 (void) kmem_free(spx->txlt_dma_cookie_list, 3470 spx->txlt_dma_cookie_list_len * 3471 sizeof (ddi_dma_cookie_t)); 3472 spx->txlt_dma_cookie_list = NULL; 3473 } 3474 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 3475 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 3476 } 3477 } 3478 3479 /* 3480 * Implementation of scsi tran_sync_pkt. 3481 * 3482 * The assumption below is that pkt is unique - there is no need to check ap 3483 * 3484 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3485 * into/from the real buffer. 3486 */ 3487 static void 3488 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3489 { 3490 #ifndef __lock_lint 3491 _NOTE(ARGUNUSED(ap)) 3492 #endif 3493 int rval; 3494 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3495 struct buf *bp; 3496 int direction; 3497 3498 ASSERT(spx != NULL); 3499 if (spx->txlt_buf_dma_handle != NULL) { 3500 direction = spx->txlt_sata_pkt-> 3501 satapkt_cmd.satacmd_flags.sata_data_direction; 3502 if (spx->txlt_sata_pkt != NULL && 3503 direction != SATA_DIR_NODATA_XFER) { 3504 if (spx->txlt_tmp_buf != NULL) { 3505 /* Intermediate DMA buffer used */ 3506 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3507 3508 if (direction & SATA_DIR_WRITE) { 3509 bcopy(bp->b_un.b_addr, 3510 spx->txlt_tmp_buf, bp->b_bcount); 3511 } 3512 } 3513 /* Sync the buffer for device or for CPU */ 3514 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3515 (direction & SATA_DIR_WRITE) ? 3516 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3517 ASSERT(rval == DDI_SUCCESS); 3518 if (spx->txlt_tmp_buf != NULL && 3519 !(direction & SATA_DIR_WRITE)) { 3520 /* Intermediate DMA buffer used for read */ 3521 bcopy(spx->txlt_tmp_buf, 3522 bp->b_un.b_addr, bp->b_bcount); 3523 } 3524 3525 } 3526 } 3527 } 3528 3529 3530 3531 /* ******************* SATA - SCSI Translation functions **************** */ 3532 /* 3533 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3534 * translation. 3535 */ 3536 3537 /* 3538 * Checks if a device exists and can be access and translates common 3539 * scsi_pkt data to sata_pkt data. 3540 * 3541 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3542 * sata_pkt was set-up. 3543 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3544 * exist and pkt_comp callback was scheduled. 3545 * Returns other TRAN_XXXXX values when error occured and command should be 3546 * rejected with the returned TRAN_XXXXX value. 3547 * 3548 * This function should be called with port mutex held. 3549 */ 3550 static int 3551 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx) 3552 { 3553 sata_drive_info_t *sdinfo; 3554 sata_device_t sata_device; 3555 const struct sata_cmd_flags sata_initial_cmd_flags = { 3556 SATA_DIR_NODATA_XFER, 3557 /* all other values to 0/FALSE */ 3558 }; 3559 /* 3560 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3561 * and that implies TRAN_ACCEPT return value. Any other returned value 3562 * indicates that the scsi packet was not accepted (the reason will not 3563 * be checked by the scsi traget driver). 3564 * To make debugging easier, we set pkt_reason to know value here. 3565 * It may be changed later when different completion reason is 3566 * determined. 3567 */ 3568 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3569 3570 /* Validate address */ 3571 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3572 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3573 3574 case -1: 3575 /* Invalid address or invalid device type */ 3576 return (TRAN_BADPKT); 3577 case 1: 3578 /* valid address but no device - it has disappeared ? */ 3579 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3580 /* 3581 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3582 * only in callback function (for normal requests) and 3583 * in the dump code path. 3584 * So, if the callback is available, we need to do 3585 * the callback rather than returning TRAN_FATAL_ERROR here. 3586 */ 3587 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3588 /* scsi callback required */ 3589 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3590 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3591 (void *)spx->txlt_scsi_pkt, 3592 TQ_SLEEP) == NULL) 3593 /* Scheduling the callback failed */ 3594 return (TRAN_BUSY); 3595 3596 return (TRAN_ACCEPT); 3597 } 3598 return (TRAN_FATAL_ERROR); 3599 default: 3600 /* all OK */ 3601 break; 3602 } 3603 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3604 &spx->txlt_sata_pkt->satapkt_device); 3605 3606 /* 3607 * If device is in reset condition, reject the packet with 3608 * TRAN_BUSY, unless: 3609 * 1. system is panicking (dumping) 3610 * In such case only one thread is running and there is no way to 3611 * process reset. 3612 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3613 * Some cfgadm operations involve drive commands, so reset condition 3614 * needs to be ignored for IOCTL operations. 3615 */ 3616 if ((sdinfo->satadrv_event_flags & 3617 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3618 3619 if (!ddi_in_panic() && 3620 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3621 sata_device.satadev_addr.cport) & 3622 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3623 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3624 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3625 "sata_scsi_start: rejecting command because " 3626 "of device reset state\n", NULL); 3627 return (TRAN_BUSY); 3628 } 3629 } 3630 3631 /* 3632 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3633 * sata_scsi_pkt_init() because pkt init had to work also with 3634 * non-existing devices. 3635 * Now we know that the packet was set-up for a real device, so its 3636 * type is known. 3637 */ 3638 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3639 3640 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3641 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3642 sata_device.satadev_addr.cport)->cport_event_flags & 3643 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3644 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3645 sata_ignore_dev_reset = B_TRUE; 3646 } 3647 /* 3648 * At this point the generic translation routine determined that the 3649 * scsi packet should be accepted. Packet completion reason may be 3650 * changed later when a different completion reason is determined. 3651 */ 3652 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3653 3654 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3655 /* Synchronous execution */ 3656 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3657 SATA_OPMODE_POLLING; 3658 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3659 sata_ignore_dev_reset = ddi_in_panic(); 3660 } else { 3661 /* Asynchronous execution */ 3662 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3663 SATA_OPMODE_INTERRUPTS; 3664 } 3665 /* Convert queuing information */ 3666 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3667 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3668 B_TRUE; 3669 else if (spx->txlt_scsi_pkt->pkt_flags & 3670 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3671 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3672 B_TRUE; 3673 3674 /* Always limit pkt time */ 3675 if (spx->txlt_scsi_pkt->pkt_time == 0) 3676 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3677 else 3678 /* Pass on scsi_pkt time */ 3679 spx->txlt_sata_pkt->satapkt_time = 3680 spx->txlt_scsi_pkt->pkt_time; 3681 3682 return (TRAN_ACCEPT); 3683 } 3684 3685 3686 /* 3687 * Translate ATA(ATAPI) Identify (Packet) Device data to SCSI Inquiry data. 3688 * SATA Identify Device data has to be valid in sata_rive_info. 3689 * Buffer has to accomodate the inquiry length (36 bytes). 3690 * 3691 * This function should be called with a port mutex held. 3692 */ 3693 static void 3694 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3695 sata_drive_info_t *sdinfo, uint8_t *buf) 3696 { 3697 3698 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3699 struct sata_id *sid = &sdinfo->satadrv_id; 3700 3701 /* Start with a nice clean slate */ 3702 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3703 3704 /* Rely on the dev_type for setting paripheral qualifier */ 3705 /* Does DTYPE_RODIRECT apply to CD/DVD R/W devices ? */ 3706 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3707 DTYPE_DIRECT : DTYPE_RODIRECT; 3708 3709 inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0; 3710 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3711 inq->inq_iso = 0; /* ISO version */ 3712 inq->inq_ecma = 0; /* ECMA version */ 3713 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3714 inq->inq_aenc = 0; /* Async event notification cap. */ 3715 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg ??? */ 3716 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3717 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3718 inq->inq_len = 31; /* Additional length */ 3719 inq->inq_dualp = 0; /* dual port device - NO */ 3720 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3721 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3722 inq->inq_linked = 0; /* Supports linked commands - NO */ 3723 /* 3724 * Queuing support - controller has to 3725 * support some sort of command queuing. 3726 */ 3727 if (SATA_QDEPTH(sata_hba_inst) > 1) 3728 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3729 else 3730 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3731 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3732 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3733 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3734 3735 #ifdef _LITTLE_ENDIAN 3736 /* Swap text fields to match SCSI format */ 3737 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3738 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3739 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3740 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3741 else 3742 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3743 #else /* _LITTLE_ENDIAN */ 3744 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3745 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3746 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3747 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3748 else 3749 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3750 #endif /* _LITTLE_ENDIAN */ 3751 } 3752 3753 3754 /* 3755 * Scsi response set up for invalid command (command not supported) 3756 * 3757 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3758 */ 3759 static int 3760 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3761 { 3762 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3763 struct scsi_extended_sense *sense; 3764 3765 scsipkt->pkt_reason = CMD_CMPLT; 3766 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3767 STATE_SENT_CMD | STATE_GOT_STATUS; 3768 3769 *scsipkt->pkt_scbp = STATUS_CHECK; 3770 3771 sense = sata_arq_sense(spx); 3772 sense->es_key = KEY_ILLEGAL_REQUEST; 3773 sense->es_add_code = SD_SCSI_INVALID_COMMAND_CODE; 3774 3775 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3776 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3777 3778 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3779 scsipkt->pkt_comp != NULL) 3780 /* scsi callback required */ 3781 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3782 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3783 (void *)spx->txlt_scsi_pkt, 3784 TQ_SLEEP) == NULL) 3785 /* Scheduling the callback failed */ 3786 return (TRAN_BUSY); 3787 return (TRAN_ACCEPT); 3788 } 3789 3790 /* 3791 * Scsi response setup for 3792 * emulated non-data command that requires no action/return data 3793 * 3794 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3795 */ 3796 static int 3797 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3798 { 3799 int rval; 3800 3801 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3802 3803 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 3804 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 3805 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3806 return (rval); 3807 } 3808 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3809 3810 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3811 STATE_SENT_CMD | STATE_GOT_STATUS; 3812 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3813 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3814 3815 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3816 "Scsi_pkt completion reason %x\n", 3817 spx->txlt_scsi_pkt->pkt_reason); 3818 3819 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3820 spx->txlt_scsi_pkt->pkt_comp != NULL) 3821 /* scsi callback required */ 3822 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3823 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3824 (void *)spx->txlt_scsi_pkt, 3825 TQ_SLEEP) == NULL) 3826 /* Scheduling the callback failed */ 3827 return (TRAN_BUSY); 3828 return (TRAN_ACCEPT); 3829 } 3830 3831 3832 /* 3833 * SATA translate command: Inquiry / Identify Device 3834 * Use cached Identify Device data for now, rather then issuing actual 3835 * Device Identify cmd request. If device is detached and re-attached, 3836 * asynchromous event processing should fetch and refresh Identify Device 3837 * data. 3838 * Two VPD pages are supported now: 3839 * Vital Product Data page 3840 * Unit Serial Number page 3841 * 3842 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3843 */ 3844 3845 #define EVPD 1 /* Extended Vital Product Data flag */ 3846 #define CMDDT 2 /* Command Support Data - Obsolete */ 3847 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3848 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3849 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3850 3851 static int 3852 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3853 { 3854 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3855 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3856 sata_drive_info_t *sdinfo; 3857 struct scsi_extended_sense *sense; 3858 int count; 3859 uint8_t *p; 3860 int i, j; 3861 uint8_t page_buf[0xff]; /* Max length */ 3862 int rval; 3863 3864 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3865 3866 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 3867 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 3868 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3869 return (rval); 3870 } 3871 3872 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3873 &spx->txlt_sata_pkt->satapkt_device); 3874 3875 ASSERT(sdinfo != NULL); 3876 3877 scsipkt->pkt_reason = CMD_CMPLT; 3878 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3879 STATE_SENT_CMD | STATE_GOT_STATUS; 3880 3881 /* Reject not supported request */ 3882 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3883 *scsipkt->pkt_scbp = STATUS_CHECK; 3884 sense = sata_arq_sense(spx); 3885 sense->es_key = KEY_ILLEGAL_REQUEST; 3886 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 3887 goto done; 3888 } 3889 3890 /* Valid Inquiry request */ 3891 *scsipkt->pkt_scbp = STATUS_GOOD; 3892 3893 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3894 3895 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3896 /* Standard Inquiry Data request */ 3897 struct scsi_inquiry inq; 3898 unsigned int bufsize; 3899 3900 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3901 sdinfo, (uint8_t *)&inq); 3902 /* Copy no more than requested */ 3903 count = MIN(bp->b_bcount, 3904 sizeof (struct scsi_inquiry)); 3905 bufsize = scsipkt->pkt_cdbp[4]; 3906 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3907 count = MIN(count, bufsize); 3908 bcopy(&inq, bp->b_un.b_addr, count); 3909 3910 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3911 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3912 bufsize - count : 0; 3913 } else { 3914 /* 3915 * peripheral_qualifier = 0; 3916 * 3917 * We are dealing only with HD and will be 3918 * dealing with CD/DVD devices soon 3919 */ 3920 uint8_t peripheral_device_type = 3921 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3922 DTYPE_DIRECT : DTYPE_RODIRECT; 3923 3924 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3925 case INQUIRY_SUP_VPD_PAGE: 3926 /* 3927 * Request for suported Vital Product Data 3928 * pages - assuming only 2 page codes 3929 * supported 3930 */ 3931 page_buf[0] = peripheral_device_type; 3932 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3933 page_buf[2] = 0; 3934 page_buf[3] = 2; /* page length */ 3935 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3936 page_buf[5] = INQUIRY_USN_PAGE; 3937 /* Copy no more than requested */ 3938 count = MIN(bp->b_bcount, 6); 3939 bcopy(page_buf, bp->b_un.b_addr, count); 3940 break; 3941 case INQUIRY_USN_PAGE: 3942 /* 3943 * Request for Unit Serial Number page 3944 */ 3945 page_buf[0] = peripheral_device_type; 3946 page_buf[1] = INQUIRY_USN_PAGE; 3947 page_buf[2] = 0; 3948 page_buf[3] = 20; /* remaining page length */ 3949 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3950 #ifdef _LITTLE_ENDIAN 3951 swab(p, &page_buf[4], 20); 3952 #else 3953 bcopy(p, &page_buf[4], 20); 3954 #endif 3955 for (i = 0; i < 20; i++) { 3956 if (page_buf[4 + i] == '\0' || 3957 page_buf[4 + i] == '\040') { 3958 break; 3959 } 3960 } 3961 /* 3962 * 'i' contains string length. 3963 * 3964 * Least significant character of the serial 3965 * number shall appear as the last byte, 3966 * according to SBC-3 spec. 3967 */ 3968 p = &page_buf[20 + 4 - 1]; 3969 for (j = i; j > 0; j--, p--) { 3970 *p = *(p - 20 + i); 3971 } 3972 p = &page_buf[4]; 3973 for (j = 20 - i; j > 0; j--) { 3974 *p++ = '\040'; 3975 } 3976 count = MIN(bp->b_bcount, 24); 3977 bcopy(page_buf, bp->b_un.b_addr, count); 3978 break; 3979 3980 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3981 /* 3982 * We may want to implement this page, when 3983 * identifiers are common for SATA devices 3984 * But not now. 3985 */ 3986 /*FALLTHROUGH*/ 3987 3988 default: 3989 /* Request for unsupported VPD page */ 3990 *scsipkt->pkt_scbp = STATUS_CHECK; 3991 sense = sata_arq_sense(spx); 3992 sense->es_key = KEY_ILLEGAL_REQUEST; 3993 sense->es_add_code = 3994 SD_SCSI_INVALID_FIELD_IN_CDB; 3995 goto done; 3996 } 3997 } 3998 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3999 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 4000 scsipkt->pkt_cdbp[4] - count : 0; 4001 } 4002 done: 4003 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4004 4005 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4006 "Scsi_pkt completion reason %x\n", 4007 scsipkt->pkt_reason); 4008 4009 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4010 scsipkt->pkt_comp != NULL) { 4011 /* scsi callback required */ 4012 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4013 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4014 TQ_SLEEP) == NULL) 4015 /* Scheduling the callback failed */ 4016 return (TRAN_BUSY); 4017 } 4018 return (TRAN_ACCEPT); 4019 } 4020 4021 /* 4022 * SATA translate command: Request Sense 4023 * emulated command (ATA version so far, no ATAPI) 4024 * Always NO SENSE, because any sense data should be reported by ARQ sense. 4025 * 4026 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4027 */ 4028 static int 4029 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 4030 { 4031 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4032 struct scsi_extended_sense sense; 4033 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4034 int rval; 4035 4036 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4037 4038 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4039 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4040 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4041 return (rval); 4042 } 4043 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4044 4045 4046 scsipkt->pkt_reason = CMD_CMPLT; 4047 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4048 STATE_SENT_CMD | STATE_GOT_STATUS; 4049 *scsipkt->pkt_scbp = STATUS_GOOD; 4050 4051 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4052 int count = MIN(bp->b_bcount, 4053 sizeof (struct scsi_extended_sense)); 4054 bzero(&sense, sizeof (struct scsi_extended_sense)); 4055 sense.es_valid = 0; /* Valid LBA */ 4056 sense.es_class = 7; /* Response code 0x70 - current err */ 4057 sense.es_key = KEY_NO_SENSE; 4058 sense.es_add_len = 6; /* Additional length */ 4059 /* Copy no more than requested */ 4060 bcopy(&sense, bp->b_un.b_addr, count); 4061 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4062 scsipkt->pkt_resid = 0; 4063 } 4064 4065 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4066 "Scsi_pkt completion reason %x\n", 4067 scsipkt->pkt_reason); 4068 4069 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4070 scsipkt->pkt_comp != NULL) 4071 /* scsi callback required */ 4072 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4073 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4074 TQ_SLEEP) == NULL) 4075 /* Scheduling the callback failed */ 4076 return (TRAN_BUSY); 4077 return (TRAN_ACCEPT); 4078 } 4079 4080 /* 4081 * SATA translate command: Test Unit Ready 4082 * At the moment this is an emulated command (ATA version so far, no ATAPI). 4083 * May be translated into Check Power Mode command in the future 4084 * 4085 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4086 */ 4087 static int 4088 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 4089 { 4090 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4091 struct scsi_extended_sense *sense; 4092 int power_state; 4093 int rval; 4094 4095 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4096 4097 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4098 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4099 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4100 return (rval); 4101 } 4102 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4103 4104 /* At this moment, emulate it rather than execute anything */ 4105 power_state = SATA_PWRMODE_ACTIVE; 4106 4107 scsipkt->pkt_reason = CMD_CMPLT; 4108 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4109 STATE_SENT_CMD | STATE_GOT_STATUS; 4110 4111 switch (power_state) { 4112 case SATA_PWRMODE_ACTIVE: 4113 case SATA_PWRMODE_IDLE: 4114 *scsipkt->pkt_scbp = STATUS_GOOD; 4115 break; 4116 default: 4117 /* PWR mode standby */ 4118 *scsipkt->pkt_scbp = STATUS_CHECK; 4119 sense = sata_arq_sense(spx); 4120 sense->es_key = KEY_NOT_READY; 4121 sense->es_add_code = SD_SCSI_LU_NOT_READY; 4122 break; 4123 } 4124 4125 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4126 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4127 4128 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4129 scsipkt->pkt_comp != NULL) 4130 /* scsi callback required */ 4131 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4132 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4133 TQ_SLEEP) == NULL) 4134 /* Scheduling the callback failed */ 4135 return (TRAN_BUSY); 4136 4137 return (TRAN_ACCEPT); 4138 } 4139 4140 4141 /* 4142 * SATA translate command: Start Stop Unit 4143 * Translation depends on a command: 4144 * Start Unit translated into Idle Immediate 4145 * Stop Unit translated into Standby Immediate 4146 * Unload Media / NOT SUPPORTED YET 4147 * Load Media / NOT SUPPROTED YET 4148 * Power condition bits are ignored, so is Immediate bit 4149 * Requesting synchronous execution. 4150 * 4151 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4152 * appropriate values in scsi_pkt fields. 4153 */ 4154 static int 4155 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 4156 { 4157 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4158 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4159 struct scsi_extended_sense *sense; 4160 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4161 int cport = SATA_TXLT_CPORT(spx); 4162 int rval; 4163 int synch; 4164 4165 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4166 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 4167 4168 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 4169 4170 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4171 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4172 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4173 return (rval); 4174 } 4175 4176 if (scsipkt->pkt_cdbp[4] & 2) { 4177 /* Load/Unload Media - invalid request */ 4178 *scsipkt->pkt_scbp = STATUS_CHECK; 4179 sense = sata_arq_sense(spx); 4180 sense->es_key = KEY_ILLEGAL_REQUEST; 4181 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 4182 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4183 4184 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4185 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4186 4187 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4188 scsipkt->pkt_comp != NULL) 4189 /* scsi callback required */ 4190 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4191 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4192 TQ_SLEEP) == NULL) 4193 /* Scheduling the callback failed */ 4194 return (TRAN_BUSY); 4195 4196 return (TRAN_ACCEPT); 4197 } 4198 scmd->satacmd_addr_type = 0; 4199 scmd->satacmd_sec_count_lsb = 0; 4200 scmd->satacmd_lba_low_lsb = 0; 4201 scmd->satacmd_lba_mid_lsb = 0; 4202 scmd->satacmd_lba_high_lsb = 0; 4203 scmd->satacmd_features_reg = 0; 4204 scmd->satacmd_device_reg = 0; 4205 scmd->satacmd_status_reg = 0; 4206 if (scsipkt->pkt_cdbp[4] & 1) { 4207 /* Start Unit */ 4208 scmd->satacmd_cmd_reg = SATAC_IDLE_IM; 4209 } else { 4210 /* Stop Unit */ 4211 scmd->satacmd_cmd_reg = SATAC_STANDBY_IM; 4212 } 4213 4214 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4215 /* Need to set-up a callback function */ 4216 spx->txlt_sata_pkt->satapkt_comp = 4217 sata_txlt_nodata_cmd_completion; 4218 synch = FALSE; 4219 } else { 4220 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4221 synch = TRUE; 4222 } 4223 4224 /* Transfer command to HBA */ 4225 if (sata_hba_start(spx, &rval) != 0) { 4226 /* Pkt not accepted for execution */ 4227 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4228 return (rval); 4229 } 4230 4231 /* 4232 * If execution is non-synchronous, 4233 * a callback function will handle potential errors, translate 4234 * the response and will do a callback to a target driver. 4235 * If it was synchronous, check execution status using the same 4236 * framework callback. 4237 */ 4238 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4239 if (synch) { 4240 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4241 "synchronous execution status %x\n", 4242 spx->txlt_sata_pkt->satapkt_reason); 4243 4244 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4245 } 4246 return (TRAN_ACCEPT); 4247 4248 } 4249 4250 4251 /* 4252 * SATA translate command: Read Capacity. 4253 * Emulated command for SATA disks. 4254 * Capacity is retrieved from cached Idenifty Device data. 4255 * Identify Device data shows effective disk capacity, not the native 4256 * capacity, which may be limitted by Set Max Address command. 4257 * This is ATA version (non-ATAPI). 4258 * 4259 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4260 */ 4261 static int 4262 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4263 { 4264 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4265 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4266 sata_drive_info_t *sdinfo; 4267 uint64_t val; 4268 uchar_t *rbuf; 4269 int rval; 4270 4271 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4272 "sata_txlt_read_capacity: ", NULL); 4273 4274 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4275 4276 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4277 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4278 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4279 return (rval); 4280 } 4281 4282 scsipkt->pkt_reason = CMD_CMPLT; 4283 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4284 STATE_SENT_CMD | STATE_GOT_STATUS; 4285 *scsipkt->pkt_scbp = STATUS_GOOD; 4286 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4287 sdinfo = sata_get_device_info( 4288 spx->txlt_sata_hba_inst, 4289 &spx->txlt_sata_pkt->satapkt_device); 4290 /* Last logical block address */ 4291 val = sdinfo->satadrv_capacity - 1; 4292 rbuf = (uchar_t *)bp->b_un.b_addr; 4293 /* Need to swap endians to match scsi format */ 4294 rbuf[0] = (val >> 24) & 0xff; 4295 rbuf[1] = (val >> 16) & 0xff; 4296 rbuf[2] = (val >> 8) & 0xff; 4297 rbuf[3] = val & 0xff; 4298 /* block size - always 512 bytes, for now */ 4299 rbuf[4] = 0; 4300 rbuf[5] = 0; 4301 rbuf[6] = 0x02; 4302 rbuf[7] = 0; 4303 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4304 scsipkt->pkt_resid = 0; 4305 4306 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4307 sdinfo->satadrv_capacity -1); 4308 } 4309 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4310 /* 4311 * If a callback was requested, do it now. 4312 */ 4313 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4314 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4315 4316 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4317 scsipkt->pkt_comp != NULL) 4318 /* scsi callback required */ 4319 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4320 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4321 TQ_SLEEP) == NULL) 4322 /* Scheduling the callback failed */ 4323 return (TRAN_BUSY); 4324 4325 return (TRAN_ACCEPT); 4326 } 4327 4328 /* 4329 * SATA translate command: Mode Sense. 4330 * Translated into appropriate SATA command or emulated. 4331 * Saved Values Page Control (03) are not supported. 4332 * 4333 * NOTE: only caching mode sense page is currently implemented. 4334 * 4335 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4336 */ 4337 4338 static int 4339 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4340 { 4341 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4342 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4343 sata_drive_info_t *sdinfo; 4344 sata_id_t *sata_id; 4345 struct scsi_extended_sense *sense; 4346 int len, bdlen, count, alc_len; 4347 int pc; /* Page Control code */ 4348 uint8_t *buf; /* mode sense buffer */ 4349 int rval; 4350 4351 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4352 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 4353 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4354 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4355 4356 buf = kmem_zalloc(1024, KM_SLEEP); 4357 4358 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4359 4360 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4361 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4362 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4363 kmem_free(buf, 1024); 4364 return (rval); 4365 } 4366 4367 scsipkt->pkt_reason = CMD_CMPLT; 4368 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4369 STATE_SENT_CMD | STATE_GOT_STATUS; 4370 4371 pc = scsipkt->pkt_cdbp[2] >> 6; 4372 4373 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4374 len = 0; 4375 bdlen = 0; 4376 if (!(scsipkt->pkt_cdbp[1] & 8)) { 4377 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 4378 (scsipkt->pkt_cdbp[0] & 0x10)) 4379 bdlen = 16; 4380 else 4381 bdlen = 8; 4382 } 4383 /* Build mode parameter header */ 4384 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4385 /* 4-byte mode parameter header */ 4386 buf[len++] = 0; /* mode data length */ 4387 buf[len++] = 0; /* medium type */ 4388 buf[len++] = 0; /* dev-specific param */ 4389 buf[len++] = bdlen; /* Block Descriptor length */ 4390 } else { 4391 /* 8-byte mode parameter header */ 4392 buf[len++] = 0; /* mode data length */ 4393 buf[len++] = 0; 4394 buf[len++] = 0; /* medium type */ 4395 buf[len++] = 0; /* dev-specific param */ 4396 if (bdlen == 16) 4397 buf[len++] = 1; /* long lba descriptor */ 4398 else 4399 buf[len++] = 0; 4400 buf[len++] = 0; 4401 buf[len++] = 0; /* Block Descriptor length */ 4402 buf[len++] = bdlen; 4403 } 4404 4405 sdinfo = sata_get_device_info( 4406 spx->txlt_sata_hba_inst, 4407 &spx->txlt_sata_pkt->satapkt_device); 4408 4409 /* Build block descriptor only if not disabled (DBD) */ 4410 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 4411 /* Block descriptor - direct-access device format */ 4412 if (bdlen == 8) { 4413 /* build regular block descriptor */ 4414 buf[len++] = 4415 (sdinfo->satadrv_capacity >> 24) & 0xff; 4416 buf[len++] = 4417 (sdinfo->satadrv_capacity >> 16) & 0xff; 4418 buf[len++] = 4419 (sdinfo->satadrv_capacity >> 8) & 0xff; 4420 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4421 buf[len++] = 0; /* density code */ 4422 buf[len++] = 0; 4423 if (sdinfo->satadrv_type == 4424 SATA_DTYPE_ATADISK) 4425 buf[len++] = 2; 4426 else 4427 /* ATAPI */ 4428 buf[len++] = 8; 4429 buf[len++] = 0; 4430 } else if (bdlen == 16) { 4431 /* Long LBA Accepted */ 4432 /* build long lba block descriptor */ 4433 #ifndef __lock_lint 4434 buf[len++] = 4435 (sdinfo->satadrv_capacity >> 56) & 0xff; 4436 buf[len++] = 4437 (sdinfo->satadrv_capacity >> 48) & 0xff; 4438 buf[len++] = 4439 (sdinfo->satadrv_capacity >> 40) & 0xff; 4440 buf[len++] = 4441 (sdinfo->satadrv_capacity >> 32) & 0xff; 4442 #endif 4443 buf[len++] = 4444 (sdinfo->satadrv_capacity >> 24) & 0xff; 4445 buf[len++] = 4446 (sdinfo->satadrv_capacity >> 16) & 0xff; 4447 buf[len++] = 4448 (sdinfo->satadrv_capacity >> 8) & 0xff; 4449 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4450 buf[len++] = 0; 4451 buf[len++] = 0; /* density code */ 4452 buf[len++] = 0; 4453 buf[len++] = 0; 4454 if (sdinfo->satadrv_type == 4455 SATA_DTYPE_ATADISK) 4456 buf[len++] = 2; 4457 else 4458 /* ATAPI */ 4459 buf[len++] = 8; 4460 buf[len++] = 0; 4461 } 4462 } 4463 4464 sata_id = &sdinfo->satadrv_id; 4465 4466 /* 4467 * Add requested pages. 4468 * Page 3 and 4 are obsolete and we are not supporting them. 4469 * We deal now with: 4470 * caching (read/write cache control). 4471 * We should eventually deal with following mode pages: 4472 * error recovery (0x01), 4473 * power condition (0x1a), 4474 * exception control page (enables SMART) (0x1c), 4475 * enclosure management (ses), 4476 * protocol-specific port mode (port control). 4477 */ 4478 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 4479 case MODEPAGE_RW_ERRRECOV: 4480 /* DAD_MODE_ERR_RECOV */ 4481 /* R/W recovery */ 4482 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4483 break; 4484 case MODEPAGE_CACHING: 4485 /* DAD_MODE_CACHE */ 4486 /* Reject not supported request for saved parameters */ 4487 if (pc == 3) { 4488 *scsipkt->pkt_scbp = STATUS_CHECK; 4489 sense = sata_arq_sense(spx); 4490 sense->es_key = KEY_ILLEGAL_REQUEST; 4491 sense->es_add_code = 4492 SD_SCSI_SAVING_PARAMS_NOT_SUP; 4493 goto done; 4494 } 4495 4496 /* caching */ 4497 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4498 break; 4499 case MODEPAGE_INFO_EXCPT: 4500 /* exception cntrl */ 4501 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4502 len += sata_build_msense_page_1c(sdinfo, pc, 4503 buf+len); 4504 } 4505 else 4506 goto err; 4507 break; 4508 case MODEPAGE_POWER_COND: 4509 /* DAD_MODE_POWER_COND */ 4510 /* power condition */ 4511 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4512 break; 4513 case MODEPAGE_ALLPAGES: 4514 /* all pages */ 4515 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4516 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4517 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4518 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4519 len += sata_build_msense_page_1c(sdinfo, pc, 4520 buf+len); 4521 } 4522 break; 4523 default: 4524 err: 4525 /* Invalid request */ 4526 *scsipkt->pkt_scbp = STATUS_CHECK; 4527 sense = sata_arq_sense(spx); 4528 sense->es_key = KEY_ILLEGAL_REQUEST; 4529 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 4530 goto done; 4531 } 4532 4533 /* fix total mode data length */ 4534 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4535 /* 4-byte mode parameter header */ 4536 buf[0] = len - 1; /* mode data length */ 4537 } else { 4538 buf[0] = (len -2) >> 8; 4539 buf[1] = (len -2) & 0xff; 4540 } 4541 4542 4543 /* Check allocation length */ 4544 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4545 alc_len = scsipkt->pkt_cdbp[4]; 4546 } else { 4547 alc_len = scsipkt->pkt_cdbp[7]; 4548 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4549 } 4550 /* 4551 * We do not check for possible parameters truncation 4552 * (alc_len < len) assuming that the target driver works 4553 * correctly. Just avoiding overrun. 4554 * Copy no more than requested and possible, buffer-wise. 4555 */ 4556 count = MIN(alc_len, len); 4557 count = MIN(bp->b_bcount, count); 4558 bcopy(buf, bp->b_un.b_addr, count); 4559 4560 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4561 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4562 } 4563 *scsipkt->pkt_scbp = STATUS_GOOD; 4564 done: 4565 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4566 (void) kmem_free(buf, 1024); 4567 4568 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4569 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4570 4571 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4572 scsipkt->pkt_comp != NULL) 4573 /* scsi callback required */ 4574 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4575 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4576 TQ_SLEEP) == NULL) 4577 /* Scheduling the callback failed */ 4578 return (TRAN_BUSY); 4579 4580 return (TRAN_ACCEPT); 4581 } 4582 4583 4584 /* 4585 * SATA translate command: Mode Select. 4586 * Translated into appropriate SATA command or emulated. 4587 * Saving parameters is not supported. 4588 * Changing device capacity is not supported (although theoretically 4589 * possible by executing SET FEATURES/SET MAX ADDRESS) 4590 * 4591 * Assumption is that the target driver is working correctly. 4592 * 4593 * More than one SATA command may be executed to perform operations specified 4594 * by mode select pages. The first error terminates further execution. 4595 * Operations performed successully are not backed-up in such case. 4596 * 4597 * NOTE: only caching mode select page is implemented. 4598 * Caching setup is remembered so it could be re-stored in case of 4599 * an unexpected device reset. 4600 * 4601 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4602 */ 4603 4604 static int 4605 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 4606 { 4607 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4608 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4609 struct scsi_extended_sense *sense; 4610 int len, pagelen, count, pllen; 4611 uint8_t *buf; /* mode select buffer */ 4612 int rval, stat; 4613 uint_t nointr_flag; 4614 int dmod = 0; 4615 4616 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4617 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 4618 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4619 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4620 4621 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4622 4623 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4624 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4625 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4626 return (rval); 4627 } 4628 4629 rval = TRAN_ACCEPT; 4630 4631 scsipkt->pkt_reason = CMD_CMPLT; 4632 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4633 STATE_SENT_CMD | STATE_GOT_STATUS; 4634 4635 /* Reject not supported request */ 4636 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 4637 *scsipkt->pkt_scbp = STATUS_CHECK; 4638 sense = sata_arq_sense(spx); 4639 sense->es_key = KEY_ILLEGAL_REQUEST; 4640 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 4641 goto done; 4642 } 4643 4644 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4645 pllen = scsipkt->pkt_cdbp[4]; 4646 } else { 4647 pllen = scsipkt->pkt_cdbp[7]; 4648 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 4649 } 4650 4651 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4652 4653 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 4654 buf = (uint8_t *)bp->b_un.b_addr; 4655 count = MIN(bp->b_bcount, pllen); 4656 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4657 scsipkt->pkt_resid = 0; 4658 pllen = count; 4659 4660 /* 4661 * Check the header to skip the block descriptor(s) - we 4662 * do not support setting device capacity. 4663 * Existing macros do not recognize long LBA dscriptor, 4664 * hence manual calculation. 4665 */ 4666 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4667 /* 6-bytes CMD, 4 bytes header */ 4668 if (count <= 4) 4669 goto done; /* header only */ 4670 len = buf[3] + 4; 4671 } else { 4672 /* 10-bytes CMD, 8 bytes header */ 4673 if (count <= 8) 4674 goto done; /* header only */ 4675 len = buf[6]; 4676 len = (len << 8) + buf[7] + 8; 4677 } 4678 if (len >= count) 4679 goto done; /* header + descriptor(s) only */ 4680 4681 pllen -= len; /* remaining data length */ 4682 4683 /* 4684 * We may be executing SATA command and want to execute it 4685 * in SYNCH mode, regardless of scsi_pkt setting. 4686 * Save scsi_pkt setting and indicate SYNCH mode 4687 */ 4688 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 4689 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4690 scsipkt->pkt_comp != NULL) { 4691 scsipkt->pkt_flags |= FLAG_NOINTR; 4692 } 4693 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4694 4695 /* 4696 * len is now the offset to a first mode select page 4697 * Process all pages 4698 */ 4699 while (pllen > 0) { 4700 switch ((int)buf[len]) { 4701 case MODEPAGE_CACHING: 4702 /* No support for SP (saving) */ 4703 if (scsipkt->pkt_cdbp[1] & 0x01) { 4704 *scsipkt->pkt_scbp = STATUS_CHECK; 4705 sense = sata_arq_sense(spx); 4706 sense->es_key = KEY_ILLEGAL_REQUEST; 4707 sense->es_add_code = 4708 SD_SCSI_INVALID_FIELD_IN_CDB; 4709 goto done; 4710 } 4711 stat = sata_mode_select_page_8(spx, 4712 (struct mode_cache_scsi3 *)&buf[len], 4713 pllen, &pagelen, &rval, &dmod); 4714 /* 4715 * The pagelen value indicates the number of 4716 * parameter bytes already processed. 4717 * The rval is the return value from 4718 * sata_tran_start(). 4719 * The stat indicates the overall status of 4720 * the operation(s). 4721 */ 4722 if (stat != SATA_SUCCESS) 4723 /* 4724 * Page processing did not succeed - 4725 * all error info is already set-up, 4726 * just return 4727 */ 4728 pllen = 0; /* this breaks the loop */ 4729 else { 4730 len += pagelen; 4731 pllen -= pagelen; 4732 } 4733 break; 4734 4735 case MODEPAGE_INFO_EXCPT: 4736 stat = sata_mode_select_page_1c(spx, 4737 (struct mode_info_excpt_page *)&buf[len], 4738 pllen, &pagelen, &rval, &dmod); 4739 /* 4740 * The pagelen value indicates the number of 4741 * parameter bytes already processed. 4742 * The rval is the return value from 4743 * sata_tran_start(). 4744 * The stat indicates the overall status of 4745 * the operation(s). 4746 */ 4747 if (stat != SATA_SUCCESS) 4748 /* 4749 * Page processing did not succeed - 4750 * all error info is already set-up, 4751 * just return 4752 */ 4753 pllen = 0; /* this breaks the loop */ 4754 else { 4755 len += pagelen; 4756 pllen -= pagelen; 4757 } 4758 break; 4759 4760 default: 4761 *scsipkt->pkt_scbp = STATUS_CHECK; 4762 sense = sata_arq_sense(spx); 4763 sense->es_key = KEY_ILLEGAL_REQUEST; 4764 sense->es_add_code = 4765 SD_SCSI_INVALID_FIELD_IN_PARAMETER_LIST; 4766 goto done; 4767 } 4768 } 4769 } 4770 done: 4771 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4772 /* 4773 * If device parameters were modified, fetch and store the new 4774 * Identify Device data. Since port mutex could have been released 4775 * for accessing HBA driver, we need to re-check device existence. 4776 */ 4777 if (dmod != 0) { 4778 sata_drive_info_t new_sdinfo, *sdinfo; 4779 int rv; 4780 4781 new_sdinfo.satadrv_addr = 4782 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4783 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4784 &new_sdinfo); 4785 4786 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4787 /* 4788 * Since port mutex could have been released when 4789 * accessing HBA driver, we need to re-check that the 4790 * framework still holds the device info structure. 4791 */ 4792 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4793 &spx->txlt_sata_pkt->satapkt_device); 4794 if (sdinfo != NULL) { 4795 /* 4796 * Device still has info structure in the 4797 * sata framework. Copy newly fetched info 4798 */ 4799 if (rv == 0) { 4800 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4801 sata_save_drive_settings(sdinfo); 4802 } else { 4803 /* 4804 * Could not fetch new data - invalidate 4805 * sata_drive_info. That makes device 4806 * unusable. 4807 */ 4808 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4809 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4810 } 4811 } 4812 if (rv != 0 || sdinfo == NULL) { 4813 /* 4814 * This changes the overall mode select completion 4815 * reason to a failed one !!!!! 4816 */ 4817 *scsipkt->pkt_scbp = STATUS_CHECK; 4818 sense = sata_arq_sense(spx); 4819 scsipkt->pkt_reason = CMD_INCOMPLETE; 4820 rval = TRAN_ACCEPT; 4821 } 4822 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4823 } 4824 /* Restore the scsi pkt flags */ 4825 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4826 scsipkt->pkt_flags |= nointr_flag; 4827 4828 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4829 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4830 4831 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4832 scsipkt->pkt_comp != NULL) 4833 /* scsi callback required */ 4834 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4835 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4836 TQ_SLEEP) == NULL) 4837 /* Scheduling the callback failed */ 4838 return (TRAN_BUSY); 4839 4840 return (rval); 4841 } 4842 4843 4844 4845 /* 4846 * Translate command: Log Sense 4847 */ 4848 static int 4849 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4850 { 4851 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4852 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4853 sata_drive_info_t *sdinfo; 4854 struct scsi_extended_sense *sense; 4855 int len, count, alc_len; 4856 int pc; /* Page Control code */ 4857 int page_code; /* Page code */ 4858 uint8_t *buf; /* log sense buffer */ 4859 int rval; 4860 #define MAX_LOG_SENSE_PAGE_SIZE 512 4861 4862 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4863 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4864 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4865 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4866 4867 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4868 4869 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4870 4871 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 4872 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 4873 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4874 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4875 return (rval); 4876 } 4877 4878 scsipkt->pkt_reason = CMD_CMPLT; 4879 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4880 STATE_SENT_CMD | STATE_GOT_STATUS; 4881 4882 pc = scsipkt->pkt_cdbp[2] >> 6; 4883 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4884 4885 /* Reject not supported request for all but cumulative values */ 4886 switch (pc) { 4887 case PC_CUMULATIVE_VALUES: 4888 break; 4889 default: 4890 *scsipkt->pkt_scbp = STATUS_CHECK; 4891 sense = sata_arq_sense(spx); 4892 sense->es_key = KEY_ILLEGAL_REQUEST; 4893 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 4894 goto done; 4895 } 4896 4897 switch (page_code) { 4898 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4899 case PAGE_CODE_SELF_TEST_RESULTS: 4900 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4901 case PAGE_CODE_SMART_READ_DATA: 4902 break; 4903 default: 4904 *scsipkt->pkt_scbp = STATUS_CHECK; 4905 sense = sata_arq_sense(spx); 4906 sense->es_key = KEY_ILLEGAL_REQUEST; 4907 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 4908 goto done; 4909 } 4910 4911 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4912 sata_id_t *sata_id; 4913 len = 0; 4914 4915 /* Build log parameter header */ 4916 buf[len++] = page_code; /* page code as in the CDB */ 4917 buf[len++] = 0; /* reserved */ 4918 buf[len++] = 0; /* Zero out page length for now (MSB) */ 4919 buf[len++] = 0; /* (LSB) */ 4920 4921 sdinfo = sata_get_device_info( 4922 spx->txlt_sata_hba_inst, 4923 &spx->txlt_sata_pkt->satapkt_device); 4924 4925 4926 /* 4927 * Add requested pages. 4928 */ 4929 switch (page_code) { 4930 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4931 len = sata_build_lsense_page_0(sdinfo, buf + len); 4932 break; 4933 case PAGE_CODE_SELF_TEST_RESULTS: 4934 sata_id = &sdinfo->satadrv_id; 4935 if ((! (sata_id->ai_cmdset84 & 4936 SATA_SMART_SELF_TEST_SUPPORTED)) || 4937 (! (sata_id->ai_features87 & 4938 SATA_SMART_SELF_TEST_SUPPORTED))) { 4939 *scsipkt->pkt_scbp = STATUS_CHECK; 4940 sense = sata_arq_sense(spx); 4941 sense->es_key = KEY_ILLEGAL_REQUEST; 4942 sense->es_add_code = 4943 SD_SCSI_INVALID_FIELD_IN_CDB; 4944 4945 goto done; 4946 } 4947 len = sata_build_lsense_page_10(sdinfo, buf + len, 4948 spx->txlt_sata_hba_inst); 4949 break; 4950 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4951 sata_id = &sdinfo->satadrv_id; 4952 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4953 *scsipkt->pkt_scbp = STATUS_CHECK; 4954 sense = sata_arq_sense(spx); 4955 sense->es_key = KEY_ILLEGAL_REQUEST; 4956 sense->es_add_code = 4957 SD_SCSI_INVALID_FIELD_IN_CDB; 4958 4959 goto done; 4960 } 4961 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4962 *scsipkt->pkt_scbp = STATUS_CHECK; 4963 sense = sata_arq_sense(spx); 4964 sense->es_key = KEY_ABORTED_COMMAND; 4965 sense->es_add_code = 4966 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4967 sense->es_qual_code = 4968 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4969 4970 goto done; 4971 } 4972 4973 len = sata_build_lsense_page_2f(sdinfo, buf + len, 4974 spx->txlt_sata_hba_inst); 4975 break; 4976 case PAGE_CODE_SMART_READ_DATA: 4977 sata_id = &sdinfo->satadrv_id; 4978 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4979 *scsipkt->pkt_scbp = STATUS_CHECK; 4980 sense = sata_arq_sense(spx); 4981 sense->es_key = KEY_ILLEGAL_REQUEST; 4982 sense->es_add_code = 4983 SD_SCSI_INVALID_FIELD_IN_CDB; 4984 4985 goto done; 4986 } 4987 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4988 *scsipkt->pkt_scbp = STATUS_CHECK; 4989 sense = sata_arq_sense(spx); 4990 sense->es_key = KEY_ABORTED_COMMAND; 4991 sense->es_add_code = 4992 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4993 sense->es_qual_code = 4994 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4995 4996 goto done; 4997 } 4998 4999 /* This page doesn't include a page header */ 5000 len = sata_build_lsense_page_30(sdinfo, buf, 5001 spx->txlt_sata_hba_inst); 5002 goto no_header; 5003 default: 5004 /* Invalid request */ 5005 *scsipkt->pkt_scbp = STATUS_CHECK; 5006 sense = sata_arq_sense(spx); 5007 sense->es_key = KEY_ILLEGAL_REQUEST; 5008 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 5009 goto done; 5010 } 5011 5012 /* set parameter log sense data length */ 5013 buf[2] = len >> 8; /* log sense length (MSB) */ 5014 buf[3] = len & 0xff; /* log sense length (LSB) */ 5015 5016 len += SCSI_LOG_PAGE_HDR_LEN; 5017 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 5018 5019 no_header: 5020 /* Check allocation length */ 5021 alc_len = scsipkt->pkt_cdbp[7]; 5022 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5023 5024 /* 5025 * We do not check for possible parameters truncation 5026 * (alc_len < len) assuming that the target driver works 5027 * correctly. Just avoiding overrun. 5028 * Copy no more than requested and possible, buffer-wise. 5029 */ 5030 count = MIN(alc_len, len); 5031 count = MIN(bp->b_bcount, count); 5032 bcopy(buf, bp->b_un.b_addr, count); 5033 5034 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5035 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5036 } 5037 *scsipkt->pkt_scbp = STATUS_GOOD; 5038 done: 5039 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5040 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5041 5042 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5043 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5044 5045 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5046 scsipkt->pkt_comp != NULL) 5047 /* scsi callback required */ 5048 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5049 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5050 TQ_SLEEP) == NULL) 5051 /* Scheduling the callback failed */ 5052 return (TRAN_BUSY); 5053 5054 return (TRAN_ACCEPT); 5055 } 5056 5057 /* 5058 * Translate command: Log Select 5059 * Not implemented at this time - returns invalid command response. 5060 */ 5061 static int 5062 sata_txlt_log_select(sata_pkt_txlate_t *spx) 5063 { 5064 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5065 "sata_txlt_log_select\n", NULL); 5066 5067 return (sata_txlt_invalid_command(spx)); 5068 } 5069 5070 5071 /* 5072 * Translate command: Read (various types). 5073 * Translated into appropriate type of ATA READ command 5074 * (NO ATAPI implementation yet). 5075 * Both the device capabilities and requested operation mode are 5076 * considered. 5077 * 5078 * Following scsi cdb fields are ignored: 5079 * rdprotect, dpo, fua, fua_nv, group_number. 5080 * 5081 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5082 * enable variable sata_func_enable), the capability of the controller and 5083 * capability of a device are checked and if both support queueing, read 5084 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 5085 * command rather than plain READ_XXX command. 5086 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5087 * both the controller and device suport such functionality, the read 5088 * request will be translated to READ_FPDMA_QUEUED command. 5089 * 5090 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5091 * appropriate values in scsi_pkt fields. 5092 */ 5093 static int 5094 sata_txlt_read(sata_pkt_txlate_t *spx) 5095 { 5096 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5097 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5098 sata_drive_info_t *sdinfo; 5099 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5100 int cport = SATA_TXLT_CPORT(spx); 5101 uint16_t sec_count; 5102 uint64_t lba; 5103 int rval; 5104 int synch; 5105 5106 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5107 5108 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5109 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5110 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5111 return (rval); 5112 } 5113 5114 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5115 &spx->txlt_sata_pkt->satapkt_device); 5116 5117 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5118 /* 5119 * Extract LBA and sector count from scsi CDB. 5120 */ 5121 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5122 case SCMD_READ: 5123 /* 6-byte scsi read cmd : 0x08 */ 5124 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5125 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5126 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5127 sec_count = scsipkt->pkt_cdbp[4]; 5128 /* sec_count 0 will be interpreted as 256 by a device */ 5129 break; 5130 case SCMD_READ_G1: 5131 /* 10-bytes scsi read command : 0x28 */ 5132 lba = scsipkt->pkt_cdbp[2]; 5133 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5134 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5135 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5136 sec_count = scsipkt->pkt_cdbp[7]; 5137 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5138 break; 5139 case SCMD_READ_G5: 5140 /* 12-bytes scsi read command : 0xA8 */ 5141 lba = scsipkt->pkt_cdbp[2]; 5142 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5143 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5144 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5145 sec_count = scsipkt->pkt_cdbp[6]; 5146 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5147 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5148 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5149 break; 5150 case SCMD_READ_G4: 5151 /* 16-bytes scsi read command : 0x88 */ 5152 lba = scsipkt->pkt_cdbp[2]; 5153 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5154 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5155 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5156 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5157 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5158 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5159 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5160 sec_count = scsipkt->pkt_cdbp[10]; 5161 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5162 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5163 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5164 break; 5165 default: 5166 /* Unsupported command */ 5167 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5168 return (sata_txlt_invalid_command(spx)); 5169 } 5170 5171 /* 5172 * Check if specified address exceeds device capacity 5173 */ 5174 if ((lba >= sdinfo->satadrv_capacity) || 5175 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5176 /* LBA out of range */ 5177 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5178 return (sata_txlt_lba_out_of_range(spx)); 5179 } 5180 5181 /* 5182 * For zero-length transfer, emulate good completion of the command 5183 * (reasons for rejecting the command were already checked). 5184 * No DMA resources were allocated. 5185 */ 5186 if (spx->txlt_dma_cookie_list == NULL) { 5187 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5188 return (sata_emul_rw_completion(spx)); 5189 } 5190 5191 /* 5192 * Build cmd block depending on the device capability and 5193 * requested operation mode. 5194 * Do not bother with non-dma mode - we are working only with 5195 * devices supporting DMA. 5196 */ 5197 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5198 scmd->satacmd_device_reg = SATA_ADH_LBA; 5199 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 5200 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5201 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5202 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 5203 scmd->satacmd_sec_count_msb = sec_count >> 8; 5204 #ifndef __lock_lint 5205 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5206 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5207 scmd->satacmd_lba_high_msb = lba >> 40; 5208 #endif 5209 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5210 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5211 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5212 } 5213 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5214 scmd->satacmd_lba_low_lsb = lba & 0xff; 5215 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5216 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5217 scmd->satacmd_features_reg = 0; 5218 scmd->satacmd_status_reg = 0; 5219 scmd->satacmd_error_reg = 0; 5220 5221 /* 5222 * Check if queueing commands should be used and switch 5223 * to appropriate command if possible 5224 */ 5225 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5226 boolean_t using_queuing; 5227 5228 /* Queuing supported by controller and device? */ 5229 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5230 (sdinfo->satadrv_features_support & 5231 SATA_DEV_F_NCQ) && 5232 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5233 SATA_CTLF_NCQ)) { 5234 using_queuing = B_TRUE; 5235 5236 /* NCQ supported - use FPDMA READ */ 5237 scmd->satacmd_cmd_reg = 5238 SATAC_READ_FPDMA_QUEUED; 5239 scmd->satacmd_features_reg_ext = 5240 scmd->satacmd_sec_count_msb; 5241 scmd->satacmd_sec_count_msb = 0; 5242 scmd->satacmd_rle_sata_cmd = &sata_rle_cmd; 5243 } else if ((sdinfo->satadrv_features_support & 5244 SATA_DEV_F_TCQ) && 5245 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5246 SATA_CTLF_QCMD)) { 5247 using_queuing = B_TRUE; 5248 5249 /* Legacy queueing */ 5250 if (sdinfo->satadrv_features_support & 5251 SATA_DEV_F_LBA48) { 5252 scmd->satacmd_cmd_reg = 5253 SATAC_READ_DMA_QUEUED_EXT; 5254 scmd->satacmd_features_reg_ext = 5255 scmd->satacmd_sec_count_msb; 5256 scmd->satacmd_sec_count_msb = 0; 5257 } else { 5258 scmd->satacmd_cmd_reg = 5259 SATAC_READ_DMA_QUEUED; 5260 } 5261 } else /* Queuing not supported */ 5262 using_queuing = B_FALSE; 5263 5264 /* 5265 * If queuing, the sector count goes in the features register 5266 * and the secount count will contain the tag. 5267 */ 5268 if (using_queuing) { 5269 scmd->satacmd_features_reg = 5270 scmd->satacmd_sec_count_lsb; 5271 scmd->satacmd_sec_count_lsb = 0; 5272 scmd->satacmd_flags.sata_queued = B_TRUE; 5273 } 5274 } 5275 5276 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 5277 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 5278 scmd->satacmd_cmd_reg, lba, sec_count); 5279 5280 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5281 /* Need callback function */ 5282 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5283 synch = FALSE; 5284 } else 5285 synch = TRUE; 5286 5287 /* Transfer command to HBA */ 5288 if (sata_hba_start(spx, &rval) != 0) { 5289 /* Pkt not accepted for execution */ 5290 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5291 return (rval); 5292 } 5293 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5294 /* 5295 * If execution is non-synchronous, 5296 * a callback function will handle potential errors, translate 5297 * the response and will do a callback to a target driver. 5298 * If it was synchronous, check execution status using the same 5299 * framework callback. 5300 */ 5301 if (synch) { 5302 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5303 "synchronous execution status %x\n", 5304 spx->txlt_sata_pkt->satapkt_reason); 5305 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5306 } 5307 return (TRAN_ACCEPT); 5308 } 5309 5310 5311 /* 5312 * SATA translate command: Write (various types) 5313 * Translated into appropriate type of ATA WRITE command 5314 * (NO ATAPI implementation yet). 5315 * Both the device capabilities and requested operation mode are 5316 * considered. 5317 * 5318 * Following scsi cdb fields are ignored: 5319 * rwprotect, dpo, fua, fua_nv, group_number. 5320 * 5321 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5322 * appropriate values in scsi_pkt fields. 5323 */ 5324 static int 5325 sata_txlt_write(sata_pkt_txlate_t *spx) 5326 { 5327 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5328 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5329 sata_drive_info_t *sdinfo; 5330 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5331 int cport = SATA_TXLT_CPORT(spx); 5332 uint16_t sec_count; 5333 uint64_t lba; 5334 int rval; 5335 int synch; 5336 5337 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5338 5339 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5340 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5341 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5342 return (rval); 5343 } 5344 5345 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5346 &spx->txlt_sata_pkt->satapkt_device); 5347 5348 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5349 /* 5350 * Extract LBA and sector count from scsi CDB 5351 */ 5352 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5353 case SCMD_WRITE: 5354 /* 6-byte scsi read cmd : 0x0A */ 5355 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5356 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5357 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5358 sec_count = scsipkt->pkt_cdbp[4]; 5359 /* sec_count 0 will be interpreted as 256 by a device */ 5360 break; 5361 case SCMD_WRITE_G1: 5362 /* 10-bytes scsi write command : 0x2A */ 5363 lba = scsipkt->pkt_cdbp[2]; 5364 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5365 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5366 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5367 sec_count = scsipkt->pkt_cdbp[7]; 5368 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5369 break; 5370 case SCMD_WRITE_G5: 5371 /* 12-bytes scsi read command : 0xAA */ 5372 lba = scsipkt->pkt_cdbp[2]; 5373 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5374 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5375 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5376 sec_count = scsipkt->pkt_cdbp[6]; 5377 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5378 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5379 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5380 break; 5381 case SCMD_WRITE_G4: 5382 /* 16-bytes scsi write command : 0x8A */ 5383 lba = scsipkt->pkt_cdbp[2]; 5384 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5385 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5386 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5387 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5388 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5389 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5390 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5391 sec_count = scsipkt->pkt_cdbp[10]; 5392 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5393 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5394 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5395 break; 5396 default: 5397 /* Unsupported command */ 5398 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5399 return (sata_txlt_invalid_command(spx)); 5400 } 5401 5402 /* 5403 * Check if specified address and length exceeds device capacity 5404 */ 5405 if ((lba >= sdinfo->satadrv_capacity) || 5406 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5407 /* LBA out of range */ 5408 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5409 return (sata_txlt_lba_out_of_range(spx)); 5410 } 5411 5412 /* 5413 * For zero-length transfer, emulate good completion of the command 5414 * (reasons for rejecting the command were already checked). 5415 * No DMA resources were allocated. 5416 */ 5417 if (spx->txlt_dma_cookie_list == NULL) { 5418 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5419 return (sata_emul_rw_completion(spx)); 5420 } 5421 5422 /* 5423 * Build cmd block depending on the device capability and 5424 * requested operation mode. 5425 * Do not bother with non-dma mode- we are working only with 5426 * devices supporting DMA. 5427 */ 5428 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5429 scmd->satacmd_device_reg = SATA_ADH_LBA; 5430 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 5431 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5432 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5433 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 5434 scmd->satacmd_sec_count_msb = sec_count >> 8; 5435 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5436 #ifndef __lock_lint 5437 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5438 scmd->satacmd_lba_high_msb = lba >> 40; 5439 #endif 5440 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5441 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5442 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5443 } 5444 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5445 scmd->satacmd_lba_low_lsb = lba & 0xff; 5446 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5447 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5448 scmd->satacmd_features_reg = 0; 5449 scmd->satacmd_status_reg = 0; 5450 scmd->satacmd_error_reg = 0; 5451 5452 /* 5453 * Check if queueing commands should be used and switch 5454 * to appropriate command if possible 5455 */ 5456 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5457 boolean_t using_queuing; 5458 5459 /* Queuing supported by controller and device? */ 5460 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5461 (sdinfo->satadrv_features_support & 5462 SATA_DEV_F_NCQ) && 5463 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5464 SATA_CTLF_NCQ)) { 5465 using_queuing = B_TRUE; 5466 5467 /* NCQ supported - use FPDMA WRITE */ 5468 scmd->satacmd_cmd_reg = 5469 SATAC_WRITE_FPDMA_QUEUED; 5470 scmd->satacmd_features_reg_ext = 5471 scmd->satacmd_sec_count_msb; 5472 scmd->satacmd_sec_count_msb = 0; 5473 scmd->satacmd_rle_sata_cmd = &sata_rle_cmd; 5474 } else if ((sdinfo->satadrv_features_support & 5475 SATA_DEV_F_TCQ) && 5476 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5477 SATA_CTLF_QCMD)) { 5478 using_queuing = B_TRUE; 5479 5480 /* Legacy queueing */ 5481 if (sdinfo->satadrv_features_support & 5482 SATA_DEV_F_LBA48) { 5483 scmd->satacmd_cmd_reg = 5484 SATAC_WRITE_DMA_QUEUED_EXT; 5485 scmd->satacmd_features_reg_ext = 5486 scmd->satacmd_sec_count_msb; 5487 scmd->satacmd_sec_count_msb = 0; 5488 } else { 5489 scmd->satacmd_cmd_reg = 5490 SATAC_WRITE_DMA_QUEUED; 5491 } 5492 } else /* Queuing not supported */ 5493 using_queuing = B_FALSE; 5494 5495 if (using_queuing) { 5496 scmd->satacmd_features_reg = 5497 scmd->satacmd_sec_count_lsb; 5498 scmd->satacmd_sec_count_lsb = 0; 5499 scmd->satacmd_flags.sata_queued = B_TRUE; 5500 } 5501 } 5502 5503 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5504 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 5505 scmd->satacmd_cmd_reg, lba, sec_count); 5506 5507 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5508 /* Need callback function */ 5509 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5510 synch = FALSE; 5511 } else 5512 synch = TRUE; 5513 5514 /* Transfer command to HBA */ 5515 if (sata_hba_start(spx, &rval) != 0) { 5516 /* Pkt not accepted for execution */ 5517 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5518 return (rval); 5519 } 5520 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5521 5522 /* 5523 * If execution is non-synchronous, 5524 * a callback function will handle potential errors, translate 5525 * the response and will do a callback to a target driver. 5526 * If it was synchronous, check execution status using the same 5527 * framework callback. 5528 */ 5529 if (synch) { 5530 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5531 "synchronous execution status %x\n", 5532 spx->txlt_sata_pkt->satapkt_reason); 5533 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5534 } 5535 return (TRAN_ACCEPT); 5536 } 5537 5538 5539 /* 5540 * Implements SCSI SBC WRITE BUFFER command download microcode option 5541 */ 5542 static int 5543 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 5544 { 5545 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 5546 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 5547 5548 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5549 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5550 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5551 struct scsi_extended_sense *sense; 5552 int rval, mode, sector_count; 5553 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5554 int cport = SATA_TXLT_CPORT(spx); 5555 boolean_t synch; 5556 5557 synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0; 5558 mode = scsipkt->pkt_cdbp[1] & 0x1f; 5559 5560 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5561 "sata_txlt_write_buffer, mode 0x%x\n", mode); 5562 5563 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5564 5565 if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) { 5566 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5567 return (rval); 5568 } 5569 5570 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5571 5572 scsipkt->pkt_reason = CMD_CMPLT; 5573 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5574 STATE_SENT_CMD | STATE_GOT_STATUS; 5575 5576 /* 5577 * The SCSI to ATA translation specification only calls 5578 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5579 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5580 * ATA 8 (draft) got rid of download microcode for temp 5581 * and it is even optional for ATA 7, so it may be aborted. 5582 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5583 * it is not specified and the buffer offset for SCSI is a 16-bit 5584 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5585 * sectors. Thus the offset really doesn't buy us anything. 5586 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5587 * is revised, this can be revisisted. 5588 */ 5589 /* Reject not supported request */ 5590 switch (mode) { 5591 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5592 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5593 break; 5594 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5595 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5596 break; 5597 default: 5598 goto bad_param; 5599 } 5600 5601 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5602 5603 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5604 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5605 goto bad_param; 5606 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5607 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5608 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5609 scmd->satacmd_lba_mid_lsb = 0; 5610 scmd->satacmd_lba_high_lsb = 0; 5611 scmd->satacmd_device_reg = 0; 5612 spx->txlt_sata_pkt->satapkt_comp = 5613 sata_txlt_download_mcode_cmd_completion; 5614 scmd->satacmd_addr_type = 0; 5615 5616 /* Transfer command to HBA */ 5617 if (sata_hba_start(spx, &rval) != 0) { 5618 /* Pkt not accepted for execution */ 5619 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5620 return (rval); 5621 } 5622 5623 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5624 /* 5625 * If execution is non-synchronous, 5626 * a callback function will handle potential errors, translate 5627 * the response and will do a callback to a target driver. 5628 * If it was synchronous, check execution status using the same 5629 * framework callback. 5630 */ 5631 if (synch) { 5632 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5633 "synchronous execution\n", NULL); 5634 /* Calling pre-set completion routine */ 5635 (*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt); 5636 } 5637 return (TRAN_ACCEPT); 5638 5639 bad_param: 5640 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5641 *scsipkt->pkt_scbp = STATUS_CHECK; 5642 sense = sata_arq_sense(spx); 5643 sense->es_key = KEY_ILLEGAL_REQUEST; 5644 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 5645 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5646 scsipkt->pkt_comp != NULL) { 5647 /* scsi callback required */ 5648 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5649 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5650 TQ_SLEEP) == 0) { 5651 /* Scheduling the callback failed */ 5652 rval = TRAN_BUSY; 5653 } 5654 } 5655 return (rval); 5656 } 5657 5658 5659 /* 5660 * Retry identify device when command returns SATA_INCOMPLETE_DATA 5661 * after doing a firmware download. 5662 */ 5663 static void 5664 sata_retry_identify_device(void *arg) 5665 { 5666 #define DOWNLOAD_WAIT_TIME_SECS 60 5667 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5668 int rval; 5669 int retry_cnt; 5670 sata_pkt_t *sata_pkt = (sata_pkt_t *)arg; 5671 sata_pkt_txlate_t *spx = 5672 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5673 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5674 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5675 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5676 sata_drive_info_t *sdinfo; 5677 5678 /* 5679 * Before returning good status, probe device. 5680 * Device probing will get IDENTIFY DEVICE data, if possible. 5681 * The assumption is that the new microcode is applied by the 5682 * device. It is a caller responsibility to verify this. 5683 */ 5684 for (retry_cnt = 0; 5685 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5686 retry_cnt++) { 5687 rval = sata_probe_device(sata_hba_inst, &sata_device); 5688 5689 if (rval == SATA_SUCCESS) { /* Set default features */ 5690 sdinfo = sata_get_device_info(sata_hba_inst, 5691 &sata_device); 5692 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5693 SATA_SUCCESS) { 5694 /* retry */ 5695 (void) sata_initialize_device(sata_hba_inst, 5696 sdinfo); 5697 } 5698 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5699 scsipkt->pkt_comp != NULL) 5700 (*scsipkt->pkt_comp)(scsipkt); 5701 return; 5702 } else if (rval == SATA_RETRY) { 5703 delay(drv_usectohz(1000000 * 5704 DOWNLOAD_WAIT_INTERVAL_SECS)); 5705 continue; 5706 } else /* failed - no reason to retry */ 5707 break; 5708 } 5709 5710 /* 5711 * Something went wrong, device probing failed. 5712 */ 5713 SATA_LOG_D((sata_hba_inst, CE_WARN, 5714 "Cannot probe device after downloading microcode\n")); 5715 5716 /* Reset device to force retrying the probe. */ 5717 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5718 (SATA_DIP(sata_hba_inst), &sata_device); 5719 5720 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5721 scsipkt->pkt_comp != NULL) 5722 (*scsipkt->pkt_comp)(scsipkt); 5723 } 5724 5725 /* 5726 * Translate completion status of download microcode command. 5727 * pkt completion_reason is checked to determine the completion status. 5728 * Do scsi callback if necessary (FLAG_NOINTR == 0) 5729 * 5730 * Note: this function may be called also for synchronously executed 5731 * command. 5732 * This function may be used only if scsi_pkt is non-NULL. 5733 */ 5734 static void 5735 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt) 5736 { 5737 sata_pkt_txlate_t *spx = 5738 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5739 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5740 struct scsi_extended_sense *sense; 5741 sata_drive_info_t *sdinfo; 5742 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5743 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5744 int rval; 5745 5746 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5747 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5748 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5749 scsipkt->pkt_reason = CMD_CMPLT; 5750 5751 rval = sata_probe_device(sata_hba_inst, &sata_device); 5752 5753 if (rval == SATA_SUCCESS) { /* Set default features */ 5754 sdinfo = sata_get_device_info(sata_hba_inst, 5755 &sata_device); 5756 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5757 SATA_SUCCESS) { 5758 /* retry */ 5759 (void) sata_initialize_device(sata_hba_inst, 5760 sdinfo); 5761 } 5762 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5763 scsipkt->pkt_comp != NULL) 5764 (*scsipkt->pkt_comp)(scsipkt); 5765 } else { 5766 (void) ddi_taskq_dispatch( 5767 (ddi_taskq_t *)SATA_TXLT_TASKQ(spx), 5768 sata_retry_identify_device, 5769 (void *)sata_pkt, TQ_NOSLEEP); 5770 } 5771 5772 5773 } else { 5774 /* Something went wrong, microcode download command failed */ 5775 scsipkt->pkt_reason = CMD_INCOMPLETE; 5776 *scsipkt->pkt_scbp = STATUS_CHECK; 5777 sense = sata_arq_sense(spx); 5778 switch (sata_pkt->satapkt_reason) { 5779 case SATA_PKT_PORT_ERROR: 5780 /* 5781 * We have no device data. Assume no data transfered. 5782 */ 5783 sense->es_key = KEY_HARDWARE_ERROR; 5784 break; 5785 5786 case SATA_PKT_DEV_ERROR: 5787 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5788 SATA_STATUS_ERR) { 5789 /* 5790 * determine dev error reason from error 5791 * reg content 5792 */ 5793 sata_decode_device_error(spx, sense); 5794 break; 5795 } 5796 /* No extended sense key - no info available */ 5797 break; 5798 5799 case SATA_PKT_TIMEOUT: 5800 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 5801 scsipkt->pkt_reason = CMD_INCOMPLETE; 5802 /* No extended sense key ? */ 5803 break; 5804 5805 case SATA_PKT_ABORTED: 5806 scsipkt->pkt_reason = CMD_ABORTED; 5807 /* No extended sense key ? */ 5808 break; 5809 5810 case SATA_PKT_RESET: 5811 /* pkt aborted by an explicit reset from a host */ 5812 scsipkt->pkt_reason = CMD_RESET; 5813 break; 5814 5815 default: 5816 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5817 "sata_txlt_nodata_cmd_completion: " 5818 "invalid packet completion reason %d", 5819 sata_pkt->satapkt_reason)); 5820 scsipkt->pkt_reason = CMD_TRAN_ERR; 5821 break; 5822 } 5823 5824 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5825 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5826 5827 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5828 scsipkt->pkt_comp != NULL) 5829 /* scsi callback required */ 5830 (*scsipkt->pkt_comp)(scsipkt); 5831 } 5832 } 5833 5834 5835 /* 5836 * NOTE: NOT FUNCTIONAL IMPLEMENTATION. THIS IS A PLACEHOLDER for the function 5837 * that will be fixed in phase 2 of the development. 5838 * Currently ATAPI is not supported. ATAPI devices are threated as not-valid 5839 * devices. 5840 * This function is not called, since scsi_sata_start() will bail-out prior 5841 * to calling it. 5842 */ 5843 static int 5844 sata_txlt_atapi(sata_pkt_txlate_t *spx) 5845 { 5846 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5847 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5848 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5849 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5850 int cport = SATA_TXLT_CPORT(spx); 5851 int rval; 5852 int synch; 5853 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 5854 5855 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5856 5857 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5858 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5859 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5860 return (rval); 5861 } 5862 5863 /* 5864 * scmd->satacmd_flags.sata_data_direction default - 5865 * SATA_DIR_NODATA_XFER - is set by 5866 * sata_txlt_generic_pkt_info(). 5867 */ 5868 if (scmd->satacmd_bp) { 5869 if (scmd->satacmd_bp->b_flags & B_READ) { 5870 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5871 } else { 5872 scmd->satacmd_flags.sata_data_direction = 5873 SATA_DIR_WRITE; 5874 } 5875 } 5876 5877 scmd->satacmd_acdb_len = scsi_cdb_size[GETGROUP(cdbp)]; 5878 scmd->satacmd_cmd_reg = SATAC_PACKET; 5879 bcopy(cdbp, scmd->satacmd_acdb, 16); 5880 5881 /* 5882 * For non-read/write commands we need to 5883 * map buffer 5884 */ 5885 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5886 case SCMD_READ: 5887 case SCMD_READ_G1: 5888 case SCMD_READ_G5: 5889 case SCMD_READ_G4: 5890 case SCMD_WRITE: 5891 case SCMD_WRITE_G1: 5892 case SCMD_WRITE_G5: 5893 case SCMD_WRITE_G4: 5894 break; 5895 default: 5896 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 5897 bp_mapin(bp); 5898 break; 5899 } 5900 5901 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5902 /* Need callback function */ 5903 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 5904 synch = FALSE; 5905 } else 5906 synch = TRUE; 5907 5908 /* Transfer command to HBA */ 5909 if (sata_hba_start(spx, &rval) != 0) { 5910 /* Pkt not accepted for execution */ 5911 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5912 return (rval); 5913 } 5914 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5915 /* 5916 * If execution is non-synchronous, 5917 * a callback function will handle potential errors, translate 5918 * the response and will do a callback to a target driver. 5919 * If it was synchronous, check execution status using the same 5920 * framework callback. 5921 */ 5922 if (synch) { 5923 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5924 "synchronous execution status %x\n", 5925 spx->txlt_sata_pkt->satapkt_reason); 5926 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 5927 } 5928 return (TRAN_ACCEPT); 5929 } 5930 5931 /* 5932 * Translate command: Synchronize Cache. 5933 * Translates into Flush Cache command. 5934 * (NO ATAPI implementation yet). 5935 * 5936 * NOTE: We should check if Flush Cache is supported by the device (ATAPI 5937 * devices) 5938 * 5939 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5940 * appropriate values in scsi_pkt fields. 5941 */ 5942 static int 5943 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5944 { 5945 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5946 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5947 int cport = SATA_TXLT_CPORT(spx); 5948 int rval; 5949 int synch; 5950 5951 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5952 5953 if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) || 5954 (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) { 5955 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5956 return (rval); 5957 } 5958 5959 scmd->satacmd_addr_type = 0; 5960 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 5961 scmd->satacmd_device_reg = 0; 5962 scmd->satacmd_sec_count_lsb = 0; 5963 scmd->satacmd_lba_low_lsb = 0; 5964 scmd->satacmd_lba_mid_lsb = 0; 5965 scmd->satacmd_lba_high_lsb = 0; 5966 scmd->satacmd_features_reg = 0; 5967 scmd->satacmd_status_reg = 0; 5968 scmd->satacmd_error_reg = 0; 5969 5970 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5971 "sata_txlt_synchronize_cache\n", NULL); 5972 5973 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5974 /* Need to set-up a callback function */ 5975 spx->txlt_sata_pkt->satapkt_comp = 5976 sata_txlt_nodata_cmd_completion; 5977 synch = FALSE; 5978 } else 5979 synch = TRUE; 5980 5981 /* Transfer command to HBA */ 5982 if (sata_hba_start(spx, &rval) != 0) { 5983 /* Pkt not accepted for execution */ 5984 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5985 return (rval); 5986 } 5987 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5988 5989 /* 5990 * If execution non-synchronous, it had to be completed 5991 * a callback function will handle potential errors, translate 5992 * the response and will do a callback to a target driver. 5993 * If it was synchronous, check status, using the same 5994 * framework callback. 5995 */ 5996 if (synch) { 5997 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5998 "synchronous execution status %x\n", 5999 spx->txlt_sata_pkt->satapkt_reason); 6000 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6001 } 6002 return (TRAN_ACCEPT); 6003 } 6004 6005 /* 6006 * Send pkt to SATA HBA driver 6007 * 6008 * This function may be called only if the operation is requested by scsi_pkt, 6009 * i.e. scsi_pkt is not NULL. 6010 * 6011 * This function has to be called with cport mutex held. It does release 6012 * the mutex when it calls HBA driver sata_tran_start function and 6013 * re-acquires it afterwards. 6014 * 6015 * If return value is 0, pkt was accepted, -1 otherwise 6016 * rval is set to appropriate sata_scsi_start return value. 6017 * 6018 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6019 * have called the sata_pkt callback function for this packet. 6020 * 6021 * The scsi callback has to be performed by the caller of this routine. 6022 * 6023 * Note 2: No port multiplier support for now. 6024 */ 6025 static int 6026 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6027 { 6028 int stat, cport; 6029 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6030 sata_drive_info_t *sdinfo; 6031 sata_device_t *sata_device; 6032 uint8_t cmd; 6033 struct sata_cmd_flags cmd_flags; 6034 6035 ASSERT(spx->txlt_sata_pkt != NULL); 6036 6037 cport = SATA_TXLT_CPORT(spx); 6038 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6039 6040 sdinfo = sata_get_device_info(sata_hba_inst, 6041 &spx->txlt_sata_pkt->satapkt_device); 6042 ASSERT(sdinfo != NULL); 6043 6044 /* Clear device reset state? */ 6045 if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 6046 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6047 sata_clear_dev_reset = B_TRUE; 6048 sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET; 6049 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6050 "sata_hba_start: clearing device reset state\n", NULL); 6051 } 6052 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 6053 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 6054 sata_device = &spx->txlt_sata_pkt->satapkt_device; 6055 6056 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6057 6058 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6059 "Sata cmd 0x%2x\n", cmd); 6060 6061 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 6062 spx->txlt_sata_pkt); 6063 6064 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6065 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6066 /* 6067 * If sata pkt was accepted and executed in asynchronous mode, i.e. 6068 * with the sata callback, the sata_pkt could be already destroyed 6069 * by the time we check ther return status from the hba_start() 6070 * function, because sata_scsi_destroy_pkt() could have been already 6071 * called (perhaps in the interrupt context). So, in such case, there 6072 * should be no references to it. In other cases, sata_pkt still 6073 * exists. 6074 */ 6075 switch (stat) { 6076 case SATA_TRAN_ACCEPTED: 6077 /* 6078 * pkt accepted for execution. 6079 * If it was executed synchronously, it is already completed 6080 * and pkt completion_reason indicates completion status. 6081 */ 6082 *rval = TRAN_ACCEPT; 6083 return (0); 6084 6085 case SATA_TRAN_QUEUE_FULL: 6086 /* 6087 * Controller detected queue full condition. 6088 */ 6089 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 6090 "sata_hba_start: queue full\n", NULL); 6091 6092 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6093 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 6094 6095 *rval = TRAN_BUSY; 6096 break; 6097 6098 case SATA_TRAN_PORT_ERROR: 6099 /* 6100 * Communication/link with device or general port error 6101 * detected before pkt execution begun. 6102 */ 6103 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6104 SATA_ADDR_CPORT || 6105 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6106 SATA_ADDR_DCPORT) 6107 sata_log(sata_hba_inst, CE_CONT, 6108 "SATA port %d error", 6109 sata_device->satadev_addr.cport); 6110 else 6111 sata_log(sata_hba_inst, CE_CONT, 6112 "SATA port %d pmport %d error\n", 6113 sata_device->satadev_addr.cport, 6114 sata_device->satadev_addr.pmport); 6115 6116 /* 6117 * Update the port/device structure. 6118 * sata_pkt should be still valid. Since port error is 6119 * returned, sata_device content should reflect port 6120 * state - it means, that sata address have been changed, 6121 * because original packet's sata address refered to a device 6122 * attached to some port. 6123 */ 6124 sata_update_port_info(sata_hba_inst, sata_device); 6125 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6126 *rval = TRAN_FATAL_ERROR; 6127 break; 6128 6129 case SATA_TRAN_CMD_UNSUPPORTED: 6130 /* 6131 * Command rejected by HBA as unsupported. It was HBA driver 6132 * that rejected the command, command was not sent to 6133 * an attached device. 6134 */ 6135 if ((sdinfo != NULL) && 6136 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 6137 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6138 "sat_hba_start: cmd 0x%2x rejected " 6139 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 6140 6141 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6142 (void) sata_txlt_invalid_command(spx); 6143 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6144 6145 *rval = TRAN_ACCEPT; 6146 break; 6147 6148 case SATA_TRAN_BUSY: 6149 /* 6150 * Command rejected by HBA because other operation prevents 6151 * accepting the packet, or device is in RESET condition. 6152 */ 6153 if (sdinfo != NULL) { 6154 sdinfo->satadrv_state = 6155 spx->txlt_sata_pkt->satapkt_device.satadev_state; 6156 6157 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 6158 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6159 "sata_hba_start: cmd 0x%2x rejected " 6160 "because of device reset condition\n", 6161 cmd); 6162 } else { 6163 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6164 "sata_hba_start: cmd 0x%2x rejected " 6165 "with SATA_TRAN_BUSY status\n", 6166 cmd); 6167 } 6168 } 6169 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6170 *rval = TRAN_BUSY; 6171 break; 6172 6173 default: 6174 /* Unrecognized HBA response */ 6175 SATA_LOG_D((sata_hba_inst, CE_WARN, 6176 "sata_hba_start: unrecognized HBA response " 6177 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 6178 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6179 *rval = TRAN_FATAL_ERROR; 6180 break; 6181 } 6182 6183 /* 6184 * If we got here, the packet was rejected. 6185 * Check if we need to remember reset state clearing request 6186 */ 6187 if (cmd_flags.sata_clear_dev_reset) { 6188 /* 6189 * Check if device is still configured - it may have 6190 * disapeared from the configuration 6191 */ 6192 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6193 if (sdinfo != NULL) { 6194 /* 6195 * Restore the flag that requests clearing of 6196 * the device reset state, 6197 * so the next sata packet may carry it to HBA. 6198 */ 6199 sdinfo->satadrv_event_flags |= 6200 SATA_EVNT_CLEAR_DEVICE_RESET; 6201 } 6202 } 6203 return (-1); 6204 } 6205 6206 /* 6207 * Scsi response setup for invalid LBA 6208 * 6209 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 6210 */ 6211 static int 6212 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 6213 { 6214 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6215 struct scsi_extended_sense *sense; 6216 6217 scsipkt->pkt_reason = CMD_CMPLT; 6218 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6219 STATE_SENT_CMD | STATE_GOT_STATUS; 6220 *scsipkt->pkt_scbp = STATUS_CHECK; 6221 6222 *scsipkt->pkt_scbp = STATUS_CHECK; 6223 sense = sata_arq_sense(spx); 6224 sense->es_key = KEY_ILLEGAL_REQUEST; 6225 sense->es_add_code = SD_SCSI_LBA_OUT_OF_RANGE; 6226 6227 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6228 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6229 6230 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6231 scsipkt->pkt_comp != NULL) 6232 /* scsi callback required */ 6233 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6234 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6235 TQ_SLEEP) == NULL) 6236 /* Scheduling the callback failed */ 6237 return (TRAN_BUSY); 6238 return (TRAN_ACCEPT); 6239 } 6240 6241 6242 /* 6243 * Analyze device status and error registers and translate them into 6244 * appropriate scsi sense codes. 6245 * NOTE: non-packet commands only for now 6246 */ 6247 static void 6248 sata_decode_device_error(sata_pkt_txlate_t *spx, 6249 struct scsi_extended_sense *sense) 6250 { 6251 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 6252 6253 ASSERT(sense != NULL); 6254 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 6255 SATA_STATUS_ERR); 6256 6257 6258 if (err_reg & SATA_ERROR_ICRC) { 6259 sense->es_key = KEY_ABORTED_COMMAND; 6260 sense->es_add_code = 0x08; /* Communication failure */ 6261 return; 6262 } 6263 6264 if (err_reg & SATA_ERROR_UNC) { 6265 sense->es_key = KEY_MEDIUM_ERROR; 6266 /* Information bytes (LBA) need to be set by a caller */ 6267 return; 6268 } 6269 6270 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 6271 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 6272 sense->es_key = KEY_UNIT_ATTENTION; 6273 sense->es_add_code = 0x3a; /* No media present */ 6274 return; 6275 } 6276 6277 if (err_reg & SATA_ERROR_IDNF) { 6278 if (err_reg & SATA_ERROR_ABORT) { 6279 sense->es_key = KEY_ABORTED_COMMAND; 6280 } else { 6281 sense->es_key = KEY_ILLEGAL_REQUEST; 6282 sense->es_add_code = 0x21; /* LBA out of range */ 6283 } 6284 return; 6285 } 6286 6287 if (err_reg & SATA_ERROR_ABORT) { 6288 ASSERT(spx->txlt_sata_pkt != NULL); 6289 sense->es_key = KEY_ABORTED_COMMAND; 6290 return; 6291 } 6292 } 6293 6294 /* 6295 * Extract error LBA from sata_pkt.satapkt_cmd register fields 6296 */ 6297 static void 6298 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 6299 { 6300 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 6301 6302 *lba = 0; 6303 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 6304 *lba = sata_cmd->satacmd_lba_high_msb; 6305 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 6306 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 6307 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 6308 *lba = sata_cmd->satacmd_device_reg & 0xf; 6309 } 6310 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 6311 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 6312 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 6313 } 6314 6315 /* 6316 * This is fixed sense format - if LBA exceeds the info field size, 6317 * no valid info will be returned (valid bit in extended sense will 6318 * be set to 0). 6319 */ 6320 static struct scsi_extended_sense * 6321 sata_arq_sense(sata_pkt_txlate_t *spx) 6322 { 6323 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6324 struct scsi_arq_status *arqs; 6325 struct scsi_extended_sense *sense; 6326 6327 /* Fill ARQ sense data */ 6328 scsipkt->pkt_state |= STATE_ARQ_DONE; 6329 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6330 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6331 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6332 arqs->sts_rqpkt_reason = CMD_CMPLT; 6333 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6334 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6335 arqs->sts_rqpkt_resid = 0; 6336 sense = &arqs->sts_sensedata; 6337 bzero(sense, sizeof (struct scsi_extended_sense)); 6338 sense->es_valid = 1; /* Valid sense */ 6339 sense->es_class = 7; /* Response code 0x70 - current err */ 6340 sense->es_key = KEY_NO_SENSE; 6341 sense->es_info_1 = 0; 6342 sense->es_info_2 = 0; 6343 sense->es_info_3 = 0; 6344 sense->es_info_4 = 0; 6345 sense->es_add_len = 6; /* Additional length */ 6346 sense->es_cmd_info[0] = 0; 6347 sense->es_cmd_info[1] = 0; 6348 sense->es_cmd_info[2] = 0; 6349 sense->es_cmd_info[3] = 0; 6350 sense->es_add_code = 0; 6351 sense->es_qual_code = 0; 6352 return (sense); 6353 } 6354 6355 6356 /* 6357 * Emulated SATA Read/Write command completion for zero-length requests. 6358 * This request always succedes, so in synchronous mode it always returns 6359 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 6360 * callback cannot be scheduled. 6361 */ 6362 static int 6363 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 6364 { 6365 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6366 6367 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6368 STATE_SENT_CMD | STATE_GOT_STATUS; 6369 scsipkt->pkt_reason = CMD_CMPLT; 6370 *scsipkt->pkt_scbp = STATUS_GOOD; 6371 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6372 /* scsi callback required - have to schedule it */ 6373 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6374 (task_func_t *)scsipkt->pkt_comp, 6375 (void *)scsipkt, TQ_SLEEP) == NULL) 6376 /* Scheduling the callback failed */ 6377 return (TRAN_BUSY); 6378 } 6379 return (TRAN_ACCEPT); 6380 } 6381 6382 6383 /* 6384 * Translate completion status of SATA read/write commands into scsi response. 6385 * pkt completion_reason is checked to determine the completion status. 6386 * Do scsi callback if necessary. 6387 * 6388 * Note: this function may be called also for synchronously executed 6389 * commands. 6390 * This function may be used only if scsi_pkt is non-NULL. 6391 */ 6392 static void 6393 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 6394 { 6395 sata_pkt_txlate_t *spx = 6396 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6397 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6398 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6399 struct scsi_extended_sense *sense; 6400 uint64_t lba; 6401 struct buf *bp; 6402 int rval; 6403 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6404 /* Normal completion */ 6405 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6406 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6407 scsipkt->pkt_reason = CMD_CMPLT; 6408 *scsipkt->pkt_scbp = STATUS_GOOD; 6409 if (spx->txlt_tmp_buf != NULL) { 6410 /* Temporary buffer was used */ 6411 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6412 if (bp->b_flags & B_READ) { 6413 rval = ddi_dma_sync( 6414 spx->txlt_buf_dma_handle, 0, 0, 6415 DDI_DMA_SYNC_FORCPU); 6416 ASSERT(rval == DDI_SUCCESS); 6417 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6418 bp->b_bcount); 6419 } 6420 } 6421 } else { 6422 /* 6423 * Something went wrong - analyze return 6424 */ 6425 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6426 STATE_SENT_CMD | STATE_GOT_STATUS; 6427 scsipkt->pkt_reason = CMD_INCOMPLETE; 6428 *scsipkt->pkt_scbp = STATUS_CHECK; 6429 sense = sata_arq_sense(spx); 6430 ASSERT(sense != NULL); 6431 6432 /* 6433 * SATA_PKT_DEV_ERROR is the only case where we may be able to 6434 * extract from device registers the failing LBA. 6435 */ 6436 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 6437 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 6438 (scmd->satacmd_lba_mid_msb != 0 || 6439 scmd->satacmd_lba_high_msb != 0)) { 6440 /* 6441 * We have problem reporting this cmd LBA 6442 * in fixed sense data format, because of 6443 * the size of the scsi LBA fields. 6444 */ 6445 sense->es_valid = 0; 6446 } else { 6447 sata_extract_error_lba(spx, &lba); 6448 sense->es_info_1 = (lba & 0xFF000000) >> 24; 6449 sense->es_info_2 = (lba & 0xFF0000) >> 16; 6450 sense->es_info_3 = (lba & 0xFF00) >> 8; 6451 sense->es_info_4 = lba & 0xFF; 6452 } 6453 } else { 6454 /* Invalid extended sense info */ 6455 sense->es_valid = 0; 6456 } 6457 6458 switch (sata_pkt->satapkt_reason) { 6459 case SATA_PKT_PORT_ERROR: 6460 /* We may want to handle DEV GONE state as well */ 6461 /* 6462 * We have no device data. Assume no data transfered. 6463 */ 6464 sense->es_key = KEY_HARDWARE_ERROR; 6465 break; 6466 6467 case SATA_PKT_DEV_ERROR: 6468 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6469 SATA_STATUS_ERR) { 6470 /* 6471 * determine dev error reason from error 6472 * reg content 6473 */ 6474 sata_decode_device_error(spx, sense); 6475 if (sense->es_key == KEY_MEDIUM_ERROR) { 6476 switch (scmd->satacmd_cmd_reg) { 6477 case SATAC_READ_DMA: 6478 case SATAC_READ_DMA_EXT: 6479 case SATAC_READ_DMA_QUEUED: 6480 case SATAC_READ_DMA_QUEUED_EXT: 6481 case SATAC_READ_FPDMA_QUEUED: 6482 /* Unrecovered read error */ 6483 sense->es_add_code = 6484 SD_SCSI_UNREC_READ_ERROR; 6485 break; 6486 case SATAC_WRITE_DMA: 6487 case SATAC_WRITE_DMA_EXT: 6488 case SATAC_WRITE_DMA_QUEUED: 6489 case SATAC_WRITE_DMA_QUEUED_EXT: 6490 case SATAC_WRITE_FPDMA_QUEUED: 6491 /* Write error */ 6492 sense->es_add_code = 6493 SD_SCSI_WRITE_ERROR; 6494 break; 6495 default: 6496 /* Internal error */ 6497 SATA_LOG_D(( 6498 spx->txlt_sata_hba_inst, 6499 CE_WARN, 6500 "sata_txlt_rw_completion :" 6501 "internal error - invalid " 6502 "command 0x%2x", 6503 scmd->satacmd_cmd_reg)); 6504 break; 6505 } 6506 } 6507 break; 6508 } 6509 /* No extended sense key - no info available */ 6510 scsipkt->pkt_reason = CMD_INCOMPLETE; 6511 break; 6512 6513 case SATA_PKT_TIMEOUT: 6514 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 6515 scsipkt->pkt_reason = CMD_INCOMPLETE; 6516 /* No extended sense key ? */ 6517 break; 6518 6519 case SATA_PKT_ABORTED: 6520 scsipkt->pkt_reason = CMD_ABORTED; 6521 /* No extended sense key ? */ 6522 break; 6523 6524 case SATA_PKT_RESET: 6525 scsipkt->pkt_reason = CMD_RESET; 6526 break; 6527 6528 default: 6529 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6530 "sata_txlt_rw_completion: " 6531 "invalid packet completion reason")); 6532 scsipkt->pkt_reason = CMD_TRAN_ERR; 6533 break; 6534 } 6535 } 6536 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6537 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6538 6539 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6540 scsipkt->pkt_comp != NULL) 6541 /* scsi callback required */ 6542 (*scsipkt->pkt_comp)(scsipkt); 6543 } 6544 6545 /* 6546 * NON FUNCTIONAL IMPLEMENTATION. THIS IS A PLACE HOLDER. 6547 * ATAPI devices are not supported currently (are not be attached recognized 6548 * as valid devices). 6549 * Will be fixed in phase 2 of the development. 6550 */ 6551 static void 6552 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 6553 { 6554 sata_pkt_txlate_t *spx = 6555 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6556 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6557 struct scsi_arq_status *arqs; 6558 6559 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6560 /* Normal completion */ 6561 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6562 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6563 scsipkt->pkt_reason = CMD_CMPLT; 6564 *scsipkt->pkt_scbp = STATUS_GOOD; 6565 scsipkt->pkt_resid = 0; 6566 } else { 6567 /* 6568 * Something went wrong - analyze return 6569 */ 6570 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6571 STATE_SENT_CMD | STATE_GOT_STATUS | STATE_ARQ_DONE; 6572 scsipkt->pkt_reason = CMD_CMPLT; 6573 6574 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6575 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6576 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6577 arqs->sts_rqpkt_reason = CMD_CMPLT; 6578 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6579 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6580 arqs->sts_rqpkt_resid = 0; 6581 6582 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, 6583 &arqs->sts_sensedata, SATA_ATAPI_RQSENSE_LEN); 6584 } 6585 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6586 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6587 6588 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6589 scsipkt->pkt_comp != NULL) { 6590 /* scsi callback required */ 6591 (*scsipkt->pkt_comp)(scsipkt); 6592 } 6593 } 6594 6595 6596 /* 6597 * Translate completion status of non-data commands (i.e. commands returning 6598 * no data). 6599 * pkt completion_reason is checked to determine the completion status. 6600 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6601 * 6602 * Note: this function may be called also for synchronously executed 6603 * commands. 6604 * This function may be used only if scsi_pkt is non-NULL. 6605 */ 6606 6607 static void 6608 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 6609 { 6610 sata_pkt_txlate_t *spx = 6611 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6612 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6613 struct scsi_extended_sense *sense; 6614 6615 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6616 STATE_SENT_CMD | STATE_GOT_STATUS; 6617 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6618 /* Normal completion */ 6619 scsipkt->pkt_reason = CMD_CMPLT; 6620 *scsipkt->pkt_scbp = STATUS_GOOD; 6621 } else { 6622 /* Something went wrong */ 6623 scsipkt->pkt_reason = CMD_INCOMPLETE; 6624 *scsipkt->pkt_scbp = STATUS_CHECK; 6625 sense = sata_arq_sense(spx); 6626 switch (sata_pkt->satapkt_reason) { 6627 case SATA_PKT_PORT_ERROR: 6628 /* 6629 * We have no device data. Assume no data transfered. 6630 */ 6631 sense->es_key = KEY_HARDWARE_ERROR; 6632 break; 6633 6634 case SATA_PKT_DEV_ERROR: 6635 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6636 SATA_STATUS_ERR) { 6637 /* 6638 * determine dev error reason from error 6639 * reg content 6640 */ 6641 sata_decode_device_error(spx, sense); 6642 break; 6643 } 6644 /* No extended sense key - no info available */ 6645 break; 6646 6647 case SATA_PKT_TIMEOUT: 6648 /* scsipkt->pkt_reason = CMD_TIMEOUT; */ 6649 scsipkt->pkt_reason = CMD_INCOMPLETE; 6650 /* No extended sense key ? */ 6651 break; 6652 6653 case SATA_PKT_ABORTED: 6654 scsipkt->pkt_reason = CMD_ABORTED; 6655 /* No extended sense key ? */ 6656 break; 6657 6658 case SATA_PKT_RESET: 6659 /* pkt aborted by an explicit reset from a host */ 6660 scsipkt->pkt_reason = CMD_RESET; 6661 break; 6662 6663 default: 6664 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6665 "sata_txlt_nodata_cmd_completion: " 6666 "invalid packet completion reason %d", 6667 sata_pkt->satapkt_reason)); 6668 scsipkt->pkt_reason = CMD_TRAN_ERR; 6669 break; 6670 } 6671 6672 } 6673 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6674 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6675 6676 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6677 scsipkt->pkt_comp != NULL) 6678 /* scsi callback required */ 6679 (*scsipkt->pkt_comp)(scsipkt); 6680 } 6681 6682 6683 /* 6684 * Build Mode sense R/W recovery page 6685 * NOT IMPLEMENTED 6686 */ 6687 6688 static int 6689 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6690 { 6691 #ifndef __lock_lint 6692 _NOTE(ARGUNUSED(sdinfo)) 6693 _NOTE(ARGUNUSED(pcntrl)) 6694 _NOTE(ARGUNUSED(buf)) 6695 #endif 6696 return (0); 6697 } 6698 6699 /* 6700 * Build Mode sense caching page - scsi-3 implementation. 6701 * Page length distinguishes previous format from scsi-3 format. 6702 * buf must have space for 0x12 bytes. 6703 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 6704 * 6705 */ 6706 static int 6707 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6708 { 6709 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 6710 sata_id_t *sata_id = &sdinfo->satadrv_id; 6711 6712 /* 6713 * Most of the fields are set to 0, being not supported and/or disabled 6714 */ 6715 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 6716 6717 /* Saved paramters not supported */ 6718 if (pcntrl == 3) 6719 return (0); 6720 if (pcntrl == 0 || pcntrl == 2) { 6721 /* 6722 * For now treat current and default parameters as same 6723 * That may have to change, if target driver will complain 6724 */ 6725 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 6726 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6727 6728 if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 6729 !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) { 6730 page->dra = 1; /* Read Ahead disabled */ 6731 page->rcd = 1; /* Read Cache disabled */ 6732 } 6733 if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) && 6734 (sata_id->ai_features85 & SATA_WRITE_CACHE)) 6735 page->wce = 1; /* Write Cache enabled */ 6736 } else { 6737 /* Changeable parameters */ 6738 page->mode_page.code = MODEPAGE_CACHING; 6739 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6740 if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) { 6741 page->dra = 1; 6742 page->rcd = 1; 6743 } 6744 if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE) 6745 page->wce = 1; 6746 } 6747 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6748 sizeof (struct mode_page)); 6749 } 6750 6751 /* 6752 * Build Mode sense exception cntrl page 6753 */ 6754 static int 6755 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6756 { 6757 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 6758 sata_id_t *sata_id = &sdinfo->satadrv_id; 6759 6760 /* 6761 * Most of the fields are set to 0, being not supported and/or disabled 6762 */ 6763 bzero(buf, PAGELENGTH_INFO_EXCPT); 6764 6765 page->mode_page.code = MODEPAGE_INFO_EXCPT; 6766 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 6767 6768 /* Indicate that this is page is saveable */ 6769 page->mode_page.ps = 1; 6770 6771 /* 6772 * We will return the same data for default, current and saved page. 6773 * The only changeable bit is dexcpt and that bit is required 6774 * by the ATA specification to be preserved across power cycles. 6775 */ 6776 if (pcntrl != 1) { 6777 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6778 page->mrie = MRIE_ONLY_ON_REQUEST; 6779 } 6780 else 6781 page->dexcpt = 1; /* Only changeable parameter */ 6782 6783 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page)); 6784 } 6785 6786 6787 /* 6788 * Build Mode sense power condition page 6789 * NOT IMPLEMENTED. 6790 */ 6791 static int 6792 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6793 { 6794 #ifndef __lock_lint 6795 _NOTE(ARGUNUSED(sdinfo)) 6796 _NOTE(ARGUNUSED(pcntrl)) 6797 _NOTE(ARGUNUSED(buf)) 6798 #endif 6799 return (0); 6800 } 6801 6802 6803 /* 6804 * Process mode select caching page 8 (scsi3 format only). 6805 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6806 * if these features are supported by the device. If these features are not 6807 * supported, quietly ignore them. 6808 * This function fails only if the SET FEATURE command sent to 6809 * the device fails. The page format is not varified, assuming that the 6810 * target driver operates correctly - if parameters length is too short, 6811 * we just drop the page. 6812 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6813 * setting have to be changed. 6814 * SET FEATURE command is executed synchronously, i.e. we wait here until 6815 * it is completed, regardless of the scsi pkt directives. 6816 * 6817 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6818 * changing DRA will change RCD. 6819 * 6820 * More than one SATA command may be executed to perform operations specified 6821 * by mode select pages. The first error terminates further execution. 6822 * Operations performed successully are not backed-up in such case. 6823 * 6824 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6825 * If operation resulted in changing device setup, dmod flag should be set to 6826 * one (1). If parameters were not changed, dmod flag should be set to 0. 6827 * Upon return, if operation required sending command to the device, the rval 6828 * should be set to the value returned by sata_hba_start. If operation 6829 * did not require device access, rval should be set to TRAN_ACCEPT. 6830 * The pagelen should be set to the length of the page. 6831 * 6832 * This function has to be called with a port mutex held. 6833 * 6834 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6835 */ 6836 int 6837 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6838 int parmlen, int *pagelen, int *rval, int *dmod) 6839 { 6840 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6841 sata_drive_info_t *sdinfo; 6842 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6843 sata_id_t *sata_id; 6844 struct scsi_extended_sense *sense; 6845 int wce, dra; /* Current settings */ 6846 6847 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6848 &spx->txlt_sata_pkt->satapkt_device); 6849 sata_id = &sdinfo->satadrv_id; 6850 *dmod = 0; 6851 6852 /* Verify parameters length. If too short, drop it */ 6853 if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6854 sizeof (struct mode_page) < parmlen) { 6855 *scsipkt->pkt_scbp = STATUS_CHECK; 6856 sense = sata_arq_sense(spx); 6857 sense->es_key = KEY_ILLEGAL_REQUEST; 6858 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_PARAMETER_LIST; 6859 *pagelen = parmlen; 6860 *rval = TRAN_ACCEPT; 6861 return (SATA_FAILURE); 6862 } 6863 6864 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6865 6866 /* 6867 * We can manipulate only write cache and read ahead 6868 * (read cache) setting. 6869 */ 6870 if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 6871 !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) { 6872 /* 6873 * None of the features is supported - ignore 6874 */ 6875 *rval = TRAN_ACCEPT; 6876 return (SATA_SUCCESS); 6877 } 6878 6879 /* Current setting of Read Ahead (and Read Cache) */ 6880 if (sata_id->ai_features85 & SATA_LOOK_AHEAD) 6881 dra = 0; /* 0 == not disabled */ 6882 else 6883 dra = 1; 6884 /* Current setting of Write Cache */ 6885 if (sata_id->ai_features85 & SATA_WRITE_CACHE) 6886 wce = 1; 6887 else 6888 wce = 0; 6889 6890 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 6891 /* nothing to do */ 6892 *rval = TRAN_ACCEPT; 6893 return (SATA_SUCCESS); 6894 } 6895 /* 6896 * Need to flip some setting 6897 * Set-up Internal SET FEATURES command(s) 6898 */ 6899 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6900 scmd->satacmd_addr_type = 0; 6901 scmd->satacmd_device_reg = 0; 6902 scmd->satacmd_status_reg = 0; 6903 scmd->satacmd_error_reg = 0; 6904 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6905 if (page->dra != dra || page->rcd != dra) { 6906 /* Need to flip read ahead setting */ 6907 if (dra == 0) 6908 /* Disable read ahead / read cache */ 6909 scmd->satacmd_features_reg = 6910 SATAC_SF_DISABLE_READ_AHEAD; 6911 else 6912 /* Enable read ahead / read cache */ 6913 scmd->satacmd_features_reg = 6914 SATAC_SF_ENABLE_READ_AHEAD; 6915 6916 /* Transfer command to HBA */ 6917 if (sata_hba_start(spx, rval) != 0) 6918 /* 6919 * Pkt not accepted for execution. 6920 */ 6921 return (SATA_FAILURE); 6922 6923 *dmod = 1; 6924 6925 /* Now process return */ 6926 if (spx->txlt_sata_pkt->satapkt_reason != 6927 SATA_PKT_COMPLETED) { 6928 goto failure; /* Terminate */ 6929 } 6930 } 6931 6932 /* Note that the packet is not removed, so it could be re-used */ 6933 if (page->wce != wce) { 6934 /* Need to flip Write Cache setting */ 6935 if (page->wce == 1) 6936 /* Enable write cache */ 6937 scmd->satacmd_features_reg = 6938 SATAC_SF_ENABLE_WRITE_CACHE; 6939 else 6940 /* Disable write cache */ 6941 scmd->satacmd_features_reg = 6942 SATAC_SF_DISABLE_WRITE_CACHE; 6943 6944 /* Transfer command to HBA */ 6945 if (sata_hba_start(spx, rval) != 0) 6946 /* 6947 * Pkt not accepted for execution. 6948 */ 6949 return (SATA_FAILURE); 6950 6951 *dmod = 1; 6952 6953 /* Now process return */ 6954 if (spx->txlt_sata_pkt->satapkt_reason != 6955 SATA_PKT_COMPLETED) { 6956 goto failure; 6957 } 6958 } 6959 return (SATA_SUCCESS); 6960 6961 failure: 6962 sata_xlate_errors(spx); 6963 6964 return (SATA_FAILURE); 6965 } 6966 6967 /* 6968 * Process mode select informational exceptions control page 0x1c 6969 * 6970 * The only changeable bit is dexcpt (disable exceptions). 6971 * MRIE (method of reporting informational exceptions) must be 6972 * "only on request". 6973 * 6974 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6975 * If operation resulted in changing device setup, dmod flag should be set to 6976 * one (1). If parameters were not changed, dmod flag should be set to 0. 6977 * Upon return, if operation required sending command to the device, the rval 6978 * should be set to the value returned by sata_hba_start. If operation 6979 * did not require device access, rval should be set to TRAN_ACCEPT. 6980 * The pagelen should be set to the length of the page. 6981 * 6982 * This function has to be called with a port mutex held. 6983 * 6984 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6985 */ 6986 static int 6987 sata_mode_select_page_1c( 6988 sata_pkt_txlate_t *spx, 6989 struct mode_info_excpt_page *page, 6990 int parmlen, 6991 int *pagelen, 6992 int *rval, 6993 int *dmod) 6994 { 6995 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6996 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6997 sata_drive_info_t *sdinfo; 6998 sata_id_t *sata_id; 6999 struct scsi_extended_sense *sense; 7000 7001 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7002 &spx->txlt_sata_pkt->satapkt_device); 7003 sata_id = &sdinfo->satadrv_id; 7004 7005 *dmod = 0; 7006 7007 /* Verify parameters length. If too short, drop it */ 7008 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) || 7009 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 7010 *scsipkt->pkt_scbp = STATUS_CHECK; 7011 sense = sata_arq_sense(spx); 7012 sense->es_key = KEY_ILLEGAL_REQUEST; 7013 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_PARAMETER_LIST; 7014 *pagelen = parmlen; 7015 *rval = TRAN_ACCEPT; 7016 return (SATA_FAILURE); 7017 } 7018 7019 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 7020 7021 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 7022 *scsipkt->pkt_scbp = STATUS_CHECK; 7023 sense = sata_arq_sense(spx); 7024 sense->es_key = KEY_ILLEGAL_REQUEST; 7025 sense->es_add_code = SD_SCSI_INVALID_FIELD_IN_CDB; 7026 *pagelen = parmlen; 7027 *rval = TRAN_ACCEPT; 7028 return (SATA_FAILURE); 7029 } 7030 7031 /* If already in the state requested, we are done */ 7032 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 7033 /* nothing to do */ 7034 *rval = TRAN_ACCEPT; 7035 return (SATA_SUCCESS); 7036 } 7037 7038 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7039 7040 /* Build SMART_ENABLE or SMART_DISABLE command */ 7041 scmd->satacmd_addr_type = 0; /* N/A */ 7042 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 7043 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 7044 scmd->satacmd_features_reg = page->dexcpt ? 7045 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 7046 scmd->satacmd_device_reg = 0; /* Always device 0 */ 7047 scmd->satacmd_cmd_reg = SATAC_SMART; 7048 7049 /* Transfer command to HBA */ 7050 if (sata_hba_start(spx, rval) != 0) 7051 /* 7052 * Pkt not accepted for execution. 7053 */ 7054 return (SATA_FAILURE); 7055 7056 *dmod = 1; /* At least may have been modified */ 7057 7058 /* Now process return */ 7059 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 7060 return (SATA_SUCCESS); 7061 7062 /* Packet did not complete successfully */ 7063 sata_xlate_errors(spx); 7064 7065 return (SATA_FAILURE); 7066 } 7067 7068 /* 7069 * sata_build_lsense_page0() is used to create the 7070 * SCSI LOG SENSE page 0 (supported log pages) 7071 * 7072 * Currently supported pages are 0, 0x10, 0x2f and 0x30 7073 * (supported log pages, self-test results, informational exceptions 7074 * and Sun vendor specific ATA SMART data). 7075 * 7076 * Takes a sata_drive_info t * and the address of a buffer 7077 * in which to create the page information. 7078 * 7079 * Returns the number of bytes valid in the buffer. 7080 */ 7081 static int 7082 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 7083 { 7084 struct log_parameter *lpp = (struct log_parameter *)buf; 7085 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 7086 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 7087 sata_id_t *sata_id = &sdinfo->satadrv_id; 7088 7089 lpp->param_code[0] = 0; 7090 lpp->param_code[1] = 0; 7091 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7092 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 7093 7094 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 7095 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 7096 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 7097 ++num_pages_supported; 7098 } 7099 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 7100 ++num_pages_supported; 7101 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 7102 ++num_pages_supported; 7103 } 7104 7105 lpp->param_len = num_pages_supported; 7106 7107 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 7108 num_pages_supported); 7109 } 7110 7111 /* 7112 * sata_build_lsense_page_10() is used to create the 7113 * SCSI LOG SENSE page 0x10 (self-test results) 7114 * 7115 * Takes a sata_drive_info t * and the address of a buffer 7116 * in which to create the page information as well as a sata_hba_inst_t *. 7117 * 7118 * Returns the number of bytes valid in the buffer. 7119 */ 7120 static int 7121 sata_build_lsense_page_10( 7122 sata_drive_info_t *sdinfo, 7123 uint8_t *buf, 7124 sata_hba_inst_t *sata_hba_inst) 7125 { 7126 struct log_parameter *lpp = (struct log_parameter *)buf; 7127 int rval; 7128 7129 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 7130 struct smart_ext_selftest_log *ext_selftest_log; 7131 7132 ext_selftest_log = kmem_zalloc( 7133 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 7134 7135 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 7136 ext_selftest_log, 0); 7137 if (rval == 0) { 7138 int index, start_index; 7139 struct smart_ext_selftest_log_entry *entry; 7140 static const struct smart_ext_selftest_log_entry empty = 7141 {0}; 7142 uint16_t block_num; 7143 int count; 7144 boolean_t only_one_block = B_FALSE; 7145 7146 index = ext_selftest_log-> 7147 smart_ext_selftest_log_index[0]; 7148 index |= ext_selftest_log-> 7149 smart_ext_selftest_log_index[1] << 8; 7150 if (index == 0) 7151 goto out; 7152 7153 --index; /* Correct for 0 origin */ 7154 start_index = index; /* remember where we started */ 7155 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7156 if (block_num != 0) { 7157 rval = sata_ext_smart_selftest_read_log( 7158 sata_hba_inst, sdinfo, ext_selftest_log, 7159 block_num); 7160 if (rval != 0) 7161 goto out; 7162 } 7163 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7164 entry = 7165 &ext_selftest_log-> 7166 smart_ext_selftest_log_entries[index]; 7167 7168 for (count = 1; 7169 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7170 ++count) { 7171 uint8_t status; 7172 uint8_t code; 7173 uint8_t sense_key; 7174 uint8_t add_sense_code; 7175 uint8_t add_sense_code_qual; 7176 7177 /* If this is an unused entry, we are done */ 7178 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 7179 /* Broken firmware on some disks */ 7180 if (index + 1 == 7181 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 7182 --entry; 7183 --index; 7184 if (bcmp(entry, &empty, 7185 sizeof (empty)) == 0) 7186 goto out; 7187 } else 7188 goto out; 7189 } 7190 7191 if (only_one_block && 7192 start_index == index) 7193 goto out; 7194 7195 lpp->param_code[0] = 0; 7196 lpp->param_code[1] = count; 7197 lpp->param_ctrl_flags = 7198 LOG_CTRL_LP | LOG_CTRL_LBIN; 7199 lpp->param_len = 7200 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7201 7202 status = entry->smart_ext_selftest_log_status; 7203 status >>= 4; 7204 switch (status) { 7205 case 0: 7206 default: 7207 sense_key = KEY_NO_SENSE; 7208 add_sense_code = SD_SCSI_NO_ADD_SENSE; 7209 add_sense_code_qual = 0; 7210 break; 7211 case 1: 7212 sense_key = KEY_ABORTED_COMMAND; 7213 add_sense_code = 7214 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7215 add_sense_code_qual = SCSI_COMPONENT_81; 7216 break; 7217 case 2: 7218 sense_key = KEY_ABORTED_COMMAND; 7219 add_sense_code = 7220 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7221 add_sense_code_qual = SCSI_COMPONENT_82; 7222 break; 7223 case 3: 7224 sense_key = KEY_ABORTED_COMMAND; 7225 add_sense_code = 7226 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7227 add_sense_code_qual = SCSI_COMPONENT_83; 7228 break; 7229 case 4: 7230 sense_key = KEY_HARDWARE_ERROR; 7231 add_sense_code = 7232 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7233 add_sense_code_qual = SCSI_COMPONENT_84; 7234 break; 7235 case 5: 7236 sense_key = KEY_HARDWARE_ERROR; 7237 add_sense_code = 7238 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7239 add_sense_code_qual = SCSI_COMPONENT_85; 7240 break; 7241 case 6: 7242 sense_key = KEY_HARDWARE_ERROR; 7243 add_sense_code = 7244 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7245 add_sense_code_qual = SCSI_COMPONENT_86; 7246 break; 7247 case 7: 7248 sense_key = KEY_MEDIUM_ERROR; 7249 add_sense_code = 7250 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7251 add_sense_code_qual = SCSI_COMPONENT_87; 7252 break; 7253 case 8: 7254 sense_key = KEY_HARDWARE_ERROR; 7255 add_sense_code = 7256 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7257 add_sense_code_qual = SCSI_COMPONENT_88; 7258 break; 7259 } 7260 code = 0; /* unspecified */ 7261 status |= (code << 4); 7262 lpp->param_values[0] = status; 7263 lpp->param_values[1] = 0; /* unspecified */ 7264 lpp->param_values[2] = entry-> 7265 smart_ext_selftest_log_timestamp[1]; 7266 lpp->param_values[3] = entry-> 7267 smart_ext_selftest_log_timestamp[0]; 7268 if (status != 0) { 7269 lpp->param_values[4] = 0; 7270 lpp->param_values[5] = 0; 7271 lpp->param_values[6] = entry-> 7272 smart_ext_selftest_log_failing_lba 7273 [5]; 7274 lpp->param_values[7] = entry-> 7275 smart_ext_selftest_log_failing_lba 7276 [4]; 7277 lpp->param_values[8] = entry-> 7278 smart_ext_selftest_log_failing_lba 7279 [3]; 7280 lpp->param_values[9] = entry-> 7281 smart_ext_selftest_log_failing_lba 7282 [2]; 7283 lpp->param_values[10] = entry-> 7284 smart_ext_selftest_log_failing_lba 7285 [1]; 7286 lpp->param_values[11] = entry-> 7287 smart_ext_selftest_log_failing_lba 7288 [0]; 7289 } else { /* No bad block address */ 7290 lpp->param_values[4] = 0xff; 7291 lpp->param_values[5] = 0xff; 7292 lpp->param_values[6] = 0xff; 7293 lpp->param_values[7] = 0xff; 7294 lpp->param_values[8] = 0xff; 7295 lpp->param_values[9] = 0xff; 7296 lpp->param_values[10] = 0xff; 7297 lpp->param_values[11] = 0xff; 7298 } 7299 7300 lpp->param_values[12] = sense_key; 7301 lpp->param_values[13] = add_sense_code; 7302 lpp->param_values[14] = add_sense_code_qual; 7303 lpp->param_values[15] = 0; /* undefined */ 7304 7305 lpp = (struct log_parameter *) 7306 (((uint8_t *)lpp) + 7307 SCSI_LOG_PARAM_HDR_LEN + 7308 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7309 7310 --index; /* Back up to previous entry */ 7311 if (index < 0) { 7312 if (block_num > 0) { 7313 --block_num; 7314 } else { 7315 struct read_log_ext_directory 7316 logdir; 7317 7318 rval = 7319 sata_read_log_ext_directory( 7320 sata_hba_inst, sdinfo, 7321 &logdir); 7322 if (rval == -1) 7323 goto out; 7324 if ((logdir.read_log_ext_vers 7325 [0] == 0) && 7326 (logdir.read_log_ext_vers 7327 [1] == 0)) 7328 goto out; 7329 block_num = 7330 logdir.read_log_ext_nblks 7331 [EXT_SMART_SELFTEST_LOG_PAGE 7332 - 1][0]; 7333 block_num |= logdir. 7334 read_log_ext_nblks 7335 [EXT_SMART_SELFTEST_LOG_PAGE 7336 - 1][1] << 8; 7337 --block_num; 7338 only_one_block = 7339 (block_num == 0); 7340 } 7341 rval = sata_ext_smart_selftest_read_log( 7342 sata_hba_inst, sdinfo, 7343 ext_selftest_log, block_num); 7344 if (rval != 0) 7345 goto out; 7346 7347 index = 7348 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 7349 1; 7350 } 7351 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7352 entry = &ext_selftest_log-> 7353 smart_ext_selftest_log_entries[index]; 7354 } 7355 } 7356 out: 7357 kmem_free(ext_selftest_log, 7358 sizeof (struct smart_ext_selftest_log)); 7359 } else { 7360 struct smart_selftest_log *selftest_log; 7361 7362 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 7363 KM_SLEEP); 7364 7365 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 7366 selftest_log); 7367 7368 if (rval == 0) { 7369 int index; 7370 int count; 7371 struct smart_selftest_log_entry *entry; 7372 static const struct smart_selftest_log_entry empty = 7373 { 0 }; 7374 7375 index = selftest_log->smart_selftest_log_index; 7376 if (index == 0) 7377 goto done; 7378 --index; /* Correct for 0 origin */ 7379 entry = &selftest_log-> 7380 smart_selftest_log_entries[index]; 7381 for (count = 1; 7382 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7383 ++count) { 7384 uint8_t status; 7385 uint8_t code; 7386 uint8_t sense_key; 7387 uint8_t add_sense_code; 7388 uint8_t add_sense_code_qual; 7389 7390 if (bcmp(entry, &empty, sizeof (empty)) == 0) 7391 goto done; 7392 7393 lpp->param_code[0] = 0; 7394 lpp->param_code[1] = count; 7395 lpp->param_ctrl_flags = 7396 LOG_CTRL_LP | LOG_CTRL_LBIN; 7397 lpp->param_len = 7398 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7399 7400 status = entry->smart_selftest_log_status; 7401 status >>= 4; 7402 switch (status) { 7403 case 0: 7404 default: 7405 sense_key = KEY_NO_SENSE; 7406 add_sense_code = SD_SCSI_NO_ADD_SENSE; 7407 break; 7408 case 1: 7409 sense_key = KEY_ABORTED_COMMAND; 7410 add_sense_code = 7411 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7412 add_sense_code_qual = SCSI_COMPONENT_81; 7413 break; 7414 case 2: 7415 sense_key = KEY_ABORTED_COMMAND; 7416 add_sense_code = 7417 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7418 add_sense_code_qual = SCSI_COMPONENT_82; 7419 break; 7420 case 3: 7421 sense_key = KEY_ABORTED_COMMAND; 7422 add_sense_code = 7423 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7424 add_sense_code_qual = SCSI_COMPONENT_83; 7425 break; 7426 case 4: 7427 sense_key = KEY_HARDWARE_ERROR; 7428 add_sense_code = 7429 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7430 add_sense_code_qual = SCSI_COMPONENT_84; 7431 break; 7432 case 5: 7433 sense_key = KEY_HARDWARE_ERROR; 7434 add_sense_code = 7435 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7436 add_sense_code_qual = SCSI_COMPONENT_85; 7437 break; 7438 case 6: 7439 sense_key = KEY_HARDWARE_ERROR; 7440 add_sense_code = 7441 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7442 add_sense_code_qual = SCSI_COMPONENT_86; 7443 break; 7444 case 7: 7445 sense_key = KEY_MEDIUM_ERROR; 7446 add_sense_code = 7447 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7448 add_sense_code_qual = SCSI_COMPONENT_87; 7449 break; 7450 case 8: 7451 sense_key = KEY_HARDWARE_ERROR; 7452 add_sense_code = 7453 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7454 add_sense_code_qual = SCSI_COMPONENT_88; 7455 break; 7456 } 7457 code = 0; /* unspecified */ 7458 status |= (code << 4); 7459 lpp->param_values[0] = status; 7460 lpp->param_values[1] = 0; /* unspecified */ 7461 lpp->param_values[2] = entry-> 7462 smart_selftest_log_timestamp[1]; 7463 lpp->param_values[3] = entry-> 7464 smart_selftest_log_timestamp[0]; 7465 if (status != 0) { 7466 lpp->param_values[4] = 0; 7467 lpp->param_values[5] = 0; 7468 lpp->param_values[6] = 0; 7469 lpp->param_values[7] = 0; 7470 lpp->param_values[8] = entry-> 7471 smart_selftest_log_failing_lba[3]; 7472 lpp->param_values[9] = entry-> 7473 smart_selftest_log_failing_lba[2]; 7474 lpp->param_values[10] = entry-> 7475 smart_selftest_log_failing_lba[1]; 7476 lpp->param_values[11] = entry-> 7477 smart_selftest_log_failing_lba[0]; 7478 } else { /* No block address */ 7479 lpp->param_values[4] = 0xff; 7480 lpp->param_values[5] = 0xff; 7481 lpp->param_values[6] = 0xff; 7482 lpp->param_values[7] = 0xff; 7483 lpp->param_values[8] = 0xff; 7484 lpp->param_values[9] = 0xff; 7485 lpp->param_values[10] = 0xff; 7486 lpp->param_values[11] = 0xff; 7487 } 7488 lpp->param_values[12] = sense_key; 7489 lpp->param_values[13] = add_sense_code; 7490 lpp->param_values[14] = add_sense_code_qual; 7491 lpp->param_values[15] = 0; /* undefined */ 7492 7493 lpp = (struct log_parameter *) 7494 (((uint8_t *)lpp) + 7495 SCSI_LOG_PARAM_HDR_LEN + 7496 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7497 --index; /* back up to previous entry */ 7498 if (index < 0) { 7499 index = 7500 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 7501 } 7502 entry = &selftest_log-> 7503 smart_selftest_log_entries[index]; 7504 } 7505 } 7506 done: 7507 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 7508 } 7509 7510 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 7511 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 7512 } 7513 7514 /* 7515 * sata_build_lsense_page_2f() is used to create the 7516 * SCSI LOG SENSE page 0x10 (informational exceptions) 7517 * 7518 * Takes a sata_drive_info t * and the address of a buffer 7519 * in which to create the page information as well as a sata_hba_inst_t *. 7520 * 7521 * Returns the number of bytes valid in the buffer. 7522 */ 7523 static int 7524 sata_build_lsense_page_2f( 7525 sata_drive_info_t *sdinfo, 7526 uint8_t *buf, 7527 sata_hba_inst_t *sata_hba_inst) 7528 { 7529 struct log_parameter *lpp = (struct log_parameter *)buf; 7530 int rval; 7531 uint8_t *smart_data; 7532 uint8_t temp; 7533 sata_id_t *sata_id; 7534 #define SMART_NO_TEMP 0xff 7535 7536 lpp->param_code[0] = 0; 7537 lpp->param_code[1] = 0; 7538 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7539 7540 /* Now get the SMART status w.r.t. threshold exceeded */ 7541 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 7542 switch (rval) { 7543 case 1: 7544 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 7545 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 7546 break; 7547 case 0: 7548 case -1: /* failed to get data */ 7549 lpp->param_values[0] = 0; /* No failure predicted */ 7550 lpp->param_values[1] = 0; 7551 break; 7552 #if defined(SATA_DEBUG) 7553 default: 7554 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 7555 /* NOTREACHED */ 7556 #endif 7557 } 7558 7559 sata_id = &sdinfo->satadrv_id; 7560 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 7561 temp = SMART_NO_TEMP; 7562 else { 7563 /* Now get the temperature */ 7564 smart_data = kmem_zalloc(512, KM_SLEEP); 7565 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 7566 SCT_STATUS_LOG_PAGE, 1); 7567 if (rval == -1) 7568 temp = SMART_NO_TEMP; 7569 else { 7570 temp = smart_data[200]; 7571 if (temp & 0x80) { 7572 if (temp & 0x7f) 7573 temp = 0; 7574 else 7575 temp = SMART_NO_TEMP; 7576 } 7577 } 7578 kmem_free(smart_data, 512); 7579 } 7580 7581 lpp->param_values[2] = temp; /* most recent temperature */ 7582 lpp->param_values[3] = 0; /* required vendor specific byte */ 7583 7584 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 7585 7586 7587 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 7588 } 7589 7590 /* 7591 * sata_build_lsense_page_30() is used to create the 7592 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 7593 * 7594 * Takes a sata_drive_info t * and the address of a buffer 7595 * in which to create the page information as well as a sata_hba_inst_t *. 7596 * 7597 * Returns the number of bytes valid in the buffer. 7598 */ 7599 static int 7600 sata_build_lsense_page_30( 7601 sata_drive_info_t *sdinfo, 7602 uint8_t *buf, 7603 sata_hba_inst_t *sata_hba_inst) 7604 { 7605 struct smart_data *smart_data = (struct smart_data *)buf; 7606 int rval; 7607 7608 /* Now do the SMART READ DATA */ 7609 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7610 if (rval == -1) 7611 return (0); 7612 7613 return (sizeof (struct smart_data)); 7614 } 7615 7616 7617 7618 7619 7620 /* ************************** LOCAL FUNCTIONS ************************** */ 7621 7622 /* 7623 * Validate sata_tran info 7624 * SATA_FAILURE returns if structure is inconsistent or structure revision 7625 * does not match one used by the framework. 7626 * 7627 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 7628 * required function pointers. 7629 * Returns SATA_FAILURE otherwise. 7630 */ 7631 static int 7632 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 7633 { 7634 if (sata_tran->sata_tran_hba_rev != SATA_TRAN_HBA_REV) { 7635 sata_log(NULL, CE_WARN, 7636 "sata: invalid sata_hba_tran version %d for driver %s", 7637 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 7638 return (SATA_FAILURE); 7639 } 7640 7641 if (dip != sata_tran->sata_tran_hba_dip) { 7642 SATA_LOG_D((NULL, CE_WARN, 7643 "sata: inconsistent sata_tran_hba_dip " 7644 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 7645 return (SATA_FAILURE); 7646 } 7647 7648 if (sata_tran->sata_tran_probe_port == NULL || 7649 sata_tran->sata_tran_start == NULL || 7650 sata_tran->sata_tran_abort == NULL || 7651 sata_tran->sata_tran_reset_dport == NULL) { 7652 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 7653 "required functions")); 7654 } 7655 return (SATA_SUCCESS); 7656 } 7657 7658 /* 7659 * Remove HBA instance from sata_hba_list. 7660 */ 7661 static void 7662 sata_remove_hba_instance(dev_info_t *dip) 7663 { 7664 sata_hba_inst_t *sata_hba_inst; 7665 7666 mutex_enter(&sata_mutex); 7667 for (sata_hba_inst = sata_hba_list; 7668 sata_hba_inst != (struct sata_hba_inst *)NULL; 7669 sata_hba_inst = sata_hba_inst->satahba_next) { 7670 if (sata_hba_inst->satahba_dip == dip) 7671 break; 7672 } 7673 7674 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 7675 #ifdef SATA_DEBUG 7676 cmn_err(CE_WARN, "sata_remove_hba_instance: " 7677 "unknown HBA instance\n"); 7678 #endif 7679 ASSERT(FALSE); 7680 } 7681 if (sata_hba_inst == sata_hba_list) { 7682 sata_hba_list = sata_hba_inst->satahba_next; 7683 if (sata_hba_list) { 7684 sata_hba_list->satahba_prev = 7685 (struct sata_hba_inst *)NULL; 7686 } 7687 if (sata_hba_inst == sata_hba_list_tail) { 7688 sata_hba_list_tail = NULL; 7689 } 7690 } else if (sata_hba_inst == sata_hba_list_tail) { 7691 sata_hba_list_tail = sata_hba_inst->satahba_prev; 7692 if (sata_hba_list_tail) { 7693 sata_hba_list_tail->satahba_next = 7694 (struct sata_hba_inst *)NULL; 7695 } 7696 } else { 7697 sata_hba_inst->satahba_prev->satahba_next = 7698 sata_hba_inst->satahba_next; 7699 sata_hba_inst->satahba_next->satahba_prev = 7700 sata_hba_inst->satahba_prev; 7701 } 7702 mutex_exit(&sata_mutex); 7703 } 7704 7705 7706 7707 7708 7709 /* 7710 * Probe all SATA ports of the specified HBA instance. 7711 * The assumption is that there are no target and attachment point minor nodes 7712 * created by the boot subsystems, so we do not need to prune device tree. 7713 * 7714 * This function is called only from sata_hba_attach(). It does not have to 7715 * be protected by controller mutex, because the hba_attached flag is not set 7716 * yet and no one would be touching this HBA instance other then this thread. 7717 * Determines if port is active and what type of the device is attached 7718 * (if any). Allocates necessary structures for each port. 7719 * 7720 * An AP (Attachement Point) node is created for each SATA device port even 7721 * when there is no device attached. 7722 */ 7723 7724 static void 7725 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 7726 { 7727 dev_info_t *dip = SATA_DIP(sata_hba_inst); 7728 int ncport, npmport; 7729 sata_cport_info_t *cportinfo; 7730 sata_drive_info_t *drive; 7731 sata_pmult_info_t *pminfo; 7732 sata_pmport_info_t *pmportinfo; 7733 sata_device_t sata_device; 7734 int rval; 7735 dev_t minor_number; 7736 char name[16]; 7737 clock_t start_time, cur_time; 7738 7739 /* 7740 * Probe controller ports first, to find port status and 7741 * any port multiplier attached. 7742 */ 7743 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 7744 /* allocate cport structure */ 7745 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 7746 ASSERT(cportinfo != NULL); 7747 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 7748 7749 mutex_enter(&cportinfo->cport_mutex); 7750 7751 cportinfo->cport_addr.cport = ncport; 7752 cportinfo->cport_addr.pmport = 0; 7753 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 7754 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 7755 cportinfo->cport_state |= SATA_STATE_PROBING; 7756 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 7757 7758 /* 7759 * Regardless if a port is usable or not, create 7760 * an attachment point 7761 */ 7762 mutex_exit(&cportinfo->cport_mutex); 7763 minor_number = 7764 SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0); 7765 (void) sprintf(name, "%d", ncport); 7766 if (ddi_create_minor_node(dip, name, S_IFCHR, 7767 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 7768 DDI_SUCCESS) { 7769 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 7770 "cannot create SATA attachment point for port %d", 7771 ncport); 7772 } 7773 7774 /* Probe port */ 7775 start_time = ddi_get_lbolt(); 7776 reprobe_cport: 7777 sata_device.satadev_addr.cport = ncport; 7778 sata_device.satadev_addr.pmport = 0; 7779 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 7780 sata_device.satadev_rev = SATA_DEVICE_REV; 7781 7782 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 7783 (dip, &sata_device); 7784 7785 mutex_enter(&cportinfo->cport_mutex); 7786 sata_update_port_scr(&cportinfo->cport_scr, &sata_device); 7787 if (rval != SATA_SUCCESS) { 7788 /* Something went wrong? Fail the port */ 7789 cportinfo->cport_state = SATA_PSTATE_FAILED; 7790 mutex_exit(&cportinfo->cport_mutex); 7791 continue; 7792 } 7793 cportinfo->cport_state &= ~SATA_STATE_PROBING; 7794 cportinfo->cport_state |= SATA_STATE_PROBED; 7795 cportinfo->cport_dev_type = sata_device.satadev_type; 7796 7797 cportinfo->cport_state |= SATA_STATE_READY; 7798 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 7799 mutex_exit(&cportinfo->cport_mutex); 7800 continue; 7801 } 7802 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 7803 /* 7804 * There is some device attached. 7805 * Allocate device info structure 7806 */ 7807 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 7808 mutex_exit(&cportinfo->cport_mutex); 7809 SATA_CPORTINFO_DRV_INFO(cportinfo) = 7810 kmem_zalloc(sizeof (sata_drive_info_t), 7811 KM_SLEEP); 7812 mutex_enter(&cportinfo->cport_mutex); 7813 } 7814 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 7815 drive->satadrv_addr = cportinfo->cport_addr; 7816 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 7817 drive->satadrv_type = cportinfo->cport_dev_type; 7818 drive->satadrv_state = SATA_STATE_UNKNOWN; 7819 7820 mutex_exit(&cportinfo->cport_mutex); 7821 if (sata_add_device(dip, sata_hba_inst, ncport, 0) != 7822 SATA_SUCCESS) { 7823 /* 7824 * Plugged device was not correctly identified. 7825 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 7826 */ 7827 cur_time = ddi_get_lbolt(); 7828 if ((cur_time - start_time) < 7829 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 7830 /* sleep for a while */ 7831 delay(drv_usectohz( 7832 SATA_DEV_IDENTIFY_RETRY_DELAY)); 7833 goto reprobe_cport; 7834 } 7835 } 7836 } else { 7837 mutex_exit(&cportinfo->cport_mutex); 7838 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 7839 pminfo = kmem_zalloc(sizeof (sata_pmult_info_t), 7840 KM_SLEEP); 7841 mutex_enter(&cportinfo->cport_mutex); 7842 ASSERT(pminfo != NULL); 7843 SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo; 7844 pminfo->pmult_addr.cport = cportinfo->cport_addr.cport; 7845 pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT; 7846 pminfo->pmult_addr.qual = SATA_ADDR_PMPORT; 7847 pminfo->pmult_num_dev_ports = 7848 sata_device.satadev_add_info; 7849 mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER, 7850 NULL); 7851 pminfo->pmult_state = SATA_STATE_PROBING; 7852 mutex_exit(&cportinfo->cport_mutex); 7853 7854 /* Probe Port Multiplier ports */ 7855 for (npmport = 0; 7856 npmport < pminfo->pmult_num_dev_ports; 7857 npmport++) { 7858 pmportinfo = kmem_zalloc( 7859 sizeof (sata_pmport_info_t), KM_SLEEP); 7860 mutex_enter(&cportinfo->cport_mutex); 7861 ASSERT(pmportinfo != NULL); 7862 pmportinfo->pmport_addr.cport = ncport; 7863 pmportinfo->pmport_addr.pmport = npmport; 7864 pmportinfo->pmport_addr.qual = 7865 SATA_ADDR_PMPORT; 7866 pminfo->pmult_dev_port[npmport] = pmportinfo; 7867 7868 mutex_init(&pmportinfo->pmport_mutex, NULL, 7869 MUTEX_DRIVER, NULL); 7870 7871 mutex_exit(&cportinfo->cport_mutex); 7872 7873 /* Create an attachment point */ 7874 minor_number = SATA_MAKE_AP_MINOR( 7875 ddi_get_instance(dip), ncport, npmport, 1); 7876 (void) sprintf(name, "%d.%d", ncport, npmport); 7877 if (ddi_create_minor_node(dip, name, S_IFCHR, 7878 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 7879 0) != DDI_SUCCESS) { 7880 sata_log(sata_hba_inst, CE_WARN, 7881 "sata_hba_attach: " 7882 "cannot create SATA attachment " 7883 "point for port %d pmult port %d", 7884 ncport, npmport); 7885 } 7886 7887 start_time = ddi_get_lbolt(); 7888 reprobe_pmport: 7889 sata_device.satadev_addr.pmport = npmport; 7890 sata_device.satadev_addr.qual = 7891 SATA_ADDR_PMPORT; 7892 7893 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 7894 (dip, &sata_device); 7895 mutex_enter(&cportinfo->cport_mutex); 7896 7897 /* sata_update_port_info() */ 7898 sata_update_port_scr(&pmportinfo->pmport_scr, 7899 &sata_device); 7900 7901 if (rval != SATA_SUCCESS) { 7902 pmportinfo->pmport_state = 7903 SATA_PSTATE_FAILED; 7904 mutex_exit(&cportinfo->cport_mutex); 7905 continue; 7906 } 7907 pmportinfo->pmport_state &= 7908 ~SATA_STATE_PROBING; 7909 pmportinfo->pmport_state |= SATA_STATE_PROBED; 7910 pmportinfo->pmport_dev_type = 7911 sata_device.satadev_type; 7912 7913 pmportinfo->pmport_state |= SATA_STATE_READY; 7914 if (pmportinfo->pmport_dev_type == 7915 SATA_DTYPE_NONE) { 7916 mutex_exit(&cportinfo->cport_mutex); 7917 continue; 7918 } 7919 /* Port multipliers cannot be chained */ 7920 ASSERT(pmportinfo->pmport_dev_type != 7921 SATA_DTYPE_PMULT); 7922 /* 7923 * There is something attached to Port 7924 * Multiplier device port 7925 * Allocate device info structure 7926 */ 7927 if (pmportinfo->pmport_sata_drive == NULL) { 7928 mutex_exit(&cportinfo->cport_mutex); 7929 pmportinfo->pmport_sata_drive = 7930 kmem_zalloc( 7931 sizeof (sata_drive_info_t), 7932 KM_SLEEP); 7933 mutex_enter(&cportinfo->cport_mutex); 7934 } 7935 drive = pmportinfo->pmport_sata_drive; 7936 drive->satadrv_addr.cport = 7937 pmportinfo->pmport_addr.cport; 7938 drive->satadrv_addr.pmport = npmport; 7939 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 7940 drive->satadrv_type = pmportinfo-> 7941 pmport_dev_type; 7942 drive->satadrv_state = SATA_STATE_UNKNOWN; 7943 7944 mutex_exit(&cportinfo->cport_mutex); 7945 if (sata_add_device(dip, sata_hba_inst, ncport, 7946 npmport) != SATA_SUCCESS) { 7947 /* 7948 * Plugged device was not correctly 7949 * identified. Retry, within the 7950 * SATA_DEV_IDENTIFY_TIMEOUT 7951 */ 7952 cur_time = ddi_get_lbolt(); 7953 if ((cur_time - start_time) < 7954 drv_usectohz( 7955 SATA_DEV_IDENTIFY_TIMEOUT)) { 7956 /* sleep for a while */ 7957 delay(drv_usectohz( 7958 SATA_DEV_IDENTIFY_RETRY_DELAY)); 7959 goto reprobe_pmport; 7960 } 7961 } 7962 } 7963 pmportinfo->pmport_state = 7964 SATA_STATE_PROBED | SATA_STATE_READY; 7965 } 7966 } 7967 } 7968 7969 /* 7970 * Add SATA device for specified HBA instance & port (SCSI target 7971 * device nodes). 7972 * This function is called (indirectly) only from sata_hba_attach(). 7973 * A target node is created when there is a supported type device attached, 7974 * but may be removed if it cannot be put online. 7975 * 7976 * This function cannot be called from an interrupt context. 7977 * 7978 * ONLY DISK TARGET NODES ARE CREATED NOW 7979 * 7980 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 7981 * device identification failed - adding a device could be retried. 7982 * 7983 */ 7984 static int 7985 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport, 7986 int pmport) 7987 { 7988 sata_cport_info_t *cportinfo; 7989 sata_pmult_info_t *pminfo; 7990 sata_pmport_info_t *pmportinfo; 7991 dev_info_t *cdip; /* child dip */ 7992 sata_device_t sata_device; 7993 int rval; 7994 7995 7996 7997 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 7998 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 7999 mutex_enter(&cportinfo->cport_mutex); 8000 /* 8001 * Some device is attached to a controller port. 8002 * We rely on controllers distinquishing between no-device, 8003 * attached port multiplier and other kind of attached device. 8004 * We need to get Identify Device data and determine 8005 * positively the dev type before trying to attach 8006 * the target driver. 8007 */ 8008 sata_device.satadev_rev = SATA_DEVICE_REV; 8009 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8010 /* 8011 * Not port multiplier. 8012 */ 8013 sata_device.satadev_addr = cportinfo->cport_addr; 8014 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8015 mutex_exit(&cportinfo->cport_mutex); 8016 8017 rval = sata_probe_device(sata_hba_inst, &sata_device); 8018 if (rval != SATA_SUCCESS || 8019 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) 8020 return (SATA_FAILURE); 8021 8022 mutex_enter(&cportinfo->cport_mutex); 8023 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8024 /* 8025 * Could not determine device type or 8026 * a device is not supported. 8027 * Degrade this device to unknown. 8028 */ 8029 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8030 mutex_exit(&cportinfo->cport_mutex); 8031 return (SATA_SUCCESS); 8032 } 8033 cportinfo->cport_dev_type = sata_device.satadev_type; 8034 mutex_exit(&cportinfo->cport_mutex); 8035 8036 /* 8037 * Initialize device to the desired state. Even if it 8038 * fails, the device will still attach but syslog 8039 * will show the warning. 8040 */ 8041 if (sata_initialize_device(sata_hba_inst, 8042 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) 8043 /* Retry */ 8044 (void) sata_initialize_device(sata_hba_inst, 8045 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8046 8047 mutex_enter(&cportinfo->cport_mutex); 8048 sata_show_drive_info(sata_hba_inst, 8049 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8050 cportinfo->cport_tgtnode_clean = B_TRUE; 8051 mutex_exit(&cportinfo->cport_mutex); 8052 cdip = sata_create_target_node(pdip, sata_hba_inst, 8053 &sata_device.satadev_addr); 8054 mutex_enter(&cportinfo->cport_mutex); 8055 if (cdip == NULL) { 8056 /* 8057 * Attaching target node failed. 8058 * We retain sata_drive_info structure... 8059 */ 8060 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8061 satadrv_type = SATA_DTYPE_UNKNOWN; 8062 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8063 satadrv_state = SATA_STATE_UNKNOWN; 8064 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8065 mutex_exit(&cportinfo->cport_mutex); 8066 return (SATA_SUCCESS); 8067 } 8068 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8069 satadrv_state = SATA_STATE_READY; 8070 } else { 8071 /* This must be Port Multiplier type */ 8072 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8073 SATA_LOG_D((sata_hba_inst, CE_WARN, 8074 "sata_add_device: " 8075 "unrecognized dev type %x", 8076 cportinfo->cport_dev_type)); 8077 mutex_exit(&cportinfo->cport_mutex); 8078 return (SATA_SUCCESS); 8079 } 8080 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 8081 pmportinfo = pminfo->pmult_dev_port[pmport]; 8082 sata_device.satadev_addr = pmportinfo->pmport_addr; 8083 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 8084 mutex_exit(&cportinfo->cport_mutex); 8085 8086 rval = sata_probe_device(sata_hba_inst, &sata_device); 8087 if (rval != SATA_SUCCESS || 8088 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) { 8089 return (SATA_FAILURE); 8090 } 8091 mutex_enter(&cportinfo->cport_mutex); 8092 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8093 /* 8094 * Could not determine device type. 8095 * Degrade this device to unknown. 8096 */ 8097 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 8098 mutex_exit(&cportinfo->cport_mutex); 8099 return (SATA_SUCCESS); 8100 } 8101 pmportinfo->pmport_dev_type = sata_device.satadev_type; 8102 mutex_exit(&cportinfo->cport_mutex); 8103 /* 8104 * Initialize device to the desired state. 8105 * Even if it fails, the device will still 8106 * attach but syslog will show the warning. 8107 */ 8108 if (sata_initialize_device(sata_hba_inst, 8109 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) 8110 /* Retry */ 8111 (void) sata_initialize_device(sata_hba_inst, 8112 pmportinfo->pmport_sata_drive); 8113 8114 mutex_enter(&cportinfo->cport_mutex); 8115 sata_show_drive_info(sata_hba_inst, 8116 pmportinfo->pmport_sata_drive); 8117 pmportinfo->pmport_tgtnode_clean = B_TRUE; 8118 mutex_exit(&cportinfo->cport_mutex); 8119 cdip = sata_create_target_node(pdip, sata_hba_inst, 8120 &sata_device.satadev_addr); 8121 mutex_enter(&cportinfo->cport_mutex); 8122 if (cdip == NULL) { 8123 /* 8124 * Attaching target node failed. 8125 * We retain sata_drive_info structure... 8126 */ 8127 pmportinfo->pmport_sata_drive-> 8128 satadrv_type = SATA_DTYPE_UNKNOWN; 8129 pmportinfo->pmport_sata_drive-> 8130 satadrv_state = SATA_STATE_UNKNOWN; 8131 pmportinfo->pmport_dev_type = 8132 SATA_DTYPE_UNKNOWN; 8133 mutex_exit(&cportinfo->cport_mutex); 8134 return (SATA_SUCCESS); 8135 } 8136 pmportinfo->pmport_sata_drive->satadrv_state |= 8137 SATA_STATE_READY; 8138 } 8139 mutex_exit(&cportinfo->cport_mutex); 8140 return (SATA_SUCCESS); 8141 } 8142 8143 8144 /* 8145 * Create scsi target node for attached device, create node properties and 8146 * attach the node. 8147 * The node could be removed if the device onlining fails. 8148 * 8149 * A dev_info_t pointer is returned if operation is successful, NULL is 8150 * returned otherwise. 8151 * 8152 * No port multiplier support. 8153 */ 8154 8155 static dev_info_t * 8156 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 8157 sata_address_t *sata_addr) 8158 { 8159 dev_info_t *cdip = NULL; 8160 int rval; 8161 char *nname = NULL; 8162 char **compatible = NULL; 8163 int ncompatible; 8164 struct scsi_inquiry inq; 8165 sata_device_t sata_device; 8166 sata_drive_info_t *sdinfo; 8167 int target; 8168 int i; 8169 8170 sata_device.satadev_rev = SATA_DEVICE_REV; 8171 sata_device.satadev_addr = *sata_addr; 8172 8173 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 8174 8175 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8176 8177 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 8178 sata_addr->pmport, sata_addr->qual); 8179 8180 if (sdinfo == NULL) { 8181 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8182 sata_addr->cport))); 8183 SATA_LOG_D((sata_hba_inst, CE_WARN, 8184 "sata_create_target_node: no sdinfo for target %x", 8185 target)); 8186 return (NULL); 8187 } 8188 8189 /* 8190 * create scsi inquiry data, expected by 8191 * scsi_hba_nodename_compatible_get() 8192 */ 8193 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, (uint8_t *)&inq); 8194 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 8195 8196 /* determine the node name and compatible */ 8197 scsi_hba_nodename_compatible_get(&inq, NULL, 8198 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 8199 8200 #ifdef SATA_DEBUG 8201 if (sata_debug_flags & SATA_DBG_NODES) { 8202 if (nname == NULL) { 8203 cmn_err(CE_NOTE, "sata_create_target_node: " 8204 "cannot determine nodename for target %d\n", 8205 target); 8206 } else { 8207 cmn_err(CE_WARN, "sata_create_target_node: " 8208 "target %d nodename: %s\n", target, nname); 8209 } 8210 if (compatible == NULL) { 8211 cmn_err(CE_WARN, 8212 "sata_create_target_node: no compatible name\n"); 8213 } else { 8214 for (i = 0; i < ncompatible; i++) { 8215 cmn_err(CE_WARN, "sata_create_target_node: " 8216 "compatible name: %s\n", compatible[i]); 8217 } 8218 } 8219 } 8220 #endif 8221 8222 /* if nodename can't be determined, log error and exit */ 8223 if (nname == NULL) { 8224 SATA_LOG_D((sata_hba_inst, CE_WARN, 8225 "sata_create_target_node: cannot determine nodename " 8226 "for target %d\n", target)); 8227 scsi_hba_nodename_compatible_free(nname, compatible); 8228 return (NULL); 8229 } 8230 /* 8231 * Create scsi target node 8232 */ 8233 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 8234 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8235 "device-type", "scsi"); 8236 8237 if (rval != DDI_PROP_SUCCESS) { 8238 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8239 "updating device_type prop failed %d", rval)); 8240 goto fail; 8241 } 8242 8243 /* 8244 * Create target node properties: target & lun 8245 */ 8246 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 8247 if (rval != DDI_PROP_SUCCESS) { 8248 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8249 "updating target prop failed %d", rval)); 8250 goto fail; 8251 } 8252 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 8253 if (rval != DDI_PROP_SUCCESS) { 8254 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8255 "updating target prop failed %d", rval)); 8256 goto fail; 8257 } 8258 8259 /* decorate the node with compatible */ 8260 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 8261 compatible, ncompatible) != DDI_PROP_SUCCESS) { 8262 SATA_LOG_D((sata_hba_inst, CE_WARN, 8263 "sata_create_target_node: FAIL compatible props cdip 0x%p", 8264 (void *)cdip)); 8265 goto fail; 8266 } 8267 8268 /* 8269 * Now, try to attach the driver. If probing of the device fails, 8270 * the target node may be removed 8271 */ 8272 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 8273 8274 scsi_hba_nodename_compatible_free(nname, compatible); 8275 8276 if (rval == NDI_SUCCESS) 8277 return (cdip); 8278 8279 /* target node was removed - are we sure? */ 8280 return (NULL); 8281 8282 fail: 8283 scsi_hba_nodename_compatible_free(nname, compatible); 8284 ddi_prop_remove_all(cdip); 8285 rval = ndi_devi_free(cdip); 8286 if (rval != NDI_SUCCESS) { 8287 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8288 "node removal failed %d", rval)); 8289 } 8290 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 8291 "cannot create target node for SATA device at port %d", 8292 sata_addr->cport); 8293 return (NULL); 8294 } 8295 8296 8297 8298 /* 8299 * Re-probe sata port, check for a device and attach info 8300 * structures when necessary. Identify Device data is fetched, if possible. 8301 * Assumption: sata address is already validated. 8302 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 8303 * the presence of a device and its type. 8304 * 8305 * flag arg specifies that the function should try multiple times to identify 8306 * device type and to initialize it, or it should return immediately on failure. 8307 * SATA_DEV_IDENTIFY_RETRY - retry 8308 * SATA_DEV_IDENTIFY_NORETRY - no retry 8309 * 8310 * SATA_FAILURE is returned if one of the operations failed. 8311 * 8312 * This function cannot be called in interrupt context - it may sleep. 8313 */ 8314 static int 8315 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 8316 int flag) 8317 { 8318 sata_cport_info_t *cportinfo; 8319 sata_drive_info_t *sdinfo; 8320 boolean_t init_device = B_FALSE; 8321 int prev_device_type = SATA_DTYPE_NONE; 8322 int prev_device_settings = 0; 8323 clock_t start_time; 8324 int retry = B_FALSE; 8325 int rval; 8326 8327 /* We only care about host sata cport for now */ 8328 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 8329 sata_device->satadev_addr.cport); 8330 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8331 if (sdinfo != NULL) { 8332 /* 8333 * We are re-probing port with a previously attached device. 8334 * Save previous device type and settings 8335 */ 8336 prev_device_type = cportinfo->cport_dev_type; 8337 prev_device_settings = sdinfo->satadrv_settings; 8338 } 8339 if (flag == SATA_DEV_IDENTIFY_RETRY) { 8340 start_time = ddi_get_lbolt(); 8341 retry = B_TRUE; 8342 } 8343 retry_probe: 8344 8345 /* probe port */ 8346 mutex_enter(&cportinfo->cport_mutex); 8347 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8348 cportinfo->cport_state |= SATA_STATE_PROBING; 8349 mutex_exit(&cportinfo->cport_mutex); 8350 8351 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8352 (SATA_DIP(sata_hba_inst), sata_device); 8353 8354 mutex_enter(&cportinfo->cport_mutex); 8355 if (rval != SATA_SUCCESS) { 8356 cportinfo->cport_state = SATA_PSTATE_FAILED; 8357 mutex_exit(&cportinfo->cport_mutex); 8358 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 8359 "SATA port %d probing failed", 8360 cportinfo->cport_addr.cport)); 8361 return (SATA_FAILURE); 8362 } 8363 8364 /* 8365 * update sata port state and set device type 8366 */ 8367 sata_update_port_info(sata_hba_inst, sata_device); 8368 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8369 8370 /* 8371 * Sanity check - Port is active? Is the link active? 8372 * Is there any device attached? 8373 */ 8374 if ((cportinfo->cport_state & 8375 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 8376 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 8377 SATA_PORT_DEVLINK_UP) { 8378 /* 8379 * Port in non-usable state or no link active/no device. 8380 * Free info structure if necessary (direct attached drive 8381 * only, for now! 8382 */ 8383 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8384 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8385 /* Add here differentiation for device attached or not */ 8386 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8387 mutex_exit(&cportinfo->cport_mutex); 8388 if (sdinfo != NULL) 8389 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8390 return (SATA_SUCCESS); 8391 } 8392 8393 cportinfo->cport_state |= SATA_STATE_READY; 8394 cportinfo->cport_dev_type = sata_device->satadev_type; 8395 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8396 8397 /* 8398 * If we are re-probing the port, there may be 8399 * sata_drive_info structure attached 8400 * (or sata_pm_info, if PMult is supported). 8401 */ 8402 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 8403 /* 8404 * There is no device, so remove device info structure, 8405 * if necessary. Direct attached drive only! 8406 */ 8407 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8408 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8409 if (sdinfo != NULL) { 8410 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8411 sata_log(sata_hba_inst, CE_WARN, 8412 "SATA device detached " 8413 "from port %d", cportinfo->cport_addr.cport); 8414 } 8415 mutex_exit(&cportinfo->cport_mutex); 8416 return (SATA_SUCCESS); 8417 } 8418 8419 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 8420 if (sdinfo == NULL) { 8421 /* 8422 * There is some device attached, but there is 8423 * no sata_drive_info structure - allocate one 8424 */ 8425 mutex_exit(&cportinfo->cport_mutex); 8426 sdinfo = kmem_zalloc( 8427 sizeof (sata_drive_info_t), KM_SLEEP); 8428 mutex_enter(&cportinfo->cport_mutex); 8429 /* 8430 * Recheck, that the port state did not change when we 8431 * released mutex. 8432 */ 8433 if (cportinfo->cport_state & SATA_STATE_READY) { 8434 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 8435 sdinfo->satadrv_addr = cportinfo->cport_addr; 8436 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 8437 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8438 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 8439 } else { 8440 /* 8441 * Port is not in ready state, we 8442 * cannot attach a device. 8443 */ 8444 mutex_exit(&cportinfo->cport_mutex); 8445 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8446 return (SATA_SUCCESS); 8447 } 8448 /* 8449 * Since we are adding device, presumably new one, 8450 * indicate that it should be initalized, 8451 * as well as some internal framework states). 8452 */ 8453 init_device = B_TRUE; 8454 } 8455 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8456 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 8457 } else { 8458 /* 8459 * The device is a port multiplier - not handled now. 8460 */ 8461 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8462 mutex_exit(&cportinfo->cport_mutex); 8463 return (SATA_SUCCESS); 8464 } 8465 mutex_exit(&cportinfo->cport_mutex); 8466 /* 8467 * Figure out what kind of device we are really 8468 * dealing with. 8469 */ 8470 rval = sata_probe_device(sata_hba_inst, sata_device); 8471 8472 if (rval == SATA_SUCCESS) { 8473 /* 8474 * If we are dealing with the same type of a device as before, 8475 * restore its settings flags. 8476 */ 8477 if (sata_device->satadev_type == prev_device_type) 8478 sdinfo->satadrv_settings = prev_device_settings; 8479 8480 /* Set initial device features, if necessary */ 8481 if (init_device == B_TRUE) { 8482 rval = sata_initialize_device(sata_hba_inst, sdinfo); 8483 } 8484 if (rval == SATA_SUCCESS) 8485 return (rval); 8486 } 8487 8488 if (retry) { 8489 clock_t cur_time = ddi_get_lbolt(); 8490 /* 8491 * A device was not successfully identified or initialized. 8492 * Track retry time for device identification. 8493 */ 8494 if ((cur_time - start_time) < 8495 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 8496 /* sleep for a while */ 8497 delay(drv_usectohz(SATA_DEV_IDENTIFY_RETRY_DELAY)); 8498 goto retry_probe; 8499 } 8500 } 8501 return (rval); 8502 } 8503 8504 /* 8505 * Initialize device 8506 * Specified device is initialized to a default state. 8507 * At this point only read cache and UDMA modes are set here. 8508 * Write cache mode should be set when a disk is configured. 8509 * 8510 * Only SATA disks are initialized for now. 8511 * 8512 * Returns SATA_SUCCESS if all device features are set successfully, 8513 * SATA_FAILURE otherwise 8514 */ 8515 static int 8516 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 8517 sata_drive_info_t *sdinfo) 8518 { 8519 8520 sata_save_drive_settings(sdinfo); 8521 8522 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 8523 8524 sata_init_write_cache_mode(sdinfo); 8525 8526 return (sata_set_drive_features(sata_hba_inst, sdinfo, 0)); 8527 } 8528 8529 8530 /* 8531 * Initialize write cache mode. 8532 * 8533 * The default write cache setting is provided by sata_write_cache 8534 * static variable: 8535 * 1 - enable 8536 * 0 - disable 8537 * any other value - current drive setting 8538 * 8539 * In the future, it may be overridden by the 8540 * disk-write-cache-enable property setting, if it is defined. 8541 * Returns SATA_SUCCESS if all device features are set successfully, 8542 * SATA_FAILURE otherwise. 8543 */ 8544 static void 8545 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 8546 { 8547 if (sata_write_cache == 1) 8548 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 8549 else if (sata_write_cache == 0) 8550 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 8551 /* 8552 * When sata_write_cache value is not 0 or 1, 8553 * a current setting of the drive's write cache is used. 8554 */ 8555 } 8556 8557 8558 /* 8559 * Validate sata address. 8560 * Specified cport, pmport and qualifier has to match 8561 * passed sata_scsi configuration info. 8562 * The presence of an attached device is not verified. 8563 * 8564 * Returns 0 when address is valid, -1 otherwise. 8565 */ 8566 static int 8567 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 8568 int pmport, int qual) 8569 { 8570 if (qual == SATA_ADDR_DCPORT && pmport != 0) 8571 goto invalid_address; 8572 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 8573 goto invalid_address; 8574 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 8575 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 8576 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 8577 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 8578 goto invalid_address; 8579 8580 return (0); 8581 8582 invalid_address: 8583 return (-1); 8584 8585 } 8586 8587 /* 8588 * Validate scsi address 8589 * SCSI target address is translated into SATA cport/pmport and compared 8590 * with a controller port/device configuration. LUN has to be 0. 8591 * Returns 0 if a scsi target refers to an attached device, 8592 * returns 1 if address is valid but device is not attached, 8593 * returns -1 if bad address or device is of an unsupported type. 8594 * Upon return sata_device argument is set. 8595 */ 8596 static int 8597 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 8598 struct scsi_address *ap, sata_device_t *sata_device) 8599 { 8600 int cport, pmport, qual, rval; 8601 8602 rval = -1; /* Invalid address */ 8603 if (ap->a_lun != 0) 8604 goto out; 8605 8606 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 8607 cport = SCSI_TO_SATA_CPORT(ap->a_target); 8608 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 8609 8610 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 8611 goto out; 8612 8613 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 8614 0) { 8615 8616 sata_cport_info_t *cportinfo; 8617 sata_pmult_info_t *pmultinfo; 8618 sata_drive_info_t *sdinfo = NULL; 8619 8620 rval = 1; /* Valid sata address */ 8621 8622 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 8623 if (qual == SATA_ADDR_DCPORT) { 8624 if (cportinfo == NULL || 8625 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 8626 goto out; 8627 8628 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT || 8629 (cportinfo->cport_dev_type & 8630 SATA_VALID_DEV_TYPE) == 0) { 8631 rval = -1; 8632 goto out; 8633 } 8634 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8635 8636 } else if (qual == SATA_ADDR_DPMPORT) { 8637 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 8638 if (pmultinfo == NULL) { 8639 rval = -1; 8640 goto out; 8641 } 8642 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 8643 NULL || 8644 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 8645 pmport) == SATA_DTYPE_NONE) 8646 goto out; 8647 8648 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 8649 pmport); 8650 } else { 8651 rval = -1; 8652 goto out; 8653 } 8654 if ((sdinfo == NULL) || 8655 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 8656 goto out; 8657 8658 sata_device->satadev_type = sdinfo->satadrv_type; 8659 sata_device->satadev_addr.qual = qual; 8660 sata_device->satadev_addr.cport = cport; 8661 sata_device->satadev_addr.pmport = pmport; 8662 sata_device->satadev_rev = SATA_DEVICE_REV_1; 8663 return (0); 8664 } 8665 out: 8666 if (rval == 1) { 8667 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 8668 "sata_validate_scsi_address: no valid target %x lun %x", 8669 ap->a_target, ap->a_lun); 8670 } 8671 return (rval); 8672 } 8673 8674 /* 8675 * Find dip corresponding to passed device number 8676 * 8677 * Returns NULL if invalid device number is passed or device cannot be found, 8678 * Returns dip is device is found. 8679 */ 8680 static dev_info_t * 8681 sata_devt_to_devinfo(dev_t dev) 8682 { 8683 dev_info_t *dip; 8684 #ifndef __lock_lint 8685 struct devnames *dnp; 8686 major_t major = getmajor(dev); 8687 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 8688 8689 if (major >= devcnt) 8690 return (NULL); 8691 8692 dnp = &devnamesp[major]; 8693 LOCK_DEV_OPS(&(dnp->dn_lock)); 8694 dip = dnp->dn_head; 8695 while (dip && (ddi_get_instance(dip) != instance)) { 8696 dip = ddi_get_next(dip); 8697 } 8698 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 8699 #endif 8700 8701 return (dip); 8702 } 8703 8704 8705 /* 8706 * Probe device. 8707 * This function issues Identify Device command and initializes local 8708 * sata_drive_info structure if the device can be identified. 8709 * The device type is determined by examining Identify Device 8710 * command response. 8711 * If the sata_hba_inst has linked drive info structure for this 8712 * device address, the Identify Device data is stored into sata_drive_info 8713 * structure linked to the port info structure. 8714 * 8715 * sata_device has to refer to the valid sata port(s) for HBA described 8716 * by sata_hba_inst structure. 8717 * 8718 * Returns: 8719 * SATA_SUCCESS if device type was successfully probed and port-linked 8720 * drive info structure was updated; 8721 * SATA_FAILURE if there is no device, or device was not probed 8722 * successully; 8723 * SATA_RETRY if device probe can be retried later. 8724 * If a device cannot be identified, sata_device's dev_state and dev_type 8725 * fields are set to unknown. 8726 * There are no retries in this function. Any retries should be managed by 8727 * the caller. 8728 */ 8729 8730 8731 static int 8732 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 8733 { 8734 sata_drive_info_t *sdinfo; 8735 sata_drive_info_t new_sdinfo; /* local drive info struct */ 8736 int rval; 8737 8738 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 8739 sata_device->satadev_addr.cport) & 8740 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 8741 8742 sata_device->satadev_type = SATA_DTYPE_NONE; 8743 8744 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 8745 sata_device->satadev_addr.cport))); 8746 8747 /* Get pointer to port-linked sata device info structure */ 8748 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 8749 if (sdinfo != NULL) { 8750 sdinfo->satadrv_state &= 8751 ~(SATA_STATE_PROBED | SATA_STATE_READY); 8752 sdinfo->satadrv_state |= SATA_STATE_PROBING; 8753 } else { 8754 /* No device to probe */ 8755 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8756 sata_device->satadev_addr.cport))); 8757 sata_device->satadev_type = SATA_DTYPE_NONE; 8758 sata_device->satadev_state = SATA_STATE_UNKNOWN; 8759 return (SATA_FAILURE); 8760 } 8761 /* 8762 * Need to issue both types of identify device command and 8763 * determine device type by examining retreived data/status. 8764 * First, ATA Identify Device. 8765 */ 8766 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 8767 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 8768 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8769 sata_device->satadev_addr.cport))); 8770 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 8771 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 8772 if (rval == SATA_RETRY) { 8773 /* We may try to check for ATAPI device */ 8774 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 8775 /* 8776 * HBA supports ATAPI - try to issue Identify Packet 8777 * Device command. 8778 */ 8779 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD; 8780 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 8781 } 8782 } else if (rval == SATA_FAILURE) 8783 goto failure; 8784 else /* if (rval == SATA_SUCCESS) */ { 8785 /* 8786 * Got something responding to ATA Identify Device or to 8787 * Identify Packet Device cmd. 8788 */ 8789 sata_device->satadev_type = new_sdinfo.satadrv_type; 8790 8791 /* save device info, if possible */ 8792 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 8793 sata_device->satadev_addr.cport))); 8794 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 8795 if (sdinfo == NULL) { 8796 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8797 sata_device->satadev_addr.cport))); 8798 return (SATA_FAILURE); 8799 } 8800 /* 8801 * Copy drive info into the port-linked drive info structure. 8802 */ 8803 *sdinfo = new_sdinfo; 8804 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 8805 sdinfo->satadrv_state |= SATA_STATE_PROBED; 8806 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 8807 SATA_CPORT_DEV_TYPE(sata_hba_inst, 8808 sata_device->satadev_addr.cport) = 8809 sdinfo->satadrv_type; 8810 else /* SATA_ADDR_DPMPORT */ 8811 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 8812 sata_device->satadev_addr.cport, 8813 sata_device->satadev_addr.pmport) = 8814 sdinfo->satadrv_type; 8815 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8816 sata_device->satadev_addr.cport))); 8817 return (SATA_SUCCESS); 8818 } 8819 8820 failure: 8821 /* 8822 * Looks like we cannot determine the device type. 8823 */ 8824 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 8825 sata_device->satadev_addr.cport))); 8826 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 8827 if (sdinfo != NULL) { 8828 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 8829 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8830 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 8831 sdinfo->satadrv_state = SATA_STATE_PROBED; 8832 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 8833 SATA_CPORT_DEV_TYPE(sata_hba_inst, 8834 sata_device->satadev_addr.cport) = 8835 SATA_DTYPE_UNKNOWN; 8836 else { 8837 /* SATA_ADDR_DPMPORT */ 8838 if ((SATA_PMULT_INFO(sata_hba_inst, 8839 sata_device->satadev_addr.cport) != NULL) && 8840 (SATA_PMPORT_INFO(sata_hba_inst, 8841 sata_device->satadev_addr.cport, 8842 sata_device->satadev_addr.pmport) != NULL)) 8843 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 8844 sata_device->satadev_addr.cport, 8845 sata_device->satadev_addr.pmport) = 8846 SATA_DTYPE_UNKNOWN; 8847 } 8848 } 8849 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8850 sata_device->satadev_addr.cport))); 8851 return (rval); 8852 } 8853 8854 8855 /* 8856 * Get pointer to sata_drive_info structure. 8857 * 8858 * The sata_device has to contain address (cport, pmport and qualifier) for 8859 * specified sata_scsi structure. 8860 * 8861 * Returns NULL if device address is not valid for this HBA configuration. 8862 * Otherwise, returns a pointer to sata_drive_info structure. 8863 * 8864 * This function should be called with a port mutex held. 8865 */ 8866 static sata_drive_info_t * 8867 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 8868 sata_device_t *sata_device) 8869 { 8870 uint8_t cport = sata_device->satadev_addr.cport; 8871 uint8_t pmport = sata_device->satadev_addr.pmport; 8872 uint8_t qual = sata_device->satadev_addr.qual; 8873 8874 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 8875 return (NULL); 8876 8877 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 8878 (SATA_STATE_PROBED | SATA_STATE_READY))) 8879 /* Port not probed yet */ 8880 return (NULL); 8881 8882 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 8883 return (NULL); 8884 8885 if (qual == SATA_ADDR_DCPORT) { 8886 /* Request for a device on a controller port */ 8887 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 8888 SATA_DTYPE_PMULT) 8889 /* Port multiplier attached */ 8890 return (NULL); 8891 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 8892 } 8893 if (qual == SATA_ADDR_DPMPORT) { 8894 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 8895 SATA_DTYPE_PMULT) 8896 return (NULL); 8897 8898 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 8899 return (NULL); 8900 8901 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 8902 } 8903 8904 /* we should not get here */ 8905 return (NULL); 8906 } 8907 8908 8909 /* 8910 * sata_identify_device. 8911 * Send Identify Device command to SATA HBA driver. 8912 * If command executes successfully, update sata_drive_info structure pointed 8913 * to by sdinfo argument, including Identify Device data. 8914 * If command fails, invalidate data in sata_drive_info. 8915 * 8916 * Cannot be called from interrupt level. 8917 * 8918 * Returns: 8919 * SATA_SUCCESS if the device was identified as a supported device, 8920 * SATA_RETRY if the device was not identified but could be retried, 8921 * SATA_FAILURE if the device was not identified and identify attempt 8922 * should not be retried. 8923 */ 8924 static int 8925 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 8926 sata_drive_info_t *sdinfo) 8927 { 8928 uint16_t cfg_word; 8929 int rval; 8930 int i; 8931 8932 /* fetch device identify data */ 8933 if ((rval = sata_fetch_device_identify_data( 8934 sata_hba_inst, sdinfo)) != 0) 8935 goto fail_unknown; 8936 8937 cfg_word = sdinfo->satadrv_id.ai_config; 8938 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK && 8939 (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) { 8940 /* Change device type to reflect Identify Device data */ 8941 if (((cfg_word & SATA_ATAPI_TYPE_MASK) == 8942 SATA_ATAPI_TYPE) && 8943 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) == 8944 SATA_ATAPI_CDROM_DEV)) { 8945 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 8946 } else { 8947 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8948 } 8949 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD && 8950 (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) || 8951 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) { 8952 /* Change device type to reflect Identify Device data ! */ 8953 if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) == 8954 SATA_ATA_TYPE) { 8955 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 8956 } else { 8957 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8958 } 8959 } 8960 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 8961 if (sdinfo->satadrv_capacity == 0) { 8962 /* Non-LBA disk. Too bad... */ 8963 sata_log(sata_hba_inst, CE_WARN, 8964 "SATA disk device at port %d does not support LBA", 8965 sdinfo->satadrv_addr.cport); 8966 rval = SATA_FAILURE; 8967 goto fail_unknown; 8968 } 8969 } 8970 /* Check for Ultra DMA modes 6 through 0 being supported */ 8971 for (i = 6; i >= 0; --i) { 8972 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 8973 break; 8974 } 8975 /* 8976 * At least UDMA 4 mode has to be supported. If mode 4 or 8977 * higher are not supported by the device, fail this 8978 * device. 8979 */ 8980 if (i < 4) { 8981 /* No required Ultra DMA mode supported */ 8982 sata_log(sata_hba_inst, CE_WARN, 8983 "SATA disk device at port %d does not support UDMA " 8984 "mode 4 or higher", sdinfo->satadrv_addr.cport); 8985 SATA_LOG_D((sata_hba_inst, CE_WARN, 8986 "mode 4 or higher required, %d supported", i)); 8987 rval = SATA_FAILURE; 8988 goto fail_unknown; 8989 } 8990 8991 return (SATA_SUCCESS); 8992 8993 fail_unknown: 8994 /* Invalidate sata_drive_info ? */ 8995 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8996 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 8997 return (rval); 8998 } 8999 9000 /* 9001 * Log/display device information 9002 */ 9003 static void 9004 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 9005 sata_drive_info_t *sdinfo) 9006 { 9007 int valid_version; 9008 char msg_buf[MAXPATHLEN]; 9009 9010 /* Show HBA path */ 9011 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 9012 9013 cmn_err(CE_CONT, "?%s :\n", msg_buf); 9014 9015 if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) { 9016 (void) sprintf(msg_buf, 9017 "Unsupported SATA device type (cfg 0x%x) at ", 9018 sdinfo->satadrv_id.ai_config); 9019 } else { 9020 (void) sprintf(msg_buf, "SATA %s device at", 9021 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 9022 "disk":"CD/DVD (ATAPI)"); 9023 } 9024 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 9025 cmn_err(CE_CONT, "?\t%s port %d\n", 9026 msg_buf, sdinfo->satadrv_addr.cport); 9027 else 9028 cmn_err(CE_CONT, "?\t%s port %d pmport %d\n", 9029 msg_buf, sdinfo->satadrv_addr.cport, 9030 sdinfo->satadrv_addr.pmport); 9031 9032 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 9033 sizeof (sdinfo->satadrv_id.ai_model)); 9034 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 9035 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 9036 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 9037 9038 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 9039 sizeof (sdinfo->satadrv_id.ai_fw)); 9040 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 9041 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 9042 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 9043 9044 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 9045 sizeof (sdinfo->satadrv_id.ai_drvser)); 9046 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 9047 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 9048 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9049 9050 #ifdef SATA_DEBUG 9051 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9052 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 9053 int i; 9054 for (i = 14; i >= 2; i--) { 9055 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 9056 valid_version = i; 9057 break; 9058 } 9059 } 9060 cmn_err(CE_CONT, 9061 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 9062 valid_version, 9063 sdinfo->satadrv_id.ai_majorversion, 9064 sdinfo->satadrv_id.ai_minorversion); 9065 } 9066 #endif 9067 /* Log some info */ 9068 cmn_err(CE_CONT, "?\tsupported features:\n"); 9069 msg_buf[0] = '\0'; 9070 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 9071 (void) strlcat(msg_buf, "48-bit LBA", MAXPATHLEN); 9072 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 9073 (void) strlcat(msg_buf, "28-bit LBA", MAXPATHLEN); 9074 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 9075 (void) strlcat(msg_buf, ", DMA", MAXPATHLEN); 9076 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 9077 (void) strlcat(msg_buf, ", Native Command Queueing", 9078 MAXPATHLEN); 9079 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 9080 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 9081 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 9082 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 9083 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 9084 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 9085 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 9086 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 9087 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 9088 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 9089 cmn_err(CE_CONT, "?\tSATA1 & SATA2 compatible\n"); 9090 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 9091 cmn_err(CE_CONT, "?\tSATA1 compatible\n"); 9092 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) { 9093 cmn_err(CE_CONT, "?\tQueue depth %d\n", 9094 sdinfo->satadrv_queue_depth); 9095 } 9096 9097 if (sdinfo->satadrv_features_support & 9098 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 9099 (void) sprintf(msg_buf, "\tqueue depth %d\n", 9100 sdinfo->satadrv_queue_depth); 9101 cmn_err(CE_CONT, "?%s", msg_buf); 9102 } 9103 9104 #ifdef __i386 9105 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 9106 sdinfo->satadrv_capacity); 9107 #else 9108 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 9109 sdinfo->satadrv_capacity); 9110 #endif 9111 cmn_err(CE_CONT, "?%s", msg_buf); 9112 } 9113 9114 9115 /* 9116 * sata_save_drive_settings extracts current setting of the device and stores 9117 * it for future reference, in case the device setup would need to be restored 9118 * after the device reset. 9119 * 9120 * At the moment only read ahead and write cache settings are saved, if the 9121 * device supports these features at all. 9122 */ 9123 static void 9124 sata_save_drive_settings(sata_drive_info_t *sdinfo) 9125 { 9126 if (!(sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) && 9127 !(sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 9128 /* None of the features is supported - do nothing */ 9129 return; 9130 } 9131 9132 /* Current setting of Read Ahead (and Read Cache) */ 9133 if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD) 9134 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 9135 else 9136 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 9137 9138 /* Current setting of Write Cache */ 9139 if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE) 9140 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9141 else 9142 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9143 } 9144 9145 9146 /* 9147 * sata_check_capacity function determines a disk capacity 9148 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 9149 * 9150 * NOTE: CHS mode is not supported! If a device does not support LBA, 9151 * this function is not called. 9152 * 9153 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 9154 */ 9155 static uint64_t 9156 sata_check_capacity(sata_drive_info_t *sdinfo) 9157 { 9158 uint64_t capacity = 0; 9159 int i; 9160 9161 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 9162 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 9163 /* Capacity valid only for LBA-addressable disk devices */ 9164 return (0); 9165 9166 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 9167 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 9168 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 9169 /* LBA48 mode supported and enabled */ 9170 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 9171 SATA_DEV_F_LBA28; 9172 for (i = 3; i >= 0; --i) { 9173 capacity <<= 16; 9174 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 9175 } 9176 } else { 9177 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 9178 capacity <<= 16; 9179 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 9180 if (capacity >= 0x1000000) 9181 /* LBA28 mode */ 9182 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 9183 } 9184 return (capacity); 9185 } 9186 9187 9188 /* 9189 * Allocate consistent buffer for DMA transfer 9190 * 9191 * Cannot be called from interrupt level or with mutex held - it may sleep. 9192 * 9193 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 9194 */ 9195 static struct buf * 9196 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 9197 { 9198 struct scsi_address ap; 9199 struct buf *bp; 9200 ddi_dma_attr_t cur_dma_attr; 9201 9202 ASSERT(spx->txlt_sata_pkt != NULL); 9203 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 9204 ap.a_target = SATA_TO_SCSI_TARGET( 9205 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 9206 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 9207 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 9208 ap.a_lun = 0; 9209 9210 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 9211 B_READ, SLEEP_FUNC, NULL); 9212 9213 if (bp != NULL) { 9214 /* Allocate DMA resources for this buffer */ 9215 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 9216 /* 9217 * We use a local version of the dma_attr, to account 9218 * for a device addressing limitations. 9219 * sata_adjust_dma_attr() will handle sdinfo == NULL which 9220 * will cause dma attributes to be adjusted to a lowest 9221 * acceptable level. 9222 */ 9223 sata_adjust_dma_attr(NULL, 9224 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 9225 9226 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 9227 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 9228 scsi_free_consistent_buf(bp); 9229 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9230 bp = NULL; 9231 } 9232 } 9233 return (bp); 9234 } 9235 9236 /* 9237 * Release local buffer (consistent buffer for DMA transfer) allocated 9238 * via sata_alloc_local_buffer(). 9239 */ 9240 static void 9241 sata_free_local_buffer(sata_pkt_txlate_t *spx) 9242 { 9243 ASSERT(spx->txlt_sata_pkt != NULL); 9244 ASSERT(spx->txlt_dma_cookie_list != NULL); 9245 ASSERT(spx->txlt_dma_cookie_list_len != 0); 9246 ASSERT(spx->txlt_buf_dma_handle != NULL); 9247 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 9248 9249 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 9250 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 9251 9252 /* Free DMA resources */ 9253 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 9254 ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 9255 spx->txlt_buf_dma_handle = 0; 9256 9257 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 9258 kmem_free(spx->txlt_dma_cookie_list, 9259 spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t)); 9260 spx->txlt_dma_cookie_list = NULL; 9261 spx->txlt_dma_cookie_list_len = 0; 9262 } 9263 /* Free buffer */ 9264 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 9265 } 9266 9267 9268 9269 9270 /* 9271 * Allocate sata_pkt 9272 * Pkt structure version and embedded strcutures version are initialized. 9273 * sata_pkt and sata_pkt_txlate structures are cross-linked. 9274 * 9275 * Since this may be called in interrupt context by sata_scsi_init_pkt, 9276 * callback argument determines if it can sleep or not. 9277 * Hence, it should not be called from interrupt context. 9278 * 9279 * If successful, non-NULL pointer to a sata pkt is returned. 9280 * Upon failure, NULL pointer is returned. 9281 */ 9282 static sata_pkt_t * 9283 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 9284 { 9285 sata_pkt_t *spkt; 9286 int kmsflag; 9287 9288 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 9289 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 9290 if (spkt == NULL) { 9291 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9292 "sata_pkt_alloc: failed")); 9293 return (NULL); 9294 } 9295 spkt->satapkt_rev = SATA_PKT_REV; 9296 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 9297 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 9298 spkt->satapkt_framework_private = spx; 9299 spx->txlt_sata_pkt = spkt; 9300 return (spkt); 9301 } 9302 9303 /* 9304 * Free sata pkt allocated via sata_pkt_alloc() 9305 */ 9306 static void 9307 sata_pkt_free(sata_pkt_txlate_t *spx) 9308 { 9309 ASSERT(spx->txlt_sata_pkt != NULL); 9310 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 9311 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 9312 spx->txlt_sata_pkt = NULL; 9313 } 9314 9315 9316 /* 9317 * Adjust DMA attributes. 9318 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 9319 * from 8 bits to 16 bits, depending on a command being used. 9320 * Limiting max block count arbitrarily to 256 for all read/write 9321 * commands may affects performance, so check both the device and 9322 * controller capability before adjusting dma attributes. 9323 * For ATAPI CD/DVD dma granularity has to be adjusted as well, 9324 * because these devices support block size of 2k rather 9325 * then 512 bytes. 9326 */ 9327 void 9328 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 9329 ddi_dma_attr_t *adj_dma_attr) 9330 { 9331 uint32_t count_max; 9332 9333 /* Copy original attributes */ 9334 *adj_dma_attr = *dma_attr; 9335 9336 /* 9337 * Things to consider: device addressing capability, 9338 * "excessive" controller DMA capabilities. 9339 * If a device is being probed/initialized, there are 9340 * no device info - use default limits then. 9341 */ 9342 if (sdinfo == NULL) { 9343 count_max = dma_attr->dma_attr_granular * 0x100; 9344 if (dma_attr->dma_attr_count_max > count_max) 9345 adj_dma_attr->dma_attr_count_max = count_max; 9346 if (dma_attr->dma_attr_maxxfer > count_max) 9347 adj_dma_attr->dma_attr_maxxfer = count_max; 9348 return; 9349 } 9350 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9351 /* arbitrarily modify controller dma granularity */ 9352 adj_dma_attr->dma_attr_granular = SATA_ATAPI_SECTOR_SIZE; 9353 } 9354 9355 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 9356 /* 9357 * 16-bit sector count may be used - we rely on 9358 * the assumption that only read and write cmds 9359 * will request more than 256 sectors worth of data 9360 */ 9361 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 9362 } else { 9363 /* 9364 * 8-bit sector count will be used - default limits 9365 * for dma attributes 9366 */ 9367 count_max = adj_dma_attr->dma_attr_granular * 0x100; 9368 } 9369 9370 9371 /* 9372 * Adjust controler dma attributes, if necessary 9373 */ 9374 if (dma_attr->dma_attr_count_max > count_max) 9375 adj_dma_attr->dma_attr_count_max = count_max; 9376 if (dma_attr->dma_attr_maxxfer > count_max) 9377 adj_dma_attr->dma_attr_maxxfer = count_max; 9378 } 9379 9380 9381 /* 9382 * Allocate DMA resources for the buffer 9383 * This function handles initial DMA resource allocation as well as 9384 * DMA window shift and may be called repeatedly for the same DMA window 9385 * until all DMA cookies in the DMA window are processed. 9386 * To guarantee that there is always a coherent set of cookies to process 9387 * by SATA HBA driver (observing alignment, device granularity, etc.), 9388 * the number of slots for DMA cookies is equal to lesser of a number of 9389 * cookies in a DMA window and a max number of scatter/gather entries. 9390 * 9391 * Returns DDI_SUCCESS upon successful operation. 9392 * Return failure code of a failing command or DDI_FAILURE when 9393 * internal cleanup failed. 9394 */ 9395 static int 9396 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 9397 int (*callback)(caddr_t), caddr_t arg, 9398 ddi_dma_attr_t *cur_dma_attr) 9399 { 9400 int rval; 9401 off_t offset; 9402 size_t size; 9403 int max_sg_len, req_len, i; 9404 uint_t dma_flags; 9405 struct buf *bp; 9406 uint64_t cur_txfer_len; 9407 9408 9409 ASSERT(spx->txlt_sata_pkt != NULL); 9410 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9411 ASSERT(bp != NULL); 9412 9413 9414 if (spx->txlt_buf_dma_handle == NULL) { 9415 /* 9416 * No DMA resources allocated so far - this is a first call 9417 * for this sata pkt. 9418 */ 9419 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 9420 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 9421 9422 if (rval != DDI_SUCCESS) { 9423 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9424 "sata_dma_buf_setup: no buf DMA resources %x", 9425 rval)); 9426 return (rval); 9427 } 9428 9429 if (bp->b_flags & B_READ) 9430 dma_flags = DDI_DMA_READ; 9431 else 9432 dma_flags = DDI_DMA_WRITE; 9433 9434 if (flags & PKT_CONSISTENT) 9435 dma_flags |= DDI_DMA_CONSISTENT; 9436 9437 if (flags & PKT_DMA_PARTIAL) 9438 dma_flags |= DDI_DMA_PARTIAL; 9439 9440 /* 9441 * Check buffer alignment and size against dma attributes 9442 * Consider dma_attr_align only. There may be requests 9443 * with the size lower then device granularity, but they 9444 * will not read/write from/to the device, so no adjustment 9445 * is necessary. The dma_attr_minxfer theoretically should 9446 * be considered, but no HBA driver is checking it. 9447 */ 9448 if (IS_P2ALIGNED(bp->b_un.b_addr, 9449 cur_dma_attr->dma_attr_align)) { 9450 rval = ddi_dma_buf_bind_handle( 9451 spx->txlt_buf_dma_handle, 9452 bp, dma_flags, callback, arg, 9453 &spx->txlt_dma_cookie, 9454 &spx->txlt_curwin_num_dma_cookies); 9455 } else { /* Buffer is not aligned */ 9456 9457 int (*ddicallback)(caddr_t); 9458 size_t bufsz; 9459 9460 /* Check id sleeping is allowed */ 9461 ddicallback = (callback == NULL_FUNC) ? 9462 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 9463 9464 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9465 "mis-aligned buffer: addr=0x%p, cnt=%lu", 9466 (void *)bp->b_un.b_addr, bp->b_bcount); 9467 9468 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 9469 /* 9470 * CPU will need to access data in the buffer 9471 * (for copying) so map it. 9472 */ 9473 bp_mapin(bp); 9474 9475 ASSERT(spx->txlt_tmp_buf == NULL); 9476 9477 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 9478 rval = ddi_dma_mem_alloc( 9479 spx->txlt_buf_dma_handle, 9480 bp->b_bcount, 9481 &sata_acc_attr, 9482 DDI_DMA_STREAMING, 9483 ddicallback, NULL, 9484 &spx->txlt_tmp_buf, 9485 &bufsz, 9486 &spx->txlt_tmp_buf_handle); 9487 9488 if (rval != DDI_SUCCESS) { 9489 /* DMA mapping failed */ 9490 (void) ddi_dma_free_handle( 9491 &spx->txlt_buf_dma_handle); 9492 spx->txlt_buf_dma_handle = NULL; 9493 #ifdef SATA_DEBUG 9494 mbuffail_count++; 9495 #endif 9496 SATADBG1(SATA_DBG_DMA_SETUP, 9497 spx->txlt_sata_hba_inst, 9498 "sata_dma_buf_setup: " 9499 "buf dma mem alloc failed %x\n", rval); 9500 return (rval); 9501 } 9502 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 9503 cur_dma_attr->dma_attr_align)); 9504 9505 #ifdef SATA_DEBUG 9506 mbuf_count++; 9507 9508 if (bp->b_bcount != bufsz) 9509 /* 9510 * This will require special handling, because 9511 * DMA cookies will be based on the temporary 9512 * buffer size, not the original buffer 9513 * b_bcount, so the residue may have to 9514 * be counted differently. 9515 */ 9516 SATADBG2(SATA_DBG_DMA_SETUP, 9517 spx->txlt_sata_hba_inst, 9518 "sata_dma_buf_setup: bp size %x != " 9519 "bufsz %x\n", bp->b_bcount, bufsz); 9520 #endif 9521 if (dma_flags & DDI_DMA_WRITE) { 9522 /* 9523 * Write operation - copy data into 9524 * an aligned temporary buffer. Buffer will be 9525 * synced for device by ddi_dma_addr_bind_handle 9526 */ 9527 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 9528 bp->b_bcount); 9529 } 9530 9531 rval = ddi_dma_addr_bind_handle( 9532 spx->txlt_buf_dma_handle, 9533 NULL, 9534 spx->txlt_tmp_buf, 9535 bufsz, dma_flags, ddicallback, 0, 9536 &spx->txlt_dma_cookie, 9537 &spx->txlt_curwin_num_dma_cookies); 9538 } 9539 9540 switch (rval) { 9541 case DDI_DMA_PARTIAL_MAP: 9542 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9543 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 9544 /* 9545 * Partial DMA mapping. 9546 * Retrieve number of DMA windows for this request. 9547 */ 9548 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 9549 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 9550 if (spx->txlt_tmp_buf != NULL) { 9551 ddi_dma_mem_free( 9552 &spx->txlt_tmp_buf_handle); 9553 spx->txlt_tmp_buf = NULL; 9554 } 9555 (void) ddi_dma_unbind_handle( 9556 spx->txlt_buf_dma_handle); 9557 (void) ddi_dma_free_handle( 9558 &spx->txlt_buf_dma_handle); 9559 spx->txlt_buf_dma_handle = NULL; 9560 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9561 "sata_dma_buf_setup: numwin failed\n")); 9562 return (DDI_FAILURE); 9563 } 9564 SATADBG2(SATA_DBG_DMA_SETUP, 9565 spx->txlt_sata_hba_inst, 9566 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 9567 spx->txlt_num_dma_win, 9568 spx->txlt_curwin_num_dma_cookies); 9569 spx->txlt_cur_dma_win = 0; 9570 break; 9571 9572 case DDI_DMA_MAPPED: 9573 /* DMA fully mapped */ 9574 spx->txlt_num_dma_win = 1; 9575 spx->txlt_cur_dma_win = 0; 9576 SATADBG1(SATA_DBG_DMA_SETUP, 9577 spx->txlt_sata_hba_inst, 9578 "sata_dma_buf_setup: windows: 1 " 9579 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 9580 break; 9581 9582 default: 9583 /* DMA mapping failed */ 9584 if (spx->txlt_tmp_buf != NULL) { 9585 ddi_dma_mem_free( 9586 &spx->txlt_tmp_buf_handle); 9587 spx->txlt_tmp_buf = NULL; 9588 } 9589 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 9590 spx->txlt_buf_dma_handle = NULL; 9591 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9592 "sata_dma_buf_setup: buf dma handle binding " 9593 "failed %x\n", rval)); 9594 return (rval); 9595 } 9596 spx->txlt_curwin_processed_dma_cookies = 0; 9597 spx->txlt_dma_cookie_list = NULL; 9598 } else { 9599 /* 9600 * DMA setup is reused. Check if we need to process more 9601 * cookies in current window, or to get next window, if any. 9602 */ 9603 9604 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 9605 spx->txlt_curwin_num_dma_cookies); 9606 9607 if (spx->txlt_curwin_processed_dma_cookies == 9608 spx->txlt_curwin_num_dma_cookies) { 9609 /* 9610 * All cookies from current DMA window were processed. 9611 * Get next DMA window. 9612 */ 9613 spx->txlt_cur_dma_win++; 9614 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 9615 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 9616 spx->txlt_cur_dma_win, &offset, &size, 9617 &spx->txlt_dma_cookie, 9618 &spx->txlt_curwin_num_dma_cookies); 9619 spx->txlt_curwin_processed_dma_cookies = 0; 9620 } else { 9621 /* No more windows! End of request! */ 9622 /* What to do? - panic for now */ 9623 ASSERT(spx->txlt_cur_dma_win >= 9624 spx->txlt_num_dma_win); 9625 9626 spx->txlt_curwin_num_dma_cookies = 0; 9627 spx->txlt_curwin_processed_dma_cookies = 0; 9628 spx->txlt_sata_pkt-> 9629 satapkt_cmd.satacmd_num_dma_cookies = 0; 9630 return (DDI_SUCCESS); 9631 } 9632 } 9633 } 9634 /* There better be at least one DMA cookie outstanding */ 9635 ASSERT((spx->txlt_curwin_num_dma_cookies - 9636 spx->txlt_curwin_processed_dma_cookies) > 0); 9637 9638 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 9639 /* The default cookie slot was used in previous run */ 9640 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 9641 spx->txlt_dma_cookie_list = NULL; 9642 spx->txlt_dma_cookie_list_len = 0; 9643 } 9644 if (spx->txlt_curwin_processed_dma_cookies == 0) { 9645 /* 9646 * Processing a new DMA window - set-up dma cookies list. 9647 * We may reuse previously allocated cookie array if it is 9648 * possible. 9649 */ 9650 if (spx->txlt_dma_cookie_list != NULL && 9651 spx->txlt_dma_cookie_list_len < 9652 spx->txlt_curwin_num_dma_cookies) { 9653 /* 9654 * New DMA window contains more cookies than 9655 * the previous one. We need larger cookie list - free 9656 * the old one. 9657 */ 9658 (void) kmem_free(spx->txlt_dma_cookie_list, 9659 spx->txlt_dma_cookie_list_len * 9660 sizeof (ddi_dma_cookie_t)); 9661 spx->txlt_dma_cookie_list = NULL; 9662 spx->txlt_dma_cookie_list_len = 0; 9663 } 9664 if (spx->txlt_dma_cookie_list == NULL) { 9665 /* 9666 * Calculate lesser of number of cookies in this 9667 * DMA window and number of s/g entries. 9668 */ 9669 max_sg_len = cur_dma_attr->dma_attr_sgllen; 9670 req_len = MIN(max_sg_len, 9671 spx->txlt_curwin_num_dma_cookies); 9672 9673 /* Allocate new dma cookie array if necessary */ 9674 if (req_len == 1) { 9675 /* Only one cookie - no need for a list */ 9676 spx->txlt_dma_cookie_list = 9677 &spx->txlt_dma_cookie; 9678 spx->txlt_dma_cookie_list_len = 1; 9679 } else { 9680 /* 9681 * More than one cookie - try to allocate space. 9682 */ 9683 spx->txlt_dma_cookie_list = kmem_zalloc( 9684 sizeof (ddi_dma_cookie_t) * req_len, 9685 callback == NULL_FUNC ? KM_NOSLEEP : 9686 KM_SLEEP); 9687 if (spx->txlt_dma_cookie_list == NULL) { 9688 SATADBG1(SATA_DBG_DMA_SETUP, 9689 spx->txlt_sata_hba_inst, 9690 "sata_dma_buf_setup: cookie list " 9691 "allocation failed\n", NULL); 9692 /* 9693 * We could not allocate space for 9694 * neccessary number of dma cookies in 9695 * this window, so we fail this request. 9696 * Next invocation would try again to 9697 * allocate space for cookie list. 9698 * Note:Packet residue was not modified. 9699 */ 9700 return (DDI_DMA_NORESOURCES); 9701 } else { 9702 spx->txlt_dma_cookie_list_len = req_len; 9703 } 9704 } 9705 } 9706 /* 9707 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 9708 * First cookie was already fetched. 9709 */ 9710 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 9711 cur_txfer_len = 9712 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 9713 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 9714 spx->txlt_curwin_processed_dma_cookies++; 9715 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 9716 (i < spx->txlt_curwin_num_dma_cookies); i++) { 9717 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 9718 &spx->txlt_dma_cookie_list[i]); 9719 cur_txfer_len += 9720 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 9721 spx->txlt_curwin_processed_dma_cookies++; 9722 spx->txlt_sata_pkt-> 9723 satapkt_cmd.satacmd_num_dma_cookies += 1; 9724 } 9725 } else { 9726 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9727 "sata_dma_buf_setup: sliding within DMA window, " 9728 "cur cookie %d, total cookies %d\n", 9729 spx->txlt_curwin_processed_dma_cookies, 9730 spx->txlt_curwin_num_dma_cookies); 9731 9732 /* 9733 * Not all cookies from the current dma window were used because 9734 * of s/g limitation. 9735 * There is no need to re-size the list - it was set at 9736 * optimal size, or only default entry is used (s/g = 1). 9737 */ 9738 if (spx->txlt_dma_cookie_list == NULL) { 9739 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 9740 spx->txlt_dma_cookie_list_len = 1; 9741 } 9742 /* 9743 * Since we are processing remaining cookies in a DMA window, 9744 * there may be less of them than the number of entries in the 9745 * current dma cookie list. 9746 */ 9747 req_len = MIN(spx->txlt_dma_cookie_list_len, 9748 (spx->txlt_curwin_num_dma_cookies - 9749 spx->txlt_curwin_processed_dma_cookies)); 9750 9751 /* Fetch the next batch of cookies */ 9752 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 9753 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 9754 &spx->txlt_dma_cookie_list[i]); 9755 cur_txfer_len += 9756 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 9757 spx->txlt_sata_pkt-> 9758 satapkt_cmd.satacmd_num_dma_cookies++; 9759 spx->txlt_curwin_processed_dma_cookies++; 9760 } 9761 } 9762 9763 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 9764 9765 /* Point sata_cmd to the cookie list */ 9766 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 9767 &spx->txlt_dma_cookie_list[0]; 9768 9769 /* Remember number of DMA cookies passed in sata packet */ 9770 spx->txlt_num_dma_cookies = 9771 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 9772 9773 ASSERT(cur_txfer_len != 0); 9774 if (cur_txfer_len <= bp->b_bcount) 9775 spx->txlt_total_residue -= cur_txfer_len; 9776 else { 9777 /* 9778 * Temporary DMA buffer has been padded by 9779 * ddi_dma_mem_alloc()! 9780 * This requires special handling, because DMA cookies are 9781 * based on the temporary buffer size, not the b_bcount, 9782 * and we have extra bytes to transfer - but the packet 9783 * residue has to stay correct because we will copy only 9784 * the requested number of bytes. 9785 */ 9786 spx->txlt_total_residue -= bp->b_bcount; 9787 } 9788 9789 return (DDI_SUCCESS); 9790 } 9791 9792 9793 /* 9794 * Fetch Device Identify data. 9795 * Send DEVICE IDENTIFY command to a device and get the device identify data. 9796 * The device_info structure has to be set to device type (for selecting proper 9797 * device identify command). 9798 * 9799 * Returns: 9800 * SATA_SUCCESS if cmd succeeded 9801 * SATA_RETRY if cmd was rejected and could be retried, 9802 * SATA_FAILURE if cmd failed and should not be retried (port error) 9803 * 9804 * Cannot be called in an interrupt context. 9805 */ 9806 9807 static int 9808 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 9809 sata_drive_info_t *sdinfo) 9810 { 9811 struct buf *bp; 9812 sata_pkt_t *spkt; 9813 sata_cmd_t *scmd; 9814 sata_pkt_txlate_t *spx; 9815 int rval; 9816 9817 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 9818 spx->txlt_sata_hba_inst = sata_hba_inst; 9819 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 9820 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 9821 if (spkt == NULL) { 9822 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9823 return (SATA_RETRY); /* may retry later */ 9824 } 9825 /* address is needed now */ 9826 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 9827 9828 /* 9829 * Allocate buffer for Identify Data return data 9830 */ 9831 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 9832 if (bp == NULL) { 9833 sata_pkt_free(spx); 9834 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9835 SATA_LOG_D((sata_hba_inst, CE_WARN, 9836 "sata_fetch_device_identify_data: " 9837 "cannot allocate buffer for ID")); 9838 return (SATA_RETRY); /* may retry later */ 9839 } 9840 9841 /* Fill sata_pkt */ 9842 sdinfo->satadrv_state = SATA_STATE_PROBING; 9843 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 9844 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9845 /* Synchronous mode, no callback */ 9846 spkt->satapkt_comp = NULL; 9847 /* Timeout 30s */ 9848 spkt->satapkt_time = sata_default_pkt_time; 9849 9850 scmd = &spkt->satapkt_cmd; 9851 scmd->satacmd_bp = bp; 9852 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9853 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9854 9855 /* Build Identify Device cmd in the sata_pkt */ 9856 scmd->satacmd_addr_type = 0; /* N/A */ 9857 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 9858 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 9859 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 9860 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 9861 scmd->satacmd_features_reg = 0; /* N/A */ 9862 scmd->satacmd_device_reg = 0; /* Always device 0 */ 9863 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9864 /* Identify Packet Device cmd */ 9865 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 9866 } else { 9867 /* Identify Device cmd - mandatory for all other devices */ 9868 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 9869 } 9870 9871 /* Send pkt to SATA HBA driver */ 9872 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 9873 if (rval == SATA_TRAN_ACCEPTED && 9874 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 9875 if ((sdinfo->satadrv_id.ai_config & SATA_INCOMPLETE_DATA) == 9876 SATA_INCOMPLETE_DATA) { 9877 SATA_LOG_D((sata_hba_inst, CE_WARN, 9878 "SATA disk device at port %d - " 9879 "partial Identify Data", 9880 sdinfo->satadrv_addr.cport)); 9881 rval = SATA_RETRY; /* may retry later */ 9882 goto fail; 9883 } 9884 /* Update sata_drive_info */ 9885 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 9886 DDI_DMA_SYNC_FORKERNEL); 9887 ASSERT(rval == DDI_SUCCESS); 9888 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 9889 sizeof (sata_id_t)); 9890 9891 sdinfo->satadrv_features_support = 0; 9892 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9893 /* 9894 * Retrieve capacity (disks only) and addressing mode 9895 */ 9896 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 9897 } else { 9898 /* 9899 * For ATAPI devices one has to issue Get Capacity cmd 9900 * (not needed at the moment) 9901 */ 9902 sdinfo->satadrv_capacity = 0; 9903 } 9904 /* Setup supported features flags */ 9905 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 9906 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 9907 9908 /* Check for NCQ support */ 9909 if (sdinfo->satadrv_id.ai_satacap != 0 && 9910 sdinfo->satadrv_id.ai_satacap != 0xffff) { 9911 /* SATA compliance */ 9912 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 9913 sdinfo->satadrv_features_support |= 9914 SATA_DEV_F_NCQ; 9915 if (sdinfo->satadrv_id.ai_satacap & 9916 (SATA_1_SPEED | SATA_2_SPEED)) { 9917 if (sdinfo->satadrv_id.ai_satacap & 9918 SATA_2_SPEED) 9919 sdinfo->satadrv_features_support |= 9920 SATA_DEV_F_SATA2; 9921 if (sdinfo->satadrv_id.ai_satacap & 9922 SATA_1_SPEED) 9923 sdinfo->satadrv_features_support |= 9924 SATA_DEV_F_SATA1; 9925 } else { 9926 sdinfo->satadrv_features_support |= 9927 SATA_DEV_F_SATA1; 9928 } 9929 } 9930 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 9931 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 9932 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 9933 9934 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 9935 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 9936 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) 9937 ++sdinfo->satadrv_queue_depth; 9938 rval = SATA_SUCCESS; 9939 } else { 9940 /* 9941 * Woops, no Identify Data. 9942 */ 9943 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 9944 rval = SATA_RETRY; /* may retry later */ 9945 } else if (rval == SATA_TRAN_ACCEPTED) { 9946 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 9947 spkt->satapkt_reason == SATA_PKT_ABORTED || 9948 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 9949 spkt->satapkt_reason == SATA_PKT_RESET) 9950 rval = SATA_RETRY; /* may retry later */ 9951 else 9952 rval = SATA_FAILURE; 9953 } else { 9954 rval = SATA_FAILURE; 9955 } 9956 } 9957 fail: 9958 /* Free allocated resources */ 9959 sata_free_local_buffer(spx); 9960 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9961 sata_pkt_free(spx); 9962 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9963 9964 return (rval); 9965 } 9966 9967 9968 /* 9969 * SATA spec requires that the device supports at least UDMA 4 mode and 9970 * UDMA mode is selected. 9971 * Some devices (bridged devices) may not come-up with default UDMA mode 9972 * set correctly, so this function is setting it. 9973 * 9974 * Returns SATA_SUCCESS if proper UDMA mode is selected. 9975 * Returns SATA_FAILURE if proper UDMA mode could not be selected. 9976 */ 9977 static int 9978 sata_set_udma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 9979 { 9980 sata_pkt_t *spkt; 9981 sata_cmd_t *scmd; 9982 sata_pkt_txlate_t *spx; 9983 int result = SATA_SUCCESS; 9984 int i, mode; 9985 9986 ASSERT(sdinfo != NULL); 9987 ASSERT(sata_hba_inst != NULL); 9988 9989 /* Find highest Ultra DMA mode supported */ 9990 for (mode = 6; mode >= 0; --mode) { 9991 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 9992 break; 9993 } 9994 if (mode < 4) 9995 return (SATA_FAILURE); 9996 9997 /* Find UDMA mode currently selected */ 9998 for (i = 6; i >= 0; --i) { 9999 if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8))) 10000 break; 10001 } 10002 10003 if (i < mode) { 10004 /* Set UDMA mode via SET FEATURES COMMAND */ 10005 /* Prepare packet for SET FEATURES COMMAND */ 10006 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10007 spx->txlt_sata_hba_inst = sata_hba_inst; 10008 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10009 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10010 if (spkt == NULL) { 10011 result = SATA_FAILURE; 10012 goto failure; 10013 } 10014 /* Fill sata_pkt */ 10015 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10016 /* Timeout 30s */ 10017 spkt->satapkt_time = sata_default_pkt_time; 10018 /* Synchronous mode, no callback, interrupts */ 10019 spkt->satapkt_op_mode = 10020 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10021 spkt->satapkt_comp = NULL; 10022 scmd = &spkt->satapkt_cmd; 10023 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10024 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10025 scmd->satacmd_addr_type = 0; 10026 scmd->satacmd_device_reg = 0; 10027 scmd->satacmd_status_reg = 0; 10028 scmd->satacmd_error_reg = 0; 10029 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10030 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 10031 scmd->satacmd_sec_count_lsb = 10032 SATAC_TRANSFER_MODE_ULTRA_DMA | mode; 10033 10034 /* Transfer command to HBA */ 10035 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 10036 spkt) != SATA_TRAN_ACCEPTED || 10037 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 10038 /* Pkt execution failed */ 10039 result = SATA_FAILURE; 10040 } 10041 failure: 10042 if (result == SATA_FAILURE) 10043 SATA_LOG_D((sata_hba_inst, CE_WARN, 10044 "sata_set_udma_mode: could not set UDMA " 10045 "mode %", mode)); 10046 10047 /* Free allocated resources */ 10048 if (spkt != NULL) 10049 sata_pkt_free(spx); 10050 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10051 } 10052 return (result); 10053 } 10054 10055 10056 /* 10057 * Set device caching mode. 10058 * One of the following operations should be specified: 10059 * SATAC_SF_ENABLE_READ_AHEAD 10060 * SATAC_SF_DISABLE_READ_AHEAD 10061 * SATAC_SF_ENABLE_WRITE_CACHE 10062 * SATAC_SF_DISABLE_WRITE_CACHE 10063 * 10064 * If operation fails, system log messgage is emitted. 10065 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 10066 */ 10067 10068 static int 10069 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10070 int cache_op) 10071 { 10072 sata_pkt_t *spkt; 10073 sata_cmd_t *scmd; 10074 sata_pkt_txlate_t *spx; 10075 int rval = SATA_SUCCESS; 10076 char *infop; 10077 10078 ASSERT(sdinfo != NULL); 10079 ASSERT(sata_hba_inst != NULL); 10080 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 10081 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 10082 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 10083 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 10084 10085 10086 /* Prepare packet for SET FEATURES COMMAND */ 10087 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10088 spx->txlt_sata_hba_inst = sata_hba_inst; 10089 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10090 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10091 if (spkt == NULL) { 10092 rval = SATA_FAILURE; 10093 goto failure; 10094 } 10095 /* Fill sata_pkt */ 10096 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10097 /* Timeout 30s */ 10098 spkt->satapkt_time = sata_default_pkt_time; 10099 /* Synchronous mode, no callback, interrupts */ 10100 spkt->satapkt_op_mode = 10101 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10102 spkt->satapkt_comp = NULL; 10103 scmd = &spkt->satapkt_cmd; 10104 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10105 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10106 scmd->satacmd_addr_type = 0; 10107 scmd->satacmd_device_reg = 0; 10108 scmd->satacmd_status_reg = 0; 10109 scmd->satacmd_error_reg = 0; 10110 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10111 scmd->satacmd_features_reg = cache_op; 10112 10113 /* Transfer command to HBA */ 10114 if (((*SATA_START_FUNC(sata_hba_inst))( 10115 SATA_DIP(sata_hba_inst), spkt) != 0) || 10116 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10117 /* Pkt execution failed */ 10118 switch (cache_op) { 10119 case SATAC_SF_ENABLE_READ_AHEAD: 10120 infop = "enabling read ahead failed"; 10121 break; 10122 case SATAC_SF_DISABLE_READ_AHEAD: 10123 infop = "disabling read ahead failed"; 10124 break; 10125 case SATAC_SF_ENABLE_WRITE_CACHE: 10126 infop = "enabling write cache failed"; 10127 break; 10128 case SATAC_SF_DISABLE_WRITE_CACHE: 10129 infop = "disabling write cache failed"; 10130 break; 10131 } 10132 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10133 rval = SATA_FAILURE; 10134 } 10135 failure: 10136 /* Free allocated resources */ 10137 if (spkt != NULL) 10138 sata_pkt_free(spx); 10139 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10140 return (rval); 10141 } 10142 10143 10144 10145 /* 10146 * Update port SCR block 10147 */ 10148 static void 10149 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device) 10150 { 10151 port_scr->sstatus = device->satadev_scr.sstatus; 10152 port_scr->serror = device->satadev_scr.serror; 10153 port_scr->scontrol = device->satadev_scr.scontrol; 10154 port_scr->sactive = device->satadev_scr.sactive; 10155 port_scr->snotific = device->satadev_scr.snotific; 10156 } 10157 10158 /* 10159 * Update state and copy port ss* values from passed sata_device structure. 10160 * sata_address is validated - if not valid, nothing is changed in sata_scsi 10161 * configuration struct. 10162 * 10163 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 10164 * regardless of the state in device argument. 10165 * 10166 * Port mutex should be held while calling this function. 10167 */ 10168 static void 10169 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 10170 sata_device_t *sata_device) 10171 { 10172 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, 10173 sata_device->satadev_addr.cport))); 10174 10175 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 10176 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 10177 10178 sata_cport_info_t *cportinfo; 10179 10180 if (SATA_NUM_CPORTS(sata_hba_inst) <= 10181 sata_device->satadev_addr.cport) 10182 return; 10183 10184 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 10185 sata_device->satadev_addr.cport); 10186 sata_update_port_scr(&cportinfo->cport_scr, sata_device); 10187 10188 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10189 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 10190 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 10191 cportinfo->cport_state |= 10192 sata_device->satadev_state & SATA_PSTATE_VALID; 10193 } else { 10194 sata_pmport_info_t *pmportinfo; 10195 10196 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) || 10197 (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 10198 SATA_NUM_PMPORTS(sata_hba_inst, 10199 sata_device->satadev_addr.cport) < 10200 sata_device->satadev_addr.pmport) 10201 return; 10202 10203 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 10204 sata_device->satadev_addr.cport, 10205 sata_device->satadev_addr.pmport); 10206 sata_update_port_scr(&pmportinfo->pmport_scr, sata_device); 10207 10208 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10209 pmportinfo->pmport_state &= 10210 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | 10211 SATA_PSTATE_FAILED); 10212 pmportinfo->pmport_state |= 10213 sata_device->satadev_state & SATA_PSTATE_VALID; 10214 } 10215 } 10216 10217 10218 10219 /* 10220 * Extract SATA port specification from an IOCTL argument. 10221 * 10222 * This function return the port the user land send us as is, unless it 10223 * cannot retrieve port spec, then -1 is returned. 10224 * 10225 * Note: Only cport - no port multiplier port. 10226 */ 10227 static int32_t 10228 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 10229 { 10230 int32_t port; 10231 10232 /* Extract port number from nvpair in dca structure */ 10233 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 10234 SATA_LOG_D((sata_hba_inst, CE_NOTE, 10235 "sata_get_port_num: invalid port spec 0x%x in ioctl", 10236 port)); 10237 port = -1; 10238 } 10239 10240 return (port); 10241 } 10242 10243 /* 10244 * Get dev_info_t pointer to the device node pointed to by port argument. 10245 * NOTE: target argument is a value used in ioctls to identify 10246 * the AP - it is not a sata_address. 10247 * It is a combination of cport, pmport and address qualifier, encodded same 10248 * way as a scsi target number. 10249 * At this moment it carries only cport number. 10250 * 10251 * No PMult hotplug support. 10252 * 10253 * Returns dev_info_t pointer if target device was found, NULL otherwise. 10254 */ 10255 10256 static dev_info_t * 10257 sata_get_target_dip(dev_info_t *dip, int32_t port) 10258 { 10259 dev_info_t *cdip = NULL; 10260 int target, tgt; 10261 int ncport; 10262 int circ; 10263 10264 ncport = port & SATA_CFGA_CPORT_MASK; 10265 target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT); 10266 10267 ndi_devi_enter(dip, &circ); 10268 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 10269 dev_info_t *next = ddi_get_next_sibling(cdip); 10270 10271 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 10272 DDI_PROP_DONTPASS, "target", -1); 10273 if (tgt == -1) { 10274 /* 10275 * This is actually an error condition, but not 10276 * a fatal one. Just continue the search. 10277 */ 10278 cdip = next; 10279 continue; 10280 } 10281 10282 if (tgt == target) 10283 break; 10284 10285 cdip = next; 10286 } 10287 ndi_devi_exit(dip, circ); 10288 10289 return (cdip); 10290 } 10291 10292 10293 /* 10294 * sata_cfgadm_state: 10295 * Use the sata port state and state of the target node to figure out 10296 * the cfgadm_state. 10297 * 10298 * The port argument is a value with encoded cport, 10299 * pmport and address qualifier, in the same manner as a scsi target number. 10300 * SCSI_TO_SATA_CPORT macro extracts cport number, 10301 * SCSI_TO_SATA_PMPORT extracts pmport number and 10302 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 10303 * 10304 * For now, support is for cports only - no port multiplier device ports. 10305 */ 10306 10307 static void 10308 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 10309 devctl_ap_state_t *ap_state) 10310 { 10311 uint16_t cport; 10312 int port_state; 10313 10314 /* Cport only */ 10315 cport = SCSI_TO_SATA_CPORT(port); 10316 10317 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 10318 if (port_state & SATA_PSTATE_SHUTDOWN || 10319 port_state & SATA_PSTATE_FAILED) { 10320 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 10321 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 10322 if (port_state & SATA_PSTATE_FAILED) 10323 ap_state->ap_condition = AP_COND_FAILED; 10324 else 10325 ap_state->ap_condition = AP_COND_UNKNOWN; 10326 10327 return; 10328 } 10329 10330 /* Need to check pmult device port here as well, when supported */ 10331 10332 /* Port is enabled and ready */ 10333 10334 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 10335 case SATA_DTYPE_NONE: 10336 { 10337 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 10338 ap_state->ap_condition = AP_COND_OK; 10339 /* No device attached */ 10340 ap_state->ap_rstate = AP_RSTATE_EMPTY; 10341 break; 10342 } 10343 case SATA_DTYPE_UNKNOWN: 10344 case SATA_DTYPE_ATAPINONCD: 10345 case SATA_DTYPE_PMULT: /* Until PMult is supported */ 10346 case SATA_DTYPE_ATADISK: 10347 case SATA_DTYPE_ATAPICD: 10348 { 10349 dev_info_t *tdip = NULL; 10350 dev_info_t *dip = NULL; 10351 int circ; 10352 10353 dip = SATA_DIP(sata_hba_inst); 10354 tdip = sata_get_target_dip(dip, port); 10355 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 10356 if (tdip != NULL) { 10357 ndi_devi_enter(dip, &circ); 10358 mutex_enter(&(DEVI(tdip)->devi_lock)); 10359 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 10360 /* 10361 * There could be the case where previously 10362 * configured and opened device was removed 10363 * and unknown device was plugged. 10364 * In such case we want to show a device, and 10365 * its configured or unconfigured state but 10366 * indicate unusable condition untill the 10367 * old target node is released and removed. 10368 */ 10369 ap_state->ap_condition = AP_COND_UNUSABLE; 10370 } else { 10371 ap_state->ap_condition = AP_COND_OK; 10372 } 10373 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 10374 (DEVI_IS_DEVICE_DOWN(tdip))) { 10375 ap_state->ap_ostate = 10376 AP_OSTATE_UNCONFIGURED; 10377 } else { 10378 ap_state->ap_ostate = 10379 AP_OSTATE_CONFIGURED; 10380 } 10381 mutex_exit(&(DEVI(tdip)->devi_lock)); 10382 ndi_devi_exit(dip, circ); 10383 } else { 10384 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 10385 ap_state->ap_condition = AP_COND_UNKNOWN; 10386 } 10387 break; 10388 } 10389 default: 10390 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 10391 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 10392 ap_state->ap_condition = AP_COND_UNKNOWN; 10393 /* 10394 * This is actually internal error condition (non fatal), 10395 * because we have already checked all defined device types. 10396 */ 10397 SATA_LOG_D((sata_hba_inst, CE_WARN, 10398 "sata_cfgadm_state: Internal error: " 10399 "unknown device type")); 10400 break; 10401 } 10402 } 10403 10404 /* 10405 * Start or terminate the thread, depending on flag arg and current state 10406 */ 10407 static void 10408 sata_event_thread_control(int startstop) 10409 { 10410 static int sata_event_thread_terminating = 0; 10411 static int sata_event_thread_starting = 0; 10412 int i; 10413 10414 mutex_enter(&sata_event_mutex); 10415 10416 if (startstop == 0 && (sata_event_thread_starting == 1 || 10417 sata_event_thread_terminating == 1)) { 10418 mutex_exit(&sata_event_mutex); 10419 return; 10420 } 10421 if (startstop == 1 && sata_event_thread_starting == 1) { 10422 mutex_exit(&sata_event_mutex); 10423 return; 10424 } 10425 if (startstop == 1 && sata_event_thread_terminating == 1) { 10426 sata_event_thread_starting = 1; 10427 /* wait til terminate operation completes */ 10428 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 10429 while (sata_event_thread_terminating == 1) { 10430 if (i-- <= 0) { 10431 sata_event_thread_starting = 0; 10432 mutex_exit(&sata_event_mutex); 10433 #ifdef SATA_DEBUG 10434 cmn_err(CE_WARN, "sata_event_thread_control: " 10435 "timeout waiting for thread to terminate"); 10436 #endif 10437 return; 10438 } 10439 mutex_exit(&sata_event_mutex); 10440 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 10441 mutex_enter(&sata_event_mutex); 10442 } 10443 } 10444 if (startstop == 1) { 10445 if (sata_event_thread == NULL) { 10446 sata_event_thread = thread_create(NULL, 0, 10447 (void (*)())sata_event_daemon, 10448 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 10449 } 10450 sata_event_thread_starting = 0; 10451 mutex_exit(&sata_event_mutex); 10452 return; 10453 } 10454 10455 /* 10456 * If we got here, thread may need to be terminated 10457 */ 10458 if (sata_event_thread != NULL) { 10459 int i; 10460 /* Signal event thread to go away */ 10461 sata_event_thread_terminating = 1; 10462 sata_event_thread_terminate = 1; 10463 cv_signal(&sata_event_cv); 10464 /* 10465 * Wait til daemon terminates. 10466 */ 10467 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 10468 while (sata_event_thread_terminate == 1) { 10469 mutex_exit(&sata_event_mutex); 10470 if (i-- <= 0) { 10471 /* Daemon did not go away !!! */ 10472 #ifdef SATA_DEBUG 10473 cmn_err(CE_WARN, "sata_event_thread_control: " 10474 "cannot terminate event daemon thread"); 10475 #endif 10476 mutex_enter(&sata_event_mutex); 10477 break; 10478 } 10479 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 10480 mutex_enter(&sata_event_mutex); 10481 } 10482 sata_event_thread_terminating = 0; 10483 } 10484 ASSERT(sata_event_thread_terminating == 0); 10485 ASSERT(sata_event_thread_starting == 0); 10486 mutex_exit(&sata_event_mutex); 10487 } 10488 10489 10490 /* 10491 * Log sata message 10492 * dev pathname msg line preceeds the logged message. 10493 */ 10494 10495 static void 10496 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 10497 { 10498 char pathname[128]; 10499 dev_info_t *dip; 10500 va_list ap; 10501 10502 mutex_enter(&sata_log_mutex); 10503 10504 va_start(ap, fmt); 10505 (void) vsprintf(sata_log_buf, fmt, ap); 10506 va_end(ap); 10507 10508 if (sata_hba_inst != NULL) { 10509 dip = SATA_DIP(sata_hba_inst); 10510 (void) ddi_pathname(dip, pathname); 10511 } else { 10512 pathname[0] = 0; 10513 } 10514 if (level == CE_CONT) { 10515 if (sata_debug_flags == 0) 10516 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 10517 else 10518 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 10519 } else 10520 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 10521 10522 mutex_exit(&sata_log_mutex); 10523 } 10524 10525 10526 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 10527 10528 /* 10529 * SATA HBA event notification function. 10530 * Events reported by SATA HBA drivers per HBA instance relate to a change in 10531 * a port and/or device state or a controller itself. 10532 * Events for different addresses/addr types cannot be combined. 10533 * A warning message is generated for each event type. 10534 * Events are not processed by this function, so only the 10535 * event flag(s)is set for an affected entity and the event thread is 10536 * waken up. Event daemon thread processes all events. 10537 * 10538 * NOTE: Since more than one event may be reported at the same time, one 10539 * cannot determine a sequence of events when opposite event are reported, eg. 10540 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 10541 * is taking precedence over reported events, i.e. may cause ignoring some 10542 * events. 10543 */ 10544 #define SATA_EVENT_MAX_MSG_LENGTH 79 10545 10546 void 10547 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 10548 { 10549 sata_hba_inst_t *sata_hba_inst = NULL; 10550 sata_address_t *saddr; 10551 sata_drive_info_t *sdinfo; 10552 sata_port_stats_t *pstats; 10553 int cport, pmport; 10554 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 10555 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 10556 char *lcp; 10557 static char *err_msg_evnt_1 = 10558 "sata_hba_event_notify: invalid port event 0x%x "; 10559 static char *err_msg_evnt_2 = 10560 "sata_hba_event_notify: invalid device event 0x%x "; 10561 int linkevent; 10562 10563 /* 10564 * There is a possibility that an event will be generated on HBA 10565 * that has not completed attachment or is detaching. 10566 * HBA driver should prevent this, but just in case it does not, 10567 * we need to ignore events for such HBA. 10568 */ 10569 mutex_enter(&sata_mutex); 10570 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 10571 sata_hba_inst = sata_hba_inst->satahba_next) { 10572 if (SATA_DIP(sata_hba_inst) == dip) 10573 if (sata_hba_inst->satahba_attached == 1) 10574 break; 10575 } 10576 mutex_exit(&sata_mutex); 10577 if (sata_hba_inst == NULL) 10578 /* HBA not attached */ 10579 return; 10580 10581 ASSERT(sata_device != NULL); 10582 10583 /* 10584 * Validate address before - do not proceed with invalid address. 10585 */ 10586 saddr = &sata_device->satadev_addr; 10587 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 10588 return; 10589 if (saddr->qual == SATA_ADDR_PMPORT || 10590 saddr->qual == SATA_ADDR_DPMPORT) 10591 /* Port Multiplier not supported yet */ 10592 return; 10593 10594 cport = saddr->cport; 10595 pmport = saddr->pmport; 10596 10597 buf1[0] = buf2[0] = '\0'; 10598 10599 /* 10600 * Events refer to devices, ports and controllers - each has 10601 * unique address. Events for different addresses cannot be combined. 10602 */ 10603 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 10604 10605 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10606 10607 /* qualify this event(s) */ 10608 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 10609 /* Invalid event for the device port */ 10610 (void) sprintf(buf2, err_msg_evnt_1, 10611 event & SATA_EVNT_PORT_EVENTS); 10612 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10613 goto event_info; 10614 } 10615 if (saddr->qual == SATA_ADDR_CPORT) { 10616 /* Controller's device port event */ 10617 10618 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 10619 cport_event_flags |= 10620 event & SATA_EVNT_PORT_EVENTS; 10621 pstats = 10622 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 10623 cport_stats; 10624 } else { 10625 /* Port multiplier's device port event */ 10626 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 10627 pmport_event_flags |= 10628 event & SATA_EVNT_PORT_EVENTS; 10629 pstats = 10630 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 10631 pmport_stats; 10632 } 10633 10634 /* 10635 * Add to statistics and log the message. We have to do it 10636 * here rather than in the event daemon, because there may be 10637 * multiple events occuring before they are processed. 10638 */ 10639 linkevent = event & 10640 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 10641 if (linkevent) { 10642 if (linkevent == (SATA_EVNT_LINK_LOST | 10643 SATA_EVNT_LINK_ESTABLISHED)) { 10644 /* This is likely event combination */ 10645 (void) strlcat(buf1, "link lost/established, ", 10646 SATA_EVENT_MAX_MSG_LENGTH); 10647 10648 if (pstats->link_lost < 0xffffffffffffffffULL) 10649 pstats->link_lost++; 10650 if (pstats->link_established < 10651 0xffffffffffffffffULL) 10652 pstats->link_established++; 10653 linkevent = 0; 10654 } else if (linkevent & SATA_EVNT_LINK_LOST) { 10655 (void) strlcat(buf1, "link lost, ", 10656 SATA_EVENT_MAX_MSG_LENGTH); 10657 10658 if (pstats->link_lost < 0xffffffffffffffffULL) 10659 pstats->link_lost++; 10660 } else { 10661 (void) strlcat(buf1, "link established, ", 10662 SATA_EVENT_MAX_MSG_LENGTH); 10663 if (pstats->link_established < 10664 0xffffffffffffffffULL) 10665 pstats->link_established++; 10666 } 10667 } 10668 if (event & SATA_EVNT_DEVICE_ATTACHED) { 10669 (void) strlcat(buf1, "device attached, ", 10670 SATA_EVENT_MAX_MSG_LENGTH); 10671 if (pstats->device_attached < 0xffffffffffffffffULL) 10672 pstats->device_attached++; 10673 } 10674 if (event & SATA_EVNT_DEVICE_DETACHED) { 10675 (void) strlcat(buf1, "device detached, ", 10676 SATA_EVENT_MAX_MSG_LENGTH); 10677 if (pstats->device_detached < 0xffffffffffffffffULL) 10678 pstats->device_detached++; 10679 } 10680 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 10681 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 10682 "port %d power level changed", cport); 10683 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 10684 pstats->port_pwr_changed++; 10685 } 10686 10687 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 10688 /* There should be no other events for this address */ 10689 (void) sprintf(buf2, err_msg_evnt_1, 10690 event & ~SATA_EVNT_PORT_EVENTS); 10691 } 10692 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10693 10694 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 10695 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10696 10697 /* qualify this event */ 10698 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 10699 /* Invalid event for a device */ 10700 (void) sprintf(buf2, err_msg_evnt_2, 10701 event & SATA_EVNT_DEVICE_RESET); 10702 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10703 goto event_info; 10704 } 10705 /* drive event */ 10706 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 10707 if (sdinfo != NULL) { 10708 if (event & SATA_EVNT_DEVICE_RESET) { 10709 (void) strlcat(buf1, "device reset, ", 10710 SATA_EVENT_MAX_MSG_LENGTH); 10711 if (sdinfo->satadrv_stats.drive_reset < 10712 0xffffffffffffffffULL) 10713 sdinfo->satadrv_stats.drive_reset++; 10714 sdinfo->satadrv_event_flags |= 10715 SATA_EVNT_DEVICE_RESET; 10716 } 10717 } 10718 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 10719 /* Invalid event for a device */ 10720 (void) sprintf(buf2, err_msg_evnt_2, 10721 event & ~SATA_EVNT_DRIVE_EVENTS); 10722 } 10723 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 10724 } else { 10725 if (saddr->qual != SATA_ADDR_NULL) { 10726 /* Wrong address qualifier */ 10727 SATA_LOG_D((sata_hba_inst, CE_WARN, 10728 "sata_hba_event_notify: invalid address 0x%x", 10729 *(uint32_t *)saddr)); 10730 return; 10731 } 10732 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 10733 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 10734 /* Invalid event for the controller */ 10735 SATA_LOG_D((sata_hba_inst, CE_WARN, 10736 "sata_hba_event_notify: invalid event 0x%x for " 10737 "controller", 10738 event & SATA_EVNT_CONTROLLER_EVENTS)); 10739 return; 10740 } 10741 buf1[0] = '\0'; 10742 /* This may be a frequent and not interesting event */ 10743 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 10744 "controller power level changed\n", NULL); 10745 10746 mutex_enter(&sata_hba_inst->satahba_mutex); 10747 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 10748 0xffffffffffffffffULL) 10749 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 10750 10751 sata_hba_inst->satahba_event_flags |= 10752 SATA_EVNT_PWR_LEVEL_CHANGED; 10753 mutex_exit(&sata_hba_inst->satahba_mutex); 10754 } 10755 /* 10756 * If we got here, there is something to do with this HBA 10757 * instance. 10758 */ 10759 mutex_enter(&sata_hba_inst->satahba_mutex); 10760 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 10761 mutex_exit(&sata_hba_inst->satahba_mutex); 10762 mutex_enter(&sata_mutex); 10763 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 10764 mutex_exit(&sata_mutex); 10765 10766 /* Tickle event thread */ 10767 mutex_enter(&sata_event_mutex); 10768 if (sata_event_thread_active == 0) 10769 cv_signal(&sata_event_cv); 10770 mutex_exit(&sata_event_mutex); 10771 10772 event_info: 10773 if (buf1[0] != '\0') { 10774 lcp = strrchr(buf1, ','); 10775 if (lcp != NULL) 10776 *lcp = '\0'; 10777 } 10778 if (saddr->qual == SATA_ADDR_CPORT || 10779 saddr->qual == SATA_ADDR_DCPORT) { 10780 if (buf1[0] != '\0') { 10781 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 10782 cport, buf1); 10783 } 10784 if (buf2[0] != '\0') { 10785 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 10786 cport, buf2); 10787 } 10788 } else if (saddr->qual == SATA_ADDR_PMPORT || 10789 saddr->qual == SATA_ADDR_DPMPORT) { 10790 if (buf1[0] != '\0') { 10791 sata_log(sata_hba_inst, CE_NOTE, 10792 "port %d pmport %d: %s\n", cport, pmport, buf1); 10793 } 10794 if (buf2[0] != '\0') { 10795 sata_log(sata_hba_inst, CE_NOTE, 10796 "port %d pmport %d: %s\n", cport, pmport, buf2); 10797 } 10798 } 10799 } 10800 10801 10802 /* 10803 * Event processing thread. 10804 * Arg is a pointer to the sata_hba_list pointer. 10805 * It is not really needed, because sata_hba_list is global and static 10806 */ 10807 static void 10808 sata_event_daemon(void *arg) 10809 { 10810 #ifndef __lock_lint 10811 _NOTE(ARGUNUSED(arg)) 10812 #endif 10813 sata_hba_inst_t *sata_hba_inst; 10814 clock_t lbolt; 10815 10816 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 10817 "SATA event daemon started\n", NULL); 10818 loop: 10819 /* 10820 * Process events here. Walk through all registered HBAs 10821 */ 10822 mutex_enter(&sata_mutex); 10823 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 10824 sata_hba_inst = sata_hba_inst->satahba_next) { 10825 ASSERT(sata_hba_inst != NULL); 10826 mutex_enter(&sata_hba_inst->satahba_mutex); 10827 if (sata_hba_inst->satahba_attached != 1 || 10828 (sata_hba_inst->satahba_event_flags & 10829 SATA_EVNT_SKIP) != 0) { 10830 mutex_exit(&sata_hba_inst->satahba_mutex); 10831 continue; 10832 } 10833 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 10834 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 10835 mutex_exit(&sata_hba_inst->satahba_mutex); 10836 mutex_exit(&sata_mutex); 10837 /* Got the controller with pending event */ 10838 sata_process_controller_events(sata_hba_inst); 10839 /* 10840 * Since global mutex was released, there is a 10841 * possibility that HBA list has changed, so start 10842 * over from the top. Just processed controller 10843 * will be passed-over because of the SKIP flag. 10844 */ 10845 goto loop; 10846 } 10847 mutex_exit(&sata_hba_inst->satahba_mutex); 10848 } 10849 /* Clear SKIP flag in all controllers */ 10850 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 10851 sata_hba_inst = sata_hba_inst->satahba_next) { 10852 mutex_enter(&sata_hba_inst->satahba_mutex); 10853 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 10854 mutex_exit(&sata_hba_inst->satahba_mutex); 10855 } 10856 mutex_exit(&sata_mutex); 10857 10858 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 10859 "SATA EVENT DAEMON suspending itself", NULL); 10860 10861 #ifdef SATA_DEBUG 10862 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 10863 sata_log(sata_hba_inst, CE_WARN, 10864 "SATA EVENTS PROCESSING DISABLED\n"); 10865 thread_exit(); /* Daemon will not run again */ 10866 } 10867 #endif 10868 mutex_enter(&sata_event_mutex); 10869 sata_event_thread_active = 0; 10870 mutex_exit(&sata_event_mutex); 10871 /* 10872 * Go to sleep/suspend itself and wake up either because new event or 10873 * wait timeout. Exit if there is a termination request (driver 10874 * unload). 10875 */ 10876 do { 10877 lbolt = ddi_get_lbolt(); 10878 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 10879 mutex_enter(&sata_event_mutex); 10880 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 10881 10882 if (sata_event_thread_active != 0) { 10883 mutex_exit(&sata_event_mutex); 10884 continue; 10885 } 10886 10887 /* Check if it is time to go away */ 10888 if (sata_event_thread_terminate == 1) { 10889 /* 10890 * It is up to the thread setting above flag to make 10891 * sure that this thread is not killed prematurely. 10892 */ 10893 sata_event_thread_terminate = 0; 10894 sata_event_thread = NULL; 10895 mutex_exit(&sata_event_mutex); 10896 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 10897 "SATA_EVENT_DAEMON_TERMINATING", NULL); 10898 thread_exit(); { _NOTE(NOT_REACHED) } 10899 } 10900 mutex_exit(&sata_event_mutex); 10901 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 10902 10903 mutex_enter(&sata_event_mutex); 10904 sata_event_thread_active = 1; 10905 mutex_exit(&sata_event_mutex); 10906 10907 mutex_enter(&sata_mutex); 10908 sata_event_pending &= ~SATA_EVNT_MAIN; 10909 mutex_exit(&sata_mutex); 10910 10911 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 10912 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 10913 10914 goto loop; 10915 } 10916 10917 /* 10918 * Specific HBA instance event processing. 10919 * 10920 * NOTE: At the moment, device event processing is limited to hard disks 10921 * only. 10922 * cports only are supported - no pmports. 10923 */ 10924 static void 10925 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 10926 { 10927 int ncport; 10928 uint32_t event_flags; 10929 sata_address_t *saddr; 10930 10931 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 10932 "Processing controller %d event(s)", 10933 ddi_get_instance(SATA_DIP(sata_hba_inst))); 10934 10935 mutex_enter(&sata_hba_inst->satahba_mutex); 10936 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 10937 event_flags = sata_hba_inst->satahba_event_flags; 10938 mutex_exit(&sata_hba_inst->satahba_mutex); 10939 /* 10940 * Process controller power change first 10941 * HERE 10942 */ 10943 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 10944 sata_process_cntrl_pwr_level_change(sata_hba_inst); 10945 10946 /* 10947 * Search through ports/devices to identify affected port/device. 10948 * We may have to process events for more than one port/device. 10949 */ 10950 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 10951 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 10952 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 10953 cport_event_flags; 10954 /* Check if port was locked by IOCTL processing */ 10955 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 10956 /* 10957 * We ignore port events because port is busy 10958 * with AP control processing. Set again 10959 * controller and main event flag, so that 10960 * events may be processed by the next daemon 10961 * run. 10962 */ 10963 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 10964 mutex_enter(&sata_hba_inst->satahba_mutex); 10965 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 10966 mutex_exit(&sata_hba_inst->satahba_mutex); 10967 mutex_enter(&sata_mutex); 10968 sata_event_pending |= SATA_EVNT_MAIN; 10969 mutex_exit(&sata_mutex); 10970 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 10971 "Event processing postponed until " 10972 "AP control processing completes", 10973 NULL); 10974 /* Check other ports */ 10975 continue; 10976 } else { 10977 /* 10978 * Set BSY flag so that AP control would not 10979 * interfere with events processing for 10980 * this port. 10981 */ 10982 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 10983 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 10984 } 10985 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 10986 10987 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 10988 10989 if ((event_flags & 10990 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 10991 /* 10992 * Got port event. 10993 * We need some hierarchy of event processing as they 10994 * are affecting each other: 10995 * 1. port failed 10996 * 2. device detached/attached 10997 * 3. link events - link events may trigger device 10998 * detached or device attached events in some 10999 * circumstances. 11000 * 4. port power level changed 11001 */ 11002 if (event_flags & SATA_EVNT_PORT_FAILED) { 11003 sata_process_port_failed_event(sata_hba_inst, 11004 saddr); 11005 } 11006 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 11007 sata_process_device_detached(sata_hba_inst, 11008 saddr); 11009 } 11010 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 11011 sata_process_device_attached(sata_hba_inst, 11012 saddr); 11013 } 11014 if (event_flags & 11015 (SATA_EVNT_LINK_ESTABLISHED | 11016 SATA_EVNT_LINK_LOST)) { 11017 sata_process_port_link_events(sata_hba_inst, 11018 saddr); 11019 } 11020 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 11021 sata_process_port_pwr_change(sata_hba_inst, 11022 saddr); 11023 } 11024 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 11025 sata_process_target_node_cleanup( 11026 sata_hba_inst, saddr); 11027 } 11028 } 11029 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 11030 SATA_DTYPE_NONE) { 11031 /* May have device event */ 11032 sata_process_device_reset(sata_hba_inst, saddr); 11033 } 11034 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 11035 /* Release PORT_BUSY flag */ 11036 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 11037 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 11038 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 11039 11040 } /* End of loop through the controller SATA ports */ 11041 } 11042 11043 /* 11044 * Process HBA power level change reported by HBA driver. 11045 * Not implemented at this time - event is ignored. 11046 */ 11047 static void 11048 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 11049 { 11050 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11051 "Processing controller power level change", NULL); 11052 11053 /* Ignoring it for now */ 11054 mutex_enter(&sata_hba_inst->satahba_mutex); 11055 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 11056 mutex_exit(&sata_hba_inst->satahba_mutex); 11057 } 11058 11059 /* 11060 * Process port power level change reported by HBA driver. 11061 * Not implemented at this time - event is ignored. 11062 */ 11063 static void 11064 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 11065 sata_address_t *saddr) 11066 { 11067 sata_cport_info_t *cportinfo; 11068 11069 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11070 "Processing port power level change", NULL); 11071 11072 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11073 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11074 /* Reset event flag */ 11075 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 11076 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11077 } 11078 11079 /* 11080 * Process port failure reported by HBA driver. 11081 * cports support only - no pmports. 11082 */ 11083 static void 11084 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 11085 sata_address_t *saddr) 11086 { 11087 sata_cport_info_t *cportinfo; 11088 11089 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11090 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11091 /* Reset event flag first */ 11092 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 11093 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 11094 if ((cportinfo->cport_state & 11095 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 11096 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11097 cport_mutex); 11098 return; 11099 } 11100 /* Fail the port */ 11101 cportinfo->cport_state = SATA_PSTATE_FAILED; 11102 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11103 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 11104 } 11105 11106 /* 11107 * Device Reset Event processing. 11108 * The seqeunce is managed by 3 stage flags: 11109 * - reset event reported, 11110 * - reset event being processed, 11111 * - request to clear device reset state. 11112 */ 11113 static void 11114 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 11115 sata_address_t *saddr) 11116 { 11117 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 11118 sata_drive_info_t *sdinfo; 11119 sata_cport_info_t *cportinfo; 11120 sata_device_t sata_device; 11121 int rval; 11122 11123 /* We only care about host sata cport for now */ 11124 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11125 11126 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11127 11128 /* If the port is in SHUTDOWN or FAILED state, ignore reset event. */ 11129 if ((cportinfo->cport_state & 11130 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 11131 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11132 cport_mutex); 11133 return; 11134 } 11135 11136 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 11137 SATA_VALID_DEV_TYPE) == 0) { 11138 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11139 cport_mutex); 11140 return; 11141 } 11142 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 11143 if (sdinfo == NULL) { 11144 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11145 cport_mutex); 11146 return; 11147 } 11148 11149 if ((sdinfo->satadrv_event_flags & 11150 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 11151 /* Nothing to do */ 11152 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11153 cport_mutex); 11154 return; 11155 } 11156 #ifdef SATA_DEBUG 11157 if ((sdinfo->satadrv_event_flags & 11158 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 11159 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 11160 /* Something is weird - new device reset event */ 11161 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11162 "Overlapping device reset events!", NULL); 11163 } 11164 #endif 11165 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11166 "Processing port %d device reset", saddr->cport); 11167 11168 /* Clear event flag */ 11169 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 11170 11171 /* It seems that we always need to check the port state first */ 11172 sata_device.satadev_rev = SATA_DEVICE_REV; 11173 sata_device.satadev_addr = *saddr; 11174 /* 11175 * We have to exit mutex, because the HBA probe port function may 11176 * block on its own mutex. 11177 */ 11178 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11179 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11180 (SATA_DIP(sata_hba_inst), &sata_device); 11181 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11182 sata_update_port_info(sata_hba_inst, &sata_device); 11183 if (rval != SATA_SUCCESS) { 11184 /* Something went wrong? Fail the port */ 11185 cportinfo->cport_state = SATA_PSTATE_FAILED; 11186 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11187 cport_mutex); 11188 SATA_LOG_D((sata_hba_inst, CE_WARN, 11189 "SATA port %d probing failed", 11190 saddr->cport)); 11191 return; 11192 } 11193 if ((sata_device.satadev_scr.sstatus & 11194 SATA_PORT_DEVLINK_UP_MASK) != 11195 SATA_PORT_DEVLINK_UP || 11196 sata_device.satadev_type == SATA_DTYPE_NONE) { 11197 /* 11198 * No device to process, anymore. Some other event processing 11199 * would or have already performed port info cleanup. 11200 * To be safe (HBA may need it), request clearing device 11201 * reset condition. 11202 */ 11203 sdinfo->satadrv_event_flags = 0; 11204 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 11205 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11206 cport_mutex); 11207 return; 11208 } 11209 11210 /* Mark device reset processing as active */ 11211 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 11212 11213 old_sdinfo = *sdinfo; /* local copy of the drive info */ 11214 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11215 11216 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 11217 SATA_FAILURE) { 11218 /* 11219 * Restoring drive setting failed. 11220 * Probe the port first, to check if the port state has changed 11221 */ 11222 sata_device.satadev_rev = SATA_DEVICE_REV; 11223 sata_device.satadev_addr = *saddr; 11224 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 11225 /* probe port */ 11226 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11227 (SATA_DIP(sata_hba_inst), &sata_device); 11228 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11229 cport_mutex); 11230 if (rval == SATA_SUCCESS && 11231 (sata_device.satadev_state & 11232 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 11233 (sata_device.satadev_scr.sstatus & 11234 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 11235 (sata_device.satadev_type & SATA_DTYPE_ATADISK) != 0) { 11236 /* 11237 * We may retry this a bit later - in-process reset 11238 * condition is already set. 11239 */ 11240 if ((cportinfo->cport_dev_type & 11241 SATA_VALID_DEV_TYPE) != 0 && 11242 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11243 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11244 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11245 saddr->cport)->cport_mutex); 11246 mutex_enter(&sata_hba_inst->satahba_mutex); 11247 sata_hba_inst->satahba_event_flags |= 11248 SATA_EVNT_MAIN; 11249 mutex_exit(&sata_hba_inst->satahba_mutex); 11250 mutex_enter(&sata_mutex); 11251 sata_event_pending |= SATA_EVNT_MAIN; 11252 mutex_exit(&sata_mutex); 11253 return; 11254 } 11255 } else { 11256 /* 11257 * No point of retrying - some other event processing 11258 * would or already did port info cleanup. 11259 * To be safe (HBA may need it), 11260 * request clearing device reset condition. 11261 */ 11262 sdinfo->satadrv_event_flags = 0; 11263 sdinfo->satadrv_event_flags |= 11264 SATA_EVNT_CLEAR_DEVICE_RESET; 11265 } 11266 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11267 cport_mutex); 11268 return; 11269 } 11270 11271 /* 11272 * Raise the flag indicating that the next sata command could 11273 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 11274 * reset is reported. 11275 */ 11276 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11277 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 && 11278 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11279 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11280 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 11281 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 11282 } 11283 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11284 } 11285 11286 11287 /* 11288 * Port Link Events processing. 11289 * Every link established event may involve device reset (due to 11290 * COMRESET signal, equivalent of the hard reset) so arbitrarily 11291 * set device reset event for an attached device (if any). 11292 * If the port is in SHUTDOWN or FAILED state, ignore link events. 11293 * 11294 * The link established event processing varies, depending on the state 11295 * of the target node, HBA hotplugging capabilities, state of the port. 11296 * If the link is not active, the link established event is ignored. 11297 * If HBA cannot detect device attachment and there is no target node, 11298 * the link established event triggers device attach event processing. 11299 * Else, link established event triggers device reset event processing. 11300 * 11301 * The link lost event processing varies, depending on a HBA hotplugging 11302 * capability and the state of the port (link active or not active). 11303 * If the link is active, the lost link event is ignored. 11304 * If HBA cannot detect device removal, the lost link event triggers 11305 * device detached event processing after link lost timeout. 11306 * Else, the event is ignored. 11307 * 11308 * NOTE: Only cports are processed for now, i.e. no port multiplier ports 11309 */ 11310 static void 11311 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 11312 sata_address_t *saddr) 11313 { 11314 sata_device_t sata_device; 11315 sata_cport_info_t *cportinfo; 11316 sata_drive_info_t *sdinfo; 11317 uint32_t event_flags; 11318 int rval; 11319 11320 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11321 "Processing port %d link event(s)", saddr->cport); 11322 11323 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11324 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11325 event_flags = cportinfo->cport_event_flags; 11326 11327 /* Reset event flags first */ 11328 cportinfo->cport_event_flags &= 11329 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 11330 11331 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 11332 if ((cportinfo->cport_state & 11333 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 11334 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11335 cport_mutex); 11336 return; 11337 } 11338 11339 /* 11340 * For the sanity sake get current port state. 11341 * Set device address only. Other sata_device fields should be 11342 * set by HBA driver. 11343 */ 11344 sata_device.satadev_rev = SATA_DEVICE_REV; 11345 sata_device.satadev_addr = *saddr; 11346 /* 11347 * We have to exit mutex, because the HBA probe port function may 11348 * block on its own mutex. 11349 */ 11350 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11351 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11352 (SATA_DIP(sata_hba_inst), &sata_device); 11353 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11354 sata_update_port_info(sata_hba_inst, &sata_device); 11355 if (rval != SATA_SUCCESS) { 11356 /* Something went wrong? Fail the port */ 11357 cportinfo->cport_state = SATA_PSTATE_FAILED; 11358 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11359 cport_mutex); 11360 SATA_LOG_D((sata_hba_inst, CE_WARN, 11361 "SATA port %d probing failed", 11362 saddr->cport)); 11363 /* 11364 * We may want to release device info structure, but 11365 * it is not necessary. 11366 */ 11367 return; 11368 } else { 11369 /* port probed successfully */ 11370 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 11371 } 11372 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 11373 11374 if ((sata_device.satadev_scr.sstatus & 11375 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 11376 /* Ignore event */ 11377 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11378 "Ignoring port %d link established event - " 11379 "link down", 11380 saddr->cport); 11381 goto linklost; 11382 } 11383 11384 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11385 "Processing port %d link established event", 11386 saddr->cport); 11387 11388 /* 11389 * For the sanity sake check if a device is attached - check 11390 * return state of a port probing. 11391 */ 11392 if (sata_device.satadev_type != SATA_DTYPE_NONE && 11393 sata_device.satadev_type != SATA_DTYPE_PMULT) { 11394 /* 11395 * HBA port probe indicated that there is a device 11396 * attached. Check if the framework had device info 11397 * structure attached for this device. 11398 */ 11399 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11400 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 11401 NULL); 11402 11403 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11404 if ((sdinfo->satadrv_type & 11405 SATA_VALID_DEV_TYPE) != 0) { 11406 /* 11407 * Dev info structure is present. 11408 * If dev_type is set to known type in 11409 * the framework's drive info struct 11410 * then the device existed before and 11411 * the link was probably lost 11412 * momentarily - in such case 11413 * we may want to check device 11414 * identity. 11415 * Identity check is not supported now. 11416 * 11417 * Link established event 11418 * triggers device reset event. 11419 */ 11420 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 11421 satadrv_event_flags |= 11422 SATA_EVNT_DEVICE_RESET; 11423 } 11424 } else if (cportinfo->cport_dev_type == 11425 SATA_DTYPE_NONE) { 11426 /* 11427 * We got new device attached! If HBA does not 11428 * generate device attached events, trigger it 11429 * here. 11430 */ 11431 if (!(SATA_FEATURES(sata_hba_inst) & 11432 SATA_CTLF_HOTPLUG)) { 11433 cportinfo->cport_event_flags |= 11434 SATA_EVNT_DEVICE_ATTACHED; 11435 } 11436 } 11437 /* Reset link lost timeout */ 11438 cportinfo->cport_link_lost_time = 0; 11439 } 11440 } 11441 linklost: 11442 if (event_flags & SATA_EVNT_LINK_LOST) { 11443 if ((sata_device.satadev_scr.sstatus & 11444 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 11445 /* Ignore event */ 11446 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11447 "Ignoring port %d link lost event - link is up", 11448 saddr->cport); 11449 goto done; 11450 } 11451 #ifdef SATA_DEBUG 11452 if (cportinfo->cport_link_lost_time == 0) { 11453 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11454 "Processing port %d link lost event", 11455 saddr->cport); 11456 } 11457 #endif 11458 /* 11459 * When HBA cannot generate device attached/detached events, 11460 * we need to track link lost time and eventually generate 11461 * device detach event. 11462 */ 11463 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 11464 /* We are tracking link lost time */ 11465 if (cportinfo->cport_link_lost_time == 0) { 11466 /* save current time (lbolt value) */ 11467 cportinfo->cport_link_lost_time = 11468 ddi_get_lbolt(); 11469 /* just keep link lost event */ 11470 cportinfo->cport_event_flags |= 11471 SATA_EVNT_LINK_LOST; 11472 } else { 11473 clock_t cur_time = ddi_get_lbolt(); 11474 if ((cur_time - 11475 cportinfo->cport_link_lost_time) >= 11476 drv_usectohz( 11477 SATA_EVNT_LINK_LOST_TIMEOUT)) { 11478 /* trigger device detach event */ 11479 cportinfo->cport_event_flags |= 11480 SATA_EVNT_DEVICE_DETACHED; 11481 cportinfo->cport_link_lost_time = 0; 11482 SATADBG1(SATA_DBG_EVENTS, 11483 sata_hba_inst, 11484 "Triggering port %d " 11485 "device detached event", 11486 saddr->cport); 11487 } else { 11488 /* keep link lost event */ 11489 cportinfo->cport_event_flags |= 11490 SATA_EVNT_LINK_LOST; 11491 } 11492 } 11493 } 11494 /* 11495 * We could change port state to disable/delay access to 11496 * the attached device until the link is recovered. 11497 */ 11498 } 11499 done: 11500 event_flags = cportinfo->cport_event_flags; 11501 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11502 if (event_flags != 0) { 11503 mutex_enter(&sata_hba_inst->satahba_mutex); 11504 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 11505 mutex_exit(&sata_hba_inst->satahba_mutex); 11506 mutex_enter(&sata_mutex); 11507 sata_event_pending |= SATA_EVNT_MAIN; 11508 mutex_exit(&sata_mutex); 11509 } 11510 } 11511 11512 /* 11513 * Device Detached Event processing. 11514 * Port is probed to find if a device is really gone. If so, 11515 * the device info structure is detached from the SATA port info structure 11516 * and released. 11517 * Port status is updated. 11518 * 11519 * NOTE: Process cports event only, no port multiplier ports. 11520 */ 11521 static void 11522 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 11523 sata_address_t *saddr) 11524 { 11525 sata_cport_info_t *cportinfo; 11526 sata_drive_info_t *sdevinfo; 11527 sata_device_t sata_device; 11528 dev_info_t *tdip; 11529 int rval; 11530 11531 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11532 "Processing port %d device detached", saddr->cport); 11533 11534 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11535 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11536 /* Clear event flag */ 11537 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 11538 11539 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 11540 if ((cportinfo->cport_state & 11541 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 11542 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11543 cport_mutex); 11544 return; 11545 } 11546 /* For sanity, re-probe the port */ 11547 sata_device.satadev_rev = SATA_DEVICE_REV; 11548 sata_device.satadev_addr = *saddr; 11549 11550 /* 11551 * We have to exit mutex, because the HBA probe port function may 11552 * block on its own mutex. 11553 */ 11554 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11555 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11556 (SATA_DIP(sata_hba_inst), &sata_device); 11557 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11558 sata_update_port_info(sata_hba_inst, &sata_device); 11559 if (rval != SATA_SUCCESS) { 11560 /* Something went wrong? Fail the port */ 11561 cportinfo->cport_state = SATA_PSTATE_FAILED; 11562 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11563 cport_mutex); 11564 SATA_LOG_D((sata_hba_inst, CE_WARN, 11565 "SATA port %d probing failed", 11566 saddr->cport)); 11567 /* 11568 * We may want to release device info structure, but 11569 * it is not necessary. 11570 */ 11571 return; 11572 } else { 11573 /* port probed successfully */ 11574 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 11575 } 11576 /* 11577 * Check if a device is still attached. For sanity, check also 11578 * link status - if no link, there is no device. 11579 */ 11580 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 11581 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 11582 SATA_DTYPE_NONE) { 11583 /* 11584 * Device is still attached - ignore detach event. 11585 */ 11586 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11587 cport_mutex); 11588 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11589 "Ignoring detach - device still attached to port %d", 11590 sata_device.satadev_addr.cport); 11591 return; 11592 } 11593 /* 11594 * We need to detach and release device info structure here 11595 */ 11596 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11597 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11598 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11599 (void) kmem_free((void *)sdevinfo, 11600 sizeof (sata_drive_info_t)); 11601 } 11602 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11603 /* 11604 * Device cannot be reached anymore, even if the target node may be 11605 * still present. 11606 */ 11607 11608 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11609 sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d", 11610 sata_device.satadev_addr.cport); 11611 11612 /* 11613 * Try to offline a device and remove target node if it still exists 11614 */ 11615 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 11616 if (tdip != NULL) { 11617 /* 11618 * Target node exists. Unconfigure device then remove 11619 * the target node (one ndi operation). 11620 */ 11621 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11622 /* 11623 * PROBLEM - no device, but target node remained 11624 * This happens when the file was open or node was 11625 * waiting for resources. 11626 */ 11627 SATA_LOG_D((sata_hba_inst, CE_WARN, 11628 "sata_process_device_detached: " 11629 "Failed to remove target node for " 11630 "detached SATA device.")); 11631 /* 11632 * Set target node state to DEVI_DEVICE_REMOVED. 11633 * But re-check first that the node still exists. 11634 */ 11635 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 11636 saddr->cport); 11637 if (tdip != NULL) { 11638 sata_set_device_removed(tdip); 11639 /* 11640 * Instruct event daemon to retry the 11641 * cleanup later. 11642 */ 11643 sata_set_target_node_cleanup(sata_hba_inst, 11644 saddr->cport); 11645 } 11646 } 11647 } 11648 /* 11649 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11650 * with the hint: SE_HINT_REMOVE 11651 */ 11652 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 11653 } 11654 11655 11656 /* 11657 * Device Attached Event processing. 11658 * Port state is checked to verify that a device is really attached. If so, 11659 * the device info structure is created and attached to the SATA port info 11660 * structure. 11661 * 11662 * If attached device cannot be identified or set-up, the retry for the 11663 * attach processing is set-up. Subsequent daemon run would try again to 11664 * identify the device, until the time limit is reached 11665 * (SATA_DEV_IDENTIFY_TIMEOUT). 11666 * 11667 * This function cannot be called in interrupt context (it may sleep). 11668 * 11669 * NOTE: Process cports event only, no port multiplier ports. 11670 */ 11671 static void 11672 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 11673 sata_address_t *saddr) 11674 { 11675 sata_cport_info_t *cportinfo; 11676 sata_drive_info_t *sdevinfo; 11677 sata_device_t sata_device; 11678 dev_info_t *tdip; 11679 uint32_t event_flags; 11680 int rval; 11681 11682 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11683 "Processing port %d device attached", saddr->cport); 11684 11685 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11686 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11687 11688 /* Clear attach event flag first */ 11689 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 11690 11691 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 11692 if ((cportinfo->cport_state & 11693 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 11694 cportinfo->cport_dev_attach_time = 0; 11695 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11696 cport_mutex); 11697 return; 11698 } 11699 11700 /* 11701 * If the sata_drive_info structure is found attached to the port info, 11702 * despite the fact the device was removed and now it is re-attached, 11703 * the old drive info structure was not removed. 11704 * Arbitrarily release device info structure. 11705 */ 11706 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11707 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11708 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11709 (void) kmem_free((void *)sdevinfo, 11710 sizeof (sata_drive_info_t)); 11711 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11712 "Arbitrarily detaching old device info.", NULL); 11713 } 11714 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11715 11716 /* For sanity, re-probe the port */ 11717 sata_device.satadev_rev = SATA_DEVICE_REV; 11718 sata_device.satadev_addr = *saddr; 11719 11720 /* 11721 * We have to exit mutex, because the HBA probe port function may 11722 * block on its own mutex. 11723 */ 11724 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11725 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11726 (SATA_DIP(sata_hba_inst), &sata_device); 11727 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11728 sata_update_port_info(sata_hba_inst, &sata_device); 11729 if (rval != SATA_SUCCESS) { 11730 /* Something went wrong? Fail the port */ 11731 cportinfo->cport_state = SATA_PSTATE_FAILED; 11732 cportinfo->cport_dev_attach_time = 0; 11733 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11734 cport_mutex); 11735 SATA_LOG_D((sata_hba_inst, CE_WARN, 11736 "SATA port %d probing failed", 11737 saddr->cport)); 11738 return; 11739 } else { 11740 /* port probed successfully */ 11741 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 11742 } 11743 /* 11744 * Check if a device is still attached. For sanity, check also 11745 * link status - if no link, there is no device. 11746 */ 11747 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11748 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 11749 SATA_DTYPE_NONE) { 11750 /* 11751 * No device - ignore attach event. 11752 */ 11753 cportinfo->cport_dev_attach_time = 0; 11754 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11755 cport_mutex); 11756 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11757 "Ignoring attach - no device connected to port %d", 11758 sata_device.satadev_addr.cport); 11759 return; 11760 } 11761 11762 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11763 /* 11764 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11765 * with the hint: SE_HINT_INSERT 11766 */ 11767 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 11768 11769 /* 11770 * Port reprobing will take care of the creation of the device 11771 * info structure and determination of the device type. 11772 */ 11773 sata_device.satadev_addr = *saddr; 11774 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 11775 SATA_DEV_IDENTIFY_NORETRY); 11776 11777 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 11778 cport_mutex); 11779 if ((cportinfo->cport_state & SATA_STATE_READY) && 11780 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 11781 /* Some device is attached to the port */ 11782 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 11783 /* 11784 * A device was not successfully attached. 11785 * Track retry time for device identification. 11786 */ 11787 if (cportinfo->cport_dev_attach_time != 0) { 11788 clock_t cur_time = ddi_get_lbolt(); 11789 /* 11790 * If the retry time limit was not exceeded, 11791 * reinstate attach event. 11792 */ 11793 if ((cur_time - 11794 cportinfo->cport_dev_attach_time) < 11795 drv_usectohz( 11796 SATA_DEV_IDENTIFY_TIMEOUT)) { 11797 /* OK, restore attach event */ 11798 cportinfo->cport_event_flags |= 11799 SATA_EVNT_DEVICE_ATTACHED; 11800 } else { 11801 /* Timeout - cannot identify device */ 11802 cportinfo->cport_dev_attach_time = 0; 11803 sata_log(sata_hba_inst, 11804 CE_WARN, 11805 "Cannot identify SATA device " 11806 "at port %d - device will not be " 11807 "attached.", 11808 saddr->cport); 11809 } 11810 } else { 11811 /* 11812 * Start tracking time for device 11813 * identification. 11814 * Save current time (lbolt value). 11815 */ 11816 cportinfo->cport_dev_attach_time = 11817 ddi_get_lbolt(); 11818 /* Restore attach event */ 11819 cportinfo->cport_event_flags |= 11820 SATA_EVNT_DEVICE_ATTACHED; 11821 } 11822 } else { 11823 /* 11824 * If device was successfully attached, an explicit 11825 * 'configure' command will be needed to configure it. 11826 * Log the message indicating that a device 11827 * was attached. 11828 */ 11829 cportinfo->cport_dev_attach_time = 0; 11830 sata_log(sata_hba_inst, CE_WARN, 11831 "SATA device detected at port %d", saddr->cport); 11832 11833 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11834 sata_drive_info_t new_sdinfo; 11835 11836 /* Log device info data */ 11837 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 11838 cportinfo)); 11839 sata_show_drive_info(sata_hba_inst, 11840 &new_sdinfo); 11841 } 11842 11843 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11844 saddr->cport)->cport_mutex); 11845 11846 /* 11847 * Make sure that there is no target node for that 11848 * device. If so, release it. It should not happen, 11849 * unless we had problem removing the node when 11850 * device was detached. 11851 */ 11852 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 11853 saddr->cport); 11854 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11855 saddr->cport)->cport_mutex); 11856 if (tdip != NULL) { 11857 11858 #ifdef SATA_DEBUG 11859 if ((cportinfo->cport_event_flags & 11860 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 11861 sata_log(sata_hba_inst, CE_WARN, 11862 "sata_process_device_attached: " 11863 "old device target node exists!"); 11864 #endif 11865 /* 11866 * target node exists - try to unconfigure 11867 * device and remove the node. 11868 */ 11869 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11870 saddr->cport)->cport_mutex); 11871 rval = ndi_devi_offline(tdip, 11872 NDI_DEVI_REMOVE); 11873 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11874 saddr->cport)->cport_mutex); 11875 11876 if (rval == NDI_SUCCESS) { 11877 cportinfo->cport_event_flags &= 11878 ~SATA_EVNT_TARGET_NODE_CLEANUP; 11879 cportinfo->cport_tgtnode_clean = B_TRUE; 11880 } else { 11881 /* 11882 * PROBLEM - the target node remained 11883 * and it belongs to a previously 11884 * attached device. 11885 * This happens when the file was open 11886 * or the node was waiting for 11887 * resources at the time the 11888 * associated device was removed. 11889 * Instruct event daemon to retry the 11890 * cleanup later. 11891 */ 11892 sata_log(sata_hba_inst, 11893 CE_WARN, 11894 "Application(s) accessing " 11895 "previously attached SATA " 11896 "device have to release " 11897 "it before newly inserted " 11898 "device can be made accessible.", 11899 saddr->cport); 11900 cportinfo->cport_event_flags |= 11901 SATA_EVNT_TARGET_NODE_CLEANUP; 11902 cportinfo->cport_tgtnode_clean = 11903 B_FALSE; 11904 } 11905 } 11906 11907 } 11908 } else { 11909 cportinfo->cport_dev_attach_time = 0; 11910 } 11911 11912 event_flags = cportinfo->cport_event_flags; 11913 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 11914 if (event_flags != 0) { 11915 mutex_enter(&sata_hba_inst->satahba_mutex); 11916 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 11917 mutex_exit(&sata_hba_inst->satahba_mutex); 11918 mutex_enter(&sata_mutex); 11919 sata_event_pending |= SATA_EVNT_MAIN; 11920 mutex_exit(&sata_mutex); 11921 } 11922 } 11923 11924 11925 /* 11926 * Device Target Node Cleanup Event processing. 11927 * If the target node associated with a sata port device is in 11928 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 11929 * If the target node cannot be removed, the event flag is left intact, 11930 * so that event daemon may re-run this function later. 11931 * 11932 * This function cannot be called in interrupt context (it may sleep). 11933 * 11934 * NOTE: Processes cport events only, not port multiplier ports. 11935 */ 11936 static void 11937 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 11938 sata_address_t *saddr) 11939 { 11940 sata_cport_info_t *cportinfo; 11941 dev_info_t *tdip; 11942 11943 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11944 "Processing port %d device target node cleanup", saddr->cport); 11945 11946 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 11947 11948 /* 11949 * Check if there is target node for that device and it is in the 11950 * DEVI_DEVICE_REMOVED state. If so, release it. 11951 */ 11952 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 11953 if (tdip != NULL) { 11954 /* 11955 * target node exists - check if it is target node of 11956 * a removed device. 11957 */ 11958 if (sata_check_device_removed(tdip) == B_TRUE) { 11959 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 11960 "sata_process_target_node_cleanup: " 11961 "old device target node exists!", NULL); 11962 /* 11963 * Unconfigure and remove the target node 11964 */ 11965 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 11966 NDI_SUCCESS) { 11967 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11968 saddr->cport)->cport_mutex); 11969 cportinfo->cport_event_flags &= 11970 ~SATA_EVNT_TARGET_NODE_CLEANUP; 11971 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11972 saddr->cport)->cport_mutex); 11973 return; 11974 } 11975 /* 11976 * Event daemon will retry the cleanup later. 11977 */ 11978 mutex_enter(&sata_hba_inst->satahba_mutex); 11979 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 11980 mutex_exit(&sata_hba_inst->satahba_mutex); 11981 mutex_enter(&sata_mutex); 11982 sata_event_pending |= SATA_EVNT_MAIN; 11983 mutex_exit(&sata_mutex); 11984 } 11985 } else { 11986 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11987 saddr->cport)->cport_mutex); 11988 cportinfo->cport_event_flags &= 11989 ~SATA_EVNT_TARGET_NODE_CLEANUP; 11990 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11991 saddr->cport)->cport_mutex); 11992 } 11993 } 11994 11995 11996 11997 /* 11998 * sata_set_drive_features function compares current device features setting 11999 * with the saved device features settings and, if there is a difference, 12000 * it restores device features setting to the previously saved state. 12001 * Device Identify data has to be current. 12002 * At the moment only read ahead and write cache settings are considered. 12003 * 12004 * This function cannot be called in the interrupt context (it may sleep). 12005 * 12006 * The input argument sdinfo should point to the drive info structure 12007 * to be updated after features are set. 12008 * 12009 * Returns TRUE if successful or there was nothing to do. 12010 * Returns FALSE if device features could not be set . 12011 * 12012 * Note: This function may fail the port, making it inaccessible. 12013 * Explicit port disconnect/connect or physical device 12014 * detach/attach is required to re-evaluate it's state afterwards 12015 */ 12016 12017 static int 12018 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 12019 sata_drive_info_t *sdinfo, int restore) 12020 { 12021 int rval = SATA_SUCCESS; 12022 sata_drive_info_t new_sdinfo; 12023 char *finfo = "sata_set_drive_features: cannot"; 12024 char *finfox; 12025 int cache_op; 12026 12027 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12028 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 12029 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 12030 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12031 /* 12032 * Cannot get device identification - retry later 12033 */ 12034 SATA_LOG_D((sata_hba_inst, CE_WARN, 12035 "%s fetch device identify data\n", finfo)); 12036 return (SATA_FAILURE); 12037 } 12038 /* Arbitrarily set UDMA mode */ 12039 if (sata_set_udma_mode(sata_hba_inst, &new_sdinfo) != SATA_SUCCESS) { 12040 SATA_LOG_D((sata_hba_inst, CE_WARN, 12041 "%s set UDMA mode\n", finfo)); 12042 return (SATA_FAILURE); 12043 } 12044 12045 if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) && 12046 !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 12047 /* None of the features is supported - do nothing */ 12048 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12049 "settable features not supported\n", NULL); 12050 return (SATA_SUCCESS); 12051 } 12052 12053 if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12054 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 12055 ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12056 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12057 /* Nothing to do */ 12058 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12059 "no device features to set\n", NULL); 12060 return (SATA_SUCCESS); 12061 } 12062 12063 finfox = (restore != 0) ? " restore device features" : 12064 " initialize device features\n"; 12065 12066 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12067 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) { 12068 if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) { 12069 /* Enable read ahead / read cache */ 12070 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 12071 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12072 "enabling read cache\n", NULL); 12073 } else { 12074 /* Disable read ahead / read cache */ 12075 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 12076 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12077 "disabling read cache\n", NULL); 12078 } 12079 12080 /* Try to set read cache mode */ 12081 if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12082 cache_op) != SATA_SUCCESS) { 12083 /* Pkt execution failed */ 12084 rval = SATA_FAILURE; 12085 } 12086 } 12087 12088 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12089 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12090 if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) { 12091 /* Enable write cache */ 12092 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 12093 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12094 "enabling write cache\n", NULL); 12095 } else { 12096 /* Disable write cache */ 12097 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 12098 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12099 "disabling write cache\n", NULL); 12100 } 12101 /* Try to set write cache mode */ 12102 if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12103 cache_op) != SATA_SUCCESS) { 12104 /* Pkt execution failed */ 12105 rval = SATA_FAILURE; 12106 } 12107 } 12108 12109 if (rval == SATA_FAILURE) 12110 SATA_LOG_D((sata_hba_inst, CE_WARN, 12111 "%s %s", finfo, finfox)); 12112 12113 /* 12114 * We need to fetch Device Identify data again 12115 */ 12116 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12117 /* 12118 * Cannot get device identification - retry later 12119 */ 12120 SATA_LOG_D((sata_hba_inst, CE_WARN, 12121 "%s cannot re-fetch device identify data\n")); 12122 rval = SATA_FAILURE; 12123 } 12124 /* Copy device sata info. */ 12125 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 12126 12127 return (rval); 12128 } 12129 12130 12131 /* 12132 * 12133 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 12134 * unable to determine. 12135 * 12136 * Cannot be called in an interrupt context. 12137 * 12138 * Called by sata_build_lsense_page_2f() 12139 */ 12140 12141 static int 12142 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 12143 sata_drive_info_t *sdinfo) 12144 { 12145 sata_pkt_t *spkt; 12146 sata_cmd_t *scmd; 12147 sata_pkt_txlate_t *spx; 12148 int rval; 12149 12150 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12151 spx->txlt_sata_hba_inst = sata_hba_inst; 12152 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12153 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12154 if (spkt == NULL) { 12155 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12156 return (-1); 12157 } 12158 /* address is needed now */ 12159 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12160 12161 12162 /* Fill sata_pkt */ 12163 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12164 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12165 /* Synchronous mode, no callback */ 12166 spkt->satapkt_comp = NULL; 12167 /* Timeout 30s */ 12168 spkt->satapkt_time = sata_default_pkt_time; 12169 12170 scmd = &spkt->satapkt_cmd; 12171 scmd->satacmd_flags.sata_special_regs = B_TRUE; 12172 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12173 12174 /* Set up which registers need to be returned */ 12175 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 12176 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 12177 12178 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 12179 scmd->satacmd_addr_type = 0; /* N/A */ 12180 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12181 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12182 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12183 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12184 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 12185 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12186 scmd->satacmd_cmd_reg = SATAC_SMART; 12187 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12188 sdinfo->satadrv_addr.cport))); 12189 12190 12191 /* Send pkt to SATA HBA driver */ 12192 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12193 SATA_TRAN_ACCEPTED || 12194 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12195 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12196 sdinfo->satadrv_addr.cport))); 12197 /* 12198 * Whoops, no SMART RETURN STATUS 12199 */ 12200 rval = -1; 12201 } else { 12202 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12203 sdinfo->satadrv_addr.cport))); 12204 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 12205 rval = -1; 12206 goto fail; 12207 } 12208 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 12209 rval = -1; 12210 goto fail; 12211 } 12212 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 12213 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 12214 rval = 0; 12215 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 12216 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 12217 rval = 1; 12218 else { 12219 rval = -1; 12220 goto fail; 12221 } 12222 } 12223 fail: 12224 /* Free allocated resources */ 12225 sata_pkt_free(spx); 12226 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12227 12228 return (rval); 12229 } 12230 12231 /* 12232 * 12233 * Returns 0 if succeeded, -1 otherwise 12234 * 12235 * Cannot be called in an interrupt context. 12236 * 12237 */ 12238 static int 12239 sata_fetch_smart_data( 12240 sata_hba_inst_t *sata_hba_inst, 12241 sata_drive_info_t *sdinfo, 12242 struct smart_data *smart_data) 12243 { 12244 sata_pkt_t *spkt; 12245 sata_cmd_t *scmd; 12246 sata_pkt_txlate_t *spx; 12247 int rval; 12248 12249 #if ! defined(lint) 12250 ASSERT(sizeof (struct smart_data) == 512); 12251 #endif 12252 12253 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12254 spx->txlt_sata_hba_inst = sata_hba_inst; 12255 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12256 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12257 if (spkt == NULL) { 12258 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12259 return (-1); 12260 } 12261 /* address is needed now */ 12262 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12263 12264 12265 /* Fill sata_pkt */ 12266 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12267 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12268 /* Synchronous mode, no callback */ 12269 spkt->satapkt_comp = NULL; 12270 /* Timeout 30s */ 12271 spkt->satapkt_time = sata_default_pkt_time; 12272 12273 scmd = &spkt->satapkt_cmd; 12274 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12275 12276 /* 12277 * Allocate buffer for SMART data 12278 */ 12279 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12280 sizeof (struct smart_data)); 12281 if (scmd->satacmd_bp == NULL) { 12282 sata_pkt_free(spx); 12283 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12284 SATA_LOG_D((sata_hba_inst, CE_WARN, 12285 "sata_fetch_smart_data: " 12286 "cannot allocate buffer")); 12287 return (-1); 12288 } 12289 12290 12291 /* Build SMART_READ_DATA cmd in the sata_pkt */ 12292 scmd->satacmd_addr_type = 0; /* N/A */ 12293 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12294 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12295 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12296 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12297 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 12298 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12299 scmd->satacmd_cmd_reg = SATAC_SMART; 12300 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12301 sdinfo->satadrv_addr.cport))); 12302 12303 /* Send pkt to SATA HBA driver */ 12304 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12305 SATA_TRAN_ACCEPTED || 12306 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12307 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12308 sdinfo->satadrv_addr.cport))); 12309 /* 12310 * Whoops, no SMART DATA available 12311 */ 12312 rval = -1; 12313 goto fail; 12314 } else { 12315 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12316 sdinfo->satadrv_addr.cport))); 12317 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12318 DDI_DMA_SYNC_FORKERNEL); 12319 ASSERT(rval == DDI_SUCCESS); 12320 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 12321 sizeof (struct smart_data)); 12322 } 12323 12324 fail: 12325 /* Free allocated resources */ 12326 sata_free_local_buffer(spx); 12327 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12328 sata_pkt_free(spx); 12329 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12330 12331 return (rval); 12332 } 12333 12334 /* 12335 * Used by LOG SENSE page 0x10 12336 * 12337 * return 0 for success, -1 otherwise 12338 * 12339 */ 12340 static int 12341 sata_ext_smart_selftest_read_log( 12342 sata_hba_inst_t *sata_hba_inst, 12343 sata_drive_info_t *sdinfo, 12344 struct smart_ext_selftest_log *ext_selftest_log, 12345 uint16_t block_num) 12346 { 12347 sata_pkt_txlate_t *spx; 12348 sata_pkt_t *spkt; 12349 sata_cmd_t *scmd; 12350 int rval; 12351 12352 #if ! defined(lint) 12353 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 12354 #endif 12355 12356 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12357 spx->txlt_sata_hba_inst = sata_hba_inst; 12358 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12359 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12360 if (spkt == NULL) { 12361 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12362 return (-1); 12363 } 12364 /* address is needed now */ 12365 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12366 12367 12368 /* Fill sata_pkt */ 12369 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12370 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12371 /* Synchronous mode, no callback */ 12372 spkt->satapkt_comp = NULL; 12373 /* Timeout 30s */ 12374 spkt->satapkt_time = sata_default_pkt_time; 12375 12376 scmd = &spkt->satapkt_cmd; 12377 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12378 12379 /* 12380 * Allocate buffer for SMART extended self-test log 12381 */ 12382 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12383 sizeof (struct smart_ext_selftest_log)); 12384 if (scmd->satacmd_bp == NULL) { 12385 sata_pkt_free(spx); 12386 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12387 SATA_LOG_D((sata_hba_inst, CE_WARN, 12388 "sata_ext_smart_selftest_log: " 12389 "cannot allocate buffer")); 12390 return (-1); 12391 } 12392 12393 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 12394 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 12395 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 12396 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 12397 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 12398 scmd->satacmd_lba_low_msb = 0; 12399 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 12400 scmd->satacmd_lba_mid_msb = block_num >> 8; 12401 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12402 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 12403 12404 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12405 sdinfo->satadrv_addr.cport))); 12406 12407 /* Send pkt to SATA HBA driver */ 12408 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12409 SATA_TRAN_ACCEPTED || 12410 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12411 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12412 sdinfo->satadrv_addr.cport))); 12413 12414 /* 12415 * Whoops, no SMART selftest log info available 12416 */ 12417 rval = -1; 12418 goto fail; 12419 } else { 12420 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12421 sdinfo->satadrv_addr.cport))); 12422 12423 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12424 DDI_DMA_SYNC_FORKERNEL); 12425 ASSERT(rval == DDI_SUCCESS); 12426 bcopy(scmd->satacmd_bp->b_un.b_addr, 12427 (uint8_t *)ext_selftest_log, 12428 sizeof (struct smart_ext_selftest_log)); 12429 rval = 0; 12430 } 12431 12432 fail: 12433 /* Free allocated resources */ 12434 sata_free_local_buffer(spx); 12435 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12436 sata_pkt_free(spx); 12437 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12438 12439 return (rval); 12440 } 12441 12442 /* 12443 * Returns 0 for success, -1 otherwise 12444 * 12445 * SMART self-test log data is returned in buffer pointed to by selftest_log 12446 */ 12447 static int 12448 sata_smart_selftest_log( 12449 sata_hba_inst_t *sata_hba_inst, 12450 sata_drive_info_t *sdinfo, 12451 struct smart_selftest_log *selftest_log) 12452 { 12453 sata_pkt_t *spkt; 12454 sata_cmd_t *scmd; 12455 sata_pkt_txlate_t *spx; 12456 int rval; 12457 12458 #if ! defined(lint) 12459 ASSERT(sizeof (struct smart_selftest_log) == 512); 12460 #endif 12461 12462 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12463 spx->txlt_sata_hba_inst = sata_hba_inst; 12464 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12465 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12466 if (spkt == NULL) { 12467 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12468 return (-1); 12469 } 12470 /* address is needed now */ 12471 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12472 12473 12474 /* Fill sata_pkt */ 12475 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12476 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12477 /* Synchronous mode, no callback */ 12478 spkt->satapkt_comp = NULL; 12479 /* Timeout 30s */ 12480 spkt->satapkt_time = sata_default_pkt_time; 12481 12482 scmd = &spkt->satapkt_cmd; 12483 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12484 12485 /* 12486 * Allocate buffer for SMART SELFTEST LOG 12487 */ 12488 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12489 sizeof (struct smart_selftest_log)); 12490 if (scmd->satacmd_bp == NULL) { 12491 sata_pkt_free(spx); 12492 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12493 SATA_LOG_D((sata_hba_inst, CE_WARN, 12494 "sata_smart_selftest_log: " 12495 "cannot allocate buffer")); 12496 return (-1); 12497 } 12498 12499 /* Build SMART_READ_LOG cmd in the sata_pkt */ 12500 scmd->satacmd_addr_type = 0; /* N/A */ 12501 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 12502 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 12503 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12504 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12505 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 12506 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12507 scmd->satacmd_cmd_reg = SATAC_SMART; 12508 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12509 sdinfo->satadrv_addr.cport))); 12510 12511 /* Send pkt to SATA HBA driver */ 12512 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12513 SATA_TRAN_ACCEPTED || 12514 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12515 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12516 sdinfo->satadrv_addr.cport))); 12517 /* 12518 * Whoops, no SMART DATA available 12519 */ 12520 rval = -1; 12521 goto fail; 12522 } else { 12523 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12524 sdinfo->satadrv_addr.cport))); 12525 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12526 DDI_DMA_SYNC_FORKERNEL); 12527 ASSERT(rval == DDI_SUCCESS); 12528 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 12529 sizeof (struct smart_selftest_log)); 12530 rval = 0; 12531 } 12532 12533 fail: 12534 /* Free allocated resources */ 12535 sata_free_local_buffer(spx); 12536 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12537 sata_pkt_free(spx); 12538 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12539 12540 return (rval); 12541 } 12542 12543 12544 /* 12545 * Returns 0 for success, -1 otherwise 12546 * 12547 * SMART READ LOG data is returned in buffer pointed to by smart_log 12548 */ 12549 static int 12550 sata_smart_read_log( 12551 sata_hba_inst_t *sata_hba_inst, 12552 sata_drive_info_t *sdinfo, 12553 uint8_t *smart_log, /* where the data should be returned */ 12554 uint8_t which_log, /* which log should be returned */ 12555 uint8_t log_size) /* # of 512 bytes in log */ 12556 { 12557 sata_pkt_t *spkt; 12558 sata_cmd_t *scmd; 12559 sata_pkt_txlate_t *spx; 12560 int rval; 12561 12562 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12563 spx->txlt_sata_hba_inst = sata_hba_inst; 12564 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12565 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12566 if (spkt == NULL) { 12567 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12568 return (-1); 12569 } 12570 /* address is needed now */ 12571 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12572 12573 12574 /* Fill sata_pkt */ 12575 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12576 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12577 /* Synchronous mode, no callback */ 12578 spkt->satapkt_comp = NULL; 12579 /* Timeout 30s */ 12580 spkt->satapkt_time = sata_default_pkt_time; 12581 12582 scmd = &spkt->satapkt_cmd; 12583 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12584 12585 /* 12586 * Allocate buffer for SMART READ LOG 12587 */ 12588 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 12589 if (scmd->satacmd_bp == NULL) { 12590 sata_pkt_free(spx); 12591 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12592 SATA_LOG_D((sata_hba_inst, CE_WARN, 12593 "sata_smart_read_log: " "cannot allocate buffer")); 12594 return (-1); 12595 } 12596 12597 /* Build SMART_READ_LOG cmd in the sata_pkt */ 12598 scmd->satacmd_addr_type = 0; /* N/A */ 12599 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 12600 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 12601 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12602 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12603 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 12604 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12605 scmd->satacmd_cmd_reg = SATAC_SMART; 12606 12607 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12608 sdinfo->satadrv_addr.cport))); 12609 12610 /* Send pkt to SATA HBA driver */ 12611 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12612 SATA_TRAN_ACCEPTED || 12613 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12614 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12615 sdinfo->satadrv_addr.cport))); 12616 12617 /* 12618 * Whoops, no SMART DATA available 12619 */ 12620 rval = -1; 12621 goto fail; 12622 } else { 12623 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12624 sdinfo->satadrv_addr.cport))); 12625 12626 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12627 DDI_DMA_SYNC_FORKERNEL); 12628 ASSERT(rval == DDI_SUCCESS); 12629 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 12630 rval = 0; 12631 } 12632 12633 fail: 12634 /* Free allocated resources */ 12635 sata_free_local_buffer(spx); 12636 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12637 sata_pkt_free(spx); 12638 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12639 12640 return (rval); 12641 } 12642 12643 /* 12644 * Used by LOG SENSE page 0x10 12645 * 12646 * return 0 for success, -1 otherwise 12647 * 12648 */ 12649 static int 12650 sata_read_log_ext_directory( 12651 sata_hba_inst_t *sata_hba_inst, 12652 sata_drive_info_t *sdinfo, 12653 struct read_log_ext_directory *logdir) 12654 { 12655 sata_pkt_txlate_t *spx; 12656 sata_pkt_t *spkt; 12657 sata_cmd_t *scmd; 12658 int rval; 12659 12660 #if ! defined(lint) 12661 ASSERT(sizeof (struct read_log_ext_directory) == 512); 12662 #endif 12663 12664 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12665 spx->txlt_sata_hba_inst = sata_hba_inst; 12666 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12667 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12668 if (spkt == NULL) { 12669 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12670 return (-1); 12671 } 12672 12673 /* Fill sata_pkt */ 12674 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12675 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12676 /* Synchronous mode, no callback */ 12677 spkt->satapkt_comp = NULL; 12678 /* Timeout 30s */ 12679 spkt->satapkt_time = sata_default_pkt_time; 12680 12681 scmd = &spkt->satapkt_cmd; 12682 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12683 12684 /* 12685 * Allocate buffer for SMART READ LOG EXTENDED command 12686 */ 12687 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 12688 sizeof (struct read_log_ext_directory)); 12689 if (scmd->satacmd_bp == NULL) { 12690 sata_pkt_free(spx); 12691 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12692 SATA_LOG_D((sata_hba_inst, CE_WARN, 12693 "sata_read_log_ext_directory: " 12694 "cannot allocate buffer")); 12695 return (-1); 12696 } 12697 12698 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 12699 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 12700 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 12701 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 12702 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 12703 scmd->satacmd_lba_low_msb = 0; 12704 scmd->satacmd_lba_mid_lsb = 0; 12705 scmd->satacmd_lba_mid_msb = 0; 12706 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12707 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 12708 12709 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12710 sdinfo->satadrv_addr.cport))); 12711 12712 /* Send pkt to SATA HBA driver */ 12713 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 12714 SATA_TRAN_ACCEPTED || 12715 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12716 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12717 sdinfo->satadrv_addr.cport))); 12718 /* 12719 * Whoops, no SMART selftest log info available 12720 */ 12721 rval = -1; 12722 goto fail; 12723 } else { 12724 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12725 sdinfo->satadrv_addr.cport))); 12726 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12727 DDI_DMA_SYNC_FORKERNEL); 12728 ASSERT(rval == DDI_SUCCESS); 12729 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 12730 sizeof (struct read_log_ext_directory)); 12731 rval = 0; 12732 } 12733 12734 fail: 12735 /* Free allocated resources */ 12736 sata_free_local_buffer(spx); 12737 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12738 sata_pkt_free(spx); 12739 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12740 12741 return (rval); 12742 } 12743 12744 static void 12745 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 12746 int hint) 12747 { 12748 char ap[MAXPATHLEN]; 12749 nvlist_t *ev_attr_list = NULL; 12750 int err; 12751 12752 /* Allocate and build sysevent attribute list */ 12753 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 12754 if (err != 0) { 12755 SATA_LOG_D((sata_hba_inst, CE_WARN, 12756 "sata_gen_sysevent: " 12757 "cannot allocate memory for sysevent attributes\n")); 12758 return; 12759 } 12760 /* Add hint attribute */ 12761 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 12762 if (err != 0) { 12763 SATA_LOG_D((sata_hba_inst, CE_WARN, 12764 "sata_gen_sysevent: " 12765 "failed to add DR_HINT attr for sysevent")); 12766 nvlist_free(ev_attr_list); 12767 return; 12768 } 12769 /* 12770 * Add AP attribute. 12771 * Get controller pathname and convert it into AP pathname by adding 12772 * a target number. 12773 */ 12774 (void) snprintf(ap, MAXPATHLEN, "/devices"); 12775 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 12776 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 12777 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 12778 12779 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 12780 if (err != 0) { 12781 SATA_LOG_D((sata_hba_inst, CE_WARN, 12782 "sata_gen_sysevent: " 12783 "failed to add DR_AP_ID attr for sysevent")); 12784 nvlist_free(ev_attr_list); 12785 return; 12786 } 12787 12788 /* Generate/log sysevent */ 12789 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 12790 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 12791 if (err != DDI_SUCCESS) { 12792 SATA_LOG_D((sata_hba_inst, CE_WARN, 12793 "sata_gen_sysevent: " 12794 "cannot log sysevent, err code %x\n", err)); 12795 } 12796 12797 nvlist_free(ev_attr_list); 12798 } 12799 12800 /* 12801 * sata_xlate_errors() is used to translate (S)ATA error 12802 * information to SCSI information returned in the SCSI 12803 * packet. 12804 */ 12805 static void 12806 sata_xlate_errors(sata_pkt_txlate_t *spx) 12807 { 12808 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 12809 struct scsi_extended_sense *sense; 12810 12811 scsipkt->pkt_reason = CMD_INCOMPLETE; 12812 *scsipkt->pkt_scbp = STATUS_CHECK; 12813 sense = sata_arq_sense(spx); 12814 12815 switch (spx->txlt_sata_pkt->satapkt_reason) { 12816 case SATA_PKT_PORT_ERROR: 12817 /* 12818 * We have no device data. Assume no data transfered. 12819 */ 12820 sense->es_key = KEY_HARDWARE_ERROR; 12821 break; 12822 12823 case SATA_PKT_DEV_ERROR: 12824 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 12825 SATA_STATUS_ERR) { 12826 /* 12827 * determine dev error reason from error 12828 * reg content 12829 */ 12830 sata_decode_device_error(spx, sense); 12831 break; 12832 } 12833 /* No extended sense key - no info available */ 12834 break; 12835 12836 case SATA_PKT_TIMEOUT: 12837 /* 12838 * scsipkt->pkt_reason = CMD_TIMEOUT; This causes problems. 12839 */ 12840 scsipkt->pkt_reason = CMD_INCOMPLETE; 12841 /* No extended sense key */ 12842 break; 12843 12844 case SATA_PKT_ABORTED: 12845 scsipkt->pkt_reason = CMD_ABORTED; 12846 /* No extended sense key */ 12847 break; 12848 12849 case SATA_PKT_RESET: 12850 /* 12851 * pkt aborted either by an explicit reset request from 12852 * a host, or due to error recovery 12853 */ 12854 scsipkt->pkt_reason = CMD_RESET; 12855 break; 12856 12857 default: 12858 scsipkt->pkt_reason = CMD_TRAN_ERR; 12859 break; 12860 } 12861 } 12862 12863 12864 12865 /* 12866 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 12867 */ 12868 static void 12869 sata_set_device_removed(dev_info_t *tdip) 12870 { 12871 int circ; 12872 12873 ASSERT(tdip != NULL); 12874 12875 ndi_devi_enter(tdip, &circ); 12876 mutex_enter(&DEVI(tdip)->devi_lock); 12877 DEVI_SET_DEVICE_REMOVED(tdip); 12878 mutex_exit(&DEVI(tdip)->devi_lock); 12879 ndi_devi_exit(tdip, circ); 12880 } 12881 12882 12883 /* 12884 * Set internal event instructing event daemon to try 12885 * to perform the target node cleanup. 12886 */ 12887 static void 12888 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, int cport) 12889 { 12890 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12891 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) |= 12892 SATA_EVNT_TARGET_NODE_CLEANUP; 12893 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean = B_FALSE; 12894 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12895 mutex_enter(&sata_hba_inst->satahba_mutex); 12896 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 12897 mutex_exit(&sata_hba_inst->satahba_mutex); 12898 mutex_enter(&sata_mutex); 12899 sata_event_pending |= SATA_EVNT_MAIN; 12900 mutex_exit(&sata_mutex); 12901 } 12902 12903 12904 /* 12905 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 12906 * i.e. check if the target node state indicates that it belongs to a removed 12907 * device. 12908 * 12909 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 12910 * B_FALSE otherwise. 12911 * 12912 * NOTE: No port multiplier support. 12913 */ 12914 static boolean_t 12915 sata_check_device_removed(dev_info_t *tdip) 12916 { 12917 ASSERT(tdip != NULL); 12918 12919 if (DEVI_IS_DEVICE_REMOVED(tdip)) 12920 return (B_TRUE); 12921 else 12922 return (B_FALSE); 12923 } 12924