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