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