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