xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision ddee57fa6897d2e39c7a9c3ea4247b298ac7da48)
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 static int
4023 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4024 {
4025 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4026 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4027 	sata_drive_info_t *sdinfo;
4028 	sata_id_t *sata_id;
4029 	struct scsi_extended_sense *sense;
4030 	int 		len, bdlen, count, alc_len;
4031 	int		pc;	/* Page Control code */
4032 	uint8_t		*buf;	/* mode sense buffer */
4033 	int		rval, reason;
4034 
4035 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4036 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4037 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4038 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4039 
4040 	buf = kmem_zalloc(1024, KM_SLEEP);
4041 
4042 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4043 
4044 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4045 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4046 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4047 		kmem_free(buf, 1024);
4048 		return (rval);
4049 	}
4050 
4051 	scsipkt->pkt_reason = CMD_CMPLT;
4052 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4053 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4054 
4055 	pc = scsipkt->pkt_cdbp[2] >> 6;
4056 
4057 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4058 		/*
4059 		 * Because it is fully emulated command storing data
4060 		 * programatically in the specified buffer, release
4061 		 * preallocated DMA resources before storing data in the buffer,
4062 		 * so no unwanted DMA sync would take place.
4063 		 */
4064 		sata_scsi_dmafree(NULL, scsipkt);
4065 
4066 		len = 0;
4067 		bdlen = 0;
4068 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4069 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4070 			    (scsipkt->pkt_cdbp[0] & 0x10))
4071 				bdlen = 16;
4072 			else
4073 				bdlen = 8;
4074 		}
4075 		/* Build mode parameter header */
4076 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4077 			/* 4-byte mode parameter header */
4078 			buf[len++] = 0;		/* mode data length */
4079 			buf[len++] = 0;		/* medium type */
4080 			buf[len++] = 0;		/* dev-specific param */
4081 			buf[len++] = bdlen;	/* Block Descriptor length */
4082 		} else {
4083 			/* 8-byte mode parameter header */
4084 			buf[len++] = 0;		/* mode data length */
4085 			buf[len++] = 0;
4086 			buf[len++] = 0;		/* medium type */
4087 			buf[len++] = 0;		/* dev-specific param */
4088 			if (bdlen == 16)
4089 				buf[len++] = 1;	/* long lba descriptor */
4090 			else
4091 				buf[len++] = 0;
4092 			buf[len++] = 0;
4093 			buf[len++] = 0;		/* Block Descriptor length */
4094 			buf[len++] = bdlen;
4095 		}
4096 
4097 		sdinfo = sata_get_device_info(
4098 		    spx->txlt_sata_hba_inst,
4099 		    &spx->txlt_sata_pkt->satapkt_device);
4100 
4101 		/* Build block descriptor only if not disabled (DBD) */
4102 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4103 			/* Block descriptor - direct-access device format */
4104 			if (bdlen == 8) {
4105 				/* build regular block descriptor */
4106 				buf[len++] =
4107 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4108 				buf[len++] =
4109 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4110 				buf[len++] =
4111 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4112 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4113 				buf[len++] = 0; /* density code */
4114 				buf[len++] = 0;
4115 				if (sdinfo->satadrv_type ==
4116 				    SATA_DTYPE_ATADISK)
4117 					buf[len++] = 2;
4118 				else
4119 					/* ATAPI */
4120 					buf[len++] = 8;
4121 				buf[len++] = 0;
4122 			} else if (bdlen == 16) {
4123 				/* Long LBA Accepted */
4124 				/* build long lba block descriptor */
4125 #ifndef __lock_lint
4126 				buf[len++] =
4127 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4128 				buf[len++] =
4129 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4130 				buf[len++] =
4131 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4132 				buf[len++] =
4133 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4134 #endif
4135 				buf[len++] =
4136 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4137 				buf[len++] =
4138 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4139 				buf[len++] =
4140 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4141 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4142 				buf[len++] = 0;
4143 				buf[len++] = 0; /* density code */
4144 				buf[len++] = 0;
4145 				buf[len++] = 0;
4146 				if (sdinfo->satadrv_type ==
4147 				    SATA_DTYPE_ATADISK)
4148 					buf[len++] = 2;
4149 				else
4150 					/* ATAPI */
4151 					buf[len++] = 8;
4152 				buf[len++] = 0;
4153 			}
4154 		}
4155 
4156 		sata_id = &sdinfo->satadrv_id;
4157 
4158 		/*
4159 		 * Add requested pages.
4160 		 * Page 3 and 4 are obsolete and we are not supporting them.
4161 		 * We deal now with:
4162 		 * caching (read/write cache control).
4163 		 * We should eventually deal with following mode pages:
4164 		 * error recovery  (0x01),
4165 		 * power condition (0x1a),
4166 		 * exception control page (enables SMART) (0x1c),
4167 		 * enclosure management (ses),
4168 		 * protocol-specific port mode (port control).
4169 		 */
4170 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4171 		case MODEPAGE_RW_ERRRECOV:
4172 			/* DAD_MODE_ERR_RECOV */
4173 			/* R/W recovery */
4174 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4175 			break;
4176 		case MODEPAGE_CACHING:
4177 			/* DAD_MODE_CACHE */
4178 			/* Reject not supported request for saved parameters */
4179 			if (pc == 3) {
4180 				*scsipkt->pkt_scbp = STATUS_CHECK;
4181 				sense = sata_arq_sense(spx);
4182 				sense->es_key = KEY_ILLEGAL_REQUEST;
4183 				sense->es_add_code =
4184 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4185 				goto done;
4186 			}
4187 
4188 			/* caching */
4189 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4190 			break;
4191 		case MODEPAGE_INFO_EXCPT:
4192 			/* exception cntrl */
4193 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4194 				len += sata_build_msense_page_1c(sdinfo, pc,
4195 				    buf+len);
4196 			}
4197 			else
4198 				goto err;
4199 			break;
4200 		case MODEPAGE_POWER_COND:
4201 			/* DAD_MODE_POWER_COND */
4202 			/* power condition */
4203 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4204 			break;
4205 
4206 		case MODEPAGE_ACOUSTIC_MANAG:
4207 			/* acoustic management */
4208 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4209 			break;
4210 		case MODEPAGE_ALLPAGES:
4211 			/* all pages */
4212 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4213 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4214 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4215 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4216 				len += sata_build_msense_page_1c(sdinfo, pc,
4217 				    buf+len);
4218 			}
4219 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4220 			break;
4221 		default:
4222 		err:
4223 			/* Invalid request */
4224 			*scsipkt->pkt_scbp = STATUS_CHECK;
4225 			sense = sata_arq_sense(spx);
4226 			sense->es_key = KEY_ILLEGAL_REQUEST;
4227 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4228 			goto done;
4229 		}
4230 
4231 		/* fix total mode data length */
4232 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4233 			/* 4-byte mode parameter header */
4234 			buf[0] = len - 1;	/* mode data length */
4235 		} else {
4236 			buf[0] = (len -2) >> 8;
4237 			buf[1] = (len -2) & 0xff;
4238 		}
4239 
4240 
4241 		/* Check allocation length */
4242 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4243 			alc_len = scsipkt->pkt_cdbp[4];
4244 		} else {
4245 			alc_len = scsipkt->pkt_cdbp[7];
4246 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4247 		}
4248 		/*
4249 		 * We do not check for possible parameters truncation
4250 		 * (alc_len < len) assuming that the target driver works
4251 		 * correctly. Just avoiding overrun.
4252 		 * Copy no more than requested and possible, buffer-wise.
4253 		 */
4254 		count = MIN(alc_len, len);
4255 		count = MIN(bp->b_bcount, count);
4256 		bcopy(buf, bp->b_un.b_addr, count);
4257 
4258 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4259 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4260 	}
4261 	*scsipkt->pkt_scbp = STATUS_GOOD;
4262 done:
4263 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4264 	(void) kmem_free(buf, 1024);
4265 
4266 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4267 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4268 
4269 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4270 	    scsipkt->pkt_comp != NULL)
4271 		/* scsi callback required */
4272 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4273 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4274 		    TQ_SLEEP) == NULL)
4275 			/* Scheduling the callback failed */
4276 			return (TRAN_BUSY);
4277 
4278 	return (TRAN_ACCEPT);
4279 }
4280 
4281 
4282 /*
4283  * SATA translate command: Mode Select.
4284  * Translated into appropriate SATA command or emulated.
4285  * Saving parameters is not supported.
4286  * Changing device capacity is not supported (although theoretically
4287  * possible by executing SET FEATURES/SET MAX ADDRESS)
4288  *
4289  * Assumption is that the target driver is working correctly.
4290  *
4291  * More than one SATA command may be executed to perform operations specified
4292  * by mode select pages. The first error terminates further execution.
4293  * Operations performed successully are not backed-up in such case.
4294  *
4295  * NOTE: Implemented pages:
4296  * - caching page
4297  * - informational exception page
4298  * - acoustic management page
4299  * - power condition page
4300  * Caching setup is remembered so it could be re-stored in case of
4301  * an unexpected device reset.
4302  *
4303  * Returns TRAN_XXXX.
4304  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
4305  */
4306 
4307 static int
4308 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4309 {
4310 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4311 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4312 	struct scsi_extended_sense *sense;
4313 	int len, pagelen, count, pllen;
4314 	uint8_t *buf;	/* mode select buffer */
4315 	int rval, stat, reason;
4316 	uint_t nointr_flag;
4317 	int dmod = 0;
4318 
4319 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4320 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4321 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4322 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4323 
4324 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4325 
4326 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4327 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4328 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4329 		return (rval);
4330 	}
4331 	/*
4332 	 * If in interrupt context, reject this packet because it may result
4333 	 * in issuing a synchronous command to HBA.
4334 	 */
4335 	if (servicing_interrupt()) {
4336 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4337 		    "sata_txlt_mode_select: rejecting command because "
4338 		    "of interrupt context\n", NULL);
4339 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4340 		return (TRAN_BUSY);
4341 	}
4342 
4343 	rval = TRAN_ACCEPT;
4344 
4345 	scsipkt->pkt_reason = CMD_CMPLT;
4346 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4347 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4348 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4349 
4350 	/* Reject not supported request */
4351 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4352 		*scsipkt->pkt_scbp = STATUS_CHECK;
4353 		sense = sata_arq_sense(spx);
4354 		sense->es_key = KEY_ILLEGAL_REQUEST;
4355 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4356 		goto done;
4357 	}
4358 
4359 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4360 		pllen = scsipkt->pkt_cdbp[4];
4361 	} else {
4362 		pllen = scsipkt->pkt_cdbp[7];
4363 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4364 	}
4365 
4366 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4367 
4368 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4369 		buf = (uint8_t *)bp->b_un.b_addr;
4370 		count = MIN(bp->b_bcount, pllen);
4371 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4372 		scsipkt->pkt_resid = 0;
4373 		pllen = count;
4374 
4375 		/*
4376 		 * Check the header to skip the block descriptor(s) - we
4377 		 * do not support setting device capacity.
4378 		 * Existing macros do not recognize long LBA dscriptor,
4379 		 * hence manual calculation.
4380 		 */
4381 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4382 			/* 6-bytes CMD, 4 bytes header */
4383 			if (count <= 4)
4384 				goto done;		/* header only */
4385 			len = buf[3] + 4;
4386 		} else {
4387 			/* 10-bytes CMD, 8 bytes header */
4388 			if (count <= 8)
4389 				goto done;		/* header only */
4390 			len = buf[6];
4391 			len = (len << 8) + buf[7] + 8;
4392 		}
4393 		if (len >= count)
4394 			goto done;	/* header + descriptor(s) only */
4395 
4396 		pllen -= len;		/* remaining data length */
4397 
4398 		/*
4399 		 * We may be executing SATA command and want to execute it
4400 		 * in SYNCH mode, regardless of scsi_pkt setting.
4401 		 * Save scsi_pkt setting and indicate SYNCH mode
4402 		 */
4403 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4404 		    scsipkt->pkt_comp != NULL) {
4405 			scsipkt->pkt_flags |= FLAG_NOINTR;
4406 		}
4407 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4408 
4409 		/*
4410 		 * len is now the offset to a first mode select page
4411 		 * Process all pages
4412 		 */
4413 		while (pllen > 0) {
4414 			switch ((int)buf[len]) {
4415 			case MODEPAGE_CACHING:
4416 				/* No support for SP (saving) */
4417 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4418 					*scsipkt->pkt_scbp = STATUS_CHECK;
4419 					sense = sata_arq_sense(spx);
4420 					sense->es_key = KEY_ILLEGAL_REQUEST;
4421 					sense->es_add_code =
4422 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4423 					goto done;
4424 				}
4425 				stat = sata_mode_select_page_8(spx,
4426 				    (struct mode_cache_scsi3 *)&buf[len],
4427 				    pllen, &pagelen, &rval, &dmod);
4428 				/*
4429 				 * The pagelen value indicates the number of
4430 				 * parameter bytes already processed.
4431 				 * The rval is the return value from
4432 				 * sata_tran_start().
4433 				 * The stat indicates the overall status of
4434 				 * the operation(s).
4435 				 */
4436 				if (stat != SATA_SUCCESS)
4437 					/*
4438 					 * Page processing did not succeed -
4439 					 * all error info is already set-up,
4440 					 * just return
4441 					 */
4442 					pllen = 0; /* this breaks the loop */
4443 				else {
4444 					len += pagelen;
4445 					pllen -= pagelen;
4446 				}
4447 				break;
4448 
4449 			case MODEPAGE_INFO_EXCPT:
4450 				stat = sata_mode_select_page_1c(spx,
4451 				    (struct mode_info_excpt_page *)&buf[len],
4452 				    pllen, &pagelen, &rval, &dmod);
4453 				/*
4454 				 * The pagelen value indicates the number of
4455 				 * parameter bytes already processed.
4456 				 * The rval is the return value from
4457 				 * sata_tran_start().
4458 				 * The stat indicates the overall status of
4459 				 * the operation(s).
4460 				 */
4461 				if (stat != SATA_SUCCESS)
4462 					/*
4463 					 * Page processing did not succeed -
4464 					 * all error info is already set-up,
4465 					 * just return
4466 					 */
4467 					pllen = 0; /* this breaks the loop */
4468 				else {
4469 					len += pagelen;
4470 					pllen -= pagelen;
4471 				}
4472 				break;
4473 
4474 			case MODEPAGE_ACOUSTIC_MANAG:
4475 				stat = sata_mode_select_page_30(spx,
4476 				    (struct mode_acoustic_management *)
4477 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4478 				/*
4479 				 * The pagelen value indicates the number of
4480 				 * parameter bytes already processed.
4481 				 * The rval is the return value from
4482 				 * sata_tran_start().
4483 				 * The stat indicates the overall status of
4484 				 * the operation(s).
4485 				 */
4486 				if (stat != SATA_SUCCESS)
4487 					/*
4488 					 * Page processing did not succeed -
4489 					 * all error info is already set-up,
4490 					 * just return
4491 					 */
4492 					pllen = 0; /* this breaks the loop */
4493 				else {
4494 					len += pagelen;
4495 					pllen -= pagelen;
4496 				}
4497 
4498 				break;
4499 			case MODEPAGE_POWER_COND:
4500 				stat = sata_mode_select_page_1a(spx,
4501 				    (struct mode_info_power_cond *)&buf[len],
4502 				    pllen, &pagelen, &rval, &dmod);
4503 				/*
4504 				 * The pagelen value indicates the number of
4505 				 * parameter bytes already processed.
4506 				 * The rval is the return value from
4507 				 * sata_tran_start().
4508 				 * The stat indicates the overall status of
4509 				 * the operation(s).
4510 				 */
4511 				if (stat != SATA_SUCCESS)
4512 					/*
4513 					 * Page processing did not succeed -
4514 					 * all error info is already set-up,
4515 					 * just return
4516 					 */
4517 					pllen = 0; /* this breaks the loop */
4518 				else {
4519 					len += pagelen;
4520 					pllen -= pagelen;
4521 				}
4522 				break;
4523 			default:
4524 				*scsipkt->pkt_scbp = STATUS_CHECK;
4525 				sense = sata_arq_sense(spx);
4526 				sense->es_key = KEY_ILLEGAL_REQUEST;
4527 				sense->es_add_code =
4528 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4529 				goto done;
4530 			}
4531 		}
4532 	}
4533 done:
4534 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4535 	/*
4536 	 * If device parameters were modified, fetch and store the new
4537 	 * Identify Device data. Since port mutex could have been released
4538 	 * for accessing HBA driver, we need to re-check device existence.
4539 	 */
4540 	if (dmod != 0) {
4541 		sata_drive_info_t new_sdinfo, *sdinfo;
4542 		int rv = 0;
4543 
4544 		/*
4545 		 * Following statement has to be changed if this function is
4546 		 * used for devices other than SATA hard disks.
4547 		 */
4548 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4549 
4550 		new_sdinfo.satadrv_addr =
4551 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4552 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4553 		    &new_sdinfo);
4554 
4555 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4556 		/*
4557 		 * Since port mutex could have been released when
4558 		 * accessing HBA driver, we need to re-check that the
4559 		 * framework still holds the device info structure.
4560 		 */
4561 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4562 		    &spx->txlt_sata_pkt->satapkt_device);
4563 		if (sdinfo != NULL) {
4564 			/*
4565 			 * Device still has info structure in the
4566 			 * sata framework. Copy newly fetched info
4567 			 */
4568 			if (rv == 0) {
4569 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4570 				sata_save_drive_settings(sdinfo);
4571 			} else {
4572 				/*
4573 				 * Could not fetch new data - invalidate
4574 				 * sata_drive_info. That makes device
4575 				 * unusable.
4576 				 */
4577 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4578 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4579 			}
4580 		}
4581 		if (rv != 0 || sdinfo == NULL) {
4582 			/*
4583 			 * This changes the overall mode select completion
4584 			 * reason to a failed one !!!!!
4585 			 */
4586 			*scsipkt->pkt_scbp = STATUS_CHECK;
4587 			sense = sata_arq_sense(spx);
4588 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4589 			rval = TRAN_ACCEPT;
4590 		}
4591 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4592 	}
4593 	/* Restore the scsi pkt flags */
4594 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4595 	scsipkt->pkt_flags |= nointr_flag;
4596 
4597 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4598 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4599 
4600 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4601 	    scsipkt->pkt_comp != NULL)
4602 		/* scsi callback required */
4603 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4604 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4605 		    TQ_SLEEP) == NULL)
4606 			/* Scheduling the callback failed */
4607 			return (TRAN_BUSY);
4608 
4609 	return (rval);
4610 }
4611 
4612 
4613 
4614 /*
4615  * Translate command: Log Sense
4616  */
4617 static 	int
4618 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4619 {
4620 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4621 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4622 	sata_drive_info_t *sdinfo;
4623 	struct scsi_extended_sense *sense;
4624 	int 		len, count, alc_len;
4625 	int		pc;	/* Page Control code */
4626 	int		page_code;	/* Page code */
4627 	uint8_t		*buf;	/* log sense buffer */
4628 	int		rval, reason;
4629 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4630 
4631 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4632 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4633 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4634 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4635 
4636 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4637 
4638 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4639 
4640 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4641 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4642 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4643 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4644 		return (rval);
4645 	}
4646 	/*
4647 	 * If in interrupt context, reject this packet because it may result
4648 	 * in issuing a synchronous command to HBA.
4649 	 */
4650 	if (servicing_interrupt()) {
4651 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4652 		    "sata_log_sense: rejecting command because "
4653 		    "of interrupt context\n", NULL);
4654 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4655 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4656 		return (TRAN_BUSY);
4657 	}
4658 
4659 	scsipkt->pkt_reason = CMD_CMPLT;
4660 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4661 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4662 
4663 	pc = scsipkt->pkt_cdbp[2] >> 6;
4664 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4665 
4666 	/* Reject not supported request for all but cumulative values */
4667 	switch (pc) {
4668 	case PC_CUMULATIVE_VALUES:
4669 		break;
4670 	default:
4671 		*scsipkt->pkt_scbp = STATUS_CHECK;
4672 		sense = sata_arq_sense(spx);
4673 		sense->es_key = KEY_ILLEGAL_REQUEST;
4674 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4675 		goto done;
4676 	}
4677 
4678 	switch (page_code) {
4679 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4680 	case PAGE_CODE_SELF_TEST_RESULTS:
4681 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4682 	case PAGE_CODE_SMART_READ_DATA:
4683 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4684 		break;
4685 	default:
4686 		*scsipkt->pkt_scbp = STATUS_CHECK;
4687 		sense = sata_arq_sense(spx);
4688 		sense->es_key = KEY_ILLEGAL_REQUEST;
4689 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4690 		goto done;
4691 	}
4692 
4693 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4694 		/*
4695 		 * Because log sense uses local buffers for data retrieval from
4696 		 * the devices and sets the data programatically in the
4697 		 * original specified buffer, release preallocated DMA
4698 		 * resources before storing data in the original buffer,
4699 		 * so no unwanted DMA sync would take place.
4700 		 */
4701 		sata_id_t *sata_id;
4702 
4703 		sata_scsi_dmafree(NULL, scsipkt);
4704 
4705 		len = 0;
4706 
4707 		/* Build log parameter header */
4708 		buf[len++] = page_code;	/* page code as in the CDB */
4709 		buf[len++] = 0;		/* reserved */
4710 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4711 		buf[len++] = 0;		/* (LSB) */
4712 
4713 		sdinfo = sata_get_device_info(
4714 		    spx->txlt_sata_hba_inst,
4715 		    &spx->txlt_sata_pkt->satapkt_device);
4716 
4717 		/*
4718 		 * Add requested pages.
4719 		 */
4720 		switch (page_code) {
4721 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4722 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4723 			break;
4724 		case PAGE_CODE_SELF_TEST_RESULTS:
4725 			sata_id = &sdinfo->satadrv_id;
4726 			if ((! (sata_id->ai_cmdset84 &
4727 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4728 			    (! (sata_id->ai_features87 &
4729 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4730 				*scsipkt->pkt_scbp = STATUS_CHECK;
4731 				sense = sata_arq_sense(spx);
4732 				sense->es_key = KEY_ILLEGAL_REQUEST;
4733 				sense->es_add_code =
4734 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4735 
4736 				goto done;
4737 			}
4738 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4739 			    spx->txlt_sata_hba_inst);
4740 			break;
4741 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4742 			sata_id = &sdinfo->satadrv_id;
4743 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4744 				*scsipkt->pkt_scbp = STATUS_CHECK;
4745 				sense = sata_arq_sense(spx);
4746 				sense->es_key = KEY_ILLEGAL_REQUEST;
4747 				sense->es_add_code =
4748 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4749 
4750 				goto done;
4751 			}
4752 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4753 				*scsipkt->pkt_scbp = STATUS_CHECK;
4754 				sense = sata_arq_sense(spx);
4755 				sense->es_key = KEY_ABORTED_COMMAND;
4756 				sense->es_add_code =
4757 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4758 				sense->es_qual_code =
4759 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4760 
4761 				goto done;
4762 			}
4763 
4764 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4765 			    spx->txlt_sata_hba_inst);
4766 			break;
4767 		case PAGE_CODE_SMART_READ_DATA:
4768 			sata_id = &sdinfo->satadrv_id;
4769 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4770 				*scsipkt->pkt_scbp = STATUS_CHECK;
4771 				sense = sata_arq_sense(spx);
4772 				sense->es_key = KEY_ILLEGAL_REQUEST;
4773 				sense->es_add_code =
4774 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4775 
4776 				goto done;
4777 			}
4778 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4779 				*scsipkt->pkt_scbp = STATUS_CHECK;
4780 				sense = sata_arq_sense(spx);
4781 				sense->es_key = KEY_ABORTED_COMMAND;
4782 				sense->es_add_code =
4783 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4784 				sense->es_qual_code =
4785 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4786 
4787 				goto done;
4788 			}
4789 
4790 			/* This page doesn't include a page header */
4791 			len = sata_build_lsense_page_30(sdinfo, buf,
4792 			    spx->txlt_sata_hba_inst);
4793 			goto no_header;
4794 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4795 			sata_id = &sdinfo->satadrv_id;
4796 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4797 				*scsipkt->pkt_scbp = STATUS_CHECK;
4798 				sense = sata_arq_sense(spx);
4799 				sense->es_key = KEY_ILLEGAL_REQUEST;
4800 				sense->es_add_code =
4801 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4802 
4803 				goto done;
4804 			}
4805 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4806 				*scsipkt->pkt_scbp = STATUS_CHECK;
4807 				sense = sata_arq_sense(spx);
4808 				sense->es_key = KEY_ABORTED_COMMAND;
4809 				sense->es_add_code =
4810 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4811 				sense->es_qual_code =
4812 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4813 
4814 				goto done;
4815 			}
4816 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
4817 			goto no_header;
4818 		default:
4819 			/* Invalid request */
4820 			*scsipkt->pkt_scbp = STATUS_CHECK;
4821 			sense = sata_arq_sense(spx);
4822 			sense->es_key = KEY_ILLEGAL_REQUEST;
4823 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4824 			goto done;
4825 		}
4826 
4827 		/* set parameter log sense data length */
4828 		buf[2] = len >> 8;	/* log sense length (MSB) */
4829 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4830 
4831 		len += SCSI_LOG_PAGE_HDR_LEN;
4832 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4833 
4834 no_header:
4835 		/* Check allocation length */
4836 		alc_len = scsipkt->pkt_cdbp[7];
4837 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4838 
4839 		/*
4840 		 * We do not check for possible parameters truncation
4841 		 * (alc_len < len) assuming that the target driver works
4842 		 * correctly. Just avoiding overrun.
4843 		 * Copy no more than requested and possible, buffer-wise.
4844 		 */
4845 		count = MIN(alc_len, len);
4846 		count = MIN(bp->b_bcount, count);
4847 		bcopy(buf, bp->b_un.b_addr, count);
4848 
4849 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4850 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4851 	}
4852 	*scsipkt->pkt_scbp = STATUS_GOOD;
4853 done:
4854 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4855 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4856 
4857 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4858 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4859 
4860 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4861 	    scsipkt->pkt_comp != NULL)
4862 		/* scsi callback required */
4863 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4864 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4865 		    TQ_SLEEP) == NULL)
4866 			/* Scheduling the callback failed */
4867 			return (TRAN_BUSY);
4868 
4869 	return (TRAN_ACCEPT);
4870 }
4871 
4872 /*
4873  * Translate command: Log Select
4874  * Not implemented at this time - returns invalid command response.
4875  */
4876 static	int
4877 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4878 {
4879 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4880 	    "sata_txlt_log_select\n", NULL);
4881 
4882 	return (sata_txlt_invalid_command(spx));
4883 }
4884 
4885 
4886 /*
4887  * Translate command: Read (various types).
4888  * Translated into appropriate type of ATA READ command
4889  * for SATA hard disks.
4890  * Both the device capabilities and requested operation mode are
4891  * considered.
4892  *
4893  * Following scsi cdb fields are ignored:
4894  * rdprotect, dpo, fua, fua_nv, group_number.
4895  *
4896  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4897  * enable variable sata_func_enable), the capability of the controller and
4898  * capability of a device are checked and if both support queueing, read
4899  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4900  * command rather than plain READ_XXX command.
4901  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4902  * both the controller and device suport such functionality, the read
4903  * request will be translated to READ_FPDMA_QUEUED command.
4904  * In both cases the maximum queue depth is derived as minimum of:
4905  * HBA capability,device capability and sata_max_queue_depth variable setting.
4906  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4907  * used to pass max queue depth value, and the maximum possible queue depth
4908  * is 32.
4909  *
4910  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4911  * appropriate values in scsi_pkt fields.
4912  */
4913 static int
4914 sata_txlt_read(sata_pkt_txlate_t *spx)
4915 {
4916 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4917 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4918 	sata_drive_info_t *sdinfo;
4919 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4920 	int cport = SATA_TXLT_CPORT(spx);
4921 	uint16_t sec_count;
4922 	uint64_t lba;
4923 	int rval, reason;
4924 	int synch;
4925 
4926 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4927 
4928 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4929 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4930 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4931 		return (rval);
4932 	}
4933 
4934 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4935 	    &spx->txlt_sata_pkt->satapkt_device);
4936 
4937 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4938 	/*
4939 	 * Extract LBA and sector count from scsi CDB.
4940 	 */
4941 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4942 	case SCMD_READ:
4943 		/* 6-byte scsi read cmd : 0x08 */
4944 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4945 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4946 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4947 		sec_count = scsipkt->pkt_cdbp[4];
4948 		/* sec_count 0 will be interpreted as 256 by a device */
4949 		break;
4950 	case SCMD_READ_G1:
4951 		/* 10-bytes scsi read command : 0x28 */
4952 		lba = scsipkt->pkt_cdbp[2];
4953 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4954 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4955 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4956 		sec_count = scsipkt->pkt_cdbp[7];
4957 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4958 		break;
4959 	case SCMD_READ_G5:
4960 		/* 12-bytes scsi read command : 0xA8 */
4961 		lba = scsipkt->pkt_cdbp[2];
4962 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4963 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4964 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4965 		sec_count = scsipkt->pkt_cdbp[6];
4966 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4967 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4968 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4969 		break;
4970 	case SCMD_READ_G4:
4971 		/* 16-bytes scsi read command : 0x88 */
4972 		lba = scsipkt->pkt_cdbp[2];
4973 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4974 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4975 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4976 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4977 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4978 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4979 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4980 		sec_count = scsipkt->pkt_cdbp[10];
4981 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4982 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4983 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4984 		break;
4985 	default:
4986 		/* Unsupported command */
4987 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4988 		return (sata_txlt_invalid_command(spx));
4989 	}
4990 
4991 	/*
4992 	 * Check if specified address exceeds device capacity
4993 	 */
4994 	if ((lba >= sdinfo->satadrv_capacity) ||
4995 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4996 		/* LBA out of range */
4997 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4998 		return (sata_txlt_lba_out_of_range(spx));
4999 	}
5000 
5001 	/*
5002 	 * For zero-length transfer, emulate good completion of the command
5003 	 * (reasons for rejecting the command were already checked).
5004 	 * No DMA resources were allocated.
5005 	 */
5006 	if (spx->txlt_dma_cookie_list == NULL) {
5007 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5008 		return (sata_emul_rw_completion(spx));
5009 	}
5010 
5011 	/*
5012 	 * Build cmd block depending on the device capability and
5013 	 * requested operation mode.
5014 	 * Do not bother with non-dma mode - we are working only with
5015 	 * devices supporting DMA.
5016 	 */
5017 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5018 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5019 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5020 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5021 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5022 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5023 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5024 #ifndef __lock_lint
5025 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5026 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5027 		scmd->satacmd_lba_high_msb = lba >> 40;
5028 #endif
5029 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5030 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5031 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5032 	}
5033 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5034 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5035 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5036 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5037 	scmd->satacmd_features_reg = 0;
5038 	scmd->satacmd_status_reg = 0;
5039 	scmd->satacmd_error_reg = 0;
5040 
5041 	/*
5042 	 * Check if queueing commands should be used and switch
5043 	 * to appropriate command if possible
5044 	 */
5045 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5046 		boolean_t using_queuing;
5047 
5048 		/* Queuing supported by controller and device? */
5049 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5050 		    (sdinfo->satadrv_features_support &
5051 		    SATA_DEV_F_NCQ) &&
5052 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5053 		    SATA_CTLF_NCQ)) {
5054 			using_queuing = B_TRUE;
5055 
5056 			/* NCQ supported - use FPDMA READ */
5057 			scmd->satacmd_cmd_reg =
5058 			    SATAC_READ_FPDMA_QUEUED;
5059 			scmd->satacmd_features_reg_ext =
5060 			    scmd->satacmd_sec_count_msb;
5061 			scmd->satacmd_sec_count_msb = 0;
5062 		} else if ((sdinfo->satadrv_features_support &
5063 		    SATA_DEV_F_TCQ) &&
5064 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5065 		    SATA_CTLF_QCMD)) {
5066 			using_queuing = B_TRUE;
5067 
5068 			/* Legacy queueing */
5069 			if (sdinfo->satadrv_features_support &
5070 			    SATA_DEV_F_LBA48) {
5071 				scmd->satacmd_cmd_reg =
5072 				    SATAC_READ_DMA_QUEUED_EXT;
5073 				scmd->satacmd_features_reg_ext =
5074 				    scmd->satacmd_sec_count_msb;
5075 				scmd->satacmd_sec_count_msb = 0;
5076 			} else {
5077 				scmd->satacmd_cmd_reg =
5078 				    SATAC_READ_DMA_QUEUED;
5079 			}
5080 		} else	/* NCQ nor legacy queuing not supported */
5081 			using_queuing = B_FALSE;
5082 
5083 		/*
5084 		 * If queuing, the sector count goes in the features register
5085 		 * and the secount count will contain the tag.
5086 		 */
5087 		if (using_queuing) {
5088 			scmd->satacmd_features_reg =
5089 			    scmd->satacmd_sec_count_lsb;
5090 			scmd->satacmd_sec_count_lsb = 0;
5091 			scmd->satacmd_flags.sata_queued = B_TRUE;
5092 
5093 			/* Set-up maximum queue depth */
5094 			scmd->satacmd_flags.sata_max_queue_depth =
5095 			    sdinfo->satadrv_max_queue_depth - 1;
5096 		} else if (sdinfo->satadrv_features_enabled &
5097 		    SATA_DEV_F_E_UNTAGGED_QING) {
5098 			/*
5099 			 * Although NCQ/TCQ is not enabled, untagged queuing
5100 			 * may be still used.
5101 			 * Set-up the maximum untagged queue depth.
5102 			 * Use controller's queue depth from sata_hba_tran.
5103 			 * SATA HBA drivers may ignore this value and rely on
5104 			 * the internal limits.For drivers that do not
5105 			 * ignore untaged queue depth, limit the value to
5106 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5107 			 * largest value that can be passed via
5108 			 * satacmd_flags.sata_max_queue_depth.
5109 			 */
5110 			scmd->satacmd_flags.sata_max_queue_depth =
5111 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5112 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5113 
5114 		} else {
5115 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5116 		}
5117 	} else
5118 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5119 
5120 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5121 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5122 	    scmd->satacmd_cmd_reg, lba, sec_count);
5123 
5124 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5125 		/* Need callback function */
5126 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5127 		synch = FALSE;
5128 	} else
5129 		synch = TRUE;
5130 
5131 	/* Transfer command to HBA */
5132 	if (sata_hba_start(spx, &rval) != 0) {
5133 		/* Pkt not accepted for execution */
5134 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5135 		return (rval);
5136 	}
5137 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5138 	/*
5139 	 * If execution is non-synchronous,
5140 	 * a callback function will handle potential errors, translate
5141 	 * the response and will do a callback to a target driver.
5142 	 * If it was synchronous, check execution status using the same
5143 	 * framework callback.
5144 	 */
5145 	if (synch) {
5146 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5147 		    "synchronous execution status %x\n",
5148 		    spx->txlt_sata_pkt->satapkt_reason);
5149 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5150 	}
5151 	return (TRAN_ACCEPT);
5152 }
5153 
5154 
5155 /*
5156  * SATA translate command: Write (various types)
5157  * Translated into appropriate type of ATA WRITE command
5158  * for SATA hard disks.
5159  * Both the device capabilities and requested operation mode are
5160  * considered.
5161  *
5162  * Following scsi cdb fields are ignored:
5163  * rwprotect, dpo, fua, fua_nv, group_number.
5164  *
5165  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5166  * enable variable sata_func_enable), the capability of the controller and
5167  * capability of a device are checked and if both support queueing, write
5168  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
5169  * command rather than plain WRITE_XXX command.
5170  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5171  * both the controller and device suport such functionality, the write
5172  * request will be translated to WRITE_FPDMA_QUEUED command.
5173  * In both cases the maximum queue depth is derived as minimum of:
5174  * HBA capability,device capability and sata_max_queue_depth variable setting.
5175  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5176  * used to pass max queue depth value, and the maximum possible queue depth
5177  * is 32.
5178  *
5179  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5180  * appropriate values in scsi_pkt fields.
5181  */
5182 static int
5183 sata_txlt_write(sata_pkt_txlate_t *spx)
5184 {
5185 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5186 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5187 	sata_drive_info_t *sdinfo;
5188 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5189 	int cport = SATA_TXLT_CPORT(spx);
5190 	uint16_t sec_count;
5191 	uint64_t lba;
5192 	int rval, reason;
5193 	int synch;
5194 
5195 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5196 
5197 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5198 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5199 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5200 		return (rval);
5201 	}
5202 
5203 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5204 	    &spx->txlt_sata_pkt->satapkt_device);
5205 
5206 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5207 	/*
5208 	 * Extract LBA and sector count from scsi CDB
5209 	 */
5210 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5211 	case SCMD_WRITE:
5212 		/* 6-byte scsi read cmd : 0x0A */
5213 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5214 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5215 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5216 		sec_count = scsipkt->pkt_cdbp[4];
5217 		/* sec_count 0 will be interpreted as 256 by a device */
5218 		break;
5219 	case SCMD_WRITE_G1:
5220 		/* 10-bytes scsi write command : 0x2A */
5221 		lba = scsipkt->pkt_cdbp[2];
5222 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5223 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5224 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5225 		sec_count = scsipkt->pkt_cdbp[7];
5226 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5227 		break;
5228 	case SCMD_WRITE_G5:
5229 		/* 12-bytes scsi read command : 0xAA */
5230 		lba = scsipkt->pkt_cdbp[2];
5231 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5232 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5233 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5234 		sec_count = scsipkt->pkt_cdbp[6];
5235 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5236 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5237 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5238 		break;
5239 	case SCMD_WRITE_G4:
5240 		/* 16-bytes scsi write command : 0x8A */
5241 		lba = scsipkt->pkt_cdbp[2];
5242 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5243 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5244 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5245 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5246 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5247 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5248 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5249 		sec_count = scsipkt->pkt_cdbp[10];
5250 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5251 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5252 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5253 		break;
5254 	default:
5255 		/* Unsupported command */
5256 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5257 		return (sata_txlt_invalid_command(spx));
5258 	}
5259 
5260 	/*
5261 	 * Check if specified address and length exceeds device capacity
5262 	 */
5263 	if ((lba >= sdinfo->satadrv_capacity) ||
5264 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5265 		/* LBA out of range */
5266 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5267 		return (sata_txlt_lba_out_of_range(spx));
5268 	}
5269 
5270 	/*
5271 	 * For zero-length transfer, emulate good completion of the command
5272 	 * (reasons for rejecting the command were already checked).
5273 	 * No DMA resources were allocated.
5274 	 */
5275 	if (spx->txlt_dma_cookie_list == NULL) {
5276 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5277 		return (sata_emul_rw_completion(spx));
5278 	}
5279 
5280 	/*
5281 	 * Build cmd block depending on the device capability and
5282 	 * requested operation mode.
5283 	 * Do not bother with non-dma mode- we are working only with
5284 	 * devices supporting DMA.
5285 	 */
5286 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5287 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5288 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5289 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5290 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5291 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5292 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5293 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5294 #ifndef __lock_lint
5295 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5296 		scmd->satacmd_lba_high_msb = lba >> 40;
5297 #endif
5298 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5299 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5300 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5301 	}
5302 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5303 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5304 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5305 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5306 	scmd->satacmd_features_reg = 0;
5307 	scmd->satacmd_status_reg = 0;
5308 	scmd->satacmd_error_reg = 0;
5309 
5310 	/*
5311 	 * Check if queueing commands should be used and switch
5312 	 * to appropriate command if possible
5313 	 */
5314 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5315 		boolean_t using_queuing;
5316 
5317 		/* Queuing supported by controller and device? */
5318 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5319 		    (sdinfo->satadrv_features_support &
5320 		    SATA_DEV_F_NCQ) &&
5321 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5322 		    SATA_CTLF_NCQ)) {
5323 			using_queuing = B_TRUE;
5324 
5325 			/* NCQ supported - use FPDMA WRITE */
5326 			scmd->satacmd_cmd_reg =
5327 			    SATAC_WRITE_FPDMA_QUEUED;
5328 			scmd->satacmd_features_reg_ext =
5329 			    scmd->satacmd_sec_count_msb;
5330 			scmd->satacmd_sec_count_msb = 0;
5331 		} else if ((sdinfo->satadrv_features_support &
5332 		    SATA_DEV_F_TCQ) &&
5333 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5334 		    SATA_CTLF_QCMD)) {
5335 			using_queuing = B_TRUE;
5336 
5337 			/* Legacy queueing */
5338 			if (sdinfo->satadrv_features_support &
5339 			    SATA_DEV_F_LBA48) {
5340 				scmd->satacmd_cmd_reg =
5341 				    SATAC_WRITE_DMA_QUEUED_EXT;
5342 				scmd->satacmd_features_reg_ext =
5343 				    scmd->satacmd_sec_count_msb;
5344 				scmd->satacmd_sec_count_msb = 0;
5345 			} else {
5346 				scmd->satacmd_cmd_reg =
5347 				    SATAC_WRITE_DMA_QUEUED;
5348 			}
5349 		} else	/*  NCQ nor legacy queuing not supported */
5350 			using_queuing = B_FALSE;
5351 
5352 		if (using_queuing) {
5353 			scmd->satacmd_features_reg =
5354 			    scmd->satacmd_sec_count_lsb;
5355 			scmd->satacmd_sec_count_lsb = 0;
5356 			scmd->satacmd_flags.sata_queued = B_TRUE;
5357 			/* Set-up maximum queue depth */
5358 			scmd->satacmd_flags.sata_max_queue_depth =
5359 			    sdinfo->satadrv_max_queue_depth - 1;
5360 		} else if (sdinfo->satadrv_features_enabled &
5361 		    SATA_DEV_F_E_UNTAGGED_QING) {
5362 			/*
5363 			 * Although NCQ/TCQ is not enabled, untagged queuing
5364 			 * may be still used.
5365 			 * Set-up the maximum untagged queue depth.
5366 			 * Use controller's queue depth from sata_hba_tran.
5367 			 * SATA HBA drivers may ignore this value and rely on
5368 			 * the internal limits. For drivera that do not
5369 			 * ignore untaged queue depth, limit the value to
5370 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5371 			 * largest value that can be passed via
5372 			 * satacmd_flags.sata_max_queue_depth.
5373 			 */
5374 			scmd->satacmd_flags.sata_max_queue_depth =
5375 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5376 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5377 
5378 		} else {
5379 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5380 		}
5381 	} else
5382 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5383 
5384 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5385 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5386 	    scmd->satacmd_cmd_reg, lba, sec_count);
5387 
5388 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5389 		/* Need callback function */
5390 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5391 		synch = FALSE;
5392 	} else
5393 		synch = TRUE;
5394 
5395 	/* Transfer command to HBA */
5396 	if (sata_hba_start(spx, &rval) != 0) {
5397 		/* Pkt not accepted for execution */
5398 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5399 		return (rval);
5400 	}
5401 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5402 
5403 	/*
5404 	 * If execution is non-synchronous,
5405 	 * a callback function will handle potential errors, translate
5406 	 * the response and will do a callback to a target driver.
5407 	 * If it was synchronous, check execution status using the same
5408 	 * framework callback.
5409 	 */
5410 	if (synch) {
5411 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5412 		    "synchronous execution status %x\n",
5413 		    spx->txlt_sata_pkt->satapkt_reason);
5414 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5415 	}
5416 	return (TRAN_ACCEPT);
5417 }
5418 
5419 
5420 /*
5421  * Implements SCSI SBC WRITE BUFFER command download microcode option
5422  */
5423 static int
5424 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5425 {
5426 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5427 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5428 
5429 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
5430 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5431 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
5432 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5433 
5434 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5435 	struct scsi_extended_sense *sense;
5436 	int rval, mode, sector_count, reason;
5437 	int cport = SATA_TXLT_CPORT(spx);
5438 
5439 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5440 
5441 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5442 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5443 
5444 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5445 
5446 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
5447 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5448 		return (rval);
5449 	}
5450 	/*
5451 	 * If in interrupt context, reject this packet because it would issue
5452 	 * a synchronous command to HBA.
5453 	 */
5454 	if (servicing_interrupt()) {
5455 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
5456 		    "sata_txlt_write_buffer: rejecting command because "
5457 		    "of interrupt context\n", NULL);
5458 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5459 		return (TRAN_BUSY);
5460 	}
5461 
5462 	/* Use synchronous mode */
5463 	spx->txlt_sata_pkt->satapkt_op_mode
5464 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
5465 
5466 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5467 
5468 	scsipkt->pkt_reason = CMD_CMPLT;
5469 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5470 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5471 
5472 	/*
5473 	 * The SCSI to ATA translation specification only calls
5474 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5475 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5476 	 * ATA 8 (draft) got rid of download microcode for temp
5477 	 * and it is even optional for ATA 7, so it may be aborted.
5478 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5479 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5480 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5481 	 * sectors.  Thus the offset really doesn't buy us anything.
5482 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5483 	 * is revised, this can be revisisted.
5484 	 */
5485 	/* Reject not supported request */
5486 	switch (mode) {
5487 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5488 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5489 		break;
5490 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5491 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5492 		break;
5493 	default:
5494 		goto bad_param;
5495 	}
5496 
5497 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5498 
5499 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5500 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5501 		goto bad_param;
5502 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5503 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5504 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5505 	scmd->satacmd_lba_mid_lsb = 0;
5506 	scmd->satacmd_lba_high_lsb = 0;
5507 	scmd->satacmd_device_reg = 0;
5508 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5509 	scmd->satacmd_addr_type = 0;
5510 
5511 	/* Transfer command to HBA */
5512 	if (sata_hba_start(spx, &rval) != 0) {
5513 		/* Pkt not accepted for execution */
5514 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5515 		return (rval);
5516 	}
5517 
5518 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5519 
5520 	/* Then we need synchronous check the status of the disk */
5521 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5522 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5523 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5524 		scsipkt->pkt_reason = CMD_CMPLT;
5525 
5526 		/* Download commmand succeed, so probe and identify device */
5527 		sata_reidentify_device(spx);
5528 	} else {
5529 		/* Something went wrong, microcode download command failed */
5530 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5531 		*scsipkt->pkt_scbp = STATUS_CHECK;
5532 		sense = sata_arq_sense(spx);
5533 		switch (sata_pkt->satapkt_reason) {
5534 		case SATA_PKT_PORT_ERROR:
5535 			/*
5536 			 * We have no device data. Assume no data transfered.
5537 			 */
5538 			sense->es_key = KEY_HARDWARE_ERROR;
5539 			break;
5540 
5541 		case SATA_PKT_DEV_ERROR:
5542 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5543 			    SATA_STATUS_ERR) {
5544 				/*
5545 				 * determine dev error reason from error
5546 				 * reg content
5547 				 */
5548 				sata_decode_device_error(spx, sense);
5549 				break;
5550 			}
5551 			/* No extended sense key - no info available */
5552 			break;
5553 
5554 		case SATA_PKT_TIMEOUT:
5555 			scsipkt->pkt_reason = CMD_TIMEOUT;
5556 			scsipkt->pkt_statistics |=
5557 			    STAT_TIMEOUT | STAT_DEV_RESET;
5558 			/* No extended sense key ? */
5559 			break;
5560 
5561 		case SATA_PKT_ABORTED:
5562 			scsipkt->pkt_reason = CMD_ABORTED;
5563 			scsipkt->pkt_statistics |= STAT_ABORTED;
5564 			/* No extended sense key ? */
5565 			break;
5566 
5567 		case SATA_PKT_RESET:
5568 			/* pkt aborted by an explicit reset from a host */
5569 			scsipkt->pkt_reason = CMD_RESET;
5570 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5571 			break;
5572 
5573 		default:
5574 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5575 			    "sata_txlt_nodata_cmd_completion: "
5576 			    "invalid packet completion reason %d",
5577 			    sata_pkt->satapkt_reason));
5578 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5579 			break;
5580 		}
5581 
5582 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5583 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5584 
5585 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5586 			/* scsi callback required */
5587 			scsi_hba_pkt_comp(scsipkt);
5588 	}
5589 	return (TRAN_ACCEPT);
5590 
5591 bad_param:
5592 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5593 	*scsipkt->pkt_scbp = STATUS_CHECK;
5594 	sense = sata_arq_sense(spx);
5595 	sense->es_key = KEY_ILLEGAL_REQUEST;
5596 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5597 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5598 	    scsipkt->pkt_comp != NULL) {
5599 		/* scsi callback required */
5600 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5601 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5602 		    TQ_SLEEP) == 0) {
5603 			/* Scheduling the callback failed */
5604 			rval = TRAN_BUSY;
5605 		}
5606 	}
5607 	return (rval);
5608 }
5609 
5610 /*
5611  * Re-identify device after doing a firmware download.
5612  */
5613 static void
5614 sata_reidentify_device(sata_pkt_txlate_t *spx)
5615 {
5616 #define	DOWNLOAD_WAIT_TIME_SECS	60
5617 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5618 	int rval;
5619 	int retry_cnt;
5620 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5621 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5622 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5623 	sata_drive_info_t *sdinfo;
5624 
5625 	/*
5626 	 * Before returning good status, probe device.
5627 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5628 	 * The assumption is that the new microcode is applied by the
5629 	 * device. It is a caller responsibility to verify this.
5630 	 */
5631 	for (retry_cnt = 0;
5632 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5633 	    retry_cnt++) {
5634 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5635 
5636 		if (rval == SATA_SUCCESS) { /* Set default features */
5637 			sdinfo = sata_get_device_info(sata_hba_inst,
5638 			    &sata_device);
5639 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5640 			    SATA_SUCCESS) {
5641 				/* retry */
5642 				rval = sata_initialize_device(sata_hba_inst,
5643 				    sdinfo);
5644 				if (rval == SATA_RETRY)
5645 					sata_log(sata_hba_inst, CE_WARN,
5646 					    "SATA device at port %d pmport %d -"
5647 					    " default device features could not"
5648 					    " be set. Device may not operate "
5649 					    "as expected.",
5650 					    sata_device.satadev_addr.cport,
5651 					    sata_device.satadev_addr.pmport);
5652 			}
5653 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5654 				scsi_hba_pkt_comp(scsipkt);
5655 			return;
5656 		} else if (rval == SATA_RETRY) {
5657 			delay(drv_usectohz(1000000 *
5658 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5659 			continue;
5660 		} else	/* failed - no reason to retry */
5661 			break;
5662 	}
5663 
5664 	/*
5665 	 * Something went wrong, device probing failed.
5666 	 */
5667 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5668 	    "Cannot probe device after downloading microcode\n"));
5669 
5670 	/* Reset device to force retrying the probe. */
5671 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5672 	    (SATA_DIP(sata_hba_inst), &sata_device);
5673 
5674 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5675 		scsi_hba_pkt_comp(scsipkt);
5676 }
5677 
5678 
5679 /*
5680  * Translate command: Synchronize Cache.
5681  * Translates into Flush Cache command for SATA hard disks.
5682  *
5683  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5684  * appropriate values in scsi_pkt fields.
5685  */
5686 static 	int
5687 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5688 {
5689 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5690 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5691 	int cport = SATA_TXLT_CPORT(spx);
5692 	int rval, reason;
5693 	int synch;
5694 
5695 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5696 
5697 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5698 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5699 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5700 		return (rval);
5701 	}
5702 
5703 	scmd->satacmd_addr_type = 0;
5704 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5705 	scmd->satacmd_device_reg = 0;
5706 	scmd->satacmd_sec_count_lsb = 0;
5707 	scmd->satacmd_lba_low_lsb = 0;
5708 	scmd->satacmd_lba_mid_lsb = 0;
5709 	scmd->satacmd_lba_high_lsb = 0;
5710 	scmd->satacmd_features_reg = 0;
5711 	scmd->satacmd_status_reg = 0;
5712 	scmd->satacmd_error_reg = 0;
5713 
5714 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5715 	    "sata_txlt_synchronize_cache\n", NULL);
5716 
5717 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5718 		/* Need to set-up a callback function */
5719 		spx->txlt_sata_pkt->satapkt_comp =
5720 		    sata_txlt_nodata_cmd_completion;
5721 		synch = FALSE;
5722 	} else
5723 		synch = TRUE;
5724 
5725 	/* Transfer command to HBA */
5726 	if (sata_hba_start(spx, &rval) != 0) {
5727 		/* Pkt not accepted for execution */
5728 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5729 		return (rval);
5730 	}
5731 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5732 
5733 	/*
5734 	 * If execution non-synchronous, it had to be completed
5735 	 * a callback function will handle potential errors, translate
5736 	 * the response and will do a callback to a target driver.
5737 	 * If it was synchronous, check status, using the same
5738 	 * framework callback.
5739 	 */
5740 	if (synch) {
5741 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5742 		    "synchronous execution status %x\n",
5743 		    spx->txlt_sata_pkt->satapkt_reason);
5744 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5745 	}
5746 	return (TRAN_ACCEPT);
5747 }
5748 
5749 
5750 /*
5751  * Send pkt to SATA HBA driver
5752  *
5753  * This function may be called only if the operation is requested by scsi_pkt,
5754  * i.e. scsi_pkt is not NULL.
5755  *
5756  * This function has to be called with cport mutex held. It does release
5757  * the mutex when it calls HBA driver sata_tran_start function and
5758  * re-acquires it afterwards.
5759  *
5760  * If return value is 0, pkt was accepted, -1 otherwise
5761  * rval is set to appropriate sata_scsi_start return value.
5762  *
5763  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5764  * have called the sata_pkt callback function for this packet.
5765  *
5766  * The scsi callback has to be performed by the caller of this routine.
5767  *
5768  * Note 2: No port multiplier support for now.
5769  */
5770 static int
5771 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5772 {
5773 	int stat, cport;
5774 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5775 	sata_drive_info_t *sdinfo;
5776 	sata_device_t *sata_device;
5777 	uint8_t cmd;
5778 	struct sata_cmd_flags cmd_flags;
5779 
5780 	ASSERT(spx->txlt_sata_pkt != NULL);
5781 
5782 	cport = SATA_TXLT_CPORT(spx);
5783 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5784 
5785 	sdinfo = sata_get_device_info(sata_hba_inst,
5786 	    &spx->txlt_sata_pkt->satapkt_device);
5787 	ASSERT(sdinfo != NULL);
5788 
5789 	/* Clear device reset state? */
5790 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5791 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5792 		    sata_clear_dev_reset = B_TRUE;
5793 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5794 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5795 		    "sata_hba_start: clearing device reset state\n", NULL);
5796 	}
5797 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5798 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5799 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5800 
5801 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5802 
5803 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5804 	    "Sata cmd 0x%2x\n", cmd);
5805 
5806 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5807 	    spx->txlt_sata_pkt);
5808 
5809 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5810 	/*
5811 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5812 	 * with the sata callback, the sata_pkt could be already destroyed
5813 	 * by the time we check ther return status from the hba_start()
5814 	 * function, because sata_scsi_destroy_pkt() could have been already
5815 	 * called (perhaps in the interrupt context). So, in such case, there
5816 	 * should be no references to it. In other cases, sata_pkt still
5817 	 * exists.
5818 	 */
5819 	if (stat == SATA_TRAN_ACCEPTED) {
5820 		/*
5821 		 * pkt accepted for execution.
5822 		 * If it was executed synchronously, it is already completed
5823 		 * and pkt completion_reason indicates completion status.
5824 		 */
5825 		*rval = TRAN_ACCEPT;
5826 		return (0);
5827 	}
5828 
5829 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5830 	switch (stat) {
5831 	case SATA_TRAN_QUEUE_FULL:
5832 		/*
5833 		 * Controller detected queue full condition.
5834 		 */
5835 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5836 		    "sata_hba_start: queue full\n", NULL);
5837 
5838 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5839 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5840 
5841 		*rval = TRAN_BUSY;
5842 		break;
5843 
5844 	case SATA_TRAN_PORT_ERROR:
5845 		/*
5846 		 * Communication/link with device or general port error
5847 		 * detected before pkt execution begun.
5848 		 */
5849 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5850 		    SATA_ADDR_CPORT ||
5851 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5852 		    SATA_ADDR_DCPORT)
5853 			sata_log(sata_hba_inst, CE_CONT,
5854 			    "SATA port %d error",
5855 			    sata_device->satadev_addr.cport);
5856 		else
5857 			sata_log(sata_hba_inst, CE_CONT,
5858 			    "SATA port %d pmport %d error\n",
5859 			    sata_device->satadev_addr.cport,
5860 			    sata_device->satadev_addr.pmport);
5861 
5862 		/*
5863 		 * Update the port/device structure.
5864 		 * sata_pkt should be still valid. Since port error is
5865 		 * returned, sata_device content should reflect port
5866 		 * state - it means, that sata address have been changed,
5867 		 * because original packet's sata address refered to a device
5868 		 * attached to some port.
5869 		 */
5870 		sata_update_port_info(sata_hba_inst, sata_device);
5871 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5872 		*rval = TRAN_FATAL_ERROR;
5873 		break;
5874 
5875 	case SATA_TRAN_CMD_UNSUPPORTED:
5876 		/*
5877 		 * Command rejected by HBA as unsupported. It was HBA driver
5878 		 * that rejected the command, command was not sent to
5879 		 * an attached device.
5880 		 */
5881 		if ((sdinfo != NULL) &&
5882 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5883 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5884 			    "sat_hba_start: cmd 0x%2x rejected "
5885 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5886 
5887 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5888 		(void) sata_txlt_invalid_command(spx);
5889 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5890 
5891 		*rval = TRAN_ACCEPT;
5892 		break;
5893 
5894 	case SATA_TRAN_BUSY:
5895 		/*
5896 		 * Command rejected by HBA because other operation prevents
5897 		 * accepting the packet, or device is in RESET condition.
5898 		 */
5899 		if (sdinfo != NULL) {
5900 			sdinfo->satadrv_state =
5901 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5902 
5903 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5904 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5905 				    "sata_hba_start: cmd 0x%2x rejected "
5906 				    "because of device reset condition\n",
5907 				    cmd);
5908 			} else {
5909 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5910 				    "sata_hba_start: cmd 0x%2x rejected "
5911 				    "with SATA_TRAN_BUSY status\n",
5912 				    cmd);
5913 			}
5914 		}
5915 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5916 		*rval = TRAN_BUSY;
5917 		break;
5918 
5919 	default:
5920 		/* Unrecognized HBA response */
5921 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5922 		    "sata_hba_start: unrecognized HBA response "
5923 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5924 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5925 		*rval = TRAN_FATAL_ERROR;
5926 		break;
5927 	}
5928 
5929 	/*
5930 	 * If we got here, the packet was rejected.
5931 	 * Check if we need to remember reset state clearing request
5932 	 */
5933 	if (cmd_flags.sata_clear_dev_reset) {
5934 		/*
5935 		 * Check if device is still configured - it may have
5936 		 * disapeared from the configuration
5937 		 */
5938 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5939 		if (sdinfo != NULL) {
5940 			/*
5941 			 * Restore the flag that requests clearing of
5942 			 * the device reset state,
5943 			 * so the next sata packet may carry it to HBA.
5944 			 */
5945 			sdinfo->satadrv_event_flags |=
5946 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5947 		}
5948 	}
5949 	return (-1);
5950 }
5951 
5952 /*
5953  * Scsi response setup for invalid LBA
5954  *
5955  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5956  */
5957 static int
5958 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5959 {
5960 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5961 	struct scsi_extended_sense *sense;
5962 
5963 	scsipkt->pkt_reason = CMD_CMPLT;
5964 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5965 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5966 	*scsipkt->pkt_scbp = STATUS_CHECK;
5967 
5968 	*scsipkt->pkt_scbp = STATUS_CHECK;
5969 	sense = sata_arq_sense(spx);
5970 	sense->es_key = KEY_ILLEGAL_REQUEST;
5971 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5972 
5973 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5974 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5975 
5976 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5977 	    scsipkt->pkt_comp != NULL)
5978 		/* scsi callback required */
5979 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5980 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5981 		    TQ_SLEEP) == NULL)
5982 			/* Scheduling the callback failed */
5983 			return (TRAN_BUSY);
5984 	return (TRAN_ACCEPT);
5985 }
5986 
5987 
5988 /*
5989  * Analyze device status and error registers and translate them into
5990  * appropriate scsi sense codes.
5991  * NOTE: non-packet commands only for now
5992  */
5993 static void
5994 sata_decode_device_error(sata_pkt_txlate_t *spx,
5995     struct scsi_extended_sense *sense)
5996 {
5997 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5998 
5999 	ASSERT(sense != NULL);
6000 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6001 	    SATA_STATUS_ERR);
6002 
6003 
6004 	if (err_reg & SATA_ERROR_ICRC) {
6005 		sense->es_key = KEY_ABORTED_COMMAND;
6006 		sense->es_add_code = 0x08; /* Communication failure */
6007 		return;
6008 	}
6009 
6010 	if (err_reg & SATA_ERROR_UNC) {
6011 		sense->es_key = KEY_MEDIUM_ERROR;
6012 		/* Information bytes (LBA) need to be set by a caller */
6013 		return;
6014 	}
6015 
6016 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6017 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6018 		sense->es_key = KEY_UNIT_ATTENTION;
6019 		sense->es_add_code = 0x3a; /* No media present */
6020 		return;
6021 	}
6022 
6023 	if (err_reg & SATA_ERROR_IDNF) {
6024 		if (err_reg & SATA_ERROR_ABORT) {
6025 			sense->es_key = KEY_ABORTED_COMMAND;
6026 		} else {
6027 			sense->es_key = KEY_ILLEGAL_REQUEST;
6028 			sense->es_add_code = 0x21; /* LBA out of range */
6029 		}
6030 		return;
6031 	}
6032 
6033 	if (err_reg & SATA_ERROR_ABORT) {
6034 		ASSERT(spx->txlt_sata_pkt != NULL);
6035 		sense->es_key = KEY_ABORTED_COMMAND;
6036 		return;
6037 	}
6038 }
6039 
6040 /*
6041  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6042  */
6043 static void
6044 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6045 {
6046 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6047 
6048 	*lba = 0;
6049 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6050 		*lba = sata_cmd->satacmd_lba_high_msb;
6051 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6052 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6053 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6054 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6055 	}
6056 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6057 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6058 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6059 }
6060 
6061 /*
6062  * This is fixed sense format - if LBA exceeds the info field size,
6063  * no valid info will be returned (valid bit in extended sense will
6064  * be set to 0).
6065  */
6066 static struct scsi_extended_sense *
6067 sata_arq_sense(sata_pkt_txlate_t *spx)
6068 {
6069 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6070 	struct scsi_arq_status *arqs;
6071 	struct scsi_extended_sense *sense;
6072 
6073 	/* Fill ARQ sense data */
6074 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6075 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6076 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6077 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6078 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6079 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6080 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6081 	arqs->sts_rqpkt_resid = 0;
6082 	sense = &arqs->sts_sensedata;
6083 	bzero(sense, sizeof (struct scsi_extended_sense));
6084 	sata_fixed_sense_data_preset(sense);
6085 	return (sense);
6086 }
6087 
6088 
6089 /*
6090  * Emulated SATA Read/Write command completion for zero-length requests.
6091  * This request always succedes, so in synchronous mode it always returns
6092  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6093  * callback cannot be scheduled.
6094  */
6095 static int
6096 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6097 {
6098 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6099 
6100 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6101 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6102 	scsipkt->pkt_reason = CMD_CMPLT;
6103 	*scsipkt->pkt_scbp = STATUS_GOOD;
6104 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6105 		/* scsi callback required - have to schedule it */
6106 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6107 		    (task_func_t *)scsipkt->pkt_comp,
6108 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6109 			/* Scheduling the callback failed */
6110 			return (TRAN_BUSY);
6111 	}
6112 	return (TRAN_ACCEPT);
6113 }
6114 
6115 
6116 /*
6117  * Translate completion status of SATA read/write commands into scsi response.
6118  * pkt completion_reason is checked to determine the completion status.
6119  * Do scsi callback if necessary.
6120  *
6121  * Note: this function may be called also for synchronously executed
6122  * commands.
6123  * This function may be used only if scsi_pkt is non-NULL.
6124  */
6125 static void
6126 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6127 {
6128 	sata_pkt_txlate_t *spx =
6129 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6130 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6131 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6132 	struct scsi_extended_sense *sense;
6133 	uint64_t lba;
6134 	struct buf *bp;
6135 	int rval;
6136 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6137 		/* Normal completion */
6138 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6139 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6140 		scsipkt->pkt_reason = CMD_CMPLT;
6141 		*scsipkt->pkt_scbp = STATUS_GOOD;
6142 		if (spx->txlt_tmp_buf != NULL) {
6143 			/* Temporary buffer was used */
6144 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6145 			if (bp->b_flags & B_READ) {
6146 				rval = ddi_dma_sync(
6147 				    spx->txlt_buf_dma_handle, 0, 0,
6148 				    DDI_DMA_SYNC_FORCPU);
6149 				ASSERT(rval == DDI_SUCCESS);
6150 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6151 				    bp->b_bcount);
6152 			}
6153 		}
6154 	} else {
6155 		/*
6156 		 * Something went wrong - analyze return
6157 		 */
6158 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6159 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6160 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6161 		*scsipkt->pkt_scbp = STATUS_CHECK;
6162 		sense = sata_arq_sense(spx);
6163 		ASSERT(sense != NULL);
6164 
6165 		/*
6166 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6167 		 * extract from device registers the failing LBA.
6168 		 */
6169 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6170 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6171 			    (scmd->satacmd_lba_mid_msb != 0 ||
6172 			    scmd->satacmd_lba_high_msb != 0)) {
6173 				/*
6174 				 * We have problem reporting this cmd LBA
6175 				 * in fixed sense data format, because of
6176 				 * the size of the scsi LBA fields.
6177 				 */
6178 				sense->es_valid = 0;
6179 			} else {
6180 				sata_extract_error_lba(spx, &lba);
6181 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6182 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6183 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6184 				sense->es_info_4 = lba & 0xFF;
6185 			}
6186 		} else {
6187 			/* Invalid extended sense info */
6188 			sense->es_valid = 0;
6189 		}
6190 
6191 		switch (sata_pkt->satapkt_reason) {
6192 		case SATA_PKT_PORT_ERROR:
6193 			/* We may want to handle DEV GONE state as well */
6194 			/*
6195 			 * We have no device data. Assume no data transfered.
6196 			 */
6197 			sense->es_key = KEY_HARDWARE_ERROR;
6198 			break;
6199 
6200 		case SATA_PKT_DEV_ERROR:
6201 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6202 			    SATA_STATUS_ERR) {
6203 				/*
6204 				 * determine dev error reason from error
6205 				 * reg content
6206 				 */
6207 				sata_decode_device_error(spx, sense);
6208 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6209 					switch (scmd->satacmd_cmd_reg) {
6210 					case SATAC_READ_DMA:
6211 					case SATAC_READ_DMA_EXT:
6212 					case SATAC_READ_DMA_QUEUED:
6213 					case SATAC_READ_DMA_QUEUED_EXT:
6214 					case SATAC_READ_FPDMA_QUEUED:
6215 						/* Unrecovered read error */
6216 						sense->es_add_code =
6217 						    SD_SCSI_ASC_UNREC_READ_ERR;
6218 						break;
6219 					case SATAC_WRITE_DMA:
6220 					case SATAC_WRITE_DMA_EXT:
6221 					case SATAC_WRITE_DMA_QUEUED:
6222 					case SATAC_WRITE_DMA_QUEUED_EXT:
6223 					case SATAC_WRITE_FPDMA_QUEUED:
6224 						/* Write error */
6225 						sense->es_add_code =
6226 						    SD_SCSI_ASC_WRITE_ERR;
6227 						break;
6228 					default:
6229 						/* Internal error */
6230 						SATA_LOG_D((
6231 						    spx->txlt_sata_hba_inst,
6232 						    CE_WARN,
6233 						    "sata_txlt_rw_completion :"
6234 						    "internal error - invalid "
6235 						    "command 0x%2x",
6236 						    scmd->satacmd_cmd_reg));
6237 						break;
6238 					}
6239 				}
6240 				break;
6241 			}
6242 			/* No extended sense key - no info available */
6243 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6244 			break;
6245 
6246 		case SATA_PKT_TIMEOUT:
6247 			scsipkt->pkt_reason = CMD_TIMEOUT;
6248 			scsipkt->pkt_statistics |=
6249 			    STAT_TIMEOUT | STAT_DEV_RESET;
6250 			sense->es_key = KEY_ABORTED_COMMAND;
6251 			break;
6252 
6253 		case SATA_PKT_ABORTED:
6254 			scsipkt->pkt_reason = CMD_ABORTED;
6255 			scsipkt->pkt_statistics |= STAT_ABORTED;
6256 			sense->es_key = KEY_ABORTED_COMMAND;
6257 			break;
6258 
6259 		case SATA_PKT_RESET:
6260 			scsipkt->pkt_reason = CMD_RESET;
6261 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6262 			sense->es_key = KEY_ABORTED_COMMAND;
6263 			break;
6264 
6265 		default:
6266 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6267 			    "sata_txlt_rw_completion: "
6268 			    "invalid packet completion reason"));
6269 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6270 			break;
6271 		}
6272 	}
6273 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6274 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6275 
6276 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6277 		/* scsi callback required */
6278 		scsi_hba_pkt_comp(scsipkt);
6279 }
6280 
6281 
6282 /*
6283  * Translate completion status of non-data commands (i.e. commands returning
6284  * no data).
6285  * pkt completion_reason is checked to determine the completion status.
6286  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6287  *
6288  * Note: this function may be called also for synchronously executed
6289  * commands.
6290  * This function may be used only if scsi_pkt is non-NULL.
6291  */
6292 
6293 static	void
6294 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6295 {
6296 	sata_pkt_txlate_t *spx =
6297 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6298 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6299 
6300 	sata_set_arq_data(sata_pkt);
6301 
6302 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6303 		/* scsi callback required */
6304 		scsi_hba_pkt_comp(scsipkt);
6305 }
6306 
6307 static	void
6308 sata_set_arq_data(sata_pkt_t *sata_pkt)
6309 {
6310 	sata_pkt_txlate_t *spx =
6311 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6312 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6313 	struct scsi_extended_sense *sense;
6314 
6315 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6316 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6317 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6318 		/* Normal completion */
6319 		scsipkt->pkt_reason = CMD_CMPLT;
6320 		*scsipkt->pkt_scbp = STATUS_GOOD;
6321 	} else {
6322 		/* Something went wrong */
6323 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6324 		*scsipkt->pkt_scbp = STATUS_CHECK;
6325 		sense = sata_arq_sense(spx);
6326 		switch (sata_pkt->satapkt_reason) {
6327 		case SATA_PKT_PORT_ERROR:
6328 			/*
6329 			 * We have no device data. Assume no data transfered.
6330 			 */
6331 			sense->es_key = KEY_HARDWARE_ERROR;
6332 			break;
6333 
6334 		case SATA_PKT_DEV_ERROR:
6335 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6336 			    SATA_STATUS_ERR) {
6337 				/*
6338 				 * determine dev error reason from error
6339 				 * reg content
6340 				 */
6341 				sata_decode_device_error(spx, sense);
6342 				break;
6343 			}
6344 			/* No extended sense key - no info available */
6345 			break;
6346 
6347 		case SATA_PKT_TIMEOUT:
6348 			scsipkt->pkt_reason = CMD_TIMEOUT;
6349 			scsipkt->pkt_statistics |=
6350 			    STAT_TIMEOUT | STAT_DEV_RESET;
6351 			/* No extended sense key ? */
6352 			break;
6353 
6354 		case SATA_PKT_ABORTED:
6355 			scsipkt->pkt_reason = CMD_ABORTED;
6356 			scsipkt->pkt_statistics |= STAT_ABORTED;
6357 			/* No extended sense key ? */
6358 			break;
6359 
6360 		case SATA_PKT_RESET:
6361 			/* pkt aborted by an explicit reset from a host */
6362 			scsipkt->pkt_reason = CMD_RESET;
6363 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6364 			break;
6365 
6366 		default:
6367 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6368 			    "sata_txlt_nodata_cmd_completion: "
6369 			    "invalid packet completion reason %d",
6370 			    sata_pkt->satapkt_reason));
6371 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6372 			break;
6373 		}
6374 
6375 	}
6376 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6377 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6378 }
6379 
6380 
6381 /*
6382  * Build Mode sense R/W recovery page
6383  * NOT IMPLEMENTED
6384  */
6385 
6386 static int
6387 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6388 {
6389 #ifndef __lock_lint
6390 	_NOTE(ARGUNUSED(sdinfo))
6391 	_NOTE(ARGUNUSED(pcntrl))
6392 	_NOTE(ARGUNUSED(buf))
6393 #endif
6394 	return (0);
6395 }
6396 
6397 /*
6398  * Build Mode sense caching page  -  scsi-3 implementation.
6399  * Page length distinguishes previous format from scsi-3 format.
6400  * buf must have space for 0x12 bytes.
6401  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6402  *
6403  */
6404 static int
6405 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6406 {
6407 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6408 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6409 
6410 	/*
6411 	 * Most of the fields are set to 0, being not supported and/or disabled
6412 	 */
6413 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6414 
6415 	/* Saved paramters not supported */
6416 	if (pcntrl == 3)
6417 		return (0);
6418 	if (pcntrl == 0 || pcntrl == 2) {
6419 		/*
6420 		 * For now treat current and default parameters as same
6421 		 * That may have to change, if target driver will complain
6422 		 */
6423 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6424 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6425 
6426 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6427 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
6428 			page->dra = 1;		/* Read Ahead disabled */
6429 			page->rcd = 1;		/* Read Cache disabled */
6430 		}
6431 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
6432 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
6433 			page->wce = 1;		/* Write Cache enabled */
6434 	} else {
6435 		/* Changeable parameters */
6436 		page->mode_page.code = MODEPAGE_CACHING;
6437 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6438 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6439 			page->dra = 1;
6440 			page->rcd = 1;
6441 		}
6442 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
6443 			page->wce = 1;
6444 	}
6445 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6446 	    sizeof (struct mode_page));
6447 }
6448 
6449 /*
6450  * Build Mode sense exception cntrl page
6451  */
6452 static int
6453 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6454 {
6455 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6456 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6457 
6458 	/*
6459 	 * Most of the fields are set to 0, being not supported and/or disabled
6460 	 */
6461 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6462 
6463 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6464 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6465 
6466 	/* Indicate that this is page is saveable */
6467 	page->mode_page.ps = 1;
6468 
6469 	/*
6470 	 * We will return the same data for default, current and saved page.
6471 	 * The only changeable bit is dexcpt and that bit is required
6472 	 * by the ATA specification to be preserved across power cycles.
6473 	 */
6474 	if (pcntrl != 1) {
6475 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6476 		page->mrie = MRIE_ONLY_ON_REQUEST;
6477 	}
6478 	else
6479 		page->dexcpt = 1;	/* Only changeable parameter */
6480 
6481 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
6482 }
6483 
6484 
6485 static int
6486 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6487 {
6488 	struct mode_acoustic_management *page =
6489 	    (struct mode_acoustic_management *)buf;
6490 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6491 
6492 	/*
6493 	 * Most of the fields are set to 0, being not supported and/or disabled
6494 	 */
6495 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6496 
6497 	switch (pcntrl) {
6498 	case P_CNTRL_DEFAULT:
6499 		/*  default paramters not supported */
6500 		return (0);
6501 
6502 	case P_CNTRL_CURRENT:
6503 	case P_CNTRL_SAVED:
6504 		/* Saved and current are supported and are identical */
6505 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6506 		page->mode_page.length =
6507 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6508 		page->mode_page.ps = 1;
6509 
6510 		/* Word 83 indicates if feature is supported */
6511 		/* If feature is not supported */
6512 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6513 			page->acoustic_manag_enable =
6514 			    ACOUSTIC_DISABLED;
6515 		} else {
6516 			page->acoustic_manag_enable =
6517 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6518 			    != 0);
6519 			/* Word 94 inidicates the value */
6520 #ifdef	_LITTLE_ENDIAN
6521 			page->acoustic_manag_level =
6522 			    (uchar_t)sata_id->ai_acoustic;
6523 			page->vendor_recommended_value =
6524 			    sata_id->ai_acoustic >> 8;
6525 #else
6526 			page->acoustic_manag_level =
6527 			    sata_id->ai_acoustic >> 8;
6528 			page->vendor_recommended_value =
6529 			    (uchar_t)sata_id->ai_acoustic;
6530 #endif
6531 		}
6532 		break;
6533 
6534 	case P_CNTRL_CHANGEABLE:
6535 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6536 		page->mode_page.length =
6537 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6538 		page->mode_page.ps = 1;
6539 
6540 		/* Word 83 indicates if the feature is supported */
6541 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6542 			page->acoustic_manag_enable =
6543 			    ACOUSTIC_ENABLED;
6544 			page->acoustic_manag_level = 0xff;
6545 		}
6546 		break;
6547 	}
6548 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6549 	    sizeof (struct mode_page));
6550 }
6551 
6552 
6553 /*
6554  * Build Mode sense power condition page.
6555  */
6556 static int
6557 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6558 {
6559 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
6560 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6561 
6562 	/*
6563 	 * Most of the fields are set to 0, being not supported and/or disabled
6564 	 * power condition page length was 0x0a
6565 	 */
6566 	bzero(buf, sizeof (struct mode_info_power_cond));
6567 
6568 	if (pcntrl == P_CNTRL_DEFAULT) {
6569 		/*  default paramters not supported */
6570 		return (0);
6571 	}
6572 
6573 	page->mode_page.code = MODEPAGE_POWER_COND;
6574 	page->mode_page.length = sizeof (struct mode_info_power_cond);
6575 
6576 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
6577 		page->standby = 1;
6578 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
6579 		    sizeof (uchar_t) * 4);
6580 	}
6581 
6582 	return (sizeof (struct mode_info_power_cond));
6583 }
6584 
6585 /*
6586  * Process mode select caching page 8 (scsi3 format only).
6587  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6588  * if these features are supported by the device. If these features are not
6589  * supported, quietly ignore them.
6590  * This function fails only if the SET FEATURE command sent to
6591  * the device fails. The page format is not varified, assuming that the
6592  * target driver operates correctly - if parameters length is too short,
6593  * we just drop the page.
6594  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6595  * setting have to be changed.
6596  * SET FEATURE command is executed synchronously, i.e. we wait here until
6597  * it is completed, regardless of the scsi pkt directives.
6598  *
6599  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6600  * changing DRA will change RCD.
6601  *
6602  * More than one SATA command may be executed to perform operations specified
6603  * by mode select pages. The first error terminates further execution.
6604  * Operations performed successully are not backed-up in such case.
6605  *
6606  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6607  * If operation resulted in changing device setup, dmod flag should be set to
6608  * one (1). If parameters were not changed, dmod flag should be set to 0.
6609  * Upon return, if operation required sending command to the device, the rval
6610  * should be set to the value returned by sata_hba_start. If operation
6611  * did not require device access, rval should be set to TRAN_ACCEPT.
6612  * The pagelen should be set to the length of the page.
6613  *
6614  * This function has to be called with a port mutex held.
6615  *
6616  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6617  */
6618 int
6619 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6620     int parmlen, int *pagelen, int *rval, int *dmod)
6621 {
6622 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6623 	sata_drive_info_t *sdinfo;
6624 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6625 	sata_id_t *sata_id;
6626 	struct scsi_extended_sense *sense;
6627 	int wce, dra;	/* Current settings */
6628 
6629 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6630 	    &spx->txlt_sata_pkt->satapkt_device);
6631 	sata_id = &sdinfo->satadrv_id;
6632 	*dmod = 0;
6633 
6634 	/* Verify parameters length. If too short, drop it */
6635 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6636 	    sizeof (struct mode_page) < parmlen) {
6637 		*scsipkt->pkt_scbp = STATUS_CHECK;
6638 		sense = sata_arq_sense(spx);
6639 		sense->es_key = KEY_ILLEGAL_REQUEST;
6640 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6641 		*pagelen = parmlen;
6642 		*rval = TRAN_ACCEPT;
6643 		return (SATA_FAILURE);
6644 	}
6645 
6646 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6647 
6648 	/*
6649 	 * We can manipulate only write cache and read ahead
6650 	 * (read cache) setting.
6651 	 */
6652 	if (!SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6653 	    !SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
6654 		/*
6655 		 * None of the features is supported - ignore
6656 		 */
6657 		*rval = TRAN_ACCEPT;
6658 		return (SATA_SUCCESS);
6659 	}
6660 
6661 	/* Current setting of Read Ahead (and Read Cache) */
6662 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
6663 		dra = 0;	/* 0 == not disabled */
6664 	else
6665 		dra = 1;
6666 	/* Current setting of Write Cache */
6667 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
6668 		wce = 1;
6669 	else
6670 		wce = 0;
6671 
6672 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6673 		/* nothing to do */
6674 		*rval = TRAN_ACCEPT;
6675 		return (SATA_SUCCESS);
6676 	}
6677 
6678 	/*
6679 	 * Need to flip some setting
6680 	 * Set-up Internal SET FEATURES command(s)
6681 	 */
6682 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6683 	scmd->satacmd_addr_type = 0;
6684 	scmd->satacmd_device_reg = 0;
6685 	scmd->satacmd_status_reg = 0;
6686 	scmd->satacmd_error_reg = 0;
6687 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6688 	if (page->dra != dra || page->rcd != dra) {
6689 		/* Need to flip read ahead setting */
6690 		if (dra == 0)
6691 			/* Disable read ahead / read cache */
6692 			scmd->satacmd_features_reg =
6693 			    SATAC_SF_DISABLE_READ_AHEAD;
6694 		else
6695 			/* Enable read ahead  / read cache */
6696 			scmd->satacmd_features_reg =
6697 			    SATAC_SF_ENABLE_READ_AHEAD;
6698 
6699 		/* Transfer command to HBA */
6700 		if (sata_hba_start(spx, rval) != 0)
6701 			/*
6702 			 * Pkt not accepted for execution.
6703 			 */
6704 			return (SATA_FAILURE);
6705 
6706 		*dmod = 1;
6707 
6708 		/* Now process return */
6709 		if (spx->txlt_sata_pkt->satapkt_reason !=
6710 		    SATA_PKT_COMPLETED) {
6711 			goto failure;	/* Terminate */
6712 		}
6713 	}
6714 
6715 	/* Note that the packet is not removed, so it could be re-used */
6716 	if (page->wce != wce) {
6717 		/* Need to flip Write Cache setting */
6718 		if (page->wce == 1)
6719 			/* Enable write cache */
6720 			scmd->satacmd_features_reg =
6721 			    SATAC_SF_ENABLE_WRITE_CACHE;
6722 		else
6723 			/* Disable write cache */
6724 			scmd->satacmd_features_reg =
6725 			    SATAC_SF_DISABLE_WRITE_CACHE;
6726 
6727 		/* Transfer command to HBA */
6728 		if (sata_hba_start(spx, rval) != 0)
6729 			/*
6730 			 * Pkt not accepted for execution.
6731 			 */
6732 			return (SATA_FAILURE);
6733 
6734 		*dmod = 1;
6735 
6736 		/* Now process return */
6737 		if (spx->txlt_sata_pkt->satapkt_reason !=
6738 		    SATA_PKT_COMPLETED) {
6739 			goto failure;
6740 		}
6741 	}
6742 	return (SATA_SUCCESS);
6743 
6744 failure:
6745 	sata_xlate_errors(spx);
6746 
6747 	return (SATA_FAILURE);
6748 }
6749 
6750 /*
6751  * Process mode select informational exceptions control page 0x1c
6752  *
6753  * The only changeable bit is dexcpt (disable exceptions).
6754  * MRIE (method of reporting informational exceptions) must be
6755  * "only on request".
6756  * This page applies to informational exceptions that report
6757  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
6758  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
6759  * Informational exception conditions occur as the result of background scan
6760  * errors, background self-test errors, or vendor specific events within a
6761  * logical unit. An informational exception condition may occur asynchronous
6762  * to any commands.
6763  *
6764  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6765  * If operation resulted in changing device setup, dmod flag should be set to
6766  * one (1). If parameters were not changed, dmod flag should be set to 0.
6767  * Upon return, if operation required sending command to the device, the rval
6768  * should be set to the value returned by sata_hba_start. If operation
6769  * did not require device access, rval should be set to TRAN_ACCEPT.
6770  * The pagelen should be set to the length of the page.
6771  *
6772  * This function has to be called with a port mutex held.
6773  *
6774  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6775  *
6776  * Cannot be called in the interrupt context.
6777  */
6778 static	int
6779 sata_mode_select_page_1c(
6780 	sata_pkt_txlate_t *spx,
6781 	struct mode_info_excpt_page *page,
6782 	int parmlen,
6783 	int *pagelen,
6784 	int *rval,
6785 	int *dmod)
6786 {
6787 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6788 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6789 	sata_drive_info_t *sdinfo;
6790 	sata_id_t *sata_id;
6791 	struct scsi_extended_sense *sense;
6792 
6793 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6794 	    &spx->txlt_sata_pkt->satapkt_device);
6795 	sata_id = &sdinfo->satadrv_id;
6796 
6797 	*dmod = 0;
6798 
6799 	/* Verify parameters length. If too short, drop it */
6800 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6801 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6802 		*scsipkt->pkt_scbp = STATUS_CHECK;
6803 		sense = sata_arq_sense(spx);
6804 		sense->es_key = KEY_ILLEGAL_REQUEST;
6805 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6806 		*pagelen = parmlen;
6807 		*rval = TRAN_ACCEPT;
6808 		return (SATA_FAILURE);
6809 	}
6810 
6811 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6812 
6813 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6814 		*scsipkt->pkt_scbp = STATUS_CHECK;
6815 		sense = sata_arq_sense(spx);
6816 		sense->es_key = KEY_ILLEGAL_REQUEST;
6817 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6818 		*pagelen = parmlen;
6819 		*rval = TRAN_ACCEPT;
6820 		return (SATA_FAILURE);
6821 	}
6822 
6823 	/* If already in the state requested, we are done */
6824 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6825 		/* nothing to do */
6826 		*rval = TRAN_ACCEPT;
6827 		return (SATA_SUCCESS);
6828 	}
6829 
6830 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6831 
6832 	/* Build SMART_ENABLE or SMART_DISABLE command */
6833 	scmd->satacmd_addr_type = 0;		/* N/A */
6834 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6835 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6836 	scmd->satacmd_features_reg = page->dexcpt ?
6837 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6838 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6839 	scmd->satacmd_cmd_reg = SATAC_SMART;
6840 
6841 	/* Transfer command to HBA */
6842 	if (sata_hba_start(spx, rval) != 0)
6843 		/*
6844 		 * Pkt not accepted for execution.
6845 		 */
6846 		return (SATA_FAILURE);
6847 
6848 	*dmod = 1;	/* At least may have been modified */
6849 
6850 	/* Now process return */
6851 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6852 		return (SATA_SUCCESS);
6853 
6854 	/* Packet did not complete successfully */
6855 	sata_xlate_errors(spx);
6856 
6857 	return (SATA_FAILURE);
6858 }
6859 
6860 /*
6861  * Process mode select acoustic management control page 0x30
6862  *
6863  *
6864  * This function has to be called with a port mutex held.
6865  *
6866  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6867  *
6868  * Cannot be called in the interrupt context.
6869  */
6870 int
6871 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6872     mode_acoustic_management *page, int parmlen, int *pagelen,
6873     int *rval, int *dmod)
6874 {
6875 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6876 	sata_drive_info_t *sdinfo;
6877 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6878 	sata_id_t *sata_id;
6879 	struct scsi_extended_sense *sense;
6880 
6881 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6882 	    &spx->txlt_sata_pkt->satapkt_device);
6883 	sata_id = &sdinfo->satadrv_id;
6884 	*dmod = 0;
6885 
6886 	/* If parmlen is too short or the feature is not supported, drop it */
6887 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6888 	    sizeof (struct mode_page)) < parmlen) ||
6889 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6890 		*scsipkt->pkt_scbp = STATUS_CHECK;
6891 		sense = sata_arq_sense(spx);
6892 		sense->es_key = KEY_ILLEGAL_REQUEST;
6893 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6894 		*pagelen = parmlen;
6895 		*rval = TRAN_ACCEPT;
6896 		return (SATA_FAILURE);
6897 	}
6898 
6899 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6900 	    sizeof (struct mode_page);
6901 
6902 	/*
6903 	 * We can enable and disable acoustice management and
6904 	 * set the acoustic management level.
6905 	 */
6906 
6907 	/*
6908 	 * Set-up Internal SET FEATURES command(s)
6909 	 */
6910 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6911 	scmd->satacmd_addr_type = 0;
6912 	scmd->satacmd_device_reg = 0;
6913 	scmd->satacmd_status_reg = 0;
6914 	scmd->satacmd_error_reg = 0;
6915 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6916 	if (page->acoustic_manag_enable) {
6917 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6918 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6919 	} else {	/* disabling acoustic management */
6920 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6921 	}
6922 
6923 	/* Transfer command to HBA */
6924 	if (sata_hba_start(spx, rval) != 0)
6925 		/*
6926 		 * Pkt not accepted for execution.
6927 		 */
6928 		return (SATA_FAILURE);
6929 
6930 	/* Now process return */
6931 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6932 		sata_xlate_errors(spx);
6933 		return (SATA_FAILURE);
6934 	}
6935 
6936 	*dmod = 1;
6937 
6938 	return (SATA_SUCCESS);
6939 }
6940 
6941 /*
6942  * Process mode select power condition page 0x1a
6943  *
6944  * This function has to be called with a port mutex held.
6945  *
6946  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6947  *
6948  * Cannot be called in the interrupt context.
6949  */
6950 int
6951 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
6952     mode_info_power_cond *page, int parmlen, int *pagelen,
6953     int *rval, int *dmod)
6954 {
6955 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6956 	sata_drive_info_t *sdinfo;
6957 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6958 	sata_id_t *sata_id;
6959 	struct scsi_extended_sense *sense;
6960 	uint8_t ata_count;
6961 	int i, len;
6962 
6963 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6964 	    &spx->txlt_sata_pkt->satapkt_device);
6965 	sata_id = &sdinfo->satadrv_id;
6966 	*dmod = 0;
6967 
6968 	len = sizeof (struct mode_info_power_cond);
6969 	len += sizeof (struct mode_page);
6970 
6971 	/* If parmlen is too short or the feature is not supported, drop it */
6972 	if ((len < parmlen) || (page->idle == 1) ||
6973 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
6974 		*scsipkt->pkt_scbp = STATUS_CHECK;
6975 		sense = sata_arq_sense(spx);
6976 		sense->es_key = KEY_ILLEGAL_REQUEST;
6977 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6978 		*pagelen = parmlen;
6979 		*rval = TRAN_ACCEPT;
6980 		return (SATA_FAILURE);
6981 	}
6982 
6983 	*pagelen = len;
6984 
6985 	/*
6986 	 * Set-up Internal STANDBY command(s)
6987 	 */
6988 	if (page->standby == 0)
6989 		goto out;
6990 
6991 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
6992 
6993 	scmd->satacmd_addr_type = 0;
6994 	scmd->satacmd_sec_count_lsb = ata_count;
6995 	scmd->satacmd_lba_low_lsb = 0;
6996 	scmd->satacmd_lba_mid_lsb = 0;
6997 	scmd->satacmd_lba_high_lsb = 0;
6998 	scmd->satacmd_features_reg = 0;
6999 	scmd->satacmd_device_reg = 0;
7000 	scmd->satacmd_status_reg = 0;
7001 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
7002 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
7003 
7004 	/* Transfer command to HBA */
7005 	if (sata_hba_start(spx, rval) != 0) {
7006 		return (SATA_FAILURE);
7007 	} else {
7008 		if ((scmd->satacmd_error_reg != 0) ||
7009 		    (spx->txlt_sata_pkt->satapkt_reason !=
7010 		    SATA_PKT_COMPLETED)) {
7011 			sata_xlate_errors(spx);
7012 			return (SATA_FAILURE);
7013 		}
7014 	}
7015 
7016 	for (i = 0; i < 4; i++) {
7017 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
7018 	}
7019 out:
7020 	*dmod = 1;
7021 	return (SATA_SUCCESS);
7022 }
7023 
7024 /*
7025  * sata_build_lsense_page0() is used to create the
7026  * SCSI LOG SENSE page 0 (supported log pages)
7027  *
7028  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
7029  * (supported log pages, self-test results, informational exceptions
7030  * Sun vendor specific ATA SMART data, and start stop cycle counter).
7031  *
7032  * Takes a sata_drive_info t * and the address of a buffer
7033  * in which to create the page information.
7034  *
7035  * Returns the number of bytes valid in the buffer.
7036  */
7037 static	int
7038 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7039 {
7040 	struct log_parameter *lpp = (struct log_parameter *)buf;
7041 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7042 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7043 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7044 
7045 	lpp->param_code[0] = 0;
7046 	lpp->param_code[1] = 0;
7047 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7048 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7049 
7050 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7051 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7052 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7053 			++num_pages_supported;
7054 		}
7055 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7056 		++num_pages_supported;
7057 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7058 		++num_pages_supported;
7059 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
7060 		++num_pages_supported;
7061 	}
7062 
7063 	lpp->param_len = num_pages_supported;
7064 
7065 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7066 	    num_pages_supported);
7067 }
7068 
7069 /*
7070  * sata_build_lsense_page_10() is used to create the
7071  * SCSI LOG SENSE page 0x10 (self-test results)
7072  *
7073  * Takes a sata_drive_info t * and the address of a buffer
7074  * in which to create the page information as well as a sata_hba_inst_t *.
7075  *
7076  * Returns the number of bytes valid in the buffer.
7077  *
7078  * Note: Self test and SMART data is accessible in device log pages.
7079  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
7080  * of data can be transferred by a single command), or by the General Purpose
7081  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
7082  * - approximately 33MB - can be transferred by a single command.
7083  * The SCT Command response (either error or command) is the same for both
7084  * the SMART and GPL methods of issuing commands.
7085  * This function uses READ LOG EXT command when drive supports LBA48, and
7086  * SMART READ command otherwise.
7087  *
7088  * Since above commands are executed in a synchronous mode, this function
7089  * should not be called in an interrupt context.
7090  */
7091 static	int
7092 sata_build_lsense_page_10(
7093 	sata_drive_info_t *sdinfo,
7094 	uint8_t *buf,
7095 	sata_hba_inst_t *sata_hba_inst)
7096 {
7097 	struct log_parameter *lpp = (struct log_parameter *)buf;
7098 	int rval;
7099 
7100 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7101 		struct smart_ext_selftest_log *ext_selftest_log;
7102 
7103 		ext_selftest_log = kmem_zalloc(
7104 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7105 
7106 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7107 		    ext_selftest_log, 0);
7108 		if (rval == 0) {
7109 			int index, start_index;
7110 			struct smart_ext_selftest_log_entry *entry;
7111 			static const struct smart_ext_selftest_log_entry empty =
7112 			    {0};
7113 			uint16_t block_num;
7114 			int count;
7115 			boolean_t only_one_block = B_FALSE;
7116 
7117 			index = ext_selftest_log->
7118 			    smart_ext_selftest_log_index[0];
7119 			index |= ext_selftest_log->
7120 			    smart_ext_selftest_log_index[1] << 8;
7121 			if (index == 0)
7122 				goto out;
7123 
7124 			--index;	/* Correct for 0 origin */
7125 			start_index = index;	/* remember where we started */
7126 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7127 			if (block_num != 0) {
7128 				rval = sata_ext_smart_selftest_read_log(
7129 				    sata_hba_inst, sdinfo, ext_selftest_log,
7130 				    block_num);
7131 				if (rval != 0)
7132 					goto out;
7133 			}
7134 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7135 			entry =
7136 			    &ext_selftest_log->
7137 			    smart_ext_selftest_log_entries[index];
7138 
7139 			for (count = 1;
7140 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7141 			    ++count) {
7142 				uint8_t status;
7143 				uint8_t code;
7144 				uint8_t sense_key;
7145 				uint8_t add_sense_code;
7146 				uint8_t add_sense_code_qual;
7147 
7148 				/* If this is an unused entry, we are done */
7149 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7150 					/* Broken firmware on some disks */
7151 					if (index + 1 ==
7152 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7153 						--entry;
7154 						--index;
7155 						if (bcmp(entry, &empty,
7156 						    sizeof (empty)) == 0)
7157 							goto out;
7158 					} else
7159 						goto out;
7160 				}
7161 
7162 				if (only_one_block &&
7163 				    start_index == index)
7164 					goto out;
7165 
7166 				lpp->param_code[0] = 0;
7167 				lpp->param_code[1] = count;
7168 				lpp->param_ctrl_flags =
7169 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7170 				lpp->param_len =
7171 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7172 
7173 				status = entry->smart_ext_selftest_log_status;
7174 				status >>= 4;
7175 				switch (status) {
7176 				case 0:
7177 				default:
7178 					sense_key = KEY_NO_SENSE;
7179 					add_sense_code =
7180 					    SD_SCSI_ASC_NO_ADD_SENSE;
7181 					add_sense_code_qual = 0;
7182 					break;
7183 				case 1:
7184 					sense_key = KEY_ABORTED_COMMAND;
7185 					add_sense_code =
7186 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7187 					add_sense_code_qual = SCSI_COMPONENT_81;
7188 					break;
7189 				case 2:
7190 					sense_key = KEY_ABORTED_COMMAND;
7191 					add_sense_code =
7192 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7193 					add_sense_code_qual = SCSI_COMPONENT_82;
7194 					break;
7195 				case 3:
7196 					sense_key = KEY_ABORTED_COMMAND;
7197 					add_sense_code =
7198 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7199 					add_sense_code_qual = SCSI_COMPONENT_83;
7200 					break;
7201 				case 4:
7202 					sense_key = KEY_HARDWARE_ERROR;
7203 					add_sense_code =
7204 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7205 					add_sense_code_qual = SCSI_COMPONENT_84;
7206 					break;
7207 				case 5:
7208 					sense_key = KEY_HARDWARE_ERROR;
7209 					add_sense_code =
7210 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7211 					add_sense_code_qual = SCSI_COMPONENT_85;
7212 					break;
7213 				case 6:
7214 					sense_key = KEY_HARDWARE_ERROR;
7215 					add_sense_code =
7216 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7217 					add_sense_code_qual = SCSI_COMPONENT_86;
7218 					break;
7219 				case 7:
7220 					sense_key = KEY_MEDIUM_ERROR;
7221 					add_sense_code =
7222 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7223 					add_sense_code_qual = SCSI_COMPONENT_87;
7224 					break;
7225 				case 8:
7226 					sense_key = KEY_HARDWARE_ERROR;
7227 					add_sense_code =
7228 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7229 					add_sense_code_qual = SCSI_COMPONENT_88;
7230 					break;
7231 				}
7232 				code = 0;	/* unspecified */
7233 				status |= (code << 4);
7234 				lpp->param_values[0] = status;
7235 				lpp->param_values[1] = 0; /* unspecified */
7236 				lpp->param_values[2] = entry->
7237 				    smart_ext_selftest_log_timestamp[1];
7238 				lpp->param_values[3] = entry->
7239 				    smart_ext_selftest_log_timestamp[0];
7240 				if (status != 0) {
7241 					lpp->param_values[4] = 0;
7242 					lpp->param_values[5] = 0;
7243 					lpp->param_values[6] = entry->
7244 					    smart_ext_selftest_log_failing_lba
7245 					    [5];
7246 					lpp->param_values[7] = entry->
7247 					    smart_ext_selftest_log_failing_lba
7248 					    [4];
7249 					lpp->param_values[8] = entry->
7250 					    smart_ext_selftest_log_failing_lba
7251 					    [3];
7252 					lpp->param_values[9] = entry->
7253 					    smart_ext_selftest_log_failing_lba
7254 					    [2];
7255 					lpp->param_values[10] = entry->
7256 					    smart_ext_selftest_log_failing_lba
7257 					    [1];
7258 					lpp->param_values[11] = entry->
7259 					    smart_ext_selftest_log_failing_lba
7260 					    [0];
7261 				} else {	/* No bad block address */
7262 					lpp->param_values[4] = 0xff;
7263 					lpp->param_values[5] = 0xff;
7264 					lpp->param_values[6] = 0xff;
7265 					lpp->param_values[7] = 0xff;
7266 					lpp->param_values[8] = 0xff;
7267 					lpp->param_values[9] = 0xff;
7268 					lpp->param_values[10] = 0xff;
7269 					lpp->param_values[11] = 0xff;
7270 				}
7271 
7272 				lpp->param_values[12] = sense_key;
7273 				lpp->param_values[13] = add_sense_code;
7274 				lpp->param_values[14] = add_sense_code_qual;
7275 				lpp->param_values[15] = 0; /* undefined */
7276 
7277 				lpp = (struct log_parameter *)
7278 				    (((uint8_t *)lpp) +
7279 				    SCSI_LOG_PARAM_HDR_LEN +
7280 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7281 
7282 				--index;	/* Back up to previous entry */
7283 				if (index < 0) {
7284 					if (block_num > 0) {
7285 						--block_num;
7286 					} else {
7287 						struct read_log_ext_directory
7288 						    logdir;
7289 
7290 						rval =
7291 						    sata_read_log_ext_directory(
7292 						    sata_hba_inst, sdinfo,
7293 						    &logdir);
7294 						if (rval == -1)
7295 							goto out;
7296 						if ((logdir.read_log_ext_vers
7297 						    [0] == 0) &&
7298 						    (logdir.read_log_ext_vers
7299 						    [1] == 0))
7300 							goto out;
7301 						block_num =
7302 						    logdir.read_log_ext_nblks
7303 						    [EXT_SMART_SELFTEST_LOG_PAGE
7304 						    - 1][0];
7305 						block_num |= logdir.
7306 						    read_log_ext_nblks
7307 						    [EXT_SMART_SELFTEST_LOG_PAGE
7308 						    - 1][1] << 8;
7309 						--block_num;
7310 						only_one_block =
7311 						    (block_num == 0);
7312 					}
7313 					rval = sata_ext_smart_selftest_read_log(
7314 					    sata_hba_inst, sdinfo,
7315 					    ext_selftest_log, block_num);
7316 					if (rval != 0)
7317 						goto out;
7318 
7319 					index =
7320 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7321 					    1;
7322 				}
7323 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7324 				entry = &ext_selftest_log->
7325 				    smart_ext_selftest_log_entries[index];
7326 			}
7327 		}
7328 out:
7329 		kmem_free(ext_selftest_log,
7330 		    sizeof (struct smart_ext_selftest_log));
7331 	} else {
7332 		struct smart_selftest_log *selftest_log;
7333 
7334 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7335 		    KM_SLEEP);
7336 
7337 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7338 		    selftest_log);
7339 
7340 		if (rval == 0) {
7341 			int index;
7342 			int count;
7343 			struct smart_selftest_log_entry *entry;
7344 			static const struct smart_selftest_log_entry empty =
7345 			    { 0 };
7346 
7347 			index = selftest_log->smart_selftest_log_index;
7348 			if (index == 0)
7349 				goto done;
7350 			--index;	/* Correct for 0 origin */
7351 			entry = &selftest_log->
7352 			    smart_selftest_log_entries[index];
7353 			for (count = 1;
7354 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7355 			    ++count) {
7356 				uint8_t status;
7357 				uint8_t code;
7358 				uint8_t sense_key;
7359 				uint8_t add_sense_code;
7360 				uint8_t add_sense_code_qual;
7361 
7362 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7363 					goto done;
7364 
7365 				lpp->param_code[0] = 0;
7366 				lpp->param_code[1] = count;
7367 				lpp->param_ctrl_flags =
7368 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7369 				lpp->param_len =
7370 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7371 
7372 				status = entry->smart_selftest_log_status;
7373 				status >>= 4;
7374 				switch (status) {
7375 				case 0:
7376 				default:
7377 					sense_key = KEY_NO_SENSE;
7378 					add_sense_code =
7379 					    SD_SCSI_ASC_NO_ADD_SENSE;
7380 					break;
7381 				case 1:
7382 					sense_key = KEY_ABORTED_COMMAND;
7383 					add_sense_code =
7384 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7385 					add_sense_code_qual = SCSI_COMPONENT_81;
7386 					break;
7387 				case 2:
7388 					sense_key = KEY_ABORTED_COMMAND;
7389 					add_sense_code =
7390 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7391 					add_sense_code_qual = SCSI_COMPONENT_82;
7392 					break;
7393 				case 3:
7394 					sense_key = KEY_ABORTED_COMMAND;
7395 					add_sense_code =
7396 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7397 					add_sense_code_qual = SCSI_COMPONENT_83;
7398 					break;
7399 				case 4:
7400 					sense_key = KEY_HARDWARE_ERROR;
7401 					add_sense_code =
7402 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7403 					add_sense_code_qual = SCSI_COMPONENT_84;
7404 					break;
7405 				case 5:
7406 					sense_key = KEY_HARDWARE_ERROR;
7407 					add_sense_code =
7408 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7409 					add_sense_code_qual = SCSI_COMPONENT_85;
7410 					break;
7411 				case 6:
7412 					sense_key = KEY_HARDWARE_ERROR;
7413 					add_sense_code =
7414 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7415 					add_sense_code_qual = SCSI_COMPONENT_86;
7416 					break;
7417 				case 7:
7418 					sense_key = KEY_MEDIUM_ERROR;
7419 					add_sense_code =
7420 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7421 					add_sense_code_qual = SCSI_COMPONENT_87;
7422 					break;
7423 				case 8:
7424 					sense_key = KEY_HARDWARE_ERROR;
7425 					add_sense_code =
7426 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7427 					add_sense_code_qual = SCSI_COMPONENT_88;
7428 					break;
7429 				}
7430 				code = 0;	/* unspecified */
7431 				status |= (code << 4);
7432 				lpp->param_values[0] = status;
7433 				lpp->param_values[1] = 0; /* unspecified */
7434 				lpp->param_values[2] = entry->
7435 				    smart_selftest_log_timestamp[1];
7436 				lpp->param_values[3] = entry->
7437 				    smart_selftest_log_timestamp[0];
7438 				if (status != 0) {
7439 					lpp->param_values[4] = 0;
7440 					lpp->param_values[5] = 0;
7441 					lpp->param_values[6] = 0;
7442 					lpp->param_values[7] = 0;
7443 					lpp->param_values[8] = entry->
7444 					    smart_selftest_log_failing_lba[3];
7445 					lpp->param_values[9] = entry->
7446 					    smart_selftest_log_failing_lba[2];
7447 					lpp->param_values[10] = entry->
7448 					    smart_selftest_log_failing_lba[1];
7449 					lpp->param_values[11] = entry->
7450 					    smart_selftest_log_failing_lba[0];
7451 				} else {	/* No block address */
7452 					lpp->param_values[4] = 0xff;
7453 					lpp->param_values[5] = 0xff;
7454 					lpp->param_values[6] = 0xff;
7455 					lpp->param_values[7] = 0xff;
7456 					lpp->param_values[8] = 0xff;
7457 					lpp->param_values[9] = 0xff;
7458 					lpp->param_values[10] = 0xff;
7459 					lpp->param_values[11] = 0xff;
7460 				}
7461 				lpp->param_values[12] = sense_key;
7462 				lpp->param_values[13] = add_sense_code;
7463 				lpp->param_values[14] = add_sense_code_qual;
7464 				lpp->param_values[15] = 0; /* undefined */
7465 
7466 				lpp = (struct log_parameter *)
7467 				    (((uint8_t *)lpp) +
7468 				    SCSI_LOG_PARAM_HDR_LEN +
7469 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7470 				--index;	/* back up to previous entry */
7471 				if (index < 0) {
7472 					index =
7473 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7474 				}
7475 				entry = &selftest_log->
7476 				    smart_selftest_log_entries[index];
7477 			}
7478 		}
7479 done:
7480 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7481 	}
7482 
7483 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7484 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7485 }
7486 
7487 /*
7488  * sata_build_lsense_page_2f() is used to create the
7489  * SCSI LOG SENSE page 0x2f (informational exceptions)
7490  *
7491  * Takes a sata_drive_info t * and the address of a buffer
7492  * in which to create the page information as well as a sata_hba_inst_t *.
7493  *
7494  * Returns the number of bytes valid in the buffer.
7495  *
7496  * Because it invokes function(s) that send synchronously executed command
7497  * to the HBA, it cannot be called in the interrupt context.
7498  */
7499 static	int
7500 sata_build_lsense_page_2f(
7501 	sata_drive_info_t *sdinfo,
7502 	uint8_t *buf,
7503 	sata_hba_inst_t *sata_hba_inst)
7504 {
7505 	struct log_parameter *lpp = (struct log_parameter *)buf;
7506 	int rval;
7507 	uint8_t *smart_data;
7508 	uint8_t temp;
7509 	sata_id_t *sata_id;
7510 #define	SMART_NO_TEMP	0xff
7511 
7512 	lpp->param_code[0] = 0;
7513 	lpp->param_code[1] = 0;
7514 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7515 
7516 	/* Now get the SMART status w.r.t. threshold exceeded */
7517 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7518 	switch (rval) {
7519 	case 1:
7520 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7521 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7522 		break;
7523 	case 0:
7524 	case -1:	/* failed to get data */
7525 		lpp->param_values[0] = 0;	/* No failure predicted */
7526 		lpp->param_values[1] = 0;
7527 		break;
7528 #if defined(SATA_DEBUG)
7529 	default:
7530 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7531 		/* NOTREACHED */
7532 #endif
7533 	}
7534 
7535 	sata_id = &sdinfo->satadrv_id;
7536 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7537 		temp = SMART_NO_TEMP;
7538 	else {
7539 		/* Now get the temperature */
7540 		smart_data = kmem_zalloc(512, KM_SLEEP);
7541 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7542 		    SCT_STATUS_LOG_PAGE, 1);
7543 		if (rval == -1)
7544 			temp = SMART_NO_TEMP;
7545 		else {
7546 			temp = smart_data[200];
7547 			if (temp & 0x80) {
7548 				if (temp & 0x7f)
7549 					temp = 0;
7550 				else
7551 					temp = SMART_NO_TEMP;
7552 			}
7553 		}
7554 		kmem_free(smart_data, 512);
7555 	}
7556 
7557 	lpp->param_values[2] = temp;	/* most recent temperature */
7558 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7559 
7560 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7561 
7562 
7563 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7564 }
7565 
7566 /*
7567  * sata_build_lsense_page_30() is used to create the
7568  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7569  *
7570  * Takes a sata_drive_info t * and the address of a buffer
7571  * in which to create the page information as well as a sata_hba_inst_t *.
7572  *
7573  * Returns the number of bytes valid in the buffer.
7574  */
7575 static int
7576 sata_build_lsense_page_30(
7577 	sata_drive_info_t *sdinfo,
7578 	uint8_t *buf,
7579 	sata_hba_inst_t *sata_hba_inst)
7580 {
7581 	struct smart_data *smart_data = (struct smart_data *)buf;
7582 	int rval;
7583 
7584 	/* Now do the SMART READ DATA */
7585 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7586 	if (rval == -1)
7587 		return (0);
7588 
7589 	return (sizeof (struct smart_data));
7590 }
7591 
7592 /*
7593  * sata_build_lsense_page_0e() is used to create the
7594  * SCSI LOG SENSE page 0e (supported log pages)
7595  *
7596  * Date of Manufacture (0x0001)
7597  *	YEAR = "0000"
7598  *	WEEK = "00"
7599  * Accounting Date (0x0002)
7600  *	6 ASCII space character(20h)
7601  * Specified cycle count over device lifetime
7602  *	VALUE - THRESH - the delta between max and min;
7603  * Accumulated start-stop cycles
7604  *	VALUE - WORST - the accumulated cycles;
7605  *
7606  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
7607  *
7608  * Takes a sata_drive_info t * and the address of a buffer
7609  * in which to create the page information as well as a sata_hba_inst_t *.
7610  *
7611  * Returns the number of bytes valid in the buffer.
7612  */
7613 static	int
7614 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
7615 	sata_pkt_txlate_t *spx)
7616 {
7617 	struct start_stop_cycle_counter_log *log_page;
7618 	int i, rval, index;
7619 	uint8_t smart_data[512], id, value, worst, thresh;
7620 	uint32_t max_count, cycles;
7621 
7622 	/* Now do the SMART READ DATA */
7623 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
7624 	    (struct smart_data *)smart_data);
7625 	if (rval == -1)
7626 		return (0);
7627 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
7628 		index = (i * 12) + 2;
7629 		id = smart_data[index];
7630 		if (id != SMART_START_STOP_COUNT_ID)
7631 			continue;
7632 		else {
7633 			thresh = smart_data[index + 2];
7634 			value = smart_data[index + 3];
7635 			worst = smart_data[index + 4];
7636 			break;
7637 		}
7638 	}
7639 	if (id != SMART_START_STOP_COUNT_ID)
7640 		return (0);
7641 	max_count = value - thresh;
7642 	cycles = value - worst;
7643 
7644 	log_page = (struct start_stop_cycle_counter_log *)buf;
7645 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
7646 	log_page->code = 0x0e;
7647 	log_page->page_len_low = 0x24;
7648 
7649 	log_page->manufactor_date_low = 0x1;
7650 	log_page->param_1.fmt_link = 0x1; /* 01b */
7651 	log_page->param_len_1 = 0x06;
7652 	for (i = 0; i < 4; i++) {
7653 		log_page->year_manu[i] = 0x30;
7654 		if (i < 2)
7655 			log_page->week_manu[i] = 0x30;
7656 	}
7657 
7658 	log_page->account_date_low = 0x02;
7659 	log_page->param_2.fmt_link = 0x01; /* 01b */
7660 	log_page->param_len_2 = 0x06;
7661 	for (i = 0; i < 4; i++) {
7662 		log_page->year_account[i] = 0x20;
7663 		if (i < 2)
7664 			log_page->week_account[i] = 0x20;
7665 	}
7666 
7667 	log_page->lifetime_code_low = 0x03;
7668 	log_page->param_3.fmt_link = 0x03; /* 11b */
7669 	log_page->param_len_3 = 0x04;
7670 	/* VALUE - THRESH - the delta between max and min */
7671 	log_page->cycle_code_low = 0x04;
7672 	log_page->param_4.fmt_link = 0x03; /* 11b */
7673 	log_page->param_len_4 = 0x04;
7674 	/* WORST - THRESH - the distance from 'now' to min */
7675 
7676 	for (i = 0; i < 4; i++) {
7677 		log_page->cycle_lifetime[i] =
7678 		    (max_count >> (8 * (3 - i))) & 0xff;
7679 		log_page->cycle_accumulated[i] =
7680 		    (cycles >> (8 * (3 - i))) & 0xff;
7681 	}
7682 
7683 	return (sizeof (struct start_stop_cycle_counter_log));
7684 }
7685 
7686 /*
7687  * This function was used for build a ATA read verify sector command
7688  */
7689 static void
7690 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
7691 {
7692 	scmd->satacmd_cmd_reg = SATAC_RDVER;
7693 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
7694 
7695 	scmd->satacmd_sec_count_lsb = sec & 0xff;
7696 	scmd->satacmd_lba_low_lsb = lba & 0xff;
7697 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
7698 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
7699 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
7700 	scmd->satacmd_features_reg = 0;
7701 	scmd->satacmd_status_reg = 0;
7702 	scmd->satacmd_error_reg = 0;
7703 }
7704 
7705 /*
7706  * This function was used for building an ATA
7707  * command, and only command register need to
7708  * be defined, other register will be zero or na.
7709  */
7710 static void
7711 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
7712 {
7713 	scmd->satacmd_addr_type = 0;
7714 	scmd->satacmd_cmd_reg = cmd;
7715 	scmd->satacmd_device_reg = 0;
7716 	scmd->satacmd_sec_count_lsb = 0;
7717 	scmd->satacmd_lba_low_lsb = 0;
7718 	scmd->satacmd_lba_mid_lsb = 0;
7719 	scmd->satacmd_lba_high_lsb = 0;
7720 	scmd->satacmd_features_reg = 0;
7721 	scmd->satacmd_status_reg = 0;
7722 	scmd->satacmd_error_reg = 0;
7723 }
7724 
7725 /*
7726  * This function was used for changing the standby
7727  * timer format from SCSI to ATA.
7728  */
7729 static uint8_t
7730 sata_get_standby_timer(uint8_t *timer)
7731 {
7732 	uint32_t i = 0, count = 0;
7733 	uint8_t ata_count;
7734 
7735 	for (i = 0; i < 4; i++) {
7736 		count = count << 8 | timer[i];
7737 	}
7738 
7739 	if (count == 0)
7740 		return (0);
7741 
7742 	if (count >= 1 && count <= 12000)
7743 		ata_count = (count -1) / 50 + 1;
7744 	else if (count > 12000 && count <= 12600)
7745 		ata_count = 0xfc;
7746 	else if (count > 12601 && count <= 12750)
7747 		ata_count = 0xff;
7748 	else if (count > 12750 && count <= 17999)
7749 		ata_count = 0xf1;
7750 	else if (count > 18000 && count <= 198000)
7751 		ata_count = count / 18000 + 240;
7752 	else
7753 		ata_count = 0xfd;
7754 	return (ata_count);
7755 }
7756 
7757 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7758 
7759 /*
7760  * Start command for ATAPI device.
7761  * This function processes scsi_pkt requests.
7762  * Now CD/DVD, tape and ATAPI disk devices are supported.
7763  * Most commands are packet without any translation into Packet Command.
7764  * Some may be trapped and executed as SATA commands (not clear which one).
7765  *
7766  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7767  * execution).
7768  * Returns other TRAN_XXXX codes if command is not accepted or completed
7769  * (see return values for sata_hba_start()).
7770  *
7771  * Note:
7772  * Inquiry cdb format differs between transport version 2 and 3.
7773  * However, the transport version 3 devices that were checked did not adhere
7774  * to the specification (ignored MSB of the allocation length). Therefore,
7775  * the transport version is not checked, but Inquiry allocation length is
7776  * truncated to 255 bytes if the original allocation length set-up by the
7777  * target driver is greater than 255 bytes.
7778  */
7779 static int
7780 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7781 {
7782 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7783 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7784 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7785 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7786 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7787 	    &spx->txlt_sata_pkt->satapkt_device);
7788 	int cport = SATA_TXLT_CPORT(spx);
7789 	int cdblen;
7790 	int rval, reason;
7791 	int synch;
7792 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7793 
7794 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7795 
7796 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
7797 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7798 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7799 		return (rval);
7800 	}
7801 
7802 	/*
7803 	 * ATAPI device executes some ATA commands in addition to those
7804 	 * commands sent via PACKET command. These ATA commands may be
7805 	 * executed by the regular SATA translation functions. None needs
7806 	 * to be captured now.
7807 	 *
7808 	 * Commands sent via PACKET command include:
7809 	 *	MMC command set for ATAPI CD/DVD device
7810 	 *	SSC command set for ATAPI TAPE device
7811 	 *	SBC command set for ATAPI disk device
7812 	 *
7813 	 */
7814 
7815 	/* Check the size of cdb */
7816 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7817 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7818 		sata_log(NULL, CE_WARN,
7819 		    "sata: invalid ATAPI cdb length %d",
7820 		    scsipkt->pkt_cdblen);
7821 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7822 		return (TRAN_BADPKT);
7823 	}
7824 
7825 	SATAATAPITRACE(spx, cdblen);
7826 
7827 	/*
7828 	 * For non-read/write commands we need to
7829 	 * map buffer
7830 	 */
7831 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7832 	case SCMD_READ:
7833 	case SCMD_READ_G1:
7834 	case SCMD_READ_G5:
7835 	case SCMD_READ_G4:
7836 	case SCMD_WRITE:
7837 	case SCMD_WRITE_G1:
7838 	case SCMD_WRITE_G5:
7839 	case SCMD_WRITE_G4:
7840 		break;
7841 	default:
7842 		if (bp != NULL) {
7843 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7844 				bp_mapin(bp);
7845 		}
7846 		break;
7847 	}
7848 	/*
7849 	 * scmd->satacmd_flags.sata_data_direction default -
7850 	 * SATA_DIR_NODATA_XFER - is set by
7851 	 * sata_txlt_generic_pkt_info().
7852 	 */
7853 	if (scmd->satacmd_bp) {
7854 		if (scmd->satacmd_bp->b_flags & B_READ) {
7855 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7856 		} else {
7857 			scmd->satacmd_flags.sata_data_direction =
7858 			    SATA_DIR_WRITE;
7859 		}
7860 	}
7861 
7862 	/*
7863 	 * Set up ATAPI packet command.
7864 	 */
7865 
7866 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7867 
7868 	/* Copy cdb into sata_cmd */
7869 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7870 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7871 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7872 
7873 	/* See note in the command header */
7874 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7875 		if (scmd->satacmd_acdb[3] != 0)
7876 			scmd->satacmd_acdb[4] = 255;
7877 	}
7878 
7879 #ifdef SATA_DEBUG
7880 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7881 		uint8_t *p = scmd->satacmd_acdb;
7882 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7883 
7884 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7885 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7886 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7887 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7888 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7889 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7890 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7891 	}
7892 #endif
7893 
7894 	/*
7895 	 * Preset request sense data to NO SENSE.
7896 	 * If there is no way to get error information via Request Sense,
7897 	 * the packet request sense data would not have to be modified by HBA,
7898 	 * but it could be returned as is.
7899 	 */
7900 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7901 	sata_fixed_sense_data_preset(
7902 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7903 
7904 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7905 		/* Need callback function */
7906 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7907 		synch = FALSE;
7908 	} else
7909 		synch = TRUE;
7910 
7911 	/* Transfer command to HBA */
7912 	if (sata_hba_start(spx, &rval) != 0) {
7913 		/* Pkt not accepted for execution */
7914 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7915 		return (rval);
7916 	}
7917 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7918 	/*
7919 	 * If execution is non-synchronous,
7920 	 * a callback function will handle potential errors, translate
7921 	 * the response and will do a callback to a target driver.
7922 	 * If it was synchronous, use the same framework callback to check
7923 	 * an execution status.
7924 	 */
7925 	if (synch) {
7926 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7927 		    "synchronous execution status %x\n",
7928 		    spx->txlt_sata_pkt->satapkt_reason);
7929 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7930 	}
7931 	return (TRAN_ACCEPT);
7932 }
7933 
7934 
7935 /*
7936  * ATAPI Packet command completion.
7937  *
7938  * Failure of the command passed via Packet command are considered device
7939  * error. SATA HBA driver would have to retrieve error data (via Request
7940  * Sense command delivered via error retrieval sata packet) and copy it
7941  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7942  */
7943 static void
7944 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7945 {
7946 	sata_pkt_txlate_t *spx =
7947 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7948 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7949 	struct scsi_extended_sense *sense;
7950 	struct buf *bp;
7951 	int rval;
7952 
7953 #ifdef SATA_DEBUG
7954 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7955 #endif
7956 
7957 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7958 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7959 
7960 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7961 		/* Normal completion */
7962 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7963 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7964 		scsipkt->pkt_reason = CMD_CMPLT;
7965 		*scsipkt->pkt_scbp = STATUS_GOOD;
7966 		if (spx->txlt_tmp_buf != NULL) {
7967 			/* Temporary buffer was used */
7968 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7969 			if (bp->b_flags & B_READ) {
7970 				rval = ddi_dma_sync(
7971 				    spx->txlt_buf_dma_handle, 0, 0,
7972 				    DDI_DMA_SYNC_FORCPU);
7973 				ASSERT(rval == DDI_SUCCESS);
7974 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7975 				    bp->b_bcount);
7976 			}
7977 		}
7978 	} else {
7979 		/*
7980 		 * Something went wrong - analyze return
7981 		 */
7982 		*scsipkt->pkt_scbp = STATUS_CHECK;
7983 		sense = sata_arq_sense(spx);
7984 
7985 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7986 			/*
7987 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
7988 			 * Under this condition ERR bit is set for ATA command,
7989 			 * and CHK bit set for ATAPI command.
7990 			 *
7991 			 * Please check st_intr & sdintr about how pkt_reason
7992 			 * is used.
7993 			 */
7994 			scsipkt->pkt_reason = CMD_CMPLT;
7995 
7996 			/*
7997 			 * We may not have ARQ data if there was a double
7998 			 * error. But sense data in sata packet was pre-set
7999 			 * with NO SENSE so it is valid even if HBA could
8000 			 * not retrieve a real sense data.
8001 			 * Just copy this sense data into scsi pkt sense area.
8002 			 */
8003 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8004 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8005 #ifdef SATA_DEBUG
8006 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8007 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8008 				    "sata_txlt_atapi_completion: %02x\n"
8009 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8010 				    "          %02x %02x %02x %02x %02x %02x "
8011 				    "          %02x %02x %02x %02x %02x %02x\n",
8012 				    scsipkt->pkt_reason,
8013 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8014 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8015 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8016 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8017 				    rqsp[16], rqsp[17]);
8018 			}
8019 #endif
8020 		} else {
8021 			switch (sata_pkt->satapkt_reason) {
8022 			case SATA_PKT_PORT_ERROR:
8023 				/*
8024 				 * We have no device data.
8025 				 */
8026 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8027 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8028 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8029 				    STATE_GOT_STATUS);
8030 				sense->es_key = KEY_HARDWARE_ERROR;
8031 				break;
8032 
8033 			case SATA_PKT_TIMEOUT:
8034 				scsipkt->pkt_reason = CMD_TIMEOUT;
8035 				scsipkt->pkt_statistics |=
8036 				    STAT_TIMEOUT | STAT_DEV_RESET;
8037 				/*
8038 				 * Need to check if HARDWARE_ERROR/
8039 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8040 				 * appropriate.
8041 				 */
8042 				break;
8043 
8044 			case SATA_PKT_ABORTED:
8045 				scsipkt->pkt_reason = CMD_ABORTED;
8046 				scsipkt->pkt_statistics |= STAT_ABORTED;
8047 				/* Should we set key COMMAND_ABPRTED? */
8048 				break;
8049 
8050 			case SATA_PKT_RESET:
8051 				scsipkt->pkt_reason = CMD_RESET;
8052 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
8053 				/*
8054 				 * May be we should set Unit Attention /
8055 				 * Reset. Perhaps the same should be
8056 				 * returned for disks....
8057 				 */
8058 				sense->es_key = KEY_UNIT_ATTENTION;
8059 				sense->es_add_code = SD_SCSI_ASC_RESET;
8060 				break;
8061 
8062 			default:
8063 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8064 				    "sata_txlt_atapi_completion: "
8065 				    "invalid packet completion reason"));
8066 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8067 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8068 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8069 				    STATE_GOT_STATUS);
8070 				break;
8071 			}
8072 		}
8073 	}
8074 
8075 	SATAATAPITRACE(spx, 0);
8076 
8077 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8078 	    scsipkt->pkt_comp != NULL) {
8079 		/* scsi callback required */
8080 		(*scsipkt->pkt_comp)(scsipkt);
8081 	}
8082 }
8083 
8084 /*
8085  * Set up error retrieval sata command for ATAPI Packet Command error data
8086  * recovery.
8087  *
8088  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8089  * returns SATA_FAILURE otherwise.
8090  */
8091 
8092 static int
8093 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8094 {
8095 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8096 	sata_cmd_t *scmd;
8097 	struct buf *bp;
8098 
8099 	/*
8100 	 * Allocate dma-able buffer error data.
8101 	 * Buffer allocation will take care of buffer alignment and other DMA
8102 	 * attributes.
8103 	 */
8104 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8105 	if (bp == NULL) {
8106 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8107 		    "sata_get_err_retrieval_pkt: "
8108 		    "cannot allocate buffer for error data", NULL);
8109 		return (SATA_FAILURE);
8110 	}
8111 	bp_mapin(bp); /* make data buffer accessible */
8112 
8113 	/* Operation modes are up to the caller */
8114 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8115 
8116 	/* Synchronous mode, no callback - may be changed by the caller */
8117 	spkt->satapkt_comp = NULL;
8118 	spkt->satapkt_time = sata_default_pkt_time;
8119 
8120 	scmd = &spkt->satapkt_cmd;
8121 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8122 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8123 
8124 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8125 
8126 	/*
8127 	 * Set-up acdb. Request Sense CDB (packet command content) is
8128 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8129 	 * it is transfered into packet FIS).
8130 	 */
8131 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8132 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8133 	/* Following zeroing of pad bytes may not be necessary */
8134 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8135 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8136 
8137 	/*
8138 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8139 	 * before accessing it. Handle is in usual place in translate struct.
8140 	 */
8141 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8142 
8143 	/*
8144 	 * Preset request sense data to NO SENSE.
8145 	 * Here it is redundant, only for a symetry with scsi-originated
8146 	 * packets. It should not be used for anything but debugging.
8147 	 */
8148 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8149 	sata_fixed_sense_data_preset(
8150 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8151 
8152 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8153 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8154 
8155 	return (SATA_SUCCESS);
8156 }
8157 
8158 /*
8159  * Set-up ATAPI packet command.
8160  * Data transfer direction has to be set-up in sata_cmd structure prior to
8161  * calling this function.
8162  *
8163  * Returns void
8164  */
8165 
8166 static void
8167 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8168 {
8169 	scmd->satacmd_addr_type = 0;		/* N/A */
8170 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8171 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8172 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8173 	scmd->satacmd_lba_high_lsb =
8174 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8175 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8176 
8177 	/*
8178 	 * We want all data to be transfered via DMA.
8179 	 * But specify it only if drive supports DMA and DMA mode is
8180 	 * selected - some drives are sensitive about it.
8181 	 * Hopefully it wil work for all drives....
8182 	 */
8183 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8184 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8185 
8186 	/*
8187 	 * Features register requires special care for devices that use
8188 	 * Serial ATA bridge - they need an explicit specification of
8189 	 * the data transfer direction for Packet DMA commands.
8190 	 * Setting this bit is harmless if DMA is not used.
8191 	 *
8192 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8193 	 * spec they follow.
8194 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8195 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8196 	 * ATA/ATAPI-7 support is explicitly indicated.
8197 	 */
8198 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8199 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8200 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8201 		/*
8202 		 * Specification of major version is valid and version 7
8203 		 * is supported. It does automatically imply that all
8204 		 * spec features are supported. For now, we assume that
8205 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8206 		 */
8207 		if ((sdinfo->satadrv_id.ai_dirdma &
8208 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8209 			if (scmd->satacmd_flags.sata_data_direction ==
8210 			    SATA_DIR_READ)
8211 			scmd->satacmd_features_reg |=
8212 			    SATA_ATAPI_F_DATA_DIR_READ;
8213 		}
8214 	}
8215 }
8216 
8217 
8218 #ifdef SATA_DEBUG
8219 
8220 /* Display 18 bytes of Inquiry data */
8221 static void
8222 sata_show_inqry_data(uint8_t *buf)
8223 {
8224 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8225 	uint8_t *p;
8226 
8227 	cmn_err(CE_NOTE, "Inquiry data:");
8228 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8229 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8230 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8231 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8232 	    SATA_ATAPI_TRANS_VERSION(inq));
8233 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8234 	    inq->inq_rdf, inq->inq_aenc);
8235 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8236 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8237 	p = (uint8_t *)inq->inq_vid;
8238 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8239 	    "%02x %02x %02x %02x",
8240 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8241 	p = (uint8_t *)inq->inq_vid;
8242 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8243 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8244 
8245 	p = (uint8_t *)inq->inq_pid;
8246 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8247 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8248 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8249 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8250 	p = (uint8_t *)inq->inq_pid;
8251 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8252 	    "%c %c %c %c %c %c %c %c",
8253 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8254 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8255 
8256 	p = (uint8_t *)inq->inq_revision;
8257 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8258 	    p[0], p[1], p[2], p[3]);
8259 	p = (uint8_t *)inq->inq_revision;
8260 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8261 	    p[0], p[1], p[2], p[3]);
8262 
8263 }
8264 
8265 
8266 static void
8267 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8268 {
8269 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8270 
8271 	if (scsi_pkt == NULL)
8272 		return;
8273 	if (count != 0) {
8274 		/* saving cdb */
8275 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8276 		    SATA_ATAPI_MAX_CDB_LEN);
8277 		bcopy(scsi_pkt->pkt_cdbp,
8278 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8279 	} else {
8280 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8281 		    sts_sensedata,
8282 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8283 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8284 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8285 		    scsi_pkt->pkt_reason;
8286 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8287 		    spx->txlt_sata_pkt->satapkt_reason;
8288 
8289 		if (++sata_atapi_trace_index >= 64)
8290 			sata_atapi_trace_index = 0;
8291 	}
8292 }
8293 
8294 #endif
8295 
8296 /*
8297  * Fetch inquiry data from ATAPI device
8298  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8299  *
8300  * Note:
8301  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
8302  * where the caller expects to see the inquiry data.
8303  *
8304  */
8305 
8306 static int
8307 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8308     sata_address_t *saddr, struct scsi_inquiry *inq)
8309 {
8310 	sata_pkt_txlate_t *spx;
8311 	sata_pkt_t *spkt;
8312 	struct buf *bp;
8313 	sata_drive_info_t *sdinfo;
8314 	sata_cmd_t *scmd;
8315 	int rval;
8316 	uint8_t *rqsp;
8317 #ifdef SATA_DEBUG
8318 	char msg_buf[MAXPATHLEN];
8319 #endif
8320 
8321 	ASSERT(sata_hba != NULL);
8322 
8323 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8324 	spx->txlt_sata_hba_inst = sata_hba;
8325 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8326 	spkt = sata_pkt_alloc(spx, NULL);
8327 	if (spkt == NULL) {
8328 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8329 		return (SATA_FAILURE);
8330 	}
8331 	/* address is needed now */
8332 	spkt->satapkt_device.satadev_addr = *saddr;
8333 
8334 	/* scsi_inquiry size buffer */
8335 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8336 	if (bp == NULL) {
8337 		sata_pkt_free(spx);
8338 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8339 		SATA_LOG_D((sata_hba, CE_WARN,
8340 		    "sata_get_atapi_inquiry_data: "
8341 		    "cannot allocate data buffer"));
8342 		return (SATA_FAILURE);
8343 	}
8344 	bp_mapin(bp); /* make data buffer accessible */
8345 
8346 	scmd = &spkt->satapkt_cmd;
8347 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8348 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8349 
8350 	/* Use synchronous mode */
8351 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8352 	spkt->satapkt_comp = NULL;
8353 	spkt->satapkt_time = sata_default_pkt_time;
8354 
8355 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8356 
8357 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8358 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8359 
8360 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8361 	sdinfo = sata_get_device_info(sata_hba,
8362 	    &spx->txlt_sata_pkt->satapkt_device);
8363 	if (sdinfo == NULL) {
8364 		/* we have to be carefull about the disapearing device */
8365 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8366 		rval = SATA_FAILURE;
8367 		goto cleanup;
8368 	}
8369 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8370 
8371 	/*
8372 	 * Set-up acdb. This works for atapi transport version 2 and later.
8373 	 */
8374 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8375 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8376 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8377 	scmd->satacmd_acdb[1] = 0x00;
8378 	scmd->satacmd_acdb[2] = 0x00;
8379 	scmd->satacmd_acdb[3] = 0x00;
8380 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8381 	scmd->satacmd_acdb[5] = 0x00;
8382 
8383 	sata_fixed_sense_data_preset(
8384 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8385 
8386 	/* Transfer command to HBA */
8387 	if (sata_hba_start(spx, &rval) != 0) {
8388 		/* Pkt not accepted for execution */
8389 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8390 		    "sata_get_atapi_inquiry_data: "
8391 		    "Packet not accepted for execution - ret: %02x", rval);
8392 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8393 		rval = SATA_FAILURE;
8394 		goto cleanup;
8395 	}
8396 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8397 
8398 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8399 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8400 		    "sata_get_atapi_inquiry_data: "
8401 		    "Packet completed successfully - ret: %02x", rval);
8402 		if (spx->txlt_buf_dma_handle != NULL) {
8403 			/*
8404 			 * Sync buffer. Handle is in usual place in translate
8405 			 * struct.
8406 			 */
8407 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8408 			    DDI_DMA_SYNC_FORCPU);
8409 			ASSERT(rval == DDI_SUCCESS);
8410 		}
8411 		/*
8412 		 * Normal completion - copy data into caller's buffer
8413 		 */
8414 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8415 		    sizeof (struct scsi_inquiry));
8416 #ifdef SATA_DEBUG
8417 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8418 			sata_show_inqry_data((uint8_t *)inq);
8419 		}
8420 #endif
8421 		rval = SATA_SUCCESS;
8422 	} else {
8423 		/*
8424 		 * Something went wrong - analyze return - check rqsense data
8425 		 */
8426 		rval = SATA_FAILURE;
8427 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8428 			/*
8429 			 * ARQ data hopefull show something other than NO SENSE
8430 			 */
8431 			rqsp = scmd->satacmd_rqsense;
8432 #ifdef SATA_DEBUG
8433 			if (sata_debug_flags & SATA_DBG_ATAPI) {
8434 				msg_buf[0] = '\0';
8435 				(void) snprintf(msg_buf, MAXPATHLEN,
8436 				    "ATAPI packet completion reason: %02x\n"
8437 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
8438 				    "          %02x %02x %02x %02x %02x %02x\n"
8439 				    "          %02x %02x %02x %02x %02x %02x",
8440 				    spkt->satapkt_reason,
8441 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8442 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8443 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8444 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8445 				    rqsp[16], rqsp[17]);
8446 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8447 				    "%s", msg_buf);
8448 			}
8449 #endif
8450 		} else {
8451 			switch (spkt->satapkt_reason) {
8452 			case SATA_PKT_PORT_ERROR:
8453 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8454 				    "sata_get_atapi_inquiry_data: "
8455 				    "packet reason: port error", NULL);
8456 				break;
8457 
8458 			case SATA_PKT_TIMEOUT:
8459 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8460 				    "sata_get_atapi_inquiry_data: "
8461 				    "packet reason: timeout", NULL);
8462 				break;
8463 
8464 			case SATA_PKT_ABORTED:
8465 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8466 				    "sata_get_atapi_inquiry_data: "
8467 				    "packet reason: aborted", NULL);
8468 				break;
8469 
8470 			case SATA_PKT_RESET:
8471 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8472 				    "sata_get_atapi_inquiry_data: "
8473 				    "packet reason: reset\n", NULL);
8474 				break;
8475 			default:
8476 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8477 				    "sata_get_atapi_inquiry_data: "
8478 				    "invalid packet reason: %02x\n",
8479 				    spkt->satapkt_reason);
8480 				break;
8481 			}
8482 		}
8483 	}
8484 cleanup:
8485 	sata_free_local_buffer(spx);
8486 	sata_pkt_free(spx);
8487 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8488 	return (rval);
8489 }
8490 
8491 
8492 
8493 
8494 
8495 #if 0
8496 #ifdef SATA_DEBUG
8497 
8498 /*
8499  * Test ATAPI packet command.
8500  * Single threaded test: send packet command in synch mode, process completion
8501  *
8502  */
8503 static void
8504 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8505 {
8506 	sata_pkt_txlate_t *spx;
8507 	sata_pkt_t *spkt;
8508 	struct buf *bp;
8509 	sata_device_t sata_device;
8510 	sata_drive_info_t *sdinfo;
8511 	sata_cmd_t *scmd;
8512 	int rval;
8513 	uint8_t *rqsp;
8514 
8515 	ASSERT(sata_hba_inst != NULL);
8516 	sata_device.satadev_addr.cport = cport;
8517 	sata_device.satadev_addr.pmport = 0;
8518 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8519 	sata_device.satadev_rev = SATA_DEVICE_REV;
8520 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8521 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8522 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8523 	if (sdinfo == NULL) {
8524 		sata_log(sata_hba_inst, CE_WARN,
8525 		    "sata_test_atapi_packet_command: "
8526 		    "no device info for cport %d",
8527 		    sata_device.satadev_addr.cport);
8528 		return;
8529 	}
8530 
8531 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8532 	spx->txlt_sata_hba_inst = sata_hba_inst;
8533 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8534 	spkt = sata_pkt_alloc(spx, NULL);
8535 	if (spkt == NULL) {
8536 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8537 		return;
8538 	}
8539 	/* address is needed now */
8540 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8541 
8542 	/* 1024k buffer */
8543 	bp = sata_alloc_local_buffer(spx, 1024);
8544 	if (bp == NULL) {
8545 		sata_pkt_free(spx);
8546 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8547 		sata_log(sata_hba_inst, CE_WARN,
8548 		    "sata_test_atapi_packet_command: "
8549 		    "cannot allocate data buffer");
8550 		return;
8551 	}
8552 	bp_mapin(bp); /* make data buffer accessible */
8553 
8554 	scmd = &spkt->satapkt_cmd;
8555 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8556 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8557 
8558 	/* Use synchronous mode */
8559 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8560 
8561 	/* Synchronous mode, no callback - may be changed by the caller */
8562 	spkt->satapkt_comp = NULL;
8563 	spkt->satapkt_time = sata_default_pkt_time;
8564 
8565 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8566 
8567 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8568 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8569 
8570 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8571 
8572 	/* Set-up acdb. */
8573 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8574 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8575 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8576 	scmd->satacmd_acdb[1] = 0x00;
8577 	scmd->satacmd_acdb[2] = 0x00;
8578 	scmd->satacmd_acdb[3] = 0x00;
8579 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8580 	scmd->satacmd_acdb[5] = 0x00;
8581 
8582 	sata_fixed_sense_data_preset(
8583 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8584 
8585 	/* Transfer command to HBA */
8586 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8587 	if (sata_hba_start(spx, &rval) != 0) {
8588 		/* Pkt not accepted for execution */
8589 		sata_log(sata_hba_inst, CE_WARN,
8590 		    "sata_test_atapi_packet_command: "
8591 		    "Packet not accepted for execution - ret: %02x", rval);
8592 		mutex_exit(
8593 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8594 		goto cleanup;
8595 	}
8596 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8597 
8598 	if (spx->txlt_buf_dma_handle != NULL) {
8599 		/*
8600 		 * Sync buffer. Handle is in usual place in translate struct.
8601 		 */
8602 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8603 		    DDI_DMA_SYNC_FORCPU);
8604 		ASSERT(rval == DDI_SUCCESS);
8605 	}
8606 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8607 		sata_log(sata_hba_inst, CE_WARN,
8608 		    "sata_test_atapi_packet_command: "
8609 		    "Packet completed successfully");
8610 		/*
8611 		 * Normal completion - show inquiry data
8612 		 */
8613 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8614 	} else {
8615 		/*
8616 		 * Something went wrong - analyze return - check rqsense data
8617 		 */
8618 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8619 			/*
8620 			 * ARQ data hopefull show something other than NO SENSE
8621 			 */
8622 			rqsp = scmd->satacmd_rqsense;
8623 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8624 			    "ATAPI packet completion reason: %02x\n"
8625 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8626 			    "          %02x %02x %02x %02x %02x %02x "
8627 			    "          %02x %02x %02x %02x %02x %02x\n",
8628 			    spkt->satapkt_reason,
8629 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8630 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8631 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8632 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8633 			    rqsp[16], rqsp[17]);
8634 		} else {
8635 			switch (spkt->satapkt_reason) {
8636 			case SATA_PKT_PORT_ERROR:
8637 				sata_log(sata_hba_inst, CE_WARN,
8638 				    "sata_test_atapi_packet_command: "
8639 				    "packet reason: port error\n");
8640 				break;
8641 
8642 			case SATA_PKT_TIMEOUT:
8643 				sata_log(sata_hba_inst, CE_WARN,
8644 				    "sata_test_atapi_packet_command: "
8645 				    "packet reason: timeout\n");
8646 				break;
8647 
8648 			case SATA_PKT_ABORTED:
8649 				sata_log(sata_hba_inst, CE_WARN,
8650 				    "sata_test_atapi_packet_command: "
8651 				    "packet reason: aborted\n");
8652 				break;
8653 
8654 			case SATA_PKT_RESET:
8655 				sata_log(sata_hba_inst, CE_WARN,
8656 				    "sata_test_atapi_packet_command: "
8657 				    "packet reason: reset\n");
8658 				break;
8659 			default:
8660 				sata_log(sata_hba_inst, CE_WARN,
8661 				    "sata_test_atapi_packet_command: "
8662 				    "invalid packet reason: %02x\n",
8663 				    spkt->satapkt_reason);
8664 				break;
8665 			}
8666 		}
8667 	}
8668 cleanup:
8669 	sata_free_local_buffer(spx);
8670 	sata_pkt_free(spx);
8671 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8672 }
8673 
8674 #endif /* SATA_DEBUG */
8675 #endif /* 1 */
8676 
8677 
8678 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
8679 
8680 /*
8681  * Validate sata_tran info
8682  * SATA_FAILURE returns if structure is inconsistent or structure revision
8683  * does not match one used by the framework.
8684  *
8685  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
8686  * required function pointers.
8687  * Returns SATA_FAILURE otherwise.
8688  */
8689 static int
8690 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
8691 {
8692 	/*
8693 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
8694 	 * of the SATA interface.
8695 	 */
8696 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
8697 		sata_log(NULL, CE_WARN,
8698 		    "sata: invalid sata_hba_tran version %d for driver %s",
8699 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
8700 		return (SATA_FAILURE);
8701 	}
8702 
8703 	if (dip != sata_tran->sata_tran_hba_dip) {
8704 		SATA_LOG_D((NULL, CE_WARN,
8705 		    "sata: inconsistent sata_tran_hba_dip "
8706 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
8707 		return (SATA_FAILURE);
8708 	}
8709 
8710 	if (sata_tran->sata_tran_probe_port == NULL ||
8711 	    sata_tran->sata_tran_start == NULL ||
8712 	    sata_tran->sata_tran_abort == NULL ||
8713 	    sata_tran->sata_tran_reset_dport == NULL ||
8714 	    sata_tran->sata_tran_hotplug_ops == NULL ||
8715 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
8716 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
8717 	    NULL) {
8718 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
8719 		    "required functions"));
8720 	}
8721 	return (SATA_SUCCESS);
8722 }
8723 
8724 /*
8725  * Remove HBA instance from sata_hba_list.
8726  */
8727 static void
8728 sata_remove_hba_instance(dev_info_t *dip)
8729 {
8730 	sata_hba_inst_t	*sata_hba_inst;
8731 
8732 	mutex_enter(&sata_mutex);
8733 	for (sata_hba_inst = sata_hba_list;
8734 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
8735 	    sata_hba_inst = sata_hba_inst->satahba_next) {
8736 		if (sata_hba_inst->satahba_dip == dip)
8737 			break;
8738 	}
8739 
8740 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
8741 #ifdef SATA_DEBUG
8742 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
8743 		    "unknown HBA instance\n");
8744 #endif
8745 		ASSERT(FALSE);
8746 	}
8747 	if (sata_hba_inst == sata_hba_list) {
8748 		sata_hba_list = sata_hba_inst->satahba_next;
8749 		if (sata_hba_list) {
8750 			sata_hba_list->satahba_prev =
8751 			    (struct sata_hba_inst *)NULL;
8752 		}
8753 		if (sata_hba_inst == sata_hba_list_tail) {
8754 			sata_hba_list_tail = NULL;
8755 		}
8756 	} else if (sata_hba_inst == sata_hba_list_tail) {
8757 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
8758 		if (sata_hba_list_tail) {
8759 			sata_hba_list_tail->satahba_next =
8760 			    (struct sata_hba_inst *)NULL;
8761 		}
8762 	} else {
8763 		sata_hba_inst->satahba_prev->satahba_next =
8764 		    sata_hba_inst->satahba_next;
8765 		sata_hba_inst->satahba_next->satahba_prev =
8766 		    sata_hba_inst->satahba_prev;
8767 	}
8768 	mutex_exit(&sata_mutex);
8769 }
8770 
8771 
8772 
8773 
8774 
8775 /*
8776  * Probe all SATA ports of the specified HBA instance.
8777  * The assumption is that there are no target and attachment point minor nodes
8778  * created by the boot subsystems, so we do not need to prune device tree.
8779  *
8780  * This function is called only from sata_hba_attach(). It does not have to
8781  * be protected by controller mutex, because the hba_attached flag is not set
8782  * yet and no one would be touching this HBA instance other than this thread.
8783  * Determines if port is active and what type of the device is attached
8784  * (if any). Allocates necessary structures for each port.
8785  *
8786  * An AP (Attachement Point) node is created for each SATA device port even
8787  * when there is no device attached.
8788  */
8789 
8790 static 	void
8791 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8792 {
8793 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8794 	int			ncport, npmport;
8795 	sata_cport_info_t 	*cportinfo;
8796 	sata_drive_info_t	*drive;
8797 	sata_pmult_info_t	*pminfo;
8798 	sata_pmport_info_t 	*pmportinfo;
8799 	sata_device_t		sata_device;
8800 	int			rval;
8801 	dev_t			minor_number;
8802 	char			name[16];
8803 	clock_t			start_time, cur_time;
8804 
8805 	/*
8806 	 * Probe controller ports first, to find port status and
8807 	 * any port multiplier attached.
8808 	 */
8809 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8810 		/* allocate cport structure */
8811 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8812 		ASSERT(cportinfo != NULL);
8813 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8814 
8815 		mutex_enter(&cportinfo->cport_mutex);
8816 
8817 		cportinfo->cport_addr.cport = ncport;
8818 		cportinfo->cport_addr.pmport = 0;
8819 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8820 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8821 		cportinfo->cport_state |= SATA_STATE_PROBING;
8822 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8823 
8824 		/*
8825 		 * Regardless if a port is usable or not, create
8826 		 * an attachment point
8827 		 */
8828 		mutex_exit(&cportinfo->cport_mutex);
8829 		minor_number =
8830 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8831 		(void) sprintf(name, "%d", ncport);
8832 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8833 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8834 		    DDI_SUCCESS) {
8835 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8836 			    "cannot create SATA attachment point for port %d",
8837 			    ncport);
8838 		}
8839 
8840 		/* Probe port */
8841 		start_time = ddi_get_lbolt();
8842 	reprobe_cport:
8843 		sata_device.satadev_addr.cport = ncport;
8844 		sata_device.satadev_addr.pmport = 0;
8845 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8846 		sata_device.satadev_rev = SATA_DEVICE_REV;
8847 
8848 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8849 		    (dip, &sata_device);
8850 
8851 		mutex_enter(&cportinfo->cport_mutex);
8852 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8853 		if (rval != SATA_SUCCESS) {
8854 			/* Something went wrong? Fail the port */
8855 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8856 			mutex_exit(&cportinfo->cport_mutex);
8857 			continue;
8858 		}
8859 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8860 		cportinfo->cport_state |= SATA_STATE_PROBED;
8861 		cportinfo->cport_dev_type = sata_device.satadev_type;
8862 
8863 		cportinfo->cport_state |= SATA_STATE_READY;
8864 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8865 			mutex_exit(&cportinfo->cport_mutex);
8866 			continue;
8867 		}
8868 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8869 			/*
8870 			 * There is some device attached.
8871 			 * Allocate device info structure
8872 			 */
8873 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8874 				mutex_exit(&cportinfo->cport_mutex);
8875 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8876 				    kmem_zalloc(sizeof (sata_drive_info_t),
8877 				    KM_SLEEP);
8878 				mutex_enter(&cportinfo->cport_mutex);
8879 			}
8880 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8881 			drive->satadrv_addr = cportinfo->cport_addr;
8882 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8883 			drive->satadrv_type = cportinfo->cport_dev_type;
8884 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8885 
8886 			mutex_exit(&cportinfo->cport_mutex);
8887 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8888 			    SATA_SUCCESS) {
8889 				/*
8890 				 * Plugged device was not correctly identified.
8891 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8892 				 */
8893 				cur_time = ddi_get_lbolt();
8894 				if ((cur_time - start_time) <
8895 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8896 					/* sleep for a while */
8897 					delay(drv_usectohz(
8898 					    SATA_DEV_RETRY_DLY));
8899 					goto reprobe_cport;
8900 				}
8901 			}
8902 		} else {
8903 			mutex_exit(&cportinfo->cport_mutex);
8904 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8905 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8906 			    KM_SLEEP);
8907 			mutex_enter(&cportinfo->cport_mutex);
8908 			ASSERT(pminfo != NULL);
8909 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8910 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8911 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8912 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8913 			pminfo->pmult_num_dev_ports =
8914 			    sata_device.satadev_add_info;
8915 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8916 			    NULL);
8917 			pminfo->pmult_state = SATA_STATE_PROBING;
8918 			mutex_exit(&cportinfo->cport_mutex);
8919 
8920 			/* Probe Port Multiplier ports */
8921 			for (npmport = 0;
8922 			    npmport < pminfo->pmult_num_dev_ports;
8923 			    npmport++) {
8924 				pmportinfo = kmem_zalloc(
8925 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8926 				mutex_enter(&cportinfo->cport_mutex);
8927 				ASSERT(pmportinfo != NULL);
8928 				pmportinfo->pmport_addr.cport = ncport;
8929 				pmportinfo->pmport_addr.pmport = npmport;
8930 				pmportinfo->pmport_addr.qual =
8931 				    SATA_ADDR_PMPORT;
8932 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8933 
8934 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8935 				    MUTEX_DRIVER, NULL);
8936 
8937 				mutex_exit(&cportinfo->cport_mutex);
8938 
8939 				/* Create an attachment point */
8940 				minor_number = SATA_MAKE_AP_MINOR(
8941 				    ddi_get_instance(dip), ncport, npmport, 1);
8942 				(void) sprintf(name, "%d.%d", ncport, npmport);
8943 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8944 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8945 				    0) != DDI_SUCCESS) {
8946 					sata_log(sata_hba_inst, CE_WARN,
8947 					    "sata_hba_attach: "
8948 					    "cannot create SATA attachment "
8949 					    "point for port %d pmult port %d",
8950 					    ncport, npmport);
8951 				}
8952 
8953 				start_time = ddi_get_lbolt();
8954 			reprobe_pmport:
8955 				sata_device.satadev_addr.pmport = npmport;
8956 				sata_device.satadev_addr.qual =
8957 				    SATA_ADDR_PMPORT;
8958 
8959 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8960 				    (dip, &sata_device);
8961 				mutex_enter(&cportinfo->cport_mutex);
8962 
8963 				/* sata_update_port_info() */
8964 				sata_update_port_scr(&pmportinfo->pmport_scr,
8965 				    &sata_device);
8966 
8967 				if (rval != SATA_SUCCESS) {
8968 					pmportinfo->pmport_state =
8969 					    SATA_PSTATE_FAILED;
8970 					mutex_exit(&cportinfo->cport_mutex);
8971 					continue;
8972 				}
8973 				pmportinfo->pmport_state &=
8974 				    ~SATA_STATE_PROBING;
8975 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8976 				pmportinfo->pmport_dev_type =
8977 				    sata_device.satadev_type;
8978 
8979 				pmportinfo->pmport_state |= SATA_STATE_READY;
8980 				if (pmportinfo->pmport_dev_type ==
8981 				    SATA_DTYPE_NONE) {
8982 					mutex_exit(&cportinfo->cport_mutex);
8983 					continue;
8984 				}
8985 				/* Port multipliers cannot be chained */
8986 				ASSERT(pmportinfo->pmport_dev_type !=
8987 				    SATA_DTYPE_PMULT);
8988 				/*
8989 				 * There is something attached to Port
8990 				 * Multiplier device port
8991 				 * Allocate device info structure
8992 				 */
8993 				if (pmportinfo->pmport_sata_drive == NULL) {
8994 					mutex_exit(&cportinfo->cport_mutex);
8995 					pmportinfo->pmport_sata_drive =
8996 					    kmem_zalloc(
8997 					    sizeof (sata_drive_info_t),
8998 					    KM_SLEEP);
8999 					mutex_enter(&cportinfo->cport_mutex);
9000 				}
9001 				drive = pmportinfo->pmport_sata_drive;
9002 				drive->satadrv_addr.cport =
9003 				    pmportinfo->pmport_addr.cport;
9004 				drive->satadrv_addr.pmport = npmport;
9005 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9006 				drive->satadrv_type = pmportinfo->
9007 				    pmport_dev_type;
9008 				drive->satadrv_state = SATA_STATE_UNKNOWN;
9009 
9010 				mutex_exit(&cportinfo->cport_mutex);
9011 				if (sata_add_device(dip, sata_hba_inst, ncport,
9012 				    npmport) != SATA_SUCCESS) {
9013 					/*
9014 					 * Plugged device was not correctly
9015 					 * identified. Retry, within the
9016 					 * SATA_DEV_IDENTIFY_TIMEOUT
9017 					 */
9018 					cur_time = ddi_get_lbolt();
9019 					if ((cur_time - start_time) <
9020 					    drv_usectohz(
9021 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
9022 						/* sleep for a while */
9023 						delay(drv_usectohz(
9024 						    SATA_DEV_RETRY_DLY));
9025 						goto reprobe_pmport;
9026 					}
9027 				}
9028 			}
9029 			pmportinfo->pmport_state =
9030 			    SATA_STATE_PROBED | SATA_STATE_READY;
9031 		}
9032 	}
9033 }
9034 
9035 /*
9036  * Add SATA device for specified HBA instance & port (SCSI target
9037  * device nodes).
9038  * This function is called (indirectly) only from sata_hba_attach().
9039  * A target node is created when there is a supported type device attached,
9040  * but may be removed if it cannot be put online.
9041  *
9042  * This function cannot be called from an interrupt context.
9043  *
9044  * ONLY DISK TARGET NODES ARE CREATED NOW
9045  *
9046  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9047  * device identification failed - adding a device could be retried.
9048  *
9049  */
9050 static 	int
9051 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
9052     int pmport)
9053 {
9054 	sata_cport_info_t 	*cportinfo;
9055 	sata_pmult_info_t	*pminfo;
9056 	sata_pmport_info_t	*pmportinfo;
9057 	dev_info_t		*cdip;		/* child dip */
9058 	sata_device_t		sata_device;
9059 	int			rval;
9060 
9061 
9062 
9063 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9064 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9065 	mutex_enter(&cportinfo->cport_mutex);
9066 	/*
9067 	 * Some device is attached to a controller port.
9068 	 * We rely on controllers distinquishing between no-device,
9069 	 * attached port multiplier and other kind of attached device.
9070 	 * We need to get Identify Device data and determine
9071 	 * positively the dev type before trying to attach
9072 	 * the target driver.
9073 	 */
9074 	sata_device.satadev_rev = SATA_DEVICE_REV;
9075 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9076 		/*
9077 		 * Not port multiplier.
9078 		 */
9079 		sata_device.satadev_addr = cportinfo->cport_addr;
9080 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
9081 		mutex_exit(&cportinfo->cport_mutex);
9082 
9083 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9084 		if (rval != SATA_SUCCESS ||
9085 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
9086 			return (SATA_FAILURE);
9087 
9088 		mutex_enter(&cportinfo->cport_mutex);
9089 		sata_show_drive_info(sata_hba_inst,
9090 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9091 
9092 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9093 			/*
9094 			 * Could not determine device type or
9095 			 * a device is not supported.
9096 			 * Degrade this device to unknown.
9097 			 */
9098 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9099 			mutex_exit(&cportinfo->cport_mutex);
9100 			return (SATA_SUCCESS);
9101 		}
9102 		cportinfo->cport_dev_type = sata_device.satadev_type;
9103 		cportinfo->cport_tgtnode_clean = B_TRUE;
9104 		mutex_exit(&cportinfo->cport_mutex);
9105 
9106 		/*
9107 		 * Initialize device to the desired state. Even if it
9108 		 * fails, the device will still attach but syslog
9109 		 * will show the warning.
9110 		 */
9111 		if (sata_initialize_device(sata_hba_inst,
9112 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
9113 			/* Retry */
9114 			rval = sata_initialize_device(sata_hba_inst,
9115 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9116 
9117 			if (rval == SATA_RETRY)
9118 				sata_log(sata_hba_inst, CE_WARN,
9119 				    "SATA device at port %d - "
9120 				    "default device features could not be set."
9121 				    " Device may not operate as expected.",
9122 				    cportinfo->cport_addr.cport);
9123 		}
9124 
9125 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9126 		    &sata_device.satadev_addr);
9127 		mutex_enter(&cportinfo->cport_mutex);
9128 		if (cdip == NULL) {
9129 			/*
9130 			 * Attaching target node failed.
9131 			 * We retain sata_drive_info structure...
9132 			 */
9133 			mutex_exit(&cportinfo->cport_mutex);
9134 			return (SATA_SUCCESS);
9135 		}
9136 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9137 		    satadrv_state = SATA_STATE_READY;
9138 	} else {
9139 		/* This must be Port Multiplier type */
9140 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9141 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9142 			    "sata_add_device: "
9143 			    "unrecognized dev type %x",
9144 			    cportinfo->cport_dev_type));
9145 			mutex_exit(&cportinfo->cport_mutex);
9146 			return (SATA_SUCCESS);
9147 		}
9148 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9149 		pmportinfo = pminfo->pmult_dev_port[pmport];
9150 		sata_device.satadev_addr = pmportinfo->pmport_addr;
9151 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
9152 		mutex_exit(&cportinfo->cport_mutex);
9153 
9154 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9155 		if (rval != SATA_SUCCESS ||
9156 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
9157 			return (SATA_FAILURE);
9158 		}
9159 		mutex_enter(&cportinfo->cport_mutex);
9160 		sata_show_drive_info(sata_hba_inst,
9161 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9162 
9163 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9164 			/*
9165 			 * Could not determine device type.
9166 			 * Degrade this device to unknown.
9167 			 */
9168 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9169 			mutex_exit(&cportinfo->cport_mutex);
9170 			return (SATA_SUCCESS);
9171 		}
9172 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9173 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9174 		mutex_exit(&cportinfo->cport_mutex);
9175 
9176 		/*
9177 		 * Initialize device to the desired state.
9178 		 * Even if it fails, the device will still
9179 		 * attach but syslog will show the warning.
9180 		 */
9181 		if (sata_initialize_device(sata_hba_inst,
9182 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
9183 			/* Retry */
9184 			rval = sata_initialize_device(sata_hba_inst,
9185 			    pmportinfo->pmport_sata_drive);
9186 
9187 			if (rval == SATA_RETRY)
9188 				sata_log(sata_hba_inst, CE_WARN,
9189 				    "SATA device at port %d pmport %d - "
9190 				    "default device features could not be set."
9191 				    " Device may not operate as expected.",
9192 				    pmportinfo->pmport_addr.cport,
9193 				    pmportinfo->pmport_addr.pmport);
9194 		}
9195 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9196 		    &sata_device.satadev_addr);
9197 		mutex_enter(&cportinfo->cport_mutex);
9198 		if (cdip == NULL) {
9199 			/*
9200 			 * Attaching target node failed.
9201 			 * We retain sata_drive_info structure...
9202 			 */
9203 			mutex_exit(&cportinfo->cport_mutex);
9204 			return (SATA_SUCCESS);
9205 		}
9206 		pmportinfo->pmport_sata_drive->satadrv_state |=
9207 		    SATA_STATE_READY;
9208 	}
9209 	mutex_exit(&cportinfo->cport_mutex);
9210 	return (SATA_SUCCESS);
9211 }
9212 
9213 
9214 
9215 /*
9216  * Create scsi target node for attached device, create node properties and
9217  * attach the node.
9218  * The node could be removed if the device onlining fails.
9219  *
9220  * A dev_info_t pointer is returned if operation is successful, NULL is
9221  * returned otherwise.
9222  *
9223  * No port multiplier support.
9224  */
9225 
9226 static dev_info_t *
9227 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9228 			sata_address_t *sata_addr)
9229 {
9230 	dev_info_t *cdip = NULL;
9231 	int rval;
9232 	char *nname = NULL;
9233 	char **compatible = NULL;
9234 	int ncompatible;
9235 	struct scsi_inquiry inq;
9236 	sata_device_t sata_device;
9237 	sata_drive_info_t *sdinfo;
9238 	int target;
9239 	int i;
9240 
9241 	sata_device.satadev_rev = SATA_DEVICE_REV;
9242 	sata_device.satadev_addr = *sata_addr;
9243 
9244 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9245 
9246 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9247 
9248 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9249 	    sata_addr->pmport, sata_addr->qual);
9250 
9251 	if (sdinfo == NULL) {
9252 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9253 		    sata_addr->cport)));
9254 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9255 		    "sata_create_target_node: no sdinfo for target %x",
9256 		    target));
9257 		return (NULL);
9258 	}
9259 
9260 	/*
9261 	 * create or get scsi inquiry data, expected by
9262 	 * scsi_hba_nodename_compatible_get()
9263 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9264 	 * ATAPI devices respond directly to Inquiry request.
9265 	 */
9266 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9267 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9268 		    (uint8_t *)&inq);
9269 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9270 		    sata_addr->cport)));
9271 	} else { /* Assume supported ATAPI device */
9272 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9273 		    sata_addr->cport)));
9274 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9275 		    &inq) == SATA_FAILURE)
9276 			return (NULL);
9277 		/*
9278 		 * Save supported ATAPI transport version
9279 		 */
9280 		sdinfo->satadrv_atapi_trans_ver =
9281 		    SATA_ATAPI_TRANS_VERSION(&inq);
9282 	}
9283 
9284 	/* determine the node name and compatible */
9285 	scsi_hba_nodename_compatible_get(&inq, NULL,
9286 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9287 
9288 #ifdef SATA_DEBUG
9289 	if (sata_debug_flags & SATA_DBG_NODES) {
9290 		if (nname == NULL) {
9291 			cmn_err(CE_NOTE, "sata_create_target_node: "
9292 			    "cannot determine nodename for target %d\n",
9293 			    target);
9294 		} else {
9295 			cmn_err(CE_WARN, "sata_create_target_node: "
9296 			    "target %d nodename: %s\n", target, nname);
9297 		}
9298 		if (compatible == NULL) {
9299 			cmn_err(CE_WARN,
9300 			    "sata_create_target_node: no compatible name\n");
9301 		} else {
9302 			for (i = 0; i < ncompatible; i++) {
9303 				cmn_err(CE_WARN, "sata_create_target_node: "
9304 				    "compatible name: %s\n", compatible[i]);
9305 			}
9306 		}
9307 	}
9308 #endif
9309 
9310 	/* if nodename can't be determined, log error and exit */
9311 	if (nname == NULL) {
9312 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9313 		    "sata_create_target_node: cannot determine nodename "
9314 		    "for target %d\n", target));
9315 		scsi_hba_nodename_compatible_free(nname, compatible);
9316 		return (NULL);
9317 	}
9318 	/*
9319 	 * Create scsi target node
9320 	 */
9321 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9322 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9323 	    "device-type", "scsi");
9324 
9325 	if (rval != DDI_PROP_SUCCESS) {
9326 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9327 		    "updating device_type prop failed %d", rval));
9328 		goto fail;
9329 	}
9330 
9331 	/*
9332 	 * Create target node properties: target & lun
9333 	 */
9334 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9335 	if (rval != DDI_PROP_SUCCESS) {
9336 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9337 		    "updating target prop failed %d", rval));
9338 		goto fail;
9339 	}
9340 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9341 	if (rval != DDI_PROP_SUCCESS) {
9342 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9343 		    "updating target prop failed %d", rval));
9344 		goto fail;
9345 	}
9346 
9347 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
9348 		/*
9349 		 * Add "variant" property
9350 		 */
9351 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9352 		    "variant", "atapi");
9353 		if (rval != DDI_PROP_SUCCESS) {
9354 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9355 			    "sata_create_target_node: variant atapi "
9356 			    "property could not be created: %d", rval));
9357 			goto fail;
9358 		}
9359 	}
9360 	/* decorate the node with compatible */
9361 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9362 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9363 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9364 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9365 		    (void *)cdip));
9366 		goto fail;
9367 	}
9368 
9369 
9370 	/*
9371 	 * Now, try to attach the driver. If probing of the device fails,
9372 	 * the target node may be removed
9373 	 */
9374 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9375 
9376 	scsi_hba_nodename_compatible_free(nname, compatible);
9377 
9378 	if (rval == NDI_SUCCESS)
9379 		return (cdip);
9380 
9381 	/* target node was removed - are we sure? */
9382 	return (NULL);
9383 
9384 fail:
9385 	scsi_hba_nodename_compatible_free(nname, compatible);
9386 	ddi_prop_remove_all(cdip);
9387 	rval = ndi_devi_free(cdip);
9388 	if (rval != NDI_SUCCESS) {
9389 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9390 		    "node removal failed %d", rval));
9391 	}
9392 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9393 	    "cannot create target node for SATA device at port %d",
9394 	    sata_addr->cport);
9395 	return (NULL);
9396 }
9397 
9398 
9399 
9400 /*
9401  * Re-probe sata port, check for a device and attach info
9402  * structures when necessary. Identify Device data is fetched, if possible.
9403  * Assumption: sata address is already validated.
9404  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9405  * the presence of a device and its type.
9406  *
9407  * flag arg specifies that the function should try multiple times to identify
9408  * device type and to initialize it, or it should return immediately on failure.
9409  * SATA_DEV_IDENTIFY_RETRY - retry
9410  * SATA_DEV_IDENTIFY_NORETRY - no retry
9411  *
9412  * SATA_FAILURE is returned if one of the operations failed.
9413  *
9414  * This function cannot be called in interrupt context - it may sleep.
9415  *
9416  * NOte: Port multiplier is not supported yet, although there may be some
9417  * pieces of code referencing to it.
9418  */
9419 static int
9420 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9421     int flag)
9422 {
9423 	sata_cport_info_t *cportinfo;
9424 	sata_drive_info_t *sdinfo, *osdinfo;
9425 	boolean_t init_device = B_FALSE;
9426 	int prev_device_type = SATA_DTYPE_NONE;
9427 	int prev_device_settings = 0;
9428 	int prev_device_state = 0;
9429 	clock_t start_time;
9430 	int retry = B_FALSE;
9431 	int rval_probe, rval_init;
9432 
9433 	/* We only care about host sata cport for now */
9434 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9435 	    sata_device->satadev_addr.cport);
9436 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9437 	if (osdinfo != NULL) {
9438 		/*
9439 		 * We are re-probing port with a previously attached device.
9440 		 * Save previous device type and settings.
9441 		 */
9442 		prev_device_type = cportinfo->cport_dev_type;
9443 		prev_device_settings = osdinfo->satadrv_settings;
9444 		prev_device_state = osdinfo->satadrv_state;
9445 	}
9446 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9447 		start_time = ddi_get_lbolt();
9448 		retry = B_TRUE;
9449 	}
9450 retry_probe:
9451 
9452 	/* probe port */
9453 	mutex_enter(&cportinfo->cport_mutex);
9454 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9455 	cportinfo->cport_state |= SATA_STATE_PROBING;
9456 	mutex_exit(&cportinfo->cport_mutex);
9457 
9458 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9459 	    (SATA_DIP(sata_hba_inst), sata_device);
9460 
9461 	mutex_enter(&cportinfo->cport_mutex);
9462 	if (rval_probe != SATA_SUCCESS) {
9463 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9464 		mutex_exit(&cportinfo->cport_mutex);
9465 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9466 		    "SATA port %d probing failed",
9467 		    cportinfo->cport_addr.cport));
9468 		return (SATA_FAILURE);
9469 	}
9470 
9471 	/*
9472 	 * update sata port state and set device type
9473 	 */
9474 	sata_update_port_info(sata_hba_inst, sata_device);
9475 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9476 
9477 	/*
9478 	 * Sanity check - Port is active? Is the link active?
9479 	 * Is there any device attached?
9480 	 */
9481 	if ((cportinfo->cport_state &
9482 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9483 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9484 	    SATA_PORT_DEVLINK_UP) {
9485 		/*
9486 		 * Port in non-usable state or no link active/no device.
9487 		 * Free info structure if necessary (direct attached drive
9488 		 * only, for now!
9489 		 */
9490 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9491 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9492 		/* Add here differentiation for device attached or not */
9493 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9494 		mutex_exit(&cportinfo->cport_mutex);
9495 		if (sdinfo != NULL)
9496 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9497 		return (SATA_SUCCESS);
9498 	}
9499 
9500 	cportinfo->cport_state |= SATA_STATE_READY;
9501 	cportinfo->cport_dev_type = sata_device->satadev_type;
9502 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9503 
9504 	/*
9505 	 * If we are re-probing the port, there may be
9506 	 * sata_drive_info structure attached
9507 	 * (or sata_pm_info, if PMult is supported).
9508 	 */
9509 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
9510 		/*
9511 		 * There is no device, so remove device info structure,
9512 		 * if necessary.
9513 		 * Only direct attached drive is considered now, until
9514 		 * port multiplier is supported. If the previously
9515 		 * attached device was a port multiplier, we would need
9516 		 * to take care of devices attached beyond the port
9517 		 * multiplier.
9518 		 */
9519 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9520 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9521 		if (sdinfo != NULL) {
9522 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9523 			sata_log(sata_hba_inst, CE_WARN,
9524 			    "SATA device detached "
9525 			    "from port %d", cportinfo->cport_addr.cport);
9526 		}
9527 		mutex_exit(&cportinfo->cport_mutex);
9528 		return (SATA_SUCCESS);
9529 	}
9530 
9531 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
9532 		if (sdinfo == NULL) {
9533 			/*
9534 			 * There is some device attached, but there is
9535 			 * no sata_drive_info structure - allocate one
9536 			 */
9537 			mutex_exit(&cportinfo->cport_mutex);
9538 			sdinfo = kmem_zalloc(
9539 			    sizeof (sata_drive_info_t), KM_SLEEP);
9540 			mutex_enter(&cportinfo->cport_mutex);
9541 			/*
9542 			 * Recheck, that the port state did not change when we
9543 			 * released mutex.
9544 			 */
9545 			if (cportinfo->cport_state & SATA_STATE_READY) {
9546 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
9547 				sdinfo->satadrv_addr = cportinfo->cport_addr;
9548 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
9549 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9550 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9551 			} else {
9552 				/*
9553 				 * Port is not in ready state, we
9554 				 * cannot attach a device.
9555 				 */
9556 				mutex_exit(&cportinfo->cport_mutex);
9557 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
9558 				return (SATA_SUCCESS);
9559 			}
9560 			/*
9561 			 * Since we are adding device, presumably new one,
9562 			 * indicate that it  should be initalized,
9563 			 * as well as some internal framework states).
9564 			 */
9565 			init_device = B_TRUE;
9566 		}
9567 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9568 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
9569 	} else {
9570 		/*
9571 		 * The device is a port multiplier - not handled now.
9572 		 */
9573 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9574 		mutex_exit(&cportinfo->cport_mutex);
9575 		return (SATA_SUCCESS);
9576 	}
9577 	mutex_exit(&cportinfo->cport_mutex);
9578 	/*
9579 	 * Figure out what kind of device we are really
9580 	 * dealing with. Failure of identifying device does not fail this
9581 	 * function.
9582 	 */
9583 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
9584 	rval_init = SATA_FAILURE;
9585 	mutex_enter(&cportinfo->cport_mutex);
9586 	if (rval_probe == SATA_SUCCESS) {
9587 		/*
9588 		 * If we are dealing with the same type of a device as before,
9589 		 * restore its settings flags.
9590 		 */
9591 		if (osdinfo != NULL &&
9592 		    sata_device->satadev_type == prev_device_type)
9593 			sdinfo->satadrv_settings = prev_device_settings;
9594 
9595 		mutex_exit(&cportinfo->cport_mutex);
9596 		rval_init = SATA_SUCCESS;
9597 		/* Set initial device features, if necessary */
9598 		if (init_device == B_TRUE) {
9599 			rval_init = sata_initialize_device(sata_hba_inst,
9600 			    sdinfo);
9601 		}
9602 		if (rval_init == SATA_SUCCESS)
9603 			return (rval_init);
9604 		/* else we will retry if retry was asked for */
9605 
9606 	} else {
9607 		/*
9608 		 * If there was some device info before we probe the device,
9609 		 * restore previous device setting, so we can retry from scratch
9610 		 * later. Providing, of course, that device has not disapear
9611 		 * during probing process.
9612 		 */
9613 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
9614 			if (osdinfo != NULL) {
9615 				cportinfo->cport_dev_type = prev_device_type;
9616 				sdinfo->satadrv_type = prev_device_type;
9617 				sdinfo->satadrv_state = prev_device_state;
9618 			}
9619 		} else {
9620 			/* device is gone */
9621 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9622 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9623 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9624 			mutex_exit(&cportinfo->cport_mutex);
9625 			return (SATA_SUCCESS);
9626 		}
9627 		mutex_exit(&cportinfo->cport_mutex);
9628 	}
9629 
9630 	if (retry) {
9631 		clock_t cur_time = ddi_get_lbolt();
9632 		/*
9633 		 * A device was not successfully identified or initialized.
9634 		 * Track retry time for device identification.
9635 		 */
9636 		if ((cur_time - start_time) <
9637 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
9638 			/* sleep for a while */
9639 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
9640 			goto retry_probe;
9641 		}
9642 		/* else no more retries */
9643 		mutex_enter(&cportinfo->cport_mutex);
9644 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
9645 			if (rval_init == SATA_RETRY) {
9646 				/*
9647 				 * Setting drive features have failed, but
9648 				 * because the drive is still accessible,
9649 				 * keep it and emit a warning message.
9650 				 */
9651 				sata_log(sata_hba_inst, CE_WARN,
9652 				    "SATA device at port %d - desired "
9653 				    "drive features could not be set. "
9654 				    "Device may not operate as expected.",
9655 				    cportinfo->cport_addr.cport);
9656 			} else {
9657 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
9658 				    satadrv_state = SATA_DSTATE_FAILED;
9659 			}
9660 		}
9661 		mutex_exit(&cportinfo->cport_mutex);
9662 	}
9663 	return (SATA_SUCCESS);
9664 }
9665 
9666 /*
9667  * Initialize device
9668  * Specified device is initialized to a default state.
9669  *
9670  * Returns SATA_SUCCESS if all device features are set successfully,
9671  * SATA_RETRY if device is accessible but device features were not set
9672  * successfully, and SATA_FAILURE otherwise.
9673  */
9674 static int
9675 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
9676     sata_drive_info_t *sdinfo)
9677 {
9678 	int rval;
9679 
9680 	sata_save_drive_settings(sdinfo);
9681 
9682 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9683 
9684 	sata_init_write_cache_mode(sdinfo);
9685 
9686 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
9687 
9688 	/* Determine current data transfer mode */
9689 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
9690 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9691 	} else if ((sdinfo->satadrv_id.ai_validinfo &
9692 	    SATA_VALIDINFO_88) != 0 &&
9693 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
9694 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9695 	} else if ((sdinfo->satadrv_id.ai_dworddma &
9696 	    SATA_MDMA_SEL_MASK) != 0) {
9697 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9698 	} else
9699 		/* DMA supported, not no DMA transfer mode is selected !? */
9700 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9701 
9702 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
9703 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
9704 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
9705 
9706 	return (rval);
9707 }
9708 
9709 
9710 /*
9711  * Initialize write cache mode.
9712  *
9713  * The default write cache setting for SATA HDD is provided by sata_write_cache
9714  * static variable. ATAPI CD/DVDs devices have write cache default is
9715  * determined by sata_atapicdvd_write_cache static variable.
9716  * ATAPI tape devices have write cache default is determined by
9717  * sata_atapitape_write_cache static variable.
9718  * ATAPI disk devices have write cache default is determined by
9719  * sata_atapidisk_write_cache static variable.
9720  * 1 - enable
9721  * 0 - disable
9722  * any other value - current drive setting
9723  *
9724  * Although there is not reason to disable write cache on CD/DVD devices,
9725  * tape devices and ATAPI disk devices, the default setting control is provided
9726  * for the maximun flexibility.
9727  *
9728  * In the future, it may be overridden by the
9729  * disk-write-cache-enable property setting, if it is defined.
9730  * Returns SATA_SUCCESS if all device features are set successfully,
9731  * SATA_FAILURE otherwise.
9732  */
9733 static void
9734 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
9735 {
9736 	switch (sdinfo->satadrv_type) {
9737 	case SATA_DTYPE_ATADISK:
9738 		if (sata_write_cache == 1)
9739 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9740 		else if (sata_write_cache == 0)
9741 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9742 		/*
9743 		 * When sata_write_cache value is not 0 or 1,
9744 		 * a current setting of the drive's write cache is used.
9745 		 */
9746 		break;
9747 	case SATA_DTYPE_ATAPICD:
9748 		if (sata_atapicdvd_write_cache == 1)
9749 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9750 		else if (sata_atapicdvd_write_cache == 0)
9751 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9752 		/*
9753 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
9754 		 * a current setting of the drive's write cache is used.
9755 		 */
9756 		break;
9757 	case SATA_DTYPE_ATAPITAPE:
9758 		if (sata_atapitape_write_cache == 1)
9759 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9760 		else if (sata_atapitape_write_cache == 0)
9761 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9762 		/*
9763 		 * When sata_atapitape_write_cache value is not 0 or 1,
9764 		 * a current setting of the drive's write cache is used.
9765 		 */
9766 		break;
9767 	case SATA_DTYPE_ATAPIDISK:
9768 		if (sata_atapidisk_write_cache == 1)
9769 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9770 		else if (sata_atapidisk_write_cache == 0)
9771 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9772 		/*
9773 		 * When sata_atapidisk_write_cache value is not 0 or 1,
9774 		 * a current setting of the drive's write cache is used.
9775 		 */
9776 		break;
9777 	}
9778 }
9779 
9780 
9781 /*
9782  * Validate sata address.
9783  * Specified cport, pmport and qualifier has to match
9784  * passed sata_scsi configuration info.
9785  * The presence of an attached device is not verified.
9786  *
9787  * Returns 0 when address is valid, -1 otherwise.
9788  */
9789 static int
9790 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9791 	int pmport, int qual)
9792 {
9793 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9794 		goto invalid_address;
9795 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9796 		goto invalid_address;
9797 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9798 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9799 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9800 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9801 		goto invalid_address;
9802 
9803 	return (0);
9804 
9805 invalid_address:
9806 	return (-1);
9807 
9808 }
9809 
9810 /*
9811  * Validate scsi address
9812  * SCSI target address is translated into SATA cport/pmport and compared
9813  * with a controller port/device configuration. LUN has to be 0.
9814  * Returns 0 if a scsi target refers to an attached device,
9815  * returns 1 if address is valid but device is not attached,
9816  * returns -1 if bad address or device is of an unsupported type.
9817  * Upon return sata_device argument is set.
9818  */
9819 static int
9820 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9821 	struct scsi_address *ap, sata_device_t *sata_device)
9822 {
9823 	int cport, pmport, qual, rval;
9824 
9825 	rval = -1;	/* Invalid address */
9826 	if (ap->a_lun != 0)
9827 		goto out;
9828 
9829 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9830 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9831 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9832 
9833 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9834 		goto out;
9835 
9836 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9837 	    0) {
9838 
9839 		sata_cport_info_t *cportinfo;
9840 		sata_pmult_info_t *pmultinfo;
9841 		sata_drive_info_t *sdinfo = NULL;
9842 
9843 		rval = 1;	/* Valid sata address */
9844 
9845 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9846 		if (qual == SATA_ADDR_DCPORT) {
9847 			if (cportinfo == NULL ||
9848 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9849 				goto out;
9850 
9851 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9852 			    (cportinfo->cport_dev_type &
9853 			    SATA_VALID_DEV_TYPE) == 0) {
9854 				rval = -1;
9855 				goto out;
9856 			}
9857 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9858 
9859 		} else if (qual == SATA_ADDR_DPMPORT) {
9860 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9861 			if (pmultinfo == NULL) {
9862 				rval = -1;
9863 				goto out;
9864 			}
9865 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9866 			    NULL ||
9867 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9868 			    pmport) == SATA_DTYPE_NONE)
9869 				goto out;
9870 
9871 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9872 			    pmport);
9873 		} else {
9874 			rval = -1;
9875 			goto out;
9876 		}
9877 		if ((sdinfo == NULL) ||
9878 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9879 			goto out;
9880 
9881 		sata_device->satadev_type = sdinfo->satadrv_type;
9882 		sata_device->satadev_addr.qual = qual;
9883 		sata_device->satadev_addr.cport = cport;
9884 		sata_device->satadev_addr.pmport = pmport;
9885 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9886 		return (0);
9887 	}
9888 out:
9889 	if (rval == 1) {
9890 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9891 		    "sata_validate_scsi_address: no valid target %x lun %x",
9892 		    ap->a_target, ap->a_lun);
9893 	}
9894 	return (rval);
9895 }
9896 
9897 /*
9898  * Find dip corresponding to passed device number
9899  *
9900  * Returns NULL if invalid device number is passed or device cannot be found,
9901  * Returns dip is device is found.
9902  */
9903 static dev_info_t *
9904 sata_devt_to_devinfo(dev_t dev)
9905 {
9906 	dev_info_t *dip;
9907 #ifndef __lock_lint
9908 	struct devnames *dnp;
9909 	major_t major = getmajor(dev);
9910 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9911 
9912 	if (major >= devcnt)
9913 		return (NULL);
9914 
9915 	dnp = &devnamesp[major];
9916 	LOCK_DEV_OPS(&(dnp->dn_lock));
9917 	dip = dnp->dn_head;
9918 	while (dip && (ddi_get_instance(dip) != instance)) {
9919 		dip = ddi_get_next(dip);
9920 	}
9921 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9922 #endif
9923 
9924 	return (dip);
9925 }
9926 
9927 
9928 /*
9929  * Probe device.
9930  * This function issues Identify Device command and initializes local
9931  * sata_drive_info structure if the device can be identified.
9932  * The device type is determined by examining Identify Device
9933  * command response.
9934  * If the sata_hba_inst has linked drive info structure for this
9935  * device address, the Identify Device data is stored into sata_drive_info
9936  * structure linked to the port info structure.
9937  *
9938  * sata_device has to refer to the valid sata port(s) for HBA described
9939  * by sata_hba_inst structure.
9940  *
9941  * Returns:
9942  *	SATA_SUCCESS if device type was successfully probed and port-linked
9943  *		drive info structure was updated;
9944  * 	SATA_FAILURE if there is no device, or device was not probed
9945  *		successully;
9946  *	SATA_RETRY if device probe can be retried later.
9947  * If a device cannot be identified, sata_device's dev_state and dev_type
9948  * fields are set to unknown.
9949  * There are no retries in this function. Any retries should be managed by
9950  * the caller.
9951  */
9952 
9953 
9954 static int
9955 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9956 {
9957 	sata_drive_info_t *sdinfo;
9958 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9959 	int rval;
9960 
9961 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9962 	    sata_device->satadev_addr.cport) &
9963 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9964 
9965 	sata_device->satadev_type = SATA_DTYPE_NONE;
9966 
9967 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9968 	    sata_device->satadev_addr.cport)));
9969 
9970 	/* Get pointer to port-linked sata device info structure */
9971 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9972 	if (sdinfo != NULL) {
9973 		sdinfo->satadrv_state &=
9974 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9975 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9976 	} else {
9977 		/* No device to probe */
9978 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9979 		    sata_device->satadev_addr.cport)));
9980 		sata_device->satadev_type = SATA_DTYPE_NONE;
9981 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9982 		return (SATA_FAILURE);
9983 	}
9984 	/*
9985 	 * Need to issue both types of identify device command and
9986 	 * determine device type by examining retreived data/status.
9987 	 * First, ATA Identify Device.
9988 	 */
9989 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9990 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9991 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9992 	    sata_device->satadev_addr.cport)));
9993 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9994 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9995 	if (rval == SATA_RETRY) {
9996 		/* We may try to check for ATAPI device */
9997 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9998 			/*
9999 			 * HBA supports ATAPI - try to issue Identify Packet
10000 			 * Device command.
10001 			 */
10002 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
10003 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
10004 		}
10005 	}
10006 	if (rval == SATA_SUCCESS) {
10007 		/*
10008 		 * Got something responding positively to ATA Identify Device
10009 		 * or to Identify Packet Device cmd.
10010 		 * Save last used device type.
10011 		 */
10012 		sata_device->satadev_type = new_sdinfo.satadrv_type;
10013 
10014 		/* save device info, if possible */
10015 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10016 		    sata_device->satadev_addr.cport)));
10017 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10018 		if (sdinfo == NULL) {
10019 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10020 			    sata_device->satadev_addr.cport)));
10021 			return (SATA_FAILURE);
10022 		}
10023 		/*
10024 		 * Copy drive info into the port-linked drive info structure.
10025 		 */
10026 		*sdinfo = new_sdinfo;
10027 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10028 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
10029 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10030 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10031 			    sata_device->satadev_addr.cport) =
10032 			    sdinfo->satadrv_type;
10033 		else /* SATA_ADDR_DPMPORT */
10034 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10035 			    sata_device->satadev_addr.cport,
10036 			    sata_device->satadev_addr.pmport) =
10037 			    sdinfo->satadrv_type;
10038 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10039 		    sata_device->satadev_addr.cport)));
10040 		return (SATA_SUCCESS);
10041 	}
10042 
10043 	/*
10044 	 * It may be SATA_RETRY or SATA_FAILURE return.
10045 	 * Looks like we cannot determine the device type at this time.
10046 	 */
10047 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10048 	    sata_device->satadev_addr.cport)));
10049 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10050 	if (sdinfo != NULL) {
10051 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
10052 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10053 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10054 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
10055 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10056 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10057 			    sata_device->satadev_addr.cport) =
10058 			    SATA_DTYPE_UNKNOWN;
10059 		else {
10060 			/* SATA_ADDR_DPMPORT */
10061 			if ((SATA_PMULT_INFO(sata_hba_inst,
10062 			    sata_device->satadev_addr.cport) != NULL) &&
10063 			    (SATA_PMPORT_INFO(sata_hba_inst,
10064 			    sata_device->satadev_addr.cport,
10065 			    sata_device->satadev_addr.pmport) != NULL))
10066 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10067 				    sata_device->satadev_addr.cport,
10068 				    sata_device->satadev_addr.pmport) =
10069 				    SATA_DTYPE_UNKNOWN;
10070 		}
10071 	}
10072 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10073 	    sata_device->satadev_addr.cport)));
10074 	return (rval);
10075 }
10076 
10077 
10078 /*
10079  * Get pointer to sata_drive_info structure.
10080  *
10081  * The sata_device has to contain address (cport, pmport and qualifier) for
10082  * specified sata_scsi structure.
10083  *
10084  * Returns NULL if device address is not valid for this HBA configuration.
10085  * Otherwise, returns a pointer to sata_drive_info structure.
10086  *
10087  * This function should be called with a port mutex held.
10088  */
10089 static sata_drive_info_t *
10090 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
10091     sata_device_t *sata_device)
10092 {
10093 	uint8_t cport = sata_device->satadev_addr.cport;
10094 	uint8_t pmport = sata_device->satadev_addr.pmport;
10095 	uint8_t qual = sata_device->satadev_addr.qual;
10096 
10097 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10098 		return (NULL);
10099 
10100 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
10101 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
10102 		/* Port not probed yet */
10103 		return (NULL);
10104 
10105 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
10106 		return (NULL);
10107 
10108 	if (qual == SATA_ADDR_DCPORT) {
10109 		/* Request for a device on a controller port */
10110 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
10111 		    SATA_DTYPE_PMULT)
10112 			/* Port multiplier attached */
10113 			return (NULL);
10114 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
10115 	}
10116 	if (qual == SATA_ADDR_DPMPORT) {
10117 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
10118 		    SATA_DTYPE_PMULT)
10119 			return (NULL);
10120 
10121 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
10122 			return (NULL);
10123 
10124 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
10125 	}
10126 
10127 	/* we should not get here */
10128 	return (NULL);
10129 }
10130 
10131 
10132 /*
10133  * sata_identify_device.
10134  * Send Identify Device command to SATA HBA driver.
10135  * If command executes successfully, update sata_drive_info structure pointed
10136  * to by sdinfo argument, including Identify Device data.
10137  * If command fails, invalidate data in sata_drive_info.
10138  *
10139  * Cannot be called from interrupt level.
10140  *
10141  * Returns:
10142  * SATA_SUCCESS if the device was identified as a supported device,
10143  * SATA_RETRY if the device was not identified but could be retried,
10144  * SATA_FAILURE if the device was not identified and identify attempt
10145  *	should not be retried.
10146  */
10147 static int
10148 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
10149     sata_drive_info_t *sdinfo)
10150 {
10151 	uint16_t cfg_word;
10152 	int rval;
10153 
10154 	/* fetch device identify data */
10155 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
10156 	    sdinfo)) != SATA_SUCCESS)
10157 		goto fail_unknown;
10158 
10159 	cfg_word = sdinfo->satadrv_id.ai_config;
10160 
10161 	/* Set the correct device type */
10162 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
10163 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10164 	} else if (cfg_word == SATA_CFA_TYPE) {
10165 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
10166 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10167 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
10168 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
10169 		case SATA_ATAPI_CDROM_DEV:
10170 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
10171 			break;
10172 		case SATA_ATAPI_SQACC_DEV:
10173 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
10174 			break;
10175 		case SATA_ATAPI_DIRACC_DEV:
10176 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
10177 			break;
10178 		default:
10179 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10180 		}
10181 	} else {
10182 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10183 	}
10184 
10185 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10186 		if (sdinfo->satadrv_capacity == 0) {
10187 			/* Non-LBA disk. Too bad... */
10188 			sata_log(sata_hba_inst, CE_WARN,
10189 			    "SATA disk device at port %d does not support LBA",
10190 			    sdinfo->satadrv_addr.cport);
10191 			rval = SATA_FAILURE;
10192 			goto fail_unknown;
10193 		}
10194 	}
10195 #if 0
10196 	/* Left for historical reason */
10197 	/*
10198 	 * Some initial version of SATA spec indicated that at least
10199 	 * UDMA mode 4 has to be supported. It is not metioned in
10200 	 * SerialATA 2.6, so this restriction is removed.
10201 	 */
10202 	/* Check for Ultra DMA modes 6 through 0 being supported */
10203 	for (i = 6; i >= 0; --i) {
10204 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
10205 			break;
10206 	}
10207 
10208 	/*
10209 	 * At least UDMA 4 mode has to be supported. If mode 4 or
10210 	 * higher are not supported by the device, fail this
10211 	 * device.
10212 	 */
10213 	if (i < 4) {
10214 		/* No required Ultra DMA mode supported */
10215 		sata_log(sata_hba_inst, CE_WARN,
10216 		    "SATA disk device at port %d does not support UDMA "
10217 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
10218 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10219 		    "mode 4 or higher required, %d supported", i));
10220 		rval = SATA_FAILURE;
10221 		goto fail_unknown;
10222 	}
10223 #endif
10224 
10225 	/*
10226 	 * For Disk devices, if it doesn't support UDMA mode, we would
10227 	 * like to return failure directly.
10228 	 */
10229 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
10230 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10231 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
10232 		sata_log(sata_hba_inst, CE_WARN,
10233 		    "SATA disk device at port %d does not support UDMA",
10234 		    sdinfo->satadrv_addr.cport);
10235 		rval = SATA_FAILURE;
10236 		goto fail_unknown;
10237 	}
10238 
10239 	return (SATA_SUCCESS);
10240 
10241 fail_unknown:
10242 	/* Invalidate sata_drive_info ? */
10243 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10244 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10245 	return (rval);
10246 }
10247 
10248 /*
10249  * Log/display device information
10250  */
10251 static void
10252 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
10253     sata_drive_info_t *sdinfo)
10254 {
10255 	int valid_version;
10256 	char msg_buf[MAXPATHLEN];
10257 	int i;
10258 
10259 	/* Show HBA path */
10260 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
10261 
10262 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
10263 
10264 	switch (sdinfo->satadrv_type) {
10265 	case SATA_DTYPE_ATADISK:
10266 		(void) sprintf(msg_buf, "SATA disk device at");
10267 		break;
10268 
10269 	case SATA_DTYPE_ATAPICD:
10270 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
10271 		break;
10272 
10273 	case SATA_DTYPE_ATAPITAPE:
10274 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
10275 		break;
10276 
10277 	case SATA_DTYPE_ATAPIDISK:
10278 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
10279 		break;
10280 
10281 	case SATA_DTYPE_UNKNOWN:
10282 		(void) sprintf(msg_buf,
10283 		    "Unsupported SATA device type (cfg 0x%x) at ",
10284 		    sdinfo->satadrv_id.ai_config);
10285 		break;
10286 	}
10287 
10288 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
10289 		cmn_err(CE_CONT, "?\t%s port %d\n",
10290 		    msg_buf, sdinfo->satadrv_addr.cport);
10291 	else
10292 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
10293 		    msg_buf, sdinfo->satadrv_addr.cport,
10294 		    sdinfo->satadrv_addr.pmport);
10295 
10296 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
10297 	    sizeof (sdinfo->satadrv_id.ai_model));
10298 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
10299 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
10300 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
10301 
10302 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
10303 	    sizeof (sdinfo->satadrv_id.ai_fw));
10304 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
10305 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
10306 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
10307 
10308 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
10309 	    sizeof (sdinfo->satadrv_id.ai_drvser));
10310 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
10311 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
10312 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10313 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10314 	} else {
10315 		/*
10316 		 * Some drives do not implement serial number and may
10317 		 * violate the spec by providing spaces rather than zeros
10318 		 * in serial number field. Scan the buffer to detect it.
10319 		 */
10320 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
10321 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
10322 				break;
10323 		}
10324 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
10325 			cmn_err(CE_CONT, "?\tserial number - none\n");
10326 		} else {
10327 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10328 		}
10329 	}
10330 
10331 #ifdef SATA_DEBUG
10332 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
10333 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
10334 		int i;
10335 		for (i = 14; i >= 2; i--) {
10336 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
10337 				valid_version = i;
10338 				break;
10339 			}
10340 		}
10341 		cmn_err(CE_CONT,
10342 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
10343 		    valid_version,
10344 		    sdinfo->satadrv_id.ai_majorversion,
10345 		    sdinfo->satadrv_id.ai_minorversion);
10346 	}
10347 #endif
10348 	/* Log some info */
10349 	cmn_err(CE_CONT, "?\tsupported features:\n");
10350 	msg_buf[0] = '\0';
10351 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10352 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
10353 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
10354 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
10355 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
10356 	}
10357 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
10358 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
10359 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
10360 		(void) strlcat(msg_buf, ", Native Command Queueing",
10361 		    MAXPATHLEN);
10362 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
10363 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
10364 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
10365 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
10366 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
10367 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
10368 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
10369 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
10370 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
10371 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
10372 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
10373 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
10374 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
10375 	if (sdinfo->satadrv_features_support &
10376 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
10377 		msg_buf[0] = '\0';
10378 		(void) snprintf(msg_buf, MAXPATHLEN,
10379 		    "Supported queue depth %d",
10380 		    sdinfo->satadrv_queue_depth);
10381 		if (!(sata_func_enable &
10382 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
10383 			(void) strlcat(msg_buf,
10384 			    " - queueing disabled globally", MAXPATHLEN);
10385 		else if (sdinfo->satadrv_queue_depth >
10386 		    sdinfo->satadrv_max_queue_depth) {
10387 			(void) snprintf(&msg_buf[strlen(msg_buf)],
10388 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
10389 			    (int)sdinfo->satadrv_max_queue_depth);
10390 		}
10391 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
10392 	}
10393 
10394 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10395 #ifdef __i386
10396 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
10397 		    sdinfo->satadrv_capacity);
10398 #else
10399 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
10400 		    sdinfo->satadrv_capacity);
10401 #endif
10402 		cmn_err(CE_CONT, "?%s", msg_buf);
10403 	}
10404 }
10405 
10406 
10407 /*
10408  * sata_save_drive_settings extracts current setting of the device and stores
10409  * it for future reference, in case the device setup would need to be restored
10410  * after the device reset.
10411  *
10412  * For all devices read ahead and write cache settings are saved, if the
10413  * device supports these features at all.
10414  * For ATAPI devices the Removable Media Status Notification setting is saved.
10415  */
10416 static void
10417 sata_save_drive_settings(sata_drive_info_t *sdinfo)
10418 {
10419 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
10420 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
10421 
10422 		/* Current setting of Read Ahead (and Read Cache) */
10423 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
10424 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10425 		else
10426 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
10427 
10428 		/* Current setting of Write Cache */
10429 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
10430 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10431 		else
10432 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10433 	}
10434 
10435 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10436 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
10437 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
10438 		else
10439 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
10440 	}
10441 }
10442 
10443 
10444 /*
10445  * sata_check_capacity function determines a disk capacity
10446  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
10447  *
10448  * NOTE: CHS mode is not supported! If a device does not support LBA,
10449  * this function is not called.
10450  *
10451  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
10452  */
10453 static uint64_t
10454 sata_check_capacity(sata_drive_info_t *sdinfo)
10455 {
10456 	uint64_t capacity = 0;
10457 	int i;
10458 
10459 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
10460 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
10461 		/* Capacity valid only for LBA-addressable disk devices */
10462 		return (0);
10463 
10464 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
10465 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
10466 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
10467 		/* LBA48 mode supported and enabled */
10468 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
10469 		    SATA_DEV_F_LBA28;
10470 		for (i = 3;  i >= 0;  --i) {
10471 			capacity <<= 16;
10472 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
10473 		}
10474 	} else {
10475 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
10476 		capacity <<= 16;
10477 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
10478 		if (capacity >= 0x1000000)
10479 			/* LBA28 mode */
10480 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
10481 	}
10482 	return (capacity);
10483 }
10484 
10485 
10486 /*
10487  * Allocate consistent buffer for DMA transfer
10488  *
10489  * Cannot be called from interrupt level or with mutex held - it may sleep.
10490  *
10491  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
10492  */
10493 static struct buf *
10494 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
10495 {
10496 	struct scsi_address ap;
10497 	struct buf *bp;
10498 	ddi_dma_attr_t	cur_dma_attr;
10499 
10500 	ASSERT(spx->txlt_sata_pkt != NULL);
10501 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
10502 	ap.a_target = SATA_TO_SCSI_TARGET(
10503 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
10504 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
10505 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
10506 	ap.a_lun = 0;
10507 
10508 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
10509 	    B_READ, SLEEP_FUNC, NULL);
10510 
10511 	if (bp != NULL) {
10512 		/* Allocate DMA resources for this buffer */
10513 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
10514 		/*
10515 		 * We use a local version of the dma_attr, to account
10516 		 * for a device addressing limitations.
10517 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
10518 		 * will cause dma attributes to be adjusted to a lowest
10519 		 * acceptable level.
10520 		 */
10521 		sata_adjust_dma_attr(NULL,
10522 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
10523 
10524 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
10525 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
10526 			scsi_free_consistent_buf(bp);
10527 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10528 			bp = NULL;
10529 		}
10530 	}
10531 	return (bp);
10532 }
10533 
10534 /*
10535  * Release local buffer (consistent buffer for DMA transfer) allocated
10536  * via sata_alloc_local_buffer().
10537  */
10538 static void
10539 sata_free_local_buffer(sata_pkt_txlate_t *spx)
10540 {
10541 	ASSERT(spx->txlt_sata_pkt != NULL);
10542 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
10543 
10544 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
10545 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
10546 
10547 	sata_common_free_dma_rsrcs(spx);
10548 
10549 	/* Free buffer */
10550 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
10551 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10552 }
10553 
10554 /*
10555  * Allocate sata_pkt
10556  * Pkt structure version and embedded strcutures version are initialized.
10557  * sata_pkt and sata_pkt_txlate structures are cross-linked.
10558  *
10559  * Since this may be called in interrupt context by sata_scsi_init_pkt,
10560  * callback argument determines if it can sleep or not.
10561  * Hence, it should not be called from interrupt context.
10562  *
10563  * If successful, non-NULL pointer to a sata pkt is returned.
10564  * Upon failure, NULL pointer is returned.
10565  */
10566 static sata_pkt_t *
10567 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
10568 {
10569 	sata_pkt_t *spkt;
10570 	int kmsflag;
10571 
10572 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
10573 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
10574 	if (spkt == NULL) {
10575 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10576 		    "sata_pkt_alloc: failed"));
10577 		return (NULL);
10578 	}
10579 	spkt->satapkt_rev = SATA_PKT_REV;
10580 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
10581 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
10582 	spkt->satapkt_framework_private = spx;
10583 	spx->txlt_sata_pkt = spkt;
10584 	return (spkt);
10585 }
10586 
10587 /*
10588  * Free sata pkt allocated via sata_pkt_alloc()
10589  */
10590 static void
10591 sata_pkt_free(sata_pkt_txlate_t *spx)
10592 {
10593 	ASSERT(spx->txlt_sata_pkt != NULL);
10594 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
10595 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
10596 	spx->txlt_sata_pkt = NULL;
10597 }
10598 
10599 
10600 /*
10601  * Adjust DMA attributes.
10602  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
10603  * from 8 bits to 16 bits, depending on a command being used.
10604  * Limiting max block count arbitrarily to 256 for all read/write
10605  * commands may affects performance, so check both the device and
10606  * controller capability before adjusting dma attributes.
10607  */
10608 void
10609 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
10610     ddi_dma_attr_t *adj_dma_attr)
10611 {
10612 	uint32_t count_max;
10613 
10614 	/* Copy original attributes */
10615 	*adj_dma_attr = *dma_attr;
10616 	/*
10617 	 * Things to consider: device addressing capability,
10618 	 * "excessive" controller DMA capabilities.
10619 	 * If a device is being probed/initialized, there are
10620 	 * no device info - use default limits then.
10621 	 */
10622 	if (sdinfo == NULL) {
10623 		count_max = dma_attr->dma_attr_granular * 0x100;
10624 		if (dma_attr->dma_attr_count_max > count_max)
10625 			adj_dma_attr->dma_attr_count_max = count_max;
10626 		if (dma_attr->dma_attr_maxxfer > count_max)
10627 			adj_dma_attr->dma_attr_maxxfer = count_max;
10628 		return;
10629 	}
10630 
10631 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10632 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
10633 			/*
10634 			 * 16-bit sector count may be used - we rely on
10635 			 * the assumption that only read and write cmds
10636 			 * will request more than 256 sectors worth of data
10637 			 */
10638 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
10639 		} else {
10640 			/*
10641 			 * 8-bit sector count will be used - default limits
10642 			 * for dma attributes
10643 			 */
10644 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
10645 		}
10646 		/*
10647 		 * Adjust controler dma attributes, if necessary
10648 		 */
10649 		if (dma_attr->dma_attr_count_max > count_max)
10650 			adj_dma_attr->dma_attr_count_max = count_max;
10651 		if (dma_attr->dma_attr_maxxfer > count_max)
10652 			adj_dma_attr->dma_attr_maxxfer = count_max;
10653 	}
10654 }
10655 
10656 
10657 /*
10658  * Allocate DMA resources for the buffer
10659  * This function handles initial DMA resource allocation as well as
10660  * DMA window shift and may be called repeatedly for the same DMA window
10661  * until all DMA cookies in the DMA window are processed.
10662  * To guarantee that there is always a coherent set of cookies to process
10663  * by SATA HBA driver (observing alignment, device granularity, etc.),
10664  * the number of slots for DMA cookies is equal to lesser of  a number of
10665  * cookies in a DMA window and a max number of scatter/gather entries.
10666  *
10667  * Returns DDI_SUCCESS upon successful operation.
10668  * Return failure code of a failing command or DDI_FAILURE when
10669  * internal cleanup failed.
10670  */
10671 static int
10672 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
10673     int (*callback)(caddr_t), caddr_t arg,
10674     ddi_dma_attr_t *cur_dma_attr)
10675 {
10676 	int	rval;
10677 	off_t	offset;
10678 	size_t	size;
10679 	int	max_sg_len, req_len, i;
10680 	uint_t	dma_flags;
10681 	struct buf	*bp;
10682 	uint64_t	cur_txfer_len;
10683 
10684 
10685 	ASSERT(spx->txlt_sata_pkt != NULL);
10686 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
10687 	ASSERT(bp != NULL);
10688 
10689 
10690 	if (spx->txlt_buf_dma_handle == NULL) {
10691 		/*
10692 		 * No DMA resources allocated so far - this is a first call
10693 		 * for this sata pkt.
10694 		 */
10695 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
10696 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
10697 
10698 		if (rval != DDI_SUCCESS) {
10699 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10700 			    "sata_dma_buf_setup: no buf DMA resources %x",
10701 			    rval));
10702 			return (rval);
10703 		}
10704 
10705 		if (bp->b_flags & B_READ)
10706 			dma_flags = DDI_DMA_READ;
10707 		else
10708 			dma_flags = DDI_DMA_WRITE;
10709 
10710 		if (flags & PKT_CONSISTENT)
10711 			dma_flags |= DDI_DMA_CONSISTENT;
10712 
10713 		if (flags & PKT_DMA_PARTIAL)
10714 			dma_flags |= DDI_DMA_PARTIAL;
10715 
10716 		/*
10717 		 * Check buffer alignment and size against dma attributes
10718 		 * Consider dma_attr_align only. There may be requests
10719 		 * with the size lower than device granularity, but they
10720 		 * will not read/write from/to the device, so no adjustment
10721 		 * is necessary. The dma_attr_minxfer theoretically should
10722 		 * be considered, but no HBA driver is checking it.
10723 		 */
10724 		if (IS_P2ALIGNED(bp->b_un.b_addr,
10725 		    cur_dma_attr->dma_attr_align)) {
10726 			rval = ddi_dma_buf_bind_handle(
10727 			    spx->txlt_buf_dma_handle,
10728 			    bp, dma_flags, callback, arg,
10729 			    &spx->txlt_dma_cookie,
10730 			    &spx->txlt_curwin_num_dma_cookies);
10731 		} else { /* Buffer is not aligned */
10732 
10733 			int	(*ddicallback)(caddr_t);
10734 			size_t	bufsz;
10735 
10736 			/* Check id sleeping is allowed */
10737 			ddicallback = (callback == NULL_FUNC) ?
10738 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
10739 
10740 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10741 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
10742 			    (void *)bp->b_un.b_addr, bp->b_bcount);
10743 
10744 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
10745 				/*
10746 				 * CPU will need to access data in the buffer
10747 				 * (for copying) so map it.
10748 				 */
10749 				bp_mapin(bp);
10750 
10751 			ASSERT(spx->txlt_tmp_buf == NULL);
10752 
10753 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
10754 			rval = ddi_dma_mem_alloc(
10755 			    spx->txlt_buf_dma_handle,
10756 			    bp->b_bcount,
10757 			    &sata_acc_attr,
10758 			    DDI_DMA_STREAMING,
10759 			    ddicallback, NULL,
10760 			    &spx->txlt_tmp_buf,
10761 			    &bufsz,
10762 			    &spx->txlt_tmp_buf_handle);
10763 
10764 			if (rval != DDI_SUCCESS) {
10765 				/* DMA mapping failed */
10766 				(void) ddi_dma_free_handle(
10767 				    &spx->txlt_buf_dma_handle);
10768 				spx->txlt_buf_dma_handle = NULL;
10769 #ifdef SATA_DEBUG
10770 				mbuffail_count++;
10771 #endif
10772 				SATADBG1(SATA_DBG_DMA_SETUP,
10773 				    spx->txlt_sata_hba_inst,
10774 				    "sata_dma_buf_setup: "
10775 				    "buf dma mem alloc failed %x\n", rval);
10776 				return (rval);
10777 			}
10778 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
10779 			    cur_dma_attr->dma_attr_align));
10780 
10781 #ifdef SATA_DEBUG
10782 			mbuf_count++;
10783 
10784 			if (bp->b_bcount != bufsz)
10785 				/*
10786 				 * This will require special handling, because
10787 				 * DMA cookies will be based on the temporary
10788 				 * buffer size, not the original buffer
10789 				 * b_bcount, so the residue may have to
10790 				 * be counted differently.
10791 				 */
10792 				SATADBG2(SATA_DBG_DMA_SETUP,
10793 				    spx->txlt_sata_hba_inst,
10794 				    "sata_dma_buf_setup: bp size %x != "
10795 				    "bufsz %x\n", bp->b_bcount, bufsz);
10796 #endif
10797 			if (dma_flags & DDI_DMA_WRITE) {
10798 				/*
10799 				 * Write operation - copy data into
10800 				 * an aligned temporary buffer. Buffer will be
10801 				 * synced for device by ddi_dma_addr_bind_handle
10802 				 */
10803 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10804 				    bp->b_bcount);
10805 			}
10806 
10807 			rval = ddi_dma_addr_bind_handle(
10808 			    spx->txlt_buf_dma_handle,
10809 			    NULL,
10810 			    spx->txlt_tmp_buf,
10811 			    bufsz, dma_flags, ddicallback, 0,
10812 			    &spx->txlt_dma_cookie,
10813 			    &spx->txlt_curwin_num_dma_cookies);
10814 		}
10815 
10816 		switch (rval) {
10817 		case DDI_DMA_PARTIAL_MAP:
10818 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10819 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10820 			/*
10821 			 * Partial DMA mapping.
10822 			 * Retrieve number of DMA windows for this request.
10823 			 */
10824 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10825 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10826 				if (spx->txlt_tmp_buf != NULL) {
10827 					ddi_dma_mem_free(
10828 					    &spx->txlt_tmp_buf_handle);
10829 					spx->txlt_tmp_buf = NULL;
10830 				}
10831 				(void) ddi_dma_unbind_handle(
10832 				    spx->txlt_buf_dma_handle);
10833 				(void) ddi_dma_free_handle(
10834 				    &spx->txlt_buf_dma_handle);
10835 				spx->txlt_buf_dma_handle = NULL;
10836 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10837 				    "sata_dma_buf_setup: numwin failed\n"));
10838 				return (DDI_FAILURE);
10839 			}
10840 			SATADBG2(SATA_DBG_DMA_SETUP,
10841 			    spx->txlt_sata_hba_inst,
10842 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10843 			    spx->txlt_num_dma_win,
10844 			    spx->txlt_curwin_num_dma_cookies);
10845 			spx->txlt_cur_dma_win = 0;
10846 			break;
10847 
10848 		case DDI_DMA_MAPPED:
10849 			/* DMA fully mapped */
10850 			spx->txlt_num_dma_win = 1;
10851 			spx->txlt_cur_dma_win = 0;
10852 			SATADBG1(SATA_DBG_DMA_SETUP,
10853 			    spx->txlt_sata_hba_inst,
10854 			    "sata_dma_buf_setup: windows: 1 "
10855 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10856 			break;
10857 
10858 		default:
10859 			/* DMA mapping failed */
10860 			if (spx->txlt_tmp_buf != NULL) {
10861 				ddi_dma_mem_free(
10862 				    &spx->txlt_tmp_buf_handle);
10863 				spx->txlt_tmp_buf = NULL;
10864 			}
10865 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10866 			spx->txlt_buf_dma_handle = NULL;
10867 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10868 			    "sata_dma_buf_setup: buf dma handle binding "
10869 			    "failed %x\n", rval));
10870 			return (rval);
10871 		}
10872 		spx->txlt_curwin_processed_dma_cookies = 0;
10873 		spx->txlt_dma_cookie_list = NULL;
10874 	} else {
10875 		/*
10876 		 * DMA setup is reused. Check if we need to process more
10877 		 * cookies in current window, or to get next window, if any.
10878 		 */
10879 
10880 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10881 		    spx->txlt_curwin_num_dma_cookies);
10882 
10883 		if (spx->txlt_curwin_processed_dma_cookies ==
10884 		    spx->txlt_curwin_num_dma_cookies) {
10885 			/*
10886 			 * All cookies from current DMA window were processed.
10887 			 * Get next DMA window.
10888 			 */
10889 			spx->txlt_cur_dma_win++;
10890 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10891 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10892 				    spx->txlt_cur_dma_win, &offset, &size,
10893 				    &spx->txlt_dma_cookie,
10894 				    &spx->txlt_curwin_num_dma_cookies);
10895 				spx->txlt_curwin_processed_dma_cookies = 0;
10896 			} else {
10897 				/* No more windows! End of request! */
10898 				/* What to do? - panic for now */
10899 				ASSERT(spx->txlt_cur_dma_win >=
10900 				    spx->txlt_num_dma_win);
10901 
10902 				spx->txlt_curwin_num_dma_cookies = 0;
10903 				spx->txlt_curwin_processed_dma_cookies = 0;
10904 				spx->txlt_sata_pkt->
10905 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10906 				return (DDI_SUCCESS);
10907 			}
10908 		}
10909 	}
10910 	/* There better be at least one DMA cookie outstanding */
10911 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10912 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10913 
10914 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10915 		/* The default cookie slot was used in previous run */
10916 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10917 		spx->txlt_dma_cookie_list = NULL;
10918 		spx->txlt_dma_cookie_list_len = 0;
10919 	}
10920 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10921 		/*
10922 		 * Processing a new DMA window - set-up dma cookies list.
10923 		 * We may reuse previously allocated cookie array if it is
10924 		 * possible.
10925 		 */
10926 		if (spx->txlt_dma_cookie_list != NULL &&
10927 		    spx->txlt_dma_cookie_list_len <
10928 		    spx->txlt_curwin_num_dma_cookies) {
10929 			/*
10930 			 * New DMA window contains more cookies than
10931 			 * the previous one. We need larger cookie list - free
10932 			 * the old one.
10933 			 */
10934 			(void) kmem_free(spx->txlt_dma_cookie_list,
10935 			    spx->txlt_dma_cookie_list_len *
10936 			    sizeof (ddi_dma_cookie_t));
10937 			spx->txlt_dma_cookie_list = NULL;
10938 			spx->txlt_dma_cookie_list_len = 0;
10939 		}
10940 		if (spx->txlt_dma_cookie_list == NULL) {
10941 			/*
10942 			 * Calculate lesser of number of cookies in this
10943 			 * DMA window and number of s/g entries.
10944 			 */
10945 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10946 			req_len = MIN(max_sg_len,
10947 			    spx->txlt_curwin_num_dma_cookies);
10948 
10949 			/* Allocate new dma cookie array if necessary */
10950 			if (req_len == 1) {
10951 				/* Only one cookie - no need for a list */
10952 				spx->txlt_dma_cookie_list =
10953 				    &spx->txlt_dma_cookie;
10954 				spx->txlt_dma_cookie_list_len = 1;
10955 			} else {
10956 				/*
10957 				 * More than one cookie - try to allocate space.
10958 				 */
10959 				spx->txlt_dma_cookie_list = kmem_zalloc(
10960 				    sizeof (ddi_dma_cookie_t) * req_len,
10961 				    callback == NULL_FUNC ? KM_NOSLEEP :
10962 				    KM_SLEEP);
10963 				if (spx->txlt_dma_cookie_list == NULL) {
10964 					SATADBG1(SATA_DBG_DMA_SETUP,
10965 					    spx->txlt_sata_hba_inst,
10966 					    "sata_dma_buf_setup: cookie list "
10967 					    "allocation failed\n", NULL);
10968 					/*
10969 					 * We could not allocate space for
10970 					 * neccessary number of dma cookies in
10971 					 * this window, so we fail this request.
10972 					 * Next invocation would try again to
10973 					 * allocate space for cookie list.
10974 					 * Note:Packet residue was not modified.
10975 					 */
10976 					return (DDI_DMA_NORESOURCES);
10977 				} else {
10978 					spx->txlt_dma_cookie_list_len = req_len;
10979 				}
10980 			}
10981 		}
10982 		/*
10983 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10984 		 * First cookie was already fetched.
10985 		 */
10986 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10987 		cur_txfer_len =
10988 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10989 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10990 		spx->txlt_curwin_processed_dma_cookies++;
10991 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10992 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10993 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10994 			    &spx->txlt_dma_cookie_list[i]);
10995 			cur_txfer_len +=
10996 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10997 			spx->txlt_curwin_processed_dma_cookies++;
10998 			spx->txlt_sata_pkt->
10999 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
11000 		}
11001 	} else {
11002 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
11003 		    "sata_dma_buf_setup: sliding within DMA window, "
11004 		    "cur cookie %d, total cookies %d\n",
11005 		    spx->txlt_curwin_processed_dma_cookies,
11006 		    spx->txlt_curwin_num_dma_cookies);
11007 
11008 		/*
11009 		 * Not all cookies from the current dma window were used because
11010 		 * of s/g limitation.
11011 		 * There is no need to re-size the list - it was set at
11012 		 * optimal size, or only default entry is used (s/g = 1).
11013 		 */
11014 		if (spx->txlt_dma_cookie_list == NULL) {
11015 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
11016 			spx->txlt_dma_cookie_list_len = 1;
11017 		}
11018 		/*
11019 		 * Since we are processing remaining cookies in a DMA window,
11020 		 * there may be less of them than the number of entries in the
11021 		 * current dma cookie list.
11022 		 */
11023 		req_len = MIN(spx->txlt_dma_cookie_list_len,
11024 		    (spx->txlt_curwin_num_dma_cookies -
11025 		    spx->txlt_curwin_processed_dma_cookies));
11026 
11027 		/* Fetch the next batch of cookies */
11028 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
11029 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
11030 			    &spx->txlt_dma_cookie_list[i]);
11031 			cur_txfer_len +=
11032 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
11033 			spx->txlt_sata_pkt->
11034 			    satapkt_cmd.satacmd_num_dma_cookies++;
11035 			spx->txlt_curwin_processed_dma_cookies++;
11036 		}
11037 	}
11038 
11039 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
11040 
11041 	/* Point sata_cmd to the cookie list */
11042 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
11043 	    &spx->txlt_dma_cookie_list[0];
11044 
11045 	/* Remember number of DMA cookies passed in sata packet */
11046 	spx->txlt_num_dma_cookies =
11047 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
11048 
11049 	ASSERT(cur_txfer_len != 0);
11050 	if (cur_txfer_len <= bp->b_bcount)
11051 		spx->txlt_total_residue -= cur_txfer_len;
11052 	else {
11053 		/*
11054 		 * Temporary DMA buffer has been padded by
11055 		 * ddi_dma_mem_alloc()!
11056 		 * This requires special handling, because DMA cookies are
11057 		 * based on the temporary buffer size, not the b_bcount,
11058 		 * and we have extra bytes to transfer - but the packet
11059 		 * residue has to stay correct because we will copy only
11060 		 * the requested number of bytes.
11061 		 */
11062 		spx->txlt_total_residue -= bp->b_bcount;
11063 	}
11064 
11065 	return (DDI_SUCCESS);
11066 }
11067 
11068 /*
11069  * Common routine for releasing DMA resources
11070  */
11071 static void
11072 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
11073 {
11074 	if (spx->txlt_buf_dma_handle != NULL) {
11075 		if (spx->txlt_tmp_buf != NULL)  {
11076 			/*
11077 			 * Intermediate DMA buffer was allocated.
11078 			 * Free allocated buffer and associated access handle.
11079 			 */
11080 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
11081 			spx->txlt_tmp_buf = NULL;
11082 		}
11083 		/*
11084 		 * Free DMA resources - cookies and handles
11085 		 */
11086 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
11087 		if (spx->txlt_dma_cookie_list != NULL) {
11088 			if (spx->txlt_dma_cookie_list !=
11089 			    &spx->txlt_dma_cookie) {
11090 				(void) kmem_free(spx->txlt_dma_cookie_list,
11091 				    spx->txlt_dma_cookie_list_len *
11092 				    sizeof (ddi_dma_cookie_t));
11093 				spx->txlt_dma_cookie_list = NULL;
11094 			}
11095 		}
11096 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
11097 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
11098 		spx->txlt_buf_dma_handle = NULL;
11099 	}
11100 }
11101 
11102 /*
11103  * Free DMA resources
11104  * Used by the HBA driver to release DMA resources that it does not use.
11105  *
11106  * Returns Void
11107  */
11108 void
11109 sata_free_dma_resources(sata_pkt_t *sata_pkt)
11110 {
11111 	sata_pkt_txlate_t *spx;
11112 
11113 	if (sata_pkt == NULL)
11114 		return;
11115 
11116 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
11117 
11118 	sata_common_free_dma_rsrcs(spx);
11119 }
11120 
11121 /*
11122  * Fetch Device Identify data.
11123  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
11124  * command to a device and get the device identify data.
11125  * The device_info structure has to be set to device type (for selecting proper
11126  * device identify command).
11127  *
11128  * Returns:
11129  * SATA_SUCCESS if cmd succeeded
11130  * SATA_RETRY if cmd was rejected and could be retried,
11131  * SATA_FAILURE if cmd failed and should not be retried (port error)
11132  *
11133  * Cannot be called in an interrupt context.
11134  */
11135 
11136 static int
11137 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
11138     sata_drive_info_t *sdinfo)
11139 {
11140 	struct buf *bp;
11141 	sata_pkt_t *spkt;
11142 	sata_cmd_t *scmd;
11143 	sata_pkt_txlate_t *spx;
11144 	int rval;
11145 
11146 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11147 	spx->txlt_sata_hba_inst = sata_hba_inst;
11148 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
11149 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11150 	if (spkt == NULL) {
11151 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11152 		return (SATA_RETRY); /* may retry later */
11153 	}
11154 	/* address is needed now */
11155 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11156 
11157 	/*
11158 	 * Allocate buffer for Identify Data return data
11159 	 */
11160 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
11161 	if (bp == NULL) {
11162 		sata_pkt_free(spx);
11163 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11164 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11165 		    "sata_fetch_device_identify_data: "
11166 		    "cannot allocate buffer for ID"));
11167 		return (SATA_RETRY); /* may retry later */
11168 	}
11169 
11170 	/* Fill sata_pkt */
11171 	sdinfo->satadrv_state = SATA_STATE_PROBING;
11172 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11173 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11174 	/* Synchronous mode, no callback */
11175 	spkt->satapkt_comp = NULL;
11176 	/* Timeout 30s */
11177 	spkt->satapkt_time = sata_default_pkt_time;
11178 
11179 	scmd = &spkt->satapkt_cmd;
11180 	scmd->satacmd_bp = bp;
11181 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
11182 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11183 
11184 	/* Build Identify Device cmd in the sata_pkt */
11185 	scmd->satacmd_addr_type = 0;		/* N/A */
11186 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
11187 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
11188 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
11189 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
11190 	scmd->satacmd_features_reg = 0;		/* N/A */
11191 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
11192 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11193 		/* Identify Packet Device cmd */
11194 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
11195 	} else {
11196 		/* Identify Device cmd - mandatory for all other devices */
11197 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
11198 	}
11199 
11200 	/* Send pkt to SATA HBA driver */
11201 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
11202 
11203 #ifdef SATA_INJECT_FAULTS
11204 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
11205 #endif
11206 
11207 	if (rval == SATA_TRAN_ACCEPTED &&
11208 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
11209 		if (spx->txlt_buf_dma_handle != NULL) {
11210 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
11211 			    DDI_DMA_SYNC_FORKERNEL);
11212 			ASSERT(rval == DDI_SUCCESS);
11213 		}
11214 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
11215 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
11216 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11217 			    "SATA disk device at port %d - "
11218 			    "partial Identify Data",
11219 			    sdinfo->satadrv_addr.cport));
11220 			rval = SATA_RETRY; /* may retry later */
11221 			goto fail;
11222 		}
11223 		/* Update sata_drive_info */
11224 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
11225 		    sizeof (sata_id_t));
11226 
11227 		sdinfo->satadrv_features_support = 0;
11228 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11229 			/*
11230 			 * Retrieve capacity (disks only) and addressing mode
11231 			 */
11232 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
11233 		} else {
11234 			/*
11235 			 * For ATAPI devices one would have to issue
11236 			 * Get Capacity cmd for media capacity. Not here.
11237 			 */
11238 			sdinfo->satadrv_capacity = 0;
11239 			/*
11240 			 * Check what cdb length is supported
11241 			 */
11242 			if ((sdinfo->satadrv_id.ai_config &
11243 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
11244 				sdinfo->satadrv_atapi_cdb_len = 16;
11245 			else
11246 				sdinfo->satadrv_atapi_cdb_len = 12;
11247 		}
11248 		/* Setup supported features flags */
11249 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
11250 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
11251 
11252 		/* Check for SATA GEN and NCQ support */
11253 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
11254 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
11255 			/* SATA compliance */
11256 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
11257 				sdinfo->satadrv_features_support |=
11258 				    SATA_DEV_F_NCQ;
11259 			if (sdinfo->satadrv_id.ai_satacap &
11260 			    (SATA_1_SPEED | SATA_2_SPEED)) {
11261 				if (sdinfo->satadrv_id.ai_satacap &
11262 				    SATA_2_SPEED)
11263 					sdinfo->satadrv_features_support |=
11264 					    SATA_DEV_F_SATA2;
11265 				if (sdinfo->satadrv_id.ai_satacap &
11266 				    SATA_1_SPEED)
11267 					sdinfo->satadrv_features_support |=
11268 					    SATA_DEV_F_SATA1;
11269 			} else {
11270 				sdinfo->satadrv_features_support |=
11271 				    SATA_DEV_F_SATA1;
11272 			}
11273 		}
11274 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
11275 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
11276 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
11277 
11278 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
11279 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
11280 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
11281 			++sdinfo->satadrv_queue_depth;
11282 			/* Adjust according to controller capabilities */
11283 			sdinfo->satadrv_max_queue_depth = MIN(
11284 			    sdinfo->satadrv_queue_depth,
11285 			    SATA_QDEPTH(sata_hba_inst));
11286 			/* Adjust according to global queue depth limit */
11287 			sdinfo->satadrv_max_queue_depth = MIN(
11288 			    sdinfo->satadrv_max_queue_depth,
11289 			    sata_current_max_qdepth);
11290 			if (sdinfo->satadrv_max_queue_depth == 0)
11291 				sdinfo->satadrv_max_queue_depth = 1;
11292 		} else
11293 			sdinfo->satadrv_max_queue_depth = 1;
11294 
11295 		rval = SATA_SUCCESS;
11296 	} else {
11297 		/*
11298 		 * Woops, no Identify Data.
11299 		 */
11300 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
11301 			rval = SATA_RETRY; /* may retry later */
11302 		} else if (rval == SATA_TRAN_ACCEPTED) {
11303 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
11304 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
11305 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
11306 			    spkt->satapkt_reason == SATA_PKT_RESET)
11307 				rval = SATA_RETRY; /* may retry later */
11308 			else
11309 				rval = SATA_FAILURE;
11310 		} else {
11311 			rval = SATA_FAILURE;
11312 		}
11313 	}
11314 fail:
11315 	/* Free allocated resources */
11316 	sata_free_local_buffer(spx);
11317 	sata_pkt_free(spx);
11318 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
11319 
11320 	return (rval);
11321 }
11322 
11323 
11324 /*
11325  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
11326  * UDMA mode is checked first, followed by MWDMA mode.
11327  * set correctly, so this function is setting it to the highest supported level.
11328  * Older SATA spec required that the device supports at least DMA 4 mode and
11329  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
11330  * restriction has been removed.
11331  *
11332  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
11333  * Returns SATA_FAILURE if proper DMA mode could not be selected.
11334  *
11335  * NOTE: This function should be called only if DMA mode is supported.
11336  */
11337 static int
11338 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
11339 {
11340 	sata_pkt_t *spkt;
11341 	sata_cmd_t *scmd;
11342 	sata_pkt_txlate_t *spx;
11343 	int mode;
11344 	uint8_t subcmd;
11345 	int rval = SATA_SUCCESS;
11346 
11347 	ASSERT(sdinfo != NULL);
11348 	ASSERT(sata_hba_inst != NULL);
11349 
11350 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11351 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
11352 		/* Find highest Ultra DMA mode supported */
11353 		for (mode = 6; mode >= 0; --mode) {
11354 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
11355 				break;
11356 		}
11357 #if 0
11358 		/* Left for historical reasons */
11359 		/*
11360 		 * Some initial version of SATA spec indicated that at least
11361 		 * UDMA mode 4 has to be supported. It is not mentioned in
11362 		 * SerialATA 2.6, so this restriction is removed.
11363 		 */
11364 		if (mode < 4)
11365 			return (SATA_FAILURE);
11366 #endif
11367 
11368 		/*
11369 		 * We're still going to set DMA mode whatever is selected
11370 		 * by default
11371 		 *
11372 		 * We saw an old maxtor sata drive will select Ultra DMA and
11373 		 * Multi-Word DMA simultaneouly by default, which is going
11374 		 * to cause DMA command timed out, so we need to select DMA
11375 		 * mode even when it's already done by default
11376 		 */
11377 
11378 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
11379 
11380 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
11381 		/* Find highest MultiWord DMA mode supported */
11382 		for (mode = 2; mode >= 0; --mode) {
11383 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
11384 				break;
11385 		}
11386 
11387 		/*
11388 		 * We're still going to set DMA mode whatever is selected
11389 		 * by default
11390 		 *
11391 		 * We saw an old maxtor sata drive will select Ultra DMA and
11392 		 * Multi-Word DMA simultaneouly by default, which is going
11393 		 * to cause DMA command timed out, so we need to select DMA
11394 		 * mode even when it's already done by default
11395 		 */
11396 
11397 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
11398 	} else
11399 		return (SATA_SUCCESS);
11400 
11401 	/*
11402 	 * Set DMA mode via SET FEATURES COMMAND.
11403 	 * Prepare packet for SET FEATURES COMMAND.
11404 	 */
11405 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11406 	spx->txlt_sata_hba_inst = sata_hba_inst;
11407 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11408 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11409 	if (spkt == NULL) {
11410 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11411 		    "sata_set_dma_mode: could not set DMA mode %", mode));
11412 		rval = SATA_FAILURE;
11413 		goto done;
11414 	}
11415 	/* Fill sata_pkt */
11416 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11417 	/* Timeout 30s */
11418 	spkt->satapkt_time = sata_default_pkt_time;
11419 	/* Synchronous mode, no callback, interrupts */
11420 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11421 	spkt->satapkt_comp = NULL;
11422 	scmd = &spkt->satapkt_cmd;
11423 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11424 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11425 	scmd->satacmd_addr_type = 0;
11426 	scmd->satacmd_device_reg = 0;
11427 	scmd->satacmd_status_reg = 0;
11428 	scmd->satacmd_error_reg = 0;
11429 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11430 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
11431 	scmd->satacmd_sec_count_lsb = subcmd | mode;
11432 
11433 	/* Transfer command to HBA */
11434 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11435 	    spkt) != SATA_TRAN_ACCEPTED ||
11436 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
11437 		/* Pkt execution failed */
11438 		rval = SATA_FAILURE;
11439 	}
11440 done:
11441 
11442 	/* Free allocated resources */
11443 	if (spkt != NULL)
11444 		sata_pkt_free(spx);
11445 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11446 
11447 	return (rval);
11448 }
11449 
11450 
11451 /*
11452  * Set device caching mode.
11453  * One of the following operations should be specified:
11454  * SATAC_SF_ENABLE_READ_AHEAD
11455  * SATAC_SF_DISABLE_READ_AHEAD
11456  * SATAC_SF_ENABLE_WRITE_CACHE
11457  * SATAC_SF_DISABLE_WRITE_CACHE
11458  *
11459  * If operation fails, system log messgage is emitted.
11460  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
11461  * command was sent but did not succeed, and SATA_FAILURE otherwise.
11462  */
11463 
11464 static int
11465 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11466     int cache_op)
11467 {
11468 	sata_pkt_t *spkt;
11469 	sata_cmd_t *scmd;
11470 	sata_pkt_txlate_t *spx;
11471 	int rval = SATA_SUCCESS;
11472 	int hba_rval;
11473 	char *infop;
11474 
11475 	ASSERT(sdinfo != NULL);
11476 	ASSERT(sata_hba_inst != NULL);
11477 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
11478 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
11479 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
11480 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
11481 
11482 
11483 	/* Prepare packet for SET FEATURES COMMAND */
11484 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11485 	spx->txlt_sata_hba_inst = sata_hba_inst;
11486 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11487 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11488 	if (spkt == NULL) {
11489 		rval = SATA_FAILURE;
11490 		goto failure;
11491 	}
11492 	/* Fill sata_pkt */
11493 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11494 	/* Timeout 30s */
11495 	spkt->satapkt_time = sata_default_pkt_time;
11496 	/* Synchronous mode, no callback, interrupts */
11497 	spkt->satapkt_op_mode =
11498 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11499 	spkt->satapkt_comp = NULL;
11500 	scmd = &spkt->satapkt_cmd;
11501 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11502 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11503 	scmd->satacmd_addr_type = 0;
11504 	scmd->satacmd_device_reg = 0;
11505 	scmd->satacmd_status_reg = 0;
11506 	scmd->satacmd_error_reg = 0;
11507 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11508 	scmd->satacmd_features_reg = cache_op;
11509 
11510 	/* Transfer command to HBA */
11511 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
11512 	    SATA_DIP(sata_hba_inst), spkt);
11513 
11514 #ifdef SATA_INJECT_FAULTS
11515 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
11516 #endif
11517 
11518 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
11519 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11520 		/* Pkt execution failed */
11521 		switch (cache_op) {
11522 		case SATAC_SF_ENABLE_READ_AHEAD:
11523 			infop = "enabling read ahead failed";
11524 			break;
11525 		case SATAC_SF_DISABLE_READ_AHEAD:
11526 			infop = "disabling read ahead failed";
11527 			break;
11528 		case SATAC_SF_ENABLE_WRITE_CACHE:
11529 			infop = "enabling write cache failed";
11530 			break;
11531 		case SATAC_SF_DISABLE_WRITE_CACHE:
11532 			infop = "disabling write cache failed";
11533 			break;
11534 		}
11535 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11536 		rval = SATA_RETRY;
11537 	}
11538 failure:
11539 	/* Free allocated resources */
11540 	if (spkt != NULL)
11541 		sata_pkt_free(spx);
11542 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11543 	return (rval);
11544 }
11545 
11546 /*
11547  * Set Removable Media Status Notification (enable/disable)
11548  * state == 0 , disable
11549  * state != 0 , enable
11550  *
11551  * If operation fails, system log messgage is emitted.
11552  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11553  */
11554 
11555 static int
11556 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11557     int state)
11558 {
11559 	sata_pkt_t *spkt;
11560 	sata_cmd_t *scmd;
11561 	sata_pkt_txlate_t *spx;
11562 	int rval = SATA_SUCCESS;
11563 	char *infop;
11564 
11565 	ASSERT(sdinfo != NULL);
11566 	ASSERT(sata_hba_inst != NULL);
11567 
11568 	/* Prepare packet for SET FEATURES COMMAND */
11569 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11570 	spx->txlt_sata_hba_inst = sata_hba_inst;
11571 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11572 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11573 	if (spkt == NULL) {
11574 		rval = SATA_FAILURE;
11575 		goto failure;
11576 	}
11577 	/* Fill sata_pkt */
11578 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11579 	/* Timeout 30s */
11580 	spkt->satapkt_time = sata_default_pkt_time;
11581 	/* Synchronous mode, no callback, interrupts */
11582 	spkt->satapkt_op_mode =
11583 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11584 	spkt->satapkt_comp = NULL;
11585 	scmd = &spkt->satapkt_cmd;
11586 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11587 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11588 	scmd->satacmd_addr_type = 0;
11589 	scmd->satacmd_device_reg = 0;
11590 	scmd->satacmd_status_reg = 0;
11591 	scmd->satacmd_error_reg = 0;
11592 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11593 	if (state == 0)
11594 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
11595 	else
11596 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
11597 
11598 	/* Transfer command to HBA */
11599 	if (((*SATA_START_FUNC(sata_hba_inst))(
11600 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11601 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11602 		/* Pkt execution failed */
11603 		if (state == 0)
11604 			infop = "disabling Removable Media Status "
11605 			    "Notification failed";
11606 		else
11607 			infop = "enabling Removable Media Status "
11608 			    "Notification failed";
11609 
11610 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11611 		rval = SATA_FAILURE;
11612 	}
11613 failure:
11614 	/* Free allocated resources */
11615 	if (spkt != NULL)
11616 		sata_pkt_free(spx);
11617 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11618 	return (rval);
11619 }
11620 
11621 
11622 /*
11623  * Update port SCR block
11624  */
11625 static void
11626 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
11627 {
11628 	port_scr->sstatus = device->satadev_scr.sstatus;
11629 	port_scr->serror = device->satadev_scr.serror;
11630 	port_scr->scontrol = device->satadev_scr.scontrol;
11631 	port_scr->sactive = device->satadev_scr.sactive;
11632 	port_scr->snotific = device->satadev_scr.snotific;
11633 }
11634 
11635 /*
11636  * Update state and copy port ss* values from passed sata_device structure.
11637  * sata_address is validated - if not valid, nothing is changed in sata_scsi
11638  * configuration struct.
11639  *
11640  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
11641  * regardless of the state in device argument.
11642  *
11643  * Port mutex should be held while calling this function.
11644  */
11645 static void
11646 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
11647 	sata_device_t *sata_device)
11648 {
11649 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
11650 	    sata_device->satadev_addr.cport)));
11651 
11652 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
11653 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
11654 
11655 		sata_cport_info_t *cportinfo;
11656 
11657 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
11658 		    sata_device->satadev_addr.cport)
11659 			return;
11660 
11661 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11662 		    sata_device->satadev_addr.cport);
11663 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
11664 
11665 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11666 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
11667 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
11668 		cportinfo->cport_state |=
11669 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11670 	} else {
11671 		sata_pmport_info_t *pmportinfo;
11672 
11673 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
11674 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
11675 		    SATA_NUM_PMPORTS(sata_hba_inst,
11676 		    sata_device->satadev_addr.cport) <
11677 		    sata_device->satadev_addr.pmport)
11678 			return;
11679 
11680 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11681 		    sata_device->satadev_addr.cport,
11682 		    sata_device->satadev_addr.pmport);
11683 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
11684 
11685 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11686 		pmportinfo->pmport_state &=
11687 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
11688 		    SATA_PSTATE_FAILED);
11689 		pmportinfo->pmport_state |=
11690 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11691 	}
11692 }
11693 
11694 
11695 
11696 /*
11697  * Extract SATA port specification from an IOCTL argument.
11698  *
11699  * This function return the port the user land send us as is, unless it
11700  * cannot retrieve port spec, then -1 is returned.
11701  *
11702  * Note: Only cport  - no port multiplier port.
11703  */
11704 static int32_t
11705 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
11706 {
11707 	int32_t port;
11708 
11709 	/* Extract port number from nvpair in dca structure  */
11710 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
11711 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
11712 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
11713 		    port));
11714 		port = -1;
11715 	}
11716 
11717 	return (port);
11718 }
11719 
11720 /*
11721  * Get dev_info_t pointer to the device node pointed to by port argument.
11722  * NOTE: target argument is a value used in ioctls to identify
11723  * the AP - it is not a sata_address.
11724  * It is a combination of cport, pmport and address qualifier, encodded same
11725  * way as a scsi target number.
11726  * At this moment it carries only cport number.
11727  *
11728  * No PMult hotplug support.
11729  *
11730  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11731  */
11732 
11733 static dev_info_t *
11734 sata_get_target_dip(dev_info_t *dip, int32_t port)
11735 {
11736 	dev_info_t	*cdip = NULL;
11737 	int		target, tgt;
11738 	int		ncport;
11739 	int 		circ;
11740 
11741 	ncport = port & SATA_CFGA_CPORT_MASK;
11742 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
11743 
11744 	ndi_devi_enter(dip, &circ);
11745 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11746 		dev_info_t *next = ddi_get_next_sibling(cdip);
11747 
11748 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11749 		    DDI_PROP_DONTPASS, "target", -1);
11750 		if (tgt == -1) {
11751 			/*
11752 			 * This is actually an error condition, but not
11753 			 * a fatal one. Just continue the search.
11754 			 */
11755 			cdip = next;
11756 			continue;
11757 		}
11758 
11759 		if (tgt == target)
11760 			break;
11761 
11762 		cdip = next;
11763 	}
11764 	ndi_devi_exit(dip, circ);
11765 
11766 	return (cdip);
11767 }
11768 
11769 /*
11770  * Get dev_info_t pointer to the device node pointed to by port argument.
11771  * NOTE: target argument is a value used in ioctls to identify
11772  * the AP - it is not a sata_address.
11773  * It is a combination of cport, pmport and address qualifier, encoded same
11774  * way as a scsi target number.
11775  * At this moment it carries only cport number.
11776  *
11777  * No PMult hotplug support.
11778  *
11779  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11780  */
11781 
11782 static dev_info_t *
11783 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
11784 {
11785 	dev_info_t	*cdip = NULL;
11786 	int		target, tgt;
11787 	int 		circ;
11788 
11789 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
11790 
11791 	ndi_devi_enter(dip, &circ);
11792 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11793 		dev_info_t *next = ddi_get_next_sibling(cdip);
11794 
11795 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11796 		    DDI_PROP_DONTPASS, "target", -1);
11797 		if (tgt == -1) {
11798 			/*
11799 			 * This is actually an error condition, but not
11800 			 * a fatal one. Just continue the search.
11801 			 */
11802 			cdip = next;
11803 			continue;
11804 		}
11805 
11806 		if (tgt == target)
11807 			break;
11808 
11809 		cdip = next;
11810 	}
11811 	ndi_devi_exit(dip, circ);
11812 
11813 	return (cdip);
11814 }
11815 
11816 /*
11817  * Process sata port disconnect request.
11818  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
11819  * before this request. Nevertheless, if a device is still configured,
11820  * we need to attempt to offline and unconfigure device.
11821  * Regardless of the unconfigure operation results the port is marked as
11822  * deactivated and no access to the attached device is possible.
11823  * If the target node remains because unconfigure operation failed, its state
11824  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
11825  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
11826  * the device and remove old target node.
11827  *
11828  * This function invokes sata_hba_inst->satahba_tran->
11829  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11830  * If successful, the device structure (if any) attached to the specified port
11831  * is removed and state of the port marked appropriately.
11832  * Failure of the port_deactivate may keep port in the physically active state,
11833  * or may fail the port.
11834  *
11835  * NOTE: Port multiplier code is not completed nor tested.
11836  */
11837 
11838 static int
11839 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
11840     sata_device_t *sata_device)
11841 {
11842 	sata_drive_info_t *sdinfo = NULL;
11843 	sata_cport_info_t *cportinfo = NULL;
11844 	sata_pmport_info_t *pmportinfo = NULL;
11845 	sata_pmult_info_t *pmultinfo = NULL;
11846 	dev_info_t *tdip;
11847 	int cport, pmport, qual;
11848 	int rval = SATA_SUCCESS;
11849 	int rv = 0;
11850 
11851 	cport = sata_device->satadev_addr.cport;
11852 	pmport = sata_device->satadev_addr.pmport;
11853 	qual = sata_device->satadev_addr.qual;
11854 
11855 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11856 
11857 	/*
11858 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
11859 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11860 	 * Do the sanity check.
11861 	 */
11862 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
11863 		/* No physical port deactivation supported. */
11864 		return (EINVAL);
11865 	}
11866 
11867 	/* Check the current state of the port */
11868 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11869 	    (SATA_DIP(sata_hba_inst), sata_device);
11870 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11871 	sata_update_port_info(sata_hba_inst, sata_device);
11872 	if (rval != SATA_SUCCESS ||
11873 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11874 		/* Device port status is unknown or it is in failed state */
11875 		if (qual == SATA_ADDR_PMPORT) {
11876 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11877 			    SATA_PSTATE_FAILED;
11878 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11879 			    "sata_hba_ioctl: connect: failed to deactivate "
11880 			    "SATA port %d", cport);
11881 		} else {
11882 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11883 			    SATA_PSTATE_FAILED;
11884 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11885 			    "sata_hba_ioctl: connect: failed to deactivate "
11886 			    "SATA port %d:%d", cport, pmport);
11887 		}
11888 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11889 		    cport)->cport_mutex);
11890 		return (EIO);
11891 	}
11892 	/*
11893 	 * Set port's dev_state to not ready - this will disable
11894 	 * an access to a potentially attached device.
11895 	 */
11896 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11897 	if (qual == SATA_ADDR_PMPORT) {
11898 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11899 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11900 			sdinfo = pmportinfo->pmport_sata_drive;
11901 			ASSERT(sdinfo != NULL);
11902 		}
11903 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11904 	} else {
11905 		/* Assuming cport */
11906 
11907 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11908 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
11909 				pmultinfo =
11910 				    cportinfo->cport_devp.cport_sata_pmult;
11911 				ASSERT(pmultinfo != NULL);
11912 			} else {
11913 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
11914 			}
11915 		}
11916 		cportinfo->cport_state &= ~SATA_STATE_READY;
11917 	}
11918 	if (sdinfo != NULL) {
11919 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
11920 			/*
11921 			 * If a target node exists, try to offline
11922 			 * a device and remove target node.
11923 			 */
11924 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11925 			    cport)->cport_mutex);
11926 			/* We are addressing attached device, not a port */
11927 			sata_device->satadev_addr.qual =
11928 			    sdinfo->satadrv_addr.qual;
11929 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11930 			    &sata_device->satadev_addr);
11931 			if (tdip != NULL && ndi_devi_offline(tdip,
11932 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11933 				/*
11934 				 * Problem
11935 				 * The target node remained attached.
11936 				 * This happens when the device file was open
11937 				 * or a node was waiting for resources.
11938 				 * Cannot do anything about it.
11939 				 */
11940 				if (qual == SATA_ADDR_CPORT) {
11941 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11942 					    "sata_hba_ioctl: disconnect: could "
11943 					    "not unconfigure device before "
11944 					    "disconnecting the SATA port %d",
11945 					    cport));
11946 				} else {
11947 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11948 					    "sata_hba_ioctl: disconnect: could "
11949 					    "not unconfigure device before "
11950 					    "disconnecting the SATA port %d:%d",
11951 					    cport, pmport));
11952 				}
11953 				/*
11954 				 * Set DEVICE REMOVED state in the target
11955 				 * node. It will prevent access to the device
11956 				 * even when a new device is attached, until
11957 				 * the old target node is released, removed and
11958 				 * recreated for a new  device.
11959 				 */
11960 				sata_set_device_removed(tdip);
11961 
11962 				/*
11963 				 * Instruct event daemon to try the target
11964 				 * node cleanup later.
11965 				 */
11966 				sata_set_target_node_cleanup(
11967 				    sata_hba_inst, &sata_device->satadev_addr);
11968 			}
11969 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11970 			    cport)->cport_mutex);
11971 		}
11972 
11973 		/* Remove and release sata_drive info structure. */
11974 		if (pmportinfo != NULL) {
11975 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
11976 			    NULL;
11977 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11978 		} else {
11979 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11980 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11981 		}
11982 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11983 	}
11984 #if 0
11985 	else if (pmultinfo != NULL) {
11986 		/*
11987 		 * Port Multiplier itself needs special handling.
11988 		 * All device ports need to be processed here!
11989 		 */
11990 	}
11991 #endif
11992 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11993 	/* Just ask HBA driver to deactivate port */
11994 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
11995 
11996 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11997 	    (SATA_DIP(sata_hba_inst), sata_device);
11998 
11999 	/*
12000 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12001 	 * without the hint (to force listener to investivate the state).
12002 	 */
12003 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12004 	    SE_NO_HINT);
12005 
12006 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12007 	sata_update_port_info(sata_hba_inst, sata_device);
12008 
12009 	if (rval != SATA_SUCCESS) {
12010 		/*
12011 		 * Port deactivation failure - do not
12012 		 * change port state unless the state
12013 		 * returned by HBA indicates a port failure.
12014 		 * NOTE: device structures were released, so devices now are
12015 		 * invisible! Port reset is needed to re-enumerate devices.
12016 		 */
12017 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12018 			if (pmportinfo != NULL)
12019 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
12020 			else
12021 				cportinfo->cport_state = SATA_PSTATE_FAILED;
12022 			rv = EIO;
12023 		}
12024 	} else {
12025 		/*
12026 		 * Deactivation succeded. From now on the sata framework
12027 		 * will not care what is happening to the device, until
12028 		 * the port is activated again.
12029 		 */
12030 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
12031 	}
12032 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12033 	return (rv);
12034 }
12035 
12036 
12037 
12038 /*
12039  * Process sata port connect request
12040  * The sata cfgadm pluging will invoke this operation only if port was found
12041  * in the disconnect state (failed state is also treated as the disconnected
12042  * state).
12043  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
12044  * sata_tran_hotplug_ops->sata_tran_port_activate().
12045  * If successful and a device is found attached to the port,
12046  * the initialization sequence is executed to attach a device structure to
12047  * a port structure. The state of the port and a device would be set
12048  * appropriately.
12049  * The device is not set in configured state (system-wise) by this operation.
12050  *
12051  * Note, that activating the port may generate link events,
12052  * so it is important that following processing and the
12053  * event processing does not interfere with each other!
12054  *
12055  * This operation may remove port failed state and will
12056  * try to make port active and in good standing.
12057  *
12058  * NOTE: Port multiplier code is not completed nor tested.
12059  */
12060 
12061 static int
12062 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
12063     sata_device_t *sata_device)
12064 {
12065 	int cport, pmport, qual;
12066 	int rv = 0;
12067 
12068 	cport = sata_device->satadev_addr.cport;
12069 	pmport = sata_device->satadev_addr.pmport;
12070 	qual = sata_device->satadev_addr.qual;
12071 
12072 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
12073 
12074 	/*
12075 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
12076 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
12077 	 * Perform sanity check now.
12078 	 */
12079 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
12080 		/* No physical port activation supported. */
12081 		return (EINVAL);
12082 	}
12083 
12084 	/* Just ask HBA driver to activate port */
12085 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12086 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12087 		/*
12088 		 * Port activation failure.
12089 		 */
12090 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12091 		    cport)->cport_mutex);
12092 		sata_update_port_info(sata_hba_inst, sata_device);
12093 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12094 			if (qual == SATA_ADDR_DCPORT) {
12095 				SATA_CPORT_STATE(sata_hba_inst, cport) =
12096 				    SATA_PSTATE_FAILED;
12097 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
12098 				    "sata_hba_ioctl: connect: failed to "
12099 				    "activate SATA port %d", cport);
12100 			} else { /* port multiplier device port */
12101 				SATA_PMPORT_STATE(sata_hba_inst, cport,
12102 				    pmport) = SATA_PSTATE_FAILED;
12103 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
12104 				    "sata_hba_ioctl: connect: failed to "
12105 				    "activate SATA port %d:%d", cport, pmport);
12106 
12107 			}
12108 		}
12109 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12110 		    cport)->cport_mutex);
12111 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
12112 		    "sata_hba_ioctl: connect: failed to activate SATA "
12113 		    "port %d:%d", cport, pmport);
12114 		return (EIO);
12115 	}
12116 
12117 	/* Virgin port state - will be updated by the port re-probe. */
12118 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12119 	if (qual == SATA_ADDR_CPORT)
12120 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
12121 	else /* port multiplier device port */
12122 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
12123 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12124 
12125 	/*
12126 	 * Probe the port to find its state and attached device.
12127 	 */
12128 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12129 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
12130 		rv = EIO;
12131 
12132 	/*
12133 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12134 	 * without the hint
12135 	 */
12136 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12137 	    SE_NO_HINT);
12138 
12139 	/*
12140 	 * If there is a device attached to the port, emit
12141 	 * a message.
12142 	 */
12143 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
12144 
12145 		if (qual == SATA_ADDR_CPORT) {
12146 			sata_log(sata_hba_inst, CE_WARN,
12147 			    "SATA device detected at port %d", cport);
12148 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12149 				/*
12150 				 * A device was not successfully identified
12151 				 */
12152 				sata_log(sata_hba_inst, CE_WARN,
12153 				    "Could not identify SATA "
12154 				    "device at port %d", cport);
12155 			}
12156 		} else { /* port multiplier device port */
12157 			sata_log(sata_hba_inst, CE_WARN,
12158 			    "SATA device detected at port %d:%d",
12159 			    cport, pmport);
12160 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12161 				/*
12162 				 * A device was not successfully identified
12163 				 */
12164 				sata_log(sata_hba_inst, CE_WARN,
12165 				    "Could not identify SATA "
12166 				    "device at port %d:%d", cport, pmport);
12167 			}
12168 		}
12169 	}
12170 
12171 	return (rv);
12172 }
12173 
12174 
12175 /*
12176  * Process sata device unconfigure request.
12177  * The unconfigure operation uses generic nexus operation to
12178  * offline a device. It leaves a target device node attached.
12179  * and obviously sata_drive_info attached as well, because
12180  * from the hardware point of view nothing has changed.
12181  */
12182 static int
12183 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
12184     sata_device_t *sata_device)
12185 {
12186 	int rv = 0;
12187 	dev_info_t *tdip;
12188 
12189 	/* We are addressing attached device, not a port */
12190 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12191 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12192 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
12193 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12194 
12195 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12196 	    &sata_device->satadev_addr)) != NULL) {
12197 
12198 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
12199 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12200 			    "sata_hba_ioctl: unconfigure: "
12201 			    "failed to unconfigure device at SATA port %d:%d",
12202 			    sata_device->satadev_addr.cport,
12203 			    sata_device->satadev_addr.pmport));
12204 			rv = EIO;
12205 		}
12206 		/*
12207 		 * The target node devi_state should be marked with
12208 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
12209 		 * This would be the indication for cfgadm that
12210 		 * the AP node occupant state is 'unconfigured'.
12211 		 */
12212 
12213 	} else {
12214 		/*
12215 		 * This would indicate a failure on the part of cfgadm
12216 		 * to detect correct state of the node prior to this
12217 		 * call - one cannot unconfigure non-existing device.
12218 		 */
12219 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12220 		    "sata_hba_ioctl: unconfigure: "
12221 		    "attempt to unconfigure non-existing device "
12222 		    "at SATA port %d:%d",
12223 		    sata_device->satadev_addr.cport,
12224 		    sata_device->satadev_addr.pmport));
12225 		rv = ENXIO;
12226 	}
12227 	return (rv);
12228 }
12229 
12230 /*
12231  * Process sata device configure request
12232  * If port is in a failed state, operation is aborted - one has to use
12233  * an explicit connect or port activate request to try to get a port into
12234  * non-failed mode. Port reset wil also work in such situation.
12235  * If the port is in disconnected (shutdown) state, the connect operation is
12236  * attempted prior to any other action.
12237  * When port is in the active state, there is a device attached and the target
12238  * node exists, a device was most likely offlined.
12239  * If target node does not exist, a new target node is created. In both cases
12240  * an attempt is made to online (configure) the device.
12241  *
12242  * NOTE: Port multiplier code is not completed nor tested.
12243  */
12244 static int
12245 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
12246     sata_device_t *sata_device)
12247 {
12248 	int cport, pmport, qual;
12249 	int rval;
12250 	boolean_t target = TRUE;
12251 	sata_cport_info_t *cportinfo;
12252 	sata_pmport_info_t *pmportinfo = NULL;
12253 	dev_info_t *tdip;
12254 	sata_drive_info_t *sdinfo;
12255 
12256 	cport = sata_device->satadev_addr.cport;
12257 	pmport = sata_device->satadev_addr.pmport;
12258 	qual = sata_device->satadev_addr.qual;
12259 
12260 	/* Get current port state */
12261 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12262 	    (SATA_DIP(sata_hba_inst), sata_device);
12263 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12264 	sata_update_port_info(sata_hba_inst, sata_device);
12265 
12266 	if (rval != SATA_SUCCESS ||
12267 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
12268 		/*
12269 		 * Obviously, device on a failed port is not visible
12270 		 */
12271 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12272 		return (ENXIO);
12273 	}
12274 
12275 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12276 	if (qual == SATA_ADDR_PMPORT)
12277 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12278 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12279 
12280 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
12281 		/* need to activate port */
12282 		target = FALSE;
12283 
12284 		/* Sanity check */
12285 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
12286 			return (ENXIO);
12287 
12288 		/* Just let HBA driver to activate port */
12289 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12290 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12291 			/*
12292 			 * Port activation failure - do not change port state
12293 			 * unless the state returned by HBA indicates a port
12294 			 * failure.
12295 			 */
12296 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12297 			    cport)->cport_mutex);
12298 			sata_update_port_info(sata_hba_inst, sata_device);
12299 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12300 				if (qual == SATA_ADDR_PMPORT)
12301 					pmportinfo->pmport_state =
12302 					    SATA_PSTATE_FAILED;
12303 				else
12304 					cportinfo->cport_state =
12305 					    SATA_PSTATE_FAILED;
12306 			}
12307 			mutex_exit(&SATA_CPORT_INFO(
12308 			    sata_hba_inst, cport)->cport_mutex);
12309 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12310 			    "sata_hba_ioctl: configure: "
12311 			    "failed to activate SATA port %d:%d",
12312 			    cport, pmport));
12313 			return (EIO);
12314 		}
12315 		/*
12316 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12317 		 * without the hint.
12318 		 */
12319 		sata_gen_sysevent(sata_hba_inst,
12320 		    &sata_device->satadev_addr, SE_NO_HINT);
12321 
12322 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12323 		    cport_mutex);
12324 		/* Virgin port state */
12325 		if (qual == SATA_ADDR_PMPORT)
12326 			pmportinfo->pmport_state = 0;
12327 		else
12328 			cportinfo->cport_state = 0;
12329 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12330 	}
12331 	/*
12332 	 * Always reprobe port, to get current device info.
12333 	 */
12334 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12335 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12336 		return (EIO);
12337 
12338 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
12339 		if (qual == SATA_ADDR_PMPORT) {
12340 			/*
12341 			 * That's the transition from "inactive" port
12342 			 * to active one with device attached.
12343 			 */
12344 			sata_log(sata_hba_inst, CE_WARN,
12345 			    "SATA device detected at port %d:%d",
12346 			    cport, pmport);
12347 		} else {
12348 			/*
12349 			 * When PM is attached to the cport and cport is
12350 			 * activated, every PM device port needs to be reprobed.
12351 			 * We need to emit message for all devices detected
12352 			 * at port multiplier's device ports.
12353 			 * Add such code here.
12354 			 * For now, just inform about device attached to
12355 			 * cport.
12356 			 */
12357 			sata_log(sata_hba_inst, CE_WARN,
12358 			    "SATA device detected at port %d", cport);
12359 		}
12360 	}
12361 
12362 	/*
12363 	 * This is where real configuration operation starts.
12364 	 *
12365 	 * When PM is attached to the cport and cport is activated,
12366 	 * devices attached PM device ports may have to be configured
12367 	 * explicitly. This may change when port multiplier is supported.
12368 	 * For now, configure only disks and other valid target devices.
12369 	 */
12370 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
12371 		if (qual == SATA_ADDR_CPORT) {
12372 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12373 				/*
12374 				 * A device was not successfully identified
12375 				 */
12376 				sata_log(sata_hba_inst, CE_WARN,
12377 				    "Could not identify SATA "
12378 				    "device at port %d", cport);
12379 			}
12380 		} else { /* port multiplier device port */
12381 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
12382 				/*
12383 				 * A device was not successfully identified
12384 				 */
12385 				sata_log(sata_hba_inst, CE_WARN,
12386 				    "Could not identify SATA "
12387 				    "device at port %d:%d", cport, pmport);
12388 			}
12389 		}
12390 		return (ENXIO);		/* No device to configure */
12391 	}
12392 
12393 	/*
12394 	 * Here we may have a device in reset condition,
12395 	 * but because we are just configuring it, there is
12396 	 * no need to process the reset other than just
12397 	 * to clear device reset condition in the HBA driver.
12398 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
12399 	 * cause a first command sent the HBA driver with the request
12400 	 * to clear device reset condition.
12401 	 */
12402 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12403 	if (qual == SATA_ADDR_PMPORT)
12404 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12405 	else
12406 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12407 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12408 	if (sdinfo == NULL) {
12409 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12410 		return (ENXIO);
12411 	}
12412 	if (sdinfo->satadrv_event_flags &
12413 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
12414 		sdinfo->satadrv_event_flags = 0;
12415 	}
12416 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
12417 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12418 
12419 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12420 	    &sata_device->satadev_addr)) != NULL) {
12421 		/*
12422 		 * Target node exists. Verify, that it belongs
12423 		 * to existing, attached device and not to
12424 		 * a removed device.
12425 		 */
12426 		if (sata_check_device_removed(tdip) == B_TRUE) {
12427 			if (qual == SATA_ADDR_DPMPORT)
12428 				sata_log(sata_hba_inst, CE_WARN,
12429 				    "SATA device at port %d cannot be "
12430 				    "configured. "
12431 				    "Application(s) accessing "
12432 				    "previously attached device "
12433 				    "have to release it before newly "
12434 				    "inserted device can be made accessible.",
12435 				    cport);
12436 			else
12437 				sata_log(sata_hba_inst, CE_WARN,
12438 				    "SATA device at port %d:%d cannot be"
12439 				    "configured. "
12440 				    "Application(s) accessing "
12441 				    "previously attached device "
12442 				    "have to release it before newly "
12443 				    "inserted device can be made accessible.",
12444 				    cport, pmport);
12445 			return (EIO);
12446 		}
12447 		/*
12448 		 * Device was not removed and re-inserted.
12449 		 * Try to online it.
12450 		 */
12451 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
12452 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12453 			    "sata_hba_ioctl: configure: "
12454 			    "onlining device at SATA port "
12455 			    "%d:%d failed", cport, pmport));
12456 			return (EIO);
12457 		}
12458 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12459 		    cport)->cport_mutex);
12460 
12461 		if (qual == SATA_ADDR_DPMPORT)
12462 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
12463 		else
12464 			cportinfo-> cport_tgtnode_clean = B_TRUE;
12465 
12466 		mutex_exit(&SATA_CPORT_INFO(
12467 		    sata_hba_inst, cport)->cport_mutex);
12468 	} else {
12469 		/*
12470 		 * No target node - need to create a new target node.
12471 		 */
12472 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12473 		    cport_mutex);
12474 		if (qual == SATA_ADDR_DPMPORT)
12475 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
12476 		else
12477 			cportinfo-> cport_tgtnode_clean = B_TRUE;
12478 
12479 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12480 		    cport_mutex);
12481 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
12482 		    sata_hba_inst, &sata_device->satadev_addr);
12483 		if (tdip == NULL) {
12484 			/* Configure operation failed */
12485 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12486 			    "sata_hba_ioctl: configure: "
12487 			    "configuring SATA device at port %d:%d "
12488 			    "failed", cport, pmport));
12489 			return (EIO);
12490 		}
12491 	}
12492 	return (0);
12493 }
12494 
12495 
12496 /*
12497  * Process ioctl deactivate port request.
12498  * Arbitrarily unconfigure attached device, if any.
12499  * Even if the unconfigure fails, proceed with the
12500  * port deactivation.
12501  *
12502  * NOTE: Port Multiplier code is not completed and tested.
12503  */
12504 
12505 static int
12506 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
12507     sata_device_t *sata_device)
12508 {
12509 	int cport, pmport, qual;
12510 	int rval, rv = 0;
12511 	sata_cport_info_t *cportinfo;
12512 	sata_pmport_info_t *pmportinfo = NULL;
12513 	dev_info_t *tdip;
12514 	sata_drive_info_t *sdinfo = NULL;
12515 
12516 	/* Sanity check */
12517 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
12518 		return (ENOTSUP);
12519 
12520 	cport = sata_device->satadev_addr.cport;
12521 	pmport = sata_device->satadev_addr.pmport;
12522 	qual = sata_device->satadev_addr.qual;
12523 
12524 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12525 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12526 	if (qual == SATA_ADDR_CPORT) {
12527 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12528 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
12529 			/*
12530 			 * For now, assume that port multiplier is not
12531 			 * supported, i.e. deal only with valid devices
12532 			 */
12533 			if ((cportinfo->cport_dev_type &
12534 			    SATA_VALID_DEV_TYPE) != 0)
12535 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12536 			/*
12537 			 * If attached device is a port multiplier, we will
12538 			 * have to unconfigure all devices attached to the
12539 			 * port multiplier. Add this code here.
12540 			 */
12541 		}
12542 		cportinfo->cport_state &= ~SATA_STATE_READY;
12543 	} else {
12544 		/* Port multiplier device port */
12545 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12546 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12547 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
12548 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
12549 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
12550 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
12551 	}
12552 
12553 	if (sdinfo != NULL) {
12554 		/*
12555 		 * If a target node exists, try to offline a device and
12556 		 * to remove a target node.
12557 		 */
12558 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12559 		    cport_mutex);
12560 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12561 		    &sata_device->satadev_addr);
12562 		if (tdip != NULL) {
12563 			/* target node exist */
12564 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
12565 			    "sata_hba_ioctl: port deactivate: "
12566 			    "target node exists.", NULL);
12567 
12568 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
12569 			    NDI_SUCCESS) {
12570 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12571 				    "sata_hba_ioctl: port deactivate: "
12572 				    "failed to unconfigure device at port "
12573 				    "%d:%d before deactivating the port",
12574 				    cport, pmport));
12575 				/*
12576 				 * Set DEVICE REMOVED state in the target
12577 				 * node. It will prevent an access to
12578 				 * the device even when a new device is
12579 				 * attached, until the old target node is
12580 				 * released, removed and recreated for a new
12581 				 * device.
12582 				 */
12583 				sata_set_device_removed(tdip);
12584 
12585 				/*
12586 				 * Instruct the event daemon to try the
12587 				 * target node cleanup later.
12588 				 */
12589 				sata_set_target_node_cleanup(sata_hba_inst,
12590 				    &sata_device->satadev_addr);
12591 			}
12592 		}
12593 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12594 		    cport_mutex);
12595 		/*
12596 		 * In any case, remove and release sata_drive_info
12597 		 * structure.
12598 		 */
12599 		if (qual == SATA_ADDR_CPORT) {
12600 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
12601 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12602 		} else { /* port multiplier device port */
12603 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12604 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
12605 		}
12606 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
12607 	}
12608 	if (qual == SATA_ADDR_CPORT) {
12609 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
12610 		    SATA_STATE_PROBING);
12611 	} else { /* port multiplier device port */
12612 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
12613 		    SATA_STATE_PROBING);
12614 	}
12615 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12616 
12617 	/* Just let HBA driver to deactivate port */
12618 	sata_device->satadev_addr.qual = qual;
12619 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
12620 	    (SATA_DIP(sata_hba_inst), sata_device);
12621 
12622 	/*
12623 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12624 	 * without the hint
12625 	 */
12626 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12627 	    SE_NO_HINT);
12628 
12629 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12630 	sata_update_port_info(sata_hba_inst, sata_device);
12631 	if (qual == SATA_ADDR_CPORT) {
12632 		if (rval != SATA_SUCCESS) {
12633 			/*
12634 			 * Port deactivation failure - do not change port state
12635 			 * unless the state returned by HBA indicates a port
12636 			 * failure.
12637 			 */
12638 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12639 				SATA_CPORT_STATE(sata_hba_inst, cport) =
12640 				    SATA_PSTATE_FAILED;
12641 			}
12642 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12643 			    "sata_hba_ioctl: port deactivate: "
12644 			    "cannot deactivate SATA port %d", cport));
12645 			rv = EIO;
12646 		} else {
12647 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
12648 		}
12649 	} else {
12650 		if (rval != SATA_SUCCESS) {
12651 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12652 				SATA_PMPORT_STATE(sata_hba_inst, cport,
12653 				    pmport) = SATA_PSTATE_FAILED;
12654 			}
12655 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12656 			    "sata_hba_ioctl: port deactivate: "
12657 			    "cannot deactivate SATA port %d:%d",
12658 			    cport, pmport));
12659 			rv = EIO;
12660 		} else {
12661 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
12662 		}
12663 	}
12664 
12665 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12666 
12667 	return (rv);
12668 }
12669 
12670 /*
12671  * Process ioctl port activate request.
12672  *
12673  * NOTE: Port multiplier code is not completed nor tested.
12674  */
12675 static int
12676 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
12677     sata_device_t *sata_device)
12678 {
12679 	int cport, pmport, qual;
12680 	sata_cport_info_t *cportinfo;
12681 	sata_pmport_info_t *pmportinfo = NULL;
12682 	boolean_t dev_existed = TRUE;
12683 
12684 	/* Sanity check */
12685 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
12686 		return (ENOTSUP);
12687 
12688 	cport = sata_device->satadev_addr.cport;
12689 	pmport = sata_device->satadev_addr.pmport;
12690 	qual = sata_device->satadev_addr.qual;
12691 
12692 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12693 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12694 	if (qual == SATA_ADDR_PMPORT) {
12695 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
12696 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
12697 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
12698 			dev_existed = FALSE;
12699 	} else { /* cport */
12700 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
12701 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12702 			dev_existed = FALSE;
12703 	}
12704 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12705 
12706 	/* Just let HBA driver to activate port, if necessary */
12707 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
12708 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12709 		/*
12710 		 * Port activation failure - do not change port state unless
12711 		 * the state returned by HBA indicates a port failure.
12712 		 */
12713 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12714 		    cport)->cport_mutex);
12715 		sata_update_port_info(sata_hba_inst, sata_device);
12716 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
12717 			if (qual == SATA_ADDR_PMPORT)
12718 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
12719 			else
12720 				cportinfo->cport_state = SATA_PSTATE_FAILED;
12721 
12722 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12723 			    cport)->cport_mutex);
12724 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12725 			    "sata_hba_ioctl: port activate: cannot activate "
12726 			    "SATA port %d:%d", cport, pmport));
12727 			return (EIO);
12728 		}
12729 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12730 	}
12731 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12732 	if (qual == SATA_ADDR_PMPORT)
12733 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
12734 	else
12735 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
12736 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12737 
12738 	/*
12739 	 * Re-probe port to find its current state and possibly attached device.
12740 	 * Port re-probing may change the cportinfo device type if device is
12741 	 * found attached.
12742 	 * If port probing failed, the device type would be set to
12743 	 * SATA_DTYPE_NONE.
12744 	 */
12745 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
12746 	    SATA_DEV_IDENTIFY_RETRY);
12747 
12748 	/*
12749 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
12750 	 * without the hint.
12751 	 */
12752 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
12753 	    SE_NO_HINT);
12754 
12755 	if (dev_existed == FALSE) {
12756 		if (qual == SATA_ADDR_PMPORT &&
12757 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
12758 			/*
12759 			 * That's the transition from the "inactive" port state
12760 			 * or the active port without a device attached to the
12761 			 * active port state with a device attached.
12762 			 */
12763 			sata_log(sata_hba_inst, CE_WARN,
12764 			    "SATA device detected at port %d:%d",
12765 			    cport, pmport);
12766 		} else if (qual == SATA_ADDR_CPORT &&
12767 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
12768 			/*
12769 			 * That's the transition from the "inactive" port state
12770 			 * or the active port without a device attached to the
12771 			 * active port state with a device attached.
12772 			 */
12773 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
12774 				sata_log(sata_hba_inst, CE_WARN,
12775 				    "SATA device detected at port %d", cport);
12776 			} else {
12777 				sata_log(sata_hba_inst, CE_WARN,
12778 				    "SATA port multiplier detected at port %d",
12779 				    cport);
12780 				/*
12781 				 * Because the detected device is a port
12782 				 * multiplier, we need to reprobe every device
12783 				 * port on the port multiplier and show every
12784 				 * device found attached.
12785 				 * Add this code here.
12786 				 */
12787 			}
12788 		}
12789 	}
12790 	return (0);
12791 }
12792 
12793 
12794 
12795 /*
12796  * Process ioctl reset port request.
12797  *
12798  * NOTE: Port multiplier code is not completed nor tested.
12799  */
12800 static int
12801 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
12802     sata_device_t *sata_device)
12803 {
12804 	int cport, pmport, qual;
12805 	int rv = 0;
12806 
12807 	cport = sata_device->satadev_addr.cport;
12808 	pmport = sata_device->satadev_addr.pmport;
12809 	qual = sata_device->satadev_addr.qual;
12810 
12811 	/* Sanity check */
12812 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12813 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12814 		    "sata_hba_ioctl: sata_hba_tran missing required "
12815 		    "function sata_tran_reset_dport"));
12816 		return (ENOTSUP);
12817 	}
12818 
12819 	/* Ask HBA to reset port */
12820 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12821 	    sata_device) != SATA_SUCCESS) {
12822 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12823 		    "sata_hba_ioctl: reset port: failed %d:%d",
12824 		    cport, pmport));
12825 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12826 		    cport_mutex);
12827 		sata_update_port_info(sata_hba_inst, sata_device);
12828 		if (qual == SATA_ADDR_CPORT)
12829 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12830 			    SATA_PSTATE_FAILED;
12831 		else
12832 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12833 			    SATA_PSTATE_FAILED;
12834 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12835 		    cport_mutex);
12836 		rv = EIO;
12837 	}
12838 	/*
12839 	 * Beacuse the port was reset, it should be probed and
12840 	 * attached device reinitialized. At this point the
12841 	 * port state is unknown - it's state is HBA-specific.
12842 	 * Re-probe port to get its state.
12843 	 */
12844 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12845 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
12846 		rv = EIO;
12847 	}
12848 	return (rv);
12849 }
12850 
12851 /*
12852  * Process ioctl reset device request.
12853  *
12854  * NOTE: Port multiplier code is not completed nor tested.
12855  */
12856 static int
12857 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
12858     sata_device_t *sata_device)
12859 {
12860 	sata_drive_info_t *sdinfo;
12861 	int cport, pmport;
12862 	int rv = 0;
12863 
12864 	/* Sanity check */
12865 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12866 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12867 		    "sata_hba_ioctl: sata_hba_tran missing required "
12868 		    "function sata_tran_reset_dport"));
12869 		return (ENOTSUP);
12870 	}
12871 
12872 	cport = sata_device->satadev_addr.cport;
12873 	pmport = sata_device->satadev_addr.pmport;
12874 
12875 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12876 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
12877 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12878 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12879 		    sata_device->satadev_addr.cport);
12880 	} else { /* port multiplier */
12881 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12882 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12883 		    sata_device->satadev_addr.cport,
12884 		    sata_device->satadev_addr.pmport);
12885 	}
12886 	if (sdinfo == NULL) {
12887 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12888 		return (EINVAL);
12889 	}
12890 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12891 
12892 	/* Ask HBA to reset device */
12893 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12894 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12895 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12896 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
12897 		    cport, pmport));
12898 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12899 		    cport_mutex);
12900 		sata_update_port_info(sata_hba_inst, sata_device);
12901 		/*
12902 		 * Device info structure remains attached. Another device reset
12903 		 * or port disconnect/connect and re-probing is
12904 		 * needed to change it's state
12905 		 */
12906 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
12907 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
12908 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12909 		rv = EIO;
12910 	}
12911 	/*
12912 	 * If attached device was a port multiplier, some extra processing
12913 	 * may be needed, to bring it back (if port re-probing did not handle
12914 	 * it). Add such code here.
12915 	 */
12916 	return (rv);
12917 }
12918 
12919 
12920 /*
12921  * Process ioctl reset all request.
12922  *
12923  * NOTE: Port multiplier code is not completed nor tested.
12924  */
12925 static int
12926 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
12927 {
12928 	sata_device_t sata_device;
12929 	int rv = 0;
12930 	int tcport;
12931 	int tpmport = 0;
12932 
12933 	sata_device.satadev_rev = SATA_DEVICE_REV;
12934 
12935 	/*
12936 	 * There is no protection here for configured devices.
12937 	 */
12938 	/* Sanity check */
12939 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12940 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12941 		    "sata_hba_ioctl: sata_hba_tran missing required "
12942 		    "function sata_tran_reset_dport"));
12943 		return (ENOTSUP);
12944 	}
12945 
12946 	/*
12947 	 * Need to lock all ports, not just one.
12948 	 * If any port is locked by event processing, fail the whole operation.
12949 	 * One port is already locked, but for simplicity lock it again.
12950 	 */
12951 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12952 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12953 		    cport_mutex);
12954 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
12955 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
12956 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12957 			    cport_mutex);
12958 			rv = EBUSY;
12959 			break;
12960 		} else {
12961 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
12962 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
12963 			/*
12964 			 * If there is a port multiplier attached, we may need
12965 			 * to lock its port as well. If so, add such code here.
12966 			 */
12967 		}
12968 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12969 		    cport_mutex);
12970 	}
12971 
12972 	if (rv == 0) {
12973 		/*
12974 		 * All cports were successfully locked.
12975 		 * Reset main SATA controller only for now - no PMult.
12976 		 * Set the device address to port 0, to have a valid device
12977 		 * address.
12978 		 */
12979 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
12980 		sata_device.satadev_addr.cport = 0;
12981 		sata_device.satadev_addr.pmport = 0;
12982 
12983 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12984 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
12985 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12986 			    "sata_hba_ioctl: reset controller failed"));
12987 			return (EIO);
12988 		}
12989 		/*
12990 		 * Because ports were reset, port states are unknown.
12991 		 * They should be re-probed to get their state and
12992 		 * attached devices should be reinitialized.
12993 		 * Add code here to re-probe port multiplier device ports.
12994 		 */
12995 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
12996 		    tcport++) {
12997 			sata_device.satadev_addr.cport = tcport;
12998 			sata_device.satadev_addr.pmport = tpmport;
12999 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
13000 
13001 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
13002 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13003 				rv = EIO;
13004 		}
13005 	}
13006 	/*
13007 	 * Unlock all ports
13008 	 */
13009 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
13010 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
13011 		    cport_mutex);
13012 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
13013 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
13014 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
13015 		    cport_mutex);
13016 	}
13017 
13018 	/*
13019 	 * This operation returns EFAULT if either reset
13020 	 * controller failed or a re-probing of any port failed.
13021 	 */
13022 	return (rv);
13023 }
13024 
13025 
13026 /*
13027  * Process ioctl port self test request.
13028  *
13029  * NOTE: Port multiplier code is not completed nor tested.
13030  */
13031 static int
13032 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
13033     sata_device_t *sata_device)
13034 {
13035 	int cport, pmport, qual;
13036 	int rv = 0;
13037 
13038 	/* Sanity check */
13039 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
13040 		return (ENOTSUP);
13041 
13042 	cport = sata_device->satadev_addr.cport;
13043 	pmport = sata_device->satadev_addr.pmport;
13044 	qual = sata_device->satadev_addr.qual;
13045 
13046 	/*
13047 	 * There is no protection here for a configured
13048 	 * device attached to this port.
13049 	 */
13050 
13051 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
13052 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13053 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13054 		    "sata_hba_ioctl: port selftest: "
13055 		    "failed port %d:%d", cport, pmport));
13056 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13057 		    cport_mutex);
13058 		sata_update_port_info(sata_hba_inst, sata_device);
13059 		if (qual == SATA_ADDR_CPORT)
13060 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13061 			    SATA_PSTATE_FAILED;
13062 		else /* port ultiplier device port */
13063 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
13064 			    SATA_PSTATE_FAILED;
13065 
13066 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13067 		    cport_mutex);
13068 		return (EIO);
13069 	}
13070 	/*
13071 	 * Beacuse the port was reset in the course of testing, it should be
13072 	 * re-probed and attached device state should be restored. At this
13073 	 * point the port state is unknown - it's state is HBA-specific.
13074 	 * Force port re-probing to get it into a known state.
13075 	 */
13076 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13077 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13078 		rv = EIO;
13079 	return (rv);
13080 }
13081 
13082 
13083 /*
13084  * sata_cfgadm_state:
13085  * Use the sata port state and state of the target node to figure out
13086  * the cfgadm_state.
13087  *
13088  * The port argument is a value with encoded cport,
13089  * pmport and address qualifier, in the same manner as a scsi target number.
13090  * SCSI_TO_SATA_CPORT macro extracts cport number,
13091  * SCSI_TO_SATA_PMPORT extracts pmport number and
13092  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
13093  *
13094  * For now, support is for cports only - no port multiplier device ports.
13095  */
13096 
13097 static void
13098 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
13099     devctl_ap_state_t *ap_state)
13100 {
13101 	uint16_t	cport;
13102 	int		port_state;
13103 	sata_drive_info_t *sdinfo;
13104 
13105 	/* Cport only */
13106 	cport = SCSI_TO_SATA_CPORT(port);
13107 
13108 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
13109 	if (port_state & SATA_PSTATE_SHUTDOWN ||
13110 	    port_state & SATA_PSTATE_FAILED) {
13111 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
13112 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13113 		if (port_state & SATA_PSTATE_FAILED)
13114 			ap_state->ap_condition = AP_COND_FAILED;
13115 		else
13116 			ap_state->ap_condition = AP_COND_UNKNOWN;
13117 
13118 		return;
13119 	}
13120 
13121 	/* Need to check pmult device port here as well, when supported */
13122 
13123 	/* Port is enabled and ready */
13124 
13125 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
13126 	case SATA_DTYPE_NONE:
13127 	{
13128 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13129 		ap_state->ap_condition = AP_COND_OK;
13130 		/* No device attached */
13131 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
13132 		break;
13133 	}
13134 	case SATA_DTYPE_UNKNOWN:
13135 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
13136 	case SATA_DTYPE_ATADISK:
13137 	case SATA_DTYPE_ATAPICD:
13138 	case SATA_DTYPE_ATAPITAPE:
13139 	case SATA_DTYPE_ATAPIDISK:
13140 	{
13141 		dev_info_t *tdip = NULL;
13142 		dev_info_t *dip = NULL;
13143 		int circ;
13144 
13145 		dip = SATA_DIP(sata_hba_inst);
13146 		tdip = sata_get_target_dip(dip, port);
13147 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
13148 		if (tdip != NULL) {
13149 			ndi_devi_enter(dip, &circ);
13150 			mutex_enter(&(DEVI(tdip)->devi_lock));
13151 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
13152 				/*
13153 				 * There could be the case where previously
13154 				 * configured and opened device was removed
13155 				 * and unknown device was plugged.
13156 				 * In such case we want to show a device, and
13157 				 * its configured or unconfigured state but
13158 				 * indicate unusable condition untill the
13159 				 * old target node is released and removed.
13160 				 */
13161 				ap_state->ap_condition = AP_COND_UNUSABLE;
13162 			} else {
13163 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
13164 				    cport));
13165 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13166 				    cport);
13167 				if (sdinfo != NULL) {
13168 					if ((sdinfo->satadrv_state &
13169 					    SATA_DSTATE_FAILED) != 0)
13170 						ap_state->ap_condition =
13171 						    AP_COND_FAILED;
13172 					else
13173 						ap_state->ap_condition =
13174 						    AP_COND_OK;
13175 				} else {
13176 					ap_state->ap_condition =
13177 					    AP_COND_UNKNOWN;
13178 				}
13179 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
13180 				    cport));
13181 			}
13182 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
13183 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
13184 				ap_state->ap_ostate =
13185 				    AP_OSTATE_UNCONFIGURED;
13186 			} else {
13187 				ap_state->ap_ostate =
13188 				    AP_OSTATE_CONFIGURED;
13189 			}
13190 			mutex_exit(&(DEVI(tdip)->devi_lock));
13191 			ndi_devi_exit(dip, circ);
13192 		} else {
13193 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13194 			ap_state->ap_condition = AP_COND_UNKNOWN;
13195 		}
13196 		break;
13197 	}
13198 	default:
13199 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
13200 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
13201 		ap_state->ap_condition = AP_COND_UNKNOWN;
13202 		/*
13203 		 * This is actually internal error condition (non fatal),
13204 		 * because we have already checked all defined device types.
13205 		 */
13206 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13207 		    "sata_cfgadm_state: Internal error: "
13208 		    "unknown device type"));
13209 		break;
13210 	}
13211 }
13212 
13213 
13214 /*
13215  * Process ioctl get device path request.
13216  *
13217  * NOTE: Port multiplier code is not completed nor tested.
13218  */
13219 static int
13220 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
13221     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13222 {
13223 	char path[MAXPATHLEN];
13224 	uint32_t size;
13225 	dev_info_t *tdip;
13226 
13227 	(void) strcpy(path, "/devices");
13228 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13229 	    &sata_device->satadev_addr)) == NULL) {
13230 		/*
13231 		 * No such device. If this is a request for a size, do not
13232 		 * return EINVAL for non-existing target, because cfgadm
13233 		 * will then indicate a meaningless ioctl failure.
13234 		 * If this is a request for a path, indicate invalid
13235 		 * argument.
13236 		 */
13237 		if (ioc->get_size == 0)
13238 			return (EINVAL);
13239 	} else {
13240 		(void) ddi_pathname(tdip, path + strlen(path));
13241 	}
13242 	size = strlen(path) + 1;
13243 
13244 	if (ioc->get_size != 0) {
13245 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
13246 		    mode) != 0)
13247 			return (EFAULT);
13248 	} else {
13249 		if (ioc->bufsiz != size)
13250 			return (EINVAL);
13251 
13252 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
13253 		    mode) != 0)
13254 			return (EFAULT);
13255 	}
13256 	return (0);
13257 }
13258 
13259 /*
13260  * Process ioctl get attachment point type request.
13261  *
13262  * NOTE: Port multiplier code is not completed nor tested.
13263  */
13264 static	int
13265 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
13266     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13267 {
13268 	uint32_t	type_len;
13269 	const char	*ap_type;
13270 	int		dev_type;
13271 
13272 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13273 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
13274 		    sata_device->satadev_addr.cport);
13275 	else /* pmport */
13276 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
13277 		    sata_device->satadev_addr.cport,
13278 		    sata_device->satadev_addr.pmport);
13279 
13280 	switch (dev_type) {
13281 	case SATA_DTYPE_NONE:
13282 		ap_type = "port";
13283 		break;
13284 
13285 	case SATA_DTYPE_ATADISK:
13286 	case SATA_DTYPE_ATAPIDISK:
13287 		ap_type = "disk";
13288 		break;
13289 
13290 	case SATA_DTYPE_ATAPICD:
13291 		ap_type = "cd/dvd";
13292 		break;
13293 
13294 	case SATA_DTYPE_ATAPITAPE:
13295 		ap_type = "tape";
13296 		break;
13297 
13298 	case SATA_DTYPE_PMULT:
13299 		ap_type = "pmult";
13300 		break;
13301 
13302 	case SATA_DTYPE_UNKNOWN:
13303 		ap_type = "unknown";
13304 		break;
13305 
13306 	default:
13307 		ap_type = "unsupported";
13308 		break;
13309 
13310 	} /* end of dev_type switch */
13311 
13312 	type_len = strlen(ap_type) + 1;
13313 
13314 	if (ioc->get_size) {
13315 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
13316 		    mode) != 0)
13317 			return (EFAULT);
13318 	} else {
13319 		if (ioc->bufsiz != type_len)
13320 			return (EINVAL);
13321 
13322 		if (ddi_copyout((void *)ap_type, ioc->buf,
13323 		    ioc->bufsiz, mode) != 0)
13324 			return (EFAULT);
13325 	}
13326 	return (0);
13327 
13328 }
13329 
13330 /*
13331  * Process ioctl get device model info request.
13332  * This operation should return to cfgadm the device model
13333  * information string
13334  *
13335  * NOTE: Port multiplier code is not completed nor tested.
13336  */
13337 static	int
13338 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
13339     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13340 {
13341 	sata_drive_info_t *sdinfo;
13342 	uint32_t info_len;
13343 	char ap_info[SATA_ID_MODEL_LEN + 1];
13344 
13345 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13346 	    sata_device->satadev_addr.cport)->cport_mutex);
13347 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13348 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13349 		    sata_device->satadev_addr.cport);
13350 	else /* port multiplier */
13351 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13352 		    sata_device->satadev_addr.cport,
13353 		    sata_device->satadev_addr.pmport);
13354 	if (sdinfo == NULL) {
13355 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13356 		    sata_device->satadev_addr.cport)->cport_mutex);
13357 		return (EINVAL);
13358 	}
13359 
13360 #ifdef	_LITTLE_ENDIAN
13361 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
13362 #else	/* _LITTLE_ENDIAN */
13363 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
13364 #endif	/* _LITTLE_ENDIAN */
13365 
13366 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13367 	    sata_device->satadev_addr.cport)->cport_mutex);
13368 
13369 	ap_info[SATA_ID_MODEL_LEN] = '\0';
13370 
13371 	info_len = strlen(ap_info) + 1;
13372 
13373 	if (ioc->get_size) {
13374 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13375 		    mode) != 0)
13376 			return (EFAULT);
13377 	} else {
13378 		if (ioc->bufsiz < info_len)
13379 			return (EINVAL);
13380 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13381 		    mode) != 0)
13382 			return (EFAULT);
13383 	}
13384 	return (0);
13385 }
13386 
13387 
13388 /*
13389  * Process ioctl get device firmware revision info request.
13390  * This operation should return to cfgadm the device firmware revision
13391  * information string
13392  *
13393  * NOTE: Port multiplier code is not completed nor tested.
13394  */
13395 static	int
13396 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
13397     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13398 {
13399 	sata_drive_info_t *sdinfo;
13400 	uint32_t info_len;
13401 	char ap_info[SATA_ID_FW_LEN + 1];
13402 
13403 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13404 	    sata_device->satadev_addr.cport)->cport_mutex);
13405 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13406 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13407 		    sata_device->satadev_addr.cport);
13408 	else /* port multiplier */
13409 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13410 		    sata_device->satadev_addr.cport,
13411 		    sata_device->satadev_addr.pmport);
13412 	if (sdinfo == NULL) {
13413 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13414 		    sata_device->satadev_addr.cport)->cport_mutex);
13415 		return (EINVAL);
13416 	}
13417 
13418 #ifdef	_LITTLE_ENDIAN
13419 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
13420 #else	/* _LITTLE_ENDIAN */
13421 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
13422 #endif	/* _LITTLE_ENDIAN */
13423 
13424 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13425 	    sata_device->satadev_addr.cport)->cport_mutex);
13426 
13427 	ap_info[SATA_ID_FW_LEN] = '\0';
13428 
13429 	info_len = strlen(ap_info) + 1;
13430 
13431 	if (ioc->get_size) {
13432 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13433 		    mode) != 0)
13434 			return (EFAULT);
13435 	} else {
13436 		if (ioc->bufsiz < info_len)
13437 			return (EINVAL);
13438 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13439 		    mode) != 0)
13440 			return (EFAULT);
13441 	}
13442 	return (0);
13443 }
13444 
13445 
13446 /*
13447  * Process ioctl get device serial number info request.
13448  * This operation should return to cfgadm the device serial number string.
13449  *
13450  * NOTE: Port multiplier code is not completed nor tested.
13451  */
13452 static	int
13453 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
13454     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
13455 {
13456 	sata_drive_info_t *sdinfo;
13457 	uint32_t info_len;
13458 	char ap_info[SATA_ID_SERIAL_LEN + 1];
13459 
13460 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13461 	    sata_device->satadev_addr.cport)->cport_mutex);
13462 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13463 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
13464 		    sata_device->satadev_addr.cport);
13465 	else /* port multiplier */
13466 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
13467 		    sata_device->satadev_addr.cport,
13468 		    sata_device->satadev_addr.pmport);
13469 	if (sdinfo == NULL) {
13470 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13471 		    sata_device->satadev_addr.cport)->cport_mutex);
13472 		return (EINVAL);
13473 	}
13474 
13475 #ifdef	_LITTLE_ENDIAN
13476 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
13477 #else	/* _LITTLE_ENDIAN */
13478 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
13479 #endif	/* _LITTLE_ENDIAN */
13480 
13481 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13482 	    sata_device->satadev_addr.cport)->cport_mutex);
13483 
13484 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
13485 
13486 	info_len = strlen(ap_info) + 1;
13487 
13488 	if (ioc->get_size) {
13489 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
13490 		    mode) != 0)
13491 			return (EFAULT);
13492 	} else {
13493 		if (ioc->bufsiz < info_len)
13494 			return (EINVAL);
13495 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
13496 		    mode) != 0)
13497 			return (EFAULT);
13498 	}
13499 	return (0);
13500 }
13501 
13502 
13503 /*
13504  * Preset scsi extended sense data (to NO SENSE)
13505  * First 18 bytes of the sense data are preset to current valid sense
13506  * with a key NO SENSE data.
13507  *
13508  * Returns void
13509  */
13510 static void
13511 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
13512 {
13513 	sense->es_valid = 1;		/* Valid sense */
13514 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
13515 	sense->es_key = KEY_NO_SENSE;
13516 	sense->es_info_1 = 0;
13517 	sense->es_info_2 = 0;
13518 	sense->es_info_3 = 0;
13519 	sense->es_info_4 = 0;
13520 	sense->es_add_len = 10;	/* Additional length - replace with a def */
13521 	sense->es_cmd_info[0] = 0;
13522 	sense->es_cmd_info[1] = 0;
13523 	sense->es_cmd_info[2] = 0;
13524 	sense->es_cmd_info[3] = 0;
13525 	sense->es_add_code = 0;
13526 	sense->es_qual_code = 0;
13527 }
13528 
13529 /*
13530  * Register a legacy cmdk-style devid for the target (disk) device.
13531  *
13532  * Note: This function is called only when the HBA devinfo node has the
13533  * property "use-cmdk-devid-format" set. This property indicates that
13534  * devid compatible with old cmdk (target) driver is to be generated
13535  * for any target device attached to this controller. This will take
13536  * precedence over the devid generated by sd (target) driver.
13537  * This function is derived from cmdk_devid_setup() function in cmdk.c.
13538  */
13539 static void
13540 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
13541 {
13542 	char	*hwid;
13543 	int	modlen;
13544 	int	serlen;
13545 	int	rval;
13546 	ddi_devid_t	devid;
13547 
13548 	/*
13549 	 * device ID is a concatanation of model number, "=", serial number.
13550 	 */
13551 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
13552 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
13553 	    sizeof (sdinfo->satadrv_id.ai_model));
13554 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
13555 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
13556 	if (modlen == 0)
13557 		goto err;
13558 	hwid[modlen++] = '=';
13559 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
13560 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13561 	swab(&hwid[modlen], &hwid[modlen],
13562 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13563 	serlen = sata_check_modser(&hwid[modlen],
13564 	    sizeof (sdinfo->satadrv_id.ai_drvser));
13565 	if (serlen == 0)
13566 		goto err;
13567 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
13568 
13569 	/* initialize/register devid */
13570 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
13571 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
13572 		rval = ddi_devid_register(dip, devid);
13573 		/*
13574 		 * Free up the allocated devid buffer.
13575 		 * NOTE: This doesn't mean unregistering devid.
13576 		 */
13577 		ddi_devid_free(devid);
13578 	}
13579 
13580 	if (rval != DDI_SUCCESS)
13581 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
13582 		    " on port %d", sdinfo->satadrv_addr.cport);
13583 err:
13584 	kmem_free(hwid, LEGACY_HWID_LEN);
13585 }
13586 
13587 /*
13588  * valid model/serial string must contain a non-zero non-space characters.
13589  * trim trailing spaces/NULLs.
13590  */
13591 static int
13592 sata_check_modser(char *buf, int buf_len)
13593 {
13594 	boolean_t ret;
13595 	char *s;
13596 	int i;
13597 	int tb;
13598 	char ch;
13599 
13600 	ret = B_FALSE;
13601 	s = buf;
13602 	for (i = 0; i < buf_len; i++) {
13603 		ch = *s++;
13604 		if (ch != ' ' && ch != '\0')
13605 			tb = i + 1;
13606 		if (ch != ' ' && ch != '\0' && ch != '0')
13607 			ret = B_TRUE;
13608 	}
13609 
13610 	if (ret == B_FALSE)
13611 		return (0); /* invalid string */
13612 
13613 	return (tb); /* return length */
13614 }
13615 
13616 /*
13617  * sata_set_drive_features function compares current device features setting
13618  * with the saved device features settings and, if there is a difference,
13619  * it restores device features setting to the previously saved state.
13620  * It also arbitrarily tries to select the highest supported DMA mode.
13621  * Device Identify or Identify Packet Device data has to be current.
13622  * At the moment read ahead and write cache are considered for all devices.
13623  * For atapi devices, Removable Media Status Notification is set in addition
13624  * to common features.
13625  *
13626  * This function cannot be called in the interrupt context (it may sleep).
13627  *
13628  * The input argument sdinfo should point to the drive info structure
13629  * to be updated after features are set. Note, that only
13630  * device (packet) identify data is updated, not the flags indicating the
13631  * supported features.
13632  *
13633  * Returns SATA_SUCCESS if successful or there was nothing to do.
13634  * Device Identify data in the drive info structure pointed to by the sdinfo
13635  * arguments is updated even when no features were set or changed.
13636  *
13637  * Returns SATA_FAILURE if device features could not be set or DMA mode
13638  * for a disk cannot be set and device identify data cannot be fetched.
13639  *
13640  * Returns SATA_RETRY if device features could not be set (other than disk
13641  * DMA mode) but the device identify data was fetched successfully.
13642  *
13643  * Note: This function may fail the port, making it inaccessible.
13644  * In such case the explicit port disconnect/connect or physical device
13645  * detach/attach is required to re-evaluate port state again.
13646  */
13647 
13648 static int
13649 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
13650     sata_drive_info_t *sdinfo, int restore)
13651 {
13652 	int rval = SATA_SUCCESS;
13653 	int rval_set;
13654 	sata_drive_info_t new_sdinfo;
13655 	char *finfo = "sata_set_drive_features: cannot";
13656 	char *finfox;
13657 	int cache_op;
13658 
13659 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
13660 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
13661 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
13662 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
13663 		/*
13664 		 * Cannot get device identification - caller may retry later
13665 		 */
13666 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13667 		    "%s fetch device identify data\n", finfo);
13668 		return (SATA_FAILURE);
13669 	}
13670 	finfox = (restore != 0) ? " restore device features" :
13671 	    " initialize device features\n";
13672 
13673 	switch (sdinfo->satadrv_type) {
13674 	case SATA_DTYPE_ATADISK:
13675 		/* Arbitrarily set UDMA mode */
13676 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
13677 		    SATA_SUCCESS) {
13678 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13679 			    "%s set UDMA mode\n", finfo));
13680 			return (SATA_FAILURE);
13681 		}
13682 		break;
13683 	case SATA_DTYPE_ATAPICD:
13684 	case SATA_DTYPE_ATAPITAPE:
13685 	case SATA_DTYPE_ATAPIDISK:
13686 		/*  Set Removable Media Status Notification, if necessary */
13687 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
13688 		    restore != 0) {
13689 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
13690 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
13691 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
13692 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
13693 				/* Current setting does not match saved one */
13694 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
13695 				    sdinfo->satadrv_settings &
13696 				    SATA_DEV_RMSN) != SATA_SUCCESS)
13697 					rval = SATA_FAILURE;
13698 			}
13699 		}
13700 		/*
13701 		 * We have to set Multiword DMA or UDMA, if it is supported, as
13702 		 * we want to use DMA transfer mode whenever possible.
13703 		 * Some devices require explicit setting of the DMA mode.
13704 		 */
13705 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
13706 			/* Set highest supported DMA mode */
13707 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
13708 			    SATA_SUCCESS) {
13709 				SATA_LOG_D((sata_hba_inst, CE_WARN,
13710 				    "%s set UDMA mode\n", finfo));
13711 				rval = SATA_FAILURE;
13712 			}
13713 		}
13714 		break;
13715 	}
13716 
13717 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
13718 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
13719 		/*
13720 		 * neither READ AHEAD nor WRITE CACHE is supported
13721 		 * - do nothing
13722 		 */
13723 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13724 		    "settable features not supported\n", NULL);
13725 		goto update_sdinfo;
13726 	}
13727 
13728 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
13729 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
13730 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
13731 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
13732 		/*
13733 		 * both READ AHEAD and WRITE CACHE are enabled
13734 		 * - Nothing to do
13735 		 */
13736 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13737 		    "no device features to set\n", NULL);
13738 		goto update_sdinfo;
13739 	}
13740 
13741 	cache_op = 0;
13742 
13743 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
13744 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13745 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13746 			/* Enable read ahead / read cache */
13747 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
13748 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13749 			    "enabling read cache\n", NULL);
13750 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
13751 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
13752 			/* Disable read ahead  / read cache */
13753 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
13754 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13755 			    "disabling read cache\n", NULL);
13756 		}
13757 
13758 		if (cache_op != 0) {
13759 			/* Try to set read cache mode */
13760 			rval_set = sata_set_cache_mode(sata_hba_inst,
13761 			    &new_sdinfo, cache_op);
13762 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13763 				rval = rval_set;
13764 		}
13765 	}
13766 
13767 	cache_op = 0;
13768 
13769 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
13770 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13771 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13772 			/* Enable write cache */
13773 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
13774 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13775 			    "enabling write cache\n", NULL);
13776 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
13777 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
13778 			/* Disable write cache */
13779 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
13780 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
13781 			    "disabling write cache\n", NULL);
13782 		}
13783 
13784 		if (cache_op != 0) {
13785 			/* Try to set write cache mode */
13786 			rval_set = sata_set_cache_mode(sata_hba_inst,
13787 			    &new_sdinfo, cache_op);
13788 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
13789 				rval = rval_set;
13790 		}
13791 	}
13792 	if (rval != SATA_SUCCESS)
13793 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13794 		    "%s %s", finfo, finfox));
13795 
13796 update_sdinfo:
13797 	/*
13798 	 * We need to fetch Device Identify data again
13799 	 */
13800 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
13801 		/*
13802 		 * Cannot get device identification - retry later
13803 		 */
13804 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13805 		    "%s re-fetch device identify data\n", finfo));
13806 		rval = SATA_FAILURE;
13807 	}
13808 	/* Copy device sata info. */
13809 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
13810 
13811 	return (rval);
13812 }
13813 
13814 
13815 /*
13816  *
13817  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
13818  * unable to determine.
13819  *
13820  * Cannot be called in an interrupt context.
13821  *
13822  * Called by sata_build_lsense_page_2f()
13823  */
13824 
13825 static int
13826 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
13827     sata_drive_info_t *sdinfo)
13828 {
13829 	sata_pkt_t *spkt;
13830 	sata_cmd_t *scmd;
13831 	sata_pkt_txlate_t *spx;
13832 	int rval;
13833 
13834 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13835 	spx->txlt_sata_hba_inst = sata_hba_inst;
13836 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13837 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13838 	if (spkt == NULL) {
13839 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13840 		return (-1);
13841 	}
13842 	/* address is needed now */
13843 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13844 
13845 
13846 	/* Fill sata_pkt */
13847 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13848 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13849 	/* Synchronous mode, no callback */
13850 	spkt->satapkt_comp = NULL;
13851 	/* Timeout 30s */
13852 	spkt->satapkt_time = sata_default_pkt_time;
13853 
13854 	scmd = &spkt->satapkt_cmd;
13855 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
13856 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13857 
13858 	/* Set up which registers need to be returned */
13859 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
13860 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
13861 
13862 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
13863 	scmd->satacmd_addr_type = 0;		/* N/A */
13864 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13865 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13866 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13867 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13868 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
13869 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13870 	scmd->satacmd_cmd_reg = SATAC_SMART;
13871 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13872 	    sdinfo->satadrv_addr.cport)));
13873 
13874 
13875 	/* Send pkt to SATA HBA driver */
13876 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13877 	    SATA_TRAN_ACCEPTED ||
13878 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13879 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13880 		    sdinfo->satadrv_addr.cport)));
13881 		/*
13882 		 * Whoops, no SMART RETURN STATUS
13883 		 */
13884 		rval = -1;
13885 	} else {
13886 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13887 		    sdinfo->satadrv_addr.cport)));
13888 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
13889 			rval = -1;
13890 			goto fail;
13891 		}
13892 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
13893 			rval = -1;
13894 			goto fail;
13895 		}
13896 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
13897 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
13898 			rval = 0;
13899 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
13900 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
13901 			rval = 1;
13902 		else {
13903 			rval = -1;
13904 			goto fail;
13905 		}
13906 	}
13907 fail:
13908 	/* Free allocated resources */
13909 	sata_pkt_free(spx);
13910 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13911 
13912 	return (rval);
13913 }
13914 
13915 /*
13916  *
13917  * Returns 0 if succeeded, -1 otherwise
13918  *
13919  * Cannot be called in an interrupt context.
13920  *
13921  */
13922 static int
13923 sata_fetch_smart_data(
13924 	sata_hba_inst_t *sata_hba_inst,
13925 	sata_drive_info_t *sdinfo,
13926 	struct smart_data *smart_data)
13927 {
13928 	sata_pkt_t *spkt;
13929 	sata_cmd_t *scmd;
13930 	sata_pkt_txlate_t *spx;
13931 	int rval;
13932 
13933 #if ! defined(lint)
13934 	ASSERT(sizeof (struct smart_data) == 512);
13935 #endif
13936 
13937 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13938 	spx->txlt_sata_hba_inst = sata_hba_inst;
13939 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13940 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13941 	if (spkt == NULL) {
13942 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13943 		return (-1);
13944 	}
13945 	/* address is needed now */
13946 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13947 
13948 
13949 	/* Fill sata_pkt */
13950 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13951 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13952 	/* Synchronous mode, no callback */
13953 	spkt->satapkt_comp = NULL;
13954 	/* Timeout 30s */
13955 	spkt->satapkt_time = sata_default_pkt_time;
13956 
13957 	scmd = &spkt->satapkt_cmd;
13958 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13959 
13960 	/*
13961 	 * Allocate buffer for SMART data
13962 	 */
13963 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13964 	    sizeof (struct smart_data));
13965 	if (scmd->satacmd_bp == NULL) {
13966 		sata_pkt_free(spx);
13967 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13968 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13969 		    "sata_fetch_smart_data: "
13970 		    "cannot allocate buffer"));
13971 		return (-1);
13972 	}
13973 
13974 
13975 	/* Build SMART_READ_DATA cmd in the sata_pkt */
13976 	scmd->satacmd_addr_type = 0;		/* N/A */
13977 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13978 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13979 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13980 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13981 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
13982 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13983 	scmd->satacmd_cmd_reg = SATAC_SMART;
13984 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13985 	    sdinfo->satadrv_addr.cport)));
13986 
13987 	/* Send pkt to SATA HBA driver */
13988 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13989 	    SATA_TRAN_ACCEPTED ||
13990 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13991 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13992 		    sdinfo->satadrv_addr.cport)));
13993 		/*
13994 		 * Whoops, no SMART DATA available
13995 		 */
13996 		rval = -1;
13997 		goto fail;
13998 	} else {
13999 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14000 		    sdinfo->satadrv_addr.cport)));
14001 		if (spx->txlt_buf_dma_handle != NULL) {
14002 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14003 			    DDI_DMA_SYNC_FORKERNEL);
14004 			ASSERT(rval == DDI_SUCCESS);
14005 		}
14006 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
14007 		    sizeof (struct smart_data));
14008 	}
14009 
14010 fail:
14011 	/* Free allocated resources */
14012 	sata_free_local_buffer(spx);
14013 	sata_pkt_free(spx);
14014 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14015 
14016 	return (rval);
14017 }
14018 
14019 /*
14020  * Used by LOG SENSE page 0x10
14021  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
14022  * Note: cannot be called in the interrupt context.
14023  *
14024  * return 0 for success, -1 otherwise
14025  *
14026  */
14027 static int
14028 sata_ext_smart_selftest_read_log(
14029 	sata_hba_inst_t *sata_hba_inst,
14030 	sata_drive_info_t *sdinfo,
14031 	struct smart_ext_selftest_log *ext_selftest_log,
14032 	uint16_t block_num)
14033 {
14034 	sata_pkt_txlate_t *spx;
14035 	sata_pkt_t *spkt;
14036 	sata_cmd_t *scmd;
14037 	int rval;
14038 
14039 #if ! defined(lint)
14040 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
14041 #endif
14042 
14043 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14044 	spx->txlt_sata_hba_inst = sata_hba_inst;
14045 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14046 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14047 	if (spkt == NULL) {
14048 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14049 		return (-1);
14050 	}
14051 	/* address is needed now */
14052 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14053 
14054 
14055 	/* Fill sata_pkt */
14056 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14057 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14058 	/* Synchronous mode, no callback */
14059 	spkt->satapkt_comp = NULL;
14060 	/* Timeout 30s */
14061 	spkt->satapkt_time = sata_default_pkt_time;
14062 
14063 	scmd = &spkt->satapkt_cmd;
14064 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14065 
14066 	/*
14067 	 * Allocate buffer for SMART extended self-test log
14068 	 */
14069 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14070 	    sizeof (struct smart_ext_selftest_log));
14071 	if (scmd->satacmd_bp == NULL) {
14072 		sata_pkt_free(spx);
14073 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14074 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14075 		    "sata_ext_smart_selftest_log: "
14076 		    "cannot allocate buffer"));
14077 		return (-1);
14078 	}
14079 
14080 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
14081 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
14082 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
14083 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
14084 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
14085 	scmd->satacmd_lba_low_msb = 0;
14086 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
14087 	scmd->satacmd_lba_mid_msb = block_num >> 8;
14088 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14089 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
14090 
14091 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14092 	    sdinfo->satadrv_addr.cport)));
14093 
14094 	/* Send pkt to SATA HBA driver */
14095 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14096 	    SATA_TRAN_ACCEPTED ||
14097 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14098 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14099 		    sdinfo->satadrv_addr.cport)));
14100 
14101 		/*
14102 		 * Whoops, no SMART selftest log info available
14103 		 */
14104 		rval = -1;
14105 		goto fail;
14106 	} else {
14107 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14108 		    sdinfo->satadrv_addr.cport)));
14109 
14110 		if (spx->txlt_buf_dma_handle != NULL) {
14111 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14112 			    DDI_DMA_SYNC_FORKERNEL);
14113 			ASSERT(rval == DDI_SUCCESS);
14114 		}
14115 		bcopy(scmd->satacmd_bp->b_un.b_addr,
14116 		    (uint8_t *)ext_selftest_log,
14117 		    sizeof (struct smart_ext_selftest_log));
14118 		rval = 0;
14119 	}
14120 
14121 fail:
14122 	/* Free allocated resources */
14123 	sata_free_local_buffer(spx);
14124 	sata_pkt_free(spx);
14125 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14126 
14127 	return (rval);
14128 }
14129 
14130 /*
14131  * Returns 0 for success, -1 otherwise
14132  *
14133  * SMART self-test log data is returned in buffer pointed to by selftest_log
14134  */
14135 static int
14136 sata_smart_selftest_log(
14137 	sata_hba_inst_t *sata_hba_inst,
14138 	sata_drive_info_t *sdinfo,
14139 	struct smart_selftest_log *selftest_log)
14140 {
14141 	sata_pkt_t *spkt;
14142 	sata_cmd_t *scmd;
14143 	sata_pkt_txlate_t *spx;
14144 	int rval;
14145 
14146 #if ! defined(lint)
14147 	ASSERT(sizeof (struct smart_selftest_log) == 512);
14148 #endif
14149 
14150 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14151 	spx->txlt_sata_hba_inst = sata_hba_inst;
14152 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14153 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14154 	if (spkt == NULL) {
14155 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14156 		return (-1);
14157 	}
14158 	/* address is needed now */
14159 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14160 
14161 
14162 	/* Fill sata_pkt */
14163 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14164 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14165 	/* Synchronous mode, no callback */
14166 	spkt->satapkt_comp = NULL;
14167 	/* Timeout 30s */
14168 	spkt->satapkt_time = sata_default_pkt_time;
14169 
14170 	scmd = &spkt->satapkt_cmd;
14171 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14172 
14173 	/*
14174 	 * Allocate buffer for SMART SELFTEST LOG
14175 	 */
14176 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14177 	    sizeof (struct smart_selftest_log));
14178 	if (scmd->satacmd_bp == NULL) {
14179 		sata_pkt_free(spx);
14180 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14181 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14182 		    "sata_smart_selftest_log: "
14183 		    "cannot allocate buffer"));
14184 		return (-1);
14185 	}
14186 
14187 	/* Build SMART_READ_LOG cmd in the sata_pkt */
14188 	scmd->satacmd_addr_type = 0;		/* N/A */
14189 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
14190 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
14191 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
14192 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
14193 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
14194 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14195 	scmd->satacmd_cmd_reg = SATAC_SMART;
14196 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14197 	    sdinfo->satadrv_addr.cport)));
14198 
14199 	/* Send pkt to SATA HBA driver */
14200 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14201 	    SATA_TRAN_ACCEPTED ||
14202 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14203 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14204 		    sdinfo->satadrv_addr.cport)));
14205 		/*
14206 		 * Whoops, no SMART DATA available
14207 		 */
14208 		rval = -1;
14209 		goto fail;
14210 	} else {
14211 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14212 		    sdinfo->satadrv_addr.cport)));
14213 		if (spx->txlt_buf_dma_handle != NULL) {
14214 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14215 			    DDI_DMA_SYNC_FORKERNEL);
14216 			ASSERT(rval == DDI_SUCCESS);
14217 		}
14218 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
14219 		    sizeof (struct smart_selftest_log));
14220 		rval = 0;
14221 	}
14222 
14223 fail:
14224 	/* Free allocated resources */
14225 	sata_free_local_buffer(spx);
14226 	sata_pkt_free(spx);
14227 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14228 
14229 	return (rval);
14230 }
14231 
14232 
14233 /*
14234  * Returns 0 for success, -1 otherwise
14235  *
14236  * SMART READ LOG data is returned in buffer pointed to by smart_log
14237  */
14238 static int
14239 sata_smart_read_log(
14240 	sata_hba_inst_t *sata_hba_inst,
14241 	sata_drive_info_t *sdinfo,
14242 	uint8_t *smart_log,		/* where the data should be returned */
14243 	uint8_t which_log,		/* which log should be returned */
14244 	uint8_t log_size)		/* # of 512 bytes in log */
14245 {
14246 	sata_pkt_t *spkt;
14247 	sata_cmd_t *scmd;
14248 	sata_pkt_txlate_t *spx;
14249 	int rval;
14250 
14251 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14252 	spx->txlt_sata_hba_inst = sata_hba_inst;
14253 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14254 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14255 	if (spkt == NULL) {
14256 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14257 		return (-1);
14258 	}
14259 	/* address is needed now */
14260 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14261 
14262 
14263 	/* Fill sata_pkt */
14264 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14265 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14266 	/* Synchronous mode, no callback */
14267 	spkt->satapkt_comp = NULL;
14268 	/* Timeout 30s */
14269 	spkt->satapkt_time = sata_default_pkt_time;
14270 
14271 	scmd = &spkt->satapkt_cmd;
14272 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14273 
14274 	/*
14275 	 * Allocate buffer for SMART READ LOG
14276 	 */
14277 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
14278 	if (scmd->satacmd_bp == NULL) {
14279 		sata_pkt_free(spx);
14280 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14281 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14282 		    "sata_smart_read_log: " "cannot allocate buffer"));
14283 		return (-1);
14284 	}
14285 
14286 	/* Build SMART_READ_LOG cmd in the sata_pkt */
14287 	scmd->satacmd_addr_type = 0;		/* N/A */
14288 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
14289 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
14290 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
14291 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
14292 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
14293 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14294 	scmd->satacmd_cmd_reg = SATAC_SMART;
14295 
14296 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14297 	    sdinfo->satadrv_addr.cport)));
14298 
14299 	/* Send pkt to SATA HBA driver */
14300 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14301 	    SATA_TRAN_ACCEPTED ||
14302 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14303 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14304 		    sdinfo->satadrv_addr.cport)));
14305 
14306 		/*
14307 		 * Whoops, no SMART DATA available
14308 		 */
14309 		rval = -1;
14310 		goto fail;
14311 	} else {
14312 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14313 		    sdinfo->satadrv_addr.cport)));
14314 
14315 		if (spx->txlt_buf_dma_handle != NULL) {
14316 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14317 			    DDI_DMA_SYNC_FORKERNEL);
14318 			ASSERT(rval == DDI_SUCCESS);
14319 		}
14320 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
14321 		rval = 0;
14322 	}
14323 
14324 fail:
14325 	/* Free allocated resources */
14326 	sata_free_local_buffer(spx);
14327 	sata_pkt_free(spx);
14328 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14329 
14330 	return (rval);
14331 }
14332 
14333 /*
14334  * Used by LOG SENSE page 0x10
14335  *
14336  * return 0 for success, -1 otherwise
14337  *
14338  */
14339 static int
14340 sata_read_log_ext_directory(
14341 	sata_hba_inst_t *sata_hba_inst,
14342 	sata_drive_info_t *sdinfo,
14343 	struct read_log_ext_directory *logdir)
14344 {
14345 	sata_pkt_txlate_t *spx;
14346 	sata_pkt_t *spkt;
14347 	sata_cmd_t *scmd;
14348 	int rval;
14349 
14350 #if ! defined(lint)
14351 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
14352 #endif
14353 
14354 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14355 	spx->txlt_sata_hba_inst = sata_hba_inst;
14356 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
14357 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14358 	if (spkt == NULL) {
14359 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14360 		return (-1);
14361 	}
14362 
14363 	/* Fill sata_pkt */
14364 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14365 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14366 	/* Synchronous mode, no callback */
14367 	spkt->satapkt_comp = NULL;
14368 	/* Timeout 30s */
14369 	spkt->satapkt_time = sata_default_pkt_time;
14370 
14371 	scmd = &spkt->satapkt_cmd;
14372 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
14373 
14374 	/*
14375 	 * Allocate buffer for SMART READ LOG EXTENDED command
14376 	 */
14377 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
14378 	    sizeof (struct read_log_ext_directory));
14379 	if (scmd->satacmd_bp == NULL) {
14380 		sata_pkt_free(spx);
14381 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
14382 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14383 		    "sata_read_log_ext_directory: "
14384 		    "cannot allocate buffer"));
14385 		return (-1);
14386 	}
14387 
14388 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
14389 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
14390 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
14391 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
14392 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
14393 	scmd->satacmd_lba_low_msb = 0;
14394 	scmd->satacmd_lba_mid_lsb = 0;
14395 	scmd->satacmd_lba_mid_msb = 0;
14396 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
14397 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
14398 
14399 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
14400 	    sdinfo->satadrv_addr.cport)));
14401 
14402 	/* Send pkt to SATA HBA driver */
14403 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
14404 	    SATA_TRAN_ACCEPTED ||
14405 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14406 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14407 		    sdinfo->satadrv_addr.cport)));
14408 		/*
14409 		 * Whoops, no SMART selftest log info available
14410 		 */
14411 		rval = -1;
14412 		goto fail;
14413 	} else {
14414 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
14415 		    sdinfo->satadrv_addr.cport)));
14416 		if (spx->txlt_buf_dma_handle != NULL) {
14417 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
14418 			    DDI_DMA_SYNC_FORKERNEL);
14419 			ASSERT(rval == DDI_SUCCESS);
14420 		}
14421 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
14422 		    sizeof (struct read_log_ext_directory));
14423 		rval = 0;
14424 	}
14425 
14426 fail:
14427 	/* Free allocated resources */
14428 	sata_free_local_buffer(spx);
14429 	sata_pkt_free(spx);
14430 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
14431 
14432 	return (rval);
14433 }
14434 
14435 /*
14436  * Set up error retrieval sata command for NCQ command error data
14437  * recovery.
14438  *
14439  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
14440  * returns SATA_FAILURE otherwise.
14441  */
14442 static int
14443 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
14444 {
14445 #ifndef __lock_lint
14446 	_NOTE(ARGUNUSED(sdinfo))
14447 #endif
14448 
14449 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
14450 	sata_cmd_t *scmd;
14451 	struct buf *bp;
14452 
14453 	/* Operation modes are up to the caller */
14454 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14455 
14456 	/* Synchronous mode, no callback - may be changed by the caller */
14457 	spkt->satapkt_comp = NULL;
14458 	spkt->satapkt_time = sata_default_pkt_time;
14459 
14460 	scmd = &spkt->satapkt_cmd;
14461 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
14462 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14463 
14464 	/*
14465 	 * Allocate dma_able buffer error data.
14466 	 * Buffer allocation will take care of buffer alignment and other DMA
14467 	 * attributes.
14468 	 */
14469 	bp = sata_alloc_local_buffer(spx,
14470 	    sizeof (struct sata_ncq_error_recovery_page));
14471 	if (bp == NULL)
14472 		return (SATA_FAILURE);
14473 
14474 	bp_mapin(bp); /* make data buffer accessible */
14475 	scmd->satacmd_bp = bp;
14476 
14477 	/*
14478 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
14479 	 * before accessing it. Handle is in usual place in translate struct.
14480 	 */
14481 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
14482 
14483 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
14484 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
14485 
14486 	return (SATA_SUCCESS);
14487 }
14488 
14489 /*
14490  * sata_xlate_errors() is used to translate (S)ATA error
14491  * information to SCSI information returned in the SCSI
14492  * packet.
14493  */
14494 static void
14495 sata_xlate_errors(sata_pkt_txlate_t *spx)
14496 {
14497 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
14498 	struct scsi_extended_sense *sense;
14499 
14500 	scsipkt->pkt_reason = CMD_INCOMPLETE;
14501 	*scsipkt->pkt_scbp = STATUS_CHECK;
14502 	sense = sata_arq_sense(spx);
14503 
14504 	switch (spx->txlt_sata_pkt->satapkt_reason) {
14505 	case SATA_PKT_PORT_ERROR:
14506 		/*
14507 		 * We have no device data. Assume no data transfered.
14508 		 */
14509 		sense->es_key = KEY_HARDWARE_ERROR;
14510 		break;
14511 
14512 	case SATA_PKT_DEV_ERROR:
14513 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
14514 		    SATA_STATUS_ERR) {
14515 			/*
14516 			 * determine dev error reason from error
14517 			 * reg content
14518 			 */
14519 			sata_decode_device_error(spx, sense);
14520 			break;
14521 		}
14522 		/* No extended sense key - no info available */
14523 		break;
14524 
14525 	case SATA_PKT_TIMEOUT:
14526 		scsipkt->pkt_reason = CMD_TIMEOUT;
14527 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
14528 		/* No extended sense key */
14529 		break;
14530 
14531 	case SATA_PKT_ABORTED:
14532 		scsipkt->pkt_reason = CMD_ABORTED;
14533 		scsipkt->pkt_statistics |= STAT_ABORTED;
14534 		/* No extended sense key */
14535 		break;
14536 
14537 	case SATA_PKT_RESET:
14538 		/*
14539 		 * pkt aborted either by an explicit reset request from
14540 		 * a host, or due to error recovery
14541 		 */
14542 		scsipkt->pkt_reason = CMD_RESET;
14543 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
14544 		break;
14545 
14546 	default:
14547 		scsipkt->pkt_reason = CMD_TRAN_ERR;
14548 		break;
14549 	}
14550 }
14551 
14552 
14553 
14554 
14555 /*
14556  * Log sata message
14557  * dev pathname msg line preceeds the logged message.
14558  */
14559 
14560 static	void
14561 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
14562 {
14563 	char pathname[128];
14564 	dev_info_t *dip = NULL;
14565 	va_list ap;
14566 
14567 	mutex_enter(&sata_log_mutex);
14568 
14569 	va_start(ap, fmt);
14570 	(void) vsprintf(sata_log_buf, fmt, ap);
14571 	va_end(ap);
14572 
14573 	if (sata_hba_inst != NULL) {
14574 		dip = SATA_DIP(sata_hba_inst);
14575 		(void) ddi_pathname(dip, pathname);
14576 	} else {
14577 		pathname[0] = 0;
14578 	}
14579 	if (level == CE_CONT) {
14580 		if (sata_debug_flags == 0)
14581 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
14582 		else
14583 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
14584 	} else {
14585 		if (level != CE_NOTE) {
14586 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
14587 		} else if (sata_msg) {
14588 			cmn_err(level, "%s:\n %s", pathname,
14589 			    sata_log_buf);
14590 		}
14591 	}
14592 
14593 	/* sata trace debug */
14594 	sata_trace_debug(dip, sata_log_buf);
14595 
14596 	mutex_exit(&sata_log_mutex);
14597 }
14598 
14599 
14600 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
14601 
14602 /*
14603  * Start or terminate the thread, depending on flag arg and current state
14604  */
14605 static void
14606 sata_event_thread_control(int startstop)
14607 {
14608 	static 	int sata_event_thread_terminating = 0;
14609 	static 	int sata_event_thread_starting = 0;
14610 	int i;
14611 
14612 	mutex_enter(&sata_event_mutex);
14613 
14614 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
14615 	    sata_event_thread_terminating == 1)) {
14616 		mutex_exit(&sata_event_mutex);
14617 		return;
14618 	}
14619 	if (startstop == 1 && sata_event_thread_starting == 1) {
14620 		mutex_exit(&sata_event_mutex);
14621 		return;
14622 	}
14623 	if (startstop == 1 && sata_event_thread_terminating == 1) {
14624 		sata_event_thread_starting = 1;
14625 		/* wait til terminate operation completes */
14626 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
14627 		while (sata_event_thread_terminating == 1) {
14628 			if (i-- <= 0) {
14629 				sata_event_thread_starting = 0;
14630 				mutex_exit(&sata_event_mutex);
14631 #ifdef SATA_DEBUG
14632 				cmn_err(CE_WARN, "sata_event_thread_control: "
14633 				    "timeout waiting for thread to terminate");
14634 #endif
14635 				return;
14636 			}
14637 			mutex_exit(&sata_event_mutex);
14638 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
14639 			mutex_enter(&sata_event_mutex);
14640 		}
14641 	}
14642 	if (startstop == 1) {
14643 		if (sata_event_thread == NULL) {
14644 			sata_event_thread = thread_create(NULL, 0,
14645 			    (void (*)())sata_event_daemon,
14646 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
14647 		}
14648 		sata_event_thread_starting = 0;
14649 		mutex_exit(&sata_event_mutex);
14650 		return;
14651 	}
14652 
14653 	/*
14654 	 * If we got here, thread may need to be terminated
14655 	 */
14656 	if (sata_event_thread != NULL) {
14657 		int i;
14658 		/* Signal event thread to go away */
14659 		sata_event_thread_terminating = 1;
14660 		sata_event_thread_terminate = 1;
14661 		cv_signal(&sata_event_cv);
14662 		/*
14663 		 * Wait til daemon terminates.
14664 		 */
14665 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
14666 		while (sata_event_thread_terminate == 1) {
14667 			mutex_exit(&sata_event_mutex);
14668 			if (i-- <= 0) {
14669 				/* Daemon did not go away !!! */
14670 #ifdef SATA_DEBUG
14671 				cmn_err(CE_WARN, "sata_event_thread_control: "
14672 				    "cannot terminate event daemon thread");
14673 #endif
14674 				mutex_enter(&sata_event_mutex);
14675 				break;
14676 			}
14677 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
14678 			mutex_enter(&sata_event_mutex);
14679 		}
14680 		sata_event_thread_terminating = 0;
14681 	}
14682 	ASSERT(sata_event_thread_terminating == 0);
14683 	ASSERT(sata_event_thread_starting == 0);
14684 	mutex_exit(&sata_event_mutex);
14685 }
14686 
14687 
14688 /*
14689  * SATA HBA event notification function.
14690  * Events reported by SATA HBA drivers per HBA instance relate to a change in
14691  * a port and/or device state or a controller itself.
14692  * Events for different addresses/addr types cannot be combined.
14693  * A warning message is generated for each event type.
14694  * Events are not processed by this function, so only the
14695  * event flag(s)is set for an affected entity and the event thread is
14696  * waken up. Event daemon thread processes all events.
14697  *
14698  * NOTE: Since more than one event may be reported at the same time, one
14699  * cannot determine a sequence of events when opposite event are reported, eg.
14700  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
14701  * is taking precedence over reported events, i.e. may cause ignoring some
14702  * events.
14703  */
14704 #define	SATA_EVENT_MAX_MSG_LENGTH	79
14705 
14706 void
14707 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
14708 {
14709 	sata_hba_inst_t *sata_hba_inst = NULL;
14710 	sata_address_t *saddr;
14711 	sata_drive_info_t *sdinfo;
14712 	sata_port_stats_t *pstats;
14713 	sata_cport_info_t *cportinfo;
14714 	sata_pmport_info_t *pmportinfo;
14715 	int cport, pmport;
14716 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
14717 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
14718 	char *lcp;
14719 	static char *err_msg_evnt_1 =
14720 	    "sata_hba_event_notify: invalid port event 0x%x ";
14721 	static char *err_msg_evnt_2 =
14722 	    "sata_hba_event_notify: invalid device event 0x%x ";
14723 	int linkevent;
14724 
14725 	/*
14726 	 * There is a possibility that an event will be generated on HBA
14727 	 * that has not completed attachment or is detaching. We still want
14728 	 * to process events until HBA is detached.
14729 	 */
14730 	mutex_enter(&sata_mutex);
14731 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14732 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14733 		if (SATA_DIP(sata_hba_inst) == dip)
14734 			if (sata_hba_inst->satahba_attached == 1)
14735 				break;
14736 	}
14737 	mutex_exit(&sata_mutex);
14738 	if (sata_hba_inst == NULL)
14739 		/* HBA not attached */
14740 		return;
14741 
14742 	ASSERT(sata_device != NULL);
14743 
14744 	/*
14745 	 * Validate address before - do not proceed with invalid address.
14746 	 */
14747 	saddr = &sata_device->satadev_addr;
14748 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
14749 		return;
14750 	if (saddr->qual == SATA_ADDR_PMPORT ||
14751 	    saddr->qual == SATA_ADDR_DPMPORT)
14752 		/* Port Multiplier not supported yet */
14753 		return;
14754 
14755 	cport = saddr->cport;
14756 	pmport = saddr->pmport;
14757 
14758 	buf1[0] = buf2[0] = '\0';
14759 
14760 	/*
14761 	 * If event relates to port or device, check port state.
14762 	 * Port has to be initialized, or we cannot accept an event.
14763 	 */
14764 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
14765 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) {
14766 		if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) {
14767 			mutex_enter(&sata_hba_inst->satahba_mutex);
14768 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14769 			mutex_exit(&sata_hba_inst->satahba_mutex);
14770 			if (cportinfo == NULL || cportinfo->cport_state == 0)
14771 				return;
14772 		} else {
14773 			mutex_enter(&sata_hba_inst->satahba_mutex);
14774 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14775 			    cport, pmport);
14776 			mutex_exit(&sata_hba_inst->satahba_mutex);
14777 			if (pmportinfo == NULL || pmportinfo->pmport_state == 0)
14778 				return;
14779 		}
14780 	}
14781 
14782 	/*
14783 	 * Events refer to devices, ports and controllers - each has
14784 	 * unique address. Events for different addresses cannot be combined.
14785 	 */
14786 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
14787 
14788 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14789 
14790 		/* qualify this event(s) */
14791 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
14792 			/* Invalid event for the device port */
14793 			(void) sprintf(buf2, err_msg_evnt_1,
14794 			    event & SATA_EVNT_PORT_EVENTS);
14795 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14796 			goto event_info;
14797 		}
14798 		if (saddr->qual == SATA_ADDR_CPORT) {
14799 			/* Controller's device port event */
14800 
14801 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
14802 			    cport_event_flags |=
14803 			    event & SATA_EVNT_PORT_EVENTS;
14804 			pstats =
14805 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
14806 			    cport_stats;
14807 		} else {
14808 			/* Port multiplier's device port event */
14809 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14810 			    pmport_event_flags |=
14811 			    event & SATA_EVNT_PORT_EVENTS;
14812 			pstats =
14813 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14814 			    pmport_stats;
14815 		}
14816 
14817 		/*
14818 		 * Add to statistics and log the message. We have to do it
14819 		 * here rather than in the event daemon, because there may be
14820 		 * multiple events occuring before they are processed.
14821 		 */
14822 		linkevent = event &
14823 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
14824 		if (linkevent) {
14825 			if (linkevent == (SATA_EVNT_LINK_LOST |
14826 			    SATA_EVNT_LINK_ESTABLISHED)) {
14827 				/* This is likely event combination */
14828 				(void) strlcat(buf1, "link lost/established, ",
14829 				    SATA_EVENT_MAX_MSG_LENGTH);
14830 
14831 				if (pstats->link_lost < 0xffffffffffffffffULL)
14832 					pstats->link_lost++;
14833 				if (pstats->link_established <
14834 				    0xffffffffffffffffULL)
14835 					pstats->link_established++;
14836 				linkevent = 0;
14837 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
14838 				(void) strlcat(buf1, "link lost, ",
14839 				    SATA_EVENT_MAX_MSG_LENGTH);
14840 
14841 				if (pstats->link_lost < 0xffffffffffffffffULL)
14842 					pstats->link_lost++;
14843 			} else {
14844 				(void) strlcat(buf1, "link established, ",
14845 				    SATA_EVENT_MAX_MSG_LENGTH);
14846 				if (pstats->link_established <
14847 				    0xffffffffffffffffULL)
14848 					pstats->link_established++;
14849 			}
14850 		}
14851 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
14852 			(void) strlcat(buf1, "device attached, ",
14853 			    SATA_EVENT_MAX_MSG_LENGTH);
14854 			if (pstats->device_attached < 0xffffffffffffffffULL)
14855 				pstats->device_attached++;
14856 		}
14857 		if (event & SATA_EVNT_DEVICE_DETACHED) {
14858 			(void) strlcat(buf1, "device detached, ",
14859 			    SATA_EVENT_MAX_MSG_LENGTH);
14860 			if (pstats->device_detached < 0xffffffffffffffffULL)
14861 				pstats->device_detached++;
14862 		}
14863 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
14864 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14865 			    "port %d power level changed", cport);
14866 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
14867 				pstats->port_pwr_changed++;
14868 		}
14869 
14870 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
14871 			/* There should be no other events for this address */
14872 			(void) sprintf(buf2, err_msg_evnt_1,
14873 			    event & ~SATA_EVNT_PORT_EVENTS);
14874 		}
14875 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14876 
14877 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
14878 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14879 
14880 		/* qualify this event */
14881 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
14882 			/* Invalid event for a device */
14883 			(void) sprintf(buf2, err_msg_evnt_2,
14884 			    event & SATA_EVNT_DEVICE_RESET);
14885 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14886 			goto event_info;
14887 		}
14888 		/* drive event */
14889 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
14890 		if (sdinfo != NULL) {
14891 			if (event & SATA_EVNT_DEVICE_RESET) {
14892 				(void) strlcat(buf1, "device reset, ",
14893 				    SATA_EVENT_MAX_MSG_LENGTH);
14894 				if (sdinfo->satadrv_stats.drive_reset <
14895 				    0xffffffffffffffffULL)
14896 					sdinfo->satadrv_stats.drive_reset++;
14897 				sdinfo->satadrv_event_flags |=
14898 				    SATA_EVNT_DEVICE_RESET;
14899 			}
14900 		}
14901 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
14902 			/* Invalid event for a device */
14903 			(void) sprintf(buf2, err_msg_evnt_2,
14904 			    event & ~SATA_EVNT_DRIVE_EVENTS);
14905 		}
14906 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14907 	} else {
14908 		if (saddr->qual != SATA_ADDR_NULL) {
14909 			/* Wrong address qualifier */
14910 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14911 			    "sata_hba_event_notify: invalid address 0x%x",
14912 			    *(uint32_t *)saddr));
14913 			return;
14914 		}
14915 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
14916 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
14917 			/* Invalid event for the controller */
14918 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14919 			    "sata_hba_event_notify: invalid event 0x%x for "
14920 			    "controller",
14921 			    event & SATA_EVNT_CONTROLLER_EVENTS));
14922 			return;
14923 		}
14924 		buf1[0] = '\0';
14925 		/* This may be a frequent and not interesting event */
14926 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14927 		    "controller power level changed\n", NULL);
14928 
14929 		mutex_enter(&sata_hba_inst->satahba_mutex);
14930 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
14931 		    0xffffffffffffffffULL)
14932 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
14933 
14934 		sata_hba_inst->satahba_event_flags |=
14935 		    SATA_EVNT_PWR_LEVEL_CHANGED;
14936 		mutex_exit(&sata_hba_inst->satahba_mutex);
14937 	}
14938 	/*
14939 	 * If we got here, there is something to do with this HBA
14940 	 * instance.
14941 	 */
14942 	mutex_enter(&sata_hba_inst->satahba_mutex);
14943 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14944 	mutex_exit(&sata_hba_inst->satahba_mutex);
14945 	mutex_enter(&sata_mutex);
14946 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
14947 	mutex_exit(&sata_mutex);
14948 
14949 	/* Tickle event thread */
14950 	mutex_enter(&sata_event_mutex);
14951 	if (sata_event_thread_active == 0)
14952 		cv_signal(&sata_event_cv);
14953 	mutex_exit(&sata_event_mutex);
14954 
14955 event_info:
14956 	if (buf1[0] != '\0') {
14957 		lcp = strrchr(buf1, ',');
14958 		if (lcp != NULL)
14959 			*lcp = '\0';
14960 	}
14961 	if (saddr->qual == SATA_ADDR_CPORT ||
14962 	    saddr->qual == SATA_ADDR_DCPORT) {
14963 		if (buf1[0] != '\0') {
14964 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14965 			    cport, buf1);
14966 		}
14967 		if (buf2[0] != '\0') {
14968 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14969 			    cport, buf2);
14970 		}
14971 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
14972 	    saddr->qual == SATA_ADDR_DPMPORT) {
14973 		if (buf1[0] != '\0') {
14974 			sata_log(sata_hba_inst, CE_NOTE,
14975 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
14976 		}
14977 		if (buf2[0] != '\0') {
14978 			sata_log(sata_hba_inst, CE_NOTE,
14979 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
14980 		}
14981 	}
14982 }
14983 
14984 
14985 /*
14986  * Event processing thread.
14987  * Arg is a pointer to the sata_hba_list pointer.
14988  * It is not really needed, because sata_hba_list is global and static
14989  */
14990 static void
14991 sata_event_daemon(void *arg)
14992 {
14993 #ifndef __lock_lint
14994 	_NOTE(ARGUNUSED(arg))
14995 #endif
14996 	sata_hba_inst_t *sata_hba_inst;
14997 	clock_t lbolt;
14998 
14999 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15000 	    "SATA event daemon started\n", NULL);
15001 loop:
15002 	/*
15003 	 * Process events here. Walk through all registered HBAs
15004 	 */
15005 	mutex_enter(&sata_mutex);
15006 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
15007 	    sata_hba_inst = sata_hba_inst->satahba_next) {
15008 		ASSERT(sata_hba_inst != NULL);
15009 		mutex_enter(&sata_hba_inst->satahba_mutex);
15010 		if (sata_hba_inst->satahba_attached == 0 ||
15011 		    (sata_hba_inst->satahba_event_flags &
15012 		    SATA_EVNT_SKIP) != 0) {
15013 			mutex_exit(&sata_hba_inst->satahba_mutex);
15014 			continue;
15015 		}
15016 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
15017 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
15018 			mutex_exit(&sata_hba_inst->satahba_mutex);
15019 			mutex_exit(&sata_mutex);
15020 			/* Got the controller with pending event */
15021 			sata_process_controller_events(sata_hba_inst);
15022 			/*
15023 			 * Since global mutex was released, there is a
15024 			 * possibility that HBA list has changed, so start
15025 			 * over from the top. Just processed controller
15026 			 * will be passed-over because of the SKIP flag.
15027 			 */
15028 			goto loop;
15029 		}
15030 		mutex_exit(&sata_hba_inst->satahba_mutex);
15031 	}
15032 	/* Clear SKIP flag in all controllers */
15033 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
15034 	    sata_hba_inst = sata_hba_inst->satahba_next) {
15035 		mutex_enter(&sata_hba_inst->satahba_mutex);
15036 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
15037 		mutex_exit(&sata_hba_inst->satahba_mutex);
15038 	}
15039 	mutex_exit(&sata_mutex);
15040 
15041 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15042 	    "SATA EVENT DAEMON suspending itself", NULL);
15043 
15044 #ifdef SATA_DEBUG
15045 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
15046 		sata_log(sata_hba_inst, CE_WARN,
15047 		    "SATA EVENTS PROCESSING DISABLED\n");
15048 		thread_exit(); /* Daemon will not run again */
15049 	}
15050 #endif
15051 	mutex_enter(&sata_event_mutex);
15052 	sata_event_thread_active = 0;
15053 	mutex_exit(&sata_event_mutex);
15054 	/*
15055 	 * Go to sleep/suspend itself and wake up either because new event or
15056 	 * wait timeout. Exit if there is a termination request (driver
15057 	 * unload).
15058 	 */
15059 	do {
15060 		lbolt = ddi_get_lbolt();
15061 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
15062 		mutex_enter(&sata_event_mutex);
15063 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
15064 
15065 		if (sata_event_thread_active != 0) {
15066 			mutex_exit(&sata_event_mutex);
15067 			continue;
15068 		}
15069 
15070 		/* Check if it is time to go away */
15071 		if (sata_event_thread_terminate == 1) {
15072 			/*
15073 			 * It is up to the thread setting above flag to make
15074 			 * sure that this thread is not killed prematurely.
15075 			 */
15076 			sata_event_thread_terminate = 0;
15077 			sata_event_thread = NULL;
15078 			mutex_exit(&sata_event_mutex);
15079 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15080 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
15081 			thread_exit();  { _NOTE(NOT_REACHED) }
15082 		}
15083 		mutex_exit(&sata_event_mutex);
15084 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
15085 
15086 	mutex_enter(&sata_event_mutex);
15087 	sata_event_thread_active = 1;
15088 	mutex_exit(&sata_event_mutex);
15089 
15090 	mutex_enter(&sata_mutex);
15091 	sata_event_pending &= ~SATA_EVNT_MAIN;
15092 	mutex_exit(&sata_mutex);
15093 
15094 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
15095 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
15096 
15097 	goto loop;
15098 }
15099 
15100 /*
15101  * Specific HBA instance event processing.
15102  *
15103  * NOTE: At the moment, device event processing is limited to hard disks
15104  * only.
15105  * cports only are supported - no pmports.
15106  */
15107 static void
15108 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
15109 {
15110 	int ncport;
15111 	uint32_t event_flags;
15112 	sata_address_t *saddr;
15113 	sata_cport_info_t *cportinfo;
15114 
15115 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
15116 	    "Processing controller %d event(s)",
15117 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
15118 
15119 	mutex_enter(&sata_hba_inst->satahba_mutex);
15120 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
15121 	event_flags = sata_hba_inst->satahba_event_flags;
15122 	mutex_exit(&sata_hba_inst->satahba_mutex);
15123 	/*
15124 	 * Process controller power change first
15125 	 * HERE
15126 	 */
15127 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
15128 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
15129 
15130 	/*
15131 	 * Search through ports/devices to identify affected port/device.
15132 	 * We may have to process events for more than one port/device.
15133 	 */
15134 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
15135 		/*
15136 		 * Not all ports may be processed in attach by the time we
15137 		 * get an event. Check if port info is initialized.
15138 		 */
15139 		mutex_enter(&sata_hba_inst->satahba_mutex);
15140 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
15141 		mutex_exit(&sata_hba_inst->satahba_mutex);
15142 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
15143 			continue;
15144 
15145 		/* We have initialized controller port info */
15146 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15147 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
15148 		    cport_event_flags;
15149 		/* Check if port was locked by IOCTL processing */
15150 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
15151 			/*
15152 			 * We ignore port events because port is busy
15153 			 * with AP control processing. Set again
15154 			 * controller and main event flag, so that
15155 			 * events may be processed by the next daemon
15156 			 * run.
15157 			 */
15158 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15159 			mutex_enter(&sata_hba_inst->satahba_mutex);
15160 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15161 			mutex_exit(&sata_hba_inst->satahba_mutex);
15162 			mutex_enter(&sata_mutex);
15163 			sata_event_pending |= SATA_EVNT_MAIN;
15164 			mutex_exit(&sata_mutex);
15165 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
15166 			    "Event processing postponed until "
15167 			    "AP control processing completes",
15168 			    NULL);
15169 			/* Check other ports */
15170 			continue;
15171 		} else {
15172 			/*
15173 			 * Set BSY flag so that AP control would not
15174 			 * interfere with events processing for
15175 			 * this port.
15176 			 */
15177 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
15178 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
15179 		}
15180 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15181 
15182 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
15183 
15184 		if ((event_flags &
15185 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
15186 			/*
15187 			 * Got port event.
15188 			 * We need some hierarchy of event processing as they
15189 			 * are affecting each other:
15190 			 * 1. port failed
15191 			 * 2. device detached/attached
15192 			 * 3. link events - link events may trigger device
15193 			 *    detached or device attached events in some
15194 			 *    circumstances.
15195 			 * 4. port power level changed
15196 			 */
15197 			if (event_flags & SATA_EVNT_PORT_FAILED) {
15198 				sata_process_port_failed_event(sata_hba_inst,
15199 				    saddr);
15200 			}
15201 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
15202 				sata_process_device_detached(sata_hba_inst,
15203 				    saddr);
15204 			}
15205 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
15206 				sata_process_device_attached(sata_hba_inst,
15207 				    saddr);
15208 			}
15209 			if (event_flags &
15210 			    (SATA_EVNT_LINK_ESTABLISHED |
15211 			    SATA_EVNT_LINK_LOST)) {
15212 				sata_process_port_link_events(sata_hba_inst,
15213 				    saddr);
15214 			}
15215 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
15216 				sata_process_port_pwr_change(sata_hba_inst,
15217 				    saddr);
15218 			}
15219 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
15220 				sata_process_target_node_cleanup(
15221 				    sata_hba_inst, saddr);
15222 			}
15223 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
15224 				sata_process_device_autoonline(
15225 				    sata_hba_inst, saddr);
15226 			}
15227 		}
15228 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15229 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
15230 		    SATA_DTYPE_NONE) &&
15231 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
15232 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
15233 			    satadrv_event_flags &
15234 			    (SATA_EVNT_DEVICE_RESET |
15235 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
15236 				/* Have device event */
15237 				sata_process_device_reset(sata_hba_inst,
15238 				    saddr);
15239 			}
15240 		}
15241 		/* Release PORT_BUSY flag */
15242 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
15243 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
15244 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
15245 
15246 	} /* End of loop through the controller SATA ports */
15247 }
15248 
15249 /*
15250  * Process HBA power level change reported by HBA driver.
15251  * Not implemented at this time - event is ignored.
15252  */
15253 static void
15254 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
15255 {
15256 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15257 	    "Processing controller power level change", NULL);
15258 
15259 	/* Ignoring it for now */
15260 	mutex_enter(&sata_hba_inst->satahba_mutex);
15261 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
15262 	mutex_exit(&sata_hba_inst->satahba_mutex);
15263 }
15264 
15265 /*
15266  * Process port power level change reported by HBA driver.
15267  * Not implemented at this time - event is ignored.
15268  */
15269 static void
15270 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
15271     sata_address_t *saddr)
15272 {
15273 	sata_cport_info_t *cportinfo;
15274 
15275 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15276 	    "Processing port power level change", NULL);
15277 
15278 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15279 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15280 	/* Reset event flag */
15281 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
15282 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15283 }
15284 
15285 /*
15286  * Process port failure reported by HBA driver.
15287  * cports support only - no pmports.
15288  */
15289 static void
15290 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
15291     sata_address_t *saddr)
15292 {
15293 	sata_cport_info_t *cportinfo;
15294 
15295 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15296 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15297 	/* Reset event flag first */
15298 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
15299 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
15300 	if ((cportinfo->cport_state &
15301 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
15302 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15303 		    cport_mutex);
15304 		return;
15305 	}
15306 	/* Fail the port */
15307 	cportinfo->cport_state = SATA_PSTATE_FAILED;
15308 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15309 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
15310 }
15311 
15312 /*
15313  * Device Reset Event processing.
15314  * The seqeunce is managed by 3 stage flags:
15315  * - reset event reported,
15316  * - reset event being processed,
15317  * - request to clear device reset state.
15318  *
15319  * NOTE: This function has to be entered with cport mutex held. It exits with
15320  * mutex held as well, but can release mutex during the processing.
15321  */
15322 static void
15323 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
15324     sata_address_t *saddr)
15325 {
15326 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
15327 	sata_drive_info_t *sdinfo;
15328 	sata_cport_info_t *cportinfo;
15329 	sata_device_t sata_device;
15330 	int rval_probe, rval_set;
15331 
15332 	/* We only care about host sata cport for now */
15333 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15334 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15335 	/*
15336 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
15337 	 * state, ignore reset event.
15338 	 */
15339 	if (((cportinfo->cport_state &
15340 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
15341 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
15342 		sdinfo->satadrv_event_flags &=
15343 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
15344 		return;
15345 	}
15346 
15347 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
15348 	    SATA_VALID_DEV_TYPE) == 0) {
15349 		/*
15350 		 * This should not happen - coding error.
15351 		 * But we can recover, so do not panic, just clean up
15352 		 * and if in debug mode, log the message.
15353 		 */
15354 #ifdef SATA_DEBUG
15355 		sata_log(sata_hba_inst, CE_WARN,
15356 		    "sata_process_device_reset: "
15357 		    "Invalid device type with sdinfo!", NULL);
15358 #endif
15359 		sdinfo->satadrv_event_flags = 0;
15360 		return;
15361 	}
15362 
15363 #ifdef SATA_DEBUG
15364 	if ((sdinfo->satadrv_event_flags &
15365 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
15366 		/* Nothing to do */
15367 		/* Something is weird - why we are processing dev reset? */
15368 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15369 		    "No device reset event!!!!", NULL);
15370 
15371 		return;
15372 	}
15373 	if ((sdinfo->satadrv_event_flags &
15374 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
15375 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15376 		/* Something is weird - new device reset event */
15377 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15378 		    "Overlapping device reset events!", NULL);
15379 	}
15380 #endif
15381 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15382 	    "Processing port %d device reset", saddr->cport);
15383 
15384 	/* Clear event flag */
15385 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
15386 
15387 	/* It seems that we always need to check the port state first */
15388 	sata_device.satadev_rev = SATA_DEVICE_REV;
15389 	sata_device.satadev_addr = *saddr;
15390 	/*
15391 	 * We have to exit mutex, because the HBA probe port function may
15392 	 * block on its own mutex.
15393 	 */
15394 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15395 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15396 	    (SATA_DIP(sata_hba_inst), &sata_device);
15397 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15398 	sata_update_port_info(sata_hba_inst, &sata_device);
15399 	if (rval_probe != SATA_SUCCESS) {
15400 		/* Something went wrong? Fail the port */
15401 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15402 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15403 		if (sdinfo != NULL)
15404 			sdinfo->satadrv_event_flags = 0;
15405 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15406 		    cport_mutex);
15407 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15408 		    "SATA port %d probing failed",
15409 		    saddr->cport));
15410 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15411 		    saddr->cport)->cport_mutex);
15412 		return;
15413 	}
15414 	if ((sata_device.satadev_scr.sstatus  &
15415 	    SATA_PORT_DEVLINK_UP_MASK) !=
15416 	    SATA_PORT_DEVLINK_UP ||
15417 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
15418 		/*
15419 		 * No device to process, anymore. Some other event processing
15420 		 * would or have already performed port info cleanup.
15421 		 * To be safe (HBA may need it), request clearing device
15422 		 * reset condition.
15423 		 */
15424 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15425 		if (sdinfo != NULL) {
15426 			sdinfo->satadrv_event_flags &=
15427 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
15428 			sdinfo->satadrv_event_flags |=
15429 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15430 		}
15431 		return;
15432 	}
15433 
15434 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
15435 	if (sdinfo == NULL) {
15436 		return;
15437 	}
15438 	if ((sdinfo->satadrv_event_flags &
15439 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
15440 		/*
15441 		 * Start tracking time for device feature restoration and
15442 		 * identification. Save current time (lbolt value).
15443 		 */
15444 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
15445 	}
15446 	/* Mark device reset processing as active */
15447 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
15448 
15449 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
15450 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15451 
15452 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
15453 
15454 	if (rval_set  != SATA_SUCCESS) {
15455 		/*
15456 		 * Restoring drive setting failed.
15457 		 * Probe the port first, to check if the port state has changed
15458 		 */
15459 		sata_device.satadev_rev = SATA_DEVICE_REV;
15460 		sata_device.satadev_addr = *saddr;
15461 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
15462 		/* probe port */
15463 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15464 		    (SATA_DIP(sata_hba_inst), &sata_device);
15465 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15466 		    cport_mutex);
15467 		if (rval_probe == SATA_SUCCESS &&
15468 		    (sata_device.satadev_state &
15469 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
15470 		    (sata_device.satadev_scr.sstatus  &
15471 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
15472 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
15473 			/*
15474 			 * We may retry this a bit later - in-process reset
15475 			 * condition should be already set.
15476 			 * Track retry time for device identification.
15477 			 */
15478 			if ((cportinfo->cport_dev_type &
15479 			    SATA_VALID_DEV_TYPE) != 0 &&
15480 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
15481 			    sdinfo->satadrv_reset_time != 0) {
15482 				clock_t cur_time = ddi_get_lbolt();
15483 				/*
15484 				 * If the retry time limit was not
15485 				 * exceeded, retry.
15486 				 */
15487 				if ((cur_time - sdinfo->satadrv_reset_time) <
15488 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
15489 					mutex_enter(
15490 					    &sata_hba_inst->satahba_mutex);
15491 					sata_hba_inst->satahba_event_flags |=
15492 					    SATA_EVNT_MAIN;
15493 					mutex_exit(
15494 					    &sata_hba_inst->satahba_mutex);
15495 					mutex_enter(&sata_mutex);
15496 					sata_event_pending |= SATA_EVNT_MAIN;
15497 					mutex_exit(&sata_mutex);
15498 					return;
15499 				}
15500 				if (rval_set == SATA_RETRY) {
15501 					/*
15502 					 * Setting drive features failed, but
15503 					 * the drive is still accessible,
15504 					 * so emit a warning message before
15505 					 * return.
15506 					 */
15507 					mutex_exit(&SATA_CPORT_INFO(
15508 					    sata_hba_inst,
15509 					    saddr->cport)->cport_mutex);
15510 					goto done;
15511 				}
15512 			}
15513 			/* Fail the drive */
15514 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
15515 
15516 			sata_log(sata_hba_inst, CE_WARN,
15517 			    "SATA device at port %d - device failed",
15518 			    saddr->cport);
15519 		}
15520 		/*
15521 		 * No point of retrying - device failed or some other event
15522 		 * processing or already did or will do port info cleanup.
15523 		 * To be safe (HBA may need it),
15524 		 * request clearing device reset condition.
15525 		 */
15526 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15527 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
15528 		sdinfo->satadrv_reset_time = 0;
15529 		return;
15530 	}
15531 done:
15532 	/*
15533 	 * If setting of drive features failed, but the drive is still
15534 	 * accessible, emit a warning message.
15535 	 */
15536 	if (rval_set == SATA_RETRY) {
15537 		sata_log(sata_hba_inst, CE_WARN,
15538 		    "SATA device at port %d - desired setting could not be "
15539 		    "restored after reset. Device may not operate as expected.",
15540 		    saddr->cport);
15541 	}
15542 	/*
15543 	 * Raise the flag indicating that the next sata command could
15544 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
15545 	 * reset is reported.
15546 	 */
15547 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15548 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15549 		sdinfo->satadrv_reset_time = 0;
15550 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
15551 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15552 			sdinfo->satadrv_event_flags &=
15553 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
15554 			sdinfo->satadrv_event_flags |=
15555 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15556 		}
15557 	}
15558 }
15559 
15560 
15561 /*
15562  * Port Link Events processing.
15563  * Every link established event may involve device reset (due to
15564  * COMRESET signal, equivalent of the hard reset) so arbitrarily
15565  * set device reset event for an attached device (if any).
15566  * If the port is in SHUTDOWN or FAILED state, ignore link events.
15567  *
15568  * The link established event processing varies, depending on the state
15569  * of the target node, HBA hotplugging capabilities, state of the port.
15570  * If the link is not active, the link established event is ignored.
15571  * If HBA cannot detect device attachment and there is no target node,
15572  * the link established event triggers device attach event processing.
15573  * Else, link established event triggers device reset event processing.
15574  *
15575  * The link lost event processing varies, depending on a HBA hotplugging
15576  * capability and the state of the port (link active or not active).
15577  * If the link is active, the lost link event is ignored.
15578  * If HBA cannot detect device removal, the lost link event triggers
15579  * device detached event processing after link lost timeout.
15580  * Else, the event is ignored.
15581  *
15582  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
15583  */
15584 static void
15585 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
15586     sata_address_t *saddr)
15587 {
15588 	sata_device_t sata_device;
15589 	sata_cport_info_t *cportinfo;
15590 	sata_drive_info_t *sdinfo;
15591 	uint32_t event_flags;
15592 	int rval;
15593 
15594 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15595 	    "Processing port %d link event(s)", saddr->cport);
15596 
15597 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15598 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15599 	event_flags = cportinfo->cport_event_flags;
15600 
15601 	/* Reset event flags first */
15602 	cportinfo->cport_event_flags &=
15603 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
15604 
15605 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
15606 	if ((cportinfo->cport_state &
15607 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15608 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15609 		    cport_mutex);
15610 		return;
15611 	}
15612 
15613 	/*
15614 	 * For the sanity sake get current port state.
15615 	 * Set device address only. Other sata_device fields should be
15616 	 * set by HBA driver.
15617 	 */
15618 	sata_device.satadev_rev = SATA_DEVICE_REV;
15619 	sata_device.satadev_addr = *saddr;
15620 	/*
15621 	 * We have to exit mutex, because the HBA probe port function may
15622 	 * block on its own mutex.
15623 	 */
15624 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15625 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15626 	    (SATA_DIP(sata_hba_inst), &sata_device);
15627 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15628 	sata_update_port_info(sata_hba_inst, &sata_device);
15629 	if (rval != SATA_SUCCESS) {
15630 		/* Something went wrong? Fail the port */
15631 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15632 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15633 		    cport_mutex);
15634 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15635 		    "SATA port %d probing failed",
15636 		    saddr->cport));
15637 		/*
15638 		 * We may want to release device info structure, but
15639 		 * it is not necessary.
15640 		 */
15641 		return;
15642 	} else {
15643 		/* port probed successfully */
15644 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15645 	}
15646 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
15647 
15648 		if ((sata_device.satadev_scr.sstatus &
15649 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
15650 			/* Ignore event */
15651 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15652 			    "Ignoring port %d link established event - "
15653 			    "link down",
15654 			    saddr->cport);
15655 			goto linklost;
15656 		}
15657 
15658 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15659 		    "Processing port %d link established event",
15660 		    saddr->cport);
15661 
15662 		/*
15663 		 * For the sanity sake check if a device is attached - check
15664 		 * return state of a port probing.
15665 		 */
15666 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
15667 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
15668 			/*
15669 			 * HBA port probe indicated that there is a device
15670 			 * attached. Check if the framework had device info
15671 			 * structure attached for this device.
15672 			 */
15673 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15674 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
15675 				    NULL);
15676 
15677 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15678 				if ((sdinfo->satadrv_type &
15679 				    SATA_VALID_DEV_TYPE) != 0) {
15680 					/*
15681 					 * Dev info structure is present.
15682 					 * If dev_type is set to known type in
15683 					 * the framework's drive info struct
15684 					 * then the device existed before and
15685 					 * the link was probably lost
15686 					 * momentarily - in such case
15687 					 * we may want to check device
15688 					 * identity.
15689 					 * Identity check is not supported now.
15690 					 *
15691 					 * Link established event
15692 					 * triggers device reset event.
15693 					 */
15694 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
15695 					    satadrv_event_flags |=
15696 					    SATA_EVNT_DEVICE_RESET;
15697 				}
15698 			} else if (cportinfo->cport_dev_type ==
15699 			    SATA_DTYPE_NONE) {
15700 				/*
15701 				 * We got new device attached! If HBA does not
15702 				 * generate device attached events, trigger it
15703 				 * here.
15704 				 */
15705 				if (!(SATA_FEATURES(sata_hba_inst) &
15706 				    SATA_CTLF_HOTPLUG)) {
15707 					cportinfo->cport_event_flags |=
15708 					    SATA_EVNT_DEVICE_ATTACHED;
15709 				}
15710 			}
15711 			/* Reset link lost timeout */
15712 			cportinfo->cport_link_lost_time = 0;
15713 		}
15714 	}
15715 linklost:
15716 	if (event_flags & SATA_EVNT_LINK_LOST) {
15717 		if ((sata_device.satadev_scr.sstatus &
15718 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
15719 			/* Ignore event */
15720 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15721 			    "Ignoring port %d link lost event - link is up",
15722 			    saddr->cport);
15723 			goto done;
15724 		}
15725 #ifdef SATA_DEBUG
15726 		if (cportinfo->cport_link_lost_time == 0) {
15727 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15728 			    "Processing port %d link lost event",
15729 			    saddr->cport);
15730 		}
15731 #endif
15732 		/*
15733 		 * When HBA cannot generate device attached/detached events,
15734 		 * we need to track link lost time and eventually generate
15735 		 * device detach event.
15736 		 */
15737 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
15738 			/* We are tracking link lost time */
15739 			if (cportinfo->cport_link_lost_time == 0) {
15740 				/* save current time (lbolt value) */
15741 				cportinfo->cport_link_lost_time =
15742 				    ddi_get_lbolt();
15743 				/* just keep link lost event */
15744 				cportinfo->cport_event_flags |=
15745 				    SATA_EVNT_LINK_LOST;
15746 			} else {
15747 				clock_t cur_time = ddi_get_lbolt();
15748 				if ((cur_time -
15749 				    cportinfo->cport_link_lost_time) >=
15750 				    drv_usectohz(
15751 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
15752 					/* trigger device detach event */
15753 					cportinfo->cport_event_flags |=
15754 					    SATA_EVNT_DEVICE_DETACHED;
15755 					cportinfo->cport_link_lost_time = 0;
15756 					SATADBG1(SATA_DBG_EVENTS,
15757 					    sata_hba_inst,
15758 					    "Triggering port %d "
15759 					    "device detached event",
15760 					    saddr->cport);
15761 				} else {
15762 					/* keep link lost event */
15763 					cportinfo->cport_event_flags |=
15764 					    SATA_EVNT_LINK_LOST;
15765 				}
15766 			}
15767 		}
15768 		/*
15769 		 * We could change port state to disable/delay access to
15770 		 * the attached device until the link is recovered.
15771 		 */
15772 	}
15773 done:
15774 	event_flags = cportinfo->cport_event_flags;
15775 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15776 	if (event_flags != 0) {
15777 		mutex_enter(&sata_hba_inst->satahba_mutex);
15778 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15779 		mutex_exit(&sata_hba_inst->satahba_mutex);
15780 		mutex_enter(&sata_mutex);
15781 		sata_event_pending |= SATA_EVNT_MAIN;
15782 		mutex_exit(&sata_mutex);
15783 	}
15784 }
15785 
15786 /*
15787  * Device Detached Event processing.
15788  * Port is probed to find if a device is really gone. If so,
15789  * the device info structure is detached from the SATA port info structure
15790  * and released.
15791  * Port status is updated.
15792  *
15793  * NOTE: Process cports event only, no port multiplier ports.
15794  */
15795 static void
15796 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
15797     sata_address_t *saddr)
15798 {
15799 	sata_cport_info_t *cportinfo;
15800 	sata_drive_info_t *sdevinfo;
15801 	sata_device_t sata_device;
15802 	dev_info_t *tdip;
15803 	int rval;
15804 
15805 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15806 	    "Processing port %d device detached", saddr->cport);
15807 
15808 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15809 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15810 	/* Clear event flag */
15811 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
15812 
15813 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
15814 	if ((cportinfo->cport_state &
15815 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15816 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15817 		    cport_mutex);
15818 		return;
15819 	}
15820 	/* For sanity, re-probe the port */
15821 	sata_device.satadev_rev = SATA_DEVICE_REV;
15822 	sata_device.satadev_addr = *saddr;
15823 
15824 	/*
15825 	 * We have to exit mutex, because the HBA probe port function may
15826 	 * block on its own mutex.
15827 	 */
15828 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15829 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15830 	    (SATA_DIP(sata_hba_inst), &sata_device);
15831 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15832 	sata_update_port_info(sata_hba_inst, &sata_device);
15833 	if (rval != SATA_SUCCESS) {
15834 		/* Something went wrong? Fail the port */
15835 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15836 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15837 		    cport_mutex);
15838 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15839 		    "SATA port %d probing failed",
15840 		    saddr->cport));
15841 		/*
15842 		 * We may want to release device info structure, but
15843 		 * it is not necessary.
15844 		 */
15845 		return;
15846 	} else {
15847 		/* port probed successfully */
15848 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15849 	}
15850 	/*
15851 	 * Check if a device is still attached. For sanity, check also
15852 	 * link status - if no link, there is no device.
15853 	 */
15854 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
15855 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
15856 	    SATA_DTYPE_NONE) {
15857 		/*
15858 		 * Device is still attached - ignore detach event.
15859 		 */
15860 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15861 		    cport_mutex);
15862 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15863 		    "Ignoring detach - device still attached to port %d",
15864 		    sata_device.satadev_addr.cport);
15865 		return;
15866 	}
15867 	/*
15868 	 * We need to detach and release device info structure here
15869 	 */
15870 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15871 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15872 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15873 		(void) kmem_free((void *)sdevinfo,
15874 		    sizeof (sata_drive_info_t));
15875 	}
15876 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15877 	/*
15878 	 * Device cannot be reached anymore, even if the target node may be
15879 	 * still present.
15880 	 */
15881 
15882 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15883 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
15884 	    sata_device.satadev_addr.cport);
15885 
15886 	/*
15887 	 * Try to offline a device and remove target node if it still exists
15888 	 */
15889 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15890 	if (tdip != NULL) {
15891 		/*
15892 		 * Target node exists.  Unconfigure device then remove
15893 		 * the target node (one ndi operation).
15894 		 */
15895 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
15896 			/*
15897 			 * PROBLEM - no device, but target node remained
15898 			 * This happens when the file was open or node was
15899 			 * waiting for resources.
15900 			 */
15901 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15902 			    "sata_process_device_detached: "
15903 			    "Failed to remove target node for "
15904 			    "detached SATA device."));
15905 			/*
15906 			 * Set target node state to DEVI_DEVICE_REMOVED.
15907 			 * But re-check first that the node still exists.
15908 			 */
15909 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15910 			    saddr->cport);
15911 			if (tdip != NULL) {
15912 				sata_set_device_removed(tdip);
15913 				/*
15914 				 * Instruct event daemon to retry the
15915 				 * cleanup later.
15916 				 */
15917 				sata_set_target_node_cleanup(sata_hba_inst,
15918 				    &sata_device.satadev_addr);
15919 			}
15920 		}
15921 	}
15922 	/*
15923 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15924 	 * with the hint: SE_HINT_REMOVE
15925 	 */
15926 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
15927 }
15928 
15929 
15930 /*
15931  * Device Attached Event processing.
15932  * Port state is checked to verify that a device is really attached. If so,
15933  * the device info structure is created and attached to the SATA port info
15934  * structure.
15935  *
15936  * If attached device cannot be identified or set-up, the retry for the
15937  * attach processing is set-up. Subsequent daemon run would try again to
15938  * identify the device, until the time limit is reached
15939  * (SATA_DEV_IDENTIFY_TIMEOUT).
15940  *
15941  * This function cannot be called in interrupt context (it may sleep).
15942  *
15943  * NOTE: Process cports event only, no port multiplier ports.
15944  */
15945 static void
15946 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
15947     sata_address_t *saddr)
15948 {
15949 	sata_cport_info_t *cportinfo;
15950 	sata_drive_info_t *sdevinfo;
15951 	sata_device_t sata_device;
15952 	dev_info_t *tdip;
15953 	uint32_t event_flags;
15954 	int rval;
15955 
15956 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15957 	    "Processing port %d device attached", saddr->cport);
15958 
15959 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15960 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15961 
15962 	/* Clear attach event flag first */
15963 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
15964 
15965 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
15966 	if ((cportinfo->cport_state &
15967 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15968 		cportinfo->cport_dev_attach_time = 0;
15969 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15970 		    cport_mutex);
15971 		return;
15972 	}
15973 
15974 	/*
15975 	 * If the sata_drive_info structure is found attached to the port info,
15976 	 * despite the fact the device was removed and now it is re-attached,
15977 	 * the old drive info structure was not removed.
15978 	 * Arbitrarily release device info structure.
15979 	 */
15980 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15981 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15982 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15983 		(void) kmem_free((void *)sdevinfo,
15984 		    sizeof (sata_drive_info_t));
15985 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15986 		    "Arbitrarily detaching old device info.", NULL);
15987 	}
15988 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15989 
15990 	/* For sanity, re-probe the port */
15991 	sata_device.satadev_rev = SATA_DEVICE_REV;
15992 	sata_device.satadev_addr = *saddr;
15993 
15994 	/*
15995 	 * We have to exit mutex, because the HBA probe port function may
15996 	 * block on its own mutex.
15997 	 */
15998 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15999 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
16000 	    (SATA_DIP(sata_hba_inst), &sata_device);
16001 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16002 	sata_update_port_info(sata_hba_inst, &sata_device);
16003 	if (rval != SATA_SUCCESS) {
16004 		/* Something went wrong? Fail the port */
16005 		cportinfo->cport_state = SATA_PSTATE_FAILED;
16006 		cportinfo->cport_dev_attach_time = 0;
16007 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16008 		    cport_mutex);
16009 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16010 		    "SATA port %d probing failed",
16011 		    saddr->cport));
16012 		return;
16013 	} else {
16014 		/* port probed successfully */
16015 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
16016 	}
16017 	/*
16018 	 * Check if a device is still attached. For sanity, check also
16019 	 * link status - if no link, there is no device.
16020 	 */
16021 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
16022 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
16023 	    SATA_DTYPE_NONE) {
16024 		/*
16025 		 * No device - ignore attach event.
16026 		 */
16027 		cportinfo->cport_dev_attach_time = 0;
16028 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16029 		    cport_mutex);
16030 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16031 		    "Ignoring attach - no device connected to port %d",
16032 		    sata_device.satadev_addr.cport);
16033 		return;
16034 	}
16035 
16036 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16037 	/*
16038 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
16039 	 * with the hint: SE_HINT_INSERT
16040 	 */
16041 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
16042 
16043 	/*
16044 	 * Port reprobing will take care of the creation of the device
16045 	 * info structure and determination of the device type.
16046 	 */
16047 	sata_device.satadev_addr = *saddr;
16048 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
16049 	    SATA_DEV_IDENTIFY_NORETRY);
16050 
16051 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
16052 	    cport_mutex);
16053 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
16054 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
16055 		/* Some device is attached to the port */
16056 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
16057 			/*
16058 			 * A device was not successfully attached.
16059 			 * Track retry time for device identification.
16060 			 */
16061 			if (cportinfo->cport_dev_attach_time != 0) {
16062 				clock_t cur_time = ddi_get_lbolt();
16063 				/*
16064 				 * If the retry time limit was not exceeded,
16065 				 * reinstate attach event.
16066 				 */
16067 				if ((cur_time -
16068 				    cportinfo->cport_dev_attach_time) <
16069 				    drv_usectohz(
16070 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
16071 					/* OK, restore attach event */
16072 					cportinfo->cport_event_flags |=
16073 					    SATA_EVNT_DEVICE_ATTACHED;
16074 				} else {
16075 					/* Timeout - cannot identify device */
16076 					cportinfo->cport_dev_attach_time = 0;
16077 					sata_log(sata_hba_inst,
16078 					    CE_WARN,
16079 					    "Could not identify SATA device "
16080 					    "at port %d",
16081 					    saddr->cport);
16082 				}
16083 			} else {
16084 				/*
16085 				 * Start tracking time for device
16086 				 * identification.
16087 				 * Save current time (lbolt value).
16088 				 */
16089 				cportinfo->cport_dev_attach_time =
16090 				    ddi_get_lbolt();
16091 				/* Restore attach event */
16092 				cportinfo->cport_event_flags |=
16093 				    SATA_EVNT_DEVICE_ATTACHED;
16094 			}
16095 		} else {
16096 			/*
16097 			 * If device was successfully attached, the subsequent
16098 			 * action depends on a state of the
16099 			 * sata_auto_online variable. If it is set to zero.
16100 			 * an explicit 'configure' command will be needed to
16101 			 * configure it. If its value is non-zero, we will
16102 			 * attempt to online (configure) the device.
16103 			 * First, log the message indicating that a device
16104 			 * was attached.
16105 			 */
16106 			cportinfo->cport_dev_attach_time = 0;
16107 			sata_log(sata_hba_inst, CE_WARN,
16108 			    "SATA device detected at port %d", saddr->cport);
16109 
16110 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
16111 				sata_drive_info_t new_sdinfo;
16112 
16113 				/* Log device info data */
16114 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
16115 				    cportinfo));
16116 				sata_show_drive_info(sata_hba_inst,
16117 				    &new_sdinfo);
16118 			}
16119 
16120 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16121 			    saddr->cport)->cport_mutex);
16122 
16123 			/*
16124 			 * Make sure that there is no target node for that
16125 			 * device. If so, release it. It should not happen,
16126 			 * unless we had problem removing the node when
16127 			 * device was detached.
16128 			 */
16129 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
16130 			    saddr->cport);
16131 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16132 			    saddr->cport)->cport_mutex);
16133 			if (tdip != NULL) {
16134 
16135 #ifdef SATA_DEBUG
16136 				if ((cportinfo->cport_event_flags &
16137 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
16138 					sata_log(sata_hba_inst, CE_WARN,
16139 					    "sata_process_device_attached: "
16140 					    "old device target node exists!");
16141 #endif
16142 				/*
16143 				 * target node exists - try to unconfigure
16144 				 * device and remove the node.
16145 				 */
16146 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16147 				    saddr->cport)->cport_mutex);
16148 				rval = ndi_devi_offline(tdip,
16149 				    NDI_DEVI_REMOVE);
16150 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16151 				    saddr->cport)->cport_mutex);
16152 
16153 				if (rval == NDI_SUCCESS) {
16154 					cportinfo->cport_event_flags &=
16155 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16156 					cportinfo->cport_tgtnode_clean = B_TRUE;
16157 				} else {
16158 					/*
16159 					 * PROBLEM - the target node remained
16160 					 * and it belongs to a previously
16161 					 * attached device.
16162 					 * This happens when the file was open
16163 					 * or the node was waiting for
16164 					 * resources at the time the
16165 					 * associated device was removed.
16166 					 * Instruct event daemon to retry the
16167 					 * cleanup later.
16168 					 */
16169 					sata_log(sata_hba_inst,
16170 					    CE_WARN,
16171 					    "Application(s) accessing "
16172 					    "previously attached SATA "
16173 					    "device have to release "
16174 					    "it before newly inserted "
16175 					    "device can be made accessible.",
16176 					    saddr->cport);
16177 					cportinfo->cport_event_flags |=
16178 					    SATA_EVNT_TARGET_NODE_CLEANUP;
16179 					cportinfo->cport_tgtnode_clean =
16180 					    B_FALSE;
16181 				}
16182 			}
16183 			if (sata_auto_online != 0) {
16184 				cportinfo->cport_event_flags |=
16185 				    SATA_EVNT_AUTOONLINE_DEVICE;
16186 			}
16187 
16188 		}
16189 	} else {
16190 		cportinfo->cport_dev_attach_time = 0;
16191 	}
16192 
16193 	event_flags = cportinfo->cport_event_flags;
16194 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16195 	if (event_flags != 0) {
16196 		mutex_enter(&sata_hba_inst->satahba_mutex);
16197 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16198 		mutex_exit(&sata_hba_inst->satahba_mutex);
16199 		mutex_enter(&sata_mutex);
16200 		sata_event_pending |= SATA_EVNT_MAIN;
16201 		mutex_exit(&sata_mutex);
16202 	}
16203 }
16204 
16205 
16206 /*
16207  * Device Target Node Cleanup Event processing.
16208  * If the target node associated with a sata port device is in
16209  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
16210  * If the target node cannot be removed, the event flag is left intact,
16211  * so that event daemon may re-run this function later.
16212  *
16213  * This function cannot be called in interrupt context (it may sleep).
16214  *
16215  * NOTE: Processes cport events only, not port multiplier ports.
16216  */
16217 static void
16218 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
16219     sata_address_t *saddr)
16220 {
16221 	sata_cport_info_t *cportinfo;
16222 	dev_info_t *tdip;
16223 
16224 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16225 	    "Processing port %d device target node cleanup", saddr->cport);
16226 
16227 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
16228 
16229 	/*
16230 	 * Check if there is target node for that device and it is in the
16231 	 * DEVI_DEVICE_REMOVED state. If so, release it.
16232 	 */
16233 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
16234 	if (tdip != NULL) {
16235 		/*
16236 		 * target node exists - check if it is target node of
16237 		 * a removed device.
16238 		 */
16239 		if (sata_check_device_removed(tdip) == B_TRUE) {
16240 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16241 			    "sata_process_target_node_cleanup: "
16242 			    "old device target node exists!", NULL);
16243 			/*
16244 			 * Unconfigure and remove the target node
16245 			 */
16246 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
16247 			    NDI_SUCCESS) {
16248 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16249 				    saddr->cport)->cport_mutex);
16250 				cportinfo->cport_event_flags &=
16251 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16252 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16253 				    saddr->cport)->cport_mutex);
16254 				return;
16255 			}
16256 			/*
16257 			 * Event daemon will retry the cleanup later.
16258 			 */
16259 			mutex_enter(&sata_hba_inst->satahba_mutex);
16260 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16261 			mutex_exit(&sata_hba_inst->satahba_mutex);
16262 			mutex_enter(&sata_mutex);
16263 			sata_event_pending |= SATA_EVNT_MAIN;
16264 			mutex_exit(&sata_mutex);
16265 		}
16266 	} else {
16267 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16268 		    saddr->cport)->cport_mutex);
16269 		cportinfo->cport_event_flags &=
16270 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
16271 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16272 		    saddr->cport)->cport_mutex);
16273 	}
16274 }
16275 
16276 /*
16277  * Device AutoOnline Event processing.
16278  * If attached device is to be onlined, an attempt is made to online this
16279  * device, but only if there is no lingering (old) target node present.
16280  * If the device cannot be onlined, the event flag is left intact,
16281  * so that event daemon may re-run this function later.
16282  *
16283  * This function cannot be called in interrupt context (it may sleep).
16284  *
16285  * NOTE: Processes cport events only, not port multiplier ports.
16286  */
16287 static void
16288 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
16289     sata_address_t *saddr)
16290 {
16291 	sata_cport_info_t *cportinfo;
16292 	sata_drive_info_t *sdinfo;
16293 	sata_device_t sata_device;
16294 	dev_info_t *tdip;
16295 
16296 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16297 	    "Processing port %d attached device auto-onlining", saddr->cport);
16298 
16299 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
16300 
16301 	/*
16302 	 * Check if device is present and recognized. If not, reset event.
16303 	 */
16304 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16305 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
16306 		/* Nothing to online */
16307 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
16308 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16309 		    saddr->cport)->cport_mutex);
16310 		return;
16311 	}
16312 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16313 
16314 	/*
16315 	 * Check if there is target node for this device and if it is in the
16316 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
16317 	 * the event for later processing.
16318 	 */
16319 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
16320 	if (tdip != NULL) {
16321 		/*
16322 		 * target node exists - check if it is target node of
16323 		 * a removed device.
16324 		 */
16325 		if (sata_check_device_removed(tdip) == B_TRUE) {
16326 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
16327 			    "sata_process_device_autoonline: "
16328 			    "old device target node exists!", NULL);
16329 			/*
16330 			 * Event daemon will retry device onlining later.
16331 			 */
16332 			mutex_enter(&sata_hba_inst->satahba_mutex);
16333 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16334 			mutex_exit(&sata_hba_inst->satahba_mutex);
16335 			mutex_enter(&sata_mutex);
16336 			sata_event_pending |= SATA_EVNT_MAIN;
16337 			mutex_exit(&sata_mutex);
16338 			return;
16339 		}
16340 		/*
16341 		 * If the target node is not in the 'removed" state, assume
16342 		 * that it belongs to this device. There is nothing more to do,
16343 		 * but reset the event.
16344 		 */
16345 	} else {
16346 
16347 		/*
16348 		 * Try to online the device
16349 		 * If there is any reset-related event, remove it. We are
16350 		 * configuring the device and no state restoring is needed.
16351 		 */
16352 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16353 		    saddr->cport)->cport_mutex);
16354 		sata_device.satadev_addr = *saddr;
16355 		if (saddr->qual == SATA_ADDR_CPORT)
16356 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
16357 		else
16358 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
16359 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
16360 		if (sdinfo != NULL) {
16361 			if (sdinfo->satadrv_event_flags &
16362 			    (SATA_EVNT_DEVICE_RESET |
16363 			    SATA_EVNT_INPROC_DEVICE_RESET))
16364 				sdinfo->satadrv_event_flags = 0;
16365 			sdinfo->satadrv_event_flags |=
16366 			    SATA_EVNT_CLEAR_DEVICE_RESET;
16367 
16368 			/* Need to create a new target node. */
16369 			cportinfo->cport_tgtnode_clean = B_TRUE;
16370 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16371 			    saddr->cport)->cport_mutex);
16372 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
16373 			    sata_hba_inst, &sata_device.satadev_addr);
16374 			if (tdip == NULL) {
16375 				/*
16376 				 * Configure (onlining) failed.
16377 				 * We will NOT retry
16378 				 */
16379 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16380 				    "sata_process_device_autoonline: "
16381 				    "configuring SATA device at port %d failed",
16382 				    saddr->cport));
16383 			}
16384 		} else {
16385 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16386 			    saddr->cport)->cport_mutex);
16387 		}
16388 
16389 	}
16390 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16391 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
16392 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16393 	    saddr->cport)->cport_mutex);
16394 }
16395 
16396 
16397 static void
16398 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
16399     int hint)
16400 {
16401 	char ap[MAXPATHLEN];
16402 	nvlist_t *ev_attr_list = NULL;
16403 	int err;
16404 
16405 	/* Allocate and build sysevent attribute list */
16406 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
16407 	if (err != 0) {
16408 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16409 		    "sata_gen_sysevent: "
16410 		    "cannot allocate memory for sysevent attributes\n"));
16411 		return;
16412 	}
16413 	/* Add hint attribute */
16414 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
16415 	if (err != 0) {
16416 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16417 		    "sata_gen_sysevent: "
16418 		    "failed to add DR_HINT attr for sysevent"));
16419 		nvlist_free(ev_attr_list);
16420 		return;
16421 	}
16422 	/*
16423 	 * Add AP attribute.
16424 	 * Get controller pathname and convert it into AP pathname by adding
16425 	 * a target number.
16426 	 */
16427 	(void) snprintf(ap, MAXPATHLEN, "/devices");
16428 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
16429 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
16430 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
16431 
16432 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
16433 	if (err != 0) {
16434 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16435 		    "sata_gen_sysevent: "
16436 		    "failed to add DR_AP_ID attr for sysevent"));
16437 		nvlist_free(ev_attr_list);
16438 		return;
16439 	}
16440 
16441 	/* Generate/log sysevent */
16442 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
16443 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
16444 	if (err != DDI_SUCCESS) {
16445 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16446 		    "sata_gen_sysevent: "
16447 		    "cannot log sysevent, err code %x\n", err));
16448 	}
16449 
16450 	nvlist_free(ev_attr_list);
16451 }
16452 
16453 
16454 
16455 
16456 /*
16457  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
16458  */
16459 static void
16460 sata_set_device_removed(dev_info_t *tdip)
16461 {
16462 	int circ;
16463 
16464 	ASSERT(tdip != NULL);
16465 
16466 	ndi_devi_enter(tdip, &circ);
16467 	mutex_enter(&DEVI(tdip)->devi_lock);
16468 	DEVI_SET_DEVICE_REMOVED(tdip);
16469 	mutex_exit(&DEVI(tdip)->devi_lock);
16470 	ndi_devi_exit(tdip, circ);
16471 }
16472 
16473 
16474 /*
16475  * Set internal event instructing event daemon to try
16476  * to perform the target node cleanup.
16477  */
16478 static void
16479 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
16480     sata_address_t *saddr)
16481 {
16482 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16483 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
16484 	    SATA_EVNT_TARGET_NODE_CLEANUP;
16485 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
16486 	    B_FALSE;
16487 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
16488 	mutex_enter(&sata_hba_inst->satahba_mutex);
16489 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16490 	mutex_exit(&sata_hba_inst->satahba_mutex);
16491 	mutex_enter(&sata_mutex);
16492 	sata_event_pending |= SATA_EVNT_MAIN;
16493 	mutex_exit(&sata_mutex);
16494 }
16495 
16496 
16497 /*
16498  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
16499  * i.e. check if the target node state indicates that it belongs to a removed
16500  * device.
16501  *
16502  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
16503  * B_FALSE otherwise.
16504  *
16505  * NOTE: No port multiplier support.
16506  */
16507 static boolean_t
16508 sata_check_device_removed(dev_info_t *tdip)
16509 {
16510 	ASSERT(tdip != NULL);
16511 
16512 	if (DEVI_IS_DEVICE_REMOVED(tdip))
16513 		return (B_TRUE);
16514 	else
16515 		return (B_FALSE);
16516 }
16517 
16518 /* ************************ FAULT INJECTTION **************************** */
16519 
16520 #ifdef SATA_INJECT_FAULTS
16521 
16522 static	uint32_t sata_fault_count = 0;
16523 static	uint32_t sata_fault_suspend_count = 0;
16524 
16525 /*
16526  * Inject sata pkt fault
16527  * It modifies returned values of the sata packet.
16528  * It returns immediately if:
16529  * pkt fault injection is not enabled (via sata_inject_fault,
16530  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
16531  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
16532  * pkt is not directed to specified fault controller/device
16533  * (sata_fault_ctrl_dev and sata_fault_device).
16534  * If fault controller is not specified, fault injection applies to all
16535  * controllers and devices.
16536  *
16537  * First argument is the pointer to the executed sata packet.
16538  * Second argument is a pointer to a value returned by the HBA tran_start
16539  * function.
16540  * Third argument specifies injected error. Injected sata packet faults
16541  * are the satapkt_reason values.
16542  * SATA_PKT_BUSY		-1	Not completed, busy
16543  * SATA_PKT_DEV_ERROR		1	Device reported error
16544  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
16545  * SATA_PKT_PORT_ERROR		3	Not completed, port error
16546  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
16547  * SATA_PKT_ABORTED		5	Aborted by request
16548  * SATA_PKT_TIMEOUT		6	Operation timeut
16549  * SATA_PKT_RESET		7	Aborted by reset request
16550  *
16551  * Additional global variables affecting the execution:
16552  *
16553  * sata_inject_fault_count variable specifies number of times in row the
16554  * error is injected. Value of -1 specifies permanent fault, ie. every time
16555  * the fault injection point is reached, the fault is injected and a pause
16556  * between fault injection specified by sata_inject_fault_pause_count is
16557  * ignored). Fault injection routine decrements sata_inject_fault_count
16558  * (if greater than zero) until it reaches 0. No fault is injected when
16559  * sata_inject_fault_count is 0 (zero).
16560  *
16561  * sata_inject_fault_pause_count variable specifies number of times a fault
16562  * injection is bypassed (pause between fault injections).
16563  * If set to 0, a fault is injected only a number of times specified by
16564  * sata_inject_fault_count.
16565  *
16566  * The fault counts are static, so for periodic errors they have to be manually
16567  * reset to start repetition sequence from scratch.
16568  * If the original value returned by the HBA tran_start function is not
16569  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
16570  * is injected (to avoid masking real problems);
16571  *
16572  * NOTE: In its current incarnation, this function should be invoked only for
16573  * commands executed in SYNCHRONOUS mode.
16574  */
16575 
16576 
16577 static void
16578 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
16579 {
16580 
16581 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
16582 		return;
16583 
16584 	if (sata_inject_fault_count == 0)
16585 		return;
16586 
16587 	if (fault == 0)
16588 		return;
16589 
16590 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
16591 		return;
16592 
16593 	if (sata_fault_ctrl != NULL) {
16594 		sata_pkt_txlate_t *spx =
16595 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
16596 
16597 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
16598 		    spx->txlt_sata_hba_inst->satahba_dip)
16599 			return;
16600 
16601 		if (sata_fault_device.satadev_addr.cport !=
16602 		    spkt->satapkt_device.satadev_addr.cport ||
16603 		    sata_fault_device.satadev_addr.pmport !=
16604 		    spkt->satapkt_device.satadev_addr.pmport ||
16605 		    sata_fault_device.satadev_addr.qual !=
16606 		    spkt->satapkt_device.satadev_addr.qual)
16607 			return;
16608 	}
16609 
16610 	/* Modify pkt return parameters */
16611 	if (*rval != SATA_TRAN_ACCEPTED ||
16612 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16613 		sata_fault_count = 0;
16614 		sata_fault_suspend_count = 0;
16615 		return;
16616 	}
16617 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
16618 		/* Pause in the injection */
16619 		sata_fault_suspend_count -= 1;
16620 		return;
16621 	}
16622 
16623 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
16624 		/*
16625 		 * Init inject fault cycle. If fault count is set to -1,
16626 		 * it is a permanent fault.
16627 		 */
16628 		if (sata_inject_fault_count != -1) {
16629 			sata_fault_count = sata_inject_fault_count;
16630 			sata_fault_suspend_count =
16631 			    sata_inject_fault_pause_count;
16632 			if (sata_fault_suspend_count == 0)
16633 				sata_inject_fault_count = 0;
16634 		}
16635 	}
16636 
16637 	if (sata_fault_count != 0)
16638 		sata_fault_count -= 1;
16639 
16640 	switch (fault) {
16641 	case SATA_PKT_BUSY:
16642 		*rval = SATA_TRAN_BUSY;
16643 		spkt->satapkt_reason = SATA_PKT_BUSY;
16644 		break;
16645 
16646 	case SATA_PKT_QUEUE_FULL:
16647 		*rval = SATA_TRAN_QUEUE_FULL;
16648 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
16649 		break;
16650 
16651 	case SATA_PKT_CMD_UNSUPPORTED:
16652 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
16653 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
16654 		break;
16655 
16656 	case SATA_PKT_PORT_ERROR:
16657 		/* This is "rejected" command */
16658 		*rval = SATA_TRAN_PORT_ERROR;
16659 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
16660 		/* Additional error setup could be done here - port state */
16661 		break;
16662 
16663 	case SATA_PKT_DEV_ERROR:
16664 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
16665 		/*
16666 		 * Additional error setup could be done here
16667 		 */
16668 		break;
16669 
16670 	case SATA_PKT_ABORTED:
16671 		spkt->satapkt_reason = SATA_PKT_ABORTED;
16672 		break;
16673 
16674 	case SATA_PKT_TIMEOUT:
16675 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
16676 		/* Additional error setup could be done here */
16677 		break;
16678 
16679 	case SATA_PKT_RESET:
16680 		spkt->satapkt_reason = SATA_PKT_RESET;
16681 		/*
16682 		 * Additional error setup could be done here - device reset
16683 		 */
16684 		break;
16685 
16686 	default:
16687 		break;
16688 	}
16689 }
16690 
16691 #endif
16692 
16693 /*
16694  * SATA Trace Ring Buffer
16695  * ----------------------
16696  *
16697  * Overview
16698  *
16699  * The SATA trace ring buffer is a ring buffer created and managed by
16700  * the SATA framework module that can be used by any module or driver
16701  * within the SATA framework to store debug messages.
16702  *
16703  * Ring Buffer Interfaces:
16704  *
16705  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
16706  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
16707  *
16708  *	Note that the sata_trace_debug() interface was created to give
16709  *	consumers the flexibilty of sending debug messages to ring buffer
16710  *	as variable arguments.  Consumers can send type va_list debug
16711  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
16712  *	and sata_vtrace_debug() relationship is similar to that of
16713  *	cmn_err(9F) and vcmn_err(9F).
16714  *
16715  * Below is a diagram of the SATA trace ring buffer interfaces and
16716  * sample consumers:
16717  *
16718  * +---------------------------------+
16719  * |    o  o  SATA Framework Module  |
16720  * | o  SATA  o     +------------------+      +------------------+
16721  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
16722  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
16723  * | o        o     +------------------+   |  +------------------+
16724  * |    o  o                ^        |     +--|SATA HBA Driver #2|
16725  * |                        |        |        +------------------+
16726  * |           +------------------+  |
16727  * |           |SATA Debug Message|  |
16728  * |           +------------------+  |
16729  * +---------------------------------+
16730  *
16731  * Supporting Routines:
16732  *
16733  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
16734  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
16735  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
16736  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
16737  *
16738  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
16739  * The ring buffer size can be adjusted by setting dmsg_ring_size in
16740  * /etc/system to desired size in unit of bytes.
16741  *
16742  * The individual debug message size in the ring buffer is restricted
16743  * to DMSG_BUF_SIZE.
16744  */
16745 void
16746 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
16747 {
16748 	sata_trace_dmsg_t *dmsg;
16749 
16750 	if (sata_debug_rbuf == NULL) {
16751 		return;
16752 	}
16753 
16754 	/*
16755 	 * If max size of ring buffer is smaller than size
16756 	 * required for one debug message then just return
16757 	 * since we have no room for the debug message.
16758 	 */
16759 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
16760 		return;
16761 	}
16762 
16763 	mutex_enter(&sata_debug_rbuf->lock);
16764 
16765 	/* alloc or reuse on ring buffer */
16766 	dmsg = sata_trace_dmsg_alloc();
16767 
16768 	if (dmsg == NULL) {
16769 		/* resource allocation failed */
16770 		mutex_exit(&sata_debug_rbuf->lock);
16771 		return;
16772 	}
16773 
16774 	dmsg->dip = dip;
16775 	gethrestime(&dmsg->timestamp);
16776 
16777 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
16778 
16779 	mutex_exit(&sata_debug_rbuf->lock);
16780 }
16781 
16782 void
16783 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
16784 {
16785 	va_list ap;
16786 
16787 	va_start(ap, fmt);
16788 	sata_vtrace_debug(dip, fmt, ap);
16789 	va_end(ap);
16790 }
16791 
16792 /*
16793  * This routine is used to manage debug messages
16794  * on ring buffer.
16795  */
16796 static sata_trace_dmsg_t *
16797 sata_trace_dmsg_alloc(void)
16798 {
16799 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
16800 
16801 	if (sata_debug_rbuf->looped == TRUE) {
16802 		sata_debug_rbuf->dmsgp = dmsg->next;
16803 		return (sata_debug_rbuf->dmsgp);
16804 	}
16805 
16806 	/*
16807 	 * If we're looping for the first time,
16808 	 * connect the ring.
16809 	 */
16810 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
16811 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
16812 		dmsg->next = sata_debug_rbuf->dmsgh;
16813 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
16814 		sata_debug_rbuf->looped = TRUE;
16815 		return (sata_debug_rbuf->dmsgp);
16816 	}
16817 
16818 	/* If we've gotten this far then memory allocation is needed */
16819 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
16820 	if (dmsg_alloc == NULL) {
16821 		sata_debug_rbuf->allocfailed++;
16822 		return (dmsg_alloc);
16823 	} else {
16824 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
16825 	}
16826 
16827 	if (sata_debug_rbuf->dmsgp != NULL) {
16828 		dmsg->next = dmsg_alloc;
16829 		sata_debug_rbuf->dmsgp = dmsg->next;
16830 		return (sata_debug_rbuf->dmsgp);
16831 	} else {
16832 		/*
16833 		 * We should only be here if we're initializing
16834 		 * the ring buffer.
16835 		 */
16836 		if (sata_debug_rbuf->dmsgh == NULL) {
16837 			sata_debug_rbuf->dmsgh = dmsg_alloc;
16838 		} else {
16839 			/* Something is wrong */
16840 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
16841 			return (NULL);
16842 		}
16843 
16844 		sata_debug_rbuf->dmsgp = dmsg_alloc;
16845 		return (sata_debug_rbuf->dmsgp);
16846 	}
16847 }
16848 
16849 
16850 /*
16851  * Free all messages on debug ring buffer.
16852  */
16853 static void
16854 sata_trace_dmsg_free(void)
16855 {
16856 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
16857 
16858 	while (dmsg != NULL) {
16859 		dmsg_next = dmsg->next;
16860 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
16861 
16862 		/*
16863 		 * If we've looped around the ring than we're done.
16864 		 */
16865 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
16866 			break;
16867 		} else {
16868 			dmsg = dmsg_next;
16869 		}
16870 	}
16871 }
16872 
16873 
16874 /*
16875  * This function can block
16876  */
16877 static void
16878 sata_trace_rbuf_alloc(void)
16879 {
16880 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
16881 
16882 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
16883 
16884 	if (dmsg_ring_size > 0) {
16885 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
16886 	}
16887 }
16888 
16889 
16890 static void
16891 sata_trace_rbuf_free(void)
16892 {
16893 	sata_trace_dmsg_free();
16894 	mutex_destroy(&sata_debug_rbuf->lock);
16895 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
16896 }
16897 
16898 /*
16899  * If SATA_DEBUG is not defined then this routine is called instead
16900  * of sata_log() via the SATA_LOG_D macro.
16901  */
16902 static void
16903 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
16904     const char *fmt, ...)
16905 {
16906 #ifndef __lock_lint
16907 	_NOTE(ARGUNUSED(level))
16908 #endif
16909 
16910 	dev_info_t *dip = NULL;
16911 	va_list ap;
16912 
16913 	if (sata_hba_inst != NULL) {
16914 		dip = SATA_DIP(sata_hba_inst);
16915 	}
16916 
16917 	va_start(ap, fmt);
16918 	sata_vtrace_debug(dip, fmt, ap);
16919 	va_end(ap);
16920 }
16921