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