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