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