xref: /titanic_50/usr/src/uts/common/io/sata/impl/sata.c (revision 9a4611f412a6b1f7a0bc7d53d2bb046a95daa4bc)
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_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
205 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
206 static	void sata_txlt_rw_completion(sata_pkt_t *);
207 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
208 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
209 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
210 static	struct scsi_extended_sense *sata_immediate_error_response(
211     sata_pkt_txlate_t *, int);
212 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
213 
214 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
215 static	void sata_txlt_atapi_completion(sata_pkt_t *);
216 
217 /*
218  * Local functions for ioctl
219  */
220 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
221 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
222     devctl_ap_state_t *);
223 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
224 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
225 static	dev_info_t *sata_devt_to_devinfo(dev_t);
226 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
230 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
232 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
233 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
234 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
235 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
236 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
237     sata_ioctl_data_t *, int mode);
238 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
239     sata_ioctl_data_t *, int mode);
240 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
241     sata_ioctl_data_t *, int mode);
242 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
243     sata_ioctl_data_t *, int mode);
244 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
245     sata_device_t *, sata_ioctl_data_t *, int mode);
246 
247 /*
248  * Local functions
249  */
250 static 	void sata_remove_hba_instance(dev_info_t *);
251 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
252 static 	void sata_probe_ports(sata_hba_inst_t *);
253 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
254 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
255     int pmport);
256 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
257     sata_address_t *);
258 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
259     struct scsi_address *, sata_device_t *);
260 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
261 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
262 static	void sata_pkt_free(sata_pkt_txlate_t *);
263 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
264     caddr_t, ddi_dma_attr_t *);
265 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
266 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
267 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
268     sata_device_t *);
269 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
270 static	void sata_reidentify_device(sata_pkt_txlate_t *);
271 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
272 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
273 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
274 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
275     ddi_dma_attr_t *);
276 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
277     sata_drive_info_t *);
278 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
279 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
280 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
281 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
282 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
283 static	int sata_set_drive_features(sata_hba_inst_t *,
284     sata_drive_info_t *, int flag);
285 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
286 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
287 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
288     uint8_t *);
289 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
290     struct scsi_inquiry *);
291 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
292 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
293 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
294 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
295 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
296 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
297     struct mode_cache_scsi3 *, int, int *, int *, int *);
298 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
299     struct mode_info_power_cond *, int, int *, int *, int *);
300 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
301     struct mode_info_excpt_page *, int, int *, int *, int *);
302 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
303     struct mode_acoustic_management *, int, int *, int *, int *);
304 
305 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
306 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
307     sata_hba_inst_t *);
308 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
309     sata_hba_inst_t *);
310 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
311     sata_hba_inst_t *);
312 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
313     sata_pkt_txlate_t *);
314 
315 static	void sata_set_arq_data(sata_pkt_t *);
316 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
317 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
318 static	uint8_t sata_get_standby_timer(uint8_t *timer);
319 
320 static	void sata_save_drive_settings(sata_drive_info_t *);
321 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
322 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
323 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
324 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
325     sata_drive_info_t *);
326 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
327     struct smart_data *);
328 static	int sata_smart_selftest_log(sata_hba_inst_t *,
329     sata_drive_info_t *,
330     struct smart_selftest_log *);
331 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
332     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
333 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
334     uint8_t *, uint8_t, uint8_t);
335 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
336     struct read_log_ext_directory *);
337 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
338 static	void sata_xlate_errors(sata_pkt_txlate_t *);
339 static	void sata_decode_device_error(sata_pkt_txlate_t *,
340     struct scsi_extended_sense *);
341 static	void sata_set_device_removed(dev_info_t *);
342 static	boolean_t sata_check_device_removed(dev_info_t *);
343 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
344 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
345     sata_drive_info_t *);
346 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
347     sata_drive_info_t *);
348 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
349 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
350 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
351 static  int sata_check_modser(char *, int);
352 
353 
354 
355 /*
356  * SATA Framework will ignore SATA HBA driver cb_ops structure and
357  * register following one with SCSA framework.
358  * Open & close are provided, so scsi framework will not use its own
359  */
360 static struct cb_ops sata_cb_ops = {
361 	sata_hba_open,			/* open */
362 	sata_hba_close,			/* close */
363 	nodev,				/* strategy */
364 	nodev,				/* print */
365 	nodev,				/* dump */
366 	nodev,				/* read */
367 	nodev,				/* write */
368 	sata_hba_ioctl,			/* ioctl */
369 	nodev,				/* devmap */
370 	nodev,				/* mmap */
371 	nodev,				/* segmap */
372 	nochpoll,			/* chpoll */
373 	ddi_prop_op,			/* cb_prop_op */
374 	0,				/* streamtab */
375 	D_NEW | D_MP,			/* cb_flag */
376 	CB_REV,				/* rev */
377 	nodev,				/* aread */
378 	nodev				/* awrite */
379 };
380 
381 
382 extern struct mod_ops mod_miscops;
383 extern uchar_t	scsi_cdb_size[];
384 
385 static struct modlmisc modlmisc = {
386 	&mod_miscops,			/* Type of module */
387 	"SATA Module"			/* module name */
388 };
389 
390 
391 static struct modlinkage modlinkage = {
392 	MODREV_1,
393 	(void *)&modlmisc,
394 	NULL
395 };
396 
397 /*
398  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
399  * i.e. when scsi_pkt has not timeout specified.
400  */
401 static int sata_default_pkt_time = 60;	/* 60 seconds */
402 
403 /*
404  * Intermediate buffer device access attributes - they are required,
405  * but not necessarily used.
406  */
407 static ddi_device_acc_attr_t sata_acc_attr = {
408 	DDI_DEVICE_ATTR_V0,
409 	DDI_STRUCTURE_LE_ACC,
410 	DDI_STRICTORDER_ACC
411 };
412 
413 
414 /*
415  * Mutexes protecting structures in multithreaded operations.
416  * Because events are relatively rare, a single global mutex protecting
417  * data structures should be sufficient. To increase performance, add
418  * separate mutex per each sata port and use global mutex only to protect
419  * common data structures.
420  */
421 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
422 static	kmutex_t sata_log_mutex;	/* protects log */
423 
424 static 	char sata_log_buf[256];
425 
426 /*
427  * sata trace debug
428  */
429 static	sata_trace_rbuf_t *sata_debug_rbuf;
430 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
431 static	void sata_trace_dmsg_free(void);
432 static	void sata_trace_rbuf_alloc(void);
433 static	void sata_trace_rbuf_free(void);
434 
435 int	dmsg_ring_size = DMSG_RING_SIZE;
436 
437 /* Default write cache setting for SATA hard disks */
438 int	sata_write_cache = 1;		/* enabled */
439 
440 /* Default write cache setting for SATA ATAPI CD/DVD */
441 int	sata_atapicdvd_write_cache = 1; /* enabled */
442 
443 /* Default write cache setting for SATA ATAPI tape */
444 int	sata_atapitape_write_cache = 1; /* enabled */
445 
446 /* Default write cache setting for SATA ATAPI disk */
447 int	sata_atapidisk_write_cache = 1;	/* enabled */
448 
449 /*
450  * Linked list of HBA instances
451  */
452 static 	sata_hba_inst_t *sata_hba_list = NULL;
453 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
454 /*
455  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
456  * structure and in sata soft state.
457  */
458 
459 /*
460  * Event daemon related variables
461  */
462 static 	kmutex_t sata_event_mutex;
463 static 	kcondvar_t sata_event_cv;
464 static 	kthread_t *sata_event_thread = NULL;
465 static 	int sata_event_thread_terminate = 0;
466 static 	int sata_event_pending = 0;
467 static 	int sata_event_thread_active = 0;
468 extern 	pri_t minclsyspri;
469 
470 /*
471  * NCQ error recovery command
472  */
473 static const sata_cmd_t sata_rle_cmd = {
474 	SATA_CMD_REV,
475 	NULL,
476 	{
477 		SATA_DIR_READ
478 	},
479 	ATA_ADDR_LBA48,
480 	0,
481 	0,
482 	0,
483 	0,
484 	0,
485 	1,
486 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
487 	0,
488 	0,
489 	0,
490 	SATAC_READ_LOG_EXT,
491 	0,
492 	0,
493 	0,
494 };
495 
496 /*
497  * ATAPI error recovery CDB
498  */
499 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
500 	SCMD_REQUEST_SENSE,
501 	0,			/* Only fixed RQ format is supported */
502 	0,
503 	0,
504 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
505 	0
506 };
507 
508 
509 /* Warlock directives */
510 
511 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
512 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
513 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
514 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
515 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
516 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
517 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
518 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
520 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
521 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
522 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
523 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
524 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
525     sata_hba_inst::satahba_scsi_tran))
526 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
527 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
528 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
529 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
530 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
531     sata_hba_inst::satahba_event_flags))
532 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
533     sata_cport_info::cport_devp))
534 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
535 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
536 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
537     sata_cport_info::cport_dev_type))
538 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
539 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
540     sata_cport_info::cport_state))
541 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
542 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
543     sata_pmport_info::pmport_state))
544 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
545 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
546 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
547 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
548 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
549 #ifdef SATA_DEBUG
550 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
551 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
552 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
553 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
554 #endif
555 
556 /* End of warlock directives */
557 
558 /* ************** loadable module configuration functions ************** */
559 
560 int
561 _init()
562 {
563 	int rval;
564 
565 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
566 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
567 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
568 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
569 	sata_trace_rbuf_alloc();
570 	if ((rval = mod_install(&modlinkage)) != 0) {
571 #ifdef SATA_DEBUG
572 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
573 #endif
574 		sata_trace_rbuf_free();
575 		mutex_destroy(&sata_log_mutex);
576 		cv_destroy(&sata_event_cv);
577 		mutex_destroy(&sata_event_mutex);
578 		mutex_destroy(&sata_mutex);
579 	}
580 	return (rval);
581 }
582 
583 int
584 _fini()
585 {
586 	int rval;
587 
588 	if ((rval = mod_remove(&modlinkage)) != 0)
589 		return (rval);
590 
591 	sata_trace_rbuf_free();
592 	mutex_destroy(&sata_log_mutex);
593 	cv_destroy(&sata_event_cv);
594 	mutex_destroy(&sata_event_mutex);
595 	mutex_destroy(&sata_mutex);
596 	return (rval);
597 }
598 
599 int
600 _info(struct modinfo *modinfop)
601 {
602 	return (mod_info(&modlinkage, modinfop));
603 }
604 
605 
606 
607 /* ********************* SATA HBA entry points ********************* */
608 
609 
610 /*
611  * Called by SATA HBA from _init().
612  * Registers HBA driver instance/sata framework pair with scsi framework, by
613  * calling scsi_hba_init().
614  *
615  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
616  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
617  * cb_ops pointer in SATA HBA driver dev_ops structure.
618  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
619  *
620  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
621  * driver.
622  */
623 int
624 sata_hba_init(struct modlinkage *modlp)
625 {
626 	int rval;
627 	struct dev_ops *hba_ops;
628 
629 	SATADBG1(SATA_DBG_HBA_IF, NULL,
630 	    "sata_hba_init: name %s \n",
631 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
632 	/*
633 	 * Fill-up cb_ops and dev_ops when necessary
634 	 */
635 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
636 	/*
637 	 * Provide pointer to SATA dev_ops
638 	 */
639 	hba_ops->devo_cb_ops = &sata_cb_ops;
640 
641 	/*
642 	 * Register SATA HBA with SCSI framework
643 	 */
644 	if ((rval = scsi_hba_init(modlp)) != 0) {
645 		SATADBG1(SATA_DBG_HBA_IF, NULL,
646 		    "sata_hba_init: scsi hba init failed\n", NULL);
647 		return (rval);
648 	}
649 
650 	return (0);
651 }
652 
653 
654 /* HBA attach stages */
655 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
656 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
657 #define	HBA_ATTACH_STAGE_SETUP		4
658 #define	HBA_ATTACH_STAGE_LINKED		8
659 
660 
661 /*
662  *
663  * Called from SATA HBA driver's attach routine to attach an instance of
664  * the HBA.
665  *
666  * For DDI_ATTACH command:
667  * sata_hba_inst structure is allocated here and initialized with pointers to
668  * SATA framework implementation of required scsi tran functions.
669  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
670  * to the soft structure (sata_hba_inst) allocated by SATA framework for
671  * SATA HBA instance related data.
672  * The scsi_tran's tran_hba_private field is used by SATA framework to
673  * store a pointer to per-HBA-instance of sata_hba_inst structure.
674  * The sata_hba_inst structure is cross-linked to scsi tran structure.
675  * Among other info, a pointer to sata_hba_tran structure is stored in
676  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
677  * linked together into the list, pointed to by sata_hba_list.
678  * On the first HBA instance attach the sata event thread is initialized.
679  * Attachment points are created for all SATA ports of the HBA being attached.
680  * All HBA instance's SATA ports are probed and type of plugged devices is
681  * determined. For each device of a supported type, a target node is created.
682  *
683  * DDI_SUCCESS is returned when attachment process is successful,
684  * DDI_FAILURE is returned otherwise.
685  *
686  * For DDI_RESUME command:
687  * Not implemented at this time (postponed until phase 2 of the development).
688  */
689 int
690 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
691     ddi_attach_cmd_t cmd)
692 {
693 	sata_hba_inst_t	*sata_hba_inst;
694 	scsi_hba_tran_t *scsi_tran = NULL;
695 	int hba_attach_state = 0;
696 	char taskq_name[MAXPATHLEN];
697 
698 	SATADBG3(SATA_DBG_HBA_IF, NULL,
699 	    "sata_hba_attach: node %s (%s%d)\n",
700 	    ddi_node_name(dip), ddi_driver_name(dip),
701 	    ddi_get_instance(dip));
702 
703 	if (cmd == DDI_RESUME) {
704 		/*
705 		 * Postponed until phase 2 of the development
706 		 */
707 		return (DDI_FAILURE);
708 	}
709 
710 	if (cmd != DDI_ATTACH) {
711 		return (DDI_FAILURE);
712 	}
713 
714 	/* cmd == DDI_ATTACH */
715 
716 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
717 		SATA_LOG_D((NULL, CE_WARN,
718 		    "sata_hba_attach: invalid sata_hba_tran"));
719 		return (DDI_FAILURE);
720 	}
721 	/*
722 	 * Allocate and initialize SCSI tran structure.
723 	 * SATA copy of tran_bus_config is provided to create port nodes.
724 	 */
725 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
726 	if (scsi_tran == NULL)
727 		return (DDI_FAILURE);
728 	/*
729 	 * Allocate soft structure for SATA HBA instance.
730 	 * There is a separate softstate for each HBA instance.
731 	 */
732 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
733 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
734 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
735 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
736 
737 	/*
738 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
739 	 * soft structure allocated by SATA framework for
740 	 * SATA HBA instance related data.
741 	 */
742 	scsi_tran->tran_hba_private	= sata_hba_inst;
743 	scsi_tran->tran_tgt_private	= NULL;
744 
745 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
746 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
747 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
748 
749 	scsi_tran->tran_start		= sata_scsi_start;
750 	scsi_tran->tran_reset		= sata_scsi_reset;
751 	scsi_tran->tran_abort		= sata_scsi_abort;
752 	scsi_tran->tran_getcap		= sata_scsi_getcap;
753 	scsi_tran->tran_setcap		= sata_scsi_setcap;
754 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
755 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
756 
757 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
758 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
759 
760 	scsi_tran->tran_reset_notify	= NULL;
761 	scsi_tran->tran_get_bus_addr	= NULL;
762 	scsi_tran->tran_quiesce		= NULL;
763 	scsi_tran->tran_unquiesce	= NULL;
764 	scsi_tran->tran_bus_reset	= NULL;
765 
766 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
767 	    scsi_tran, 0) != DDI_SUCCESS) {
768 #ifdef SATA_DEBUG
769 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
770 		    ddi_driver_name(dip), ddi_get_instance(dip));
771 #endif
772 		goto fail;
773 	}
774 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
775 
776 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
777 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
778 		    "sata", 1) != DDI_PROP_SUCCESS) {
779 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
780 			    "failed to create hba sata prop"));
781 			goto fail;
782 		}
783 	}
784 
785 	/*
786 	 * Save pointers in hba instance soft state.
787 	 */
788 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
789 	sata_hba_inst->satahba_tran = sata_tran;
790 	sata_hba_inst->satahba_dip = dip;
791 
792 	/*
793 	 * Create a task queue to handle emulated commands completion
794 	 * Use node name, dash, instance number as the queue name.
795 	 */
796 	taskq_name[0] = '\0';
797 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
798 	    sizeof (taskq_name));
799 	(void) snprintf(taskq_name + strlen(taskq_name),
800 	    sizeof (taskq_name) - strlen(taskq_name),
801 	    "-%d", DEVI(dip)->devi_instance);
802 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
803 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
804 	    TASKQ_DYNAMIC);
805 
806 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
807 
808 	/*
809 	 * Create events thread if not created yet.
810 	 */
811 	sata_event_thread_control(1);
812 
813 	/*
814 	 * Link this hba instance into the list.
815 	 */
816 	mutex_enter(&sata_mutex);
817 
818 	if (sata_hba_list == NULL) {
819 		/*
820 		 * The first instance of HBA is attached.
821 		 * Set current/active default maximum NCQ/TCQ queue depth for
822 		 * all SATA devices. It is done here and now, to eliminate the
823 		 * possibility of the dynamic, programatic modification of the
824 		 * queue depth via global (and public) sata_max_queue_depth
825 		 * variable (this would require special handling in HBA drivers)
826 		 */
827 		sata_current_max_qdepth = sata_max_queue_depth;
828 		if (sata_current_max_qdepth > 32)
829 			sata_current_max_qdepth = 32;
830 		else if (sata_current_max_qdepth < 1)
831 			sata_current_max_qdepth = 1;
832 	}
833 
834 	sata_hba_inst->satahba_next = NULL;
835 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
836 	if (sata_hba_list == NULL) {
837 		sata_hba_list = sata_hba_inst;
838 	}
839 	if (sata_hba_list_tail != NULL) {
840 		sata_hba_list_tail->satahba_next = sata_hba_inst;
841 	}
842 	sata_hba_list_tail = sata_hba_inst;
843 	mutex_exit(&sata_mutex);
844 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
845 
846 	/*
847 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
848 	 * SATA HBA driver should not use its own open/close entry points.
849 	 *
850 	 * Make sure that instance number doesn't overflow
851 	 * when forming minor numbers.
852 	 */
853 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
854 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
855 	    INST2DEVCTL(ddi_get_instance(dip)),
856 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
857 #ifdef SATA_DEBUG
858 		cmn_err(CE_WARN, "sata_hba_attach: "
859 		    "cannot create devctl minor node");
860 #endif
861 		goto fail;
862 	}
863 
864 
865 	/*
866 	 * Set-up kstats here, if necessary.
867 	 * (postponed until future phase of the development).
868 	 */
869 
870 	/*
871 	 * Indicate that HBA is attached. This will enable events processing
872 	 * for this HBA.
873 	 */
874 	sata_hba_inst->satahba_attached = 1;
875 	/*
876 	 * Probe controller ports. This operation will describe a current
877 	 * controller/port/multipliers/device configuration and will create
878 	 * attachment points.
879 	 * We may end-up with just a controller with no devices attached.
880 	 * For the ports with a supported device attached, device target nodes
881 	 * are created and devices are initialized.
882 	 */
883 	sata_probe_ports(sata_hba_inst);
884 
885 	return (DDI_SUCCESS);
886 
887 fail:
888 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
889 		(void) sata_remove_hba_instance(dip);
890 		if (sata_hba_list == NULL)
891 			sata_event_thread_control(0);
892 	}
893 
894 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
895 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
896 		taskq_destroy(sata_hba_inst->satahba_taskq);
897 	}
898 
899 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
900 		(void) scsi_hba_detach(dip);
901 
902 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
903 		mutex_destroy(&sata_hba_inst->satahba_mutex);
904 		kmem_free((void *)sata_hba_inst,
905 		    sizeof (struct sata_hba_inst));
906 		scsi_hba_tran_free(scsi_tran);
907 	}
908 
909 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
910 	    ddi_driver_name(dip), ddi_get_instance(dip));
911 
912 	return (DDI_FAILURE);
913 }
914 
915 
916 /*
917  * Called by SATA HBA from to detach an instance of the driver.
918  *
919  * For DDI_DETACH command:
920  * Free local structures allocated for SATA HBA instance during
921  * sata_hba_attach processing.
922  *
923  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
924  *
925  * For DDI_SUSPEND command:
926  * Not implemented at this time (postponed until phase 2 of the development)
927  * Returnd DDI_SUCCESS.
928  *
929  * When the last HBA instance is detached, the event daemon is terminated.
930  *
931  * NOTE: cport support only, no port multiplier support.
932  */
933 int
934 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
935 {
936 	dev_info_t	*tdip;
937 	sata_hba_inst_t	*sata_hba_inst;
938 	scsi_hba_tran_t *scsi_hba_tran;
939 	sata_cport_info_t *cportinfo;
940 	sata_drive_info_t *sdinfo;
941 	int ncport;
942 
943 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
944 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
945 
946 	switch (cmd) {
947 	case DDI_DETACH:
948 
949 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
950 			return (DDI_FAILURE);
951 
952 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
953 		if (sata_hba_inst == NULL)
954 			return (DDI_FAILURE);
955 
956 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
957 			sata_hba_inst->satahba_attached = 1;
958 			return (DDI_FAILURE);
959 		}
960 
961 		/*
962 		 * Free all target nodes - at this point
963 		 * devices should be at least offlined
964 		 * otherwise scsi_hba_detach() should not be called.
965 		 */
966 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
967 		    ncport++) {
968 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
969 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
970 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
971 				if (sdinfo != NULL) {
972 					tdip = sata_get_target_dip(dip,
973 					    ncport);
974 					if (tdip != NULL) {
975 						if (ndi_devi_offline(tdip,
976 						    NDI_DEVI_REMOVE) !=
977 						    NDI_SUCCESS) {
978 							SATA_LOG_D((
979 							    sata_hba_inst,
980 							    CE_WARN,
981 							    "sata_hba_detach: "
982 							    "Target node not "
983 							    "removed !"));
984 							return (DDI_FAILURE);
985 						}
986 					}
987 				}
988 			}
989 		}
990 		/*
991 		 * Disable sata event daemon processing for this HBA
992 		 */
993 		sata_hba_inst->satahba_attached = 0;
994 
995 		/*
996 		 * Remove event daemon thread, if it is last HBA instance.
997 		 */
998 
999 		mutex_enter(&sata_mutex);
1000 		if (sata_hba_list->satahba_next == NULL) {
1001 			mutex_exit(&sata_mutex);
1002 			sata_event_thread_control(0);
1003 			mutex_enter(&sata_mutex);
1004 		}
1005 		mutex_exit(&sata_mutex);
1006 
1007 		/* Remove this HBA instance from the HBA list */
1008 		sata_remove_hba_instance(dip);
1009 
1010 		/*
1011 		 * At this point there should be no target nodes attached.
1012 		 * Detach and destroy device and port info structures.
1013 		 */
1014 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1015 		    ncport++) {
1016 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1017 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1018 				sdinfo =
1019 				    cportinfo->cport_devp.cport_sata_drive;
1020 				if (sdinfo != NULL) {
1021 					/* Release device structure */
1022 					kmem_free(sdinfo,
1023 					    sizeof (sata_drive_info_t));
1024 				}
1025 				/* Release cport info */
1026 				mutex_destroy(&cportinfo->cport_mutex);
1027 				kmem_free(cportinfo,
1028 				    sizeof (sata_cport_info_t));
1029 			}
1030 		}
1031 
1032 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1033 
1034 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1035 
1036 		taskq_destroy(sata_hba_inst->satahba_taskq);
1037 
1038 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1039 		kmem_free((void *)sata_hba_inst,
1040 		    sizeof (struct sata_hba_inst));
1041 
1042 		return (DDI_SUCCESS);
1043 
1044 	case DDI_SUSPEND:
1045 		/*
1046 		 * Postponed until phase 2
1047 		 */
1048 		return (DDI_FAILURE);
1049 
1050 	default:
1051 		return (DDI_FAILURE);
1052 	}
1053 }
1054 
1055 
1056 /*
1057  * Called by an HBA drive from _fini() routine.
1058  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1059  */
1060 void
1061 sata_hba_fini(struct modlinkage *modlp)
1062 {
1063 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1064 	    "sata_hba_fini: name %s\n",
1065 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1066 
1067 	scsi_hba_fini(modlp);
1068 }
1069 
1070 
1071 /*
1072  * Default open and close routine for sata_hba framework.
1073  *
1074  */
1075 /*
1076  * Open devctl node.
1077  *
1078  * Returns:
1079  * 0 if node was open successfully, error code otherwise.
1080  *
1081  *
1082  */
1083 
1084 static int
1085 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1086 {
1087 #ifndef __lock_lint
1088 	_NOTE(ARGUNUSED(credp))
1089 #endif
1090 	int rv = 0;
1091 	dev_info_t *dip;
1092 	scsi_hba_tran_t *scsi_hba_tran;
1093 	sata_hba_inst_t	*sata_hba_inst;
1094 
1095 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1096 
1097 	if (otyp != OTYP_CHR)
1098 		return (EINVAL);
1099 
1100 	dip = sata_devt_to_devinfo(*devp);
1101 	if (dip == NULL)
1102 		return (ENXIO);
1103 
1104 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1105 		return (ENXIO);
1106 
1107 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1108 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1109 		return (ENXIO);
1110 
1111 	mutex_enter(&sata_mutex);
1112 	if (flags & FEXCL) {
1113 		if (sata_hba_inst->satahba_open_flag != 0) {
1114 			rv = EBUSY;
1115 		} else {
1116 			sata_hba_inst->satahba_open_flag =
1117 			    SATA_DEVCTL_EXOPENED;
1118 		}
1119 	} else {
1120 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1121 			rv = EBUSY;
1122 		} else {
1123 			sata_hba_inst->satahba_open_flag =
1124 			    SATA_DEVCTL_SOPENED;
1125 		}
1126 	}
1127 	mutex_exit(&sata_mutex);
1128 
1129 	return (rv);
1130 }
1131 
1132 
1133 /*
1134  * Close devctl node.
1135  * Returns:
1136  * 0 if node was closed successfully, error code otherwise.
1137  *
1138  */
1139 
1140 static int
1141 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1142 {
1143 #ifndef __lock_lint
1144 	_NOTE(ARGUNUSED(credp))
1145 	_NOTE(ARGUNUSED(flag))
1146 #endif
1147 	dev_info_t *dip;
1148 	scsi_hba_tran_t *scsi_hba_tran;
1149 	sata_hba_inst_t	*sata_hba_inst;
1150 
1151 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1152 
1153 	if (otyp != OTYP_CHR)
1154 		return (EINVAL);
1155 
1156 	dip = sata_devt_to_devinfo(dev);
1157 	if (dip == NULL)
1158 		return (ENXIO);
1159 
1160 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1161 		return (ENXIO);
1162 
1163 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1164 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1165 		return (ENXIO);
1166 
1167 	mutex_enter(&sata_mutex);
1168 	sata_hba_inst->satahba_open_flag = 0;
1169 	mutex_exit(&sata_mutex);
1170 	return (0);
1171 }
1172 
1173 
1174 
1175 /*
1176  * Standard IOCTL commands for SATA hotplugging.
1177  * Implemented DEVCTL_AP commands:
1178  * DEVCTL_AP_CONNECT
1179  * DEVCTL_AP_DISCONNECT
1180  * DEVCTL_AP_CONFIGURE
1181  * DEVCTL_UNCONFIGURE
1182  * DEVCTL_AP_CONTROL
1183  *
1184  * Commands passed to default ndi ioctl handler:
1185  * DEVCTL_DEVICE_GETSTATE
1186  * DEVCTL_DEVICE_ONLINE
1187  * DEVCTL_DEVICE_OFFLINE
1188  * DEVCTL_DEVICE_REMOVE
1189  * DEVCTL_DEVICE_INSERT
1190  * DEVCTL_BUS_GETSTATE
1191  *
1192  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1193  * if not.
1194  *
1195  * Returns:
1196  * 0 if successful,
1197  * error code if operation failed.
1198  *
1199  * NOTE: Port Multiplier is not supported.
1200  *
1201  */
1202 
1203 static int
1204 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1205     int *rvalp)
1206 {
1207 #ifndef __lock_lint
1208 	_NOTE(ARGUNUSED(credp))
1209 	_NOTE(ARGUNUSED(rvalp))
1210 #endif
1211 	int rv = 0;
1212 	int32_t	comp_port = -1;
1213 	dev_info_t *dip;
1214 	devctl_ap_state_t ap_state;
1215 	struct devctl_iocdata *dcp = NULL;
1216 	scsi_hba_tran_t *scsi_hba_tran;
1217 	sata_hba_inst_t *sata_hba_inst;
1218 	sata_device_t sata_device;
1219 	sata_cport_info_t *cportinfo;
1220 	int cport, pmport, qual;
1221 	int rval = SATA_SUCCESS;
1222 
1223 	dip = sata_devt_to_devinfo(dev);
1224 	if (dip == NULL)
1225 		return (ENXIO);
1226 
1227 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1228 		return (ENXIO);
1229 
1230 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1231 	if (sata_hba_inst == NULL)
1232 		return (ENXIO);
1233 
1234 	if (sata_hba_inst->satahba_tran == NULL)
1235 		return (ENXIO);
1236 
1237 	switch (cmd) {
1238 
1239 	case DEVCTL_DEVICE_GETSTATE:
1240 	case DEVCTL_DEVICE_ONLINE:
1241 	case DEVCTL_DEVICE_OFFLINE:
1242 	case DEVCTL_DEVICE_REMOVE:
1243 	case DEVCTL_BUS_GETSTATE:
1244 		/*
1245 		 * There may be more cases that we want to pass to default
1246 		 * handler rather than fail them.
1247 		 */
1248 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1249 	}
1250 
1251 	/* read devctl ioctl data */
1252 	if (cmd != DEVCTL_AP_CONTROL) {
1253 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1254 			return (EFAULT);
1255 
1256 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1257 		    -1) {
1258 			if (dcp)
1259 				ndi_dc_freehdl(dcp);
1260 			return (EINVAL);
1261 		}
1262 
1263 		cport = SCSI_TO_SATA_CPORT(comp_port);
1264 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1265 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1266 		qual = SATA_ADDR_CPORT;
1267 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1268 		    qual) != 0) {
1269 			ndi_dc_freehdl(dcp);
1270 			return (EINVAL);
1271 		}
1272 
1273 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1274 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1275 		    cport_mutex);
1276 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1277 			/*
1278 			 * Cannot process ioctl request now. Come back later.
1279 			 */
1280 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1281 			    cport_mutex);
1282 			ndi_dc_freehdl(dcp);
1283 			return (EBUSY);
1284 		}
1285 		/* Block event processing for this port */
1286 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1287 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1288 
1289 		sata_device.satadev_addr.cport = cport;
1290 		sata_device.satadev_addr.pmport = pmport;
1291 		sata_device.satadev_addr.qual = qual;
1292 		sata_device.satadev_rev = SATA_DEVICE_REV;
1293 	}
1294 
1295 	switch (cmd) {
1296 
1297 	case DEVCTL_AP_DISCONNECT:
1298 
1299 		/*
1300 		 * Normally, cfgadm sata plugin will try to offline
1301 		 * (unconfigure) device before this request. Nevertheless,
1302 		 * if a device is still configured, we need to
1303 		 * attempt to offline and unconfigure device first, and we will
1304 		 * deactivate the port regardless of the unconfigure
1305 		 * operation results.
1306 		 *
1307 		 */
1308 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1309 
1310 		break;
1311 
1312 	case DEVCTL_AP_UNCONFIGURE:
1313 
1314 		/*
1315 		 * The unconfigure operation uses generic nexus operation to
1316 		 * offline a device. It leaves a target device node attached.
1317 		 * and obviously sata_drive_info attached as well, because
1318 		 * from the hardware point of view nothing has changed.
1319 		 */
1320 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1321 		break;
1322 
1323 	case DEVCTL_AP_CONNECT:
1324 	{
1325 		/*
1326 		 * The sata cfgadm pluging will invoke this operation only if
1327 		 * port was found in the disconnect state (failed state
1328 		 * is also treated as the disconnected state).
1329 		 * If port activation is successful and a device is found
1330 		 * attached to the port, the initialization sequence is
1331 		 * executed to probe the port and attach
1332 		 * a device structure to a port structure. The device is not
1333 		 * set in configured state (system-wise) by this operation.
1334 		 */
1335 
1336 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1337 
1338 		break;
1339 	}
1340 
1341 	case DEVCTL_AP_CONFIGURE:
1342 	{
1343 		/*
1344 		 * A port may be in an active or shutdown state.
1345 		 * If port is in a failed state, operation is aborted.
1346 		 * If a port is in a shutdown state, sata_tran_port_activate()
1347 		 * is invoked prior to any other operation.
1348 		 *
1349 		 * Onlining the device involves creating a new target node.
1350 		 * If there is an old target node present (belonging to
1351 		 * previously removed device), the operation is aborted - the
1352 		 * old node has to be released and removed before configure
1353 		 * operation is attempted.
1354 		 */
1355 
1356 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1357 
1358 		break;
1359 	}
1360 
1361 	case DEVCTL_AP_GETSTATE:
1362 
1363 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1364 
1365 		ap_state.ap_last_change = (time_t)-1;
1366 		ap_state.ap_error_code = 0;
1367 		ap_state.ap_in_transition = 0;
1368 
1369 		/* Copy the return AP-state information to the user space */
1370 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1371 			rv = EFAULT;
1372 		}
1373 		break;
1374 
1375 	case DEVCTL_AP_CONTROL:
1376 	{
1377 		/*
1378 		 * Generic devctl for hardware specific functionality
1379 		 */
1380 		sata_ioctl_data_t	ioc;
1381 
1382 		ASSERT(dcp == NULL);
1383 
1384 		/* Copy in user ioctl data first */
1385 #ifdef _MULTI_DATAMODEL
1386 		if (ddi_model_convert_from(mode & FMODELS) ==
1387 		    DDI_MODEL_ILP32) {
1388 
1389 			sata_ioctl_data_32_t	ioc32;
1390 
1391 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1392 			    sizeof (ioc32), mode) != 0) {
1393 				rv = EFAULT;
1394 				break;
1395 			}
1396 			ioc.cmd 	= (uint_t)ioc32.cmd;
1397 			ioc.port	= (uint_t)ioc32.port;
1398 			ioc.get_size	= (uint_t)ioc32.get_size;
1399 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1400 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1401 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1402 		} else
1403 #endif /* _MULTI_DATAMODEL */
1404 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1405 		    mode) != 0) {
1406 			return (EFAULT);
1407 		}
1408 
1409 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1410 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1411 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1412 
1413 		/*
1414 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1415 		 * a 32-bit number.
1416 		 */
1417 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1418 			return (EINVAL);
1419 		}
1420 		/* validate address */
1421 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1422 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1423 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1424 
1425 		/* Override address qualifier - handle cport only for now */
1426 		qual = SATA_ADDR_CPORT;
1427 
1428 		if (sata_validate_sata_address(sata_hba_inst, cport,
1429 		    pmport, qual) != 0)
1430 			return (EINVAL);
1431 
1432 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1433 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1434 		    cport_mutex);
1435 		/* Is the port locked by event processing daemon ? */
1436 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1437 			/*
1438 			 * Cannot process ioctl request now. Come back later
1439 			 */
1440 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1441 			    cport_mutex);
1442 			return (EBUSY);
1443 		}
1444 		/* Block event processing for this port */
1445 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1446 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1447 
1448 
1449 		sata_device.satadev_addr.cport = cport;
1450 		sata_device.satadev_addr.pmport = pmport;
1451 		sata_device.satadev_addr.qual = qual;
1452 		sata_device.satadev_rev = SATA_DEVICE_REV;
1453 
1454 		switch (ioc.cmd) {
1455 
1456 		case SATA_CFGA_RESET_PORT:
1457 			/*
1458 			 * There is no protection for configured device.
1459 			 */
1460 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1461 			break;
1462 
1463 		case SATA_CFGA_RESET_DEVICE:
1464 			/*
1465 			 * There is no protection for configured device.
1466 			 */
1467 			rv = sata_ioctl_reset_device(sata_hba_inst,
1468 			    &sata_device);
1469 			break;
1470 
1471 		case SATA_CFGA_RESET_ALL:
1472 			/*
1473 			 * There is no protection for configured devices.
1474 			 */
1475 			rv = sata_ioctl_reset_all(sata_hba_inst);
1476 			/*
1477 			 * We return here, because common return is for
1478 			 * a single port operation - we have already unlocked
1479 			 * all ports and no dc handle was allocated.
1480 			 */
1481 			return (rv);
1482 
1483 		case SATA_CFGA_PORT_DEACTIVATE:
1484 			/*
1485 			 * Arbitrarily unconfigure attached device, if any.
1486 			 * Even if the unconfigure fails, proceed with the
1487 			 * port deactivation.
1488 			 */
1489 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1490 
1491 			break;
1492 
1493 		case SATA_CFGA_PORT_ACTIVATE:
1494 
1495 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1496 			break;
1497 
1498 		case SATA_CFGA_PORT_SELF_TEST:
1499 
1500 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1501 			    &sata_device);
1502 			break;
1503 
1504 		case SATA_CFGA_GET_DEVICE_PATH:
1505 			if (qual == SATA_ADDR_CPORT)
1506 				sata_device.satadev_addr.qual =
1507 				    SATA_ADDR_DCPORT;
1508 			else
1509 				sata_device.satadev_addr.qual =
1510 				    SATA_ADDR_DPMPORT;
1511 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1512 			    &sata_device, &ioc, mode);
1513 			break;
1514 
1515 		case SATA_CFGA_GET_AP_TYPE:
1516 
1517 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1518 			    &sata_device, &ioc, mode);
1519 			break;
1520 
1521 		case SATA_CFGA_GET_MODEL_INFO:
1522 
1523 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1524 			    &sata_device, &ioc, mode);
1525 			break;
1526 
1527 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1528 
1529 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1530 			    &sata_device, &ioc, mode);
1531 			break;
1532 
1533 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1534 
1535 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1536 			    &sata_device, &ioc, mode);
1537 			break;
1538 
1539 		default:
1540 			rv = EINVAL;
1541 			break;
1542 
1543 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1544 
1545 		break;
1546 	}
1547 
1548 	default:
1549 	{
1550 		/*
1551 		 * If we got here, we got an IOCTL that SATA HBA Framework
1552 		 * does not recognize. Pass ioctl to HBA driver, in case
1553 		 * it could process it.
1554 		 */
1555 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1556 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1557 
1558 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1559 		    "IOCTL 0x%2x not supported in SATA framework, "
1560 		    "passthrough to HBA", cmd);
1561 
1562 		if (sata_tran->sata_tran_ioctl == NULL) {
1563 			rv = EINVAL;
1564 			break;
1565 		}
1566 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1567 		if (rval != 0) {
1568 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1569 			    "IOCTL 0x%2x failed in HBA", cmd);
1570 			rv = rval;
1571 		}
1572 		break;
1573 	}
1574 
1575 	} /* End of main IOCTL switch */
1576 
1577 	if (dcp) {
1578 		ndi_dc_freehdl(dcp);
1579 	}
1580 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1581 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1582 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1583 
1584 	return (rv);
1585 }
1586 
1587 
1588 /*
1589  * Create error retrieval sata packet
1590  *
1591  * A sata packet is allocated and set-up to contain specified error retrieval
1592  * command and appropriate dma-able data buffer.
1593  * No association with any scsi packet is made and no callback routine is
1594  * specified.
1595  *
1596  * Returns a pointer to sata packet upon successfull packet creation.
1597  * Returns NULL, if packet cannot be created.
1598  */
1599 sata_pkt_t *
1600 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1601     int pkt_type)
1602 {
1603 	sata_hba_inst_t	*sata_hba_inst;
1604 	sata_pkt_txlate_t *spx;
1605 	sata_pkt_t *spkt;
1606 	sata_drive_info_t *sdinfo;
1607 
1608 	mutex_enter(&sata_mutex);
1609 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1610 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1611 		if (SATA_DIP(sata_hba_inst) == dip)
1612 			break;
1613 	}
1614 	mutex_exit(&sata_mutex);
1615 	ASSERT(sata_hba_inst != NULL);
1616 
1617 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1618 	if (sdinfo == NULL) {
1619 		sata_log(sata_hba_inst, CE_WARN,
1620 		    "sata: error recovery request for non-attached device at "
1621 		    "cport %d", sata_device->satadev_addr.cport);
1622 		return (NULL);
1623 	}
1624 
1625 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1626 	spx->txlt_sata_hba_inst = sata_hba_inst;
1627 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1628 	spkt = sata_pkt_alloc(spx, NULL);
1629 	if (spkt == NULL) {
1630 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1631 		return (NULL);
1632 	}
1633 	/* address is needed now */
1634 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1635 
1636 	switch (pkt_type) {
1637 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1638 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1639 			return (spkt);
1640 		break;
1641 
1642 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1643 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1644 			return (spkt);
1645 		break;
1646 
1647 	default:
1648 		break;
1649 	}
1650 
1651 	sata_pkt_free(spx);
1652 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1653 	return (NULL);
1654 
1655 }
1656 
1657 
1658 /*
1659  * Free error retrieval sata packet
1660  *
1661  * Free sata packet and any associated resources allocated previously by
1662  * sata_get_error_retrieval_pkt().
1663  *
1664  * Void return.
1665  */
1666 void
1667 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1668 {
1669 	sata_pkt_txlate_t *spx =
1670 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1671 
1672 	ASSERT(sata_pkt != NULL);
1673 
1674 	sata_free_local_buffer(spx);
1675 	sata_pkt_free(spx);
1676 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1677 
1678 }
1679 
1680 /*
1681  * sata_name_child is for composing the name of the node
1682  * the format of the name is "target,0".
1683  */
1684 static int
1685 sata_name_child(dev_info_t *dip, char *name, int namelen)
1686 {
1687 	int target;
1688 
1689 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1690 	    DDI_PROP_DONTPASS, "target", -1);
1691 	if (target == -1)
1692 		return (DDI_FAILURE);
1693 	(void) snprintf(name, namelen, "%x,0", target);
1694 	return (DDI_SUCCESS);
1695 }
1696 
1697 
1698 
1699 /* ****************** SCSA required entry points *********************** */
1700 
1701 /*
1702  * Implementation of scsi tran_tgt_init.
1703  * sata_scsi_tgt_init() initializes scsi_device structure
1704  *
1705  * If successful, DDI_SUCCESS is returned.
1706  * DDI_FAILURE is returned if addressed device does not exist
1707  */
1708 
1709 static int
1710 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1711     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1712 {
1713 #ifndef __lock_lint
1714 	_NOTE(ARGUNUSED(hba_dip))
1715 	_NOTE(ARGUNUSED(tgt_dip))
1716 #endif
1717 	sata_device_t		sata_device;
1718 	sata_drive_info_t	*sdinfo;
1719 	struct sata_id		*sid;
1720 	sata_hba_inst_t		*sata_hba_inst;
1721 	char			model[SATA_ID_MODEL_LEN + 1];
1722 	char			fw[SATA_ID_FW_LEN + 1];
1723 	char			*vid, *pid;
1724 	int			i;
1725 
1726 	/*
1727 	 * Fail tran_tgt_init for .conf stub node
1728 	 */
1729 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1730 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1731 		ddi_set_name_addr(tgt_dip, NULL);
1732 		return (DDI_FAILURE);
1733 	}
1734 
1735 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1736 
1737 	/* Validate scsi device address */
1738 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1739 	    &sata_device) != 0)
1740 		return (DDI_FAILURE);
1741 
1742 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1743 	    sata_device.satadev_addr.cport)));
1744 
1745 	/* sata_device now contains a valid sata address */
1746 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1747 	if (sdinfo == NULL) {
1748 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1749 		    sata_device.satadev_addr.cport)));
1750 		return (DDI_FAILURE);
1751 	}
1752 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1753 	    sata_device.satadev_addr.cport)));
1754 
1755 	/*
1756 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1757 	 * the target disks.
1758 	 *
1759 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1760 	 * if we need to create cmdk-style devid for all the disk devices
1761 	 * attached to this controller. This property may have been set
1762 	 * from HBA driver's .conf file or by the HBA driver in its
1763 	 * attach(9F) function.
1764 	 */
1765 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1766 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1767 	    "use-cmdk-devid-format", 0) == 1)) {
1768 		/* register a legacy devid for this target node */
1769 		sata_target_devid_register(tgt_dip, sdinfo);
1770 	}
1771 
1772 
1773 	/*
1774 	 * 'Identify Device Data' does not always fit in standard SCSI
1775 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1776 	 * of information.
1777 	 */
1778 	sid = &sdinfo->satadrv_id;
1779 #ifdef	_LITTLE_ENDIAN
1780 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1781 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1782 #else	/* _LITTLE_ENDIAN */
1783 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1784 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1785 #endif	/* _LITTLE_ENDIAN */
1786 	model[SATA_ID_MODEL_LEN] = 0;
1787 	fw[SATA_ID_FW_LEN] = 0;
1788 
1789 	/* split model into into vid/pid */
1790 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1791 		if ((*pid == ' ') || (*pid == '\t'))
1792 			break;
1793 	if (i < SATA_ID_MODEL_LEN) {
1794 		vid = model;
1795 		*pid++ = 0;		/* terminate vid, establish pid */
1796 	} else {
1797 		vid = NULL;		/* vid will stay "ATA     " */
1798 		pid = model;		/* model is all pid */
1799 	}
1800 
1801 	if (vid)
1802 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1803 		    vid, strlen(vid));
1804 	if (pid)
1805 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1806 		    pid, strlen(pid));
1807 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1808 	    fw, strlen(fw));
1809 
1810 	return (DDI_SUCCESS);
1811 }
1812 
1813 /*
1814  * Implementation of scsi tran_tgt_probe.
1815  * Probe target, by calling default scsi routine scsi_hba_probe()
1816  */
1817 static int
1818 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1819 {
1820 	sata_hba_inst_t *sata_hba_inst =
1821 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1822 	int rval;
1823 	uint32_t pm_cap;
1824 
1825 	rval = scsi_hba_probe(sd, callback);
1826 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
1827 	    SATA_CAP_LOG_SENSE;
1828 
1829 	if (rval == SCSIPROBE_EXISTS) {
1830 		/*
1831 		 * Set property "pm-capable" on the target device node, so that
1832 		 * the target driver will not try to fetch scsi cycle counters
1833 		 * before enabling device power-management.
1834 		 */
1835 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1836 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
1837 			sata_log(sata_hba_inst, CE_WARN,
1838 			    "SATA device at port %d: "
1839 			    "will not be power-managed ",
1840 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1841 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1842 			    "failure updating pm-capable property"));
1843 		}
1844 	}
1845 	return (rval);
1846 }
1847 
1848 /*
1849  * Implementation of scsi tran_tgt_free.
1850  * Release all resources allocated for scsi_device
1851  */
1852 static void
1853 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1854     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1855 {
1856 #ifndef __lock_lint
1857 	_NOTE(ARGUNUSED(hba_dip))
1858 #endif
1859 	sata_device_t		sata_device;
1860 	sata_drive_info_t	*sdinfo;
1861 	sata_hba_inst_t		*sata_hba_inst;
1862 	ddi_devid_t		devid;
1863 
1864 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1865 
1866 	/* Validate scsi device address */
1867 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1868 	    &sata_device) != 0)
1869 		return;
1870 
1871 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1872 	    sata_device.satadev_addr.cport)));
1873 
1874 	/* sata_device now should contain a valid sata address */
1875 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1876 	if (sdinfo == NULL) {
1877 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1878 		    sata_device.satadev_addr.cport)));
1879 		return;
1880 	}
1881 	/*
1882 	 * We did not allocate any resources in sata_scsi_tgt_init()
1883 	 * other than few properties.
1884 	 * Free them.
1885 	 */
1886 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1887 	    sata_device.satadev_addr.cport)));
1888 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1889 
1890 	/*
1891 	 * If devid was previously created but not freed up from
1892 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1893 	 */
1894 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1895 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1896 	    "use-cmdk-devid-format", 0) == 1) &&
1897 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1898 		ddi_devid_unregister(tgt_dip);
1899 		ddi_devid_free(devid);
1900 	}
1901 }
1902 
1903 /*
1904  * Implementation of scsi tran_init_pkt
1905  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1906  *
1907  * It seems that we should always allocate pkt, even if the address is
1908  * for non-existing device - just use some default for dma_attr.
1909  * The reason is that there is no way to communicate this to a caller here.
1910  * Subsequent call to sata_scsi_start may fail appropriately.
1911  * Simply returning NULL does not seem to discourage a target driver...
1912  *
1913  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1914  */
1915 static struct scsi_pkt *
1916 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1917     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1918     int (*callback)(caddr_t), caddr_t arg)
1919 {
1920 	sata_hba_inst_t *sata_hba_inst =
1921 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1922 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1923 	sata_device_t sata_device;
1924 	sata_drive_info_t *sdinfo;
1925 	sata_pkt_txlate_t *spx;
1926 	ddi_dma_attr_t cur_dma_attr;
1927 	int rval;
1928 	boolean_t new_pkt = TRUE;
1929 
1930 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1931 
1932 	/*
1933 	 * We need to translate the address, even if it could be
1934 	 * a bogus one, for a non-existing device
1935 	 */
1936 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1937 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1938 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1939 	sata_device.satadev_rev = SATA_DEVICE_REV;
1940 
1941 	if (pkt == NULL) {
1942 		/*
1943 		 * Have to allocate a brand new scsi packet.
1944 		 * We need to operate with auto request sense enabled.
1945 		 */
1946 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1947 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1948 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1949 
1950 		if (pkt == NULL)
1951 			return (NULL);
1952 
1953 		/* Fill scsi packet structure */
1954 		pkt->pkt_comp		= (void (*)())NULL;
1955 		pkt->pkt_time		= 0;
1956 		pkt->pkt_resid		= 0;
1957 		pkt->pkt_statistics	= 0;
1958 		pkt->pkt_reason		= 0;
1959 
1960 		/*
1961 		 * pkt_hba_private will point to sata pkt txlate structure
1962 		 */
1963 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1964 		bzero(spx, sizeof (sata_pkt_txlate_t));
1965 
1966 		spx->txlt_scsi_pkt = pkt;
1967 		spx->txlt_sata_hba_inst = sata_hba_inst;
1968 
1969 		/* Allocate sata_pkt */
1970 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1971 		if (spx->txlt_sata_pkt == NULL) {
1972 			/* Could not allocate sata pkt */
1973 			scsi_hba_pkt_free(ap, pkt);
1974 			return (NULL);
1975 		}
1976 		/* Set sata address */
1977 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1978 		    sata_device.satadev_addr;
1979 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1980 		    sata_device.satadev_rev;
1981 
1982 		if ((bp == NULL) || (bp->b_bcount == 0))
1983 			return (pkt);
1984 
1985 		spx->txlt_total_residue = bp->b_bcount;
1986 	} else {
1987 		new_pkt = FALSE;
1988 		/*
1989 		 * Packet was preallocated/initialized by previous call
1990 		 */
1991 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1992 
1993 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1994 			return (pkt);
1995 		}
1996 
1997 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1998 	}
1999 
2000 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2001 
2002 	/*
2003 	 * We use an adjusted version of the dma_attr, to account
2004 	 * for device addressing limitations.
2005 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2006 	 * happen when a device is not yet configured.
2007 	 */
2008 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2009 	    sata_device.satadev_addr.cport)));
2010 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2011 	    &spx->txlt_sata_pkt->satapkt_device);
2012 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2013 	sata_adjust_dma_attr(sdinfo,
2014 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2015 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2016 	    sata_device.satadev_addr.cport)));
2017 	/*
2018 	 * Allocate necessary DMA resources for the packet's data buffer
2019 	 * NOTE:
2020 	 * In case of read/write commands, DMA resource allocation here is
2021 	 * based on the premise that the transfer length specified in
2022 	 * the read/write scsi cdb will match exactly DMA resources -
2023 	 * returning correct packet residue is crucial.
2024 	 */
2025 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2026 	    &cur_dma_attr)) != DDI_SUCCESS) {
2027 		/*
2028 		 * If a DMA allocation request fails with
2029 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2030 		 * bioerror(9F) with bp and an error code of EFAULT.
2031 		 * If a DMA allocation request fails with
2032 		 * DDI_DMA_TOOBIG, indicate the error by calling
2033 		 * bioerror(9F) with bp and an error code of EINVAL.
2034 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2035 		 * Request may be repeated later - there is no real error.
2036 		 */
2037 		switch (rval) {
2038 		case DDI_DMA_NORESOURCES:
2039 			bioerror(bp, 0);
2040 			break;
2041 		case DDI_DMA_NOMAPPING:
2042 		case DDI_DMA_BADATTR:
2043 			bioerror(bp, EFAULT);
2044 			break;
2045 		case DDI_DMA_TOOBIG:
2046 		default:
2047 			bioerror(bp, EINVAL);
2048 			break;
2049 		}
2050 		if (new_pkt == TRUE) {
2051 			/*
2052 			 * Since this is a new packet, we can clean-up
2053 			 * everything
2054 			 */
2055 			sata_scsi_destroy_pkt(ap, pkt);
2056 		} else {
2057 			/*
2058 			 * This is a re-used packet. It will be target driver's
2059 			 * responsibility to eventually destroy it (which
2060 			 * will free allocated resources).
2061 			 * Here, we just "complete" the request, leaving
2062 			 * allocated resources intact, so the request may
2063 			 * be retried.
2064 			 */
2065 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2066 			sata_pkt_free(spx);
2067 		}
2068 		return (NULL);
2069 	}
2070 	/* Set number of bytes that are not yet accounted for */
2071 	pkt->pkt_resid = spx->txlt_total_residue;
2072 	ASSERT(pkt->pkt_resid >= 0);
2073 
2074 	return (pkt);
2075 }
2076 
2077 /*
2078  * Implementation of scsi tran_start.
2079  * Translate scsi cmd into sata operation and return status.
2080  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2081  * are supported.
2082  * For SATA hard disks, supported scsi commands:
2083  * SCMD_INQUIRY
2084  * SCMD_TEST_UNIT_READY
2085  * SCMD_START_STOP
2086  * SCMD_READ_CAPACITY
2087  * SCMD_REQUEST_SENSE
2088  * SCMD_LOG_SENSE_G1
2089  * SCMD_LOG_SELECT_G1
2090  * SCMD_MODE_SENSE	(specific pages)
2091  * SCMD_MODE_SENSE_G1	(specific pages)
2092  * SCMD_MODE_SELECT	(specific pages)
2093  * SCMD_MODE_SELECT_G1	(specific pages)
2094  * SCMD_SYNCHRONIZE_CACHE
2095  * SCMD_SYNCHRONIZE_CACHE_G1
2096  * SCMD_READ
2097  * SCMD_READ_G1
2098  * SCMD_READ_G4
2099  * SCMD_READ_G5
2100  * SCMD_WRITE
2101  * SCMD_WRITE_BUFFER
2102  * SCMD_WRITE_G1
2103  * SCMD_WRITE_G4
2104  * SCMD_WRITE_G5
2105  * SCMD_SEEK		(noop)
2106  * SCMD_SDIAG
2107  *
2108  * All other commands are rejected as unsupported.
2109  *
2110  * Returns:
2111  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2112  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2113  * a callback could be scheduled.
2114  * TRAN_BADPKT if cmd was directed to invalid address.
2115  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2116  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2117  * was removed and there was no callback specified in scsi pkt.
2118  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2119  * framework was busy performing some other operation(s).
2120  *
2121  */
2122 static int
2123 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2124 {
2125 	sata_hba_inst_t *sata_hba_inst =
2126 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2127 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2128 	sata_drive_info_t *sdinfo;
2129 	struct buf *bp;
2130 	int cport;
2131 	int rval;
2132 
2133 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2134 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2135 
2136 	ASSERT(spx != NULL &&
2137 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2138 
2139 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2140 
2141 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2142 	sdinfo = sata_get_device_info(sata_hba_inst,
2143 	    &spx->txlt_sata_pkt->satapkt_device);
2144 	if (sdinfo == NULL ||
2145 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2146 	    B_FALSE ||
2147 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2148 
2149 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2150 		pkt->pkt_reason = CMD_DEV_GONE;
2151 		/*
2152 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2153 		 * only in callback function (for normal requests) and
2154 		 * in the dump code path.
2155 		 * So, if the callback is available, we need to do
2156 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2157 		 */
2158 		if (pkt->pkt_comp != NULL) {
2159 			/* scsi callback required */
2160 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2161 			    (task_func_t *)pkt->pkt_comp,
2162 			    (void *)pkt, TQ_SLEEP) == NULL)
2163 				/* Scheduling the callback failed */
2164 				return (TRAN_BUSY);
2165 			return (TRAN_ACCEPT);
2166 		}
2167 		/* No callback available */
2168 		return (TRAN_FATAL_ERROR);
2169 	}
2170 
2171 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2172 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2173 		rval = sata_txlt_atapi(spx);
2174 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2175 		    "sata_scsi_start atapi: rval %d\n", rval);
2176 		return (rval);
2177 	}
2178 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2179 
2180 	/*
2181 	 * Checking for power state, if it was on
2182 	 * STOPPED state, then the drive is not capable
2183 	 * of processing media access command.  And
2184 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2185 	 * in the function for different power state.
2186 	 */
2187 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2188 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2189 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2190 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2191 		    SD_SCSI_ASC_LU_NOT_READY));
2192 	}
2193 
2194 	/* ATA Disk commands processing starts here */
2195 
2196 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2197 
2198 	switch (pkt->pkt_cdbp[0]) {
2199 
2200 	case SCMD_INQUIRY:
2201 		/* Mapped to identify device */
2202 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2203 			bp_mapin(bp);
2204 		rval = sata_txlt_inquiry(spx);
2205 		break;
2206 
2207 	case SCMD_TEST_UNIT_READY:
2208 		/*
2209 		 * SAT "SATA to ATA Translation" doc specifies translation
2210 		 * to ATA CHECK POWER MODE.
2211 		 */
2212 		rval = sata_txlt_test_unit_ready(spx);
2213 		break;
2214 
2215 	case SCMD_START_STOP:
2216 		/* Mapping depends on the command */
2217 		rval = sata_txlt_start_stop_unit(spx);
2218 		break;
2219 
2220 	case SCMD_READ_CAPACITY:
2221 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2222 			bp_mapin(bp);
2223 		rval = sata_txlt_read_capacity(spx);
2224 		break;
2225 
2226 	case SCMD_REQUEST_SENSE:
2227 		/*
2228 		 * Always No Sense, since we force ARQ
2229 		 */
2230 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2231 			bp_mapin(bp);
2232 		rval = sata_txlt_request_sense(spx);
2233 		break;
2234 
2235 	case SCMD_LOG_SENSE_G1:
2236 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2237 			bp_mapin(bp);
2238 		rval = sata_txlt_log_sense(spx);
2239 		break;
2240 
2241 	case SCMD_LOG_SELECT_G1:
2242 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2243 			bp_mapin(bp);
2244 		rval = sata_txlt_log_select(spx);
2245 		break;
2246 
2247 	case SCMD_MODE_SENSE:
2248 	case SCMD_MODE_SENSE_G1:
2249 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2250 			bp_mapin(bp);
2251 		rval = sata_txlt_mode_sense(spx);
2252 		break;
2253 
2254 
2255 	case SCMD_MODE_SELECT:
2256 	case SCMD_MODE_SELECT_G1:
2257 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2258 			bp_mapin(bp);
2259 		rval = sata_txlt_mode_select(spx);
2260 		break;
2261 
2262 	case SCMD_SYNCHRONIZE_CACHE:
2263 	case SCMD_SYNCHRONIZE_CACHE_G1:
2264 		rval = sata_txlt_synchronize_cache(spx);
2265 		break;
2266 
2267 	case SCMD_READ:
2268 	case SCMD_READ_G1:
2269 	case SCMD_READ_G4:
2270 	case SCMD_READ_G5:
2271 		rval = sata_txlt_read(spx);
2272 		break;
2273 	case SCMD_WRITE_BUFFER:
2274 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2275 			bp_mapin(bp);
2276 		rval = sata_txlt_write_buffer(spx);
2277 		break;
2278 
2279 	case SCMD_WRITE:
2280 	case SCMD_WRITE_G1:
2281 	case SCMD_WRITE_G4:
2282 	case SCMD_WRITE_G5:
2283 		rval = sata_txlt_write(spx);
2284 		break;
2285 
2286 	case SCMD_SEEK:
2287 		rval = sata_txlt_nodata_cmd_immediate(spx);
2288 		break;
2289 
2290 		/* Other cases will be filed later */
2291 		/* postponed until phase 2 of the development */
2292 	default:
2293 		rval = sata_txlt_invalid_command(spx);
2294 		break;
2295 	}
2296 
2297 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2298 	    "sata_scsi_start: rval %d\n", rval);
2299 
2300 	return (rval);
2301 }
2302 
2303 /*
2304  * Implementation of scsi tran_abort.
2305  * Abort specific pkt or all packets.
2306  *
2307  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2308  *
2309  * May be called from an interrupt level.
2310  */
2311 static int
2312 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2313 {
2314 	sata_hba_inst_t *sata_hba_inst =
2315 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2316 	sata_device_t	sata_device;
2317 	sata_pkt_t	*sata_pkt;
2318 
2319 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2320 	    "sata_scsi_abort: %s at target: 0x%x\n",
2321 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2322 
2323 	/* Validate address */
2324 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2325 		/* Invalid address */
2326 		return (0);
2327 
2328 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2329 	    sata_device.satadev_addr.cport)));
2330 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2331 		/* invalid address */
2332 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2333 		    sata_device.satadev_addr.cport)));
2334 		return (0);
2335 	}
2336 	if (scsi_pkt == NULL) {
2337 		/*
2338 		 * Abort all packets.
2339 		 * Although we do not have specific packet, we still need
2340 		 * dummy packet structure to pass device address to HBA.
2341 		 * Allocate one, without sleeping. Fail if pkt cannot be
2342 		 * allocated.
2343 		 */
2344 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2345 		if (sata_pkt == NULL) {
2346 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2347 			    sata_device.satadev_addr.cport)));
2348 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2349 			    "could not allocate sata_pkt"));
2350 			return (0);
2351 		}
2352 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2353 		sata_pkt->satapkt_device = sata_device;
2354 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2355 	} else {
2356 		if (scsi_pkt->pkt_ha_private == NULL) {
2357 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2358 			    sata_device.satadev_addr.cport)));
2359 			return (0); /* Bad scsi pkt */
2360 		}
2361 		/* extract pointer to sata pkt */
2362 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2363 		    txlt_sata_pkt;
2364 	}
2365 
2366 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2367 	    sata_device.satadev_addr.cport)));
2368 	/* Send abort request to HBA */
2369 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2370 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2371 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2372 	    SATA_SUCCESS) {
2373 		if (scsi_pkt == NULL)
2374 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2375 		/* Success */
2376 		return (1);
2377 	}
2378 	/* Else, something did not go right */
2379 	if (scsi_pkt == NULL)
2380 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2381 	/* Failure */
2382 	return (0);
2383 }
2384 
2385 
2386 /*
2387  * Implementation of scsi tran_reset.
2388  * RESET_ALL request is translated into port reset.
2389  * RESET_TARGET requests is translated into a device reset,
2390  * RESET_LUN request is accepted only for LUN 0 and translated into
2391  * device reset.
2392  * The target reset should cause all HBA active and queued packets to
2393  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2394  * the return. HBA should report reset event for the device.
2395  *
2396  * Returns 1 upon success, 0 upon failure.
2397  */
2398 static int
2399 sata_scsi_reset(struct scsi_address *ap, int level)
2400 {
2401 	sata_hba_inst_t	*sata_hba_inst =
2402 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2403 	sata_device_t	sata_device;
2404 	int		val;
2405 
2406 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2407 	    "sata_scsi_reset: level %d target: 0x%x\n",
2408 	    level, ap->a_target);
2409 
2410 	/* Validate address */
2411 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2412 	if (val == -1)
2413 		/* Invalid address */
2414 		return (0);
2415 
2416 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2417 	    sata_device.satadev_addr.cport)));
2418 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2419 		/* invalid address */
2420 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2421 		    sata_device.satadev_addr.cport)));
2422 		return (0);
2423 	}
2424 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2425 	    sata_device.satadev_addr.cport)));
2426 	if (level == RESET_ALL) {
2427 		/* port reset - cport only */
2428 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2429 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2430 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2431 			return (1);
2432 		else
2433 			return (0);
2434 
2435 	} else if (val == 0 &&
2436 	    (level == RESET_TARGET || level == RESET_LUN)) {
2437 		/* reset device (device attached) */
2438 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2439 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2440 			return (1);
2441 		else
2442 			return (0);
2443 	}
2444 	return (0);
2445 }
2446 
2447 
2448 /*
2449  * Implementation of scsi tran_getcap (get transport/device capabilities).
2450  * Supported capabilities for SATA hard disks:
2451  * auto-rqsense		(always supported)
2452  * tagged-qing		(supported if HBA supports it)
2453  * untagged-qing	(could be supported if disk supports it, but because
2454  *			 caching behavior allowing untagged queuing actually
2455  *			 results in reduced performance.  sd tries to throttle
2456  *			 back to only 3 outstanding commands, which may
2457  *			 work for real SCSI disks, but with read ahead
2458  *			 caching, having more than 1 outstanding command
2459  *			 results in cache thrashing.)
2460  * sector_size
2461  * dma_max
2462  * interconnect-type	(INTERCONNECT_SATA)
2463  *
2464  * Supported capabilities for ATAPI CD/DVD devices:
2465  * auto-rqsense		(always supported)
2466  * sector_size
2467  * dma_max
2468  * max-cdb-length
2469  * interconnect-type	(INTERCONNECT_SATA)
2470  *
2471  * Supported capabilities for ATAPI TAPE devices:
2472  * auto-rqsense		(always supported)
2473  * dma_max
2474  * max-cdb-length
2475  *
2476  * Supported capabilities for SATA ATAPI hard disks:
2477  * auto-rqsense		(always supported)
2478  * interconnect-type	(INTERCONNECT_SATA)
2479  * max-cdb-length
2480  *
2481  * Request for other capabilities is rejected as unsupported.
2482  *
2483  * Returns supported capability value, or -1 if capability is unsuppported or
2484  * the address is invalid - no device.
2485  */
2486 
2487 static int
2488 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2489 {
2490 
2491 	sata_hba_inst_t 	*sata_hba_inst =
2492 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2493 	sata_device_t		sata_device;
2494 	sata_drive_info_t	*sdinfo;
2495 	ddi_dma_attr_t		adj_dma_attr;
2496 	int 			rval;
2497 
2498 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2499 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2500 	    ap->a_target, cap);
2501 
2502 	/*
2503 	 * We want to process the capabilities on per port granularity.
2504 	 * So, we are specifically restricting ourselves to whom != 0
2505 	 * to exclude the controller wide handling.
2506 	 */
2507 	if (cap == NULL || whom == 0)
2508 		return (-1);
2509 
2510 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2511 		/* Invalid address */
2512 		return (-1);
2513 	}
2514 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2515 	    sata_device.satadev_addr.cport)));
2516 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2517 	    NULL) {
2518 		/* invalid address */
2519 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2520 		    sata_device.satadev_addr.cport)));
2521 		return (-1);
2522 	}
2523 
2524 	switch (scsi_hba_lookup_capstr(cap)) {
2525 	case SCSI_CAP_ARQ:
2526 		rval = 1;		/* ARQ supported, turned on */
2527 		break;
2528 
2529 	case SCSI_CAP_SECTOR_SIZE:
2530 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2531 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2532 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2533 			rval = SATA_ATAPI_SECTOR_SIZE;
2534 		else rval = -1;
2535 		break;
2536 
2537 	/*
2538 	 * untagged queuing cause a performance inversion because of
2539 	 * the way sd operates.  Because of this reason we do not
2540 	 * use it when available.
2541 	 */
2542 	case SCSI_CAP_UNTAGGED_QING:
2543 		if (sdinfo->satadrv_features_enabled &
2544 		    SATA_DEV_F_E_UNTAGGED_QING)
2545 			rval = 1;	/* Untagged queuing available */
2546 		else
2547 			rval = -1;	/* Untagged queuing not available */
2548 		break;
2549 
2550 	case SCSI_CAP_TAGGED_QING:
2551 		if ((sdinfo->satadrv_features_enabled &
2552 		    SATA_DEV_F_E_TAGGED_QING) &&
2553 		    (sdinfo->satadrv_max_queue_depth > 1))
2554 			rval = 1;	/* Tagged queuing available */
2555 		else
2556 			rval = -1;	/* Tagged queuing not available */
2557 		break;
2558 
2559 	case SCSI_CAP_DMA_MAX:
2560 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2561 		    &adj_dma_attr);
2562 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2563 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2564 		break;
2565 
2566 	case SCSI_CAP_INTERCONNECT_TYPE:
2567 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2568 		break;
2569 
2570 	case SCSI_CAP_CDB_LEN:
2571 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2572 			rval = sdinfo->satadrv_atapi_cdb_len;
2573 		else
2574 			rval = -1;
2575 		break;
2576 
2577 	default:
2578 		rval = -1;
2579 		break;
2580 	}
2581 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2582 	    sata_device.satadev_addr.cport)));
2583 	return (rval);
2584 }
2585 
2586 /*
2587  * Implementation of scsi tran_setcap
2588  *
2589  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2590  *
2591  */
2592 static int
2593 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2594 {
2595 	sata_hba_inst_t	*sata_hba_inst =
2596 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2597 	sata_device_t	sata_device;
2598 	sata_drive_info_t	*sdinfo;
2599 	int		rval;
2600 
2601 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2602 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2603 
2604 	/*
2605 	 * We want to process the capabilities on per port granularity.
2606 	 * So, we are specifically restricting ourselves to whom != 0
2607 	 * to exclude the controller wide handling.
2608 	 */
2609 	if (cap == NULL || whom == 0) {
2610 		return (-1);
2611 	}
2612 
2613 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2614 		/* Invalid address */
2615 		return (-1);
2616 	}
2617 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2618 	    sata_device.satadev_addr.cport)));
2619 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2620 	    &sata_device)) == NULL) {
2621 		/* invalid address */
2622 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2623 		    sata_device.satadev_addr.cport)));
2624 		return (-1);
2625 	}
2626 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2627 	    sata_device.satadev_addr.cport)));
2628 
2629 	switch (scsi_hba_lookup_capstr(cap)) {
2630 	case SCSI_CAP_ARQ:
2631 	case SCSI_CAP_SECTOR_SIZE:
2632 	case SCSI_CAP_DMA_MAX:
2633 	case SCSI_CAP_INTERCONNECT_TYPE:
2634 		rval = 0;
2635 		break;
2636 	case SCSI_CAP_UNTAGGED_QING:
2637 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2638 			rval = 1;
2639 			if (value == 1) {
2640 				sdinfo->satadrv_features_enabled |=
2641 				    SATA_DEV_F_E_UNTAGGED_QING;
2642 			} else if (value == 0) {
2643 				sdinfo->satadrv_features_enabled &=
2644 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2645 			} else {
2646 				rval = -1;
2647 			}
2648 		} else {
2649 			rval = 0;
2650 		}
2651 		break;
2652 	case SCSI_CAP_TAGGED_QING:
2653 		/* This can TCQ or NCQ */
2654 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2655 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2656 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2657 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2658 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2659 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2660 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2661 			rval = 1;
2662 			if (value == 1) {
2663 				sdinfo->satadrv_features_enabled |=
2664 				    SATA_DEV_F_E_TAGGED_QING;
2665 			} else if (value == 0) {
2666 				sdinfo->satadrv_features_enabled &=
2667 				    ~SATA_DEV_F_E_TAGGED_QING;
2668 			} else {
2669 				rval = -1;
2670 			}
2671 		} else {
2672 			rval = 0;
2673 		}
2674 		break;
2675 	default:
2676 		rval = -1;
2677 		break;
2678 	}
2679 	return (rval);
2680 }
2681 
2682 /*
2683  * Implementations of scsi tran_destroy_pkt.
2684  * Free resources allocated by sata_scsi_init_pkt()
2685  */
2686 static void
2687 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2688 {
2689 	sata_pkt_txlate_t *spx;
2690 
2691 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2692 
2693 	sata_common_free_dma_rsrcs(spx);
2694 
2695 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2696 	sata_pkt_free(spx);
2697 
2698 	scsi_hba_pkt_free(ap, pkt);
2699 }
2700 
2701 /*
2702  * Implementation of scsi tran_dmafree.
2703  * Free DMA resources allocated by sata_scsi_init_pkt()
2704  */
2705 
2706 static void
2707 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2708 {
2709 #ifndef __lock_lint
2710 	_NOTE(ARGUNUSED(ap))
2711 #endif
2712 	sata_pkt_txlate_t *spx;
2713 
2714 	ASSERT(pkt != NULL);
2715 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2716 
2717 	sata_common_free_dma_rsrcs(spx);
2718 }
2719 
2720 /*
2721  * Implementation of scsi tran_sync_pkt.
2722  *
2723  * The assumption below is that pkt is unique - there is no need to check ap
2724  *
2725  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2726  * into/from the real buffer.
2727  */
2728 static void
2729 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2730 {
2731 #ifndef __lock_lint
2732 	_NOTE(ARGUNUSED(ap))
2733 #endif
2734 	int rval;
2735 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2736 	struct buf *bp;
2737 	int direction;
2738 
2739 	ASSERT(spx != NULL);
2740 	if (spx->txlt_buf_dma_handle != NULL) {
2741 		direction = spx->txlt_sata_pkt->
2742 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2743 		if (spx->txlt_sata_pkt != NULL &&
2744 		    direction != SATA_DIR_NODATA_XFER) {
2745 			if (spx->txlt_tmp_buf != NULL) {
2746 				/* Intermediate DMA buffer used */
2747 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2748 
2749 				if (direction & SATA_DIR_WRITE) {
2750 					bcopy(bp->b_un.b_addr,
2751 					    spx->txlt_tmp_buf, bp->b_bcount);
2752 				}
2753 			}
2754 			/* Sync the buffer for device or for CPU */
2755 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2756 			    (direction & SATA_DIR_WRITE) ?
2757 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2758 			ASSERT(rval == DDI_SUCCESS);
2759 			if (spx->txlt_tmp_buf != NULL &&
2760 			    !(direction & SATA_DIR_WRITE)) {
2761 				/* Intermediate DMA buffer used for read */
2762 				bcopy(spx->txlt_tmp_buf,
2763 				    bp->b_un.b_addr, bp->b_bcount);
2764 			}
2765 
2766 		}
2767 	}
2768 }
2769 
2770 
2771 
2772 /* *******************  SATA - SCSI Translation functions **************** */
2773 /*
2774  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2775  * translation.
2776  */
2777 
2778 /*
2779  * Checks if a device exists and can be access and translates common
2780  * scsi_pkt data to sata_pkt data.
2781  *
2782  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2783  * sata_pkt was set-up.
2784  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2785  * exist and pkt_comp callback was scheduled.
2786  * Returns other TRAN_XXXXX values when error occured and command should be
2787  * rejected with the returned TRAN_XXXXX value.
2788  *
2789  * This function should be called with port mutex held.
2790  */
2791 static int
2792 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2793 {
2794 	sata_drive_info_t *sdinfo;
2795 	sata_device_t sata_device;
2796 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2797 		SATA_DIR_NODATA_XFER,
2798 		/* all other values to 0/FALSE */
2799 	};
2800 	/*
2801 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2802 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2803 	 * indicates that the scsi packet was not accepted (the reason will not
2804 	 * be checked by the scsi target driver).
2805 	 * To make debugging easier, we set pkt_reason to know value here.
2806 	 * It may be changed later when different completion reason is
2807 	 * determined.
2808 	 */
2809 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2810 	*reason = CMD_TRAN_ERR;
2811 
2812 	/* Validate address */
2813 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2814 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2815 
2816 	case -1:
2817 		/* Invalid address or invalid device type */
2818 		return (TRAN_BADPKT);
2819 	case 1:
2820 		/* valid address but no device - it has disappeared ? */
2821 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2822 		*reason = CMD_DEV_GONE;
2823 		/*
2824 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2825 		 * only in callback function (for normal requests) and
2826 		 * in the dump code path.
2827 		 * So, if the callback is available, we need to do
2828 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2829 		 */
2830 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2831 			/* scsi callback required */
2832 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2833 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2834 			    (void *)spx->txlt_scsi_pkt,
2835 			    TQ_SLEEP) == NULL)
2836 				/* Scheduling the callback failed */
2837 				return (TRAN_BUSY);
2838 
2839 			return (TRAN_ACCEPT);
2840 		}
2841 		return (TRAN_FATAL_ERROR);
2842 	default:
2843 		/* all OK; pkt reason will be overwritten later */
2844 		break;
2845 	}
2846 	/*
2847 	 * If in an interrupt context, reject packet if it is to be
2848 	 * executed in polling mode
2849 	 */
2850 	if (servicing_interrupt() &&
2851 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2852 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
2853 		    "sata_scsi_start: rejecting synchronous command because "
2854 		    "of interrupt context\n", NULL);
2855 		return (TRAN_BUSY);
2856 	}
2857 
2858 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2859 	    &spx->txlt_sata_pkt->satapkt_device);
2860 
2861 	/*
2862 	 * If device is in reset condition, reject the packet with
2863 	 * TRAN_BUSY, unless:
2864 	 * 1. system is panicking (dumping)
2865 	 * In such case only one thread is running and there is no way to
2866 	 * process reset.
2867 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2868 	 * Some cfgadm operations involve drive commands, so reset condition
2869 	 * needs to be ignored for IOCTL operations.
2870 	 */
2871 	if ((sdinfo->satadrv_event_flags &
2872 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2873 
2874 		if (!ddi_in_panic() &&
2875 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2876 		    sata_device.satadev_addr.cport) &
2877 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2878 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2879 			*reason = CMD_INCOMPLETE;
2880 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2881 			    "sata_scsi_start: rejecting command because "
2882 			    "of device reset state\n", NULL);
2883 			return (TRAN_BUSY);
2884 		}
2885 	}
2886 
2887 	/*
2888 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2889 	 * sata_scsi_pkt_init() because pkt init had to work also with
2890 	 * non-existing devices.
2891 	 * Now we know that the packet was set-up for a real device, so its
2892 	 * type is known.
2893 	 */
2894 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2895 
2896 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2897 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2898 	    sata_device.satadev_addr.cport)->cport_event_flags &
2899 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2900 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2901 		    sata_ignore_dev_reset = B_TRUE;
2902 	}
2903 	/*
2904 	 * At this point the generic translation routine determined that the
2905 	 * scsi packet should be accepted. Packet completion reason may be
2906 	 * changed later when a different completion reason is determined.
2907 	 */
2908 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2909 	*reason = CMD_CMPLT;
2910 
2911 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2912 		/* Synchronous execution */
2913 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2914 		    SATA_OPMODE_POLLING;
2915 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2916 		    sata_ignore_dev_reset = ddi_in_panic();
2917 	} else {
2918 		/* Asynchronous execution */
2919 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2920 		    SATA_OPMODE_INTERRUPTS;
2921 	}
2922 	/* Convert queuing information */
2923 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2924 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2925 		    B_TRUE;
2926 	else if (spx->txlt_scsi_pkt->pkt_flags &
2927 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2928 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2929 		    B_TRUE;
2930 
2931 	/* Always limit pkt time */
2932 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2933 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2934 	else
2935 		/* Pass on scsi_pkt time */
2936 		spx->txlt_sata_pkt->satapkt_time =
2937 		    spx->txlt_scsi_pkt->pkt_time;
2938 
2939 	return (TRAN_ACCEPT);
2940 }
2941 
2942 
2943 /*
2944  * Translate ATA Identify Device data to SCSI Inquiry data.
2945  * This function may be called only for ATA devices.
2946  * This function should not be called for ATAPI devices - they
2947  * respond directly to SCSI Inquiry command.
2948  *
2949  * SATA Identify Device data has to be valid in sata_drive_info.
2950  * Buffer has to accomodate the inquiry length (36 bytes).
2951  *
2952  * This function should be called with a port mutex held.
2953  */
2954 static	void
2955 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2956     sata_drive_info_t *sdinfo, uint8_t *buf)
2957 {
2958 
2959 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2960 	struct sata_id *sid = &sdinfo->satadrv_id;
2961 
2962 	/* Start with a nice clean slate */
2963 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2964 
2965 	/*
2966 	 * Rely on the dev_type for setting paripheral qualifier.
2967 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2968 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2969 	 * ATAPI Inquiry may provide more data to the target driver.
2970 	 */
2971 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2972 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2973 
2974 	/* CFA type device is not a removable media device */
2975 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
2976 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
2977 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2978 	inq->inq_iso = 0;	/* ISO version */
2979 	inq->inq_ecma = 0;	/* ECMA version */
2980 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2981 	inq->inq_aenc = 0;	/* Async event notification cap. */
2982 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2983 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2984 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2985 	inq->inq_len = 31;	/* Additional length */
2986 	inq->inq_dualp = 0;	/* dual port device - NO */
2987 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2988 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2989 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2990 				/*
2991 				 * Queuing support - controller has to
2992 				 * support some sort of command queuing.
2993 				 */
2994 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2995 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2996 	else
2997 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2998 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2999 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3000 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3001 
3002 #ifdef	_LITTLE_ENDIAN
3003 	/* Swap text fields to match SCSI format */
3004 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3005 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3006 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3007 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3008 	else
3009 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3010 #else	/* _LITTLE_ENDIAN */
3011 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3012 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3013 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3014 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3015 	else
3016 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3017 #endif	/* _LITTLE_ENDIAN */
3018 }
3019 
3020 
3021 /*
3022  * Scsi response set up for invalid command (command not supported)
3023  *
3024  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3025  */
3026 static int
3027 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3028 {
3029 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3030 	struct scsi_extended_sense *sense;
3031 
3032 	scsipkt->pkt_reason = CMD_CMPLT;
3033 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3034 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3035 
3036 	*scsipkt->pkt_scbp = STATUS_CHECK;
3037 
3038 	sense = sata_arq_sense(spx);
3039 	sense->es_key = KEY_ILLEGAL_REQUEST;
3040 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3041 
3042 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3043 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3044 
3045 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3046 	    scsipkt->pkt_comp != NULL)
3047 		/* scsi callback required */
3048 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3049 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3050 		    (void *)spx->txlt_scsi_pkt,
3051 		    TQ_SLEEP) == NULL)
3052 			/* Scheduling the callback failed */
3053 			return (TRAN_BUSY);
3054 	return (TRAN_ACCEPT);
3055 }
3056 
3057 /*
3058  * Scsi response set up for check condition with special sense key
3059  * and additional sense code.
3060  *
3061  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3062  */
3063 static int
3064 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3065 {
3066 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3067 	int cport = SATA_TXLT_CPORT(spx);
3068 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3069 	struct scsi_extended_sense *sense;
3070 
3071 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3072 	scsipkt->pkt_reason = CMD_CMPLT;
3073 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3074 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3075 
3076 	*scsipkt->pkt_scbp = STATUS_CHECK;
3077 
3078 	sense = sata_arq_sense(spx);
3079 	sense->es_key = key;
3080 	sense->es_add_code = code;
3081 
3082 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3083 
3084 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3085 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3086 
3087 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3088 		/* scsi callback required */
3089 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3090 		    (task_func_t *)scsi_hba_pkt_comp,
3091 		    (void *)spx->txlt_scsi_pkt,
3092 		    TQ_SLEEP) == NULL)
3093 			/* Scheduling the callback failed */
3094 			return (TRAN_BUSY);
3095 	return (TRAN_ACCEPT);
3096 }
3097 
3098 /*
3099  * Scsi response setup for
3100  * emulated non-data command that requires no action/return data
3101  *
3102  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3103  */
3104 static	int
3105 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3106 {
3107 	int rval;
3108 	int reason;
3109 
3110 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3111 
3112 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3113 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3114 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3115 		return (rval);
3116 	}
3117 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3118 
3119 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3120 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3121 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3122 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3123 
3124 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3125 	    "Scsi_pkt completion reason %x\n",
3126 	    spx->txlt_scsi_pkt->pkt_reason);
3127 
3128 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3129 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3130 		/* scsi callback required */
3131 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3132 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3133 		    (void *)spx->txlt_scsi_pkt,
3134 		    TQ_SLEEP) == NULL)
3135 			/* Scheduling the callback failed */
3136 			return (TRAN_BUSY);
3137 	return (TRAN_ACCEPT);
3138 }
3139 
3140 
3141 /*
3142  * SATA translate command: Inquiry / Identify Device
3143  * Use cached Identify Device data for now, rather than issuing actual
3144  * Device Identify cmd request. If device is detached and re-attached,
3145  * asynchromous event processing should fetch and refresh Identify Device
3146  * data.
3147  * Two VPD pages are supported now:
3148  * Vital Product Data page
3149  * Unit Serial Number page
3150  *
3151  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3152  */
3153 
3154 #define	EVPD			1	/* Extended Vital Product Data flag */
3155 #define	CMDDT			2	/* Command Support Data - Obsolete */
3156 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3157 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3158 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3159 
3160 static int
3161 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3162 {
3163 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3164 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3165 	sata_drive_info_t *sdinfo;
3166 	struct scsi_extended_sense *sense;
3167 	int count;
3168 	uint8_t *p;
3169 	int i, j;
3170 	uint8_t page_buf[0xff]; /* Max length */
3171 	int rval, reason;
3172 
3173 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3174 
3175 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3176 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3177 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3178 		return (rval);
3179 	}
3180 
3181 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3182 	    &spx->txlt_sata_pkt->satapkt_device);
3183 
3184 	ASSERT(sdinfo != NULL);
3185 
3186 	scsipkt->pkt_reason = CMD_CMPLT;
3187 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3188 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3189 
3190 	/* Reject not supported request */
3191 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3192 		*scsipkt->pkt_scbp = STATUS_CHECK;
3193 		sense = sata_arq_sense(spx);
3194 		sense->es_key = KEY_ILLEGAL_REQUEST;
3195 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3196 		goto done;
3197 	}
3198 
3199 	/* Valid Inquiry request */
3200 	*scsipkt->pkt_scbp = STATUS_GOOD;
3201 
3202 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3203 
3204 		/*
3205 		 * Because it is fully emulated command storing data
3206 		 * programatically in the specified buffer, release
3207 		 * preallocated DMA resources before storing data in the buffer,
3208 		 * so no unwanted DMA sync would take place.
3209 		 */
3210 		sata_scsi_dmafree(NULL, scsipkt);
3211 
3212 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3213 			/* Standard Inquiry Data request */
3214 			struct scsi_inquiry inq;
3215 			unsigned int bufsize;
3216 
3217 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3218 			    sdinfo, (uint8_t *)&inq);
3219 			/* Copy no more than requested */
3220 			count = MIN(bp->b_bcount,
3221 			    sizeof (struct scsi_inquiry));
3222 			bufsize = scsipkt->pkt_cdbp[4];
3223 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3224 			count = MIN(count, bufsize);
3225 			bcopy(&inq, bp->b_un.b_addr, count);
3226 
3227 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3228 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3229 			    bufsize - count : 0;
3230 		} else {
3231 			/*
3232 			 * peripheral_qualifier = 0;
3233 			 *
3234 			 * We are dealing only with HD and will be
3235 			 * dealing with CD/DVD devices soon
3236 			 */
3237 			uint8_t peripheral_device_type =
3238 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3239 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3240 
3241 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3242 			case INQUIRY_SUP_VPD_PAGE:
3243 				/*
3244 				 * Request for suported Vital Product Data
3245 				 * pages - assuming only 2 page codes
3246 				 * supported.
3247 				 */
3248 				page_buf[0] = peripheral_device_type;
3249 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3250 				page_buf[2] = 0;
3251 				page_buf[3] = 2; /* page length */
3252 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3253 				page_buf[5] = INQUIRY_USN_PAGE;
3254 				/* Copy no more than requested */
3255 				count = MIN(bp->b_bcount, 6);
3256 				bcopy(page_buf, bp->b_un.b_addr, count);
3257 				break;
3258 
3259 			case INQUIRY_USN_PAGE:
3260 				/*
3261 				 * Request for Unit Serial Number page.
3262 				 * Set-up the page.
3263 				 */
3264 				page_buf[0] = peripheral_device_type;
3265 				page_buf[1] = INQUIRY_USN_PAGE;
3266 				page_buf[2] = 0;
3267 				/* remaining page length */
3268 				page_buf[3] = SATA_ID_SERIAL_LEN;
3269 
3270 				/*
3271 				 * Copy serial number from Identify Device data
3272 				 * words into the inquiry page and swap bytes
3273 				 * when necessary.
3274 				 */
3275 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3276 #ifdef	_LITTLE_ENDIAN
3277 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3278 #else
3279 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3280 #endif
3281 				/*
3282 				 * Least significant character of the serial
3283 				 * number shall appear as the last byte,
3284 				 * according to SBC-3 spec.
3285 				 * Count trailing spaces to determine the
3286 				 * necessary shift length.
3287 				 */
3288 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3289 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3290 					if (*(p - j) != '\0' &&
3291 					    *(p - j) != '\040')
3292 						break;
3293 				}
3294 
3295 				/*
3296 				 * Shift SN string right, so that the last
3297 				 * non-blank character would appear in last
3298 				 * byte of SN field in the page.
3299 				 * 'j' is the shift length.
3300 				 */
3301 				for (i = 0;
3302 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3303 				    i++, p--)
3304 					*p = *(p - j);
3305 
3306 				/*
3307 				 * Add leading spaces - same number as the
3308 				 * shift size
3309 				 */
3310 				for (; j > 0; j--)
3311 					page_buf[4 + j - 1] = '\040';
3312 
3313 				count = MIN(bp->b_bcount,
3314 				    SATA_ID_SERIAL_LEN + 4);
3315 				bcopy(page_buf, bp->b_un.b_addr, count);
3316 				break;
3317 
3318 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3319 				/*
3320 				 * We may want to implement this page, when
3321 				 * identifiers are common for SATA devices
3322 				 * But not now.
3323 				 */
3324 				/*FALLTHROUGH*/
3325 
3326 			default:
3327 				/* Request for unsupported VPD page */
3328 				*scsipkt->pkt_scbp = STATUS_CHECK;
3329 				sense = sata_arq_sense(spx);
3330 				sense->es_key = KEY_ILLEGAL_REQUEST;
3331 				sense->es_add_code =
3332 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3333 				goto done;
3334 			}
3335 		}
3336 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3337 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3338 		    scsipkt->pkt_cdbp[4] - count : 0;
3339 	}
3340 done:
3341 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3342 
3343 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3344 	    "Scsi_pkt completion reason %x\n",
3345 	    scsipkt->pkt_reason);
3346 
3347 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3348 	    scsipkt->pkt_comp != NULL) {
3349 		/* scsi callback required */
3350 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3351 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3352 		    TQ_SLEEP) == NULL)
3353 			/* Scheduling the callback failed */
3354 			return (TRAN_BUSY);
3355 	}
3356 	return (TRAN_ACCEPT);
3357 }
3358 
3359 /*
3360  * SATA translate command: Request Sense.
3361  *
3362  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3363  * At the moment this is an emulated command (ATA version for SATA hard disks).
3364  * May be translated into Check Power Mode command in the future.
3365  *
3366  * Note: There is a mismatch between already implemented Informational
3367  * Exception Mode Select page 0x1C and this function.
3368  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3369  * NO SENSE and set additional sense code to the exception code - this is not
3370  * implemented here.
3371  */
3372 static int
3373 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3374 {
3375 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3376 	struct scsi_extended_sense sense;
3377 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3378 	sata_drive_info_t *sdinfo;
3379 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3380 	int rval, reason, power_state = 0;
3381 
3382 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3383 
3384 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3385 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3386 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3387 		return (rval);
3388 	}
3389 
3390 	scsipkt->pkt_reason = CMD_CMPLT;
3391 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3392 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3393 	*scsipkt->pkt_scbp = STATUS_GOOD;
3394 
3395 	/*
3396 	 * when CONTROL field's NACA bit == 1
3397 	 * return ILLEGAL_REQUEST
3398 	 */
3399 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3400 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3401 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3402 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3403 	}
3404 
3405 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3406 	    &spx->txlt_sata_pkt->satapkt_device);
3407 	ASSERT(sdinfo != NULL);
3408 
3409 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3410 
3411 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3412 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3413 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3414 	if (sata_hba_start(spx, &rval) != 0) {
3415 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3416 		return (rval);
3417 	} else {
3418 		if (scmd->satacmd_error_reg != 0) {
3419 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3420 			return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3421 			    SD_SCSI_ASC_NO_ADD_SENSE));
3422 		}
3423 	}
3424 
3425 	switch (scmd->satacmd_sec_count_lsb) {
3426 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3427 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3428 			power_state = SATA_POWER_STOPPED;
3429 		else {
3430 			power_state = SATA_POWER_STANDBY;
3431 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3432 		}
3433 		break;
3434 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3435 		power_state = SATA_POWER_IDLE;
3436 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3437 		break;
3438 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3439 	default:		  /* 0x40, 0x41 active mode */
3440 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3441 			power_state = SATA_POWER_IDLE;
3442 		else {
3443 			power_state = SATA_POWER_ACTIVE;
3444 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3445 		}
3446 		break;
3447 	}
3448 
3449 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3450 
3451 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3452 		/*
3453 		 * Because it is fully emulated command storing data
3454 		 * programatically in the specified buffer, release
3455 		 * preallocated DMA resources before storing data in the buffer,
3456 		 * so no unwanted DMA sync would take place.
3457 		 */
3458 		int count = MIN(bp->b_bcount,
3459 		    sizeof (struct scsi_extended_sense));
3460 		sata_scsi_dmafree(NULL, scsipkt);
3461 		bzero(&sense, sizeof (struct scsi_extended_sense));
3462 		sense.es_valid = 0;	/* Valid LBA */
3463 		sense.es_class = 7;	/* Response code 0x70 - current err */
3464 		sense.es_key = KEY_NO_SENSE;
3465 		sense.es_add_len = 6;	/* Additional length */
3466 		/* Copy no more than requested */
3467 		bcopy(&sense, bp->b_un.b_addr, count);
3468 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3469 		scsipkt->pkt_resid = 0;
3470 		switch (power_state) {
3471 		case SATA_POWER_IDLE:
3472 		case SATA_POWER_STANDBY:
3473 			sense.es_add_code =
3474 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3475 			break;
3476 		case SATA_POWER_STOPPED:
3477 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3478 			break;
3479 		case SATA_POWER_ACTIVE:
3480 		default:
3481 			break;
3482 		}
3483 	}
3484 
3485 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3486 	    "Scsi_pkt completion reason %x\n",
3487 	    scsipkt->pkt_reason);
3488 
3489 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3490 		/* scsi callback required */
3491 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3492 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3493 		    TQ_SLEEP) == NULL)
3494 			/* Scheduling the callback failed */
3495 			return (TRAN_BUSY);
3496 	return (TRAN_ACCEPT);
3497 }
3498 
3499 /*
3500  * SATA translate command: Test Unit Ready
3501  * At the moment this is an emulated command (ATA version for SATA hard disks).
3502  * May be translated into Check Power Mode command in the future
3503  *
3504  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3505  */
3506 static int
3507 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3508 {
3509 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3510 	struct scsi_extended_sense *sense;
3511 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3512 	sata_drive_info_t *sdinfo;
3513 	int power_state;
3514 	int rval, reason;
3515 
3516 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3517 
3518 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3519 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3520 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3521 		return (rval);
3522 	}
3523 
3524 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3525 	    &spx->txlt_sata_pkt->satapkt_device);
3526 	ASSERT(sdinfo != NULL);
3527 
3528 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3529 
3530 	/* send CHECK POWER MODE command */
3531 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3532 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3533 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3534 	if (sata_hba_start(spx, &rval) != 0) {
3535 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3536 		return (rval);
3537 	} else {
3538 		if (scmd->satacmd_error_reg != 0) {
3539 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3540 			return (sata_txlt_check_condition(spx, KEY_NOT_READY,
3541 			    SD_SCSI_ASC_LU_NOT_RESPONSE));
3542 		}
3543 	}
3544 
3545 	power_state = scmd->satacmd_sec_count_lsb;
3546 
3547 	/*
3548 	 * return NOT READY when device in STOPPED mode
3549 	 */
3550 	if (power_state == SATA_PWRMODE_STANDBY &&
3551 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
3552 		*scsipkt->pkt_scbp = STATUS_CHECK;
3553 		sense = sata_arq_sense(spx);
3554 		sense->es_key = KEY_NOT_READY;
3555 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3556 	} else {
3557 		/*
3558 		 * For other power mode, return GOOD status
3559 		 */
3560 		*scsipkt->pkt_scbp = STATUS_GOOD;
3561 	}
3562 
3563 	scsipkt->pkt_reason = CMD_CMPLT;
3564 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3565 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3566 
3567 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3568 
3569 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3570 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3571 
3572 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3573 		/* scsi callback required */
3574 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3575 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3576 		    TQ_SLEEP) == NULL)
3577 			/* Scheduling the callback failed */
3578 			return (TRAN_BUSY);
3579 
3580 	return (TRAN_ACCEPT);
3581 }
3582 
3583 /*
3584  * SATA translate command: Start Stop Unit
3585  * Translation depends on a command:
3586  *
3587  * Power condition bits will be supported
3588  * and the power level should be maintained by SATL,
3589  * When SATL received a command, it will check the
3590  * power level firstly, and return the status according
3591  * to SAT2 v2.6 and SAT-2 Standby Modifications
3592  *
3593  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
3594  * -----------------------------------------------------------------------
3595  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
3596  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
3597  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
3598  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
3599  *
3600  *	Unload Media / NOT SUPPORTED YET
3601  *	Load Media / NOT SUPPROTED YET
3602  *	Immediate bit / NOT SUPPORTED YET (deferred error)
3603  *
3604  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3605  * appropriate values in scsi_pkt fields.
3606  */
3607 static int
3608 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3609 {
3610 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3611 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3612 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3613 	int cport = SATA_TXLT_CPORT(spx);
3614 	int rval, reason;
3615 	sata_drive_info_t *sdinfo;
3616 	sata_id_t *sata_id;
3617 
3618 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3619 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3620 
3621 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3622 
3623 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3624 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3625 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3626 		return (rval);
3627 	}
3628 
3629 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
3630 		/* IMMED bit - not supported */
3631 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3632 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3633 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
3634 	}
3635 
3636 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3637 	spx->txlt_sata_pkt->satapkt_comp = NULL;
3638 
3639 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3640 	    &spx->txlt_sata_pkt->satapkt_device);
3641 	ASSERT(sdinfo != NULL);
3642 	sata_id = &sdinfo->satadrv_id;
3643 
3644 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
3645 	case 0:
3646 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
3647 			/* Load/Unload Media - invalid request */
3648 			goto err_out;
3649 		}
3650 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
3651 			/* Start Unit */
3652 			sata_build_read_verify_cmd(scmd, 1, 5);
3653 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3654 			/* Transfer command to HBA */
3655 			if (sata_hba_start(spx, &rval) != 0) {
3656 				/* Pkt not accepted for execution */
3657 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3658 				return (rval);
3659 			} else {
3660 				if (scmd->satacmd_error_reg != 0) {
3661 					goto err_out;
3662 				}
3663 			}
3664 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3665 		} else {
3666 			/* Stop Unit */
3667 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3668 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3669 			if (sata_hba_start(spx, &rval) != 0) {
3670 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3671 				return (rval);
3672 			} else {
3673 				if (scmd->satacmd_error_reg != 0) {
3674 					goto err_out;
3675 				}
3676 			}
3677 			/* ata standby immediate command */
3678 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
3679 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3680 			if (sata_hba_start(spx, &rval) != 0) {
3681 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3682 				return (rval);
3683 			} else {
3684 				if (scmd->satacmd_error_reg != 0) {
3685 					goto err_out;
3686 				}
3687 			}
3688 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
3689 		}
3690 		break;
3691 	case 0x1:
3692 		sata_build_generic_cmd(scmd, SATAC_IDLE);
3693 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3694 		if (sata_hba_start(spx, &rval) != 0) {
3695 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3696 			return (rval);
3697 		} else {
3698 			if (scmd->satacmd_error_reg != 0) {
3699 				goto err_out;
3700 			}
3701 		}
3702 		sata_build_read_verify_cmd(scmd, 1, 5);
3703 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3704 		/* Transfer command to HBA */
3705 		if (sata_hba_start(spx, &rval) != 0) {
3706 			/* Pkt not accepted for execution */
3707 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3708 			return (rval);
3709 		} else {
3710 			if (scmd->satacmd_error_reg != 0) {
3711 				goto err_out;
3712 			}
3713 		}
3714 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3715 		break;
3716 	case 0x2:
3717 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3718 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3719 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
3720 			if (sata_hba_start(spx, &rval) != 0) {
3721 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3722 				return (rval);
3723 			} else {
3724 				if (scmd->satacmd_error_reg != 0) {
3725 					goto err_out;
3726 				}
3727 			}
3728 		}
3729 		sata_build_generic_cmd(scmd, SATAC_IDLE);
3730 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3731 		if (sata_hba_start(spx, &rval) != 0) {
3732 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3733 			return (rval);
3734 		} else {
3735 			if (scmd->satacmd_error_reg != 0) {
3736 				goto err_out;
3737 			}
3738 		}
3739 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
3740 			/*
3741 			 *  POWER CONDITION MODIFIER bit set
3742 			 *  to 0x1 or larger it will be handled
3743 			 *  on the same way as bit = 0x1
3744 			 */
3745 			if (!(sata_id->ai_cmdset84 &
3746 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
3747 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3748 				break;
3749 			}
3750 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
3751 			scmd->satacmd_features_reg = 0x44;
3752 			scmd->satacmd_lba_low_lsb = 0x4c;
3753 			scmd->satacmd_lba_mid_lsb = 0x4e;
3754 			scmd->satacmd_lba_high_lsb = 0x55;
3755 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3756 			if (sata_hba_start(spx, &rval) != 0) {
3757 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3758 				return (rval);
3759 			} else {
3760 				if (scmd->satacmd_error_reg != 0) {
3761 					goto err_out;
3762 				}
3763 			}
3764 		}
3765 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3766 		break;
3767 	case 0x3:
3768 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3769 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3770 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
3771 			if (sata_hba_start(spx, &rval) != 0) {
3772 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3773 				return (rval);
3774 			} else {
3775 				if (scmd->satacmd_error_reg != 0) {
3776 					goto err_out;
3777 				}
3778 			}
3779 		}
3780 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
3781 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3782 		if (sata_hba_start(spx, &rval) != 0) {
3783 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3784 			return (rval);
3785 		} else {
3786 			if (scmd->satacmd_error_reg != 0) {
3787 				goto err_out;
3788 			}
3789 		}
3790 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3791 		break;
3792 	case 0x7:
3793 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3794 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3795 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3796 		if (sata_hba_start(spx, &rval) != 0) {
3797 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3798 			return (rval);
3799 		} else {
3800 			if (scmd->satacmd_error_reg != 0) {
3801 				goto err_out;
3802 			}
3803 		}
3804 		switch (scmd->satacmd_sec_count_lsb) {
3805 		case SATA_PWRMODE_STANDBY:
3806 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
3807 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
3808 			    sdinfo->satadrv_standby_timer);
3809 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3810 			if (sata_hba_start(spx, &rval) != 0) {
3811 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3812 				return (rval);
3813 			} else {
3814 				if (scmd->satacmd_error_reg != 0) {
3815 					goto err_out;
3816 				}
3817 			}
3818 			break;
3819 		case SATA_PWRMODE_IDLE:
3820 			sata_build_generic_cmd(scmd, SATAC_IDLE);
3821 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
3822 			    sdinfo->satadrv_standby_timer);
3823 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3824 			if (sata_hba_start(spx, &rval) != 0) {
3825 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3826 				return (rval);
3827 			} else {
3828 				if (scmd->satacmd_error_reg != 0) {
3829 					goto err_out;
3830 				}
3831 			}
3832 			break;
3833 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
3834 		case SATA_PWRMODE_ACTIVE_SPINUP:
3835 		case SATA_PWRMODE_ACTIVE:
3836 			sata_build_generic_cmd(scmd, SATAC_IDLE);
3837 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
3838 			    sdinfo->satadrv_standby_timer);
3839 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3840 			if (sata_hba_start(spx, &rval) != 0) {
3841 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3842 				return (rval);
3843 			} else {
3844 				if (scmd->satacmd_error_reg != 0) {
3845 					goto err_out;
3846 				}
3847 			}
3848 			sata_build_read_verify_cmd(scmd, 1, 5);
3849 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3850 			if (sata_hba_start(spx, &rval) != 0) {
3851 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3852 				return (rval);
3853 			} else {
3854 				if (scmd->satacmd_error_reg != 0) {
3855 					goto err_out;
3856 				}
3857 			}
3858 			break;
3859 		default:
3860 			goto err_out;
3861 		}
3862 		break;
3863 	case 0xb:
3864 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
3865 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
3866 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3867 			return (sata_txlt_check_condition(spx,
3868 			    KEY_ILLEGAL_REQUEST,
3869 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
3870 		}
3871 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3872 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3873 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
3874 			if (sata_hba_start(spx, &rval) != 0) {
3875 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3876 				return (rval);
3877 			} else {
3878 				if (scmd->satacmd_error_reg != 0) {
3879 					goto err_out;
3880 				}
3881 			}
3882 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
3883 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3884 			if (sata_hba_start(spx, &rval) != 0) {
3885 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3886 				return (rval);
3887 			} else {
3888 				if (scmd->satacmd_error_reg != 0) {
3889 					goto err_out;
3890 				}
3891 			}
3892 		}
3893 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
3894 		break;
3895 	default:
3896 err_out:
3897 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3898 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3899 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
3900 	}
3901 
3902 	/*
3903 	 * since it was synchronous commands,
3904 	 * a callback function will be called directely.
3905 	 */
3906 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3907 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3908 	    "synchronous execution status %x\n",
3909 	    spx->txlt_sata_pkt->satapkt_reason);
3910 
3911 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) {
3912 		sata_set_arq_data(spx->txlt_sata_pkt);
3913 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3914 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3915 		    TQ_SLEEP) == 0) {
3916 			return (TRAN_BUSY);
3917 		}
3918 	}
3919 	else
3920 
3921 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3922 
3923 	return (TRAN_ACCEPT);
3924 
3925 }
3926 
3927 /*
3928  * SATA translate command:  Read Capacity.
3929  * Emulated command for SATA disks.
3930  * Capacity is retrieved from cached Idenifty Device data.
3931  * Identify Device data shows effective disk capacity, not the native
3932  * capacity, which may be limitted by Set Max Address command.
3933  * This is ATA version for SATA hard disks.
3934  *
3935  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3936  */
3937 static int
3938 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3939 {
3940 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3941 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3942 	sata_drive_info_t *sdinfo;
3943 	uint64_t val;
3944 	uchar_t *rbuf;
3945 	int rval, reason;
3946 
3947 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3948 	    "sata_txlt_read_capacity: ", NULL);
3949 
3950 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3951 
3952 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3953 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3954 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3955 		return (rval);
3956 	}
3957 
3958 	scsipkt->pkt_reason = CMD_CMPLT;
3959 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3960 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3961 	*scsipkt->pkt_scbp = STATUS_GOOD;
3962 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3963 		/*
3964 		 * Because it is fully emulated command storing data
3965 		 * programatically in the specified buffer, release
3966 		 * preallocated DMA resources before storing data in the buffer,
3967 		 * so no unwanted DMA sync would take place.
3968 		 */
3969 		sata_scsi_dmafree(NULL, scsipkt);
3970 
3971 		sdinfo = sata_get_device_info(
3972 		    spx->txlt_sata_hba_inst,
3973 		    &spx->txlt_sata_pkt->satapkt_device);
3974 		/* Last logical block address */
3975 		val = sdinfo->satadrv_capacity - 1;
3976 		rbuf = (uchar_t *)bp->b_un.b_addr;
3977 		/* Need to swap endians to match scsi format */
3978 		rbuf[0] = (val >> 24) & 0xff;
3979 		rbuf[1] = (val >> 16) & 0xff;
3980 		rbuf[2] = (val >> 8) & 0xff;
3981 		rbuf[3] = val & 0xff;
3982 		/* block size - always 512 bytes, for now */
3983 		rbuf[4] = 0;
3984 		rbuf[5] = 0;
3985 		rbuf[6] = 0x02;
3986 		rbuf[7] = 0;
3987 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3988 		scsipkt->pkt_resid = 0;
3989 
3990 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3991 		    sdinfo->satadrv_capacity -1);
3992 	}
3993 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3994 	/*
3995 	 * If a callback was requested, do it now.
3996 	 */
3997 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3998 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3999 
4000 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4001 	    scsipkt->pkt_comp != NULL)
4002 		/* scsi callback required */
4003 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4004 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4005 		    TQ_SLEEP) == NULL)
4006 			/* Scheduling the callback failed */
4007 			return (TRAN_BUSY);
4008 
4009 	return (TRAN_ACCEPT);
4010 }
4011 
4012 /*
4013  * SATA translate command: Mode Sense.
4014  * Translated into appropriate SATA command or emulated.
4015  * Saved Values Page Control (03) are not supported.
4016  *
4017  * NOTE: only caching mode sense page is currently implemented.
4018  *
4019  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4020  */
4021 
4022 #define	LLBAA	0x10	/* Long LBA Accepted */
4023 
4024 static int
4025 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4026 {
4027 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4028 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4029 	sata_drive_info_t *sdinfo;
4030 	sata_id_t *sata_id;
4031 	struct scsi_extended_sense *sense;
4032 	int 		len, bdlen, count, alc_len;
4033 	int		pc;	/* Page Control code */
4034 	uint8_t		*buf;	/* mode sense buffer */
4035 	int		rval, reason;
4036 
4037 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4038 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4039 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4040 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4041 
4042 	buf = kmem_zalloc(1024, KM_SLEEP);
4043 
4044 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4045 
4046 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4047 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4048 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4049 		kmem_free(buf, 1024);
4050 		return (rval);
4051 	}
4052 
4053 	scsipkt->pkt_reason = CMD_CMPLT;
4054 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4055 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4056 
4057 	pc = scsipkt->pkt_cdbp[2] >> 6;
4058 
4059 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4060 		/*
4061 		 * Because it is fully emulated command storing data
4062 		 * programatically in the specified buffer, release
4063 		 * preallocated DMA resources before storing data in the buffer,
4064 		 * so no unwanted DMA sync would take place.
4065 		 */
4066 		sata_scsi_dmafree(NULL, scsipkt);
4067 
4068 		len = 0;
4069 		bdlen = 0;
4070 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4071 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4072 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4073 				bdlen = 16;
4074 			else
4075 				bdlen = 8;
4076 		}
4077 		/* Build mode parameter header */
4078 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4079 			/* 4-byte mode parameter header */
4080 			buf[len++] = 0;		/* mode data length */
4081 			buf[len++] = 0;		/* medium type */
4082 			buf[len++] = 0;		/* dev-specific param */
4083 			buf[len++] = bdlen;	/* Block Descriptor length */
4084 		} else {
4085 			/* 8-byte mode parameter header */
4086 			buf[len++] = 0;		/* mode data length */
4087 			buf[len++] = 0;
4088 			buf[len++] = 0;		/* medium type */
4089 			buf[len++] = 0;		/* dev-specific param */
4090 			if (bdlen == 16)
4091 				buf[len++] = 1;	/* long lba descriptor */
4092 			else
4093 				buf[len++] = 0;
4094 			buf[len++] = 0;
4095 			buf[len++] = 0;		/* Block Descriptor length */
4096 			buf[len++] = bdlen;
4097 		}
4098 
4099 		sdinfo = sata_get_device_info(
4100 		    spx->txlt_sata_hba_inst,
4101 		    &spx->txlt_sata_pkt->satapkt_device);
4102 
4103 		/* Build block descriptor only if not disabled (DBD) */
4104 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4105 			/* Block descriptor - direct-access device format */
4106 			if (bdlen == 8) {
4107 				/* build regular block descriptor */
4108 				buf[len++] =
4109 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4110 				buf[len++] =
4111 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4112 				buf[len++] =
4113 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4114 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4115 				buf[len++] = 0; /* density code */
4116 				buf[len++] = 0;
4117 				if (sdinfo->satadrv_type ==
4118 				    SATA_DTYPE_ATADISK)
4119 					buf[len++] = 2;
4120 				else
4121 					/* ATAPI */
4122 					buf[len++] = 8;
4123 				buf[len++] = 0;
4124 			} else if (bdlen == 16) {
4125 				/* Long LBA Accepted */
4126 				/* build long lba block descriptor */
4127 #ifndef __lock_lint
4128 				buf[len++] =
4129 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4130 				buf[len++] =
4131 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4132 				buf[len++] =
4133 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4134 				buf[len++] =
4135 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4136 #endif
4137 				buf[len++] =
4138 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4139 				buf[len++] =
4140 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4141 				buf[len++] =
4142 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4143 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4144 				buf[len++] = 0;
4145 				buf[len++] = 0; /* density code */
4146 				buf[len++] = 0;
4147 				buf[len++] = 0;
4148 				if (sdinfo->satadrv_type ==
4149 				    SATA_DTYPE_ATADISK)
4150 					buf[len++] = 2;
4151 				else
4152 					/* ATAPI */
4153 					buf[len++] = 8;
4154 				buf[len++] = 0;
4155 			}
4156 		}
4157 
4158 		sata_id = &sdinfo->satadrv_id;
4159 
4160 		/*
4161 		 * Add requested pages.
4162 		 * Page 3 and 4 are obsolete and we are not supporting them.
4163 		 * We deal now with:
4164 		 * caching (read/write cache control).
4165 		 * We should eventually deal with following mode pages:
4166 		 * error recovery  (0x01),
4167 		 * power condition (0x1a),
4168 		 * exception control page (enables SMART) (0x1c),
4169 		 * enclosure management (ses),
4170 		 * protocol-specific port mode (port control).
4171 		 */
4172 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4173 		case MODEPAGE_RW_ERRRECOV:
4174 			/* DAD_MODE_ERR_RECOV */
4175 			/* R/W recovery */
4176 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4177 			break;
4178 		case MODEPAGE_CACHING:
4179 			/* DAD_MODE_CACHE */
4180 			/* Reject not supported request for saved parameters */
4181 			if (pc == 3) {
4182 				*scsipkt->pkt_scbp = STATUS_CHECK;
4183 				sense = sata_arq_sense(spx);
4184 				sense->es_key = KEY_ILLEGAL_REQUEST;
4185 				sense->es_add_code =
4186 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4187 				goto done;
4188 			}
4189 
4190 			/* caching */
4191 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4192 			break;
4193 		case MODEPAGE_INFO_EXCPT:
4194 			/* exception cntrl */
4195 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4196 				len += sata_build_msense_page_1c(sdinfo, pc,
4197 				    buf+len);
4198 			}
4199 			else
4200 				goto err;
4201 			break;
4202 		case MODEPAGE_POWER_COND:
4203 			/* DAD_MODE_POWER_COND */
4204 			/* power condition */
4205 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4206 			break;
4207 
4208 		case MODEPAGE_ACOUSTIC_MANAG:
4209 			/* acoustic management */
4210 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4211 			break;
4212 		case MODEPAGE_ALLPAGES:
4213 			/* all pages */
4214 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4215 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4216 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4217 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4218 				len += sata_build_msense_page_1c(sdinfo, pc,
4219 				    buf+len);
4220 			}
4221 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4222 			break;
4223 		default:
4224 		err:
4225 			/* Invalid request */
4226 			*scsipkt->pkt_scbp = STATUS_CHECK;
4227 			sense = sata_arq_sense(spx);
4228 			sense->es_key = KEY_ILLEGAL_REQUEST;
4229 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4230 			goto done;
4231 		}
4232 
4233 		/* fix total mode data length */
4234 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4235 			/* 4-byte mode parameter header */
4236 			buf[0] = len - 1;	/* mode data length */
4237 		} else {
4238 			buf[0] = (len -2) >> 8;
4239 			buf[1] = (len -2) & 0xff;
4240 		}
4241 
4242 
4243 		/* Check allocation length */
4244 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4245 			alc_len = scsipkt->pkt_cdbp[4];
4246 		} else {
4247 			alc_len = scsipkt->pkt_cdbp[7];
4248 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4249 		}
4250 		/*
4251 		 * We do not check for possible parameters truncation
4252 		 * (alc_len < len) assuming that the target driver works
4253 		 * correctly. Just avoiding overrun.
4254 		 * Copy no more than requested and possible, buffer-wise.
4255 		 */
4256 		count = MIN(alc_len, len);
4257 		count = MIN(bp->b_bcount, count);
4258 		bcopy(buf, bp->b_un.b_addr, count);
4259 
4260 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4261 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4262 	}
4263 	*scsipkt->pkt_scbp = STATUS_GOOD;
4264 done:
4265 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4266 	(void) kmem_free(buf, 1024);
4267 
4268 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4269 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4270 
4271 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4272 	    scsipkt->pkt_comp != NULL)
4273 		/* scsi callback required */
4274 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4275 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4276 		    TQ_SLEEP) == NULL)
4277 			/* Scheduling the callback failed */
4278 			return (TRAN_BUSY);
4279 
4280 	return (TRAN_ACCEPT);
4281 }
4282 
4283 
4284 /*
4285  * SATA translate command: Mode Select.
4286  * Translated into appropriate SATA command or emulated.
4287  * Saving parameters is not supported.
4288  * Changing device capacity is not supported (although theoretically
4289  * possible by executing SET FEATURES/SET MAX ADDRESS)
4290  *
4291  * Assumption is that the target driver is working correctly.
4292  *
4293  * More than one SATA command may be executed to perform operations specified
4294  * by mode select pages. The first error terminates further execution.
4295  * Operations performed successully are not backed-up in such case.
4296  *
4297  * NOTE: Implemented pages:
4298  * - caching page
4299  * - informational exception page
4300  * - acoustic management page
4301  * - power condition page
4302  * Caching setup is remembered so it could be re-stored in case of
4303  * an unexpected device reset.
4304  *
4305  * Returns TRAN_XXXX.
4306  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
4307  */
4308 
4309 static int
4310 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4311 {
4312 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4313 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4314 	struct scsi_extended_sense *sense;
4315 	int len, pagelen, count, pllen;
4316 	uint8_t *buf;	/* mode select buffer */
4317 	int rval, stat, reason;
4318 	uint_t nointr_flag;
4319 	int dmod = 0;
4320 
4321 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4322 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4323 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4324 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4325 
4326 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4327 
4328 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4329 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4330 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4331 		return (rval);
4332 	}
4333 	/*
4334 	 * If in interrupt context, reject this packet because it may result
4335 	 * in issuing a synchronous command to HBA.
4336 	 */
4337 	if (servicing_interrupt()) {
4338 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4339 		    "sata_txlt_mode_select: rejecting command because "
4340 		    "of interrupt context\n", NULL);
4341 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4342 		return (TRAN_BUSY);
4343 	}
4344 
4345 	rval = TRAN_ACCEPT;
4346 
4347 	scsipkt->pkt_reason = CMD_CMPLT;
4348 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4349 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4350 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4351 
4352 	/* Reject not supported request */
4353 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4354 		*scsipkt->pkt_scbp = STATUS_CHECK;
4355 		sense = sata_arq_sense(spx);
4356 		sense->es_key = KEY_ILLEGAL_REQUEST;
4357 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4358 		goto done;
4359 	}
4360 
4361 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4362 		pllen = scsipkt->pkt_cdbp[4];
4363 	} else {
4364 		pllen = scsipkt->pkt_cdbp[7];
4365 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4366 	}
4367 
4368 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4369 
4370 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4371 		buf = (uint8_t *)bp->b_un.b_addr;
4372 		count = MIN(bp->b_bcount, pllen);
4373 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4374 		scsipkt->pkt_resid = 0;
4375 		pllen = count;
4376 
4377 		/*
4378 		 * Check the header to skip the block descriptor(s) - we
4379 		 * do not support setting device capacity.
4380 		 * Existing macros do not recognize long LBA dscriptor,
4381 		 * hence manual calculation.
4382 		 */
4383 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4384 			/* 6-bytes CMD, 4 bytes header */
4385 			if (count <= 4)
4386 				goto done;		/* header only */
4387 			len = buf[3] + 4;
4388 		} else {
4389 			/* 10-bytes CMD, 8 bytes header */
4390 			if (count <= 8)
4391 				goto done;		/* header only */
4392 			len = buf[6];
4393 			len = (len << 8) + buf[7] + 8;
4394 		}
4395 		if (len >= count)
4396 			goto done;	/* header + descriptor(s) only */
4397 
4398 		pllen -= len;		/* remaining data length */
4399 
4400 		/*
4401 		 * We may be executing SATA command and want to execute it
4402 		 * in SYNCH mode, regardless of scsi_pkt setting.
4403 		 * Save scsi_pkt setting and indicate SYNCH mode
4404 		 */
4405 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4406 		    scsipkt->pkt_comp != NULL) {
4407 			scsipkt->pkt_flags |= FLAG_NOINTR;
4408 		}
4409 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4410 
4411 		/*
4412 		 * len is now the offset to a first mode select page
4413 		 * Process all pages
4414 		 */
4415 		while (pllen > 0) {
4416 			switch ((int)buf[len]) {
4417 			case MODEPAGE_CACHING:
4418 				/* No support for SP (saving) */
4419 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4420 					*scsipkt->pkt_scbp = STATUS_CHECK;
4421 					sense = sata_arq_sense(spx);
4422 					sense->es_key = KEY_ILLEGAL_REQUEST;
4423 					sense->es_add_code =
4424 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4425 					goto done;
4426 				}
4427 				stat = sata_mode_select_page_8(spx,
4428 				    (struct mode_cache_scsi3 *)&buf[len],
4429 				    pllen, &pagelen, &rval, &dmod);
4430 				/*
4431 				 * The pagelen value indicates the number of
4432 				 * parameter bytes already processed.
4433 				 * The rval is the return value from
4434 				 * sata_tran_start().
4435 				 * The stat indicates the overall status of
4436 				 * the operation(s).
4437 				 */
4438 				if (stat != SATA_SUCCESS)
4439 					/*
4440 					 * Page processing did not succeed -
4441 					 * all error info is already set-up,
4442 					 * just return
4443 					 */
4444 					pllen = 0; /* this breaks the loop */
4445 				else {
4446 					len += pagelen;
4447 					pllen -= pagelen;
4448 				}
4449 				break;
4450 
4451 			case MODEPAGE_INFO_EXCPT:
4452 				stat = sata_mode_select_page_1c(spx,
4453 				    (struct mode_info_excpt_page *)&buf[len],
4454 				    pllen, &pagelen, &rval, &dmod);
4455 				/*
4456 				 * The pagelen value indicates the number of
4457 				 * parameter bytes already processed.
4458 				 * The rval is the return value from
4459 				 * sata_tran_start().
4460 				 * The stat indicates the overall status of
4461 				 * the operation(s).
4462 				 */
4463 				if (stat != SATA_SUCCESS)
4464 					/*
4465 					 * Page processing did not succeed -
4466 					 * all error info is already set-up,
4467 					 * just return
4468 					 */
4469 					pllen = 0; /* this breaks the loop */
4470 				else {
4471 					len += pagelen;
4472 					pllen -= pagelen;
4473 				}
4474 				break;
4475 
4476 			case MODEPAGE_ACOUSTIC_MANAG:
4477 				stat = sata_mode_select_page_30(spx,
4478 				    (struct mode_acoustic_management *)
4479 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4480 				/*
4481 				 * The pagelen value indicates the number of
4482 				 * parameter bytes already processed.
4483 				 * The rval is the return value from
4484 				 * sata_tran_start().
4485 				 * The stat indicates the overall status of
4486 				 * the operation(s).
4487 				 */
4488 				if (stat != SATA_SUCCESS)
4489 					/*
4490 					 * Page processing did not succeed -
4491 					 * all error info is already set-up,
4492 					 * just return
4493 					 */
4494 					pllen = 0; /* this breaks the loop */
4495 				else {
4496 					len += pagelen;
4497 					pllen -= pagelen;
4498 				}
4499 
4500 				break;
4501 			case MODEPAGE_POWER_COND:
4502 				stat = sata_mode_select_page_1a(spx,
4503 				    (struct mode_info_power_cond *)&buf[len],
4504 				    pllen, &pagelen, &rval, &dmod);
4505 				/*
4506 				 * The pagelen value indicates the number of
4507 				 * parameter bytes already processed.
4508 				 * The rval is the return value from
4509 				 * sata_tran_start().
4510 				 * The stat indicates the overall status of
4511 				 * the operation(s).
4512 				 */
4513 				if (stat != SATA_SUCCESS)
4514 					/*
4515 					 * Page processing did not succeed -
4516 					 * all error info is already set-up,
4517 					 * just return
4518 					 */
4519 					pllen = 0; /* this breaks the loop */
4520 				else {
4521 					len += pagelen;
4522 					pllen -= pagelen;
4523 				}
4524 				break;
4525 			default:
4526 				*scsipkt->pkt_scbp = STATUS_CHECK;
4527 				sense = sata_arq_sense(spx);
4528 				sense->es_key = KEY_ILLEGAL_REQUEST;
4529 				sense->es_add_code =
4530 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4531 				goto done;
4532 			}
4533 		}
4534 	}
4535 done:
4536 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4537 	/*
4538 	 * If device parameters were modified, fetch and store the new
4539 	 * Identify Device data. Since port mutex could have been released
4540 	 * for accessing HBA driver, we need to re-check device existence.
4541 	 */
4542 	if (dmod != 0) {
4543 		sata_drive_info_t new_sdinfo, *sdinfo;
4544 		int rv = 0;
4545 
4546 		/*
4547 		 * Following statement has to be changed if this function is
4548 		 * used for devices other than SATA hard disks.
4549 		 */
4550 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4551 
4552 		new_sdinfo.satadrv_addr =
4553 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4554 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4555 		    &new_sdinfo);
4556 
4557 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4558 		/*
4559 		 * Since port mutex could have been released when
4560 		 * accessing HBA driver, we need to re-check that the
4561 		 * framework still holds the device info structure.
4562 		 */
4563 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4564 		    &spx->txlt_sata_pkt->satapkt_device);
4565 		if (sdinfo != NULL) {
4566 			/*
4567 			 * Device still has info structure in the
4568 			 * sata framework. Copy newly fetched info
4569 			 */
4570 			if (rv == 0) {
4571 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4572 				sata_save_drive_settings(sdinfo);
4573 			} else {
4574 				/*
4575 				 * Could not fetch new data - invalidate
4576 				 * sata_drive_info. That makes device
4577 				 * unusable.
4578 				 */
4579 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4580 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4581 			}
4582 		}
4583 		if (rv != 0 || sdinfo == NULL) {
4584 			/*
4585 			 * This changes the overall mode select completion
4586 			 * reason to a failed one !!!!!
4587 			 */
4588 			*scsipkt->pkt_scbp = STATUS_CHECK;
4589 			sense = sata_arq_sense(spx);
4590 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4591 			rval = TRAN_ACCEPT;
4592 		}
4593 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4594 	}
4595 	/* Restore the scsi pkt flags */
4596 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4597 	scsipkt->pkt_flags |= nointr_flag;
4598 
4599 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4600 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4601 
4602 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4603 	    scsipkt->pkt_comp != NULL)
4604 		/* scsi callback required */
4605 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4606 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4607 		    TQ_SLEEP) == NULL)
4608 			/* Scheduling the callback failed */
4609 			return (TRAN_BUSY);
4610 
4611 	return (rval);
4612 }
4613 
4614 
4615 
4616 /*
4617  * Translate command: Log Sense
4618  */
4619 static 	int
4620 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4621 {
4622 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4623 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4624 	sata_drive_info_t *sdinfo;
4625 	struct scsi_extended_sense *sense;
4626 	int 		len, count, alc_len;
4627 	int		pc;	/* Page Control code */
4628 	int		page_code;	/* Page code */
4629 	uint8_t		*buf;	/* log sense buffer */
4630 	int		rval, reason;
4631 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4632 
4633 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4634 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4635 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4636 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4637 
4638 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4639 
4640 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4641 
4642 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4643 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4644 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4645 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4646 		return (rval);
4647 	}
4648 	/*
4649 	 * If in interrupt context, reject this packet because it may result
4650 	 * in issuing a synchronous command to HBA.
4651 	 */
4652 	if (servicing_interrupt()) {
4653 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4654 		    "sata_log_sense: rejecting command because "
4655 		    "of interrupt context\n", NULL);
4656 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4657 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4658 		return (TRAN_BUSY);
4659 	}
4660 
4661 	scsipkt->pkt_reason = CMD_CMPLT;
4662 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4663 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4664 
4665 	pc = scsipkt->pkt_cdbp[2] >> 6;
4666 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4667 
4668 	/* Reject not supported request for all but cumulative values */
4669 	switch (pc) {
4670 	case PC_CUMULATIVE_VALUES:
4671 		break;
4672 	default:
4673 		*scsipkt->pkt_scbp = STATUS_CHECK;
4674 		sense = sata_arq_sense(spx);
4675 		sense->es_key = KEY_ILLEGAL_REQUEST;
4676 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4677 		goto done;
4678 	}
4679 
4680 	switch (page_code) {
4681 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4682 	case PAGE_CODE_SELF_TEST_RESULTS:
4683 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4684 	case PAGE_CODE_SMART_READ_DATA:
4685 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4686 		break;
4687 	default:
4688 		*scsipkt->pkt_scbp = STATUS_CHECK;
4689 		sense = sata_arq_sense(spx);
4690 		sense->es_key = KEY_ILLEGAL_REQUEST;
4691 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4692 		goto done;
4693 	}
4694 
4695 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4696 		/*
4697 		 * Because log sense uses local buffers for data retrieval from
4698 		 * the devices and sets the data programatically in the
4699 		 * original specified buffer, release preallocated DMA
4700 		 * resources before storing data in the original buffer,
4701 		 * so no unwanted DMA sync would take place.
4702 		 */
4703 		sata_id_t *sata_id;
4704 
4705 		sata_scsi_dmafree(NULL, scsipkt);
4706 
4707 		len = 0;
4708 
4709 		/* Build log parameter header */
4710 		buf[len++] = page_code;	/* page code as in the CDB */
4711 		buf[len++] = 0;		/* reserved */
4712 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4713 		buf[len++] = 0;		/* (LSB) */
4714 
4715 		sdinfo = sata_get_device_info(
4716 		    spx->txlt_sata_hba_inst,
4717 		    &spx->txlt_sata_pkt->satapkt_device);
4718 
4719 		/*
4720 		 * Add requested pages.
4721 		 */
4722 		switch (page_code) {
4723 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4724 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4725 			break;
4726 		case PAGE_CODE_SELF_TEST_RESULTS:
4727 			sata_id = &sdinfo->satadrv_id;
4728 			if ((! (sata_id->ai_cmdset84 &
4729 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4730 			    (! (sata_id->ai_features87 &
4731 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4732 				*scsipkt->pkt_scbp = STATUS_CHECK;
4733 				sense = sata_arq_sense(spx);
4734 				sense->es_key = KEY_ILLEGAL_REQUEST;
4735 				sense->es_add_code =
4736 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4737 
4738 				goto done;
4739 			}
4740 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4741 			    spx->txlt_sata_hba_inst);
4742 			break;
4743 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4744 			sata_id = &sdinfo->satadrv_id;
4745 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4746 				*scsipkt->pkt_scbp = STATUS_CHECK;
4747 				sense = sata_arq_sense(spx);
4748 				sense->es_key = KEY_ILLEGAL_REQUEST;
4749 				sense->es_add_code =
4750 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4751 
4752 				goto done;
4753 			}
4754 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4755 				*scsipkt->pkt_scbp = STATUS_CHECK;
4756 				sense = sata_arq_sense(spx);
4757 				sense->es_key = KEY_ABORTED_COMMAND;
4758 				sense->es_add_code =
4759 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4760 				sense->es_qual_code =
4761 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4762 
4763 				goto done;
4764 			}
4765 
4766 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4767 			    spx->txlt_sata_hba_inst);
4768 			break;
4769 		case PAGE_CODE_SMART_READ_DATA:
4770 			sata_id = &sdinfo->satadrv_id;
4771 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4772 				*scsipkt->pkt_scbp = STATUS_CHECK;
4773 				sense = sata_arq_sense(spx);
4774 				sense->es_key = KEY_ILLEGAL_REQUEST;
4775 				sense->es_add_code =
4776 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4777 
4778 				goto done;
4779 			}
4780 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4781 				*scsipkt->pkt_scbp = STATUS_CHECK;
4782 				sense = sata_arq_sense(spx);
4783 				sense->es_key = KEY_ABORTED_COMMAND;
4784 				sense->es_add_code =
4785 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4786 				sense->es_qual_code =
4787 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4788 
4789 				goto done;
4790 			}
4791 
4792 			/* This page doesn't include a page header */
4793 			len = sata_build_lsense_page_30(sdinfo, buf,
4794 			    spx->txlt_sata_hba_inst);
4795 			goto no_header;
4796 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4797 			sata_id = &sdinfo->satadrv_id;
4798 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4799 				*scsipkt->pkt_scbp = STATUS_CHECK;
4800 				sense = sata_arq_sense(spx);
4801 				sense->es_key = KEY_ILLEGAL_REQUEST;
4802 				sense->es_add_code =
4803 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4804 
4805 				goto done;
4806 			}
4807 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4808 				*scsipkt->pkt_scbp = STATUS_CHECK;
4809 				sense = sata_arq_sense(spx);
4810 				sense->es_key = KEY_ABORTED_COMMAND;
4811 				sense->es_add_code =
4812 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4813 				sense->es_qual_code =
4814 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4815 
4816 				goto done;
4817 			}
4818 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
4819 			goto no_header;
4820 		default:
4821 			/* Invalid request */
4822 			*scsipkt->pkt_scbp = STATUS_CHECK;
4823 			sense = sata_arq_sense(spx);
4824 			sense->es_key = KEY_ILLEGAL_REQUEST;
4825 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4826 			goto done;
4827 		}
4828 
4829 		/* set parameter log sense data length */
4830 		buf[2] = len >> 8;	/* log sense length (MSB) */
4831 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4832 
4833 		len += SCSI_LOG_PAGE_HDR_LEN;
4834 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4835 
4836 no_header:
4837 		/* Check allocation length */
4838 		alc_len = scsipkt->pkt_cdbp[7];
4839 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4840 
4841 		/*
4842 		 * We do not check for possible parameters truncation
4843 		 * (alc_len < len) assuming that the target driver works
4844 		 * correctly. Just avoiding overrun.
4845 		 * Copy no more than requested and possible, buffer-wise.
4846 		 */
4847 		count = MIN(alc_len, len);
4848 		count = MIN(bp->b_bcount, count);
4849 		bcopy(buf, bp->b_un.b_addr, count);
4850 
4851 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4852 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4853 	}
4854 	*scsipkt->pkt_scbp = STATUS_GOOD;
4855 done:
4856 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4857 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4858 
4859 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4860 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4861 
4862 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4863 	    scsipkt->pkt_comp != NULL)
4864 		/* scsi callback required */
4865 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4866 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4867 		    TQ_SLEEP) == NULL)
4868 			/* Scheduling the callback failed */
4869 			return (TRAN_BUSY);
4870 
4871 	return (TRAN_ACCEPT);
4872 }
4873 
4874 /*
4875  * Translate command: Log Select
4876  * Not implemented at this time - returns invalid command response.
4877  */
4878 static	int
4879 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4880 {
4881 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4882 	    "sata_txlt_log_select\n", NULL);
4883 
4884 	return (sata_txlt_invalid_command(spx));
4885 }
4886 
4887 
4888 /*
4889  * Translate command: Read (various types).
4890  * Translated into appropriate type of ATA READ command
4891  * for SATA hard disks.
4892  * Both the device capabilities and requested operation mode are
4893  * considered.
4894  *
4895  * Following scsi cdb fields are ignored:
4896  * rdprotect, dpo, fua, fua_nv, group_number.
4897  *
4898  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4899  * enable variable sata_func_enable), the capability of the controller and
4900  * capability of a device are checked and if both support queueing, read
4901  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4902  * command rather than plain READ_XXX command.
4903  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4904  * both the controller and device suport such functionality, the read
4905  * request will be translated to READ_FPDMA_QUEUED command.
4906  * In both cases the maximum queue depth is derived as minimum of:
4907  * HBA capability,device capability and sata_max_queue_depth variable setting.
4908  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4909  * used to pass max queue depth value, and the maximum possible queue depth
4910  * is 32.
4911  *
4912  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4913  * appropriate values in scsi_pkt fields.
4914  */
4915 static int
4916 sata_txlt_read(sata_pkt_txlate_t *spx)
4917 {
4918 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4919 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4920 	sata_drive_info_t *sdinfo;
4921 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4922 	int cport = SATA_TXLT_CPORT(spx);
4923 	uint16_t sec_count;
4924 	uint64_t lba;
4925 	int rval, reason;
4926 	int synch;
4927 
4928 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4929 
4930 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4931 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4932 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4933 		return (rval);
4934 	}
4935 
4936 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4937 	    &spx->txlt_sata_pkt->satapkt_device);
4938 
4939 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4940 	/*
4941 	 * Extract LBA and sector count from scsi CDB.
4942 	 */
4943 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4944 	case SCMD_READ:
4945 		/* 6-byte scsi read cmd : 0x08 */
4946 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4947 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4948 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4949 		sec_count = scsipkt->pkt_cdbp[4];
4950 		/* sec_count 0 will be interpreted as 256 by a device */
4951 		break;
4952 	case SCMD_READ_G1:
4953 		/* 10-bytes scsi read command : 0x28 */
4954 		lba = scsipkt->pkt_cdbp[2];
4955 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4956 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4957 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4958 		sec_count = scsipkt->pkt_cdbp[7];
4959 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4960 		break;
4961 	case SCMD_READ_G5:
4962 		/* 12-bytes scsi read command : 0xA8 */
4963 		lba = scsipkt->pkt_cdbp[2];
4964 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4965 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4966 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4967 		sec_count = scsipkt->pkt_cdbp[6];
4968 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4969 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4970 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4971 		break;
4972 	case SCMD_READ_G4:
4973 		/* 16-bytes scsi read command : 0x88 */
4974 		lba = scsipkt->pkt_cdbp[2];
4975 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4976 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4977 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4978 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4979 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4980 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4981 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4982 		sec_count = scsipkt->pkt_cdbp[10];
4983 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4984 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4985 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4986 		break;
4987 	default:
4988 		/* Unsupported command */
4989 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4990 		return (sata_txlt_invalid_command(spx));
4991 	}
4992 
4993 	/*
4994 	 * Check if specified address exceeds device capacity
4995 	 */
4996 	if ((lba >= sdinfo->satadrv_capacity) ||
4997 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4998 		/* LBA out of range */
4999 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5000 		return (sata_txlt_lba_out_of_range(spx));
5001 	}
5002 
5003 	/*
5004 	 * For zero-length transfer, emulate good completion of the command
5005 	 * (reasons for rejecting the command were already checked).
5006 	 * No DMA resources were allocated.
5007 	 */
5008 	if (spx->txlt_dma_cookie_list == NULL) {
5009 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5010 		return (sata_emul_rw_completion(spx));
5011 	}
5012 
5013 	/*
5014 	 * Build cmd block depending on the device capability and
5015 	 * requested operation mode.
5016 	 * Do not bother with non-dma mode - we are working only with
5017 	 * devices supporting DMA.
5018 	 */
5019 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5020 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5021 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5022 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5023 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5024 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5025 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5026 #ifndef __lock_lint
5027 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5028 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5029 		scmd->satacmd_lba_high_msb = lba >> 40;
5030 #endif
5031 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5032 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5033 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5034 	}
5035 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5036 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5037 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5038 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5039 	scmd->satacmd_features_reg = 0;
5040 	scmd->satacmd_status_reg = 0;
5041 	scmd->satacmd_error_reg = 0;
5042 
5043 	/*
5044 	 * Check if queueing commands should be used and switch
5045 	 * to appropriate command if possible
5046 	 */
5047 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5048 		boolean_t using_queuing;
5049 
5050 		/* Queuing supported by controller and device? */
5051 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5052 		    (sdinfo->satadrv_features_support &
5053 		    SATA_DEV_F_NCQ) &&
5054 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5055 		    SATA_CTLF_NCQ)) {
5056 			using_queuing = B_TRUE;
5057 
5058 			/* NCQ supported - use FPDMA READ */
5059 			scmd->satacmd_cmd_reg =
5060 			    SATAC_READ_FPDMA_QUEUED;
5061 			scmd->satacmd_features_reg_ext =
5062 			    scmd->satacmd_sec_count_msb;
5063 			scmd->satacmd_sec_count_msb = 0;
5064 		} else if ((sdinfo->satadrv_features_support &
5065 		    SATA_DEV_F_TCQ) &&
5066 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5067 		    SATA_CTLF_QCMD)) {
5068 			using_queuing = B_TRUE;
5069 
5070 			/* Legacy queueing */
5071 			if (sdinfo->satadrv_features_support &
5072 			    SATA_DEV_F_LBA48) {
5073 				scmd->satacmd_cmd_reg =
5074 				    SATAC_READ_DMA_QUEUED_EXT;
5075 				scmd->satacmd_features_reg_ext =
5076 				    scmd->satacmd_sec_count_msb;
5077 				scmd->satacmd_sec_count_msb = 0;
5078 			} else {
5079 				scmd->satacmd_cmd_reg =
5080 				    SATAC_READ_DMA_QUEUED;
5081 			}
5082 		} else	/* NCQ nor legacy queuing not supported */
5083 			using_queuing = B_FALSE;
5084 
5085 		/*
5086 		 * If queuing, the sector count goes in the features register
5087 		 * and the secount count will contain the tag.
5088 		 */
5089 		if (using_queuing) {
5090 			scmd->satacmd_features_reg =
5091 			    scmd->satacmd_sec_count_lsb;
5092 			scmd->satacmd_sec_count_lsb = 0;
5093 			scmd->satacmd_flags.sata_queued = B_TRUE;
5094 
5095 			/* Set-up maximum queue depth */
5096 			scmd->satacmd_flags.sata_max_queue_depth =
5097 			    sdinfo->satadrv_max_queue_depth - 1;
5098 		} else if (sdinfo->satadrv_features_enabled &
5099 		    SATA_DEV_F_E_UNTAGGED_QING) {
5100 			/*
5101 			 * Although NCQ/TCQ is not enabled, untagged queuing
5102 			 * may be still used.
5103 			 * Set-up the maximum untagged queue depth.
5104 			 * Use controller's queue depth from sata_hba_tran.
5105 			 * SATA HBA drivers may ignore this value and rely on
5106 			 * the internal limits.For drivers that do not
5107 			 * ignore untaged queue depth, limit the value to
5108 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5109 			 * largest value that can be passed via
5110 			 * satacmd_flags.sata_max_queue_depth.
5111 			 */
5112 			scmd->satacmd_flags.sata_max_queue_depth =
5113 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5114 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5115 
5116 		} else {
5117 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5118 		}
5119 	} else
5120 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5121 
5122 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5123 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5124 	    scmd->satacmd_cmd_reg, lba, sec_count);
5125 
5126 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5127 		/* Need callback function */
5128 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5129 		synch = FALSE;
5130 	} else
5131 		synch = TRUE;
5132 
5133 	/* Transfer command to HBA */
5134 	if (sata_hba_start(spx, &rval) != 0) {
5135 		/* Pkt not accepted for execution */
5136 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5137 		return (rval);
5138 	}
5139 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5140 	/*
5141 	 * If execution is non-synchronous,
5142 	 * a callback function will handle potential errors, translate
5143 	 * the response and will do a callback to a target driver.
5144 	 * If it was synchronous, check execution status using the same
5145 	 * framework callback.
5146 	 */
5147 	if (synch) {
5148 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5149 		    "synchronous execution status %x\n",
5150 		    spx->txlt_sata_pkt->satapkt_reason);
5151 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5152 	}
5153 	return (TRAN_ACCEPT);
5154 }
5155 
5156 
5157 /*
5158  * SATA translate command: Write (various types)
5159  * Translated into appropriate type of ATA WRITE command
5160  * for SATA hard disks.
5161  * Both the device capabilities and requested operation mode are
5162  * considered.
5163  *
5164  * Following scsi cdb fields are ignored:
5165  * rwprotect, dpo, fua, fua_nv, group_number.
5166  *
5167  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5168  * enable variable sata_func_enable), the capability of the controller and
5169  * capability of a device are checked and if both support queueing, write
5170  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
5171  * command rather than plain WRITE_XXX command.
5172  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5173  * both the controller and device suport such functionality, the write
5174  * request will be translated to WRITE_FPDMA_QUEUED command.
5175  * In both cases the maximum queue depth is derived as minimum of:
5176  * HBA capability,device capability and sata_max_queue_depth variable setting.
5177  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5178  * used to pass max queue depth value, and the maximum possible queue depth
5179  * is 32.
5180  *
5181  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5182  * appropriate values in scsi_pkt fields.
5183  */
5184 static int
5185 sata_txlt_write(sata_pkt_txlate_t *spx)
5186 {
5187 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5188 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5189 	sata_drive_info_t *sdinfo;
5190 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5191 	int cport = SATA_TXLT_CPORT(spx);
5192 	uint16_t sec_count;
5193 	uint64_t lba;
5194 	int rval, reason;
5195 	int synch;
5196 
5197 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5198 
5199 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5200 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5201 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5202 		return (rval);
5203 	}
5204 
5205 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5206 	    &spx->txlt_sata_pkt->satapkt_device);
5207 
5208 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5209 	/*
5210 	 * Extract LBA and sector count from scsi CDB
5211 	 */
5212 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5213 	case SCMD_WRITE:
5214 		/* 6-byte scsi read cmd : 0x0A */
5215 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5216 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5217 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5218 		sec_count = scsipkt->pkt_cdbp[4];
5219 		/* sec_count 0 will be interpreted as 256 by a device */
5220 		break;
5221 	case SCMD_WRITE_G1:
5222 		/* 10-bytes scsi write command : 0x2A */
5223 		lba = scsipkt->pkt_cdbp[2];
5224 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5225 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5226 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5227 		sec_count = scsipkt->pkt_cdbp[7];
5228 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5229 		break;
5230 	case SCMD_WRITE_G5:
5231 		/* 12-bytes scsi read command : 0xAA */
5232 		lba = scsipkt->pkt_cdbp[2];
5233 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5234 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5235 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5236 		sec_count = scsipkt->pkt_cdbp[6];
5237 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5238 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5239 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5240 		break;
5241 	case SCMD_WRITE_G4:
5242 		/* 16-bytes scsi write command : 0x8A */
5243 		lba = scsipkt->pkt_cdbp[2];
5244 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5245 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5246 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5247 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5248 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5249 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5250 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5251 		sec_count = scsipkt->pkt_cdbp[10];
5252 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5253 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5254 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5255 		break;
5256 	default:
5257 		/* Unsupported command */
5258 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5259 		return (sata_txlt_invalid_command(spx));
5260 	}
5261 
5262 	/*
5263 	 * Check if specified address and length exceeds device capacity
5264 	 */
5265 	if ((lba >= sdinfo->satadrv_capacity) ||
5266 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5267 		/* LBA out of range */
5268 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5269 		return (sata_txlt_lba_out_of_range(spx));
5270 	}
5271 
5272 	/*
5273 	 * For zero-length transfer, emulate good completion of the command
5274 	 * (reasons for rejecting the command were already checked).
5275 	 * No DMA resources were allocated.
5276 	 */
5277 	if (spx->txlt_dma_cookie_list == NULL) {
5278 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5279 		return (sata_emul_rw_completion(spx));
5280 	}
5281 
5282 	/*
5283 	 * Build cmd block depending on the device capability and
5284 	 * requested operation mode.
5285 	 * Do not bother with non-dma mode- we are working only with
5286 	 * devices supporting DMA.
5287 	 */
5288 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5289 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5290 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5291 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5292 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5293 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5294 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5295 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5296 #ifndef __lock_lint
5297 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5298 		scmd->satacmd_lba_high_msb = lba >> 40;
5299 #endif
5300 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5301 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5302 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5303 	}
5304 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5305 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5306 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5307 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5308 	scmd->satacmd_features_reg = 0;
5309 	scmd->satacmd_status_reg = 0;
5310 	scmd->satacmd_error_reg = 0;
5311 
5312 	/*
5313 	 * Check if queueing commands should be used and switch
5314 	 * to appropriate command if possible
5315 	 */
5316 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5317 		boolean_t using_queuing;
5318 
5319 		/* Queuing supported by controller and device? */
5320 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5321 		    (sdinfo->satadrv_features_support &
5322 		    SATA_DEV_F_NCQ) &&
5323 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5324 		    SATA_CTLF_NCQ)) {
5325 			using_queuing = B_TRUE;
5326 
5327 			/* NCQ supported - use FPDMA WRITE */
5328 			scmd->satacmd_cmd_reg =
5329 			    SATAC_WRITE_FPDMA_QUEUED;
5330 			scmd->satacmd_features_reg_ext =
5331 			    scmd->satacmd_sec_count_msb;
5332 			scmd->satacmd_sec_count_msb = 0;
5333 		} else if ((sdinfo->satadrv_features_support &
5334 		    SATA_DEV_F_TCQ) &&
5335 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5336 		    SATA_CTLF_QCMD)) {
5337 			using_queuing = B_TRUE;
5338 
5339 			/* Legacy queueing */
5340 			if (sdinfo->satadrv_features_support &
5341 			    SATA_DEV_F_LBA48) {
5342 				scmd->satacmd_cmd_reg =
5343 				    SATAC_WRITE_DMA_QUEUED_EXT;
5344 				scmd->satacmd_features_reg_ext =
5345 				    scmd->satacmd_sec_count_msb;
5346 				scmd->satacmd_sec_count_msb = 0;
5347 			} else {
5348 				scmd->satacmd_cmd_reg =
5349 				    SATAC_WRITE_DMA_QUEUED;
5350 			}
5351 		} else	/*  NCQ nor legacy queuing not supported */
5352 			using_queuing = B_FALSE;
5353 
5354 		if (using_queuing) {
5355 			scmd->satacmd_features_reg =
5356 			    scmd->satacmd_sec_count_lsb;
5357 			scmd->satacmd_sec_count_lsb = 0;
5358 			scmd->satacmd_flags.sata_queued = B_TRUE;
5359 			/* Set-up maximum queue depth */
5360 			scmd->satacmd_flags.sata_max_queue_depth =
5361 			    sdinfo->satadrv_max_queue_depth - 1;
5362 		} else if (sdinfo->satadrv_features_enabled &
5363 		    SATA_DEV_F_E_UNTAGGED_QING) {
5364 			/*
5365 			 * Although NCQ/TCQ is not enabled, untagged queuing
5366 			 * may be still used.
5367 			 * Set-up the maximum untagged queue depth.
5368 			 * Use controller's queue depth from sata_hba_tran.
5369 			 * SATA HBA drivers may ignore this value and rely on
5370 			 * the internal limits. For drivera that do not
5371 			 * ignore untaged queue depth, limit the value to
5372 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5373 			 * largest value that can be passed via
5374 			 * satacmd_flags.sata_max_queue_depth.
5375 			 */
5376 			scmd->satacmd_flags.sata_max_queue_depth =
5377 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5378 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5379 
5380 		} else {
5381 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5382 		}
5383 	} else
5384 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5385 
5386 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5387 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5388 	    scmd->satacmd_cmd_reg, lba, sec_count);
5389 
5390 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5391 		/* Need callback function */
5392 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5393 		synch = FALSE;
5394 	} else
5395 		synch = TRUE;
5396 
5397 	/* Transfer command to HBA */
5398 	if (sata_hba_start(spx, &rval) != 0) {
5399 		/* Pkt not accepted for execution */
5400 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5401 		return (rval);
5402 	}
5403 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5404 
5405 	/*
5406 	 * If execution is non-synchronous,
5407 	 * a callback function will handle potential errors, translate
5408 	 * the response and will do a callback to a target driver.
5409 	 * If it was synchronous, check execution status using the same
5410 	 * framework callback.
5411 	 */
5412 	if (synch) {
5413 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5414 		    "synchronous execution status %x\n",
5415 		    spx->txlt_sata_pkt->satapkt_reason);
5416 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5417 	}
5418 	return (TRAN_ACCEPT);
5419 }
5420 
5421 
5422 /*
5423  * Implements SCSI SBC WRITE BUFFER command download microcode option
5424  */
5425 static int
5426 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5427 {
5428 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5429 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5430 
5431 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
5432 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5433 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
5434 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5435 
5436 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5437 	struct scsi_extended_sense *sense;
5438 	int rval, mode, sector_count, reason;
5439 	int cport = SATA_TXLT_CPORT(spx);
5440 
5441 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5442 
5443 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5444 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5445 
5446 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5447 
5448 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
5449 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5450 		return (rval);
5451 	}
5452 	/*
5453 	 * If in interrupt context, reject this packet because it would issue
5454 	 * a synchronous command to HBA.
5455 	 */
5456 	if (servicing_interrupt()) {
5457 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
5458 		    "sata_txlt_write_buffer: rejecting command because "
5459 		    "of interrupt context\n", NULL);
5460 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5461 		return (TRAN_BUSY);
5462 	}
5463 
5464 	/* Use synchronous mode */
5465 	spx->txlt_sata_pkt->satapkt_op_mode
5466 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
5467 
5468 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5469 
5470 	scsipkt->pkt_reason = CMD_CMPLT;
5471 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5472 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5473 
5474 	/*
5475 	 * The SCSI to ATA translation specification only calls
5476 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5477 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5478 	 * ATA 8 (draft) got rid of download microcode for temp
5479 	 * and it is even optional for ATA 7, so it may be aborted.
5480 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5481 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5482 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5483 	 * sectors.  Thus the offset really doesn't buy us anything.
5484 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5485 	 * is revised, this can be revisisted.
5486 	 */
5487 	/* Reject not supported request */
5488 	switch (mode) {
5489 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5490 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5491 		break;
5492 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5493 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5494 		break;
5495 	default:
5496 		goto bad_param;
5497 	}
5498 
5499 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5500 
5501 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5502 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5503 		goto bad_param;
5504 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5505 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5506 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5507 	scmd->satacmd_lba_mid_lsb = 0;
5508 	scmd->satacmd_lba_high_lsb = 0;
5509 	scmd->satacmd_device_reg = 0;
5510 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5511 	scmd->satacmd_addr_type = 0;
5512 
5513 	/* Transfer command to HBA */
5514 	if (sata_hba_start(spx, &rval) != 0) {
5515 		/* Pkt not accepted for execution */
5516 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5517 		return (rval);
5518 	}
5519 
5520 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5521 
5522 	/* Then we need synchronous check the status of the disk */
5523 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5524 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5525 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5526 		scsipkt->pkt_reason = CMD_CMPLT;
5527 
5528 		/* Download commmand succeed, so probe and identify device */
5529 		sata_reidentify_device(spx);
5530 	} else {
5531 		/* Something went wrong, microcode download command failed */
5532 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5533 		*scsipkt->pkt_scbp = STATUS_CHECK;
5534 		sense = sata_arq_sense(spx);
5535 		switch (sata_pkt->satapkt_reason) {
5536 		case SATA_PKT_PORT_ERROR:
5537 			/*
5538 			 * We have no device data. Assume no data transfered.
5539 			 */
5540 			sense->es_key = KEY_HARDWARE_ERROR;
5541 			break;
5542 
5543 		case SATA_PKT_DEV_ERROR:
5544 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5545 			    SATA_STATUS_ERR) {
5546 				/*
5547 				 * determine dev error reason from error
5548 				 * reg content
5549 				 */
5550 				sata_decode_device_error(spx, sense);
5551 				break;
5552 			}
5553 			/* No extended sense key - no info available */
5554 			break;
5555 
5556 		case SATA_PKT_TIMEOUT:
5557 			scsipkt->pkt_reason = CMD_TIMEOUT;
5558 			scsipkt->pkt_statistics |=
5559 			    STAT_TIMEOUT | STAT_DEV_RESET;
5560 			/* No extended sense key ? */
5561 			break;
5562 
5563 		case SATA_PKT_ABORTED:
5564 			scsipkt->pkt_reason = CMD_ABORTED;
5565 			scsipkt->pkt_statistics |= STAT_ABORTED;
5566 			/* No extended sense key ? */
5567 			break;
5568 
5569 		case SATA_PKT_RESET:
5570 			/* pkt aborted by an explicit reset from a host */
5571 			scsipkt->pkt_reason = CMD_RESET;
5572 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5573 			break;
5574 
5575 		default:
5576 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5577 			    "sata_txlt_nodata_cmd_completion: "
5578 			    "invalid packet completion reason %d",
5579 			    sata_pkt->satapkt_reason));
5580 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5581 			break;
5582 		}
5583 
5584 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5585 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5586 
5587 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5588 			/* scsi callback required */
5589 			scsi_hba_pkt_comp(scsipkt);
5590 	}
5591 	return (TRAN_ACCEPT);
5592 
5593 bad_param:
5594 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5595 	*scsipkt->pkt_scbp = STATUS_CHECK;
5596 	sense = sata_arq_sense(spx);
5597 	sense->es_key = KEY_ILLEGAL_REQUEST;
5598 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5599 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5600 	    scsipkt->pkt_comp != NULL) {
5601 		/* scsi callback required */
5602 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5603 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5604 		    TQ_SLEEP) == 0) {
5605 			/* Scheduling the callback failed */
5606 			rval = TRAN_BUSY;
5607 		}
5608 	}
5609 	return (rval);
5610 }
5611 
5612 /*
5613  * Re-identify device after doing a firmware download.
5614  */
5615 static void
5616 sata_reidentify_device(sata_pkt_txlate_t *spx)
5617 {
5618 #define	DOWNLOAD_WAIT_TIME_SECS	60
5619 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5620 	int rval;
5621 	int retry_cnt;
5622 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5623 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5624 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5625 	sata_drive_info_t *sdinfo;
5626 
5627 	/*
5628 	 * Before returning good status, probe device.
5629 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5630 	 * The assumption is that the new microcode is applied by the
5631 	 * device. It is a caller responsibility to verify this.
5632 	 */
5633 	for (retry_cnt = 0;
5634 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5635 	    retry_cnt++) {
5636 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5637 
5638 		if (rval == SATA_SUCCESS) { /* Set default features */
5639 			sdinfo = sata_get_device_info(sata_hba_inst,
5640 			    &sata_device);
5641 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5642 			    SATA_SUCCESS) {
5643 				/* retry */
5644 				rval = sata_initialize_device(sata_hba_inst,
5645 				    sdinfo);
5646 				if (rval == SATA_RETRY)
5647 					sata_log(sata_hba_inst, CE_WARN,
5648 					    "SATA device at port %d pmport %d -"
5649 					    " default device features could not"
5650 					    " be set. Device may not operate "
5651 					    "as expected.",
5652 					    sata_device.satadev_addr.cport,
5653 					    sata_device.satadev_addr.pmport);
5654 			}
5655 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5656 				scsi_hba_pkt_comp(scsipkt);
5657 			return;
5658 		} else if (rval == SATA_RETRY) {
5659 			delay(drv_usectohz(1000000 *
5660 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5661 			continue;
5662 		} else	/* failed - no reason to retry */
5663 			break;
5664 	}
5665 
5666 	/*
5667 	 * Something went wrong, device probing failed.
5668 	 */
5669 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5670 	    "Cannot probe device after downloading microcode\n"));
5671 
5672 	/* Reset device to force retrying the probe. */
5673 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5674 	    (SATA_DIP(sata_hba_inst), &sata_device);
5675 
5676 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5677 		scsi_hba_pkt_comp(scsipkt);
5678 }
5679 
5680 
5681 /*
5682  * Translate command: Synchronize Cache.
5683  * Translates into Flush Cache command for SATA hard disks.
5684  *
5685  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5686  * appropriate values in scsi_pkt fields.
5687  */
5688 static 	int
5689 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5690 {
5691 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5692 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5693 	int cport = SATA_TXLT_CPORT(spx);
5694 	int rval, reason;
5695 	int synch;
5696 
5697 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5698 
5699 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5700 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5701 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5702 		return (rval);
5703 	}
5704 
5705 	scmd->satacmd_addr_type = 0;
5706 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5707 	scmd->satacmd_device_reg = 0;
5708 	scmd->satacmd_sec_count_lsb = 0;
5709 	scmd->satacmd_lba_low_lsb = 0;
5710 	scmd->satacmd_lba_mid_lsb = 0;
5711 	scmd->satacmd_lba_high_lsb = 0;
5712 	scmd->satacmd_features_reg = 0;
5713 	scmd->satacmd_status_reg = 0;
5714 	scmd->satacmd_error_reg = 0;
5715 
5716 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5717 	    "sata_txlt_synchronize_cache\n", NULL);
5718 
5719 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5720 		/* Need to set-up a callback function */
5721 		spx->txlt_sata_pkt->satapkt_comp =
5722 		    sata_txlt_nodata_cmd_completion;
5723 		synch = FALSE;
5724 	} else
5725 		synch = TRUE;
5726 
5727 	/* Transfer command to HBA */
5728 	if (sata_hba_start(spx, &rval) != 0) {
5729 		/* Pkt not accepted for execution */
5730 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5731 		return (rval);
5732 	}
5733 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5734 
5735 	/*
5736 	 * If execution non-synchronous, it had to be completed
5737 	 * a callback function will handle potential errors, translate
5738 	 * the response and will do a callback to a target driver.
5739 	 * If it was synchronous, check status, using the same
5740 	 * framework callback.
5741 	 */
5742 	if (synch) {
5743 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5744 		    "synchronous execution status %x\n",
5745 		    spx->txlt_sata_pkt->satapkt_reason);
5746 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5747 	}
5748 	return (TRAN_ACCEPT);
5749 }
5750 
5751 
5752 /*
5753  * Send pkt to SATA HBA driver
5754  *
5755  * This function may be called only if the operation is requested by scsi_pkt,
5756  * i.e. scsi_pkt is not NULL.
5757  *
5758  * This function has to be called with cport mutex held. It does release
5759  * the mutex when it calls HBA driver sata_tran_start function and
5760  * re-acquires it afterwards.
5761  *
5762  * If return value is 0, pkt was accepted, -1 otherwise
5763  * rval is set to appropriate sata_scsi_start return value.
5764  *
5765  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5766  * have called the sata_pkt callback function for this packet.
5767  *
5768  * The scsi callback has to be performed by the caller of this routine.
5769  *
5770  * Note 2: No port multiplier support for now.
5771  */
5772 static int
5773 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5774 {
5775 	int stat, cport;
5776 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5777 	sata_drive_info_t *sdinfo;
5778 	sata_device_t *sata_device;
5779 	uint8_t cmd;
5780 	struct sata_cmd_flags cmd_flags;
5781 
5782 	ASSERT(spx->txlt_sata_pkt != NULL);
5783 
5784 	cport = SATA_TXLT_CPORT(spx);
5785 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5786 
5787 	sdinfo = sata_get_device_info(sata_hba_inst,
5788 	    &spx->txlt_sata_pkt->satapkt_device);
5789 	ASSERT(sdinfo != NULL);
5790 
5791 	/* Clear device reset state? */
5792 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5793 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5794 		    sata_clear_dev_reset = B_TRUE;
5795 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5796 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5797 		    "sata_hba_start: clearing device reset state\n", NULL);
5798 	}
5799 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5800 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5801 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5802 
5803 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5804 
5805 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5806 	    "Sata cmd 0x%2x\n", cmd);
5807 
5808 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5809 	    spx->txlt_sata_pkt);
5810 
5811 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5812 	/*
5813 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5814 	 * with the sata callback, the sata_pkt could be already destroyed
5815 	 * by the time we check ther return status from the hba_start()
5816 	 * function, because sata_scsi_destroy_pkt() could have been already
5817 	 * called (perhaps in the interrupt context). So, in such case, there
5818 	 * should be no references to it. In other cases, sata_pkt still
5819 	 * exists.
5820 	 */
5821 	if (stat == SATA_TRAN_ACCEPTED) {
5822 		/*
5823 		 * pkt accepted for execution.
5824 		 * If it was executed synchronously, it is already completed
5825 		 * and pkt completion_reason indicates completion status.
5826 		 */
5827 		*rval = TRAN_ACCEPT;
5828 		return (0);
5829 	}
5830 
5831 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5832 	switch (stat) {
5833 	case SATA_TRAN_QUEUE_FULL:
5834 		/*
5835 		 * Controller detected queue full condition.
5836 		 */
5837 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5838 		    "sata_hba_start: queue full\n", NULL);
5839 
5840 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5841 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5842 
5843 		*rval = TRAN_BUSY;
5844 		break;
5845 
5846 	case SATA_TRAN_PORT_ERROR:
5847 		/*
5848 		 * Communication/link with device or general port error
5849 		 * detected before pkt execution begun.
5850 		 */
5851 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5852 		    SATA_ADDR_CPORT ||
5853 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5854 		    SATA_ADDR_DCPORT)
5855 			sata_log(sata_hba_inst, CE_CONT,
5856 			    "SATA port %d error",
5857 			    sata_device->satadev_addr.cport);
5858 		else
5859 			sata_log(sata_hba_inst, CE_CONT,
5860 			    "SATA port %d pmport %d error\n",
5861 			    sata_device->satadev_addr.cport,
5862 			    sata_device->satadev_addr.pmport);
5863 
5864 		/*
5865 		 * Update the port/device structure.
5866 		 * sata_pkt should be still valid. Since port error is
5867 		 * returned, sata_device content should reflect port
5868 		 * state - it means, that sata address have been changed,
5869 		 * because original packet's sata address refered to a device
5870 		 * attached to some port.
5871 		 */
5872 		sata_update_port_info(sata_hba_inst, sata_device);
5873 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5874 		*rval = TRAN_FATAL_ERROR;
5875 		break;
5876 
5877 	case SATA_TRAN_CMD_UNSUPPORTED:
5878 		/*
5879 		 * Command rejected by HBA as unsupported. It was HBA driver
5880 		 * that rejected the command, command was not sent to
5881 		 * an attached device.
5882 		 */
5883 		if ((sdinfo != NULL) &&
5884 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5885 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5886 			    "sat_hba_start: cmd 0x%2x rejected "
5887 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5888 
5889 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5890 		(void) sata_txlt_invalid_command(spx);
5891 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5892 
5893 		*rval = TRAN_ACCEPT;
5894 		break;
5895 
5896 	case SATA_TRAN_BUSY:
5897 		/*
5898 		 * Command rejected by HBA because other operation prevents
5899 		 * accepting the packet, or device is in RESET condition.
5900 		 */
5901 		if (sdinfo != NULL) {
5902 			sdinfo->satadrv_state =
5903 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5904 
5905 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5906 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5907 				    "sata_hba_start: cmd 0x%2x rejected "
5908 				    "because of device reset condition\n",
5909 				    cmd);
5910 			} else {
5911 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5912 				    "sata_hba_start: cmd 0x%2x rejected "
5913 				    "with SATA_TRAN_BUSY status\n",
5914 				    cmd);
5915 			}
5916 		}
5917 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5918 		*rval = TRAN_BUSY;
5919 		break;
5920 
5921 	default:
5922 		/* Unrecognized HBA response */
5923 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5924 		    "sata_hba_start: unrecognized HBA response "
5925 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5926 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5927 		*rval = TRAN_FATAL_ERROR;
5928 		break;
5929 	}
5930 
5931 	/*
5932 	 * If we got here, the packet was rejected.
5933 	 * Check if we need to remember reset state clearing request
5934 	 */
5935 	if (cmd_flags.sata_clear_dev_reset) {
5936 		/*
5937 		 * Check if device is still configured - it may have
5938 		 * disapeared from the configuration
5939 		 */
5940 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5941 		if (sdinfo != NULL) {
5942 			/*
5943 			 * Restore the flag that requests clearing of
5944 			 * the device reset state,
5945 			 * so the next sata packet may carry it to HBA.
5946 			 */
5947 			sdinfo->satadrv_event_flags |=
5948 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5949 		}
5950 	}
5951 	return (-1);
5952 }
5953 
5954 /*
5955  * Scsi response setup for invalid LBA
5956  *
5957  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5958  */
5959 static int
5960 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5961 {
5962 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5963 	struct scsi_extended_sense *sense;
5964 
5965 	scsipkt->pkt_reason = CMD_CMPLT;
5966 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5967 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5968 	*scsipkt->pkt_scbp = STATUS_CHECK;
5969 
5970 	*scsipkt->pkt_scbp = STATUS_CHECK;
5971 	sense = sata_arq_sense(spx);
5972 	sense->es_key = KEY_ILLEGAL_REQUEST;
5973 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5974 
5975 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5976 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5977 
5978 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5979 	    scsipkt->pkt_comp != NULL)
5980 		/* scsi callback required */
5981 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5982 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5983 		    TQ_SLEEP) == NULL)
5984 			/* Scheduling the callback failed */
5985 			return (TRAN_BUSY);
5986 	return (TRAN_ACCEPT);
5987 }
5988 
5989 
5990 /*
5991  * Analyze device status and error registers and translate them into
5992  * appropriate scsi sense codes.
5993  * NOTE: non-packet commands only for now
5994  */
5995 static void
5996 sata_decode_device_error(sata_pkt_txlate_t *spx,
5997     struct scsi_extended_sense *sense)
5998 {
5999 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
6000 
6001 	ASSERT(sense != NULL);
6002 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6003 	    SATA_STATUS_ERR);
6004 
6005 
6006 	if (err_reg & SATA_ERROR_ICRC) {
6007 		sense->es_key = KEY_ABORTED_COMMAND;
6008 		sense->es_add_code = 0x08; /* Communication failure */
6009 		return;
6010 	}
6011 
6012 	if (err_reg & SATA_ERROR_UNC) {
6013 		sense->es_key = KEY_MEDIUM_ERROR;
6014 		/* Information bytes (LBA) need to be set by a caller */
6015 		return;
6016 	}
6017 
6018 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6019 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6020 		sense->es_key = KEY_UNIT_ATTENTION;
6021 		sense->es_add_code = 0x3a; /* No media present */
6022 		return;
6023 	}
6024 
6025 	if (err_reg & SATA_ERROR_IDNF) {
6026 		if (err_reg & SATA_ERROR_ABORT) {
6027 			sense->es_key = KEY_ABORTED_COMMAND;
6028 		} else {
6029 			sense->es_key = KEY_ILLEGAL_REQUEST;
6030 			sense->es_add_code = 0x21; /* LBA out of range */
6031 		}
6032 		return;
6033 	}
6034 
6035 	if (err_reg & SATA_ERROR_ABORT) {
6036 		ASSERT(spx->txlt_sata_pkt != NULL);
6037 		sense->es_key = KEY_ABORTED_COMMAND;
6038 		return;
6039 	}
6040 }
6041 
6042 /*
6043  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6044  */
6045 static void
6046 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6047 {
6048 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6049 
6050 	*lba = 0;
6051 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6052 		*lba = sata_cmd->satacmd_lba_high_msb;
6053 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6054 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6055 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6056 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6057 	}
6058 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6059 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6060 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6061 }
6062 
6063 /*
6064  * This is fixed sense format - if LBA exceeds the info field size,
6065  * no valid info will be returned (valid bit in extended sense will
6066  * be set to 0).
6067  */
6068 static struct scsi_extended_sense *
6069 sata_arq_sense(sata_pkt_txlate_t *spx)
6070 {
6071 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6072 	struct scsi_arq_status *arqs;
6073 	struct scsi_extended_sense *sense;
6074 
6075 	/* Fill ARQ sense data */
6076 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6077 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6078 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6079 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6080 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6081 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6082 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6083 	arqs->sts_rqpkt_resid = 0;
6084 	sense = &arqs->sts_sensedata;
6085 	bzero(sense, sizeof (struct scsi_extended_sense));
6086 	sata_fixed_sense_data_preset(sense);
6087 	return (sense);
6088 }
6089 
6090 
6091 /*
6092  * Emulated SATA Read/Write command completion for zero-length requests.
6093  * This request always succedes, so in synchronous mode it always returns
6094  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6095  * callback cannot be scheduled.
6096  */
6097 static int
6098 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6099 {
6100 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6101 
6102 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6103 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6104 	scsipkt->pkt_reason = CMD_CMPLT;
6105 	*scsipkt->pkt_scbp = STATUS_GOOD;
6106 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6107 		/* scsi callback required - have to schedule it */
6108 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6109 		    (task_func_t *)scsipkt->pkt_comp,
6110 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6111 			/* Scheduling the callback failed */
6112 			return (TRAN_BUSY);
6113 	}
6114 	return (TRAN_ACCEPT);
6115 }
6116 
6117 
6118 /*
6119  * Translate completion status of SATA read/write commands into scsi response.
6120  * pkt completion_reason is checked to determine the completion status.
6121  * Do scsi callback if necessary.
6122  *
6123  * Note: this function may be called also for synchronously executed
6124  * commands.
6125  * This function may be used only if scsi_pkt is non-NULL.
6126  */
6127 static void
6128 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6129 {
6130 	sata_pkt_txlate_t *spx =
6131 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6132 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6133 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6134 	struct scsi_extended_sense *sense;
6135 	uint64_t lba;
6136 	struct buf *bp;
6137 	int rval;
6138 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6139 		/* Normal completion */
6140 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6141 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6142 		scsipkt->pkt_reason = CMD_CMPLT;
6143 		*scsipkt->pkt_scbp = STATUS_GOOD;
6144 		if (spx->txlt_tmp_buf != NULL) {
6145 			/* Temporary buffer was used */
6146 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6147 			if (bp->b_flags & B_READ) {
6148 				rval = ddi_dma_sync(
6149 				    spx->txlt_buf_dma_handle, 0, 0,
6150 				    DDI_DMA_SYNC_FORCPU);
6151 				ASSERT(rval == DDI_SUCCESS);
6152 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6153 				    bp->b_bcount);
6154 			}
6155 		}
6156 	} else {
6157 		/*
6158 		 * Something went wrong - analyze return
6159 		 */
6160 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6161 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6162 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6163 		*scsipkt->pkt_scbp = STATUS_CHECK;
6164 		sense = sata_arq_sense(spx);
6165 		ASSERT(sense != NULL);
6166 
6167 		/*
6168 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6169 		 * extract from device registers the failing LBA.
6170 		 */
6171 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6172 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6173 			    (scmd->satacmd_lba_mid_msb != 0 ||
6174 			    scmd->satacmd_lba_high_msb != 0)) {
6175 				/*
6176 				 * We have problem reporting this cmd LBA
6177 				 * in fixed sense data format, because of
6178 				 * the size of the scsi LBA fields.
6179 				 */
6180 				sense->es_valid = 0;
6181 			} else {
6182 				sata_extract_error_lba(spx, &lba);
6183 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6184 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6185 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6186 				sense->es_info_4 = lba & 0xFF;
6187 			}
6188 		} else {
6189 			/* Invalid extended sense info */
6190 			sense->es_valid = 0;
6191 		}
6192 
6193 		switch (sata_pkt->satapkt_reason) {
6194 		case SATA_PKT_PORT_ERROR:
6195 			/* We may want to handle DEV GONE state as well */
6196 			/*
6197 			 * We have no device data. Assume no data transfered.
6198 			 */
6199 			sense->es_key = KEY_HARDWARE_ERROR;
6200 			break;
6201 
6202 		case SATA_PKT_DEV_ERROR:
6203 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6204 			    SATA_STATUS_ERR) {
6205 				/*
6206 				 * determine dev error reason from error
6207 				 * reg content
6208 				 */
6209 				sata_decode_device_error(spx, sense);
6210 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6211 					switch (scmd->satacmd_cmd_reg) {
6212 					case SATAC_READ_DMA:
6213 					case SATAC_READ_DMA_EXT:
6214 					case SATAC_READ_DMA_QUEUED:
6215 					case SATAC_READ_DMA_QUEUED_EXT:
6216 					case SATAC_READ_FPDMA_QUEUED:
6217 						/* Unrecovered read error */
6218 						sense->es_add_code =
6219 						    SD_SCSI_ASC_UNREC_READ_ERR;
6220 						break;
6221 					case SATAC_WRITE_DMA:
6222 					case SATAC_WRITE_DMA_EXT:
6223 					case SATAC_WRITE_DMA_QUEUED:
6224 					case SATAC_WRITE_DMA_QUEUED_EXT:
6225 					case SATAC_WRITE_FPDMA_QUEUED:
6226 						/* Write error */
6227 						sense->es_add_code =
6228 						    SD_SCSI_ASC_WRITE_ERR;
6229 						break;
6230 					default:
6231 						/* Internal error */
6232 						SATA_LOG_D((
6233 						    spx->txlt_sata_hba_inst,
6234 						    CE_WARN,
6235 						    "sata_txlt_rw_completion :"
6236 						    "internal error - invalid "
6237 						    "command 0x%2x",
6238 						    scmd->satacmd_cmd_reg));
6239 						break;
6240 					}
6241 				}
6242 				break;
6243 			}
6244 			/* No extended sense key - no info available */
6245 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6246 			break;
6247 
6248 		case SATA_PKT_TIMEOUT:
6249 			scsipkt->pkt_reason = CMD_TIMEOUT;
6250 			scsipkt->pkt_statistics |=
6251 			    STAT_TIMEOUT | STAT_DEV_RESET;
6252 			sense->es_key = KEY_ABORTED_COMMAND;
6253 			break;
6254 
6255 		case SATA_PKT_ABORTED:
6256 			scsipkt->pkt_reason = CMD_ABORTED;
6257 			scsipkt->pkt_statistics |= STAT_ABORTED;
6258 			sense->es_key = KEY_ABORTED_COMMAND;
6259 			break;
6260 
6261 		case SATA_PKT_RESET:
6262 			scsipkt->pkt_reason = CMD_RESET;
6263 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6264 			sense->es_key = KEY_ABORTED_COMMAND;
6265 			break;
6266 
6267 		default:
6268 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6269 			    "sata_txlt_rw_completion: "
6270 			    "invalid packet completion reason"));
6271 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6272 			break;
6273 		}
6274 	}
6275 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6276 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6277 
6278 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6279 		/* scsi callback required */
6280 		scsi_hba_pkt_comp(scsipkt);
6281 }
6282 
6283 
6284 /*
6285  * Translate completion status of non-data commands (i.e. commands returning
6286  * no data).
6287  * pkt completion_reason is checked to determine the completion status.
6288  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6289  *
6290  * Note: this function may be called also for synchronously executed
6291  * commands.
6292  * This function may be used only if scsi_pkt is non-NULL.
6293  */
6294 
6295 static	void
6296 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6297 {
6298 	sata_pkt_txlate_t *spx =
6299 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6300 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6301 
6302 	sata_set_arq_data(sata_pkt);
6303 
6304 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6305 		/* scsi callback required */
6306 		scsi_hba_pkt_comp(scsipkt);
6307 }
6308 
6309 static	void
6310 sata_set_arq_data(sata_pkt_t *sata_pkt)
6311 {
6312 	sata_pkt_txlate_t *spx =
6313 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6314 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6315 	struct scsi_extended_sense *sense;
6316 
6317 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6318 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6319 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6320 		/* Normal completion */
6321 		scsipkt->pkt_reason = CMD_CMPLT;
6322 		*scsipkt->pkt_scbp = STATUS_GOOD;
6323 	} else {
6324 		/* Something went wrong */
6325 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6326 		*scsipkt->pkt_scbp = STATUS_CHECK;
6327 		sense = sata_arq_sense(spx);
6328 		switch (sata_pkt->satapkt_reason) {
6329 		case SATA_PKT_PORT_ERROR:
6330 			/*
6331 			 * We have no device data. Assume no data transfered.
6332 			 */
6333 			sense->es_key = KEY_HARDWARE_ERROR;
6334 			break;
6335 
6336 		case SATA_PKT_DEV_ERROR:
6337 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6338 			    SATA_STATUS_ERR) {
6339 				/*
6340 				 * determine dev error reason from error
6341 				 * reg content
6342 				 */
6343 				sata_decode_device_error(spx, sense);
6344 				break;
6345 			}
6346 			/* No extended sense key - no info available */
6347 			break;
6348 
6349 		case SATA_PKT_TIMEOUT:
6350 			scsipkt->pkt_reason = CMD_TIMEOUT;
6351 			scsipkt->pkt_statistics |=
6352 			    STAT_TIMEOUT | STAT_DEV_RESET;
6353 			/* No extended sense key ? */
6354 			break;
6355 
6356 		case SATA_PKT_ABORTED:
6357 			scsipkt->pkt_reason = CMD_ABORTED;
6358 			scsipkt->pkt_statistics |= STAT_ABORTED;
6359 			/* No extended sense key ? */
6360 			break;
6361 
6362 		case SATA_PKT_RESET:
6363 			/* pkt aborted by an explicit reset from a host */
6364 			scsipkt->pkt_reason = CMD_RESET;
6365 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6366 			break;
6367 
6368 		default:
6369 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6370 			    "sata_txlt_nodata_cmd_completion: "
6371 			    "invalid packet completion reason %d",
6372 			    sata_pkt->satapkt_reason));
6373 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6374 			break;
6375 		}
6376 
6377 	}
6378 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6379 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6380 }
6381 
6382 
6383 /*
6384  * Build Mode sense R/W recovery page
6385  * NOT IMPLEMENTED
6386  */
6387 
6388 static int
6389 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6390 {
6391 #ifndef __lock_lint
6392 	_NOTE(ARGUNUSED(sdinfo))
6393 	_NOTE(ARGUNUSED(pcntrl))
6394 	_NOTE(ARGUNUSED(buf))
6395 #endif
6396 	return (0);
6397 }
6398 
6399 /*
6400  * Build Mode sense caching page  -  scsi-3 implementation.
6401  * Page length distinguishes previous format from scsi-3 format.
6402  * buf must have space for 0x12 bytes.
6403  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6404  *
6405  */
6406 static int
6407 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6408 {
6409 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6410 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6411 
6412 	/*
6413 	 * Most of the fields are set to 0, being not supported and/or disabled
6414 	 */
6415 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6416 
6417 	/* Saved paramters not supported */
6418 	if (pcntrl == 3)
6419 		return (0);
6420 	if (pcntrl == 0 || pcntrl == 2) {
6421 		/*
6422 		 * For now treat current and default parameters as same
6423 		 * That may have to change, if target driver will complain
6424 		 */
6425 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6426 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6427 
6428 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6429 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
6430 			page->dra = 1;		/* Read Ahead disabled */
6431 			page->rcd = 1;		/* Read Cache disabled */
6432 		}
6433 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
6434 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
6435 			page->wce = 1;		/* Write Cache enabled */
6436 	} else {
6437 		/* Changeable parameters */
6438 		page->mode_page.code = MODEPAGE_CACHING;
6439 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6440 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6441 			page->dra = 1;
6442 			page->rcd = 1;
6443 		}
6444 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
6445 			page->wce = 1;
6446 	}
6447 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6448 	    sizeof (struct mode_page));
6449 }
6450 
6451 /*
6452  * Build Mode sense exception cntrl page
6453  */
6454 static int
6455 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6456 {
6457 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6458 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6459 
6460 	/*
6461 	 * Most of the fields are set to 0, being not supported and/or disabled
6462 	 */
6463 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6464 
6465 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6466 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6467 
6468 	/* Indicate that this is page is saveable */
6469 	page->mode_page.ps = 1;
6470 
6471 	/*
6472 	 * We will return the same data for default, current and saved page.
6473 	 * The only changeable bit is dexcpt and that bit is required
6474 	 * by the ATA specification to be preserved across power cycles.
6475 	 */
6476 	if (pcntrl != 1) {
6477 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6478 		page->mrie = MRIE_ONLY_ON_REQUEST;
6479 	}
6480 	else
6481 		page->dexcpt = 1;	/* Only changeable parameter */
6482 
6483 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
6484 }
6485 
6486 
6487 static int
6488 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6489 {
6490 	struct mode_acoustic_management *page =
6491 	    (struct mode_acoustic_management *)buf;
6492 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6493 
6494 	/*
6495 	 * Most of the fields are set to 0, being not supported and/or disabled
6496 	 */
6497 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6498 
6499 	switch (pcntrl) {
6500 	case P_CNTRL_DEFAULT:
6501 		/*  default paramters not supported */
6502 		return (0);
6503 
6504 	case P_CNTRL_CURRENT:
6505 	case P_CNTRL_SAVED:
6506 		/* Saved and current are supported and are identical */
6507 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6508 		page->mode_page.length =
6509 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6510 		page->mode_page.ps = 1;
6511 
6512 		/* Word 83 indicates if feature is supported */
6513 		/* If feature is not supported */
6514 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6515 			page->acoustic_manag_enable =
6516 			    ACOUSTIC_DISABLED;
6517 		} else {
6518 			page->acoustic_manag_enable =
6519 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6520 			    != 0);
6521 			/* Word 94 inidicates the value */
6522 #ifdef	_LITTLE_ENDIAN
6523 			page->acoustic_manag_level =
6524 			    (uchar_t)sata_id->ai_acoustic;
6525 			page->vendor_recommended_value =
6526 			    sata_id->ai_acoustic >> 8;
6527 #else
6528 			page->acoustic_manag_level =
6529 			    sata_id->ai_acoustic >> 8;
6530 			page->vendor_recommended_value =
6531 			    (uchar_t)sata_id->ai_acoustic;
6532 #endif
6533 		}
6534 		break;
6535 
6536 	case P_CNTRL_CHANGEABLE:
6537 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6538 		page->mode_page.length =
6539 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6540 		page->mode_page.ps = 1;
6541 
6542 		/* Word 83 indicates if the feature is supported */
6543 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6544 			page->acoustic_manag_enable =
6545 			    ACOUSTIC_ENABLED;
6546 			page->acoustic_manag_level = 0xff;
6547 		}
6548 		break;
6549 	}
6550 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6551 	    sizeof (struct mode_page));
6552 }
6553 
6554 
6555 /*
6556  * Build Mode sense power condition page.
6557  */
6558 static int
6559 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6560 {
6561 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
6562 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6563 
6564 	/*
6565 	 * Most of the fields are set to 0, being not supported and/or disabled
6566 	 * power condition page length was 0x0a
6567 	 */
6568 	bzero(buf, sizeof (struct mode_info_power_cond));
6569 
6570 	if (pcntrl == P_CNTRL_DEFAULT) {
6571 		/*  default paramters not supported */
6572 		return (0);
6573 	}
6574 
6575 	page->mode_page.code = MODEPAGE_POWER_COND;
6576 	page->mode_page.length = sizeof (struct mode_info_power_cond);
6577 
6578 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
6579 		page->standby = 1;
6580 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
6581 		    sizeof (uchar_t) * 4);
6582 	}
6583 
6584 	return (sizeof (struct mode_info_power_cond));
6585 }
6586 
6587 /*
6588  * Process mode select caching page 8 (scsi3 format only).
6589  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6590  * if these features are supported by the device. If these features are not
6591  * supported, the command will be terminated with STATUS_CHECK.
6592  * This function fails only if the SET FEATURE command sent to
6593  * the device fails. The page format is not varified, assuming that the
6594  * target driver operates correctly - if parameters length is too short,
6595  * we just drop the page.
6596  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6597  * setting have to be changed.
6598  * SET FEATURE command is executed synchronously, i.e. we wait here until
6599  * it is completed, regardless of the scsi pkt directives.
6600  *
6601  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6602  * changing DRA will change RCD.
6603  *
6604  * More than one SATA command may be executed to perform operations specified
6605  * by mode select pages. The first error terminates further execution.
6606  * Operations performed successully are not backed-up in such case.
6607  *
6608  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6609  * If operation resulted in changing device setup, dmod flag should be set to
6610  * one (1). If parameters were not changed, dmod flag should be set to 0.
6611  * Upon return, if operation required sending command to the device, the rval
6612  * should be set to the value returned by sata_hba_start. If operation
6613  * did not require device access, rval should be set to TRAN_ACCEPT.
6614  * The pagelen should be set to the length of the page.
6615  *
6616  * This function has to be called with a port mutex held.
6617  *
6618  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6619  */
6620 int
6621 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6622     int parmlen, int *pagelen, int *rval, int *dmod)
6623 {
6624 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6625 	sata_drive_info_t *sdinfo;
6626 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6627 	sata_id_t *sata_id;
6628 	struct scsi_extended_sense *sense;
6629 	int wce, dra;	/* Current settings */
6630 
6631 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6632 	    &spx->txlt_sata_pkt->satapkt_device);
6633 	sata_id = &sdinfo->satadrv_id;
6634 	*dmod = 0;
6635 
6636 	/* Verify parameters length. If too short, drop it */
6637 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6638 	    sizeof (struct mode_page)) > parmlen) {
6639 		*scsipkt->pkt_scbp = STATUS_CHECK;
6640 		sense = sata_arq_sense(spx);
6641 		sense->es_key = KEY_ILLEGAL_REQUEST;
6642 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6643 		*pagelen = parmlen;
6644 		*rval = TRAN_ACCEPT;
6645 		return (SATA_FAILURE);
6646 	}
6647 
6648 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6649 
6650 	/* Current setting of Read Ahead (and Read Cache) */
6651 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
6652 		dra = 0;	/* 0 == not disabled */
6653 	else
6654 		dra = 1;
6655 	/* Current setting of Write Cache */
6656 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
6657 		wce = 1;
6658 	else
6659 		wce = 0;
6660 
6661 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6662 		/* nothing to do */
6663 		*rval = TRAN_ACCEPT;
6664 		return (SATA_SUCCESS);
6665 	}
6666 
6667 	/*
6668 	 * Need to flip some setting
6669 	 * Set-up Internal SET FEATURES command(s)
6670 	 */
6671 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6672 	scmd->satacmd_addr_type = 0;
6673 	scmd->satacmd_device_reg = 0;
6674 	scmd->satacmd_status_reg = 0;
6675 	scmd->satacmd_error_reg = 0;
6676 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6677 	if (page->dra != dra || page->rcd != dra) {
6678 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6679 			/* Need to flip read ahead setting */
6680 			if (dra == 0)
6681 				/* Disable read ahead / read cache */
6682 				scmd->satacmd_features_reg =
6683 				    SATAC_SF_DISABLE_READ_AHEAD;
6684 			else
6685 				/* Enable read ahead  / read cache */
6686 				scmd->satacmd_features_reg =
6687 				    SATAC_SF_ENABLE_READ_AHEAD;
6688 
6689 			/* Transfer command to HBA */
6690 			if (sata_hba_start(spx, rval) != 0)
6691 				/*
6692 				 * Pkt not accepted for execution.
6693 				 */
6694 				return (SATA_FAILURE);
6695 
6696 			*dmod = 1;
6697 
6698 			/* Now process return */
6699 			if (spx->txlt_sata_pkt->satapkt_reason !=
6700 			    SATA_PKT_COMPLETED) {
6701 				goto failure;	/* Terminate */
6702 			}
6703 		} else {
6704 			*scsipkt->pkt_scbp = STATUS_CHECK;
6705 			sense = sata_arq_sense(spx);
6706 			sense->es_key = KEY_ILLEGAL_REQUEST;
6707 			sense->es_add_code =
6708 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6709 			*pagelen = parmlen;
6710 			*rval = TRAN_ACCEPT;
6711 			return (SATA_FAILURE);
6712 		}
6713 	}
6714 
6715 	/* Note that the packet is not removed, so it could be re-used */
6716 	if (page->wce != wce) {
6717 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
6718 			/* Need to flip Write Cache setting */
6719 			if (page->wce == 1)
6720 				/* Enable write cache */
6721 				scmd->satacmd_features_reg =
6722 				    SATAC_SF_ENABLE_WRITE_CACHE;
6723 			else
6724 				/* Disable write cache */
6725 				scmd->satacmd_features_reg =
6726 				    SATAC_SF_DISABLE_WRITE_CACHE;
6727 
6728 			/* Transfer command to HBA */
6729 			if (sata_hba_start(spx, rval) != 0)
6730 				/*
6731 				 * Pkt not accepted for execution.
6732 				 */
6733 				return (SATA_FAILURE);
6734 
6735 			*dmod = 1;
6736 
6737 			/* Now process return */
6738 			if (spx->txlt_sata_pkt->satapkt_reason !=
6739 			    SATA_PKT_COMPLETED) {
6740 				goto failure;
6741 			}
6742 		} else {
6743 			*scsipkt->pkt_scbp = STATUS_CHECK;
6744 			sense = sata_arq_sense(spx);
6745 			sense->es_key = KEY_ILLEGAL_REQUEST;
6746 			sense->es_add_code =
6747 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6748 			*pagelen = parmlen;
6749 			*rval = TRAN_ACCEPT;
6750 			return (SATA_FAILURE);
6751 		}
6752 	}
6753 	return (SATA_SUCCESS);
6754 
6755 failure:
6756 	sata_xlate_errors(spx);
6757 
6758 	return (SATA_FAILURE);
6759 }
6760 
6761 /*
6762  * Process mode select informational exceptions control page 0x1c
6763  *
6764  * The only changeable bit is dexcpt (disable exceptions).
6765  * MRIE (method of reporting informational exceptions) must be
6766  * "only on request".
6767  * This page applies to informational exceptions that report
6768  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
6769  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
6770  * Informational exception conditions occur as the result of background scan
6771  * errors, background self-test errors, or vendor specific events within a
6772  * logical unit. An informational exception condition may occur asynchronous
6773  * to any commands.
6774  *
6775  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6776  * If operation resulted in changing device setup, dmod flag should be set to
6777  * one (1). If parameters were not changed, dmod flag should be set to 0.
6778  * Upon return, if operation required sending command to the device, the rval
6779  * should be set to the value returned by sata_hba_start. If operation
6780  * did not require device access, rval should be set to TRAN_ACCEPT.
6781  * The pagelen should be set to the length of the page.
6782  *
6783  * This function has to be called with a port mutex held.
6784  *
6785  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6786  *
6787  * Cannot be called in the interrupt context.
6788  */
6789 static	int
6790 sata_mode_select_page_1c(
6791 	sata_pkt_txlate_t *spx,
6792 	struct mode_info_excpt_page *page,
6793 	int parmlen,
6794 	int *pagelen,
6795 	int *rval,
6796 	int *dmod)
6797 {
6798 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6799 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6800 	sata_drive_info_t *sdinfo;
6801 	sata_id_t *sata_id;
6802 	struct scsi_extended_sense *sense;
6803 
6804 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6805 	    &spx->txlt_sata_pkt->satapkt_device);
6806 	sata_id = &sdinfo->satadrv_id;
6807 
6808 	*dmod = 0;
6809 
6810 	/* Verify parameters length. If too short, drop it */
6811 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
6812 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6813 		*scsipkt->pkt_scbp = STATUS_CHECK;
6814 		sense = sata_arq_sense(spx);
6815 		sense->es_key = KEY_ILLEGAL_REQUEST;
6816 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6817 		*pagelen = parmlen;
6818 		*rval = TRAN_ACCEPT;
6819 		return (SATA_FAILURE);
6820 	}
6821 
6822 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6823 
6824 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6825 		*scsipkt->pkt_scbp = STATUS_CHECK;
6826 		sense = sata_arq_sense(spx);
6827 		sense->es_key = KEY_ILLEGAL_REQUEST;
6828 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6829 		*pagelen = parmlen;
6830 		*rval = TRAN_ACCEPT;
6831 		return (SATA_FAILURE);
6832 	}
6833 
6834 	/* If already in the state requested, we are done */
6835 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6836 		/* nothing to do */
6837 		*rval = TRAN_ACCEPT;
6838 		return (SATA_SUCCESS);
6839 	}
6840 
6841 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6842 
6843 	/* Build SMART_ENABLE or SMART_DISABLE command */
6844 	scmd->satacmd_addr_type = 0;		/* N/A */
6845 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6846 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6847 	scmd->satacmd_features_reg = page->dexcpt ?
6848 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6849 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6850 	scmd->satacmd_cmd_reg = SATAC_SMART;
6851 
6852 	/* Transfer command to HBA */
6853 	if (sata_hba_start(spx, rval) != 0)
6854 		/*
6855 		 * Pkt not accepted for execution.
6856 		 */
6857 		return (SATA_FAILURE);
6858 
6859 	*dmod = 1;	/* At least may have been modified */
6860 
6861 	/* Now process return */
6862 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6863 		return (SATA_SUCCESS);
6864 
6865 	/* Packet did not complete successfully */
6866 	sata_xlate_errors(spx);
6867 
6868 	return (SATA_FAILURE);
6869 }
6870 
6871 /*
6872  * Process mode select acoustic management control page 0x30
6873  *
6874  *
6875  * This function has to be called with a port mutex held.
6876  *
6877  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6878  *
6879  * Cannot be called in the interrupt context.
6880  */
6881 int
6882 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6883     mode_acoustic_management *page, int parmlen, int *pagelen,
6884     int *rval, int *dmod)
6885 {
6886 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6887 	sata_drive_info_t *sdinfo;
6888 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6889 	sata_id_t *sata_id;
6890 	struct scsi_extended_sense *sense;
6891 
6892 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6893 	    &spx->txlt_sata_pkt->satapkt_device);
6894 	sata_id = &sdinfo->satadrv_id;
6895 	*dmod = 0;
6896 
6897 	/* If parmlen is too short or the feature is not supported, drop it */
6898 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6899 	    sizeof (struct mode_page)) > parmlen) ||
6900 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6901 		*scsipkt->pkt_scbp = STATUS_CHECK;
6902 		sense = sata_arq_sense(spx);
6903 		sense->es_key = KEY_ILLEGAL_REQUEST;
6904 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6905 		*pagelen = parmlen;
6906 		*rval = TRAN_ACCEPT;
6907 		return (SATA_FAILURE);
6908 	}
6909 
6910 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6911 	    sizeof (struct mode_page);
6912 
6913 	/*
6914 	 * We can enable and disable acoustice management and
6915 	 * set the acoustic management level.
6916 	 */
6917 
6918 	/*
6919 	 * Set-up Internal SET FEATURES command(s)
6920 	 */
6921 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6922 	scmd->satacmd_addr_type = 0;
6923 	scmd->satacmd_device_reg = 0;
6924 	scmd->satacmd_status_reg = 0;
6925 	scmd->satacmd_error_reg = 0;
6926 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6927 	if (page->acoustic_manag_enable) {
6928 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6929 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6930 	} else {	/* disabling acoustic management */
6931 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6932 	}
6933 
6934 	/* Transfer command to HBA */
6935 	if (sata_hba_start(spx, rval) != 0)
6936 		/*
6937 		 * Pkt not accepted for execution.
6938 		 */
6939 		return (SATA_FAILURE);
6940 
6941 	/* Now process return */
6942 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6943 		sata_xlate_errors(spx);
6944 		return (SATA_FAILURE);
6945 	}
6946 
6947 	*dmod = 1;
6948 
6949 	return (SATA_SUCCESS);
6950 }
6951 
6952 /*
6953  * Process mode select power condition page 0x1a
6954  *
6955  * This function has to be called with a port mutex held.
6956  *
6957  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6958  *
6959  * Cannot be called in the interrupt context.
6960  */
6961 int
6962 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
6963     mode_info_power_cond *page, int parmlen, int *pagelen,
6964     int *rval, int *dmod)
6965 {
6966 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6967 	sata_drive_info_t *sdinfo;
6968 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6969 	sata_id_t *sata_id;
6970 	struct scsi_extended_sense *sense;
6971 	uint8_t ata_count;
6972 	int i, len;
6973 
6974 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6975 	    &spx->txlt_sata_pkt->satapkt_device);
6976 	sata_id = &sdinfo->satadrv_id;
6977 	*dmod = 0;
6978 
6979 	len = sizeof (struct mode_info_power_cond);
6980 	len += sizeof (struct mode_page);
6981 
6982 	/* If parmlen is too short or the feature is not supported, drop it */
6983 	if ((len < parmlen) || (page->idle == 1) ||
6984 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
6985 		*scsipkt->pkt_scbp = STATUS_CHECK;
6986 		sense = sata_arq_sense(spx);
6987 		sense->es_key = KEY_ILLEGAL_REQUEST;
6988 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6989 		*pagelen = parmlen;
6990 		*rval = TRAN_ACCEPT;
6991 		return (SATA_FAILURE);
6992 	}
6993 
6994 	*pagelen = len;
6995 
6996 	/*
6997 	 * Set-up Internal STANDBY command(s)
6998 	 */
6999 	if (page->standby == 0)
7000 		goto out;
7001 
7002 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
7003 
7004 	scmd->satacmd_addr_type = 0;
7005 	scmd->satacmd_sec_count_lsb = ata_count;
7006 	scmd->satacmd_lba_low_lsb = 0;
7007 	scmd->satacmd_lba_mid_lsb = 0;
7008 	scmd->satacmd_lba_high_lsb = 0;
7009 	scmd->satacmd_features_reg = 0;
7010 	scmd->satacmd_device_reg = 0;
7011 	scmd->satacmd_status_reg = 0;
7012 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
7013 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
7014 
7015 	/* Transfer command to HBA */
7016 	if (sata_hba_start(spx, rval) != 0) {
7017 		return (SATA_FAILURE);
7018 	} else {
7019 		if ((scmd->satacmd_error_reg != 0) ||
7020 		    (spx->txlt_sata_pkt->satapkt_reason !=
7021 		    SATA_PKT_COMPLETED)) {
7022 			sata_xlate_errors(spx);
7023 			return (SATA_FAILURE);
7024 		}
7025 	}
7026 
7027 	for (i = 0; i < 4; i++) {
7028 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
7029 	}
7030 out:
7031 	*dmod = 1;
7032 	return (SATA_SUCCESS);
7033 }
7034 
7035 /*
7036  * sata_build_lsense_page0() is used to create the
7037  * SCSI LOG SENSE page 0 (supported log pages)
7038  *
7039  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
7040  * (supported log pages, self-test results, informational exceptions
7041  * Sun vendor specific ATA SMART data, and start stop cycle counter).
7042  *
7043  * Takes a sata_drive_info t * and the address of a buffer
7044  * in which to create the page information.
7045  *
7046  * Returns the number of bytes valid in the buffer.
7047  */
7048 static	int
7049 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7050 {
7051 	struct log_parameter *lpp = (struct log_parameter *)buf;
7052 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7053 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7054 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7055 
7056 	lpp->param_code[0] = 0;
7057 	lpp->param_code[1] = 0;
7058 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7059 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7060 
7061 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7062 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7063 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7064 			++num_pages_supported;
7065 		}
7066 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7067 		++num_pages_supported;
7068 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7069 		++num_pages_supported;
7070 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
7071 		++num_pages_supported;
7072 	}
7073 
7074 	lpp->param_len = num_pages_supported;
7075 
7076 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7077 	    num_pages_supported);
7078 }
7079 
7080 /*
7081  * sata_build_lsense_page_10() is used to create the
7082  * SCSI LOG SENSE page 0x10 (self-test results)
7083  *
7084  * Takes a sata_drive_info t * and the address of a buffer
7085  * in which to create the page information as well as a sata_hba_inst_t *.
7086  *
7087  * Returns the number of bytes valid in the buffer.
7088  *
7089  * Note: Self test and SMART data is accessible in device log pages.
7090  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
7091  * of data can be transferred by a single command), or by the General Purpose
7092  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
7093  * - approximately 33MB - can be transferred by a single command.
7094  * The SCT Command response (either error or command) is the same for both
7095  * the SMART and GPL methods of issuing commands.
7096  * This function uses READ LOG EXT command when drive supports LBA48, and
7097  * SMART READ command otherwise.
7098  *
7099  * Since above commands are executed in a synchronous mode, this function
7100  * should not be called in an interrupt context.
7101  */
7102 static	int
7103 sata_build_lsense_page_10(
7104 	sata_drive_info_t *sdinfo,
7105 	uint8_t *buf,
7106 	sata_hba_inst_t *sata_hba_inst)
7107 {
7108 	struct log_parameter *lpp = (struct log_parameter *)buf;
7109 	int rval;
7110 
7111 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7112 		struct smart_ext_selftest_log *ext_selftest_log;
7113 
7114 		ext_selftest_log = kmem_zalloc(
7115 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7116 
7117 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7118 		    ext_selftest_log, 0);
7119 		if (rval == 0) {
7120 			int index, start_index;
7121 			struct smart_ext_selftest_log_entry *entry;
7122 			static const struct smart_ext_selftest_log_entry empty =
7123 			    {0};
7124 			uint16_t block_num;
7125 			int count;
7126 			boolean_t only_one_block = B_FALSE;
7127 
7128 			index = ext_selftest_log->
7129 			    smart_ext_selftest_log_index[0];
7130 			index |= ext_selftest_log->
7131 			    smart_ext_selftest_log_index[1] << 8;
7132 			if (index == 0)
7133 				goto out;
7134 
7135 			--index;	/* Correct for 0 origin */
7136 			start_index = index;	/* remember where we started */
7137 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7138 			if (block_num != 0) {
7139 				rval = sata_ext_smart_selftest_read_log(
7140 				    sata_hba_inst, sdinfo, ext_selftest_log,
7141 				    block_num);
7142 				if (rval != 0)
7143 					goto out;
7144 			}
7145 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7146 			entry =
7147 			    &ext_selftest_log->
7148 			    smart_ext_selftest_log_entries[index];
7149 
7150 			for (count = 1;
7151 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7152 			    ++count) {
7153 				uint8_t status;
7154 				uint8_t code;
7155 				uint8_t sense_key;
7156 				uint8_t add_sense_code;
7157 				uint8_t add_sense_code_qual;
7158 
7159 				/* If this is an unused entry, we are done */
7160 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7161 					/* Broken firmware on some disks */
7162 					if (index + 1 ==
7163 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7164 						--entry;
7165 						--index;
7166 						if (bcmp(entry, &empty,
7167 						    sizeof (empty)) == 0)
7168 							goto out;
7169 					} else
7170 						goto out;
7171 				}
7172 
7173 				if (only_one_block &&
7174 				    start_index == index)
7175 					goto out;
7176 
7177 				lpp->param_code[0] = 0;
7178 				lpp->param_code[1] = count;
7179 				lpp->param_ctrl_flags =
7180 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7181 				lpp->param_len =
7182 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7183 
7184 				status = entry->smart_ext_selftest_log_status;
7185 				status >>= 4;
7186 				switch (status) {
7187 				case 0:
7188 				default:
7189 					sense_key = KEY_NO_SENSE;
7190 					add_sense_code =
7191 					    SD_SCSI_ASC_NO_ADD_SENSE;
7192 					add_sense_code_qual = 0;
7193 					break;
7194 				case 1:
7195 					sense_key = KEY_ABORTED_COMMAND;
7196 					add_sense_code =
7197 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7198 					add_sense_code_qual = SCSI_COMPONENT_81;
7199 					break;
7200 				case 2:
7201 					sense_key = KEY_ABORTED_COMMAND;
7202 					add_sense_code =
7203 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7204 					add_sense_code_qual = SCSI_COMPONENT_82;
7205 					break;
7206 				case 3:
7207 					sense_key = KEY_ABORTED_COMMAND;
7208 					add_sense_code =
7209 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7210 					add_sense_code_qual = SCSI_COMPONENT_83;
7211 					break;
7212 				case 4:
7213 					sense_key = KEY_HARDWARE_ERROR;
7214 					add_sense_code =
7215 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7216 					add_sense_code_qual = SCSI_COMPONENT_84;
7217 					break;
7218 				case 5:
7219 					sense_key = KEY_HARDWARE_ERROR;
7220 					add_sense_code =
7221 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7222 					add_sense_code_qual = SCSI_COMPONENT_85;
7223 					break;
7224 				case 6:
7225 					sense_key = KEY_HARDWARE_ERROR;
7226 					add_sense_code =
7227 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7228 					add_sense_code_qual = SCSI_COMPONENT_86;
7229 					break;
7230 				case 7:
7231 					sense_key = KEY_MEDIUM_ERROR;
7232 					add_sense_code =
7233 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7234 					add_sense_code_qual = SCSI_COMPONENT_87;
7235 					break;
7236 				case 8:
7237 					sense_key = KEY_HARDWARE_ERROR;
7238 					add_sense_code =
7239 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7240 					add_sense_code_qual = SCSI_COMPONENT_88;
7241 					break;
7242 				}
7243 				code = 0;	/* unspecified */
7244 				status |= (code << 4);
7245 				lpp->param_values[0] = status;
7246 				lpp->param_values[1] = 0; /* unspecified */
7247 				lpp->param_values[2] = entry->
7248 				    smart_ext_selftest_log_timestamp[1];
7249 				lpp->param_values[3] = entry->
7250 				    smart_ext_selftest_log_timestamp[0];
7251 				if (status != 0) {
7252 					lpp->param_values[4] = 0;
7253 					lpp->param_values[5] = 0;
7254 					lpp->param_values[6] = entry->
7255 					    smart_ext_selftest_log_failing_lba
7256 					    [5];
7257 					lpp->param_values[7] = entry->
7258 					    smart_ext_selftest_log_failing_lba
7259 					    [4];
7260 					lpp->param_values[8] = entry->
7261 					    smart_ext_selftest_log_failing_lba
7262 					    [3];
7263 					lpp->param_values[9] = entry->
7264 					    smart_ext_selftest_log_failing_lba
7265 					    [2];
7266 					lpp->param_values[10] = entry->
7267 					    smart_ext_selftest_log_failing_lba
7268 					    [1];
7269 					lpp->param_values[11] = entry->
7270 					    smart_ext_selftest_log_failing_lba
7271 					    [0];
7272 				} else {	/* No bad block address */
7273 					lpp->param_values[4] = 0xff;
7274 					lpp->param_values[5] = 0xff;
7275 					lpp->param_values[6] = 0xff;
7276 					lpp->param_values[7] = 0xff;
7277 					lpp->param_values[8] = 0xff;
7278 					lpp->param_values[9] = 0xff;
7279 					lpp->param_values[10] = 0xff;
7280 					lpp->param_values[11] = 0xff;
7281 				}
7282 
7283 				lpp->param_values[12] = sense_key;
7284 				lpp->param_values[13] = add_sense_code;
7285 				lpp->param_values[14] = add_sense_code_qual;
7286 				lpp->param_values[15] = 0; /* undefined */
7287 
7288 				lpp = (struct log_parameter *)
7289 				    (((uint8_t *)lpp) +
7290 				    SCSI_LOG_PARAM_HDR_LEN +
7291 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7292 
7293 				--index;	/* Back up to previous entry */
7294 				if (index < 0) {
7295 					if (block_num > 0) {
7296 						--block_num;
7297 					} else {
7298 						struct read_log_ext_directory
7299 						    logdir;
7300 
7301 						rval =
7302 						    sata_read_log_ext_directory(
7303 						    sata_hba_inst, sdinfo,
7304 						    &logdir);
7305 						if (rval == -1)
7306 							goto out;
7307 						if ((logdir.read_log_ext_vers
7308 						    [0] == 0) &&
7309 						    (logdir.read_log_ext_vers
7310 						    [1] == 0))
7311 							goto out;
7312 						block_num =
7313 						    logdir.read_log_ext_nblks
7314 						    [EXT_SMART_SELFTEST_LOG_PAGE
7315 						    - 1][0];
7316 						block_num |= logdir.
7317 						    read_log_ext_nblks
7318 						    [EXT_SMART_SELFTEST_LOG_PAGE
7319 						    - 1][1] << 8;
7320 						--block_num;
7321 						only_one_block =
7322 						    (block_num == 0);
7323 					}
7324 					rval = sata_ext_smart_selftest_read_log(
7325 					    sata_hba_inst, sdinfo,
7326 					    ext_selftest_log, block_num);
7327 					if (rval != 0)
7328 						goto out;
7329 
7330 					index =
7331 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7332 					    1;
7333 				}
7334 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7335 				entry = &ext_selftest_log->
7336 				    smart_ext_selftest_log_entries[index];
7337 			}
7338 		}
7339 out:
7340 		kmem_free(ext_selftest_log,
7341 		    sizeof (struct smart_ext_selftest_log));
7342 	} else {
7343 		struct smart_selftest_log *selftest_log;
7344 
7345 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7346 		    KM_SLEEP);
7347 
7348 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7349 		    selftest_log);
7350 
7351 		if (rval == 0) {
7352 			int index;
7353 			int count;
7354 			struct smart_selftest_log_entry *entry;
7355 			static const struct smart_selftest_log_entry empty =
7356 			    { 0 };
7357 
7358 			index = selftest_log->smart_selftest_log_index;
7359 			if (index == 0)
7360 				goto done;
7361 			--index;	/* Correct for 0 origin */
7362 			entry = &selftest_log->
7363 			    smart_selftest_log_entries[index];
7364 			for (count = 1;
7365 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7366 			    ++count) {
7367 				uint8_t status;
7368 				uint8_t code;
7369 				uint8_t sense_key;
7370 				uint8_t add_sense_code;
7371 				uint8_t add_sense_code_qual;
7372 
7373 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7374 					goto done;
7375 
7376 				lpp->param_code[0] = 0;
7377 				lpp->param_code[1] = count;
7378 				lpp->param_ctrl_flags =
7379 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7380 				lpp->param_len =
7381 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7382 
7383 				status = entry->smart_selftest_log_status;
7384 				status >>= 4;
7385 				switch (status) {
7386 				case 0:
7387 				default:
7388 					sense_key = KEY_NO_SENSE;
7389 					add_sense_code =
7390 					    SD_SCSI_ASC_NO_ADD_SENSE;
7391 					break;
7392 				case 1:
7393 					sense_key = KEY_ABORTED_COMMAND;
7394 					add_sense_code =
7395 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7396 					add_sense_code_qual = SCSI_COMPONENT_81;
7397 					break;
7398 				case 2:
7399 					sense_key = KEY_ABORTED_COMMAND;
7400 					add_sense_code =
7401 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7402 					add_sense_code_qual = SCSI_COMPONENT_82;
7403 					break;
7404 				case 3:
7405 					sense_key = KEY_ABORTED_COMMAND;
7406 					add_sense_code =
7407 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7408 					add_sense_code_qual = SCSI_COMPONENT_83;
7409 					break;
7410 				case 4:
7411 					sense_key = KEY_HARDWARE_ERROR;
7412 					add_sense_code =
7413 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7414 					add_sense_code_qual = SCSI_COMPONENT_84;
7415 					break;
7416 				case 5:
7417 					sense_key = KEY_HARDWARE_ERROR;
7418 					add_sense_code =
7419 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7420 					add_sense_code_qual = SCSI_COMPONENT_85;
7421 					break;
7422 				case 6:
7423 					sense_key = KEY_HARDWARE_ERROR;
7424 					add_sense_code =
7425 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7426 					add_sense_code_qual = SCSI_COMPONENT_86;
7427 					break;
7428 				case 7:
7429 					sense_key = KEY_MEDIUM_ERROR;
7430 					add_sense_code =
7431 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7432 					add_sense_code_qual = SCSI_COMPONENT_87;
7433 					break;
7434 				case 8:
7435 					sense_key = KEY_HARDWARE_ERROR;
7436 					add_sense_code =
7437 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7438 					add_sense_code_qual = SCSI_COMPONENT_88;
7439 					break;
7440 				}
7441 				code = 0;	/* unspecified */
7442 				status |= (code << 4);
7443 				lpp->param_values[0] = status;
7444 				lpp->param_values[1] = 0; /* unspecified */
7445 				lpp->param_values[2] = entry->
7446 				    smart_selftest_log_timestamp[1];
7447 				lpp->param_values[3] = entry->
7448 				    smart_selftest_log_timestamp[0];
7449 				if (status != 0) {
7450 					lpp->param_values[4] = 0;
7451 					lpp->param_values[5] = 0;
7452 					lpp->param_values[6] = 0;
7453 					lpp->param_values[7] = 0;
7454 					lpp->param_values[8] = entry->
7455 					    smart_selftest_log_failing_lba[3];
7456 					lpp->param_values[9] = entry->
7457 					    smart_selftest_log_failing_lba[2];
7458 					lpp->param_values[10] = entry->
7459 					    smart_selftest_log_failing_lba[1];
7460 					lpp->param_values[11] = entry->
7461 					    smart_selftest_log_failing_lba[0];
7462 				} else {	/* No block address */
7463 					lpp->param_values[4] = 0xff;
7464 					lpp->param_values[5] = 0xff;
7465 					lpp->param_values[6] = 0xff;
7466 					lpp->param_values[7] = 0xff;
7467 					lpp->param_values[8] = 0xff;
7468 					lpp->param_values[9] = 0xff;
7469 					lpp->param_values[10] = 0xff;
7470 					lpp->param_values[11] = 0xff;
7471 				}
7472 				lpp->param_values[12] = sense_key;
7473 				lpp->param_values[13] = add_sense_code;
7474 				lpp->param_values[14] = add_sense_code_qual;
7475 				lpp->param_values[15] = 0; /* undefined */
7476 
7477 				lpp = (struct log_parameter *)
7478 				    (((uint8_t *)lpp) +
7479 				    SCSI_LOG_PARAM_HDR_LEN +
7480 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7481 				--index;	/* back up to previous entry */
7482 				if (index < 0) {
7483 					index =
7484 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7485 				}
7486 				entry = &selftest_log->
7487 				    smart_selftest_log_entries[index];
7488 			}
7489 		}
7490 done:
7491 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7492 	}
7493 
7494 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7495 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7496 }
7497 
7498 /*
7499  * sata_build_lsense_page_2f() is used to create the
7500  * SCSI LOG SENSE page 0x2f (informational exceptions)
7501  *
7502  * Takes a sata_drive_info t * and the address of a buffer
7503  * in which to create the page information as well as a sata_hba_inst_t *.
7504  *
7505  * Returns the number of bytes valid in the buffer.
7506  *
7507  * Because it invokes function(s) that send synchronously executed command
7508  * to the HBA, it cannot be called in the interrupt context.
7509  */
7510 static	int
7511 sata_build_lsense_page_2f(
7512 	sata_drive_info_t *sdinfo,
7513 	uint8_t *buf,
7514 	sata_hba_inst_t *sata_hba_inst)
7515 {
7516 	struct log_parameter *lpp = (struct log_parameter *)buf;
7517 	int rval;
7518 	uint8_t *smart_data;
7519 	uint8_t temp;
7520 	sata_id_t *sata_id;
7521 #define	SMART_NO_TEMP	0xff
7522 
7523 	lpp->param_code[0] = 0;
7524 	lpp->param_code[1] = 0;
7525 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7526 
7527 	/* Now get the SMART status w.r.t. threshold exceeded */
7528 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7529 	switch (rval) {
7530 	case 1:
7531 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7532 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7533 		break;
7534 	case 0:
7535 	case -1:	/* failed to get data */
7536 		lpp->param_values[0] = 0;	/* No failure predicted */
7537 		lpp->param_values[1] = 0;
7538 		break;
7539 #if defined(SATA_DEBUG)
7540 	default:
7541 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7542 		/* NOTREACHED */
7543 #endif
7544 	}
7545 
7546 	sata_id = &sdinfo->satadrv_id;
7547 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7548 		temp = SMART_NO_TEMP;
7549 	else {
7550 		/* Now get the temperature */
7551 		smart_data = kmem_zalloc(512, KM_SLEEP);
7552 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7553 		    SCT_STATUS_LOG_PAGE, 1);
7554 		if (rval == -1)
7555 			temp = SMART_NO_TEMP;
7556 		else {
7557 			temp = smart_data[200];
7558 			if (temp & 0x80) {
7559 				if (temp & 0x7f)
7560 					temp = 0;
7561 				else
7562 					temp = SMART_NO_TEMP;
7563 			}
7564 		}
7565 		kmem_free(smart_data, 512);
7566 	}
7567 
7568 	lpp->param_values[2] = temp;	/* most recent temperature */
7569 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7570 
7571 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7572 
7573 
7574 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7575 }
7576 
7577 /*
7578  * sata_build_lsense_page_30() is used to create the
7579  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7580  *
7581  * Takes a sata_drive_info t * and the address of a buffer
7582  * in which to create the page information as well as a sata_hba_inst_t *.
7583  *
7584  * Returns the number of bytes valid in the buffer.
7585  */
7586 static int
7587 sata_build_lsense_page_30(
7588 	sata_drive_info_t *sdinfo,
7589 	uint8_t *buf,
7590 	sata_hba_inst_t *sata_hba_inst)
7591 {
7592 	struct smart_data *smart_data = (struct smart_data *)buf;
7593 	int rval;
7594 
7595 	/* Now do the SMART READ DATA */
7596 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7597 	if (rval == -1)
7598 		return (0);
7599 
7600 	return (sizeof (struct smart_data));
7601 }
7602 
7603 /*
7604  * sata_build_lsense_page_0e() is used to create the
7605  * SCSI LOG SENSE page 0e (supported log pages)
7606  *
7607  * Date of Manufacture (0x0001)
7608  *	YEAR = "0000"
7609  *	WEEK = "00"
7610  * Accounting Date (0x0002)
7611  *	6 ASCII space character(20h)
7612  * Specified cycle count over device lifetime
7613  *	VALUE - THRESH - the delta between max and min;
7614  * Accumulated start-stop cycles
7615  *	VALUE - WORST - the accumulated cycles;
7616  *
7617  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
7618  *
7619  * Takes a sata_drive_info t * and the address of a buffer
7620  * in which to create the page information as well as a sata_hba_inst_t *.
7621  *
7622  * Returns the number of bytes valid in the buffer.
7623  */
7624 static	int
7625 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
7626 	sata_pkt_txlate_t *spx)
7627 {
7628 	struct start_stop_cycle_counter_log *log_page;
7629 	int i, rval, index;
7630 	uint8_t smart_data[512], id, value, worst, thresh;
7631 	uint32_t max_count, cycles;
7632 
7633 	/* Now do the SMART READ DATA */
7634 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
7635 	    (struct smart_data *)smart_data);
7636 	if (rval == -1)
7637 		return (0);
7638 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
7639 		index = (i * 12) + 2;
7640 		id = smart_data[index];
7641 		if (id != SMART_START_STOP_COUNT_ID)
7642 			continue;
7643 		else {
7644 			thresh = smart_data[index + 2];
7645 			value = smart_data[index + 3];
7646 			worst = smart_data[index + 4];
7647 			break;
7648 		}
7649 	}
7650 	if (id != SMART_START_STOP_COUNT_ID)
7651 		return (0);
7652 	max_count = value - thresh;
7653 	cycles = value - worst;
7654 
7655 	log_page = (struct start_stop_cycle_counter_log *)buf;
7656 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
7657 	log_page->code = 0x0e;
7658 	log_page->page_len_low = 0x24;
7659 
7660 	log_page->manufactor_date_low = 0x1;
7661 	log_page->param_1.fmt_link = 0x1; /* 01b */
7662 	log_page->param_len_1 = 0x06;
7663 	for (i = 0; i < 4; i++) {
7664 		log_page->year_manu[i] = 0x30;
7665 		if (i < 2)
7666 			log_page->week_manu[i] = 0x30;
7667 	}
7668 
7669 	log_page->account_date_low = 0x02;
7670 	log_page->param_2.fmt_link = 0x01; /* 01b */
7671 	log_page->param_len_2 = 0x06;
7672 	for (i = 0; i < 4; i++) {
7673 		log_page->year_account[i] = 0x20;
7674 		if (i < 2)
7675 			log_page->week_account[i] = 0x20;
7676 	}
7677 
7678 	log_page->lifetime_code_low = 0x03;
7679 	log_page->param_3.fmt_link = 0x03; /* 11b */
7680 	log_page->param_len_3 = 0x04;
7681 	/* VALUE - THRESH - the delta between max and min */
7682 	log_page->cycle_code_low = 0x04;
7683 	log_page->param_4.fmt_link = 0x03; /* 11b */
7684 	log_page->param_len_4 = 0x04;
7685 	/* WORST - THRESH - the distance from 'now' to min */
7686 
7687 	for (i = 0; i < 4; i++) {
7688 		log_page->cycle_lifetime[i] =
7689 		    (max_count >> (8 * (3 - i))) & 0xff;
7690 		log_page->cycle_accumulated[i] =
7691 		    (cycles >> (8 * (3 - i))) & 0xff;
7692 	}
7693 
7694 	return (sizeof (struct start_stop_cycle_counter_log));
7695 }
7696 
7697 /*
7698  * This function was used for build a ATA read verify sector command
7699  */
7700 static void
7701 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
7702 {
7703 	scmd->satacmd_cmd_reg = SATAC_RDVER;
7704 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
7705 
7706 	scmd->satacmd_sec_count_lsb = sec & 0xff;
7707 	scmd->satacmd_lba_low_lsb = lba & 0xff;
7708 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
7709 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
7710 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
7711 	scmd->satacmd_features_reg = 0;
7712 	scmd->satacmd_status_reg = 0;
7713 	scmd->satacmd_error_reg = 0;
7714 }
7715 
7716 /*
7717  * This function was used for building an ATA
7718  * command, and only command register need to
7719  * be defined, other register will be zero or na.
7720  */
7721 static void
7722 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
7723 {
7724 	scmd->satacmd_addr_type = 0;
7725 	scmd->satacmd_cmd_reg = cmd;
7726 	scmd->satacmd_device_reg = 0;
7727 	scmd->satacmd_sec_count_lsb = 0;
7728 	scmd->satacmd_lba_low_lsb = 0;
7729 	scmd->satacmd_lba_mid_lsb = 0;
7730 	scmd->satacmd_lba_high_lsb = 0;
7731 	scmd->satacmd_features_reg = 0;
7732 	scmd->satacmd_status_reg = 0;
7733 	scmd->satacmd_error_reg = 0;
7734 }
7735 
7736 /*
7737  * This function was used for changing the standby
7738  * timer format from SCSI to ATA.
7739  */
7740 static uint8_t
7741 sata_get_standby_timer(uint8_t *timer)
7742 {
7743 	uint32_t i = 0, count = 0;
7744 	uint8_t ata_count;
7745 
7746 	for (i = 0; i < 4; i++) {
7747 		count = count << 8 | timer[i];
7748 	}
7749 
7750 	if (count == 0)
7751 		return (0);
7752 
7753 	if (count >= 1 && count <= 12000)
7754 		ata_count = (count -1) / 50 + 1;
7755 	else if (count > 12000 && count <= 12600)
7756 		ata_count = 0xfc;
7757 	else if (count > 12601 && count <= 12750)
7758 		ata_count = 0xff;
7759 	else if (count > 12750 && count <= 17999)
7760 		ata_count = 0xf1;
7761 	else if (count > 18000 && count <= 198000)
7762 		ata_count = count / 18000 + 240;
7763 	else
7764 		ata_count = 0xfd;
7765 	return (ata_count);
7766 }
7767 
7768 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7769 
7770 /*
7771  * Start command for ATAPI device.
7772  * This function processes scsi_pkt requests.
7773  * Now CD/DVD, tape and ATAPI disk devices are supported.
7774  * Most commands are packet without any translation into Packet Command.
7775  * Some may be trapped and executed as SATA commands (not clear which one).
7776  *
7777  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7778  * execution).
7779  * Returns other TRAN_XXXX codes if command is not accepted or completed
7780  * (see return values for sata_hba_start()).
7781  *
7782  * Note:
7783  * Inquiry cdb format differs between transport version 2 and 3.
7784  * However, the transport version 3 devices that were checked did not adhere
7785  * to the specification (ignored MSB of the allocation length). Therefore,
7786  * the transport version is not checked, but Inquiry allocation length is
7787  * truncated to 255 bytes if the original allocation length set-up by the
7788  * target driver is greater than 255 bytes.
7789  */
7790 static int
7791 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7792 {
7793 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7794 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7795 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7796 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7797 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7798 	    &spx->txlt_sata_pkt->satapkt_device);
7799 	int cport = SATA_TXLT_CPORT(spx);
7800 	int cdblen;
7801 	int rval, reason;
7802 	int synch;
7803 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7804 
7805 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7806 
7807 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
7808 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7809 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7810 		return (rval);
7811 	}
7812 
7813 	/*
7814 	 * ATAPI device executes some ATA commands in addition to those
7815 	 * commands sent via PACKET command. These ATA commands may be
7816 	 * executed by the regular SATA translation functions. None needs
7817 	 * to be captured now.
7818 	 *
7819 	 * Commands sent via PACKET command include:
7820 	 *	MMC command set for ATAPI CD/DVD device
7821 	 *	SSC command set for ATAPI TAPE device
7822 	 *	SBC command set for ATAPI disk device
7823 	 *
7824 	 */
7825 
7826 	/* Check the size of cdb */
7827 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7828 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7829 		sata_log(NULL, CE_WARN,
7830 		    "sata: invalid ATAPI cdb length %d",
7831 		    scsipkt->pkt_cdblen);
7832 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7833 		return (TRAN_BADPKT);
7834 	}
7835 
7836 	SATAATAPITRACE(spx, cdblen);
7837 
7838 	/*
7839 	 * For non-read/write commands we need to
7840 	 * map buffer
7841 	 */
7842 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7843 	case SCMD_READ:
7844 	case SCMD_READ_G1:
7845 	case SCMD_READ_G5:
7846 	case SCMD_READ_G4:
7847 	case SCMD_WRITE:
7848 	case SCMD_WRITE_G1:
7849 	case SCMD_WRITE_G5:
7850 	case SCMD_WRITE_G4:
7851 		break;
7852 	default:
7853 		if (bp != NULL) {
7854 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7855 				bp_mapin(bp);
7856 		}
7857 		break;
7858 	}
7859 	/*
7860 	 * scmd->satacmd_flags.sata_data_direction default -
7861 	 * SATA_DIR_NODATA_XFER - is set by
7862 	 * sata_txlt_generic_pkt_info().
7863 	 */
7864 	if (scmd->satacmd_bp) {
7865 		if (scmd->satacmd_bp->b_flags & B_READ) {
7866 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7867 		} else {
7868 			scmd->satacmd_flags.sata_data_direction =
7869 			    SATA_DIR_WRITE;
7870 		}
7871 	}
7872 
7873 	/*
7874 	 * Set up ATAPI packet command.
7875 	 */
7876 
7877 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7878 
7879 	/* Copy cdb into sata_cmd */
7880 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7881 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7882 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7883 
7884 	/* See note in the command header */
7885 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7886 		if (scmd->satacmd_acdb[3] != 0)
7887 			scmd->satacmd_acdb[4] = 255;
7888 	}
7889 
7890 #ifdef SATA_DEBUG
7891 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7892 		uint8_t *p = scmd->satacmd_acdb;
7893 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7894 
7895 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7896 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7897 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7898 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7899 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7900 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7901 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7902 	}
7903 #endif
7904 
7905 	/*
7906 	 * Preset request sense data to NO SENSE.
7907 	 * If there is no way to get error information via Request Sense,
7908 	 * the packet request sense data would not have to be modified by HBA,
7909 	 * but it could be returned as is.
7910 	 */
7911 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7912 	sata_fixed_sense_data_preset(
7913 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7914 
7915 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7916 		/* Need callback function */
7917 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7918 		synch = FALSE;
7919 	} else
7920 		synch = TRUE;
7921 
7922 	/* Transfer command to HBA */
7923 	if (sata_hba_start(spx, &rval) != 0) {
7924 		/* Pkt not accepted for execution */
7925 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7926 		return (rval);
7927 	}
7928 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7929 	/*
7930 	 * If execution is non-synchronous,
7931 	 * a callback function will handle potential errors, translate
7932 	 * the response and will do a callback to a target driver.
7933 	 * If it was synchronous, use the same framework callback to check
7934 	 * an execution status.
7935 	 */
7936 	if (synch) {
7937 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7938 		    "synchronous execution status %x\n",
7939 		    spx->txlt_sata_pkt->satapkt_reason);
7940 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7941 	}
7942 	return (TRAN_ACCEPT);
7943 }
7944 
7945 
7946 /*
7947  * ATAPI Packet command completion.
7948  *
7949  * Failure of the command passed via Packet command are considered device
7950  * error. SATA HBA driver would have to retrieve error data (via Request
7951  * Sense command delivered via error retrieval sata packet) and copy it
7952  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7953  */
7954 static void
7955 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7956 {
7957 	sata_pkt_txlate_t *spx =
7958 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7959 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7960 	struct scsi_extended_sense *sense;
7961 	struct buf *bp;
7962 	int rval;
7963 
7964 #ifdef SATA_DEBUG
7965 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7966 #endif
7967 
7968 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7969 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7970 
7971 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7972 		/* Normal completion */
7973 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7974 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7975 		scsipkt->pkt_reason = CMD_CMPLT;
7976 		*scsipkt->pkt_scbp = STATUS_GOOD;
7977 		if (spx->txlt_tmp_buf != NULL) {
7978 			/* Temporary buffer was used */
7979 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7980 			if (bp->b_flags & B_READ) {
7981 				rval = ddi_dma_sync(
7982 				    spx->txlt_buf_dma_handle, 0, 0,
7983 				    DDI_DMA_SYNC_FORCPU);
7984 				ASSERT(rval == DDI_SUCCESS);
7985 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7986 				    bp->b_bcount);
7987 			}
7988 		}
7989 	} else {
7990 		/*
7991 		 * Something went wrong - analyze return
7992 		 */
7993 		*scsipkt->pkt_scbp = STATUS_CHECK;
7994 		sense = sata_arq_sense(spx);
7995 
7996 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7997 			/*
7998 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
7999 			 * Under this condition ERR bit is set for ATA command,
8000 			 * and CHK bit set for ATAPI command.
8001 			 *
8002 			 * Please check st_intr & sdintr about how pkt_reason
8003 			 * is used.
8004 			 */
8005 			scsipkt->pkt_reason = CMD_CMPLT;
8006 
8007 			/*
8008 			 * We may not have ARQ data if there was a double
8009 			 * error. But sense data in sata packet was pre-set
8010 			 * with NO SENSE so it is valid even if HBA could
8011 			 * not retrieve a real sense data.
8012 			 * Just copy this sense data into scsi pkt sense area.
8013 			 */
8014 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8015 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8016 #ifdef SATA_DEBUG
8017 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8018 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8019 				    "sata_txlt_atapi_completion: %02x\n"
8020 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8021 				    "          %02x %02x %02x %02x %02x %02x "
8022 				    "          %02x %02x %02x %02x %02x %02x\n",
8023 				    scsipkt->pkt_reason,
8024 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8025 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8026 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8027 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8028 				    rqsp[16], rqsp[17]);
8029 			}
8030 #endif
8031 		} else {
8032 			switch (sata_pkt->satapkt_reason) {
8033 			case SATA_PKT_PORT_ERROR:
8034 				/*
8035 				 * We have no device data.
8036 				 */
8037 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8038 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8039 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8040 				    STATE_GOT_STATUS);
8041 				sense->es_key = KEY_HARDWARE_ERROR;
8042 				break;
8043 
8044 			case SATA_PKT_TIMEOUT:
8045 				scsipkt->pkt_reason = CMD_TIMEOUT;
8046 				scsipkt->pkt_statistics |=
8047 				    STAT_TIMEOUT | STAT_DEV_RESET;
8048 				/*
8049 				 * Need to check if HARDWARE_ERROR/
8050 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8051 				 * appropriate.
8052 				 */
8053 				break;
8054 
8055 			case SATA_PKT_ABORTED:
8056 				scsipkt->pkt_reason = CMD_ABORTED;
8057 				scsipkt->pkt_statistics |= STAT_ABORTED;
8058 				/* Should we set key COMMAND_ABPRTED? */
8059 				break;
8060 
8061 			case SATA_PKT_RESET:
8062 				scsipkt->pkt_reason = CMD_RESET;
8063 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
8064 				/*
8065 				 * May be we should set Unit Attention /
8066 				 * Reset. Perhaps the same should be
8067 				 * returned for disks....
8068 				 */
8069 				sense->es_key = KEY_UNIT_ATTENTION;
8070 				sense->es_add_code = SD_SCSI_ASC_RESET;
8071 				break;
8072 
8073 			default:
8074 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8075 				    "sata_txlt_atapi_completion: "
8076 				    "invalid packet completion reason"));
8077 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8078 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8079 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8080 				    STATE_GOT_STATUS);
8081 				break;
8082 			}
8083 		}
8084 	}
8085 
8086 	SATAATAPITRACE(spx, 0);
8087 
8088 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8089 	    scsipkt->pkt_comp != NULL) {
8090 		/* scsi callback required */
8091 		(*scsipkt->pkt_comp)(scsipkt);
8092 	}
8093 }
8094 
8095 /*
8096  * Set up error retrieval sata command for ATAPI Packet Command error data
8097  * recovery.
8098  *
8099  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8100  * returns SATA_FAILURE otherwise.
8101  */
8102 
8103 static int
8104 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8105 {
8106 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8107 	sata_cmd_t *scmd;
8108 	struct buf *bp;
8109 
8110 	/*
8111 	 * Allocate dma-able buffer error data.
8112 	 * Buffer allocation will take care of buffer alignment and other DMA
8113 	 * attributes.
8114 	 */
8115 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8116 	if (bp == NULL) {
8117 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8118 		    "sata_get_err_retrieval_pkt: "
8119 		    "cannot allocate buffer for error data", NULL);
8120 		return (SATA_FAILURE);
8121 	}
8122 	bp_mapin(bp); /* make data buffer accessible */
8123 
8124 	/* Operation modes are up to the caller */
8125 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8126 
8127 	/* Synchronous mode, no callback - may be changed by the caller */
8128 	spkt->satapkt_comp = NULL;
8129 	spkt->satapkt_time = sata_default_pkt_time;
8130 
8131 	scmd = &spkt->satapkt_cmd;
8132 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8133 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8134 
8135 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8136 
8137 	/*
8138 	 * Set-up acdb. Request Sense CDB (packet command content) is
8139 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8140 	 * it is transfered into packet FIS).
8141 	 */
8142 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8143 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8144 	/* Following zeroing of pad bytes may not be necessary */
8145 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8146 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8147 
8148 	/*
8149 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8150 	 * before accessing it. Handle is in usual place in translate struct.
8151 	 */
8152 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8153 
8154 	/*
8155 	 * Preset request sense data to NO SENSE.
8156 	 * Here it is redundant, only for a symetry with scsi-originated
8157 	 * packets. It should not be used for anything but debugging.
8158 	 */
8159 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8160 	sata_fixed_sense_data_preset(
8161 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8162 
8163 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8164 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8165 
8166 	return (SATA_SUCCESS);
8167 }
8168 
8169 /*
8170  * Set-up ATAPI packet command.
8171  * Data transfer direction has to be set-up in sata_cmd structure prior to
8172  * calling this function.
8173  *
8174  * Returns void
8175  */
8176 
8177 static void
8178 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8179 {
8180 	scmd->satacmd_addr_type = 0;		/* N/A */
8181 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8182 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8183 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8184 	scmd->satacmd_lba_high_lsb =
8185 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8186 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8187 
8188 	/*
8189 	 * We want all data to be transfered via DMA.
8190 	 * But specify it only if drive supports DMA and DMA mode is
8191 	 * selected - some drives are sensitive about it.
8192 	 * Hopefully it wil work for all drives....
8193 	 */
8194 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8195 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8196 
8197 	/*
8198 	 * Features register requires special care for devices that use
8199 	 * Serial ATA bridge - they need an explicit specification of
8200 	 * the data transfer direction for Packet DMA commands.
8201 	 * Setting this bit is harmless if DMA is not used.
8202 	 *
8203 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8204 	 * spec they follow.
8205 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8206 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8207 	 * ATA/ATAPI-7 support is explicitly indicated.
8208 	 */
8209 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8210 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8211 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8212 		/*
8213 		 * Specification of major version is valid and version 7
8214 		 * is supported. It does automatically imply that all
8215 		 * spec features are supported. For now, we assume that
8216 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8217 		 */
8218 		if ((sdinfo->satadrv_id.ai_dirdma &
8219 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8220 			if (scmd->satacmd_flags.sata_data_direction ==
8221 			    SATA_DIR_READ)
8222 			scmd->satacmd_features_reg |=
8223 			    SATA_ATAPI_F_DATA_DIR_READ;
8224 		}
8225 	}
8226 }
8227 
8228 
8229 #ifdef SATA_DEBUG
8230 
8231 /* Display 18 bytes of Inquiry data */
8232 static void
8233 sata_show_inqry_data(uint8_t *buf)
8234 {
8235 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8236 	uint8_t *p;
8237 
8238 	cmn_err(CE_NOTE, "Inquiry data:");
8239 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8240 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8241 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8242 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8243 	    SATA_ATAPI_TRANS_VERSION(inq));
8244 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8245 	    inq->inq_rdf, inq->inq_aenc);
8246 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8247 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8248 	p = (uint8_t *)inq->inq_vid;
8249 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8250 	    "%02x %02x %02x %02x",
8251 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8252 	p = (uint8_t *)inq->inq_vid;
8253 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8254 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8255 
8256 	p = (uint8_t *)inq->inq_pid;
8257 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8258 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8259 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8260 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8261 	p = (uint8_t *)inq->inq_pid;
8262 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8263 	    "%c %c %c %c %c %c %c %c",
8264 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8265 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8266 
8267 	p = (uint8_t *)inq->inq_revision;
8268 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8269 	    p[0], p[1], p[2], p[3]);
8270 	p = (uint8_t *)inq->inq_revision;
8271 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8272 	    p[0], p[1], p[2], p[3]);
8273 
8274 }
8275 
8276 
8277 static void
8278 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8279 {
8280 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8281 
8282 	if (scsi_pkt == NULL)
8283 		return;
8284 	if (count != 0) {
8285 		/* saving cdb */
8286 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8287 		    SATA_ATAPI_MAX_CDB_LEN);
8288 		bcopy(scsi_pkt->pkt_cdbp,
8289 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8290 	} else {
8291 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8292 		    sts_sensedata,
8293 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8294 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8295 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8296 		    scsi_pkt->pkt_reason;
8297 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8298 		    spx->txlt_sata_pkt->satapkt_reason;
8299 
8300 		if (++sata_atapi_trace_index >= 64)
8301 			sata_atapi_trace_index = 0;
8302 	}
8303 }
8304 
8305 #endif
8306 
8307 /*
8308  * Fetch inquiry data from ATAPI device
8309  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8310  *
8311  * Note:
8312  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
8313  * where the caller expects to see the inquiry data.
8314  *
8315  */
8316 
8317 static int
8318 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8319     sata_address_t *saddr, struct scsi_inquiry *inq)
8320 {
8321 	sata_pkt_txlate_t *spx;
8322 	sata_pkt_t *spkt;
8323 	struct buf *bp;
8324 	sata_drive_info_t *sdinfo;
8325 	sata_cmd_t *scmd;
8326 	int rval;
8327 	uint8_t *rqsp;
8328 #ifdef SATA_DEBUG
8329 	char msg_buf[MAXPATHLEN];
8330 #endif
8331 
8332 	ASSERT(sata_hba != NULL);
8333 
8334 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8335 	spx->txlt_sata_hba_inst = sata_hba;
8336 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8337 	spkt = sata_pkt_alloc(spx, NULL);
8338 	if (spkt == NULL) {
8339 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8340 		return (SATA_FAILURE);
8341 	}
8342 	/* address is needed now */
8343 	spkt->satapkt_device.satadev_addr = *saddr;
8344 
8345 	/* scsi_inquiry size buffer */
8346 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8347 	if (bp == NULL) {
8348 		sata_pkt_free(spx);
8349 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8350 		SATA_LOG_D((sata_hba, CE_WARN,
8351 		    "sata_get_atapi_inquiry_data: "
8352 		    "cannot allocate data buffer"));
8353 		return (SATA_FAILURE);
8354 	}
8355 	bp_mapin(bp); /* make data buffer accessible */
8356 
8357 	scmd = &spkt->satapkt_cmd;
8358 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8359 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8360 
8361 	/* Use synchronous mode */
8362 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8363 	spkt->satapkt_comp = NULL;
8364 	spkt->satapkt_time = sata_default_pkt_time;
8365 
8366 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8367 
8368 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8369 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8370 
8371 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8372 	sdinfo = sata_get_device_info(sata_hba,
8373 	    &spx->txlt_sata_pkt->satapkt_device);
8374 	if (sdinfo == NULL) {
8375 		/* we have to be carefull about the disapearing device */
8376 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8377 		rval = SATA_FAILURE;
8378 		goto cleanup;
8379 	}
8380 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8381 
8382 	/*
8383 	 * Set-up acdb. This works for atapi transport version 2 and later.
8384 	 */
8385 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8386 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8387 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8388 	scmd->satacmd_acdb[1] = 0x00;
8389 	scmd->satacmd_acdb[2] = 0x00;
8390 	scmd->satacmd_acdb[3] = 0x00;
8391 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8392 	scmd->satacmd_acdb[5] = 0x00;
8393 
8394 	sata_fixed_sense_data_preset(
8395 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8396 
8397 	/* Transfer command to HBA */
8398 	if (sata_hba_start(spx, &rval) != 0) {
8399 		/* Pkt not accepted for execution */
8400 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8401 		    "sata_get_atapi_inquiry_data: "
8402 		    "Packet not accepted for execution - ret: %02x", rval);
8403 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8404 		rval = SATA_FAILURE;
8405 		goto cleanup;
8406 	}
8407 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8408 
8409 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8410 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8411 		    "sata_get_atapi_inquiry_data: "
8412 		    "Packet completed successfully - ret: %02x", rval);
8413 		if (spx->txlt_buf_dma_handle != NULL) {
8414 			/*
8415 			 * Sync buffer. Handle is in usual place in translate
8416 			 * struct.
8417 			 */
8418 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8419 			    DDI_DMA_SYNC_FORCPU);
8420 			ASSERT(rval == DDI_SUCCESS);
8421 		}
8422 		/*
8423 		 * Normal completion - copy data into caller's buffer
8424 		 */
8425 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8426 		    sizeof (struct scsi_inquiry));
8427 #ifdef SATA_DEBUG
8428 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8429 			sata_show_inqry_data((uint8_t *)inq);
8430 		}
8431 #endif
8432 		rval = SATA_SUCCESS;
8433 	} else {
8434 		/*
8435 		 * Something went wrong - analyze return - check rqsense data
8436 		 */
8437 		rval = SATA_FAILURE;
8438 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8439 			/*
8440 			 * ARQ data hopefull show something other than NO SENSE
8441 			 */
8442 			rqsp = scmd->satacmd_rqsense;
8443 #ifdef SATA_DEBUG
8444 			if (sata_debug_flags & SATA_DBG_ATAPI) {
8445 				msg_buf[0] = '\0';
8446 				(void) snprintf(msg_buf, MAXPATHLEN,
8447 				    "ATAPI packet completion reason: %02x\n"
8448 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
8449 				    "          %02x %02x %02x %02x %02x %02x\n"
8450 				    "          %02x %02x %02x %02x %02x %02x",
8451 				    spkt->satapkt_reason,
8452 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8453 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8454 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8455 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8456 				    rqsp[16], rqsp[17]);
8457 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8458 				    "%s", msg_buf);
8459 			}
8460 #endif
8461 		} else {
8462 			switch (spkt->satapkt_reason) {
8463 			case SATA_PKT_PORT_ERROR:
8464 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8465 				    "sata_get_atapi_inquiry_data: "
8466 				    "packet reason: port error", NULL);
8467 				break;
8468 
8469 			case SATA_PKT_TIMEOUT:
8470 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8471 				    "sata_get_atapi_inquiry_data: "
8472 				    "packet reason: timeout", NULL);
8473 				break;
8474 
8475 			case SATA_PKT_ABORTED:
8476 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8477 				    "sata_get_atapi_inquiry_data: "
8478 				    "packet reason: aborted", NULL);
8479 				break;
8480 
8481 			case SATA_PKT_RESET:
8482 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8483 				    "sata_get_atapi_inquiry_data: "
8484 				    "packet reason: reset\n", NULL);
8485 				break;
8486 			default:
8487 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8488 				    "sata_get_atapi_inquiry_data: "
8489 				    "invalid packet reason: %02x\n",
8490 				    spkt->satapkt_reason);
8491 				break;
8492 			}
8493 		}
8494 	}
8495 cleanup:
8496 	sata_free_local_buffer(spx);
8497 	sata_pkt_free(spx);
8498 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8499 	return (rval);
8500 }
8501 
8502 
8503 
8504 
8505 
8506 #if 0
8507 #ifdef SATA_DEBUG
8508 
8509 /*
8510  * Test ATAPI packet command.
8511  * Single threaded test: send packet command in synch mode, process completion
8512  *
8513  */
8514 static void
8515 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8516 {
8517 	sata_pkt_txlate_t *spx;
8518 	sata_pkt_t *spkt;
8519 	struct buf *bp;
8520 	sata_device_t sata_device;
8521 	sata_drive_info_t *sdinfo;
8522 	sata_cmd_t *scmd;
8523 	int rval;
8524 	uint8_t *rqsp;
8525 
8526 	ASSERT(sata_hba_inst != NULL);
8527 	sata_device.satadev_addr.cport = cport;
8528 	sata_device.satadev_addr.pmport = 0;
8529 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8530 	sata_device.satadev_rev = SATA_DEVICE_REV;
8531 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8532 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8533 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8534 	if (sdinfo == NULL) {
8535 		sata_log(sata_hba_inst, CE_WARN,
8536 		    "sata_test_atapi_packet_command: "
8537 		    "no device info for cport %d",
8538 		    sata_device.satadev_addr.cport);
8539 		return;
8540 	}
8541 
8542 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8543 	spx->txlt_sata_hba_inst = sata_hba_inst;
8544 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8545 	spkt = sata_pkt_alloc(spx, NULL);
8546 	if (spkt == NULL) {
8547 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8548 		return;
8549 	}
8550 	/* address is needed now */
8551 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8552 
8553 	/* 1024k buffer */
8554 	bp = sata_alloc_local_buffer(spx, 1024);
8555 	if (bp == NULL) {
8556 		sata_pkt_free(spx);
8557 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8558 		sata_log(sata_hba_inst, CE_WARN,
8559 		    "sata_test_atapi_packet_command: "
8560 		    "cannot allocate data buffer");
8561 		return;
8562 	}
8563 	bp_mapin(bp); /* make data buffer accessible */
8564 
8565 	scmd = &spkt->satapkt_cmd;
8566 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8567 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8568 
8569 	/* Use synchronous mode */
8570 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8571 
8572 	/* Synchronous mode, no callback - may be changed by the caller */
8573 	spkt->satapkt_comp = NULL;
8574 	spkt->satapkt_time = sata_default_pkt_time;
8575 
8576 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8577 
8578 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8579 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8580 
8581 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8582 
8583 	/* Set-up acdb. */
8584 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8585 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8586 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8587 	scmd->satacmd_acdb[1] = 0x00;
8588 	scmd->satacmd_acdb[2] = 0x00;
8589 	scmd->satacmd_acdb[3] = 0x00;
8590 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8591 	scmd->satacmd_acdb[5] = 0x00;
8592 
8593 	sata_fixed_sense_data_preset(
8594 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8595 
8596 	/* Transfer command to HBA */
8597 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8598 	if (sata_hba_start(spx, &rval) != 0) {
8599 		/* Pkt not accepted for execution */
8600 		sata_log(sata_hba_inst, CE_WARN,
8601 		    "sata_test_atapi_packet_command: "
8602 		    "Packet not accepted for execution - ret: %02x", rval);
8603 		mutex_exit(
8604 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8605 		goto cleanup;
8606 	}
8607 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8608 
8609 	if (spx->txlt_buf_dma_handle != NULL) {
8610 		/*
8611 		 * Sync buffer. Handle is in usual place in translate struct.
8612 		 */
8613 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8614 		    DDI_DMA_SYNC_FORCPU);
8615 		ASSERT(rval == DDI_SUCCESS);
8616 	}
8617 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8618 		sata_log(sata_hba_inst, CE_WARN,
8619 		    "sata_test_atapi_packet_command: "
8620 		    "Packet completed successfully");
8621 		/*
8622 		 * Normal completion - show inquiry data
8623 		 */
8624 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8625 	} else {
8626 		/*
8627 		 * Something went wrong - analyze return - check rqsense data
8628 		 */
8629 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8630 			/*
8631 			 * ARQ data hopefull show something other than NO SENSE
8632 			 */
8633 			rqsp = scmd->satacmd_rqsense;
8634 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8635 			    "ATAPI packet completion reason: %02x\n"
8636 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8637 			    "          %02x %02x %02x %02x %02x %02x "
8638 			    "          %02x %02x %02x %02x %02x %02x\n",
8639 			    spkt->satapkt_reason,
8640 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8641 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8642 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8643 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8644 			    rqsp[16], rqsp[17]);
8645 		} else {
8646 			switch (spkt->satapkt_reason) {
8647 			case SATA_PKT_PORT_ERROR:
8648 				sata_log(sata_hba_inst, CE_WARN,
8649 				    "sata_test_atapi_packet_command: "
8650 				    "packet reason: port error\n");
8651 				break;
8652 
8653 			case SATA_PKT_TIMEOUT:
8654 				sata_log(sata_hba_inst, CE_WARN,
8655 				    "sata_test_atapi_packet_command: "
8656 				    "packet reason: timeout\n");
8657 				break;
8658 
8659 			case SATA_PKT_ABORTED:
8660 				sata_log(sata_hba_inst, CE_WARN,
8661 				    "sata_test_atapi_packet_command: "
8662 				    "packet reason: aborted\n");
8663 				break;
8664 
8665 			case SATA_PKT_RESET:
8666 				sata_log(sata_hba_inst, CE_WARN,
8667 				    "sata_test_atapi_packet_command: "
8668 				    "packet reason: reset\n");
8669 				break;
8670 			default:
8671 				sata_log(sata_hba_inst, CE_WARN,
8672 				    "sata_test_atapi_packet_command: "
8673 				    "invalid packet reason: %02x\n",
8674 				    spkt->satapkt_reason);
8675 				break;
8676 			}
8677 		}
8678 	}
8679 cleanup:
8680 	sata_free_local_buffer(spx);
8681 	sata_pkt_free(spx);
8682 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8683 }
8684 
8685 #endif /* SATA_DEBUG */
8686 #endif /* 1 */
8687 
8688 
8689 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
8690 
8691 /*
8692  * Validate sata_tran info
8693  * SATA_FAILURE returns if structure is inconsistent or structure revision
8694  * does not match one used by the framework.
8695  *
8696  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
8697  * required function pointers.
8698  * Returns SATA_FAILURE otherwise.
8699  */
8700 static int
8701 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
8702 {
8703 	/*
8704 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
8705 	 * of the SATA interface.
8706 	 */
8707 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
8708 		sata_log(NULL, CE_WARN,
8709 		    "sata: invalid sata_hba_tran version %d for driver %s",
8710 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
8711 		return (SATA_FAILURE);
8712 	}
8713 
8714 	if (dip != sata_tran->sata_tran_hba_dip) {
8715 		SATA_LOG_D((NULL, CE_WARN,
8716 		    "sata: inconsistent sata_tran_hba_dip "
8717 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
8718 		return (SATA_FAILURE);
8719 	}
8720 
8721 	if (sata_tran->sata_tran_probe_port == NULL ||
8722 	    sata_tran->sata_tran_start == NULL ||
8723 	    sata_tran->sata_tran_abort == NULL ||
8724 	    sata_tran->sata_tran_reset_dport == NULL ||
8725 	    sata_tran->sata_tran_hotplug_ops == NULL ||
8726 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
8727 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
8728 	    NULL) {
8729 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
8730 		    "required functions"));
8731 	}
8732 	return (SATA_SUCCESS);
8733 }
8734 
8735 /*
8736  * Remove HBA instance from sata_hba_list.
8737  */
8738 static void
8739 sata_remove_hba_instance(dev_info_t *dip)
8740 {
8741 	sata_hba_inst_t	*sata_hba_inst;
8742 
8743 	mutex_enter(&sata_mutex);
8744 	for (sata_hba_inst = sata_hba_list;
8745 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
8746 	    sata_hba_inst = sata_hba_inst->satahba_next) {
8747 		if (sata_hba_inst->satahba_dip == dip)
8748 			break;
8749 	}
8750 
8751 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
8752 #ifdef SATA_DEBUG
8753 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
8754 		    "unknown HBA instance\n");
8755 #endif
8756 		ASSERT(FALSE);
8757 	}
8758 	if (sata_hba_inst == sata_hba_list) {
8759 		sata_hba_list = sata_hba_inst->satahba_next;
8760 		if (sata_hba_list) {
8761 			sata_hba_list->satahba_prev =
8762 			    (struct sata_hba_inst *)NULL;
8763 		}
8764 		if (sata_hba_inst == sata_hba_list_tail) {
8765 			sata_hba_list_tail = NULL;
8766 		}
8767 	} else if (sata_hba_inst == sata_hba_list_tail) {
8768 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
8769 		if (sata_hba_list_tail) {
8770 			sata_hba_list_tail->satahba_next =
8771 			    (struct sata_hba_inst *)NULL;
8772 		}
8773 	} else {
8774 		sata_hba_inst->satahba_prev->satahba_next =
8775 		    sata_hba_inst->satahba_next;
8776 		sata_hba_inst->satahba_next->satahba_prev =
8777 		    sata_hba_inst->satahba_prev;
8778 	}
8779 	mutex_exit(&sata_mutex);
8780 }
8781 
8782 
8783 
8784 
8785 
8786 /*
8787  * Probe all SATA ports of the specified HBA instance.
8788  * The assumption is that there are no target and attachment point minor nodes
8789  * created by the boot subsystems, so we do not need to prune device tree.
8790  *
8791  * This function is called only from sata_hba_attach(). It does not have to
8792  * be protected by controller mutex, because the hba_attached flag is not set
8793  * yet and no one would be touching this HBA instance other than this thread.
8794  * Determines if port is active and what type of the device is attached
8795  * (if any). Allocates necessary structures for each port.
8796  *
8797  * An AP (Attachement Point) node is created for each SATA device port even
8798  * when there is no device attached.
8799  */
8800 
8801 static 	void
8802 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8803 {
8804 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8805 	int			ncport, npmport;
8806 	sata_cport_info_t 	*cportinfo;
8807 	sata_drive_info_t	*drive;
8808 	sata_pmult_info_t	*pminfo;
8809 	sata_pmport_info_t 	*pmportinfo;
8810 	sata_device_t		sata_device;
8811 	int			rval;
8812 	dev_t			minor_number;
8813 	char			name[16];
8814 	clock_t			start_time, cur_time;
8815 
8816 	/*
8817 	 * Probe controller ports first, to find port status and
8818 	 * any port multiplier attached.
8819 	 */
8820 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8821 		/* allocate cport structure */
8822 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8823 		ASSERT(cportinfo != NULL);
8824 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8825 
8826 		mutex_enter(&cportinfo->cport_mutex);
8827 
8828 		cportinfo->cport_addr.cport = ncport;
8829 		cportinfo->cport_addr.pmport = 0;
8830 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8831 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8832 		cportinfo->cport_state |= SATA_STATE_PROBING;
8833 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8834 
8835 		/*
8836 		 * Regardless if a port is usable or not, create
8837 		 * an attachment point
8838 		 */
8839 		mutex_exit(&cportinfo->cport_mutex);
8840 		minor_number =
8841 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8842 		(void) sprintf(name, "%d", ncport);
8843 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8844 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8845 		    DDI_SUCCESS) {
8846 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8847 			    "cannot create SATA attachment point for port %d",
8848 			    ncport);
8849 		}
8850 
8851 		/* Probe port */
8852 		start_time = ddi_get_lbolt();
8853 	reprobe_cport:
8854 		sata_device.satadev_addr.cport = ncport;
8855 		sata_device.satadev_addr.pmport = 0;
8856 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8857 		sata_device.satadev_rev = SATA_DEVICE_REV;
8858 
8859 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8860 		    (dip, &sata_device);
8861 
8862 		mutex_enter(&cportinfo->cport_mutex);
8863 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8864 		if (rval != SATA_SUCCESS) {
8865 			/* Something went wrong? Fail the port */
8866 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8867 			mutex_exit(&cportinfo->cport_mutex);
8868 			continue;
8869 		}
8870 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8871 		cportinfo->cport_state |= SATA_STATE_PROBED;
8872 		cportinfo->cport_dev_type = sata_device.satadev_type;
8873 
8874 		cportinfo->cport_state |= SATA_STATE_READY;
8875 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8876 			mutex_exit(&cportinfo->cport_mutex);
8877 			continue;
8878 		}
8879 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8880 			/*
8881 			 * There is some device attached.
8882 			 * Allocate device info structure
8883 			 */
8884 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8885 				mutex_exit(&cportinfo->cport_mutex);
8886 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8887 				    kmem_zalloc(sizeof (sata_drive_info_t),
8888 				    KM_SLEEP);
8889 				mutex_enter(&cportinfo->cport_mutex);
8890 			}
8891 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8892 			drive->satadrv_addr = cportinfo->cport_addr;
8893 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8894 			drive->satadrv_type = cportinfo->cport_dev_type;
8895 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8896 
8897 			mutex_exit(&cportinfo->cport_mutex);
8898 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8899 			    SATA_SUCCESS) {
8900 				/*
8901 				 * Plugged device was not correctly identified.
8902 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8903 				 */
8904 				cur_time = ddi_get_lbolt();
8905 				if ((cur_time - start_time) <
8906 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8907 					/* sleep for a while */
8908 					delay(drv_usectohz(
8909 					    SATA_DEV_RETRY_DLY));
8910 					goto reprobe_cport;
8911 				}
8912 			}
8913 		} else {
8914 			mutex_exit(&cportinfo->cport_mutex);
8915 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8916 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8917 			    KM_SLEEP);
8918 			mutex_enter(&cportinfo->cport_mutex);
8919 			ASSERT(pminfo != NULL);
8920 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8921 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8922 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8923 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8924 			pminfo->pmult_num_dev_ports =
8925 			    sata_device.satadev_add_info;
8926 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8927 			    NULL);
8928 			pminfo->pmult_state = SATA_STATE_PROBING;
8929 			mutex_exit(&cportinfo->cport_mutex);
8930 
8931 			/* Probe Port Multiplier ports */
8932 			for (npmport = 0;
8933 			    npmport < pminfo->pmult_num_dev_ports;
8934 			    npmport++) {
8935 				pmportinfo = kmem_zalloc(
8936 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8937 				mutex_enter(&cportinfo->cport_mutex);
8938 				ASSERT(pmportinfo != NULL);
8939 				pmportinfo->pmport_addr.cport = ncport;
8940 				pmportinfo->pmport_addr.pmport = npmport;
8941 				pmportinfo->pmport_addr.qual =
8942 				    SATA_ADDR_PMPORT;
8943 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8944 
8945 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8946 				    MUTEX_DRIVER, NULL);
8947 
8948 				mutex_exit(&cportinfo->cport_mutex);
8949 
8950 				/* Create an attachment point */
8951 				minor_number = SATA_MAKE_AP_MINOR(
8952 				    ddi_get_instance(dip), ncport, npmport, 1);
8953 				(void) sprintf(name, "%d.%d", ncport, npmport);
8954 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8955 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8956 				    0) != DDI_SUCCESS) {
8957 					sata_log(sata_hba_inst, CE_WARN,
8958 					    "sata_hba_attach: "
8959 					    "cannot create SATA attachment "
8960 					    "point for port %d pmult port %d",
8961 					    ncport, npmport);
8962 				}
8963 
8964 				start_time = ddi_get_lbolt();
8965 			reprobe_pmport:
8966 				sata_device.satadev_addr.pmport = npmport;
8967 				sata_device.satadev_addr.qual =
8968 				    SATA_ADDR_PMPORT;
8969 
8970 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8971 				    (dip, &sata_device);
8972 				mutex_enter(&cportinfo->cport_mutex);
8973 
8974 				/* sata_update_port_info() */
8975 				sata_update_port_scr(&pmportinfo->pmport_scr,
8976 				    &sata_device);
8977 
8978 				if (rval != SATA_SUCCESS) {
8979 					pmportinfo->pmport_state =
8980 					    SATA_PSTATE_FAILED;
8981 					mutex_exit(&cportinfo->cport_mutex);
8982 					continue;
8983 				}
8984 				pmportinfo->pmport_state &=
8985 				    ~SATA_STATE_PROBING;
8986 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8987 				pmportinfo->pmport_dev_type =
8988 				    sata_device.satadev_type;
8989 
8990 				pmportinfo->pmport_state |= SATA_STATE_READY;
8991 				if (pmportinfo->pmport_dev_type ==
8992 				    SATA_DTYPE_NONE) {
8993 					mutex_exit(&cportinfo->cport_mutex);
8994 					continue;
8995 				}
8996 				/* Port multipliers cannot be chained */
8997 				ASSERT(pmportinfo->pmport_dev_type !=
8998 				    SATA_DTYPE_PMULT);
8999 				/*
9000 				 * There is something attached to Port
9001 				 * Multiplier device port
9002 				 * Allocate device info structure
9003 				 */
9004 				if (pmportinfo->pmport_sata_drive == NULL) {
9005 					mutex_exit(&cportinfo->cport_mutex);
9006 					pmportinfo->pmport_sata_drive =
9007 					    kmem_zalloc(
9008 					    sizeof (sata_drive_info_t),
9009 					    KM_SLEEP);
9010 					mutex_enter(&cportinfo->cport_mutex);
9011 				}
9012 				drive = pmportinfo->pmport_sata_drive;
9013 				drive->satadrv_addr.cport =
9014 				    pmportinfo->pmport_addr.cport;
9015 				drive->satadrv_addr.pmport = npmport;
9016 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9017 				drive->satadrv_type = pmportinfo->
9018 				    pmport_dev_type;
9019 				drive->satadrv_state = SATA_STATE_UNKNOWN;
9020 
9021 				mutex_exit(&cportinfo->cport_mutex);
9022 				if (sata_add_device(dip, sata_hba_inst, ncport,
9023 				    npmport) != SATA_SUCCESS) {
9024 					/*
9025 					 * Plugged device was not correctly
9026 					 * identified. Retry, within the
9027 					 * SATA_DEV_IDENTIFY_TIMEOUT
9028 					 */
9029 					cur_time = ddi_get_lbolt();
9030 					if ((cur_time - start_time) <
9031 					    drv_usectohz(
9032 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
9033 						/* sleep for a while */
9034 						delay(drv_usectohz(
9035 						    SATA_DEV_RETRY_DLY));
9036 						goto reprobe_pmport;
9037 					}
9038 				}
9039 			}
9040 			pmportinfo->pmport_state =
9041 			    SATA_STATE_PROBED | SATA_STATE_READY;
9042 		}
9043 	}
9044 }
9045 
9046 /*
9047  * Add SATA device for specified HBA instance & port (SCSI target
9048  * device nodes).
9049  * This function is called (indirectly) only from sata_hba_attach().
9050  * A target node is created when there is a supported type device attached,
9051  * but may be removed if it cannot be put online.
9052  *
9053  * This function cannot be called from an interrupt context.
9054  *
9055  * ONLY DISK TARGET NODES ARE CREATED NOW
9056  *
9057  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9058  * device identification failed - adding a device could be retried.
9059  *
9060  */
9061 static 	int
9062 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
9063     int pmport)
9064 {
9065 	sata_cport_info_t 	*cportinfo;
9066 	sata_pmult_info_t	*pminfo;
9067 	sata_pmport_info_t	*pmportinfo;
9068 	dev_info_t		*cdip;		/* child dip */
9069 	sata_device_t		sata_device;
9070 	int			rval;
9071 
9072 
9073 
9074 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9075 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9076 	mutex_enter(&cportinfo->cport_mutex);
9077 	/*
9078 	 * Some device is attached to a controller port.
9079 	 * We rely on controllers distinquishing between no-device,
9080 	 * attached port multiplier and other kind of attached device.
9081 	 * We need to get Identify Device data and determine
9082 	 * positively the dev type before trying to attach
9083 	 * the target driver.
9084 	 */
9085 	sata_device.satadev_rev = SATA_DEVICE_REV;
9086 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9087 		/*
9088 		 * Not port multiplier.
9089 		 */
9090 		sata_device.satadev_addr = cportinfo->cport_addr;
9091 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
9092 		mutex_exit(&cportinfo->cport_mutex);
9093 
9094 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9095 		if (rval != SATA_SUCCESS ||
9096 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
9097 			return (SATA_FAILURE);
9098 
9099 		mutex_enter(&cportinfo->cport_mutex);
9100 		sata_show_drive_info(sata_hba_inst,
9101 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9102 
9103 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9104 			/*
9105 			 * Could not determine device type or
9106 			 * a device is not supported.
9107 			 * Degrade this device to unknown.
9108 			 */
9109 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9110 			mutex_exit(&cportinfo->cport_mutex);
9111 			return (SATA_SUCCESS);
9112 		}
9113 		cportinfo->cport_dev_type = sata_device.satadev_type;
9114 		cportinfo->cport_tgtnode_clean = B_TRUE;
9115 		mutex_exit(&cportinfo->cport_mutex);
9116 
9117 		/*
9118 		 * Initialize device to the desired state. Even if it
9119 		 * fails, the device will still attach but syslog
9120 		 * will show the warning.
9121 		 */
9122 		if (sata_initialize_device(sata_hba_inst,
9123 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
9124 			/* Retry */
9125 			rval = sata_initialize_device(sata_hba_inst,
9126 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9127 
9128 			if (rval == SATA_RETRY)
9129 				sata_log(sata_hba_inst, CE_WARN,
9130 				    "SATA device at port %d - "
9131 				    "default device features could not be set."
9132 				    " Device may not operate as expected.",
9133 				    cportinfo->cport_addr.cport);
9134 		}
9135 
9136 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9137 		    &sata_device.satadev_addr);
9138 		mutex_enter(&cportinfo->cport_mutex);
9139 		if (cdip == NULL) {
9140 			/*
9141 			 * Attaching target node failed.
9142 			 * We retain sata_drive_info structure...
9143 			 */
9144 			mutex_exit(&cportinfo->cport_mutex);
9145 			return (SATA_SUCCESS);
9146 		}
9147 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9148 		    satadrv_state = SATA_STATE_READY;
9149 	} else {
9150 		/* This must be Port Multiplier type */
9151 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9152 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9153 			    "sata_add_device: "
9154 			    "unrecognized dev type %x",
9155 			    cportinfo->cport_dev_type));
9156 			mutex_exit(&cportinfo->cport_mutex);
9157 			return (SATA_SUCCESS);
9158 		}
9159 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9160 		pmportinfo = pminfo->pmult_dev_port[pmport];
9161 		sata_device.satadev_addr = pmportinfo->pmport_addr;
9162 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
9163 		mutex_exit(&cportinfo->cport_mutex);
9164 
9165 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9166 		if (rval != SATA_SUCCESS ||
9167 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
9168 			return (SATA_FAILURE);
9169 		}
9170 		mutex_enter(&cportinfo->cport_mutex);
9171 		sata_show_drive_info(sata_hba_inst,
9172 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9173 
9174 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9175 			/*
9176 			 * Could not determine device type.
9177 			 * Degrade this device to unknown.
9178 			 */
9179 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9180 			mutex_exit(&cportinfo->cport_mutex);
9181 			return (SATA_SUCCESS);
9182 		}
9183 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9184 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9185 		mutex_exit(&cportinfo->cport_mutex);
9186 
9187 		/*
9188 		 * Initialize device to the desired state.
9189 		 * Even if it fails, the device will still
9190 		 * attach but syslog will show the warning.
9191 		 */
9192 		if (sata_initialize_device(sata_hba_inst,
9193 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
9194 			/* Retry */
9195 			rval = sata_initialize_device(sata_hba_inst,
9196 			    pmportinfo->pmport_sata_drive);
9197 
9198 			if (rval == SATA_RETRY)
9199 				sata_log(sata_hba_inst, CE_WARN,
9200 				    "SATA device at port %d pmport %d - "
9201 				    "default device features could not be set."
9202 				    " Device may not operate as expected.",
9203 				    pmportinfo->pmport_addr.cport,
9204 				    pmportinfo->pmport_addr.pmport);
9205 		}
9206 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9207 		    &sata_device.satadev_addr);
9208 		mutex_enter(&cportinfo->cport_mutex);
9209 		if (cdip == NULL) {
9210 			/*
9211 			 * Attaching target node failed.
9212 			 * We retain sata_drive_info structure...
9213 			 */
9214 			mutex_exit(&cportinfo->cport_mutex);
9215 			return (SATA_SUCCESS);
9216 		}
9217 		pmportinfo->pmport_sata_drive->satadrv_state |=
9218 		    SATA_STATE_READY;
9219 	}
9220 	mutex_exit(&cportinfo->cport_mutex);
9221 	return (SATA_SUCCESS);
9222 }
9223 
9224 
9225 
9226 /*
9227  * Create scsi target node for attached device, create node properties and
9228  * attach the node.
9229  * The node could be removed if the device onlining fails.
9230  *
9231  * A dev_info_t pointer is returned if operation is successful, NULL is
9232  * returned otherwise.
9233  *
9234  * No port multiplier support.
9235  */
9236 
9237 static dev_info_t *
9238 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9239 			sata_address_t *sata_addr)
9240 {
9241 	dev_info_t *cdip = NULL;
9242 	int rval;
9243 	char *nname = NULL;
9244 	char **compatible = NULL;
9245 	int ncompatible;
9246 	struct scsi_inquiry inq;
9247 	sata_device_t sata_device;
9248 	sata_drive_info_t *sdinfo;
9249 	int target;
9250 	int i;
9251 
9252 	sata_device.satadev_rev = SATA_DEVICE_REV;
9253 	sata_device.satadev_addr = *sata_addr;
9254 
9255 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9256 
9257 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9258 
9259 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9260 	    sata_addr->pmport, sata_addr->qual);
9261 
9262 	if (sdinfo == NULL) {
9263 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9264 		    sata_addr->cport)));
9265 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9266 		    "sata_create_target_node: no sdinfo for target %x",
9267 		    target));
9268 		return (NULL);
9269 	}
9270 
9271 	/*
9272 	 * create or get scsi inquiry data, expected by
9273 	 * scsi_hba_nodename_compatible_get()
9274 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9275 	 * ATAPI devices respond directly to Inquiry request.
9276 	 */
9277 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9278 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9279 		    (uint8_t *)&inq);
9280 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9281 		    sata_addr->cport)));
9282 	} else { /* Assume supported ATAPI device */
9283 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9284 		    sata_addr->cport)));
9285 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9286 		    &inq) == SATA_FAILURE)
9287 			return (NULL);
9288 		/*
9289 		 * Save supported ATAPI transport version
9290 		 */
9291 		sdinfo->satadrv_atapi_trans_ver =
9292 		    SATA_ATAPI_TRANS_VERSION(&inq);
9293 	}
9294 
9295 	/* determine the node name and compatible */
9296 	scsi_hba_nodename_compatible_get(&inq, NULL,
9297 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9298 
9299 #ifdef SATA_DEBUG
9300 	if (sata_debug_flags & SATA_DBG_NODES) {
9301 		if (nname == NULL) {
9302 			cmn_err(CE_NOTE, "sata_create_target_node: "
9303 			    "cannot determine nodename for target %d\n",
9304 			    target);
9305 		} else {
9306 			cmn_err(CE_WARN, "sata_create_target_node: "
9307 			    "target %d nodename: %s\n", target, nname);
9308 		}
9309 		if (compatible == NULL) {
9310 			cmn_err(CE_WARN,
9311 			    "sata_create_target_node: no compatible name\n");
9312 		} else {
9313 			for (i = 0; i < ncompatible; i++) {
9314 				cmn_err(CE_WARN, "sata_create_target_node: "
9315 				    "compatible name: %s\n", compatible[i]);
9316 			}
9317 		}
9318 	}
9319 #endif
9320 
9321 	/* if nodename can't be determined, log error and exit */
9322 	if (nname == NULL) {
9323 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9324 		    "sata_create_target_node: cannot determine nodename "
9325 		    "for target %d\n", target));
9326 		scsi_hba_nodename_compatible_free(nname, compatible);
9327 		return (NULL);
9328 	}
9329 	/*
9330 	 * Create scsi target node
9331 	 */
9332 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9333 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9334 	    "device-type", "scsi");
9335 
9336 	if (rval != DDI_PROP_SUCCESS) {
9337 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9338 		    "updating device_type prop failed %d", rval));
9339 		goto fail;
9340 	}
9341 
9342 	/*
9343 	 * Create target node properties: target & lun
9344 	 */
9345 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9346 	if (rval != DDI_PROP_SUCCESS) {
9347 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9348 		    "updating target prop failed %d", rval));
9349 		goto fail;
9350 	}
9351 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9352 	if (rval != DDI_PROP_SUCCESS) {
9353 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9354 		    "updating target prop failed %d", rval));
9355 		goto fail;
9356 	}
9357 
9358 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
9359 		/*
9360 		 * Add "variant" property
9361 		 */
9362 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9363 		    "variant", "atapi");
9364 		if (rval != DDI_PROP_SUCCESS) {
9365 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9366 			    "sata_create_target_node: variant atapi "
9367 			    "property could not be created: %d", rval));
9368 			goto fail;
9369 		}
9370 	}
9371 	/* decorate the node with compatible */
9372 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9373 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9374 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9375 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9376 		    (void *)cdip));
9377 		goto fail;
9378 	}
9379 
9380 
9381 	/*
9382 	 * Now, try to attach the driver. If probing of the device fails,
9383 	 * the target node may be removed
9384 	 */
9385 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9386 
9387 	scsi_hba_nodename_compatible_free(nname, compatible);
9388 
9389 	if (rval == NDI_SUCCESS)
9390 		return (cdip);
9391 
9392 	/* target node was removed - are we sure? */
9393 	return (NULL);
9394 
9395 fail:
9396 	scsi_hba_nodename_compatible_free(nname, compatible);
9397 	ddi_prop_remove_all(cdip);
9398 	rval = ndi_devi_free(cdip);
9399 	if (rval != NDI_SUCCESS) {
9400 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9401 		    "node removal failed %d", rval));
9402 	}
9403 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9404 	    "cannot create target node for SATA device at port %d",
9405 	    sata_addr->cport);
9406 	return (NULL);
9407 }
9408 
9409 
9410 
9411 /*
9412  * Re-probe sata port, check for a device and attach info
9413  * structures when necessary. Identify Device data is fetched, if possible.
9414  * Assumption: sata address is already validated.
9415  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9416  * the presence of a device and its type.
9417  *
9418  * flag arg specifies that the function should try multiple times to identify
9419  * device type and to initialize it, or it should return immediately on failure.
9420  * SATA_DEV_IDENTIFY_RETRY - retry
9421  * SATA_DEV_IDENTIFY_NORETRY - no retry
9422  *
9423  * SATA_FAILURE is returned if one of the operations failed.
9424  *
9425  * This function cannot be called in interrupt context - it may sleep.
9426  *
9427  * NOte: Port multiplier is not supported yet, although there may be some
9428  * pieces of code referencing to it.
9429  */
9430 static int
9431 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9432     int flag)
9433 {
9434 	sata_cport_info_t *cportinfo;
9435 	sata_drive_info_t *sdinfo, *osdinfo;
9436 	boolean_t init_device = B_FALSE;
9437 	int prev_device_type = SATA_DTYPE_NONE;
9438 	int prev_device_settings = 0;
9439 	int prev_device_state = 0;
9440 	clock_t start_time;
9441 	int retry = B_FALSE;
9442 	int rval_probe, rval_init;
9443 
9444 	/* We only care about host sata cport for now */
9445 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9446 	    sata_device->satadev_addr.cport);
9447 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9448 	if (osdinfo != NULL) {
9449 		/*
9450 		 * We are re-probing port with a previously attached device.
9451 		 * Save previous device type and settings.
9452 		 */
9453 		prev_device_type = cportinfo->cport_dev_type;
9454 		prev_device_settings = osdinfo->satadrv_settings;
9455 		prev_device_state = osdinfo->satadrv_state;
9456 	}
9457 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9458 		start_time = ddi_get_lbolt();
9459 		retry = B_TRUE;
9460 	}
9461 retry_probe:
9462 
9463 	/* probe port */
9464 	mutex_enter(&cportinfo->cport_mutex);
9465 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9466 	cportinfo->cport_state |= SATA_STATE_PROBING;
9467 	mutex_exit(&cportinfo->cport_mutex);
9468 
9469 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9470 	    (SATA_DIP(sata_hba_inst), sata_device);
9471 
9472 	mutex_enter(&cportinfo->cport_mutex);
9473 	if (rval_probe != SATA_SUCCESS) {
9474 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9475 		mutex_exit(&cportinfo->cport_mutex);
9476 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9477 		    "SATA port %d probing failed",
9478 		    cportinfo->cport_addr.cport));
9479 		return (SATA_FAILURE);
9480 	}
9481 
9482 	/*
9483 	 * update sata port state and set device type
9484 	 */
9485 	sata_update_port_info(sata_hba_inst, sata_device);
9486 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9487 
9488 	/*
9489 	 * Sanity check - Port is active? Is the link active?
9490 	 * Is there any device attached?
9491 	 */
9492 	if ((cportinfo->cport_state &
9493 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9494 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9495 	    SATA_PORT_DEVLINK_UP) {
9496 		/*
9497 		 * Port in non-usable state or no link active/no device.
9498 		 * Free info structure if necessary (direct attached drive
9499 		 * only, for now!
9500 		 */
9501 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9502 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9503 		/* Add here differentiation for device attached or not */
9504 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9505 		mutex_exit(&cportinfo->cport_mutex);
9506 		if (sdinfo != NULL)
9507 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9508 		return (SATA_SUCCESS);
9509 	}
9510 
9511 	cportinfo->cport_state |= SATA_STATE_READY;
9512 	cportinfo->cport_dev_type = sata_device->satadev_type;
9513 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9514 
9515 	/*
9516 	 * If we are re-probing the port, there may be
9517 	 * sata_drive_info structure attached
9518 	 * (or sata_pm_info, if PMult is supported).
9519 	 */
9520 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
9521 		/*
9522 		 * There is no device, so remove device info structure,
9523 		 * if necessary.
9524 		 * Only direct attached drive is considered now, until
9525 		 * port multiplier is supported. If the previously
9526 		 * attached device was a port multiplier, we would need
9527 		 * to take care of devices attached beyond the port
9528 		 * multiplier.
9529 		 */
9530 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9531 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9532 		if (sdinfo != NULL) {
9533 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9534 			sata_log(sata_hba_inst, CE_WARN,
9535 			    "SATA device detached "
9536 			    "from port %d", cportinfo->cport_addr.cport);
9537 		}
9538 		mutex_exit(&cportinfo->cport_mutex);
9539 		return (SATA_SUCCESS);
9540 	}
9541 
9542 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
9543 		if (sdinfo == NULL) {
9544 			/*
9545 			 * There is some device attached, but there is
9546 			 * no sata_drive_info structure - allocate one
9547 			 */
9548 			mutex_exit(&cportinfo->cport_mutex);
9549 			sdinfo = kmem_zalloc(
9550 			    sizeof (sata_drive_info_t), KM_SLEEP);
9551 			mutex_enter(&cportinfo->cport_mutex);
9552 			/*
9553 			 * Recheck, that the port state did not change when we
9554 			 * released mutex.
9555 			 */
9556 			if (cportinfo->cport_state & SATA_STATE_READY) {
9557 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
9558 				sdinfo->satadrv_addr = cportinfo->cport_addr;
9559 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
9560 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9561 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9562 			} else {
9563 				/*
9564 				 * Port is not in ready state, we
9565 				 * cannot attach a device.
9566 				 */
9567 				mutex_exit(&cportinfo->cport_mutex);
9568 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
9569 				return (SATA_SUCCESS);
9570 			}
9571 			/*
9572 			 * Since we are adding device, presumably new one,
9573 			 * indicate that it  should be initalized,
9574 			 * as well as some internal framework states).
9575 			 */
9576 			init_device = B_TRUE;
9577 		}
9578 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9579 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
9580 	} else {
9581 		/*
9582 		 * The device is a port multiplier - not handled now.
9583 		 */
9584 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9585 		mutex_exit(&cportinfo->cport_mutex);
9586 		return (SATA_SUCCESS);
9587 	}
9588 	mutex_exit(&cportinfo->cport_mutex);
9589 	/*
9590 	 * Figure out what kind of device we are really
9591 	 * dealing with. Failure of identifying device does not fail this
9592 	 * function.
9593 	 */
9594 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
9595 	rval_init = SATA_FAILURE;
9596 	mutex_enter(&cportinfo->cport_mutex);
9597 	if (rval_probe == SATA_SUCCESS) {
9598 		/*
9599 		 * If we are dealing with the same type of a device as before,
9600 		 * restore its settings flags.
9601 		 */
9602 		if (osdinfo != NULL &&
9603 		    sata_device->satadev_type == prev_device_type)
9604 			sdinfo->satadrv_settings = prev_device_settings;
9605 
9606 		mutex_exit(&cportinfo->cport_mutex);
9607 		rval_init = SATA_SUCCESS;
9608 		/* Set initial device features, if necessary */
9609 		if (init_device == B_TRUE) {
9610 			rval_init = sata_initialize_device(sata_hba_inst,
9611 			    sdinfo);
9612 		}
9613 		if (rval_init == SATA_SUCCESS)
9614 			return (rval_init);
9615 		/* else we will retry if retry was asked for */
9616 
9617 	} else {
9618 		/*
9619 		 * If there was some device info before we probe the device,
9620 		 * restore previous device setting, so we can retry from scratch
9621 		 * later. Providing, of course, that device has not disapear
9622 		 * during probing process.
9623 		 */
9624 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
9625 			if (osdinfo != NULL) {
9626 				cportinfo->cport_dev_type = prev_device_type;
9627 				sdinfo->satadrv_type = prev_device_type;
9628 				sdinfo->satadrv_state = prev_device_state;
9629 			}
9630 		} else {
9631 			/* device is gone */
9632 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9633 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9634 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9635 			mutex_exit(&cportinfo->cport_mutex);
9636 			return (SATA_SUCCESS);
9637 		}
9638 		mutex_exit(&cportinfo->cport_mutex);
9639 	}
9640 
9641 	if (retry) {
9642 		clock_t cur_time = ddi_get_lbolt();
9643 		/*
9644 		 * A device was not successfully identified or initialized.
9645 		 * Track retry time for device identification.
9646 		 */
9647 		if ((cur_time - start_time) <
9648 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
9649 			/* sleep for a while */
9650 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
9651 			goto retry_probe;
9652 		}
9653 		/* else no more retries */
9654 		mutex_enter(&cportinfo->cport_mutex);
9655 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
9656 			if (rval_init == SATA_RETRY) {
9657 				/*
9658 				 * Setting drive features have failed, but
9659 				 * because the drive is still accessible,
9660 				 * keep it and emit a warning message.
9661 				 */
9662 				sata_log(sata_hba_inst, CE_WARN,
9663 				    "SATA device at port %d - desired "
9664 				    "drive features could not be set. "
9665 				    "Device may not operate as expected.",
9666 				    cportinfo->cport_addr.cport);
9667 			} else {
9668 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
9669 				    satadrv_state = SATA_DSTATE_FAILED;
9670 			}
9671 		}
9672 		mutex_exit(&cportinfo->cport_mutex);
9673 	}
9674 	return (SATA_SUCCESS);
9675 }
9676 
9677 /*
9678  * Initialize device
9679  * Specified device is initialized to a default state.
9680  *
9681  * Returns SATA_SUCCESS if all device features are set successfully,
9682  * SATA_RETRY if device is accessible but device features were not set
9683  * successfully, and SATA_FAILURE otherwise.
9684  */
9685 static int
9686 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
9687     sata_drive_info_t *sdinfo)
9688 {
9689 	int rval;
9690 
9691 	sata_save_drive_settings(sdinfo);
9692 
9693 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9694 
9695 	sata_init_write_cache_mode(sdinfo);
9696 
9697 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
9698 
9699 	/* Determine current data transfer mode */
9700 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
9701 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9702 	} else if ((sdinfo->satadrv_id.ai_validinfo &
9703 	    SATA_VALIDINFO_88) != 0 &&
9704 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
9705 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9706 	} else if ((sdinfo->satadrv_id.ai_dworddma &
9707 	    SATA_MDMA_SEL_MASK) != 0) {
9708 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9709 	} else
9710 		/* DMA supported, not no DMA transfer mode is selected !? */
9711 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9712 
9713 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
9714 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
9715 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
9716 
9717 	return (rval);
9718 }
9719 
9720 
9721 /*
9722  * Initialize write cache mode.
9723  *
9724  * The default write cache setting for SATA HDD is provided by sata_write_cache
9725  * static variable. ATAPI CD/DVDs devices have write cache default is
9726  * determined by sata_atapicdvd_write_cache static variable.
9727  * ATAPI tape devices have write cache default is determined by
9728  * sata_atapitape_write_cache static variable.
9729  * ATAPI disk devices have write cache default is determined by
9730  * sata_atapidisk_write_cache static variable.
9731  * 1 - enable
9732  * 0 - disable
9733  * any other value - current drive setting
9734  *
9735  * Although there is not reason to disable write cache on CD/DVD devices,
9736  * tape devices and ATAPI disk devices, the default setting control is provided
9737  * for the maximun flexibility.
9738  *
9739  * In the future, it may be overridden by the
9740  * disk-write-cache-enable property setting, if it is defined.
9741  * Returns SATA_SUCCESS if all device features are set successfully,
9742  * SATA_FAILURE otherwise.
9743  */
9744 static void
9745 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
9746 {
9747 	switch (sdinfo->satadrv_type) {
9748 	case SATA_DTYPE_ATADISK:
9749 		if (sata_write_cache == 1)
9750 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9751 		else if (sata_write_cache == 0)
9752 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9753 		/*
9754 		 * When sata_write_cache value is not 0 or 1,
9755 		 * a current setting of the drive's write cache is used.
9756 		 */
9757 		break;
9758 	case SATA_DTYPE_ATAPICD:
9759 		if (sata_atapicdvd_write_cache == 1)
9760 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9761 		else if (sata_atapicdvd_write_cache == 0)
9762 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9763 		/*
9764 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
9765 		 * a current setting of the drive's write cache is used.
9766 		 */
9767 		break;
9768 	case SATA_DTYPE_ATAPITAPE:
9769 		if (sata_atapitape_write_cache == 1)
9770 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9771 		else if (sata_atapitape_write_cache == 0)
9772 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9773 		/*
9774 		 * When sata_atapitape_write_cache value is not 0 or 1,
9775 		 * a current setting of the drive's write cache is used.
9776 		 */
9777 		break;
9778 	case SATA_DTYPE_ATAPIDISK:
9779 		if (sata_atapidisk_write_cache == 1)
9780 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9781 		else if (sata_atapidisk_write_cache == 0)
9782 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9783 		/*
9784 		 * When sata_atapidisk_write_cache value is not 0 or 1,
9785 		 * a current setting of the drive's write cache is used.
9786 		 */
9787 		break;
9788 	}
9789 }
9790 
9791 
9792 /*
9793  * Validate sata address.
9794  * Specified cport, pmport and qualifier has to match
9795  * passed sata_scsi configuration info.
9796  * The presence of an attached device is not verified.
9797  *
9798  * Returns 0 when address is valid, -1 otherwise.
9799  */
9800 static int
9801 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9802 	int pmport, int qual)
9803 {
9804 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9805 		goto invalid_address;
9806 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9807 		goto invalid_address;
9808 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9809 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9810 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9811 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9812 		goto invalid_address;
9813 
9814 	return (0);
9815 
9816 invalid_address:
9817 	return (-1);
9818 
9819 }
9820 
9821 /*
9822  * Validate scsi address
9823  * SCSI target address is translated into SATA cport/pmport and compared
9824  * with a controller port/device configuration. LUN has to be 0.
9825  * Returns 0 if a scsi target refers to an attached device,
9826  * returns 1 if address is valid but device is not attached,
9827  * returns -1 if bad address or device is of an unsupported type.
9828  * Upon return sata_device argument is set.
9829  */
9830 static int
9831 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9832 	struct scsi_address *ap, sata_device_t *sata_device)
9833 {
9834 	int cport, pmport, qual, rval;
9835 
9836 	rval = -1;	/* Invalid address */
9837 	if (ap->a_lun != 0)
9838 		goto out;
9839 
9840 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9841 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9842 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9843 
9844 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9845 		goto out;
9846 
9847 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9848 	    0) {
9849 
9850 		sata_cport_info_t *cportinfo;
9851 		sata_pmult_info_t *pmultinfo;
9852 		sata_drive_info_t *sdinfo = NULL;
9853 
9854 		rval = 1;	/* Valid sata address */
9855 
9856 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9857 		if (qual == SATA_ADDR_DCPORT) {
9858 			if (cportinfo == NULL ||
9859 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9860 				goto out;
9861 
9862 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9863 			    (cportinfo->cport_dev_type &
9864 			    SATA_VALID_DEV_TYPE) == 0) {
9865 				rval = -1;
9866 				goto out;
9867 			}
9868 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9869 
9870 		} else if (qual == SATA_ADDR_DPMPORT) {
9871 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9872 			if (pmultinfo == NULL) {
9873 				rval = -1;
9874 				goto out;
9875 			}
9876 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9877 			    NULL ||
9878 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9879 			    pmport) == SATA_DTYPE_NONE)
9880 				goto out;
9881 
9882 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9883 			    pmport);
9884 		} else {
9885 			rval = -1;
9886 			goto out;
9887 		}
9888 		if ((sdinfo == NULL) ||
9889 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9890 			goto out;
9891 
9892 		sata_device->satadev_type = sdinfo->satadrv_type;
9893 		sata_device->satadev_addr.qual = qual;
9894 		sata_device->satadev_addr.cport = cport;
9895 		sata_device->satadev_addr.pmport = pmport;
9896 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9897 		return (0);
9898 	}
9899 out:
9900 	if (rval == 1) {
9901 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9902 		    "sata_validate_scsi_address: no valid target %x lun %x",
9903 		    ap->a_target, ap->a_lun);
9904 	}
9905 	return (rval);
9906 }
9907 
9908 /*
9909  * Find dip corresponding to passed device number
9910  *
9911  * Returns NULL if invalid device number is passed or device cannot be found,
9912  * Returns dip is device is found.
9913  */
9914 static dev_info_t *
9915 sata_devt_to_devinfo(dev_t dev)
9916 {
9917 	dev_info_t *dip;
9918 #ifndef __lock_lint
9919 	struct devnames *dnp;
9920 	major_t major = getmajor(dev);
9921 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9922 
9923 	if (major >= devcnt)
9924 		return (NULL);
9925 
9926 	dnp = &devnamesp[major];
9927 	LOCK_DEV_OPS(&(dnp->dn_lock));
9928 	dip = dnp->dn_head;
9929 	while (dip && (ddi_get_instance(dip) != instance)) {
9930 		dip = ddi_get_next(dip);
9931 	}
9932 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9933 #endif
9934 
9935 	return (dip);
9936 }
9937 
9938 
9939 /*
9940  * Probe device.
9941  * This function issues Identify Device command and initializes local
9942  * sata_drive_info structure if the device can be identified.
9943  * The device type is determined by examining Identify Device
9944  * command response.
9945  * If the sata_hba_inst has linked drive info structure for this
9946  * device address, the Identify Device data is stored into sata_drive_info
9947  * structure linked to the port info structure.
9948  *
9949  * sata_device has to refer to the valid sata port(s) for HBA described
9950  * by sata_hba_inst structure.
9951  *
9952  * Returns:
9953  *	SATA_SUCCESS if device type was successfully probed and port-linked
9954  *		drive info structure was updated;
9955  * 	SATA_FAILURE if there is no device, or device was not probed
9956  *		successully;
9957  *	SATA_RETRY if device probe can be retried later.
9958  * If a device cannot be identified, sata_device's dev_state and dev_type
9959  * fields are set to unknown.
9960  * There are no retries in this function. Any retries should be managed by
9961  * the caller.
9962  */
9963 
9964 
9965 static int
9966 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9967 {
9968 	sata_drive_info_t *sdinfo;
9969 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9970 	int rval;
9971 
9972 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9973 	    sata_device->satadev_addr.cport) &
9974 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9975 
9976 	sata_device->satadev_type = SATA_DTYPE_NONE;
9977 
9978 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9979 	    sata_device->satadev_addr.cport)));
9980 
9981 	/* Get pointer to port-linked sata device info structure */
9982 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9983 	if (sdinfo != NULL) {
9984 		sdinfo->satadrv_state &=
9985 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9986 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9987 	} else {
9988 		/* No device to probe */
9989 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9990 		    sata_device->satadev_addr.cport)));
9991 		sata_device->satadev_type = SATA_DTYPE_NONE;
9992 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9993 		return (SATA_FAILURE);
9994 	}
9995 	/*
9996 	 * Need to issue both types of identify device command and
9997 	 * determine device type by examining retreived data/status.
9998 	 * First, ATA Identify Device.
9999 	 */
10000 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
10001 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
10002 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10003 	    sata_device->satadev_addr.cport)));
10004 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
10005 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
10006 	if (rval == SATA_RETRY) {
10007 		/* We may try to check for ATAPI device */
10008 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
10009 			/*
10010 			 * HBA supports ATAPI - try to issue Identify Packet
10011 			 * Device command.
10012 			 */
10013 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
10014 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
10015 		}
10016 	}
10017 	if (rval == SATA_SUCCESS) {
10018 		/*
10019 		 * Got something responding positively to ATA Identify Device
10020 		 * or to Identify Packet Device cmd.
10021 		 * Save last used device type.
10022 		 */
10023 		sata_device->satadev_type = new_sdinfo.satadrv_type;
10024 
10025 		/* save device info, if possible */
10026 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10027 		    sata_device->satadev_addr.cport)));
10028 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10029 		if (sdinfo == NULL) {
10030 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10031 			    sata_device->satadev_addr.cport)));
10032 			return (SATA_FAILURE);
10033 		}
10034 		/*
10035 		 * Copy drive info into the port-linked drive info structure.
10036 		 */
10037 		*sdinfo = new_sdinfo;
10038 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10039 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
10040 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10041 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10042 			    sata_device->satadev_addr.cport) =
10043 			    sdinfo->satadrv_type;
10044 		else /* SATA_ADDR_DPMPORT */
10045 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10046 			    sata_device->satadev_addr.cport,
10047 			    sata_device->satadev_addr.pmport) =
10048 			    sdinfo->satadrv_type;
10049 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10050 		    sata_device->satadev_addr.cport)));
10051 		return (SATA_SUCCESS);
10052 	}
10053 
10054 	/*
10055 	 * It may be SATA_RETRY or SATA_FAILURE return.
10056 	 * Looks like we cannot determine the device type at this time.
10057 	 */
10058 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10059 	    sata_device->satadev_addr.cport)));
10060 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10061 	if (sdinfo != NULL) {
10062 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
10063 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10064 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10065 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
10066 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10067 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10068 			    sata_device->satadev_addr.cport) =
10069 			    SATA_DTYPE_UNKNOWN;
10070 		else {
10071 			/* SATA_ADDR_DPMPORT */
10072 			if ((SATA_PMULT_INFO(sata_hba_inst,
10073 			    sata_device->satadev_addr.cport) != NULL) &&
10074 			    (SATA_PMPORT_INFO(sata_hba_inst,
10075 			    sata_device->satadev_addr.cport,
10076 			    sata_device->satadev_addr.pmport) != NULL))
10077 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10078 				    sata_device->satadev_addr.cport,
10079 				    sata_device->satadev_addr.pmport) =
10080 				    SATA_DTYPE_UNKNOWN;
10081 		}
10082 	}
10083 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10084 	    sata_device->satadev_addr.cport)));
10085 	return (rval);
10086 }
10087 
10088 
10089 /*
10090  * Get pointer to sata_drive_info structure.
10091  *
10092  * The sata_device has to contain address (cport, pmport and qualifier) for
10093  * specified sata_scsi structure.
10094  *
10095  * Returns NULL if device address is not valid for this HBA configuration.
10096  * Otherwise, returns a pointer to sata_drive_info structure.
10097  *
10098  * This function should be called with a port mutex held.
10099  */
10100 static sata_drive_info_t *
10101 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
10102     sata_device_t *sata_device)
10103 {
10104 	uint8_t cport = sata_device->satadev_addr.cport;
10105 	uint8_t pmport = sata_device->satadev_addr.pmport;
10106 	uint8_t qual = sata_device->satadev_addr.qual;
10107 
10108 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10109 		return (NULL);
10110 
10111 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
10112 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
10113 		/* Port not probed yet */
10114 		return (NULL);
10115 
10116 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
10117 		return (NULL);
10118 
10119 	if (qual == SATA_ADDR_DCPORT) {
10120 		/* Request for a device on a controller port */
10121 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
10122 		    SATA_DTYPE_PMULT)
10123 			/* Port multiplier attached */
10124 			return (NULL);
10125 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
10126 	}
10127 	if (qual == SATA_ADDR_DPMPORT) {
10128 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
10129 		    SATA_DTYPE_PMULT)
10130 			return (NULL);
10131 
10132 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
10133 			return (NULL);
10134 
10135 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
10136 	}
10137 
10138 	/* we should not get here */
10139 	return (NULL);
10140 }
10141 
10142 
10143 /*
10144  * sata_identify_device.
10145  * Send Identify Device command to SATA HBA driver.
10146  * If command executes successfully, update sata_drive_info structure pointed
10147  * to by sdinfo argument, including Identify Device data.
10148  * If command fails, invalidate data in sata_drive_info.
10149  *
10150  * Cannot be called from interrupt level.
10151  *
10152  * Returns:
10153  * SATA_SUCCESS if the device was identified as a supported device,
10154  * SATA_RETRY if the device was not identified but could be retried,
10155  * SATA_FAILURE if the device was not identified and identify attempt
10156  *	should not be retried.
10157  */
10158 static int
10159 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
10160     sata_drive_info_t *sdinfo)
10161 {
10162 	uint16_t cfg_word;
10163 	int rval;
10164 
10165 	/* fetch device identify data */
10166 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
10167 	    sdinfo)) != SATA_SUCCESS)
10168 		goto fail_unknown;
10169 
10170 	cfg_word = sdinfo->satadrv_id.ai_config;
10171 
10172 	/* Set the correct device type */
10173 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
10174 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10175 	} else if (cfg_word == SATA_CFA_TYPE) {
10176 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
10177 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10178 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
10179 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
10180 		case SATA_ATAPI_CDROM_DEV:
10181 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
10182 			break;
10183 		case SATA_ATAPI_SQACC_DEV:
10184 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
10185 			break;
10186 		case SATA_ATAPI_DIRACC_DEV:
10187 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
10188 			break;
10189 		default:
10190 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10191 		}
10192 	} else {
10193 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10194 	}
10195 
10196 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10197 		if (sdinfo->satadrv_capacity == 0) {
10198 			/* Non-LBA disk. Too bad... */
10199 			sata_log(sata_hba_inst, CE_WARN,
10200 			    "SATA disk device at port %d does not support LBA",
10201 			    sdinfo->satadrv_addr.cport);
10202 			rval = SATA_FAILURE;
10203 			goto fail_unknown;
10204 		}
10205 	}
10206 #if 0
10207 	/* Left for historical reason */
10208 	/*
10209 	 * Some initial version of SATA spec indicated that at least
10210 	 * UDMA mode 4 has to be supported. It is not metioned in
10211 	 * SerialATA 2.6, so this restriction is removed.
10212 	 */
10213 	/* Check for Ultra DMA modes 6 through 0 being supported */
10214 	for (i = 6; i >= 0; --i) {
10215 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
10216 			break;
10217 	}
10218 
10219 	/*
10220 	 * At least UDMA 4 mode has to be supported. If mode 4 or
10221 	 * higher are not supported by the device, fail this
10222 	 * device.
10223 	 */
10224 	if (i < 4) {
10225 		/* No required Ultra DMA mode supported */
10226 		sata_log(sata_hba_inst, CE_WARN,
10227 		    "SATA disk device at port %d does not support UDMA "
10228 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
10229 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10230 		    "mode 4 or higher required, %d supported", i));
10231 		rval = SATA_FAILURE;
10232 		goto fail_unknown;
10233 	}
10234 #endif
10235 
10236 	/*
10237 	 * For Disk devices, if it doesn't support UDMA mode, we would
10238 	 * like to return failure directly.
10239 	 */
10240 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
10241 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10242 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
10243 		sata_log(sata_hba_inst, CE_WARN,
10244 		    "SATA disk device at port %d does not support UDMA",
10245 		    sdinfo->satadrv_addr.cport);
10246 		rval = SATA_FAILURE;
10247 		goto fail_unknown;
10248 	}
10249 
10250 	return (SATA_SUCCESS);
10251 
10252 fail_unknown:
10253 	/* Invalidate sata_drive_info ? */
10254 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10255 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10256 	return (rval);
10257 }
10258 
10259 /*
10260  * Log/display device information
10261  */
10262 static void
10263 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
10264     sata_drive_info_t *sdinfo)
10265 {
10266 	int valid_version;
10267 	char msg_buf[MAXPATHLEN];
10268 	int i;
10269 
10270 	/* Show HBA path */
10271 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
10272 
10273 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
10274 
10275 	switch (sdinfo->satadrv_type) {
10276 	case SATA_DTYPE_ATADISK:
10277 		(void) sprintf(msg_buf, "SATA disk device at");
10278 		break;
10279 
10280 	case SATA_DTYPE_ATAPICD:
10281 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
10282 		break;
10283 
10284 	case SATA_DTYPE_ATAPITAPE:
10285 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
10286 		break;
10287 
10288 	case SATA_DTYPE_ATAPIDISK:
10289 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
10290 		break;
10291 
10292 	case SATA_DTYPE_UNKNOWN:
10293 		(void) sprintf(msg_buf,
10294 		    "Unsupported SATA device type (cfg 0x%x) at ",
10295 		    sdinfo->satadrv_id.ai_config);
10296 		break;
10297 	}
10298 
10299 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
10300 		cmn_err(CE_CONT, "?\t%s port %d\n",
10301 		    msg_buf, sdinfo->satadrv_addr.cport);
10302 	else
10303 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
10304 		    msg_buf, sdinfo->satadrv_addr.cport,
10305 		    sdinfo->satadrv_addr.pmport);
10306 
10307 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
10308 	    sizeof (sdinfo->satadrv_id.ai_model));
10309 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
10310 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
10311 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
10312 
10313 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
10314 	    sizeof (sdinfo->satadrv_id.ai_fw));
10315 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
10316 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
10317 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
10318 
10319 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
10320 	    sizeof (sdinfo->satadrv_id.ai_drvser));
10321 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
10322 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
10323 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10324 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10325 	} else {
10326 		/*
10327 		 * Some drives do not implement serial number and may
10328 		 * violate the spec by providing spaces rather than zeros
10329 		 * in serial number field. Scan the buffer to detect it.
10330 		 */
10331 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
10332 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
10333 				break;
10334 		}
10335 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
10336 			cmn_err(CE_CONT, "?\tserial number - none\n");
10337 		} else {
10338 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10339 		}
10340 	}
10341 
10342 #ifdef SATA_DEBUG
10343 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
10344 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
10345 		int i;
10346 		for (i = 14; i >= 2; i--) {
10347 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
10348 				valid_version = i;
10349 				break;
10350 			}
10351 		}
10352 		cmn_err(CE_CONT,
10353 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
10354 		    valid_version,
10355 		    sdinfo->satadrv_id.ai_majorversion,
10356 		    sdinfo->satadrv_id.ai_minorversion);
10357 	}
10358 #endif
10359 	/* Log some info */
10360 	cmn_err(CE_CONT, "?\tsupported features:\n");
10361 	msg_buf[0] = '\0';
10362 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10363 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
10364 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
10365 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
10366 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
10367 	}
10368 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
10369 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
10370 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
10371 		(void) strlcat(msg_buf, ", Native Command Queueing",
10372 		    MAXPATHLEN);
10373 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
10374 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
10375 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
10376 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
10377 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
10378 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
10379 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
10380 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
10381 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
10382 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
10383 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
10384 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
10385 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
10386 	if (sdinfo->satadrv_features_support &
10387 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
10388 		msg_buf[0] = '\0';
10389 		(void) snprintf(msg_buf, MAXPATHLEN,
10390 		    "Supported queue depth %d",
10391 		    sdinfo->satadrv_queue_depth);
10392 		if (!(sata_func_enable &
10393 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
10394 			(void) strlcat(msg_buf,
10395 			    " - queueing disabled globally", MAXPATHLEN);
10396 		else if (sdinfo->satadrv_queue_depth >
10397 		    sdinfo->satadrv_max_queue_depth) {
10398 			(void) snprintf(&msg_buf[strlen(msg_buf)],
10399 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
10400 			    (int)sdinfo->satadrv_max_queue_depth);
10401 		}
10402 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
10403 	}
10404 
10405 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10406 #ifdef __i386
10407 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
10408 		    sdinfo->satadrv_capacity);
10409 #else
10410 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
10411 		    sdinfo->satadrv_capacity);
10412 #endif
10413 		cmn_err(CE_CONT, "?%s", msg_buf);
10414 	}
10415 }
10416 
10417 
10418 /*
10419  * sata_save_drive_settings extracts current setting of the device and stores
10420  * it for future reference, in case the device setup would need to be restored
10421  * after the device reset.
10422  *
10423  * For all devices read ahead and write cache settings are saved, if the
10424  * device supports these features at all.
10425  * For ATAPI devices the Removable Media Status Notification setting is saved.
10426  */
10427 static void
10428 sata_save_drive_settings(sata_drive_info_t *sdinfo)
10429 {
10430 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
10431 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
10432 
10433 		/* Current setting of Read Ahead (and Read Cache) */
10434 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
10435 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10436 		else
10437 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
10438 
10439 		/* Current setting of Write Cache */
10440 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
10441 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10442 		else
10443 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10444 	}
10445 
10446 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10447 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
10448 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
10449 		else
10450 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
10451 	}
10452 }
10453 
10454 
10455 /*
10456  * sata_check_capacity function determines a disk capacity
10457  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
10458  *
10459  * NOTE: CHS mode is not supported! If a device does not support LBA,
10460  * this function is not called.
10461  *
10462  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
10463  */
10464 static uint64_t
10465 sata_check_capacity(sata_drive_info_t *sdinfo)
10466 {
10467 	uint64_t capacity = 0;
10468 	int i;
10469 
10470 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
10471 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
10472 		/* Capacity valid only for LBA-addressable disk devices */
10473 		return (0);
10474 
10475 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
10476 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
10477 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
10478 		/* LBA48 mode supported and enabled */
10479 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
10480 		    SATA_DEV_F_LBA28;
10481 		for (i = 3;  i >= 0;  --i) {
10482 			capacity <<= 16;
10483 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
10484 		}
10485 	} else {
10486 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
10487 		capacity <<= 16;
10488 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
10489 		if (capacity >= 0x1000000)
10490 			/* LBA28 mode */
10491 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
10492 	}
10493 	return (capacity);
10494 }
10495 
10496 
10497 /*
10498  * Allocate consistent buffer for DMA transfer
10499  *
10500  * Cannot be called from interrupt level or with mutex held - it may sleep.
10501  *
10502  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
10503  */
10504 static struct buf *
10505 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
10506 {
10507 	struct scsi_address ap;
10508 	struct buf *bp;
10509 	ddi_dma_attr_t	cur_dma_attr;
10510 
10511 	ASSERT(spx->txlt_sata_pkt != NULL);
10512 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
10513 	ap.a_target = SATA_TO_SCSI_TARGET(
10514 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
10515 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
10516 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
10517 	ap.a_lun = 0;
10518 
10519 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
10520 	    B_READ, SLEEP_FUNC, NULL);
10521 
10522 	if (bp != NULL) {
10523 		/* Allocate DMA resources for this buffer */
10524 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
10525 		/*
10526 		 * We use a local version of the dma_attr, to account
10527 		 * for a device addressing limitations.
10528 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
10529 		 * will cause dma attributes to be adjusted to a lowest
10530 		 * acceptable level.
10531 		 */
10532 		sata_adjust_dma_attr(NULL,
10533 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
10534 
10535 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
10536 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
10537 			scsi_free_consistent_buf(bp);
10538 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10539 			bp = NULL;
10540 		}
10541 	}
10542 	return (bp);
10543 }
10544 
10545 /*
10546  * Release local buffer (consistent buffer for DMA transfer) allocated
10547  * via sata_alloc_local_buffer().
10548  */
10549 static void
10550 sata_free_local_buffer(sata_pkt_txlate_t *spx)
10551 {
10552 	ASSERT(spx->txlt_sata_pkt != NULL);
10553 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
10554 
10555 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
10556 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
10557 
10558 	sata_common_free_dma_rsrcs(spx);
10559 
10560 	/* Free buffer */
10561 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
10562 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10563 }
10564 
10565 /*
10566  * Allocate sata_pkt
10567  * Pkt structure version and embedded strcutures version are initialized.
10568  * sata_pkt and sata_pkt_txlate structures are cross-linked.
10569  *
10570  * Since this may be called in interrupt context by sata_scsi_init_pkt,
10571  * callback argument determines if it can sleep or not.
10572  * Hence, it should not be called from interrupt context.
10573  *
10574  * If successful, non-NULL pointer to a sata pkt is returned.
10575  * Upon failure, NULL pointer is returned.
10576  */
10577 static sata_pkt_t *
10578 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
10579 {
10580 	sata_pkt_t *spkt;
10581 	int kmsflag;
10582 
10583 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
10584 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
10585 	if (spkt == NULL) {
10586 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10587 		    "sata_pkt_alloc: failed"));
10588 		return (NULL);
10589 	}
10590 	spkt->satapkt_rev = SATA_PKT_REV;
10591 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
10592 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
10593 	spkt->satapkt_framework_private = spx;
10594 	spx->txlt_sata_pkt = spkt;
10595 	return (spkt);
10596 }
10597 
10598 /*
10599  * Free sata pkt allocated via sata_pkt_alloc()
10600  */
10601 static void
10602 sata_pkt_free(sata_pkt_txlate_t *spx)
10603 {
10604 	ASSERT(spx->txlt_sata_pkt != NULL);
10605 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
10606 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
10607 	spx->txlt_sata_pkt = NULL;
10608 }
10609 
10610 
10611 /*
10612  * Adjust DMA attributes.
10613  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
10614  * from 8 bits to 16 bits, depending on a command being used.
10615  * Limiting max block count arbitrarily to 256 for all read/write
10616  * commands may affects performance, so check both the device and
10617  * controller capability before adjusting dma attributes.
10618  */
10619 void
10620 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
10621     ddi_dma_attr_t *adj_dma_attr)
10622 {
10623 	uint32_t count_max;
10624 
10625 	/* Copy original attributes */
10626 	*adj_dma_attr = *dma_attr;
10627 	/*
10628 	 * Things to consider: device addressing capability,
10629 	 * "excessive" controller DMA capabilities.
10630 	 * If a device is being probed/initialized, there are
10631 	 * no device info - use default limits then.
10632 	 */
10633 	if (sdinfo == NULL) {
10634 		count_max = dma_attr->dma_attr_granular * 0x100;
10635 		if (dma_attr->dma_attr_count_max > count_max)
10636 			adj_dma_attr->dma_attr_count_max = count_max;
10637 		if (dma_attr->dma_attr_maxxfer > count_max)
10638 			adj_dma_attr->dma_attr_maxxfer = count_max;
10639 		return;
10640 	}
10641 
10642 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10643 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
10644 			/*
10645 			 * 16-bit sector count may be used - we rely on
10646 			 * the assumption that only read and write cmds
10647 			 * will request more than 256 sectors worth of data
10648 			 */
10649 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
10650 		} else {
10651 			/*
10652 			 * 8-bit sector count will be used - default limits
10653 			 * for dma attributes
10654 			 */
10655 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
10656 		}
10657 		/*
10658 		 * Adjust controler dma attributes, if necessary
10659 		 */
10660 		if (dma_attr->dma_attr_count_max > count_max)
10661 			adj_dma_attr->dma_attr_count_max = count_max;
10662 		if (dma_attr->dma_attr_maxxfer > count_max)
10663 			adj_dma_attr->dma_attr_maxxfer = count_max;
10664 	}
10665 }
10666 
10667 
10668 /*
10669  * Allocate DMA resources for the buffer
10670  * This function handles initial DMA resource allocation as well as
10671  * DMA window shift and may be called repeatedly for the same DMA window
10672  * until all DMA cookies in the DMA window are processed.
10673  * To guarantee that there is always a coherent set of cookies to process
10674  * by SATA HBA driver (observing alignment, device granularity, etc.),
10675  * the number of slots for DMA cookies is equal to lesser of  a number of
10676  * cookies in a DMA window and a max number of scatter/gather entries.
10677  *
10678  * Returns DDI_SUCCESS upon successful operation.
10679  * Return failure code of a failing command or DDI_FAILURE when
10680  * internal cleanup failed.
10681  */
10682 static int
10683 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
10684     int (*callback)(caddr_t), caddr_t arg,
10685     ddi_dma_attr_t *cur_dma_attr)
10686 {
10687 	int	rval;
10688 	off_t	offset;
10689 	size_t	size;
10690 	int	max_sg_len, req_len, i;
10691 	uint_t	dma_flags;
10692 	struct buf	*bp;
10693 	uint64_t	cur_txfer_len;
10694 
10695 
10696 	ASSERT(spx->txlt_sata_pkt != NULL);
10697 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
10698 	ASSERT(bp != NULL);
10699 
10700 
10701 	if (spx->txlt_buf_dma_handle == NULL) {
10702 		/*
10703 		 * No DMA resources allocated so far - this is a first call
10704 		 * for this sata pkt.
10705 		 */
10706 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
10707 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
10708 
10709 		if (rval != DDI_SUCCESS) {
10710 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10711 			    "sata_dma_buf_setup: no buf DMA resources %x",
10712 			    rval));
10713 			return (rval);
10714 		}
10715 
10716 		if (bp->b_flags & B_READ)
10717 			dma_flags = DDI_DMA_READ;
10718 		else
10719 			dma_flags = DDI_DMA_WRITE;
10720 
10721 		if (flags & PKT_CONSISTENT)
10722 			dma_flags |= DDI_DMA_CONSISTENT;
10723 
10724 		if (flags & PKT_DMA_PARTIAL)
10725 			dma_flags |= DDI_DMA_PARTIAL;
10726 
10727 		/*
10728 		 * Check buffer alignment and size against dma attributes
10729 		 * Consider dma_attr_align only. There may be requests
10730 		 * with the size lower than device granularity, but they
10731 		 * will not read/write from/to the device, so no adjustment
10732 		 * is necessary. The dma_attr_minxfer theoretically should
10733 		 * be considered, but no HBA driver is checking it.
10734 		 */
10735 		if (IS_P2ALIGNED(bp->b_un.b_addr,
10736 		    cur_dma_attr->dma_attr_align)) {
10737 			rval = ddi_dma_buf_bind_handle(
10738 			    spx->txlt_buf_dma_handle,
10739 			    bp, dma_flags, callback, arg,
10740 			    &spx->txlt_dma_cookie,
10741 			    &spx->txlt_curwin_num_dma_cookies);
10742 		} else { /* Buffer is not aligned */
10743 
10744 			int	(*ddicallback)(caddr_t);
10745 			size_t	bufsz;
10746 
10747 			/* Check id sleeping is allowed */
10748 			ddicallback = (callback == NULL_FUNC) ?
10749 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
10750 
10751 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10752 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
10753 			    (void *)bp->b_un.b_addr, bp->b_bcount);
10754 
10755 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
10756 				/*
10757 				 * CPU will need to access data in the buffer
10758 				 * (for copying) so map it.
10759 				 */
10760 				bp_mapin(bp);
10761 
10762 			ASSERT(spx->txlt_tmp_buf == NULL);
10763 
10764 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
10765 			rval = ddi_dma_mem_alloc(
10766 			    spx->txlt_buf_dma_handle,
10767 			    bp->b_bcount,
10768 			    &sata_acc_attr,
10769 			    DDI_DMA_STREAMING,
10770 			    ddicallback, NULL,
10771 			    &spx->txlt_tmp_buf,
10772 			    &bufsz,
10773 			    &spx->txlt_tmp_buf_handle);
10774 
10775 			if (rval != DDI_SUCCESS) {
10776 				/* DMA mapping failed */
10777 				(void) ddi_dma_free_handle(
10778 				    &spx->txlt_buf_dma_handle);
10779 				spx->txlt_buf_dma_handle = NULL;
10780 #ifdef SATA_DEBUG
10781 				mbuffail_count++;
10782 #endif
10783 				SATADBG1(SATA_DBG_DMA_SETUP,
10784 				    spx->txlt_sata_hba_inst,
10785 				    "sata_dma_buf_setup: "
10786 				    "buf dma mem alloc failed %x\n", rval);
10787 				return (rval);
10788 			}
10789 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
10790 			    cur_dma_attr->dma_attr_align));
10791 
10792 #ifdef SATA_DEBUG
10793 			mbuf_count++;
10794 
10795 			if (bp->b_bcount != bufsz)
10796 				/*
10797 				 * This will require special handling, because
10798 				 * DMA cookies will be based on the temporary
10799 				 * buffer size, not the original buffer
10800 				 * b_bcount, so the residue may have to
10801 				 * be counted differently.
10802 				 */
10803 				SATADBG2(SATA_DBG_DMA_SETUP,
10804 				    spx->txlt_sata_hba_inst,
10805 				    "sata_dma_buf_setup: bp size %x != "
10806 				    "bufsz %x\n", bp->b_bcount, bufsz);
10807 #endif
10808 			if (dma_flags & DDI_DMA_WRITE) {
10809 				/*
10810 				 * Write operation - copy data into
10811 				 * an aligned temporary buffer. Buffer will be
10812 				 * synced for device by ddi_dma_addr_bind_handle
10813 				 */
10814 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10815 				    bp->b_bcount);
10816 			}
10817 
10818 			rval = ddi_dma_addr_bind_handle(
10819 			    spx->txlt_buf_dma_handle,
10820 			    NULL,
10821 			    spx->txlt_tmp_buf,
10822 			    bufsz, dma_flags, ddicallback, 0,
10823 			    &spx->txlt_dma_cookie,
10824 			    &spx->txlt_curwin_num_dma_cookies);
10825 		}
10826 
10827 		switch (rval) {
10828 		case DDI_DMA_PARTIAL_MAP:
10829 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10830 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10831 			/*
10832 			 * Partial DMA mapping.
10833 			 * Retrieve number of DMA windows for this request.
10834 			 */
10835 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10836 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10837 				if (spx->txlt_tmp_buf != NULL) {
10838 					ddi_dma_mem_free(
10839 					    &spx->txlt_tmp_buf_handle);
10840 					spx->txlt_tmp_buf = NULL;
10841 				}
10842 				(void) ddi_dma_unbind_handle(
10843 				    spx->txlt_buf_dma_handle);
10844 				(void) ddi_dma_free_handle(
10845 				    &spx->txlt_buf_dma_handle);
10846 				spx->txlt_buf_dma_handle = NULL;
10847 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10848 				    "sata_dma_buf_setup: numwin failed\n"));
10849 				return (DDI_FAILURE);
10850 			}
10851 			SATADBG2(SATA_DBG_DMA_SETUP,
10852 			    spx->txlt_sata_hba_inst,
10853 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10854 			    spx->txlt_num_dma_win,
10855 			    spx->txlt_curwin_num_dma_cookies);
10856 			spx->txlt_cur_dma_win = 0;
10857 			break;
10858 
10859 		case DDI_DMA_MAPPED:
10860 			/* DMA fully mapped */
10861 			spx->txlt_num_dma_win = 1;
10862 			spx->txlt_cur_dma_win = 0;
10863 			SATADBG1(SATA_DBG_DMA_SETUP,
10864 			    spx->txlt_sata_hba_inst,
10865 			    "sata_dma_buf_setup: windows: 1 "
10866 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10867 			break;
10868 
10869 		default:
10870 			/* DMA mapping failed */
10871 			if (spx->txlt_tmp_buf != NULL) {
10872 				ddi_dma_mem_free(
10873 				    &spx->txlt_tmp_buf_handle);
10874 				spx->txlt_tmp_buf = NULL;
10875 			}
10876 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10877 			spx->txlt_buf_dma_handle = NULL;
10878 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10879 			    "sata_dma_buf_setup: buf dma handle binding "
10880 			    "failed %x\n", rval));
10881 			return (rval);
10882 		}
10883 		spx->txlt_curwin_processed_dma_cookies = 0;
10884 		spx->txlt_dma_cookie_list = NULL;
10885 	} else {
10886 		/*
10887 		 * DMA setup is reused. Check if we need to process more
10888 		 * cookies in current window, or to get next window, if any.
10889 		 */
10890 
10891 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10892 		    spx->txlt_curwin_num_dma_cookies);
10893 
10894 		if (spx->txlt_curwin_processed_dma_cookies ==
10895 		    spx->txlt_curwin_num_dma_cookies) {
10896 			/*
10897 			 * All cookies from current DMA window were processed.
10898 			 * Get next DMA window.
10899 			 */
10900 			spx->txlt_cur_dma_win++;
10901 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10902 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10903 				    spx->txlt_cur_dma_win, &offset, &size,
10904 				    &spx->txlt_dma_cookie,
10905 				    &spx->txlt_curwin_num_dma_cookies);
10906 				spx->txlt_curwin_processed_dma_cookies = 0;
10907 			} else {
10908 				/* No more windows! End of request! */
10909 				/* What to do? - panic for now */
10910 				ASSERT(spx->txlt_cur_dma_win >=
10911 				    spx->txlt_num_dma_win);
10912 
10913 				spx->txlt_curwin_num_dma_cookies = 0;
10914 				spx->txlt_curwin_processed_dma_cookies = 0;
10915 				spx->txlt_sata_pkt->
10916 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10917 				return (DDI_SUCCESS);
10918 			}
10919 		}
10920 	}
10921 	/* There better be at least one DMA cookie outstanding */
10922 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10923 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10924 
10925 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10926 		/* The default cookie slot was used in previous run */
10927 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10928 		spx->txlt_dma_cookie_list = NULL;
10929 		spx->txlt_dma_cookie_list_len = 0;
10930 	}
10931 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10932 		/*
10933 		 * Processing a new DMA window - set-up dma cookies list.
10934 		 * We may reuse previously allocated cookie array if it is
10935 		 * possible.
10936 		 */
10937 		if (spx->txlt_dma_cookie_list != NULL &&
10938 		    spx->txlt_dma_cookie_list_len <
10939 		    spx->txlt_curwin_num_dma_cookies) {
10940 			/*
10941 			 * New DMA window contains more cookies than
10942 			 * the previous one. We need larger cookie list - free
10943 			 * the old one.
10944 			 */
10945 			(void) kmem_free(spx->txlt_dma_cookie_list,
10946 			    spx->txlt_dma_cookie_list_len *
10947 			    sizeof (ddi_dma_cookie_t));
10948 			spx->txlt_dma_cookie_list = NULL;
10949 			spx->txlt_dma_cookie_list_len = 0;
10950 		}
10951 		if (spx->txlt_dma_cookie_list == NULL) {
10952 			/*
10953 			 * Calculate lesser of number of cookies in this
10954 			 * DMA window and number of s/g entries.
10955 			 */
10956 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10957 			req_len = MIN(max_sg_len,
10958 			    spx->txlt_curwin_num_dma_cookies);
10959 
10960 			/* Allocate new dma cookie array if necessary */
10961 			if (req_len == 1) {
10962 				/* Only one cookie - no need for a list */
10963 				spx->txlt_dma_cookie_list =
10964 				    &spx->txlt_dma_cookie;
10965 				spx->txlt_dma_cookie_list_len = 1;
10966 			} else {
10967 				/*
10968 				 * More than one cookie - try to allocate space.
10969 				 */
10970 				spx->txlt_dma_cookie_list = kmem_zalloc(
10971 				    sizeof (ddi_dma_cookie_t) * req_len,
10972 				    callback == NULL_FUNC ? KM_NOSLEEP :
10973 				    KM_SLEEP);
10974 				if (spx->txlt_dma_cookie_list == NULL) {
10975 					SATADBG1(SATA_DBG_DMA_SETUP,
10976 					    spx->txlt_sata_hba_inst,
10977 					    "sata_dma_buf_setup: cookie list "
10978 					    "allocation failed\n", NULL);
10979 					/*
10980 					 * We could not allocate space for
10981 					 * neccessary number of dma cookies in
10982 					 * this window, so we fail this request.
10983 					 * Next invocation would try again to
10984 					 * allocate space for cookie list.
10985 					 * Note:Packet residue was not modified.
10986 					 */
10987 					return (DDI_DMA_NORESOURCES);
10988 				} else {
10989 					spx->txlt_dma_cookie_list_len = req_len;
10990 				}
10991 			}
10992 		}
10993 		/*
10994 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10995 		 * First cookie was already fetched.
10996 		 */
10997 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10998 		cur_txfer_len =
10999 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
11000 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
11001 		spx->txlt_curwin_processed_dma_cookies++;
11002 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
11003 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
11004 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
11005 			    &spx->txlt_dma_cookie_list[i]);
11006 			cur_txfer_len +=
11007 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
11008 			spx->txlt_curwin_processed_dma_cookies++;
11009 			spx->txlt_sata_pkt->
11010 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
11011 		}
11012 	} else {
11013 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
11014 		    "sata_dma_buf_setup: sliding within DMA window, "
11015 		    "cur cookie %d, total cookies %d\n",
11016 		    spx->txlt_curwin_processed_dma_cookies,
11017 		    spx->txlt_curwin_num_dma_cookies);
11018 
11019 		/*
11020 		 * Not all cookies from the current dma window were used because
11021 		 * of s/g limitation.
11022 		 * There is no need to re-size the list - it was set at
11023 		 * optimal size, or only default entry is used (s/g = 1).
11024 		 */
11025 		if (spx->txlt_dma_cookie_list == NULL) {
11026 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
11027 			spx->txlt_dma_cookie_list_len = 1;
11028 		}
11029 		/*
11030 		 * Since we are processing remaining cookies in a DMA window,
11031 		 * there may be less of them than the number of entries in the
11032 		 * current dma cookie list.
11033 		 */
11034 		req_len = MIN(spx->txlt_dma_cookie_list_len,
11035 		    (spx->txlt_curwin_num_dma_cookies -
11036 		    spx->txlt_curwin_processed_dma_cookies));
11037 
11038 		/* Fetch the next batch of cookies */
11039 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
11040 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
11041 			    &spx->txlt_dma_cookie_list[i]);
11042 			cur_txfer_len +=
11043 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
11044 			spx->txlt_sata_pkt->
11045 			    satapkt_cmd.satacmd_num_dma_cookies++;
11046 			spx->txlt_curwin_processed_dma_cookies++;
11047 		}
11048 	}
11049 
11050 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
11051 
11052 	/* Point sata_cmd to the cookie list */
11053 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
11054 	    &spx->txlt_dma_cookie_list[0];
11055 
11056 	/* Remember number of DMA cookies passed in sata packet */
11057 	spx->txlt_num_dma_cookies =
11058 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
11059 
11060 	ASSERT(cur_txfer_len != 0);
11061 	if (cur_txfer_len <= bp->b_bcount)
11062 		spx->txlt_total_residue -= cur_txfer_len;
11063 	else {
11064 		/*
11065 		 * Temporary DMA buffer has been padded by
11066 		 * ddi_dma_mem_alloc()!
11067 		 * This requires special handling, because DMA cookies are
11068 		 * based on the temporary buffer size, not the b_bcount,
11069 		 * and we have extra bytes to transfer - but the packet
11070 		 * residue has to stay correct because we will copy only
11071 		 * the requested number of bytes.
11072 		 */
11073 		spx->txlt_total_residue -= bp->b_bcount;
11074 	}
11075 
11076 	return (DDI_SUCCESS);
11077 }
11078 
11079 /*
11080  * Common routine for releasing DMA resources
11081  */
11082 static void
11083 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
11084 {
11085 	if (spx->txlt_buf_dma_handle != NULL) {
11086 		if (spx->txlt_tmp_buf != NULL)  {
11087 			/*
11088 			 * Intermediate DMA buffer was allocated.
11089 			 * Free allocated buffer and associated access handle.
11090 			 */
11091 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
11092 			spx->txlt_tmp_buf = NULL;
11093 		}
11094 		/*
11095 		 * Free DMA resources - cookies and handles
11096 		 */
11097 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
11098 		if (spx->txlt_dma_cookie_list != NULL) {
11099 			if (spx->txlt_dma_cookie_list !=
11100 			    &spx->txlt_dma_cookie) {
11101 				(void) kmem_free(spx->txlt_dma_cookie_list,
11102 				    spx->txlt_dma_cookie_list_len *
11103 				    sizeof (ddi_dma_cookie_t));
11104 				spx->txlt_dma_cookie_list = NULL;
11105 			}
11106 		}
11107 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
11108 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
11109 		spx->txlt_buf_dma_handle = NULL;
11110 	}
11111 }
11112 
11113 /*
11114  * Free DMA resources
11115  * Used by the HBA driver to release DMA resources that it does not use.
11116  *
11117  * Returns Void
11118  */
11119 void
11120 sata_free_dma_resources(sata_pkt_t *sata_pkt)
11121 {
11122 	sata_pkt_txlate_t *spx;
11123 
11124 	if (sata_pkt == NULL)
11125 		return;
11126 
11127 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
11128 
11129 	sata_common_free_dma_rsrcs(spx);
11130 }
11131 
11132 /*
11133  * Fetch Device Identify data.
11134  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
11135  * command to a device and get the device identify data.
11136  * The device_info structure has to be set to device type (for selecting proper
11137  * device identify command).
11138  *
11139  * Returns:
11140  * SATA_SUCCESS if cmd succeeded
11141  * SATA_RETRY if cmd was rejected and could be retried,
11142  * SATA_FAILURE if cmd failed and should not be retried (port error)
11143  *
11144  * Cannot be called in an interrupt context.
11145  */
11146 
11147 static int
11148 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
11149     sata_drive_info_t *sdinfo)
11150 {
11151 	struct buf *bp;
11152 	sata_pkt_t *spkt;
11153 	sata_cmd_t *scmd;
11154 	sata_pkt_txlate_t *spx;
11155 	int rval;
11156 
11157 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11158 	spx->txlt_sata_hba_inst = sata_hba_inst;
11159 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
11160 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11161 	if (spkt == NULL) {
11162 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11163 		return (SATA_RETRY); /* may retry later */
11164 	}
11165 	/* address is needed now */
11166 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11167 
11168 	/*
11169 	 * Allocate buffer for Identify Data return data
11170 	 */
11171 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
11172 	if (bp == NULL) {
11173 		sata_pkt_free(spx);
11174 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11175 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11176 		    "sata_fetch_device_identify_data: "
11177 		    "cannot allocate buffer for ID"));
11178 		return (SATA_RETRY); /* may retry later */
11179 	}
11180 
11181 	/* Fill sata_pkt */
11182 	sdinfo->satadrv_state = SATA_STATE_PROBING;
11183 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11184 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11185 	/* Synchronous mode, no callback */
11186 	spkt->satapkt_comp = NULL;
11187 	/* Timeout 30s */
11188 	spkt->satapkt_time = sata_default_pkt_time;
11189 
11190 	scmd = &spkt->satapkt_cmd;
11191 	scmd->satacmd_bp = bp;
11192 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
11193 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11194 
11195 	/* Build Identify Device cmd in the sata_pkt */
11196 	scmd->satacmd_addr_type = 0;		/* N/A */
11197 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
11198 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
11199 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
11200 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
11201 	scmd->satacmd_features_reg = 0;		/* N/A */
11202 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
11203 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11204 		/* Identify Packet Device cmd */
11205 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
11206 	} else {
11207 		/* Identify Device cmd - mandatory for all other devices */
11208 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
11209 	}
11210 
11211 	/* Send pkt to SATA HBA driver */
11212 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
11213 
11214 #ifdef SATA_INJECT_FAULTS
11215 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
11216 #endif
11217 
11218 	if (rval == SATA_TRAN_ACCEPTED &&
11219 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
11220 		if (spx->txlt_buf_dma_handle != NULL) {
11221 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
11222 			    DDI_DMA_SYNC_FORKERNEL);
11223 			ASSERT(rval == DDI_SUCCESS);
11224 		}
11225 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
11226 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
11227 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11228 			    "SATA disk device at port %d - "
11229 			    "partial Identify Data",
11230 			    sdinfo->satadrv_addr.cport));
11231 			rval = SATA_RETRY; /* may retry later */
11232 			goto fail;
11233 		}
11234 		/* Update sata_drive_info */
11235 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
11236 		    sizeof (sata_id_t));
11237 
11238 		sdinfo->satadrv_features_support = 0;
11239 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11240 			/*
11241 			 * Retrieve capacity (disks only) and addressing mode
11242 			 */
11243 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
11244 		} else {
11245 			/*
11246 			 * For ATAPI devices one would have to issue
11247 			 * Get Capacity cmd for media capacity. Not here.
11248 			 */
11249 			sdinfo->satadrv_capacity = 0;
11250 			/*
11251 			 * Check what cdb length is supported
11252 			 */
11253 			if ((sdinfo->satadrv_id.ai_config &
11254 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
11255 				sdinfo->satadrv_atapi_cdb_len = 16;
11256 			else
11257 				sdinfo->satadrv_atapi_cdb_len = 12;
11258 		}
11259 		/* Setup supported features flags */
11260 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
11261 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
11262 
11263 		/* Check for SATA GEN and NCQ support */
11264 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
11265 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
11266 			/* SATA compliance */
11267 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
11268 				sdinfo->satadrv_features_support |=
11269 				    SATA_DEV_F_NCQ;
11270 			if (sdinfo->satadrv_id.ai_satacap &
11271 			    (SATA_1_SPEED | SATA_2_SPEED)) {
11272 				if (sdinfo->satadrv_id.ai_satacap &
11273 				    SATA_2_SPEED)
11274 					sdinfo->satadrv_features_support |=
11275 					    SATA_DEV_F_SATA2;
11276 				if (sdinfo->satadrv_id.ai_satacap &
11277 				    SATA_1_SPEED)
11278 					sdinfo->satadrv_features_support |=
11279 					    SATA_DEV_F_SATA1;
11280 			} else {
11281 				sdinfo->satadrv_features_support |=
11282 				    SATA_DEV_F_SATA1;
11283 			}
11284 		}
11285 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
11286 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
11287 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
11288 
11289 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
11290 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
11291 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
11292 			++sdinfo->satadrv_queue_depth;
11293 			/* Adjust according to controller capabilities */
11294 			sdinfo->satadrv_max_queue_depth = MIN(
11295 			    sdinfo->satadrv_queue_depth,
11296 			    SATA_QDEPTH(sata_hba_inst));
11297 			/* Adjust according to global queue depth limit */
11298 			sdinfo->satadrv_max_queue_depth = MIN(
11299 			    sdinfo->satadrv_max_queue_depth,
11300 			    sata_current_max_qdepth);
11301 			if (sdinfo->satadrv_max_queue_depth == 0)
11302 				sdinfo->satadrv_max_queue_depth = 1;
11303 		} else
11304 			sdinfo->satadrv_max_queue_depth = 1;
11305 
11306 		rval = SATA_SUCCESS;
11307 	} else {
11308 		/*
11309 		 * Woops, no Identify Data.
11310 		 */
11311 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
11312 			rval = SATA_RETRY; /* may retry later */
11313 		} else if (rval == SATA_TRAN_ACCEPTED) {
11314 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
11315 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
11316 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
11317 			    spkt->satapkt_reason == SATA_PKT_RESET)
11318 				rval = SATA_RETRY; /* may retry later */
11319 			else
11320 				rval = SATA_FAILURE;
11321 		} else {
11322 			rval = SATA_FAILURE;
11323 		}
11324 	}
11325 fail:
11326 	/* Free allocated resources */
11327 	sata_free_local_buffer(spx);
11328 	sata_pkt_free(spx);
11329 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
11330 
11331 	return (rval);
11332 }
11333 
11334 
11335 /*
11336  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
11337  * UDMA mode is checked first, followed by MWDMA mode.
11338  * set correctly, so this function is setting it to the highest supported level.
11339  * Older SATA spec required that the device supports at least DMA 4 mode and
11340  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
11341  * restriction has been removed.
11342  *
11343  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
11344  * Returns SATA_FAILURE if proper DMA mode could not be selected.
11345  *
11346  * NOTE: This function should be called only if DMA mode is supported.
11347  */
11348 static int
11349 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
11350 {
11351 	sata_pkt_t *spkt;
11352 	sata_cmd_t *scmd;
11353 	sata_pkt_txlate_t *spx;
11354 	int mode;
11355 	uint8_t subcmd;
11356 	int rval = SATA_SUCCESS;
11357 
11358 	ASSERT(sdinfo != NULL);
11359 	ASSERT(sata_hba_inst != NULL);
11360 
11361 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11362 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
11363 		/* Find highest Ultra DMA mode supported */
11364 		for (mode = 6; mode >= 0; --mode) {
11365 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
11366 				break;
11367 		}
11368 #if 0
11369 		/* Left for historical reasons */
11370 		/*
11371 		 * Some initial version of SATA spec indicated that at least
11372 		 * UDMA mode 4 has to be supported. It is not mentioned in
11373 		 * SerialATA 2.6, so this restriction is removed.
11374 		 */
11375 		if (mode < 4)
11376 			return (SATA_FAILURE);
11377 #endif
11378 
11379 		/*
11380 		 * We're still going to set DMA mode whatever is selected
11381 		 * by default
11382 		 *
11383 		 * We saw an old maxtor sata drive will select Ultra DMA and
11384 		 * Multi-Word DMA simultaneouly by default, which is going
11385 		 * to cause DMA command timed out, so we need to select DMA
11386 		 * mode even when it's already done by default
11387 		 */
11388 
11389 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
11390 
11391 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
11392 		/* Find highest MultiWord DMA mode supported */
11393 		for (mode = 2; mode >= 0; --mode) {
11394 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
11395 				break;
11396 		}
11397 
11398 		/*
11399 		 * We're still going to set DMA mode whatever is selected
11400 		 * by default
11401 		 *
11402 		 * We saw an old maxtor sata drive will select Ultra DMA and
11403 		 * Multi-Word DMA simultaneouly by default, which is going
11404 		 * to cause DMA command timed out, so we need to select DMA
11405 		 * mode even when it's already done by default
11406 		 */
11407 
11408 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
11409 	} else
11410 		return (SATA_SUCCESS);
11411 
11412 	/*
11413 	 * Set DMA mode via SET FEATURES COMMAND.
11414 	 * Prepare packet for SET FEATURES COMMAND.
11415 	 */
11416 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11417 	spx->txlt_sata_hba_inst = sata_hba_inst;
11418 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11419 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11420 	if (spkt == NULL) {
11421 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11422 		    "sata_set_dma_mode: could not set DMA mode %", mode));
11423 		rval = SATA_FAILURE;
11424 		goto done;
11425 	}
11426 	/* Fill sata_pkt */
11427 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11428 	/* Timeout 30s */
11429 	spkt->satapkt_time = sata_default_pkt_time;
11430 	/* Synchronous mode, no callback, interrupts */
11431 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11432 	spkt->satapkt_comp = NULL;
11433 	scmd = &spkt->satapkt_cmd;
11434 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11435 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11436 	scmd->satacmd_addr_type = 0;
11437 	scmd->satacmd_device_reg = 0;
11438 	scmd->satacmd_status_reg = 0;
11439 	scmd->satacmd_error_reg = 0;
11440 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11441 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
11442 	scmd->satacmd_sec_count_lsb = subcmd | mode;
11443 
11444 	/* Transfer command to HBA */
11445 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11446 	    spkt) != SATA_TRAN_ACCEPTED ||
11447 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
11448 		/* Pkt execution failed */
11449 		rval = SATA_FAILURE;
11450 	}
11451 done:
11452 
11453 	/* Free allocated resources */
11454 	if (spkt != NULL)
11455 		sata_pkt_free(spx);
11456 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11457 
11458 	return (rval);
11459 }
11460 
11461 
11462 /*
11463  * Set device caching mode.
11464  * One of the following operations should be specified:
11465  * SATAC_SF_ENABLE_READ_AHEAD
11466  * SATAC_SF_DISABLE_READ_AHEAD
11467  * SATAC_SF_ENABLE_WRITE_CACHE
11468  * SATAC_SF_DISABLE_WRITE_CACHE
11469  *
11470  * If operation fails, system log messgage is emitted.
11471  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
11472  * command was sent but did not succeed, and SATA_FAILURE otherwise.
11473  */
11474 
11475 static int
11476 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11477     int cache_op)
11478 {
11479 	sata_pkt_t *spkt;
11480 	sata_cmd_t *scmd;
11481 	sata_pkt_txlate_t *spx;
11482 	int rval = SATA_SUCCESS;
11483 	int hba_rval;
11484 	char *infop;
11485 
11486 	ASSERT(sdinfo != NULL);
11487 	ASSERT(sata_hba_inst != NULL);
11488 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
11489 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
11490 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
11491 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
11492 
11493 
11494 	/* Prepare packet for SET FEATURES COMMAND */
11495 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11496 	spx->txlt_sata_hba_inst = sata_hba_inst;
11497 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11498 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11499 	if (spkt == NULL) {
11500 		rval = SATA_FAILURE;
11501 		goto failure;
11502 	}
11503 	/* Fill sata_pkt */
11504 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11505 	/* Timeout 30s */
11506 	spkt->satapkt_time = sata_default_pkt_time;
11507 	/* Synchronous mode, no callback, interrupts */
11508 	spkt->satapkt_op_mode =
11509 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11510 	spkt->satapkt_comp = NULL;
11511 	scmd = &spkt->satapkt_cmd;
11512 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11513 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11514 	scmd->satacmd_addr_type = 0;
11515 	scmd->satacmd_device_reg = 0;
11516 	scmd->satacmd_status_reg = 0;
11517 	scmd->satacmd_error_reg = 0;
11518 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11519 	scmd->satacmd_features_reg = cache_op;
11520 
11521 	/* Transfer command to HBA */
11522 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
11523 	    SATA_DIP(sata_hba_inst), spkt);
11524 
11525 #ifdef SATA_INJECT_FAULTS
11526 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
11527 #endif
11528 
11529 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
11530 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11531 		/* Pkt execution failed */
11532 		switch (cache_op) {
11533 		case SATAC_SF_ENABLE_READ_AHEAD:
11534 			infop = "enabling read ahead failed";
11535 			break;
11536 		case SATAC_SF_DISABLE_READ_AHEAD:
11537 			infop = "disabling read ahead failed";
11538 			break;
11539 		case SATAC_SF_ENABLE_WRITE_CACHE:
11540 			infop = "enabling write cache failed";
11541 			break;
11542 		case SATAC_SF_DISABLE_WRITE_CACHE:
11543 			infop = "disabling write cache failed";
11544 			break;
11545 		}
11546 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11547 		rval = SATA_RETRY;
11548 	}
11549 failure:
11550 	/* Free allocated resources */
11551 	if (spkt != NULL)
11552 		sata_pkt_free(spx);
11553 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11554 	return (rval);
11555 }
11556 
11557 /*
11558  * Set Removable Media Status Notification (enable/disable)
11559  * state == 0 , disable
11560  * state != 0 , enable
11561  *
11562  * If operation fails, system log messgage is emitted.
11563  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11564  */
11565 
11566 static int
11567 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11568     int state)
11569 {
11570 	sata_pkt_t *spkt;
11571 	sata_cmd_t *scmd;
11572 	sata_pkt_txlate_t *spx;
11573 	int rval = SATA_SUCCESS;
11574 	char *infop;
11575 
11576 	ASSERT(sdinfo != NULL);
11577 	ASSERT(sata_hba_inst != NULL);
11578 
11579 	/* Prepare packet for SET FEATURES COMMAND */
11580 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11581 	spx->txlt_sata_hba_inst = sata_hba_inst;
11582 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11583 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11584 	if (spkt == NULL) {
11585 		rval = SATA_FAILURE;
11586 		goto failure;
11587 	}
11588 	/* Fill sata_pkt */
11589 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11590 	/* Timeout 30s */
11591 	spkt->satapkt_time = sata_default_pkt_time;
11592 	/* Synchronous mode, no callback, interrupts */
11593 	spkt->satapkt_op_mode =
11594 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11595 	spkt->satapkt_comp = NULL;
11596 	scmd = &spkt->satapkt_cmd;
11597 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11598 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11599 	scmd->satacmd_addr_type = 0;
11600 	scmd->satacmd_device_reg = 0;
11601 	scmd->satacmd_status_reg = 0;
11602 	scmd->satacmd_error_reg = 0;
11603 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11604 	if (state == 0)
11605 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
11606 	else
11607 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
11608 
11609 	/* Transfer command to HBA */
11610 	if (((*SATA_START_FUNC(sata_hba_inst))(
11611 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11612 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11613 		/* Pkt execution failed */
11614 		if (state == 0)
11615 			infop = "disabling Removable Media Status "
11616 			    "Notification failed";
11617 		else
11618 			infop = "enabling Removable Media Status "
11619 			    "Notification failed";
11620 
11621 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11622 		rval = SATA_FAILURE;
11623 	}
11624 failure:
11625 	/* Free allocated resources */
11626 	if (spkt != NULL)
11627 		sata_pkt_free(spx);
11628 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11629 	return (rval);
11630 }
11631 
11632 
11633 /*
11634  * Update port SCR block
11635  */
11636 static void
11637 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
11638 {
11639 	port_scr->sstatus = device->satadev_scr.sstatus;
11640 	port_scr->serror = device->satadev_scr.serror;
11641 	port_scr->scontrol = device->satadev_scr.scontrol;
11642 	port_scr->sactive = device->satadev_scr.sactive;
11643 	port_scr->snotific = device->satadev_scr.snotific;
11644 }
11645 
11646 /*
11647  * Update state and copy port ss* values from passed sata_device structure.
11648  * sata_address is validated - if not valid, nothing is changed in sata_scsi
11649  * configuration struct.
11650  *
11651  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
11652  * regardless of the state in device argument.
11653  *
11654  * Port mutex should be held while calling this function.
11655  */
11656 static void
11657 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
11658 	sata_device_t *sata_device)
11659 {
11660 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
11661 	    sata_device->satadev_addr.cport)));
11662 
11663 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
11664 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
11665 
11666 		sata_cport_info_t *cportinfo;
11667 
11668 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
11669 		    sata_device->satadev_addr.cport)
11670 			return;
11671 
11672 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11673 		    sata_device->satadev_addr.cport);
11674 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
11675 
11676 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11677 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
11678 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
11679 		cportinfo->cport_state |=
11680 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11681 	} else {
11682 		sata_pmport_info_t *pmportinfo;
11683 
11684 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
11685 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
11686 		    SATA_NUM_PMPORTS(sata_hba_inst,
11687 		    sata_device->satadev_addr.cport) <
11688 		    sata_device->satadev_addr.pmport)
11689 			return;
11690 
11691 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11692 		    sata_device->satadev_addr.cport,
11693 		    sata_device->satadev_addr.pmport);
11694 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
11695 
11696 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11697 		pmportinfo->pmport_state &=
11698 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
11699 		    SATA_PSTATE_FAILED);
11700 		pmportinfo->pmport_state |=
11701 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11702 	}
11703 }
11704 
11705 
11706 
11707 /*
11708  * Extract SATA port specification from an IOCTL argument.
11709  *
11710  * This function return the port the user land send us as is, unless it
11711  * cannot retrieve port spec, then -1 is returned.
11712  *
11713  * Note: Only cport  - no port multiplier port.
11714  */
11715 static int32_t
11716 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
11717 {
11718 	int32_t port;
11719 
11720 	/* Extract port number from nvpair in dca structure  */
11721 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
11722 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
11723 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
11724 		    port));
11725 		port = -1;
11726 	}
11727 
11728 	return (port);
11729 }
11730 
11731 /*
11732  * Get dev_info_t pointer to the device node pointed to by port argument.
11733  * NOTE: target argument is a value used in ioctls to identify
11734  * the AP - it is not a sata_address.
11735  * It is a combination of cport, pmport and address qualifier, encodded same
11736  * way as a scsi target number.
11737  * At this moment it carries only cport number.
11738  *
11739  * No PMult hotplug support.
11740  *
11741  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11742  */
11743 
11744 static dev_info_t *
11745 sata_get_target_dip(dev_info_t *dip, int32_t port)
11746 {
11747 	dev_info_t	*cdip = NULL;
11748 	int		target, tgt;
11749 	int		ncport;
11750 	int 		circ;
11751 
11752 	ncport = port & SATA_CFGA_CPORT_MASK;
11753 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
11754 
11755 	ndi_devi_enter(dip, &circ);
11756 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11757 		dev_info_t *next = ddi_get_next_sibling(cdip);
11758 
11759 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11760 		    DDI_PROP_DONTPASS, "target", -1);
11761 		if (tgt == -1) {
11762 			/*
11763 			 * This is actually an error condition, but not
11764 			 * a fatal one. Just continue the search.
11765 			 */
11766 			cdip = next;
11767 			continue;
11768 		}
11769 
11770 		if (tgt == target)
11771 			break;
11772 
11773 		cdip = next;
11774 	}
11775 	ndi_devi_exit(dip, circ);
11776 
11777 	return (cdip);
11778 }
11779 
11780 /*
11781  * Get dev_info_t pointer to the device node pointed to by port argument.
11782  * NOTE: target argument is a value used in ioctls to identify
11783  * the AP - it is not a sata_address.
11784  * It is a combination of cport, pmport and address qualifier, encoded same
11785  * way as a scsi target number.
11786  * At this moment it carries only cport number.
11787  *
11788  * No PMult hotplug support.
11789  *
11790  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11791  */
11792 
11793 static dev_info_t *
11794 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
11795 {
11796 	dev_info_t	*cdip = NULL;
11797 	int		target, tgt;
11798 	int 		circ;
11799 
11800 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
11801 
11802 	ndi_devi_enter(dip, &circ);
11803 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11804 		dev_info_t *next = ddi_get_next_sibling(cdip);
11805 
11806 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11807 		    DDI_PROP_DONTPASS, "target", -1);
11808 		if (tgt == -1) {
11809 			/*
11810 			 * This is actually an error condition, but not
11811 			 * a fatal one. Just continue the search.
11812 			 */
11813 			cdip = next;
11814 			continue;
11815 		}
11816 
11817 		if (tgt == target)
11818 			break;
11819 
11820 		cdip = next;
11821 	}
11822 	ndi_devi_exit(dip, circ);
11823 
11824 	return (cdip);
11825 }
11826 
11827 /*
11828  * Process sata port disconnect request.
11829  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
11830  * before this request. Nevertheless, if a device is still configured,
11831  * we need to attempt to offline and unconfigure device.
11832  * Regardless of the unconfigure operation results the port is marked as
11833  * deactivated and no access to the attached device is possible.
11834  * If the target node remains because unconfigure operation failed, its state
11835  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
11836  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
11837  * the device and remove old target node.
11838  *
11839  * This function invokes sata_hba_inst->satahba_tran->
11840  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11841  * If successful, the device structure (if any) attached to the specified port
11842  * is removed and state of the port marked appropriately.
11843  * Failure of the port_deactivate may keep port in the physically active state,
11844  * or may fail the port.
11845  *
11846  * NOTE: Port multiplier code is not completed nor tested.
11847  */
11848 
11849 static int
11850 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
11851     sata_device_t *sata_device)
11852 {
11853 	sata_drive_info_t *sdinfo = NULL;
11854 	sata_cport_info_t *cportinfo = NULL;
11855 	sata_pmport_info_t *pmportinfo = NULL;
11856 	sata_pmult_info_t *pmultinfo = NULL;
11857 	dev_info_t *tdip;
11858 	int cport, pmport, qual;
11859 	int rval = SATA_SUCCESS;
11860 	int rv = 0;
11861 
11862 	cport = sata_device->satadev_addr.cport;
11863 	pmport = sata_device->satadev_addr.pmport;
11864 	qual = sata_device->satadev_addr.qual;
11865 
11866 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11867 
11868 	/*
11869 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
11870 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11871 	 * Do the sanity check.
11872 	 */
11873 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
11874 		/* No physical port deactivation supported. */
11875 		return (EINVAL);
11876 	}
11877 
11878 	/* Check the current state of the port */
11879 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11880 	    (SATA_DIP(sata_hba_inst), sata_device);
11881 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11882 	sata_update_port_info(sata_hba_inst, sata_device);
11883 	if (rval != SATA_SUCCESS ||
11884 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11885 		/* Device port status is unknown or it is in failed state */
11886 		if (qual == SATA_ADDR_PMPORT) {
11887 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11888 			    SATA_PSTATE_FAILED;
11889 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11890 			    "sata_hba_ioctl: connect: failed to deactivate "
11891 			    "SATA port %d", cport);
11892 		} else {
11893 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11894 			    SATA_PSTATE_FAILED;
11895 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11896 			    "sata_hba_ioctl: connect: failed to deactivate "
11897 			    "SATA port %d:%d", cport, pmport);
11898 		}
11899 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11900 		    cport)->cport_mutex);
11901 		return (EIO);
11902 	}
11903 	/*
11904 	 * Set port's dev_state to not ready - this will disable
11905 	 * an access to a potentially attached device.
11906 	 */
11907 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11908 	if (qual == SATA_ADDR_PMPORT) {
11909 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11910 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11911 			sdinfo = pmportinfo->pmport_sata_drive;
11912 			ASSERT(sdinfo != NULL);
11913 		}
11914 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11915 	} else {
11916 		/* Assuming cport */
11917 
11918 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11919 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
11920 				pmultinfo =
11921 				    cportinfo->cport_devp.cport_sata_pmult;
11922 				ASSERT(pmultinfo != NULL);
11923 			} else {
11924 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
11925 			}
11926 		}
11927 		cportinfo->cport_state &= ~SATA_STATE_READY;
11928 	}
11929 	if (sdinfo != NULL) {
11930 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
11931 			/*
11932 			 * If a target node exists, try to offline
11933 			 * a device and remove target node.
11934 			 */
11935 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11936 			    cport)->cport_mutex);
11937 			/* We are addressing attached device, not a port */
11938 			sata_device->satadev_addr.qual =
11939 			    sdinfo->satadrv_addr.qual;
11940 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11941 			    &sata_device->satadev_addr);
11942 			if (tdip != NULL && ndi_devi_offline(tdip,
11943 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11944 				/*
11945 				 * Problem
11946 				 * The target node remained attached.
11947 				 * This happens when the device file was open
11948 				 * or a node was waiting for resources.
11949 				 * Cannot do anything about it.
11950 				 */
11951 				if (qual == SATA_ADDR_CPORT) {
11952 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11953 					    "sata_hba_ioctl: disconnect: could "
11954 					    "not unconfigure device before "
11955 					    "disconnecting the SATA port %d",
11956 					    cport));
11957 				} else {
11958 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11959 					    "sata_hba_ioctl: disconnect: could "
11960 					    "not unconfigure device before "
11961 					    "disconnecting the SATA port %d:%d",
11962 					    cport, pmport));
11963 				}
11964 				/*
11965 				 * Set DEVICE REMOVED state in the target
11966 				 * node. It will prevent access to the device
11967 				 * even when a new device is attached, until
11968 				 * the old target node is released, removed and
11969 				 * recreated for a new  device.
11970 				 */
11971 				sata_set_device_removed(tdip);
11972 
11973 				/*
11974 				 * Instruct event daemon to try the target
11975 				 * node cleanup later.
11976 				 */
11977 				sata_set_target_node_cleanup(
11978 				    sata_hba_inst, &sata_device->satadev_addr);
11979 			}
11980 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11981 			    cport)->cport_mutex);
11982 		}
11983 
11984 		/* Remove and release sata_drive info structure. */
11985 		if (pmportinfo != NULL) {
11986 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
11987 			    NULL;
11988 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11989 		} else {
11990 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11991 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11992 		}
11993 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11994 	}
11995 #if 0
11996 	else if (pmultinfo != NULL) {
11997 		/*
11998 		 * Port Multiplier itself needs special handling.
11999 		 * All device ports need to be processed here!
12000 		 */
12001 	}
12002 #endif
12003 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12004 	/* Just ask HBA driver to deactivate port */
12005 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
12006 
12007 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
12008 	    (SATA_DIP(sata_hba_inst), sata_device);
12009 
12010 	/*
12011 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12012 	 * without the hint (to force listener to investivate the state).
12013 	 */
12014 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12015 	    SE_NO_HINT);
12016 
12017 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12018 	sata_update_port_info(sata_hba_inst, sata_device);
12019 
12020 	if (rval != SATA_SUCCESS) {
12021 		/*
12022 		 * Port deactivation failure - do not
12023 		 * change port state unless the state
12024 		 * returned by HBA indicates a port failure.
12025 		 * NOTE: device structures were released, so devices now are
12026 		 * invisible! Port reset is needed to re-enumerate devices.
12027 		 */
12028 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12029 			if (pmportinfo != NULL)
12030 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
12031 			else
12032 				cportinfo->cport_state = SATA_PSTATE_FAILED;
12033 			rv = EIO;
12034 		}
12035 	} else {
12036 		/*
12037 		 * Deactivation succeded. From now on the sata framework
12038 		 * will not care what is happening to the device, until
12039 		 * the port is activated again.
12040 		 */
12041 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
12042 	}
12043 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12044 	return (rv);
12045 }
12046 
12047 
12048 
12049 /*
12050  * Process sata port connect request
12051  * The sata cfgadm pluging will invoke this operation only if port was found
12052  * in the disconnect state (failed state is also treated as the disconnected
12053  * state).
12054  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
12055  * sata_tran_hotplug_ops->sata_tran_port_activate().
12056  * If successful and a device is found attached to the port,
12057  * the initialization sequence is executed to attach a device structure to
12058  * a port structure. The state of the port and a device would be set
12059  * appropriately.
12060  * The device is not set in configured state (system-wise) by this operation.
12061  *
12062  * Note, that activating the port may generate link events,
12063  * so it is important that following processing and the
12064  * event processing does not interfere with each other!
12065  *
12066  * This operation may remove port failed state and will
12067  * try to make port active and in good standing.
12068  *
12069  * NOTE: Port multiplier code is not completed nor tested.
12070  */
12071 
12072 static int
12073 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
12074     sata_device_t *sata_device)
12075 {
12076 	int cport, pmport, qual;
12077 	int rv = 0;
12078 
12079 	cport = sata_device->satadev_addr.cport;
12080 	pmport = sata_device->satadev_addr.pmport;
12081 	qual = sata_device->satadev_addr.qual;
12082 
12083 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
12084 
12085 	/*
12086 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
12087 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
12088 	 * Perform sanity check now.
12089 	 */
12090 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
12091 		/* No physical port activation supported. */
12092 		return (EINVAL);
12093 	}
12094 
12095 	/* Just ask HBA driver to activate port */
12096 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12097 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12098 		/*
12099 		 * Port activation failure.
12100 		 */
12101 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12102 		    cport)->cport_mutex);
12103 		sata_update_port_info(sata_hba_inst, sata_device);
12104 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12105 			if (qual == SATA_ADDR_DCPORT) {
12106 				SATA_CPORT_STATE(sata_hba_inst, cport) =
12107 				    SATA_PSTATE_FAILED;
12108 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
12109 				    "sata_hba_ioctl: connect: failed to "
12110 				    "activate SATA port %d", cport);
12111 			} else { /* port multiplier device port */
12112 				SATA_PMPORT_STATE(sata_hba_inst, cport,
12113 				    pmport) = SATA_PSTATE_FAILED;
12114 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
12115 				    "sata_hba_ioctl: connect: failed to "
12116 				    "activate SATA port %d:%d", cport, pmport);
12117 
12118 			}
12119 		}
12120 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12121 		    cport)->cport_mutex);
12122 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
12123 		    "sata_hba_ioctl: connect: failed to activate SATA "
12124 		    "port %d:%d", cport, pmport);
12125 		return (EIO);
12126 	}
12127 
12128 	/* Virgin port state - will be updated by the port re-probe. */
12129 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12130 	if (qual == SATA_ADDR_CPORT)
12131 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
12132 	else /* port multiplier device port */
12133 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
12134 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12135 
12136 	/*
12137 	 * Probe the port to find its state and attached device.
12138 	 */
12139 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12140 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
12141 		rv = EIO;
12142 
12143 	/*
12144 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12145 	 * without the hint
12146 	 */
12147 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12148 	    SE_NO_HINT);
12149 
12150 	/*
12151 	 * If there is a device attached to the port, emit
12152 	 * a message.
12153 	 */
12154 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
12155 
12156 		if (qual == SATA_ADDR_CPORT) {
12157 			sata_log(sata_hba_inst, CE_WARN,
12158 			    "SATA device detected at port %d", cport);
12159 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12160 				/*
12161 				 * A device was not successfully identified
12162 				 */
12163 				sata_log(sata_hba_inst, CE_WARN,
12164 				    "Could not identify SATA "
12165 				    "device at port %d", cport);
12166 			}
12167 		} else { /* port multiplier device port */
12168 			sata_log(sata_hba_inst, CE_WARN,
12169 			    "SATA device detected at port %d:%d",
12170 			    cport, pmport);
12171 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12172 				/*
12173 				 * A device was not successfully identified
12174 				 */
12175 				sata_log(sata_hba_inst, CE_WARN,
12176 				    "Could not identify SATA "
12177 				    "device at port %d:%d", cport, pmport);
12178 			}
12179 		}
12180 	}
12181 
12182 	return (rv);
12183 }
12184 
12185 
12186 /*
12187  * Process sata device unconfigure request.
12188  * The unconfigure operation uses generic nexus operation to
12189  * offline a device. It leaves a target device node attached.
12190  * and obviously sata_drive_info attached as well, because
12191  * from the hardware point of view nothing has changed.
12192  */
12193 static int
12194 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
12195     sata_device_t *sata_device)
12196 {
12197 	int rv = 0;
12198 	dev_info_t *tdip;
12199 
12200 	/* We are addressing attached device, not a port */
12201 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12202 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12203 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
12204 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12205 
12206 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12207 	    &sata_device->satadev_addr)) != NULL) {
12208 
12209 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
12210 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12211 			    "sata_hba_ioctl: unconfigure: "
12212 			    "failed to unconfigure device at SATA port %d:%d",
12213 			    sata_device->satadev_addr.cport,
12214 			    sata_device->satadev_addr.pmport));
12215 			rv = EIO;
12216 		}
12217 		/*
12218 		 * The target node devi_state should be marked with
12219 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
12220 		 * This would be the indication for cfgadm that
12221 		 * the AP node occupant state is 'unconfigured'.
12222 		 */
12223 
12224 	} else {
12225 		/*
12226 		 * This would indicate a failure on the part of cfgadm
12227 		 * to detect correct state of the node prior to this
12228 		 * call - one cannot unconfigure non-existing device.
12229 		 */
12230 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12231 		    "sata_hba_ioctl: unconfigure: "
12232 		    "attempt to unconfigure non-existing device "
12233 		    "at SATA port %d:%d",
12234 		    sata_device->satadev_addr.cport,
12235 		    sata_device->satadev_addr.pmport));
12236 		rv = ENXIO;
12237 	}
12238 	return (rv);
12239 }
12240 
12241 /*
12242  * Process sata device configure request
12243  * If port is in a failed state, operation is aborted - one has to use
12244  * an explicit connect or port activate request to try to get a port into
12245  * non-failed mode. Port reset wil also work in such situation.
12246  * If the port is in disconnected (shutdown) state, the connect operation is
12247  * attempted prior to any other action.
12248  * When port is in the active state, there is a device attached and the target
12249  * node exists, a device was most likely offlined.
12250  * If target node does not exist, a new target node is created. In both cases
12251  * an attempt is made to online (configure) the device.
12252  *
12253  * NOTE: Port multiplier code is not completed nor tested.
12254  */
12255 static int
12256 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
12257     sata_device_t *sata_device)
12258 {
12259 	int cport, pmport, qual;
12260 	int rval;
12261 	boolean_t target = TRUE;
12262 	sata_cport_info_t *cportinfo;
12263 	sata_pmport_info_t *pmportinfo = NULL;
12264 	dev_info_t *tdip;
12265 	sata_drive_info_t *sdinfo;
12266 
12267 	cport = sata_device->satadev_addr.cport;
12268 	pmport = sata_device->satadev_addr.pmport;
12269 	qual = sata_device->satadev_addr.qual;
12270 
12271 	/* Get current port state */
12272 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12273 	    (SATA_DIP(sata_hba_inst), sata_device);
12274 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12275 	sata_update_port_info(sata_hba_inst, sata_device);
12276 
12277 	if (rval != SATA_SUCCESS ||
12278 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
12279 		/*
12280 		 * Obviously, device on a failed port is not visible
12281 		 */
12282 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12283 		return (ENXIO);
12284 	}
12285 
12286 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12287 	if (qual == SATA_ADDR_PMPORT)
12288 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12289 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12290 
12291 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
12292 		/* need to activate port */
12293 		target = FALSE;
12294 
12295 		/* Sanity check */
12296 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
12297 			return (ENXIO);
12298 
12299 		/* Just let HBA driver to activate port */
12300 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12301 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12302 			/*
12303 			 * Port activation failure - do not change port state
12304 			 * unless the state returned by HBA indicates a port
12305 			 * failure.
12306 			 */
12307 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12308 			    cport)->cport_mutex);
12309 			sata_update_port_info(sata_hba_inst, sata_device);
12310 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12311 				if (qual == SATA_ADDR_PMPORT)
12312 					pmportinfo->pmport_state =
12313 					    SATA_PSTATE_FAILED;
12314 				else
12315 					cportinfo->cport_state =
12316 					    SATA_PSTATE_FAILED;
12317 			}
12318 			mutex_exit(&SATA_CPORT_INFO(
12319 			    sata_hba_inst, cport)->cport_mutex);
12320 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12321 			    "sata_hba_ioctl: configure: "
12322 			    "failed to activate SATA port %d:%d",
12323 			    cport, pmport));
12324 			return (EIO);
12325 		}
12326 		/*
12327 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12328 		 * without the hint.
12329 		 */
12330 		sata_gen_sysevent(sata_hba_inst,
12331 		    &sata_device->satadev_addr, SE_NO_HINT);
12332 
12333 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12334 		    cport_mutex);
12335 		/* Virgin port state */
12336 		if (qual == SATA_ADDR_PMPORT)
12337 			pmportinfo->pmport_state = 0;
12338 		else
12339 			cportinfo->cport_state = 0;
12340 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12341 	}
12342 	/*
12343 	 * Always reprobe port, to get current device info.
12344 	 */
12345 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12346 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12347 		return (EIO);
12348 
12349 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
12350 		if (qual == SATA_ADDR_PMPORT) {
12351 			/*
12352 			 * That's the transition from "inactive" port
12353 			 * to active one with device attached.
12354 			 */
12355 			sata_log(sata_hba_inst, CE_WARN,
12356 			    "SATA device detected at port %d:%d",
12357 			    cport, pmport);
12358 		} else {
12359 			/*
12360 			 * When PM is attached to the cport and cport is
12361 			 * activated, every PM device port needs to be reprobed.
12362 			 * We need to emit message for all devices detected
12363 			 * at port multiplier's device ports.
12364 			 * Add such code here.
12365 			 * For now, just inform about device attached to
12366 			 * cport.
12367 			 */
12368 			sata_log(sata_hba_inst, CE_WARN,
12369 			    "SATA device detected at port %d", cport);
12370 		}
12371 	}
12372 
12373 	/*
12374 	 * This is where real configuration operation starts.
12375 	 *
12376 	 * When PM is attached to the cport and cport is activated,
12377 	 * devices attached PM device ports may have to be configured
12378 	 * explicitly. This may change when port multiplier is supported.
12379 	 * For now, configure only disks and other valid target devices.
12380 	 */
12381 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
12382 		if (qual == SATA_ADDR_CPORT) {
12383 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12384 				/*
12385 				 * A device was not successfully identified
12386 				 */
12387 				sata_log(sata_hba_inst, CE_WARN,
12388 				    "Could not identify SATA "
12389 				    "device at port %d", cport);
12390 			}
12391 		} else { /* port multiplier device port */
12392 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12393 				/*
12394 				 * A device was not successfully identified
12395 				 */
12396 				sata_log(sata_hba_inst, CE_WARN,
12397 				    "Could not identify SATA "
12398 				    "device at port %d:%d", cport, pmport);
12399 			}
12400 		}
12401 		return (ENXIO);		/* No device to configure */
12402 	}
12403 
12404 	/*
12405 	 * Here we may have a device in reset condition,
12406 	 * but because we are just configuring it, there is
12407 	 * no need to process the reset other than just
12408 	 * to clear device reset condition in the HBA driver.
12409 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
12410 	 * cause a first command sent the HBA driver with the request
12411 	 * to clear device reset condition.
12412 	 */
12413 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12414 	if (qual == SATA_ADDR_PMPORT)
12415 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12416 	else
12417 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12418 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12419 	if (sdinfo == NULL) {
12420 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12421 		return (ENXIO);
12422 	}
12423 	if (sdinfo->satadrv_event_flags &
12424 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
12425 		sdinfo->satadrv_event_flags = 0;
12426 	}
12427 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
12428 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12429 
12430 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12431 	    &sata_device->satadev_addr)) != NULL) {
12432 		/*
12433 		 * Target node exists. Verify, that it belongs
12434 		 * to existing, attached device and not to
12435 		 * a removed device.
12436 		 */
12437 		if (sata_check_device_removed(tdip) == B_TRUE) {
12438 			if (qual == SATA_ADDR_DPMPORT)
12439 				sata_log(sata_hba_inst, CE_WARN,
12440 				    "SATA device at port %d cannot be "
12441 				    "configured. "
12442 				    "Application(s) accessing "
12443 				    "previously attached device "
12444 				    "have to release it before newly "
12445 				    "inserted device can be made accessible.",
12446 				    cport);
12447 			else
12448 				sata_log(sata_hba_inst, CE_WARN,
12449 				    "SATA device at port %d:%d cannot be"
12450 				    "configured. "
12451 				    "Application(s) accessing "
12452 				    "previously attached device "
12453 				    "have to release it before newly "
12454 				    "inserted device can be made accessible.",
12455 				    cport, pmport);
12456 			return (EIO);
12457 		}
12458 		/*
12459 		 * Device was not removed and re-inserted.
12460 		 * Try to online it.
12461 		 */
12462 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
12463 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12464 			    "sata_hba_ioctl: configure: "
12465 			    "onlining device at SATA port "
12466 			    "%d:%d failed", cport, pmport));
12467 			return (EIO);
12468 		}
12469 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12470 		    cport)->cport_mutex);
12471 
12472 		if (qual == SATA_ADDR_DPMPORT)
12473 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
12474 		else
12475 			cportinfo-> cport_tgtnode_clean = B_TRUE;
12476 
12477 		mutex_exit(&SATA_CPORT_INFO(
12478 		    sata_hba_inst, cport)->cport_mutex);
12479 	} else {
12480 		/*
12481 		 * No target node - need to create a new target node.
12482 		 */
12483 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12484 		    cport_mutex);
12485 		if (qual == SATA_ADDR_DPMPORT)
12486 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
12487 		else
12488 			cportinfo-> cport_tgtnode_clean = B_TRUE;
12489 
12490 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12491 		    cport_mutex);
12492 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
12493 		    sata_hba_inst, &sata_device->satadev_addr);
12494 		if (tdip == NULL) {
12495 			/* Configure operation failed */
12496 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12497 			    "sata_hba_ioctl: configure: "
12498 			    "configuring SATA device at port %d:%d "
12499 			    "failed", cport, pmport));
12500 			return (EIO);
12501 		}
12502 	}
12503 	return (0);
12504 }
12505 
12506 
12507 /*
12508  * Process ioctl deactivate port request.
12509  * Arbitrarily unconfigure attached device, if any.
12510  * Even if the unconfigure fails, proceed with the
12511  * port deactivation.
12512  *
12513  * NOTE: Port Multiplier code is not completed and tested.
12514  */
12515 
12516 static int
12517 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
12518     sata_device_t *sata_device)
12519 {
12520 	int cport, pmport, qual;
12521 	int rval, rv = 0;
12522 	sata_cport_info_t *cportinfo;
12523 	sata_pmport_info_t *pmportinfo = NULL;
12524 	dev_info_t *tdip;
12525 	sata_drive_info_t *sdinfo = NULL;
12526 
12527 	/* Sanity check */
12528 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
12529 		return (ENOTSUP);
12530 
12531 	cport = sata_device->satadev_addr.cport;
12532 	pmport = sata_device->satadev_addr.pmport;
12533 	qual = sata_device->satadev_addr.qual;
12534 
12535 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12536 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12537 	if (qual == SATA_ADDR_CPORT) {
12538 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12539 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
12540 			/*
12541 			 * For now, assume that port multiplier is not
12542 			 * supported, i.e. deal only with valid devices
12543 			 */
12544 			if ((cportinfo->cport_dev_type &
12545 			    SATA_VALID_DEV_TYPE) != 0)
12546 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12547 			/*
12548 			 * If attached device is a port multiplier, we will
12549 			 * have to unconfigure all devices attached to the
12550 			 * port multiplier. Add this code here.
12551 			 */
12552 		}
12553 		cportinfo->cport_state &= ~SATA_STATE_READY;
12554 	} else {
12555 		/* Port multiplier device port */
12556 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12557 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12558 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
12559 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
12560 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
12561 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
12562 	}
12563 
12564 	if (sdinfo != NULL) {
12565 		/*
12566 		 * If a target node exists, try to offline a device and
12567 		 * to remove a target node.
12568 		 */
12569 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12570 		    cport_mutex);
12571 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12572 		    &sata_device->satadev_addr);
12573 		if (tdip != NULL) {
12574 			/* target node exist */
12575 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
12576 			    "sata_hba_ioctl: port deactivate: "
12577 			    "target node exists.", NULL);
12578 
12579 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
12580 			    NDI_SUCCESS) {
12581 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12582 				    "sata_hba_ioctl: port deactivate: "
12583 				    "failed to unconfigure device at port "
12584 				    "%d:%d before deactivating the port",
12585 				    cport, pmport));
12586 				/*
12587 				 * Set DEVICE REMOVED state in the target
12588 				 * node. It will prevent an access to
12589 				 * the device even when a new device is
12590 				 * attached, until the old target node is
12591 				 * released, removed and recreated for a new
12592 				 * device.
12593 				 */
12594 				sata_set_device_removed(tdip);
12595 
12596 				/*
12597 				 * Instruct the event daemon to try the
12598 				 * target node cleanup later.
12599 				 */
12600 				sata_set_target_node_cleanup(sata_hba_inst,
12601 				    &sata_device->satadev_addr);
12602 			}
12603 		}
12604 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12605 		    cport_mutex);
12606 		/*
12607 		 * In any case, remove and release sata_drive_info
12608 		 * structure.
12609 		 */
12610 		if (qual == SATA_ADDR_CPORT) {
12611 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
12612 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12613 		} else { /* port multiplier device port */
12614 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12615 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
12616 		}
12617 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
12618 	}
12619 	if (qual == SATA_ADDR_CPORT) {
12620 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
12621 		    SATA_STATE_PROBING);
12622 	} else { /* port multiplier device port */
12623 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
12624 		    SATA_STATE_PROBING);
12625 	}
12626 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12627 
12628 	/* Just let HBA driver to deactivate port */
12629 	sata_device->satadev_addr.qual = qual;
12630 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
12631 	    (SATA_DIP(sata_hba_inst), sata_device);
12632 
12633 	/*
12634 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12635 	 * without the hint
12636 	 */
12637 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12638 	    SE_NO_HINT);
12639 
12640 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12641 	sata_update_port_info(sata_hba_inst, sata_device);
12642 	if (qual == SATA_ADDR_CPORT) {
12643 		if (rval != SATA_SUCCESS) {
12644 			/*
12645 			 * Port deactivation failure - do not change port state
12646 			 * unless the state returned by HBA indicates a port
12647 			 * failure.
12648 			 */
12649 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12650 				SATA_CPORT_STATE(sata_hba_inst, cport) =
12651 				    SATA_PSTATE_FAILED;
12652 			}
12653 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12654 			    "sata_hba_ioctl: port deactivate: "
12655 			    "cannot deactivate SATA port %d", cport));
12656 			rv = EIO;
12657 		} else {
12658 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
12659 		}
12660 	} else {
12661 		if (rval != SATA_SUCCESS) {
12662 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12663 				SATA_PMPORT_STATE(sata_hba_inst, cport,
12664 				    pmport) = SATA_PSTATE_FAILED;
12665 			}
12666 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12667 			    "sata_hba_ioctl: port deactivate: "
12668 			    "cannot deactivate SATA port %d:%d",
12669 			    cport, pmport));
12670 			rv = EIO;
12671 		} else {
12672 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
12673 		}
12674 	}
12675 
12676 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12677 
12678 	return (rv);
12679 }
12680 
12681 /*
12682  * Process ioctl port activate request.
12683  *
12684  * NOTE: Port multiplier code is not completed nor tested.
12685  */
12686 static int
12687 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
12688     sata_device_t *sata_device)
12689 {
12690 	int cport, pmport, qual;
12691 	sata_cport_info_t *cportinfo;
12692 	sata_pmport_info_t *pmportinfo = NULL;
12693 	boolean_t dev_existed = TRUE;
12694 
12695 	/* Sanity check */
12696 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
12697 		return (ENOTSUP);
12698 
12699 	cport = sata_device->satadev_addr.cport;
12700 	pmport = sata_device->satadev_addr.pmport;
12701 	qual = sata_device->satadev_addr.qual;
12702 
12703 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12704 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12705 	if (qual == SATA_ADDR_PMPORT) {
12706 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12707 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
12708 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
12709 			dev_existed = FALSE;
12710 	} else { /* cport */
12711 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
12712 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12713 			dev_existed = FALSE;
12714 	}
12715 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12716 
12717 	/* Just let HBA driver to activate port, if necessary */
12718 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12719 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12720 		/*
12721 		 * Port activation failure - do not change port state unless
12722 		 * the state returned by HBA indicates a port failure.
12723 		 */
12724 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12725 		    cport)->cport_mutex);
12726 		sata_update_port_info(sata_hba_inst, sata_device);
12727 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12728 			if (qual == SATA_ADDR_PMPORT)
12729 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
12730 			else
12731 				cportinfo->cport_state = SATA_PSTATE_FAILED;
12732 
12733 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12734 			    cport)->cport_mutex);
12735 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12736 			    "sata_hba_ioctl: port activate: cannot activate "
12737 			    "SATA port %d:%d", cport, pmport));
12738 			return (EIO);
12739 		}
12740 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12741 	}
12742 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12743 	if (qual == SATA_ADDR_PMPORT)
12744 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
12745 	else
12746 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
12747 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12748 
12749 	/*
12750 	 * Re-probe port to find its current state and possibly attached device.
12751 	 * Port re-probing may change the cportinfo device type if device is
12752 	 * found attached.
12753 	 * If port probing failed, the device type would be set to
12754 	 * SATA_DTYPE_NONE.
12755 	 */
12756 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
12757 	    SATA_DEV_IDENTIFY_RETRY);
12758 
12759 	/*
12760 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12761 	 * without the hint.
12762 	 */
12763 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12764 	    SE_NO_HINT);
12765 
12766 	if (dev_existed == FALSE) {
12767 		if (qual == SATA_ADDR_PMPORT &&
12768 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
12769 			/*
12770 			 * That's the transition from the "inactive" port state
12771 			 * or the active port without a device attached to the
12772 			 * active port state with a device attached.
12773 			 */
12774 			sata_log(sata_hba_inst, CE_WARN,
12775 			    "SATA device detected at port %d:%d",
12776 			    cport, pmport);
12777 		} else if (qual == SATA_ADDR_CPORT &&
12778 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
12779 			/*
12780 			 * That's the transition from the "inactive" port state
12781 			 * or the active port without a device attached to the
12782 			 * active port state with a device attached.
12783 			 */
12784 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
12785 				sata_log(sata_hba_inst, CE_WARN,
12786 				    "SATA device detected at port %d", cport);
12787 			} else {
12788 				sata_log(sata_hba_inst, CE_WARN,
12789 				    "SATA port multiplier detected at port %d",
12790 				    cport);
12791 				/*
12792 				 * Because the detected device is a port
12793 				 * multiplier, we need to reprobe every device
12794 				 * port on the port multiplier and show every
12795 				 * device found attached.
12796 				 * Add this code here.
12797 				 */
12798 			}
12799 		}
12800 	}
12801 	return (0);
12802 }
12803 
12804 
12805 
12806 /*
12807  * Process ioctl reset port request.
12808  *
12809  * NOTE: Port multiplier code is not completed nor tested.
12810  */
12811 static int
12812 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
12813     sata_device_t *sata_device)
12814 {
12815 	int cport, pmport, qual;
12816 	int rv = 0;
12817 
12818 	cport = sata_device->satadev_addr.cport;
12819 	pmport = sata_device->satadev_addr.pmport;
12820 	qual = sata_device->satadev_addr.qual;
12821 
12822 	/* Sanity check */
12823 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12824 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12825 		    "sata_hba_ioctl: sata_hba_tran missing required "
12826 		    "function sata_tran_reset_dport"));
12827 		return (ENOTSUP);
12828 	}
12829 
12830 	/* Ask HBA to reset port */
12831 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12832 	    sata_device) != SATA_SUCCESS) {
12833 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12834 		    "sata_hba_ioctl: reset port: failed %d:%d",
12835 		    cport, pmport));
12836 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12837 		    cport_mutex);
12838 		sata_update_port_info(sata_hba_inst, sata_device);
12839 		if (qual == SATA_ADDR_CPORT)
12840 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12841 			    SATA_PSTATE_FAILED;
12842 		else
12843 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12844 			    SATA_PSTATE_FAILED;
12845 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12846 		    cport_mutex);
12847 		rv = EIO;
12848 	}
12849 	/*
12850 	 * Beacuse the port was reset, it should be probed and
12851 	 * attached device reinitialized. At this point the
12852 	 * port state is unknown - it's state is HBA-specific.
12853 	 * Re-probe port to get its state.
12854 	 */
12855 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12856 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
12857 		rv = EIO;
12858 	}
12859 	return (rv);
12860 }
12861 
12862 /*
12863  * Process ioctl reset device request.
12864  *
12865  * NOTE: Port multiplier code is not completed nor tested.
12866  */
12867 static int
12868 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
12869     sata_device_t *sata_device)
12870 {
12871 	sata_drive_info_t *sdinfo;
12872 	int cport, pmport;
12873 	int rv = 0;
12874 
12875 	/* Sanity check */
12876 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12877 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12878 		    "sata_hba_ioctl: sata_hba_tran missing required "
12879 		    "function sata_tran_reset_dport"));
12880 		return (ENOTSUP);
12881 	}
12882 
12883 	cport = sata_device->satadev_addr.cport;
12884 	pmport = sata_device->satadev_addr.pmport;
12885 
12886 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12887 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
12888 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12889 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12890 		    sata_device->satadev_addr.cport);
12891 	} else { /* port multiplier */
12892 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12893 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12894 		    sata_device->satadev_addr.cport,
12895 		    sata_device->satadev_addr.pmport);
12896 	}
12897 	if (sdinfo == NULL) {
12898 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12899 		return (EINVAL);
12900 	}
12901 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12902 
12903 	/* Ask HBA to reset device */
12904 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12905 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12906 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12907 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
12908 		    cport, pmport));
12909 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12910 		    cport_mutex);
12911 		sata_update_port_info(sata_hba_inst, sata_device);
12912 		/*
12913 		 * Device info structure remains attached. Another device reset
12914 		 * or port disconnect/connect and re-probing is
12915 		 * needed to change it's state
12916 		 */
12917 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
12918 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
12919 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12920 		rv = EIO;
12921 	}
12922 	/*
12923 	 * If attached device was a port multiplier, some extra processing
12924 	 * may be needed, to bring it back (if port re-probing did not handle
12925 	 * it). Add such code here.
12926 	 */
12927 	return (rv);
12928 }
12929 
12930 
12931 /*
12932  * Process ioctl reset all request.
12933  *
12934  * NOTE: Port multiplier code is not completed nor tested.
12935  */
12936 static int
12937 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
12938 {
12939 	sata_device_t sata_device;
12940 	int rv = 0;
12941 	int tcport;
12942 	int tpmport = 0;
12943 
12944 	sata_device.satadev_rev = SATA_DEVICE_REV;
12945 
12946 	/*
12947 	 * There is no protection here for configured devices.
12948 	 */
12949 	/* Sanity check */
12950 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12951 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12952 		    "sata_hba_ioctl: sata_hba_tran missing required "
12953 		    "function sata_tran_reset_dport"));
12954 		return (ENOTSUP);
12955 	}
12956 
12957 	/*
12958 	 * Need to lock all ports, not just one.
12959 	 * If any port is locked by event processing, fail the whole operation.
12960 	 * One port is already locked, but for simplicity lock it again.
12961 	 */
12962 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12963 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12964 		    cport_mutex);
12965 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
12966 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
12967 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12968 			    cport_mutex);
12969 			rv = EBUSY;
12970 			break;
12971 		} else {
12972 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
12973 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
12974 			/*
12975 			 * If there is a port multiplier attached, we may need
12976 			 * to lock its port as well. If so, add such code here.
12977 			 */
12978 		}
12979 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12980 		    cport_mutex);
12981 	}
12982 
12983 	if (rv == 0) {
12984 		/*
12985 		 * All cports were successfully locked.
12986 		 * Reset main SATA controller only for now - no PMult.
12987 		 * Set the device address to port 0, to have a valid device
12988 		 * address.
12989 		 */
12990 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
12991 		sata_device.satadev_addr.cport = 0;
12992 		sata_device.satadev_addr.pmport = 0;
12993 
12994 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12995 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
12996 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12997 			    "sata_hba_ioctl: reset controller failed"));
12998 			return (EIO);
12999 		}
13000 		/*
13001 		 * Because ports were reset, port states are unknown.
13002 		 * They should be re-probed to get their state and
13003 		 * attached devices should be reinitialized.
13004 		 * Add code here to re-probe port multiplier device ports.
13005 		 */
13006 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
13007 		    tcport++) {
13008 			sata_device.satadev_addr.cport = tcport;
13009 			sata_device.satadev_addr.pmport = tpmport;
13010 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
13011 
13012 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
13013 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13014 				rv = EIO;
13015 		}
13016 	}
13017 	/*
13018 	 * Unlock all ports
13019 	 */
13020 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
13021 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
13022 		    cport_mutex);
13023 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
13024 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
13025 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
13026 		    cport_mutex);
13027 	}
13028 
13029 	/*
13030 	 * This operation returns EFAULT if either reset
13031 	 * controller failed or a re-probing of any port failed.
13032 	 */
13033 	return (rv);
13034 }
13035 
13036 
13037 /*
13038  * Process ioctl port self test request.
13039  *
13040  * NOTE: Port multiplier code is not completed nor tested.
13041  */
13042 static int
13043 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
13044     sata_device_t *sata_device)
13045 {
13046 	int cport, pmport, qual;
13047 	int rv = 0;
13048 
13049 	/* Sanity check */
13050 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
13051 		return (ENOTSUP);
13052 
13053 	cport = sata_device->satadev_addr.cport;
13054 	pmport = sata_device->satadev_addr.pmport;
13055 	qual = sata_device->satadev_addr.qual;
13056 
13057 	/*
13058 	 * There is no protection here for a configured
13059 	 * device attached to this port.
13060 	 */
13061 
13062 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
13063 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13064 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13065 		    "sata_hba_ioctl: port selftest: "
13066 		    "failed port %d:%d", cport, pmport));
13067 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13068 		    cport_mutex);
13069 		sata_update_port_info(sata_hba_inst, sata_device);
13070 		if (qual == SATA_ADDR_CPORT)
13071 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13072 			    SATA_PSTATE_FAILED;
13073 		else /* port ultiplier device port */
13074 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
13075 			    SATA_PSTATE_FAILED;
13076 
13077 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13078 		    cport_mutex);
13079 		return (EIO);
13080 	}
13081 	/*
13082 	 * Beacuse the port was reset in the course of testing, it should be
13083 	 * re-probed and attached device state should be restored. At this
13084 	 * point the port state is unknown - it's state is HBA-specific.
13085 	 * Force port re-probing to get it into a known state.
13086 	 */
13087 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13088 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13089 		rv = EIO;
13090 	return (rv);
13091 }
13092 
13093 
13094 /*
13095  * sata_cfgadm_state:
13096  * Use the sata port state and state of the target node to figure out
13097  * the cfgadm_state.
13098  *
13099  * The port argument is a value with encoded cport,
13100  * pmport and address qualifier, in the same manner as a scsi target number.
13101  * SCSI_TO_SATA_CPORT macro extracts cport number,
13102  * SCSI_TO_SATA_PMPORT extracts pmport number and
13103  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
13104  *
13105  * For now, support is for cports only - no port multiplier device ports.
13106  */
13107 
13108 static void
13109 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
13110     devctl_ap_state_t *ap_state)
13111 {
13112 	uint16_t	cport;
13113 	int		port_state;
13114 	sata_drive_info_t *sdinfo;
13115 
13116 	/* Cport only */
13117 	cport = SCSI_TO_SATA_CPORT(port);
13118 
13119 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
13120 	if (port_state & SATA_PSTATE_SHUTDOWN ||
13121 	    port_state & SATA_PSTATE_FAILED) {
13122 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
13123 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13124 		if (port_state & SATA_PSTATE_FAILED)
13125 			ap_state->ap_condition = AP_COND_FAILED;
13126 		else
13127 			ap_state->ap_condition = AP_COND_UNKNOWN;
13128 
13129 		return;
13130 	}
13131 
13132 	/* Need to check pmult device port here as well, when supported */
13133 
13134 	/* Port is enabled and ready */
13135 
13136 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
13137 	case SATA_DTYPE_NONE:
13138 	{
13139 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13140 		ap_state->ap_condition = AP_COND_OK;
13141 		/* No device attached */
13142 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
13143 		break;
13144 	}
13145 	case SATA_DTYPE_UNKNOWN:
13146 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
13147 	case SATA_DTYPE_ATADISK:
13148 	case SATA_DTYPE_ATAPICD:
13149 	case SATA_DTYPE_ATAPITAPE:
13150 	case SATA_DTYPE_ATAPIDISK:
13151 	{
13152 		dev_info_t *tdip = NULL;
13153 		dev_info_t *dip = NULL;
13154 		int circ;
13155 
13156 		dip = SATA_DIP(sata_hba_inst);
13157 		tdip = sata_get_target_dip(dip, port);
13158 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
13159 		if (tdip != NULL) {
13160 			ndi_devi_enter(dip, &circ);
13161 			mutex_enter(&(DEVI(tdip)->devi_lock));
13162 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
13163 				/*
13164 				 * There could be the case where previously
13165 				 * configured and opened device was removed
13166 				 * and unknown device was plugged.
13167 				 * In such case we want to show a device, and
13168 				 * its configured or unconfigured state but
13169 				 * indicate unusable condition untill the
13170 				 * old target node is released and removed.
13171 				 */
13172 				ap_state->ap_condition = AP_COND_UNUSABLE;
13173 			} else {
13174 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
13175 				    cport));
13176 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13177 				    cport);
13178 				if (sdinfo != NULL) {
13179 					if ((sdinfo->satadrv_state &
13180 					    SATA_DSTATE_FAILED) != 0)
13181 						ap_state->ap_condition =
13182 						    AP_COND_FAILED;
13183 					else
13184 						ap_state->ap_condition =
13185 						    AP_COND_OK;
13186 				} else {
13187 					ap_state->ap_condition =
13188 					    AP_COND_UNKNOWN;
13189 				}
13190 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
13191 				    cport));
13192 			}
13193 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
13194 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
13195 				ap_state->ap_ostate =
13196 				    AP_OSTATE_UNCONFIGURED;
13197 			} else {
13198 				ap_state->ap_ostate =
13199 				    AP_OSTATE_CONFIGURED;
13200 			}
13201 			mutex_exit(&(DEVI(tdip)->devi_lock));
13202 			ndi_devi_exit(dip, circ);
13203 		} else {
13204 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13205 			ap_state->ap_condition = AP_COND_UNKNOWN;
13206 		}
13207 		break;
13208 	}
13209 	default:
13210 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
13211 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13212 		ap_state->ap_condition = AP_COND_UNKNOWN;
13213 		/*
13214 		 * This is actually internal error condition (non fatal),
13215 		 * because we have already checked all defined device types.
13216 		 */
13217 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13218 		    "sata_cfgadm_state: Internal error: "
13219 		    "unknown device type"));
13220 		break;
13221 	}
13222 }
13223 
13224 
13225 /*
13226  * Process ioctl get device path request.
13227  *
13228  * NOTE: Port multiplier code is not completed nor tested.
13229  */
13230 static int
13231 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
13232     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13233 {
13234 	char path[MAXPATHLEN];
13235 	uint32_t size;
13236 	dev_info_t *tdip;
13237 
13238 	(void) strcpy(path, "/devices");
13239 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13240 	    &sata_device->satadev_addr)) == NULL) {
13241 		/*
13242 		 * No such device. If this is a request for a size, do not
13243 		 * return EINVAL for non-existing target, because cfgadm
13244 		 * will then indicate a meaningless ioctl failure.
13245 		 * If this is a request for a path, indicate invalid
13246 		 * argument.
13247 		 */
13248 		if (ioc->get_size == 0)
13249 			return (EINVAL);
13250 	} else {
13251 		(void) ddi_pathname(tdip, path + strlen(path));
13252 	}
13253 	size = strlen(path) + 1;
13254 
13255 	if (ioc->get_size != 0) {
13256 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
13257 		    mode) != 0)
13258 			return (EFAULT);
13259 	} else {
13260 		if (ioc->bufsiz != size)
13261 			return (EINVAL);
13262 
13263 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
13264 		    mode) != 0)
13265 			return (EFAULT);
13266 	}
13267 	return (0);
13268 }
13269 
13270 /*
13271  * Process ioctl get attachment point type request.
13272  *
13273  * NOTE: Port multiplier code is not completed nor tested.
13274  */
13275 static	int
13276 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
13277     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13278 {
13279 	uint32_t	type_len;
13280 	const char	*ap_type;
13281 	int		dev_type;
13282 
13283 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13284 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
13285 		    sata_device->satadev_addr.cport);
13286 	else /* pmport */
13287 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
13288 		    sata_device->satadev_addr.cport,
13289 		    sata_device->satadev_addr.pmport);
13290 
13291 	switch (dev_type) {
13292 	case SATA_DTYPE_NONE:
13293 		ap_type = "port";
13294 		break;
13295 
13296 	case SATA_DTYPE_ATADISK:
13297 	case SATA_DTYPE_ATAPIDISK:
13298 		ap_type = "disk";
13299 		break;
13300 
13301 	case SATA_DTYPE_ATAPICD:
13302 		ap_type = "cd/dvd";
13303 		break;
13304 
13305 	case SATA_DTYPE_ATAPITAPE:
13306 		ap_type = "tape";
13307 		break;
13308 
13309 	case SATA_DTYPE_PMULT:
13310 		ap_type = "pmult";
13311 		break;
13312 
13313 	case SATA_DTYPE_UNKNOWN:
13314 		ap_type = "unknown";
13315 		break;
13316 
13317 	default:
13318 		ap_type = "unsupported";
13319 		break;
13320 
13321 	} /* end of dev_type switch */
13322 
13323 	type_len = strlen(ap_type) + 1;
13324 
13325 	if (ioc->get_size) {
13326 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
13327 		    mode) != 0)
13328 			return (EFAULT);
13329 	} else {
13330 		if (ioc->bufsiz != type_len)
13331 			return (EINVAL);
13332 
13333 		if (ddi_copyout((void *)ap_type, ioc->buf,
13334 		    ioc->bufsiz, mode) != 0)
13335 			return (EFAULT);
13336 	}
13337 	return (0);
13338 
13339 }
13340 
13341 /*
13342  * Process ioctl get device model info request.
13343  * This operation should return to cfgadm the device model
13344  * information string
13345  *
13346  * NOTE: Port multiplier code is not completed nor tested.
13347  */
13348 static	int
13349 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
13350     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13351 {
13352 	sata_drive_info_t *sdinfo;
13353 	uint32_t info_len;
13354 	char ap_info[SATA_ID_MODEL_LEN + 1];
13355 
13356 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13357 	    sata_device->satadev_addr.cport)->cport_mutex);
13358 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13359 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13360 		    sata_device->satadev_addr.cport);
13361 	else /* port multiplier */
13362 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13363 		    sata_device->satadev_addr.cport,
13364 		    sata_device->satadev_addr.pmport);
13365 	if (sdinfo == NULL) {
13366 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13367 		    sata_device->satadev_addr.cport)->cport_mutex);
13368 		return (EINVAL);
13369 	}
13370 
13371 #ifdef	_LITTLE_ENDIAN
13372 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
13373 #else	/* _LITTLE_ENDIAN */
13374 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
13375 #endif	/* _LITTLE_ENDIAN */
13376 
13377 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13378 	    sata_device->satadev_addr.cport)->cport_mutex);
13379 
13380 	ap_info[SATA_ID_MODEL_LEN] = '\0';
13381 
13382 	info_len = strlen(ap_info) + 1;
13383 
13384 	if (ioc->get_size) {
13385 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13386 		    mode) != 0)
13387 			return (EFAULT);
13388 	} else {
13389 		if (ioc->bufsiz < info_len)
13390 			return (EINVAL);
13391 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13392 		    mode) != 0)
13393 			return (EFAULT);
13394 	}
13395 	return (0);
13396 }
13397 
13398 
13399 /*
13400  * Process ioctl get device firmware revision info request.
13401  * This operation should return to cfgadm the device firmware revision
13402  * information string
13403  *
13404  * NOTE: Port multiplier code is not completed nor tested.
13405  */
13406 static	int
13407 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
13408     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13409 {
13410 	sata_drive_info_t *sdinfo;
13411 	uint32_t info_len;
13412 	char ap_info[SATA_ID_FW_LEN + 1];
13413 
13414 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13415 	    sata_device->satadev_addr.cport)->cport_mutex);
13416 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13417 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13418 		    sata_device->satadev_addr.cport);
13419 	else /* port multiplier */
13420 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13421 		    sata_device->satadev_addr.cport,
13422 		    sata_device->satadev_addr.pmport);
13423 	if (sdinfo == NULL) {
13424 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13425 		    sata_device->satadev_addr.cport)->cport_mutex);
13426 		return (EINVAL);
13427 	}
13428 
13429 #ifdef	_LITTLE_ENDIAN
13430 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
13431 #else	/* _LITTLE_ENDIAN */
13432 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
13433 #endif	/* _LITTLE_ENDIAN */
13434 
13435 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13436 	    sata_device->satadev_addr.cport)->cport_mutex);
13437 
13438 	ap_info[SATA_ID_FW_LEN] = '\0';
13439 
13440 	info_len = strlen(ap_info) + 1;
13441 
13442 	if (ioc->get_size) {
13443 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13444 		    mode) != 0)
13445 			return (EFAULT);
13446 	} else {
13447 		if (ioc->bufsiz < info_len)
13448 			return (EINVAL);
13449 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13450 		    mode) != 0)
13451 			return (EFAULT);
13452 	}
13453 	return (0);
13454 }
13455 
13456 
13457 /*
13458  * Process ioctl get device serial number info request.
13459  * This operation should return to cfgadm the device serial number string.
13460  *
13461  * NOTE: Port multiplier code is not completed nor tested.
13462  */
13463 static	int
13464 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
13465     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13466 {
13467 	sata_drive_info_t *sdinfo;
13468 	uint32_t info_len;
13469 	char ap_info[SATA_ID_SERIAL_LEN + 1];
13470 
13471 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13472 	    sata_device->satadev_addr.cport)->cport_mutex);
13473 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13474 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13475 		    sata_device->satadev_addr.cport);
13476 	else /* port multiplier */
13477 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13478 		    sata_device->satadev_addr.cport,
13479 		    sata_device->satadev_addr.pmport);
13480 	if (sdinfo == NULL) {
13481 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13482 		    sata_device->satadev_addr.cport)->cport_mutex);
13483 		return (EINVAL);
13484 	}
13485 
13486 #ifdef	_LITTLE_ENDIAN
13487 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
13488 #else	/* _LITTLE_ENDIAN */
13489 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
13490 #endif	/* _LITTLE_ENDIAN */
13491 
13492 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13493 	    sata_device->satadev_addr.cport)->cport_mutex);
13494 
13495 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
13496 
13497 	info_len = strlen(ap_info) + 1;
13498 
13499 	if (ioc->get_size) {
13500 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13501 		    mode) != 0)
13502 			return (EFAULT);
13503 	} else {
13504 		if (ioc->bufsiz < info_len)
13505 			return (EINVAL);
13506 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13507 		    mode) != 0)
13508 			return (EFAULT);
13509 	}
13510 	return (0);
13511 }
13512 
13513 
13514 /*
13515  * Preset scsi extended sense data (to NO SENSE)
13516  * First 18 bytes of the sense data are preset to current valid sense
13517  * with a key NO SENSE data.
13518  *
13519  * Returns void
13520  */
13521 static void
13522 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
13523 {
13524 	sense->es_valid = 1;		/* Valid sense */
13525 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
13526 	sense->es_key = KEY_NO_SENSE;
13527 	sense->es_info_1 = 0;
13528 	sense->es_info_2 = 0;
13529 	sense->es_info_3 = 0;
13530 	sense->es_info_4 = 0;
13531 	sense->es_add_len = 10;	/* Additional length - replace with a def */
13532 	sense->es_cmd_info[0] = 0;
13533 	sense->es_cmd_info[1] = 0;
13534 	sense->es_cmd_info[2] = 0;
13535 	sense->es_cmd_info[3] = 0;
13536 	sense->es_add_code = 0;
13537 	sense->es_qual_code = 0;
13538 }
13539 
13540 /*
13541  * Register a legacy cmdk-style devid for the target (disk) device.
13542  *
13543  * Note: This function is called only when the HBA devinfo node has the
13544  * property "use-cmdk-devid-format" set. This property indicates that
13545  * devid compatible with old cmdk (target) driver is to be generated
13546  * for any target device attached to this controller. This will take
13547  * precedence over the devid generated by sd (target) driver.
13548  * This function is derived from cmdk_devid_setup() function in cmdk.c.
13549  */
13550 static void
13551 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
13552 {
13553 	char	*hwid;
13554 	int	modlen;
13555 	int	serlen;
13556 	int	rval;
13557 	ddi_devid_t	devid;
13558 
13559 	/*
13560 	 * device ID is a concatanation of model number, "=", serial number.
13561 	 */
13562 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
13563 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
13564 	    sizeof (sdinfo->satadrv_id.ai_model));
13565 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
13566 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
13567 	if (modlen == 0)
13568 		goto err;
13569 	hwid[modlen++] = '=';
13570 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
13571 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13572 	swab(&hwid[modlen], &hwid[modlen],
13573 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13574 	serlen = sata_check_modser(&hwid[modlen],
13575 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13576 	if (serlen == 0)
13577 		goto err;
13578 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
13579 
13580 	/* initialize/register devid */
13581 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
13582 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
13583 		rval = ddi_devid_register(dip, devid);
13584 		/*
13585 		 * Free up the allocated devid buffer.
13586 		 * NOTE: This doesn't mean unregistering devid.
13587 		 */
13588 		ddi_devid_free(devid);
13589 	}
13590 
13591 	if (rval != DDI_SUCCESS)
13592 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
13593 		    " on port %d", sdinfo->satadrv_addr.cport);
13594 err:
13595 	kmem_free(hwid, LEGACY_HWID_LEN);
13596 }
13597 
13598 /*
13599  * valid model/serial string must contain a non-zero non-space characters.
13600  * trim trailing spaces/NULLs.
13601  */
13602 static int
13603 sata_check_modser(char *buf, int buf_len)
13604 {
13605 	boolean_t ret;
13606 	char *s;
13607 	int i;
13608 	int tb;
13609 	char ch;
13610 
13611 	ret = B_FALSE;
13612 	s = buf;
13613 	for (i = 0; i < buf_len; i++) {
13614 		ch = *s++;
13615 		if (ch != ' ' && ch != '\0')
13616 			tb = i + 1;
13617 		if (ch != ' ' && ch != '\0' && ch != '0')
13618 			ret = B_TRUE;
13619 	}
13620 
13621 	if (ret == B_FALSE)
13622 		return (0); /* invalid string */
13623 
13624 	return (tb); /* return length */
13625 }
13626 
13627 /*
13628  * sata_set_drive_features function compares current device features setting
13629  * with the saved device features settings and, if there is a difference,
13630  * it restores device features setting to the previously saved state.
13631  * It also arbitrarily tries to select the highest supported DMA mode.
13632  * Device Identify or Identify Packet Device data has to be current.
13633  * At the moment read ahead and write cache are considered for all devices.
13634  * For atapi devices, Removable Media Status Notification is set in addition
13635  * to common features.
13636  *
13637  * This function cannot be called in the interrupt context (it may sleep).
13638  *
13639  * The input argument sdinfo should point to the drive info structure
13640  * to be updated after features are set. Note, that only
13641  * device (packet) identify data is updated, not the flags indicating the
13642  * supported features.
13643  *
13644  * Returns SATA_SUCCESS if successful or there was nothing to do.
13645  * Device Identify data in the drive info structure pointed to by the sdinfo
13646  * arguments is updated even when no features were set or changed.
13647  *
13648  * Returns SATA_FAILURE if device features could not be set or DMA mode
13649  * for a disk cannot be set and device identify data cannot be fetched.
13650  *
13651  * Returns SATA_RETRY if device features could not be set (other than disk
13652  * DMA mode) but the device identify data was fetched successfully.
13653  *
13654  * Note: This function may fail the port, making it inaccessible.
13655  * In such case the explicit port disconnect/connect or physical device
13656  * detach/attach is required to re-evaluate port state again.
13657  */
13658 
13659 static int
13660 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
13661     sata_drive_info_t *sdinfo, int restore)
13662 {
13663 	int rval = SATA_SUCCESS;
13664 	int rval_set;
13665 	sata_drive_info_t new_sdinfo;
13666 	char *finfo = "sata_set_drive_features: cannot";
13667 	char *finfox;
13668 	int cache_op;
13669 
13670 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
13671 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
13672 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
13673 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
13674 		/*
13675 		 * Cannot get device identification - caller may retry later
13676 		 */
13677 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13678 		    "%s fetch device identify data\n", finfo);
13679 		return (SATA_FAILURE);
13680 	}
13681 	finfox = (restore != 0) ? " restore device features" :
13682 	    " initialize device features\n";
13683 
13684 	switch (sdinfo->satadrv_type) {
13685 	case SATA_DTYPE_ATADISK:
13686 		/* Arbitrarily set UDMA mode */
13687 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
13688 		    SATA_SUCCESS) {
13689 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13690 			    "%s set UDMA mode\n", finfo));
13691 			return (SATA_FAILURE);
13692 		}
13693 		break;
13694 	case SATA_DTYPE_ATAPICD:
13695 	case SATA_DTYPE_ATAPITAPE:
13696 	case SATA_DTYPE_ATAPIDISK:
13697 		/*  Set Removable Media Status Notification, if necessary */
13698 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
13699 		    restore != 0) {
13700 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
13701 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
13702 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
13703 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
13704 				/* Current setting does not match saved one */
13705 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
13706 				    sdinfo->satadrv_settings &
13707 				    SATA_DEV_RMSN) != SATA_SUCCESS)
13708 					rval = SATA_FAILURE;
13709 			}
13710 		}
13711 		/*
13712 		 * We have to set Multiword DMA or UDMA, if it is supported, as
13713 		 * we want to use DMA transfer mode whenever possible.
13714 		 * Some devices require explicit setting of the DMA mode.
13715 		 */
13716 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
13717 			/* Set highest supported DMA mode */
13718 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
13719 			    SATA_SUCCESS) {
13720 				SATA_LOG_D((sata_hba_inst, CE_WARN,
13721 				    "%s set UDMA mode\n", finfo));
13722 				rval = SATA_FAILURE;
13723 			}
13724 		}
13725 		break;
13726 	}
13727 
13728 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
13729 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
13730 		/*
13731 		 * neither READ AHEAD nor WRITE CACHE is supported
13732 		 * - do nothing
13733 		 */
13734 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13735 		    "settable features not supported\n", NULL);
13736 		goto update_sdinfo;
13737 	}
13738 
13739 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
13740 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
13741 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
13742 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
13743 		/*
13744 		 * both READ AHEAD and WRITE CACHE are enabled
13745 		 * - Nothing to do
13746 		 */
13747 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13748 		    "no device features to set\n", NULL);
13749 		goto update_sdinfo;
13750 	}
13751 
13752 	cache_op = 0;
13753 
13754 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
13755 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13756 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13757 			/* Enable read ahead / read cache */
13758 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
13759 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13760 			    "enabling read cache\n", NULL);
13761 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13762 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13763 			/* Disable read ahead  / read cache */
13764 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
13765 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13766 			    "disabling read cache\n", NULL);
13767 		}
13768 
13769 		if (cache_op != 0) {
13770 			/* Try to set read cache mode */
13771 			rval_set = sata_set_cache_mode(sata_hba_inst,
13772 			    &new_sdinfo, cache_op);
13773 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13774 				rval = rval_set;
13775 		}
13776 	}
13777 
13778 	cache_op = 0;
13779 
13780 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
13781 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13782 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13783 			/* Enable write cache */
13784 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
13785 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13786 			    "enabling write cache\n", NULL);
13787 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13788 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13789 			/* Disable write cache */
13790 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
13791 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13792 			    "disabling write cache\n", NULL);
13793 		}
13794 
13795 		if (cache_op != 0) {
13796 			/* Try to set write cache mode */
13797 			rval_set = sata_set_cache_mode(sata_hba_inst,
13798 			    &new_sdinfo, cache_op);
13799 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13800 				rval = rval_set;
13801 		}
13802 	}
13803 	if (rval != SATA_SUCCESS)
13804 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13805 		    "%s %s", finfo, finfox));
13806 
13807 update_sdinfo:
13808 	/*
13809 	 * We need to fetch Device Identify data again
13810 	 */
13811 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
13812 		/*
13813 		 * Cannot get device identification - retry later
13814 		 */
13815 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13816 		    "%s re-fetch device identify data\n", finfo));
13817 		rval = SATA_FAILURE;
13818 	}
13819 	/* Copy device sata info. */
13820 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
13821 
13822 	return (rval);
13823 }
13824 
13825 
13826 /*
13827  *
13828  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
13829  * unable to determine.
13830  *
13831  * Cannot be called in an interrupt context.
13832  *
13833  * Called by sata_build_lsense_page_2f()
13834  */
13835 
13836 static int
13837 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
13838     sata_drive_info_t *sdinfo)
13839 {
13840 	sata_pkt_t *spkt;
13841 	sata_cmd_t *scmd;
13842 	sata_pkt_txlate_t *spx;
13843 	int rval;
13844 
13845 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13846 	spx->txlt_sata_hba_inst = sata_hba_inst;
13847 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13848 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13849 	if (spkt == NULL) {
13850 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13851 		return (-1);
13852 	}
13853 	/* address is needed now */
13854 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13855 
13856 
13857 	/* Fill sata_pkt */
13858 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13859 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13860 	/* Synchronous mode, no callback */
13861 	spkt->satapkt_comp = NULL;
13862 	/* Timeout 30s */
13863 	spkt->satapkt_time = sata_default_pkt_time;
13864 
13865 	scmd = &spkt->satapkt_cmd;
13866 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
13867 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13868 
13869 	/* Set up which registers need to be returned */
13870 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
13871 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
13872 
13873 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
13874 	scmd->satacmd_addr_type = 0;		/* N/A */
13875 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13876 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13877 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13878 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13879 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
13880 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13881 	scmd->satacmd_cmd_reg = SATAC_SMART;
13882 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13883 	    sdinfo->satadrv_addr.cport)));
13884 
13885 
13886 	/* Send pkt to SATA HBA driver */
13887 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13888 	    SATA_TRAN_ACCEPTED ||
13889 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13890 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13891 		    sdinfo->satadrv_addr.cport)));
13892 		/*
13893 		 * Whoops, no SMART RETURN STATUS
13894 		 */
13895 		rval = -1;
13896 	} else {
13897 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13898 		    sdinfo->satadrv_addr.cport)));
13899 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
13900 			rval = -1;
13901 			goto fail;
13902 		}
13903 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
13904 			rval = -1;
13905 			goto fail;
13906 		}
13907 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
13908 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
13909 			rval = 0;
13910 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
13911 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
13912 			rval = 1;
13913 		else {
13914 			rval = -1;
13915 			goto fail;
13916 		}
13917 	}
13918 fail:
13919 	/* Free allocated resources */
13920 	sata_pkt_free(spx);
13921 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13922 
13923 	return (rval);
13924 }
13925 
13926 /*
13927  *
13928  * Returns 0 if succeeded, -1 otherwise
13929  *
13930  * Cannot be called in an interrupt context.
13931  *
13932  */
13933 static int
13934 sata_fetch_smart_data(
13935 	sata_hba_inst_t *sata_hba_inst,
13936 	sata_drive_info_t *sdinfo,
13937 	struct smart_data *smart_data)
13938 {
13939 	sata_pkt_t *spkt;
13940 	sata_cmd_t *scmd;
13941 	sata_pkt_txlate_t *spx;
13942 	int rval;
13943 
13944 #if ! defined(lint)
13945 	ASSERT(sizeof (struct smart_data) == 512);
13946 #endif
13947 
13948 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13949 	spx->txlt_sata_hba_inst = sata_hba_inst;
13950 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13951 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13952 	if (spkt == NULL) {
13953 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13954 		return (-1);
13955 	}
13956 	/* address is needed now */
13957 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13958 
13959 
13960 	/* Fill sata_pkt */
13961 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13962 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13963 	/* Synchronous mode, no callback */
13964 	spkt->satapkt_comp = NULL;
13965 	/* Timeout 30s */
13966 	spkt->satapkt_time = sata_default_pkt_time;
13967 
13968 	scmd = &spkt->satapkt_cmd;
13969 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13970 
13971 	/*
13972 	 * Allocate buffer for SMART data
13973 	 */
13974 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13975 	    sizeof (struct smart_data));
13976 	if (scmd->satacmd_bp == NULL) {
13977 		sata_pkt_free(spx);
13978 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13979 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13980 		    "sata_fetch_smart_data: "
13981 		    "cannot allocate buffer"));
13982 		return (-1);
13983 	}
13984 
13985 
13986 	/* Build SMART_READ_DATA cmd in the sata_pkt */
13987 	scmd->satacmd_addr_type = 0;		/* N/A */
13988 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13989 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13990 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13991 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13992 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
13993 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13994 	scmd->satacmd_cmd_reg = SATAC_SMART;
13995 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13996 	    sdinfo->satadrv_addr.cport)));
13997 
13998 	/* Send pkt to SATA HBA driver */
13999 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14000 	    SATA_TRAN_ACCEPTED ||
14001 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14002 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14003 		    sdinfo->satadrv_addr.cport)));
14004 		/*
14005 		 * Whoops, no SMART DATA available
14006 		 */
14007 		rval = -1;
14008 		goto fail;
14009 	} else {
14010 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14011 		    sdinfo->satadrv_addr.cport)));
14012 		if (spx->txlt_buf_dma_handle != NULL) {
14013 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14014 			    DDI_DMA_SYNC_FORKERNEL);
14015 			ASSERT(rval == DDI_SUCCESS);
14016 		}
14017 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
14018 		    sizeof (struct smart_data));
14019 	}
14020 
14021 fail:
14022 	/* Free allocated resources */
14023 	sata_free_local_buffer(spx);
14024 	sata_pkt_free(spx);
14025 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14026 
14027 	return (rval);
14028 }
14029 
14030 /*
14031  * Used by LOG SENSE page 0x10
14032  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
14033  * Note: cannot be called in the interrupt context.
14034  *
14035  * return 0 for success, -1 otherwise
14036  *
14037  */
14038 static int
14039 sata_ext_smart_selftest_read_log(
14040 	sata_hba_inst_t *sata_hba_inst,
14041 	sata_drive_info_t *sdinfo,
14042 	struct smart_ext_selftest_log *ext_selftest_log,
14043 	uint16_t block_num)
14044 {
14045 	sata_pkt_txlate_t *spx;
14046 	sata_pkt_t *spkt;
14047 	sata_cmd_t *scmd;
14048 	int rval;
14049 
14050 #if ! defined(lint)
14051 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
14052 #endif
14053 
14054 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14055 	spx->txlt_sata_hba_inst = sata_hba_inst;
14056 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14057 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14058 	if (spkt == NULL) {
14059 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14060 		return (-1);
14061 	}
14062 	/* address is needed now */
14063 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14064 
14065 
14066 	/* Fill sata_pkt */
14067 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14068 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14069 	/* Synchronous mode, no callback */
14070 	spkt->satapkt_comp = NULL;
14071 	/* Timeout 30s */
14072 	spkt->satapkt_time = sata_default_pkt_time;
14073 
14074 	scmd = &spkt->satapkt_cmd;
14075 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14076 
14077 	/*
14078 	 * Allocate buffer for SMART extended self-test log
14079 	 */
14080 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14081 	    sizeof (struct smart_ext_selftest_log));
14082 	if (scmd->satacmd_bp == NULL) {
14083 		sata_pkt_free(spx);
14084 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14085 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14086 		    "sata_ext_smart_selftest_log: "
14087 		    "cannot allocate buffer"));
14088 		return (-1);
14089 	}
14090 
14091 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
14092 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
14093 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
14094 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
14095 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
14096 	scmd->satacmd_lba_low_msb = 0;
14097 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
14098 	scmd->satacmd_lba_mid_msb = block_num >> 8;
14099 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14100 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
14101 
14102 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14103 	    sdinfo->satadrv_addr.cport)));
14104 
14105 	/* Send pkt to SATA HBA driver */
14106 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14107 	    SATA_TRAN_ACCEPTED ||
14108 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14109 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14110 		    sdinfo->satadrv_addr.cport)));
14111 
14112 		/*
14113 		 * Whoops, no SMART selftest log info available
14114 		 */
14115 		rval = -1;
14116 		goto fail;
14117 	} else {
14118 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14119 		    sdinfo->satadrv_addr.cport)));
14120 
14121 		if (spx->txlt_buf_dma_handle != NULL) {
14122 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14123 			    DDI_DMA_SYNC_FORKERNEL);
14124 			ASSERT(rval == DDI_SUCCESS);
14125 		}
14126 		bcopy(scmd->satacmd_bp->b_un.b_addr,
14127 		    (uint8_t *)ext_selftest_log,
14128 		    sizeof (struct smart_ext_selftest_log));
14129 		rval = 0;
14130 	}
14131 
14132 fail:
14133 	/* Free allocated resources */
14134 	sata_free_local_buffer(spx);
14135 	sata_pkt_free(spx);
14136 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14137 
14138 	return (rval);
14139 }
14140 
14141 /*
14142  * Returns 0 for success, -1 otherwise
14143  *
14144  * SMART self-test log data is returned in buffer pointed to by selftest_log
14145  */
14146 static int
14147 sata_smart_selftest_log(
14148 	sata_hba_inst_t *sata_hba_inst,
14149 	sata_drive_info_t *sdinfo,
14150 	struct smart_selftest_log *selftest_log)
14151 {
14152 	sata_pkt_t *spkt;
14153 	sata_cmd_t *scmd;
14154 	sata_pkt_txlate_t *spx;
14155 	int rval;
14156 
14157 #if ! defined(lint)
14158 	ASSERT(sizeof (struct smart_selftest_log) == 512);
14159 #endif
14160 
14161 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14162 	spx->txlt_sata_hba_inst = sata_hba_inst;
14163 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14164 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14165 	if (spkt == NULL) {
14166 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14167 		return (-1);
14168 	}
14169 	/* address is needed now */
14170 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14171 
14172 
14173 	/* Fill sata_pkt */
14174 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14175 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14176 	/* Synchronous mode, no callback */
14177 	spkt->satapkt_comp = NULL;
14178 	/* Timeout 30s */
14179 	spkt->satapkt_time = sata_default_pkt_time;
14180 
14181 	scmd = &spkt->satapkt_cmd;
14182 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14183 
14184 	/*
14185 	 * Allocate buffer for SMART SELFTEST LOG
14186 	 */
14187 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14188 	    sizeof (struct smart_selftest_log));
14189 	if (scmd->satacmd_bp == NULL) {
14190 		sata_pkt_free(spx);
14191 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14192 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14193 		    "sata_smart_selftest_log: "
14194 		    "cannot allocate buffer"));
14195 		return (-1);
14196 	}
14197 
14198 	/* Build SMART_READ_LOG cmd in the sata_pkt */
14199 	scmd->satacmd_addr_type = 0;		/* N/A */
14200 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
14201 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
14202 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
14203 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
14204 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
14205 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14206 	scmd->satacmd_cmd_reg = SATAC_SMART;
14207 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14208 	    sdinfo->satadrv_addr.cport)));
14209 
14210 	/* Send pkt to SATA HBA driver */
14211 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14212 	    SATA_TRAN_ACCEPTED ||
14213 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14214 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14215 		    sdinfo->satadrv_addr.cport)));
14216 		/*
14217 		 * Whoops, no SMART DATA available
14218 		 */
14219 		rval = -1;
14220 		goto fail;
14221 	} else {
14222 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14223 		    sdinfo->satadrv_addr.cport)));
14224 		if (spx->txlt_buf_dma_handle != NULL) {
14225 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14226 			    DDI_DMA_SYNC_FORKERNEL);
14227 			ASSERT(rval == DDI_SUCCESS);
14228 		}
14229 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
14230 		    sizeof (struct smart_selftest_log));
14231 		rval = 0;
14232 	}
14233 
14234 fail:
14235 	/* Free allocated resources */
14236 	sata_free_local_buffer(spx);
14237 	sata_pkt_free(spx);
14238 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14239 
14240 	return (rval);
14241 }
14242 
14243 
14244 /*
14245  * Returns 0 for success, -1 otherwise
14246  *
14247  * SMART READ LOG data is returned in buffer pointed to by smart_log
14248  */
14249 static int
14250 sata_smart_read_log(
14251 	sata_hba_inst_t *sata_hba_inst,
14252 	sata_drive_info_t *sdinfo,
14253 	uint8_t *smart_log,		/* where the data should be returned */
14254 	uint8_t which_log,		/* which log should be returned */
14255 	uint8_t log_size)		/* # of 512 bytes in log */
14256 {
14257 	sata_pkt_t *spkt;
14258 	sata_cmd_t *scmd;
14259 	sata_pkt_txlate_t *spx;
14260 	int rval;
14261 
14262 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14263 	spx->txlt_sata_hba_inst = sata_hba_inst;
14264 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14265 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14266 	if (spkt == NULL) {
14267 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14268 		return (-1);
14269 	}
14270 	/* address is needed now */
14271 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14272 
14273 
14274 	/* Fill sata_pkt */
14275 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14276 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14277 	/* Synchronous mode, no callback */
14278 	spkt->satapkt_comp = NULL;
14279 	/* Timeout 30s */
14280 	spkt->satapkt_time = sata_default_pkt_time;
14281 
14282 	scmd = &spkt->satapkt_cmd;
14283 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14284 
14285 	/*
14286 	 * Allocate buffer for SMART READ LOG
14287 	 */
14288 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
14289 	if (scmd->satacmd_bp == NULL) {
14290 		sata_pkt_free(spx);
14291 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14292 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14293 		    "sata_smart_read_log: " "cannot allocate buffer"));
14294 		return (-1);
14295 	}
14296 
14297 	/* Build SMART_READ_LOG cmd in the sata_pkt */
14298 	scmd->satacmd_addr_type = 0;		/* N/A */
14299 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
14300 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
14301 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
14302 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
14303 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
14304 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14305 	scmd->satacmd_cmd_reg = SATAC_SMART;
14306 
14307 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14308 	    sdinfo->satadrv_addr.cport)));
14309 
14310 	/* Send pkt to SATA HBA driver */
14311 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14312 	    SATA_TRAN_ACCEPTED ||
14313 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14314 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14315 		    sdinfo->satadrv_addr.cport)));
14316 
14317 		/*
14318 		 * Whoops, no SMART DATA available
14319 		 */
14320 		rval = -1;
14321 		goto fail;
14322 	} else {
14323 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14324 		    sdinfo->satadrv_addr.cport)));
14325 
14326 		if (spx->txlt_buf_dma_handle != NULL) {
14327 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14328 			    DDI_DMA_SYNC_FORKERNEL);
14329 			ASSERT(rval == DDI_SUCCESS);
14330 		}
14331 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
14332 		rval = 0;
14333 	}
14334 
14335 fail:
14336 	/* Free allocated resources */
14337 	sata_free_local_buffer(spx);
14338 	sata_pkt_free(spx);
14339 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14340 
14341 	return (rval);
14342 }
14343 
14344 /*
14345  * Used by LOG SENSE page 0x10
14346  *
14347  * return 0 for success, -1 otherwise
14348  *
14349  */
14350 static int
14351 sata_read_log_ext_directory(
14352 	sata_hba_inst_t *sata_hba_inst,
14353 	sata_drive_info_t *sdinfo,
14354 	struct read_log_ext_directory *logdir)
14355 {
14356 	sata_pkt_txlate_t *spx;
14357 	sata_pkt_t *spkt;
14358 	sata_cmd_t *scmd;
14359 	int rval;
14360 
14361 #if ! defined(lint)
14362 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
14363 #endif
14364 
14365 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14366 	spx->txlt_sata_hba_inst = sata_hba_inst;
14367 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14368 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14369 	if (spkt == NULL) {
14370 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14371 		return (-1);
14372 	}
14373 
14374 	/* Fill sata_pkt */
14375 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14376 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14377 	/* Synchronous mode, no callback */
14378 	spkt->satapkt_comp = NULL;
14379 	/* Timeout 30s */
14380 	spkt->satapkt_time = sata_default_pkt_time;
14381 
14382 	scmd = &spkt->satapkt_cmd;
14383 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14384 
14385 	/*
14386 	 * Allocate buffer for SMART READ LOG EXTENDED command
14387 	 */
14388 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14389 	    sizeof (struct read_log_ext_directory));
14390 	if (scmd->satacmd_bp == NULL) {
14391 		sata_pkt_free(spx);
14392 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14393 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14394 		    "sata_read_log_ext_directory: "
14395 		    "cannot allocate buffer"));
14396 		return (-1);
14397 	}
14398 
14399 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
14400 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
14401 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
14402 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
14403 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
14404 	scmd->satacmd_lba_low_msb = 0;
14405 	scmd->satacmd_lba_mid_lsb = 0;
14406 	scmd->satacmd_lba_mid_msb = 0;
14407 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14408 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
14409 
14410 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14411 	    sdinfo->satadrv_addr.cport)));
14412 
14413 	/* Send pkt to SATA HBA driver */
14414 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14415 	    SATA_TRAN_ACCEPTED ||
14416 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14417 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14418 		    sdinfo->satadrv_addr.cport)));
14419 		/*
14420 		 * Whoops, no SMART selftest log info available
14421 		 */
14422 		rval = -1;
14423 		goto fail;
14424 	} else {
14425 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14426 		    sdinfo->satadrv_addr.cport)));
14427 		if (spx->txlt_buf_dma_handle != NULL) {
14428 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14429 			    DDI_DMA_SYNC_FORKERNEL);
14430 			ASSERT(rval == DDI_SUCCESS);
14431 		}
14432 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
14433 		    sizeof (struct read_log_ext_directory));
14434 		rval = 0;
14435 	}
14436 
14437 fail:
14438 	/* Free allocated resources */
14439 	sata_free_local_buffer(spx);
14440 	sata_pkt_free(spx);
14441 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14442 
14443 	return (rval);
14444 }
14445 
14446 /*
14447  * Set up error retrieval sata command for NCQ command error data
14448  * recovery.
14449  *
14450  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
14451  * returns SATA_FAILURE otherwise.
14452  */
14453 static int
14454 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
14455 {
14456 #ifndef __lock_lint
14457 	_NOTE(ARGUNUSED(sdinfo))
14458 #endif
14459 
14460 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
14461 	sata_cmd_t *scmd;
14462 	struct buf *bp;
14463 
14464 	/* Operation modes are up to the caller */
14465 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14466 
14467 	/* Synchronous mode, no callback - may be changed by the caller */
14468 	spkt->satapkt_comp = NULL;
14469 	spkt->satapkt_time = sata_default_pkt_time;
14470 
14471 	scmd = &spkt->satapkt_cmd;
14472 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
14473 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14474 
14475 	/*
14476 	 * Allocate dma_able buffer error data.
14477 	 * Buffer allocation will take care of buffer alignment and other DMA
14478 	 * attributes.
14479 	 */
14480 	bp = sata_alloc_local_buffer(spx,
14481 	    sizeof (struct sata_ncq_error_recovery_page));
14482 	if (bp == NULL)
14483 		return (SATA_FAILURE);
14484 
14485 	bp_mapin(bp); /* make data buffer accessible */
14486 	scmd->satacmd_bp = bp;
14487 
14488 	/*
14489 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
14490 	 * before accessing it. Handle is in usual place in translate struct.
14491 	 */
14492 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
14493 
14494 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
14495 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
14496 
14497 	return (SATA_SUCCESS);
14498 }
14499 
14500 /*
14501  * sata_xlate_errors() is used to translate (S)ATA error
14502  * information to SCSI information returned in the SCSI
14503  * packet.
14504  */
14505 static void
14506 sata_xlate_errors(sata_pkt_txlate_t *spx)
14507 {
14508 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
14509 	struct scsi_extended_sense *sense;
14510 
14511 	scsipkt->pkt_reason = CMD_INCOMPLETE;
14512 	*scsipkt->pkt_scbp = STATUS_CHECK;
14513 	sense = sata_arq_sense(spx);
14514 
14515 	switch (spx->txlt_sata_pkt->satapkt_reason) {
14516 	case SATA_PKT_PORT_ERROR:
14517 		/*
14518 		 * We have no device data. Assume no data transfered.
14519 		 */
14520 		sense->es_key = KEY_HARDWARE_ERROR;
14521 		break;
14522 
14523 	case SATA_PKT_DEV_ERROR:
14524 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
14525 		    SATA_STATUS_ERR) {
14526 			/*
14527 			 * determine dev error reason from error
14528 			 * reg content
14529 			 */
14530 			sata_decode_device_error(spx, sense);
14531 			break;
14532 		}
14533 		/* No extended sense key - no info available */
14534 		break;
14535 
14536 	case SATA_PKT_TIMEOUT:
14537 		scsipkt->pkt_reason = CMD_TIMEOUT;
14538 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
14539 		/* No extended sense key */
14540 		break;
14541 
14542 	case SATA_PKT_ABORTED:
14543 		scsipkt->pkt_reason = CMD_ABORTED;
14544 		scsipkt->pkt_statistics |= STAT_ABORTED;
14545 		/* No extended sense key */
14546 		break;
14547 
14548 	case SATA_PKT_RESET:
14549 		/*
14550 		 * pkt aborted either by an explicit reset request from
14551 		 * a host, or due to error recovery
14552 		 */
14553 		scsipkt->pkt_reason = CMD_RESET;
14554 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
14555 		break;
14556 
14557 	default:
14558 		scsipkt->pkt_reason = CMD_TRAN_ERR;
14559 		break;
14560 	}
14561 }
14562 
14563 
14564 
14565 
14566 /*
14567  * Log sata message
14568  * dev pathname msg line preceeds the logged message.
14569  */
14570 
14571 static	void
14572 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
14573 {
14574 	char pathname[128];
14575 	dev_info_t *dip = NULL;
14576 	va_list ap;
14577 
14578 	mutex_enter(&sata_log_mutex);
14579 
14580 	va_start(ap, fmt);
14581 	(void) vsprintf(sata_log_buf, fmt, ap);
14582 	va_end(ap);
14583 
14584 	if (sata_hba_inst != NULL) {
14585 		dip = SATA_DIP(sata_hba_inst);
14586 		(void) ddi_pathname(dip, pathname);
14587 	} else {
14588 		pathname[0] = 0;
14589 	}
14590 	if (level == CE_CONT) {
14591 		if (sata_debug_flags == 0)
14592 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
14593 		else
14594 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
14595 	} else {
14596 		if (level != CE_NOTE) {
14597 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
14598 		} else if (sata_msg) {
14599 			cmn_err(level, "%s:\n %s", pathname,
14600 			    sata_log_buf);
14601 		}
14602 	}
14603 
14604 	/* sata trace debug */
14605 	sata_trace_debug(dip, sata_log_buf);
14606 
14607 	mutex_exit(&sata_log_mutex);
14608 }
14609 
14610 
14611 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
14612 
14613 /*
14614  * Start or terminate the thread, depending on flag arg and current state
14615  */
14616 static void
14617 sata_event_thread_control(int startstop)
14618 {
14619 	static 	int sata_event_thread_terminating = 0;
14620 	static 	int sata_event_thread_starting = 0;
14621 	int i;
14622 
14623 	mutex_enter(&sata_event_mutex);
14624 
14625 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
14626 	    sata_event_thread_terminating == 1)) {
14627 		mutex_exit(&sata_event_mutex);
14628 		return;
14629 	}
14630 	if (startstop == 1 && sata_event_thread_starting == 1) {
14631 		mutex_exit(&sata_event_mutex);
14632 		return;
14633 	}
14634 	if (startstop == 1 && sata_event_thread_terminating == 1) {
14635 		sata_event_thread_starting = 1;
14636 		/* wait til terminate operation completes */
14637 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
14638 		while (sata_event_thread_terminating == 1) {
14639 			if (i-- <= 0) {
14640 				sata_event_thread_starting = 0;
14641 				mutex_exit(&sata_event_mutex);
14642 #ifdef SATA_DEBUG
14643 				cmn_err(CE_WARN, "sata_event_thread_control: "
14644 				    "timeout waiting for thread to terminate");
14645 #endif
14646 				return;
14647 			}
14648 			mutex_exit(&sata_event_mutex);
14649 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
14650 			mutex_enter(&sata_event_mutex);
14651 		}
14652 	}
14653 	if (startstop == 1) {
14654 		if (sata_event_thread == NULL) {
14655 			sata_event_thread = thread_create(NULL, 0,
14656 			    (void (*)())sata_event_daemon,
14657 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
14658 		}
14659 		sata_event_thread_starting = 0;
14660 		mutex_exit(&sata_event_mutex);
14661 		return;
14662 	}
14663 
14664 	/*
14665 	 * If we got here, thread may need to be terminated
14666 	 */
14667 	if (sata_event_thread != NULL) {
14668 		int i;
14669 		/* Signal event thread to go away */
14670 		sata_event_thread_terminating = 1;
14671 		sata_event_thread_terminate = 1;
14672 		cv_signal(&sata_event_cv);
14673 		/*
14674 		 * Wait til daemon terminates.
14675 		 */
14676 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
14677 		while (sata_event_thread_terminate == 1) {
14678 			mutex_exit(&sata_event_mutex);
14679 			if (i-- <= 0) {
14680 				/* Daemon did not go away !!! */
14681 #ifdef SATA_DEBUG
14682 				cmn_err(CE_WARN, "sata_event_thread_control: "
14683 				    "cannot terminate event daemon thread");
14684 #endif
14685 				mutex_enter(&sata_event_mutex);
14686 				break;
14687 			}
14688 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
14689 			mutex_enter(&sata_event_mutex);
14690 		}
14691 		sata_event_thread_terminating = 0;
14692 	}
14693 	ASSERT(sata_event_thread_terminating == 0);
14694 	ASSERT(sata_event_thread_starting == 0);
14695 	mutex_exit(&sata_event_mutex);
14696 }
14697 
14698 
14699 /*
14700  * SATA HBA event notification function.
14701  * Events reported by SATA HBA drivers per HBA instance relate to a change in
14702  * a port and/or device state or a controller itself.
14703  * Events for different addresses/addr types cannot be combined.
14704  * A warning message is generated for each event type.
14705  * Events are not processed by this function, so only the
14706  * event flag(s)is set for an affected entity and the event thread is
14707  * waken up. Event daemon thread processes all events.
14708  *
14709  * NOTE: Since more than one event may be reported at the same time, one
14710  * cannot determine a sequence of events when opposite event are reported, eg.
14711  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
14712  * is taking precedence over reported events, i.e. may cause ignoring some
14713  * events.
14714  */
14715 #define	SATA_EVENT_MAX_MSG_LENGTH	79
14716 
14717 void
14718 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
14719 {
14720 	sata_hba_inst_t *sata_hba_inst = NULL;
14721 	sata_address_t *saddr;
14722 	sata_drive_info_t *sdinfo;
14723 	sata_port_stats_t *pstats;
14724 	sata_cport_info_t *cportinfo;
14725 	sata_pmport_info_t *pmportinfo;
14726 	int cport, pmport;
14727 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
14728 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
14729 	char *lcp;
14730 	static char *err_msg_evnt_1 =
14731 	    "sata_hba_event_notify: invalid port event 0x%x ";
14732 	static char *err_msg_evnt_2 =
14733 	    "sata_hba_event_notify: invalid device event 0x%x ";
14734 	int linkevent;
14735 
14736 	/*
14737 	 * There is a possibility that an event will be generated on HBA
14738 	 * that has not completed attachment or is detaching. We still want
14739 	 * to process events until HBA is detached.
14740 	 */
14741 	mutex_enter(&sata_mutex);
14742 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14743 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14744 		if (SATA_DIP(sata_hba_inst) == dip)
14745 			if (sata_hba_inst->satahba_attached == 1)
14746 				break;
14747 	}
14748 	mutex_exit(&sata_mutex);
14749 	if (sata_hba_inst == NULL)
14750 		/* HBA not attached */
14751 		return;
14752 
14753 	ASSERT(sata_device != NULL);
14754 
14755 	/*
14756 	 * Validate address before - do not proceed with invalid address.
14757 	 */
14758 	saddr = &sata_device->satadev_addr;
14759 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
14760 		return;
14761 	if (saddr->qual == SATA_ADDR_PMPORT ||
14762 	    saddr->qual == SATA_ADDR_DPMPORT)
14763 		/* Port Multiplier not supported yet */
14764 		return;
14765 
14766 	cport = saddr->cport;
14767 	pmport = saddr->pmport;
14768 
14769 	buf1[0] = buf2[0] = '\0';
14770 
14771 	/*
14772 	 * If event relates to port or device, check port state.
14773 	 * Port has to be initialized, or we cannot accept an event.
14774 	 */
14775 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
14776 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) {
14777 		if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) {
14778 			mutex_enter(&sata_hba_inst->satahba_mutex);
14779 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14780 			mutex_exit(&sata_hba_inst->satahba_mutex);
14781 			if (cportinfo == NULL || cportinfo->cport_state == 0)
14782 				return;
14783 		} else {
14784 			mutex_enter(&sata_hba_inst->satahba_mutex);
14785 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14786 			    cport, pmport);
14787 			mutex_exit(&sata_hba_inst->satahba_mutex);
14788 			if (pmportinfo == NULL || pmportinfo->pmport_state == 0)
14789 				return;
14790 		}
14791 	}
14792 
14793 	/*
14794 	 * Events refer to devices, ports and controllers - each has
14795 	 * unique address. Events for different addresses cannot be combined.
14796 	 */
14797 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
14798 
14799 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14800 
14801 		/* qualify this event(s) */
14802 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
14803 			/* Invalid event for the device port */
14804 			(void) sprintf(buf2, err_msg_evnt_1,
14805 			    event & SATA_EVNT_PORT_EVENTS);
14806 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14807 			goto event_info;
14808 		}
14809 		if (saddr->qual == SATA_ADDR_CPORT) {
14810 			/* Controller's device port event */
14811 
14812 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
14813 			    cport_event_flags |=
14814 			    event & SATA_EVNT_PORT_EVENTS;
14815 			pstats =
14816 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
14817 			    cport_stats;
14818 		} else {
14819 			/* Port multiplier's device port event */
14820 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14821 			    pmport_event_flags |=
14822 			    event & SATA_EVNT_PORT_EVENTS;
14823 			pstats =
14824 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14825 			    pmport_stats;
14826 		}
14827 
14828 		/*
14829 		 * Add to statistics and log the message. We have to do it
14830 		 * here rather than in the event daemon, because there may be
14831 		 * multiple events occuring before they are processed.
14832 		 */
14833 		linkevent = event &
14834 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
14835 		if (linkevent) {
14836 			if (linkevent == (SATA_EVNT_LINK_LOST |
14837 			    SATA_EVNT_LINK_ESTABLISHED)) {
14838 				/* This is likely event combination */
14839 				(void) strlcat(buf1, "link lost/established, ",
14840 				    SATA_EVENT_MAX_MSG_LENGTH);
14841 
14842 				if (pstats->link_lost < 0xffffffffffffffffULL)
14843 					pstats->link_lost++;
14844 				if (pstats->link_established <
14845 				    0xffffffffffffffffULL)
14846 					pstats->link_established++;
14847 				linkevent = 0;
14848 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
14849 				(void) strlcat(buf1, "link lost, ",
14850 				    SATA_EVENT_MAX_MSG_LENGTH);
14851 
14852 				if (pstats->link_lost < 0xffffffffffffffffULL)
14853 					pstats->link_lost++;
14854 			} else {
14855 				(void) strlcat(buf1, "link established, ",
14856 				    SATA_EVENT_MAX_MSG_LENGTH);
14857 				if (pstats->link_established <
14858 				    0xffffffffffffffffULL)
14859 					pstats->link_established++;
14860 			}
14861 		}
14862 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
14863 			(void) strlcat(buf1, "device attached, ",
14864 			    SATA_EVENT_MAX_MSG_LENGTH);
14865 			if (pstats->device_attached < 0xffffffffffffffffULL)
14866 				pstats->device_attached++;
14867 		}
14868 		if (event & SATA_EVNT_DEVICE_DETACHED) {
14869 			(void) strlcat(buf1, "device detached, ",
14870 			    SATA_EVENT_MAX_MSG_LENGTH);
14871 			if (pstats->device_detached < 0xffffffffffffffffULL)
14872 				pstats->device_detached++;
14873 		}
14874 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
14875 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14876 			    "port %d power level changed", cport);
14877 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
14878 				pstats->port_pwr_changed++;
14879 		}
14880 
14881 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
14882 			/* There should be no other events for this address */
14883 			(void) sprintf(buf2, err_msg_evnt_1,
14884 			    event & ~SATA_EVNT_PORT_EVENTS);
14885 		}
14886 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14887 
14888 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
14889 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14890 
14891 		/* qualify this event */
14892 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
14893 			/* Invalid event for a device */
14894 			(void) sprintf(buf2, err_msg_evnt_2,
14895 			    event & SATA_EVNT_DEVICE_RESET);
14896 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14897 			goto event_info;
14898 		}
14899 		/* drive event */
14900 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
14901 		if (sdinfo != NULL) {
14902 			if (event & SATA_EVNT_DEVICE_RESET) {
14903 				(void) strlcat(buf1, "device reset, ",
14904 				    SATA_EVENT_MAX_MSG_LENGTH);
14905 				if (sdinfo->satadrv_stats.drive_reset <
14906 				    0xffffffffffffffffULL)
14907 					sdinfo->satadrv_stats.drive_reset++;
14908 				sdinfo->satadrv_event_flags |=
14909 				    SATA_EVNT_DEVICE_RESET;
14910 			}
14911 		}
14912 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
14913 			/* Invalid event for a device */
14914 			(void) sprintf(buf2, err_msg_evnt_2,
14915 			    event & ~SATA_EVNT_DRIVE_EVENTS);
14916 		}
14917 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14918 	} else {
14919 		if (saddr->qual != SATA_ADDR_NULL) {
14920 			/* Wrong address qualifier */
14921 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14922 			    "sata_hba_event_notify: invalid address 0x%x",
14923 			    *(uint32_t *)saddr));
14924 			return;
14925 		}
14926 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
14927 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
14928 			/* Invalid event for the controller */
14929 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14930 			    "sata_hba_event_notify: invalid event 0x%x for "
14931 			    "controller",
14932 			    event & SATA_EVNT_CONTROLLER_EVENTS));
14933 			return;
14934 		}
14935 		buf1[0] = '\0';
14936 		/* This may be a frequent and not interesting event */
14937 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14938 		    "controller power level changed\n", NULL);
14939 
14940 		mutex_enter(&sata_hba_inst->satahba_mutex);
14941 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
14942 		    0xffffffffffffffffULL)
14943 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
14944 
14945 		sata_hba_inst->satahba_event_flags |=
14946 		    SATA_EVNT_PWR_LEVEL_CHANGED;
14947 		mutex_exit(&sata_hba_inst->satahba_mutex);
14948 	}
14949 	/*
14950 	 * If we got here, there is something to do with this HBA
14951 	 * instance.
14952 	 */
14953 	mutex_enter(&sata_hba_inst->satahba_mutex);
14954 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14955 	mutex_exit(&sata_hba_inst->satahba_mutex);
14956 	mutex_enter(&sata_mutex);
14957 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
14958 	mutex_exit(&sata_mutex);
14959 
14960 	/* Tickle event thread */
14961 	mutex_enter(&sata_event_mutex);
14962 	if (sata_event_thread_active == 0)
14963 		cv_signal(&sata_event_cv);
14964 	mutex_exit(&sata_event_mutex);
14965 
14966 event_info:
14967 	if (buf1[0] != '\0') {
14968 		lcp = strrchr(buf1, ',');
14969 		if (lcp != NULL)
14970 			*lcp = '\0';
14971 	}
14972 	if (saddr->qual == SATA_ADDR_CPORT ||
14973 	    saddr->qual == SATA_ADDR_DCPORT) {
14974 		if (buf1[0] != '\0') {
14975 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14976 			    cport, buf1);
14977 		}
14978 		if (buf2[0] != '\0') {
14979 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14980 			    cport, buf2);
14981 		}
14982 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
14983 	    saddr->qual == SATA_ADDR_DPMPORT) {
14984 		if (buf1[0] != '\0') {
14985 			sata_log(sata_hba_inst, CE_NOTE,
14986 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
14987 		}
14988 		if (buf2[0] != '\0') {
14989 			sata_log(sata_hba_inst, CE_NOTE,
14990 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
14991 		}
14992 	}
14993 }
14994 
14995 
14996 /*
14997  * Event processing thread.
14998  * Arg is a pointer to the sata_hba_list pointer.
14999  * It is not really needed, because sata_hba_list is global and static
15000  */
15001 static void
15002 sata_event_daemon(void *arg)
15003 {
15004 #ifndef __lock_lint
15005 	_NOTE(ARGUNUSED(arg))
15006 #endif
15007 	sata_hba_inst_t *sata_hba_inst;
15008 	clock_t lbolt;
15009 
15010 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15011 	    "SATA event daemon started\n", NULL);
15012 loop:
15013 	/*
15014 	 * Process events here. Walk through all registered HBAs
15015 	 */
15016 	mutex_enter(&sata_mutex);
15017 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
15018 	    sata_hba_inst = sata_hba_inst->satahba_next) {
15019 		ASSERT(sata_hba_inst != NULL);
15020 		mutex_enter(&sata_hba_inst->satahba_mutex);
15021 		if (sata_hba_inst->satahba_attached == 0 ||
15022 		    (sata_hba_inst->satahba_event_flags &
15023 		    SATA_EVNT_SKIP) != 0) {
15024 			mutex_exit(&sata_hba_inst->satahba_mutex);
15025 			continue;
15026 		}
15027 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
15028 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
15029 			mutex_exit(&sata_hba_inst->satahba_mutex);
15030 			mutex_exit(&sata_mutex);
15031 			/* Got the controller with pending event */
15032 			sata_process_controller_events(sata_hba_inst);
15033 			/*
15034 			 * Since global mutex was released, there is a
15035 			 * possibility that HBA list has changed, so start
15036 			 * over from the top. Just processed controller
15037 			 * will be passed-over because of the SKIP flag.
15038 			 */
15039 			goto loop;
15040 		}
15041 		mutex_exit(&sata_hba_inst->satahba_mutex);
15042 	}
15043 	/* Clear SKIP flag in all controllers */
15044 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
15045 	    sata_hba_inst = sata_hba_inst->satahba_next) {
15046 		mutex_enter(&sata_hba_inst->satahba_mutex);
15047 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
15048 		mutex_exit(&sata_hba_inst->satahba_mutex);
15049 	}
15050 	mutex_exit(&sata_mutex);
15051 
15052 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15053 	    "SATA EVENT DAEMON suspending itself", NULL);
15054 
15055 #ifdef SATA_DEBUG
15056 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
15057 		sata_log(sata_hba_inst, CE_WARN,
15058 		    "SATA EVENTS PROCESSING DISABLED\n");
15059 		thread_exit(); /* Daemon will not run again */
15060 	}
15061 #endif
15062 	mutex_enter(&sata_event_mutex);
15063 	sata_event_thread_active = 0;
15064 	mutex_exit(&sata_event_mutex);
15065 	/*
15066 	 * Go to sleep/suspend itself and wake up either because new event or
15067 	 * wait timeout. Exit if there is a termination request (driver
15068 	 * unload).
15069 	 */
15070 	do {
15071 		lbolt = ddi_get_lbolt();
15072 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
15073 		mutex_enter(&sata_event_mutex);
15074 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
15075 
15076 		if (sata_event_thread_active != 0) {
15077 			mutex_exit(&sata_event_mutex);
15078 			continue;
15079 		}
15080 
15081 		/* Check if it is time to go away */
15082 		if (sata_event_thread_terminate == 1) {
15083 			/*
15084 			 * It is up to the thread setting above flag to make
15085 			 * sure that this thread is not killed prematurely.
15086 			 */
15087 			sata_event_thread_terminate = 0;
15088 			sata_event_thread = NULL;
15089 			mutex_exit(&sata_event_mutex);
15090 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15091 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
15092 			thread_exit();  { _NOTE(NOT_REACHED) }
15093 		}
15094 		mutex_exit(&sata_event_mutex);
15095 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
15096 
15097 	mutex_enter(&sata_event_mutex);
15098 	sata_event_thread_active = 1;
15099 	mutex_exit(&sata_event_mutex);
15100 
15101 	mutex_enter(&sata_mutex);
15102 	sata_event_pending &= ~SATA_EVNT_MAIN;
15103 	mutex_exit(&sata_mutex);
15104 
15105 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15106 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
15107 
15108 	goto loop;
15109 }
15110 
15111 /*
15112  * Specific HBA instance event processing.
15113  *
15114  * NOTE: At the moment, device event processing is limited to hard disks
15115  * only.
15116  * cports only are supported - no pmports.
15117  */
15118 static void
15119 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
15120 {
15121 	int ncport;
15122 	uint32_t event_flags;
15123 	sata_address_t *saddr;
15124 	sata_cport_info_t *cportinfo;
15125 
15126 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
15127 	    "Processing controller %d event(s)",
15128 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
15129 
15130 	mutex_enter(&sata_hba_inst->satahba_mutex);
15131 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
15132 	event_flags = sata_hba_inst->satahba_event_flags;
15133 	mutex_exit(&sata_hba_inst->satahba_mutex);
15134 	/*
15135 	 * Process controller power change first
15136 	 * HERE
15137 	 */
15138 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
15139 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
15140 
15141 	/*
15142 	 * Search through ports/devices to identify affected port/device.
15143 	 * We may have to process events for more than one port/device.
15144 	 */
15145 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
15146 		/*
15147 		 * Not all ports may be processed in attach by the time we
15148 		 * get an event. Check if port info is initialized.
15149 		 */
15150 		mutex_enter(&sata_hba_inst->satahba_mutex);
15151 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
15152 		mutex_exit(&sata_hba_inst->satahba_mutex);
15153 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
15154 			continue;
15155 
15156 		/* We have initialized controller port info */
15157 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15158 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
15159 		    cport_event_flags;
15160 		/* Check if port was locked by IOCTL processing */
15161 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
15162 			/*
15163 			 * We ignore port events because port is busy
15164 			 * with AP control processing. Set again
15165 			 * controller and main event flag, so that
15166 			 * events may be processed by the next daemon
15167 			 * run.
15168 			 */
15169 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15170 			mutex_enter(&sata_hba_inst->satahba_mutex);
15171 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15172 			mutex_exit(&sata_hba_inst->satahba_mutex);
15173 			mutex_enter(&sata_mutex);
15174 			sata_event_pending |= SATA_EVNT_MAIN;
15175 			mutex_exit(&sata_mutex);
15176 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
15177 			    "Event processing postponed until "
15178 			    "AP control processing completes",
15179 			    NULL);
15180 			/* Check other ports */
15181 			continue;
15182 		} else {
15183 			/*
15184 			 * Set BSY flag so that AP control would not
15185 			 * interfere with events processing for
15186 			 * this port.
15187 			 */
15188 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
15189 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
15190 		}
15191 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15192 
15193 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
15194 
15195 		if ((event_flags &
15196 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
15197 			/*
15198 			 * Got port event.
15199 			 * We need some hierarchy of event processing as they
15200 			 * are affecting each other:
15201 			 * 1. port failed
15202 			 * 2. device detached/attached
15203 			 * 3. link events - link events may trigger device
15204 			 *    detached or device attached events in some
15205 			 *    circumstances.
15206 			 * 4. port power level changed
15207 			 */
15208 			if (event_flags & SATA_EVNT_PORT_FAILED) {
15209 				sata_process_port_failed_event(sata_hba_inst,
15210 				    saddr);
15211 			}
15212 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
15213 				sata_process_device_detached(sata_hba_inst,
15214 				    saddr);
15215 			}
15216 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
15217 				sata_process_device_attached(sata_hba_inst,
15218 				    saddr);
15219 			}
15220 			if (event_flags &
15221 			    (SATA_EVNT_LINK_ESTABLISHED |
15222 			    SATA_EVNT_LINK_LOST)) {
15223 				sata_process_port_link_events(sata_hba_inst,
15224 				    saddr);
15225 			}
15226 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
15227 				sata_process_port_pwr_change(sata_hba_inst,
15228 				    saddr);
15229 			}
15230 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
15231 				sata_process_target_node_cleanup(
15232 				    sata_hba_inst, saddr);
15233 			}
15234 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
15235 				sata_process_device_autoonline(
15236 				    sata_hba_inst, saddr);
15237 			}
15238 		}
15239 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15240 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
15241 		    SATA_DTYPE_NONE) &&
15242 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
15243 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
15244 			    satadrv_event_flags &
15245 			    (SATA_EVNT_DEVICE_RESET |
15246 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
15247 				/* Have device event */
15248 				sata_process_device_reset(sata_hba_inst,
15249 				    saddr);
15250 			}
15251 		}
15252 		/* Release PORT_BUSY flag */
15253 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
15254 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
15255 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15256 
15257 	} /* End of loop through the controller SATA ports */
15258 }
15259 
15260 /*
15261  * Process HBA power level change reported by HBA driver.
15262  * Not implemented at this time - event is ignored.
15263  */
15264 static void
15265 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
15266 {
15267 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15268 	    "Processing controller power level change", NULL);
15269 
15270 	/* Ignoring it for now */
15271 	mutex_enter(&sata_hba_inst->satahba_mutex);
15272 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
15273 	mutex_exit(&sata_hba_inst->satahba_mutex);
15274 }
15275 
15276 /*
15277  * Process port power level change reported by HBA driver.
15278  * Not implemented at this time - event is ignored.
15279  */
15280 static void
15281 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
15282     sata_address_t *saddr)
15283 {
15284 	sata_cport_info_t *cportinfo;
15285 
15286 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15287 	    "Processing port power level change", NULL);
15288 
15289 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15290 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15291 	/* Reset event flag */
15292 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
15293 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15294 }
15295 
15296 /*
15297  * Process port failure reported by HBA driver.
15298  * cports support only - no pmports.
15299  */
15300 static void
15301 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
15302     sata_address_t *saddr)
15303 {
15304 	sata_cport_info_t *cportinfo;
15305 
15306 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15307 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15308 	/* Reset event flag first */
15309 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
15310 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
15311 	if ((cportinfo->cport_state &
15312 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
15313 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15314 		    cport_mutex);
15315 		return;
15316 	}
15317 	/* Fail the port */
15318 	cportinfo->cport_state = SATA_PSTATE_FAILED;
15319 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15320 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
15321 }
15322 
15323 /*
15324  * Device Reset Event processing.
15325  * The seqeunce is managed by 3 stage flags:
15326  * - reset event reported,
15327  * - reset event being processed,
15328  * - request to clear device reset state.
15329  *
15330  * NOTE: This function has to be entered with cport mutex held. It exits with
15331  * mutex held as well, but can release mutex during the processing.
15332  */
15333 static void
15334 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
15335     sata_address_t *saddr)
15336 {
15337 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
15338 	sata_drive_info_t *sdinfo;
15339 	sata_cport_info_t *cportinfo;
15340 	sata_device_t sata_device;
15341 	int rval_probe, rval_set;
15342 
15343 	/* We only care about host sata cport for now */
15344 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15345 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15346 	/*
15347 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
15348 	 * state, ignore reset event.
15349 	 */
15350 	if (((cportinfo->cport_state &
15351 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
15352 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
15353 		sdinfo->satadrv_event_flags &=
15354 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
15355 		return;
15356 	}
15357 
15358 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
15359 	    SATA_VALID_DEV_TYPE) == 0) {
15360 		/*
15361 		 * This should not happen - coding error.
15362 		 * But we can recover, so do not panic, just clean up
15363 		 * and if in debug mode, log the message.
15364 		 */
15365 #ifdef SATA_DEBUG
15366 		sata_log(sata_hba_inst, CE_WARN,
15367 		    "sata_process_device_reset: "
15368 		    "Invalid device type with sdinfo!", NULL);
15369 #endif
15370 		sdinfo->satadrv_event_flags = 0;
15371 		return;
15372 	}
15373 
15374 #ifdef SATA_DEBUG
15375 	if ((sdinfo->satadrv_event_flags &
15376 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
15377 		/* Nothing to do */
15378 		/* Something is weird - why we are processing dev reset? */
15379 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15380 		    "No device reset event!!!!", NULL);
15381 
15382 		return;
15383 	}
15384 	if ((sdinfo->satadrv_event_flags &
15385 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
15386 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15387 		/* Something is weird - new device reset event */
15388 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15389 		    "Overlapping device reset events!", NULL);
15390 	}
15391 #endif
15392 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15393 	    "Processing port %d device reset", saddr->cport);
15394 
15395 	/* Clear event flag */
15396 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
15397 
15398 	/* It seems that we always need to check the port state first */
15399 	sata_device.satadev_rev = SATA_DEVICE_REV;
15400 	sata_device.satadev_addr = *saddr;
15401 	/*
15402 	 * We have to exit mutex, because the HBA probe port function may
15403 	 * block on its own mutex.
15404 	 */
15405 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15406 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15407 	    (SATA_DIP(sata_hba_inst), &sata_device);
15408 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15409 	sata_update_port_info(sata_hba_inst, &sata_device);
15410 	if (rval_probe != SATA_SUCCESS) {
15411 		/* Something went wrong? Fail the port */
15412 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15413 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15414 		if (sdinfo != NULL)
15415 			sdinfo->satadrv_event_flags = 0;
15416 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15417 		    cport_mutex);
15418 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15419 		    "SATA port %d probing failed",
15420 		    saddr->cport));
15421 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15422 		    saddr->cport)->cport_mutex);
15423 		return;
15424 	}
15425 	if ((sata_device.satadev_scr.sstatus  &
15426 	    SATA_PORT_DEVLINK_UP_MASK) !=
15427 	    SATA_PORT_DEVLINK_UP ||
15428 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
15429 		/*
15430 		 * No device to process, anymore. Some other event processing
15431 		 * would or have already performed port info cleanup.
15432 		 * To be safe (HBA may need it), request clearing device
15433 		 * reset condition.
15434 		 */
15435 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15436 		if (sdinfo != NULL) {
15437 			sdinfo->satadrv_event_flags &=
15438 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
15439 			sdinfo->satadrv_event_flags |=
15440 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15441 		}
15442 		return;
15443 	}
15444 
15445 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15446 	if (sdinfo == NULL) {
15447 		return;
15448 	}
15449 	if ((sdinfo->satadrv_event_flags &
15450 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
15451 		/*
15452 		 * Start tracking time for device feature restoration and
15453 		 * identification. Save current time (lbolt value).
15454 		 */
15455 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
15456 	}
15457 	/* Mark device reset processing as active */
15458 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
15459 
15460 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
15461 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15462 
15463 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
15464 
15465 	if (rval_set  != SATA_SUCCESS) {
15466 		/*
15467 		 * Restoring drive setting failed.
15468 		 * Probe the port first, to check if the port state has changed
15469 		 */
15470 		sata_device.satadev_rev = SATA_DEVICE_REV;
15471 		sata_device.satadev_addr = *saddr;
15472 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
15473 		/* probe port */
15474 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15475 		    (SATA_DIP(sata_hba_inst), &sata_device);
15476 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15477 		    cport_mutex);
15478 		if (rval_probe == SATA_SUCCESS &&
15479 		    (sata_device.satadev_state &
15480 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
15481 		    (sata_device.satadev_scr.sstatus  &
15482 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
15483 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
15484 			/*
15485 			 * We may retry this a bit later - in-process reset
15486 			 * condition should be already set.
15487 			 * Track retry time for device identification.
15488 			 */
15489 			if ((cportinfo->cport_dev_type &
15490 			    SATA_VALID_DEV_TYPE) != 0 &&
15491 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
15492 			    sdinfo->satadrv_reset_time != 0) {
15493 				clock_t cur_time = ddi_get_lbolt();
15494 				/*
15495 				 * If the retry time limit was not
15496 				 * exceeded, retry.
15497 				 */
15498 				if ((cur_time - sdinfo->satadrv_reset_time) <
15499 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
15500 					mutex_enter(
15501 					    &sata_hba_inst->satahba_mutex);
15502 					sata_hba_inst->satahba_event_flags |=
15503 					    SATA_EVNT_MAIN;
15504 					mutex_exit(
15505 					    &sata_hba_inst->satahba_mutex);
15506 					mutex_enter(&sata_mutex);
15507 					sata_event_pending |= SATA_EVNT_MAIN;
15508 					mutex_exit(&sata_mutex);
15509 					return;
15510 				}
15511 				if (rval_set == SATA_RETRY) {
15512 					/*
15513 					 * Setting drive features failed, but
15514 					 * the drive is still accessible,
15515 					 * so emit a warning message before
15516 					 * return.
15517 					 */
15518 					mutex_exit(&SATA_CPORT_INFO(
15519 					    sata_hba_inst,
15520 					    saddr->cport)->cport_mutex);
15521 					goto done;
15522 				}
15523 			}
15524 			/* Fail the drive */
15525 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
15526 
15527 			sata_log(sata_hba_inst, CE_WARN,
15528 			    "SATA device at port %d - device failed",
15529 			    saddr->cport);
15530 		}
15531 		/*
15532 		 * No point of retrying - device failed or some other event
15533 		 * processing or already did or will do port info cleanup.
15534 		 * To be safe (HBA may need it),
15535 		 * request clearing device reset condition.
15536 		 */
15537 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15538 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
15539 		sdinfo->satadrv_reset_time = 0;
15540 		return;
15541 	}
15542 done:
15543 	/*
15544 	 * If setting of drive features failed, but the drive is still
15545 	 * accessible, emit a warning message.
15546 	 */
15547 	if (rval_set == SATA_RETRY) {
15548 		sata_log(sata_hba_inst, CE_WARN,
15549 		    "SATA device at port %d - desired setting could not be "
15550 		    "restored after reset. Device may not operate as expected.",
15551 		    saddr->cport);
15552 	}
15553 	/*
15554 	 * Raise the flag indicating that the next sata command could
15555 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
15556 	 * reset is reported.
15557 	 */
15558 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15559 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15560 		sdinfo->satadrv_reset_time = 0;
15561 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
15562 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15563 			sdinfo->satadrv_event_flags &=
15564 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
15565 			sdinfo->satadrv_event_flags |=
15566 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15567 		}
15568 	}
15569 }
15570 
15571 
15572 /*
15573  * Port Link Events processing.
15574  * Every link established event may involve device reset (due to
15575  * COMRESET signal, equivalent of the hard reset) so arbitrarily
15576  * set device reset event for an attached device (if any).
15577  * If the port is in SHUTDOWN or FAILED state, ignore link events.
15578  *
15579  * The link established event processing varies, depending on the state
15580  * of the target node, HBA hotplugging capabilities, state of the port.
15581  * If the link is not active, the link established event is ignored.
15582  * If HBA cannot detect device attachment and there is no target node,
15583  * the link established event triggers device attach event processing.
15584  * Else, link established event triggers device reset event processing.
15585  *
15586  * The link lost event processing varies, depending on a HBA hotplugging
15587  * capability and the state of the port (link active or not active).
15588  * If the link is active, the lost link event is ignored.
15589  * If HBA cannot detect device removal, the lost link event triggers
15590  * device detached event processing after link lost timeout.
15591  * Else, the event is ignored.
15592  *
15593  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
15594  */
15595 static void
15596 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
15597     sata_address_t *saddr)
15598 {
15599 	sata_device_t sata_device;
15600 	sata_cport_info_t *cportinfo;
15601 	sata_drive_info_t *sdinfo;
15602 	uint32_t event_flags;
15603 	int rval;
15604 
15605 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15606 	    "Processing port %d link event(s)", saddr->cport);
15607 
15608 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15609 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15610 	event_flags = cportinfo->cport_event_flags;
15611 
15612 	/* Reset event flags first */
15613 	cportinfo->cport_event_flags &=
15614 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
15615 
15616 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
15617 	if ((cportinfo->cport_state &
15618 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15619 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15620 		    cport_mutex);
15621 		return;
15622 	}
15623 
15624 	/*
15625 	 * For the sanity sake get current port state.
15626 	 * Set device address only. Other sata_device fields should be
15627 	 * set by HBA driver.
15628 	 */
15629 	sata_device.satadev_rev = SATA_DEVICE_REV;
15630 	sata_device.satadev_addr = *saddr;
15631 	/*
15632 	 * We have to exit mutex, because the HBA probe port function may
15633 	 * block on its own mutex.
15634 	 */
15635 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15636 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15637 	    (SATA_DIP(sata_hba_inst), &sata_device);
15638 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15639 	sata_update_port_info(sata_hba_inst, &sata_device);
15640 	if (rval != SATA_SUCCESS) {
15641 		/* Something went wrong? Fail the port */
15642 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15643 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15644 		    cport_mutex);
15645 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15646 		    "SATA port %d probing failed",
15647 		    saddr->cport));
15648 		/*
15649 		 * We may want to release device info structure, but
15650 		 * it is not necessary.
15651 		 */
15652 		return;
15653 	} else {
15654 		/* port probed successfully */
15655 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15656 	}
15657 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
15658 
15659 		if ((sata_device.satadev_scr.sstatus &
15660 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
15661 			/* Ignore event */
15662 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15663 			    "Ignoring port %d link established event - "
15664 			    "link down",
15665 			    saddr->cport);
15666 			goto linklost;
15667 		}
15668 
15669 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15670 		    "Processing port %d link established event",
15671 		    saddr->cport);
15672 
15673 		/*
15674 		 * For the sanity sake check if a device is attached - check
15675 		 * return state of a port probing.
15676 		 */
15677 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
15678 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
15679 			/*
15680 			 * HBA port probe indicated that there is a device
15681 			 * attached. Check if the framework had device info
15682 			 * structure attached for this device.
15683 			 */
15684 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15685 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
15686 				    NULL);
15687 
15688 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15689 				if ((sdinfo->satadrv_type &
15690 				    SATA_VALID_DEV_TYPE) != 0) {
15691 					/*
15692 					 * Dev info structure is present.
15693 					 * If dev_type is set to known type in
15694 					 * the framework's drive info struct
15695 					 * then the device existed before and
15696 					 * the link was probably lost
15697 					 * momentarily - in such case
15698 					 * we may want to check device
15699 					 * identity.
15700 					 * Identity check is not supported now.
15701 					 *
15702 					 * Link established event
15703 					 * triggers device reset event.
15704 					 */
15705 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
15706 					    satadrv_event_flags |=
15707 					    SATA_EVNT_DEVICE_RESET;
15708 				}
15709 			} else if (cportinfo->cport_dev_type ==
15710 			    SATA_DTYPE_NONE) {
15711 				/*
15712 				 * We got new device attached! If HBA does not
15713 				 * generate device attached events, trigger it
15714 				 * here.
15715 				 */
15716 				if (!(SATA_FEATURES(sata_hba_inst) &
15717 				    SATA_CTLF_HOTPLUG)) {
15718 					cportinfo->cport_event_flags |=
15719 					    SATA_EVNT_DEVICE_ATTACHED;
15720 				}
15721 			}
15722 			/* Reset link lost timeout */
15723 			cportinfo->cport_link_lost_time = 0;
15724 		}
15725 	}
15726 linklost:
15727 	if (event_flags & SATA_EVNT_LINK_LOST) {
15728 		if ((sata_device.satadev_scr.sstatus &
15729 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
15730 			/* Ignore event */
15731 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15732 			    "Ignoring port %d link lost event - link is up",
15733 			    saddr->cport);
15734 			goto done;
15735 		}
15736 #ifdef SATA_DEBUG
15737 		if (cportinfo->cport_link_lost_time == 0) {
15738 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15739 			    "Processing port %d link lost event",
15740 			    saddr->cport);
15741 		}
15742 #endif
15743 		/*
15744 		 * When HBA cannot generate device attached/detached events,
15745 		 * we need to track link lost time and eventually generate
15746 		 * device detach event.
15747 		 */
15748 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
15749 			/* We are tracking link lost time */
15750 			if (cportinfo->cport_link_lost_time == 0) {
15751 				/* save current time (lbolt value) */
15752 				cportinfo->cport_link_lost_time =
15753 				    ddi_get_lbolt();
15754 				/* just keep link lost event */
15755 				cportinfo->cport_event_flags |=
15756 				    SATA_EVNT_LINK_LOST;
15757 			} else {
15758 				clock_t cur_time = ddi_get_lbolt();
15759 				if ((cur_time -
15760 				    cportinfo->cport_link_lost_time) >=
15761 				    drv_usectohz(
15762 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
15763 					/* trigger device detach event */
15764 					cportinfo->cport_event_flags |=
15765 					    SATA_EVNT_DEVICE_DETACHED;
15766 					cportinfo->cport_link_lost_time = 0;
15767 					SATADBG1(SATA_DBG_EVENTS,
15768 					    sata_hba_inst,
15769 					    "Triggering port %d "
15770 					    "device detached event",
15771 					    saddr->cport);
15772 				} else {
15773 					/* keep link lost event */
15774 					cportinfo->cport_event_flags |=
15775 					    SATA_EVNT_LINK_LOST;
15776 				}
15777 			}
15778 		}
15779 		/*
15780 		 * We could change port state to disable/delay access to
15781 		 * the attached device until the link is recovered.
15782 		 */
15783 	}
15784 done:
15785 	event_flags = cportinfo->cport_event_flags;
15786 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15787 	if (event_flags != 0) {
15788 		mutex_enter(&sata_hba_inst->satahba_mutex);
15789 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15790 		mutex_exit(&sata_hba_inst->satahba_mutex);
15791 		mutex_enter(&sata_mutex);
15792 		sata_event_pending |= SATA_EVNT_MAIN;
15793 		mutex_exit(&sata_mutex);
15794 	}
15795 }
15796 
15797 /*
15798  * Device Detached Event processing.
15799  * Port is probed to find if a device is really gone. If so,
15800  * the device info structure is detached from the SATA port info structure
15801  * and released.
15802  * Port status is updated.
15803  *
15804  * NOTE: Process cports event only, no port multiplier ports.
15805  */
15806 static void
15807 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
15808     sata_address_t *saddr)
15809 {
15810 	sata_cport_info_t *cportinfo;
15811 	sata_drive_info_t *sdevinfo;
15812 	sata_device_t sata_device;
15813 	dev_info_t *tdip;
15814 	int rval;
15815 
15816 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15817 	    "Processing port %d device detached", saddr->cport);
15818 
15819 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15820 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15821 	/* Clear event flag */
15822 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
15823 
15824 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
15825 	if ((cportinfo->cport_state &
15826 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15827 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15828 		    cport_mutex);
15829 		return;
15830 	}
15831 	/* For sanity, re-probe the port */
15832 	sata_device.satadev_rev = SATA_DEVICE_REV;
15833 	sata_device.satadev_addr = *saddr;
15834 
15835 	/*
15836 	 * We have to exit mutex, because the HBA probe port function may
15837 	 * block on its own mutex.
15838 	 */
15839 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15840 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15841 	    (SATA_DIP(sata_hba_inst), &sata_device);
15842 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15843 	sata_update_port_info(sata_hba_inst, &sata_device);
15844 	if (rval != SATA_SUCCESS) {
15845 		/* Something went wrong? Fail the port */
15846 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15847 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15848 		    cport_mutex);
15849 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15850 		    "SATA port %d probing failed",
15851 		    saddr->cport));
15852 		/*
15853 		 * We may want to release device info structure, but
15854 		 * it is not necessary.
15855 		 */
15856 		return;
15857 	} else {
15858 		/* port probed successfully */
15859 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15860 	}
15861 	/*
15862 	 * Check if a device is still attached. For sanity, check also
15863 	 * link status - if no link, there is no device.
15864 	 */
15865 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
15866 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
15867 	    SATA_DTYPE_NONE) {
15868 		/*
15869 		 * Device is still attached - ignore detach event.
15870 		 */
15871 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15872 		    cport_mutex);
15873 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15874 		    "Ignoring detach - device still attached to port %d",
15875 		    sata_device.satadev_addr.cport);
15876 		return;
15877 	}
15878 	/*
15879 	 * We need to detach and release device info structure here
15880 	 */
15881 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15882 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15883 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15884 		(void) kmem_free((void *)sdevinfo,
15885 		    sizeof (sata_drive_info_t));
15886 	}
15887 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15888 	/*
15889 	 * Device cannot be reached anymore, even if the target node may be
15890 	 * still present.
15891 	 */
15892 
15893 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15894 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
15895 	    sata_device.satadev_addr.cport);
15896 
15897 	/*
15898 	 * Try to offline a device and remove target node if it still exists
15899 	 */
15900 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15901 	if (tdip != NULL) {
15902 		/*
15903 		 * Target node exists.  Unconfigure device then remove
15904 		 * the target node (one ndi operation).
15905 		 */
15906 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
15907 			/*
15908 			 * PROBLEM - no device, but target node remained
15909 			 * This happens when the file was open or node was
15910 			 * waiting for resources.
15911 			 */
15912 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15913 			    "sata_process_device_detached: "
15914 			    "Failed to remove target node for "
15915 			    "detached SATA device."));
15916 			/*
15917 			 * Set target node state to DEVI_DEVICE_REMOVED.
15918 			 * But re-check first that the node still exists.
15919 			 */
15920 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15921 			    saddr->cport);
15922 			if (tdip != NULL) {
15923 				sata_set_device_removed(tdip);
15924 				/*
15925 				 * Instruct event daemon to retry the
15926 				 * cleanup later.
15927 				 */
15928 				sata_set_target_node_cleanup(sata_hba_inst,
15929 				    &sata_device.satadev_addr);
15930 			}
15931 		}
15932 	}
15933 	/*
15934 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15935 	 * with the hint: SE_HINT_REMOVE
15936 	 */
15937 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
15938 }
15939 
15940 
15941 /*
15942  * Device Attached Event processing.
15943  * Port state is checked to verify that a device is really attached. If so,
15944  * the device info structure is created and attached to the SATA port info
15945  * structure.
15946  *
15947  * If attached device cannot be identified or set-up, the retry for the
15948  * attach processing is set-up. Subsequent daemon run would try again to
15949  * identify the device, until the time limit is reached
15950  * (SATA_DEV_IDENTIFY_TIMEOUT).
15951  *
15952  * This function cannot be called in interrupt context (it may sleep).
15953  *
15954  * NOTE: Process cports event only, no port multiplier ports.
15955  */
15956 static void
15957 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
15958     sata_address_t *saddr)
15959 {
15960 	sata_cport_info_t *cportinfo;
15961 	sata_drive_info_t *sdevinfo;
15962 	sata_device_t sata_device;
15963 	dev_info_t *tdip;
15964 	uint32_t event_flags;
15965 	int rval;
15966 
15967 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15968 	    "Processing port %d device attached", saddr->cport);
15969 
15970 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15971 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15972 
15973 	/* Clear attach event flag first */
15974 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
15975 
15976 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
15977 	if ((cportinfo->cport_state &
15978 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15979 		cportinfo->cport_dev_attach_time = 0;
15980 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15981 		    cport_mutex);
15982 		return;
15983 	}
15984 
15985 	/*
15986 	 * If the sata_drive_info structure is found attached to the port info,
15987 	 * despite the fact the device was removed and now it is re-attached,
15988 	 * the old drive info structure was not removed.
15989 	 * Arbitrarily release device info structure.
15990 	 */
15991 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15992 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15993 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15994 		(void) kmem_free((void *)sdevinfo,
15995 		    sizeof (sata_drive_info_t));
15996 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15997 		    "Arbitrarily detaching old device info.", NULL);
15998 	}
15999 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
16000 
16001 	/* For sanity, re-probe the port */
16002 	sata_device.satadev_rev = SATA_DEVICE_REV;
16003 	sata_device.satadev_addr = *saddr;
16004 
16005 	/*
16006 	 * We have to exit mutex, because the HBA probe port function may
16007 	 * block on its own mutex.
16008 	 */
16009 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16010 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
16011 	    (SATA_DIP(sata_hba_inst), &sata_device);
16012 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16013 	sata_update_port_info(sata_hba_inst, &sata_device);
16014 	if (rval != SATA_SUCCESS) {
16015 		/* Something went wrong? Fail the port */
16016 		cportinfo->cport_state = SATA_PSTATE_FAILED;
16017 		cportinfo->cport_dev_attach_time = 0;
16018 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16019 		    cport_mutex);
16020 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16021 		    "SATA port %d probing failed",
16022 		    saddr->cport));
16023 		return;
16024 	} else {
16025 		/* port probed successfully */
16026 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
16027 	}
16028 	/*
16029 	 * Check if a device is still attached. For sanity, check also
16030 	 * link status - if no link, there is no device.
16031 	 */
16032 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
16033 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
16034 	    SATA_DTYPE_NONE) {
16035 		/*
16036 		 * No device - ignore attach event.
16037 		 */
16038 		cportinfo->cport_dev_attach_time = 0;
16039 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16040 		    cport_mutex);
16041 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16042 		    "Ignoring attach - no device connected to port %d",
16043 		    sata_device.satadev_addr.cport);
16044 		return;
16045 	}
16046 
16047 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16048 	/*
16049 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
16050 	 * with the hint: SE_HINT_INSERT
16051 	 */
16052 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
16053 
16054 	/*
16055 	 * Port reprobing will take care of the creation of the device
16056 	 * info structure and determination of the device type.
16057 	 */
16058 	sata_device.satadev_addr = *saddr;
16059 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
16060 	    SATA_DEV_IDENTIFY_NORETRY);
16061 
16062 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16063 	    cport_mutex);
16064 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
16065 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
16066 		/* Some device is attached to the port */
16067 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
16068 			/*
16069 			 * A device was not successfully attached.
16070 			 * Track retry time for device identification.
16071 			 */
16072 			if (cportinfo->cport_dev_attach_time != 0) {
16073 				clock_t cur_time = ddi_get_lbolt();
16074 				/*
16075 				 * If the retry time limit was not exceeded,
16076 				 * reinstate attach event.
16077 				 */
16078 				if ((cur_time -
16079 				    cportinfo->cport_dev_attach_time) <
16080 				    drv_usectohz(
16081 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
16082 					/* OK, restore attach event */
16083 					cportinfo->cport_event_flags |=
16084 					    SATA_EVNT_DEVICE_ATTACHED;
16085 				} else {
16086 					/* Timeout - cannot identify device */
16087 					cportinfo->cport_dev_attach_time = 0;
16088 					sata_log(sata_hba_inst,
16089 					    CE_WARN,
16090 					    "Could not identify SATA device "
16091 					    "at port %d",
16092 					    saddr->cport);
16093 				}
16094 			} else {
16095 				/*
16096 				 * Start tracking time for device
16097 				 * identification.
16098 				 * Save current time (lbolt value).
16099 				 */
16100 				cportinfo->cport_dev_attach_time =
16101 				    ddi_get_lbolt();
16102 				/* Restore attach event */
16103 				cportinfo->cport_event_flags |=
16104 				    SATA_EVNT_DEVICE_ATTACHED;
16105 			}
16106 		} else {
16107 			/*
16108 			 * If device was successfully attached, the subsequent
16109 			 * action depends on a state of the
16110 			 * sata_auto_online variable. If it is set to zero.
16111 			 * an explicit 'configure' command will be needed to
16112 			 * configure it. If its value is non-zero, we will
16113 			 * attempt to online (configure) the device.
16114 			 * First, log the message indicating that a device
16115 			 * was attached.
16116 			 */
16117 			cportinfo->cport_dev_attach_time = 0;
16118 			sata_log(sata_hba_inst, CE_WARN,
16119 			    "SATA device detected at port %d", saddr->cport);
16120 
16121 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
16122 				sata_drive_info_t new_sdinfo;
16123 
16124 				/* Log device info data */
16125 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
16126 				    cportinfo));
16127 				sata_show_drive_info(sata_hba_inst,
16128 				    &new_sdinfo);
16129 			}
16130 
16131 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16132 			    saddr->cport)->cport_mutex);
16133 
16134 			/*
16135 			 * Make sure that there is no target node for that
16136 			 * device. If so, release it. It should not happen,
16137 			 * unless we had problem removing the node when
16138 			 * device was detached.
16139 			 */
16140 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
16141 			    saddr->cport);
16142 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16143 			    saddr->cport)->cport_mutex);
16144 			if (tdip != NULL) {
16145 
16146 #ifdef SATA_DEBUG
16147 				if ((cportinfo->cport_event_flags &
16148 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
16149 					sata_log(sata_hba_inst, CE_WARN,
16150 					    "sata_process_device_attached: "
16151 					    "old device target node exists!");
16152 #endif
16153 				/*
16154 				 * target node exists - try to unconfigure
16155 				 * device and remove the node.
16156 				 */
16157 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16158 				    saddr->cport)->cport_mutex);
16159 				rval = ndi_devi_offline(tdip,
16160 				    NDI_DEVI_REMOVE);
16161 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16162 				    saddr->cport)->cport_mutex);
16163 
16164 				if (rval == NDI_SUCCESS) {
16165 					cportinfo->cport_event_flags &=
16166 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16167 					cportinfo->cport_tgtnode_clean = B_TRUE;
16168 				} else {
16169 					/*
16170 					 * PROBLEM - the target node remained
16171 					 * and it belongs to a previously
16172 					 * attached device.
16173 					 * This happens when the file was open
16174 					 * or the node was waiting for
16175 					 * resources at the time the
16176 					 * associated device was removed.
16177 					 * Instruct event daemon to retry the
16178 					 * cleanup later.
16179 					 */
16180 					sata_log(sata_hba_inst,
16181 					    CE_WARN,
16182 					    "Application(s) accessing "
16183 					    "previously attached SATA "
16184 					    "device have to release "
16185 					    "it before newly inserted "
16186 					    "device can be made accessible.",
16187 					    saddr->cport);
16188 					cportinfo->cport_event_flags |=
16189 					    SATA_EVNT_TARGET_NODE_CLEANUP;
16190 					cportinfo->cport_tgtnode_clean =
16191 					    B_FALSE;
16192 				}
16193 			}
16194 			if (sata_auto_online != 0) {
16195 				cportinfo->cport_event_flags |=
16196 				    SATA_EVNT_AUTOONLINE_DEVICE;
16197 			}
16198 
16199 		}
16200 	} else {
16201 		cportinfo->cport_dev_attach_time = 0;
16202 	}
16203 
16204 	event_flags = cportinfo->cport_event_flags;
16205 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16206 	if (event_flags != 0) {
16207 		mutex_enter(&sata_hba_inst->satahba_mutex);
16208 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16209 		mutex_exit(&sata_hba_inst->satahba_mutex);
16210 		mutex_enter(&sata_mutex);
16211 		sata_event_pending |= SATA_EVNT_MAIN;
16212 		mutex_exit(&sata_mutex);
16213 	}
16214 }
16215 
16216 
16217 /*
16218  * Device Target Node Cleanup Event processing.
16219  * If the target node associated with a sata port device is in
16220  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
16221  * If the target node cannot be removed, the event flag is left intact,
16222  * so that event daemon may re-run this function later.
16223  *
16224  * This function cannot be called in interrupt context (it may sleep).
16225  *
16226  * NOTE: Processes cport events only, not port multiplier ports.
16227  */
16228 static void
16229 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
16230     sata_address_t *saddr)
16231 {
16232 	sata_cport_info_t *cportinfo;
16233 	dev_info_t *tdip;
16234 
16235 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16236 	    "Processing port %d device target node cleanup", saddr->cport);
16237 
16238 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
16239 
16240 	/*
16241 	 * Check if there is target node for that device and it is in the
16242 	 * DEVI_DEVICE_REMOVED state. If so, release it.
16243 	 */
16244 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
16245 	if (tdip != NULL) {
16246 		/*
16247 		 * target node exists - check if it is target node of
16248 		 * a removed device.
16249 		 */
16250 		if (sata_check_device_removed(tdip) == B_TRUE) {
16251 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16252 			    "sata_process_target_node_cleanup: "
16253 			    "old device target node exists!", NULL);
16254 			/*
16255 			 * Unconfigure and remove the target node
16256 			 */
16257 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
16258 			    NDI_SUCCESS) {
16259 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16260 				    saddr->cport)->cport_mutex);
16261 				cportinfo->cport_event_flags &=
16262 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16263 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16264 				    saddr->cport)->cport_mutex);
16265 				return;
16266 			}
16267 			/*
16268 			 * Event daemon will retry the cleanup later.
16269 			 */
16270 			mutex_enter(&sata_hba_inst->satahba_mutex);
16271 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16272 			mutex_exit(&sata_hba_inst->satahba_mutex);
16273 			mutex_enter(&sata_mutex);
16274 			sata_event_pending |= SATA_EVNT_MAIN;
16275 			mutex_exit(&sata_mutex);
16276 		}
16277 	} else {
16278 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16279 		    saddr->cport)->cport_mutex);
16280 		cportinfo->cport_event_flags &=
16281 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16282 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16283 		    saddr->cport)->cport_mutex);
16284 	}
16285 }
16286 
16287 /*
16288  * Device AutoOnline Event processing.
16289  * If attached device is to be onlined, an attempt is made to online this
16290  * device, but only if there is no lingering (old) target node present.
16291  * If the device cannot be onlined, the event flag is left intact,
16292  * so that event daemon may re-run this function later.
16293  *
16294  * This function cannot be called in interrupt context (it may sleep).
16295  *
16296  * NOTE: Processes cport events only, not port multiplier ports.
16297  */
16298 static void
16299 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
16300     sata_address_t *saddr)
16301 {
16302 	sata_cport_info_t *cportinfo;
16303 	sata_drive_info_t *sdinfo;
16304 	sata_device_t sata_device;
16305 	dev_info_t *tdip;
16306 
16307 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16308 	    "Processing port %d attached device auto-onlining", saddr->cport);
16309 
16310 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
16311 
16312 	/*
16313 	 * Check if device is present and recognized. If not, reset event.
16314 	 */
16315 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16316 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
16317 		/* Nothing to online */
16318 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
16319 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16320 		    saddr->cport)->cport_mutex);
16321 		return;
16322 	}
16323 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16324 
16325 	/*
16326 	 * Check if there is target node for this device and if it is in the
16327 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
16328 	 * the event for later processing.
16329 	 */
16330 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
16331 	if (tdip != NULL) {
16332 		/*
16333 		 * target node exists - check if it is target node of
16334 		 * a removed device.
16335 		 */
16336 		if (sata_check_device_removed(tdip) == B_TRUE) {
16337 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16338 			    "sata_process_device_autoonline: "
16339 			    "old device target node exists!", NULL);
16340 			/*
16341 			 * Event daemon will retry device onlining later.
16342 			 */
16343 			mutex_enter(&sata_hba_inst->satahba_mutex);
16344 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16345 			mutex_exit(&sata_hba_inst->satahba_mutex);
16346 			mutex_enter(&sata_mutex);
16347 			sata_event_pending |= SATA_EVNT_MAIN;
16348 			mutex_exit(&sata_mutex);
16349 			return;
16350 		}
16351 		/*
16352 		 * If the target node is not in the 'removed" state, assume
16353 		 * that it belongs to this device. There is nothing more to do,
16354 		 * but reset the event.
16355 		 */
16356 	} else {
16357 
16358 		/*
16359 		 * Try to online the device
16360 		 * If there is any reset-related event, remove it. We are
16361 		 * configuring the device and no state restoring is needed.
16362 		 */
16363 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16364 		    saddr->cport)->cport_mutex);
16365 		sata_device.satadev_addr = *saddr;
16366 		if (saddr->qual == SATA_ADDR_CPORT)
16367 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
16368 		else
16369 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
16370 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
16371 		if (sdinfo != NULL) {
16372 			if (sdinfo->satadrv_event_flags &
16373 			    (SATA_EVNT_DEVICE_RESET |
16374 			    SATA_EVNT_INPROC_DEVICE_RESET))
16375 				sdinfo->satadrv_event_flags = 0;
16376 			sdinfo->satadrv_event_flags |=
16377 			    SATA_EVNT_CLEAR_DEVICE_RESET;
16378 
16379 			/* Need to create a new target node. */
16380 			cportinfo->cport_tgtnode_clean = B_TRUE;
16381 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16382 			    saddr->cport)->cport_mutex);
16383 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
16384 			    sata_hba_inst, &sata_device.satadev_addr);
16385 			if (tdip == NULL) {
16386 				/*
16387 				 * Configure (onlining) failed.
16388 				 * We will NOT retry
16389 				 */
16390 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16391 				    "sata_process_device_autoonline: "
16392 				    "configuring SATA device at port %d failed",
16393 				    saddr->cport));
16394 			}
16395 		} else {
16396 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16397 			    saddr->cport)->cport_mutex);
16398 		}
16399 
16400 	}
16401 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16402 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
16403 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16404 	    saddr->cport)->cport_mutex);
16405 }
16406 
16407 
16408 static void
16409 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
16410     int hint)
16411 {
16412 	char ap[MAXPATHLEN];
16413 	nvlist_t *ev_attr_list = NULL;
16414 	int err;
16415 
16416 	/* Allocate and build sysevent attribute list */
16417 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
16418 	if (err != 0) {
16419 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16420 		    "sata_gen_sysevent: "
16421 		    "cannot allocate memory for sysevent attributes\n"));
16422 		return;
16423 	}
16424 	/* Add hint attribute */
16425 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
16426 	if (err != 0) {
16427 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16428 		    "sata_gen_sysevent: "
16429 		    "failed to add DR_HINT attr for sysevent"));
16430 		nvlist_free(ev_attr_list);
16431 		return;
16432 	}
16433 	/*
16434 	 * Add AP attribute.
16435 	 * Get controller pathname and convert it into AP pathname by adding
16436 	 * a target number.
16437 	 */
16438 	(void) snprintf(ap, MAXPATHLEN, "/devices");
16439 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
16440 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
16441 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
16442 
16443 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
16444 	if (err != 0) {
16445 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16446 		    "sata_gen_sysevent: "
16447 		    "failed to add DR_AP_ID attr for sysevent"));
16448 		nvlist_free(ev_attr_list);
16449 		return;
16450 	}
16451 
16452 	/* Generate/log sysevent */
16453 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
16454 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
16455 	if (err != DDI_SUCCESS) {
16456 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16457 		    "sata_gen_sysevent: "
16458 		    "cannot log sysevent, err code %x\n", err));
16459 	}
16460 
16461 	nvlist_free(ev_attr_list);
16462 }
16463 
16464 
16465 
16466 
16467 /*
16468  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
16469  */
16470 static void
16471 sata_set_device_removed(dev_info_t *tdip)
16472 {
16473 	int circ;
16474 
16475 	ASSERT(tdip != NULL);
16476 
16477 	ndi_devi_enter(tdip, &circ);
16478 	mutex_enter(&DEVI(tdip)->devi_lock);
16479 	DEVI_SET_DEVICE_REMOVED(tdip);
16480 	mutex_exit(&DEVI(tdip)->devi_lock);
16481 	ndi_devi_exit(tdip, circ);
16482 }
16483 
16484 
16485 /*
16486  * Set internal event instructing event daemon to try
16487  * to perform the target node cleanup.
16488  */
16489 static void
16490 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
16491     sata_address_t *saddr)
16492 {
16493 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16494 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
16495 	    SATA_EVNT_TARGET_NODE_CLEANUP;
16496 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
16497 	    B_FALSE;
16498 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16499 	mutex_enter(&sata_hba_inst->satahba_mutex);
16500 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16501 	mutex_exit(&sata_hba_inst->satahba_mutex);
16502 	mutex_enter(&sata_mutex);
16503 	sata_event_pending |= SATA_EVNT_MAIN;
16504 	mutex_exit(&sata_mutex);
16505 }
16506 
16507 
16508 /*
16509  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
16510  * i.e. check if the target node state indicates that it belongs to a removed
16511  * device.
16512  *
16513  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
16514  * B_FALSE otherwise.
16515  *
16516  * NOTE: No port multiplier support.
16517  */
16518 static boolean_t
16519 sata_check_device_removed(dev_info_t *tdip)
16520 {
16521 	ASSERT(tdip != NULL);
16522 
16523 	if (DEVI_IS_DEVICE_REMOVED(tdip))
16524 		return (B_TRUE);
16525 	else
16526 		return (B_FALSE);
16527 }
16528 
16529 /* ************************ FAULT INJECTTION **************************** */
16530 
16531 #ifdef SATA_INJECT_FAULTS
16532 
16533 static	uint32_t sata_fault_count = 0;
16534 static	uint32_t sata_fault_suspend_count = 0;
16535 
16536 /*
16537  * Inject sata pkt fault
16538  * It modifies returned values of the sata packet.
16539  * It returns immediately if:
16540  * pkt fault injection is not enabled (via sata_inject_fault,
16541  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
16542  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
16543  * pkt is not directed to specified fault controller/device
16544  * (sata_fault_ctrl_dev and sata_fault_device).
16545  * If fault controller is not specified, fault injection applies to all
16546  * controllers and devices.
16547  *
16548  * First argument is the pointer to the executed sata packet.
16549  * Second argument is a pointer to a value returned by the HBA tran_start
16550  * function.
16551  * Third argument specifies injected error. Injected sata packet faults
16552  * are the satapkt_reason values.
16553  * SATA_PKT_BUSY		-1	Not completed, busy
16554  * SATA_PKT_DEV_ERROR		1	Device reported error
16555  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
16556  * SATA_PKT_PORT_ERROR		3	Not completed, port error
16557  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
16558  * SATA_PKT_ABORTED		5	Aborted by request
16559  * SATA_PKT_TIMEOUT		6	Operation timeut
16560  * SATA_PKT_RESET		7	Aborted by reset request
16561  *
16562  * Additional global variables affecting the execution:
16563  *
16564  * sata_inject_fault_count variable specifies number of times in row the
16565  * error is injected. Value of -1 specifies permanent fault, ie. every time
16566  * the fault injection point is reached, the fault is injected and a pause
16567  * between fault injection specified by sata_inject_fault_pause_count is
16568  * ignored). Fault injection routine decrements sata_inject_fault_count
16569  * (if greater than zero) until it reaches 0. No fault is injected when
16570  * sata_inject_fault_count is 0 (zero).
16571  *
16572  * sata_inject_fault_pause_count variable specifies number of times a fault
16573  * injection is bypassed (pause between fault injections).
16574  * If set to 0, a fault is injected only a number of times specified by
16575  * sata_inject_fault_count.
16576  *
16577  * The fault counts are static, so for periodic errors they have to be manually
16578  * reset to start repetition sequence from scratch.
16579  * If the original value returned by the HBA tran_start function is not
16580  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
16581  * is injected (to avoid masking real problems);
16582  *
16583  * NOTE: In its current incarnation, this function should be invoked only for
16584  * commands executed in SYNCHRONOUS mode.
16585  */
16586 
16587 
16588 static void
16589 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
16590 {
16591 
16592 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
16593 		return;
16594 
16595 	if (sata_inject_fault_count == 0)
16596 		return;
16597 
16598 	if (fault == 0)
16599 		return;
16600 
16601 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
16602 		return;
16603 
16604 	if (sata_fault_ctrl != NULL) {
16605 		sata_pkt_txlate_t *spx =
16606 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
16607 
16608 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
16609 		    spx->txlt_sata_hba_inst->satahba_dip)
16610 			return;
16611 
16612 		if (sata_fault_device.satadev_addr.cport !=
16613 		    spkt->satapkt_device.satadev_addr.cport ||
16614 		    sata_fault_device.satadev_addr.pmport !=
16615 		    spkt->satapkt_device.satadev_addr.pmport ||
16616 		    sata_fault_device.satadev_addr.qual !=
16617 		    spkt->satapkt_device.satadev_addr.qual)
16618 			return;
16619 	}
16620 
16621 	/* Modify pkt return parameters */
16622 	if (*rval != SATA_TRAN_ACCEPTED ||
16623 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16624 		sata_fault_count = 0;
16625 		sata_fault_suspend_count = 0;
16626 		return;
16627 	}
16628 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
16629 		/* Pause in the injection */
16630 		sata_fault_suspend_count -= 1;
16631 		return;
16632 	}
16633 
16634 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
16635 		/*
16636 		 * Init inject fault cycle. If fault count is set to -1,
16637 		 * it is a permanent fault.
16638 		 */
16639 		if (sata_inject_fault_count != -1) {
16640 			sata_fault_count = sata_inject_fault_count;
16641 			sata_fault_suspend_count =
16642 			    sata_inject_fault_pause_count;
16643 			if (sata_fault_suspend_count == 0)
16644 				sata_inject_fault_count = 0;
16645 		}
16646 	}
16647 
16648 	if (sata_fault_count != 0)
16649 		sata_fault_count -= 1;
16650 
16651 	switch (fault) {
16652 	case SATA_PKT_BUSY:
16653 		*rval = SATA_TRAN_BUSY;
16654 		spkt->satapkt_reason = SATA_PKT_BUSY;
16655 		break;
16656 
16657 	case SATA_PKT_QUEUE_FULL:
16658 		*rval = SATA_TRAN_QUEUE_FULL;
16659 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
16660 		break;
16661 
16662 	case SATA_PKT_CMD_UNSUPPORTED:
16663 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
16664 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
16665 		break;
16666 
16667 	case SATA_PKT_PORT_ERROR:
16668 		/* This is "rejected" command */
16669 		*rval = SATA_TRAN_PORT_ERROR;
16670 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
16671 		/* Additional error setup could be done here - port state */
16672 		break;
16673 
16674 	case SATA_PKT_DEV_ERROR:
16675 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
16676 		/*
16677 		 * Additional error setup could be done here
16678 		 */
16679 		break;
16680 
16681 	case SATA_PKT_ABORTED:
16682 		spkt->satapkt_reason = SATA_PKT_ABORTED;
16683 		break;
16684 
16685 	case SATA_PKT_TIMEOUT:
16686 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
16687 		/* Additional error setup could be done here */
16688 		break;
16689 
16690 	case SATA_PKT_RESET:
16691 		spkt->satapkt_reason = SATA_PKT_RESET;
16692 		/*
16693 		 * Additional error setup could be done here - device reset
16694 		 */
16695 		break;
16696 
16697 	default:
16698 		break;
16699 	}
16700 }
16701 
16702 #endif
16703 
16704 /*
16705  * SATA Trace Ring Buffer
16706  * ----------------------
16707  *
16708  * Overview
16709  *
16710  * The SATA trace ring buffer is a ring buffer created and managed by
16711  * the SATA framework module that can be used by any module or driver
16712  * within the SATA framework to store debug messages.
16713  *
16714  * Ring Buffer Interfaces:
16715  *
16716  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
16717  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
16718  *
16719  *	Note that the sata_trace_debug() interface was created to give
16720  *	consumers the flexibilty of sending debug messages to ring buffer
16721  *	as variable arguments.  Consumers can send type va_list debug
16722  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
16723  *	and sata_vtrace_debug() relationship is similar to that of
16724  *	cmn_err(9F) and vcmn_err(9F).
16725  *
16726  * Below is a diagram of the SATA trace ring buffer interfaces and
16727  * sample consumers:
16728  *
16729  * +---------------------------------+
16730  * |    o  o  SATA Framework Module  |
16731  * | o  SATA  o     +------------------+      +------------------+
16732  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
16733  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
16734  * | o        o     +------------------+   |  +------------------+
16735  * |    o  o                ^        |     +--|SATA HBA Driver #2|
16736  * |                        |        |        +------------------+
16737  * |           +------------------+  |
16738  * |           |SATA Debug Message|  |
16739  * |           +------------------+  |
16740  * +---------------------------------+
16741  *
16742  * Supporting Routines:
16743  *
16744  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
16745  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
16746  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
16747  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
16748  *
16749  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
16750  * The ring buffer size can be adjusted by setting dmsg_ring_size in
16751  * /etc/system to desired size in unit of bytes.
16752  *
16753  * The individual debug message size in the ring buffer is restricted
16754  * to DMSG_BUF_SIZE.
16755  */
16756 void
16757 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
16758 {
16759 	sata_trace_dmsg_t *dmsg;
16760 
16761 	if (sata_debug_rbuf == NULL) {
16762 		return;
16763 	}
16764 
16765 	/*
16766 	 * If max size of ring buffer is smaller than size
16767 	 * required for one debug message then just return
16768 	 * since we have no room for the debug message.
16769 	 */
16770 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
16771 		return;
16772 	}
16773 
16774 	mutex_enter(&sata_debug_rbuf->lock);
16775 
16776 	/* alloc or reuse on ring buffer */
16777 	dmsg = sata_trace_dmsg_alloc();
16778 
16779 	if (dmsg == NULL) {
16780 		/* resource allocation failed */
16781 		mutex_exit(&sata_debug_rbuf->lock);
16782 		return;
16783 	}
16784 
16785 	dmsg->dip = dip;
16786 	gethrestime(&dmsg->timestamp);
16787 
16788 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
16789 
16790 	mutex_exit(&sata_debug_rbuf->lock);
16791 }
16792 
16793 void
16794 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
16795 {
16796 	va_list ap;
16797 
16798 	va_start(ap, fmt);
16799 	sata_vtrace_debug(dip, fmt, ap);
16800 	va_end(ap);
16801 }
16802 
16803 /*
16804  * This routine is used to manage debug messages
16805  * on ring buffer.
16806  */
16807 static sata_trace_dmsg_t *
16808 sata_trace_dmsg_alloc(void)
16809 {
16810 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
16811 
16812 	if (sata_debug_rbuf->looped == TRUE) {
16813 		sata_debug_rbuf->dmsgp = dmsg->next;
16814 		return (sata_debug_rbuf->dmsgp);
16815 	}
16816 
16817 	/*
16818 	 * If we're looping for the first time,
16819 	 * connect the ring.
16820 	 */
16821 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
16822 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
16823 		dmsg->next = sata_debug_rbuf->dmsgh;
16824 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
16825 		sata_debug_rbuf->looped = TRUE;
16826 		return (sata_debug_rbuf->dmsgp);
16827 	}
16828 
16829 	/* If we've gotten this far then memory allocation is needed */
16830 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
16831 	if (dmsg_alloc == NULL) {
16832 		sata_debug_rbuf->allocfailed++;
16833 		return (dmsg_alloc);
16834 	} else {
16835 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
16836 	}
16837 
16838 	if (sata_debug_rbuf->dmsgp != NULL) {
16839 		dmsg->next = dmsg_alloc;
16840 		sata_debug_rbuf->dmsgp = dmsg->next;
16841 		return (sata_debug_rbuf->dmsgp);
16842 	} else {
16843 		/*
16844 		 * We should only be here if we're initializing
16845 		 * the ring buffer.
16846 		 */
16847 		if (sata_debug_rbuf->dmsgh == NULL) {
16848 			sata_debug_rbuf->dmsgh = dmsg_alloc;
16849 		} else {
16850 			/* Something is wrong */
16851 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
16852 			return (NULL);
16853 		}
16854 
16855 		sata_debug_rbuf->dmsgp = dmsg_alloc;
16856 		return (sata_debug_rbuf->dmsgp);
16857 	}
16858 }
16859 
16860 
16861 /*
16862  * Free all messages on debug ring buffer.
16863  */
16864 static void
16865 sata_trace_dmsg_free(void)
16866 {
16867 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
16868 
16869 	while (dmsg != NULL) {
16870 		dmsg_next = dmsg->next;
16871 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
16872 
16873 		/*
16874 		 * If we've looped around the ring than we're done.
16875 		 */
16876 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
16877 			break;
16878 		} else {
16879 			dmsg = dmsg_next;
16880 		}
16881 	}
16882 }
16883 
16884 
16885 /*
16886  * This function can block
16887  */
16888 static void
16889 sata_trace_rbuf_alloc(void)
16890 {
16891 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
16892 
16893 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
16894 
16895 	if (dmsg_ring_size > 0) {
16896 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
16897 	}
16898 }
16899 
16900 
16901 static void
16902 sata_trace_rbuf_free(void)
16903 {
16904 	sata_trace_dmsg_free();
16905 	mutex_destroy(&sata_debug_rbuf->lock);
16906 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
16907 }
16908 
16909 /*
16910  * If SATA_DEBUG is not defined then this routine is called instead
16911  * of sata_log() via the SATA_LOG_D macro.
16912  */
16913 static void
16914 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
16915     const char *fmt, ...)
16916 {
16917 #ifndef __lock_lint
16918 	_NOTE(ARGUNUSED(level))
16919 #endif
16920 
16921 	dev_info_t *dip = NULL;
16922 	va_list ap;
16923 
16924 	if (sata_hba_inst != NULL) {
16925 		dip = SATA_DIP(sata_hba_inst);
16926 	}
16927 
16928 	va_start(ap, fmt);
16929 	sata_vtrace_debug(dip, fmt, ap);
16930 	va_end(ap);
16931 }
16932