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