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