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