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