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