xref: /titanic_51/usr/src/uts/common/io/sata/impl/sata.c (revision eadbb3a2376b1b0c99cf575947d1f40b6c0886fe)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 /*
29  * SATA Framework
30  * Generic SATA Host Adapter Implementation
31  */
32 
33 #include <sys/conf.h>
34 #include <sys/file.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/modctl.h>
38 #include <sys/cmn_err.h>
39 #include <sys/errno.h>
40 #include <sys/thread.h>
41 #include <sys/kstat.h>
42 #include <sys/note.h>
43 #include <sys/sysevent.h>
44 #include <sys/sysevent/eventdefs.h>
45 #include <sys/sysevent/dr.h>
46 #include <sys/taskq.h>
47 #include <sys/disp.h>
48 
49 #include <sys/sata/impl/sata.h>
50 #include <sys/sata/sata_hba.h>
51 #include <sys/sata/sata_defs.h>
52 #include <sys/sata/sata_cfgadm.h>
53 
54 /* Debug flags - defined in sata.h */
55 int	sata_debug_flags = 0;
56 int	sata_msg = 0;
57 
58 /*
59  * Flags enabling selected SATA HBA framework functionality
60  */
61 #define	SATA_ENABLE_QUEUING		1
62 #define	SATA_ENABLE_NCQ			2
63 #define	SATA_ENABLE_PROCESS_EVENTS	4
64 int sata_func_enable =
65 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
66 
67 /*
68  * Global variable setting default maximum queue depth (NCQ or TCQ)
69  * Note:minimum queue depth is 1
70  */
71 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
72 
73 /*
74  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
75  * initialization, using value from sata_max_queue_depth
76  * It is adjusted to minimum supported by the controller and by the device,
77  * if queueing is enabled.
78  */
79 static	int sata_current_max_qdepth;
80 
81 /*
82  * Global variable determining the default behavior after device hotpluggin.
83  * If non-zero, the hotplugged device is onlined (if possible) without explicit
84  * IOCTL request (AP_CONFIGURE).
85  * If zero, hotplugged device is identified, but not onlined.
86  * Enabling (AP_CONNECT) device port with an attached device does not result
87  * in device onlining regardless of the flag setting
88  */
89 int sata_auto_online = 0;
90 
91 #ifdef SATA_DEBUG
92 
93 #define	SATA_LOG_D(args)	sata_log args
94 uint64_t mbuf_count = 0;
95 uint64_t mbuffail_count = 0;
96 
97 sata_atapi_cmd_t sata_atapi_trace[64];
98 uint32_t sata_atapi_trace_index = 0;
99 int sata_atapi_trace_save = 1;
100 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
101 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
102     sata_save_atapi_trace(spx, count);
103 
104 #else
105 #define	SATA_LOG_D(args)	sata_trace_log args
106 #define	SATAATAPITRACE(spx, count)
107 #endif
108 
109 #if 0
110 static void
111 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
112 #endif
113 
114 #ifdef SATA_INJECT_FAULTS
115 
116 #define		SATA_INJECT_PKT_FAULT	1
117 uint32_t	sata_inject_fault = 0;
118 
119 uint32_t	sata_inject_fault_count = 0;
120 uint32_t	sata_inject_fault_pause_count = 0;
121 uint32_t	sata_fault_type = 0;
122 uint32_t	sata_fault_cmd = 0;
123 dev_info_t	*sata_fault_ctrl = NULL;
124 sata_device_t	sata_fault_device;
125 
126 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
127 
128 #endif
129 
130 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
131 
132 static char sata_rev_tag[] = {"1.43"};
133 
134 /*
135  * SATA cb_ops functions
136  */
137 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
138 static 	int sata_hba_close(dev_t, int, int, cred_t *);
139 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
140 
141 /*
142  * SCSA required entry points
143  */
144 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
145     scsi_hba_tran_t *, struct scsi_device *);
146 static	int sata_scsi_tgt_probe(struct scsi_device *,
147     int (*callback)(void));
148 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
149     scsi_hba_tran_t *, struct scsi_device *);
150 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
151 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
152 static 	int sata_scsi_reset(struct scsi_address *, int);
153 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
154 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
155 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
156     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
157     caddr_t);
158 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
159 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
160 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
161 
162 /*
163  * SATA HBA interface functions are defined in sata_hba.h header file
164  */
165 
166 /* Event processing functions */
167 static	void sata_event_daemon(void *);
168 static	void sata_event_thread_control(int);
169 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
170 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
171 static	void sata_process_port_failed_event(sata_hba_inst_t *,
172     sata_address_t *);
173 static	void sata_process_port_link_events(sata_hba_inst_t *,
174     sata_address_t *);
175 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
176 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
177 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
178 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
179 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
180     sata_address_t *);
181 static	void sata_process_device_autoonline(sata_hba_inst_t *,
182     sata_address_t *saddr);
183 
184 /*
185  * Local translation functions
186  */
187 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
188 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
189 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
190 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
191 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
192 static 	int sata_txlt_read(sata_pkt_txlate_t *);
193 static 	int sata_txlt_write(sata_pkt_txlate_t *);
194 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
195 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
196 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
197 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
198 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
199 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
200 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
201 
202 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
203 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
204 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
205 static 	void sata_txlt_rw_completion(sata_pkt_t *);
206 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
207 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
208 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
209 static 	struct scsi_extended_sense *sata_immediate_error_response(
210     sata_pkt_txlate_t *, int);
211 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
212 
213 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
214 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
215 
216 /*
217  * Local functions for ioctl
218  */
219 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
220 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
221     devctl_ap_state_t *);
222 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
223 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
224 static	dev_info_t *sata_devt_to_devinfo(dev_t);
225 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
230 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
232 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
233 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
234 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
235 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
240     sata_ioctl_data_t *, int mode);
241 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
242     sata_ioctl_data_t *, int mode);
243 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
244     sata_device_t *, sata_ioctl_data_t *, int mode);
245 
246 /*
247  * Local functions
248  */
249 static 	void sata_remove_hba_instance(dev_info_t *);
250 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
251 static 	void sata_probe_ports(sata_hba_inst_t *);
252 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
253 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
254     int pmport);
255 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
256     sata_address_t *);
257 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
258     struct scsi_address *, sata_device_t *);
259 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
260 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
261 static	void sata_pkt_free(sata_pkt_txlate_t *);
262 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
263     caddr_t, ddi_dma_attr_t *);
264 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
265 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
266 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
267     sata_device_t *);
268 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
269 static	void sata_reidentify_device(sata_pkt_txlate_t *);
270 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
271 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
272 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
273 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
274     ddi_dma_attr_t *);
275 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
276     sata_drive_info_t *);
277 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
278 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
279 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
280 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
281 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
282 static	int sata_set_drive_features(sata_hba_inst_t *,
283     sata_drive_info_t *, int flag);
284 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
285 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
286 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
287     uint8_t *);
288 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
289     struct scsi_inquiry *);
290 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
291 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
292 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
293 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
294 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
295     struct mode_cache_scsi3 *, int, int *, int *, int *);
296 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
297     struct mode_info_excpt_page *, int, int *, int *, int *);
298 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
299 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
300     struct mode_acoustic_management *, int, int *, int *, int *);
301 
302 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
303 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
304     sata_hba_inst_t *);
305 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
306     sata_hba_inst_t *);
307 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
308     sata_hba_inst_t *);
309 static	void sata_save_drive_settings(sata_drive_info_t *);
310 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
311 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
312 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
313 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
314     sata_drive_info_t *);
315 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
316     struct smart_data *);
317 static	int sata_smart_selftest_log(sata_hba_inst_t *,
318     sata_drive_info_t *,
319     struct smart_selftest_log *);
320 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
321     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
322 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
323     uint8_t *, uint8_t, uint8_t);
324 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
325     struct read_log_ext_directory *);
326 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
327 static	void sata_xlate_errors(sata_pkt_txlate_t *);
328 static	void sata_decode_device_error(sata_pkt_txlate_t *,
329     struct scsi_extended_sense *);
330 static	void sata_set_device_removed(dev_info_t *);
331 static	boolean_t sata_check_device_removed(dev_info_t *);
332 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
333 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
334     sata_drive_info_t *);
335 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
336     sata_drive_info_t *);
337 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
338 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
339 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
340 static  int sata_check_modser(char *, int);
341 
342 
343 
344 /*
345  * SATA Framework will ignore SATA HBA driver cb_ops structure and
346  * register following one with SCSA framework.
347  * Open & close are provided, so scsi framework will not use its own
348  */
349 static struct cb_ops sata_cb_ops = {
350 	sata_hba_open,			/* open */
351 	sata_hba_close,			/* close */
352 	nodev,				/* strategy */
353 	nodev,				/* print */
354 	nodev,				/* dump */
355 	nodev,				/* read */
356 	nodev,				/* write */
357 	sata_hba_ioctl,			/* ioctl */
358 	nodev,				/* devmap */
359 	nodev,				/* mmap */
360 	nodev,				/* segmap */
361 	nochpoll,			/* chpoll */
362 	ddi_prop_op,			/* cb_prop_op */
363 	0,				/* streamtab */
364 	D_NEW | D_MP,			/* cb_flag */
365 	CB_REV,				/* rev */
366 	nodev,				/* aread */
367 	nodev				/* awrite */
368 };
369 
370 
371 extern struct mod_ops mod_miscops;
372 extern uchar_t	scsi_cdb_size[];
373 
374 static struct modlmisc modlmisc = {
375 	&mod_miscops,			/* Type of module */
376 	"SATA Module"			/* module name */
377 };
378 
379 
380 static struct modlinkage modlinkage = {
381 	MODREV_1,
382 	(void *)&modlmisc,
383 	NULL
384 };
385 
386 /*
387  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
388  * i.e. when scsi_pkt has not timeout specified.
389  */
390 static int sata_default_pkt_time = 60;	/* 60 seconds */
391 
392 /*
393  * Intermediate buffer device access attributes - they are required,
394  * but not necessarily used.
395  */
396 static ddi_device_acc_attr_t sata_acc_attr = {
397 	DDI_DEVICE_ATTR_V0,
398 	DDI_STRUCTURE_LE_ACC,
399 	DDI_STRICTORDER_ACC
400 };
401 
402 
403 /*
404  * Mutexes protecting structures in multithreaded operations.
405  * Because events are relatively rare, a single global mutex protecting
406  * data structures should be sufficient. To increase performance, add
407  * separate mutex per each sata port and use global mutex only to protect
408  * common data structures.
409  */
410 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
411 static	kmutex_t sata_log_mutex;	/* protects log */
412 
413 static 	char sata_log_buf[256];
414 
415 /*
416  * sata trace debug
417  */
418 static	sata_trace_rbuf_t *sata_debug_rbuf;
419 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
420 static	void sata_trace_dmsg_free(void);
421 static	void sata_trace_rbuf_alloc(void);
422 static	void sata_trace_rbuf_free(void);
423 
424 int	dmsg_ring_size = DMSG_RING_SIZE;
425 
426 /* Default write cache setting for SATA hard disks */
427 int	sata_write_cache = 1;		/* enabled */
428 
429 /* Default write cache setting for SATA ATAPI CD/DVD */
430 int	sata_atapicdvd_write_cache = 1; /* enabled */
431 
432 /* Default write cache setting for SATA ATAPI tape */
433 int	sata_atapitape_write_cache = 1; /* enabled */
434 
435 /* Default write cache setting for SATA ATAPI disk */
436 int	sata_atapidisk_write_cache = 1;	/* enabled */
437 
438 /*
439  * Linked list of HBA instances
440  */
441 static 	sata_hba_inst_t *sata_hba_list = NULL;
442 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
443 /*
444  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
445  * structure and in sata soft state.
446  */
447 
448 /*
449  * Event daemon related variables
450  */
451 static 	kmutex_t sata_event_mutex;
452 static 	kcondvar_t sata_event_cv;
453 static 	kthread_t *sata_event_thread = NULL;
454 static 	int sata_event_thread_terminate = 0;
455 static 	int sata_event_pending = 0;
456 static 	int sata_event_thread_active = 0;
457 extern 	pri_t minclsyspri;
458 
459 /*
460  * NCQ error recovery command
461  */
462 static const sata_cmd_t sata_rle_cmd = {
463 	SATA_CMD_REV,
464 	NULL,
465 	{
466 		SATA_DIR_READ
467 	},
468 	ATA_ADDR_LBA48,
469 	0,
470 	0,
471 	0,
472 	0,
473 	0,
474 	1,
475 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
476 	0,
477 	0,
478 	0,
479 	SATAC_READ_LOG_EXT,
480 	0,
481 	0,
482 	0,
483 };
484 
485 /*
486  * ATAPI error recovery CDB
487  */
488 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
489 	SCMD_REQUEST_SENSE,
490 	0,			/* Only fixed RQ format is supported */
491 	0,
492 	0,
493 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
494 	0
495 };
496 
497 
498 /* Warlock directives */
499 
500 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
502 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
503 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
504 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
505 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
506 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
507 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
508 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
509 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
510 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
511 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
512 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
513 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
514     sata_hba_inst::satahba_scsi_tran))
515 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
516 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
517 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
518 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
519 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
520     sata_hba_inst::satahba_event_flags))
521 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
522     sata_cport_info::cport_devp))
523 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
524 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
525 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
526     sata_cport_info::cport_dev_type))
527 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
528 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
529     sata_cport_info::cport_state))
530 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
531 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
532     sata_pmport_info::pmport_state))
533 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
534 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
535 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
536 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
537 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
538 #ifdef SATA_DEBUG
539 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
540 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
541 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
542 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
543 #endif
544 
545 /* End of warlock directives */
546 
547 /* ************** loadable module configuration functions ************** */
548 
549 int
550 _init()
551 {
552 	int rval;
553 
554 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
555 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
556 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
557 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
558 	sata_trace_rbuf_alloc();
559 	if ((rval = mod_install(&modlinkage)) != 0) {
560 #ifdef SATA_DEBUG
561 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
562 #endif
563 		sata_trace_rbuf_free();
564 		mutex_destroy(&sata_log_mutex);
565 		cv_destroy(&sata_event_cv);
566 		mutex_destroy(&sata_event_mutex);
567 		mutex_destroy(&sata_mutex);
568 	}
569 	return (rval);
570 }
571 
572 int
573 _fini()
574 {
575 	int rval;
576 
577 	if ((rval = mod_remove(&modlinkage)) != 0)
578 		return (rval);
579 
580 	sata_trace_rbuf_free();
581 	mutex_destroy(&sata_log_mutex);
582 	cv_destroy(&sata_event_cv);
583 	mutex_destroy(&sata_event_mutex);
584 	mutex_destroy(&sata_mutex);
585 	return (rval);
586 }
587 
588 int
589 _info(struct modinfo *modinfop)
590 {
591 	return (mod_info(&modlinkage, modinfop));
592 }
593 
594 
595 
596 /* ********************* SATA HBA entry points ********************* */
597 
598 
599 /*
600  * Called by SATA HBA from _init().
601  * Registers HBA driver instance/sata framework pair with scsi framework, by
602  * calling scsi_hba_init().
603  *
604  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
605  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
606  * cb_ops pointer in SATA HBA driver dev_ops structure.
607  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
608  *
609  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
610  * driver.
611  */
612 int
613 sata_hba_init(struct modlinkage *modlp)
614 {
615 	int rval;
616 	struct dev_ops *hba_ops;
617 
618 	SATADBG1(SATA_DBG_HBA_IF, NULL,
619 	    "sata_hba_init: name %s \n",
620 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
621 	/*
622 	 * Fill-up cb_ops and dev_ops when necessary
623 	 */
624 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
625 	/*
626 	 * Provide pointer to SATA dev_ops
627 	 */
628 	hba_ops->devo_cb_ops = &sata_cb_ops;
629 
630 	/*
631 	 * Register SATA HBA with SCSI framework
632 	 */
633 	if ((rval = scsi_hba_init(modlp)) != 0) {
634 		SATADBG1(SATA_DBG_HBA_IF, NULL,
635 		    "sata_hba_init: scsi hba init failed\n", NULL);
636 		return (rval);
637 	}
638 
639 	return (0);
640 }
641 
642 
643 /* HBA attach stages */
644 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
645 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
646 #define	HBA_ATTACH_STAGE_SETUP		4
647 #define	HBA_ATTACH_STAGE_LINKED		8
648 
649 
650 /*
651  *
652  * Called from SATA HBA driver's attach routine to attach an instance of
653  * the HBA.
654  *
655  * For DDI_ATTACH command:
656  * sata_hba_inst structure is allocated here and initialized with pointers to
657  * SATA framework implementation of required scsi tran functions.
658  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
659  * to the soft structure (sata_hba_inst) allocated by SATA framework for
660  * SATA HBA instance related data.
661  * The scsi_tran's tran_hba_private field is used by SATA framework to
662  * store a pointer to per-HBA-instance of sata_hba_inst structure.
663  * The sata_hba_inst structure is cross-linked to scsi tran structure.
664  * Among other info, a pointer to sata_hba_tran structure is stored in
665  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
666  * linked together into the list, pointed to by sata_hba_list.
667  * On the first HBA instance attach the sata event thread is initialized.
668  * Attachment points are created for all SATA ports of the HBA being attached.
669  * All HBA instance's SATA ports are probed and type of plugged devices is
670  * determined. For each device of a supported type, a target node is created.
671  *
672  * DDI_SUCCESS is returned when attachment process is successful,
673  * DDI_FAILURE is returned otherwise.
674  *
675  * For DDI_RESUME command:
676  * Not implemented at this time (postponed until phase 2 of the development).
677  */
678 int
679 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
680     ddi_attach_cmd_t cmd)
681 {
682 	sata_hba_inst_t	*sata_hba_inst;
683 	scsi_hba_tran_t *scsi_tran = NULL;
684 	int hba_attach_state = 0;
685 	char taskq_name[MAXPATHLEN];
686 
687 	SATADBG3(SATA_DBG_HBA_IF, NULL,
688 	    "sata_hba_attach: node %s (%s%d)\n",
689 	    ddi_node_name(dip), ddi_driver_name(dip),
690 	    ddi_get_instance(dip));
691 
692 	if (cmd == DDI_RESUME) {
693 		/*
694 		 * Postponed until phase 2 of the development
695 		 */
696 		return (DDI_FAILURE);
697 	}
698 
699 	if (cmd != DDI_ATTACH) {
700 		return (DDI_FAILURE);
701 	}
702 
703 	/* cmd == DDI_ATTACH */
704 
705 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
706 		SATA_LOG_D((NULL, CE_WARN,
707 		    "sata_hba_attach: invalid sata_hba_tran"));
708 		return (DDI_FAILURE);
709 	}
710 	/*
711 	 * Allocate and initialize SCSI tran structure.
712 	 * SATA copy of tran_bus_config is provided to create port nodes.
713 	 */
714 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
715 	if (scsi_tran == NULL)
716 		return (DDI_FAILURE);
717 	/*
718 	 * Allocate soft structure for SATA HBA instance.
719 	 * There is a separate softstate for each HBA instance.
720 	 */
721 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
722 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
723 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
724 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
725 
726 	/*
727 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
728 	 * soft structure allocated by SATA framework for
729 	 * SATA HBA instance related data.
730 	 */
731 	scsi_tran->tran_hba_private	= sata_hba_inst;
732 	scsi_tran->tran_tgt_private	= NULL;
733 
734 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
735 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
736 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
737 
738 	scsi_tran->tran_start		= sata_scsi_start;
739 	scsi_tran->tran_reset		= sata_scsi_reset;
740 	scsi_tran->tran_abort		= sata_scsi_abort;
741 	scsi_tran->tran_getcap		= sata_scsi_getcap;
742 	scsi_tran->tran_setcap		= sata_scsi_setcap;
743 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
744 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
745 
746 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
747 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
748 
749 	scsi_tran->tran_reset_notify	= NULL;
750 	scsi_tran->tran_get_bus_addr	= NULL;
751 	scsi_tran->tran_quiesce		= NULL;
752 	scsi_tran->tran_unquiesce	= NULL;
753 	scsi_tran->tran_bus_reset	= NULL;
754 
755 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
756 	    scsi_tran, 0) != DDI_SUCCESS) {
757 #ifdef SATA_DEBUG
758 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
759 		    ddi_driver_name(dip), ddi_get_instance(dip));
760 #endif
761 		goto fail;
762 	}
763 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
764 
765 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
766 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
767 		    "sata", 1) != DDI_PROP_SUCCESS) {
768 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
769 			    "failed to create hba sata prop"));
770 			goto fail;
771 		}
772 	}
773 
774 	/*
775 	 * Save pointers in hba instance soft state.
776 	 */
777 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
778 	sata_hba_inst->satahba_tran = sata_tran;
779 	sata_hba_inst->satahba_dip = dip;
780 
781 	/*
782 	 * Create a task queue to handle emulated commands completion
783 	 * Use node name, dash, instance number as the queue name.
784 	 */
785 	taskq_name[0] = '\0';
786 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
787 	    sizeof (taskq_name));
788 	(void) snprintf(taskq_name + strlen(taskq_name),
789 	    sizeof (taskq_name) - strlen(taskq_name),
790 	    "-%d", DEVI(dip)->devi_instance);
791 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
792 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
793 	    TASKQ_DYNAMIC);
794 
795 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
796 
797 	/*
798 	 * Create events thread if not created yet.
799 	 */
800 	sata_event_thread_control(1);
801 
802 	/*
803 	 * Link this hba instance into the list.
804 	 */
805 	mutex_enter(&sata_mutex);
806 
807 	if (sata_hba_list == NULL) {
808 		/*
809 		 * The first instance of HBA is attached.
810 		 * Set current/active default maximum NCQ/TCQ queue depth for
811 		 * all SATA devices. It is done here and now, to eliminate the
812 		 * possibility of the dynamic, programatic modification of the
813 		 * queue depth via global (and public) sata_max_queue_depth
814 		 * variable (this would require special handling in HBA drivers)
815 		 */
816 		sata_current_max_qdepth = sata_max_queue_depth;
817 		if (sata_current_max_qdepth > 32)
818 			sata_current_max_qdepth = 32;
819 		else if (sata_current_max_qdepth < 1)
820 			sata_current_max_qdepth = 1;
821 	}
822 
823 	sata_hba_inst->satahba_next = NULL;
824 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
825 	if (sata_hba_list == NULL) {
826 		sata_hba_list = sata_hba_inst;
827 	}
828 	if (sata_hba_list_tail != NULL) {
829 		sata_hba_list_tail->satahba_next = sata_hba_inst;
830 	}
831 	sata_hba_list_tail = sata_hba_inst;
832 	mutex_exit(&sata_mutex);
833 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
834 
835 	/*
836 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
837 	 * SATA HBA driver should not use its own open/close entry points.
838 	 *
839 	 * Make sure that instance number doesn't overflow
840 	 * when forming minor numbers.
841 	 */
842 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
843 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
844 	    INST2DEVCTL(ddi_get_instance(dip)),
845 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
846 #ifdef SATA_DEBUG
847 		cmn_err(CE_WARN, "sata_hba_attach: "
848 		    "cannot create devctl minor node");
849 #endif
850 		goto fail;
851 	}
852 
853 
854 	/*
855 	 * Set-up kstats here, if necessary.
856 	 * (postponed until future phase of the development).
857 	 */
858 
859 	/*
860 	 * Indicate that HBA is attached. This will enable events processing
861 	 * for this HBA.
862 	 */
863 	sata_hba_inst->satahba_attached = 1;
864 	/*
865 	 * Probe controller ports. This operation will describe a current
866 	 * controller/port/multipliers/device configuration and will create
867 	 * attachment points.
868 	 * We may end-up with just a controller with no devices attached.
869 	 * For the ports with a supported device attached, device target nodes
870 	 * are created and devices are initialized.
871 	 */
872 	sata_probe_ports(sata_hba_inst);
873 
874 	return (DDI_SUCCESS);
875 
876 fail:
877 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
878 		(void) sata_remove_hba_instance(dip);
879 		if (sata_hba_list == NULL)
880 			sata_event_thread_control(0);
881 	}
882 
883 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
884 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
885 		taskq_destroy(sata_hba_inst->satahba_taskq);
886 	}
887 
888 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
889 		(void) scsi_hba_detach(dip);
890 
891 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
892 		mutex_destroy(&sata_hba_inst->satahba_mutex);
893 		kmem_free((void *)sata_hba_inst,
894 		    sizeof (struct sata_hba_inst));
895 		scsi_hba_tran_free(scsi_tran);
896 	}
897 
898 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
899 	    ddi_driver_name(dip), ddi_get_instance(dip));
900 
901 	return (DDI_FAILURE);
902 }
903 
904 
905 /*
906  * Called by SATA HBA from to detach an instance of the driver.
907  *
908  * For DDI_DETACH command:
909  * Free local structures allocated for SATA HBA instance during
910  * sata_hba_attach processing.
911  *
912  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
913  *
914  * For DDI_SUSPEND command:
915  * Not implemented at this time (postponed until phase 2 of the development)
916  * Returnd DDI_SUCCESS.
917  *
918  * When the last HBA instance is detached, the event daemon is terminated.
919  *
920  * NOTE: cport support only, no port multiplier support.
921  */
922 int
923 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
924 {
925 	dev_info_t	*tdip;
926 	sata_hba_inst_t	*sata_hba_inst;
927 	scsi_hba_tran_t *scsi_hba_tran;
928 	sata_cport_info_t *cportinfo;
929 	sata_drive_info_t *sdinfo;
930 	int ncport;
931 
932 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
933 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
934 
935 	switch (cmd) {
936 	case DDI_DETACH:
937 
938 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
939 			return (DDI_FAILURE);
940 
941 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
942 		if (sata_hba_inst == NULL)
943 			return (DDI_FAILURE);
944 
945 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
946 			sata_hba_inst->satahba_attached = 1;
947 			return (DDI_FAILURE);
948 		}
949 
950 		/*
951 		 * Free all target nodes - at this point
952 		 * devices should be at least offlined
953 		 * otherwise scsi_hba_detach() should not be called.
954 		 */
955 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
956 		    ncport++) {
957 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
958 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
959 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
960 				if (sdinfo != NULL) {
961 					tdip = sata_get_target_dip(dip,
962 					    ncport);
963 					if (tdip != NULL) {
964 						if (ndi_devi_offline(tdip,
965 						    NDI_DEVI_REMOVE) !=
966 						    NDI_SUCCESS) {
967 							SATA_LOG_D((
968 							    sata_hba_inst,
969 							    CE_WARN,
970 							    "sata_hba_detach: "
971 							    "Target node not "
972 							    "removed !"));
973 							return (DDI_FAILURE);
974 						}
975 					}
976 				}
977 			}
978 		}
979 		/*
980 		 * Disable sata event daemon processing for this HBA
981 		 */
982 		sata_hba_inst->satahba_attached = 0;
983 
984 		/*
985 		 * Remove event daemon thread, if it is last HBA instance.
986 		 */
987 
988 		mutex_enter(&sata_mutex);
989 		if (sata_hba_list->satahba_next == NULL) {
990 			mutex_exit(&sata_mutex);
991 			sata_event_thread_control(0);
992 			mutex_enter(&sata_mutex);
993 		}
994 		mutex_exit(&sata_mutex);
995 
996 		/* Remove this HBA instance from the HBA list */
997 		sata_remove_hba_instance(dip);
998 
999 		/*
1000 		 * At this point there should be no target nodes attached.
1001 		 * Detach and destroy device and port info structures.
1002 		 */
1003 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1004 		    ncport++) {
1005 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1006 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1007 				sdinfo =
1008 				    cportinfo->cport_devp.cport_sata_drive;
1009 				if (sdinfo != NULL) {
1010 					/* Release device structure */
1011 					kmem_free(sdinfo,
1012 					    sizeof (sata_drive_info_t));
1013 				}
1014 				/* Release cport info */
1015 				mutex_destroy(&cportinfo->cport_mutex);
1016 				kmem_free(cportinfo,
1017 				    sizeof (sata_cport_info_t));
1018 			}
1019 		}
1020 
1021 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1022 
1023 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1024 
1025 		taskq_destroy(sata_hba_inst->satahba_taskq);
1026 
1027 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1028 		kmem_free((void *)sata_hba_inst,
1029 		    sizeof (struct sata_hba_inst));
1030 
1031 		return (DDI_SUCCESS);
1032 
1033 	case DDI_SUSPEND:
1034 		/*
1035 		 * Postponed until phase 2
1036 		 */
1037 		return (DDI_FAILURE);
1038 
1039 	default:
1040 		return (DDI_FAILURE);
1041 	}
1042 }
1043 
1044 
1045 /*
1046  * Called by an HBA drive from _fini() routine.
1047  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1048  */
1049 void
1050 sata_hba_fini(struct modlinkage *modlp)
1051 {
1052 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1053 	    "sata_hba_fini: name %s\n",
1054 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1055 
1056 	scsi_hba_fini(modlp);
1057 }
1058 
1059 
1060 /*
1061  * Default open and close routine for sata_hba framework.
1062  *
1063  */
1064 /*
1065  * Open devctl node.
1066  *
1067  * Returns:
1068  * 0 if node was open successfully, error code otherwise.
1069  *
1070  *
1071  */
1072 
1073 static int
1074 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1075 {
1076 #ifndef __lock_lint
1077 	_NOTE(ARGUNUSED(credp))
1078 #endif
1079 	int rv = 0;
1080 	dev_info_t *dip;
1081 	scsi_hba_tran_t *scsi_hba_tran;
1082 	sata_hba_inst_t	*sata_hba_inst;
1083 
1084 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1085 
1086 	if (otyp != OTYP_CHR)
1087 		return (EINVAL);
1088 
1089 	dip = sata_devt_to_devinfo(*devp);
1090 	if (dip == NULL)
1091 		return (ENXIO);
1092 
1093 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1094 		return (ENXIO);
1095 
1096 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1097 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1098 		return (ENXIO);
1099 
1100 	mutex_enter(&sata_mutex);
1101 	if (flags & FEXCL) {
1102 		if (sata_hba_inst->satahba_open_flag != 0) {
1103 			rv = EBUSY;
1104 		} else {
1105 			sata_hba_inst->satahba_open_flag =
1106 			    SATA_DEVCTL_EXOPENED;
1107 		}
1108 	} else {
1109 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1110 			rv = EBUSY;
1111 		} else {
1112 			sata_hba_inst->satahba_open_flag =
1113 			    SATA_DEVCTL_SOPENED;
1114 		}
1115 	}
1116 	mutex_exit(&sata_mutex);
1117 
1118 	return (rv);
1119 }
1120 
1121 
1122 /*
1123  * Close devctl node.
1124  * Returns:
1125  * 0 if node was closed successfully, error code otherwise.
1126  *
1127  */
1128 
1129 static int
1130 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1131 {
1132 #ifndef __lock_lint
1133 	_NOTE(ARGUNUSED(credp))
1134 	_NOTE(ARGUNUSED(flag))
1135 #endif
1136 	dev_info_t *dip;
1137 	scsi_hba_tran_t *scsi_hba_tran;
1138 	sata_hba_inst_t	*sata_hba_inst;
1139 
1140 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1141 
1142 	if (otyp != OTYP_CHR)
1143 		return (EINVAL);
1144 
1145 	dip = sata_devt_to_devinfo(dev);
1146 	if (dip == NULL)
1147 		return (ENXIO);
1148 
1149 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1150 		return (ENXIO);
1151 
1152 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1153 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1154 		return (ENXIO);
1155 
1156 	mutex_enter(&sata_mutex);
1157 	sata_hba_inst->satahba_open_flag = 0;
1158 	mutex_exit(&sata_mutex);
1159 	return (0);
1160 }
1161 
1162 
1163 
1164 /*
1165  * Standard IOCTL commands for SATA hotplugging.
1166  * Implemented DEVCTL_AP commands:
1167  * DEVCTL_AP_CONNECT
1168  * DEVCTL_AP_DISCONNECT
1169  * DEVCTL_AP_CONFIGURE
1170  * DEVCTL_UNCONFIGURE
1171  * DEVCTL_AP_CONTROL
1172  *
1173  * Commands passed to default ndi ioctl handler:
1174  * DEVCTL_DEVICE_GETSTATE
1175  * DEVCTL_DEVICE_ONLINE
1176  * DEVCTL_DEVICE_OFFLINE
1177  * DEVCTL_DEVICE_REMOVE
1178  * DEVCTL_DEVICE_INSERT
1179  * DEVCTL_BUS_GETSTATE
1180  *
1181  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1182  * if not.
1183  *
1184  * Returns:
1185  * 0 if successful,
1186  * error code if operation failed.
1187  *
1188  * NOTE: Port Multiplier is not supported.
1189  *
1190  */
1191 
1192 static int
1193 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1194     int *rvalp)
1195 {
1196 #ifndef __lock_lint
1197 	_NOTE(ARGUNUSED(credp))
1198 	_NOTE(ARGUNUSED(rvalp))
1199 #endif
1200 	int rv = 0;
1201 	int32_t	comp_port = -1;
1202 	dev_info_t *dip;
1203 	devctl_ap_state_t ap_state;
1204 	struct devctl_iocdata *dcp = NULL;
1205 	scsi_hba_tran_t *scsi_hba_tran;
1206 	sata_hba_inst_t *sata_hba_inst;
1207 	sata_device_t sata_device;
1208 	sata_cport_info_t *cportinfo;
1209 	int cport, pmport, qual;
1210 	int rval = SATA_SUCCESS;
1211 
1212 	dip = sata_devt_to_devinfo(dev);
1213 	if (dip == NULL)
1214 		return (ENXIO);
1215 
1216 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1217 		return (ENXIO);
1218 
1219 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1220 	if (sata_hba_inst == NULL)
1221 		return (ENXIO);
1222 
1223 	if (sata_hba_inst->satahba_tran == NULL)
1224 		return (ENXIO);
1225 
1226 	switch (cmd) {
1227 
1228 	case DEVCTL_DEVICE_GETSTATE:
1229 	case DEVCTL_DEVICE_ONLINE:
1230 	case DEVCTL_DEVICE_OFFLINE:
1231 	case DEVCTL_DEVICE_REMOVE:
1232 	case DEVCTL_BUS_GETSTATE:
1233 		/*
1234 		 * There may be more cases that we want to pass to default
1235 		 * handler rather than fail them.
1236 		 */
1237 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1238 	}
1239 
1240 	/* read devctl ioctl data */
1241 	if (cmd != DEVCTL_AP_CONTROL) {
1242 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1243 			return (EFAULT);
1244 
1245 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1246 		    -1) {
1247 			if (dcp)
1248 				ndi_dc_freehdl(dcp);
1249 			return (EINVAL);
1250 		}
1251 
1252 		cport = SCSI_TO_SATA_CPORT(comp_port);
1253 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1254 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1255 		qual = SATA_ADDR_CPORT;
1256 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1257 		    qual) != 0) {
1258 			ndi_dc_freehdl(dcp);
1259 			return (EINVAL);
1260 		}
1261 
1262 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1263 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1264 		    cport_mutex);
1265 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1266 			/*
1267 			 * Cannot process ioctl request now. Come back later.
1268 			 */
1269 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1270 			    cport_mutex);
1271 			ndi_dc_freehdl(dcp);
1272 			return (EBUSY);
1273 		}
1274 		/* Block event processing for this port */
1275 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1276 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1277 
1278 		sata_device.satadev_addr.cport = cport;
1279 		sata_device.satadev_addr.pmport = pmport;
1280 		sata_device.satadev_addr.qual = qual;
1281 		sata_device.satadev_rev = SATA_DEVICE_REV;
1282 	}
1283 
1284 	switch (cmd) {
1285 
1286 	case DEVCTL_AP_DISCONNECT:
1287 
1288 		/*
1289 		 * Normally, cfgadm sata plugin will try to offline
1290 		 * (unconfigure) device before this request. Nevertheless,
1291 		 * if a device is still configured, we need to
1292 		 * attempt to offline and unconfigure device first, and we will
1293 		 * deactivate the port regardless of the unconfigure
1294 		 * operation results.
1295 		 *
1296 		 */
1297 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1298 
1299 		break;
1300 
1301 	case DEVCTL_AP_UNCONFIGURE:
1302 
1303 		/*
1304 		 * The unconfigure operation uses generic nexus operation to
1305 		 * offline a device. It leaves a target device node attached.
1306 		 * and obviously sata_drive_info attached as well, because
1307 		 * from the hardware point of view nothing has changed.
1308 		 */
1309 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1310 		break;
1311 
1312 	case DEVCTL_AP_CONNECT:
1313 	{
1314 		/*
1315 		 * The sata cfgadm pluging will invoke this operation only if
1316 		 * port was found in the disconnect state (failed state
1317 		 * is also treated as the disconnected state).
1318 		 * If port activation is successful and a device is found
1319 		 * attached to the port, the initialization sequence is
1320 		 * executed to probe the port and attach
1321 		 * a device structure to a port structure. The device is not
1322 		 * set in configured state (system-wise) by this operation.
1323 		 */
1324 
1325 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1326 
1327 		break;
1328 	}
1329 
1330 	case DEVCTL_AP_CONFIGURE:
1331 	{
1332 		/*
1333 		 * A port may be in an active or shutdown state.
1334 		 * If port is in a failed state, operation is aborted.
1335 		 * If a port is in a shutdown state, sata_tran_port_activate()
1336 		 * is invoked prior to any other operation.
1337 		 *
1338 		 * Onlining the device involves creating a new target node.
1339 		 * If there is an old target node present (belonging to
1340 		 * previously removed device), the operation is aborted - the
1341 		 * old node has to be released and removed before configure
1342 		 * operation is attempted.
1343 		 */
1344 
1345 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1346 
1347 		break;
1348 	}
1349 
1350 	case DEVCTL_AP_GETSTATE:
1351 
1352 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1353 
1354 		ap_state.ap_last_change = (time_t)-1;
1355 		ap_state.ap_error_code = 0;
1356 		ap_state.ap_in_transition = 0;
1357 
1358 		/* Copy the return AP-state information to the user space */
1359 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1360 			rv = EFAULT;
1361 		}
1362 		break;
1363 
1364 	case DEVCTL_AP_CONTROL:
1365 	{
1366 		/*
1367 		 * Generic devctl for hardware specific functionality
1368 		 */
1369 		sata_ioctl_data_t	ioc;
1370 
1371 		ASSERT(dcp == NULL);
1372 
1373 		/* Copy in user ioctl data first */
1374 #ifdef _MULTI_DATAMODEL
1375 		if (ddi_model_convert_from(mode & FMODELS) ==
1376 		    DDI_MODEL_ILP32) {
1377 
1378 			sata_ioctl_data_32_t	ioc32;
1379 
1380 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1381 			    sizeof (ioc32), mode) != 0) {
1382 				rv = EFAULT;
1383 				break;
1384 			}
1385 			ioc.cmd 	= (uint_t)ioc32.cmd;
1386 			ioc.port	= (uint_t)ioc32.port;
1387 			ioc.get_size	= (uint_t)ioc32.get_size;
1388 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1389 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1390 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1391 		} else
1392 #endif /* _MULTI_DATAMODEL */
1393 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1394 		    mode) != 0) {
1395 			return (EFAULT);
1396 		}
1397 
1398 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1399 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1400 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1401 
1402 		/*
1403 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1404 		 * a 32-bit number.
1405 		 */
1406 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1407 			return (EINVAL);
1408 		}
1409 		/* validate address */
1410 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1411 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1412 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1413 
1414 		/* Override address qualifier - handle cport only for now */
1415 		qual = SATA_ADDR_CPORT;
1416 
1417 		if (sata_validate_sata_address(sata_hba_inst, cport,
1418 		    pmport, qual) != 0)
1419 			return (EINVAL);
1420 
1421 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1422 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1423 		    cport_mutex);
1424 		/* Is the port locked by event processing daemon ? */
1425 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1426 			/*
1427 			 * Cannot process ioctl request now. Come back later
1428 			 */
1429 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1430 			    cport_mutex);
1431 			return (EBUSY);
1432 		}
1433 		/* Block event processing for this port */
1434 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1435 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1436 
1437 
1438 		sata_device.satadev_addr.cport = cport;
1439 		sata_device.satadev_addr.pmport = pmport;
1440 		sata_device.satadev_addr.qual = qual;
1441 		sata_device.satadev_rev = SATA_DEVICE_REV;
1442 
1443 		switch (ioc.cmd) {
1444 
1445 		case SATA_CFGA_RESET_PORT:
1446 			/*
1447 			 * There is no protection for configured device.
1448 			 */
1449 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1450 			break;
1451 
1452 		case SATA_CFGA_RESET_DEVICE:
1453 			/*
1454 			 * There is no protection for configured device.
1455 			 */
1456 			rv = sata_ioctl_reset_device(sata_hba_inst,
1457 			    &sata_device);
1458 			break;
1459 
1460 		case SATA_CFGA_RESET_ALL:
1461 			/*
1462 			 * There is no protection for configured devices.
1463 			 */
1464 			rv = sata_ioctl_reset_all(sata_hba_inst);
1465 			/*
1466 			 * We return here, because common return is for
1467 			 * a single port operation - we have already unlocked
1468 			 * all ports and no dc handle was allocated.
1469 			 */
1470 			return (rv);
1471 
1472 		case SATA_CFGA_PORT_DEACTIVATE:
1473 			/*
1474 			 * Arbitrarily unconfigure attached device, if any.
1475 			 * Even if the unconfigure fails, proceed with the
1476 			 * port deactivation.
1477 			 */
1478 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1479 
1480 			break;
1481 
1482 		case SATA_CFGA_PORT_ACTIVATE:
1483 
1484 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1485 			break;
1486 
1487 		case SATA_CFGA_PORT_SELF_TEST:
1488 
1489 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1490 			    &sata_device);
1491 			break;
1492 
1493 		case SATA_CFGA_GET_DEVICE_PATH:
1494 			if (qual == SATA_ADDR_CPORT)
1495 				sata_device.satadev_addr.qual =
1496 				    SATA_ADDR_DCPORT;
1497 			else
1498 				sata_device.satadev_addr.qual =
1499 				    SATA_ADDR_DPMPORT;
1500 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1501 			    &sata_device, &ioc, mode);
1502 			break;
1503 
1504 		case SATA_CFGA_GET_AP_TYPE:
1505 
1506 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1507 			    &sata_device, &ioc, mode);
1508 			break;
1509 
1510 		case SATA_CFGA_GET_MODEL_INFO:
1511 
1512 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1513 			    &sata_device, &ioc, mode);
1514 			break;
1515 
1516 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1517 
1518 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1519 			    &sata_device, &ioc, mode);
1520 			break;
1521 
1522 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1523 
1524 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1525 			    &sata_device, &ioc, mode);
1526 			break;
1527 
1528 		default:
1529 			rv = EINVAL;
1530 			break;
1531 
1532 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1533 
1534 		break;
1535 	}
1536 
1537 	default:
1538 	{
1539 		/*
1540 		 * If we got here, we got an IOCTL that SATA HBA Framework
1541 		 * does not recognize. Pass ioctl to HBA driver, in case
1542 		 * it could process it.
1543 		 */
1544 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1545 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1546 
1547 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1548 		    "IOCTL 0x%2x not supported in SATA framework, "
1549 		    "passthrough to HBA", cmd);
1550 
1551 		if (sata_tran->sata_tran_ioctl == NULL) {
1552 			rv = EINVAL;
1553 			break;
1554 		}
1555 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1556 		if (rval != 0) {
1557 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1558 			    "IOCTL 0x%2x failed in HBA", cmd);
1559 			rv = rval;
1560 		}
1561 		break;
1562 	}
1563 
1564 	} /* End of main IOCTL switch */
1565 
1566 	if (dcp) {
1567 		ndi_dc_freehdl(dcp);
1568 	}
1569 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1570 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1571 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1572 
1573 	return (rv);
1574 }
1575 
1576 
1577 /*
1578  * Create error retrieval sata packet
1579  *
1580  * A sata packet is allocated and set-up to contain specified error retrieval
1581  * command and appropriate dma-able data buffer.
1582  * No association with any scsi packet is made and no callback routine is
1583  * specified.
1584  *
1585  * Returns a pointer to sata packet upon successfull packet creation.
1586  * Returns NULL, if packet cannot be created.
1587  */
1588 sata_pkt_t *
1589 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1590     int pkt_type)
1591 {
1592 	sata_hba_inst_t	*sata_hba_inst;
1593 	sata_pkt_txlate_t *spx;
1594 	sata_pkt_t *spkt;
1595 	sata_drive_info_t *sdinfo;
1596 
1597 	mutex_enter(&sata_mutex);
1598 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1599 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1600 		if (SATA_DIP(sata_hba_inst) == dip)
1601 			break;
1602 	}
1603 	mutex_exit(&sata_mutex);
1604 	ASSERT(sata_hba_inst != NULL);
1605 
1606 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1607 	if (sdinfo == NULL) {
1608 		sata_log(sata_hba_inst, CE_WARN,
1609 		    "sata: error recovery request for non-attached device at "
1610 		    "cport %d", sata_device->satadev_addr.cport);
1611 		return (NULL);
1612 	}
1613 
1614 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1615 	spx->txlt_sata_hba_inst = sata_hba_inst;
1616 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1617 	spkt = sata_pkt_alloc(spx, NULL);
1618 	if (spkt == NULL) {
1619 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1620 		return (NULL);
1621 	}
1622 	/* address is needed now */
1623 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1624 
1625 	switch (pkt_type) {
1626 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1627 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1628 			return (spkt);
1629 		break;
1630 
1631 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1632 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1633 			return (spkt);
1634 		break;
1635 
1636 	default:
1637 		break;
1638 	}
1639 
1640 	sata_pkt_free(spx);
1641 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1642 	return (NULL);
1643 
1644 }
1645 
1646 
1647 /*
1648  * Free error retrieval sata packet
1649  *
1650  * Free sata packet and any associated resources allocated previously by
1651  * sata_get_error_retrieval_pkt().
1652  *
1653  * Void return.
1654  */
1655 void
1656 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1657 {
1658 	sata_pkt_txlate_t *spx =
1659 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1660 
1661 	ASSERT(sata_pkt != NULL);
1662 
1663 	sata_free_local_buffer(spx);
1664 	sata_pkt_free(spx);
1665 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1666 
1667 }
1668 
1669 /*
1670  * sata_name_child is for composing the name of the node
1671  * the format of the name is "target,0".
1672  */
1673 static int
1674 sata_name_child(dev_info_t *dip, char *name, int namelen)
1675 {
1676 	int target;
1677 
1678 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1679 	    DDI_PROP_DONTPASS, "target", -1);
1680 	if (target == -1)
1681 		return (DDI_FAILURE);
1682 	(void) snprintf(name, namelen, "%x,0", target);
1683 	return (DDI_SUCCESS);
1684 }
1685 
1686 
1687 
1688 /* ****************** SCSA required entry points *********************** */
1689 
1690 /*
1691  * Implementation of scsi tran_tgt_init.
1692  * sata_scsi_tgt_init() initializes scsi_device structure
1693  *
1694  * If successful, DDI_SUCCESS is returned.
1695  * DDI_FAILURE is returned if addressed device does not exist
1696  */
1697 
1698 static int
1699 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1700     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1701 {
1702 #ifndef __lock_lint
1703 	_NOTE(ARGUNUSED(hba_dip))
1704 	_NOTE(ARGUNUSED(tgt_dip))
1705 #endif
1706 	sata_device_t		sata_device;
1707 	sata_drive_info_t	*sdinfo;
1708 	struct sata_id		*sid;
1709 	sata_hba_inst_t		*sata_hba_inst;
1710 	char			model[SATA_ID_MODEL_LEN + 1];
1711 	char			fw[SATA_ID_FW_LEN + 1];
1712 	char			*vid, *pid;
1713 	int			i;
1714 
1715 	/*
1716 	 * Fail tran_tgt_init for .conf stub node
1717 	 */
1718 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1719 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1720 		ddi_set_name_addr(tgt_dip, NULL);
1721 		return (DDI_FAILURE);
1722 	}
1723 
1724 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1725 
1726 	/* Validate scsi device address */
1727 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1728 	    &sata_device) != 0)
1729 		return (DDI_FAILURE);
1730 
1731 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1732 	    sata_device.satadev_addr.cport)));
1733 
1734 	/* sata_device now contains a valid sata address */
1735 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1736 	if (sdinfo == NULL) {
1737 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1738 		    sata_device.satadev_addr.cport)));
1739 		return (DDI_FAILURE);
1740 	}
1741 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1742 	    sata_device.satadev_addr.cport)));
1743 
1744 	/*
1745 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1746 	 * the target disks.
1747 	 *
1748 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1749 	 * if we need to create cmdk-style devid for all the disk devices
1750 	 * attached to this controller. This property may have been set
1751 	 * from HBA driver's .conf file or by the HBA driver in its
1752 	 * attach(9F) function.
1753 	 */
1754 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1755 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1756 	    "use-cmdk-devid-format", 0) == 1)) {
1757 		/* register a legacy devid for this target node */
1758 		sata_target_devid_register(tgt_dip, sdinfo);
1759 	}
1760 
1761 
1762 	/*
1763 	 * 'Identify Device Data' does not always fit in standard SCSI
1764 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1765 	 * of information.
1766 	 */
1767 	sid = &sdinfo->satadrv_id;
1768 #ifdef	_LITTLE_ENDIAN
1769 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1770 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1771 #else	/* _LITTLE_ENDIAN */
1772 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1773 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1774 #endif	/* _LITTLE_ENDIAN */
1775 	model[SATA_ID_MODEL_LEN] = 0;
1776 	fw[SATA_ID_FW_LEN] = 0;
1777 
1778 	/* split model into into vid/pid */
1779 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1780 		if ((*pid == ' ') || (*pid == '\t'))
1781 			break;
1782 	if (i < SATA_ID_MODEL_LEN) {
1783 		vid = model;
1784 		*pid++ = 0;		/* terminate vid, establish pid */
1785 	} else {
1786 		vid = NULL;		/* vid will stay "ATA     " */
1787 		pid = model;		/* model is all pid */
1788 	}
1789 
1790 	if (vid)
1791 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1792 		    vid, strlen(vid));
1793 	if (pid)
1794 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1795 		    pid, strlen(pid));
1796 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1797 	    fw, strlen(fw));
1798 
1799 	return (DDI_SUCCESS);
1800 }
1801 
1802 /*
1803  * Implementation of scsi tran_tgt_probe.
1804  * Probe target, by calling default scsi routine scsi_hba_probe()
1805  */
1806 static int
1807 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1808 {
1809 	sata_hba_inst_t *sata_hba_inst =
1810 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1811 	int rval;
1812 
1813 	rval = scsi_hba_probe(sd, callback);
1814 
1815 	if (rval == SCSIPROBE_EXISTS) {
1816 		/*
1817 		 * Set property "pm-capable" on the target device node, so that
1818 		 * the target driver will not try to fetch scsi cycle counters
1819 		 * before enabling device power-management.
1820 		 */
1821 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1822 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1823 			sata_log(sata_hba_inst, CE_WARN,
1824 			    "SATA device at port %d: "
1825 			    "will not be power-managed ",
1826 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1827 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1828 			    "failure updating pm-capable property"));
1829 		}
1830 	}
1831 	return (rval);
1832 }
1833 
1834 /*
1835  * Implementation of scsi tran_tgt_free.
1836  * Release all resources allocated for scsi_device
1837  */
1838 static void
1839 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1840     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1841 {
1842 #ifndef __lock_lint
1843 	_NOTE(ARGUNUSED(hba_dip))
1844 #endif
1845 	sata_device_t		sata_device;
1846 	sata_drive_info_t	*sdinfo;
1847 	sata_hba_inst_t		*sata_hba_inst;
1848 	ddi_devid_t		devid;
1849 
1850 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1851 
1852 	/* Validate scsi device address */
1853 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1854 	    &sata_device) != 0)
1855 		return;
1856 
1857 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1858 	    sata_device.satadev_addr.cport)));
1859 
1860 	/* sata_device now should contain a valid sata address */
1861 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1862 	if (sdinfo == NULL) {
1863 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1864 		    sata_device.satadev_addr.cport)));
1865 		return;
1866 	}
1867 	/*
1868 	 * We did not allocate any resources in sata_scsi_tgt_init()
1869 	 * other than few properties.
1870 	 * Free them.
1871 	 */
1872 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1873 	    sata_device.satadev_addr.cport)));
1874 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1875 
1876 	/*
1877 	 * If devid was previously created but not freed up from
1878 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1879 	 */
1880 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1881 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1882 	    "use-cmdk-devid-format", 0) == 1) &&
1883 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1884 		ddi_devid_unregister(tgt_dip);
1885 		ddi_devid_free(devid);
1886 	}
1887 }
1888 
1889 /*
1890  * Implementation of scsi tran_init_pkt
1891  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1892  *
1893  * It seems that we should always allocate pkt, even if the address is
1894  * for non-existing device - just use some default for dma_attr.
1895  * The reason is that there is no way to communicate this to a caller here.
1896  * Subsequent call to sata_scsi_start may fail appropriately.
1897  * Simply returning NULL does not seem to discourage a target driver...
1898  *
1899  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1900  */
1901 static struct scsi_pkt *
1902 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1903     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1904     int (*callback)(caddr_t), caddr_t arg)
1905 {
1906 	sata_hba_inst_t *sata_hba_inst =
1907 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1908 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1909 	sata_device_t sata_device;
1910 	sata_drive_info_t *sdinfo;
1911 	sata_pkt_txlate_t *spx;
1912 	ddi_dma_attr_t cur_dma_attr;
1913 	int rval;
1914 	boolean_t new_pkt = TRUE;
1915 
1916 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1917 
1918 	/*
1919 	 * We need to translate the address, even if it could be
1920 	 * a bogus one, for a non-existing device
1921 	 */
1922 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1923 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1924 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1925 	sata_device.satadev_rev = SATA_DEVICE_REV;
1926 
1927 	if (pkt == NULL) {
1928 		/*
1929 		 * Have to allocate a brand new scsi packet.
1930 		 * We need to operate with auto request sense enabled.
1931 		 */
1932 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1933 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1934 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1935 
1936 		if (pkt == NULL)
1937 			return (NULL);
1938 
1939 		/* Fill scsi packet structure */
1940 		pkt->pkt_comp		= (void (*)())NULL;
1941 		pkt->pkt_time		= 0;
1942 		pkt->pkt_resid		= 0;
1943 		pkt->pkt_statistics	= 0;
1944 		pkt->pkt_reason		= 0;
1945 
1946 		/*
1947 		 * pkt_hba_private will point to sata pkt txlate structure
1948 		 */
1949 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1950 		bzero(spx, sizeof (sata_pkt_txlate_t));
1951 
1952 		spx->txlt_scsi_pkt = pkt;
1953 		spx->txlt_sata_hba_inst = sata_hba_inst;
1954 
1955 		/* Allocate sata_pkt */
1956 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1957 		if (spx->txlt_sata_pkt == NULL) {
1958 			/* Could not allocate sata pkt */
1959 			scsi_hba_pkt_free(ap, pkt);
1960 			return (NULL);
1961 		}
1962 		/* Set sata address */
1963 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1964 		    sata_device.satadev_addr;
1965 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1966 		    sata_device.satadev_rev;
1967 
1968 		if ((bp == NULL) || (bp->b_bcount == 0))
1969 			return (pkt);
1970 
1971 		spx->txlt_total_residue = bp->b_bcount;
1972 	} else {
1973 		new_pkt = FALSE;
1974 		/*
1975 		 * Packet was preallocated/initialized by previous call
1976 		 */
1977 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1978 
1979 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1980 			return (pkt);
1981 		}
1982 
1983 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1984 	}
1985 
1986 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1987 
1988 	/*
1989 	 * We use an adjusted version of the dma_attr, to account
1990 	 * for device addressing limitations.
1991 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1992 	 * happen when a device is not yet configured.
1993 	 */
1994 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1995 	    sata_device.satadev_addr.cport)));
1996 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1997 	    &spx->txlt_sata_pkt->satapkt_device);
1998 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1999 	sata_adjust_dma_attr(sdinfo,
2000 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2001 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2002 	    sata_device.satadev_addr.cport)));
2003 	/*
2004 	 * Allocate necessary DMA resources for the packet's data buffer
2005 	 * NOTE:
2006 	 * In case of read/write commands, DMA resource allocation here is
2007 	 * based on the premise that the transfer length specified in
2008 	 * the read/write scsi cdb will match exactly DMA resources -
2009 	 * returning correct packet residue is crucial.
2010 	 */
2011 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2012 	    &cur_dma_attr)) != DDI_SUCCESS) {
2013 		/*
2014 		 * If a DMA allocation request fails with
2015 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2016 		 * bioerror(9F) with bp and an error code of EFAULT.
2017 		 * If a DMA allocation request fails with
2018 		 * DDI_DMA_TOOBIG, indicate the error by calling
2019 		 * bioerror(9F) with bp and an error code of EINVAL.
2020 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2021 		 * Request may be repeated later - there is no real error.
2022 		 */
2023 		switch (rval) {
2024 		case DDI_DMA_NORESOURCES:
2025 			bioerror(bp, 0);
2026 			break;
2027 		case DDI_DMA_NOMAPPING:
2028 		case DDI_DMA_BADATTR:
2029 			bioerror(bp, EFAULT);
2030 			break;
2031 		case DDI_DMA_TOOBIG:
2032 		default:
2033 			bioerror(bp, EINVAL);
2034 			break;
2035 		}
2036 		if (new_pkt == TRUE) {
2037 			/*
2038 			 * Since this is a new packet, we can clean-up
2039 			 * everything
2040 			 */
2041 			sata_scsi_destroy_pkt(ap, pkt);
2042 		} else {
2043 			/*
2044 			 * This is a re-used packet. It will be target driver's
2045 			 * responsibility to eventually destroy it (which
2046 			 * will free allocated resources).
2047 			 * Here, we just "complete" the request, leaving
2048 			 * allocated resources intact, so the request may
2049 			 * be retried.
2050 			 */
2051 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2052 			sata_pkt_free(spx);
2053 		}
2054 		return (NULL);
2055 	}
2056 	/* Set number of bytes that are not yet accounted for */
2057 	pkt->pkt_resid = spx->txlt_total_residue;
2058 	ASSERT(pkt->pkt_resid >= 0);
2059 
2060 	return (pkt);
2061 }
2062 
2063 /*
2064  * Implementation of scsi tran_start.
2065  * Translate scsi cmd into sata operation and return status.
2066  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2067  * are supported.
2068  * For SATA hard disks, supported scsi commands:
2069  * SCMD_INQUIRY
2070  * SCMD_TEST_UNIT_READY
2071  * SCMD_START_STOP
2072  * SCMD_READ_CAPACITY
2073  * SCMD_REQUEST_SENSE
2074  * SCMD_LOG_SENSE_G1
2075  * SCMD_LOG_SELECT_G1
2076  * SCMD_MODE_SENSE	(specific pages)
2077  * SCMD_MODE_SENSE_G1	(specific pages)
2078  * SCMD_MODE_SELECT	(specific pages)
2079  * SCMD_MODE_SELECT_G1	(specific pages)
2080  * SCMD_SYNCHRONIZE_CACHE
2081  * SCMD_SYNCHRONIZE_CACHE_G1
2082  * SCMD_READ
2083  * SCMD_READ_G1
2084  * SCMD_READ_G4
2085  * SCMD_READ_G5
2086  * SCMD_WRITE
2087  * SCMD_WRITE_BUFFER
2088  * SCMD_WRITE_G1
2089  * SCMD_WRITE_G4
2090  * SCMD_WRITE_G5
2091  * SCMD_SEEK		(noop)
2092  * SCMD_SDIAG
2093  *
2094  * All other commands are rejected as unsupported.
2095  *
2096  * Returns:
2097  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2098  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2099  * a callback could be scheduled.
2100  * TRAN_BADPKT if cmd was directed to invalid address.
2101  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2102  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2103  * was removed and there was no callback specified in scsi pkt.
2104  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2105  * framework was busy performing some other operation(s).
2106  *
2107  */
2108 static int
2109 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2110 {
2111 	sata_hba_inst_t *sata_hba_inst =
2112 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2113 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2114 	sata_drive_info_t *sdinfo;
2115 	struct buf *bp;
2116 	int cport;
2117 	int rval;
2118 
2119 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2120 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2121 
2122 	ASSERT(spx != NULL &&
2123 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2124 
2125 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2126 
2127 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2128 	sdinfo = sata_get_device_info(sata_hba_inst,
2129 	    &spx->txlt_sata_pkt->satapkt_device);
2130 	if (sdinfo == NULL ||
2131 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2132 	    B_FALSE ||
2133 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2134 
2135 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2136 		pkt->pkt_reason = CMD_DEV_GONE;
2137 		/*
2138 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2139 		 * only in callback function (for normal requests) and
2140 		 * in the dump code path.
2141 		 * So, if the callback is available, we need to do
2142 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2143 		 */
2144 		if (pkt->pkt_comp != NULL) {
2145 			/* scsi callback required */
2146 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2147 			    (task_func_t *)pkt->pkt_comp,
2148 			    (void *)pkt, TQ_SLEEP) == NULL)
2149 				/* Scheduling the callback failed */
2150 				return (TRAN_BUSY);
2151 			return (TRAN_ACCEPT);
2152 		}
2153 		/* No callback available */
2154 		return (TRAN_FATAL_ERROR);
2155 	}
2156 
2157 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2158 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2159 		rval = sata_txlt_atapi(spx);
2160 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2161 		    "sata_scsi_start atapi: rval %d\n", rval);
2162 		return (rval);
2163 	}
2164 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2165 
2166 	/* ATA Disk commands processing starts here */
2167 
2168 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2169 
2170 	switch (pkt->pkt_cdbp[0]) {
2171 
2172 	case SCMD_INQUIRY:
2173 		/* Mapped to identify device */
2174 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2175 			bp_mapin(bp);
2176 		rval = sata_txlt_inquiry(spx);
2177 		break;
2178 
2179 	case SCMD_TEST_UNIT_READY:
2180 		/*
2181 		 * SAT "SATA to ATA Translation" doc specifies translation
2182 		 * to ATA CHECK POWER MODE.
2183 		 */
2184 		rval = sata_txlt_test_unit_ready(spx);
2185 		break;
2186 
2187 	case SCMD_START_STOP:
2188 		/* Mapping depends on the command */
2189 		rval = sata_txlt_start_stop_unit(spx);
2190 		break;
2191 
2192 	case SCMD_READ_CAPACITY:
2193 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2194 			bp_mapin(bp);
2195 		rval = sata_txlt_read_capacity(spx);
2196 		break;
2197 
2198 	case SCMD_REQUEST_SENSE:
2199 		/*
2200 		 * Always No Sense, since we force ARQ
2201 		 */
2202 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2203 			bp_mapin(bp);
2204 		rval = sata_txlt_request_sense(spx);
2205 		break;
2206 
2207 	case SCMD_LOG_SENSE_G1:
2208 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2209 			bp_mapin(bp);
2210 		rval = sata_txlt_log_sense(spx);
2211 		break;
2212 
2213 	case SCMD_LOG_SELECT_G1:
2214 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2215 			bp_mapin(bp);
2216 		rval = sata_txlt_log_select(spx);
2217 		break;
2218 
2219 	case SCMD_MODE_SENSE:
2220 	case SCMD_MODE_SENSE_G1:
2221 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2222 			bp_mapin(bp);
2223 		rval = sata_txlt_mode_sense(spx);
2224 		break;
2225 
2226 
2227 	case SCMD_MODE_SELECT:
2228 	case SCMD_MODE_SELECT_G1:
2229 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2230 			bp_mapin(bp);
2231 		rval = sata_txlt_mode_select(spx);
2232 		break;
2233 
2234 	case SCMD_SYNCHRONIZE_CACHE:
2235 	case SCMD_SYNCHRONIZE_CACHE_G1:
2236 		rval = sata_txlt_synchronize_cache(spx);
2237 		break;
2238 
2239 	case SCMD_READ:
2240 	case SCMD_READ_G1:
2241 	case SCMD_READ_G4:
2242 	case SCMD_READ_G5:
2243 		rval = sata_txlt_read(spx);
2244 		break;
2245 	case SCMD_WRITE_BUFFER:
2246 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2247 			bp_mapin(bp);
2248 		rval = sata_txlt_write_buffer(spx);
2249 		break;
2250 
2251 	case SCMD_WRITE:
2252 	case SCMD_WRITE_G1:
2253 	case SCMD_WRITE_G4:
2254 	case SCMD_WRITE_G5:
2255 		rval = sata_txlt_write(spx);
2256 		break;
2257 
2258 	case SCMD_SEEK:
2259 		rval = sata_txlt_nodata_cmd_immediate(spx);
2260 		break;
2261 
2262 		/* Other cases will be filed later */
2263 		/* postponed until phase 2 of the development */
2264 	default:
2265 		rval = sata_txlt_invalid_command(spx);
2266 		break;
2267 	}
2268 
2269 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2270 	    "sata_scsi_start: rval %d\n", rval);
2271 
2272 	return (rval);
2273 }
2274 
2275 /*
2276  * Implementation of scsi tran_abort.
2277  * Abort specific pkt or all packets.
2278  *
2279  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2280  *
2281  * May be called from an interrupt level.
2282  */
2283 static int
2284 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2285 {
2286 	sata_hba_inst_t *sata_hba_inst =
2287 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2288 	sata_device_t	sata_device;
2289 	sata_pkt_t	*sata_pkt;
2290 
2291 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2292 	    "sata_scsi_abort: %s at target: 0x%x\n",
2293 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2294 
2295 	/* Validate address */
2296 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2297 		/* Invalid address */
2298 		return (0);
2299 
2300 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2301 	    sata_device.satadev_addr.cport)));
2302 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2303 		/* invalid address */
2304 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2305 		    sata_device.satadev_addr.cport)));
2306 		return (0);
2307 	}
2308 	if (scsi_pkt == NULL) {
2309 		/*
2310 		 * Abort all packets.
2311 		 * Although we do not have specific packet, we still need
2312 		 * dummy packet structure to pass device address to HBA.
2313 		 * Allocate one, without sleeping. Fail if pkt cannot be
2314 		 * allocated.
2315 		 */
2316 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2317 		if (sata_pkt == NULL) {
2318 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2319 			    sata_device.satadev_addr.cport)));
2320 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2321 			    "could not allocate sata_pkt"));
2322 			return (0);
2323 		}
2324 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2325 		sata_pkt->satapkt_device = sata_device;
2326 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2327 	} else {
2328 		if (scsi_pkt->pkt_ha_private == NULL) {
2329 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2330 			    sata_device.satadev_addr.cport)));
2331 			return (0); /* Bad scsi pkt */
2332 		}
2333 		/* extract pointer to sata pkt */
2334 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2335 		    txlt_sata_pkt;
2336 	}
2337 
2338 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2339 	    sata_device.satadev_addr.cport)));
2340 	/* Send abort request to HBA */
2341 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2342 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2343 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2344 	    SATA_SUCCESS) {
2345 		if (scsi_pkt == NULL)
2346 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2347 		/* Success */
2348 		return (1);
2349 	}
2350 	/* Else, something did not go right */
2351 	if (scsi_pkt == NULL)
2352 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2353 	/* Failure */
2354 	return (0);
2355 }
2356 
2357 
2358 /*
2359  * Implementation of scsi tran_reset.
2360  * RESET_ALL request is translated into port reset.
2361  * RESET_TARGET requests is translated into a device reset,
2362  * RESET_LUN request is accepted only for LUN 0 and translated into
2363  * device reset.
2364  * The target reset should cause all HBA active and queued packets to
2365  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2366  * the return. HBA should report reset event for the device.
2367  *
2368  * Returns 1 upon success, 0 upon failure.
2369  */
2370 static int
2371 sata_scsi_reset(struct scsi_address *ap, int level)
2372 {
2373 	sata_hba_inst_t	*sata_hba_inst =
2374 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2375 	sata_device_t	sata_device;
2376 	int		val;
2377 
2378 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2379 	    "sata_scsi_reset: level %d target: 0x%x\n",
2380 	    level, ap->a_target);
2381 
2382 	/* Validate address */
2383 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2384 	if (val == -1)
2385 		/* Invalid address */
2386 		return (0);
2387 
2388 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2389 	    sata_device.satadev_addr.cport)));
2390 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2391 		/* invalid address */
2392 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2393 		    sata_device.satadev_addr.cport)));
2394 		return (0);
2395 	}
2396 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2397 	    sata_device.satadev_addr.cport)));
2398 	if (level == RESET_ALL) {
2399 		/* port reset - cport only */
2400 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2401 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2402 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2403 			return (1);
2404 		else
2405 			return (0);
2406 
2407 	} else if (val == 0 &&
2408 	    (level == RESET_TARGET || level == RESET_LUN)) {
2409 		/* reset device (device attached) */
2410 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2411 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2412 			return (1);
2413 		else
2414 			return (0);
2415 	}
2416 	return (0);
2417 }
2418 
2419 
2420 /*
2421  * Implementation of scsi tran_getcap (get transport/device capabilities).
2422  * Supported capabilities for SATA hard disks:
2423  * auto-rqsense		(always supported)
2424  * tagged-qing		(supported if HBA supports it)
2425  * untagged-qing	(could be supported if disk supports it, but because
2426  *			 caching behavior allowing untagged queuing actually
2427  *			 results in reduced performance.  sd tries to throttle
2428  *			 back to only 3 outstanding commands, which may
2429  *			 work for real SCSI disks, but with read ahead
2430  *			 caching, having more than 1 outstanding command
2431  *			 results in cache thrashing.)
2432  * sector_size
2433  * dma_max
2434  * interconnect-type	(INTERCONNECT_SATA)
2435  *
2436  * Supported capabilities for ATAPI CD/DVD devices:
2437  * auto-rqsense		(always supported)
2438  * sector_size
2439  * dma_max
2440  * max-cdb-length
2441  * interconnect-type	(INTERCONNECT_SATA)
2442  *
2443  * Supported capabilities for ATAPI TAPE devices:
2444  * auto-rqsense		(always supported)
2445  * dma_max
2446  * max-cdb-length
2447  *
2448  * Supported capabilities for SATA ATAPI hard disks:
2449  * auto-rqsense		(always supported)
2450  * interconnect-type	(INTERCONNECT_SATA)
2451  * max-cdb-length
2452  *
2453  * Request for other capabilities is rejected as unsupported.
2454  *
2455  * Returns supported capability value, or -1 if capability is unsuppported or
2456  * the address is invalid - no device.
2457  */
2458 
2459 static int
2460 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2461 {
2462 
2463 	sata_hba_inst_t 	*sata_hba_inst =
2464 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2465 	sata_device_t		sata_device;
2466 	sata_drive_info_t	*sdinfo;
2467 	ddi_dma_attr_t		adj_dma_attr;
2468 	int 			rval;
2469 
2470 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2471 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2472 	    ap->a_target, cap);
2473 
2474 	/*
2475 	 * We want to process the capabilities on per port granularity.
2476 	 * So, we are specifically restricting ourselves to whom != 0
2477 	 * to exclude the controller wide handling.
2478 	 */
2479 	if (cap == NULL || whom == 0)
2480 		return (-1);
2481 
2482 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2483 		/* Invalid address */
2484 		return (-1);
2485 	}
2486 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2487 	    sata_device.satadev_addr.cport)));
2488 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2489 	    NULL) {
2490 		/* invalid address */
2491 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2492 		    sata_device.satadev_addr.cport)));
2493 		return (-1);
2494 	}
2495 
2496 	switch (scsi_hba_lookup_capstr(cap)) {
2497 	case SCSI_CAP_ARQ:
2498 		rval = 1;		/* ARQ supported, turned on */
2499 		break;
2500 
2501 	case SCSI_CAP_SECTOR_SIZE:
2502 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2503 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2504 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2505 			rval = SATA_ATAPI_SECTOR_SIZE;
2506 		else rval = -1;
2507 		break;
2508 
2509 	/*
2510 	 * untagged queuing cause a performance inversion because of
2511 	 * the way sd operates.  Because of this reason we do not
2512 	 * use it when available.
2513 	 */
2514 	case SCSI_CAP_UNTAGGED_QING:
2515 		if (sdinfo->satadrv_features_enabled &
2516 		    SATA_DEV_F_E_UNTAGGED_QING)
2517 			rval = 1;	/* Untagged queuing available */
2518 		else
2519 			rval = -1;	/* Untagged queuing not available */
2520 		break;
2521 
2522 	case SCSI_CAP_TAGGED_QING:
2523 		if ((sdinfo->satadrv_features_enabled &
2524 		    SATA_DEV_F_E_TAGGED_QING) &&
2525 		    (sdinfo->satadrv_max_queue_depth > 1))
2526 			rval = 1;	/* Tagged queuing available */
2527 		else
2528 			rval = -1;	/* Tagged queuing not available */
2529 		break;
2530 
2531 	case SCSI_CAP_DMA_MAX:
2532 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2533 		    &adj_dma_attr);
2534 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2535 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2536 		break;
2537 
2538 	case SCSI_CAP_INTERCONNECT_TYPE:
2539 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2540 		break;
2541 
2542 	case SCSI_CAP_CDB_LEN:
2543 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2544 			rval = sdinfo->satadrv_atapi_cdb_len;
2545 		else
2546 			rval = -1;
2547 		break;
2548 
2549 	default:
2550 		rval = -1;
2551 		break;
2552 	}
2553 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2554 	    sata_device.satadev_addr.cport)));
2555 	return (rval);
2556 }
2557 
2558 /*
2559  * Implementation of scsi tran_setcap
2560  *
2561  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2562  *
2563  */
2564 static int
2565 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2566 {
2567 	sata_hba_inst_t	*sata_hba_inst =
2568 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2569 	sata_device_t	sata_device;
2570 	sata_drive_info_t	*sdinfo;
2571 	int		rval;
2572 
2573 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2574 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2575 
2576 	/*
2577 	 * We want to process the capabilities on per port granularity.
2578 	 * So, we are specifically restricting ourselves to whom != 0
2579 	 * to exclude the controller wide handling.
2580 	 */
2581 	if (cap == NULL || whom == 0) {
2582 		return (-1);
2583 	}
2584 
2585 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2586 		/* Invalid address */
2587 		return (-1);
2588 	}
2589 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2590 	    sata_device.satadev_addr.cport)));
2591 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2592 	    &sata_device)) == NULL) {
2593 		/* invalid address */
2594 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2595 		    sata_device.satadev_addr.cport)));
2596 		return (-1);
2597 	}
2598 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2599 	    sata_device.satadev_addr.cport)));
2600 
2601 	switch (scsi_hba_lookup_capstr(cap)) {
2602 	case SCSI_CAP_ARQ:
2603 	case SCSI_CAP_SECTOR_SIZE:
2604 	case SCSI_CAP_DMA_MAX:
2605 	case SCSI_CAP_INTERCONNECT_TYPE:
2606 		rval = 0;
2607 		break;
2608 	case SCSI_CAP_UNTAGGED_QING:
2609 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2610 			rval = 1;
2611 			if (value == 1) {
2612 				sdinfo->satadrv_features_enabled |=
2613 				    SATA_DEV_F_E_UNTAGGED_QING;
2614 			} else if (value == 0) {
2615 				sdinfo->satadrv_features_enabled &=
2616 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2617 			} else {
2618 				rval = -1;
2619 			}
2620 		} else {
2621 			rval = 0;
2622 		}
2623 		break;
2624 	case SCSI_CAP_TAGGED_QING:
2625 		/* This can TCQ or NCQ */
2626 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2627 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2628 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2629 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2630 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2631 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2632 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2633 			rval = 1;
2634 			if (value == 1) {
2635 				sdinfo->satadrv_features_enabled |=
2636 				    SATA_DEV_F_E_TAGGED_QING;
2637 			} else if (value == 0) {
2638 				sdinfo->satadrv_features_enabled &=
2639 				    ~SATA_DEV_F_E_TAGGED_QING;
2640 			} else {
2641 				rval = -1;
2642 			}
2643 		} else {
2644 			rval = 0;
2645 		}
2646 		break;
2647 	default:
2648 		rval = -1;
2649 		break;
2650 	}
2651 	return (rval);
2652 }
2653 
2654 /*
2655  * Implementations of scsi tran_destroy_pkt.
2656  * Free resources allocated by sata_scsi_init_pkt()
2657  */
2658 static void
2659 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2660 {
2661 	sata_pkt_txlate_t *spx;
2662 
2663 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2664 
2665 	sata_common_free_dma_rsrcs(spx);
2666 
2667 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2668 	sata_pkt_free(spx);
2669 
2670 	scsi_hba_pkt_free(ap, pkt);
2671 }
2672 
2673 /*
2674  * Implementation of scsi tran_dmafree.
2675  * Free DMA resources allocated by sata_scsi_init_pkt()
2676  */
2677 
2678 static void
2679 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2680 {
2681 #ifndef __lock_lint
2682 	_NOTE(ARGUNUSED(ap))
2683 #endif
2684 	sata_pkt_txlate_t *spx;
2685 
2686 	ASSERT(pkt != NULL);
2687 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2688 
2689 	sata_common_free_dma_rsrcs(spx);
2690 }
2691 
2692 /*
2693  * Implementation of scsi tran_sync_pkt.
2694  *
2695  * The assumption below is that pkt is unique - there is no need to check ap
2696  *
2697  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2698  * into/from the real buffer.
2699  */
2700 static void
2701 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2702 {
2703 #ifndef __lock_lint
2704 	_NOTE(ARGUNUSED(ap))
2705 #endif
2706 	int rval;
2707 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2708 	struct buf *bp;
2709 	int direction;
2710 
2711 	ASSERT(spx != NULL);
2712 	if (spx->txlt_buf_dma_handle != NULL) {
2713 		direction = spx->txlt_sata_pkt->
2714 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2715 		if (spx->txlt_sata_pkt != NULL &&
2716 		    direction != SATA_DIR_NODATA_XFER) {
2717 			if (spx->txlt_tmp_buf != NULL) {
2718 				/* Intermediate DMA buffer used */
2719 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2720 
2721 				if (direction & SATA_DIR_WRITE) {
2722 					bcopy(bp->b_un.b_addr,
2723 					    spx->txlt_tmp_buf, bp->b_bcount);
2724 				}
2725 			}
2726 			/* Sync the buffer for device or for CPU */
2727 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2728 			    (direction & SATA_DIR_WRITE) ?
2729 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2730 			ASSERT(rval == DDI_SUCCESS);
2731 			if (spx->txlt_tmp_buf != NULL &&
2732 			    !(direction & SATA_DIR_WRITE)) {
2733 				/* Intermediate DMA buffer used for read */
2734 				bcopy(spx->txlt_tmp_buf,
2735 				    bp->b_un.b_addr, bp->b_bcount);
2736 			}
2737 
2738 		}
2739 	}
2740 }
2741 
2742 
2743 
2744 /* *******************  SATA - SCSI Translation functions **************** */
2745 /*
2746  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2747  * translation.
2748  */
2749 
2750 /*
2751  * Checks if a device exists and can be access and translates common
2752  * scsi_pkt data to sata_pkt data.
2753  *
2754  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2755  * sata_pkt was set-up.
2756  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2757  * exist and pkt_comp callback was scheduled.
2758  * Returns other TRAN_XXXXX values when error occured and command should be
2759  * rejected with the returned TRAN_XXXXX value.
2760  *
2761  * This function should be called with port mutex held.
2762  */
2763 static int
2764 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2765 {
2766 	sata_drive_info_t *sdinfo;
2767 	sata_device_t sata_device;
2768 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2769 		SATA_DIR_NODATA_XFER,
2770 		/* all other values to 0/FALSE */
2771 	};
2772 	/*
2773 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2774 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2775 	 * indicates that the scsi packet was not accepted (the reason will not
2776 	 * be checked by the scsi target driver).
2777 	 * To make debugging easier, we set pkt_reason to know value here.
2778 	 * It may be changed later when different completion reason is
2779 	 * determined.
2780 	 */
2781 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2782 	*reason = CMD_TRAN_ERR;
2783 
2784 	/* Validate address */
2785 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2786 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2787 
2788 	case -1:
2789 		/* Invalid address or invalid device type */
2790 		return (TRAN_BADPKT);
2791 	case 1:
2792 		/* valid address but no device - it has disappeared ? */
2793 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2794 		*reason = CMD_DEV_GONE;
2795 		/*
2796 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2797 		 * only in callback function (for normal requests) and
2798 		 * in the dump code path.
2799 		 * So, if the callback is available, we need to do
2800 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2801 		 */
2802 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2803 			/* scsi callback required */
2804 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2805 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2806 			    (void *)spx->txlt_scsi_pkt,
2807 			    TQ_SLEEP) == NULL)
2808 				/* Scheduling the callback failed */
2809 				return (TRAN_BUSY);
2810 
2811 			return (TRAN_ACCEPT);
2812 		}
2813 		return (TRAN_FATAL_ERROR);
2814 	default:
2815 		/* all OK; pkt reason will be overwritten later */
2816 		break;
2817 	}
2818 	/*
2819 	 * If in an interrupt context, reject packet if it is to be
2820 	 * executed in polling mode
2821 	 */
2822 	if (servicing_interrupt() &&
2823 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2824 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
2825 		    "sata_scsi_start: rejecting synchronous command because "
2826 		    "of interrupt context\n", NULL);
2827 		return (TRAN_BUSY);
2828 	}
2829 
2830 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2831 	    &spx->txlt_sata_pkt->satapkt_device);
2832 
2833 	/*
2834 	 * If device is in reset condition, reject the packet with
2835 	 * TRAN_BUSY, unless:
2836 	 * 1. system is panicking (dumping)
2837 	 * In such case only one thread is running and there is no way to
2838 	 * process reset.
2839 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2840 	 * Some cfgadm operations involve drive commands, so reset condition
2841 	 * needs to be ignored for IOCTL operations.
2842 	 */
2843 	if ((sdinfo->satadrv_event_flags &
2844 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2845 
2846 		if (!ddi_in_panic() &&
2847 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2848 		    sata_device.satadev_addr.cport) &
2849 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2850 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2851 			*reason = CMD_INCOMPLETE;
2852 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2853 			    "sata_scsi_start: rejecting command because "
2854 			    "of device reset state\n", NULL);
2855 			return (TRAN_BUSY);
2856 		}
2857 	}
2858 
2859 	/*
2860 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2861 	 * sata_scsi_pkt_init() because pkt init had to work also with
2862 	 * non-existing devices.
2863 	 * Now we know that the packet was set-up for a real device, so its
2864 	 * type is known.
2865 	 */
2866 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2867 
2868 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2869 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2870 	    sata_device.satadev_addr.cport)->cport_event_flags &
2871 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2872 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2873 		    sata_ignore_dev_reset = B_TRUE;
2874 	}
2875 	/*
2876 	 * At this point the generic translation routine determined that the
2877 	 * scsi packet should be accepted. Packet completion reason may be
2878 	 * changed later when a different completion reason is determined.
2879 	 */
2880 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2881 	*reason = CMD_CMPLT;
2882 
2883 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2884 		/* Synchronous execution */
2885 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2886 		    SATA_OPMODE_POLLING;
2887 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2888 		    sata_ignore_dev_reset = ddi_in_panic();
2889 	} else {
2890 		/* Asynchronous execution */
2891 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2892 		    SATA_OPMODE_INTERRUPTS;
2893 	}
2894 	/* Convert queuing information */
2895 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2896 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2897 		    B_TRUE;
2898 	else if (spx->txlt_scsi_pkt->pkt_flags &
2899 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2900 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2901 		    B_TRUE;
2902 
2903 	/* Always limit pkt time */
2904 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2905 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2906 	else
2907 		/* Pass on scsi_pkt time */
2908 		spx->txlt_sata_pkt->satapkt_time =
2909 		    spx->txlt_scsi_pkt->pkt_time;
2910 
2911 	return (TRAN_ACCEPT);
2912 }
2913 
2914 
2915 /*
2916  * Translate ATA Identify Device data to SCSI Inquiry data.
2917  * This function may be called only for ATA devices.
2918  * This function should not be called for ATAPI devices - they
2919  * respond directly to SCSI Inquiry command.
2920  *
2921  * SATA Identify Device data has to be valid in sata_drive_info.
2922  * Buffer has to accomodate the inquiry length (36 bytes).
2923  *
2924  * This function should be called with a port mutex held.
2925  */
2926 static	void
2927 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2928     sata_drive_info_t *sdinfo, uint8_t *buf)
2929 {
2930 
2931 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2932 	struct sata_id *sid = &sdinfo->satadrv_id;
2933 
2934 	/* Start with a nice clean slate */
2935 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2936 
2937 	/*
2938 	 * Rely on the dev_type for setting paripheral qualifier.
2939 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2940 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2941 	 * ATAPI Inquiry may provide more data to the target driver.
2942 	 */
2943 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2944 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2945 
2946 	/* CFA type device is not a removable media device */
2947 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
2948 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
2949 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2950 	inq->inq_iso = 0;	/* ISO version */
2951 	inq->inq_ecma = 0;	/* ECMA version */
2952 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2953 	inq->inq_aenc = 0;	/* Async event notification cap. */
2954 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2955 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2956 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2957 	inq->inq_len = 31;	/* Additional length */
2958 	inq->inq_dualp = 0;	/* dual port device - NO */
2959 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2960 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2961 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2962 				/*
2963 				 * Queuing support - controller has to
2964 				 * support some sort of command queuing.
2965 				 */
2966 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2967 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2968 	else
2969 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2970 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2971 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2972 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2973 
2974 #ifdef	_LITTLE_ENDIAN
2975 	/* Swap text fields to match SCSI format */
2976 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2977 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2978 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2979 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2980 	else
2981 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2982 #else	/* _LITTLE_ENDIAN */
2983 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2984 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2985 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2986 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2987 	else
2988 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2989 #endif	/* _LITTLE_ENDIAN */
2990 }
2991 
2992 
2993 /*
2994  * Scsi response set up for invalid command (command not supported)
2995  *
2996  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2997  */
2998 static int
2999 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3000 {
3001 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3002 	struct scsi_extended_sense *sense;
3003 
3004 	scsipkt->pkt_reason = CMD_CMPLT;
3005 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3006 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3007 
3008 	*scsipkt->pkt_scbp = STATUS_CHECK;
3009 
3010 	sense = sata_arq_sense(spx);
3011 	sense->es_key = KEY_ILLEGAL_REQUEST;
3012 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3013 
3014 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3015 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3016 
3017 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3018 	    scsipkt->pkt_comp != NULL)
3019 		/* scsi callback required */
3020 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3021 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3022 		    (void *)spx->txlt_scsi_pkt,
3023 		    TQ_SLEEP) == NULL)
3024 			/* Scheduling the callback failed */
3025 			return (TRAN_BUSY);
3026 	return (TRAN_ACCEPT);
3027 }
3028 
3029 /*
3030  * Scsi response setup for
3031  * emulated non-data command that requires no action/return data
3032  *
3033  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3034  */
3035 static 	int
3036 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3037 {
3038 	int rval;
3039 	int reason;
3040 
3041 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3042 
3043 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3044 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3045 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3046 		return (rval);
3047 	}
3048 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3049 
3050 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3051 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3052 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3053 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3054 
3055 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3056 	    "Scsi_pkt completion reason %x\n",
3057 	    spx->txlt_scsi_pkt->pkt_reason);
3058 
3059 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3060 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3061 		/* scsi callback required */
3062 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3063 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3064 		    (void *)spx->txlt_scsi_pkt,
3065 		    TQ_SLEEP) == NULL)
3066 			/* Scheduling the callback failed */
3067 			return (TRAN_BUSY);
3068 	return (TRAN_ACCEPT);
3069 }
3070 
3071 
3072 /*
3073  * SATA translate command: Inquiry / Identify Device
3074  * Use cached Identify Device data for now, rather than issuing actual
3075  * Device Identify cmd request. If device is detached and re-attached,
3076  * asynchromous event processing should fetch and refresh Identify Device
3077  * data.
3078  * Two VPD pages are supported now:
3079  * Vital Product Data page
3080  * Unit Serial Number page
3081  *
3082  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3083  */
3084 
3085 #define	EVPD			1	/* Extended Vital Product Data flag */
3086 #define	CMDDT			2	/* Command Support Data - Obsolete */
3087 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3088 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3089 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3090 
3091 static int
3092 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3093 {
3094 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3095 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3096 	sata_drive_info_t *sdinfo;
3097 	struct scsi_extended_sense *sense;
3098 	int count;
3099 	uint8_t *p;
3100 	int i, j;
3101 	uint8_t page_buf[0xff]; /* Max length */
3102 	int rval, reason;
3103 
3104 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3105 
3106 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3107 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3108 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3109 		return (rval);
3110 	}
3111 
3112 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3113 	    &spx->txlt_sata_pkt->satapkt_device);
3114 
3115 	ASSERT(sdinfo != NULL);
3116 
3117 	scsipkt->pkt_reason = CMD_CMPLT;
3118 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3119 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3120 
3121 	/* Reject not supported request */
3122 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3123 		*scsipkt->pkt_scbp = STATUS_CHECK;
3124 		sense = sata_arq_sense(spx);
3125 		sense->es_key = KEY_ILLEGAL_REQUEST;
3126 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3127 		goto done;
3128 	}
3129 
3130 	/* Valid Inquiry request */
3131 	*scsipkt->pkt_scbp = STATUS_GOOD;
3132 
3133 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3134 
3135 		/*
3136 		 * Because it is fully emulated command storing data
3137 		 * programatically in the specified buffer, release
3138 		 * preallocated DMA resources before storing data in the buffer,
3139 		 * so no unwanted DMA sync would take place.
3140 		 */
3141 		sata_scsi_dmafree(NULL, scsipkt);
3142 
3143 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3144 			/* Standard Inquiry Data request */
3145 			struct scsi_inquiry inq;
3146 			unsigned int bufsize;
3147 
3148 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3149 			    sdinfo, (uint8_t *)&inq);
3150 			/* Copy no more than requested */
3151 			count = MIN(bp->b_bcount,
3152 			    sizeof (struct scsi_inquiry));
3153 			bufsize = scsipkt->pkt_cdbp[4];
3154 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3155 			count = MIN(count, bufsize);
3156 			bcopy(&inq, bp->b_un.b_addr, count);
3157 
3158 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3159 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3160 			    bufsize - count : 0;
3161 		} else {
3162 			/*
3163 			 * peripheral_qualifier = 0;
3164 			 *
3165 			 * We are dealing only with HD and will be
3166 			 * dealing with CD/DVD devices soon
3167 			 */
3168 			uint8_t peripheral_device_type =
3169 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3170 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3171 
3172 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3173 			case INQUIRY_SUP_VPD_PAGE:
3174 				/*
3175 				 * Request for suported Vital Product Data
3176 				 * pages - assuming only 2 page codes
3177 				 * supported.
3178 				 */
3179 				page_buf[0] = peripheral_device_type;
3180 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3181 				page_buf[2] = 0;
3182 				page_buf[3] = 2; /* page length */
3183 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3184 				page_buf[5] = INQUIRY_USN_PAGE;
3185 				/* Copy no more than requested */
3186 				count = MIN(bp->b_bcount, 6);
3187 				bcopy(page_buf, bp->b_un.b_addr, count);
3188 				break;
3189 
3190 			case INQUIRY_USN_PAGE:
3191 				/*
3192 				 * Request for Unit Serial Number page.
3193 				 * Set-up the page.
3194 				 */
3195 				page_buf[0] = peripheral_device_type;
3196 				page_buf[1] = INQUIRY_USN_PAGE;
3197 				page_buf[2] = 0;
3198 				/* remaining page length */
3199 				page_buf[3] = SATA_ID_SERIAL_LEN;
3200 
3201 				/*
3202 				 * Copy serial number from Identify Device data
3203 				 * words into the inquiry page and swap bytes
3204 				 * when necessary.
3205 				 */
3206 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3207 #ifdef	_LITTLE_ENDIAN
3208 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3209 #else
3210 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3211 #endif
3212 				/*
3213 				 * Least significant character of the serial
3214 				 * number shall appear as the last byte,
3215 				 * according to SBC-3 spec.
3216 				 * Count trailing spaces to determine the
3217 				 * necessary shift length.
3218 				 */
3219 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3220 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3221 					if (*(p - j) != '\0' &&
3222 					    *(p - j) != '\040')
3223 						break;
3224 				}
3225 
3226 				/*
3227 				 * Shift SN string right, so that the last
3228 				 * non-blank character would appear in last
3229 				 * byte of SN field in the page.
3230 				 * 'j' is the shift length.
3231 				 */
3232 				for (i = 0;
3233 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3234 				    i++, p--)
3235 					*p = *(p - j);
3236 
3237 				/*
3238 				 * Add leading spaces - same number as the
3239 				 * shift size
3240 				 */
3241 				for (; j > 0; j--)
3242 					page_buf[4 + j - 1] = '\040';
3243 
3244 				count = MIN(bp->b_bcount,
3245 				    SATA_ID_SERIAL_LEN + 4);
3246 				bcopy(page_buf, bp->b_un.b_addr, count);
3247 				break;
3248 
3249 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3250 				/*
3251 				 * We may want to implement this page, when
3252 				 * identifiers are common for SATA devices
3253 				 * But not now.
3254 				 */
3255 				/*FALLTHROUGH*/
3256 
3257 			default:
3258 				/* Request for unsupported VPD page */
3259 				*scsipkt->pkt_scbp = STATUS_CHECK;
3260 				sense = sata_arq_sense(spx);
3261 				sense->es_key = KEY_ILLEGAL_REQUEST;
3262 				sense->es_add_code =
3263 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3264 				goto done;
3265 			}
3266 		}
3267 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3268 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3269 		    scsipkt->pkt_cdbp[4] - count : 0;
3270 	}
3271 done:
3272 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3273 
3274 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3275 	    "Scsi_pkt completion reason %x\n",
3276 	    scsipkt->pkt_reason);
3277 
3278 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3279 	    scsipkt->pkt_comp != NULL) {
3280 		/* scsi callback required */
3281 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3282 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3283 		    TQ_SLEEP) == NULL)
3284 			/* Scheduling the callback failed */
3285 			return (TRAN_BUSY);
3286 	}
3287 	return (TRAN_ACCEPT);
3288 }
3289 
3290 /*
3291  * SATA translate command: Request Sense.
3292  * Emulated command (ATA version for SATA hard disks)
3293  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3294  *
3295  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3296  *
3297  * Note: There is a mismatch between already implemented Informational
3298  * Exception Mode Select page 0x1C and this function.
3299  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3300  * NO SENSE and set additional sense code to the exception code - this is not
3301  * implemented here.
3302  */
3303 static int
3304 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3305 {
3306 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3307 	struct scsi_extended_sense sense;
3308 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3309 	int rval, reason;
3310 
3311 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3312 
3313 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3314 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3315 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3316 		return (rval);
3317 	}
3318 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3319 
3320 
3321 	scsipkt->pkt_reason = CMD_CMPLT;
3322 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3323 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3324 	*scsipkt->pkt_scbp = STATUS_GOOD;
3325 
3326 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3327 		/*
3328 		 * Because it is fully emulated command storing data
3329 		 * programatically in the specified buffer, release
3330 		 * preallocated DMA resources before storing data in the buffer,
3331 		 * so no unwanted DMA sync would take place.
3332 		 */
3333 		int count = MIN(bp->b_bcount,
3334 		    sizeof (struct scsi_extended_sense));
3335 		sata_scsi_dmafree(NULL, scsipkt);
3336 		bzero(&sense, sizeof (struct scsi_extended_sense));
3337 		sense.es_valid = 0;	/* Valid LBA */
3338 		sense.es_class = 7;	/* Response code 0x70 - current err */
3339 		sense.es_key = KEY_NO_SENSE;
3340 		sense.es_add_len = 6;	/* Additional length */
3341 		/* Copy no more than requested */
3342 		bcopy(&sense, bp->b_un.b_addr, count);
3343 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3344 		scsipkt->pkt_resid = 0;
3345 	}
3346 
3347 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3348 	    "Scsi_pkt completion reason %x\n",
3349 	    scsipkt->pkt_reason);
3350 
3351 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3352 	    scsipkt->pkt_comp != NULL)
3353 		/* scsi callback required */
3354 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3355 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3356 		    TQ_SLEEP) == NULL)
3357 			/* Scheduling the callback failed */
3358 			return (TRAN_BUSY);
3359 	return (TRAN_ACCEPT);
3360 }
3361 
3362 /*
3363  * SATA translate command: Test Unit Ready
3364  * At the moment this is an emulated command (ATA version for SATA hard disks).
3365  * May be translated into Check Power Mode command in the future
3366  *
3367  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3368  */
3369 static int
3370 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3371 {
3372 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3373 	struct scsi_extended_sense *sense;
3374 	int power_state;
3375 	int rval, reason;
3376 
3377 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3378 
3379 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3380 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3381 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3382 		return (rval);
3383 	}
3384 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3385 
3386 	/* At this moment, emulate it rather than execute anything */
3387 	power_state = SATA_PWRMODE_ACTIVE;
3388 
3389 	scsipkt->pkt_reason = CMD_CMPLT;
3390 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3391 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3392 
3393 	switch (power_state) {
3394 	case SATA_PWRMODE_ACTIVE:
3395 	case SATA_PWRMODE_IDLE:
3396 		*scsipkt->pkt_scbp = STATUS_GOOD;
3397 		break;
3398 	default:
3399 		/* PWR mode standby */
3400 		*scsipkt->pkt_scbp = STATUS_CHECK;
3401 		sense = sata_arq_sense(spx);
3402 		sense->es_key = KEY_NOT_READY;
3403 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3404 		break;
3405 	}
3406 
3407 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3408 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3409 
3410 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3411 	    scsipkt->pkt_comp != NULL)
3412 		/* scsi callback required */
3413 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3414 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3415 		    TQ_SLEEP) == NULL)
3416 			/* Scheduling the callback failed */
3417 			return (TRAN_BUSY);
3418 
3419 	return (TRAN_ACCEPT);
3420 }
3421 
3422 
3423 /*
3424  * SATA translate command: Start Stop Unit
3425  * Translation depends on a command:
3426  *	Start Unit translated into Idle Immediate
3427  *	Stop Unit translated into Standby Immediate
3428  *	Unload Media / NOT SUPPORTED YET
3429  *	Load Media / NOT SUPPROTED YET
3430  * Power condition bits are ignored, so is Immediate bit
3431  * Requesting synchronous execution.
3432  *
3433  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3434  * appropriate values in scsi_pkt fields.
3435  */
3436 static int
3437 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3438 {
3439 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3440 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3441 	struct scsi_extended_sense *sense;
3442 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3443 	int cport = SATA_TXLT_CPORT(spx);
3444 	int rval, reason;
3445 	int synch;
3446 
3447 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3448 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3449 
3450 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3451 
3452 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3453 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3454 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3455 		return (rval);
3456 	}
3457 
3458 	if (scsipkt->pkt_cdbp[4] & 2) {
3459 		/* Load/Unload Media - invalid request */
3460 		*scsipkt->pkt_scbp = STATUS_CHECK;
3461 		sense = sata_arq_sense(spx);
3462 		sense->es_key = KEY_ILLEGAL_REQUEST;
3463 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3464 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3465 
3466 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3467 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3468 
3469 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3470 		    scsipkt->pkt_comp != NULL)
3471 			/* scsi callback required */
3472 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3473 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3474 			    TQ_SLEEP) == NULL)
3475 				/* Scheduling the callback failed */
3476 				return (TRAN_BUSY);
3477 
3478 		return (TRAN_ACCEPT);
3479 	}
3480 	scmd->satacmd_addr_type = 0;
3481 	scmd->satacmd_sec_count_lsb = 0;
3482 	scmd->satacmd_lba_low_lsb = 0;
3483 	scmd->satacmd_lba_mid_lsb = 0;
3484 	scmd->satacmd_lba_high_lsb = 0;
3485 	scmd->satacmd_features_reg = 0;
3486 	scmd->satacmd_device_reg = 0;
3487 	scmd->satacmd_status_reg = 0;
3488 	if (scsipkt->pkt_cdbp[4] & 1) {
3489 		/* Start Unit */
3490 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3491 	} else {
3492 		/* Stop Unit */
3493 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3494 	}
3495 
3496 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3497 		/* Need to set-up a callback function */
3498 		spx->txlt_sata_pkt->satapkt_comp =
3499 		    sata_txlt_nodata_cmd_completion;
3500 		synch = FALSE;
3501 	} else {
3502 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3503 		synch = TRUE;
3504 	}
3505 
3506 	/* Transfer command to HBA */
3507 	if (sata_hba_start(spx, &rval) != 0) {
3508 		/* Pkt not accepted for execution */
3509 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3510 		return (rval);
3511 	}
3512 
3513 	/*
3514 	 * If execution is non-synchronous,
3515 	 * a callback function will handle potential errors, translate
3516 	 * the response and will do a callback to a target driver.
3517 	 * If it was synchronous, check execution status using the same
3518 	 * framework callback.
3519 	 */
3520 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3521 	if (synch) {
3522 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3523 		    "synchronous execution status %x\n",
3524 		    spx->txlt_sata_pkt->satapkt_reason);
3525 
3526 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3527 	}
3528 	return (TRAN_ACCEPT);
3529 
3530 }
3531 
3532 
3533 /*
3534  * SATA translate command:  Read Capacity.
3535  * Emulated command for SATA disks.
3536  * Capacity is retrieved from cached Idenifty Device data.
3537  * Identify Device data shows effective disk capacity, not the native
3538  * capacity, which may be limitted by Set Max Address command.
3539  * This is ATA version for SATA hard disks.
3540  *
3541  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3542  */
3543 static int
3544 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3545 {
3546 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3547 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3548 	sata_drive_info_t *sdinfo;
3549 	uint64_t val;
3550 	uchar_t *rbuf;
3551 	int rval, reason;
3552 
3553 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3554 	    "sata_txlt_read_capacity: ", NULL);
3555 
3556 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3557 
3558 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3559 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3560 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3561 		return (rval);
3562 	}
3563 
3564 	scsipkt->pkt_reason = CMD_CMPLT;
3565 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3566 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3567 	*scsipkt->pkt_scbp = STATUS_GOOD;
3568 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3569 		/*
3570 		 * Because it is fully emulated command storing data
3571 		 * programatically in the specified buffer, release
3572 		 * preallocated DMA resources before storing data in the buffer,
3573 		 * so no unwanted DMA sync would take place.
3574 		 */
3575 		sata_scsi_dmafree(NULL, scsipkt);
3576 
3577 		sdinfo = sata_get_device_info(
3578 		    spx->txlt_sata_hba_inst,
3579 		    &spx->txlt_sata_pkt->satapkt_device);
3580 		/* Last logical block address */
3581 		val = sdinfo->satadrv_capacity - 1;
3582 		rbuf = (uchar_t *)bp->b_un.b_addr;
3583 		/* Need to swap endians to match scsi format */
3584 		rbuf[0] = (val >> 24) & 0xff;
3585 		rbuf[1] = (val >> 16) & 0xff;
3586 		rbuf[2] = (val >> 8) & 0xff;
3587 		rbuf[3] = val & 0xff;
3588 		/* block size - always 512 bytes, for now */
3589 		rbuf[4] = 0;
3590 		rbuf[5] = 0;
3591 		rbuf[6] = 0x02;
3592 		rbuf[7] = 0;
3593 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3594 		scsipkt->pkt_resid = 0;
3595 
3596 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3597 		    sdinfo->satadrv_capacity -1);
3598 	}
3599 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3600 	/*
3601 	 * If a callback was requested, do it now.
3602 	 */
3603 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3604 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3605 
3606 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3607 	    scsipkt->pkt_comp != NULL)
3608 		/* scsi callback required */
3609 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3610 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3611 		    TQ_SLEEP) == NULL)
3612 			/* Scheduling the callback failed */
3613 			return (TRAN_BUSY);
3614 
3615 	return (TRAN_ACCEPT);
3616 }
3617 
3618 /*
3619  * SATA translate command: Mode Sense.
3620  * Translated into appropriate SATA command or emulated.
3621  * Saved Values Page Control (03) are not supported.
3622  *
3623  * NOTE: only caching mode sense page is currently implemented.
3624  *
3625  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3626  */
3627 
3628 static int
3629 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3630 {
3631 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3632 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3633 	sata_drive_info_t *sdinfo;
3634 	sata_id_t *sata_id;
3635 	struct scsi_extended_sense *sense;
3636 	int 		len, bdlen, count, alc_len;
3637 	int		pc;	/* Page Control code */
3638 	uint8_t		*buf;	/* mode sense buffer */
3639 	int		rval, reason;
3640 
3641 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3642 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3643 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3644 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3645 
3646 	buf = kmem_zalloc(1024, KM_SLEEP);
3647 
3648 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3649 
3650 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3651 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3652 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3653 		kmem_free(buf, 1024);
3654 		return (rval);
3655 	}
3656 
3657 	scsipkt->pkt_reason = CMD_CMPLT;
3658 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3659 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3660 
3661 	pc = scsipkt->pkt_cdbp[2] >> 6;
3662 
3663 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3664 		/*
3665 		 * Because it is fully emulated command storing data
3666 		 * programatically in the specified buffer, release
3667 		 * preallocated DMA resources before storing data in the buffer,
3668 		 * so no unwanted DMA sync would take place.
3669 		 */
3670 		sata_scsi_dmafree(NULL, scsipkt);
3671 
3672 		len = 0;
3673 		bdlen = 0;
3674 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3675 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3676 			    (scsipkt->pkt_cdbp[0] & 0x10))
3677 				bdlen = 16;
3678 			else
3679 				bdlen = 8;
3680 		}
3681 		/* Build mode parameter header */
3682 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3683 			/* 4-byte mode parameter header */
3684 			buf[len++] = 0;   	/* mode data length */
3685 			buf[len++] = 0;		/* medium type */
3686 			buf[len++] = 0;		/* dev-specific param */
3687 			buf[len++] = bdlen;	/* Block Descriptor length */
3688 		} else {
3689 			/* 8-byte mode parameter header */
3690 			buf[len++] = 0;		/* mode data length */
3691 			buf[len++] = 0;
3692 			buf[len++] = 0;		/* medium type */
3693 			buf[len++] = 0;		/* dev-specific param */
3694 			if (bdlen == 16)
3695 				buf[len++] = 1;	/* long lba descriptor */
3696 			else
3697 				buf[len++] = 0;
3698 			buf[len++] = 0;
3699 			buf[len++] = 0;		/* Block Descriptor length */
3700 			buf[len++] = bdlen;
3701 		}
3702 
3703 		sdinfo = sata_get_device_info(
3704 		    spx->txlt_sata_hba_inst,
3705 		    &spx->txlt_sata_pkt->satapkt_device);
3706 
3707 		/* Build block descriptor only if not disabled (DBD) */
3708 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3709 			/* Block descriptor - direct-access device format */
3710 			if (bdlen == 8) {
3711 				/* build regular block descriptor */
3712 				buf[len++] =
3713 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3714 				buf[len++] =
3715 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3716 				buf[len++] =
3717 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3718 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3719 				buf[len++] = 0; /* density code */
3720 				buf[len++] = 0;
3721 				if (sdinfo->satadrv_type ==
3722 				    SATA_DTYPE_ATADISK)
3723 					buf[len++] = 2;
3724 				else
3725 					/* ATAPI */
3726 					buf[len++] = 8;
3727 				buf[len++] = 0;
3728 			} else if (bdlen == 16) {
3729 				/* Long LBA Accepted */
3730 				/* build long lba block descriptor */
3731 #ifndef __lock_lint
3732 				buf[len++] =
3733 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3734 				buf[len++] =
3735 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3736 				buf[len++] =
3737 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3738 				buf[len++] =
3739 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3740 #endif
3741 				buf[len++] =
3742 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3743 				buf[len++] =
3744 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3745 				buf[len++] =
3746 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3747 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3748 				buf[len++] = 0;
3749 				buf[len++] = 0; /* density code */
3750 				buf[len++] = 0;
3751 				buf[len++] = 0;
3752 				if (sdinfo->satadrv_type ==
3753 				    SATA_DTYPE_ATADISK)
3754 					buf[len++] = 2;
3755 				else
3756 					/* ATAPI */
3757 					buf[len++] = 8;
3758 				buf[len++] = 0;
3759 			}
3760 		}
3761 
3762 		sata_id = &sdinfo->satadrv_id;
3763 
3764 		/*
3765 		 * Add requested pages.
3766 		 * Page 3 and 4 are obsolete and we are not supporting them.
3767 		 * We deal now with:
3768 		 * caching (read/write cache control).
3769 		 * We should eventually deal with following mode pages:
3770 		 * error recovery  (0x01),
3771 		 * power condition (0x1a),
3772 		 * exception control page (enables SMART) (0x1c),
3773 		 * enclosure management (ses),
3774 		 * protocol-specific port mode (port control).
3775 		 */
3776 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3777 		case MODEPAGE_RW_ERRRECOV:
3778 			/* DAD_MODE_ERR_RECOV */
3779 			/* R/W recovery */
3780 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3781 			break;
3782 		case MODEPAGE_CACHING:
3783 			/* DAD_MODE_CACHE */
3784 			/* Reject not supported request for saved parameters */
3785 			if (pc == 3) {
3786 				*scsipkt->pkt_scbp = STATUS_CHECK;
3787 				sense = sata_arq_sense(spx);
3788 				sense->es_key = KEY_ILLEGAL_REQUEST;
3789 				sense->es_add_code =
3790 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3791 				goto done;
3792 			}
3793 
3794 			/* caching */
3795 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3796 			break;
3797 		case MODEPAGE_INFO_EXCPT:
3798 			/* exception cntrl */
3799 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3800 				len += sata_build_msense_page_1c(sdinfo, pc,
3801 				    buf+len);
3802 			}
3803 			else
3804 				goto err;
3805 			break;
3806 		case MODEPAGE_POWER_COND:
3807 			/* DAD_MODE_POWER_COND */
3808 			/* power condition */
3809 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3810 			break;
3811 
3812 		case MODEPAGE_ACOUSTIC_MANAG:
3813 			/* acoustic management */
3814 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3815 			break;
3816 		case MODEPAGE_ALLPAGES:
3817 			/* all pages */
3818 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3819 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3820 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3821 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3822 				len += sata_build_msense_page_1c(sdinfo, pc,
3823 				    buf+len);
3824 			}
3825 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3826 			break;
3827 		default:
3828 		err:
3829 			/* Invalid request */
3830 			*scsipkt->pkt_scbp = STATUS_CHECK;
3831 			sense = sata_arq_sense(spx);
3832 			sense->es_key = KEY_ILLEGAL_REQUEST;
3833 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3834 			goto done;
3835 		}
3836 
3837 		/* fix total mode data length */
3838 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3839 			/* 4-byte mode parameter header */
3840 			buf[0] = len - 1;   	/* mode data length */
3841 		} else {
3842 			buf[0] = (len -2) >> 8;
3843 			buf[1] = (len -2) & 0xff;
3844 		}
3845 
3846 
3847 		/* Check allocation length */
3848 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3849 			alc_len = scsipkt->pkt_cdbp[4];
3850 		} else {
3851 			alc_len = scsipkt->pkt_cdbp[7];
3852 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3853 		}
3854 		/*
3855 		 * We do not check for possible parameters truncation
3856 		 * (alc_len < len) assuming that the target driver works
3857 		 * correctly. Just avoiding overrun.
3858 		 * Copy no more than requested and possible, buffer-wise.
3859 		 */
3860 		count = MIN(alc_len, len);
3861 		count = MIN(bp->b_bcount, count);
3862 		bcopy(buf, bp->b_un.b_addr, count);
3863 
3864 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3865 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3866 	}
3867 	*scsipkt->pkt_scbp = STATUS_GOOD;
3868 done:
3869 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3870 	(void) kmem_free(buf, 1024);
3871 
3872 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3873 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3874 
3875 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3876 	    scsipkt->pkt_comp != NULL)
3877 		/* scsi callback required */
3878 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3879 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3880 		    TQ_SLEEP) == NULL)
3881 			/* Scheduling the callback failed */
3882 			return (TRAN_BUSY);
3883 
3884 	return (TRAN_ACCEPT);
3885 }
3886 
3887 
3888 /*
3889  * SATA translate command: Mode Select.
3890  * Translated into appropriate SATA command or emulated.
3891  * Saving parameters is not supported.
3892  * Changing device capacity is not supported (although theoretically
3893  * possible by executing SET FEATURES/SET MAX ADDRESS)
3894  *
3895  * Assumption is that the target driver is working correctly.
3896  *
3897  * More than one SATA command may be executed to perform operations specified
3898  * by mode select pages. The first error terminates further execution.
3899  * Operations performed successully are not backed-up in such case.
3900  *
3901  * NOTE: Implemented pages:
3902  * - caching page
3903  * - informational exception page
3904  * - acoustic management page
3905  * Caching setup is remembered so it could be re-stored in case of
3906  * an unexpected device reset.
3907  *
3908  * Returns TRAN_XXXX.
3909  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
3910  */
3911 
3912 static int
3913 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3914 {
3915 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3916 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3917 	struct scsi_extended_sense *sense;
3918 	int len, pagelen, count, pllen;
3919 	uint8_t *buf;	/* mode select buffer */
3920 	int rval, stat, reason;
3921 	uint_t nointr_flag;
3922 	int dmod = 0;
3923 
3924 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3925 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3926 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3927 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3928 
3929 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3930 
3931 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3932 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3933 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3934 		return (rval);
3935 	}
3936 	/*
3937 	 * If in interrupt context, reject this packet because it may result
3938 	 * in issuing a synchronous command to HBA.
3939 	 */
3940 	if (servicing_interrupt()) {
3941 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3942 		    "sata_txlt_mode_select: rejecting command because "
3943 		    "of interrupt context\n", NULL);
3944 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3945 		return (TRAN_BUSY);
3946 	}
3947 
3948 	rval = TRAN_ACCEPT;
3949 
3950 	scsipkt->pkt_reason = CMD_CMPLT;
3951 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3952 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3953 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3954 
3955 	/* Reject not supported request */
3956 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3957 		*scsipkt->pkt_scbp = STATUS_CHECK;
3958 		sense = sata_arq_sense(spx);
3959 		sense->es_key = KEY_ILLEGAL_REQUEST;
3960 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3961 		goto done;
3962 	}
3963 
3964 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3965 		pllen = scsipkt->pkt_cdbp[4];
3966 	} else {
3967 		pllen = scsipkt->pkt_cdbp[7];
3968 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3969 	}
3970 
3971 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3972 
3973 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3974 		buf = (uint8_t *)bp->b_un.b_addr;
3975 		count = MIN(bp->b_bcount, pllen);
3976 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3977 		scsipkt->pkt_resid = 0;
3978 		pllen = count;
3979 
3980 		/*
3981 		 * Check the header to skip the block descriptor(s) - we
3982 		 * do not support setting device capacity.
3983 		 * Existing macros do not recognize long LBA dscriptor,
3984 		 * hence manual calculation.
3985 		 */
3986 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3987 			/* 6-bytes CMD, 4 bytes header */
3988 			if (count <= 4)
3989 				goto done;		/* header only */
3990 			len = buf[3] + 4;
3991 		} else {
3992 			/* 10-bytes CMD, 8 bytes header */
3993 			if (count <= 8)
3994 				goto done;		/* header only */
3995 			len = buf[6];
3996 			len = (len << 8) + buf[7] + 8;
3997 		}
3998 		if (len >= count)
3999 			goto done;	/* header + descriptor(s) only */
4000 
4001 		pllen -= len;		/* remaining data length */
4002 
4003 		/*
4004 		 * We may be executing SATA command and want to execute it
4005 		 * in SYNCH mode, regardless of scsi_pkt setting.
4006 		 * Save scsi_pkt setting and indicate SYNCH mode
4007 		 */
4008 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4009 		    scsipkt->pkt_comp != NULL) {
4010 			scsipkt->pkt_flags |= FLAG_NOINTR;
4011 		}
4012 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4013 
4014 		/*
4015 		 * len is now the offset to a first mode select page
4016 		 * Process all pages
4017 		 */
4018 		while (pllen > 0) {
4019 			switch ((int)buf[len]) {
4020 			case MODEPAGE_CACHING:
4021 				/* No support for SP (saving) */
4022 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4023 					*scsipkt->pkt_scbp = STATUS_CHECK;
4024 					sense = sata_arq_sense(spx);
4025 					sense->es_key = KEY_ILLEGAL_REQUEST;
4026 					sense->es_add_code =
4027 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4028 					goto done;
4029 				}
4030 				stat = sata_mode_select_page_8(spx,
4031 				    (struct mode_cache_scsi3 *)&buf[len],
4032 				    pllen, &pagelen, &rval, &dmod);
4033 				/*
4034 				 * The pagelen value indicates the number of
4035 				 * parameter bytes already processed.
4036 				 * The rval is the return value from
4037 				 * sata_tran_start().
4038 				 * The stat indicates the overall status of
4039 				 * the operation(s).
4040 				 */
4041 				if (stat != SATA_SUCCESS)
4042 					/*
4043 					 * Page processing did not succeed -
4044 					 * all error info is already set-up,
4045 					 * just return
4046 					 */
4047 					pllen = 0; /* this breaks the loop */
4048 				else {
4049 					len += pagelen;
4050 					pllen -= pagelen;
4051 				}
4052 				break;
4053 
4054 			case MODEPAGE_INFO_EXCPT:
4055 				stat = sata_mode_select_page_1c(spx,
4056 				    (struct mode_info_excpt_page *)&buf[len],
4057 				    pllen, &pagelen, &rval, &dmod);
4058 				/*
4059 				 * The pagelen value indicates the number of
4060 				 * parameter bytes already processed.
4061 				 * The rval is the return value from
4062 				 * sata_tran_start().
4063 				 * The stat indicates the overall status of
4064 				 * the operation(s).
4065 				 */
4066 				if (stat != SATA_SUCCESS)
4067 					/*
4068 					 * Page processing did not succeed -
4069 					 * all error info is already set-up,
4070 					 * just return
4071 					 */
4072 					pllen = 0; /* this breaks the loop */
4073 				else {
4074 					len += pagelen;
4075 					pllen -= pagelen;
4076 				}
4077 				break;
4078 
4079 			case MODEPAGE_ACOUSTIC_MANAG:
4080 				stat = sata_mode_select_page_30(spx,
4081 				    (struct mode_acoustic_management *)
4082 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4083 				/*
4084 				 * The pagelen value indicates the number of
4085 				 * parameter bytes already processed.
4086 				 * The rval is the return value from
4087 				 * sata_tran_start().
4088 				 * The stat indicates the overall status of
4089 				 * the operation(s).
4090 				 */
4091 				if (stat != SATA_SUCCESS)
4092 					/*
4093 					 * Page processing did not succeed -
4094 					 * all error info is already set-up,
4095 					 * just return
4096 					 */
4097 					pllen = 0; /* this breaks the loop */
4098 				else {
4099 					len += pagelen;
4100 					pllen -= pagelen;
4101 				}
4102 
4103 				break;
4104 			default:
4105 				*scsipkt->pkt_scbp = STATUS_CHECK;
4106 				sense = sata_arq_sense(spx);
4107 				sense->es_key = KEY_ILLEGAL_REQUEST;
4108 				sense->es_add_code =
4109 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4110 				goto done;
4111 			}
4112 		}
4113 	}
4114 done:
4115 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4116 	/*
4117 	 * If device parameters were modified, fetch and store the new
4118 	 * Identify Device data. Since port mutex could have been released
4119 	 * for accessing HBA driver, we need to re-check device existence.
4120 	 */
4121 	if (dmod != 0) {
4122 		sata_drive_info_t new_sdinfo, *sdinfo;
4123 		int rv = 0;
4124 
4125 		/*
4126 		 * Following statement has to be changed if this function is
4127 		 * used for devices other than SATA hard disks.
4128 		 */
4129 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4130 
4131 		new_sdinfo.satadrv_addr =
4132 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4133 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4134 		    &new_sdinfo);
4135 
4136 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4137 		/*
4138 		 * Since port mutex could have been released when
4139 		 * accessing HBA driver, we need to re-check that the
4140 		 * framework still holds the device info structure.
4141 		 */
4142 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4143 		    &spx->txlt_sata_pkt->satapkt_device);
4144 		if (sdinfo != NULL) {
4145 			/*
4146 			 * Device still has info structure in the
4147 			 * sata framework. Copy newly fetched info
4148 			 */
4149 			if (rv == 0) {
4150 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4151 				sata_save_drive_settings(sdinfo);
4152 			} else {
4153 				/*
4154 				 * Could not fetch new data - invalidate
4155 				 * sata_drive_info. That makes device
4156 				 * unusable.
4157 				 */
4158 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4159 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4160 			}
4161 		}
4162 		if (rv != 0 || sdinfo == NULL) {
4163 			/*
4164 			 * This changes the overall mode select completion
4165 			 * reason to a failed one !!!!!
4166 			 */
4167 			*scsipkt->pkt_scbp = STATUS_CHECK;
4168 			sense = sata_arq_sense(spx);
4169 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4170 			rval = TRAN_ACCEPT;
4171 		}
4172 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4173 	}
4174 	/* Restore the scsi pkt flags */
4175 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4176 	scsipkt->pkt_flags |= nointr_flag;
4177 
4178 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4179 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4180 
4181 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4182 	    scsipkt->pkt_comp != NULL)
4183 		/* scsi callback required */
4184 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4185 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4186 		    TQ_SLEEP) == NULL)
4187 			/* Scheduling the callback failed */
4188 			return (TRAN_BUSY);
4189 
4190 	return (rval);
4191 }
4192 
4193 
4194 
4195 /*
4196  * Translate command: Log Sense
4197  */
4198 static 	int
4199 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4200 {
4201 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4202 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4203 	sata_drive_info_t *sdinfo;
4204 	struct scsi_extended_sense *sense;
4205 	int 		len, count, alc_len;
4206 	int		pc;	/* Page Control code */
4207 	int		page_code;	/* Page code */
4208 	uint8_t		*buf;	/* log sense buffer */
4209 	int		rval, reason;
4210 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4211 
4212 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4213 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4214 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4215 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4216 
4217 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4218 
4219 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4220 
4221 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4222 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4223 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4224 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4225 		return (rval);
4226 	}
4227 	/*
4228 	 * If in interrupt context, reject this packet because it may result
4229 	 * in issuing a synchronous command to HBA.
4230 	 */
4231 	if (servicing_interrupt()) {
4232 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4233 		    "sata_log_sense: rejecting command because "
4234 		    "of interrupt context\n", NULL);
4235 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4236 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4237 		return (TRAN_BUSY);
4238 	}
4239 
4240 	scsipkt->pkt_reason = CMD_CMPLT;
4241 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4242 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4243 
4244 	pc = scsipkt->pkt_cdbp[2] >> 6;
4245 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4246 
4247 	/* Reject not supported request for all but cumulative values */
4248 	switch (pc) {
4249 	case PC_CUMULATIVE_VALUES:
4250 		break;
4251 	default:
4252 		*scsipkt->pkt_scbp = STATUS_CHECK;
4253 		sense = sata_arq_sense(spx);
4254 		sense->es_key = KEY_ILLEGAL_REQUEST;
4255 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4256 		goto done;
4257 	}
4258 
4259 	switch (page_code) {
4260 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4261 	case PAGE_CODE_SELF_TEST_RESULTS:
4262 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4263 	case PAGE_CODE_SMART_READ_DATA:
4264 		break;
4265 	default:
4266 		*scsipkt->pkt_scbp = STATUS_CHECK;
4267 		sense = sata_arq_sense(spx);
4268 		sense->es_key = KEY_ILLEGAL_REQUEST;
4269 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4270 		goto done;
4271 	}
4272 
4273 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4274 		/*
4275 		 * Because log sense uses local buffers for data retrieval from
4276 		 * the devices and sets the data programatically in the
4277 		 * original specified buffer, release preallocated DMA
4278 		 * resources before storing data in the original buffer,
4279 		 * so no unwanted DMA sync would take place.
4280 		 */
4281 		sata_id_t *sata_id;
4282 
4283 		sata_scsi_dmafree(NULL, scsipkt);
4284 
4285 		len = 0;
4286 
4287 		/* Build log parameter header */
4288 		buf[len++] = page_code;	/* page code as in the CDB */
4289 		buf[len++] = 0;		/* reserved */
4290 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4291 		buf[len++] = 0;		/* (LSB) */
4292 
4293 		sdinfo = sata_get_device_info(
4294 		    spx->txlt_sata_hba_inst,
4295 		    &spx->txlt_sata_pkt->satapkt_device);
4296 
4297 		/*
4298 		 * Add requested pages.
4299 		 */
4300 		switch (page_code) {
4301 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4302 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4303 			break;
4304 		case PAGE_CODE_SELF_TEST_RESULTS:
4305 			sata_id = &sdinfo->satadrv_id;
4306 			if ((! (sata_id->ai_cmdset84 &
4307 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4308 			    (! (sata_id->ai_features87 &
4309 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4310 				*scsipkt->pkt_scbp = STATUS_CHECK;
4311 				sense = sata_arq_sense(spx);
4312 				sense->es_key = KEY_ILLEGAL_REQUEST;
4313 				sense->es_add_code =
4314 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4315 
4316 				goto done;
4317 			}
4318 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4319 			    spx->txlt_sata_hba_inst);
4320 			break;
4321 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4322 			sata_id = &sdinfo->satadrv_id;
4323 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4324 				*scsipkt->pkt_scbp = STATUS_CHECK;
4325 				sense = sata_arq_sense(spx);
4326 				sense->es_key = KEY_ILLEGAL_REQUEST;
4327 				sense->es_add_code =
4328 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4329 
4330 				goto done;
4331 			}
4332 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4333 				*scsipkt->pkt_scbp = STATUS_CHECK;
4334 				sense = sata_arq_sense(spx);
4335 				sense->es_key = KEY_ABORTED_COMMAND;
4336 				sense->es_add_code =
4337 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4338 				sense->es_qual_code =
4339 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4340 
4341 				goto done;
4342 			}
4343 
4344 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4345 			    spx->txlt_sata_hba_inst);
4346 			break;
4347 		case PAGE_CODE_SMART_READ_DATA:
4348 			sata_id = &sdinfo->satadrv_id;
4349 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4350 				*scsipkt->pkt_scbp = STATUS_CHECK;
4351 				sense = sata_arq_sense(spx);
4352 				sense->es_key = KEY_ILLEGAL_REQUEST;
4353 				sense->es_add_code =
4354 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4355 
4356 				goto done;
4357 			}
4358 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4359 				*scsipkt->pkt_scbp = STATUS_CHECK;
4360 				sense = sata_arq_sense(spx);
4361 				sense->es_key = KEY_ABORTED_COMMAND;
4362 				sense->es_add_code =
4363 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4364 				sense->es_qual_code =
4365 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4366 
4367 				goto done;
4368 			}
4369 
4370 			/* This page doesn't include a page header */
4371 			len = sata_build_lsense_page_30(sdinfo, buf,
4372 			    spx->txlt_sata_hba_inst);
4373 			goto no_header;
4374 		default:
4375 			/* Invalid request */
4376 			*scsipkt->pkt_scbp = STATUS_CHECK;
4377 			sense = sata_arq_sense(spx);
4378 			sense->es_key = KEY_ILLEGAL_REQUEST;
4379 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4380 			goto done;
4381 		}
4382 
4383 		/* set parameter log sense data length */
4384 		buf[2] = len >> 8;	/* log sense length (MSB) */
4385 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4386 
4387 		len += SCSI_LOG_PAGE_HDR_LEN;
4388 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4389 
4390 no_header:
4391 		/* Check allocation length */
4392 		alc_len = scsipkt->pkt_cdbp[7];
4393 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4394 
4395 		/*
4396 		 * We do not check for possible parameters truncation
4397 		 * (alc_len < len) assuming that the target driver works
4398 		 * correctly. Just avoiding overrun.
4399 		 * Copy no more than requested and possible, buffer-wise.
4400 		 */
4401 		count = MIN(alc_len, len);
4402 		count = MIN(bp->b_bcount, count);
4403 		bcopy(buf, bp->b_un.b_addr, count);
4404 
4405 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4406 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4407 	}
4408 	*scsipkt->pkt_scbp = STATUS_GOOD;
4409 done:
4410 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4411 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4412 
4413 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4414 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4415 
4416 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4417 	    scsipkt->pkt_comp != NULL)
4418 		/* scsi callback required */
4419 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4420 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4421 		    TQ_SLEEP) == NULL)
4422 			/* Scheduling the callback failed */
4423 			return (TRAN_BUSY);
4424 
4425 	return (TRAN_ACCEPT);
4426 }
4427 
4428 /*
4429  * Translate command: Log Select
4430  * Not implemented at this time - returns invalid command response.
4431  */
4432 static 	int
4433 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4434 {
4435 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4436 	    "sata_txlt_log_select\n", NULL);
4437 
4438 	return (sata_txlt_invalid_command(spx));
4439 }
4440 
4441 
4442 /*
4443  * Translate command: Read (various types).
4444  * Translated into appropriate type of ATA READ command
4445  * for SATA hard disks.
4446  * Both the device capabilities and requested operation mode are
4447  * considered.
4448  *
4449  * Following scsi cdb fields are ignored:
4450  * rdprotect, dpo, fua, fua_nv, group_number.
4451  *
4452  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4453  * enable variable sata_func_enable), the capability of the controller and
4454  * capability of a device are checked and if both support queueing, read
4455  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4456  * command rather than plain READ_XXX command.
4457  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4458  * both the controller and device suport such functionality, the read
4459  * request will be translated to READ_FPDMA_QUEUED command.
4460  * In both cases the maximum queue depth is derived as minimum of:
4461  * HBA capability,device capability and sata_max_queue_depth variable setting.
4462  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4463  * used to pass max queue depth value, and the maximum possible queue depth
4464  * is 32.
4465  *
4466  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4467  * appropriate values in scsi_pkt fields.
4468  */
4469 static int
4470 sata_txlt_read(sata_pkt_txlate_t *spx)
4471 {
4472 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4473 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4474 	sata_drive_info_t *sdinfo;
4475 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4476 	int cport = SATA_TXLT_CPORT(spx);
4477 	uint16_t sec_count;
4478 	uint64_t lba;
4479 	int rval, reason;
4480 	int synch;
4481 
4482 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4483 
4484 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4485 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4486 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4487 		return (rval);
4488 	}
4489 
4490 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4491 	    &spx->txlt_sata_pkt->satapkt_device);
4492 
4493 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4494 	/*
4495 	 * Extract LBA and sector count from scsi CDB.
4496 	 */
4497 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4498 	case SCMD_READ:
4499 		/* 6-byte scsi read cmd : 0x08 */
4500 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4501 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4502 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4503 		sec_count = scsipkt->pkt_cdbp[4];
4504 		/* sec_count 0 will be interpreted as 256 by a device */
4505 		break;
4506 	case SCMD_READ_G1:
4507 		/* 10-bytes scsi read command : 0x28 */
4508 		lba = scsipkt->pkt_cdbp[2];
4509 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4510 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4511 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4512 		sec_count = scsipkt->pkt_cdbp[7];
4513 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4514 		break;
4515 	case SCMD_READ_G5:
4516 		/* 12-bytes scsi read command : 0xA8 */
4517 		lba = scsipkt->pkt_cdbp[2];
4518 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4519 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4520 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4521 		sec_count = scsipkt->pkt_cdbp[6];
4522 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4523 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4524 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4525 		break;
4526 	case SCMD_READ_G4:
4527 		/* 16-bytes scsi read command : 0x88 */
4528 		lba = scsipkt->pkt_cdbp[2];
4529 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4530 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4531 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4532 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4533 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4534 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4535 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4536 		sec_count = scsipkt->pkt_cdbp[10];
4537 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4538 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4539 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4540 		break;
4541 	default:
4542 		/* Unsupported command */
4543 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4544 		return (sata_txlt_invalid_command(spx));
4545 	}
4546 
4547 	/*
4548 	 * Check if specified address exceeds device capacity
4549 	 */
4550 	if ((lba >= sdinfo->satadrv_capacity) ||
4551 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4552 		/* LBA out of range */
4553 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4554 		return (sata_txlt_lba_out_of_range(spx));
4555 	}
4556 
4557 	/*
4558 	 * For zero-length transfer, emulate good completion of the command
4559 	 * (reasons for rejecting the command were already checked).
4560 	 * No DMA resources were allocated.
4561 	 */
4562 	if (spx->txlt_dma_cookie_list == NULL) {
4563 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4564 		return (sata_emul_rw_completion(spx));
4565 	}
4566 
4567 	/*
4568 	 * Build cmd block depending on the device capability and
4569 	 * requested operation mode.
4570 	 * Do not bother with non-dma mode - we are working only with
4571 	 * devices supporting DMA.
4572 	 */
4573 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4574 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4575 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4576 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4577 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4578 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4579 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4580 #ifndef __lock_lint
4581 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4582 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4583 		scmd->satacmd_lba_high_msb = lba >> 40;
4584 #endif
4585 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4586 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4587 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4588 	}
4589 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4590 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4591 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4592 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4593 	scmd->satacmd_features_reg = 0;
4594 	scmd->satacmd_status_reg = 0;
4595 	scmd->satacmd_error_reg = 0;
4596 
4597 	/*
4598 	 * Check if queueing commands should be used and switch
4599 	 * to appropriate command if possible
4600 	 */
4601 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4602 		boolean_t using_queuing;
4603 
4604 		/* Queuing supported by controller and device? */
4605 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4606 		    (sdinfo->satadrv_features_support &
4607 		    SATA_DEV_F_NCQ) &&
4608 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4609 		    SATA_CTLF_NCQ)) {
4610 			using_queuing = B_TRUE;
4611 
4612 			/* NCQ supported - use FPDMA READ */
4613 			scmd->satacmd_cmd_reg =
4614 			    SATAC_READ_FPDMA_QUEUED;
4615 			scmd->satacmd_features_reg_ext =
4616 			    scmd->satacmd_sec_count_msb;
4617 			scmd->satacmd_sec_count_msb = 0;
4618 		} else if ((sdinfo->satadrv_features_support &
4619 		    SATA_DEV_F_TCQ) &&
4620 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4621 		    SATA_CTLF_QCMD)) {
4622 			using_queuing = B_TRUE;
4623 
4624 			/* Legacy queueing */
4625 			if (sdinfo->satadrv_features_support &
4626 			    SATA_DEV_F_LBA48) {
4627 				scmd->satacmd_cmd_reg =
4628 				    SATAC_READ_DMA_QUEUED_EXT;
4629 				scmd->satacmd_features_reg_ext =
4630 				    scmd->satacmd_sec_count_msb;
4631 				scmd->satacmd_sec_count_msb = 0;
4632 			} else {
4633 				scmd->satacmd_cmd_reg =
4634 				    SATAC_READ_DMA_QUEUED;
4635 			}
4636 		} else	/* NCQ nor legacy queuing not supported */
4637 			using_queuing = B_FALSE;
4638 
4639 		/*
4640 		 * If queuing, the sector count goes in the features register
4641 		 * and the secount count will contain the tag.
4642 		 */
4643 		if (using_queuing) {
4644 			scmd->satacmd_features_reg =
4645 			    scmd->satacmd_sec_count_lsb;
4646 			scmd->satacmd_sec_count_lsb = 0;
4647 			scmd->satacmd_flags.sata_queued = B_TRUE;
4648 
4649 			/* Set-up maximum queue depth */
4650 			scmd->satacmd_flags.sata_max_queue_depth =
4651 			    sdinfo->satadrv_max_queue_depth - 1;
4652 		} else if (sdinfo->satadrv_features_enabled &
4653 		    SATA_DEV_F_E_UNTAGGED_QING) {
4654 			/*
4655 			 * Although NCQ/TCQ is not enabled, untagged queuing
4656 			 * may be still used.
4657 			 * Set-up the maximum untagged queue depth.
4658 			 * Use controller's queue depth from sata_hba_tran.
4659 			 * SATA HBA drivers may ignore this value and rely on
4660 			 * the internal limits.For drivers that do not
4661 			 * ignore untaged queue depth, limit the value to
4662 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4663 			 * largest value that can be passed via
4664 			 * satacmd_flags.sata_max_queue_depth.
4665 			 */
4666 			scmd->satacmd_flags.sata_max_queue_depth =
4667 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4668 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4669 
4670 		} else {
4671 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4672 		}
4673 	} else
4674 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4675 
4676 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4677 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4678 	    scmd->satacmd_cmd_reg, lba, sec_count);
4679 
4680 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4681 		/* Need callback function */
4682 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4683 		synch = FALSE;
4684 	} else
4685 		synch = TRUE;
4686 
4687 	/* Transfer command to HBA */
4688 	if (sata_hba_start(spx, &rval) != 0) {
4689 		/* Pkt not accepted for execution */
4690 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4691 		return (rval);
4692 	}
4693 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4694 	/*
4695 	 * If execution is non-synchronous,
4696 	 * a callback function will handle potential errors, translate
4697 	 * the response and will do a callback to a target driver.
4698 	 * If it was synchronous, check execution status using the same
4699 	 * framework callback.
4700 	 */
4701 	if (synch) {
4702 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4703 		    "synchronous execution status %x\n",
4704 		    spx->txlt_sata_pkt->satapkt_reason);
4705 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4706 	}
4707 	return (TRAN_ACCEPT);
4708 }
4709 
4710 
4711 /*
4712  * SATA translate command: Write (various types)
4713  * Translated into appropriate type of ATA WRITE command
4714  * for SATA hard disks.
4715  * Both the device capabilities and requested operation mode are
4716  * considered.
4717  *
4718  * Following scsi cdb fields are ignored:
4719  * rwprotect, dpo, fua, fua_nv, group_number.
4720  *
4721  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4722  * enable variable sata_func_enable), the capability of the controller and
4723  * capability of a device are checked and if both support queueing, write
4724  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4725  * command rather than plain WRITE_XXX command.
4726  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4727  * both the controller and device suport such functionality, the write
4728  * request will be translated to WRITE_FPDMA_QUEUED command.
4729  * In both cases the maximum queue depth is derived as minimum of:
4730  * HBA capability,device capability and sata_max_queue_depth variable setting.
4731  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4732  * used to pass max queue depth value, and the maximum possible queue depth
4733  * is 32.
4734  *
4735  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4736  * appropriate values in scsi_pkt fields.
4737  */
4738 static int
4739 sata_txlt_write(sata_pkt_txlate_t *spx)
4740 {
4741 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4742 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4743 	sata_drive_info_t *sdinfo;
4744 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4745 	int cport = SATA_TXLT_CPORT(spx);
4746 	uint16_t sec_count;
4747 	uint64_t lba;
4748 	int rval, reason;
4749 	int synch;
4750 
4751 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4752 
4753 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4754 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4755 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4756 		return (rval);
4757 	}
4758 
4759 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4760 	    &spx->txlt_sata_pkt->satapkt_device);
4761 
4762 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4763 	/*
4764 	 * Extract LBA and sector count from scsi CDB
4765 	 */
4766 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4767 	case SCMD_WRITE:
4768 		/* 6-byte scsi read cmd : 0x0A */
4769 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4770 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4771 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4772 		sec_count = scsipkt->pkt_cdbp[4];
4773 		/* sec_count 0 will be interpreted as 256 by a device */
4774 		break;
4775 	case SCMD_WRITE_G1:
4776 		/* 10-bytes scsi write command : 0x2A */
4777 		lba = scsipkt->pkt_cdbp[2];
4778 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4779 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4780 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4781 		sec_count = scsipkt->pkt_cdbp[7];
4782 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4783 		break;
4784 	case SCMD_WRITE_G5:
4785 		/* 12-bytes scsi read command : 0xAA */
4786 		lba = scsipkt->pkt_cdbp[2];
4787 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4788 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4789 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4790 		sec_count = scsipkt->pkt_cdbp[6];
4791 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4792 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4793 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4794 		break;
4795 	case SCMD_WRITE_G4:
4796 		/* 16-bytes scsi write command : 0x8A */
4797 		lba = scsipkt->pkt_cdbp[2];
4798 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4799 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4800 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4801 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4802 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4803 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4804 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4805 		sec_count = scsipkt->pkt_cdbp[10];
4806 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4807 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4808 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4809 		break;
4810 	default:
4811 		/* Unsupported command */
4812 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4813 		return (sata_txlt_invalid_command(spx));
4814 	}
4815 
4816 	/*
4817 	 * Check if specified address and length exceeds device capacity
4818 	 */
4819 	if ((lba >= sdinfo->satadrv_capacity) ||
4820 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4821 		/* LBA out of range */
4822 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4823 		return (sata_txlt_lba_out_of_range(spx));
4824 	}
4825 
4826 	/*
4827 	 * For zero-length transfer, emulate good completion of the command
4828 	 * (reasons for rejecting the command were already checked).
4829 	 * No DMA resources were allocated.
4830 	 */
4831 	if (spx->txlt_dma_cookie_list == NULL) {
4832 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4833 		return (sata_emul_rw_completion(spx));
4834 	}
4835 
4836 	/*
4837 	 * Build cmd block depending on the device capability and
4838 	 * requested operation mode.
4839 	 * Do not bother with non-dma mode- we are working only with
4840 	 * devices supporting DMA.
4841 	 */
4842 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4843 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4844 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4845 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4846 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4847 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4848 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4849 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4850 #ifndef __lock_lint
4851 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4852 		scmd->satacmd_lba_high_msb = lba >> 40;
4853 #endif
4854 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4855 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4856 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4857 	}
4858 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4859 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4860 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4861 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4862 	scmd->satacmd_features_reg = 0;
4863 	scmd->satacmd_status_reg = 0;
4864 	scmd->satacmd_error_reg = 0;
4865 
4866 	/*
4867 	 * Check if queueing commands should be used and switch
4868 	 * to appropriate command if possible
4869 	 */
4870 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4871 		boolean_t using_queuing;
4872 
4873 		/* Queuing supported by controller and device? */
4874 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4875 		    (sdinfo->satadrv_features_support &
4876 		    SATA_DEV_F_NCQ) &&
4877 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4878 		    SATA_CTLF_NCQ)) {
4879 			using_queuing = B_TRUE;
4880 
4881 			/* NCQ supported - use FPDMA WRITE */
4882 			scmd->satacmd_cmd_reg =
4883 			    SATAC_WRITE_FPDMA_QUEUED;
4884 			scmd->satacmd_features_reg_ext =
4885 			    scmd->satacmd_sec_count_msb;
4886 			scmd->satacmd_sec_count_msb = 0;
4887 		} else if ((sdinfo->satadrv_features_support &
4888 		    SATA_DEV_F_TCQ) &&
4889 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4890 		    SATA_CTLF_QCMD)) {
4891 			using_queuing = B_TRUE;
4892 
4893 			/* Legacy queueing */
4894 			if (sdinfo->satadrv_features_support &
4895 			    SATA_DEV_F_LBA48) {
4896 				scmd->satacmd_cmd_reg =
4897 				    SATAC_WRITE_DMA_QUEUED_EXT;
4898 				scmd->satacmd_features_reg_ext =
4899 				    scmd->satacmd_sec_count_msb;
4900 				scmd->satacmd_sec_count_msb = 0;
4901 			} else {
4902 				scmd->satacmd_cmd_reg =
4903 				    SATAC_WRITE_DMA_QUEUED;
4904 			}
4905 		} else	/*  NCQ nor legacy queuing not supported */
4906 			using_queuing = B_FALSE;
4907 
4908 		if (using_queuing) {
4909 			scmd->satacmd_features_reg =
4910 			    scmd->satacmd_sec_count_lsb;
4911 			scmd->satacmd_sec_count_lsb = 0;
4912 			scmd->satacmd_flags.sata_queued = B_TRUE;
4913 			/* Set-up maximum queue depth */
4914 			scmd->satacmd_flags.sata_max_queue_depth =
4915 			    sdinfo->satadrv_max_queue_depth - 1;
4916 		} else if (sdinfo->satadrv_features_enabled &
4917 		    SATA_DEV_F_E_UNTAGGED_QING) {
4918 			/*
4919 			 * Although NCQ/TCQ is not enabled, untagged queuing
4920 			 * may be still used.
4921 			 * Set-up the maximum untagged queue depth.
4922 			 * Use controller's queue depth from sata_hba_tran.
4923 			 * SATA HBA drivers may ignore this value and rely on
4924 			 * the internal limits. For drivera that do not
4925 			 * ignore untaged queue depth, limit the value to
4926 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4927 			 * largest value that can be passed via
4928 			 * satacmd_flags.sata_max_queue_depth.
4929 			 */
4930 			scmd->satacmd_flags.sata_max_queue_depth =
4931 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4932 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4933 
4934 		} else {
4935 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4936 		}
4937 	} else
4938 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4939 
4940 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4941 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4942 	    scmd->satacmd_cmd_reg, lba, sec_count);
4943 
4944 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4945 		/* Need callback function */
4946 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4947 		synch = FALSE;
4948 	} else
4949 		synch = TRUE;
4950 
4951 	/* Transfer command to HBA */
4952 	if (sata_hba_start(spx, &rval) != 0) {
4953 		/* Pkt not accepted for execution */
4954 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4955 		return (rval);
4956 	}
4957 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4958 
4959 	/*
4960 	 * If execution is non-synchronous,
4961 	 * a callback function will handle potential errors, translate
4962 	 * the response and will do a callback to a target driver.
4963 	 * If it was synchronous, check execution status using the same
4964 	 * framework callback.
4965 	 */
4966 	if (synch) {
4967 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4968 		    "synchronous execution status %x\n",
4969 		    spx->txlt_sata_pkt->satapkt_reason);
4970 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4971 	}
4972 	return (TRAN_ACCEPT);
4973 }
4974 
4975 
4976 /*
4977  * Implements SCSI SBC WRITE BUFFER command download microcode option
4978  */
4979 static int
4980 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4981 {
4982 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4983 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4984 
4985 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4986 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4987 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4988 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4989 
4990 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4991 	struct scsi_extended_sense *sense;
4992 	int rval, mode, sector_count, reason;
4993 	int cport = SATA_TXLT_CPORT(spx);
4994 
4995 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4996 
4997 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4998 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4999 
5000 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5001 
5002 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
5003 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5004 		return (rval);
5005 	}
5006 	/*
5007 	 * If in interrupt context, reject this packet because it would issue
5008 	 * a synchronous command to HBA.
5009 	 */
5010 	if (servicing_interrupt()) {
5011 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
5012 		    "sata_txlt_write_buffer: rejecting command because "
5013 		    "of interrupt context\n", NULL);
5014 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5015 		return (TRAN_BUSY);
5016 	}
5017 
5018 	/* Use synchronous mode */
5019 	spx->txlt_sata_pkt->satapkt_op_mode
5020 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
5021 
5022 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5023 
5024 	scsipkt->pkt_reason = CMD_CMPLT;
5025 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5026 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5027 
5028 	/*
5029 	 * The SCSI to ATA translation specification only calls
5030 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5031 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5032 	 * ATA 8 (draft) got rid of download microcode for temp
5033 	 * and it is even optional for ATA 7, so it may be aborted.
5034 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5035 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5036 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5037 	 * sectors.  Thus the offset really doesn't buy us anything.
5038 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5039 	 * is revised, this can be revisisted.
5040 	 */
5041 	/* Reject not supported request */
5042 	switch (mode) {
5043 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5044 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5045 		break;
5046 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5047 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5048 		break;
5049 	default:
5050 		goto bad_param;
5051 	}
5052 
5053 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5054 
5055 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5056 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5057 		goto bad_param;
5058 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5059 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5060 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5061 	scmd->satacmd_lba_mid_lsb = 0;
5062 	scmd->satacmd_lba_high_lsb = 0;
5063 	scmd->satacmd_device_reg = 0;
5064 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5065 	scmd->satacmd_addr_type = 0;
5066 
5067 	/* Transfer command to HBA */
5068 	if (sata_hba_start(spx, &rval) != 0) {
5069 		/* Pkt not accepted for execution */
5070 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5071 		return (rval);
5072 	}
5073 
5074 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5075 
5076 	/* Then we need synchronous check the status of the disk */
5077 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5078 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5079 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5080 		scsipkt->pkt_reason = CMD_CMPLT;
5081 
5082 		/* Download commmand succeed, so probe and identify device */
5083 		sata_reidentify_device(spx);
5084 	} else {
5085 		/* Something went wrong, microcode download command failed */
5086 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5087 		*scsipkt->pkt_scbp = STATUS_CHECK;
5088 		sense = sata_arq_sense(spx);
5089 		switch (sata_pkt->satapkt_reason) {
5090 		case SATA_PKT_PORT_ERROR:
5091 			/*
5092 			 * We have no device data. Assume no data transfered.
5093 			 */
5094 			sense->es_key = KEY_HARDWARE_ERROR;
5095 			break;
5096 
5097 		case SATA_PKT_DEV_ERROR:
5098 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5099 			    SATA_STATUS_ERR) {
5100 				/*
5101 				 * determine dev error reason from error
5102 				 * reg content
5103 				 */
5104 				sata_decode_device_error(spx, sense);
5105 				break;
5106 			}
5107 			/* No extended sense key - no info available */
5108 			break;
5109 
5110 		case SATA_PKT_TIMEOUT:
5111 			scsipkt->pkt_reason = CMD_TIMEOUT;
5112 			scsipkt->pkt_statistics |=
5113 			    STAT_TIMEOUT | STAT_DEV_RESET;
5114 			/* No extended sense key ? */
5115 			break;
5116 
5117 		case SATA_PKT_ABORTED:
5118 			scsipkt->pkt_reason = CMD_ABORTED;
5119 			scsipkt->pkt_statistics |= STAT_ABORTED;
5120 			/* No extended sense key ? */
5121 			break;
5122 
5123 		case SATA_PKT_RESET:
5124 			/* pkt aborted by an explicit reset from a host */
5125 			scsipkt->pkt_reason = CMD_RESET;
5126 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5127 			break;
5128 
5129 		default:
5130 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5131 			    "sata_txlt_nodata_cmd_completion: "
5132 			    "invalid packet completion reason %d",
5133 			    sata_pkt->satapkt_reason));
5134 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5135 			break;
5136 		}
5137 
5138 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5139 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5140 
5141 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5142 			/* scsi callback required */
5143 			scsi_hba_pkt_comp(scsipkt);
5144 	}
5145 	return (TRAN_ACCEPT);
5146 
5147 bad_param:
5148 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5149 	*scsipkt->pkt_scbp = STATUS_CHECK;
5150 	sense = sata_arq_sense(spx);
5151 	sense->es_key = KEY_ILLEGAL_REQUEST;
5152 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5153 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5154 	    scsipkt->pkt_comp != NULL) {
5155 		/* scsi callback required */
5156 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5157 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5158 		    TQ_SLEEP) == 0) {
5159 			/* Scheduling the callback failed */
5160 			rval = TRAN_BUSY;
5161 		}
5162 	}
5163 	return (rval);
5164 }
5165 
5166 /*
5167  * Re-identify device after doing a firmware download.
5168  */
5169 static void
5170 sata_reidentify_device(sata_pkt_txlate_t *spx)
5171 {
5172 #define	DOWNLOAD_WAIT_TIME_SECS	60
5173 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5174 	int rval;
5175 	int retry_cnt;
5176 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5177 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5178 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5179 	sata_drive_info_t *sdinfo;
5180 
5181 	/*
5182 	 * Before returning good status, probe device.
5183 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5184 	 * The assumption is that the new microcode is applied by the
5185 	 * device. It is a caller responsibility to verify this.
5186 	 */
5187 	for (retry_cnt = 0;
5188 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5189 	    retry_cnt++) {
5190 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5191 
5192 		if (rval == SATA_SUCCESS) { /* Set default features */
5193 			sdinfo = sata_get_device_info(sata_hba_inst,
5194 			    &sata_device);
5195 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5196 			    SATA_SUCCESS) {
5197 				/* retry */
5198 				rval = sata_initialize_device(sata_hba_inst,
5199 				    sdinfo);
5200 				if (rval == SATA_RETRY)
5201 					sata_log(sata_hba_inst, CE_WARN,
5202 					    "SATA device at port %d pmport %d -"
5203 					    " default device features could not"
5204 					    " be set. Device may not operate "
5205 					    "as expected.",
5206 					    sata_device.satadev_addr.cport,
5207 					    sata_device.satadev_addr.pmport);
5208 			}
5209 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5210 				scsi_hba_pkt_comp(scsipkt);
5211 			return;
5212 		} else if (rval == SATA_RETRY) {
5213 			delay(drv_usectohz(1000000 *
5214 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5215 			continue;
5216 		} else	/* failed - no reason to retry */
5217 			break;
5218 	}
5219 
5220 	/*
5221 	 * Something went wrong, device probing failed.
5222 	 */
5223 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5224 	    "Cannot probe device after downloading microcode\n"));
5225 
5226 	/* Reset device to force retrying the probe. */
5227 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5228 	    (SATA_DIP(sata_hba_inst), &sata_device);
5229 
5230 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5231 		scsi_hba_pkt_comp(scsipkt);
5232 }
5233 
5234 
5235 /*
5236  * Translate command: Synchronize Cache.
5237  * Translates into Flush Cache command for SATA hard disks.
5238  *
5239  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5240  * appropriate values in scsi_pkt fields.
5241  */
5242 static 	int
5243 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5244 {
5245 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5246 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5247 	int cport = SATA_TXLT_CPORT(spx);
5248 	int rval, reason;
5249 	int synch;
5250 
5251 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5252 
5253 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5254 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5255 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5256 		return (rval);
5257 	}
5258 
5259 	scmd->satacmd_addr_type = 0;
5260 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5261 	scmd->satacmd_device_reg = 0;
5262 	scmd->satacmd_sec_count_lsb = 0;
5263 	scmd->satacmd_lba_low_lsb = 0;
5264 	scmd->satacmd_lba_mid_lsb = 0;
5265 	scmd->satacmd_lba_high_lsb = 0;
5266 	scmd->satacmd_features_reg = 0;
5267 	scmd->satacmd_status_reg = 0;
5268 	scmd->satacmd_error_reg = 0;
5269 
5270 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5271 	    "sata_txlt_synchronize_cache\n", NULL);
5272 
5273 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5274 		/* Need to set-up a callback function */
5275 		spx->txlt_sata_pkt->satapkt_comp =
5276 		    sata_txlt_nodata_cmd_completion;
5277 		synch = FALSE;
5278 	} else
5279 		synch = TRUE;
5280 
5281 	/* Transfer command to HBA */
5282 	if (sata_hba_start(spx, &rval) != 0) {
5283 		/* Pkt not accepted for execution */
5284 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5285 		return (rval);
5286 	}
5287 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5288 
5289 	/*
5290 	 * If execution non-synchronous, it had to be completed
5291 	 * a callback function will handle potential errors, translate
5292 	 * the response and will do a callback to a target driver.
5293 	 * If it was synchronous, check status, using the same
5294 	 * framework callback.
5295 	 */
5296 	if (synch) {
5297 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5298 		    "synchronous execution status %x\n",
5299 		    spx->txlt_sata_pkt->satapkt_reason);
5300 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5301 	}
5302 	return (TRAN_ACCEPT);
5303 }
5304 
5305 
5306 /*
5307  * Send pkt to SATA HBA driver
5308  *
5309  * This function may be called only if the operation is requested by scsi_pkt,
5310  * i.e. scsi_pkt is not NULL.
5311  *
5312  * This function has to be called with cport mutex held. It does release
5313  * the mutex when it calls HBA driver sata_tran_start function and
5314  * re-acquires it afterwards.
5315  *
5316  * If return value is 0, pkt was accepted, -1 otherwise
5317  * rval is set to appropriate sata_scsi_start return value.
5318  *
5319  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5320  * have called the sata_pkt callback function for this packet.
5321  *
5322  * The scsi callback has to be performed by the caller of this routine.
5323  *
5324  * Note 2: No port multiplier support for now.
5325  */
5326 static int
5327 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5328 {
5329 	int stat, cport;
5330 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5331 	sata_drive_info_t *sdinfo;
5332 	sata_device_t *sata_device;
5333 	uint8_t cmd;
5334 	struct sata_cmd_flags cmd_flags;
5335 
5336 	ASSERT(spx->txlt_sata_pkt != NULL);
5337 
5338 	cport = SATA_TXLT_CPORT(spx);
5339 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5340 
5341 	sdinfo = sata_get_device_info(sata_hba_inst,
5342 	    &spx->txlt_sata_pkt->satapkt_device);
5343 	ASSERT(sdinfo != NULL);
5344 
5345 	/* Clear device reset state? */
5346 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5347 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5348 		    sata_clear_dev_reset = B_TRUE;
5349 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5350 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5351 		    "sata_hba_start: clearing device reset state\n", NULL);
5352 	}
5353 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5354 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5355 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5356 
5357 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5358 
5359 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5360 	    "Sata cmd 0x%2x\n", cmd);
5361 
5362 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5363 	    spx->txlt_sata_pkt);
5364 
5365 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5366 	/*
5367 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5368 	 * with the sata callback, the sata_pkt could be already destroyed
5369 	 * by the time we check ther return status from the hba_start()
5370 	 * function, because sata_scsi_destroy_pkt() could have been already
5371 	 * called (perhaps in the interrupt context). So, in such case, there
5372 	 * should be no references to it. In other cases, sata_pkt still
5373 	 * exists.
5374 	 */
5375 	if (stat == SATA_TRAN_ACCEPTED) {
5376 		/*
5377 		 * pkt accepted for execution.
5378 		 * If it was executed synchronously, it is already completed
5379 		 * and pkt completion_reason indicates completion status.
5380 		 */
5381 		*rval = TRAN_ACCEPT;
5382 		return (0);
5383 	}
5384 
5385 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5386 	switch (stat) {
5387 	case SATA_TRAN_QUEUE_FULL:
5388 		/*
5389 		 * Controller detected queue full condition.
5390 		 */
5391 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5392 		    "sata_hba_start: queue full\n", NULL);
5393 
5394 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5395 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5396 
5397 		*rval = TRAN_BUSY;
5398 		break;
5399 
5400 	case SATA_TRAN_PORT_ERROR:
5401 		/*
5402 		 * Communication/link with device or general port error
5403 		 * detected before pkt execution begun.
5404 		 */
5405 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5406 		    SATA_ADDR_CPORT ||
5407 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5408 		    SATA_ADDR_DCPORT)
5409 			sata_log(sata_hba_inst, CE_CONT,
5410 			    "SATA port %d error",
5411 			    sata_device->satadev_addr.cport);
5412 		else
5413 			sata_log(sata_hba_inst, CE_CONT,
5414 			    "SATA port %d pmport %d error\n",
5415 			    sata_device->satadev_addr.cport,
5416 			    sata_device->satadev_addr.pmport);
5417 
5418 		/*
5419 		 * Update the port/device structure.
5420 		 * sata_pkt should be still valid. Since port error is
5421 		 * returned, sata_device content should reflect port
5422 		 * state - it means, that sata address have been changed,
5423 		 * because original packet's sata address refered to a device
5424 		 * attached to some port.
5425 		 */
5426 		sata_update_port_info(sata_hba_inst, sata_device);
5427 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5428 		*rval = TRAN_FATAL_ERROR;
5429 		break;
5430 
5431 	case SATA_TRAN_CMD_UNSUPPORTED:
5432 		/*
5433 		 * Command rejected by HBA as unsupported. It was HBA driver
5434 		 * that rejected the command, command was not sent to
5435 		 * an attached device.
5436 		 */
5437 		if ((sdinfo != NULL) &&
5438 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5439 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5440 			    "sat_hba_start: cmd 0x%2x rejected "
5441 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5442 
5443 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5444 		(void) sata_txlt_invalid_command(spx);
5445 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5446 
5447 		*rval = TRAN_ACCEPT;
5448 		break;
5449 
5450 	case SATA_TRAN_BUSY:
5451 		/*
5452 		 * Command rejected by HBA because other operation prevents
5453 		 * accepting the packet, or device is in RESET condition.
5454 		 */
5455 		if (sdinfo != NULL) {
5456 			sdinfo->satadrv_state =
5457 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5458 
5459 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5460 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5461 				    "sata_hba_start: cmd 0x%2x rejected "
5462 				    "because of device reset condition\n",
5463 				    cmd);
5464 			} else {
5465 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5466 				    "sata_hba_start: cmd 0x%2x rejected "
5467 				    "with SATA_TRAN_BUSY status\n",
5468 				    cmd);
5469 			}
5470 		}
5471 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5472 		*rval = TRAN_BUSY;
5473 		break;
5474 
5475 	default:
5476 		/* Unrecognized HBA response */
5477 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5478 		    "sata_hba_start: unrecognized HBA response "
5479 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5480 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5481 		*rval = TRAN_FATAL_ERROR;
5482 		break;
5483 	}
5484 
5485 	/*
5486 	 * If we got here, the packet was rejected.
5487 	 * Check if we need to remember reset state clearing request
5488 	 */
5489 	if (cmd_flags.sata_clear_dev_reset) {
5490 		/*
5491 		 * Check if device is still configured - it may have
5492 		 * disapeared from the configuration
5493 		 */
5494 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5495 		if (sdinfo != NULL) {
5496 			/*
5497 			 * Restore the flag that requests clearing of
5498 			 * the device reset state,
5499 			 * so the next sata packet may carry it to HBA.
5500 			 */
5501 			sdinfo->satadrv_event_flags |=
5502 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5503 		}
5504 	}
5505 	return (-1);
5506 }
5507 
5508 /*
5509  * Scsi response setup for invalid LBA
5510  *
5511  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5512  */
5513 static int
5514 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5515 {
5516 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5517 	struct scsi_extended_sense *sense;
5518 
5519 	scsipkt->pkt_reason = CMD_CMPLT;
5520 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5521 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5522 	*scsipkt->pkt_scbp = STATUS_CHECK;
5523 
5524 	*scsipkt->pkt_scbp = STATUS_CHECK;
5525 	sense = sata_arq_sense(spx);
5526 	sense->es_key = KEY_ILLEGAL_REQUEST;
5527 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5528 
5529 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5530 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5531 
5532 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5533 	    scsipkt->pkt_comp != NULL)
5534 		/* scsi callback required */
5535 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5536 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5537 		    TQ_SLEEP) == NULL)
5538 			/* Scheduling the callback failed */
5539 			return (TRAN_BUSY);
5540 	return (TRAN_ACCEPT);
5541 }
5542 
5543 
5544 /*
5545  * Analyze device status and error registers and translate them into
5546  * appropriate scsi sense codes.
5547  * NOTE: non-packet commands only for now
5548  */
5549 static void
5550 sata_decode_device_error(sata_pkt_txlate_t *spx,
5551     struct scsi_extended_sense *sense)
5552 {
5553 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5554 
5555 	ASSERT(sense != NULL);
5556 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5557 	    SATA_STATUS_ERR);
5558 
5559 
5560 	if (err_reg & SATA_ERROR_ICRC) {
5561 		sense->es_key = KEY_ABORTED_COMMAND;
5562 		sense->es_add_code = 0x08; /* Communication failure */
5563 		return;
5564 	}
5565 
5566 	if (err_reg & SATA_ERROR_UNC) {
5567 		sense->es_key = KEY_MEDIUM_ERROR;
5568 		/* Information bytes (LBA) need to be set by a caller */
5569 		return;
5570 	}
5571 
5572 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5573 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5574 		sense->es_key = KEY_UNIT_ATTENTION;
5575 		sense->es_add_code = 0x3a; /* No media present */
5576 		return;
5577 	}
5578 
5579 	if (err_reg & SATA_ERROR_IDNF) {
5580 		if (err_reg & SATA_ERROR_ABORT) {
5581 			sense->es_key = KEY_ABORTED_COMMAND;
5582 		} else {
5583 			sense->es_key = KEY_ILLEGAL_REQUEST;
5584 			sense->es_add_code = 0x21; /* LBA out of range */
5585 		}
5586 		return;
5587 	}
5588 
5589 	if (err_reg & SATA_ERROR_ABORT) {
5590 		ASSERT(spx->txlt_sata_pkt != NULL);
5591 		sense->es_key = KEY_ABORTED_COMMAND;
5592 		return;
5593 	}
5594 }
5595 
5596 /*
5597  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5598  */
5599 static void
5600 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5601 {
5602 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5603 
5604 	*lba = 0;
5605 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5606 		*lba = sata_cmd->satacmd_lba_high_msb;
5607 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5608 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5609 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5610 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5611 	}
5612 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5613 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5614 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5615 }
5616 
5617 /*
5618  * This is fixed sense format - if LBA exceeds the info field size,
5619  * no valid info will be returned (valid bit in extended sense will
5620  * be set to 0).
5621  */
5622 static struct scsi_extended_sense *
5623 sata_arq_sense(sata_pkt_txlate_t *spx)
5624 {
5625 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5626 	struct scsi_arq_status *arqs;
5627 	struct scsi_extended_sense *sense;
5628 
5629 	/* Fill ARQ sense data */
5630 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5631 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5632 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5633 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5634 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5635 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5636 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5637 	arqs->sts_rqpkt_resid = 0;
5638 	sense = &arqs->sts_sensedata;
5639 	bzero(sense, sizeof (struct scsi_extended_sense));
5640 	sata_fixed_sense_data_preset(sense);
5641 	return (sense);
5642 }
5643 
5644 
5645 /*
5646  * Emulated SATA Read/Write command completion for zero-length requests.
5647  * This request always succedes, so in synchronous mode it always returns
5648  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5649  * callback cannot be scheduled.
5650  */
5651 static int
5652 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5653 {
5654 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5655 
5656 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5657 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5658 	scsipkt->pkt_reason = CMD_CMPLT;
5659 	*scsipkt->pkt_scbp = STATUS_GOOD;
5660 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5661 		/* scsi callback required - have to schedule it */
5662 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5663 		    (task_func_t *)scsipkt->pkt_comp,
5664 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5665 			/* Scheduling the callback failed */
5666 			return (TRAN_BUSY);
5667 	}
5668 	return (TRAN_ACCEPT);
5669 }
5670 
5671 
5672 /*
5673  * Translate completion status of SATA read/write commands into scsi response.
5674  * pkt completion_reason is checked to determine the completion status.
5675  * Do scsi callback if necessary.
5676  *
5677  * Note: this function may be called also for synchronously executed
5678  * commands.
5679  * This function may be used only if scsi_pkt is non-NULL.
5680  */
5681 static void
5682 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5683 {
5684 	sata_pkt_txlate_t *spx =
5685 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5686 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5687 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5688 	struct scsi_extended_sense *sense;
5689 	uint64_t lba;
5690 	struct buf *bp;
5691 	int rval;
5692 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5693 		/* Normal completion */
5694 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5695 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5696 		scsipkt->pkt_reason = CMD_CMPLT;
5697 		*scsipkt->pkt_scbp = STATUS_GOOD;
5698 		if (spx->txlt_tmp_buf != NULL) {
5699 			/* Temporary buffer was used */
5700 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5701 			if (bp->b_flags & B_READ) {
5702 				rval = ddi_dma_sync(
5703 				    spx->txlt_buf_dma_handle, 0, 0,
5704 				    DDI_DMA_SYNC_FORCPU);
5705 				ASSERT(rval == DDI_SUCCESS);
5706 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5707 				    bp->b_bcount);
5708 			}
5709 		}
5710 	} else {
5711 		/*
5712 		 * Something went wrong - analyze return
5713 		 */
5714 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5715 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5716 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5717 		*scsipkt->pkt_scbp = STATUS_CHECK;
5718 		sense = sata_arq_sense(spx);
5719 		ASSERT(sense != NULL);
5720 
5721 		/*
5722 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5723 		 * extract from device registers the failing LBA.
5724 		 */
5725 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5726 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5727 			    (scmd->satacmd_lba_mid_msb != 0 ||
5728 			    scmd->satacmd_lba_high_msb != 0)) {
5729 				/*
5730 				 * We have problem reporting this cmd LBA
5731 				 * in fixed sense data format, because of
5732 				 * the size of the scsi LBA fields.
5733 				 */
5734 				sense->es_valid = 0;
5735 			} else {
5736 				sata_extract_error_lba(spx, &lba);
5737 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5738 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5739 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5740 				sense->es_info_4 = lba & 0xFF;
5741 			}
5742 		} else {
5743 			/* Invalid extended sense info */
5744 			sense->es_valid = 0;
5745 		}
5746 
5747 		switch (sata_pkt->satapkt_reason) {
5748 		case SATA_PKT_PORT_ERROR:
5749 			/* We may want to handle DEV GONE state as well */
5750 			/*
5751 			 * We have no device data. Assume no data transfered.
5752 			 */
5753 			sense->es_key = KEY_HARDWARE_ERROR;
5754 			break;
5755 
5756 		case SATA_PKT_DEV_ERROR:
5757 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5758 			    SATA_STATUS_ERR) {
5759 				/*
5760 				 * determine dev error reason from error
5761 				 * reg content
5762 				 */
5763 				sata_decode_device_error(spx, sense);
5764 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5765 					switch (scmd->satacmd_cmd_reg) {
5766 					case SATAC_READ_DMA:
5767 					case SATAC_READ_DMA_EXT:
5768 					case SATAC_READ_DMA_QUEUED:
5769 					case SATAC_READ_DMA_QUEUED_EXT:
5770 					case SATAC_READ_FPDMA_QUEUED:
5771 						/* Unrecovered read error */
5772 						sense->es_add_code =
5773 						    SD_SCSI_ASC_UNREC_READ_ERR;
5774 						break;
5775 					case SATAC_WRITE_DMA:
5776 					case SATAC_WRITE_DMA_EXT:
5777 					case SATAC_WRITE_DMA_QUEUED:
5778 					case SATAC_WRITE_DMA_QUEUED_EXT:
5779 					case SATAC_WRITE_FPDMA_QUEUED:
5780 						/* Write error */
5781 						sense->es_add_code =
5782 						    SD_SCSI_ASC_WRITE_ERR;
5783 						break;
5784 					default:
5785 						/* Internal error */
5786 						SATA_LOG_D((
5787 						    spx->txlt_sata_hba_inst,
5788 						    CE_WARN,
5789 						    "sata_txlt_rw_completion :"
5790 						    "internal error - invalid "
5791 						    "command 0x%2x",
5792 						    scmd->satacmd_cmd_reg));
5793 						break;
5794 					}
5795 				}
5796 				break;
5797 			}
5798 			/* No extended sense key - no info available */
5799 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5800 			break;
5801 
5802 		case SATA_PKT_TIMEOUT:
5803 			scsipkt->pkt_reason = CMD_TIMEOUT;
5804 			scsipkt->pkt_statistics |=
5805 			    STAT_TIMEOUT | STAT_DEV_RESET;
5806 			sense->es_key = KEY_ABORTED_COMMAND;
5807 			break;
5808 
5809 		case SATA_PKT_ABORTED:
5810 			scsipkt->pkt_reason = CMD_ABORTED;
5811 			scsipkt->pkt_statistics |= STAT_ABORTED;
5812 			sense->es_key = KEY_ABORTED_COMMAND;
5813 			break;
5814 
5815 		case SATA_PKT_RESET:
5816 			scsipkt->pkt_reason = CMD_RESET;
5817 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5818 			sense->es_key = KEY_ABORTED_COMMAND;
5819 			break;
5820 
5821 		default:
5822 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5823 			    "sata_txlt_rw_completion: "
5824 			    "invalid packet completion reason"));
5825 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5826 			break;
5827 		}
5828 	}
5829 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5830 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5831 
5832 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5833 		/* scsi callback required */
5834 		scsi_hba_pkt_comp(scsipkt);
5835 }
5836 
5837 
5838 /*
5839  * Translate completion status of non-data commands (i.e. commands returning
5840  * no data).
5841  * pkt completion_reason is checked to determine the completion status.
5842  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5843  *
5844  * Note: this function may be called also for synchronously executed
5845  * commands.
5846  * This function may be used only if scsi_pkt is non-NULL.
5847  */
5848 
5849 static 	void
5850 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5851 {
5852 	sata_pkt_txlate_t *spx =
5853 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5854 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5855 	struct scsi_extended_sense *sense;
5856 
5857 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5858 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5859 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5860 		/* Normal completion */
5861 		scsipkt->pkt_reason = CMD_CMPLT;
5862 		*scsipkt->pkt_scbp = STATUS_GOOD;
5863 	} else {
5864 		/* Something went wrong */
5865 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5866 		*scsipkt->pkt_scbp = STATUS_CHECK;
5867 		sense = sata_arq_sense(spx);
5868 		switch (sata_pkt->satapkt_reason) {
5869 		case SATA_PKT_PORT_ERROR:
5870 			/*
5871 			 * We have no device data. Assume no data transfered.
5872 			 */
5873 			sense->es_key = KEY_HARDWARE_ERROR;
5874 			break;
5875 
5876 		case SATA_PKT_DEV_ERROR:
5877 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5878 			    SATA_STATUS_ERR) {
5879 				/*
5880 				 * determine dev error reason from error
5881 				 * reg content
5882 				 */
5883 				sata_decode_device_error(spx, sense);
5884 				break;
5885 			}
5886 			/* No extended sense key - no info available */
5887 			break;
5888 
5889 		case SATA_PKT_TIMEOUT:
5890 			scsipkt->pkt_reason = CMD_TIMEOUT;
5891 			scsipkt->pkt_statistics |=
5892 			    STAT_TIMEOUT | STAT_DEV_RESET;
5893 			/* No extended sense key ? */
5894 			break;
5895 
5896 		case SATA_PKT_ABORTED:
5897 			scsipkt->pkt_reason = CMD_ABORTED;
5898 			scsipkt->pkt_statistics |= STAT_ABORTED;
5899 			/* No extended sense key ? */
5900 			break;
5901 
5902 		case SATA_PKT_RESET:
5903 			/* pkt aborted by an explicit reset from a host */
5904 			scsipkt->pkt_reason = CMD_RESET;
5905 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5906 			break;
5907 
5908 		default:
5909 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5910 			    "sata_txlt_nodata_cmd_completion: "
5911 			    "invalid packet completion reason %d",
5912 			    sata_pkt->satapkt_reason));
5913 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5914 			break;
5915 		}
5916 
5917 	}
5918 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5919 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5920 
5921 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5922 		/* scsi callback required */
5923 		scsi_hba_pkt_comp(scsipkt);
5924 }
5925 
5926 
5927 /*
5928  * Build Mode sense R/W recovery page
5929  * NOT IMPLEMENTED
5930  */
5931 
5932 static int
5933 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5934 {
5935 #ifndef __lock_lint
5936 	_NOTE(ARGUNUSED(sdinfo))
5937 	_NOTE(ARGUNUSED(pcntrl))
5938 	_NOTE(ARGUNUSED(buf))
5939 #endif
5940 	return (0);
5941 }
5942 
5943 /*
5944  * Build Mode sense caching page  -  scsi-3 implementation.
5945  * Page length distinguishes previous format from scsi-3 format.
5946  * buf must have space for 0x12 bytes.
5947  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5948  *
5949  */
5950 static int
5951 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5952 {
5953 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5954 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5955 
5956 	/*
5957 	 * Most of the fields are set to 0, being not supported and/or disabled
5958 	 */
5959 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5960 
5961 	/* Saved paramters not supported */
5962 	if (pcntrl == 3)
5963 		return (0);
5964 	if (pcntrl == 0 || pcntrl == 2) {
5965 		/*
5966 		 * For now treat current and default parameters as same
5967 		 * That may have to change, if target driver will complain
5968 		 */
5969 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5970 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5971 
5972 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
5973 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
5974 			page->dra = 1;		/* Read Ahead disabled */
5975 			page->rcd = 1;		/* Read Cache disabled */
5976 		}
5977 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
5978 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
5979 			page->wce = 1;		/* Write Cache enabled */
5980 	} else {
5981 		/* Changeable parameters */
5982 		page->mode_page.code = MODEPAGE_CACHING;
5983 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5984 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
5985 			page->dra = 1;
5986 			page->rcd = 1;
5987 		}
5988 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
5989 			page->wce = 1;
5990 	}
5991 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5992 	    sizeof (struct mode_page));
5993 }
5994 
5995 /*
5996  * Build Mode sense exception cntrl page
5997  */
5998 static int
5999 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6000 {
6001 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6002 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6003 
6004 	/*
6005 	 * Most of the fields are set to 0, being not supported and/or disabled
6006 	 */
6007 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6008 
6009 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6010 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6011 
6012 	/* Indicate that this is page is saveable */
6013 	page->mode_page.ps = 1;
6014 
6015 	/*
6016 	 * We will return the same data for default, current and saved page.
6017 	 * The only changeable bit is dexcpt and that bit is required
6018 	 * by the ATA specification to be preserved across power cycles.
6019 	 */
6020 	if (pcntrl != 1) {
6021 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6022 		page->mrie = MRIE_ONLY_ON_REQUEST;
6023 	}
6024 	else
6025 		page->dexcpt = 1;	/* Only changeable parameter */
6026 
6027 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
6028 }
6029 
6030 
6031 static int
6032 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6033 {
6034 	struct mode_acoustic_management *page =
6035 	    (struct mode_acoustic_management *)buf;
6036 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6037 
6038 	/*
6039 	 * Most of the fields are set to 0, being not supported and/or disabled
6040 	 */
6041 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6042 
6043 	switch (pcntrl) {
6044 	case P_CNTRL_DEFAULT:
6045 		/*  default paramters not supported */
6046 		return (0);
6047 
6048 	case P_CNTRL_CURRENT:
6049 	case P_CNTRL_SAVED:
6050 		/* Saved and current are supported and are identical */
6051 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6052 		page->mode_page.length =
6053 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6054 		page->mode_page.ps = 1;
6055 
6056 		/* Word 83 indicates if feature is supported */
6057 		/* If feature is not supported */
6058 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6059 			page->acoustic_manag_enable =
6060 			    ACOUSTIC_DISABLED;
6061 		} else {
6062 			page->acoustic_manag_enable =
6063 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6064 			    != 0);
6065 			/* Word 94 inidicates the value */
6066 #ifdef	_LITTLE_ENDIAN
6067 			page->acoustic_manag_level =
6068 			    (uchar_t)sata_id->ai_acoustic;
6069 			page->vendor_recommended_value =
6070 			    sata_id->ai_acoustic >> 8;
6071 #else
6072 			page->acoustic_manag_level =
6073 			    sata_id->ai_acoustic >> 8;
6074 			page->vendor_recommended_value =
6075 			    (uchar_t)sata_id->ai_acoustic;
6076 #endif
6077 		}
6078 		break;
6079 
6080 	case P_CNTRL_CHANGEABLE:
6081 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6082 		page->mode_page.length =
6083 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6084 		page->mode_page.ps = 1;
6085 
6086 		/* Word 83 indicates if the feature is supported */
6087 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6088 			page->acoustic_manag_enable =
6089 			    ACOUSTIC_ENABLED;
6090 			page->acoustic_manag_level = 0xff;
6091 		}
6092 		break;
6093 	}
6094 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6095 	    sizeof (struct mode_page));
6096 }
6097 
6098 
6099 /*
6100  * Build Mode sense power condition page
6101  * NOT IMPLEMENTED.
6102  */
6103 static int
6104 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6105 {
6106 #ifndef __lock_lint
6107 	_NOTE(ARGUNUSED(sdinfo))
6108 	_NOTE(ARGUNUSED(pcntrl))
6109 	_NOTE(ARGUNUSED(buf))
6110 #endif
6111 	return (0);
6112 }
6113 
6114 
6115 /*
6116  * Process mode select caching page 8 (scsi3 format only).
6117  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6118  * if these features are supported by the device. If these features are not
6119  * supported, quietly ignore them.
6120  * This function fails only if the SET FEATURE command sent to
6121  * the device fails. The page format is not varified, assuming that the
6122  * target driver operates correctly - if parameters length is too short,
6123  * we just drop the page.
6124  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6125  * setting have to be changed.
6126  * SET FEATURE command is executed synchronously, i.e. we wait here until
6127  * it is completed, regardless of the scsi pkt directives.
6128  *
6129  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6130  * changing DRA will change RCD.
6131  *
6132  * More than one SATA command may be executed to perform operations specified
6133  * by mode select pages. The first error terminates further execution.
6134  * Operations performed successully are not backed-up in such case.
6135  *
6136  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6137  * If operation resulted in changing device setup, dmod flag should be set to
6138  * one (1). If parameters were not changed, dmod flag should be set to 0.
6139  * Upon return, if operation required sending command to the device, the rval
6140  * should be set to the value returned by sata_hba_start. If operation
6141  * did not require device access, rval should be set to TRAN_ACCEPT.
6142  * The pagelen should be set to the length of the page.
6143  *
6144  * This function has to be called with a port mutex held.
6145  *
6146  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6147  */
6148 int
6149 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6150     int parmlen, int *pagelen, int *rval, int *dmod)
6151 {
6152 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6153 	sata_drive_info_t *sdinfo;
6154 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6155 	sata_id_t *sata_id;
6156 	struct scsi_extended_sense *sense;
6157 	int wce, dra;	/* Current settings */
6158 
6159 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6160 	    &spx->txlt_sata_pkt->satapkt_device);
6161 	sata_id = &sdinfo->satadrv_id;
6162 	*dmod = 0;
6163 
6164 	/* Verify parameters length. If too short, drop it */
6165 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6166 	    sizeof (struct mode_page) < parmlen) {
6167 		*scsipkt->pkt_scbp = STATUS_CHECK;
6168 		sense = sata_arq_sense(spx);
6169 		sense->es_key = KEY_ILLEGAL_REQUEST;
6170 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6171 		*pagelen = parmlen;
6172 		*rval = TRAN_ACCEPT;
6173 		return (SATA_FAILURE);
6174 	}
6175 
6176 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6177 
6178 	/*
6179 	 * We can manipulate only write cache and read ahead
6180 	 * (read cache) setting.
6181 	 */
6182 	if (!SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6183 	    !SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
6184 		/*
6185 		 * None of the features is supported - ignore
6186 		 */
6187 		*rval = TRAN_ACCEPT;
6188 		return (SATA_SUCCESS);
6189 	}
6190 
6191 	/* Current setting of Read Ahead (and Read Cache) */
6192 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
6193 		dra = 0;	/* 0 == not disabled */
6194 	else
6195 		dra = 1;
6196 	/* Current setting of Write Cache */
6197 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
6198 		wce = 1;
6199 	else
6200 		wce = 0;
6201 
6202 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6203 		/* nothing to do */
6204 		*rval = TRAN_ACCEPT;
6205 		return (SATA_SUCCESS);
6206 	}
6207 
6208 	/*
6209 	 * Need to flip some setting
6210 	 * Set-up Internal SET FEATURES command(s)
6211 	 */
6212 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6213 	scmd->satacmd_addr_type = 0;
6214 	scmd->satacmd_device_reg = 0;
6215 	scmd->satacmd_status_reg = 0;
6216 	scmd->satacmd_error_reg = 0;
6217 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6218 	if (page->dra != dra || page->rcd != dra) {
6219 		/* Need to flip read ahead setting */
6220 		if (dra == 0)
6221 			/* Disable read ahead / read cache */
6222 			scmd->satacmd_features_reg =
6223 			    SATAC_SF_DISABLE_READ_AHEAD;
6224 		else
6225 			/* Enable read ahead  / read cache */
6226 			scmd->satacmd_features_reg =
6227 			    SATAC_SF_ENABLE_READ_AHEAD;
6228 
6229 		/* Transfer command to HBA */
6230 		if (sata_hba_start(spx, rval) != 0)
6231 			/*
6232 			 * Pkt not accepted for execution.
6233 			 */
6234 			return (SATA_FAILURE);
6235 
6236 		*dmod = 1;
6237 
6238 		/* Now process return */
6239 		if (spx->txlt_sata_pkt->satapkt_reason !=
6240 		    SATA_PKT_COMPLETED) {
6241 			goto failure;	/* Terminate */
6242 		}
6243 	}
6244 
6245 	/* Note that the packet is not removed, so it could be re-used */
6246 	if (page->wce != wce) {
6247 		/* Need to flip Write Cache setting */
6248 		if (page->wce == 1)
6249 			/* Enable write cache */
6250 			scmd->satacmd_features_reg =
6251 			    SATAC_SF_ENABLE_WRITE_CACHE;
6252 		else
6253 			/* Disable write cache */
6254 			scmd->satacmd_features_reg =
6255 			    SATAC_SF_DISABLE_WRITE_CACHE;
6256 
6257 		/* Transfer command to HBA */
6258 		if (sata_hba_start(spx, rval) != 0)
6259 			/*
6260 			 * Pkt not accepted for execution.
6261 			 */
6262 			return (SATA_FAILURE);
6263 
6264 		*dmod = 1;
6265 
6266 		/* Now process return */
6267 		if (spx->txlt_sata_pkt->satapkt_reason !=
6268 		    SATA_PKT_COMPLETED) {
6269 			goto failure;
6270 		}
6271 	}
6272 	return (SATA_SUCCESS);
6273 
6274 failure:
6275 	sata_xlate_errors(spx);
6276 
6277 	return (SATA_FAILURE);
6278 }
6279 
6280 /*
6281  * Process mode select informational exceptions control page 0x1c
6282  *
6283  * The only changeable bit is dexcpt (disable exceptions).
6284  * MRIE (method of reporting informational exceptions) must be
6285  * "only on request".
6286  * This page applies to informational exceptions that report
6287  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
6288  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
6289  * Informational exception conditions occur as the result of background scan
6290  * errors, background self-test errors, or vendor specific events within a
6291  * logical unit. An informational exception condition may occur asynchronous
6292  * to any commands.
6293  *
6294  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6295  * If operation resulted in changing device setup, dmod flag should be set to
6296  * one (1). If parameters were not changed, dmod flag should be set to 0.
6297  * Upon return, if operation required sending command to the device, the rval
6298  * should be set to the value returned by sata_hba_start. If operation
6299  * did not require device access, rval should be set to TRAN_ACCEPT.
6300  * The pagelen should be set to the length of the page.
6301  *
6302  * This function has to be called with a port mutex held.
6303  *
6304  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6305  *
6306  * Cannot be called in the interrupt context.
6307  */
6308 static	int
6309 sata_mode_select_page_1c(
6310 	sata_pkt_txlate_t *spx,
6311 	struct mode_info_excpt_page *page,
6312 	int parmlen,
6313 	int *pagelen,
6314 	int *rval,
6315 	int *dmod)
6316 {
6317 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6318 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6319 	sata_drive_info_t *sdinfo;
6320 	sata_id_t *sata_id;
6321 	struct scsi_extended_sense *sense;
6322 
6323 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6324 	    &spx->txlt_sata_pkt->satapkt_device);
6325 	sata_id = &sdinfo->satadrv_id;
6326 
6327 	*dmod = 0;
6328 
6329 	/* Verify parameters length. If too short, drop it */
6330 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6331 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6332 		*scsipkt->pkt_scbp = STATUS_CHECK;
6333 		sense = sata_arq_sense(spx);
6334 		sense->es_key = KEY_ILLEGAL_REQUEST;
6335 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6336 		*pagelen = parmlen;
6337 		*rval = TRAN_ACCEPT;
6338 		return (SATA_FAILURE);
6339 	}
6340 
6341 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6342 
6343 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6344 		*scsipkt->pkt_scbp = STATUS_CHECK;
6345 		sense = sata_arq_sense(spx);
6346 		sense->es_key = KEY_ILLEGAL_REQUEST;
6347 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6348 		*pagelen = parmlen;
6349 		*rval = TRAN_ACCEPT;
6350 		return (SATA_FAILURE);
6351 	}
6352 
6353 	/* If already in the state requested, we are done */
6354 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6355 		/* nothing to do */
6356 		*rval = TRAN_ACCEPT;
6357 		return (SATA_SUCCESS);
6358 	}
6359 
6360 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6361 
6362 	/* Build SMART_ENABLE or SMART_DISABLE command */
6363 	scmd->satacmd_addr_type = 0;		/* N/A */
6364 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6365 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6366 	scmd->satacmd_features_reg = page->dexcpt ?
6367 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6368 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6369 	scmd->satacmd_cmd_reg = SATAC_SMART;
6370 
6371 	/* Transfer command to HBA */
6372 	if (sata_hba_start(spx, rval) != 0)
6373 		/*
6374 		 * Pkt not accepted for execution.
6375 		 */
6376 		return (SATA_FAILURE);
6377 
6378 	*dmod = 1;	/* At least may have been modified */
6379 
6380 	/* Now process return */
6381 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6382 		return (SATA_SUCCESS);
6383 
6384 	/* Packet did not complete successfully */
6385 	sata_xlate_errors(spx);
6386 
6387 	return (SATA_FAILURE);
6388 }
6389 
6390 /*
6391  * Process mode select acoustic management control page 0x30
6392  *
6393  *
6394  * This function has to be called with a port mutex held.
6395  *
6396  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6397  *
6398  * Cannot be called in the interrupt context.
6399  */
6400 int
6401 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6402     mode_acoustic_management *page, int parmlen, int *pagelen,
6403     int *rval, int *dmod)
6404 {
6405 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6406 	sata_drive_info_t *sdinfo;
6407 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6408 	sata_id_t *sata_id;
6409 	struct scsi_extended_sense *sense;
6410 
6411 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6412 	    &spx->txlt_sata_pkt->satapkt_device);
6413 	sata_id = &sdinfo->satadrv_id;
6414 	*dmod = 0;
6415 
6416 	/* If parmlen is too short or the feature is not supported, drop it */
6417 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6418 	    sizeof (struct mode_page)) < parmlen) ||
6419 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6420 		*scsipkt->pkt_scbp = STATUS_CHECK;
6421 		sense = sata_arq_sense(spx);
6422 		sense->es_key = KEY_ILLEGAL_REQUEST;
6423 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6424 		*pagelen = parmlen;
6425 		*rval = TRAN_ACCEPT;
6426 		return (SATA_FAILURE);
6427 	}
6428 
6429 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6430 	    sizeof (struct mode_page);
6431 
6432 	/*
6433 	 * We can enable and disable acoustice management and
6434 	 * set the acoustic management level.
6435 	 */
6436 
6437 	/*
6438 	 * Set-up Internal SET FEATURES command(s)
6439 	 */
6440 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6441 	scmd->satacmd_addr_type = 0;
6442 	scmd->satacmd_device_reg = 0;
6443 	scmd->satacmd_status_reg = 0;
6444 	scmd->satacmd_error_reg = 0;
6445 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6446 	if (page->acoustic_manag_enable) {
6447 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6448 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6449 	} else {	/* disabling acoustic management */
6450 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6451 	}
6452 
6453 	/* Transfer command to HBA */
6454 	if (sata_hba_start(spx, rval) != 0)
6455 		/*
6456 		 * Pkt not accepted for execution.
6457 		 */
6458 		return (SATA_FAILURE);
6459 
6460 	/* Now process return */
6461 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6462 		sata_xlate_errors(spx);
6463 		return (SATA_FAILURE);
6464 	}
6465 
6466 	*dmod = 1;
6467 
6468 	return (SATA_SUCCESS);
6469 }
6470 
6471 
6472 
6473 
6474 /*
6475  * sata_build_lsense_page0() is used to create the
6476  * SCSI LOG SENSE page 0 (supported log pages)
6477  *
6478  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6479  * (supported log pages, self-test results, informational exceptions
6480  *  and Sun vendor specific ATA SMART data).
6481  *
6482  * Takes a sata_drive_info t * and the address of a buffer
6483  * in which to create the page information.
6484  *
6485  * Returns the number of bytes valid in the buffer.
6486  */
6487 static	int
6488 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6489 {
6490 	struct log_parameter *lpp = (struct log_parameter *)buf;
6491 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6492 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6493 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6494 
6495 	lpp->param_code[0] = 0;
6496 	lpp->param_code[1] = 0;
6497 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6498 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6499 
6500 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6501 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6502 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6503 			++num_pages_supported;
6504 		}
6505 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6506 		++num_pages_supported;
6507 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6508 		++num_pages_supported;
6509 	}
6510 
6511 	lpp->param_len = num_pages_supported;
6512 
6513 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6514 	    num_pages_supported);
6515 }
6516 
6517 /*
6518  * sata_build_lsense_page_10() is used to create the
6519  * SCSI LOG SENSE page 0x10 (self-test results)
6520  *
6521  * Takes a sata_drive_info t * and the address of a buffer
6522  * in which to create the page information as well as a sata_hba_inst_t *.
6523  *
6524  * Returns the number of bytes valid in the buffer.
6525  *
6526  * Note: Self test and SMART data is accessible in device log pages.
6527  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
6528  * of data can be transferred by a single command), or by the General Purpose
6529  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
6530  * - approximately 33MB - can be transferred by a single command.
6531  * The SCT Command response (either error or command) is the same for both
6532  * the SMART and GPL methods of issuing commands.
6533  * This function uses READ LOG EXT command when drive supports LBA48, and
6534  * SMART READ command otherwise.
6535  *
6536  * Since above commands are executed in a synchronous mode, this function
6537  * should not be called in an interrupt context.
6538  */
6539 static	int
6540 sata_build_lsense_page_10(
6541 	sata_drive_info_t *sdinfo,
6542 	uint8_t *buf,
6543 	sata_hba_inst_t *sata_hba_inst)
6544 {
6545 	struct log_parameter *lpp = (struct log_parameter *)buf;
6546 	int rval;
6547 
6548 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6549 		struct smart_ext_selftest_log *ext_selftest_log;
6550 
6551 		ext_selftest_log = kmem_zalloc(
6552 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6553 
6554 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6555 		    ext_selftest_log, 0);
6556 		if (rval == 0) {
6557 			int index, start_index;
6558 			struct smart_ext_selftest_log_entry *entry;
6559 			static const struct smart_ext_selftest_log_entry empty =
6560 			    {0};
6561 			uint16_t block_num;
6562 			int count;
6563 			boolean_t only_one_block = B_FALSE;
6564 
6565 			index = ext_selftest_log->
6566 			    smart_ext_selftest_log_index[0];
6567 			index |= ext_selftest_log->
6568 			    smart_ext_selftest_log_index[1] << 8;
6569 			if (index == 0)
6570 				goto out;
6571 
6572 			--index;	/* Correct for 0 origin */
6573 			start_index = index;	/* remember where we started */
6574 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6575 			if (block_num != 0) {
6576 				rval = sata_ext_smart_selftest_read_log(
6577 				    sata_hba_inst, sdinfo, ext_selftest_log,
6578 				    block_num);
6579 				if (rval != 0)
6580 					goto out;
6581 			}
6582 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6583 			entry =
6584 			    &ext_selftest_log->
6585 			    smart_ext_selftest_log_entries[index];
6586 
6587 			for (count = 1;
6588 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6589 			    ++count) {
6590 				uint8_t status;
6591 				uint8_t code;
6592 				uint8_t sense_key;
6593 				uint8_t add_sense_code;
6594 				uint8_t add_sense_code_qual;
6595 
6596 				/* If this is an unused entry, we are done */
6597 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6598 					/* Broken firmware on some disks */
6599 					if (index + 1 ==
6600 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6601 						--entry;
6602 						--index;
6603 						if (bcmp(entry, &empty,
6604 						    sizeof (empty)) == 0)
6605 							goto out;
6606 					} else
6607 						goto out;
6608 				}
6609 
6610 				if (only_one_block &&
6611 				    start_index == index)
6612 					goto out;
6613 
6614 				lpp->param_code[0] = 0;
6615 				lpp->param_code[1] = count;
6616 				lpp->param_ctrl_flags =
6617 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6618 				lpp->param_len =
6619 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6620 
6621 				status = entry->smart_ext_selftest_log_status;
6622 				status >>= 4;
6623 				switch (status) {
6624 				case 0:
6625 				default:
6626 					sense_key = KEY_NO_SENSE;
6627 					add_sense_code =
6628 					    SD_SCSI_ASC_NO_ADD_SENSE;
6629 					add_sense_code_qual = 0;
6630 					break;
6631 				case 1:
6632 					sense_key = KEY_ABORTED_COMMAND;
6633 					add_sense_code =
6634 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6635 					add_sense_code_qual = SCSI_COMPONENT_81;
6636 					break;
6637 				case 2:
6638 					sense_key = KEY_ABORTED_COMMAND;
6639 					add_sense_code =
6640 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6641 					add_sense_code_qual = SCSI_COMPONENT_82;
6642 					break;
6643 				case 3:
6644 					sense_key = KEY_ABORTED_COMMAND;
6645 					add_sense_code =
6646 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6647 					add_sense_code_qual = SCSI_COMPONENT_83;
6648 					break;
6649 				case 4:
6650 					sense_key = KEY_HARDWARE_ERROR;
6651 					add_sense_code =
6652 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6653 					add_sense_code_qual = SCSI_COMPONENT_84;
6654 					break;
6655 				case 5:
6656 					sense_key = KEY_HARDWARE_ERROR;
6657 					add_sense_code =
6658 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6659 					add_sense_code_qual = SCSI_COMPONENT_85;
6660 					break;
6661 				case 6:
6662 					sense_key = KEY_HARDWARE_ERROR;
6663 					add_sense_code =
6664 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6665 					add_sense_code_qual = SCSI_COMPONENT_86;
6666 					break;
6667 				case 7:
6668 					sense_key = KEY_MEDIUM_ERROR;
6669 					add_sense_code =
6670 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6671 					add_sense_code_qual = SCSI_COMPONENT_87;
6672 					break;
6673 				case 8:
6674 					sense_key = KEY_HARDWARE_ERROR;
6675 					add_sense_code =
6676 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6677 					add_sense_code_qual = SCSI_COMPONENT_88;
6678 					break;
6679 				}
6680 				code = 0;	/* unspecified */
6681 				status |= (code << 4);
6682 				lpp->param_values[0] = status;
6683 				lpp->param_values[1] = 0; /* unspecified */
6684 				lpp->param_values[2] = entry->
6685 				    smart_ext_selftest_log_timestamp[1];
6686 				lpp->param_values[3] = entry->
6687 				    smart_ext_selftest_log_timestamp[0];
6688 				if (status != 0) {
6689 					lpp->param_values[4] = 0;
6690 					lpp->param_values[5] = 0;
6691 					lpp->param_values[6] = entry->
6692 					    smart_ext_selftest_log_failing_lba
6693 					    [5];
6694 					lpp->param_values[7] = entry->
6695 					    smart_ext_selftest_log_failing_lba
6696 					    [4];
6697 					lpp->param_values[8] = entry->
6698 					    smart_ext_selftest_log_failing_lba
6699 					    [3];
6700 					lpp->param_values[9] = entry->
6701 					    smart_ext_selftest_log_failing_lba
6702 					    [2];
6703 					lpp->param_values[10] = entry->
6704 					    smart_ext_selftest_log_failing_lba
6705 					    [1];
6706 					lpp->param_values[11] = entry->
6707 					    smart_ext_selftest_log_failing_lba
6708 					    [0];
6709 				} else {	/* No bad block address */
6710 					lpp->param_values[4] = 0xff;
6711 					lpp->param_values[5] = 0xff;
6712 					lpp->param_values[6] = 0xff;
6713 					lpp->param_values[7] = 0xff;
6714 					lpp->param_values[8] = 0xff;
6715 					lpp->param_values[9] = 0xff;
6716 					lpp->param_values[10] = 0xff;
6717 					lpp->param_values[11] = 0xff;
6718 				}
6719 
6720 				lpp->param_values[12] = sense_key;
6721 				lpp->param_values[13] = add_sense_code;
6722 				lpp->param_values[14] = add_sense_code_qual;
6723 				lpp->param_values[15] = 0; /* undefined */
6724 
6725 				lpp = (struct log_parameter *)
6726 				    (((uint8_t *)lpp) +
6727 				    SCSI_LOG_PARAM_HDR_LEN +
6728 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6729 
6730 				--index;	/* Back up to previous entry */
6731 				if (index < 0) {
6732 					if (block_num > 0) {
6733 						--block_num;
6734 					} else {
6735 						struct read_log_ext_directory
6736 						    logdir;
6737 
6738 						rval =
6739 						    sata_read_log_ext_directory(
6740 						    sata_hba_inst, sdinfo,
6741 						    &logdir);
6742 						if (rval == -1)
6743 							goto out;
6744 						if ((logdir.read_log_ext_vers
6745 						    [0] == 0) &&
6746 						    (logdir.read_log_ext_vers
6747 						    [1] == 0))
6748 							goto out;
6749 						block_num =
6750 						    logdir.read_log_ext_nblks
6751 						    [EXT_SMART_SELFTEST_LOG_PAGE
6752 						    - 1][0];
6753 						block_num |= logdir.
6754 						    read_log_ext_nblks
6755 						    [EXT_SMART_SELFTEST_LOG_PAGE
6756 						    - 1][1] << 8;
6757 						--block_num;
6758 						only_one_block =
6759 						    (block_num == 0);
6760 					}
6761 					rval = sata_ext_smart_selftest_read_log(
6762 					    sata_hba_inst, sdinfo,
6763 					    ext_selftest_log, block_num);
6764 					if (rval != 0)
6765 						goto out;
6766 
6767 					index =
6768 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6769 					    1;
6770 				}
6771 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6772 				entry = &ext_selftest_log->
6773 				    smart_ext_selftest_log_entries[index];
6774 			}
6775 		}
6776 out:
6777 		kmem_free(ext_selftest_log,
6778 		    sizeof (struct smart_ext_selftest_log));
6779 	} else {
6780 		struct smart_selftest_log *selftest_log;
6781 
6782 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6783 		    KM_SLEEP);
6784 
6785 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6786 		    selftest_log);
6787 
6788 		if (rval == 0) {
6789 			int index;
6790 			int count;
6791 			struct smart_selftest_log_entry *entry;
6792 			static const struct smart_selftest_log_entry empty =
6793 			    { 0 };
6794 
6795 			index = selftest_log->smart_selftest_log_index;
6796 			if (index == 0)
6797 				goto done;
6798 			--index;	/* Correct for 0 origin */
6799 			entry = &selftest_log->
6800 			    smart_selftest_log_entries[index];
6801 			for (count = 1;
6802 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6803 			    ++count) {
6804 				uint8_t status;
6805 				uint8_t code;
6806 				uint8_t sense_key;
6807 				uint8_t add_sense_code;
6808 				uint8_t add_sense_code_qual;
6809 
6810 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6811 					goto done;
6812 
6813 				lpp->param_code[0] = 0;
6814 				lpp->param_code[1] = count;
6815 				lpp->param_ctrl_flags =
6816 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6817 				lpp->param_len =
6818 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6819 
6820 				status = entry->smart_selftest_log_status;
6821 				status >>= 4;
6822 				switch (status) {
6823 				case 0:
6824 				default:
6825 					sense_key = KEY_NO_SENSE;
6826 					add_sense_code =
6827 					    SD_SCSI_ASC_NO_ADD_SENSE;
6828 					break;
6829 				case 1:
6830 					sense_key = KEY_ABORTED_COMMAND;
6831 					add_sense_code =
6832 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6833 					add_sense_code_qual = SCSI_COMPONENT_81;
6834 					break;
6835 				case 2:
6836 					sense_key = KEY_ABORTED_COMMAND;
6837 					add_sense_code =
6838 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6839 					add_sense_code_qual = SCSI_COMPONENT_82;
6840 					break;
6841 				case 3:
6842 					sense_key = KEY_ABORTED_COMMAND;
6843 					add_sense_code =
6844 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6845 					add_sense_code_qual = SCSI_COMPONENT_83;
6846 					break;
6847 				case 4:
6848 					sense_key = KEY_HARDWARE_ERROR;
6849 					add_sense_code =
6850 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6851 					add_sense_code_qual = SCSI_COMPONENT_84;
6852 					break;
6853 				case 5:
6854 					sense_key = KEY_HARDWARE_ERROR;
6855 					add_sense_code =
6856 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6857 					add_sense_code_qual = SCSI_COMPONENT_85;
6858 					break;
6859 				case 6:
6860 					sense_key = KEY_HARDWARE_ERROR;
6861 					add_sense_code =
6862 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6863 					add_sense_code_qual = SCSI_COMPONENT_86;
6864 					break;
6865 				case 7:
6866 					sense_key = KEY_MEDIUM_ERROR;
6867 					add_sense_code =
6868 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6869 					add_sense_code_qual = SCSI_COMPONENT_87;
6870 					break;
6871 				case 8:
6872 					sense_key = KEY_HARDWARE_ERROR;
6873 					add_sense_code =
6874 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6875 					add_sense_code_qual = SCSI_COMPONENT_88;
6876 					break;
6877 				}
6878 				code = 0;	/* unspecified */
6879 				status |= (code << 4);
6880 				lpp->param_values[0] = status;
6881 				lpp->param_values[1] = 0; /* unspecified */
6882 				lpp->param_values[2] = entry->
6883 				    smart_selftest_log_timestamp[1];
6884 				lpp->param_values[3] = entry->
6885 				    smart_selftest_log_timestamp[0];
6886 				if (status != 0) {
6887 					lpp->param_values[4] = 0;
6888 					lpp->param_values[5] = 0;
6889 					lpp->param_values[6] = 0;
6890 					lpp->param_values[7] = 0;
6891 					lpp->param_values[8] = entry->
6892 					    smart_selftest_log_failing_lba[3];
6893 					lpp->param_values[9] = entry->
6894 					    smart_selftest_log_failing_lba[2];
6895 					lpp->param_values[10] = entry->
6896 					    smart_selftest_log_failing_lba[1];
6897 					lpp->param_values[11] = entry->
6898 					    smart_selftest_log_failing_lba[0];
6899 				} else {	/* No block address */
6900 					lpp->param_values[4] = 0xff;
6901 					lpp->param_values[5] = 0xff;
6902 					lpp->param_values[6] = 0xff;
6903 					lpp->param_values[7] = 0xff;
6904 					lpp->param_values[8] = 0xff;
6905 					lpp->param_values[9] = 0xff;
6906 					lpp->param_values[10] = 0xff;
6907 					lpp->param_values[11] = 0xff;
6908 				}
6909 				lpp->param_values[12] = sense_key;
6910 				lpp->param_values[13] = add_sense_code;
6911 				lpp->param_values[14] = add_sense_code_qual;
6912 				lpp->param_values[15] = 0; /* undefined */
6913 
6914 				lpp = (struct log_parameter *)
6915 				    (((uint8_t *)lpp) +
6916 				    SCSI_LOG_PARAM_HDR_LEN +
6917 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6918 				--index;	/* back up to previous entry */
6919 				if (index < 0) {
6920 					index =
6921 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6922 				}
6923 				entry = &selftest_log->
6924 				    smart_selftest_log_entries[index];
6925 			}
6926 		}
6927 done:
6928 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6929 	}
6930 
6931 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6932 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6933 }
6934 
6935 /*
6936  * sata_build_lsense_page_2f() is used to create the
6937  * SCSI LOG SENSE page 0x2f (informational exceptions)
6938  *
6939  * Takes a sata_drive_info t * and the address of a buffer
6940  * in which to create the page information as well as a sata_hba_inst_t *.
6941  *
6942  * Returns the number of bytes valid in the buffer.
6943  *
6944  * Because it invokes function(s) that send synchronously executed command
6945  * to the HBA, it cannot be called in the interrupt context.
6946  */
6947 static	int
6948 sata_build_lsense_page_2f(
6949 	sata_drive_info_t *sdinfo,
6950 	uint8_t *buf,
6951 	sata_hba_inst_t *sata_hba_inst)
6952 {
6953 	struct log_parameter *lpp = (struct log_parameter *)buf;
6954 	int rval;
6955 	uint8_t *smart_data;
6956 	uint8_t temp;
6957 	sata_id_t *sata_id;
6958 #define	SMART_NO_TEMP	0xff
6959 
6960 	lpp->param_code[0] = 0;
6961 	lpp->param_code[1] = 0;
6962 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6963 
6964 	/* Now get the SMART status w.r.t. threshold exceeded */
6965 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6966 	switch (rval) {
6967 	case 1:
6968 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6969 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6970 		break;
6971 	case 0:
6972 	case -1:	/* failed to get data */
6973 		lpp->param_values[0] = 0;	/* No failure predicted */
6974 		lpp->param_values[1] = 0;
6975 		break;
6976 #if defined(SATA_DEBUG)
6977 	default:
6978 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6979 		/* NOTREACHED */
6980 #endif
6981 	}
6982 
6983 	sata_id = &sdinfo->satadrv_id;
6984 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6985 		temp = SMART_NO_TEMP;
6986 	else {
6987 		/* Now get the temperature */
6988 		smart_data = kmem_zalloc(512, KM_SLEEP);
6989 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6990 		    SCT_STATUS_LOG_PAGE, 1);
6991 		if (rval == -1)
6992 			temp = SMART_NO_TEMP;
6993 		else {
6994 			temp = smart_data[200];
6995 			if (temp & 0x80) {
6996 				if (temp & 0x7f)
6997 					temp = 0;
6998 				else
6999 					temp = SMART_NO_TEMP;
7000 			}
7001 		}
7002 		kmem_free(smart_data, 512);
7003 	}
7004 
7005 	lpp->param_values[2] = temp;	/* most recent temperature */
7006 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7007 
7008 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7009 
7010 
7011 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7012 }
7013 
7014 /*
7015  * sata_build_lsense_page_30() is used to create the
7016  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7017  *
7018  * Takes a sata_drive_info t * and the address of a buffer
7019  * in which to create the page information as well as a sata_hba_inst_t *.
7020  *
7021  * Returns the number of bytes valid in the buffer.
7022  */
7023 static int
7024 sata_build_lsense_page_30(
7025 	sata_drive_info_t *sdinfo,
7026 	uint8_t *buf,
7027 	sata_hba_inst_t *sata_hba_inst)
7028 {
7029 	struct smart_data *smart_data = (struct smart_data *)buf;
7030 	int rval;
7031 
7032 	/* Now do the SMART READ DATA */
7033 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7034 	if (rval == -1)
7035 		return (0);
7036 
7037 	return (sizeof (struct smart_data));
7038 }
7039 
7040 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7041 
7042 /*
7043  * Start command for ATAPI device.
7044  * This function processes scsi_pkt requests.
7045  * Now CD/DVD, tape and ATAPI disk devices are supported.
7046  * Most commands are packet without any translation into Packet Command.
7047  * Some may be trapped and executed as SATA commands (not clear which one).
7048  *
7049  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7050  * execution).
7051  * Returns other TRAN_XXXX codes if command is not accepted or completed
7052  * (see return values for sata_hba_start()).
7053  *
7054  * Note:
7055  * Inquiry cdb format differs between transport version 2 and 3.
7056  * However, the transport version 3 devices that were checked did not adhere
7057  * to the specification (ignored MSB of the allocation length). Therefore,
7058  * the transport version is not checked, but Inquiry allocation length is
7059  * truncated to 255 bytes if the original allocation length set-up by the
7060  * target driver is greater than 255 bytes.
7061  */
7062 static int
7063 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7064 {
7065 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7066 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7067 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7068 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7069 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7070 	    &spx->txlt_sata_pkt->satapkt_device);
7071 	int cport = SATA_TXLT_CPORT(spx);
7072 	int cdblen;
7073 	int rval, reason;
7074 	int synch;
7075 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7076 
7077 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7078 
7079 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
7080 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7081 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7082 		return (rval);
7083 	}
7084 
7085 	/*
7086 	 * ATAPI device executes some ATA commands in addition to those
7087 	 * commands sent via PACKET command. These ATA commands may be
7088 	 * executed by the regular SATA translation functions. None needs
7089 	 * to be captured now.
7090 	 *
7091 	 * Commands sent via PACKET command include:
7092 	 *	MMC command set for ATAPI CD/DVD device
7093 	 *	SSC command set for ATAPI TAPE device
7094 	 *	SBC command set for ATAPI disk device
7095 	 *
7096 	 */
7097 
7098 	/* Check the size of cdb */
7099 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7100 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7101 		sata_log(NULL, CE_WARN,
7102 		    "sata: invalid ATAPI cdb length %d",
7103 		    scsipkt->pkt_cdblen);
7104 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7105 		return (TRAN_BADPKT);
7106 	}
7107 
7108 	SATAATAPITRACE(spx, cdblen);
7109 
7110 	/*
7111 	 * For non-read/write commands we need to
7112 	 * map buffer
7113 	 */
7114 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7115 	case SCMD_READ:
7116 	case SCMD_READ_G1:
7117 	case SCMD_READ_G5:
7118 	case SCMD_READ_G4:
7119 	case SCMD_WRITE:
7120 	case SCMD_WRITE_G1:
7121 	case SCMD_WRITE_G5:
7122 	case SCMD_WRITE_G4:
7123 		break;
7124 	default:
7125 		if (bp != NULL) {
7126 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7127 				bp_mapin(bp);
7128 		}
7129 		break;
7130 	}
7131 	/*
7132 	 * scmd->satacmd_flags.sata_data_direction default -
7133 	 * SATA_DIR_NODATA_XFER - is set by
7134 	 * sata_txlt_generic_pkt_info().
7135 	 */
7136 	if (scmd->satacmd_bp) {
7137 		if (scmd->satacmd_bp->b_flags & B_READ) {
7138 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7139 		} else {
7140 			scmd->satacmd_flags.sata_data_direction =
7141 			    SATA_DIR_WRITE;
7142 		}
7143 	}
7144 
7145 	/*
7146 	 * Set up ATAPI packet command.
7147 	 */
7148 
7149 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7150 
7151 	/* Copy cdb into sata_cmd */
7152 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7153 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7154 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7155 
7156 	/* See note in the command header */
7157 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7158 		if (scmd->satacmd_acdb[3] != 0)
7159 			scmd->satacmd_acdb[4] = 255;
7160 	}
7161 
7162 #ifdef SATA_DEBUG
7163 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7164 		uint8_t *p = scmd->satacmd_acdb;
7165 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7166 
7167 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7168 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7169 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7170 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7171 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7172 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7173 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7174 	}
7175 #endif
7176 
7177 	/*
7178 	 * Preset request sense data to NO SENSE.
7179 	 * If there is no way to get error information via Request Sense,
7180 	 * the packet request sense data would not have to be modified by HBA,
7181 	 * but it could be returned as is.
7182 	 */
7183 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7184 	sata_fixed_sense_data_preset(
7185 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7186 
7187 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7188 		/* Need callback function */
7189 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7190 		synch = FALSE;
7191 	} else
7192 		synch = TRUE;
7193 
7194 	/* Transfer command to HBA */
7195 	if (sata_hba_start(spx, &rval) != 0) {
7196 		/* Pkt not accepted for execution */
7197 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7198 		return (rval);
7199 	}
7200 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7201 	/*
7202 	 * If execution is non-synchronous,
7203 	 * a callback function will handle potential errors, translate
7204 	 * the response and will do a callback to a target driver.
7205 	 * If it was synchronous, use the same framework callback to check
7206 	 * an execution status.
7207 	 */
7208 	if (synch) {
7209 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7210 		    "synchronous execution status %x\n",
7211 		    spx->txlt_sata_pkt->satapkt_reason);
7212 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7213 	}
7214 	return (TRAN_ACCEPT);
7215 }
7216 
7217 
7218 /*
7219  * ATAPI Packet command completion.
7220  *
7221  * Failure of the command passed via Packet command are considered device
7222  * error. SATA HBA driver would have to retrieve error data (via Request
7223  * Sense command delivered via error retrieval sata packet) and copy it
7224  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7225  */
7226 static void
7227 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7228 {
7229 	sata_pkt_txlate_t *spx =
7230 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7231 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7232 	struct scsi_extended_sense *sense;
7233 	struct buf *bp;
7234 	int rval;
7235 
7236 #ifdef SATA_DEBUG
7237 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7238 #endif
7239 
7240 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7241 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7242 
7243 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7244 		/* Normal completion */
7245 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7246 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7247 		scsipkt->pkt_reason = CMD_CMPLT;
7248 		*scsipkt->pkt_scbp = STATUS_GOOD;
7249 		if (spx->txlt_tmp_buf != NULL) {
7250 			/* Temporary buffer was used */
7251 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7252 			if (bp->b_flags & B_READ) {
7253 				rval = ddi_dma_sync(
7254 				    spx->txlt_buf_dma_handle, 0, 0,
7255 				    DDI_DMA_SYNC_FORCPU);
7256 				ASSERT(rval == DDI_SUCCESS);
7257 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7258 				    bp->b_bcount);
7259 			}
7260 		}
7261 	} else {
7262 		/*
7263 		 * Something went wrong - analyze return
7264 		 */
7265 		*scsipkt->pkt_scbp = STATUS_CHECK;
7266 		sense = sata_arq_sense(spx);
7267 
7268 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7269 			/*
7270 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
7271 			 * Under this condition ERR bit is set for ATA command,
7272 			 * and CHK bit set for ATAPI command.
7273 			 *
7274 			 * Please check st_intr & sdintr about how pkt_reason
7275 			 * is used.
7276 			 */
7277 			scsipkt->pkt_reason = CMD_CMPLT;
7278 
7279 			/*
7280 			 * We may not have ARQ data if there was a double
7281 			 * error. But sense data in sata packet was pre-set
7282 			 * with NO SENSE so it is valid even if HBA could
7283 			 * not retrieve a real sense data.
7284 			 * Just copy this sense data into scsi pkt sense area.
7285 			 */
7286 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7287 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7288 #ifdef SATA_DEBUG
7289 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7290 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7291 				    "sata_txlt_atapi_completion: %02x\n"
7292 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7293 				    "          %02x %02x %02x %02x %02x %02x "
7294 				    "          %02x %02x %02x %02x %02x %02x\n",
7295 				    scsipkt->pkt_reason,
7296 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7297 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7298 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7299 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7300 				    rqsp[16], rqsp[17]);
7301 			}
7302 #endif
7303 		} else {
7304 			switch (sata_pkt->satapkt_reason) {
7305 			case SATA_PKT_PORT_ERROR:
7306 				/*
7307 				 * We have no device data.
7308 				 */
7309 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7310 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7311 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7312 				    STATE_GOT_STATUS);
7313 				sense->es_key = KEY_HARDWARE_ERROR;
7314 				break;
7315 
7316 			case SATA_PKT_TIMEOUT:
7317 				scsipkt->pkt_reason = CMD_TIMEOUT;
7318 				scsipkt->pkt_statistics |=
7319 				    STAT_TIMEOUT | STAT_DEV_RESET;
7320 				/*
7321 				 * Need to check if HARDWARE_ERROR/
7322 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7323 				 * appropriate.
7324 				 */
7325 				break;
7326 
7327 			case SATA_PKT_ABORTED:
7328 				scsipkt->pkt_reason = CMD_ABORTED;
7329 				scsipkt->pkt_statistics |= STAT_ABORTED;
7330 				/* Should we set key COMMAND_ABPRTED? */
7331 				break;
7332 
7333 			case SATA_PKT_RESET:
7334 				scsipkt->pkt_reason = CMD_RESET;
7335 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7336 				/*
7337 				 * May be we should set Unit Attention /
7338 				 * Reset. Perhaps the same should be
7339 				 * returned for disks....
7340 				 */
7341 				sense->es_key = KEY_UNIT_ATTENTION;
7342 				sense->es_add_code = SD_SCSI_ASC_RESET;
7343 				break;
7344 
7345 			default:
7346 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7347 				    "sata_txlt_atapi_completion: "
7348 				    "invalid packet completion reason"));
7349 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7350 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7351 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7352 				    STATE_GOT_STATUS);
7353 				break;
7354 			}
7355 		}
7356 	}
7357 
7358 	SATAATAPITRACE(spx, 0);
7359 
7360 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7361 	    scsipkt->pkt_comp != NULL) {
7362 		/* scsi callback required */
7363 		(*scsipkt->pkt_comp)(scsipkt);
7364 	}
7365 }
7366 
7367 /*
7368  * Set up error retrieval sata command for ATAPI Packet Command error data
7369  * recovery.
7370  *
7371  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7372  * returns SATA_FAILURE otherwise.
7373  */
7374 
7375 static int
7376 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7377 {
7378 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7379 	sata_cmd_t *scmd;
7380 	struct buf *bp;
7381 
7382 	/*
7383 	 * Allocate dma-able buffer error data.
7384 	 * Buffer allocation will take care of buffer alignment and other DMA
7385 	 * attributes.
7386 	 */
7387 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7388 	if (bp == NULL) {
7389 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7390 		    "sata_get_err_retrieval_pkt: "
7391 		    "cannot allocate buffer for error data", NULL);
7392 		return (SATA_FAILURE);
7393 	}
7394 	bp_mapin(bp); /* make data buffer accessible */
7395 
7396 	/* Operation modes are up to the caller */
7397 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7398 
7399 	/* Synchronous mode, no callback - may be changed by the caller */
7400 	spkt->satapkt_comp = NULL;
7401 	spkt->satapkt_time = sata_default_pkt_time;
7402 
7403 	scmd = &spkt->satapkt_cmd;
7404 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7405 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7406 
7407 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7408 
7409 	/*
7410 	 * Set-up acdb. Request Sense CDB (packet command content) is
7411 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7412 	 * it is transfered into packet FIS).
7413 	 */
7414 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7415 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7416 	/* Following zeroing of pad bytes may not be necessary */
7417 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7418 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7419 
7420 	/*
7421 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7422 	 * before accessing it. Handle is in usual place in translate struct.
7423 	 */
7424 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7425 
7426 	/*
7427 	 * Preset request sense data to NO SENSE.
7428 	 * Here it is redundant, only for a symetry with scsi-originated
7429 	 * packets. It should not be used for anything but debugging.
7430 	 */
7431 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7432 	sata_fixed_sense_data_preset(
7433 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7434 
7435 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7436 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7437 
7438 	return (SATA_SUCCESS);
7439 }
7440 
7441 /*
7442  * Set-up ATAPI packet command.
7443  * Data transfer direction has to be set-up in sata_cmd structure prior to
7444  * calling this function.
7445  *
7446  * Returns void
7447  */
7448 
7449 static void
7450 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7451 {
7452 	scmd->satacmd_addr_type = 0;		/* N/A */
7453 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7454 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7455 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7456 	scmd->satacmd_lba_high_lsb =
7457 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7458 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7459 
7460 	/*
7461 	 * We want all data to be transfered via DMA.
7462 	 * But specify it only if drive supports DMA and DMA mode is
7463 	 * selected - some drives are sensitive about it.
7464 	 * Hopefully it wil work for all drives....
7465 	 */
7466 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7467 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7468 
7469 	/*
7470 	 * Features register requires special care for devices that use
7471 	 * Serial ATA bridge - they need an explicit specification of
7472 	 * the data transfer direction for Packet DMA commands.
7473 	 * Setting this bit is harmless if DMA is not used.
7474 	 *
7475 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7476 	 * spec they follow.
7477 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7478 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7479 	 * ATA/ATAPI-7 support is explicitly indicated.
7480 	 */
7481 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7482 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7483 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7484 		/*
7485 		 * Specification of major version is valid and version 7
7486 		 * is supported. It does automatically imply that all
7487 		 * spec features are supported. For now, we assume that
7488 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7489 		 */
7490 		if ((sdinfo->satadrv_id.ai_dirdma &
7491 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7492 			if (scmd->satacmd_flags.sata_data_direction ==
7493 			    SATA_DIR_READ)
7494 			scmd->satacmd_features_reg |=
7495 			    SATA_ATAPI_F_DATA_DIR_READ;
7496 		}
7497 	}
7498 }
7499 
7500 
7501 #ifdef SATA_DEBUG
7502 
7503 /* Display 18 bytes of Inquiry data */
7504 static void
7505 sata_show_inqry_data(uint8_t *buf)
7506 {
7507 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7508 	uint8_t *p;
7509 
7510 	cmn_err(CE_NOTE, "Inquiry data:");
7511 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7512 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7513 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7514 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7515 	    SATA_ATAPI_TRANS_VERSION(inq));
7516 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7517 	    inq->inq_rdf, inq->inq_aenc);
7518 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7519 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7520 	p = (uint8_t *)inq->inq_vid;
7521 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7522 	    "%02x %02x %02x %02x",
7523 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7524 	p = (uint8_t *)inq->inq_vid;
7525 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7526 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7527 
7528 	p = (uint8_t *)inq->inq_pid;
7529 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7530 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7531 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7532 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7533 	p = (uint8_t *)inq->inq_pid;
7534 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7535 	    "%c %c %c %c %c %c %c %c",
7536 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7537 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7538 
7539 	p = (uint8_t *)inq->inq_revision;
7540 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7541 	    p[0], p[1], p[2], p[3]);
7542 	p = (uint8_t *)inq->inq_revision;
7543 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7544 	    p[0], p[1], p[2], p[3]);
7545 
7546 }
7547 
7548 
7549 static void
7550 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7551 {
7552 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7553 
7554 	if (scsi_pkt == NULL)
7555 		return;
7556 	if (count != 0) {
7557 		/* saving cdb */
7558 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7559 		    SATA_ATAPI_MAX_CDB_LEN);
7560 		bcopy(scsi_pkt->pkt_cdbp,
7561 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7562 	} else {
7563 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7564 		    sts_sensedata,
7565 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7566 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7567 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7568 		    scsi_pkt->pkt_reason;
7569 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7570 		    spx->txlt_sata_pkt->satapkt_reason;
7571 
7572 		if (++sata_atapi_trace_index >= 64)
7573 			sata_atapi_trace_index = 0;
7574 	}
7575 }
7576 
7577 #endif
7578 
7579 /*
7580  * Fetch inquiry data from ATAPI device
7581  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7582  *
7583  * Note:
7584  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7585  * where the caller expects to see the inquiry data.
7586  *
7587  */
7588 
7589 static int
7590 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7591     sata_address_t *saddr, struct scsi_inquiry *inq)
7592 {
7593 	sata_pkt_txlate_t *spx;
7594 	sata_pkt_t *spkt;
7595 	struct buf *bp;
7596 	sata_drive_info_t *sdinfo;
7597 	sata_cmd_t *scmd;
7598 	int rval;
7599 	uint8_t *rqsp;
7600 #ifdef SATA_DEBUG
7601 	char msg_buf[MAXPATHLEN];
7602 #endif
7603 
7604 	ASSERT(sata_hba != NULL);
7605 
7606 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7607 	spx->txlt_sata_hba_inst = sata_hba;
7608 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7609 	spkt = sata_pkt_alloc(spx, NULL);
7610 	if (spkt == NULL) {
7611 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7612 		return (SATA_FAILURE);
7613 	}
7614 	/* address is needed now */
7615 	spkt->satapkt_device.satadev_addr = *saddr;
7616 
7617 	/* scsi_inquiry size buffer */
7618 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7619 	if (bp == NULL) {
7620 		sata_pkt_free(spx);
7621 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7622 		SATA_LOG_D((sata_hba, CE_WARN,
7623 		    "sata_get_atapi_inquiry_data: "
7624 		    "cannot allocate data buffer"));
7625 		return (SATA_FAILURE);
7626 	}
7627 	bp_mapin(bp); /* make data buffer accessible */
7628 
7629 	scmd = &spkt->satapkt_cmd;
7630 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7631 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7632 
7633 	/* Use synchronous mode */
7634 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7635 	spkt->satapkt_comp = NULL;
7636 	spkt->satapkt_time = sata_default_pkt_time;
7637 
7638 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7639 
7640 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7641 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7642 
7643 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7644 	sdinfo = sata_get_device_info(sata_hba,
7645 	    &spx->txlt_sata_pkt->satapkt_device);
7646 	if (sdinfo == NULL) {
7647 		/* we have to be carefull about the disapearing device */
7648 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7649 		rval = SATA_FAILURE;
7650 		goto cleanup;
7651 	}
7652 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7653 
7654 	/*
7655 	 * Set-up acdb. This works for atapi transport version 2 and later.
7656 	 */
7657 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7658 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7659 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7660 	scmd->satacmd_acdb[1] = 0x00;
7661 	scmd->satacmd_acdb[2] = 0x00;
7662 	scmd->satacmd_acdb[3] = 0x00;
7663 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7664 	scmd->satacmd_acdb[5] = 0x00;
7665 
7666 	sata_fixed_sense_data_preset(
7667 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7668 
7669 	/* Transfer command to HBA */
7670 	if (sata_hba_start(spx, &rval) != 0) {
7671 		/* Pkt not accepted for execution */
7672 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7673 		    "sata_get_atapi_inquiry_data: "
7674 		    "Packet not accepted for execution - ret: %02x", rval);
7675 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7676 		rval = SATA_FAILURE;
7677 		goto cleanup;
7678 	}
7679 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7680 
7681 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7682 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7683 		    "sata_get_atapi_inquiry_data: "
7684 		    "Packet completed successfully - ret: %02x", rval);
7685 		if (spx->txlt_buf_dma_handle != NULL) {
7686 			/*
7687 			 * Sync buffer. Handle is in usual place in translate
7688 			 * struct.
7689 			 */
7690 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7691 			    DDI_DMA_SYNC_FORCPU);
7692 			ASSERT(rval == DDI_SUCCESS);
7693 		}
7694 		/*
7695 		 * Normal completion - copy data into caller's buffer
7696 		 */
7697 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7698 		    sizeof (struct scsi_inquiry));
7699 #ifdef SATA_DEBUG
7700 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7701 			sata_show_inqry_data((uint8_t *)inq);
7702 		}
7703 #endif
7704 		rval = SATA_SUCCESS;
7705 	} else {
7706 		/*
7707 		 * Something went wrong - analyze return - check rqsense data
7708 		 */
7709 		rval = SATA_FAILURE;
7710 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7711 			/*
7712 			 * ARQ data hopefull show something other than NO SENSE
7713 			 */
7714 			rqsp = scmd->satacmd_rqsense;
7715 #ifdef SATA_DEBUG
7716 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7717 				msg_buf[0] = '\0';
7718 				(void) snprintf(msg_buf, MAXPATHLEN,
7719 				    "ATAPI packet completion reason: %02x\n"
7720 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7721 				    "          %02x %02x %02x %02x %02x %02x\n"
7722 				    "          %02x %02x %02x %02x %02x %02x",
7723 				    spkt->satapkt_reason,
7724 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7725 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7726 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7727 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7728 				    rqsp[16], rqsp[17]);
7729 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7730 				    "%s", msg_buf);
7731 			}
7732 #endif
7733 		} else {
7734 			switch (spkt->satapkt_reason) {
7735 			case SATA_PKT_PORT_ERROR:
7736 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7737 				    "sata_get_atapi_inquiry_data: "
7738 				    "packet reason: port error", NULL);
7739 				break;
7740 
7741 			case SATA_PKT_TIMEOUT:
7742 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7743 				    "sata_get_atapi_inquiry_data: "
7744 				    "packet reason: timeout", NULL);
7745 				break;
7746 
7747 			case SATA_PKT_ABORTED:
7748 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7749 				    "sata_get_atapi_inquiry_data: "
7750 				    "packet reason: aborted", NULL);
7751 				break;
7752 
7753 			case SATA_PKT_RESET:
7754 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7755 				    "sata_get_atapi_inquiry_data: "
7756 				    "packet reason: reset\n", NULL);
7757 				break;
7758 			default:
7759 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7760 				    "sata_get_atapi_inquiry_data: "
7761 				    "invalid packet reason: %02x\n",
7762 				    spkt->satapkt_reason);
7763 				break;
7764 			}
7765 		}
7766 	}
7767 cleanup:
7768 	sata_free_local_buffer(spx);
7769 	sata_pkt_free(spx);
7770 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7771 	return (rval);
7772 }
7773 
7774 
7775 
7776 
7777 
7778 #if 0
7779 #ifdef SATA_DEBUG
7780 
7781 /*
7782  * Test ATAPI packet command.
7783  * Single threaded test: send packet command in synch mode, process completion
7784  *
7785  */
7786 static void
7787 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7788 {
7789 	sata_pkt_txlate_t *spx;
7790 	sata_pkt_t *spkt;
7791 	struct buf *bp;
7792 	sata_device_t sata_device;
7793 	sata_drive_info_t *sdinfo;
7794 	sata_cmd_t *scmd;
7795 	int rval;
7796 	uint8_t *rqsp;
7797 
7798 	ASSERT(sata_hba_inst != NULL);
7799 	sata_device.satadev_addr.cport = cport;
7800 	sata_device.satadev_addr.pmport = 0;
7801 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7802 	sata_device.satadev_rev = SATA_DEVICE_REV;
7803 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7804 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7805 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7806 	if (sdinfo == NULL) {
7807 		sata_log(sata_hba_inst, CE_WARN,
7808 		    "sata_test_atapi_packet_command: "
7809 		    "no device info for cport %d",
7810 		    sata_device.satadev_addr.cport);
7811 		return;
7812 	}
7813 
7814 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7815 	spx->txlt_sata_hba_inst = sata_hba_inst;
7816 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7817 	spkt = sata_pkt_alloc(spx, NULL);
7818 	if (spkt == NULL) {
7819 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7820 		return;
7821 	}
7822 	/* address is needed now */
7823 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7824 
7825 	/* 1024k buffer */
7826 	bp = sata_alloc_local_buffer(spx, 1024);
7827 	if (bp == NULL) {
7828 		sata_pkt_free(spx);
7829 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7830 		sata_log(sata_hba_inst, CE_WARN,
7831 		    "sata_test_atapi_packet_command: "
7832 		    "cannot allocate data buffer");
7833 		return;
7834 	}
7835 	bp_mapin(bp); /* make data buffer accessible */
7836 
7837 	scmd = &spkt->satapkt_cmd;
7838 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7839 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7840 
7841 	/* Use synchronous mode */
7842 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7843 
7844 	/* Synchronous mode, no callback - may be changed by the caller */
7845 	spkt->satapkt_comp = NULL;
7846 	spkt->satapkt_time = sata_default_pkt_time;
7847 
7848 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7849 
7850 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7851 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7852 
7853 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7854 
7855 	/* Set-up acdb. */
7856 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7857 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7858 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7859 	scmd->satacmd_acdb[1] = 0x00;
7860 	scmd->satacmd_acdb[2] = 0x00;
7861 	scmd->satacmd_acdb[3] = 0x00;
7862 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7863 	scmd->satacmd_acdb[5] = 0x00;
7864 
7865 	sata_fixed_sense_data_preset(
7866 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7867 
7868 	/* Transfer command to HBA */
7869 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7870 	if (sata_hba_start(spx, &rval) != 0) {
7871 		/* Pkt not accepted for execution */
7872 		sata_log(sata_hba_inst, CE_WARN,
7873 		    "sata_test_atapi_packet_command: "
7874 		    "Packet not accepted for execution - ret: %02x", rval);
7875 		mutex_exit(
7876 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7877 		goto cleanup;
7878 	}
7879 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7880 
7881 	if (spx->txlt_buf_dma_handle != NULL) {
7882 		/*
7883 		 * Sync buffer. Handle is in usual place in translate struct.
7884 		 */
7885 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7886 		    DDI_DMA_SYNC_FORCPU);
7887 		ASSERT(rval == DDI_SUCCESS);
7888 	}
7889 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7890 		sata_log(sata_hba_inst, CE_WARN,
7891 		    "sata_test_atapi_packet_command: "
7892 		    "Packet completed successfully");
7893 		/*
7894 		 * Normal completion - show inquiry data
7895 		 */
7896 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7897 	} else {
7898 		/*
7899 		 * Something went wrong - analyze return - check rqsense data
7900 		 */
7901 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7902 			/*
7903 			 * ARQ data hopefull show something other than NO SENSE
7904 			 */
7905 			rqsp = scmd->satacmd_rqsense;
7906 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7907 			    "ATAPI packet completion reason: %02x\n"
7908 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7909 			    "          %02x %02x %02x %02x %02x %02x "
7910 			    "          %02x %02x %02x %02x %02x %02x\n",
7911 			    spkt->satapkt_reason,
7912 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7913 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7914 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7915 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7916 			    rqsp[16], rqsp[17]);
7917 		} else {
7918 			switch (spkt->satapkt_reason) {
7919 			case SATA_PKT_PORT_ERROR:
7920 				sata_log(sata_hba_inst, CE_WARN,
7921 				    "sata_test_atapi_packet_command: "
7922 				    "packet reason: port error\n");
7923 				break;
7924 
7925 			case SATA_PKT_TIMEOUT:
7926 				sata_log(sata_hba_inst, CE_WARN,
7927 				    "sata_test_atapi_packet_command: "
7928 				    "packet reason: timeout\n");
7929 				break;
7930 
7931 			case SATA_PKT_ABORTED:
7932 				sata_log(sata_hba_inst, CE_WARN,
7933 				    "sata_test_atapi_packet_command: "
7934 				    "packet reason: aborted\n");
7935 				break;
7936 
7937 			case SATA_PKT_RESET:
7938 				sata_log(sata_hba_inst, CE_WARN,
7939 				    "sata_test_atapi_packet_command: "
7940 				    "packet reason: reset\n");
7941 				break;
7942 			default:
7943 				sata_log(sata_hba_inst, CE_WARN,
7944 				    "sata_test_atapi_packet_command: "
7945 				    "invalid packet reason: %02x\n",
7946 				    spkt->satapkt_reason);
7947 				break;
7948 			}
7949 		}
7950 	}
7951 cleanup:
7952 	sata_free_local_buffer(spx);
7953 	sata_pkt_free(spx);
7954 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7955 }
7956 
7957 #endif /* SATA_DEBUG */
7958 #endif /* 1 */
7959 
7960 
7961 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7962 
7963 /*
7964  * Validate sata_tran info
7965  * SATA_FAILURE returns if structure is inconsistent or structure revision
7966  * does not match one used by the framework.
7967  *
7968  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7969  * required function pointers.
7970  * Returns SATA_FAILURE otherwise.
7971  */
7972 static int
7973 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7974 {
7975 	/*
7976 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7977 	 * of the SATA interface.
7978 	 */
7979 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7980 		sata_log(NULL, CE_WARN,
7981 		    "sata: invalid sata_hba_tran version %d for driver %s",
7982 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7983 		return (SATA_FAILURE);
7984 	}
7985 
7986 	if (dip != sata_tran->sata_tran_hba_dip) {
7987 		SATA_LOG_D((NULL, CE_WARN,
7988 		    "sata: inconsistent sata_tran_hba_dip "
7989 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7990 		return (SATA_FAILURE);
7991 	}
7992 
7993 	if (sata_tran->sata_tran_probe_port == NULL ||
7994 	    sata_tran->sata_tran_start == NULL ||
7995 	    sata_tran->sata_tran_abort == NULL ||
7996 	    sata_tran->sata_tran_reset_dport == NULL ||
7997 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7998 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7999 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
8000 	    NULL) {
8001 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
8002 		    "required functions"));
8003 	}
8004 	return (SATA_SUCCESS);
8005 }
8006 
8007 /*
8008  * Remove HBA instance from sata_hba_list.
8009  */
8010 static void
8011 sata_remove_hba_instance(dev_info_t *dip)
8012 {
8013 	sata_hba_inst_t	*sata_hba_inst;
8014 
8015 	mutex_enter(&sata_mutex);
8016 	for (sata_hba_inst = sata_hba_list;
8017 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
8018 	    sata_hba_inst = sata_hba_inst->satahba_next) {
8019 		if (sata_hba_inst->satahba_dip == dip)
8020 			break;
8021 	}
8022 
8023 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
8024 #ifdef SATA_DEBUG
8025 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
8026 		    "unknown HBA instance\n");
8027 #endif
8028 		ASSERT(FALSE);
8029 	}
8030 	if (sata_hba_inst == sata_hba_list) {
8031 		sata_hba_list = sata_hba_inst->satahba_next;
8032 		if (sata_hba_list) {
8033 			sata_hba_list->satahba_prev =
8034 			    (struct sata_hba_inst *)NULL;
8035 		}
8036 		if (sata_hba_inst == sata_hba_list_tail) {
8037 			sata_hba_list_tail = NULL;
8038 		}
8039 	} else if (sata_hba_inst == sata_hba_list_tail) {
8040 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
8041 		if (sata_hba_list_tail) {
8042 			sata_hba_list_tail->satahba_next =
8043 			    (struct sata_hba_inst *)NULL;
8044 		}
8045 	} else {
8046 		sata_hba_inst->satahba_prev->satahba_next =
8047 		    sata_hba_inst->satahba_next;
8048 		sata_hba_inst->satahba_next->satahba_prev =
8049 		    sata_hba_inst->satahba_prev;
8050 	}
8051 	mutex_exit(&sata_mutex);
8052 }
8053 
8054 
8055 
8056 
8057 
8058 /*
8059  * Probe all SATA ports of the specified HBA instance.
8060  * The assumption is that there are no target and attachment point minor nodes
8061  * created by the boot subsystems, so we do not need to prune device tree.
8062  *
8063  * This function is called only from sata_hba_attach(). It does not have to
8064  * be protected by controller mutex, because the hba_attached flag is not set
8065  * yet and no one would be touching this HBA instance other than this thread.
8066  * Determines if port is active and what type of the device is attached
8067  * (if any). Allocates necessary structures for each port.
8068  *
8069  * An AP (Attachement Point) node is created for each SATA device port even
8070  * when there is no device attached.
8071  */
8072 
8073 static 	void
8074 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8075 {
8076 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8077 	int			ncport, npmport;
8078 	sata_cport_info_t 	*cportinfo;
8079 	sata_drive_info_t	*drive;
8080 	sata_pmult_info_t	*pminfo;
8081 	sata_pmport_info_t 	*pmportinfo;
8082 	sata_device_t		sata_device;
8083 	int			rval;
8084 	dev_t			minor_number;
8085 	char			name[16];
8086 	clock_t			start_time, cur_time;
8087 
8088 	/*
8089 	 * Probe controller ports first, to find port status and
8090 	 * any port multiplier attached.
8091 	 */
8092 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8093 		/* allocate cport structure */
8094 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8095 		ASSERT(cportinfo != NULL);
8096 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8097 
8098 		mutex_enter(&cportinfo->cport_mutex);
8099 
8100 		cportinfo->cport_addr.cport = ncport;
8101 		cportinfo->cport_addr.pmport = 0;
8102 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8103 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8104 		cportinfo->cport_state |= SATA_STATE_PROBING;
8105 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8106 
8107 		/*
8108 		 * Regardless if a port is usable or not, create
8109 		 * an attachment point
8110 		 */
8111 		mutex_exit(&cportinfo->cport_mutex);
8112 		minor_number =
8113 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8114 		(void) sprintf(name, "%d", ncport);
8115 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8116 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8117 		    DDI_SUCCESS) {
8118 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8119 			    "cannot create SATA attachment point for port %d",
8120 			    ncport);
8121 		}
8122 
8123 		/* Probe port */
8124 		start_time = ddi_get_lbolt();
8125 	reprobe_cport:
8126 		sata_device.satadev_addr.cport = ncport;
8127 		sata_device.satadev_addr.pmport = 0;
8128 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8129 		sata_device.satadev_rev = SATA_DEVICE_REV;
8130 
8131 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8132 		    (dip, &sata_device);
8133 
8134 		mutex_enter(&cportinfo->cport_mutex);
8135 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8136 		if (rval != SATA_SUCCESS) {
8137 			/* Something went wrong? Fail the port */
8138 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8139 			mutex_exit(&cportinfo->cport_mutex);
8140 			continue;
8141 		}
8142 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8143 		cportinfo->cport_state |= SATA_STATE_PROBED;
8144 		cportinfo->cport_dev_type = sata_device.satadev_type;
8145 
8146 		cportinfo->cport_state |= SATA_STATE_READY;
8147 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8148 			mutex_exit(&cportinfo->cport_mutex);
8149 			continue;
8150 		}
8151 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8152 			/*
8153 			 * There is some device attached.
8154 			 * Allocate device info structure
8155 			 */
8156 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8157 				mutex_exit(&cportinfo->cport_mutex);
8158 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8159 				    kmem_zalloc(sizeof (sata_drive_info_t),
8160 				    KM_SLEEP);
8161 				mutex_enter(&cportinfo->cport_mutex);
8162 			}
8163 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8164 			drive->satadrv_addr = cportinfo->cport_addr;
8165 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8166 			drive->satadrv_type = cportinfo->cport_dev_type;
8167 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8168 
8169 			mutex_exit(&cportinfo->cport_mutex);
8170 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8171 			    SATA_SUCCESS) {
8172 				/*
8173 				 * Plugged device was not correctly identified.
8174 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8175 				 */
8176 				cur_time = ddi_get_lbolt();
8177 				if ((cur_time - start_time) <
8178 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8179 					/* sleep for a while */
8180 					delay(drv_usectohz(
8181 					    SATA_DEV_RETRY_DLY));
8182 					goto reprobe_cport;
8183 				}
8184 			}
8185 		} else {
8186 			mutex_exit(&cportinfo->cport_mutex);
8187 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8188 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8189 			    KM_SLEEP);
8190 			mutex_enter(&cportinfo->cport_mutex);
8191 			ASSERT(pminfo != NULL);
8192 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8193 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8194 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8195 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8196 			pminfo->pmult_num_dev_ports =
8197 			    sata_device.satadev_add_info;
8198 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8199 			    NULL);
8200 			pminfo->pmult_state = SATA_STATE_PROBING;
8201 			mutex_exit(&cportinfo->cport_mutex);
8202 
8203 			/* Probe Port Multiplier ports */
8204 			for (npmport = 0;
8205 			    npmport < pminfo->pmult_num_dev_ports;
8206 			    npmport++) {
8207 				pmportinfo = kmem_zalloc(
8208 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8209 				mutex_enter(&cportinfo->cport_mutex);
8210 				ASSERT(pmportinfo != NULL);
8211 				pmportinfo->pmport_addr.cport = ncport;
8212 				pmportinfo->pmport_addr.pmport = npmport;
8213 				pmportinfo->pmport_addr.qual =
8214 				    SATA_ADDR_PMPORT;
8215 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8216 
8217 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8218 				    MUTEX_DRIVER, NULL);
8219 
8220 				mutex_exit(&cportinfo->cport_mutex);
8221 
8222 				/* Create an attachment point */
8223 				minor_number = SATA_MAKE_AP_MINOR(
8224 				    ddi_get_instance(dip), ncport, npmport, 1);
8225 				(void) sprintf(name, "%d.%d", ncport, npmport);
8226 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8227 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8228 				    0) != DDI_SUCCESS) {
8229 					sata_log(sata_hba_inst, CE_WARN,
8230 					    "sata_hba_attach: "
8231 					    "cannot create SATA attachment "
8232 					    "point for port %d pmult port %d",
8233 					    ncport, npmport);
8234 				}
8235 
8236 				start_time = ddi_get_lbolt();
8237 			reprobe_pmport:
8238 				sata_device.satadev_addr.pmport = npmport;
8239 				sata_device.satadev_addr.qual =
8240 				    SATA_ADDR_PMPORT;
8241 
8242 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8243 				    (dip, &sata_device);
8244 				mutex_enter(&cportinfo->cport_mutex);
8245 
8246 				/* sata_update_port_info() */
8247 				sata_update_port_scr(&pmportinfo->pmport_scr,
8248 				    &sata_device);
8249 
8250 				if (rval != SATA_SUCCESS) {
8251 					pmportinfo->pmport_state =
8252 					    SATA_PSTATE_FAILED;
8253 					mutex_exit(&cportinfo->cport_mutex);
8254 					continue;
8255 				}
8256 				pmportinfo->pmport_state &=
8257 				    ~SATA_STATE_PROBING;
8258 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8259 				pmportinfo->pmport_dev_type =
8260 				    sata_device.satadev_type;
8261 
8262 				pmportinfo->pmport_state |= SATA_STATE_READY;
8263 				if (pmportinfo->pmport_dev_type ==
8264 				    SATA_DTYPE_NONE) {
8265 					mutex_exit(&cportinfo->cport_mutex);
8266 					continue;
8267 				}
8268 				/* Port multipliers cannot be chained */
8269 				ASSERT(pmportinfo->pmport_dev_type !=
8270 				    SATA_DTYPE_PMULT);
8271 				/*
8272 				 * There is something attached to Port
8273 				 * Multiplier device port
8274 				 * Allocate device info structure
8275 				 */
8276 				if (pmportinfo->pmport_sata_drive == NULL) {
8277 					mutex_exit(&cportinfo->cport_mutex);
8278 					pmportinfo->pmport_sata_drive =
8279 					    kmem_zalloc(
8280 					    sizeof (sata_drive_info_t),
8281 					    KM_SLEEP);
8282 					mutex_enter(&cportinfo->cport_mutex);
8283 				}
8284 				drive = pmportinfo->pmport_sata_drive;
8285 				drive->satadrv_addr.cport =
8286 				    pmportinfo->pmport_addr.cport;
8287 				drive->satadrv_addr.pmport = npmport;
8288 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8289 				drive->satadrv_type = pmportinfo->
8290 				    pmport_dev_type;
8291 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8292 
8293 				mutex_exit(&cportinfo->cport_mutex);
8294 				if (sata_add_device(dip, sata_hba_inst, ncport,
8295 				    npmport) != SATA_SUCCESS) {
8296 					/*
8297 					 * Plugged device was not correctly
8298 					 * identified. Retry, within the
8299 					 * SATA_DEV_IDENTIFY_TIMEOUT
8300 					 */
8301 					cur_time = ddi_get_lbolt();
8302 					if ((cur_time - start_time) <
8303 					    drv_usectohz(
8304 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8305 						/* sleep for a while */
8306 						delay(drv_usectohz(
8307 						    SATA_DEV_RETRY_DLY));
8308 						goto reprobe_pmport;
8309 					}
8310 				}
8311 			}
8312 			pmportinfo->pmport_state =
8313 			    SATA_STATE_PROBED | SATA_STATE_READY;
8314 		}
8315 	}
8316 }
8317 
8318 /*
8319  * Add SATA device for specified HBA instance & port (SCSI target
8320  * device nodes).
8321  * This function is called (indirectly) only from sata_hba_attach().
8322  * A target node is created when there is a supported type device attached,
8323  * but may be removed if it cannot be put online.
8324  *
8325  * This function cannot be called from an interrupt context.
8326  *
8327  * ONLY DISK TARGET NODES ARE CREATED NOW
8328  *
8329  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8330  * device identification failed - adding a device could be retried.
8331  *
8332  */
8333 static 	int
8334 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8335     int pmport)
8336 {
8337 	sata_cport_info_t 	*cportinfo;
8338 	sata_pmult_info_t	*pminfo;
8339 	sata_pmport_info_t	*pmportinfo;
8340 	dev_info_t		*cdip;		/* child dip */
8341 	sata_device_t		sata_device;
8342 	int			rval;
8343 
8344 
8345 
8346 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8347 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8348 	mutex_enter(&cportinfo->cport_mutex);
8349 	/*
8350 	 * Some device is attached to a controller port.
8351 	 * We rely on controllers distinquishing between no-device,
8352 	 * attached port multiplier and other kind of attached device.
8353 	 * We need to get Identify Device data and determine
8354 	 * positively the dev type before trying to attach
8355 	 * the target driver.
8356 	 */
8357 	sata_device.satadev_rev = SATA_DEVICE_REV;
8358 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8359 		/*
8360 		 * Not port multiplier.
8361 		 */
8362 		sata_device.satadev_addr = cportinfo->cport_addr;
8363 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8364 		mutex_exit(&cportinfo->cport_mutex);
8365 
8366 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8367 		if (rval != SATA_SUCCESS ||
8368 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8369 			return (SATA_FAILURE);
8370 
8371 		mutex_enter(&cportinfo->cport_mutex);
8372 		sata_show_drive_info(sata_hba_inst,
8373 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8374 
8375 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8376 			/*
8377 			 * Could not determine device type or
8378 			 * a device is not supported.
8379 			 * Degrade this device to unknown.
8380 			 */
8381 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8382 			mutex_exit(&cportinfo->cport_mutex);
8383 			return (SATA_SUCCESS);
8384 		}
8385 		cportinfo->cport_dev_type = sata_device.satadev_type;
8386 		cportinfo->cport_tgtnode_clean = B_TRUE;
8387 		mutex_exit(&cportinfo->cport_mutex);
8388 
8389 		/*
8390 		 * Initialize device to the desired state. Even if it
8391 		 * fails, the device will still attach but syslog
8392 		 * will show the warning.
8393 		 */
8394 		if (sata_initialize_device(sata_hba_inst,
8395 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
8396 			/* Retry */
8397 			rval = sata_initialize_device(sata_hba_inst,
8398 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8399 
8400 			if (rval == SATA_RETRY)
8401 				sata_log(sata_hba_inst, CE_WARN,
8402 				    "SATA device at port %d - "
8403 				    "default device features could not be set."
8404 				    " Device may not operate as expected.",
8405 				    cportinfo->cport_addr.cport);
8406 		}
8407 
8408 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8409 		    &sata_device.satadev_addr);
8410 		mutex_enter(&cportinfo->cport_mutex);
8411 		if (cdip == NULL) {
8412 			/*
8413 			 * Attaching target node failed.
8414 			 * We retain sata_drive_info structure...
8415 			 */
8416 			mutex_exit(&cportinfo->cport_mutex);
8417 			return (SATA_SUCCESS);
8418 		}
8419 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8420 		    satadrv_state = SATA_STATE_READY;
8421 	} else {
8422 		/* This must be Port Multiplier type */
8423 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8424 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8425 			    "sata_add_device: "
8426 			    "unrecognized dev type %x",
8427 			    cportinfo->cport_dev_type));
8428 			mutex_exit(&cportinfo->cport_mutex);
8429 			return (SATA_SUCCESS);
8430 		}
8431 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8432 		pmportinfo = pminfo->pmult_dev_port[pmport];
8433 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8434 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8435 		mutex_exit(&cportinfo->cport_mutex);
8436 
8437 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8438 		if (rval != SATA_SUCCESS ||
8439 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8440 			return (SATA_FAILURE);
8441 		}
8442 		mutex_enter(&cportinfo->cport_mutex);
8443 		sata_show_drive_info(sata_hba_inst,
8444 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8445 
8446 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8447 			/*
8448 			 * Could not determine device type.
8449 			 * Degrade this device to unknown.
8450 			 */
8451 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8452 			mutex_exit(&cportinfo->cport_mutex);
8453 			return (SATA_SUCCESS);
8454 		}
8455 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8456 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8457 		mutex_exit(&cportinfo->cport_mutex);
8458 
8459 		/*
8460 		 * Initialize device to the desired state.
8461 		 * Even if it fails, the device will still
8462 		 * attach but syslog will show the warning.
8463 		 */
8464 		if (sata_initialize_device(sata_hba_inst,
8465 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
8466 			/* Retry */
8467 			rval = sata_initialize_device(sata_hba_inst,
8468 			    pmportinfo->pmport_sata_drive);
8469 
8470 			if (rval == SATA_RETRY)
8471 				sata_log(sata_hba_inst, CE_WARN,
8472 				    "SATA device at port %d pmport %d - "
8473 				    "default device features could not be set."
8474 				    " Device may not operate as expected.",
8475 				    pmportinfo->pmport_addr.cport,
8476 				    pmportinfo->pmport_addr.pmport);
8477 		}
8478 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8479 		    &sata_device.satadev_addr);
8480 		mutex_enter(&cportinfo->cport_mutex);
8481 		if (cdip == NULL) {
8482 			/*
8483 			 * Attaching target node failed.
8484 			 * We retain sata_drive_info structure...
8485 			 */
8486 			mutex_exit(&cportinfo->cport_mutex);
8487 			return (SATA_SUCCESS);
8488 		}
8489 		pmportinfo->pmport_sata_drive->satadrv_state |=
8490 		    SATA_STATE_READY;
8491 	}
8492 	mutex_exit(&cportinfo->cport_mutex);
8493 	return (SATA_SUCCESS);
8494 }
8495 
8496 
8497 
8498 /*
8499  * Create scsi target node for attached device, create node properties and
8500  * attach the node.
8501  * The node could be removed if the device onlining fails.
8502  *
8503  * A dev_info_t pointer is returned if operation is successful, NULL is
8504  * returned otherwise.
8505  *
8506  * No port multiplier support.
8507  */
8508 
8509 static dev_info_t *
8510 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8511 			sata_address_t *sata_addr)
8512 {
8513 	dev_info_t *cdip = NULL;
8514 	int rval;
8515 	char *nname = NULL;
8516 	char **compatible = NULL;
8517 	int ncompatible;
8518 	struct scsi_inquiry inq;
8519 	sata_device_t sata_device;
8520 	sata_drive_info_t *sdinfo;
8521 	int target;
8522 	int i;
8523 
8524 	sata_device.satadev_rev = SATA_DEVICE_REV;
8525 	sata_device.satadev_addr = *sata_addr;
8526 
8527 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8528 
8529 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8530 
8531 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8532 	    sata_addr->pmport, sata_addr->qual);
8533 
8534 	if (sdinfo == NULL) {
8535 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8536 		    sata_addr->cport)));
8537 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8538 		    "sata_create_target_node: no sdinfo for target %x",
8539 		    target));
8540 		return (NULL);
8541 	}
8542 
8543 	/*
8544 	 * create or get scsi inquiry data, expected by
8545 	 * scsi_hba_nodename_compatible_get()
8546 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8547 	 * ATAPI devices respond directly to Inquiry request.
8548 	 */
8549 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8550 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8551 		    (uint8_t *)&inq);
8552 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8553 		    sata_addr->cport)));
8554 	} else { /* Assume supported ATAPI device */
8555 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8556 		    sata_addr->cport)));
8557 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8558 		    &inq) == SATA_FAILURE)
8559 			return (NULL);
8560 		/*
8561 		 * Save supported ATAPI transport version
8562 		 */
8563 		sdinfo->satadrv_atapi_trans_ver =
8564 		    SATA_ATAPI_TRANS_VERSION(&inq);
8565 	}
8566 
8567 	/* determine the node name and compatible */
8568 	scsi_hba_nodename_compatible_get(&inq, NULL,
8569 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8570 
8571 #ifdef SATA_DEBUG
8572 	if (sata_debug_flags & SATA_DBG_NODES) {
8573 		if (nname == NULL) {
8574 			cmn_err(CE_NOTE, "sata_create_target_node: "
8575 			    "cannot determine nodename for target %d\n",
8576 			    target);
8577 		} else {
8578 			cmn_err(CE_WARN, "sata_create_target_node: "
8579 			    "target %d nodename: %s\n", target, nname);
8580 		}
8581 		if (compatible == NULL) {
8582 			cmn_err(CE_WARN,
8583 			    "sata_create_target_node: no compatible name\n");
8584 		} else {
8585 			for (i = 0; i < ncompatible; i++) {
8586 				cmn_err(CE_WARN, "sata_create_target_node: "
8587 				    "compatible name: %s\n", compatible[i]);
8588 			}
8589 		}
8590 	}
8591 #endif
8592 
8593 	/* if nodename can't be determined, log error and exit */
8594 	if (nname == NULL) {
8595 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8596 		    "sata_create_target_node: cannot determine nodename "
8597 		    "for target %d\n", target));
8598 		scsi_hba_nodename_compatible_free(nname, compatible);
8599 		return (NULL);
8600 	}
8601 	/*
8602 	 * Create scsi target node
8603 	 */
8604 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8605 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8606 	    "device-type", "scsi");
8607 
8608 	if (rval != DDI_PROP_SUCCESS) {
8609 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8610 		    "updating device_type prop failed %d", rval));
8611 		goto fail;
8612 	}
8613 
8614 	/*
8615 	 * Create target node properties: target & lun
8616 	 */
8617 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8618 	if (rval != DDI_PROP_SUCCESS) {
8619 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8620 		    "updating target prop failed %d", rval));
8621 		goto fail;
8622 	}
8623 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8624 	if (rval != DDI_PROP_SUCCESS) {
8625 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8626 		    "updating target prop failed %d", rval));
8627 		goto fail;
8628 	}
8629 
8630 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
8631 		/*
8632 		 * Add "variant" property
8633 		 */
8634 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8635 		    "variant", "atapi");
8636 		if (rval != DDI_PROP_SUCCESS) {
8637 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8638 			    "sata_create_target_node: variant atapi "
8639 			    "property could not be created: %d", rval));
8640 			goto fail;
8641 		}
8642 	}
8643 	/* decorate the node with compatible */
8644 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8645 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8646 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8647 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8648 		    (void *)cdip));
8649 		goto fail;
8650 	}
8651 
8652 
8653 	/*
8654 	 * Now, try to attach the driver. If probing of the device fails,
8655 	 * the target node may be removed
8656 	 */
8657 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8658 
8659 	scsi_hba_nodename_compatible_free(nname, compatible);
8660 
8661 	if (rval == NDI_SUCCESS)
8662 		return (cdip);
8663 
8664 	/* target node was removed - are we sure? */
8665 	return (NULL);
8666 
8667 fail:
8668 	scsi_hba_nodename_compatible_free(nname, compatible);
8669 	ddi_prop_remove_all(cdip);
8670 	rval = ndi_devi_free(cdip);
8671 	if (rval != NDI_SUCCESS) {
8672 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8673 		    "node removal failed %d", rval));
8674 	}
8675 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8676 	    "cannot create target node for SATA device at port %d",
8677 	    sata_addr->cport);
8678 	return (NULL);
8679 }
8680 
8681 
8682 
8683 /*
8684  * Re-probe sata port, check for a device and attach info
8685  * structures when necessary. Identify Device data is fetched, if possible.
8686  * Assumption: sata address is already validated.
8687  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8688  * the presence of a device and its type.
8689  *
8690  * flag arg specifies that the function should try multiple times to identify
8691  * device type and to initialize it, or it should return immediately on failure.
8692  * SATA_DEV_IDENTIFY_RETRY - retry
8693  * SATA_DEV_IDENTIFY_NORETRY - no retry
8694  *
8695  * SATA_FAILURE is returned if one of the operations failed.
8696  *
8697  * This function cannot be called in interrupt context - it may sleep.
8698  *
8699  * NOte: Port multiplier is not supported yet, although there may be some
8700  * pieces of code referencing to it.
8701  */
8702 static int
8703 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8704     int flag)
8705 {
8706 	sata_cport_info_t *cportinfo;
8707 	sata_drive_info_t *sdinfo, *osdinfo;
8708 	boolean_t init_device = B_FALSE;
8709 	int prev_device_type = SATA_DTYPE_NONE;
8710 	int prev_device_settings = 0;
8711 	int prev_device_state = 0;
8712 	clock_t start_time;
8713 	int retry = B_FALSE;
8714 	int rval_probe, rval_init;
8715 
8716 	/* We only care about host sata cport for now */
8717 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8718 	    sata_device->satadev_addr.cport);
8719 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8720 	if (osdinfo != NULL) {
8721 		/*
8722 		 * We are re-probing port with a previously attached device.
8723 		 * Save previous device type and settings.
8724 		 */
8725 		prev_device_type = cportinfo->cport_dev_type;
8726 		prev_device_settings = osdinfo->satadrv_settings;
8727 		prev_device_state = osdinfo->satadrv_state;
8728 	}
8729 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8730 		start_time = ddi_get_lbolt();
8731 		retry = B_TRUE;
8732 	}
8733 retry_probe:
8734 
8735 	/* probe port */
8736 	mutex_enter(&cportinfo->cport_mutex);
8737 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8738 	cportinfo->cport_state |= SATA_STATE_PROBING;
8739 	mutex_exit(&cportinfo->cport_mutex);
8740 
8741 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8742 	    (SATA_DIP(sata_hba_inst), sata_device);
8743 
8744 	mutex_enter(&cportinfo->cport_mutex);
8745 	if (rval_probe != SATA_SUCCESS) {
8746 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8747 		mutex_exit(&cportinfo->cport_mutex);
8748 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8749 		    "SATA port %d probing failed",
8750 		    cportinfo->cport_addr.cport));
8751 		return (SATA_FAILURE);
8752 	}
8753 
8754 	/*
8755 	 * update sata port state and set device type
8756 	 */
8757 	sata_update_port_info(sata_hba_inst, sata_device);
8758 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8759 
8760 	/*
8761 	 * Sanity check - Port is active? Is the link active?
8762 	 * Is there any device attached?
8763 	 */
8764 	if ((cportinfo->cport_state &
8765 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8766 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8767 	    SATA_PORT_DEVLINK_UP) {
8768 		/*
8769 		 * Port in non-usable state or no link active/no device.
8770 		 * Free info structure if necessary (direct attached drive
8771 		 * only, for now!
8772 		 */
8773 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8774 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8775 		/* Add here differentiation for device attached or not */
8776 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8777 		mutex_exit(&cportinfo->cport_mutex);
8778 		if (sdinfo != NULL)
8779 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8780 		return (SATA_SUCCESS);
8781 	}
8782 
8783 	cportinfo->cport_state |= SATA_STATE_READY;
8784 	cportinfo->cport_dev_type = sata_device->satadev_type;
8785 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8786 
8787 	/*
8788 	 * If we are re-probing the port, there may be
8789 	 * sata_drive_info structure attached
8790 	 * (or sata_pm_info, if PMult is supported).
8791 	 */
8792 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8793 		/*
8794 		 * There is no device, so remove device info structure,
8795 		 * if necessary.
8796 		 * Only direct attached drive is considered now, until
8797 		 * port multiplier is supported. If the previously
8798 		 * attached device was a port multiplier, we would need
8799 		 * to take care of devices attached beyond the port
8800 		 * multiplier.
8801 		 */
8802 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8803 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8804 		if (sdinfo != NULL) {
8805 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8806 			sata_log(sata_hba_inst, CE_WARN,
8807 			    "SATA device detached "
8808 			    "from port %d", cportinfo->cport_addr.cport);
8809 		}
8810 		mutex_exit(&cportinfo->cport_mutex);
8811 		return (SATA_SUCCESS);
8812 	}
8813 
8814 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8815 		if (sdinfo == NULL) {
8816 			/*
8817 			 * There is some device attached, but there is
8818 			 * no sata_drive_info structure - allocate one
8819 			 */
8820 			mutex_exit(&cportinfo->cport_mutex);
8821 			sdinfo = kmem_zalloc(
8822 			    sizeof (sata_drive_info_t), KM_SLEEP);
8823 			mutex_enter(&cportinfo->cport_mutex);
8824 			/*
8825 			 * Recheck, that the port state did not change when we
8826 			 * released mutex.
8827 			 */
8828 			if (cportinfo->cport_state & SATA_STATE_READY) {
8829 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8830 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8831 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8832 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8833 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8834 			} else {
8835 				/*
8836 				 * Port is not in ready state, we
8837 				 * cannot attach a device.
8838 				 */
8839 				mutex_exit(&cportinfo->cport_mutex);
8840 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8841 				return (SATA_SUCCESS);
8842 			}
8843 			/*
8844 			 * Since we are adding device, presumably new one,
8845 			 * indicate that it  should be initalized,
8846 			 * as well as some internal framework states).
8847 			 */
8848 			init_device = B_TRUE;
8849 		}
8850 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8851 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8852 	} else {
8853 		/*
8854 		 * The device is a port multiplier - not handled now.
8855 		 */
8856 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8857 		mutex_exit(&cportinfo->cport_mutex);
8858 		return (SATA_SUCCESS);
8859 	}
8860 	mutex_exit(&cportinfo->cport_mutex);
8861 	/*
8862 	 * Figure out what kind of device we are really
8863 	 * dealing with. Failure of identifying device does not fail this
8864 	 * function.
8865 	 */
8866 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
8867 	rval_init = SATA_FAILURE;
8868 	mutex_enter(&cportinfo->cport_mutex);
8869 	if (rval_probe == SATA_SUCCESS) {
8870 		/*
8871 		 * If we are dealing with the same type of a device as before,
8872 		 * restore its settings flags.
8873 		 */
8874 		if (osdinfo != NULL &&
8875 		    sata_device->satadev_type == prev_device_type)
8876 			sdinfo->satadrv_settings = prev_device_settings;
8877 
8878 		mutex_exit(&cportinfo->cport_mutex);
8879 		rval_init = SATA_SUCCESS;
8880 		/* Set initial device features, if necessary */
8881 		if (init_device == B_TRUE) {
8882 			rval_init = sata_initialize_device(sata_hba_inst,
8883 			    sdinfo);
8884 		}
8885 		if (rval_init == SATA_SUCCESS)
8886 			return (rval_init);
8887 		/* else we will retry if retry was asked for */
8888 
8889 	} else {
8890 		/*
8891 		 * If there was some device info before we probe the device,
8892 		 * restore previous device setting, so we can retry from scratch
8893 		 * later. Providing, of course, that device has not disapear
8894 		 * during probing process.
8895 		 */
8896 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8897 			if (osdinfo != NULL) {
8898 				cportinfo->cport_dev_type = prev_device_type;
8899 				sdinfo->satadrv_type = prev_device_type;
8900 				sdinfo->satadrv_state = prev_device_state;
8901 			}
8902 		} else {
8903 			/* device is gone */
8904 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8905 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8906 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8907 			mutex_exit(&cportinfo->cport_mutex);
8908 			return (SATA_SUCCESS);
8909 		}
8910 		mutex_exit(&cportinfo->cport_mutex);
8911 	}
8912 
8913 	if (retry) {
8914 		clock_t cur_time = ddi_get_lbolt();
8915 		/*
8916 		 * A device was not successfully identified or initialized.
8917 		 * Track retry time for device identification.
8918 		 */
8919 		if ((cur_time - start_time) <
8920 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8921 			/* sleep for a while */
8922 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8923 			goto retry_probe;
8924 		}
8925 		/* else no more retries */
8926 		mutex_enter(&cportinfo->cport_mutex);
8927 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
8928 			if (rval_init == SATA_RETRY) {
8929 				/*
8930 				 * Setting drive features have failed, but
8931 				 * because the drive is still accessible,
8932 				 * keep it and emit a warning message.
8933 				 */
8934 				sata_log(sata_hba_inst, CE_WARN,
8935 				    "SATA device at port %d - desired "
8936 				    "drive features could not be set. "
8937 				    "Device may not operate as expected.",
8938 				    cportinfo->cport_addr.cport);
8939 			} else {
8940 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8941 				    satadrv_state = SATA_DSTATE_FAILED;
8942 			}
8943 		}
8944 		mutex_exit(&cportinfo->cport_mutex);
8945 	}
8946 	return (SATA_SUCCESS);
8947 }
8948 
8949 /*
8950  * Initialize device
8951  * Specified device is initialized to a default state.
8952  *
8953  * Returns SATA_SUCCESS if all device features are set successfully,
8954  * SATA_RETRY if device is accessible but device features were not set
8955  * successfully, and SATA_FAILURE otherwise.
8956  */
8957 static int
8958 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8959     sata_drive_info_t *sdinfo)
8960 {
8961 	int rval;
8962 
8963 	sata_save_drive_settings(sdinfo);
8964 
8965 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8966 
8967 	sata_init_write_cache_mode(sdinfo);
8968 
8969 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8970 
8971 	/* Determine current data transfer mode */
8972 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8973 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8974 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8975 	    SATA_VALIDINFO_88) != 0 &&
8976 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8977 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8978 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8979 	    SATA_MDMA_SEL_MASK) != 0) {
8980 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8981 	} else
8982 		/* DMA supported, not no DMA transfer mode is selected !? */
8983 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8984 
8985 	return (rval);
8986 }
8987 
8988 
8989 /*
8990  * Initialize write cache mode.
8991  *
8992  * The default write cache setting for SATA HDD is provided by sata_write_cache
8993  * static variable. ATAPI CD/DVDs devices have write cache default is
8994  * determined by sata_atapicdvd_write_cache static variable.
8995  * ATAPI tape devices have write cache default is determined by
8996  * sata_atapitape_write_cache static variable.
8997  * ATAPI disk devices have write cache default is determined by
8998  * sata_atapidisk_write_cache static variable.
8999  * 1 - enable
9000  * 0 - disable
9001  * any other value - current drive setting
9002  *
9003  * Although there is not reason to disable write cache on CD/DVD devices,
9004  * tape devices and ATAPI disk devices, the default setting control is provided
9005  * for the maximun flexibility.
9006  *
9007  * In the future, it may be overridden by the
9008  * disk-write-cache-enable property setting, if it is defined.
9009  * Returns SATA_SUCCESS if all device features are set successfully,
9010  * SATA_FAILURE otherwise.
9011  */
9012 static void
9013 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
9014 {
9015 	switch (sdinfo->satadrv_type) {
9016 	case SATA_DTYPE_ATADISK:
9017 		if (sata_write_cache == 1)
9018 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9019 		else if (sata_write_cache == 0)
9020 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9021 		/*
9022 		 * When sata_write_cache value is not 0 or 1,
9023 		 * a current setting of the drive's write cache is used.
9024 		 */
9025 		break;
9026 	case SATA_DTYPE_ATAPICD:
9027 		if (sata_atapicdvd_write_cache == 1)
9028 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9029 		else if (sata_atapicdvd_write_cache == 0)
9030 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9031 		/*
9032 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
9033 		 * a current setting of the drive's write cache is used.
9034 		 */
9035 		break;
9036 	case SATA_DTYPE_ATAPITAPE:
9037 		if (sata_atapitape_write_cache == 1)
9038 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9039 		else if (sata_atapitape_write_cache == 0)
9040 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9041 		/*
9042 		 * When sata_atapitape_write_cache value is not 0 or 1,
9043 		 * a current setting of the drive's write cache is used.
9044 		 */
9045 		break;
9046 	case SATA_DTYPE_ATAPIDISK:
9047 		if (sata_atapidisk_write_cache == 1)
9048 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9049 		else if (sata_atapidisk_write_cache == 0)
9050 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9051 		/*
9052 		 * When sata_atapidisk_write_cache value is not 0 or 1,
9053 		 * a current setting of the drive's write cache is used.
9054 		 */
9055 		break;
9056 	}
9057 }
9058 
9059 
9060 /*
9061  * Validate sata address.
9062  * Specified cport, pmport and qualifier has to match
9063  * passed sata_scsi configuration info.
9064  * The presence of an attached device is not verified.
9065  *
9066  * Returns 0 when address is valid, -1 otherwise.
9067  */
9068 static int
9069 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9070 	int pmport, int qual)
9071 {
9072 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9073 		goto invalid_address;
9074 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9075 		goto invalid_address;
9076 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9077 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9078 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9079 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9080 		goto invalid_address;
9081 
9082 	return (0);
9083 
9084 invalid_address:
9085 	return (-1);
9086 
9087 }
9088 
9089 /*
9090  * Validate scsi address
9091  * SCSI target address is translated into SATA cport/pmport and compared
9092  * with a controller port/device configuration. LUN has to be 0.
9093  * Returns 0 if a scsi target refers to an attached device,
9094  * returns 1 if address is valid but device is not attached,
9095  * returns -1 if bad address or device is of an unsupported type.
9096  * Upon return sata_device argument is set.
9097  */
9098 static int
9099 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9100 	struct scsi_address *ap, sata_device_t *sata_device)
9101 {
9102 	int cport, pmport, qual, rval;
9103 
9104 	rval = -1;	/* Invalid address */
9105 	if (ap->a_lun != 0)
9106 		goto out;
9107 
9108 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9109 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9110 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9111 
9112 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9113 		goto out;
9114 
9115 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9116 	    0) {
9117 
9118 		sata_cport_info_t *cportinfo;
9119 		sata_pmult_info_t *pmultinfo;
9120 		sata_drive_info_t *sdinfo = NULL;
9121 
9122 		rval = 1;	/* Valid sata address */
9123 
9124 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9125 		if (qual == SATA_ADDR_DCPORT) {
9126 			if (cportinfo == NULL ||
9127 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9128 				goto out;
9129 
9130 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9131 			    (cportinfo->cport_dev_type &
9132 			    SATA_VALID_DEV_TYPE) == 0) {
9133 				rval = -1;
9134 				goto out;
9135 			}
9136 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9137 
9138 		} else if (qual == SATA_ADDR_DPMPORT) {
9139 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9140 			if (pmultinfo == NULL) {
9141 				rval = -1;
9142 				goto out;
9143 			}
9144 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9145 			    NULL ||
9146 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9147 			    pmport) == SATA_DTYPE_NONE)
9148 				goto out;
9149 
9150 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9151 			    pmport);
9152 		} else {
9153 			rval = -1;
9154 			goto out;
9155 		}
9156 		if ((sdinfo == NULL) ||
9157 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9158 			goto out;
9159 
9160 		sata_device->satadev_type = sdinfo->satadrv_type;
9161 		sata_device->satadev_addr.qual = qual;
9162 		sata_device->satadev_addr.cport = cport;
9163 		sata_device->satadev_addr.pmport = pmport;
9164 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9165 		return (0);
9166 	}
9167 out:
9168 	if (rval == 1) {
9169 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9170 		    "sata_validate_scsi_address: no valid target %x lun %x",
9171 		    ap->a_target, ap->a_lun);
9172 	}
9173 	return (rval);
9174 }
9175 
9176 /*
9177  * Find dip corresponding to passed device number
9178  *
9179  * Returns NULL if invalid device number is passed or device cannot be found,
9180  * Returns dip is device is found.
9181  */
9182 static dev_info_t *
9183 sata_devt_to_devinfo(dev_t dev)
9184 {
9185 	dev_info_t *dip;
9186 #ifndef __lock_lint
9187 	struct devnames *dnp;
9188 	major_t major = getmajor(dev);
9189 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9190 
9191 	if (major >= devcnt)
9192 		return (NULL);
9193 
9194 	dnp = &devnamesp[major];
9195 	LOCK_DEV_OPS(&(dnp->dn_lock));
9196 	dip = dnp->dn_head;
9197 	while (dip && (ddi_get_instance(dip) != instance)) {
9198 		dip = ddi_get_next(dip);
9199 	}
9200 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9201 #endif
9202 
9203 	return (dip);
9204 }
9205 
9206 
9207 /*
9208  * Probe device.
9209  * This function issues Identify Device command and initializes local
9210  * sata_drive_info structure if the device can be identified.
9211  * The device type is determined by examining Identify Device
9212  * command response.
9213  * If the sata_hba_inst has linked drive info structure for this
9214  * device address, the Identify Device data is stored into sata_drive_info
9215  * structure linked to the port info structure.
9216  *
9217  * sata_device has to refer to the valid sata port(s) for HBA described
9218  * by sata_hba_inst structure.
9219  *
9220  * Returns:
9221  *	SATA_SUCCESS if device type was successfully probed and port-linked
9222  *		drive info structure was updated;
9223  * 	SATA_FAILURE if there is no device, or device was not probed
9224  *		successully;
9225  *	SATA_RETRY if device probe can be retried later.
9226  * If a device cannot be identified, sata_device's dev_state and dev_type
9227  * fields are set to unknown.
9228  * There are no retries in this function. Any retries should be managed by
9229  * the caller.
9230  */
9231 
9232 
9233 static int
9234 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9235 {
9236 	sata_drive_info_t *sdinfo;
9237 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9238 	int rval;
9239 
9240 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9241 	    sata_device->satadev_addr.cport) &
9242 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9243 
9244 	sata_device->satadev_type = SATA_DTYPE_NONE;
9245 
9246 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9247 	    sata_device->satadev_addr.cport)));
9248 
9249 	/* Get pointer to port-linked sata device info structure */
9250 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9251 	if (sdinfo != NULL) {
9252 		sdinfo->satadrv_state &=
9253 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9254 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9255 	} else {
9256 		/* No device to probe */
9257 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9258 		    sata_device->satadev_addr.cport)));
9259 		sata_device->satadev_type = SATA_DTYPE_NONE;
9260 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9261 		return (SATA_FAILURE);
9262 	}
9263 	/*
9264 	 * Need to issue both types of identify device command and
9265 	 * determine device type by examining retreived data/status.
9266 	 * First, ATA Identify Device.
9267 	 */
9268 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9269 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9270 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9271 	    sata_device->satadev_addr.cport)));
9272 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9273 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9274 	if (rval == SATA_RETRY) {
9275 		/* We may try to check for ATAPI device */
9276 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9277 			/*
9278 			 * HBA supports ATAPI - try to issue Identify Packet
9279 			 * Device command.
9280 			 */
9281 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
9282 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9283 		}
9284 	}
9285 	if (rval == SATA_SUCCESS) {
9286 		/*
9287 		 * Got something responding positively to ATA Identify Device
9288 		 * or to Identify Packet Device cmd.
9289 		 * Save last used device type.
9290 		 */
9291 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9292 
9293 		/* save device info, if possible */
9294 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9295 		    sata_device->satadev_addr.cport)));
9296 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9297 		if (sdinfo == NULL) {
9298 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9299 			    sata_device->satadev_addr.cport)));
9300 			return (SATA_FAILURE);
9301 		}
9302 		/*
9303 		 * Copy drive info into the port-linked drive info structure.
9304 		 */
9305 		*sdinfo = new_sdinfo;
9306 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9307 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9308 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9309 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9310 			    sata_device->satadev_addr.cport) =
9311 			    sdinfo->satadrv_type;
9312 		else /* SATA_ADDR_DPMPORT */
9313 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9314 			    sata_device->satadev_addr.cport,
9315 			    sata_device->satadev_addr.pmport) =
9316 			    sdinfo->satadrv_type;
9317 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9318 		    sata_device->satadev_addr.cport)));
9319 		return (SATA_SUCCESS);
9320 	}
9321 
9322 	/*
9323 	 * It may be SATA_RETRY or SATA_FAILURE return.
9324 	 * Looks like we cannot determine the device type at this time.
9325 	 */
9326 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9327 	    sata_device->satadev_addr.cport)));
9328 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9329 	if (sdinfo != NULL) {
9330 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9331 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9332 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9333 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9334 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9335 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9336 			    sata_device->satadev_addr.cport) =
9337 			    SATA_DTYPE_UNKNOWN;
9338 		else {
9339 			/* SATA_ADDR_DPMPORT */
9340 			if ((SATA_PMULT_INFO(sata_hba_inst,
9341 			    sata_device->satadev_addr.cport) != NULL) &&
9342 			    (SATA_PMPORT_INFO(sata_hba_inst,
9343 			    sata_device->satadev_addr.cport,
9344 			    sata_device->satadev_addr.pmport) != NULL))
9345 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9346 				    sata_device->satadev_addr.cport,
9347 				    sata_device->satadev_addr.pmport) =
9348 				    SATA_DTYPE_UNKNOWN;
9349 		}
9350 	}
9351 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9352 	    sata_device->satadev_addr.cport)));
9353 	return (rval);
9354 }
9355 
9356 
9357 /*
9358  * Get pointer to sata_drive_info structure.
9359  *
9360  * The sata_device has to contain address (cport, pmport and qualifier) for
9361  * specified sata_scsi structure.
9362  *
9363  * Returns NULL if device address is not valid for this HBA configuration.
9364  * Otherwise, returns a pointer to sata_drive_info structure.
9365  *
9366  * This function should be called with a port mutex held.
9367  */
9368 static sata_drive_info_t *
9369 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9370     sata_device_t *sata_device)
9371 {
9372 	uint8_t cport = sata_device->satadev_addr.cport;
9373 	uint8_t pmport = sata_device->satadev_addr.pmport;
9374 	uint8_t qual = sata_device->satadev_addr.qual;
9375 
9376 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9377 		return (NULL);
9378 
9379 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9380 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9381 		/* Port not probed yet */
9382 		return (NULL);
9383 
9384 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9385 		return (NULL);
9386 
9387 	if (qual == SATA_ADDR_DCPORT) {
9388 		/* Request for a device on a controller port */
9389 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9390 		    SATA_DTYPE_PMULT)
9391 			/* Port multiplier attached */
9392 			return (NULL);
9393 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9394 	}
9395 	if (qual == SATA_ADDR_DPMPORT) {
9396 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9397 		    SATA_DTYPE_PMULT)
9398 			return (NULL);
9399 
9400 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9401 			return (NULL);
9402 
9403 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9404 	}
9405 
9406 	/* we should not get here */
9407 	return (NULL);
9408 }
9409 
9410 
9411 /*
9412  * sata_identify_device.
9413  * Send Identify Device command to SATA HBA driver.
9414  * If command executes successfully, update sata_drive_info structure pointed
9415  * to by sdinfo argument, including Identify Device data.
9416  * If command fails, invalidate data in sata_drive_info.
9417  *
9418  * Cannot be called from interrupt level.
9419  *
9420  * Returns:
9421  * SATA_SUCCESS if the device was identified as a supported device,
9422  * SATA_RETRY if the device was not identified but could be retried,
9423  * SATA_FAILURE if the device was not identified and identify attempt
9424  *	should not be retried.
9425  */
9426 static int
9427 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9428     sata_drive_info_t *sdinfo)
9429 {
9430 	uint16_t cfg_word;
9431 	int rval;
9432 
9433 	/* fetch device identify data */
9434 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9435 	    sdinfo)) != SATA_SUCCESS)
9436 		goto fail_unknown;
9437 
9438 	cfg_word = sdinfo->satadrv_id.ai_config;
9439 
9440 	/* Set the correct device type */
9441 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
9442 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9443 	} else if (cfg_word == SATA_CFA_TYPE) {
9444 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
9445 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9446 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
9447 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
9448 		case SATA_ATAPI_CDROM_DEV:
9449 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9450 			break;
9451 		case SATA_ATAPI_SQACC_DEV:
9452 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
9453 			break;
9454 		case SATA_ATAPI_DIRACC_DEV:
9455 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
9456 			break;
9457 		default:
9458 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9459 		}
9460 	} else {
9461 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9462 	}
9463 
9464 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9465 		if (sdinfo->satadrv_capacity == 0) {
9466 			/* Non-LBA disk. Too bad... */
9467 			sata_log(sata_hba_inst, CE_WARN,
9468 			    "SATA disk device at port %d does not support LBA",
9469 			    sdinfo->satadrv_addr.cport);
9470 			rval = SATA_FAILURE;
9471 			goto fail_unknown;
9472 		}
9473 	}
9474 #if 0
9475 	/* Left for historical reason */
9476 	/*
9477 	 * Some initial version of SATA spec indicated that at least
9478 	 * UDMA mode 4 has to be supported. It is not metioned in
9479 	 * SerialATA 2.6, so this restriction is removed.
9480 	 */
9481 	/* Check for Ultra DMA modes 6 through 0 being supported */
9482 	for (i = 6; i >= 0; --i) {
9483 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9484 			break;
9485 	}
9486 
9487 	/*
9488 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9489 	 * higher are not supported by the device, fail this
9490 	 * device.
9491 	 */
9492 	if (i < 4) {
9493 		/* No required Ultra DMA mode supported */
9494 		sata_log(sata_hba_inst, CE_WARN,
9495 		    "SATA disk device at port %d does not support UDMA "
9496 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9497 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9498 		    "mode 4 or higher required, %d supported", i));
9499 		rval = SATA_FAILURE;
9500 		goto fail_unknown;
9501 	}
9502 #endif
9503 
9504 	/*
9505 	 * For Disk devices, if it doesn't support UDMA mode, we would
9506 	 * like to return failure directly.
9507 	 */
9508 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
9509 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
9510 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
9511 		sata_log(sata_hba_inst, CE_WARN,
9512 		    "SATA disk device at port %d does not support UDMA",
9513 		    sdinfo->satadrv_addr.cport);
9514 		rval = SATA_FAILURE;
9515 		goto fail_unknown;
9516 	}
9517 
9518 	return (SATA_SUCCESS);
9519 
9520 fail_unknown:
9521 	/* Invalidate sata_drive_info ? */
9522 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9523 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9524 	return (rval);
9525 }
9526 
9527 /*
9528  * Log/display device information
9529  */
9530 static void
9531 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9532     sata_drive_info_t *sdinfo)
9533 {
9534 	int valid_version;
9535 	char msg_buf[MAXPATHLEN];
9536 	int i;
9537 
9538 	/* Show HBA path */
9539 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9540 
9541 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9542 
9543 	switch (sdinfo->satadrv_type) {
9544 	case SATA_DTYPE_ATADISK:
9545 		(void) sprintf(msg_buf, "SATA disk device at");
9546 		break;
9547 
9548 	case SATA_DTYPE_ATAPICD:
9549 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
9550 		break;
9551 
9552 	case SATA_DTYPE_ATAPITAPE:
9553 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
9554 		break;
9555 
9556 	case SATA_DTYPE_ATAPIDISK:
9557 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
9558 		break;
9559 
9560 	case SATA_DTYPE_UNKNOWN:
9561 		(void) sprintf(msg_buf,
9562 		    "Unsupported SATA device type (cfg 0x%x) at ",
9563 		    sdinfo->satadrv_id.ai_config);
9564 		break;
9565 	}
9566 
9567 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9568 		cmn_err(CE_CONT, "?\t%s port %d\n",
9569 		    msg_buf, sdinfo->satadrv_addr.cport);
9570 	else
9571 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9572 		    msg_buf, sdinfo->satadrv_addr.cport,
9573 		    sdinfo->satadrv_addr.pmport);
9574 
9575 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9576 	    sizeof (sdinfo->satadrv_id.ai_model));
9577 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9578 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9579 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9580 
9581 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9582 	    sizeof (sdinfo->satadrv_id.ai_fw));
9583 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9584 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9585 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9586 
9587 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9588 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9589 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9590 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9591 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9592 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9593 	} else {
9594 		/*
9595 		 * Some drives do not implement serial number and may
9596 		 * violate the spec by providing spaces rather than zeros
9597 		 * in serial number field. Scan the buffer to detect it.
9598 		 */
9599 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9600 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9601 				break;
9602 		}
9603 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9604 			cmn_err(CE_CONT, "?\tserial number - none\n");
9605 		} else {
9606 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9607 		}
9608 	}
9609 
9610 #ifdef SATA_DEBUG
9611 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9612 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9613 		int i;
9614 		for (i = 14; i >= 2; i--) {
9615 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9616 				valid_version = i;
9617 				break;
9618 			}
9619 		}
9620 		cmn_err(CE_CONT,
9621 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9622 		    valid_version,
9623 		    sdinfo->satadrv_id.ai_majorversion,
9624 		    sdinfo->satadrv_id.ai_minorversion);
9625 	}
9626 #endif
9627 	/* Log some info */
9628 	cmn_err(CE_CONT, "?\tsupported features:\n");
9629 	msg_buf[0] = '\0';
9630 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9631 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9632 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9633 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9634 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9635 	}
9636 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9637 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9638 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9639 		(void) strlcat(msg_buf, ", Native Command Queueing",
9640 		    MAXPATHLEN);
9641 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9642 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9643 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9644 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9645 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9646 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9647 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9648 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9649 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9650 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9651 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9652 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9653 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9654 	if (sdinfo->satadrv_features_support &
9655 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9656 		msg_buf[0] = '\0';
9657 		(void) snprintf(msg_buf, MAXPATHLEN,
9658 		    "Supported queue depth %d",
9659 		    sdinfo->satadrv_queue_depth);
9660 		if (!(sata_func_enable &
9661 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9662 			(void) strlcat(msg_buf,
9663 			    " - queueing disabled globally", MAXPATHLEN);
9664 		else if (sdinfo->satadrv_queue_depth >
9665 		    sdinfo->satadrv_max_queue_depth) {
9666 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9667 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9668 			    (int)sdinfo->satadrv_max_queue_depth);
9669 		}
9670 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9671 	}
9672 
9673 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9674 #ifdef __i386
9675 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9676 		    sdinfo->satadrv_capacity);
9677 #else
9678 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9679 		    sdinfo->satadrv_capacity);
9680 #endif
9681 		cmn_err(CE_CONT, "?%s", msg_buf);
9682 	}
9683 }
9684 
9685 
9686 /*
9687  * sata_save_drive_settings extracts current setting of the device and stores
9688  * it for future reference, in case the device setup would need to be restored
9689  * after the device reset.
9690  *
9691  * For all devices read ahead and write cache settings are saved, if the
9692  * device supports these features at all.
9693  * For ATAPI devices the Removable Media Status Notification setting is saved.
9694  */
9695 static void
9696 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9697 {
9698 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
9699 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
9700 
9701 		/* Current setting of Read Ahead (and Read Cache) */
9702 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
9703 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9704 		else
9705 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9706 
9707 		/* Current setting of Write Cache */
9708 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
9709 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9710 		else
9711 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9712 	}
9713 
9714 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9715 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
9716 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9717 		else
9718 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9719 	}
9720 }
9721 
9722 
9723 /*
9724  * sata_check_capacity function determines a disk capacity
9725  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9726  *
9727  * NOTE: CHS mode is not supported! If a device does not support LBA,
9728  * this function is not called.
9729  *
9730  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9731  */
9732 static uint64_t
9733 sata_check_capacity(sata_drive_info_t *sdinfo)
9734 {
9735 	uint64_t capacity = 0;
9736 	int i;
9737 
9738 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9739 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9740 		/* Capacity valid only for LBA-addressable disk devices */
9741 		return (0);
9742 
9743 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9744 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9745 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9746 		/* LBA48 mode supported and enabled */
9747 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9748 		    SATA_DEV_F_LBA28;
9749 		for (i = 3;  i >= 0;  --i) {
9750 			capacity <<= 16;
9751 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9752 		}
9753 	} else {
9754 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9755 		capacity <<= 16;
9756 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9757 		if (capacity >= 0x1000000)
9758 			/* LBA28 mode */
9759 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9760 	}
9761 	return (capacity);
9762 }
9763 
9764 
9765 /*
9766  * Allocate consistent buffer for DMA transfer
9767  *
9768  * Cannot be called from interrupt level or with mutex held - it may sleep.
9769  *
9770  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9771  */
9772 static struct buf *
9773 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9774 {
9775 	struct scsi_address ap;
9776 	struct buf *bp;
9777 	ddi_dma_attr_t	cur_dma_attr;
9778 
9779 	ASSERT(spx->txlt_sata_pkt != NULL);
9780 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9781 	ap.a_target = SATA_TO_SCSI_TARGET(
9782 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9783 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9784 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9785 	ap.a_lun = 0;
9786 
9787 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9788 	    B_READ, SLEEP_FUNC, NULL);
9789 
9790 	if (bp != NULL) {
9791 		/* Allocate DMA resources for this buffer */
9792 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9793 		/*
9794 		 * We use a local version of the dma_attr, to account
9795 		 * for a device addressing limitations.
9796 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9797 		 * will cause dma attributes to be adjusted to a lowest
9798 		 * acceptable level.
9799 		 */
9800 		sata_adjust_dma_attr(NULL,
9801 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9802 
9803 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9804 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9805 			scsi_free_consistent_buf(bp);
9806 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9807 			bp = NULL;
9808 		}
9809 	}
9810 	return (bp);
9811 }
9812 
9813 /*
9814  * Release local buffer (consistent buffer for DMA transfer) allocated
9815  * via sata_alloc_local_buffer().
9816  */
9817 static void
9818 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9819 {
9820 	ASSERT(spx->txlt_sata_pkt != NULL);
9821 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9822 
9823 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9824 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9825 
9826 	sata_common_free_dma_rsrcs(spx);
9827 
9828 	/* Free buffer */
9829 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9830 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9831 }
9832 
9833 /*
9834  * Allocate sata_pkt
9835  * Pkt structure version and embedded strcutures version are initialized.
9836  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9837  *
9838  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9839  * callback argument determines if it can sleep or not.
9840  * Hence, it should not be called from interrupt context.
9841  *
9842  * If successful, non-NULL pointer to a sata pkt is returned.
9843  * Upon failure, NULL pointer is returned.
9844  */
9845 static sata_pkt_t *
9846 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9847 {
9848 	sata_pkt_t *spkt;
9849 	int kmsflag;
9850 
9851 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9852 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9853 	if (spkt == NULL) {
9854 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9855 		    "sata_pkt_alloc: failed"));
9856 		return (NULL);
9857 	}
9858 	spkt->satapkt_rev = SATA_PKT_REV;
9859 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9860 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9861 	spkt->satapkt_framework_private = spx;
9862 	spx->txlt_sata_pkt = spkt;
9863 	return (spkt);
9864 }
9865 
9866 /*
9867  * Free sata pkt allocated via sata_pkt_alloc()
9868  */
9869 static void
9870 sata_pkt_free(sata_pkt_txlate_t *spx)
9871 {
9872 	ASSERT(spx->txlt_sata_pkt != NULL);
9873 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9874 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9875 	spx->txlt_sata_pkt = NULL;
9876 }
9877 
9878 
9879 /*
9880  * Adjust DMA attributes.
9881  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9882  * from 8 bits to 16 bits, depending on a command being used.
9883  * Limiting max block count arbitrarily to 256 for all read/write
9884  * commands may affects performance, so check both the device and
9885  * controller capability before adjusting dma attributes.
9886  */
9887 void
9888 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9889     ddi_dma_attr_t *adj_dma_attr)
9890 {
9891 	uint32_t count_max;
9892 
9893 	/* Copy original attributes */
9894 	*adj_dma_attr = *dma_attr;
9895 	/*
9896 	 * Things to consider: device addressing capability,
9897 	 * "excessive" controller DMA capabilities.
9898 	 * If a device is being probed/initialized, there are
9899 	 * no device info - use default limits then.
9900 	 */
9901 	if (sdinfo == NULL) {
9902 		count_max = dma_attr->dma_attr_granular * 0x100;
9903 		if (dma_attr->dma_attr_count_max > count_max)
9904 			adj_dma_attr->dma_attr_count_max = count_max;
9905 		if (dma_attr->dma_attr_maxxfer > count_max)
9906 			adj_dma_attr->dma_attr_maxxfer = count_max;
9907 		return;
9908 	}
9909 
9910 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9911 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9912 			/*
9913 			 * 16-bit sector count may be used - we rely on
9914 			 * the assumption that only read and write cmds
9915 			 * will request more than 256 sectors worth of data
9916 			 */
9917 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9918 		} else {
9919 			/*
9920 			 * 8-bit sector count will be used - default limits
9921 			 * for dma attributes
9922 			 */
9923 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9924 		}
9925 		/*
9926 		 * Adjust controler dma attributes, if necessary
9927 		 */
9928 		if (dma_attr->dma_attr_count_max > count_max)
9929 			adj_dma_attr->dma_attr_count_max = count_max;
9930 		if (dma_attr->dma_attr_maxxfer > count_max)
9931 			adj_dma_attr->dma_attr_maxxfer = count_max;
9932 	}
9933 }
9934 
9935 
9936 /*
9937  * Allocate DMA resources for the buffer
9938  * This function handles initial DMA resource allocation as well as
9939  * DMA window shift and may be called repeatedly for the same DMA window
9940  * until all DMA cookies in the DMA window are processed.
9941  * To guarantee that there is always a coherent set of cookies to process
9942  * by SATA HBA driver (observing alignment, device granularity, etc.),
9943  * the number of slots for DMA cookies is equal to lesser of  a number of
9944  * cookies in a DMA window and a max number of scatter/gather entries.
9945  *
9946  * Returns DDI_SUCCESS upon successful operation.
9947  * Return failure code of a failing command or DDI_FAILURE when
9948  * internal cleanup failed.
9949  */
9950 static int
9951 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9952     int (*callback)(caddr_t), caddr_t arg,
9953     ddi_dma_attr_t *cur_dma_attr)
9954 {
9955 	int	rval;
9956 	off_t	offset;
9957 	size_t	size;
9958 	int	max_sg_len, req_len, i;
9959 	uint_t	dma_flags;
9960 	struct buf	*bp;
9961 	uint64_t	cur_txfer_len;
9962 
9963 
9964 	ASSERT(spx->txlt_sata_pkt != NULL);
9965 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9966 	ASSERT(bp != NULL);
9967 
9968 
9969 	if (spx->txlt_buf_dma_handle == NULL) {
9970 		/*
9971 		 * No DMA resources allocated so far - this is a first call
9972 		 * for this sata pkt.
9973 		 */
9974 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9975 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9976 
9977 		if (rval != DDI_SUCCESS) {
9978 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9979 			    "sata_dma_buf_setup: no buf DMA resources %x",
9980 			    rval));
9981 			return (rval);
9982 		}
9983 
9984 		if (bp->b_flags & B_READ)
9985 			dma_flags = DDI_DMA_READ;
9986 		else
9987 			dma_flags = DDI_DMA_WRITE;
9988 
9989 		if (flags & PKT_CONSISTENT)
9990 			dma_flags |= DDI_DMA_CONSISTENT;
9991 
9992 		if (flags & PKT_DMA_PARTIAL)
9993 			dma_flags |= DDI_DMA_PARTIAL;
9994 
9995 		/*
9996 		 * Check buffer alignment and size against dma attributes
9997 		 * Consider dma_attr_align only. There may be requests
9998 		 * with the size lower than device granularity, but they
9999 		 * will not read/write from/to the device, so no adjustment
10000 		 * is necessary. The dma_attr_minxfer theoretically should
10001 		 * be considered, but no HBA driver is checking it.
10002 		 */
10003 		if (IS_P2ALIGNED(bp->b_un.b_addr,
10004 		    cur_dma_attr->dma_attr_align)) {
10005 			rval = ddi_dma_buf_bind_handle(
10006 			    spx->txlt_buf_dma_handle,
10007 			    bp, dma_flags, callback, arg,
10008 			    &spx->txlt_dma_cookie,
10009 			    &spx->txlt_curwin_num_dma_cookies);
10010 		} else { /* Buffer is not aligned */
10011 
10012 			int	(*ddicallback)(caddr_t);
10013 			size_t	bufsz;
10014 
10015 			/* Check id sleeping is allowed */
10016 			ddicallback = (callback == NULL_FUNC) ?
10017 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
10018 
10019 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10020 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
10021 			    (void *)bp->b_un.b_addr, bp->b_bcount);
10022 
10023 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
10024 				/*
10025 				 * CPU will need to access data in the buffer
10026 				 * (for copying) so map it.
10027 				 */
10028 				bp_mapin(bp);
10029 
10030 			ASSERT(spx->txlt_tmp_buf == NULL);
10031 
10032 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
10033 			rval = ddi_dma_mem_alloc(
10034 			    spx->txlt_buf_dma_handle,
10035 			    bp->b_bcount,
10036 			    &sata_acc_attr,
10037 			    DDI_DMA_STREAMING,
10038 			    ddicallback, NULL,
10039 			    &spx->txlt_tmp_buf,
10040 			    &bufsz,
10041 			    &spx->txlt_tmp_buf_handle);
10042 
10043 			if (rval != DDI_SUCCESS) {
10044 				/* DMA mapping failed */
10045 				(void) ddi_dma_free_handle(
10046 				    &spx->txlt_buf_dma_handle);
10047 				spx->txlt_buf_dma_handle = NULL;
10048 #ifdef SATA_DEBUG
10049 				mbuffail_count++;
10050 #endif
10051 				SATADBG1(SATA_DBG_DMA_SETUP,
10052 				    spx->txlt_sata_hba_inst,
10053 				    "sata_dma_buf_setup: "
10054 				    "buf dma mem alloc failed %x\n", rval);
10055 				return (rval);
10056 			}
10057 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
10058 			    cur_dma_attr->dma_attr_align));
10059 
10060 #ifdef SATA_DEBUG
10061 			mbuf_count++;
10062 
10063 			if (bp->b_bcount != bufsz)
10064 				/*
10065 				 * This will require special handling, because
10066 				 * DMA cookies will be based on the temporary
10067 				 * buffer size, not the original buffer
10068 				 * b_bcount, so the residue may have to
10069 				 * be counted differently.
10070 				 */
10071 				SATADBG2(SATA_DBG_DMA_SETUP,
10072 				    spx->txlt_sata_hba_inst,
10073 				    "sata_dma_buf_setup: bp size %x != "
10074 				    "bufsz %x\n", bp->b_bcount, bufsz);
10075 #endif
10076 			if (dma_flags & DDI_DMA_WRITE) {
10077 				/*
10078 				 * Write operation - copy data into
10079 				 * an aligned temporary buffer. Buffer will be
10080 				 * synced for device by ddi_dma_addr_bind_handle
10081 				 */
10082 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10083 				    bp->b_bcount);
10084 			}
10085 
10086 			rval = ddi_dma_addr_bind_handle(
10087 			    spx->txlt_buf_dma_handle,
10088 			    NULL,
10089 			    spx->txlt_tmp_buf,
10090 			    bufsz, dma_flags, ddicallback, 0,
10091 			    &spx->txlt_dma_cookie,
10092 			    &spx->txlt_curwin_num_dma_cookies);
10093 		}
10094 
10095 		switch (rval) {
10096 		case DDI_DMA_PARTIAL_MAP:
10097 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10098 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10099 			/*
10100 			 * Partial DMA mapping.
10101 			 * Retrieve number of DMA windows for this request.
10102 			 */
10103 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10104 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10105 				if (spx->txlt_tmp_buf != NULL) {
10106 					ddi_dma_mem_free(
10107 					    &spx->txlt_tmp_buf_handle);
10108 					spx->txlt_tmp_buf = NULL;
10109 				}
10110 				(void) ddi_dma_unbind_handle(
10111 				    spx->txlt_buf_dma_handle);
10112 				(void) ddi_dma_free_handle(
10113 				    &spx->txlt_buf_dma_handle);
10114 				spx->txlt_buf_dma_handle = NULL;
10115 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10116 				    "sata_dma_buf_setup: numwin failed\n"));
10117 				return (DDI_FAILURE);
10118 			}
10119 			SATADBG2(SATA_DBG_DMA_SETUP,
10120 			    spx->txlt_sata_hba_inst,
10121 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10122 			    spx->txlt_num_dma_win,
10123 			    spx->txlt_curwin_num_dma_cookies);
10124 			spx->txlt_cur_dma_win = 0;
10125 			break;
10126 
10127 		case DDI_DMA_MAPPED:
10128 			/* DMA fully mapped */
10129 			spx->txlt_num_dma_win = 1;
10130 			spx->txlt_cur_dma_win = 0;
10131 			SATADBG1(SATA_DBG_DMA_SETUP,
10132 			    spx->txlt_sata_hba_inst,
10133 			    "sata_dma_buf_setup: windows: 1 "
10134 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10135 			break;
10136 
10137 		default:
10138 			/* DMA mapping failed */
10139 			if (spx->txlt_tmp_buf != NULL) {
10140 				ddi_dma_mem_free(
10141 				    &spx->txlt_tmp_buf_handle);
10142 				spx->txlt_tmp_buf = NULL;
10143 			}
10144 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10145 			spx->txlt_buf_dma_handle = NULL;
10146 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10147 			    "sata_dma_buf_setup: buf dma handle binding "
10148 			    "failed %x\n", rval));
10149 			return (rval);
10150 		}
10151 		spx->txlt_curwin_processed_dma_cookies = 0;
10152 		spx->txlt_dma_cookie_list = NULL;
10153 	} else {
10154 		/*
10155 		 * DMA setup is reused. Check if we need to process more
10156 		 * cookies in current window, or to get next window, if any.
10157 		 */
10158 
10159 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10160 		    spx->txlt_curwin_num_dma_cookies);
10161 
10162 		if (spx->txlt_curwin_processed_dma_cookies ==
10163 		    spx->txlt_curwin_num_dma_cookies) {
10164 			/*
10165 			 * All cookies from current DMA window were processed.
10166 			 * Get next DMA window.
10167 			 */
10168 			spx->txlt_cur_dma_win++;
10169 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10170 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10171 				    spx->txlt_cur_dma_win, &offset, &size,
10172 				    &spx->txlt_dma_cookie,
10173 				    &spx->txlt_curwin_num_dma_cookies);
10174 				spx->txlt_curwin_processed_dma_cookies = 0;
10175 			} else {
10176 				/* No more windows! End of request! */
10177 				/* What to do? - panic for now */
10178 				ASSERT(spx->txlt_cur_dma_win >=
10179 				    spx->txlt_num_dma_win);
10180 
10181 				spx->txlt_curwin_num_dma_cookies = 0;
10182 				spx->txlt_curwin_processed_dma_cookies = 0;
10183 				spx->txlt_sata_pkt->
10184 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10185 				return (DDI_SUCCESS);
10186 			}
10187 		}
10188 	}
10189 	/* There better be at least one DMA cookie outstanding */
10190 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10191 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10192 
10193 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10194 		/* The default cookie slot was used in previous run */
10195 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10196 		spx->txlt_dma_cookie_list = NULL;
10197 		spx->txlt_dma_cookie_list_len = 0;
10198 	}
10199 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10200 		/*
10201 		 * Processing a new DMA window - set-up dma cookies list.
10202 		 * We may reuse previously allocated cookie array if it is
10203 		 * possible.
10204 		 */
10205 		if (spx->txlt_dma_cookie_list != NULL &&
10206 		    spx->txlt_dma_cookie_list_len <
10207 		    spx->txlt_curwin_num_dma_cookies) {
10208 			/*
10209 			 * New DMA window contains more cookies than
10210 			 * the previous one. We need larger cookie list - free
10211 			 * the old one.
10212 			 */
10213 			(void) kmem_free(spx->txlt_dma_cookie_list,
10214 			    spx->txlt_dma_cookie_list_len *
10215 			    sizeof (ddi_dma_cookie_t));
10216 			spx->txlt_dma_cookie_list = NULL;
10217 			spx->txlt_dma_cookie_list_len = 0;
10218 		}
10219 		if (spx->txlt_dma_cookie_list == NULL) {
10220 			/*
10221 			 * Calculate lesser of number of cookies in this
10222 			 * DMA window and number of s/g entries.
10223 			 */
10224 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10225 			req_len = MIN(max_sg_len,
10226 			    spx->txlt_curwin_num_dma_cookies);
10227 
10228 			/* Allocate new dma cookie array if necessary */
10229 			if (req_len == 1) {
10230 				/* Only one cookie - no need for a list */
10231 				spx->txlt_dma_cookie_list =
10232 				    &spx->txlt_dma_cookie;
10233 				spx->txlt_dma_cookie_list_len = 1;
10234 			} else {
10235 				/*
10236 				 * More than one cookie - try to allocate space.
10237 				 */
10238 				spx->txlt_dma_cookie_list = kmem_zalloc(
10239 				    sizeof (ddi_dma_cookie_t) * req_len,
10240 				    callback == NULL_FUNC ? KM_NOSLEEP :
10241 				    KM_SLEEP);
10242 				if (spx->txlt_dma_cookie_list == NULL) {
10243 					SATADBG1(SATA_DBG_DMA_SETUP,
10244 					    spx->txlt_sata_hba_inst,
10245 					    "sata_dma_buf_setup: cookie list "
10246 					    "allocation failed\n", NULL);
10247 					/*
10248 					 * We could not allocate space for
10249 					 * neccessary number of dma cookies in
10250 					 * this window, so we fail this request.
10251 					 * Next invocation would try again to
10252 					 * allocate space for cookie list.
10253 					 * Note:Packet residue was not modified.
10254 					 */
10255 					return (DDI_DMA_NORESOURCES);
10256 				} else {
10257 					spx->txlt_dma_cookie_list_len = req_len;
10258 				}
10259 			}
10260 		}
10261 		/*
10262 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10263 		 * First cookie was already fetched.
10264 		 */
10265 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10266 		cur_txfer_len =
10267 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10268 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10269 		spx->txlt_curwin_processed_dma_cookies++;
10270 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10271 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10272 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10273 			    &spx->txlt_dma_cookie_list[i]);
10274 			cur_txfer_len +=
10275 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10276 			spx->txlt_curwin_processed_dma_cookies++;
10277 			spx->txlt_sata_pkt->
10278 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10279 		}
10280 	} else {
10281 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10282 		    "sata_dma_buf_setup: sliding within DMA window, "
10283 		    "cur cookie %d, total cookies %d\n",
10284 		    spx->txlt_curwin_processed_dma_cookies,
10285 		    spx->txlt_curwin_num_dma_cookies);
10286 
10287 		/*
10288 		 * Not all cookies from the current dma window were used because
10289 		 * of s/g limitation.
10290 		 * There is no need to re-size the list - it was set at
10291 		 * optimal size, or only default entry is used (s/g = 1).
10292 		 */
10293 		if (spx->txlt_dma_cookie_list == NULL) {
10294 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10295 			spx->txlt_dma_cookie_list_len = 1;
10296 		}
10297 		/*
10298 		 * Since we are processing remaining cookies in a DMA window,
10299 		 * there may be less of them than the number of entries in the
10300 		 * current dma cookie list.
10301 		 */
10302 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10303 		    (spx->txlt_curwin_num_dma_cookies -
10304 		    spx->txlt_curwin_processed_dma_cookies));
10305 
10306 		/* Fetch the next batch of cookies */
10307 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10308 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10309 			    &spx->txlt_dma_cookie_list[i]);
10310 			cur_txfer_len +=
10311 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10312 			spx->txlt_sata_pkt->
10313 			    satapkt_cmd.satacmd_num_dma_cookies++;
10314 			spx->txlt_curwin_processed_dma_cookies++;
10315 		}
10316 	}
10317 
10318 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10319 
10320 	/* Point sata_cmd to the cookie list */
10321 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10322 	    &spx->txlt_dma_cookie_list[0];
10323 
10324 	/* Remember number of DMA cookies passed in sata packet */
10325 	spx->txlt_num_dma_cookies =
10326 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10327 
10328 	ASSERT(cur_txfer_len != 0);
10329 	if (cur_txfer_len <= bp->b_bcount)
10330 		spx->txlt_total_residue -= cur_txfer_len;
10331 	else {
10332 		/*
10333 		 * Temporary DMA buffer has been padded by
10334 		 * ddi_dma_mem_alloc()!
10335 		 * This requires special handling, because DMA cookies are
10336 		 * based on the temporary buffer size, not the b_bcount,
10337 		 * and we have extra bytes to transfer - but the packet
10338 		 * residue has to stay correct because we will copy only
10339 		 * the requested number of bytes.
10340 		 */
10341 		spx->txlt_total_residue -= bp->b_bcount;
10342 	}
10343 
10344 	return (DDI_SUCCESS);
10345 }
10346 
10347 /*
10348  * Common routine for releasing DMA resources
10349  */
10350 static void
10351 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
10352 {
10353 	if (spx->txlt_buf_dma_handle != NULL) {
10354 		if (spx->txlt_tmp_buf != NULL)  {
10355 			/*
10356 			 * Intermediate DMA buffer was allocated.
10357 			 * Free allocated buffer and associated access handle.
10358 			 */
10359 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
10360 			spx->txlt_tmp_buf = NULL;
10361 		}
10362 		/*
10363 		 * Free DMA resources - cookies and handles
10364 		 */
10365 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
10366 		if (spx->txlt_dma_cookie_list != NULL) {
10367 			if (spx->txlt_dma_cookie_list !=
10368 			    &spx->txlt_dma_cookie) {
10369 				(void) kmem_free(spx->txlt_dma_cookie_list,
10370 				    spx->txlt_dma_cookie_list_len *
10371 				    sizeof (ddi_dma_cookie_t));
10372 				spx->txlt_dma_cookie_list = NULL;
10373 			}
10374 		}
10375 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10376 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10377 		spx->txlt_buf_dma_handle = NULL;
10378 	}
10379 }
10380 
10381 /*
10382  * Free DMA resources
10383  * Used by the HBA driver to release DMA resources that it does not use.
10384  *
10385  * Returns Void
10386  */
10387 void
10388 sata_free_dma_resources(sata_pkt_t *sata_pkt)
10389 {
10390 	sata_pkt_txlate_t *spx;
10391 
10392 	if (sata_pkt == NULL)
10393 		return;
10394 
10395 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10396 
10397 	sata_common_free_dma_rsrcs(spx);
10398 }
10399 
10400 /*
10401  * Fetch Device Identify data.
10402  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10403  * command to a device and get the device identify data.
10404  * The device_info structure has to be set to device type (for selecting proper
10405  * device identify command).
10406  *
10407  * Returns:
10408  * SATA_SUCCESS if cmd succeeded
10409  * SATA_RETRY if cmd was rejected and could be retried,
10410  * SATA_FAILURE if cmd failed and should not be retried (port error)
10411  *
10412  * Cannot be called in an interrupt context.
10413  */
10414 
10415 static int
10416 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10417     sata_drive_info_t *sdinfo)
10418 {
10419 	struct buf *bp;
10420 	sata_pkt_t *spkt;
10421 	sata_cmd_t *scmd;
10422 	sata_pkt_txlate_t *spx;
10423 	int rval;
10424 
10425 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10426 	spx->txlt_sata_hba_inst = sata_hba_inst;
10427 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10428 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10429 	if (spkt == NULL) {
10430 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10431 		return (SATA_RETRY); /* may retry later */
10432 	}
10433 	/* address is needed now */
10434 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10435 
10436 	/*
10437 	 * Allocate buffer for Identify Data return data
10438 	 */
10439 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10440 	if (bp == NULL) {
10441 		sata_pkt_free(spx);
10442 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10443 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10444 		    "sata_fetch_device_identify_data: "
10445 		    "cannot allocate buffer for ID"));
10446 		return (SATA_RETRY); /* may retry later */
10447 	}
10448 
10449 	/* Fill sata_pkt */
10450 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10451 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10452 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10453 	/* Synchronous mode, no callback */
10454 	spkt->satapkt_comp = NULL;
10455 	/* Timeout 30s */
10456 	spkt->satapkt_time = sata_default_pkt_time;
10457 
10458 	scmd = &spkt->satapkt_cmd;
10459 	scmd->satacmd_bp = bp;
10460 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10461 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10462 
10463 	/* Build Identify Device cmd in the sata_pkt */
10464 	scmd->satacmd_addr_type = 0;		/* N/A */
10465 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10466 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10467 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10468 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10469 	scmd->satacmd_features_reg = 0;		/* N/A */
10470 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10471 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10472 		/* Identify Packet Device cmd */
10473 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10474 	} else {
10475 		/* Identify Device cmd - mandatory for all other devices */
10476 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10477 	}
10478 
10479 	/* Send pkt to SATA HBA driver */
10480 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10481 
10482 #ifdef SATA_INJECT_FAULTS
10483 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
10484 #endif
10485 
10486 	if (rval == SATA_TRAN_ACCEPTED &&
10487 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10488 		if (spx->txlt_buf_dma_handle != NULL) {
10489 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10490 			    DDI_DMA_SYNC_FORKERNEL);
10491 			ASSERT(rval == DDI_SUCCESS);
10492 		}
10493 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10494 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10495 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10496 			    "SATA disk device at port %d - "
10497 			    "partial Identify Data",
10498 			    sdinfo->satadrv_addr.cport));
10499 			rval = SATA_RETRY; /* may retry later */
10500 			goto fail;
10501 		}
10502 		/* Update sata_drive_info */
10503 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10504 		    sizeof (sata_id_t));
10505 
10506 		sdinfo->satadrv_features_support = 0;
10507 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10508 			/*
10509 			 * Retrieve capacity (disks only) and addressing mode
10510 			 */
10511 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10512 		} else {
10513 			/*
10514 			 * For ATAPI devices one would have to issue
10515 			 * Get Capacity cmd for media capacity. Not here.
10516 			 */
10517 			sdinfo->satadrv_capacity = 0;
10518 			/*
10519 			 * Check what cdb length is supported
10520 			 */
10521 			if ((sdinfo->satadrv_id.ai_config &
10522 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10523 				sdinfo->satadrv_atapi_cdb_len = 16;
10524 			else
10525 				sdinfo->satadrv_atapi_cdb_len = 12;
10526 		}
10527 		/* Setup supported features flags */
10528 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10529 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10530 
10531 		/* Check for SATA GEN and NCQ support */
10532 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10533 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10534 			/* SATA compliance */
10535 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10536 				sdinfo->satadrv_features_support |=
10537 				    SATA_DEV_F_NCQ;
10538 			if (sdinfo->satadrv_id.ai_satacap &
10539 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10540 				if (sdinfo->satadrv_id.ai_satacap &
10541 				    SATA_2_SPEED)
10542 					sdinfo->satadrv_features_support |=
10543 					    SATA_DEV_F_SATA2;
10544 				if (sdinfo->satadrv_id.ai_satacap &
10545 				    SATA_1_SPEED)
10546 					sdinfo->satadrv_features_support |=
10547 					    SATA_DEV_F_SATA1;
10548 			} else {
10549 				sdinfo->satadrv_features_support |=
10550 				    SATA_DEV_F_SATA1;
10551 			}
10552 		}
10553 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10554 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10555 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10556 
10557 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10558 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10559 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10560 			++sdinfo->satadrv_queue_depth;
10561 			/* Adjust according to controller capabilities */
10562 			sdinfo->satadrv_max_queue_depth = MIN(
10563 			    sdinfo->satadrv_queue_depth,
10564 			    SATA_QDEPTH(sata_hba_inst));
10565 			/* Adjust according to global queue depth limit */
10566 			sdinfo->satadrv_max_queue_depth = MIN(
10567 			    sdinfo->satadrv_max_queue_depth,
10568 			    sata_current_max_qdepth);
10569 			if (sdinfo->satadrv_max_queue_depth == 0)
10570 				sdinfo->satadrv_max_queue_depth = 1;
10571 		} else
10572 			sdinfo->satadrv_max_queue_depth = 1;
10573 
10574 		rval = SATA_SUCCESS;
10575 	} else {
10576 		/*
10577 		 * Woops, no Identify Data.
10578 		 */
10579 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10580 			rval = SATA_RETRY; /* may retry later */
10581 		} else if (rval == SATA_TRAN_ACCEPTED) {
10582 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10583 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10584 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10585 			    spkt->satapkt_reason == SATA_PKT_RESET)
10586 				rval = SATA_RETRY; /* may retry later */
10587 			else
10588 				rval = SATA_FAILURE;
10589 		} else {
10590 			rval = SATA_FAILURE;
10591 		}
10592 	}
10593 fail:
10594 	/* Free allocated resources */
10595 	sata_free_local_buffer(spx);
10596 	sata_pkt_free(spx);
10597 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10598 
10599 	return (rval);
10600 }
10601 
10602 
10603 /*
10604  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10605  * UDMA mode is checked first, followed by MWDMA mode.
10606  * set correctly, so this function is setting it to the highest supported level.
10607  * Older SATA spec required that the device supports at least DMA 4 mode and
10608  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10609  * restriction has been removed.
10610  *
10611  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10612  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10613  *
10614  * NOTE: This function should be called only if DMA mode is supported.
10615  */
10616 static int
10617 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10618 {
10619 	sata_pkt_t *spkt;
10620 	sata_cmd_t *scmd;
10621 	sata_pkt_txlate_t *spx;
10622 	int mode;
10623 	uint8_t subcmd;
10624 	int rval = SATA_SUCCESS;
10625 
10626 	ASSERT(sdinfo != NULL);
10627 	ASSERT(sata_hba_inst != NULL);
10628 
10629 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10630 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10631 		/* Find highest Ultra DMA mode supported */
10632 		for (mode = 6; mode >= 0; --mode) {
10633 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10634 				break;
10635 		}
10636 #if 0
10637 		/* Left for historical reasons */
10638 		/*
10639 		 * Some initial version of SATA spec indicated that at least
10640 		 * UDMA mode 4 has to be supported. It is not mentioned in
10641 		 * SerialATA 2.6, so this restriction is removed.
10642 		 */
10643 		if (mode < 4)
10644 			return (SATA_FAILURE);
10645 #endif
10646 
10647 		/*
10648 		 * We're still going to set DMA mode whatever is selected
10649 		 * by default
10650 		 *
10651 		 * We saw an old maxtor sata drive will select Ultra DMA and
10652 		 * Multi-Word DMA simultaneouly by default, which is going
10653 		 * to cause DMA command timed out, so we need to select DMA
10654 		 * mode even when it's already done by default
10655 		 */
10656 
10657 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10658 
10659 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10660 		/* Find highest MultiWord DMA mode supported */
10661 		for (mode = 2; mode >= 0; --mode) {
10662 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10663 				break;
10664 		}
10665 
10666 		/*
10667 		 * We're still going to set DMA mode whatever is selected
10668 		 * by default
10669 		 *
10670 		 * We saw an old maxtor sata drive will select Ultra DMA and
10671 		 * Multi-Word DMA simultaneouly by default, which is going
10672 		 * to cause DMA command timed out, so we need to select DMA
10673 		 * mode even when it's already done by default
10674 		 */
10675 
10676 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10677 	} else
10678 		return (SATA_SUCCESS);
10679 
10680 	/*
10681 	 * Set DMA mode via SET FEATURES COMMAND.
10682 	 * Prepare packet for SET FEATURES COMMAND.
10683 	 */
10684 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10685 	spx->txlt_sata_hba_inst = sata_hba_inst;
10686 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10687 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10688 	if (spkt == NULL) {
10689 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10690 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10691 		rval = SATA_FAILURE;
10692 		goto done;
10693 	}
10694 	/* Fill sata_pkt */
10695 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10696 	/* Timeout 30s */
10697 	spkt->satapkt_time = sata_default_pkt_time;
10698 	/* Synchronous mode, no callback, interrupts */
10699 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10700 	spkt->satapkt_comp = NULL;
10701 	scmd = &spkt->satapkt_cmd;
10702 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10703 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10704 	scmd->satacmd_addr_type = 0;
10705 	scmd->satacmd_device_reg = 0;
10706 	scmd->satacmd_status_reg = 0;
10707 	scmd->satacmd_error_reg = 0;
10708 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10709 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10710 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10711 
10712 	/* Transfer command to HBA */
10713 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10714 	    spkt) != SATA_TRAN_ACCEPTED ||
10715 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10716 		/* Pkt execution failed */
10717 		rval = SATA_FAILURE;
10718 	}
10719 done:
10720 
10721 	/* Free allocated resources */
10722 	if (spkt != NULL)
10723 		sata_pkt_free(spx);
10724 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10725 
10726 	return (rval);
10727 }
10728 
10729 
10730 /*
10731  * Set device caching mode.
10732  * One of the following operations should be specified:
10733  * SATAC_SF_ENABLE_READ_AHEAD
10734  * SATAC_SF_DISABLE_READ_AHEAD
10735  * SATAC_SF_ENABLE_WRITE_CACHE
10736  * SATAC_SF_DISABLE_WRITE_CACHE
10737  *
10738  * If operation fails, system log messgage is emitted.
10739  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
10740  * command was sent but did not succeed, and SATA_FAILURE otherwise.
10741  */
10742 
10743 static int
10744 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10745     int cache_op)
10746 {
10747 	sata_pkt_t *spkt;
10748 	sata_cmd_t *scmd;
10749 	sata_pkt_txlate_t *spx;
10750 	int rval = SATA_SUCCESS;
10751 	int hba_rval;
10752 	char *infop;
10753 
10754 	ASSERT(sdinfo != NULL);
10755 	ASSERT(sata_hba_inst != NULL);
10756 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10757 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10758 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10759 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10760 
10761 
10762 	/* Prepare packet for SET FEATURES COMMAND */
10763 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10764 	spx->txlt_sata_hba_inst = sata_hba_inst;
10765 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10766 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10767 	if (spkt == NULL) {
10768 		rval = SATA_FAILURE;
10769 		goto failure;
10770 	}
10771 	/* Fill sata_pkt */
10772 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10773 	/* Timeout 30s */
10774 	spkt->satapkt_time = sata_default_pkt_time;
10775 	/* Synchronous mode, no callback, interrupts */
10776 	spkt->satapkt_op_mode =
10777 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10778 	spkt->satapkt_comp = NULL;
10779 	scmd = &spkt->satapkt_cmd;
10780 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10781 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10782 	scmd->satacmd_addr_type = 0;
10783 	scmd->satacmd_device_reg = 0;
10784 	scmd->satacmd_status_reg = 0;
10785 	scmd->satacmd_error_reg = 0;
10786 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10787 	scmd->satacmd_features_reg = cache_op;
10788 
10789 	/* Transfer command to HBA */
10790 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
10791 	    SATA_DIP(sata_hba_inst), spkt);
10792 
10793 #ifdef SATA_INJECT_FAULTS
10794 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
10795 #endif
10796 
10797 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
10798 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10799 		/* Pkt execution failed */
10800 		switch (cache_op) {
10801 		case SATAC_SF_ENABLE_READ_AHEAD:
10802 			infop = "enabling read ahead failed";
10803 			break;
10804 		case SATAC_SF_DISABLE_READ_AHEAD:
10805 			infop = "disabling read ahead failed";
10806 			break;
10807 		case SATAC_SF_ENABLE_WRITE_CACHE:
10808 			infop = "enabling write cache failed";
10809 			break;
10810 		case SATAC_SF_DISABLE_WRITE_CACHE:
10811 			infop = "disabling write cache failed";
10812 			break;
10813 		}
10814 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10815 		rval = SATA_RETRY;
10816 	}
10817 failure:
10818 	/* Free allocated resources */
10819 	if (spkt != NULL)
10820 		sata_pkt_free(spx);
10821 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10822 	return (rval);
10823 }
10824 
10825 /*
10826  * Set Removable Media Status Notification (enable/disable)
10827  * state == 0 , disable
10828  * state != 0 , enable
10829  *
10830  * If operation fails, system log messgage is emitted.
10831  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10832  */
10833 
10834 static int
10835 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10836     int state)
10837 {
10838 	sata_pkt_t *spkt;
10839 	sata_cmd_t *scmd;
10840 	sata_pkt_txlate_t *spx;
10841 	int rval = SATA_SUCCESS;
10842 	char *infop;
10843 
10844 	ASSERT(sdinfo != NULL);
10845 	ASSERT(sata_hba_inst != NULL);
10846 
10847 	/* Prepare packet for SET FEATURES COMMAND */
10848 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10849 	spx->txlt_sata_hba_inst = sata_hba_inst;
10850 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10851 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10852 	if (spkt == NULL) {
10853 		rval = SATA_FAILURE;
10854 		goto failure;
10855 	}
10856 	/* Fill sata_pkt */
10857 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10858 	/* Timeout 30s */
10859 	spkt->satapkt_time = sata_default_pkt_time;
10860 	/* Synchronous mode, no callback, interrupts */
10861 	spkt->satapkt_op_mode =
10862 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10863 	spkt->satapkt_comp = NULL;
10864 	scmd = &spkt->satapkt_cmd;
10865 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10866 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10867 	scmd->satacmd_addr_type = 0;
10868 	scmd->satacmd_device_reg = 0;
10869 	scmd->satacmd_status_reg = 0;
10870 	scmd->satacmd_error_reg = 0;
10871 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10872 	if (state == 0)
10873 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10874 	else
10875 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10876 
10877 	/* Transfer command to HBA */
10878 	if (((*SATA_START_FUNC(sata_hba_inst))(
10879 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10880 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10881 		/* Pkt execution failed */
10882 		if (state == 0)
10883 			infop = "disabling Removable Media Status "
10884 			    "Notification failed";
10885 		else
10886 			infop = "enabling Removable Media Status "
10887 			    "Notification failed";
10888 
10889 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10890 		rval = SATA_FAILURE;
10891 	}
10892 failure:
10893 	/* Free allocated resources */
10894 	if (spkt != NULL)
10895 		sata_pkt_free(spx);
10896 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10897 	return (rval);
10898 }
10899 
10900 
10901 /*
10902  * Update port SCR block
10903  */
10904 static void
10905 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10906 {
10907 	port_scr->sstatus = device->satadev_scr.sstatus;
10908 	port_scr->serror = device->satadev_scr.serror;
10909 	port_scr->scontrol = device->satadev_scr.scontrol;
10910 	port_scr->sactive = device->satadev_scr.sactive;
10911 	port_scr->snotific = device->satadev_scr.snotific;
10912 }
10913 
10914 /*
10915  * Update state and copy port ss* values from passed sata_device structure.
10916  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10917  * configuration struct.
10918  *
10919  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10920  * regardless of the state in device argument.
10921  *
10922  * Port mutex should be held while calling this function.
10923  */
10924 static void
10925 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10926 	sata_device_t *sata_device)
10927 {
10928 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10929 	    sata_device->satadev_addr.cport)));
10930 
10931 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10932 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10933 
10934 		sata_cport_info_t *cportinfo;
10935 
10936 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10937 		    sata_device->satadev_addr.cport)
10938 			return;
10939 
10940 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10941 		    sata_device->satadev_addr.cport);
10942 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10943 
10944 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10945 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10946 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10947 		cportinfo->cport_state |=
10948 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10949 	} else {
10950 		sata_pmport_info_t *pmportinfo;
10951 
10952 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10953 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10954 		    SATA_NUM_PMPORTS(sata_hba_inst,
10955 		    sata_device->satadev_addr.cport) <
10956 		    sata_device->satadev_addr.pmport)
10957 			return;
10958 
10959 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10960 		    sata_device->satadev_addr.cport,
10961 		    sata_device->satadev_addr.pmport);
10962 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10963 
10964 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10965 		pmportinfo->pmport_state &=
10966 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10967 		    SATA_PSTATE_FAILED);
10968 		pmportinfo->pmport_state |=
10969 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10970 	}
10971 }
10972 
10973 
10974 
10975 /*
10976  * Extract SATA port specification from an IOCTL argument.
10977  *
10978  * This function return the port the user land send us as is, unless it
10979  * cannot retrieve port spec, then -1 is returned.
10980  *
10981  * Note: Only cport  - no port multiplier port.
10982  */
10983 static int32_t
10984 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10985 {
10986 	int32_t port;
10987 
10988 	/* Extract port number from nvpair in dca structure  */
10989 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10990 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10991 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10992 		    port));
10993 		port = -1;
10994 	}
10995 
10996 	return (port);
10997 }
10998 
10999 /*
11000  * Get dev_info_t pointer to the device node pointed to by port argument.
11001  * NOTE: target argument is a value used in ioctls to identify
11002  * the AP - it is not a sata_address.
11003  * It is a combination of cport, pmport and address qualifier, encodded same
11004  * way as a scsi target number.
11005  * At this moment it carries only cport number.
11006  *
11007  * No PMult hotplug support.
11008  *
11009  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11010  */
11011 
11012 static dev_info_t *
11013 sata_get_target_dip(dev_info_t *dip, int32_t port)
11014 {
11015 	dev_info_t	*cdip = NULL;
11016 	int		target, tgt;
11017 	int		ncport;
11018 	int 		circ;
11019 
11020 	ncport = port & SATA_CFGA_CPORT_MASK;
11021 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
11022 
11023 	ndi_devi_enter(dip, &circ);
11024 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11025 		dev_info_t *next = ddi_get_next_sibling(cdip);
11026 
11027 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11028 		    DDI_PROP_DONTPASS, "target", -1);
11029 		if (tgt == -1) {
11030 			/*
11031 			 * This is actually an error condition, but not
11032 			 * a fatal one. Just continue the search.
11033 			 */
11034 			cdip = next;
11035 			continue;
11036 		}
11037 
11038 		if (tgt == target)
11039 			break;
11040 
11041 		cdip = next;
11042 	}
11043 	ndi_devi_exit(dip, circ);
11044 
11045 	return (cdip);
11046 }
11047 
11048 /*
11049  * Get dev_info_t pointer to the device node pointed to by port argument.
11050  * NOTE: target argument is a value used in ioctls to identify
11051  * the AP - it is not a sata_address.
11052  * It is a combination of cport, pmport and address qualifier, encoded same
11053  * way as a scsi target number.
11054  * At this moment it carries only cport number.
11055  *
11056  * No PMult hotplug support.
11057  *
11058  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11059  */
11060 
11061 static dev_info_t *
11062 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
11063 {
11064 	dev_info_t	*cdip = NULL;
11065 	int		target, tgt;
11066 	int 		circ;
11067 
11068 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
11069 
11070 	ndi_devi_enter(dip, &circ);
11071 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11072 		dev_info_t *next = ddi_get_next_sibling(cdip);
11073 
11074 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11075 		    DDI_PROP_DONTPASS, "target", -1);
11076 		if (tgt == -1) {
11077 			/*
11078 			 * This is actually an error condition, but not
11079 			 * a fatal one. Just continue the search.
11080 			 */
11081 			cdip = next;
11082 			continue;
11083 		}
11084 
11085 		if (tgt == target)
11086 			break;
11087 
11088 		cdip = next;
11089 	}
11090 	ndi_devi_exit(dip, circ);
11091 
11092 	return (cdip);
11093 }
11094 
11095 /*
11096  * Process sata port disconnect request.
11097  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
11098  * before this request. Nevertheless, if a device is still configured,
11099  * we need to attempt to offline and unconfigure device.
11100  * Regardless of the unconfigure operation results the port is marked as
11101  * deactivated and no access to the attached device is possible.
11102  * If the target node remains because unconfigure operation failed, its state
11103  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
11104  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
11105  * the device and remove old target node.
11106  *
11107  * This function invokes sata_hba_inst->satahba_tran->
11108  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11109  * If successful, the device structure (if any) attached to the specified port
11110  * is removed and state of the port marked appropriately.
11111  * Failure of the port_deactivate may keep port in the physically active state,
11112  * or may fail the port.
11113  *
11114  * NOTE: Port multiplier code is not completed nor tested.
11115  */
11116 
11117 static int
11118 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
11119     sata_device_t *sata_device)
11120 {
11121 	sata_drive_info_t *sdinfo = NULL;
11122 	sata_cport_info_t *cportinfo = NULL;
11123 	sata_pmport_info_t *pmportinfo = NULL;
11124 	sata_pmult_info_t *pmultinfo = NULL;
11125 	dev_info_t *tdip;
11126 	int cport, pmport, qual;
11127 	int rval = SATA_SUCCESS;
11128 	int rv = 0;
11129 
11130 	cport = sata_device->satadev_addr.cport;
11131 	pmport = sata_device->satadev_addr.pmport;
11132 	qual = sata_device->satadev_addr.qual;
11133 
11134 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11135 
11136 	/*
11137 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
11138 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11139 	 * Do the sanity check.
11140 	 */
11141 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
11142 		/* No physical port deactivation supported. */
11143 		return (EINVAL);
11144 	}
11145 
11146 	/* Check the current state of the port */
11147 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11148 	    (SATA_DIP(sata_hba_inst), sata_device);
11149 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11150 	sata_update_port_info(sata_hba_inst, sata_device);
11151 	if (rval != SATA_SUCCESS ||
11152 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11153 		/* Device port status is unknown or it is in failed state */
11154 		if (qual == SATA_ADDR_PMPORT) {
11155 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11156 			    SATA_PSTATE_FAILED;
11157 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11158 			    "sata_hba_ioctl: connect: failed to deactivate "
11159 			    "SATA port %d", cport);
11160 		} else {
11161 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11162 			    SATA_PSTATE_FAILED;
11163 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11164 			    "sata_hba_ioctl: connect: failed to deactivate "
11165 			    "SATA port %d:%d", cport, pmport);
11166 		}
11167 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11168 		    cport)->cport_mutex);
11169 		return (EIO);
11170 	}
11171 	/*
11172 	 * Set port's dev_state to not ready - this will disable
11173 	 * an access to a potentially attached device.
11174 	 */
11175 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11176 	if (qual == SATA_ADDR_PMPORT) {
11177 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11178 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11179 			sdinfo = pmportinfo->pmport_sata_drive;
11180 			ASSERT(sdinfo != NULL);
11181 		}
11182 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11183 	} else {
11184 		/* Assuming cport */
11185 
11186 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11187 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
11188 				pmultinfo =
11189 				    cportinfo->cport_devp.cport_sata_pmult;
11190 				ASSERT(pmultinfo != NULL);
11191 			} else {
11192 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
11193 			}
11194 		}
11195 		cportinfo->cport_state &= ~SATA_STATE_READY;
11196 	}
11197 	if (sdinfo != NULL) {
11198 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
11199 			/*
11200 			 * If a target node exists, try to offline
11201 			 * a device and remove target node.
11202 			 */
11203 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11204 			    cport)->cport_mutex);
11205 			/* We are addressing attached device, not a port */
11206 			sata_device->satadev_addr.qual =
11207 			    sdinfo->satadrv_addr.qual;
11208 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11209 			    &sata_device->satadev_addr);
11210 			if (tdip != NULL && ndi_devi_offline(tdip,
11211 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11212 				/*
11213 				 * Problem
11214 				 * The target node remained attached.
11215 				 * This happens when the device file was open
11216 				 * or a node was waiting for resources.
11217 				 * Cannot do anything about it.
11218 				 */
11219 				if (qual == SATA_ADDR_CPORT) {
11220 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11221 					    "sata_hba_ioctl: disconnect: could "
11222 					    "not unconfigure device before "
11223 					    "disconnecting the SATA port %d",
11224 					    cport));
11225 				} else {
11226 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11227 					    "sata_hba_ioctl: disconnect: could "
11228 					    "not unconfigure device before "
11229 					    "disconnecting the SATA port %d:%d",
11230 					    cport, pmport));
11231 				}
11232 				/*
11233 				 * Set DEVICE REMOVED state in the target
11234 				 * node. It will prevent access to the device
11235 				 * even when a new device is attached, until
11236 				 * the old target node is released, removed and
11237 				 * recreated for a new  device.
11238 				 */
11239 				sata_set_device_removed(tdip);
11240 
11241 				/*
11242 				 * Instruct event daemon to try the target
11243 				 * node cleanup later.
11244 				 */
11245 				sata_set_target_node_cleanup(
11246 				    sata_hba_inst, &sata_device->satadev_addr);
11247 			}
11248 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11249 			    cport)->cport_mutex);
11250 		}
11251 
11252 		/* Remove and release sata_drive info structure. */
11253 		if (pmportinfo != NULL) {
11254 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
11255 			    NULL;
11256 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11257 		} else {
11258 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11259 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11260 		}
11261 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11262 	}
11263 #if 0
11264 	else if (pmultinfo != NULL) {
11265 		/*
11266 		 * Port Multiplier itself needs special handling.
11267 		 * All device ports need to be processed here!
11268 		 */
11269 	}
11270 #endif
11271 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11272 	/* Just ask HBA driver to deactivate port */
11273 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
11274 
11275 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11276 	    (SATA_DIP(sata_hba_inst), sata_device);
11277 
11278 	/*
11279 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11280 	 * without the hint (to force listener to investivate the state).
11281 	 */
11282 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11283 	    SE_NO_HINT);
11284 
11285 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11286 	sata_update_port_info(sata_hba_inst, sata_device);
11287 
11288 	if (rval != SATA_SUCCESS) {
11289 		/*
11290 		 * Port deactivation failure - do not
11291 		 * change port state unless the state
11292 		 * returned by HBA indicates a port failure.
11293 		 * NOTE: device structures were released, so devices now are
11294 		 * invisible! Port reset is needed to re-enumerate devices.
11295 		 */
11296 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11297 			if (pmportinfo != NULL)
11298 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11299 			else
11300 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11301 			rv = EIO;
11302 		}
11303 	} else {
11304 		/*
11305 		 * Deactivation succeded. From now on the sata framework
11306 		 * will not care what is happening to the device, until
11307 		 * the port is activated again.
11308 		 */
11309 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11310 	}
11311 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11312 	return (rv);
11313 }
11314 
11315 
11316 
11317 /*
11318  * Process sata port connect request
11319  * The sata cfgadm pluging will invoke this operation only if port was found
11320  * in the disconnect state (failed state is also treated as the disconnected
11321  * state).
11322  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11323  * sata_tran_hotplug_ops->sata_tran_port_activate().
11324  * If successful and a device is found attached to the port,
11325  * the initialization sequence is executed to attach a device structure to
11326  * a port structure. The state of the port and a device would be set
11327  * appropriately.
11328  * The device is not set in configured state (system-wise) by this operation.
11329  *
11330  * Note, that activating the port may generate link events,
11331  * so it is important that following processing and the
11332  * event processing does not interfere with each other!
11333  *
11334  * This operation may remove port failed state and will
11335  * try to make port active and in good standing.
11336  *
11337  * NOTE: Port multiplier code is not completed nor tested.
11338  */
11339 
11340 static int
11341 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11342     sata_device_t *sata_device)
11343 {
11344 	int cport, pmport, qual;
11345 	int rv = 0;
11346 
11347 	cport = sata_device->satadev_addr.cport;
11348 	pmport = sata_device->satadev_addr.pmport;
11349 	qual = sata_device->satadev_addr.qual;
11350 
11351 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11352 
11353 	/*
11354 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11355 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11356 	 * Perform sanity check now.
11357 	 */
11358 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11359 		/* No physical port activation supported. */
11360 		return (EINVAL);
11361 	}
11362 
11363 	/* Just ask HBA driver to activate port */
11364 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11365 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11366 		/*
11367 		 * Port activation failure.
11368 		 */
11369 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11370 		    cport)->cport_mutex);
11371 		sata_update_port_info(sata_hba_inst, sata_device);
11372 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11373 			if (qual == SATA_ADDR_DCPORT) {
11374 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11375 				    SATA_PSTATE_FAILED;
11376 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11377 				    "sata_hba_ioctl: connect: failed to "
11378 				    "activate SATA port %d", cport);
11379 			} else { /* port multiplier device port */
11380 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11381 				    pmport) = SATA_PSTATE_FAILED;
11382 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11383 				    "sata_hba_ioctl: connect: failed to "
11384 				    "activate SATA port %d:%d", cport, pmport);
11385 
11386 			}
11387 		}
11388 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11389 		    cport)->cport_mutex);
11390 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11391 		    "sata_hba_ioctl: connect: failed to activate SATA "
11392 		    "port %d:%d", cport, pmport);
11393 		return (EIO);
11394 	}
11395 
11396 	/* Virgin port state - will be updated by the port re-probe. */
11397 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11398 	if (qual == SATA_ADDR_CPORT)
11399 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11400 	else /* port multiplier device port */
11401 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11402 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11403 
11404 	/*
11405 	 * Probe the port to find its state and attached device.
11406 	 */
11407 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11408 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11409 		rv = EIO;
11410 
11411 	/*
11412 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11413 	 * without the hint
11414 	 */
11415 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11416 	    SE_NO_HINT);
11417 
11418 	/*
11419 	 * If there is a device attached to the port, emit
11420 	 * a message.
11421 	 */
11422 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11423 
11424 		if (qual == SATA_ADDR_CPORT) {
11425 			sata_log(sata_hba_inst, CE_WARN,
11426 			    "SATA device detected at port %d", cport);
11427 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11428 				/*
11429 				 * A device was not successfully identified
11430 				 */
11431 				sata_log(sata_hba_inst, CE_WARN,
11432 				    "Could not identify SATA "
11433 				    "device at port %d", cport);
11434 			}
11435 		} else { /* port multiplier device port */
11436 			sata_log(sata_hba_inst, CE_WARN,
11437 			    "SATA device detected at port %d:%d",
11438 			    cport, pmport);
11439 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11440 				/*
11441 				 * A device was not successfully identified
11442 				 */
11443 				sata_log(sata_hba_inst, CE_WARN,
11444 				    "Could not identify SATA "
11445 				    "device at port %d:%d", cport, pmport);
11446 			}
11447 		}
11448 	}
11449 
11450 	return (rv);
11451 }
11452 
11453 
11454 /*
11455  * Process sata device unconfigure request.
11456  * The unconfigure operation uses generic nexus operation to
11457  * offline a device. It leaves a target device node attached.
11458  * and obviously sata_drive_info attached as well, because
11459  * from the hardware point of view nothing has changed.
11460  */
11461 static int
11462 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11463     sata_device_t *sata_device)
11464 {
11465 	int rv = 0;
11466 	dev_info_t *tdip;
11467 
11468 	/* We are addressing attached device, not a port */
11469 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11470 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11471 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11472 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11473 
11474 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11475 	    &sata_device->satadev_addr)) != NULL) {
11476 
11477 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11478 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11479 			    "sata_hba_ioctl: unconfigure: "
11480 			    "failed to unconfigure device at SATA port %d:%d",
11481 			    sata_device->satadev_addr.cport,
11482 			    sata_device->satadev_addr.pmport));
11483 			rv = EIO;
11484 		}
11485 		/*
11486 		 * The target node devi_state should be marked with
11487 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11488 		 * This would be the indication for cfgadm that
11489 		 * the AP node occupant state is 'unconfigured'.
11490 		 */
11491 
11492 	} else {
11493 		/*
11494 		 * This would indicate a failure on the part of cfgadm
11495 		 * to detect correct state of the node prior to this
11496 		 * call - one cannot unconfigure non-existing device.
11497 		 */
11498 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11499 		    "sata_hba_ioctl: unconfigure: "
11500 		    "attempt to unconfigure non-existing device "
11501 		    "at SATA port %d:%d",
11502 		    sata_device->satadev_addr.cport,
11503 		    sata_device->satadev_addr.pmport));
11504 		rv = ENXIO;
11505 	}
11506 	return (rv);
11507 }
11508 
11509 /*
11510  * Process sata device configure request
11511  * If port is in a failed state, operation is aborted - one has to use
11512  * an explicit connect or port activate request to try to get a port into
11513  * non-failed mode. Port reset wil also work in such situation.
11514  * If the port is in disconnected (shutdown) state, the connect operation is
11515  * attempted prior to any other action.
11516  * When port is in the active state, there is a device attached and the target
11517  * node exists, a device was most likely offlined.
11518  * If target node does not exist, a new target node is created. In both cases
11519  * an attempt is made to online (configure) the device.
11520  *
11521  * NOTE: Port multiplier code is not completed nor tested.
11522  */
11523 static int
11524 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11525     sata_device_t *sata_device)
11526 {
11527 	int cport, pmport, qual;
11528 	int rval;
11529 	boolean_t target = TRUE;
11530 	sata_cport_info_t *cportinfo;
11531 	sata_pmport_info_t *pmportinfo = NULL;
11532 	dev_info_t *tdip;
11533 	sata_drive_info_t *sdinfo;
11534 
11535 	cport = sata_device->satadev_addr.cport;
11536 	pmport = sata_device->satadev_addr.pmport;
11537 	qual = sata_device->satadev_addr.qual;
11538 
11539 	/* Get current port state */
11540 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11541 	    (SATA_DIP(sata_hba_inst), sata_device);
11542 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11543 	sata_update_port_info(sata_hba_inst, sata_device);
11544 
11545 	if (rval != SATA_SUCCESS ||
11546 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11547 		/*
11548 		 * Obviously, device on a failed port is not visible
11549 		 */
11550 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11551 		return (ENXIO);
11552 	}
11553 
11554 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11555 	if (qual == SATA_ADDR_PMPORT)
11556 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11557 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11558 
11559 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11560 		/* need to activate port */
11561 		target = FALSE;
11562 
11563 		/* Sanity check */
11564 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11565 			return (ENXIO);
11566 
11567 		/* Just let HBA driver to activate port */
11568 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11569 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11570 			/*
11571 			 * Port activation failure - do not change port state
11572 			 * unless the state returned by HBA indicates a port
11573 			 * failure.
11574 			 */
11575 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11576 			    cport)->cport_mutex);
11577 			sata_update_port_info(sata_hba_inst, sata_device);
11578 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11579 				if (qual == SATA_ADDR_PMPORT)
11580 					pmportinfo->pmport_state =
11581 					    SATA_PSTATE_FAILED;
11582 				else
11583 					cportinfo->cport_state =
11584 					    SATA_PSTATE_FAILED;
11585 			}
11586 			mutex_exit(&SATA_CPORT_INFO(
11587 			    sata_hba_inst, cport)->cport_mutex);
11588 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11589 			    "sata_hba_ioctl: configure: "
11590 			    "failed to activate SATA port %d:%d",
11591 			    cport, pmport));
11592 			return (EIO);
11593 		}
11594 		/*
11595 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11596 		 * without the hint.
11597 		 */
11598 		sata_gen_sysevent(sata_hba_inst,
11599 		    &sata_device->satadev_addr, SE_NO_HINT);
11600 
11601 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11602 		    cport_mutex);
11603 		/* Virgin port state */
11604 		if (qual == SATA_ADDR_PMPORT)
11605 			pmportinfo->pmport_state = 0;
11606 		else
11607 			cportinfo->cport_state = 0;
11608 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11609 	}
11610 	/*
11611 	 * Always reprobe port, to get current device info.
11612 	 */
11613 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11614 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11615 		return (EIO);
11616 
11617 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11618 		if (qual == SATA_ADDR_PMPORT) {
11619 			/*
11620 			 * That's the transition from "inactive" port
11621 			 * to active one with device attached.
11622 			 */
11623 			sata_log(sata_hba_inst, CE_WARN,
11624 			    "SATA device detected at port %d:%d",
11625 			    cport, pmport);
11626 		} else {
11627 			/*
11628 			 * When PM is attached to the cport and cport is
11629 			 * activated, every PM device port needs to be reprobed.
11630 			 * We need to emit message for all devices detected
11631 			 * at port multiplier's device ports.
11632 			 * Add such code here.
11633 			 * For now, just inform about device attached to
11634 			 * cport.
11635 			 */
11636 			sata_log(sata_hba_inst, CE_WARN,
11637 			    "SATA device detected at port %d", cport);
11638 		}
11639 	}
11640 
11641 	/*
11642 	 * This is where real configuration operation starts.
11643 	 *
11644 	 * When PM is attached to the cport and cport is activated,
11645 	 * devices attached PM device ports may have to be configured
11646 	 * explicitly. This may change when port multiplier is supported.
11647 	 * For now, configure only disks and other valid target devices.
11648 	 */
11649 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11650 		if (qual == SATA_ADDR_CPORT) {
11651 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11652 				/*
11653 				 * A device was not successfully identified
11654 				 */
11655 				sata_log(sata_hba_inst, CE_WARN,
11656 				    "Could not identify SATA "
11657 				    "device at port %d", cport);
11658 			}
11659 		} else { /* port multiplier device port */
11660 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11661 				/*
11662 				 * A device was not successfully identified
11663 				 */
11664 				sata_log(sata_hba_inst, CE_WARN,
11665 				    "Could not identify SATA "
11666 				    "device at port %d:%d", cport, pmport);
11667 			}
11668 		}
11669 		return (ENXIO);		/* No device to configure */
11670 	}
11671 
11672 	/*
11673 	 * Here we may have a device in reset condition,
11674 	 * but because we are just configuring it, there is
11675 	 * no need to process the reset other than just
11676 	 * to clear device reset condition in the HBA driver.
11677 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11678 	 * cause a first command sent the HBA driver with the request
11679 	 * to clear device reset condition.
11680 	 */
11681 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11682 	if (qual == SATA_ADDR_PMPORT)
11683 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11684 	else
11685 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11686 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11687 	if (sdinfo == NULL) {
11688 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11689 		return (ENXIO);
11690 	}
11691 	if (sdinfo->satadrv_event_flags &
11692 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11693 		sdinfo->satadrv_event_flags = 0;
11694 	}
11695 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11696 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11697 
11698 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11699 	    &sata_device->satadev_addr)) != NULL) {
11700 		/*
11701 		 * Target node exists. Verify, that it belongs
11702 		 * to existing, attached device and not to
11703 		 * a removed device.
11704 		 */
11705 		if (sata_check_device_removed(tdip) == B_TRUE) {
11706 			if (qual == SATA_ADDR_DPMPORT)
11707 				sata_log(sata_hba_inst, CE_WARN,
11708 				    "SATA device at port %d cannot be "
11709 				    "configured. "
11710 				    "Application(s) accessing "
11711 				    "previously attached device "
11712 				    "have to release it before newly "
11713 				    "inserted device can be made accessible.",
11714 				    cport);
11715 			else
11716 				sata_log(sata_hba_inst, CE_WARN,
11717 				    "SATA device at port %d:%d cannot be"
11718 				    "configured. "
11719 				    "Application(s) accessing "
11720 				    "previously attached device "
11721 				    "have to release it before newly "
11722 				    "inserted device can be made accessible.",
11723 				    cport, pmport);
11724 			return (EIO);
11725 		}
11726 		/*
11727 		 * Device was not removed and re-inserted.
11728 		 * Try to online it.
11729 		 */
11730 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11731 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11732 			    "sata_hba_ioctl: configure: "
11733 			    "onlining device at SATA port "
11734 			    "%d:%d failed", cport, pmport));
11735 			return (EIO);
11736 		}
11737 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11738 		    cport)->cport_mutex);
11739 
11740 		if (qual == SATA_ADDR_DPMPORT)
11741 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11742 		else
11743 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11744 
11745 		mutex_exit(&SATA_CPORT_INFO(
11746 		    sata_hba_inst, cport)->cport_mutex);
11747 	} else {
11748 		/*
11749 		 * No target node - need to create a new target node.
11750 		 */
11751 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11752 		    cport_mutex);
11753 		if (qual == SATA_ADDR_DPMPORT)
11754 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11755 		else
11756 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11757 
11758 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11759 		    cport_mutex);
11760 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11761 		    sata_hba_inst, &sata_device->satadev_addr);
11762 		if (tdip == NULL) {
11763 			/* Configure operation failed */
11764 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11765 			    "sata_hba_ioctl: configure: "
11766 			    "configuring SATA device at port %d:%d "
11767 			    "failed", cport, pmport));
11768 			return (EIO);
11769 		}
11770 	}
11771 	return (0);
11772 }
11773 
11774 
11775 /*
11776  * Process ioctl deactivate port request.
11777  * Arbitrarily unconfigure attached device, if any.
11778  * Even if the unconfigure fails, proceed with the
11779  * port deactivation.
11780  *
11781  * NOTE: Port Multiplier code is not completed and tested.
11782  */
11783 
11784 static int
11785 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11786     sata_device_t *sata_device)
11787 {
11788 	int cport, pmport, qual;
11789 	int rval, rv = 0;
11790 	sata_cport_info_t *cportinfo;
11791 	sata_pmport_info_t *pmportinfo = NULL;
11792 	dev_info_t *tdip;
11793 	sata_drive_info_t *sdinfo = NULL;
11794 
11795 	/* Sanity check */
11796 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11797 		return (ENOTSUP);
11798 
11799 	cport = sata_device->satadev_addr.cport;
11800 	pmport = sata_device->satadev_addr.pmport;
11801 	qual = sata_device->satadev_addr.qual;
11802 
11803 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11804 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11805 	if (qual == SATA_ADDR_CPORT) {
11806 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11807 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11808 			/*
11809 			 * For now, assume that port multiplier is not
11810 			 * supported, i.e. deal only with valid devices
11811 			 */
11812 			if ((cportinfo->cport_dev_type &
11813 			    SATA_VALID_DEV_TYPE) != 0)
11814 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11815 			/*
11816 			 * If attached device is a port multiplier, we will
11817 			 * have to unconfigure all devices attached to the
11818 			 * port multiplier. Add this code here.
11819 			 */
11820 		}
11821 		cportinfo->cport_state &= ~SATA_STATE_READY;
11822 	} else {
11823 		/* Port multiplier device port */
11824 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11825 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11826 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11827 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11828 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11829 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11830 	}
11831 
11832 	if (sdinfo != NULL) {
11833 		/*
11834 		 * If a target node exists, try to offline a device and
11835 		 * to remove a target node.
11836 		 */
11837 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11838 		    cport_mutex);
11839 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11840 		    &sata_device->satadev_addr);
11841 		if (tdip != NULL) {
11842 			/* target node exist */
11843 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11844 			    "sata_hba_ioctl: port deactivate: "
11845 			    "target node exists.", NULL);
11846 
11847 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11848 			    NDI_SUCCESS) {
11849 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11850 				    "sata_hba_ioctl: port deactivate: "
11851 				    "failed to unconfigure device at port "
11852 				    "%d:%d before deactivating the port",
11853 				    cport, pmport));
11854 				/*
11855 				 * Set DEVICE REMOVED state in the target
11856 				 * node. It will prevent an access to
11857 				 * the device even when a new device is
11858 				 * attached, until the old target node is
11859 				 * released, removed and recreated for a new
11860 				 * device.
11861 				 */
11862 				sata_set_device_removed(tdip);
11863 
11864 				/*
11865 				 * Instruct the event daemon to try the
11866 				 * target node cleanup later.
11867 				 */
11868 				sata_set_target_node_cleanup(sata_hba_inst,
11869 				    &sata_device->satadev_addr);
11870 			}
11871 		}
11872 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11873 		    cport_mutex);
11874 		/*
11875 		 * In any case, remove and release sata_drive_info
11876 		 * structure.
11877 		 */
11878 		if (qual == SATA_ADDR_CPORT) {
11879 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11880 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11881 		} else { /* port multiplier device port */
11882 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11883 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11884 		}
11885 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11886 	}
11887 	if (qual == SATA_ADDR_CPORT) {
11888 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11889 		    SATA_STATE_PROBING);
11890 	} else { /* port multiplier device port */
11891 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11892 		    SATA_STATE_PROBING);
11893 	}
11894 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11895 
11896 	/* Just let HBA driver to deactivate port */
11897 	sata_device->satadev_addr.qual = qual;
11898 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11899 	    (SATA_DIP(sata_hba_inst), sata_device);
11900 
11901 	/*
11902 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11903 	 * without the hint
11904 	 */
11905 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11906 	    SE_NO_HINT);
11907 
11908 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11909 	sata_update_port_info(sata_hba_inst, sata_device);
11910 	if (qual == SATA_ADDR_CPORT) {
11911 		if (rval != SATA_SUCCESS) {
11912 			/*
11913 			 * Port deactivation failure - do not change port state
11914 			 * unless the state returned by HBA indicates a port
11915 			 * failure.
11916 			 */
11917 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11918 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11919 				    SATA_PSTATE_FAILED;
11920 			}
11921 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11922 			    "sata_hba_ioctl: port deactivate: "
11923 			    "cannot deactivate SATA port %d", cport));
11924 			rv = EIO;
11925 		} else {
11926 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11927 		}
11928 	} else {
11929 		if (rval != SATA_SUCCESS) {
11930 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11931 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11932 				    pmport) = SATA_PSTATE_FAILED;
11933 			}
11934 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11935 			    "sata_hba_ioctl: port deactivate: "
11936 			    "cannot deactivate SATA port %d:%d",
11937 			    cport, pmport));
11938 			rv = EIO;
11939 		} else {
11940 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11941 		}
11942 	}
11943 
11944 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11945 
11946 	return (rv);
11947 }
11948 
11949 /*
11950  * Process ioctl port activate request.
11951  *
11952  * NOTE: Port multiplier code is not completed nor tested.
11953  */
11954 static int
11955 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11956     sata_device_t *sata_device)
11957 {
11958 	int cport, pmport, qual;
11959 	sata_cport_info_t *cportinfo;
11960 	sata_pmport_info_t *pmportinfo = NULL;
11961 	boolean_t dev_existed = TRUE;
11962 
11963 	/* Sanity check */
11964 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11965 		return (ENOTSUP);
11966 
11967 	cport = sata_device->satadev_addr.cport;
11968 	pmport = sata_device->satadev_addr.pmport;
11969 	qual = sata_device->satadev_addr.qual;
11970 
11971 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11972 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11973 	if (qual == SATA_ADDR_PMPORT) {
11974 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11975 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11976 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11977 			dev_existed = FALSE;
11978 	} else { /* cport */
11979 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11980 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11981 			dev_existed = FALSE;
11982 	}
11983 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11984 
11985 	/* Just let HBA driver to activate port, if necessary */
11986 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11987 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11988 		/*
11989 		 * Port activation failure - do not change port state unless
11990 		 * the state returned by HBA indicates a port failure.
11991 		 */
11992 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11993 		    cport)->cport_mutex);
11994 		sata_update_port_info(sata_hba_inst, sata_device);
11995 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11996 			if (qual == SATA_ADDR_PMPORT)
11997 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11998 			else
11999 				cportinfo->cport_state = SATA_PSTATE_FAILED;
12000 
12001 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12002 			    cport)->cport_mutex);
12003 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12004 			    "sata_hba_ioctl: port activate: cannot activate "
12005 			    "SATA port %d:%d", cport, pmport));
12006 			return (EIO);
12007 		}
12008 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12009 	}
12010 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12011 	if (qual == SATA_ADDR_PMPORT)
12012 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
12013 	else
12014 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
12015 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12016 
12017 	/*
12018 	 * Re-probe port to find its current state and possibly attached device.
12019 	 * Port re-probing may change the cportinfo device type if device is
12020 	 * found attached.
12021 	 * If port probing failed, the device type would be set to
12022 	 * SATA_DTYPE_NONE.
12023 	 */
12024 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
12025 	    SATA_DEV_IDENTIFY_RETRY);
12026 
12027 	/*
12028 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12029 	 * without the hint.
12030 	 */
12031 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12032 	    SE_NO_HINT);
12033 
12034 	if (dev_existed == FALSE) {
12035 		if (qual == SATA_ADDR_PMPORT &&
12036 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
12037 			/*
12038 			 * That's the transition from the "inactive" port state
12039 			 * or the active port without a device attached to the
12040 			 * active port state with a device attached.
12041 			 */
12042 			sata_log(sata_hba_inst, CE_WARN,
12043 			    "SATA device detected at port %d:%d",
12044 			    cport, pmport);
12045 		} else if (qual == SATA_ADDR_CPORT &&
12046 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
12047 			/*
12048 			 * That's the transition from the "inactive" port state
12049 			 * or the active port without a device attached to the
12050 			 * active port state with a device attached.
12051 			 */
12052 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
12053 				sata_log(sata_hba_inst, CE_WARN,
12054 				    "SATA device detected at port %d", cport);
12055 			} else {
12056 				sata_log(sata_hba_inst, CE_WARN,
12057 				    "SATA port multiplier detected at port %d",
12058 				    cport);
12059 				/*
12060 				 * Because the detected device is a port
12061 				 * multiplier, we need to reprobe every device
12062 				 * port on the port multiplier and show every
12063 				 * device found attached.
12064 				 * Add this code here.
12065 				 */
12066 			}
12067 		}
12068 	}
12069 	return (0);
12070 }
12071 
12072 
12073 
12074 /*
12075  * Process ioctl reset port request.
12076  *
12077  * NOTE: Port multiplier code is not completed nor tested.
12078  */
12079 static int
12080 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
12081     sata_device_t *sata_device)
12082 {
12083 	int cport, pmport, qual;
12084 	int rv = 0;
12085 
12086 	cport = sata_device->satadev_addr.cport;
12087 	pmport = sata_device->satadev_addr.pmport;
12088 	qual = sata_device->satadev_addr.qual;
12089 
12090 	/* Sanity check */
12091 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12092 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12093 		    "sata_hba_ioctl: sata_hba_tran missing required "
12094 		    "function sata_tran_reset_dport"));
12095 		return (ENOTSUP);
12096 	}
12097 
12098 	/* Ask HBA to reset port */
12099 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12100 	    sata_device) != SATA_SUCCESS) {
12101 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12102 		    "sata_hba_ioctl: reset port: failed %d:%d",
12103 		    cport, pmport));
12104 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12105 		    cport_mutex);
12106 		sata_update_port_info(sata_hba_inst, sata_device);
12107 		if (qual == SATA_ADDR_CPORT)
12108 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12109 			    SATA_PSTATE_FAILED;
12110 		else
12111 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12112 			    SATA_PSTATE_FAILED;
12113 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12114 		    cport_mutex);
12115 		rv = EIO;
12116 	}
12117 	/*
12118 	 * Beacuse the port was reset, it should be probed and
12119 	 * attached device reinitialized. At this point the
12120 	 * port state is unknown - it's state is HBA-specific.
12121 	 * Re-probe port to get its state.
12122 	 */
12123 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12124 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
12125 		rv = EIO;
12126 	}
12127 	return (rv);
12128 }
12129 
12130 /*
12131  * Process ioctl reset device request.
12132  *
12133  * NOTE: Port multiplier code is not completed nor tested.
12134  */
12135 static int
12136 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
12137     sata_device_t *sata_device)
12138 {
12139 	sata_drive_info_t *sdinfo;
12140 	int cport, pmport;
12141 	int rv = 0;
12142 
12143 	/* Sanity check */
12144 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12145 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12146 		    "sata_hba_ioctl: sata_hba_tran missing required "
12147 		    "function sata_tran_reset_dport"));
12148 		return (ENOTSUP);
12149 	}
12150 
12151 	cport = sata_device->satadev_addr.cport;
12152 	pmport = sata_device->satadev_addr.pmport;
12153 
12154 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12155 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
12156 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12157 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12158 		    sata_device->satadev_addr.cport);
12159 	} else { /* port multiplier */
12160 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12161 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12162 		    sata_device->satadev_addr.cport,
12163 		    sata_device->satadev_addr.pmport);
12164 	}
12165 	if (sdinfo == NULL) {
12166 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12167 		return (EINVAL);
12168 	}
12169 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12170 
12171 	/* Ask HBA to reset device */
12172 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12173 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12174 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12175 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
12176 		    cport, pmport));
12177 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12178 		    cport_mutex);
12179 		sata_update_port_info(sata_hba_inst, sata_device);
12180 		/*
12181 		 * Device info structure remains attached. Another device reset
12182 		 * or port disconnect/connect and re-probing is
12183 		 * needed to change it's state
12184 		 */
12185 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
12186 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
12187 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12188 		rv = EIO;
12189 	}
12190 	/*
12191 	 * If attached device was a port multiplier, some extra processing
12192 	 * may be needed, to bring it back (if port re-probing did not handle
12193 	 * it). Add such code here.
12194 	 */
12195 	return (rv);
12196 }
12197 
12198 
12199 /*
12200  * Process ioctl reset all request.
12201  *
12202  * NOTE: Port multiplier code is not completed nor tested.
12203  */
12204 static int
12205 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
12206 {
12207 	sata_device_t sata_device;
12208 	int rv = 0;
12209 	int tcport;
12210 	int tpmport = 0;
12211 
12212 	sata_device.satadev_rev = SATA_DEVICE_REV;
12213 
12214 	/*
12215 	 * There is no protection here for configured devices.
12216 	 */
12217 	/* Sanity check */
12218 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12219 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12220 		    "sata_hba_ioctl: sata_hba_tran missing required "
12221 		    "function sata_tran_reset_dport"));
12222 		return (ENOTSUP);
12223 	}
12224 
12225 	/*
12226 	 * Need to lock all ports, not just one.
12227 	 * If any port is locked by event processing, fail the whole operation.
12228 	 * One port is already locked, but for simplicity lock it again.
12229 	 */
12230 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12231 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12232 		    cport_mutex);
12233 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
12234 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
12235 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12236 			    cport_mutex);
12237 			rv = EBUSY;
12238 			break;
12239 		} else {
12240 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
12241 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
12242 			/*
12243 			 * If there is a port multiplier attached, we may need
12244 			 * to lock its port as well. If so, add such code here.
12245 			 */
12246 		}
12247 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12248 		    cport_mutex);
12249 	}
12250 
12251 	if (rv == 0) {
12252 		/*
12253 		 * All cports were successfully locked.
12254 		 * Reset main SATA controller only for now - no PMult.
12255 		 * Set the device address to port 0, to have a valid device
12256 		 * address.
12257 		 */
12258 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
12259 		sata_device.satadev_addr.cport = 0;
12260 		sata_device.satadev_addr.pmport = 0;
12261 
12262 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12263 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
12264 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12265 			    "sata_hba_ioctl: reset controller failed"));
12266 			return (EIO);
12267 		}
12268 		/*
12269 		 * Because ports were reset, port states are unknown.
12270 		 * They should be re-probed to get their state and
12271 		 * attached devices should be reinitialized.
12272 		 * Add code here to re-probe port multiplier device ports.
12273 		 */
12274 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
12275 		    tcport++) {
12276 			sata_device.satadev_addr.cport = tcport;
12277 			sata_device.satadev_addr.pmport = tpmport;
12278 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
12279 
12280 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
12281 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12282 				rv = EIO;
12283 		}
12284 	}
12285 	/*
12286 	 * Unlock all ports
12287 	 */
12288 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12289 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12290 		    cport_mutex);
12291 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
12292 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
12293 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12294 		    cport_mutex);
12295 	}
12296 
12297 	/*
12298 	 * This operation returns EFAULT if either reset
12299 	 * controller failed or a re-probing of any port failed.
12300 	 */
12301 	return (rv);
12302 }
12303 
12304 
12305 /*
12306  * Process ioctl port self test request.
12307  *
12308  * NOTE: Port multiplier code is not completed nor tested.
12309  */
12310 static int
12311 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12312     sata_device_t *sata_device)
12313 {
12314 	int cport, pmport, qual;
12315 	int rv = 0;
12316 
12317 	/* Sanity check */
12318 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12319 		return (ENOTSUP);
12320 
12321 	cport = sata_device->satadev_addr.cport;
12322 	pmport = sata_device->satadev_addr.pmport;
12323 	qual = sata_device->satadev_addr.qual;
12324 
12325 	/*
12326 	 * There is no protection here for a configured
12327 	 * device attached to this port.
12328 	 */
12329 
12330 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12331 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12332 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12333 		    "sata_hba_ioctl: port selftest: "
12334 		    "failed port %d:%d", cport, pmport));
12335 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12336 		    cport_mutex);
12337 		sata_update_port_info(sata_hba_inst, sata_device);
12338 		if (qual == SATA_ADDR_CPORT)
12339 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12340 			    SATA_PSTATE_FAILED;
12341 		else /* port ultiplier device port */
12342 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12343 			    SATA_PSTATE_FAILED;
12344 
12345 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12346 		    cport_mutex);
12347 		return (EIO);
12348 	}
12349 	/*
12350 	 * Beacuse the port was reset in the course of testing, it should be
12351 	 * re-probed and attached device state should be restored. At this
12352 	 * point the port state is unknown - it's state is HBA-specific.
12353 	 * Force port re-probing to get it into a known state.
12354 	 */
12355 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12356 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12357 		rv = EIO;
12358 	return (rv);
12359 }
12360 
12361 
12362 /*
12363  * sata_cfgadm_state:
12364  * Use the sata port state and state of the target node to figure out
12365  * the cfgadm_state.
12366  *
12367  * The port argument is a value with encoded cport,
12368  * pmport and address qualifier, in the same manner as a scsi target number.
12369  * SCSI_TO_SATA_CPORT macro extracts cport number,
12370  * SCSI_TO_SATA_PMPORT extracts pmport number and
12371  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12372  *
12373  * For now, support is for cports only - no port multiplier device ports.
12374  */
12375 
12376 static void
12377 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12378     devctl_ap_state_t *ap_state)
12379 {
12380 	uint16_t	cport;
12381 	int		port_state;
12382 	sata_drive_info_t *sdinfo;
12383 
12384 	/* Cport only */
12385 	cport = SCSI_TO_SATA_CPORT(port);
12386 
12387 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12388 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12389 	    port_state & SATA_PSTATE_FAILED) {
12390 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12391 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12392 		if (port_state & SATA_PSTATE_FAILED)
12393 			ap_state->ap_condition = AP_COND_FAILED;
12394 		else
12395 			ap_state->ap_condition = AP_COND_UNKNOWN;
12396 
12397 		return;
12398 	}
12399 
12400 	/* Need to check pmult device port here as well, when supported */
12401 
12402 	/* Port is enabled and ready */
12403 
12404 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12405 	case SATA_DTYPE_NONE:
12406 	{
12407 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12408 		ap_state->ap_condition = AP_COND_OK;
12409 		/* No device attached */
12410 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12411 		break;
12412 	}
12413 	case SATA_DTYPE_UNKNOWN:
12414 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12415 	case SATA_DTYPE_ATADISK:
12416 	case SATA_DTYPE_ATAPICD:
12417 	case SATA_DTYPE_ATAPITAPE:
12418 	case SATA_DTYPE_ATAPIDISK:
12419 	{
12420 		dev_info_t *tdip = NULL;
12421 		dev_info_t *dip = NULL;
12422 		int circ;
12423 
12424 		dip = SATA_DIP(sata_hba_inst);
12425 		tdip = sata_get_target_dip(dip, port);
12426 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12427 		if (tdip != NULL) {
12428 			ndi_devi_enter(dip, &circ);
12429 			mutex_enter(&(DEVI(tdip)->devi_lock));
12430 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12431 				/*
12432 				 * There could be the case where previously
12433 				 * configured and opened device was removed
12434 				 * and unknown device was plugged.
12435 				 * In such case we want to show a device, and
12436 				 * its configured or unconfigured state but
12437 				 * indicate unusable condition untill the
12438 				 * old target node is released and removed.
12439 				 */
12440 				ap_state->ap_condition = AP_COND_UNUSABLE;
12441 			} else {
12442 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12443 				    cport));
12444 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12445 				    cport);
12446 				if (sdinfo != NULL) {
12447 					if ((sdinfo->satadrv_state &
12448 					    SATA_DSTATE_FAILED) != 0)
12449 						ap_state->ap_condition =
12450 						    AP_COND_FAILED;
12451 					else
12452 						ap_state->ap_condition =
12453 						    AP_COND_OK;
12454 				} else {
12455 					ap_state->ap_condition =
12456 					    AP_COND_UNKNOWN;
12457 				}
12458 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12459 				    cport));
12460 			}
12461 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12462 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12463 				ap_state->ap_ostate =
12464 				    AP_OSTATE_UNCONFIGURED;
12465 			} else {
12466 				ap_state->ap_ostate =
12467 				    AP_OSTATE_CONFIGURED;
12468 			}
12469 			mutex_exit(&(DEVI(tdip)->devi_lock));
12470 			ndi_devi_exit(dip, circ);
12471 		} else {
12472 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12473 			ap_state->ap_condition = AP_COND_UNKNOWN;
12474 		}
12475 		break;
12476 	}
12477 	default:
12478 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12479 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12480 		ap_state->ap_condition = AP_COND_UNKNOWN;
12481 		/*
12482 		 * This is actually internal error condition (non fatal),
12483 		 * because we have already checked all defined device types.
12484 		 */
12485 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12486 		    "sata_cfgadm_state: Internal error: "
12487 		    "unknown device type"));
12488 		break;
12489 	}
12490 }
12491 
12492 
12493 /*
12494  * Process ioctl get device path request.
12495  *
12496  * NOTE: Port multiplier code is not completed nor tested.
12497  */
12498 static int
12499 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12500     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12501 {
12502 	char path[MAXPATHLEN];
12503 	uint32_t size;
12504 	dev_info_t *tdip;
12505 
12506 	(void) strcpy(path, "/devices");
12507 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12508 	    &sata_device->satadev_addr)) == NULL) {
12509 		/*
12510 		 * No such device. If this is a request for a size, do not
12511 		 * return EINVAL for non-existing target, because cfgadm
12512 		 * will then indicate a meaningless ioctl failure.
12513 		 * If this is a request for a path, indicate invalid
12514 		 * argument.
12515 		 */
12516 		if (ioc->get_size == 0)
12517 			return (EINVAL);
12518 	} else {
12519 		(void) ddi_pathname(tdip, path + strlen(path));
12520 	}
12521 	size = strlen(path) + 1;
12522 
12523 	if (ioc->get_size != 0) {
12524 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12525 		    mode) != 0)
12526 			return (EFAULT);
12527 	} else {
12528 		if (ioc->bufsiz != size)
12529 			return (EINVAL);
12530 
12531 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12532 		    mode) != 0)
12533 			return (EFAULT);
12534 	}
12535 	return (0);
12536 }
12537 
12538 /*
12539  * Process ioctl get attachment point type request.
12540  *
12541  * NOTE: Port multiplier code is not completed nor tested.
12542  */
12543 static	int
12544 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12545     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12546 {
12547 	uint32_t	type_len;
12548 	const char	*ap_type;
12549 	int		dev_type;
12550 
12551 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12552 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12553 		    sata_device->satadev_addr.cport);
12554 	else /* pmport */
12555 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12556 		    sata_device->satadev_addr.cport,
12557 		    sata_device->satadev_addr.pmport);
12558 
12559 	switch (dev_type) {
12560 	case SATA_DTYPE_NONE:
12561 		ap_type = "port";
12562 		break;
12563 
12564 	case SATA_DTYPE_ATADISK:
12565 	case SATA_DTYPE_ATAPIDISK:
12566 		ap_type = "disk";
12567 		break;
12568 
12569 	case SATA_DTYPE_ATAPICD:
12570 		ap_type = "cd/dvd";
12571 		break;
12572 
12573 	case SATA_DTYPE_ATAPITAPE:
12574 		ap_type = "tape";
12575 		break;
12576 
12577 	case SATA_DTYPE_PMULT:
12578 		ap_type = "pmult";
12579 		break;
12580 
12581 	case SATA_DTYPE_UNKNOWN:
12582 		ap_type = "unknown";
12583 		break;
12584 
12585 	default:
12586 		ap_type = "unsupported";
12587 		break;
12588 
12589 	} /* end of dev_type switch */
12590 
12591 	type_len = strlen(ap_type) + 1;
12592 
12593 	if (ioc->get_size) {
12594 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12595 		    mode) != 0)
12596 			return (EFAULT);
12597 	} else {
12598 		if (ioc->bufsiz != type_len)
12599 			return (EINVAL);
12600 
12601 		if (ddi_copyout((void *)ap_type, ioc->buf,
12602 		    ioc->bufsiz, mode) != 0)
12603 			return (EFAULT);
12604 	}
12605 	return (0);
12606 
12607 }
12608 
12609 /*
12610  * Process ioctl get device model info request.
12611  * This operation should return to cfgadm the device model
12612  * information string
12613  *
12614  * NOTE: Port multiplier code is not completed nor tested.
12615  */
12616 static	int
12617 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12618     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12619 {
12620 	sata_drive_info_t *sdinfo;
12621 	uint32_t info_len;
12622 	char ap_info[SATA_ID_MODEL_LEN + 1];
12623 
12624 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12625 	    sata_device->satadev_addr.cport)->cport_mutex);
12626 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12627 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12628 		    sata_device->satadev_addr.cport);
12629 	else /* port multiplier */
12630 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12631 		    sata_device->satadev_addr.cport,
12632 		    sata_device->satadev_addr.pmport);
12633 	if (sdinfo == NULL) {
12634 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12635 		    sata_device->satadev_addr.cport)->cport_mutex);
12636 		return (EINVAL);
12637 	}
12638 
12639 #ifdef	_LITTLE_ENDIAN
12640 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12641 #else	/* _LITTLE_ENDIAN */
12642 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12643 #endif	/* _LITTLE_ENDIAN */
12644 
12645 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12646 	    sata_device->satadev_addr.cport)->cport_mutex);
12647 
12648 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12649 
12650 	info_len = strlen(ap_info) + 1;
12651 
12652 	if (ioc->get_size) {
12653 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12654 		    mode) != 0)
12655 			return (EFAULT);
12656 	} else {
12657 		if (ioc->bufsiz < info_len)
12658 			return (EINVAL);
12659 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12660 		    mode) != 0)
12661 			return (EFAULT);
12662 	}
12663 	return (0);
12664 }
12665 
12666 
12667 /*
12668  * Process ioctl get device firmware revision info request.
12669  * This operation should return to cfgadm the device firmware revision
12670  * information string
12671  *
12672  * NOTE: Port multiplier code is not completed nor tested.
12673  */
12674 static	int
12675 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12676     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12677 {
12678 	sata_drive_info_t *sdinfo;
12679 	uint32_t info_len;
12680 	char ap_info[SATA_ID_FW_LEN + 1];
12681 
12682 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12683 	    sata_device->satadev_addr.cport)->cport_mutex);
12684 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12685 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12686 		    sata_device->satadev_addr.cport);
12687 	else /* port multiplier */
12688 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12689 		    sata_device->satadev_addr.cport,
12690 		    sata_device->satadev_addr.pmport);
12691 	if (sdinfo == NULL) {
12692 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12693 		    sata_device->satadev_addr.cport)->cport_mutex);
12694 		return (EINVAL);
12695 	}
12696 
12697 #ifdef	_LITTLE_ENDIAN
12698 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12699 #else	/* _LITTLE_ENDIAN */
12700 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12701 #endif	/* _LITTLE_ENDIAN */
12702 
12703 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12704 	    sata_device->satadev_addr.cport)->cport_mutex);
12705 
12706 	ap_info[SATA_ID_FW_LEN] = '\0';
12707 
12708 	info_len = strlen(ap_info) + 1;
12709 
12710 	if (ioc->get_size) {
12711 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12712 		    mode) != 0)
12713 			return (EFAULT);
12714 	} else {
12715 		if (ioc->bufsiz < info_len)
12716 			return (EINVAL);
12717 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12718 		    mode) != 0)
12719 			return (EFAULT);
12720 	}
12721 	return (0);
12722 }
12723 
12724 
12725 /*
12726  * Process ioctl get device serial number info request.
12727  * This operation should return to cfgadm the device serial number string.
12728  *
12729  * NOTE: Port multiplier code is not completed nor tested.
12730  */
12731 static	int
12732 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12733     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12734 {
12735 	sata_drive_info_t *sdinfo;
12736 	uint32_t info_len;
12737 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12738 
12739 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12740 	    sata_device->satadev_addr.cport)->cport_mutex);
12741 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12742 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12743 		    sata_device->satadev_addr.cport);
12744 	else /* port multiplier */
12745 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12746 		    sata_device->satadev_addr.cport,
12747 		    sata_device->satadev_addr.pmport);
12748 	if (sdinfo == NULL) {
12749 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12750 		    sata_device->satadev_addr.cport)->cport_mutex);
12751 		return (EINVAL);
12752 	}
12753 
12754 #ifdef	_LITTLE_ENDIAN
12755 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12756 #else	/* _LITTLE_ENDIAN */
12757 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12758 #endif	/* _LITTLE_ENDIAN */
12759 
12760 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12761 	    sata_device->satadev_addr.cport)->cport_mutex);
12762 
12763 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12764 
12765 	info_len = strlen(ap_info) + 1;
12766 
12767 	if (ioc->get_size) {
12768 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12769 		    mode) != 0)
12770 			return (EFAULT);
12771 	} else {
12772 		if (ioc->bufsiz < info_len)
12773 			return (EINVAL);
12774 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12775 		    mode) != 0)
12776 			return (EFAULT);
12777 	}
12778 	return (0);
12779 }
12780 
12781 
12782 /*
12783  * Preset scsi extended sense data (to NO SENSE)
12784  * First 18 bytes of the sense data are preset to current valid sense
12785  * with a key NO SENSE data.
12786  *
12787  * Returns void
12788  */
12789 static void
12790 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12791 {
12792 	sense->es_valid = 1;		/* Valid sense */
12793 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12794 	sense->es_key = KEY_NO_SENSE;
12795 	sense->es_info_1 = 0;
12796 	sense->es_info_2 = 0;
12797 	sense->es_info_3 = 0;
12798 	sense->es_info_4 = 0;
12799 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12800 	sense->es_cmd_info[0] = 0;
12801 	sense->es_cmd_info[1] = 0;
12802 	sense->es_cmd_info[2] = 0;
12803 	sense->es_cmd_info[3] = 0;
12804 	sense->es_add_code = 0;
12805 	sense->es_qual_code = 0;
12806 }
12807 
12808 /*
12809  * Register a legacy cmdk-style devid for the target (disk) device.
12810  *
12811  * Note: This function is called only when the HBA devinfo node has the
12812  * property "use-cmdk-devid-format" set. This property indicates that
12813  * devid compatible with old cmdk (target) driver is to be generated
12814  * for any target device attached to this controller. This will take
12815  * precedence over the devid generated by sd (target) driver.
12816  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12817  */
12818 static void
12819 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12820 {
12821 	char	*hwid;
12822 	int	modlen;
12823 	int	serlen;
12824 	int	rval;
12825 	ddi_devid_t	devid;
12826 
12827 	/*
12828 	 * device ID is a concatanation of model number, "=", serial number.
12829 	 */
12830 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12831 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12832 	    sizeof (sdinfo->satadrv_id.ai_model));
12833 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12834 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12835 	if (modlen == 0)
12836 		goto err;
12837 	hwid[modlen++] = '=';
12838 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12839 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12840 	swab(&hwid[modlen], &hwid[modlen],
12841 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12842 	serlen = sata_check_modser(&hwid[modlen],
12843 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12844 	if (serlen == 0)
12845 		goto err;
12846 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12847 
12848 	/* initialize/register devid */
12849 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12850 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
12851 		rval = ddi_devid_register(dip, devid);
12852 		/*
12853 		 * Free up the allocated devid buffer.
12854 		 * NOTE: This doesn't mean unregistering devid.
12855 		 */
12856 		ddi_devid_free(devid);
12857 	}
12858 
12859 	if (rval != DDI_SUCCESS)
12860 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12861 		    " on port %d", sdinfo->satadrv_addr.cport);
12862 err:
12863 	kmem_free(hwid, LEGACY_HWID_LEN);
12864 }
12865 
12866 /*
12867  * valid model/serial string must contain a non-zero non-space characters.
12868  * trim trailing spaces/NULLs.
12869  */
12870 static int
12871 sata_check_modser(char *buf, int buf_len)
12872 {
12873 	boolean_t ret;
12874 	char *s;
12875 	int i;
12876 	int tb;
12877 	char ch;
12878 
12879 	ret = B_FALSE;
12880 	s = buf;
12881 	for (i = 0; i < buf_len; i++) {
12882 		ch = *s++;
12883 		if (ch != ' ' && ch != '\0')
12884 			tb = i + 1;
12885 		if (ch != ' ' && ch != '\0' && ch != '0')
12886 			ret = B_TRUE;
12887 	}
12888 
12889 	if (ret == B_FALSE)
12890 		return (0); /* invalid string */
12891 
12892 	return (tb); /* return length */
12893 }
12894 
12895 /*
12896  * sata_set_drive_features function compares current device features setting
12897  * with the saved device features settings and, if there is a difference,
12898  * it restores device features setting to the previously saved state.
12899  * It also arbitrarily tries to select the highest supported DMA mode.
12900  * Device Identify or Identify Packet Device data has to be current.
12901  * At the moment read ahead and write cache are considered for all devices.
12902  * For atapi devices, Removable Media Status Notification is set in addition
12903  * to common features.
12904  *
12905  * This function cannot be called in the interrupt context (it may sleep).
12906  *
12907  * The input argument sdinfo should point to the drive info structure
12908  * to be updated after features are set. Note, that only
12909  * device (packet) identify data is updated, not the flags indicating the
12910  * supported features.
12911  *
12912  * Returns SATA_SUCCESS if successful or there was nothing to do.
12913  * Device Identify data in the drive info structure pointed to by the sdinfo
12914  * arguments is updated even when no features were set or changed.
12915  *
12916  * Returns SATA_FAILURE if device features could not be set or DMA mode
12917  * for a disk cannot be set and device identify data cannot be fetched.
12918  *
12919  * Returns SATA_RETRY if device features could not be set (other than disk
12920  * DMA mode) but the device identify data was fetched successfully.
12921  *
12922  * Note: This function may fail the port, making it inaccessible.
12923  * In such case the explicit port disconnect/connect or physical device
12924  * detach/attach is required to re-evaluate port state again.
12925  */
12926 
12927 static int
12928 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12929     sata_drive_info_t *sdinfo, int restore)
12930 {
12931 	int rval = SATA_SUCCESS;
12932 	int rval_set;
12933 	sata_drive_info_t new_sdinfo;
12934 	char *finfo = "sata_set_drive_features: cannot";
12935 	char *finfox;
12936 	int cache_op;
12937 
12938 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12939 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12940 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12941 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12942 		/*
12943 		 * Cannot get device identification - caller may retry later
12944 		 */
12945 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12946 		    "%s fetch device identify data\n", finfo);
12947 		return (SATA_FAILURE);
12948 	}
12949 	finfox = (restore != 0) ? " restore device features" :
12950 	    " initialize device features\n";
12951 
12952 	switch (sdinfo->satadrv_type) {
12953 	case SATA_DTYPE_ATADISK:
12954 		/* Arbitrarily set UDMA mode */
12955 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12956 		    SATA_SUCCESS) {
12957 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12958 			    "%s set UDMA mode\n", finfo));
12959 			return (SATA_FAILURE);
12960 		}
12961 		break;
12962 	case SATA_DTYPE_ATAPICD:
12963 	case SATA_DTYPE_ATAPITAPE:
12964 	case SATA_DTYPE_ATAPIDISK:
12965 		/*  Set Removable Media Status Notification, if necessary */
12966 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
12967 		    restore != 0) {
12968 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12969 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
12970 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12971 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
12972 				/* Current setting does not match saved one */
12973 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12974 				    sdinfo->satadrv_settings &
12975 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12976 					rval = SATA_FAILURE;
12977 			}
12978 		}
12979 		/*
12980 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12981 		 * we want to use DMA transfer mode whenever possible.
12982 		 * Some devices require explicit setting of the DMA mode.
12983 		 */
12984 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12985 			/* Set highest supported DMA mode */
12986 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12987 			    SATA_SUCCESS) {
12988 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12989 				    "%s set UDMA mode\n", finfo));
12990 				rval = SATA_FAILURE;
12991 			}
12992 		}
12993 		break;
12994 	}
12995 
12996 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
12997 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
12998 		/*
12999 		 * neither READ AHEAD nor WRITE CACHE is supported
13000 		 * - do nothing
13001 		 */
13002 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13003 		    "settable features not supported\n", NULL);
13004 		goto update_sdinfo;
13005 	}
13006 
13007 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
13008 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
13009 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
13010 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
13011 		/*
13012 		 * both READ AHEAD and WRITE CACHE are enabled
13013 		 * - Nothing to do
13014 		 */
13015 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13016 		    "no device features to set\n", NULL);
13017 		goto update_sdinfo;
13018 	}
13019 
13020 	cache_op = 0;
13021 
13022 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
13023 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13024 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13025 			/* Enable read ahead / read cache */
13026 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
13027 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13028 			    "enabling read cache\n", NULL);
13029 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13030 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13031 			/* Disable read ahead  / read cache */
13032 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
13033 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13034 			    "disabling read cache\n", NULL);
13035 		}
13036 
13037 		if (cache_op != 0) {
13038 			/* Try to set read cache mode */
13039 			rval_set = sata_set_cache_mode(sata_hba_inst,
13040 			    &new_sdinfo, cache_op);
13041 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13042 				rval = rval_set;
13043 		}
13044 	}
13045 
13046 	cache_op = 0;
13047 
13048 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
13049 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13050 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13051 			/* Enable write cache */
13052 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
13053 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13054 			    "enabling write cache\n", NULL);
13055 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13056 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13057 			/* Disable write cache */
13058 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
13059 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13060 			    "disabling write cache\n", NULL);
13061 		}
13062 
13063 		if (cache_op != 0) {
13064 			/* Try to set write cache mode */
13065 			rval_set = sata_set_cache_mode(sata_hba_inst,
13066 			    &new_sdinfo, cache_op);
13067 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13068 				rval = rval_set;
13069 		}
13070 	}
13071 	if (rval != SATA_SUCCESS)
13072 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13073 		    "%s %s", finfo, finfox));
13074 
13075 update_sdinfo:
13076 	/*
13077 	 * We need to fetch Device Identify data again
13078 	 */
13079 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
13080 		/*
13081 		 * Cannot get device identification - retry later
13082 		 */
13083 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13084 		    "%s re-fetch device identify data\n", finfo));
13085 		rval = SATA_FAILURE;
13086 	}
13087 	/* Copy device sata info. */
13088 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
13089 
13090 	return (rval);
13091 }
13092 
13093 
13094 /*
13095  *
13096  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
13097  * unable to determine.
13098  *
13099  * Cannot be called in an interrupt context.
13100  *
13101  * Called by sata_build_lsense_page_2f()
13102  */
13103 
13104 static int
13105 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
13106     sata_drive_info_t *sdinfo)
13107 {
13108 	sata_pkt_t *spkt;
13109 	sata_cmd_t *scmd;
13110 	sata_pkt_txlate_t *spx;
13111 	int rval;
13112 
13113 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13114 	spx->txlt_sata_hba_inst = sata_hba_inst;
13115 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13116 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13117 	if (spkt == NULL) {
13118 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13119 		return (-1);
13120 	}
13121 	/* address is needed now */
13122 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13123 
13124 
13125 	/* Fill sata_pkt */
13126 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13127 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13128 	/* Synchronous mode, no callback */
13129 	spkt->satapkt_comp = NULL;
13130 	/* Timeout 30s */
13131 	spkt->satapkt_time = sata_default_pkt_time;
13132 
13133 	scmd = &spkt->satapkt_cmd;
13134 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
13135 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13136 
13137 	/* Set up which registers need to be returned */
13138 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
13139 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
13140 
13141 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
13142 	scmd->satacmd_addr_type = 0;		/* N/A */
13143 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13144 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13145 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13146 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13147 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
13148 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13149 	scmd->satacmd_cmd_reg = SATAC_SMART;
13150 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13151 	    sdinfo->satadrv_addr.cport)));
13152 
13153 
13154 	/* Send pkt to SATA HBA driver */
13155 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13156 	    SATA_TRAN_ACCEPTED ||
13157 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13158 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13159 		    sdinfo->satadrv_addr.cport)));
13160 		/*
13161 		 * Whoops, no SMART RETURN STATUS
13162 		 */
13163 		rval = -1;
13164 	} else {
13165 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13166 		    sdinfo->satadrv_addr.cport)));
13167 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
13168 			rval = -1;
13169 			goto fail;
13170 		}
13171 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
13172 			rval = -1;
13173 			goto fail;
13174 		}
13175 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
13176 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
13177 			rval = 0;
13178 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
13179 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
13180 			rval = 1;
13181 		else {
13182 			rval = -1;
13183 			goto fail;
13184 		}
13185 	}
13186 fail:
13187 	/* Free allocated resources */
13188 	sata_pkt_free(spx);
13189 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13190 
13191 	return (rval);
13192 }
13193 
13194 /*
13195  *
13196  * Returns 0 if succeeded, -1 otherwise
13197  *
13198  * Cannot be called in an interrupt context.
13199  *
13200  */
13201 static int
13202 sata_fetch_smart_data(
13203 	sata_hba_inst_t *sata_hba_inst,
13204 	sata_drive_info_t *sdinfo,
13205 	struct smart_data *smart_data)
13206 {
13207 	sata_pkt_t *spkt;
13208 	sata_cmd_t *scmd;
13209 	sata_pkt_txlate_t *spx;
13210 	int rval;
13211 
13212 #if ! defined(lint)
13213 	ASSERT(sizeof (struct smart_data) == 512);
13214 #endif
13215 
13216 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13217 	spx->txlt_sata_hba_inst = sata_hba_inst;
13218 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13219 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13220 	if (spkt == NULL) {
13221 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13222 		return (-1);
13223 	}
13224 	/* address is needed now */
13225 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13226 
13227 
13228 	/* Fill sata_pkt */
13229 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13230 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13231 	/* Synchronous mode, no callback */
13232 	spkt->satapkt_comp = NULL;
13233 	/* Timeout 30s */
13234 	spkt->satapkt_time = sata_default_pkt_time;
13235 
13236 	scmd = &spkt->satapkt_cmd;
13237 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13238 
13239 	/*
13240 	 * Allocate buffer for SMART data
13241 	 */
13242 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13243 	    sizeof (struct smart_data));
13244 	if (scmd->satacmd_bp == NULL) {
13245 		sata_pkt_free(spx);
13246 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13247 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13248 		    "sata_fetch_smart_data: "
13249 		    "cannot allocate buffer"));
13250 		return (-1);
13251 	}
13252 
13253 
13254 	/* Build SMART_READ_DATA cmd in the sata_pkt */
13255 	scmd->satacmd_addr_type = 0;		/* N/A */
13256 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13257 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13258 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13259 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13260 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
13261 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13262 	scmd->satacmd_cmd_reg = SATAC_SMART;
13263 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13264 	    sdinfo->satadrv_addr.cport)));
13265 
13266 	/* Send pkt to SATA HBA driver */
13267 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13268 	    SATA_TRAN_ACCEPTED ||
13269 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13270 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13271 		    sdinfo->satadrv_addr.cport)));
13272 		/*
13273 		 * Whoops, no SMART DATA available
13274 		 */
13275 		rval = -1;
13276 		goto fail;
13277 	} else {
13278 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13279 		    sdinfo->satadrv_addr.cport)));
13280 		if (spx->txlt_buf_dma_handle != NULL) {
13281 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13282 			    DDI_DMA_SYNC_FORKERNEL);
13283 			ASSERT(rval == DDI_SUCCESS);
13284 		}
13285 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
13286 		    sizeof (struct smart_data));
13287 	}
13288 
13289 fail:
13290 	/* Free allocated resources */
13291 	sata_free_local_buffer(spx);
13292 	sata_pkt_free(spx);
13293 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13294 
13295 	return (rval);
13296 }
13297 
13298 /*
13299  * Used by LOG SENSE page 0x10
13300  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
13301  * Note: cannot be called in the interrupt context.
13302  *
13303  * return 0 for success, -1 otherwise
13304  *
13305  */
13306 static int
13307 sata_ext_smart_selftest_read_log(
13308 	sata_hba_inst_t *sata_hba_inst,
13309 	sata_drive_info_t *sdinfo,
13310 	struct smart_ext_selftest_log *ext_selftest_log,
13311 	uint16_t block_num)
13312 {
13313 	sata_pkt_txlate_t *spx;
13314 	sata_pkt_t *spkt;
13315 	sata_cmd_t *scmd;
13316 	int rval;
13317 
13318 #if ! defined(lint)
13319 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
13320 #endif
13321 
13322 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13323 	spx->txlt_sata_hba_inst = sata_hba_inst;
13324 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13325 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13326 	if (spkt == NULL) {
13327 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13328 		return (-1);
13329 	}
13330 	/* address is needed now */
13331 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13332 
13333 
13334 	/* Fill sata_pkt */
13335 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13336 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13337 	/* Synchronous mode, no callback */
13338 	spkt->satapkt_comp = NULL;
13339 	/* Timeout 30s */
13340 	spkt->satapkt_time = sata_default_pkt_time;
13341 
13342 	scmd = &spkt->satapkt_cmd;
13343 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13344 
13345 	/*
13346 	 * Allocate buffer for SMART extended self-test log
13347 	 */
13348 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13349 	    sizeof (struct smart_ext_selftest_log));
13350 	if (scmd->satacmd_bp == NULL) {
13351 		sata_pkt_free(spx);
13352 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13353 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13354 		    "sata_ext_smart_selftest_log: "
13355 		    "cannot allocate buffer"));
13356 		return (-1);
13357 	}
13358 
13359 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13360 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13361 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13362 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13363 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13364 	scmd->satacmd_lba_low_msb = 0;
13365 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13366 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13367 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13368 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13369 
13370 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13371 	    sdinfo->satadrv_addr.cport)));
13372 
13373 	/* Send pkt to SATA HBA driver */
13374 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13375 	    SATA_TRAN_ACCEPTED ||
13376 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13377 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13378 		    sdinfo->satadrv_addr.cport)));
13379 
13380 		/*
13381 		 * Whoops, no SMART selftest log info available
13382 		 */
13383 		rval = -1;
13384 		goto fail;
13385 	} else {
13386 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13387 		    sdinfo->satadrv_addr.cport)));
13388 
13389 		if (spx->txlt_buf_dma_handle != NULL) {
13390 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13391 			    DDI_DMA_SYNC_FORKERNEL);
13392 			ASSERT(rval == DDI_SUCCESS);
13393 		}
13394 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13395 		    (uint8_t *)ext_selftest_log,
13396 		    sizeof (struct smart_ext_selftest_log));
13397 		rval = 0;
13398 	}
13399 
13400 fail:
13401 	/* Free allocated resources */
13402 	sata_free_local_buffer(spx);
13403 	sata_pkt_free(spx);
13404 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13405 
13406 	return (rval);
13407 }
13408 
13409 /*
13410  * Returns 0 for success, -1 otherwise
13411  *
13412  * SMART self-test log data is returned in buffer pointed to by selftest_log
13413  */
13414 static int
13415 sata_smart_selftest_log(
13416 	sata_hba_inst_t *sata_hba_inst,
13417 	sata_drive_info_t *sdinfo,
13418 	struct smart_selftest_log *selftest_log)
13419 {
13420 	sata_pkt_t *spkt;
13421 	sata_cmd_t *scmd;
13422 	sata_pkt_txlate_t *spx;
13423 	int rval;
13424 
13425 #if ! defined(lint)
13426 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13427 #endif
13428 
13429 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13430 	spx->txlt_sata_hba_inst = sata_hba_inst;
13431 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13432 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13433 	if (spkt == NULL) {
13434 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13435 		return (-1);
13436 	}
13437 	/* address is needed now */
13438 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13439 
13440 
13441 	/* Fill sata_pkt */
13442 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13443 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13444 	/* Synchronous mode, no callback */
13445 	spkt->satapkt_comp = NULL;
13446 	/* Timeout 30s */
13447 	spkt->satapkt_time = sata_default_pkt_time;
13448 
13449 	scmd = &spkt->satapkt_cmd;
13450 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13451 
13452 	/*
13453 	 * Allocate buffer for SMART SELFTEST LOG
13454 	 */
13455 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13456 	    sizeof (struct smart_selftest_log));
13457 	if (scmd->satacmd_bp == NULL) {
13458 		sata_pkt_free(spx);
13459 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13460 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13461 		    "sata_smart_selftest_log: "
13462 		    "cannot allocate buffer"));
13463 		return (-1);
13464 	}
13465 
13466 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13467 	scmd->satacmd_addr_type = 0;		/* N/A */
13468 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13469 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13470 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13471 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13472 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13473 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13474 	scmd->satacmd_cmd_reg = SATAC_SMART;
13475 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13476 	    sdinfo->satadrv_addr.cport)));
13477 
13478 	/* Send pkt to SATA HBA driver */
13479 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13480 	    SATA_TRAN_ACCEPTED ||
13481 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13482 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13483 		    sdinfo->satadrv_addr.cport)));
13484 		/*
13485 		 * Whoops, no SMART DATA available
13486 		 */
13487 		rval = -1;
13488 		goto fail;
13489 	} else {
13490 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13491 		    sdinfo->satadrv_addr.cport)));
13492 		if (spx->txlt_buf_dma_handle != NULL) {
13493 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13494 			    DDI_DMA_SYNC_FORKERNEL);
13495 			ASSERT(rval == DDI_SUCCESS);
13496 		}
13497 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13498 		    sizeof (struct smart_selftest_log));
13499 		rval = 0;
13500 	}
13501 
13502 fail:
13503 	/* Free allocated resources */
13504 	sata_free_local_buffer(spx);
13505 	sata_pkt_free(spx);
13506 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13507 
13508 	return (rval);
13509 }
13510 
13511 
13512 /*
13513  * Returns 0 for success, -1 otherwise
13514  *
13515  * SMART READ LOG data is returned in buffer pointed to by smart_log
13516  */
13517 static int
13518 sata_smart_read_log(
13519 	sata_hba_inst_t *sata_hba_inst,
13520 	sata_drive_info_t *sdinfo,
13521 	uint8_t *smart_log,		/* where the data should be returned */
13522 	uint8_t which_log,		/* which log should be returned */
13523 	uint8_t log_size)		/* # of 512 bytes in log */
13524 {
13525 	sata_pkt_t *spkt;
13526 	sata_cmd_t *scmd;
13527 	sata_pkt_txlate_t *spx;
13528 	int rval;
13529 
13530 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13531 	spx->txlt_sata_hba_inst = sata_hba_inst;
13532 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13533 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13534 	if (spkt == NULL) {
13535 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13536 		return (-1);
13537 	}
13538 	/* address is needed now */
13539 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13540 
13541 
13542 	/* Fill sata_pkt */
13543 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13544 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13545 	/* Synchronous mode, no callback */
13546 	spkt->satapkt_comp = NULL;
13547 	/* Timeout 30s */
13548 	spkt->satapkt_time = sata_default_pkt_time;
13549 
13550 	scmd = &spkt->satapkt_cmd;
13551 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13552 
13553 	/*
13554 	 * Allocate buffer for SMART READ LOG
13555 	 */
13556 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13557 	if (scmd->satacmd_bp == NULL) {
13558 		sata_pkt_free(spx);
13559 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13560 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13561 		    "sata_smart_read_log: " "cannot allocate buffer"));
13562 		return (-1);
13563 	}
13564 
13565 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13566 	scmd->satacmd_addr_type = 0;		/* N/A */
13567 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13568 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13569 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13570 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13571 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13572 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13573 	scmd->satacmd_cmd_reg = SATAC_SMART;
13574 
13575 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13576 	    sdinfo->satadrv_addr.cport)));
13577 
13578 	/* Send pkt to SATA HBA driver */
13579 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13580 	    SATA_TRAN_ACCEPTED ||
13581 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13582 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13583 		    sdinfo->satadrv_addr.cport)));
13584 
13585 		/*
13586 		 * Whoops, no SMART DATA available
13587 		 */
13588 		rval = -1;
13589 		goto fail;
13590 	} else {
13591 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13592 		    sdinfo->satadrv_addr.cport)));
13593 
13594 		if (spx->txlt_buf_dma_handle != NULL) {
13595 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13596 			    DDI_DMA_SYNC_FORKERNEL);
13597 			ASSERT(rval == DDI_SUCCESS);
13598 		}
13599 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13600 		rval = 0;
13601 	}
13602 
13603 fail:
13604 	/* Free allocated resources */
13605 	sata_free_local_buffer(spx);
13606 	sata_pkt_free(spx);
13607 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13608 
13609 	return (rval);
13610 }
13611 
13612 /*
13613  * Used by LOG SENSE page 0x10
13614  *
13615  * return 0 for success, -1 otherwise
13616  *
13617  */
13618 static int
13619 sata_read_log_ext_directory(
13620 	sata_hba_inst_t *sata_hba_inst,
13621 	sata_drive_info_t *sdinfo,
13622 	struct read_log_ext_directory *logdir)
13623 {
13624 	sata_pkt_txlate_t *spx;
13625 	sata_pkt_t *spkt;
13626 	sata_cmd_t *scmd;
13627 	int rval;
13628 
13629 #if ! defined(lint)
13630 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13631 #endif
13632 
13633 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13634 	spx->txlt_sata_hba_inst = sata_hba_inst;
13635 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13636 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13637 	if (spkt == NULL) {
13638 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13639 		return (-1);
13640 	}
13641 
13642 	/* Fill sata_pkt */
13643 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13644 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13645 	/* Synchronous mode, no callback */
13646 	spkt->satapkt_comp = NULL;
13647 	/* Timeout 30s */
13648 	spkt->satapkt_time = sata_default_pkt_time;
13649 
13650 	scmd = &spkt->satapkt_cmd;
13651 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13652 
13653 	/*
13654 	 * Allocate buffer for SMART READ LOG EXTENDED command
13655 	 */
13656 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13657 	    sizeof (struct read_log_ext_directory));
13658 	if (scmd->satacmd_bp == NULL) {
13659 		sata_pkt_free(spx);
13660 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13661 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13662 		    "sata_read_log_ext_directory: "
13663 		    "cannot allocate buffer"));
13664 		return (-1);
13665 	}
13666 
13667 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13668 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13669 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13670 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13671 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13672 	scmd->satacmd_lba_low_msb = 0;
13673 	scmd->satacmd_lba_mid_lsb = 0;
13674 	scmd->satacmd_lba_mid_msb = 0;
13675 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13676 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13677 
13678 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13679 	    sdinfo->satadrv_addr.cport)));
13680 
13681 	/* Send pkt to SATA HBA driver */
13682 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13683 	    SATA_TRAN_ACCEPTED ||
13684 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13685 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13686 		    sdinfo->satadrv_addr.cport)));
13687 		/*
13688 		 * Whoops, no SMART selftest log info available
13689 		 */
13690 		rval = -1;
13691 		goto fail;
13692 	} else {
13693 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13694 		    sdinfo->satadrv_addr.cport)));
13695 		if (spx->txlt_buf_dma_handle != NULL) {
13696 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13697 			    DDI_DMA_SYNC_FORKERNEL);
13698 			ASSERT(rval == DDI_SUCCESS);
13699 		}
13700 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13701 		    sizeof (struct read_log_ext_directory));
13702 		rval = 0;
13703 	}
13704 
13705 fail:
13706 	/* Free allocated resources */
13707 	sata_free_local_buffer(spx);
13708 	sata_pkt_free(spx);
13709 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13710 
13711 	return (rval);
13712 }
13713 
13714 /*
13715  * Set up error retrieval sata command for NCQ command error data
13716  * recovery.
13717  *
13718  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13719  * returns SATA_FAILURE otherwise.
13720  */
13721 static int
13722 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13723 {
13724 #ifndef __lock_lint
13725 	_NOTE(ARGUNUSED(sdinfo))
13726 #endif
13727 
13728 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13729 	sata_cmd_t *scmd;
13730 	struct buf *bp;
13731 
13732 	/* Operation modes are up to the caller */
13733 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13734 
13735 	/* Synchronous mode, no callback - may be changed by the caller */
13736 	spkt->satapkt_comp = NULL;
13737 	spkt->satapkt_time = sata_default_pkt_time;
13738 
13739 	scmd = &spkt->satapkt_cmd;
13740 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13741 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13742 
13743 	/*
13744 	 * Allocate dma_able buffer error data.
13745 	 * Buffer allocation will take care of buffer alignment and other DMA
13746 	 * attributes.
13747 	 */
13748 	bp = sata_alloc_local_buffer(spx,
13749 	    sizeof (struct sata_ncq_error_recovery_page));
13750 	if (bp == NULL)
13751 		return (SATA_FAILURE);
13752 
13753 	bp_mapin(bp); /* make data buffer accessible */
13754 	scmd->satacmd_bp = bp;
13755 
13756 	/*
13757 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13758 	 * before accessing it. Handle is in usual place in translate struct.
13759 	 */
13760 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13761 
13762 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13763 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13764 
13765 	return (SATA_SUCCESS);
13766 }
13767 
13768 /*
13769  * sata_xlate_errors() is used to translate (S)ATA error
13770  * information to SCSI information returned in the SCSI
13771  * packet.
13772  */
13773 static void
13774 sata_xlate_errors(sata_pkt_txlate_t *spx)
13775 {
13776 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13777 	struct scsi_extended_sense *sense;
13778 
13779 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13780 	*scsipkt->pkt_scbp = STATUS_CHECK;
13781 	sense = sata_arq_sense(spx);
13782 
13783 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13784 	case SATA_PKT_PORT_ERROR:
13785 		/*
13786 		 * We have no device data. Assume no data transfered.
13787 		 */
13788 		sense->es_key = KEY_HARDWARE_ERROR;
13789 		break;
13790 
13791 	case SATA_PKT_DEV_ERROR:
13792 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13793 		    SATA_STATUS_ERR) {
13794 			/*
13795 			 * determine dev error reason from error
13796 			 * reg content
13797 			 */
13798 			sata_decode_device_error(spx, sense);
13799 			break;
13800 		}
13801 		/* No extended sense key - no info available */
13802 		break;
13803 
13804 	case SATA_PKT_TIMEOUT:
13805 		scsipkt->pkt_reason = CMD_TIMEOUT;
13806 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13807 		/* No extended sense key */
13808 		break;
13809 
13810 	case SATA_PKT_ABORTED:
13811 		scsipkt->pkt_reason = CMD_ABORTED;
13812 		scsipkt->pkt_statistics |= STAT_ABORTED;
13813 		/* No extended sense key */
13814 		break;
13815 
13816 	case SATA_PKT_RESET:
13817 		/*
13818 		 * pkt aborted either by an explicit reset request from
13819 		 * a host, or due to error recovery
13820 		 */
13821 		scsipkt->pkt_reason = CMD_RESET;
13822 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13823 		break;
13824 
13825 	default:
13826 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13827 		break;
13828 	}
13829 }
13830 
13831 
13832 
13833 
13834 /*
13835  * Log sata message
13836  * dev pathname msg line preceeds the logged message.
13837  */
13838 
13839 static	void
13840 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13841 {
13842 	char pathname[128];
13843 	dev_info_t *dip = NULL;
13844 	va_list ap;
13845 
13846 	mutex_enter(&sata_log_mutex);
13847 
13848 	va_start(ap, fmt);
13849 	(void) vsprintf(sata_log_buf, fmt, ap);
13850 	va_end(ap);
13851 
13852 	if (sata_hba_inst != NULL) {
13853 		dip = SATA_DIP(sata_hba_inst);
13854 		(void) ddi_pathname(dip, pathname);
13855 	} else {
13856 		pathname[0] = 0;
13857 	}
13858 	if (level == CE_CONT) {
13859 		if (sata_debug_flags == 0)
13860 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13861 		else
13862 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13863 	} else {
13864 		if (level != CE_NOTE) {
13865 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13866 		} else if (sata_msg) {
13867 			cmn_err(level, "%s:\n %s", pathname,
13868 			    sata_log_buf);
13869 		}
13870 	}
13871 
13872 	/* sata trace debug */
13873 	sata_trace_debug(dip, sata_log_buf);
13874 
13875 	mutex_exit(&sata_log_mutex);
13876 }
13877 
13878 
13879 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13880 
13881 /*
13882  * Start or terminate the thread, depending on flag arg and current state
13883  */
13884 static void
13885 sata_event_thread_control(int startstop)
13886 {
13887 	static 	int sata_event_thread_terminating = 0;
13888 	static 	int sata_event_thread_starting = 0;
13889 	int i;
13890 
13891 	mutex_enter(&sata_event_mutex);
13892 
13893 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13894 	    sata_event_thread_terminating == 1)) {
13895 		mutex_exit(&sata_event_mutex);
13896 		return;
13897 	}
13898 	if (startstop == 1 && sata_event_thread_starting == 1) {
13899 		mutex_exit(&sata_event_mutex);
13900 		return;
13901 	}
13902 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13903 		sata_event_thread_starting = 1;
13904 		/* wait til terminate operation completes */
13905 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13906 		while (sata_event_thread_terminating == 1) {
13907 			if (i-- <= 0) {
13908 				sata_event_thread_starting = 0;
13909 				mutex_exit(&sata_event_mutex);
13910 #ifdef SATA_DEBUG
13911 				cmn_err(CE_WARN, "sata_event_thread_control: "
13912 				    "timeout waiting for thread to terminate");
13913 #endif
13914 				return;
13915 			}
13916 			mutex_exit(&sata_event_mutex);
13917 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13918 			mutex_enter(&sata_event_mutex);
13919 		}
13920 	}
13921 	if (startstop == 1) {
13922 		if (sata_event_thread == NULL) {
13923 			sata_event_thread = thread_create(NULL, 0,
13924 			    (void (*)())sata_event_daemon,
13925 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13926 		}
13927 		sata_event_thread_starting = 0;
13928 		mutex_exit(&sata_event_mutex);
13929 		return;
13930 	}
13931 
13932 	/*
13933 	 * If we got here, thread may need to be terminated
13934 	 */
13935 	if (sata_event_thread != NULL) {
13936 		int i;
13937 		/* Signal event thread to go away */
13938 		sata_event_thread_terminating = 1;
13939 		sata_event_thread_terminate = 1;
13940 		cv_signal(&sata_event_cv);
13941 		/*
13942 		 * Wait til daemon terminates.
13943 		 */
13944 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13945 		while (sata_event_thread_terminate == 1) {
13946 			mutex_exit(&sata_event_mutex);
13947 			if (i-- <= 0) {
13948 				/* Daemon did not go away !!! */
13949 #ifdef SATA_DEBUG
13950 				cmn_err(CE_WARN, "sata_event_thread_control: "
13951 				    "cannot terminate event daemon thread");
13952 #endif
13953 				mutex_enter(&sata_event_mutex);
13954 				break;
13955 			}
13956 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13957 			mutex_enter(&sata_event_mutex);
13958 		}
13959 		sata_event_thread_terminating = 0;
13960 	}
13961 	ASSERT(sata_event_thread_terminating == 0);
13962 	ASSERT(sata_event_thread_starting == 0);
13963 	mutex_exit(&sata_event_mutex);
13964 }
13965 
13966 
13967 /*
13968  * SATA HBA event notification function.
13969  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13970  * a port and/or device state or a controller itself.
13971  * Events for different addresses/addr types cannot be combined.
13972  * A warning message is generated for each event type.
13973  * Events are not processed by this function, so only the
13974  * event flag(s)is set for an affected entity and the event thread is
13975  * waken up. Event daemon thread processes all events.
13976  *
13977  * NOTE: Since more than one event may be reported at the same time, one
13978  * cannot determine a sequence of events when opposite event are reported, eg.
13979  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13980  * is taking precedence over reported events, i.e. may cause ignoring some
13981  * events.
13982  */
13983 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13984 
13985 void
13986 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13987 {
13988 	sata_hba_inst_t *sata_hba_inst = NULL;
13989 	sata_address_t *saddr;
13990 	sata_drive_info_t *sdinfo;
13991 	sata_port_stats_t *pstats;
13992 	sata_cport_info_t *cportinfo;
13993 	sata_pmport_info_t *pmportinfo;
13994 	int cport, pmport;
13995 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13996 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13997 	char *lcp;
13998 	static char *err_msg_evnt_1 =
13999 	    "sata_hba_event_notify: invalid port event 0x%x ";
14000 	static char *err_msg_evnt_2 =
14001 	    "sata_hba_event_notify: invalid device event 0x%x ";
14002 	int linkevent;
14003 
14004 	/*
14005 	 * There is a possibility that an event will be generated on HBA
14006 	 * that has not completed attachment or is detaching. We still want
14007 	 * to process events until HBA is detached.
14008 	 */
14009 	mutex_enter(&sata_mutex);
14010 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14011 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14012 		if (SATA_DIP(sata_hba_inst) == dip)
14013 			if (sata_hba_inst->satahba_attached == 1)
14014 				break;
14015 	}
14016 	mutex_exit(&sata_mutex);
14017 	if (sata_hba_inst == NULL)
14018 		/* HBA not attached */
14019 		return;
14020 
14021 	ASSERT(sata_device != NULL);
14022 
14023 	/*
14024 	 * Validate address before - do not proceed with invalid address.
14025 	 */
14026 	saddr = &sata_device->satadev_addr;
14027 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
14028 		return;
14029 	if (saddr->qual == SATA_ADDR_PMPORT ||
14030 	    saddr->qual == SATA_ADDR_DPMPORT)
14031 		/* Port Multiplier not supported yet */
14032 		return;
14033 
14034 	cport = saddr->cport;
14035 	pmport = saddr->pmport;
14036 
14037 	buf1[0] = buf2[0] = '\0';
14038 
14039 	/*
14040 	 * If event relates to port or device, check port state.
14041 	 * Port has to be initialized, or we cannot accept an event.
14042 	 */
14043 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
14044 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) {
14045 		if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) {
14046 			mutex_enter(&sata_hba_inst->satahba_mutex);
14047 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14048 			mutex_exit(&sata_hba_inst->satahba_mutex);
14049 			if (cportinfo == NULL || cportinfo->cport_state == 0)
14050 				return;
14051 		} else {
14052 			mutex_enter(&sata_hba_inst->satahba_mutex);
14053 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14054 			    cport, pmport);
14055 			mutex_exit(&sata_hba_inst->satahba_mutex);
14056 			if (pmportinfo == NULL || pmportinfo->pmport_state == 0)
14057 				return;
14058 		}
14059 	}
14060 
14061 	/*
14062 	 * Events refer to devices, ports and controllers - each has
14063 	 * unique address. Events for different addresses cannot be combined.
14064 	 */
14065 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
14066 
14067 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14068 
14069 		/* qualify this event(s) */
14070 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
14071 			/* Invalid event for the device port */
14072 			(void) sprintf(buf2, err_msg_evnt_1,
14073 			    event & SATA_EVNT_PORT_EVENTS);
14074 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14075 			goto event_info;
14076 		}
14077 		if (saddr->qual == SATA_ADDR_CPORT) {
14078 			/* Controller's device port event */
14079 
14080 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
14081 			    cport_event_flags |=
14082 			    event & SATA_EVNT_PORT_EVENTS;
14083 			pstats =
14084 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
14085 			    cport_stats;
14086 		} else {
14087 			/* Port multiplier's device port event */
14088 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14089 			    pmport_event_flags |=
14090 			    event & SATA_EVNT_PORT_EVENTS;
14091 			pstats =
14092 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14093 			    pmport_stats;
14094 		}
14095 
14096 		/*
14097 		 * Add to statistics and log the message. We have to do it
14098 		 * here rather than in the event daemon, because there may be
14099 		 * multiple events occuring before they are processed.
14100 		 */
14101 		linkevent = event &
14102 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
14103 		if (linkevent) {
14104 			if (linkevent == (SATA_EVNT_LINK_LOST |
14105 			    SATA_EVNT_LINK_ESTABLISHED)) {
14106 				/* This is likely event combination */
14107 				(void) strlcat(buf1, "link lost/established, ",
14108 				    SATA_EVENT_MAX_MSG_LENGTH);
14109 
14110 				if (pstats->link_lost < 0xffffffffffffffffULL)
14111 					pstats->link_lost++;
14112 				if (pstats->link_established <
14113 				    0xffffffffffffffffULL)
14114 					pstats->link_established++;
14115 				linkevent = 0;
14116 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
14117 				(void) strlcat(buf1, "link lost, ",
14118 				    SATA_EVENT_MAX_MSG_LENGTH);
14119 
14120 				if (pstats->link_lost < 0xffffffffffffffffULL)
14121 					pstats->link_lost++;
14122 			} else {
14123 				(void) strlcat(buf1, "link established, ",
14124 				    SATA_EVENT_MAX_MSG_LENGTH);
14125 				if (pstats->link_established <
14126 				    0xffffffffffffffffULL)
14127 					pstats->link_established++;
14128 			}
14129 		}
14130 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
14131 			(void) strlcat(buf1, "device attached, ",
14132 			    SATA_EVENT_MAX_MSG_LENGTH);
14133 			if (pstats->device_attached < 0xffffffffffffffffULL)
14134 				pstats->device_attached++;
14135 		}
14136 		if (event & SATA_EVNT_DEVICE_DETACHED) {
14137 			(void) strlcat(buf1, "device detached, ",
14138 			    SATA_EVENT_MAX_MSG_LENGTH);
14139 			if (pstats->device_detached < 0xffffffffffffffffULL)
14140 				pstats->device_detached++;
14141 		}
14142 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
14143 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14144 			    "port %d power level changed", cport);
14145 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
14146 				pstats->port_pwr_changed++;
14147 		}
14148 
14149 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
14150 			/* There should be no other events for this address */
14151 			(void) sprintf(buf2, err_msg_evnt_1,
14152 			    event & ~SATA_EVNT_PORT_EVENTS);
14153 		}
14154 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14155 
14156 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
14157 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14158 
14159 		/* qualify this event */
14160 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
14161 			/* Invalid event for a device */
14162 			(void) sprintf(buf2, err_msg_evnt_2,
14163 			    event & SATA_EVNT_DEVICE_RESET);
14164 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14165 			goto event_info;
14166 		}
14167 		/* drive event */
14168 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
14169 		if (sdinfo != NULL) {
14170 			if (event & SATA_EVNT_DEVICE_RESET) {
14171 				(void) strlcat(buf1, "device reset, ",
14172 				    SATA_EVENT_MAX_MSG_LENGTH);
14173 				if (sdinfo->satadrv_stats.drive_reset <
14174 				    0xffffffffffffffffULL)
14175 					sdinfo->satadrv_stats.drive_reset++;
14176 				sdinfo->satadrv_event_flags |=
14177 				    SATA_EVNT_DEVICE_RESET;
14178 			}
14179 		}
14180 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
14181 			/* Invalid event for a device */
14182 			(void) sprintf(buf2, err_msg_evnt_2,
14183 			    event & ~SATA_EVNT_DRIVE_EVENTS);
14184 		}
14185 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14186 	} else {
14187 		if (saddr->qual != SATA_ADDR_NULL) {
14188 			/* Wrong address qualifier */
14189 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14190 			    "sata_hba_event_notify: invalid address 0x%x",
14191 			    *(uint32_t *)saddr));
14192 			return;
14193 		}
14194 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
14195 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
14196 			/* Invalid event for the controller */
14197 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14198 			    "sata_hba_event_notify: invalid event 0x%x for "
14199 			    "controller",
14200 			    event & SATA_EVNT_CONTROLLER_EVENTS));
14201 			return;
14202 		}
14203 		buf1[0] = '\0';
14204 		/* This may be a frequent and not interesting event */
14205 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14206 		    "controller power level changed\n", NULL);
14207 
14208 		mutex_enter(&sata_hba_inst->satahba_mutex);
14209 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
14210 		    0xffffffffffffffffULL)
14211 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
14212 
14213 		sata_hba_inst->satahba_event_flags |=
14214 		    SATA_EVNT_PWR_LEVEL_CHANGED;
14215 		mutex_exit(&sata_hba_inst->satahba_mutex);
14216 	}
14217 	/*
14218 	 * If we got here, there is something to do with this HBA
14219 	 * instance.
14220 	 */
14221 	mutex_enter(&sata_hba_inst->satahba_mutex);
14222 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14223 	mutex_exit(&sata_hba_inst->satahba_mutex);
14224 	mutex_enter(&sata_mutex);
14225 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
14226 	mutex_exit(&sata_mutex);
14227 
14228 	/* Tickle event thread */
14229 	mutex_enter(&sata_event_mutex);
14230 	if (sata_event_thread_active == 0)
14231 		cv_signal(&sata_event_cv);
14232 	mutex_exit(&sata_event_mutex);
14233 
14234 event_info:
14235 	if (buf1[0] != '\0') {
14236 		lcp = strrchr(buf1, ',');
14237 		if (lcp != NULL)
14238 			*lcp = '\0';
14239 	}
14240 	if (saddr->qual == SATA_ADDR_CPORT ||
14241 	    saddr->qual == SATA_ADDR_DCPORT) {
14242 		if (buf1[0] != '\0') {
14243 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14244 			    cport, buf1);
14245 		}
14246 		if (buf2[0] != '\0') {
14247 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14248 			    cport, buf2);
14249 		}
14250 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
14251 	    saddr->qual == SATA_ADDR_DPMPORT) {
14252 		if (buf1[0] != '\0') {
14253 			sata_log(sata_hba_inst, CE_NOTE,
14254 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
14255 		}
14256 		if (buf2[0] != '\0') {
14257 			sata_log(sata_hba_inst, CE_NOTE,
14258 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
14259 		}
14260 	}
14261 }
14262 
14263 
14264 /*
14265  * Event processing thread.
14266  * Arg is a pointer to the sata_hba_list pointer.
14267  * It is not really needed, because sata_hba_list is global and static
14268  */
14269 static void
14270 sata_event_daemon(void *arg)
14271 {
14272 #ifndef __lock_lint
14273 	_NOTE(ARGUNUSED(arg))
14274 #endif
14275 	sata_hba_inst_t *sata_hba_inst;
14276 	clock_t lbolt;
14277 
14278 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14279 	    "SATA event daemon started\n", NULL);
14280 loop:
14281 	/*
14282 	 * Process events here. Walk through all registered HBAs
14283 	 */
14284 	mutex_enter(&sata_mutex);
14285 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14286 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14287 		ASSERT(sata_hba_inst != NULL);
14288 		mutex_enter(&sata_hba_inst->satahba_mutex);
14289 		if (sata_hba_inst->satahba_attached == 0 ||
14290 		    (sata_hba_inst->satahba_event_flags &
14291 		    SATA_EVNT_SKIP) != 0) {
14292 			mutex_exit(&sata_hba_inst->satahba_mutex);
14293 			continue;
14294 		}
14295 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
14296 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
14297 			mutex_exit(&sata_hba_inst->satahba_mutex);
14298 			mutex_exit(&sata_mutex);
14299 			/* Got the controller with pending event */
14300 			sata_process_controller_events(sata_hba_inst);
14301 			/*
14302 			 * Since global mutex was released, there is a
14303 			 * possibility that HBA list has changed, so start
14304 			 * over from the top. Just processed controller
14305 			 * will be passed-over because of the SKIP flag.
14306 			 */
14307 			goto loop;
14308 		}
14309 		mutex_exit(&sata_hba_inst->satahba_mutex);
14310 	}
14311 	/* Clear SKIP flag in all controllers */
14312 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14313 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14314 		mutex_enter(&sata_hba_inst->satahba_mutex);
14315 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
14316 		mutex_exit(&sata_hba_inst->satahba_mutex);
14317 	}
14318 	mutex_exit(&sata_mutex);
14319 
14320 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14321 	    "SATA EVENT DAEMON suspending itself", NULL);
14322 
14323 #ifdef SATA_DEBUG
14324 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
14325 		sata_log(sata_hba_inst, CE_WARN,
14326 		    "SATA EVENTS PROCESSING DISABLED\n");
14327 		thread_exit(); /* Daemon will not run again */
14328 	}
14329 #endif
14330 	mutex_enter(&sata_event_mutex);
14331 	sata_event_thread_active = 0;
14332 	mutex_exit(&sata_event_mutex);
14333 	/*
14334 	 * Go to sleep/suspend itself and wake up either because new event or
14335 	 * wait timeout. Exit if there is a termination request (driver
14336 	 * unload).
14337 	 */
14338 	do {
14339 		lbolt = ddi_get_lbolt();
14340 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
14341 		mutex_enter(&sata_event_mutex);
14342 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
14343 
14344 		if (sata_event_thread_active != 0) {
14345 			mutex_exit(&sata_event_mutex);
14346 			continue;
14347 		}
14348 
14349 		/* Check if it is time to go away */
14350 		if (sata_event_thread_terminate == 1) {
14351 			/*
14352 			 * It is up to the thread setting above flag to make
14353 			 * sure that this thread is not killed prematurely.
14354 			 */
14355 			sata_event_thread_terminate = 0;
14356 			sata_event_thread = NULL;
14357 			mutex_exit(&sata_event_mutex);
14358 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14359 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
14360 			thread_exit();  { _NOTE(NOT_REACHED) }
14361 		}
14362 		mutex_exit(&sata_event_mutex);
14363 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
14364 
14365 	mutex_enter(&sata_event_mutex);
14366 	sata_event_thread_active = 1;
14367 	mutex_exit(&sata_event_mutex);
14368 
14369 	mutex_enter(&sata_mutex);
14370 	sata_event_pending &= ~SATA_EVNT_MAIN;
14371 	mutex_exit(&sata_mutex);
14372 
14373 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14374 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14375 
14376 	goto loop;
14377 }
14378 
14379 /*
14380  * Specific HBA instance event processing.
14381  *
14382  * NOTE: At the moment, device event processing is limited to hard disks
14383  * only.
14384  * cports only are supported - no pmports.
14385  */
14386 static void
14387 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14388 {
14389 	int ncport;
14390 	uint32_t event_flags;
14391 	sata_address_t *saddr;
14392 	sata_cport_info_t *cportinfo;
14393 
14394 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14395 	    "Processing controller %d event(s)",
14396 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14397 
14398 	mutex_enter(&sata_hba_inst->satahba_mutex);
14399 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14400 	event_flags = sata_hba_inst->satahba_event_flags;
14401 	mutex_exit(&sata_hba_inst->satahba_mutex);
14402 	/*
14403 	 * Process controller power change first
14404 	 * HERE
14405 	 */
14406 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14407 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14408 
14409 	/*
14410 	 * Search through ports/devices to identify affected port/device.
14411 	 * We may have to process events for more than one port/device.
14412 	 */
14413 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14414 		/*
14415 		 * Not all ports may be processed in attach by the time we
14416 		 * get an event. Check if port info is initialized.
14417 		 */
14418 		mutex_enter(&sata_hba_inst->satahba_mutex);
14419 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
14420 		mutex_exit(&sata_hba_inst->satahba_mutex);
14421 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
14422 			continue;
14423 
14424 		/* We have initialized controller port info */
14425 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14426 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14427 		    cport_event_flags;
14428 		/* Check if port was locked by IOCTL processing */
14429 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14430 			/*
14431 			 * We ignore port events because port is busy
14432 			 * with AP control processing. Set again
14433 			 * controller and main event flag, so that
14434 			 * events may be processed by the next daemon
14435 			 * run.
14436 			 */
14437 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14438 			mutex_enter(&sata_hba_inst->satahba_mutex);
14439 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14440 			mutex_exit(&sata_hba_inst->satahba_mutex);
14441 			mutex_enter(&sata_mutex);
14442 			sata_event_pending |= SATA_EVNT_MAIN;
14443 			mutex_exit(&sata_mutex);
14444 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14445 			    "Event processing postponed until "
14446 			    "AP control processing completes",
14447 			    NULL);
14448 			/* Check other ports */
14449 			continue;
14450 		} else {
14451 			/*
14452 			 * Set BSY flag so that AP control would not
14453 			 * interfere with events processing for
14454 			 * this port.
14455 			 */
14456 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14457 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14458 		}
14459 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14460 
14461 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14462 
14463 		if ((event_flags &
14464 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14465 			/*
14466 			 * Got port event.
14467 			 * We need some hierarchy of event processing as they
14468 			 * are affecting each other:
14469 			 * 1. port failed
14470 			 * 2. device detached/attached
14471 			 * 3. link events - link events may trigger device
14472 			 *    detached or device attached events in some
14473 			 *    circumstances.
14474 			 * 4. port power level changed
14475 			 */
14476 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14477 				sata_process_port_failed_event(sata_hba_inst,
14478 				    saddr);
14479 			}
14480 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14481 				sata_process_device_detached(sata_hba_inst,
14482 				    saddr);
14483 			}
14484 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14485 				sata_process_device_attached(sata_hba_inst,
14486 				    saddr);
14487 			}
14488 			if (event_flags &
14489 			    (SATA_EVNT_LINK_ESTABLISHED |
14490 			    SATA_EVNT_LINK_LOST)) {
14491 				sata_process_port_link_events(sata_hba_inst,
14492 				    saddr);
14493 			}
14494 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14495 				sata_process_port_pwr_change(sata_hba_inst,
14496 				    saddr);
14497 			}
14498 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14499 				sata_process_target_node_cleanup(
14500 				    sata_hba_inst, saddr);
14501 			}
14502 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14503 				sata_process_device_autoonline(
14504 				    sata_hba_inst, saddr);
14505 			}
14506 		}
14507 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14508 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14509 		    SATA_DTYPE_NONE) &&
14510 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14511 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14512 			    satadrv_event_flags &
14513 			    (SATA_EVNT_DEVICE_RESET |
14514 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14515 				/* Have device event */
14516 				sata_process_device_reset(sata_hba_inst,
14517 				    saddr);
14518 			}
14519 		}
14520 		/* Release PORT_BUSY flag */
14521 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14522 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14523 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14524 
14525 	} /* End of loop through the controller SATA ports */
14526 }
14527 
14528 /*
14529  * Process HBA power level change reported by HBA driver.
14530  * Not implemented at this time - event is ignored.
14531  */
14532 static void
14533 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14534 {
14535 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14536 	    "Processing controller power level change", NULL);
14537 
14538 	/* Ignoring it for now */
14539 	mutex_enter(&sata_hba_inst->satahba_mutex);
14540 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14541 	mutex_exit(&sata_hba_inst->satahba_mutex);
14542 }
14543 
14544 /*
14545  * Process port power level change reported by HBA driver.
14546  * Not implemented at this time - event is ignored.
14547  */
14548 static void
14549 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14550     sata_address_t *saddr)
14551 {
14552 	sata_cport_info_t *cportinfo;
14553 
14554 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14555 	    "Processing port power level change", NULL);
14556 
14557 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14558 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14559 	/* Reset event flag */
14560 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14561 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14562 }
14563 
14564 /*
14565  * Process port failure reported by HBA driver.
14566  * cports support only - no pmports.
14567  */
14568 static void
14569 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14570     sata_address_t *saddr)
14571 {
14572 	sata_cport_info_t *cportinfo;
14573 
14574 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14575 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14576 	/* Reset event flag first */
14577 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14578 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14579 	if ((cportinfo->cport_state &
14580 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14581 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14582 		    cport_mutex);
14583 		return;
14584 	}
14585 	/* Fail the port */
14586 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14587 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14588 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14589 }
14590 
14591 /*
14592  * Device Reset Event processing.
14593  * The seqeunce is managed by 3 stage flags:
14594  * - reset event reported,
14595  * - reset event being processed,
14596  * - request to clear device reset state.
14597  *
14598  * NOTE: This function has to be entered with cport mutex held. It exits with
14599  * mutex held as well, but can release mutex during the processing.
14600  */
14601 static void
14602 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14603     sata_address_t *saddr)
14604 {
14605 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14606 	sata_drive_info_t *sdinfo;
14607 	sata_cport_info_t *cportinfo;
14608 	sata_device_t sata_device;
14609 	int rval_probe, rval_set;
14610 
14611 	/* We only care about host sata cport for now */
14612 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14613 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14614 	/*
14615 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14616 	 * state, ignore reset event.
14617 	 */
14618 	if (((cportinfo->cport_state &
14619 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14620 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14621 		sdinfo->satadrv_event_flags &=
14622 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14623 		return;
14624 	}
14625 
14626 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14627 	    SATA_VALID_DEV_TYPE) == 0) {
14628 		/*
14629 		 * This should not happen - coding error.
14630 		 * But we can recover, so do not panic, just clean up
14631 		 * and if in debug mode, log the message.
14632 		 */
14633 #ifdef SATA_DEBUG
14634 		sata_log(sata_hba_inst, CE_WARN,
14635 		    "sata_process_device_reset: "
14636 		    "Invalid device type with sdinfo!", NULL);
14637 #endif
14638 		sdinfo->satadrv_event_flags = 0;
14639 		return;
14640 	}
14641 
14642 #ifdef SATA_DEBUG
14643 	if ((sdinfo->satadrv_event_flags &
14644 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14645 		/* Nothing to do */
14646 		/* Something is weird - why we are processing dev reset? */
14647 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14648 		    "No device reset event!!!!", NULL);
14649 
14650 		return;
14651 	}
14652 	if ((sdinfo->satadrv_event_flags &
14653 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14654 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14655 		/* Something is weird - new device reset event */
14656 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14657 		    "Overlapping device reset events!", NULL);
14658 	}
14659 #endif
14660 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14661 	    "Processing port %d device reset", saddr->cport);
14662 
14663 	/* Clear event flag */
14664 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14665 
14666 	/* It seems that we always need to check the port state first */
14667 	sata_device.satadev_rev = SATA_DEVICE_REV;
14668 	sata_device.satadev_addr = *saddr;
14669 	/*
14670 	 * We have to exit mutex, because the HBA probe port function may
14671 	 * block on its own mutex.
14672 	 */
14673 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14674 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14675 	    (SATA_DIP(sata_hba_inst), &sata_device);
14676 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14677 	sata_update_port_info(sata_hba_inst, &sata_device);
14678 	if (rval_probe != SATA_SUCCESS) {
14679 		/* Something went wrong? Fail the port */
14680 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14681 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14682 		if (sdinfo != NULL)
14683 			sdinfo->satadrv_event_flags = 0;
14684 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14685 		    cport_mutex);
14686 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14687 		    "SATA port %d probing failed",
14688 		    saddr->cport));
14689 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14690 		    saddr->cport)->cport_mutex);
14691 		return;
14692 	}
14693 	if ((sata_device.satadev_scr.sstatus  &
14694 	    SATA_PORT_DEVLINK_UP_MASK) !=
14695 	    SATA_PORT_DEVLINK_UP ||
14696 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14697 		/*
14698 		 * No device to process, anymore. Some other event processing
14699 		 * would or have already performed port info cleanup.
14700 		 * To be safe (HBA may need it), request clearing device
14701 		 * reset condition.
14702 		 */
14703 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14704 		if (sdinfo != NULL) {
14705 			sdinfo->satadrv_event_flags &=
14706 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14707 			sdinfo->satadrv_event_flags |=
14708 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14709 		}
14710 		return;
14711 	}
14712 
14713 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14714 	if (sdinfo == NULL) {
14715 		return;
14716 	}
14717 	if ((sdinfo->satadrv_event_flags &
14718 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14719 		/*
14720 		 * Start tracking time for device feature restoration and
14721 		 * identification. Save current time (lbolt value).
14722 		 */
14723 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14724 	}
14725 	/* Mark device reset processing as active */
14726 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14727 
14728 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14729 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14730 
14731 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
14732 
14733 	if (rval_set  != SATA_SUCCESS) {
14734 		/*
14735 		 * Restoring drive setting failed.
14736 		 * Probe the port first, to check if the port state has changed
14737 		 */
14738 		sata_device.satadev_rev = SATA_DEVICE_REV;
14739 		sata_device.satadev_addr = *saddr;
14740 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14741 		/* probe port */
14742 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14743 		    (SATA_DIP(sata_hba_inst), &sata_device);
14744 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14745 		    cport_mutex);
14746 		if (rval_probe == SATA_SUCCESS &&
14747 		    (sata_device.satadev_state &
14748 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14749 		    (sata_device.satadev_scr.sstatus  &
14750 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14751 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14752 			/*
14753 			 * We may retry this a bit later - in-process reset
14754 			 * condition should be already set.
14755 			 * Track retry time for device identification.
14756 			 */
14757 			if ((cportinfo->cport_dev_type &
14758 			    SATA_VALID_DEV_TYPE) != 0 &&
14759 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14760 			    sdinfo->satadrv_reset_time != 0) {
14761 				clock_t cur_time = ddi_get_lbolt();
14762 				/*
14763 				 * If the retry time limit was not
14764 				 * exceeded, retry.
14765 				 */
14766 				if ((cur_time - sdinfo->satadrv_reset_time) <
14767 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14768 					mutex_enter(
14769 					    &sata_hba_inst->satahba_mutex);
14770 					sata_hba_inst->satahba_event_flags |=
14771 					    SATA_EVNT_MAIN;
14772 					mutex_exit(
14773 					    &sata_hba_inst->satahba_mutex);
14774 					mutex_enter(&sata_mutex);
14775 					sata_event_pending |= SATA_EVNT_MAIN;
14776 					mutex_exit(&sata_mutex);
14777 					return;
14778 				}
14779 				if (rval_set == SATA_RETRY) {
14780 					/*
14781 					 * Setting drive features failed, but
14782 					 * the drive is still accessible,
14783 					 * so emit a warning message before
14784 					 * return.
14785 					 */
14786 					mutex_exit(&SATA_CPORT_INFO(
14787 					    sata_hba_inst,
14788 					    saddr->cport)->cport_mutex);
14789 					goto done;
14790 				}
14791 			}
14792 			/* Fail the drive */
14793 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14794 
14795 			sata_log(sata_hba_inst, CE_WARN,
14796 			    "SATA device at port %d - device failed",
14797 			    saddr->cport);
14798 		}
14799 		/*
14800 		 * No point of retrying - device failed or some other event
14801 		 * processing or already did or will do port info cleanup.
14802 		 * To be safe (HBA may need it),
14803 		 * request clearing device reset condition.
14804 		 */
14805 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14806 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14807 		sdinfo->satadrv_reset_time = 0;
14808 		return;
14809 	}
14810 done:
14811 	/*
14812 	 * If setting of drive features failed, but the drive is still
14813 	 * accessible, emit a warning message.
14814 	 */
14815 	if (rval_set == SATA_RETRY) {
14816 		sata_log(sata_hba_inst, CE_WARN,
14817 		    "SATA device at port %d - desired setting could not be "
14818 		    "restored after reset. Device may not operate as expected.",
14819 		    saddr->cport);
14820 	}
14821 	/*
14822 	 * Raise the flag indicating that the next sata command could
14823 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14824 	 * reset is reported.
14825 	 */
14826 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14827 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14828 		sdinfo->satadrv_reset_time = 0;
14829 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14830 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14831 			sdinfo->satadrv_event_flags &=
14832 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14833 			sdinfo->satadrv_event_flags |=
14834 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14835 		}
14836 	}
14837 }
14838 
14839 
14840 /*
14841  * Port Link Events processing.
14842  * Every link established event may involve device reset (due to
14843  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14844  * set device reset event for an attached device (if any).
14845  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14846  *
14847  * The link established event processing varies, depending on the state
14848  * of the target node, HBA hotplugging capabilities, state of the port.
14849  * If the link is not active, the link established event is ignored.
14850  * If HBA cannot detect device attachment and there is no target node,
14851  * the link established event triggers device attach event processing.
14852  * Else, link established event triggers device reset event processing.
14853  *
14854  * The link lost event processing varies, depending on a HBA hotplugging
14855  * capability and the state of the port (link active or not active).
14856  * If the link is active, the lost link event is ignored.
14857  * If HBA cannot detect device removal, the lost link event triggers
14858  * device detached event processing after link lost timeout.
14859  * Else, the event is ignored.
14860  *
14861  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14862  */
14863 static void
14864 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14865     sata_address_t *saddr)
14866 {
14867 	sata_device_t sata_device;
14868 	sata_cport_info_t *cportinfo;
14869 	sata_drive_info_t *sdinfo;
14870 	uint32_t event_flags;
14871 	int rval;
14872 
14873 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14874 	    "Processing port %d link event(s)", saddr->cport);
14875 
14876 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14877 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14878 	event_flags = cportinfo->cport_event_flags;
14879 
14880 	/* Reset event flags first */
14881 	cportinfo->cport_event_flags &=
14882 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14883 
14884 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14885 	if ((cportinfo->cport_state &
14886 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14887 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14888 		    cport_mutex);
14889 		return;
14890 	}
14891 
14892 	/*
14893 	 * For the sanity sake get current port state.
14894 	 * Set device address only. Other sata_device fields should be
14895 	 * set by HBA driver.
14896 	 */
14897 	sata_device.satadev_rev = SATA_DEVICE_REV;
14898 	sata_device.satadev_addr = *saddr;
14899 	/*
14900 	 * We have to exit mutex, because the HBA probe port function may
14901 	 * block on its own mutex.
14902 	 */
14903 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14904 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14905 	    (SATA_DIP(sata_hba_inst), &sata_device);
14906 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14907 	sata_update_port_info(sata_hba_inst, &sata_device);
14908 	if (rval != SATA_SUCCESS) {
14909 		/* Something went wrong? Fail the port */
14910 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14911 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14912 		    cport_mutex);
14913 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14914 		    "SATA port %d probing failed",
14915 		    saddr->cport));
14916 		/*
14917 		 * We may want to release device info structure, but
14918 		 * it is not necessary.
14919 		 */
14920 		return;
14921 	} else {
14922 		/* port probed successfully */
14923 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14924 	}
14925 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14926 
14927 		if ((sata_device.satadev_scr.sstatus &
14928 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14929 			/* Ignore event */
14930 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14931 			    "Ignoring port %d link established event - "
14932 			    "link down",
14933 			    saddr->cport);
14934 			goto linklost;
14935 		}
14936 
14937 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14938 		    "Processing port %d link established event",
14939 		    saddr->cport);
14940 
14941 		/*
14942 		 * For the sanity sake check if a device is attached - check
14943 		 * return state of a port probing.
14944 		 */
14945 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14946 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14947 			/*
14948 			 * HBA port probe indicated that there is a device
14949 			 * attached. Check if the framework had device info
14950 			 * structure attached for this device.
14951 			 */
14952 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14953 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14954 				    NULL);
14955 
14956 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14957 				if ((sdinfo->satadrv_type &
14958 				    SATA_VALID_DEV_TYPE) != 0) {
14959 					/*
14960 					 * Dev info structure is present.
14961 					 * If dev_type is set to known type in
14962 					 * the framework's drive info struct
14963 					 * then the device existed before and
14964 					 * the link was probably lost
14965 					 * momentarily - in such case
14966 					 * we may want to check device
14967 					 * identity.
14968 					 * Identity check is not supported now.
14969 					 *
14970 					 * Link established event
14971 					 * triggers device reset event.
14972 					 */
14973 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14974 					    satadrv_event_flags |=
14975 					    SATA_EVNT_DEVICE_RESET;
14976 				}
14977 			} else if (cportinfo->cport_dev_type ==
14978 			    SATA_DTYPE_NONE) {
14979 				/*
14980 				 * We got new device attached! If HBA does not
14981 				 * generate device attached events, trigger it
14982 				 * here.
14983 				 */
14984 				if (!(SATA_FEATURES(sata_hba_inst) &
14985 				    SATA_CTLF_HOTPLUG)) {
14986 					cportinfo->cport_event_flags |=
14987 					    SATA_EVNT_DEVICE_ATTACHED;
14988 				}
14989 			}
14990 			/* Reset link lost timeout */
14991 			cportinfo->cport_link_lost_time = 0;
14992 		}
14993 	}
14994 linklost:
14995 	if (event_flags & SATA_EVNT_LINK_LOST) {
14996 		if ((sata_device.satadev_scr.sstatus &
14997 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14998 			/* Ignore event */
14999 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15000 			    "Ignoring port %d link lost event - link is up",
15001 			    saddr->cport);
15002 			goto done;
15003 		}
15004 #ifdef SATA_DEBUG
15005 		if (cportinfo->cport_link_lost_time == 0) {
15006 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15007 			    "Processing port %d link lost event",
15008 			    saddr->cport);
15009 		}
15010 #endif
15011 		/*
15012 		 * When HBA cannot generate device attached/detached events,
15013 		 * we need to track link lost time and eventually generate
15014 		 * device detach event.
15015 		 */
15016 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
15017 			/* We are tracking link lost time */
15018 			if (cportinfo->cport_link_lost_time == 0) {
15019 				/* save current time (lbolt value) */
15020 				cportinfo->cport_link_lost_time =
15021 				    ddi_get_lbolt();
15022 				/* just keep link lost event */
15023 				cportinfo->cport_event_flags |=
15024 				    SATA_EVNT_LINK_LOST;
15025 			} else {
15026 				clock_t cur_time = ddi_get_lbolt();
15027 				if ((cur_time -
15028 				    cportinfo->cport_link_lost_time) >=
15029 				    drv_usectohz(
15030 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
15031 					/* trigger device detach event */
15032 					cportinfo->cport_event_flags |=
15033 					    SATA_EVNT_DEVICE_DETACHED;
15034 					cportinfo->cport_link_lost_time = 0;
15035 					SATADBG1(SATA_DBG_EVENTS,
15036 					    sata_hba_inst,
15037 					    "Triggering port %d "
15038 					    "device detached event",
15039 					    saddr->cport);
15040 				} else {
15041 					/* keep link lost event */
15042 					cportinfo->cport_event_flags |=
15043 					    SATA_EVNT_LINK_LOST;
15044 				}
15045 			}
15046 		}
15047 		/*
15048 		 * We could change port state to disable/delay access to
15049 		 * the attached device until the link is recovered.
15050 		 */
15051 	}
15052 done:
15053 	event_flags = cportinfo->cport_event_flags;
15054 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15055 	if (event_flags != 0) {
15056 		mutex_enter(&sata_hba_inst->satahba_mutex);
15057 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15058 		mutex_exit(&sata_hba_inst->satahba_mutex);
15059 		mutex_enter(&sata_mutex);
15060 		sata_event_pending |= SATA_EVNT_MAIN;
15061 		mutex_exit(&sata_mutex);
15062 	}
15063 }
15064 
15065 /*
15066  * Device Detached Event processing.
15067  * Port is probed to find if a device is really gone. If so,
15068  * the device info structure is detached from the SATA port info structure
15069  * and released.
15070  * Port status is updated.
15071  *
15072  * NOTE: Process cports event only, no port multiplier ports.
15073  */
15074 static void
15075 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
15076     sata_address_t *saddr)
15077 {
15078 	sata_cport_info_t *cportinfo;
15079 	sata_drive_info_t *sdevinfo;
15080 	sata_device_t sata_device;
15081 	dev_info_t *tdip;
15082 	int rval;
15083 
15084 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15085 	    "Processing port %d device detached", saddr->cport);
15086 
15087 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15088 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15089 	/* Clear event flag */
15090 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
15091 
15092 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
15093 	if ((cportinfo->cport_state &
15094 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15095 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15096 		    cport_mutex);
15097 		return;
15098 	}
15099 	/* For sanity, re-probe the port */
15100 	sata_device.satadev_rev = SATA_DEVICE_REV;
15101 	sata_device.satadev_addr = *saddr;
15102 
15103 	/*
15104 	 * We have to exit mutex, because the HBA probe port function may
15105 	 * block on its own mutex.
15106 	 */
15107 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15108 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15109 	    (SATA_DIP(sata_hba_inst), &sata_device);
15110 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15111 	sata_update_port_info(sata_hba_inst, &sata_device);
15112 	if (rval != SATA_SUCCESS) {
15113 		/* Something went wrong? Fail the port */
15114 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15115 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15116 		    cport_mutex);
15117 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15118 		    "SATA port %d probing failed",
15119 		    saddr->cport));
15120 		/*
15121 		 * We may want to release device info structure, but
15122 		 * it is not necessary.
15123 		 */
15124 		return;
15125 	} else {
15126 		/* port probed successfully */
15127 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15128 	}
15129 	/*
15130 	 * Check if a device is still attached. For sanity, check also
15131 	 * link status - if no link, there is no device.
15132 	 */
15133 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
15134 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
15135 	    SATA_DTYPE_NONE) {
15136 		/*
15137 		 * Device is still attached - ignore detach event.
15138 		 */
15139 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15140 		    cport_mutex);
15141 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15142 		    "Ignoring detach - device still attached to port %d",
15143 		    sata_device.satadev_addr.cport);
15144 		return;
15145 	}
15146 	/*
15147 	 * We need to detach and release device info structure here
15148 	 */
15149 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15150 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15151 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15152 		(void) kmem_free((void *)sdevinfo,
15153 		    sizeof (sata_drive_info_t));
15154 	}
15155 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15156 	/*
15157 	 * Device cannot be reached anymore, even if the target node may be
15158 	 * still present.
15159 	 */
15160 
15161 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15162 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
15163 	    sata_device.satadev_addr.cport);
15164 
15165 	/*
15166 	 * Try to offline a device and remove target node if it still exists
15167 	 */
15168 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15169 	if (tdip != NULL) {
15170 		/*
15171 		 * Target node exists.  Unconfigure device then remove
15172 		 * the target node (one ndi operation).
15173 		 */
15174 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
15175 			/*
15176 			 * PROBLEM - no device, but target node remained
15177 			 * This happens when the file was open or node was
15178 			 * waiting for resources.
15179 			 */
15180 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15181 			    "sata_process_device_detached: "
15182 			    "Failed to remove target node for "
15183 			    "detached SATA device."));
15184 			/*
15185 			 * Set target node state to DEVI_DEVICE_REMOVED.
15186 			 * But re-check first that the node still exists.
15187 			 */
15188 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15189 			    saddr->cport);
15190 			if (tdip != NULL) {
15191 				sata_set_device_removed(tdip);
15192 				/*
15193 				 * Instruct event daemon to retry the
15194 				 * cleanup later.
15195 				 */
15196 				sata_set_target_node_cleanup(sata_hba_inst,
15197 				    &sata_device.satadev_addr);
15198 			}
15199 		}
15200 	}
15201 	/*
15202 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15203 	 * with the hint: SE_HINT_REMOVE
15204 	 */
15205 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
15206 }
15207 
15208 
15209 /*
15210  * Device Attached Event processing.
15211  * Port state is checked to verify that a device is really attached. If so,
15212  * the device info structure is created and attached to the SATA port info
15213  * structure.
15214  *
15215  * If attached device cannot be identified or set-up, the retry for the
15216  * attach processing is set-up. Subsequent daemon run would try again to
15217  * identify the device, until the time limit is reached
15218  * (SATA_DEV_IDENTIFY_TIMEOUT).
15219  *
15220  * This function cannot be called in interrupt context (it may sleep).
15221  *
15222  * NOTE: Process cports event only, no port multiplier ports.
15223  */
15224 static void
15225 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
15226     sata_address_t *saddr)
15227 {
15228 	sata_cport_info_t *cportinfo;
15229 	sata_drive_info_t *sdevinfo;
15230 	sata_device_t sata_device;
15231 	dev_info_t *tdip;
15232 	uint32_t event_flags;
15233 	int rval;
15234 
15235 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15236 	    "Processing port %d device attached", saddr->cport);
15237 
15238 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15239 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15240 
15241 	/* Clear attach event flag first */
15242 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
15243 
15244 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
15245 	if ((cportinfo->cport_state &
15246 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15247 		cportinfo->cport_dev_attach_time = 0;
15248 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15249 		    cport_mutex);
15250 		return;
15251 	}
15252 
15253 	/*
15254 	 * If the sata_drive_info structure is found attached to the port info,
15255 	 * despite the fact the device was removed and now it is re-attached,
15256 	 * the old drive info structure was not removed.
15257 	 * Arbitrarily release device info structure.
15258 	 */
15259 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15260 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15261 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15262 		(void) kmem_free((void *)sdevinfo,
15263 		    sizeof (sata_drive_info_t));
15264 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15265 		    "Arbitrarily detaching old device info.", NULL);
15266 	}
15267 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15268 
15269 	/* For sanity, re-probe the port */
15270 	sata_device.satadev_rev = SATA_DEVICE_REV;
15271 	sata_device.satadev_addr = *saddr;
15272 
15273 	/*
15274 	 * We have to exit mutex, because the HBA probe port function may
15275 	 * block on its own mutex.
15276 	 */
15277 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15278 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15279 	    (SATA_DIP(sata_hba_inst), &sata_device);
15280 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15281 	sata_update_port_info(sata_hba_inst, &sata_device);
15282 	if (rval != SATA_SUCCESS) {
15283 		/* Something went wrong? Fail the port */
15284 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15285 		cportinfo->cport_dev_attach_time = 0;
15286 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15287 		    cport_mutex);
15288 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15289 		    "SATA port %d probing failed",
15290 		    saddr->cport));
15291 		return;
15292 	} else {
15293 		/* port probed successfully */
15294 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15295 	}
15296 	/*
15297 	 * Check if a device is still attached. For sanity, check also
15298 	 * link status - if no link, there is no device.
15299 	 */
15300 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
15301 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
15302 	    SATA_DTYPE_NONE) {
15303 		/*
15304 		 * No device - ignore attach event.
15305 		 */
15306 		cportinfo->cport_dev_attach_time = 0;
15307 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15308 		    cport_mutex);
15309 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15310 		    "Ignoring attach - no device connected to port %d",
15311 		    sata_device.satadev_addr.cport);
15312 		return;
15313 	}
15314 
15315 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15316 	/*
15317 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15318 	 * with the hint: SE_HINT_INSERT
15319 	 */
15320 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
15321 
15322 	/*
15323 	 * Port reprobing will take care of the creation of the device
15324 	 * info structure and determination of the device type.
15325 	 */
15326 	sata_device.satadev_addr = *saddr;
15327 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
15328 	    SATA_DEV_IDENTIFY_NORETRY);
15329 
15330 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15331 	    cport_mutex);
15332 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
15333 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
15334 		/* Some device is attached to the port */
15335 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
15336 			/*
15337 			 * A device was not successfully attached.
15338 			 * Track retry time for device identification.
15339 			 */
15340 			if (cportinfo->cport_dev_attach_time != 0) {
15341 				clock_t cur_time = ddi_get_lbolt();
15342 				/*
15343 				 * If the retry time limit was not exceeded,
15344 				 * reinstate attach event.
15345 				 */
15346 				if ((cur_time -
15347 				    cportinfo->cport_dev_attach_time) <
15348 				    drv_usectohz(
15349 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
15350 					/* OK, restore attach event */
15351 					cportinfo->cport_event_flags |=
15352 					    SATA_EVNT_DEVICE_ATTACHED;
15353 				} else {
15354 					/* Timeout - cannot identify device */
15355 					cportinfo->cport_dev_attach_time = 0;
15356 					sata_log(sata_hba_inst,
15357 					    CE_WARN,
15358 					    "Could not identify SATA device "
15359 					    "at port %d",
15360 					    saddr->cport);
15361 				}
15362 			} else {
15363 				/*
15364 				 * Start tracking time for device
15365 				 * identification.
15366 				 * Save current time (lbolt value).
15367 				 */
15368 				cportinfo->cport_dev_attach_time =
15369 				    ddi_get_lbolt();
15370 				/* Restore attach event */
15371 				cportinfo->cport_event_flags |=
15372 				    SATA_EVNT_DEVICE_ATTACHED;
15373 			}
15374 		} else {
15375 			/*
15376 			 * If device was successfully attached, the subsequent
15377 			 * action depends on a state of the
15378 			 * sata_auto_online variable. If it is set to zero.
15379 			 * an explicit 'configure' command will be needed to
15380 			 * configure it. If its value is non-zero, we will
15381 			 * attempt to online (configure) the device.
15382 			 * First, log the message indicating that a device
15383 			 * was attached.
15384 			 */
15385 			cportinfo->cport_dev_attach_time = 0;
15386 			sata_log(sata_hba_inst, CE_WARN,
15387 			    "SATA device detected at port %d", saddr->cport);
15388 
15389 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15390 				sata_drive_info_t new_sdinfo;
15391 
15392 				/* Log device info data */
15393 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
15394 				    cportinfo));
15395 				sata_show_drive_info(sata_hba_inst,
15396 				    &new_sdinfo);
15397 			}
15398 
15399 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15400 			    saddr->cport)->cport_mutex);
15401 
15402 			/*
15403 			 * Make sure that there is no target node for that
15404 			 * device. If so, release it. It should not happen,
15405 			 * unless we had problem removing the node when
15406 			 * device was detached.
15407 			 */
15408 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15409 			    saddr->cport);
15410 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15411 			    saddr->cport)->cport_mutex);
15412 			if (tdip != NULL) {
15413 
15414 #ifdef SATA_DEBUG
15415 				if ((cportinfo->cport_event_flags &
15416 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15417 					sata_log(sata_hba_inst, CE_WARN,
15418 					    "sata_process_device_attached: "
15419 					    "old device target node exists!");
15420 #endif
15421 				/*
15422 				 * target node exists - try to unconfigure
15423 				 * device and remove the node.
15424 				 */
15425 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15426 				    saddr->cport)->cport_mutex);
15427 				rval = ndi_devi_offline(tdip,
15428 				    NDI_DEVI_REMOVE);
15429 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15430 				    saddr->cport)->cport_mutex);
15431 
15432 				if (rval == NDI_SUCCESS) {
15433 					cportinfo->cport_event_flags &=
15434 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15435 					cportinfo->cport_tgtnode_clean = B_TRUE;
15436 				} else {
15437 					/*
15438 					 * PROBLEM - the target node remained
15439 					 * and it belongs to a previously
15440 					 * attached device.
15441 					 * This happens when the file was open
15442 					 * or the node was waiting for
15443 					 * resources at the time the
15444 					 * associated device was removed.
15445 					 * Instruct event daemon to retry the
15446 					 * cleanup later.
15447 					 */
15448 					sata_log(sata_hba_inst,
15449 					    CE_WARN,
15450 					    "Application(s) accessing "
15451 					    "previously attached SATA "
15452 					    "device have to release "
15453 					    "it before newly inserted "
15454 					    "device can be made accessible.",
15455 					    saddr->cport);
15456 					cportinfo->cport_event_flags |=
15457 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15458 					cportinfo->cport_tgtnode_clean =
15459 					    B_FALSE;
15460 				}
15461 			}
15462 			if (sata_auto_online != 0) {
15463 				cportinfo->cport_event_flags |=
15464 				    SATA_EVNT_AUTOONLINE_DEVICE;
15465 			}
15466 
15467 		}
15468 	} else {
15469 		cportinfo->cport_dev_attach_time = 0;
15470 	}
15471 
15472 	event_flags = cportinfo->cport_event_flags;
15473 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15474 	if (event_flags != 0) {
15475 		mutex_enter(&sata_hba_inst->satahba_mutex);
15476 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15477 		mutex_exit(&sata_hba_inst->satahba_mutex);
15478 		mutex_enter(&sata_mutex);
15479 		sata_event_pending |= SATA_EVNT_MAIN;
15480 		mutex_exit(&sata_mutex);
15481 	}
15482 }
15483 
15484 
15485 /*
15486  * Device Target Node Cleanup Event processing.
15487  * If the target node associated with a sata port device is in
15488  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15489  * If the target node cannot be removed, the event flag is left intact,
15490  * so that event daemon may re-run this function later.
15491  *
15492  * This function cannot be called in interrupt context (it may sleep).
15493  *
15494  * NOTE: Processes cport events only, not port multiplier ports.
15495  */
15496 static void
15497 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15498     sata_address_t *saddr)
15499 {
15500 	sata_cport_info_t *cportinfo;
15501 	dev_info_t *tdip;
15502 
15503 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15504 	    "Processing port %d device target node cleanup", saddr->cport);
15505 
15506 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15507 
15508 	/*
15509 	 * Check if there is target node for that device and it is in the
15510 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15511 	 */
15512 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15513 	if (tdip != NULL) {
15514 		/*
15515 		 * target node exists - check if it is target node of
15516 		 * a removed device.
15517 		 */
15518 		if (sata_check_device_removed(tdip) == B_TRUE) {
15519 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15520 			    "sata_process_target_node_cleanup: "
15521 			    "old device target node exists!", NULL);
15522 			/*
15523 			 * Unconfigure and remove the target node
15524 			 */
15525 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15526 			    NDI_SUCCESS) {
15527 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15528 				    saddr->cport)->cport_mutex);
15529 				cportinfo->cport_event_flags &=
15530 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15531 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15532 				    saddr->cport)->cport_mutex);
15533 				return;
15534 			}
15535 			/*
15536 			 * Event daemon will retry the cleanup later.
15537 			 */
15538 			mutex_enter(&sata_hba_inst->satahba_mutex);
15539 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15540 			mutex_exit(&sata_hba_inst->satahba_mutex);
15541 			mutex_enter(&sata_mutex);
15542 			sata_event_pending |= SATA_EVNT_MAIN;
15543 			mutex_exit(&sata_mutex);
15544 		}
15545 	} else {
15546 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15547 		    saddr->cport)->cport_mutex);
15548 		cportinfo->cport_event_flags &=
15549 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15550 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15551 		    saddr->cport)->cport_mutex);
15552 	}
15553 }
15554 
15555 /*
15556  * Device AutoOnline Event processing.
15557  * If attached device is to be onlined, an attempt is made to online this
15558  * device, but only if there is no lingering (old) target node present.
15559  * If the device cannot be onlined, the event flag is left intact,
15560  * so that event daemon may re-run this function later.
15561  *
15562  * This function cannot be called in interrupt context (it may sleep).
15563  *
15564  * NOTE: Processes cport events only, not port multiplier ports.
15565  */
15566 static void
15567 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15568     sata_address_t *saddr)
15569 {
15570 	sata_cport_info_t *cportinfo;
15571 	sata_drive_info_t *sdinfo;
15572 	sata_device_t sata_device;
15573 	dev_info_t *tdip;
15574 
15575 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15576 	    "Processing port %d attached device auto-onlining", saddr->cport);
15577 
15578 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15579 
15580 	/*
15581 	 * Check if device is present and recognized. If not, reset event.
15582 	 */
15583 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15584 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15585 		/* Nothing to online */
15586 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15587 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15588 		    saddr->cport)->cport_mutex);
15589 		return;
15590 	}
15591 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15592 
15593 	/*
15594 	 * Check if there is target node for this device and if it is in the
15595 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15596 	 * the event for later processing.
15597 	 */
15598 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15599 	if (tdip != NULL) {
15600 		/*
15601 		 * target node exists - check if it is target node of
15602 		 * a removed device.
15603 		 */
15604 		if (sata_check_device_removed(tdip) == B_TRUE) {
15605 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15606 			    "sata_process_device_autoonline: "
15607 			    "old device target node exists!", NULL);
15608 			/*
15609 			 * Event daemon will retry device onlining later.
15610 			 */
15611 			mutex_enter(&sata_hba_inst->satahba_mutex);
15612 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15613 			mutex_exit(&sata_hba_inst->satahba_mutex);
15614 			mutex_enter(&sata_mutex);
15615 			sata_event_pending |= SATA_EVNT_MAIN;
15616 			mutex_exit(&sata_mutex);
15617 			return;
15618 		}
15619 		/*
15620 		 * If the target node is not in the 'removed" state, assume
15621 		 * that it belongs to this device. There is nothing more to do,
15622 		 * but reset the event.
15623 		 */
15624 	} else {
15625 
15626 		/*
15627 		 * Try to online the device
15628 		 * If there is any reset-related event, remove it. We are
15629 		 * configuring the device and no state restoring is needed.
15630 		 */
15631 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15632 		    saddr->cport)->cport_mutex);
15633 		sata_device.satadev_addr = *saddr;
15634 		if (saddr->qual == SATA_ADDR_CPORT)
15635 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15636 		else
15637 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15638 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15639 		if (sdinfo != NULL) {
15640 			if (sdinfo->satadrv_event_flags &
15641 			    (SATA_EVNT_DEVICE_RESET |
15642 			    SATA_EVNT_INPROC_DEVICE_RESET))
15643 				sdinfo->satadrv_event_flags = 0;
15644 			sdinfo->satadrv_event_flags |=
15645 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15646 
15647 			/* Need to create a new target node. */
15648 			cportinfo->cport_tgtnode_clean = B_TRUE;
15649 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15650 			    saddr->cport)->cport_mutex);
15651 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15652 			    sata_hba_inst, &sata_device.satadev_addr);
15653 			if (tdip == NULL) {
15654 				/*
15655 				 * Configure (onlining) failed.
15656 				 * We will NOT retry
15657 				 */
15658 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15659 				    "sata_process_device_autoonline: "
15660 				    "configuring SATA device at port %d failed",
15661 				    saddr->cport));
15662 			}
15663 		} else {
15664 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15665 			    saddr->cport)->cport_mutex);
15666 		}
15667 
15668 	}
15669 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15670 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15671 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15672 	    saddr->cport)->cport_mutex);
15673 }
15674 
15675 
15676 static void
15677 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15678     int hint)
15679 {
15680 	char ap[MAXPATHLEN];
15681 	nvlist_t *ev_attr_list = NULL;
15682 	int err;
15683 
15684 	/* Allocate and build sysevent attribute list */
15685 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15686 	if (err != 0) {
15687 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15688 		    "sata_gen_sysevent: "
15689 		    "cannot allocate memory for sysevent attributes\n"));
15690 		return;
15691 	}
15692 	/* Add hint attribute */
15693 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15694 	if (err != 0) {
15695 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15696 		    "sata_gen_sysevent: "
15697 		    "failed to add DR_HINT attr for sysevent"));
15698 		nvlist_free(ev_attr_list);
15699 		return;
15700 	}
15701 	/*
15702 	 * Add AP attribute.
15703 	 * Get controller pathname and convert it into AP pathname by adding
15704 	 * a target number.
15705 	 */
15706 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15707 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15708 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15709 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15710 
15711 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15712 	if (err != 0) {
15713 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15714 		    "sata_gen_sysevent: "
15715 		    "failed to add DR_AP_ID attr for sysevent"));
15716 		nvlist_free(ev_attr_list);
15717 		return;
15718 	}
15719 
15720 	/* Generate/log sysevent */
15721 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15722 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15723 	if (err != DDI_SUCCESS) {
15724 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15725 		    "sata_gen_sysevent: "
15726 		    "cannot log sysevent, err code %x\n", err));
15727 	}
15728 
15729 	nvlist_free(ev_attr_list);
15730 }
15731 
15732 
15733 
15734 
15735 /*
15736  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15737  */
15738 static void
15739 sata_set_device_removed(dev_info_t *tdip)
15740 {
15741 	int circ;
15742 
15743 	ASSERT(tdip != NULL);
15744 
15745 	ndi_devi_enter(tdip, &circ);
15746 	mutex_enter(&DEVI(tdip)->devi_lock);
15747 	DEVI_SET_DEVICE_REMOVED(tdip);
15748 	mutex_exit(&DEVI(tdip)->devi_lock);
15749 	ndi_devi_exit(tdip, circ);
15750 }
15751 
15752 
15753 /*
15754  * Set internal event instructing event daemon to try
15755  * to perform the target node cleanup.
15756  */
15757 static void
15758 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15759     sata_address_t *saddr)
15760 {
15761 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15762 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15763 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15764 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15765 	    B_FALSE;
15766 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15767 	mutex_enter(&sata_hba_inst->satahba_mutex);
15768 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15769 	mutex_exit(&sata_hba_inst->satahba_mutex);
15770 	mutex_enter(&sata_mutex);
15771 	sata_event_pending |= SATA_EVNT_MAIN;
15772 	mutex_exit(&sata_mutex);
15773 }
15774 
15775 
15776 /*
15777  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15778  * i.e. check if the target node state indicates that it belongs to a removed
15779  * device.
15780  *
15781  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15782  * B_FALSE otherwise.
15783  *
15784  * NOTE: No port multiplier support.
15785  */
15786 static boolean_t
15787 sata_check_device_removed(dev_info_t *tdip)
15788 {
15789 	ASSERT(tdip != NULL);
15790 
15791 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15792 		return (B_TRUE);
15793 	else
15794 		return (B_FALSE);
15795 }
15796 
15797 /* ************************ FAULT INJECTTION **************************** */
15798 
15799 #ifdef SATA_INJECT_FAULTS
15800 
15801 static	uint32_t sata_fault_count = 0;
15802 static	uint32_t sata_fault_suspend_count = 0;
15803 
15804 /*
15805  * Inject sata pkt fault
15806  * It modifies returned values of the sata packet.
15807  * It returns immediately if:
15808  * pkt fault injection is not enabled (via sata_inject_fault,
15809  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
15810  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
15811  * pkt is not directed to specified fault controller/device
15812  * (sata_fault_ctrl_dev and sata_fault_device).
15813  * If fault controller is not specified, fault injection applies to all
15814  * controllers and devices.
15815  *
15816  * First argument is the pointer to the executed sata packet.
15817  * Second argument is a pointer to a value returned by the HBA tran_start
15818  * function.
15819  * Third argument specifies injected error. Injected sata packet faults
15820  * are the satapkt_reason values.
15821  * SATA_PKT_BUSY		-1	Not completed, busy
15822  * SATA_PKT_DEV_ERROR		1	Device reported error
15823  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15824  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15825  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15826  * SATA_PKT_ABORTED		5	Aborted by request
15827  * SATA_PKT_TIMEOUT		6	Operation timeut
15828  * SATA_PKT_RESET		7	Aborted by reset request
15829  *
15830  * Additional global variables affecting the execution:
15831  *
15832  * sata_inject_fault_count variable specifies number of times in row the
15833  * error is injected. Value of -1 specifies permanent fault, ie. every time
15834  * the fault injection point is reached, the fault is injected and a pause
15835  * between fault injection specified by sata_inject_fault_pause_count is
15836  * ignored). Fault injection routine decrements sata_inject_fault_count
15837  * (if greater than zero) until it reaches 0. No fault is injected when
15838  * sata_inject_fault_count is 0 (zero).
15839  *
15840  * sata_inject_fault_pause_count variable specifies number of times a fault
15841  * injection is bypassed (pause between fault injections).
15842  * If set to 0, a fault is injected only a number of times specified by
15843  * sata_inject_fault_count.
15844  *
15845  * The fault counts are static, so for periodic errors they have to be manually
15846  * reset to start repetition sequence from scratch.
15847  * If the original value returned by the HBA tran_start function is not
15848  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15849  * is injected (to avoid masking real problems);
15850  *
15851  * NOTE: In its current incarnation, this function should be invoked only for
15852  * commands executed in SYNCHRONOUS mode.
15853  */
15854 
15855 
15856 static void
15857 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
15858 {
15859 
15860 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
15861 		return;
15862 
15863 	if (sata_inject_fault_count == 0)
15864 		return;
15865 
15866 	if (fault == 0)
15867 		return;
15868 
15869 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
15870 		return;
15871 
15872 	if (sata_fault_ctrl != NULL) {
15873 		sata_pkt_txlate_t *spx =
15874 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
15875 
15876 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
15877 		    spx->txlt_sata_hba_inst->satahba_dip)
15878 			return;
15879 
15880 		if (sata_fault_device.satadev_addr.cport !=
15881 		    spkt->satapkt_device.satadev_addr.cport ||
15882 		    sata_fault_device.satadev_addr.pmport !=
15883 		    spkt->satapkt_device.satadev_addr.pmport ||
15884 		    sata_fault_device.satadev_addr.qual !=
15885 		    spkt->satapkt_device.satadev_addr.qual)
15886 			return;
15887 	}
15888 
15889 	/* Modify pkt return parameters */
15890 	if (*rval != SATA_TRAN_ACCEPTED ||
15891 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15892 		sata_fault_count = 0;
15893 		sata_fault_suspend_count = 0;
15894 		return;
15895 	}
15896 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15897 		/* Pause in the injection */
15898 		sata_fault_suspend_count -= 1;
15899 		return;
15900 	}
15901 
15902 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15903 		/*
15904 		 * Init inject fault cycle. If fault count is set to -1,
15905 		 * it is a permanent fault.
15906 		 */
15907 		if (sata_inject_fault_count != -1) {
15908 			sata_fault_count = sata_inject_fault_count;
15909 			sata_fault_suspend_count =
15910 			    sata_inject_fault_pause_count;
15911 			if (sata_fault_suspend_count == 0)
15912 				sata_inject_fault_count = 0;
15913 		}
15914 	}
15915 
15916 	if (sata_fault_count != 0)
15917 		sata_fault_count -= 1;
15918 
15919 	switch (fault) {
15920 	case SATA_PKT_BUSY:
15921 		*rval = SATA_TRAN_BUSY;
15922 		spkt->satapkt_reason = SATA_PKT_BUSY;
15923 		break;
15924 
15925 	case SATA_PKT_QUEUE_FULL:
15926 		*rval = SATA_TRAN_QUEUE_FULL;
15927 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15928 		break;
15929 
15930 	case SATA_PKT_CMD_UNSUPPORTED:
15931 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15932 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15933 		break;
15934 
15935 	case SATA_PKT_PORT_ERROR:
15936 		/* This is "rejected" command */
15937 		*rval = SATA_TRAN_PORT_ERROR;
15938 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15939 		/* Additional error setup could be done here - port state */
15940 		break;
15941 
15942 	case SATA_PKT_DEV_ERROR:
15943 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15944 		/*
15945 		 * Additional error setup could be done here
15946 		 */
15947 		break;
15948 
15949 	case SATA_PKT_ABORTED:
15950 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15951 		break;
15952 
15953 	case SATA_PKT_TIMEOUT:
15954 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15955 		/* Additional error setup could be done here */
15956 		break;
15957 
15958 	case SATA_PKT_RESET:
15959 		spkt->satapkt_reason = SATA_PKT_RESET;
15960 		/*
15961 		 * Additional error setup could be done here - device reset
15962 		 */
15963 		break;
15964 
15965 	default:
15966 		break;
15967 	}
15968 }
15969 
15970 #endif
15971 
15972 /*
15973  * SATA Trace Ring Buffer
15974  * ----------------------
15975  *
15976  * Overview
15977  *
15978  * The SATA trace ring buffer is a ring buffer created and managed by
15979  * the SATA framework module that can be used by any module or driver
15980  * within the SATA framework to store debug messages.
15981  *
15982  * Ring Buffer Interfaces:
15983  *
15984  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
15985  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
15986  *
15987  *	Note that the sata_trace_debug() interface was created to give
15988  *	consumers the flexibilty of sending debug messages to ring buffer
15989  *	as variable arguments.  Consumers can send type va_list debug
15990  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
15991  *	and sata_vtrace_debug() relationship is similar to that of
15992  *	cmn_err(9F) and vcmn_err(9F).
15993  *
15994  * Below is a diagram of the SATA trace ring buffer interfaces and
15995  * sample consumers:
15996  *
15997  * +---------------------------------+
15998  * |    o  o  SATA Framework Module  |
15999  * | o  SATA  o     +------------------+      +------------------+
16000  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
16001  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
16002  * | o        o     +------------------+   |  +------------------+
16003  * |    o  o                ^        |     +--|SATA HBA Driver #2|
16004  * |                        |        |        +------------------+
16005  * |           +------------------+  |
16006  * |           |SATA Debug Message|  |
16007  * |           +------------------+  |
16008  * +---------------------------------+
16009  *
16010  * Supporting Routines:
16011  *
16012  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
16013  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
16014  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
16015  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
16016  *
16017  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
16018  * The ring buffer size can be adjusted by setting dmsg_ring_size in
16019  * /etc/system to desired size in unit of bytes.
16020  *
16021  * The individual debug message size in the ring buffer is restricted
16022  * to DMSG_BUF_SIZE.
16023  */
16024 void
16025 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
16026 {
16027 	sata_trace_dmsg_t *dmsg;
16028 
16029 	if (sata_debug_rbuf == NULL) {
16030 		return;
16031 	}
16032 
16033 	/*
16034 	 * If max size of ring buffer is smaller than size
16035 	 * required for one debug message then just return
16036 	 * since we have no room for the debug message.
16037 	 */
16038 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
16039 		return;
16040 	}
16041 
16042 	mutex_enter(&sata_debug_rbuf->lock);
16043 
16044 	/* alloc or reuse on ring buffer */
16045 	dmsg = sata_trace_dmsg_alloc();
16046 
16047 	if (dmsg == NULL) {
16048 		/* resource allocation failed */
16049 		mutex_exit(&sata_debug_rbuf->lock);
16050 		return;
16051 	}
16052 
16053 	dmsg->dip = dip;
16054 	gethrestime(&dmsg->timestamp);
16055 
16056 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
16057 
16058 	mutex_exit(&sata_debug_rbuf->lock);
16059 }
16060 
16061 void
16062 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
16063 {
16064 	va_list ap;
16065 
16066 	va_start(ap, fmt);
16067 	sata_vtrace_debug(dip, fmt, ap);
16068 	va_end(ap);
16069 }
16070 
16071 /*
16072  * This routine is used to manage debug messages
16073  * on ring buffer.
16074  */
16075 static sata_trace_dmsg_t *
16076 sata_trace_dmsg_alloc(void)
16077 {
16078 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
16079 
16080 	if (sata_debug_rbuf->looped == TRUE) {
16081 		sata_debug_rbuf->dmsgp = dmsg->next;
16082 		return (sata_debug_rbuf->dmsgp);
16083 	}
16084 
16085 	/*
16086 	 * If we're looping for the first time,
16087 	 * connect the ring.
16088 	 */
16089 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
16090 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
16091 		dmsg->next = sata_debug_rbuf->dmsgh;
16092 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
16093 		sata_debug_rbuf->looped = TRUE;
16094 		return (sata_debug_rbuf->dmsgp);
16095 	}
16096 
16097 	/* If we've gotten this far then memory allocation is needed */
16098 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
16099 	if (dmsg_alloc == NULL) {
16100 		sata_debug_rbuf->allocfailed++;
16101 		return (dmsg_alloc);
16102 	} else {
16103 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
16104 	}
16105 
16106 	if (sata_debug_rbuf->dmsgp != NULL) {
16107 		dmsg->next = dmsg_alloc;
16108 		sata_debug_rbuf->dmsgp = dmsg->next;
16109 		return (sata_debug_rbuf->dmsgp);
16110 	} else {
16111 		/*
16112 		 * We should only be here if we're initializing
16113 		 * the ring buffer.
16114 		 */
16115 		if (sata_debug_rbuf->dmsgh == NULL) {
16116 			sata_debug_rbuf->dmsgh = dmsg_alloc;
16117 		} else {
16118 			/* Something is wrong */
16119 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
16120 			return (NULL);
16121 		}
16122 
16123 		sata_debug_rbuf->dmsgp = dmsg_alloc;
16124 		return (sata_debug_rbuf->dmsgp);
16125 	}
16126 }
16127 
16128 
16129 /*
16130  * Free all messages on debug ring buffer.
16131  */
16132 static void
16133 sata_trace_dmsg_free(void)
16134 {
16135 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
16136 
16137 	while (dmsg != NULL) {
16138 		dmsg_next = dmsg->next;
16139 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
16140 
16141 		/*
16142 		 * If we've looped around the ring than we're done.
16143 		 */
16144 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
16145 			break;
16146 		} else {
16147 			dmsg = dmsg_next;
16148 		}
16149 	}
16150 }
16151 
16152 
16153 /*
16154  * This function can block
16155  */
16156 static void
16157 sata_trace_rbuf_alloc(void)
16158 {
16159 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
16160 
16161 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
16162 
16163 	if (dmsg_ring_size > 0) {
16164 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
16165 	}
16166 }
16167 
16168 
16169 static void
16170 sata_trace_rbuf_free(void)
16171 {
16172 	sata_trace_dmsg_free();
16173 	mutex_destroy(&sata_debug_rbuf->lock);
16174 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
16175 }
16176 
16177 /*
16178  * If SATA_DEBUG is not defined then this routine is called instead
16179  * of sata_log() via the SATA_LOG_D macro.
16180  */
16181 static void
16182 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
16183     const char *fmt, ...)
16184 {
16185 #ifndef __lock_lint
16186 	_NOTE(ARGUNUSED(level))
16187 #endif
16188 
16189 	dev_info_t *dip = NULL;
16190 	va_list ap;
16191 
16192 	if (sata_hba_inst != NULL) {
16193 		dip = SATA_DIP(sata_hba_inst);
16194 	}
16195 
16196 	va_start(ap, fmt);
16197 	sata_vtrace_debug(dip, fmt, ap);
16198 	va_end(ap);
16199 }
16200