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