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