xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision 653d2d91a355b2a5df7db09e17c965ddc5d3ea47)
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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
27  */
28 
29 /*
30  * SATA Framework
31  * Generic SATA Host Adapter Implementation
32  */
33 
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/modctl.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/thread.h>
42 #include <sys/kstat.h>
43 #include <sys/note.h>
44 #include <sys/sysevent.h>
45 #include <sys/sysevent/eventdefs.h>
46 #include <sys/sysevent/dr.h>
47 #include <sys/taskq.h>
48 #include <sys/disp.h>
49 #include <sys/sdt.h>
50 
51 #include <sys/sata/impl/sata.h>
52 #include <sys/sata/sata_hba.h>
53 #include <sys/sata/sata_defs.h>
54 #include <sys/sata/sata_cfgadm.h>
55 #include <sys/sata/sata_blacklist.h>
56 #include <sys/sata/sata_satl.h>
57 
58 #include <sys/scsi/impl/spc3_types.h>
59 
60 /*
61  * FMA header files
62  */
63 #include <sys/ddifm.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/fm/util.h>
66 #include <sys/fm/io/ddi.h>
67 
68 /* Debug flags - defined in sata.h */
69 int	sata_debug_flags = 0;
70 int	sata_msg = 0;
71 
72 /*
73  * Flags enabling selected SATA HBA framework functionality
74  */
75 #define	SATA_ENABLE_QUEUING		1
76 #define	SATA_ENABLE_NCQ			2
77 #define	SATA_ENABLE_PROCESS_EVENTS	4
78 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
79 int sata_func_enable =
80 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
81 
82 /*
83  * Global variable setting default maximum queue depth (NCQ or TCQ)
84  * Note:minimum queue depth is 1
85  */
86 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
87 
88 /*
89  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
90  * initialization, using value from sata_max_queue_depth
91  * It is adjusted to minimum supported by the controller and by the device,
92  * if queueing is enabled.
93  */
94 static	int sata_current_max_qdepth;
95 
96 /*
97  * Global variable determining the default behavior after device hotpluggin.
98  * If non-zero, the hotplugged device is onlined (if possible) without explicit
99  * IOCTL request (AP_CONFIGURE).
100  * If zero, hotplugged device is identified, but not onlined.
101  * Enabling (AP_CONNECT) device port with an attached device does not result
102  * in device onlining regardless of the flag setting
103  */
104 int sata_auto_online = 0;
105 
106 #ifdef SATA_DEBUG
107 
108 #define	SATA_LOG_D(args)	sata_log args
109 uint64_t mbuf_count = 0;
110 uint64_t mbuffail_count = 0;
111 
112 sata_atapi_cmd_t sata_atapi_trace[64];
113 uint32_t sata_atapi_trace_index = 0;
114 int sata_atapi_trace_save = 1;
115 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
116 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
117     sata_save_atapi_trace(spx, count);
118 
119 #else
120 #define	SATA_LOG_D(args)	sata_trace_log args
121 #define	SATAATAPITRACE(spx, count)
122 #endif
123 
124 #if 0
125 static void
126 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
127 #endif
128 
129 #ifdef SATA_INJECT_FAULTS
130 
131 #define		SATA_INJECT_PKT_FAULT	1
132 uint32_t	sata_inject_fault = 0;
133 
134 uint32_t	sata_inject_fault_count = 0;
135 uint32_t	sata_inject_fault_pause_count = 0;
136 uint32_t	sata_fault_type = 0;
137 uint32_t	sata_fault_cmd = 0;
138 dev_info_t	*sata_fault_ctrl = NULL;
139 sata_device_t	sata_fault_device;
140 
141 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
142 
143 #endif
144 
145 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
146 
147 static char sata_rev_tag[] = {"1.46"};
148 
149 /*
150  * SATA cb_ops functions
151  */
152 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
153 static 	int sata_hba_close(dev_t, int, int, cred_t *);
154 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
155 
156 /*
157  * SCSA required entry points
158  */
159 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
160     scsi_hba_tran_t *, struct scsi_device *);
161 static	int sata_scsi_tgt_probe(struct scsi_device *,
162     int (*callback)(void));
163 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
164     scsi_hba_tran_t *, struct scsi_device *);
165 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
166 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
167 static 	int sata_scsi_reset(struct scsi_address *, int);
168 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
169 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
170 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
171     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
172     caddr_t);
173 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
174 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
175 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
176 
177 /*
178  * SATA HBA interface functions are defined in sata_hba.h header file
179  */
180 
181 /* Event processing functions */
182 static	void sata_event_daemon(void *);
183 static	void sata_event_thread_control(int);
184 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
185 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
186 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
187 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
188 static	void sata_process_port_failed_event(sata_hba_inst_t *,
189     sata_address_t *);
190 static	void sata_process_port_link_events(sata_hba_inst_t *,
191     sata_address_t *);
192 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
193     sata_address_t *);
194 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
195 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
196     sata_address_t *);
197 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
198 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
199     sata_address_t *);
200 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
201 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
202 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
203     sata_address_t *);
204 static	void sata_process_device_autoonline(sata_hba_inst_t *,
205     sata_address_t *saddr);
206 
207 /*
208  * Local translation functions
209  */
210 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
211 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
212 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
213 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
214 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
215 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
216 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
217 static	int sata_txlt_read(sata_pkt_txlate_t *);
218 static	int sata_txlt_write(sata_pkt_txlate_t *);
219 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
220 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
221 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
222 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
223 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
224 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
225 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
226 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
227 
228 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
229 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
230 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
231 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
232 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
233 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
234 static	void sata_txlt_rw_completion(sata_pkt_t *);
235 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
236 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
237 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
238 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
239 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
240 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
241     uint8_t);
242 static	struct scsi_extended_sense *sata_immediate_error_response(
243     sata_pkt_txlate_t *, int);
244 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
245 
246 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
247 static	void sata_txlt_atapi_completion(sata_pkt_t *);
248 
249 /*
250  * Local functions for ioctl
251  */
252 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
253 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
254     devctl_ap_state_t *);
255 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
256 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
257 static	dev_info_t *sata_devt_to_devinfo(dev_t);
258 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
259 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
260 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
267 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
268 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
269     sata_ioctl_data_t *, int mode);
270 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
271     sata_ioctl_data_t *, int mode);
272 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
273     sata_ioctl_data_t *, int mode);
274 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
275     sata_ioctl_data_t *, int mode);
276 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
277     sata_device_t *, sata_ioctl_data_t *, int mode);
278 
279 /*
280  * Local functions
281  */
282 static 	void sata_remove_hba_instance(dev_info_t *);
283 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
284 static 	void sata_probe_ports(sata_hba_inst_t *);
285 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
286 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
287 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
288 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
289 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
290 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
291 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
292 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
293     sata_drive_info_t *);
294 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
295     sata_address_t *);
296 static 	void sata_remove_target_node(sata_hba_inst_t *,
297     sata_address_t *);
298 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
299     struct scsi_address *, sata_device_t *);
300 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
301 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
302 static	void sata_pkt_free(sata_pkt_txlate_t *);
303 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
304     caddr_t, ddi_dma_attr_t *);
305 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
306 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
307 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
308     sata_device_t *);
309 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
310 static	void sata_reidentify_device(sata_pkt_txlate_t *);
311 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
312 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
313 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
314 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
315     ddi_dma_attr_t *);
316 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
317     sata_drive_info_t *);
318 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
319 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
320 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
321 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
322 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
323 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
324 static	int sata_set_drive_features(sata_hba_inst_t *,
325     sata_drive_info_t *, int flag);
326 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
327 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
328 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
329     uint8_t *);
330 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
331     struct scsi_inquiry *);
332 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
333 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
334 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
338     struct mode_cache_scsi3 *, int, int *, int *, int *);
339 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
340     struct mode_info_power_cond *, int, int *, int *, int *);
341 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
342     struct mode_info_excpt_page *, int, int *, int *, int *);
343 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
344     struct mode_acoustic_management *, int, int *, int *, int *);
345 
346 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
347 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
348     sata_hba_inst_t *);
349 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
350     sata_hba_inst_t *);
351 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
352     sata_hba_inst_t *);
353 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
354     sata_pkt_txlate_t *);
355 
356 static	void sata_set_arq_data(sata_pkt_t *);
357 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
358 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
359 static	uint8_t sata_get_standby_timer(uint8_t *timer);
360 
361 static	void sata_save_drive_settings(sata_drive_info_t *);
362 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
363 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
364 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
365 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
366 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
367     sata_drive_info_t *);
368 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
369     struct smart_data *);
370 static	int sata_smart_selftest_log(sata_hba_inst_t *,
371     sata_drive_info_t *,
372     struct smart_selftest_log *);
373 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
374     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
375 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
376     uint8_t *, uint8_t, uint8_t);
377 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
378     struct read_log_ext_directory *);
379 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
380 static	void sata_xlate_errors(sata_pkt_txlate_t *);
381 static	void sata_decode_device_error(sata_pkt_txlate_t *,
382     struct scsi_extended_sense *);
383 static	void sata_set_device_removed(dev_info_t *);
384 static	boolean_t sata_check_device_removed(dev_info_t *);
385 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
386 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
387     sata_drive_info_t *);
388 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
389     sata_drive_info_t *);
390 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
391 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
392 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
393 static  int sata_check_modser(char *, int);
394 
395 /*
396  * FMA
397  */
398 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
399 
400 
401 /*
402  * SATA Framework will ignore SATA HBA driver cb_ops structure and
403  * register following one with SCSA framework.
404  * Open & close are provided, so scsi framework will not use its own
405  */
406 static struct cb_ops sata_cb_ops = {
407 	sata_hba_open,			/* open */
408 	sata_hba_close,			/* close */
409 	nodev,				/* strategy */
410 	nodev,				/* print */
411 	nodev,				/* dump */
412 	nodev,				/* read */
413 	nodev,				/* write */
414 	sata_hba_ioctl,			/* ioctl */
415 	nodev,				/* devmap */
416 	nodev,				/* mmap */
417 	nodev,				/* segmap */
418 	nochpoll,			/* chpoll */
419 	ddi_prop_op,			/* cb_prop_op */
420 	0,				/* streamtab */
421 	D_NEW | D_MP,			/* cb_flag */
422 	CB_REV,				/* rev */
423 	nodev,				/* aread */
424 	nodev				/* awrite */
425 };
426 
427 
428 extern struct mod_ops mod_miscops;
429 extern uchar_t	scsi_cdb_size[];
430 
431 static struct modlmisc modlmisc = {
432 	&mod_miscops,			/* Type of module */
433 	"SATA Module"			/* module name */
434 };
435 
436 
437 static struct modlinkage modlinkage = {
438 	MODREV_1,
439 	(void *)&modlmisc,
440 	NULL
441 };
442 
443 /*
444  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
445  * i.e. when scsi_pkt has not timeout specified.
446  */
447 static int sata_default_pkt_time = 60;	/* 60 seconds */
448 
449 /*
450  * Intermediate buffer device access attributes - they are required,
451  * but not necessarily used.
452  */
453 static ddi_device_acc_attr_t sata_acc_attr = {
454 	DDI_DEVICE_ATTR_V0,
455 	DDI_STRUCTURE_LE_ACC,
456 	DDI_STRICTORDER_ACC
457 };
458 
459 
460 /*
461  * Mutexes protecting structures in multithreaded operations.
462  * Because events are relatively rare, a single global mutex protecting
463  * data structures should be sufficient. To increase performance, add
464  * separate mutex per each sata port and use global mutex only to protect
465  * common data structures.
466  */
467 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
468 static	kmutex_t sata_log_mutex;	/* protects log */
469 
470 static 	char sata_log_buf[256];
471 
472 /*
473  * sata trace debug
474  */
475 static	sata_trace_rbuf_t *sata_debug_rbuf;
476 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
477 static	void sata_trace_dmsg_free(void);
478 static	void sata_trace_rbuf_alloc(void);
479 static	void sata_trace_rbuf_free(void);
480 
481 int	dmsg_ring_size = DMSG_RING_SIZE;
482 
483 /* Default write cache setting for SATA hard disks */
484 int	sata_write_cache = 1;		/* enabled */
485 
486 /* Default write cache setting for SATA ATAPI CD/DVD */
487 int	sata_atapicdvd_write_cache = 1; /* enabled */
488 
489 /* Default write cache setting for SATA ATAPI tape */
490 int	sata_atapitape_write_cache = 1; /* enabled */
491 
492 /* Default write cache setting for SATA ATAPI disk */
493 int	sata_atapidisk_write_cache = 1;	/* enabled */
494 
495 /*
496  * Linked list of HBA instances
497  */
498 static 	sata_hba_inst_t *sata_hba_list = NULL;
499 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
500 /*
501  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
502  * structure and in sata soft state.
503  */
504 
505 /*
506  * Event daemon related variables
507  */
508 static 	kmutex_t sata_event_mutex;
509 static 	kcondvar_t sata_event_cv;
510 static 	kthread_t *sata_event_thread = NULL;
511 static 	int sata_event_thread_terminate = 0;
512 static 	int sata_event_pending = 0;
513 static 	int sata_event_thread_active = 0;
514 extern 	pri_t minclsyspri;
515 
516 /*
517  * NCQ error recovery command
518  */
519 static const sata_cmd_t sata_rle_cmd = {
520 	SATA_CMD_REV,
521 	NULL,
522 	{
523 		SATA_DIR_READ
524 	},
525 	ATA_ADDR_LBA48,
526 	0,
527 	0,
528 	0,
529 	0,
530 	0,
531 	1,
532 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
533 	0,
534 	0,
535 	0,
536 	SATAC_READ_LOG_EXT,
537 	0,
538 	0,
539 	0,
540 };
541 
542 /*
543  * ATAPI error recovery CDB
544  */
545 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
546 	SCMD_REQUEST_SENSE,
547 	0,			/* Only fixed RQ format is supported */
548 	0,
549 	0,
550 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
551 	0
552 };
553 
554 
555 /* Warlock directives */
556 
557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
566 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
567 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
569 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
570 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
571     sata_hba_inst::satahba_scsi_tran))
572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
574 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
575 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
576 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
577     sata_hba_inst::satahba_event_flags))
578 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
579     sata_cport_info::cport_devp))
580 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
581 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
582 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
583     sata_cport_info::cport_dev_type))
584 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
585 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
586     sata_cport_info::cport_state))
587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
588 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
589     sata_pmport_info::pmport_state))
590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
591 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
592     sata_pmport_info::pmport_dev_type))
593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
594 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
595     sata_pmport_info::pmport_sata_drive))
596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597     sata_pmport_info::pmport_tgtnode_clean))
598 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
599     sata_pmport_info::pmport_event_flags))
600 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
601 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
603 #ifdef SATA_DEBUG
604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
608 #endif
609 
610 /* End of warlock directives */
611 
612 /* ************** loadable module configuration functions ************** */
613 
614 int
615 _init()
616 {
617 	int rval;
618 
619 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
620 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
621 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
622 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
623 	sata_trace_rbuf_alloc();
624 	if ((rval = mod_install(&modlinkage)) != 0) {
625 #ifdef SATA_DEBUG
626 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
627 #endif
628 		sata_trace_rbuf_free();
629 		mutex_destroy(&sata_log_mutex);
630 		cv_destroy(&sata_event_cv);
631 		mutex_destroy(&sata_event_mutex);
632 		mutex_destroy(&sata_mutex);
633 	}
634 	return (rval);
635 }
636 
637 int
638 _fini()
639 {
640 	int rval;
641 
642 	if ((rval = mod_remove(&modlinkage)) != 0)
643 		return (rval);
644 
645 	sata_trace_rbuf_free();
646 	mutex_destroy(&sata_log_mutex);
647 	cv_destroy(&sata_event_cv);
648 	mutex_destroy(&sata_event_mutex);
649 	mutex_destroy(&sata_mutex);
650 	return (rval);
651 }
652 
653 int
654 _info(struct modinfo *modinfop)
655 {
656 	return (mod_info(&modlinkage, modinfop));
657 }
658 
659 
660 
661 /* ********************* SATA HBA entry points ********************* */
662 
663 
664 /*
665  * Called by SATA HBA from _init().
666  * Registers HBA driver instance/sata framework pair with scsi framework, by
667  * calling scsi_hba_init().
668  *
669  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
670  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
671  * cb_ops pointer in SATA HBA driver dev_ops structure.
672  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
673  *
674  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
675  * driver.
676  */
677 int
678 sata_hba_init(struct modlinkage *modlp)
679 {
680 	int rval;
681 	struct dev_ops *hba_ops;
682 
683 	SATADBG1(SATA_DBG_HBA_IF, NULL,
684 	    "sata_hba_init: name %s \n",
685 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
686 	/*
687 	 * Fill-up cb_ops and dev_ops when necessary
688 	 */
689 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
690 	/*
691 	 * Provide pointer to SATA dev_ops
692 	 */
693 	hba_ops->devo_cb_ops = &sata_cb_ops;
694 
695 	/*
696 	 * Register SATA HBA with SCSI framework
697 	 */
698 	if ((rval = scsi_hba_init(modlp)) != 0) {
699 		SATADBG1(SATA_DBG_HBA_IF, NULL,
700 		    "sata_hba_init: scsi hba init failed\n", NULL);
701 		return (rval);
702 	}
703 
704 	return (0);
705 }
706 
707 
708 /* HBA attach stages */
709 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
710 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
711 #define	HBA_ATTACH_STAGE_SETUP		4
712 #define	HBA_ATTACH_STAGE_LINKED		8
713 
714 
715 /*
716  *
717  * Called from SATA HBA driver's attach routine to attach an instance of
718  * the HBA.
719  *
720  * For DDI_ATTACH command:
721  * sata_hba_inst structure is allocated here and initialized with pointers to
722  * SATA framework implementation of required scsi tran functions.
723  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
724  * to the soft structure (sata_hba_inst) allocated by SATA framework for
725  * SATA HBA instance related data.
726  * The scsi_tran's tran_hba_private field is used by SATA framework to
727  * store a pointer to per-HBA-instance of sata_hba_inst structure.
728  * The sata_hba_inst structure is cross-linked to scsi tran structure.
729  * Among other info, a pointer to sata_hba_tran structure is stored in
730  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
731  * linked together into the list, pointed to by sata_hba_list.
732  * On the first HBA instance attach the sata event thread is initialized.
733  * Attachment points are created for all SATA ports of the HBA being attached.
734  * All HBA instance's SATA ports are probed and type of plugged devices is
735  * determined. For each device of a supported type, a target node is created.
736  *
737  * DDI_SUCCESS is returned when attachment process is successful,
738  * DDI_FAILURE is returned otherwise.
739  *
740  * For DDI_RESUME command:
741  * Not implemented at this time (postponed until phase 2 of the development).
742  */
743 int
744 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
745     ddi_attach_cmd_t cmd)
746 {
747 	sata_hba_inst_t	*sata_hba_inst;
748 	scsi_hba_tran_t *scsi_tran = NULL;
749 	int hba_attach_state = 0;
750 	char taskq_name[MAXPATHLEN];
751 
752 	SATADBG3(SATA_DBG_HBA_IF, NULL,
753 	    "sata_hba_attach: node %s (%s%d)\n",
754 	    ddi_node_name(dip), ddi_driver_name(dip),
755 	    ddi_get_instance(dip));
756 
757 	if (cmd == DDI_RESUME) {
758 		/*
759 		 * Postponed until phase 2 of the development
760 		 */
761 		return (DDI_FAILURE);
762 	}
763 
764 	if (cmd != DDI_ATTACH) {
765 		return (DDI_FAILURE);
766 	}
767 
768 	/* cmd == DDI_ATTACH */
769 
770 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
771 		SATA_LOG_D((NULL, CE_WARN,
772 		    "sata_hba_attach: invalid sata_hba_tran"));
773 		return (DDI_FAILURE);
774 	}
775 	/*
776 	 * Allocate and initialize SCSI tran structure.
777 	 * SATA copy of tran_bus_config is provided to create port nodes.
778 	 */
779 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
780 	if (scsi_tran == NULL)
781 		return (DDI_FAILURE);
782 	/*
783 	 * Allocate soft structure for SATA HBA instance.
784 	 * There is a separate softstate for each HBA instance.
785 	 */
786 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
787 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
788 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
789 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
790 
791 	/*
792 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
793 	 * soft structure allocated by SATA framework for
794 	 * SATA HBA instance related data.
795 	 */
796 	scsi_tran->tran_hba_private	= sata_hba_inst;
797 	scsi_tran->tran_tgt_private	= NULL;
798 
799 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
800 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
801 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
802 
803 	scsi_tran->tran_start		= sata_scsi_start;
804 	scsi_tran->tran_reset		= sata_scsi_reset;
805 	scsi_tran->tran_abort		= sata_scsi_abort;
806 	scsi_tran->tran_getcap		= sata_scsi_getcap;
807 	scsi_tran->tran_setcap		= sata_scsi_setcap;
808 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
809 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
810 
811 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
812 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
813 
814 	scsi_tran->tran_reset_notify	= NULL;
815 	scsi_tran->tran_get_bus_addr	= NULL;
816 	scsi_tran->tran_quiesce		= NULL;
817 	scsi_tran->tran_unquiesce	= NULL;
818 	scsi_tran->tran_bus_reset	= NULL;
819 
820 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
821 	    scsi_tran, 0) != DDI_SUCCESS) {
822 #ifdef SATA_DEBUG
823 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
824 		    ddi_driver_name(dip), ddi_get_instance(dip));
825 #endif
826 		goto fail;
827 	}
828 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
829 
830 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
831 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
832 		    "sata", 1) != DDI_PROP_SUCCESS) {
833 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
834 			    "failed to create hba sata prop"));
835 			goto fail;
836 		}
837 	}
838 
839 	/*
840 	 * Save pointers in hba instance soft state.
841 	 */
842 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
843 	sata_hba_inst->satahba_tran = sata_tran;
844 	sata_hba_inst->satahba_dip = dip;
845 
846 	/*
847 	 * Create a task queue to handle emulated commands completion
848 	 * Use node name, dash, instance number as the queue name.
849 	 */
850 	taskq_name[0] = '\0';
851 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
852 	    sizeof (taskq_name));
853 	(void) snprintf(taskq_name + strlen(taskq_name),
854 	    sizeof (taskq_name) - strlen(taskq_name),
855 	    "-%d", DEVI(dip)->devi_instance);
856 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
857 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
858 	    TASKQ_DYNAMIC);
859 
860 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
861 
862 	/*
863 	 * Create events thread if not created yet.
864 	 */
865 	sata_event_thread_control(1);
866 
867 	/*
868 	 * Link this hba instance into the list.
869 	 */
870 	mutex_enter(&sata_mutex);
871 
872 	if (sata_hba_list == NULL) {
873 		/*
874 		 * The first instance of HBA is attached.
875 		 * Set current/active default maximum NCQ/TCQ queue depth for
876 		 * all SATA devices. It is done here and now, to eliminate the
877 		 * possibility of the dynamic, programatic modification of the
878 		 * queue depth via global (and public) sata_max_queue_depth
879 		 * variable (this would require special handling in HBA drivers)
880 		 */
881 		sata_current_max_qdepth = sata_max_queue_depth;
882 		if (sata_current_max_qdepth > 32)
883 			sata_current_max_qdepth = 32;
884 		else if (sata_current_max_qdepth < 1)
885 			sata_current_max_qdepth = 1;
886 	}
887 
888 	sata_hba_inst->satahba_next = NULL;
889 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
890 	if (sata_hba_list == NULL) {
891 		sata_hba_list = sata_hba_inst;
892 	}
893 	if (sata_hba_list_tail != NULL) {
894 		sata_hba_list_tail->satahba_next = sata_hba_inst;
895 	}
896 	sata_hba_list_tail = sata_hba_inst;
897 	mutex_exit(&sata_mutex);
898 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
899 
900 	/*
901 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
902 	 * SATA HBA driver should not use its own open/close entry points.
903 	 *
904 	 * Make sure that instance number doesn't overflow
905 	 * when forming minor numbers.
906 	 */
907 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
908 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
909 	    INST2DEVCTL(ddi_get_instance(dip)),
910 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
911 #ifdef SATA_DEBUG
912 		cmn_err(CE_WARN, "sata_hba_attach: "
913 		    "cannot create devctl minor node");
914 #endif
915 		goto fail;
916 	}
917 
918 
919 	/*
920 	 * Set-up kstats here, if necessary.
921 	 * (postponed until future phase of the development).
922 	 */
923 
924 	/*
925 	 * Indicate that HBA is attached. This will enable events processing
926 	 * for this HBA.
927 	 */
928 	sata_hba_inst->satahba_attached = 1;
929 	/*
930 	 * Probe controller ports. This operation will describe a current
931 	 * controller/port/multipliers/device configuration and will create
932 	 * attachment points.
933 	 * We may end-up with just a controller with no devices attached.
934 	 * For the ports with a supported device attached, device target nodes
935 	 * are created and devices are initialized.
936 	 */
937 	sata_probe_ports(sata_hba_inst);
938 
939 	return (DDI_SUCCESS);
940 
941 fail:
942 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
943 		(void) sata_remove_hba_instance(dip);
944 		if (sata_hba_list == NULL)
945 			sata_event_thread_control(0);
946 	}
947 
948 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
949 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
950 		taskq_destroy(sata_hba_inst->satahba_taskq);
951 	}
952 
953 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
954 		(void) scsi_hba_detach(dip);
955 
956 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
957 		mutex_destroy(&sata_hba_inst->satahba_mutex);
958 		kmem_free((void *)sata_hba_inst,
959 		    sizeof (struct sata_hba_inst));
960 		scsi_hba_tran_free(scsi_tran);
961 	}
962 
963 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
964 	    ddi_driver_name(dip), ddi_get_instance(dip));
965 
966 	return (DDI_FAILURE);
967 }
968 
969 
970 /*
971  * Called by SATA HBA from to detach an instance of the driver.
972  *
973  * For DDI_DETACH command:
974  * Free local structures allocated for SATA HBA instance during
975  * sata_hba_attach processing.
976  *
977  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
978  *
979  * For DDI_SUSPEND command:
980  * Not implemented at this time (postponed until phase 2 of the development)
981  * Returnd DDI_SUCCESS.
982  *
983  * When the last HBA instance is detached, the event daemon is terminated.
984  *
985  * NOTE: Port multiplier is supported.
986  */
987 int
988 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
989 {
990 	dev_info_t	*tdip;
991 	sata_hba_inst_t	*sata_hba_inst;
992 	scsi_hba_tran_t *scsi_hba_tran;
993 	sata_cport_info_t *cportinfo;
994 	sata_pmult_info_t *pminfo;
995 	sata_drive_info_t *sdinfo;
996 	sata_device_t	sdevice;
997 	int ncport, npmport;
998 
999 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1000 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1001 
1002 	switch (cmd) {
1003 	case DDI_DETACH:
1004 
1005 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1006 			return (DDI_FAILURE);
1007 
1008 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1009 		if (sata_hba_inst == NULL)
1010 			return (DDI_FAILURE);
1011 
1012 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1013 			sata_hba_inst->satahba_attached = 1;
1014 			return (DDI_FAILURE);
1015 		}
1016 
1017 		/*
1018 		 * Free all target nodes - at this point
1019 		 * devices should be at least offlined
1020 		 * otherwise scsi_hba_detach() should not be called.
1021 		 */
1022 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1023 		    ncport++) {
1024 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1025 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1026 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1027 				if (sdinfo != NULL) {
1028 					tdip = sata_get_target_dip(dip,
1029 					    ncport, 0);
1030 					if (tdip != NULL) {
1031 						if (ndi_devi_offline(tdip,
1032 						    NDI_DEVI_REMOVE) !=
1033 						    NDI_SUCCESS) {
1034 							SATA_LOG_D((
1035 							    sata_hba_inst,
1036 							    CE_WARN,
1037 							    "sata_hba_detach: "
1038 							    "Target node not "
1039 							    "removed !"));
1040 							return (DDI_FAILURE);
1041 						}
1042 					}
1043 				}
1044 			} else { /* SATA_DTYPE_PMULT */
1045 				mutex_enter(&cportinfo->cport_mutex);
1046 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1047 
1048 				if (pminfo == NULL) {
1049 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1050 					    "sata_hba_detach: Port multiplier "
1051 					    "not ready yet!"));
1052 					mutex_exit(&cportinfo->cport_mutex);
1053 					return (DDI_FAILURE);
1054 				}
1055 
1056 				/*
1057 				 * Detach would fail if removal of any of the
1058 				 * target nodes is failed - albeit in that
1059 				 * case some of them may have been removed.
1060 				 */
1061 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1062 				    sata_hba_inst, ncport); npmport++) {
1063 					tdip = sata_get_target_dip(dip, ncport,
1064 					    npmport);
1065 					if (tdip != NULL) {
1066 						if (ndi_devi_offline(tdip,
1067 						    NDI_DEVI_REMOVE) !=
1068 						    NDI_SUCCESS) {
1069 							SATA_LOG_D((
1070 							    sata_hba_inst,
1071 							    CE_WARN,
1072 							    "sata_hba_detach: "
1073 							    "Target node not "
1074 							    "removed !"));
1075 							mutex_exit(&cportinfo->
1076 							    cport_mutex);
1077 							return (DDI_FAILURE);
1078 						}
1079 					}
1080 				}
1081 				mutex_exit(&cportinfo->cport_mutex);
1082 			}
1083 		}
1084 		/*
1085 		 * Disable sata event daemon processing for this HBA
1086 		 */
1087 		sata_hba_inst->satahba_attached = 0;
1088 
1089 		/*
1090 		 * Remove event daemon thread, if it is last HBA instance.
1091 		 */
1092 
1093 		mutex_enter(&sata_mutex);
1094 		if (sata_hba_list->satahba_next == NULL) {
1095 			mutex_exit(&sata_mutex);
1096 			sata_event_thread_control(0);
1097 			mutex_enter(&sata_mutex);
1098 		}
1099 		mutex_exit(&sata_mutex);
1100 
1101 		/* Remove this HBA instance from the HBA list */
1102 		sata_remove_hba_instance(dip);
1103 
1104 		/*
1105 		 * At this point there should be no target nodes attached.
1106 		 * Detach and destroy device and port info structures.
1107 		 */
1108 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1109 		    ncport++) {
1110 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1111 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1112 				sdinfo =
1113 				    cportinfo->cport_devp.cport_sata_drive;
1114 				if (sdinfo != NULL) {
1115 					/* Release device structure */
1116 					kmem_free(sdinfo,
1117 					    sizeof (sata_drive_info_t));
1118 				}
1119 				/* Release cport info */
1120 				mutex_destroy(&cportinfo->cport_mutex);
1121 				kmem_free(cportinfo,
1122 				    sizeof (sata_cport_info_t));
1123 			} else { /* SATA_DTYPE_PMULT */
1124 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1125 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1126 				sata_free_pmult(sata_hba_inst, &sdevice);
1127 			}
1128 		}
1129 
1130 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1131 
1132 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1133 
1134 		taskq_destroy(sata_hba_inst->satahba_taskq);
1135 
1136 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1137 		kmem_free((void *)sata_hba_inst,
1138 		    sizeof (struct sata_hba_inst));
1139 
1140 		return (DDI_SUCCESS);
1141 
1142 	case DDI_SUSPEND:
1143 		/*
1144 		 * Postponed until phase 2
1145 		 */
1146 		return (DDI_FAILURE);
1147 
1148 	default:
1149 		return (DDI_FAILURE);
1150 	}
1151 }
1152 
1153 
1154 /*
1155  * Called by an HBA drive from _fini() routine.
1156  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1157  */
1158 void
1159 sata_hba_fini(struct modlinkage *modlp)
1160 {
1161 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1162 	    "sata_hba_fini: name %s\n",
1163 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1164 
1165 	scsi_hba_fini(modlp);
1166 }
1167 
1168 
1169 /*
1170  * Default open and close routine for sata_hba framework.
1171  *
1172  */
1173 /*
1174  * Open devctl node.
1175  *
1176  * Returns:
1177  * 0 if node was open successfully, error code otherwise.
1178  *
1179  *
1180  */
1181 
1182 static int
1183 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1184 {
1185 #ifndef __lock_lint
1186 	_NOTE(ARGUNUSED(credp))
1187 #endif
1188 	int rv = 0;
1189 	dev_info_t *dip;
1190 	scsi_hba_tran_t *scsi_hba_tran;
1191 	sata_hba_inst_t	*sata_hba_inst;
1192 
1193 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1194 
1195 	if (otyp != OTYP_CHR)
1196 		return (EINVAL);
1197 
1198 	dip = sata_devt_to_devinfo(*devp);
1199 	if (dip == NULL)
1200 		return (ENXIO);
1201 
1202 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1203 		return (ENXIO);
1204 
1205 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1206 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1207 		return (ENXIO);
1208 
1209 	mutex_enter(&sata_mutex);
1210 	if (flags & FEXCL) {
1211 		if (sata_hba_inst->satahba_open_flag != 0) {
1212 			rv = EBUSY;
1213 		} else {
1214 			sata_hba_inst->satahba_open_flag =
1215 			    SATA_DEVCTL_EXOPENED;
1216 		}
1217 	} else {
1218 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1219 			rv = EBUSY;
1220 		} else {
1221 			sata_hba_inst->satahba_open_flag =
1222 			    SATA_DEVCTL_SOPENED;
1223 		}
1224 	}
1225 	mutex_exit(&sata_mutex);
1226 
1227 	return (rv);
1228 }
1229 
1230 
1231 /*
1232  * Close devctl node.
1233  * Returns:
1234  * 0 if node was closed successfully, error code otherwise.
1235  *
1236  */
1237 
1238 static int
1239 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1240 {
1241 #ifndef __lock_lint
1242 	_NOTE(ARGUNUSED(credp))
1243 	_NOTE(ARGUNUSED(flag))
1244 #endif
1245 	dev_info_t *dip;
1246 	scsi_hba_tran_t *scsi_hba_tran;
1247 	sata_hba_inst_t	*sata_hba_inst;
1248 
1249 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1250 
1251 	if (otyp != OTYP_CHR)
1252 		return (EINVAL);
1253 
1254 	dip = sata_devt_to_devinfo(dev);
1255 	if (dip == NULL)
1256 		return (ENXIO);
1257 
1258 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1259 		return (ENXIO);
1260 
1261 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1262 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1263 		return (ENXIO);
1264 
1265 	mutex_enter(&sata_mutex);
1266 	sata_hba_inst->satahba_open_flag = 0;
1267 	mutex_exit(&sata_mutex);
1268 	return (0);
1269 }
1270 
1271 
1272 
1273 /*
1274  * Standard IOCTL commands for SATA hotplugging.
1275  * Implemented DEVCTL_AP commands:
1276  * DEVCTL_AP_CONNECT
1277  * DEVCTL_AP_DISCONNECT
1278  * DEVCTL_AP_CONFIGURE
1279  * DEVCTL_UNCONFIGURE
1280  * DEVCTL_AP_CONTROL
1281  *
1282  * Commands passed to default ndi ioctl handler:
1283  * DEVCTL_DEVICE_GETSTATE
1284  * DEVCTL_DEVICE_ONLINE
1285  * DEVCTL_DEVICE_OFFLINE
1286  * DEVCTL_DEVICE_REMOVE
1287  * DEVCTL_DEVICE_INSERT
1288  * DEVCTL_BUS_GETSTATE
1289  *
1290  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1291  * if not.
1292  *
1293  * Returns:
1294  * 0 if successful,
1295  * error code if operation failed.
1296  *
1297  * Port Multiplier support is supported now.
1298  *
1299  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1300  */
1301 
1302 static int
1303 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1304     int *rvalp)
1305 {
1306 #ifndef __lock_lint
1307 	_NOTE(ARGUNUSED(credp))
1308 	_NOTE(ARGUNUSED(rvalp))
1309 #endif
1310 	int rv = 0;
1311 	int32_t	comp_port = -1;
1312 	dev_info_t *dip;
1313 	devctl_ap_state_t ap_state;
1314 	struct devctl_iocdata *dcp = NULL;
1315 	scsi_hba_tran_t *scsi_hba_tran;
1316 	sata_hba_inst_t *sata_hba_inst;
1317 	sata_device_t sata_device;
1318 	sata_cport_info_t *cportinfo;
1319 	int cport, pmport, qual;
1320 	int rval = SATA_SUCCESS;
1321 
1322 	dip = sata_devt_to_devinfo(dev);
1323 	if (dip == NULL)
1324 		return (ENXIO);
1325 
1326 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1327 		return (ENXIO);
1328 
1329 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1330 	if (sata_hba_inst == NULL)
1331 		return (ENXIO);
1332 
1333 	if (sata_hba_inst->satahba_tran == NULL)
1334 		return (ENXIO);
1335 
1336 	switch (cmd) {
1337 
1338 	case DEVCTL_DEVICE_GETSTATE:
1339 	case DEVCTL_DEVICE_ONLINE:
1340 	case DEVCTL_DEVICE_OFFLINE:
1341 	case DEVCTL_DEVICE_REMOVE:
1342 	case DEVCTL_BUS_GETSTATE:
1343 		/*
1344 		 * There may be more cases that we want to pass to default
1345 		 * handler rather than fail them.
1346 		 */
1347 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1348 	}
1349 
1350 	/* read devctl ioctl data */
1351 	if (cmd != DEVCTL_AP_CONTROL) {
1352 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1353 			return (EFAULT);
1354 
1355 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1356 		    -1) {
1357 			if (dcp)
1358 				ndi_dc_freehdl(dcp);
1359 			return (EINVAL);
1360 		}
1361 
1362 		/*
1363 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1364 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1365 		 */
1366 		cport = SCSI_TO_SATA_CPORT(comp_port);
1367 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1368 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1369 
1370 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1371 		    qual) != 0) {
1372 			ndi_dc_freehdl(dcp);
1373 			return (EINVAL);
1374 		}
1375 
1376 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1377 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1378 		    cport_mutex);
1379 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1380 			/*
1381 			 * Cannot process ioctl request now. Come back later.
1382 			 */
1383 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1384 			    cport_mutex);
1385 			ndi_dc_freehdl(dcp);
1386 			return (EBUSY);
1387 		}
1388 		/* Block event processing for this port */
1389 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1390 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1391 
1392 		sata_device.satadev_addr.cport = cport;
1393 		sata_device.satadev_addr.pmport = pmport;
1394 		sata_device.satadev_addr.qual = qual;
1395 		sata_device.satadev_rev = SATA_DEVICE_REV;
1396 	}
1397 
1398 	switch (cmd) {
1399 
1400 	case DEVCTL_AP_DISCONNECT:
1401 
1402 		/*
1403 		 * Normally, cfgadm sata plugin will try to offline
1404 		 * (unconfigure) device before this request. Nevertheless,
1405 		 * if a device is still configured, we need to
1406 		 * attempt to offline and unconfigure device first, and we will
1407 		 * deactivate the port regardless of the unconfigure
1408 		 * operation results.
1409 		 *
1410 		 */
1411 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1412 
1413 		break;
1414 
1415 	case DEVCTL_AP_UNCONFIGURE:
1416 
1417 		/*
1418 		 * The unconfigure operation uses generic nexus operation to
1419 		 * offline a device. It leaves a target device node attached.
1420 		 * and obviously sata_drive_info attached as well, because
1421 		 * from the hardware point of view nothing has changed.
1422 		 */
1423 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1424 		break;
1425 
1426 	case DEVCTL_AP_CONNECT:
1427 	{
1428 		/*
1429 		 * The sata cfgadm pluging will invoke this operation only if
1430 		 * port was found in the disconnect state (failed state
1431 		 * is also treated as the disconnected state).
1432 		 * If port activation is successful and a device is found
1433 		 * attached to the port, the initialization sequence is
1434 		 * executed to probe the port and attach
1435 		 * a device structure to a port structure. The device is not
1436 		 * set in configured state (system-wise) by this operation.
1437 		 */
1438 
1439 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1440 
1441 		break;
1442 	}
1443 
1444 	case DEVCTL_AP_CONFIGURE:
1445 	{
1446 		/*
1447 		 * A port may be in an active or shutdown state.
1448 		 * If port is in a failed state, operation is aborted.
1449 		 * If a port is in a shutdown state, sata_tran_port_activate()
1450 		 * is invoked prior to any other operation.
1451 		 *
1452 		 * Onlining the device involves creating a new target node.
1453 		 * If there is an old target node present (belonging to
1454 		 * previously removed device), the operation is aborted - the
1455 		 * old node has to be released and removed before configure
1456 		 * operation is attempted.
1457 		 */
1458 
1459 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1460 
1461 		break;
1462 	}
1463 
1464 	case DEVCTL_AP_GETSTATE:
1465 
1466 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1467 
1468 		ap_state.ap_last_change = (time_t)-1;
1469 		ap_state.ap_error_code = 0;
1470 		ap_state.ap_in_transition = 0;
1471 
1472 		/* Copy the return AP-state information to the user space */
1473 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1474 			rv = EFAULT;
1475 		}
1476 		break;
1477 
1478 	case DEVCTL_AP_CONTROL:
1479 	{
1480 		/*
1481 		 * Generic devctl for hardware specific functionality
1482 		 */
1483 		sata_ioctl_data_t	ioc;
1484 
1485 		ASSERT(dcp == NULL);
1486 
1487 		/* Copy in user ioctl data first */
1488 #ifdef _MULTI_DATAMODEL
1489 		if (ddi_model_convert_from(mode & FMODELS) ==
1490 		    DDI_MODEL_ILP32) {
1491 
1492 			sata_ioctl_data_32_t	ioc32;
1493 
1494 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1495 			    sizeof (ioc32), mode) != 0) {
1496 				rv = EFAULT;
1497 				break;
1498 			}
1499 			ioc.cmd 	= (uint_t)ioc32.cmd;
1500 			ioc.port	= (uint_t)ioc32.port;
1501 			ioc.get_size	= (uint_t)ioc32.get_size;
1502 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1503 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1504 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1505 		} else
1506 #endif /* _MULTI_DATAMODEL */
1507 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1508 		    mode) != 0) {
1509 			return (EFAULT);
1510 		}
1511 
1512 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1513 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1514 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1515 
1516 		/*
1517 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1518 		 * a 32-bit number.
1519 		 */
1520 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1521 			return (EINVAL);
1522 		}
1523 		/* validate address */
1524 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1525 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1526 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1527 
1528 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1529 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1530 		    cport, pmport, qual);
1531 
1532 		if (sata_validate_sata_address(sata_hba_inst, cport,
1533 		    pmport, qual) != 0)
1534 			return (EINVAL);
1535 
1536 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1537 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1538 		    cport_mutex);
1539 		/* Is the port locked by event processing daemon ? */
1540 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1541 			/*
1542 			 * Cannot process ioctl request now. Come back later
1543 			 */
1544 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1545 			    cport_mutex);
1546 			return (EBUSY);
1547 		}
1548 		/* Block event processing for this port */
1549 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1550 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1551 
1552 
1553 		sata_device.satadev_addr.cport = cport;
1554 		sata_device.satadev_addr.pmport = pmport;
1555 		sata_device.satadev_addr.qual = qual;
1556 		sata_device.satadev_rev = SATA_DEVICE_REV;
1557 
1558 		switch (ioc.cmd) {
1559 
1560 		case SATA_CFGA_RESET_PORT:
1561 			/*
1562 			 * There is no protection for configured device.
1563 			 */
1564 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1565 			break;
1566 
1567 		case SATA_CFGA_RESET_DEVICE:
1568 			/*
1569 			 * There is no protection for configured device.
1570 			 */
1571 			rv = sata_ioctl_reset_device(sata_hba_inst,
1572 			    &sata_device);
1573 			break;
1574 
1575 		case SATA_CFGA_RESET_ALL:
1576 			/*
1577 			 * There is no protection for configured devices.
1578 			 */
1579 			rv = sata_ioctl_reset_all(sata_hba_inst);
1580 			/*
1581 			 * We return here, because common return is for
1582 			 * a single port operation - we have already unlocked
1583 			 * all ports and no dc handle was allocated.
1584 			 */
1585 			return (rv);
1586 
1587 		case SATA_CFGA_PORT_DEACTIVATE:
1588 			/*
1589 			 * Arbitrarily unconfigure attached device, if any.
1590 			 * Even if the unconfigure fails, proceed with the
1591 			 * port deactivation.
1592 			 */
1593 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1594 
1595 			break;
1596 
1597 		case SATA_CFGA_PORT_ACTIVATE:
1598 
1599 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1600 			break;
1601 
1602 		case SATA_CFGA_PORT_SELF_TEST:
1603 
1604 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1605 			    &sata_device);
1606 			break;
1607 
1608 		case SATA_CFGA_GET_DEVICE_PATH:
1609 
1610 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1611 			    &sata_device, &ioc, mode);
1612 			break;
1613 
1614 		case SATA_CFGA_GET_AP_TYPE:
1615 
1616 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1617 			    &sata_device, &ioc, mode);
1618 			break;
1619 
1620 		case SATA_CFGA_GET_MODEL_INFO:
1621 
1622 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1623 			    &sata_device, &ioc, mode);
1624 			break;
1625 
1626 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1627 
1628 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1629 			    &sata_device, &ioc, mode);
1630 			break;
1631 
1632 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1633 
1634 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1635 			    &sata_device, &ioc, mode);
1636 			break;
1637 
1638 		default:
1639 			rv = EINVAL;
1640 			break;
1641 
1642 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1643 
1644 		break;
1645 	}
1646 
1647 	default:
1648 	{
1649 		/*
1650 		 * If we got here, we got an IOCTL that SATA HBA Framework
1651 		 * does not recognize. Pass ioctl to HBA driver, in case
1652 		 * it could process it.
1653 		 */
1654 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1655 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1656 
1657 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1658 		    "IOCTL 0x%2x not supported in SATA framework, "
1659 		    "passthrough to HBA", cmd);
1660 
1661 		if (sata_tran->sata_tran_ioctl == NULL) {
1662 			rv = EINVAL;
1663 			break;
1664 		}
1665 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1666 		if (rval != 0) {
1667 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1668 			    "IOCTL 0x%2x failed in HBA", cmd);
1669 			rv = rval;
1670 		}
1671 		break;
1672 	}
1673 
1674 	} /* End of main IOCTL switch */
1675 
1676 	if (dcp) {
1677 		ndi_dc_freehdl(dcp);
1678 	}
1679 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1680 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1681 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1682 
1683 	return (rv);
1684 }
1685 
1686 
1687 /*
1688  * Create error retrieval sata packet
1689  *
1690  * A sata packet is allocated and set-up to contain specified error retrieval
1691  * command and appropriate dma-able data buffer.
1692  * No association with any scsi packet is made and no callback routine is
1693  * specified.
1694  *
1695  * Returns a pointer to sata packet upon successful packet creation.
1696  * Returns NULL, if packet cannot be created.
1697  */
1698 sata_pkt_t *
1699 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1700     int pkt_type)
1701 {
1702 	sata_hba_inst_t	*sata_hba_inst;
1703 	sata_pkt_txlate_t *spx;
1704 	sata_pkt_t *spkt;
1705 	sata_drive_info_t *sdinfo;
1706 
1707 	mutex_enter(&sata_mutex);
1708 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1709 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1710 		if (SATA_DIP(sata_hba_inst) == dip)
1711 			break;
1712 	}
1713 	mutex_exit(&sata_mutex);
1714 	ASSERT(sata_hba_inst != NULL);
1715 
1716 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1717 	if (sdinfo == NULL) {
1718 		sata_log(sata_hba_inst, CE_WARN,
1719 		    "sata: error recovery request for non-attached device at "
1720 		    "cport %d", sata_device->satadev_addr.cport);
1721 		return (NULL);
1722 	}
1723 
1724 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1725 	spx->txlt_sata_hba_inst = sata_hba_inst;
1726 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1727 	spkt = sata_pkt_alloc(spx, NULL);
1728 	if (spkt == NULL) {
1729 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1730 		return (NULL);
1731 	}
1732 	/* address is needed now */
1733 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1734 
1735 	switch (pkt_type) {
1736 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1737 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1738 			if (sata_check_for_dma_error(dip, spx)) {
1739 				ddi_fm_service_impact(dip,
1740 				    DDI_SERVICE_UNAFFECTED);
1741 				break;
1742 			}
1743 			return (spkt);
1744 		}
1745 		break;
1746 
1747 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1748 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1749 			if (sata_check_for_dma_error(dip, spx)) {
1750 				ddi_fm_service_impact(dip,
1751 				    DDI_SERVICE_UNAFFECTED);
1752 				break;
1753 			}
1754 			return (spkt);
1755 		}
1756 		break;
1757 
1758 	default:
1759 		break;
1760 	}
1761 
1762 	sata_pkt_free(spx);
1763 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1764 	return (NULL);
1765 
1766 }
1767 
1768 
1769 /*
1770  * Free error retrieval sata packet
1771  *
1772  * Free sata packet and any associated resources allocated previously by
1773  * sata_get_error_retrieval_pkt().
1774  *
1775  * Void return.
1776  */
1777 void
1778 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1779 {
1780 	sata_pkt_txlate_t *spx =
1781 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1782 
1783 	ASSERT(sata_pkt != NULL);
1784 
1785 	sata_free_local_buffer(spx);
1786 	sata_pkt_free(spx);
1787 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1788 
1789 }
1790 
1791 /*
1792  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1793  *
1794  * No association with any scsi packet is made and no callback routine is
1795  * specified.
1796  *
1797  * Returns a pointer to sata packet upon successful packet creation.
1798  * Returns NULL, if packet cannot be created.
1799  *
1800  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1801  * only lower 32 bits are available currently.
1802  */
1803 sata_pkt_t *
1804 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1805     uint8_t regn, uint32_t regv, uint32_t type)
1806 {
1807 	sata_hba_inst_t	*sata_hba_inst;
1808 	sata_pkt_txlate_t *spx;
1809 	sata_pkt_t *spkt;
1810 	sata_cmd_t *scmd;
1811 
1812 	/* Only READ/WRITE commands are accepted. */
1813 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1814 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1815 
1816 	mutex_enter(&sata_mutex);
1817 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1818 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1819 		if (SATA_DIP(sata_hba_inst) == dip)
1820 			break;
1821 	}
1822 	mutex_exit(&sata_mutex);
1823 	ASSERT(sata_hba_inst != NULL);
1824 
1825 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1826 	spx->txlt_sata_hba_inst = sata_hba_inst;
1827 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1828 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1829 	if (spkt == NULL) {
1830 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1831 		return (NULL);
1832 	}
1833 
1834 	/*
1835 	 * NOTE: We need to send this command to the port multiplier,
1836 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1837 	 *
1838 	 * sata_device contains the address of actual target device, and the
1839 	 * pmport number in the command comes from the sata_device structure.
1840 	 */
1841 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1842 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1843 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1844 
1845 	/* Fill sata_pkt */
1846 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1847 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1848 	spkt->satapkt_time = 10; /* Timeout 10s */
1849 
1850 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1851 	scmd = &spkt->satapkt_cmd;
1852 	scmd->satacmd_features_reg = regn & 0xff;
1853 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1854 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1855 	scmd->satacmd_addr_type = 0;		/* N/A */
1856 
1857 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1858 
1859 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1860 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1861 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1862 		scmd->satacmd_flags.sata_special_regs = 1;
1863 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1864 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1865 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1866 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1867 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1868 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1869 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1870 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1871 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1872 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1873 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1874 	}
1875 
1876 	return (spkt);
1877 }
1878 
1879 /*
1880  * Free sata packet and any associated resources allocated previously by
1881  * sata_get_rdwr_pmult_pkt().
1882  *
1883  * Void return.
1884  */
1885 void
1886 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1887 {
1888 	sata_pkt_txlate_t *spx =
1889 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1890 
1891 	/* Free allocated resources */
1892 	sata_pkt_free(spx);
1893 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1894 }
1895 
1896 /*
1897  * Register a port multiplier to framework.
1898  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1899  * 2) Search in the blacklist and update the number of the device ports of the
1900  * port multiplier.
1901  *
1902  * Void return.
1903  */
1904 void
1905 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1906 {
1907 	sata_hba_inst_t *sata_hba_inst = NULL;
1908 	sata_pmult_info_t *pmultinfo;
1909 	sata_pmult_bl_t *blp;
1910 	int cport = sd->satadev_addr.cport;
1911 
1912 	mutex_enter(&sata_mutex);
1913 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1914 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1915 		if (SATA_DIP(sata_hba_inst) == dip)
1916 			if (sata_hba_inst->satahba_attached == 1)
1917 				break;
1918 	}
1919 	mutex_exit(&sata_mutex);
1920 	/* HBA not attached? */
1921 	if (sata_hba_inst == NULL)
1922 		return;
1923 
1924 	/* Number of pmports */
1925 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1926 
1927 	/* Check the blacklist */
1928 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1929 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1930 			continue;
1931 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1932 			continue;
1933 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1934 			continue;
1935 
1936 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1937 		sd->satadev_add_info = blp->bl_flags;
1938 		break;
1939 	}
1940 
1941 	/* Register the port multiplier GSCR */
1942 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1943 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1944 	if (pmultinfo != NULL) {
1945 		pmultinfo->pmult_gscr = *sg;
1946 		pmultinfo->pmult_num_dev_ports =
1947 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1948 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1949 		    "Port multiplier registered at port %d", cport);
1950 	}
1951 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1952 }
1953 
1954 /*
1955  * sata_name_child is for composing the name of the node
1956  * the format of the name is "target,0".
1957  */
1958 static int
1959 sata_name_child(dev_info_t *dip, char *name, int namelen)
1960 {
1961 	int target;
1962 
1963 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1964 	    DDI_PROP_DONTPASS, "target", -1);
1965 	if (target == -1)
1966 		return (DDI_FAILURE);
1967 	(void) snprintf(name, namelen, "%x,0", target);
1968 	return (DDI_SUCCESS);
1969 }
1970 
1971 
1972 
1973 /* ****************** SCSA required entry points *********************** */
1974 
1975 /*
1976  * Implementation of scsi tran_tgt_init.
1977  * sata_scsi_tgt_init() initializes scsi_device structure
1978  *
1979  * If successful, DDI_SUCCESS is returned.
1980  * DDI_FAILURE is returned if addressed device does not exist
1981  */
1982 
1983 static int
1984 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1985     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1986 {
1987 #ifndef __lock_lint
1988 	_NOTE(ARGUNUSED(hba_dip))
1989 	_NOTE(ARGUNUSED(tgt_dip))
1990 #endif
1991 	sata_device_t		sata_device;
1992 	sata_drive_info_t	*sdinfo;
1993 	struct sata_id		*sid;
1994 	sata_hba_inst_t		*sata_hba_inst;
1995 	char			model[SATA_ID_MODEL_LEN + 1];
1996 	char			fw[SATA_ID_FW_LEN + 1];
1997 	char			*vid, *pid;
1998 	int			i;
1999 
2000 	/*
2001 	 * Fail tran_tgt_init for .conf stub node
2002 	 */
2003 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2004 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2005 		ddi_set_name_addr(tgt_dip, NULL);
2006 		return (DDI_FAILURE);
2007 	}
2008 
2009 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2010 
2011 	/* Validate scsi device address */
2012 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2013 	    &sata_device) != 0)
2014 		return (DDI_FAILURE);
2015 
2016 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2017 	    sata_device.satadev_addr.cport)));
2018 
2019 	/* sata_device now contains a valid sata address */
2020 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2021 	if (sdinfo == NULL) {
2022 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2023 		    sata_device.satadev_addr.cport)));
2024 		return (DDI_FAILURE);
2025 	}
2026 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2027 	    sata_device.satadev_addr.cport)));
2028 
2029 	/*
2030 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2031 	 * the target disks.
2032 	 *
2033 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2034 	 * if we need to create cmdk-style devid for all the disk devices
2035 	 * attached to this controller. This property may have been set
2036 	 * from HBA driver's .conf file or by the HBA driver in its
2037 	 * attach(9F) function.
2038 	 */
2039 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2040 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2041 	    "use-cmdk-devid-format", 0) == 1)) {
2042 		/* register a legacy devid for this target node */
2043 		sata_target_devid_register(tgt_dip, sdinfo);
2044 	}
2045 
2046 
2047 	/*
2048 	 * 'Identify Device Data' does not always fit in standard SCSI
2049 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2050 	 * of information.
2051 	 */
2052 	sid = &sdinfo->satadrv_id;
2053 #ifdef	_LITTLE_ENDIAN
2054 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2055 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2056 #else	/* _LITTLE_ENDIAN */
2057 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2058 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2059 #endif	/* _LITTLE_ENDIAN */
2060 	model[SATA_ID_MODEL_LEN] = 0;
2061 	fw[SATA_ID_FW_LEN] = 0;
2062 
2063 	/* split model into into vid/pid */
2064 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2065 		if ((*pid == ' ') || (*pid == '\t'))
2066 			break;
2067 	if (i < SATA_ID_MODEL_LEN) {
2068 		vid = model;
2069 		*pid++ = 0;		/* terminate vid, establish pid */
2070 	} else {
2071 		vid = NULL;		/* vid will stay "ATA     " */
2072 		pid = model;		/* model is all pid */
2073 	}
2074 
2075 	if (vid)
2076 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2077 		    vid, strlen(vid));
2078 	if (pid)
2079 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2080 		    pid, strlen(pid));
2081 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2082 	    fw, strlen(fw));
2083 
2084 	return (DDI_SUCCESS);
2085 }
2086 
2087 /*
2088  * Implementation of scsi tran_tgt_probe.
2089  * Probe target, by calling default scsi routine scsi_hba_probe()
2090  */
2091 static int
2092 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2093 {
2094 	sata_hba_inst_t *sata_hba_inst =
2095 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2096 	int rval;
2097 	uint32_t pm_cap;
2098 
2099 	rval = scsi_hba_probe(sd, callback);
2100 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2101 	    SATA_CAP_LOG_SENSE;
2102 
2103 	if (rval == SCSIPROBE_EXISTS) {
2104 		/*
2105 		 * Set property "pm-capable" on the target device node, so that
2106 		 * the target driver will not try to fetch scsi cycle counters
2107 		 * before enabling device power-management.
2108 		 */
2109 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2110 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2111 			sata_log(sata_hba_inst, CE_WARN,
2112 			    "SATA device at port %d: "
2113 			    "will not be power-managed ",
2114 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2115 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2116 			    "failure updating pm-capable property"));
2117 		}
2118 	}
2119 	return (rval);
2120 }
2121 
2122 /*
2123  * Implementation of scsi tran_tgt_free.
2124  * Release all resources allocated for scsi_device
2125  */
2126 static void
2127 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2128     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2129 {
2130 #ifndef __lock_lint
2131 	_NOTE(ARGUNUSED(hba_dip))
2132 #endif
2133 	sata_device_t		sata_device;
2134 	sata_drive_info_t	*sdinfo;
2135 	sata_hba_inst_t		*sata_hba_inst;
2136 	ddi_devid_t		devid;
2137 
2138 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2139 
2140 	/* Validate scsi device address */
2141 	/*
2142 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2143 	 * was a device at this address, so even if the sata framework internal
2144 	 * resources were alredy released because a device was detached,
2145 	 * this function should be executed as long as its actions do
2146 	 * not require the internal sata view of a device and the address
2147 	 * refers to a valid sata address.
2148 	 * Validating the address here means that we do not trust SCSA...
2149 	 */
2150 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2151 	    &sata_device) == -1)
2152 		return;
2153 
2154 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2155 	    sata_device.satadev_addr.cport)));
2156 
2157 	/* sata_device now should contain a valid sata address */
2158 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2159 	if (sdinfo == NULL) {
2160 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2161 		    sata_device.satadev_addr.cport)));
2162 		return;
2163 	}
2164 	/*
2165 	 * We did not allocate any resources in sata_scsi_tgt_init()
2166 	 * other than few properties.
2167 	 * Free them.
2168 	 */
2169 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2170 	    sata_device.satadev_addr.cport)));
2171 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2172 
2173 	/*
2174 	 * If devid was previously created but not freed up from
2175 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2176 	 */
2177 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2178 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2179 	    "use-cmdk-devid-format", 0) == 1) &&
2180 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2181 		ddi_devid_unregister(tgt_dip);
2182 		ddi_devid_free(devid);
2183 	}
2184 }
2185 
2186 /*
2187  * Implementation of scsi tran_init_pkt
2188  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2189  *
2190  * It seems that we should always allocate pkt, even if the address is
2191  * for non-existing device - just use some default for dma_attr.
2192  * The reason is that there is no way to communicate this to a caller here.
2193  * Subsequent call to sata_scsi_start may fail appropriately.
2194  * Simply returning NULL does not seem to discourage a target driver...
2195  *
2196  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2197  */
2198 static struct scsi_pkt *
2199 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2200     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2201     int (*callback)(caddr_t), caddr_t arg)
2202 {
2203 	sata_hba_inst_t *sata_hba_inst =
2204 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2205 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2206 	sata_device_t sata_device;
2207 	sata_drive_info_t *sdinfo;
2208 	sata_pkt_txlate_t *spx;
2209 	ddi_dma_attr_t cur_dma_attr;
2210 	int rval;
2211 	boolean_t new_pkt = B_TRUE;
2212 
2213 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2214 
2215 	/*
2216 	 * We need to translate the address, even if it could be
2217 	 * a bogus one, for a non-existing device
2218 	 */
2219 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2220 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2221 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2222 	sata_device.satadev_rev = SATA_DEVICE_REV;
2223 
2224 	if (pkt == NULL) {
2225 		/*
2226 		 * Have to allocate a brand new scsi packet.
2227 		 * We need to operate with auto request sense enabled.
2228 		 */
2229 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2230 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2231 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2232 
2233 		if (pkt == NULL)
2234 			return (NULL);
2235 
2236 		/* Fill scsi packet structure */
2237 		pkt->pkt_comp		= (void (*)())NULL;
2238 		pkt->pkt_time		= 0;
2239 		pkt->pkt_resid		= 0;
2240 		pkt->pkt_statistics	= 0;
2241 		pkt->pkt_reason		= 0;
2242 
2243 		/*
2244 		 * pkt_hba_private will point to sata pkt txlate structure
2245 		 */
2246 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2247 		bzero(spx, sizeof (sata_pkt_txlate_t));
2248 
2249 		spx->txlt_scsi_pkt = pkt;
2250 		spx->txlt_sata_hba_inst = sata_hba_inst;
2251 
2252 		/* Allocate sata_pkt */
2253 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2254 		if (spx->txlt_sata_pkt == NULL) {
2255 			/* Could not allocate sata pkt */
2256 			scsi_hba_pkt_free(ap, pkt);
2257 			return (NULL);
2258 		}
2259 		/* Set sata address */
2260 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2261 		    sata_device.satadev_addr;
2262 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2263 		    sata_device.satadev_rev;
2264 
2265 		if ((bp == NULL) || (bp->b_bcount == 0))
2266 			return (pkt);
2267 
2268 		spx->txlt_total_residue = bp->b_bcount;
2269 	} else {
2270 		new_pkt = B_FALSE;
2271 		/*
2272 		 * Packet was preallocated/initialized by previous call
2273 		 */
2274 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2275 
2276 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2277 			return (pkt);
2278 		}
2279 
2280 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2281 	}
2282 
2283 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2284 
2285 	/*
2286 	 * We use an adjusted version of the dma_attr, to account
2287 	 * for device addressing limitations.
2288 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2289 	 * happen when a device is not yet configured.
2290 	 */
2291 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2292 	    sata_device.satadev_addr.cport)));
2293 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2294 	    &spx->txlt_sata_pkt->satapkt_device);
2295 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2296 	sata_adjust_dma_attr(sdinfo,
2297 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2298 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2299 	    sata_device.satadev_addr.cport)));
2300 	/*
2301 	 * Allocate necessary DMA resources for the packet's data buffer
2302 	 * NOTE:
2303 	 * In case of read/write commands, DMA resource allocation here is
2304 	 * based on the premise that the transfer length specified in
2305 	 * the read/write scsi cdb will match exactly DMA resources -
2306 	 * returning correct packet residue is crucial.
2307 	 */
2308 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2309 	    &cur_dma_attr)) != DDI_SUCCESS) {
2310 		/*
2311 		 * If a DMA allocation request fails with
2312 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2313 		 * bioerror(9F) with bp and an error code of EFAULT.
2314 		 * If a DMA allocation request fails with
2315 		 * DDI_DMA_TOOBIG, indicate the error by calling
2316 		 * bioerror(9F) with bp and an error code of EINVAL.
2317 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2318 		 * Request may be repeated later - there is no real error.
2319 		 */
2320 		switch (rval) {
2321 		case DDI_DMA_NORESOURCES:
2322 			bioerror(bp, 0);
2323 			break;
2324 		case DDI_DMA_NOMAPPING:
2325 		case DDI_DMA_BADATTR:
2326 			bioerror(bp, EFAULT);
2327 			break;
2328 		case DDI_DMA_TOOBIG:
2329 		default:
2330 			bioerror(bp, EINVAL);
2331 			break;
2332 		}
2333 		goto fail;
2334 	}
2335 
2336 	if (sata_check_for_dma_error(dip, spx)) {
2337 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2338 		bioerror(bp, EFAULT);
2339 		goto fail;
2340 	}
2341 
2342 success:
2343 	/* Set number of bytes that are not yet accounted for */
2344 	pkt->pkt_resid = spx->txlt_total_residue;
2345 	ASSERT(pkt->pkt_resid >= 0);
2346 
2347 	return (pkt);
2348 
2349 fail:
2350 	if (new_pkt == B_TRUE) {
2351 		/*
2352 		 * Since this is a new packet, we can clean-up
2353 		 * everything
2354 		 */
2355 		sata_scsi_destroy_pkt(ap, pkt);
2356 	} else {
2357 		/*
2358 		 * This is a re-used packet. It will be target driver's
2359 		 * responsibility to eventually destroy it (which
2360 		 * will free allocated resources).
2361 		 * Here, we just "complete" the request, leaving
2362 		 * allocated resources intact, so the request may
2363 		 * be retried.
2364 		 */
2365 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2366 		sata_pkt_free(spx);
2367 	}
2368 	return (NULL);
2369 }
2370 
2371 /*
2372  * Implementation of scsi tran_start.
2373  * Translate scsi cmd into sata operation and return status.
2374  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2375  * are supported.
2376  * For SATA hard disks, supported scsi commands:
2377  * SCMD_INQUIRY
2378  * SCMD_TEST_UNIT_READY
2379  * SCMD_START_STOP
2380  * SCMD_READ_CAPACITY
2381  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2382  * SCMD_REQUEST_SENSE
2383  * SCMD_LOG_SENSE_G1
2384  * SCMD_LOG_SELECT_G1
2385  * SCMD_MODE_SENSE	(specific pages)
2386  * SCMD_MODE_SENSE_G1	(specific pages)
2387  * SCMD_MODE_SELECT	(specific pages)
2388  * SCMD_MODE_SELECT_G1	(specific pages)
2389  * SCMD_SYNCHRONIZE_CACHE
2390  * SCMD_SYNCHRONIZE_CACHE_G1
2391  * SCMD_READ
2392  * SCMD_READ_G1
2393  * SCMD_READ_G4
2394  * SCMD_READ_G5
2395  * SCMD_WRITE
2396  * SCMD_WRITE_BUFFER
2397  * SCMD_WRITE_G1
2398  * SCMD_WRITE_G4
2399  * SCMD_WRITE_G5
2400  * SCMD_SEEK		(noop)
2401  * SCMD_SDIAG
2402  *
2403  * All other commands are rejected as unsupported.
2404  *
2405  * Returns:
2406  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2407  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2408  * a callback could be scheduled.
2409  * TRAN_BADPKT if cmd was directed to invalid address.
2410  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2411  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2412  * was removed and there was no callback specified in scsi pkt.
2413  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2414  * framework was busy performing some other operation(s).
2415  *
2416  */
2417 static int
2418 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2419 {
2420 	sata_hba_inst_t *sata_hba_inst =
2421 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2422 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2423 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2424 	sata_drive_info_t *sdinfo;
2425 	struct buf *bp;
2426 	uint8_t cport, pmport;
2427 	boolean_t dev_gone = B_FALSE;
2428 	int rval;
2429 
2430 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2431 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2432 
2433 	ASSERT(spx != NULL &&
2434 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2435 
2436 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2437 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2438 
2439 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2440 
2441 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2442 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2443 		if (sdinfo == NULL ||
2444 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2445 		    cport_tgtnode_clean == B_FALSE ||
2446 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2447 			dev_gone = B_TRUE;
2448 		}
2449 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2450 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2451 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2452 		    cport) == NULL) {
2453 			dev_gone = B_TRUE;
2454 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2455 		    pmport) == NULL) {
2456 			dev_gone = B_TRUE;
2457 		} else {
2458 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2459 			    cport, pmport)));
2460 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2461 			if (sdinfo == NULL ||
2462 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2463 			    pmport_tgtnode_clean == B_FALSE ||
2464 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2465 				dev_gone = B_TRUE;
2466 			}
2467 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2468 			    cport, pmport)));
2469 		}
2470 	}
2471 
2472 	if (dev_gone == B_TRUE) {
2473 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2474 		pkt->pkt_reason = CMD_DEV_GONE;
2475 		/*
2476 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2477 		 * only in callback function (for normal requests) and
2478 		 * in the dump code path.
2479 		 * So, if the callback is available, we need to do
2480 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2481 		 */
2482 		if (pkt->pkt_comp != NULL) {
2483 			/* scsi callback required */
2484 			if (servicing_interrupt()) {
2485 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2486 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2487 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2488 				    NULL) {
2489 					return (TRAN_BUSY);
2490 				}
2491 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2492 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2493 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2494 				/* Scheduling the callback failed */
2495 				return (TRAN_BUSY);
2496 			}
2497 			return (TRAN_ACCEPT);
2498 		}
2499 		/* No callback available */
2500 		return (TRAN_FATAL_ERROR);
2501 	}
2502 
2503 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2504 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2505 		rval = sata_txlt_atapi(spx);
2506 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2507 		    "sata_scsi_start atapi: rval %d\n", rval);
2508 		return (rval);
2509 	}
2510 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2511 
2512 	/*
2513 	 * Checking for power state, if it was on
2514 	 * STOPPED state, then the drive is not capable
2515 	 * of processing media access command.  And
2516 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2517 	 * in the function for different power state.
2518 	 */
2519 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2520 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2521 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2522 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2523 		    SD_SCSI_ASC_LU_NOT_READY));
2524 	}
2525 
2526 	/* ATA Disk commands processing starts here */
2527 
2528 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2529 
2530 	switch (pkt->pkt_cdbp[0]) {
2531 
2532 	case SCMD_INQUIRY:
2533 		/* Mapped to identify device */
2534 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2535 			bp_mapin(bp);
2536 		rval = sata_txlt_inquiry(spx);
2537 		break;
2538 
2539 	case SCMD_TEST_UNIT_READY:
2540 		/*
2541 		 * SAT "SATA to ATA Translation" doc specifies translation
2542 		 * to ATA CHECK POWER MODE.
2543 		 */
2544 		rval = sata_txlt_test_unit_ready(spx);
2545 		break;
2546 
2547 	case SCMD_START_STOP:
2548 		/* Mapping depends on the command */
2549 		rval = sata_txlt_start_stop_unit(spx);
2550 		break;
2551 
2552 	case SCMD_READ_CAPACITY:
2553 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2554 			bp_mapin(bp);
2555 		rval = sata_txlt_read_capacity(spx);
2556 		break;
2557 
2558 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2559 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2560 			bp_mapin(bp);
2561 		rval = sata_txlt_read_capacity16(spx);
2562 		break;
2563 
2564 	case SCMD_REQUEST_SENSE:
2565 		/*
2566 		 * Always No Sense, since we force ARQ
2567 		 */
2568 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2569 			bp_mapin(bp);
2570 		rval = sata_txlt_request_sense(spx);
2571 		break;
2572 
2573 	case SCMD_LOG_SENSE_G1:
2574 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2575 			bp_mapin(bp);
2576 		rval = sata_txlt_log_sense(spx);
2577 		break;
2578 
2579 	case SCMD_LOG_SELECT_G1:
2580 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2581 			bp_mapin(bp);
2582 		rval = sata_txlt_log_select(spx);
2583 		break;
2584 
2585 	case SCMD_MODE_SENSE:
2586 	case SCMD_MODE_SENSE_G1:
2587 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2588 			bp_mapin(bp);
2589 		rval = sata_txlt_mode_sense(spx);
2590 		break;
2591 
2592 
2593 	case SCMD_MODE_SELECT:
2594 	case SCMD_MODE_SELECT_G1:
2595 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2596 			bp_mapin(bp);
2597 		rval = sata_txlt_mode_select(spx);
2598 		break;
2599 
2600 	case SCMD_SYNCHRONIZE_CACHE:
2601 	case SCMD_SYNCHRONIZE_CACHE_G1:
2602 		rval = sata_txlt_synchronize_cache(spx);
2603 		break;
2604 
2605 	case SCMD_READ:
2606 	case SCMD_READ_G1:
2607 	case SCMD_READ_G4:
2608 	case SCMD_READ_G5:
2609 		rval = sata_txlt_read(spx);
2610 		break;
2611 	case SCMD_WRITE_BUFFER:
2612 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2613 			bp_mapin(bp);
2614 		rval = sata_txlt_write_buffer(spx);
2615 		break;
2616 
2617 	case SCMD_WRITE:
2618 	case SCMD_WRITE_G1:
2619 	case SCMD_WRITE_G4:
2620 	case SCMD_WRITE_G5:
2621 		rval = sata_txlt_write(spx);
2622 		break;
2623 
2624 	case SCMD_SEEK:
2625 		rval = sata_txlt_nodata_cmd_immediate(spx);
2626 		break;
2627 
2628 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2629 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2630 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2631 			bp_mapin(bp);
2632 		rval = sata_txlt_ata_pass_thru(spx);
2633 		break;
2634 
2635 		/* Other cases will be filed later */
2636 		/* postponed until phase 2 of the development */
2637 	case SPC3_CMD_UNMAP:
2638 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2639 			bp_mapin(bp);
2640 		rval = sata_txlt_unmap(spx);
2641 		break;
2642 	default:
2643 		rval = sata_txlt_invalid_command(spx);
2644 		break;
2645 	}
2646 
2647 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2648 	    "sata_scsi_start: rval %d\n", rval);
2649 
2650 	return (rval);
2651 }
2652 
2653 /*
2654  * Implementation of scsi tran_abort.
2655  * Abort specific pkt or all packets.
2656  *
2657  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2658  *
2659  * May be called from an interrupt level.
2660  */
2661 static int
2662 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2663 {
2664 	sata_hba_inst_t *sata_hba_inst =
2665 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2666 	sata_device_t	sata_device;
2667 	sata_pkt_t	*sata_pkt;
2668 
2669 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2670 	    "sata_scsi_abort: %s at target: 0x%x\n",
2671 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2672 
2673 	/* Validate address */
2674 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2675 		/* Invalid address */
2676 		return (0);
2677 
2678 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2679 	    sata_device.satadev_addr.cport)));
2680 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2681 		/* invalid address */
2682 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2683 		    sata_device.satadev_addr.cport)));
2684 		return (0);
2685 	}
2686 	if (scsi_pkt == NULL) {
2687 		/*
2688 		 * Abort all packets.
2689 		 * Although we do not have specific packet, we still need
2690 		 * dummy packet structure to pass device address to HBA.
2691 		 * Allocate one, without sleeping. Fail if pkt cannot be
2692 		 * allocated.
2693 		 */
2694 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2695 		if (sata_pkt == NULL) {
2696 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2697 			    sata_device.satadev_addr.cport)));
2698 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2699 			    "could not allocate sata_pkt"));
2700 			return (0);
2701 		}
2702 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2703 		sata_pkt->satapkt_device = sata_device;
2704 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2705 	} else {
2706 		if (scsi_pkt->pkt_ha_private == NULL) {
2707 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2708 			    sata_device.satadev_addr.cport)));
2709 			return (0); /* Bad scsi pkt */
2710 		}
2711 		/* extract pointer to sata pkt */
2712 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2713 		    txlt_sata_pkt;
2714 	}
2715 
2716 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2717 	    sata_device.satadev_addr.cport)));
2718 	/* Send abort request to HBA */
2719 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2720 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2721 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2722 	    SATA_SUCCESS) {
2723 		if (scsi_pkt == NULL)
2724 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2725 		/* Success */
2726 		return (1);
2727 	}
2728 	/* Else, something did not go right */
2729 	if (scsi_pkt == NULL)
2730 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2731 	/* Failure */
2732 	return (0);
2733 }
2734 
2735 
2736 /*
2737  * Implementation of scsi tran_reset.
2738  * RESET_ALL request is translated into port reset.
2739  * RESET_TARGET requests is translated into a device reset,
2740  * RESET_LUN request is accepted only for LUN 0 and translated into
2741  * device reset.
2742  * The target reset should cause all HBA active and queued packets to
2743  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2744  * the return. HBA should report reset event for the device.
2745  *
2746  * Returns 1 upon success, 0 upon failure.
2747  */
2748 static int
2749 sata_scsi_reset(struct scsi_address *ap, int level)
2750 {
2751 	sata_hba_inst_t	*sata_hba_inst =
2752 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2753 	sata_device_t	sata_device;
2754 	int		val;
2755 
2756 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2757 	    "sata_scsi_reset: level %d target: 0x%x\n",
2758 	    level, ap->a_target);
2759 
2760 	/* Validate address */
2761 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2762 	if (val == -1)
2763 		/* Invalid address */
2764 		return (0);
2765 
2766 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2767 	    sata_device.satadev_addr.cport)));
2768 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2769 		/* invalid address */
2770 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2771 		    sata_device.satadev_addr.cport)));
2772 		return (0);
2773 	}
2774 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2775 	    sata_device.satadev_addr.cport)));
2776 	if (level == RESET_ALL) {
2777 		/* port reset */
2778 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2779 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2780 		else
2781 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2782 
2783 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2784 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2785 			return (1);
2786 		else
2787 			return (0);
2788 
2789 	} else if (val == 0 &&
2790 	    (level == RESET_TARGET || level == RESET_LUN)) {
2791 		/* reset device (device attached) */
2792 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2793 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2794 			return (1);
2795 		else
2796 			return (0);
2797 	}
2798 	return (0);
2799 }
2800 
2801 
2802 /*
2803  * Implementation of scsi tran_getcap (get transport/device capabilities).
2804  * Supported capabilities for SATA hard disks:
2805  * auto-rqsense		(always supported)
2806  * tagged-qing		(supported if HBA supports it)
2807  * untagged-qing	(could be supported if disk supports it, but because
2808  *			 caching behavior allowing untagged queuing actually
2809  *			 results in reduced performance.  sd tries to throttle
2810  *			 back to only 3 outstanding commands, which may
2811  *			 work for real SCSI disks, but with read ahead
2812  *			 caching, having more than 1 outstanding command
2813  *			 results in cache thrashing.)
2814  * sector_size
2815  * dma_max
2816  * interconnect-type	(INTERCONNECT_SATA)
2817  *
2818  * Supported capabilities for ATAPI CD/DVD devices:
2819  * auto-rqsense		(always supported)
2820  * sector_size
2821  * dma_max
2822  * max-cdb-length
2823  * interconnect-type	(INTERCONNECT_SATA)
2824  *
2825  * Supported capabilities for ATAPI TAPE devices:
2826  * auto-rqsense		(always supported)
2827  * dma_max
2828  * max-cdb-length
2829  *
2830  * Supported capabilities for SATA ATAPI hard disks:
2831  * auto-rqsense		(always supported)
2832  * interconnect-type	(INTERCONNECT_SATA)
2833  * max-cdb-length
2834  *
2835  * Request for other capabilities is rejected as unsupported.
2836  *
2837  * Returns supported capability value, or -1 if capability is unsuppported or
2838  * the address is invalid - no device.
2839  */
2840 
2841 static int
2842 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2843 {
2844 
2845 	sata_hba_inst_t 	*sata_hba_inst =
2846 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2847 	sata_device_t		sata_device;
2848 	sata_drive_info_t	*sdinfo;
2849 	ddi_dma_attr_t		adj_dma_attr;
2850 	int 			rval;
2851 
2852 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2853 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2854 	    ap->a_target, cap);
2855 
2856 	/*
2857 	 * We want to process the capabilities on per port granularity.
2858 	 * So, we are specifically restricting ourselves to whom != 0
2859 	 * to exclude the controller wide handling.
2860 	 */
2861 	if (cap == NULL || whom == 0)
2862 		return (-1);
2863 
2864 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2865 		/* Invalid address */
2866 		return (-1);
2867 	}
2868 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2869 	    sata_device.satadev_addr.cport)));
2870 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2871 	    NULL) {
2872 		/* invalid address */
2873 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2874 		    sata_device.satadev_addr.cport)));
2875 		return (-1);
2876 	}
2877 
2878 	switch (scsi_hba_lookup_capstr(cap)) {
2879 	case SCSI_CAP_ARQ:
2880 		rval = 1;		/* ARQ supported, turned on */
2881 		break;
2882 
2883 	case SCSI_CAP_SECTOR_SIZE:
2884 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2885 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2886 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2887 			rval = SATA_ATAPI_SECTOR_SIZE;
2888 		else rval = -1;
2889 		break;
2890 
2891 	/*
2892 	 * untagged queuing cause a performance inversion because of
2893 	 * the way sd operates.  Because of this reason we do not
2894 	 * use it when available.
2895 	 */
2896 	case SCSI_CAP_UNTAGGED_QING:
2897 		if (sdinfo->satadrv_features_enabled &
2898 		    SATA_DEV_F_E_UNTAGGED_QING)
2899 			rval = 1;	/* Untagged queuing available */
2900 		else
2901 			rval = -1;	/* Untagged queuing not available */
2902 		break;
2903 
2904 	case SCSI_CAP_TAGGED_QING:
2905 		if ((sdinfo->satadrv_features_enabled &
2906 		    SATA_DEV_F_E_TAGGED_QING) &&
2907 		    (sdinfo->satadrv_max_queue_depth > 1))
2908 			rval = 1;	/* Tagged queuing available */
2909 		else
2910 			rval = -1;	/* Tagged queuing not available */
2911 		break;
2912 
2913 	case SCSI_CAP_DMA_MAX:
2914 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2915 		    &adj_dma_attr);
2916 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2917 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2918 		break;
2919 
2920 	case SCSI_CAP_INTERCONNECT_TYPE:
2921 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2922 		break;
2923 
2924 	case SCSI_CAP_CDB_LEN:
2925 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2926 			rval = sdinfo->satadrv_atapi_cdb_len;
2927 		else
2928 			rval = -1;
2929 		break;
2930 
2931 	default:
2932 		rval = -1;
2933 		break;
2934 	}
2935 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2936 	    sata_device.satadev_addr.cport)));
2937 	return (rval);
2938 }
2939 
2940 /*
2941  * Implementation of scsi tran_setcap
2942  *
2943  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2944  *
2945  */
2946 static int
2947 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2948 {
2949 	sata_hba_inst_t	*sata_hba_inst =
2950 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2951 	sata_device_t	sata_device;
2952 	sata_drive_info_t	*sdinfo;
2953 	int		rval;
2954 
2955 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2956 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2957 
2958 	/*
2959 	 * We want to process the capabilities on per port granularity.
2960 	 * So, we are specifically restricting ourselves to whom != 0
2961 	 * to exclude the controller wide handling.
2962 	 */
2963 	if (cap == NULL || whom == 0) {
2964 		return (-1);
2965 	}
2966 
2967 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2968 		/* Invalid address */
2969 		return (-1);
2970 	}
2971 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2972 	    sata_device.satadev_addr.cport)));
2973 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2974 	    &sata_device)) == NULL) {
2975 		/* invalid address */
2976 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2977 		    sata_device.satadev_addr.cport)));
2978 		return (-1);
2979 	}
2980 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2981 	    sata_device.satadev_addr.cport)));
2982 
2983 	switch (scsi_hba_lookup_capstr(cap)) {
2984 	case SCSI_CAP_ARQ:
2985 	case SCSI_CAP_SECTOR_SIZE:
2986 	case SCSI_CAP_DMA_MAX:
2987 	case SCSI_CAP_INTERCONNECT_TYPE:
2988 		rval = 0;
2989 		break;
2990 	case SCSI_CAP_UNTAGGED_QING:
2991 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2992 			rval = 1;
2993 			if (value == 1) {
2994 				sdinfo->satadrv_features_enabled |=
2995 				    SATA_DEV_F_E_UNTAGGED_QING;
2996 			} else if (value == 0) {
2997 				sdinfo->satadrv_features_enabled &=
2998 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2999 			} else {
3000 				rval = -1;
3001 			}
3002 		} else {
3003 			rval = 0;
3004 		}
3005 		break;
3006 	case SCSI_CAP_TAGGED_QING:
3007 		/* This can TCQ or NCQ */
3008 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3009 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3010 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3011 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3012 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3013 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3014 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3015 			rval = 1;
3016 			if (value == 1) {
3017 				sdinfo->satadrv_features_enabled |=
3018 				    SATA_DEV_F_E_TAGGED_QING;
3019 			} else if (value == 0) {
3020 				sdinfo->satadrv_features_enabled &=
3021 				    ~SATA_DEV_F_E_TAGGED_QING;
3022 			} else {
3023 				rval = -1;
3024 			}
3025 		} else {
3026 			rval = 0;
3027 		}
3028 		break;
3029 	default:
3030 		rval = -1;
3031 		break;
3032 	}
3033 	return (rval);
3034 }
3035 
3036 /*
3037  * Implementations of scsi tran_destroy_pkt.
3038  * Free resources allocated by sata_scsi_init_pkt()
3039  */
3040 static void
3041 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3042 {
3043 	sata_pkt_txlate_t *spx;
3044 
3045 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3046 
3047 	sata_common_free_dma_rsrcs(spx);
3048 
3049 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3050 	sata_pkt_free(spx);
3051 
3052 	scsi_hba_pkt_free(ap, pkt);
3053 }
3054 
3055 /*
3056  * Implementation of scsi tran_dmafree.
3057  * Free DMA resources allocated by sata_scsi_init_pkt()
3058  */
3059 
3060 static void
3061 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3062 {
3063 #ifndef __lock_lint
3064 	_NOTE(ARGUNUSED(ap))
3065 #endif
3066 	sata_pkt_txlate_t *spx;
3067 
3068 	ASSERT(pkt != NULL);
3069 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3070 
3071 	sata_common_free_dma_rsrcs(spx);
3072 }
3073 
3074 /*
3075  * Implementation of scsi tran_sync_pkt.
3076  *
3077  * The assumption below is that pkt is unique - there is no need to check ap
3078  *
3079  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3080  * into/from the real buffer.
3081  */
3082 static void
3083 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3084 {
3085 #ifndef __lock_lint
3086 	_NOTE(ARGUNUSED(ap))
3087 #endif
3088 	int rval;
3089 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3090 	struct buf *bp;
3091 	int direction;
3092 
3093 	ASSERT(spx != NULL);
3094 	if (spx->txlt_buf_dma_handle != NULL) {
3095 		direction = spx->txlt_sata_pkt->
3096 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3097 		if (spx->txlt_sata_pkt != NULL &&
3098 		    direction != SATA_DIR_NODATA_XFER) {
3099 			if (spx->txlt_tmp_buf != NULL) {
3100 				/* Intermediate DMA buffer used */
3101 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3102 
3103 				if (direction & SATA_DIR_WRITE) {
3104 					bcopy(bp->b_un.b_addr,
3105 					    spx->txlt_tmp_buf, bp->b_bcount);
3106 				}
3107 			}
3108 			/* Sync the buffer for device or for CPU */
3109 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3110 			    (direction & SATA_DIR_WRITE) ?
3111 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3112 			ASSERT(rval == DDI_SUCCESS);
3113 			if (spx->txlt_tmp_buf != NULL &&
3114 			    !(direction & SATA_DIR_WRITE)) {
3115 				/* Intermediate DMA buffer used for read */
3116 				bcopy(spx->txlt_tmp_buf,
3117 				    bp->b_un.b_addr, bp->b_bcount);
3118 			}
3119 
3120 		}
3121 	}
3122 }
3123 
3124 
3125 
3126 /* *******************  SATA - SCSI Translation functions **************** */
3127 /*
3128  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3129  * translation.
3130  */
3131 
3132 /*
3133  * Checks if a device exists and can be access and translates common
3134  * scsi_pkt data to sata_pkt data.
3135  *
3136  * Flag argument indicates that a non-read/write ATA command may be sent
3137  * to HBA in arbitrary SYNC mode to execute this packet.
3138  *
3139  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3140  * sata_pkt was set-up.
3141  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3142  * exist and pkt_comp callback was scheduled.
3143  * Returns other TRAN_XXXXX values when error occured and command should be
3144  * rejected with the returned TRAN_XXXXX value.
3145  *
3146  * This function should be called with port mutex held.
3147  */
3148 static int
3149 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3150 {
3151 	sata_drive_info_t *sdinfo;
3152 	sata_device_t sata_device;
3153 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3154 		SATA_DIR_NODATA_XFER,
3155 		/* all other values to 0/FALSE */
3156 	};
3157 	/*
3158 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3159 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3160 	 * indicates that the scsi packet was not accepted (the reason will not
3161 	 * be checked by the scsi target driver).
3162 	 * To make debugging easier, we set pkt_reason to know value here.
3163 	 * It may be changed later when different completion reason is
3164 	 * determined.
3165 	 */
3166 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3167 	*reason = CMD_TRAN_ERR;
3168 
3169 	/* Validate address */
3170 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3171 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3172 
3173 	case -1:
3174 		/* Invalid address or invalid device type */
3175 		return (TRAN_BADPKT);
3176 	case 2:
3177 		/*
3178 		 * Valid address but device type is unknown - Chack if it is
3179 		 * in the reset state and therefore in an indeterminate state.
3180 		 */
3181 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3182 		    &spx->txlt_sata_pkt->satapkt_device);
3183 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3184 		    (SATA_EVNT_DEVICE_RESET |
3185 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3186 			if (!ddi_in_panic()) {
3187 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3188 				*reason = CMD_INCOMPLETE;
3189 				SATADBG1(SATA_DBG_SCSI_IF,
3190 				    spx->txlt_sata_hba_inst,
3191 				    "sata_scsi_start: rejecting command "
3192 				    "because of device reset state\n", NULL);
3193 				return (TRAN_BUSY);
3194 			}
3195 		}
3196 		/* FALLTHROUGH */
3197 	case 1:
3198 		/* valid address but no valid device - it has disappeared */
3199 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3200 		*reason = CMD_DEV_GONE;
3201 		/*
3202 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3203 		 * only in callback function (for normal requests) and
3204 		 * in the dump code path.
3205 		 * So, if the callback is available, we need to do
3206 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3207 		 */
3208 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3209 			/* scsi callback required */
3210 			if (servicing_interrupt()) {
3211 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3212 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3213 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3214 				    NULL) {
3215 					return (TRAN_BUSY);
3216 				}
3217 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3218 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3219 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3220 				/* Scheduling the callback failed */
3221 				return (TRAN_BUSY);
3222 			}
3223 
3224 			return (TRAN_ACCEPT);
3225 		}
3226 		return (TRAN_FATAL_ERROR);
3227 	default:
3228 		/* all OK; pkt reason will be overwritten later */
3229 		break;
3230 	}
3231 	/*
3232 	 * If pkt is to be executed in polling mode and a command will not be
3233 	 * emulated in SATA module (requires sending a non-read/write ATA
3234 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3235 	 * interrupt context and not in the panic dump, then reject the packet
3236 	 * to avoid a possible interrupt stack overrun or hang caused by
3237 	 * a potentially blocked interrupt.
3238 	 */
3239 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3240 	    servicing_interrupt() && !ddi_in_panic()) {
3241 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3242 		    "sata_scsi_start: rejecting synchronous command because "
3243 		    "of interrupt context\n", NULL);
3244 		return (TRAN_BUSY);
3245 	}
3246 
3247 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3248 	    &spx->txlt_sata_pkt->satapkt_device);
3249 
3250 	/*
3251 	 * If device is in reset condition, reject the packet with
3252 	 * TRAN_BUSY, unless:
3253 	 * 1. system is panicking (dumping)
3254 	 * In such case only one thread is running and there is no way to
3255 	 * process reset.
3256 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3257 	 * Some cfgadm operations involve drive commands, so reset condition
3258 	 * needs to be ignored for IOCTL operations.
3259 	 */
3260 	if ((sdinfo->satadrv_event_flags &
3261 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3262 
3263 		if (!ddi_in_panic() &&
3264 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3265 		    sata_device.satadev_addr.cport) &
3266 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3267 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3268 			*reason = CMD_INCOMPLETE;
3269 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3270 			    "sata_scsi_start: rejecting command because "
3271 			    "of device reset state\n", NULL);
3272 			return (TRAN_BUSY);
3273 		}
3274 	}
3275 
3276 	/*
3277 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3278 	 * sata_scsi_pkt_init() because pkt init had to work also with
3279 	 * non-existing devices.
3280 	 * Now we know that the packet was set-up for a real device, so its
3281 	 * type is known.
3282 	 */
3283 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3284 
3285 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3286 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3287 	    sata_device.satadev_addr.cport)->cport_event_flags &
3288 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3289 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3290 		    sata_ignore_dev_reset = B_TRUE;
3291 	}
3292 	/*
3293 	 * At this point the generic translation routine determined that the
3294 	 * scsi packet should be accepted. Packet completion reason may be
3295 	 * changed later when a different completion reason is determined.
3296 	 */
3297 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3298 	*reason = CMD_CMPLT;
3299 
3300 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3301 		/* Synchronous execution */
3302 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3303 		    SATA_OPMODE_POLLING;
3304 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3305 		    sata_ignore_dev_reset = ddi_in_panic();
3306 	} else {
3307 		/* Asynchronous execution */
3308 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3309 		    SATA_OPMODE_INTERRUPTS;
3310 	}
3311 	/* Convert queuing information */
3312 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3313 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3314 		    B_TRUE;
3315 	else if (spx->txlt_scsi_pkt->pkt_flags &
3316 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3317 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3318 		    B_TRUE;
3319 
3320 	/* Always limit pkt time */
3321 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3322 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3323 	else
3324 		/* Pass on scsi_pkt time */
3325 		spx->txlt_sata_pkt->satapkt_time =
3326 		    spx->txlt_scsi_pkt->pkt_time;
3327 
3328 	return (TRAN_ACCEPT);
3329 }
3330 
3331 
3332 /*
3333  * Translate ATA Identify Device data to SCSI Inquiry data.
3334  * This function may be called only for ATA devices.
3335  * This function should not be called for ATAPI devices - they
3336  * respond directly to SCSI Inquiry command.
3337  *
3338  * SATA Identify Device data has to be valid in sata_drive_info.
3339  * Buffer has to accomodate the inquiry length (36 bytes).
3340  *
3341  * This function should be called with a port mutex held.
3342  */
3343 static	void
3344 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3345     sata_drive_info_t *sdinfo, uint8_t *buf)
3346 {
3347 
3348 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3349 	struct sata_id *sid = &sdinfo->satadrv_id;
3350 
3351 	/* Start with a nice clean slate */
3352 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3353 
3354 	/*
3355 	 * Rely on the dev_type for setting paripheral qualifier.
3356 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3357 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3358 	 * ATAPI Inquiry may provide more data to the target driver.
3359 	 */
3360 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3361 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3362 
3363 	/* CFA type device is not a removable media device */
3364 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3365 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3366 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3367 	inq->inq_iso = 0;	/* ISO version */
3368 	inq->inq_ecma = 0;	/* ECMA version */
3369 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3370 	inq->inq_aenc = 0;	/* Async event notification cap. */
3371 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3372 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3373 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3374 	inq->inq_len = 31;	/* Additional length */
3375 	inq->inq_dualp = 0;	/* dual port device - NO */
3376 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3377 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3378 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3379 				/*
3380 				 * Queuing support - controller has to
3381 				 * support some sort of command queuing.
3382 				 */
3383 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3384 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3385 	else
3386 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3387 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3388 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3389 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3390 
3391 #ifdef	_LITTLE_ENDIAN
3392 	/* Swap text fields to match SCSI format */
3393 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3394 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3395 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3396 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3397 	else
3398 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3399 #else	/* _LITTLE_ENDIAN */
3400 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3401 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3402 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3403 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3404 	else
3405 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3406 #endif	/* _LITTLE_ENDIAN */
3407 }
3408 
3409 
3410 /*
3411  * Scsi response set up for invalid command (command not supported)
3412  *
3413  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3414  */
3415 static int
3416 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3417 {
3418 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3419 	struct scsi_extended_sense *sense;
3420 
3421 	scsipkt->pkt_reason = CMD_CMPLT;
3422 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3423 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3424 
3425 	*scsipkt->pkt_scbp = STATUS_CHECK;
3426 
3427 	sense = sata_arq_sense(spx);
3428 	sense->es_key = KEY_ILLEGAL_REQUEST;
3429 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3430 
3431 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3432 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3433 
3434 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3435 	    scsipkt->pkt_comp != NULL) {
3436 		/* scsi callback required */
3437 		if (servicing_interrupt()) {
3438 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3439 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3440 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3441 				return (TRAN_BUSY);
3442 			}
3443 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3444 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3445 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3446 			/* Scheduling the callback failed */
3447 			return (TRAN_BUSY);
3448 		}
3449 	}
3450 	return (TRAN_ACCEPT);
3451 }
3452 
3453 /*
3454  * Scsi response set up for check condition with special sense key
3455  * and additional sense code.
3456  *
3457  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3458  */
3459 static int
3460 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3461 {
3462 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3463 	int cport = SATA_TXLT_CPORT(spx);
3464 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3465 	struct scsi_extended_sense *sense;
3466 
3467 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3468 	scsipkt->pkt_reason = CMD_CMPLT;
3469 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3470 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3471 
3472 	*scsipkt->pkt_scbp = STATUS_CHECK;
3473 
3474 	sense = sata_arq_sense(spx);
3475 	sense->es_key = key;
3476 	sense->es_add_code = code;
3477 
3478 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3479 
3480 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3481 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3482 
3483 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3484 	    scsipkt->pkt_comp != NULL) {
3485 		/* scsi callback required */
3486 		if (servicing_interrupt()) {
3487 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3488 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3489 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3490 				return (TRAN_BUSY);
3491 			}
3492 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3493 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3494 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3495 			/* Scheduling the callback failed */
3496 			return (TRAN_BUSY);
3497 		}
3498 	}
3499 	return (TRAN_ACCEPT);
3500 }
3501 
3502 /*
3503  * Scsi response setup for
3504  * emulated non-data command that requires no action/return data
3505  *
3506  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3507  */
3508 static	int
3509 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3510 {
3511 	int rval;
3512 	int reason;
3513 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3514 
3515 	mutex_enter(cport_mutex);
3516 
3517 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3518 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3519 		mutex_exit(cport_mutex);
3520 		return (rval);
3521 	}
3522 	mutex_exit(cport_mutex);
3523 
3524 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3525 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3526 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3527 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3528 
3529 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3530 	    "Scsi_pkt completion reason %x\n",
3531 	    spx->txlt_scsi_pkt->pkt_reason);
3532 
3533 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3534 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3535 		/* scsi callback required */
3536 		if (servicing_interrupt()) {
3537 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3538 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3539 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3540 				return (TRAN_BUSY);
3541 			}
3542 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3543 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3544 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3545 			/* Scheduling the callback failed */
3546 			return (TRAN_BUSY);
3547 		}
3548 	}
3549 	return (TRAN_ACCEPT);
3550 }
3551 
3552 
3553 /*
3554  * SATA translate command: Inquiry / Identify Device
3555  * Use cached Identify Device data for now, rather than issuing actual
3556  * Device Identify cmd request. If device is detached and re-attached,
3557  * asynchronous event processing should fetch and refresh Identify Device
3558  * data.
3559  * VPD pages supported now:
3560  * Vital Product Data page
3561  * Unit Serial Number page
3562  * Block Device Characteristics Page
3563  * ATA Information Page
3564  *
3565  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3566  */
3567 
3568 #define	EVPD			1	/* Extended Vital Product Data flag */
3569 #define	CMDDT			2	/* Command Support Data - Obsolete */
3570 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3571 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3572 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3573 					/* Code */
3574 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3575 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3576 
3577 static int
3578 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3579 {
3580 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3581 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3582 	sata_drive_info_t *sdinfo;
3583 	struct scsi_extended_sense *sense;
3584 	int count;
3585 	uint8_t *p;
3586 	int i, j;
3587 	uint8_t page_buf[1024]; /* Max length */
3588 	int rval, reason;
3589 	ushort_t rate;
3590 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3591 
3592 	mutex_enter(cport_mutex);
3593 
3594 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3595 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3596 		mutex_exit(cport_mutex);
3597 		return (rval);
3598 	}
3599 
3600 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3601 	    &spx->txlt_sata_pkt->satapkt_device);
3602 
3603 	ASSERT(sdinfo != NULL);
3604 
3605 	scsipkt->pkt_reason = CMD_CMPLT;
3606 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3607 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3608 
3609 	/* Reject not supported request */
3610 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3611 		*scsipkt->pkt_scbp = STATUS_CHECK;
3612 		sense = sata_arq_sense(spx);
3613 		sense->es_key = KEY_ILLEGAL_REQUEST;
3614 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3615 		goto done;
3616 	}
3617 
3618 	/* Valid Inquiry request */
3619 	*scsipkt->pkt_scbp = STATUS_GOOD;
3620 
3621 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3622 
3623 		/*
3624 		 * Because it is fully emulated command storing data
3625 		 * programatically in the specified buffer, release
3626 		 * preallocated DMA resources before storing data in the buffer,
3627 		 * so no unwanted DMA sync would take place.
3628 		 */
3629 		sata_scsi_dmafree(NULL, scsipkt);
3630 
3631 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3632 			/* Standard Inquiry Data request */
3633 			struct scsi_inquiry inq;
3634 			unsigned int bufsize;
3635 
3636 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3637 			    sdinfo, (uint8_t *)&inq);
3638 			/* Copy no more than requested */
3639 			count = MIN(bp->b_bcount,
3640 			    sizeof (struct scsi_inquiry));
3641 			bufsize = scsipkt->pkt_cdbp[4];
3642 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3643 			count = MIN(count, bufsize);
3644 			bcopy(&inq, bp->b_un.b_addr, count);
3645 
3646 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3647 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3648 			    bufsize - count : 0;
3649 		} else {
3650 			/*
3651 			 * peripheral_qualifier = 0;
3652 			 *
3653 			 * We are dealing only with HD and will be
3654 			 * dealing with CD/DVD devices soon
3655 			 */
3656 			uint8_t peripheral_device_type =
3657 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3658 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3659 
3660 			bzero(page_buf, sizeof (page_buf));
3661 
3662 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3663 			case INQUIRY_SUP_VPD_PAGE:
3664 				/*
3665 				 * Request for supported Vital Product Data
3666 				 * pages.
3667 				 */
3668 				page_buf[0] = peripheral_device_type;
3669 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3670 				page_buf[2] = 0;
3671 				page_buf[3] = 4; /* page length */
3672 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3673 				page_buf[5] = INQUIRY_USN_PAGE;
3674 				page_buf[6] = INQUIRY_BDC_PAGE;
3675 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3676 				/* Copy no more than requested */
3677 				count = MIN(bp->b_bcount, 8);
3678 				bcopy(page_buf, bp->b_un.b_addr, count);
3679 				break;
3680 
3681 			case INQUIRY_USN_PAGE:
3682 				/*
3683 				 * Request for Unit Serial Number page.
3684 				 * Set-up the page.
3685 				 */
3686 				page_buf[0] = peripheral_device_type;
3687 				page_buf[1] = INQUIRY_USN_PAGE;
3688 				page_buf[2] = 0;
3689 				/* remaining page length */
3690 				page_buf[3] = SATA_ID_SERIAL_LEN;
3691 
3692 				/*
3693 				 * Copy serial number from Identify Device data
3694 				 * words into the inquiry page and swap bytes
3695 				 * when necessary.
3696 				 */
3697 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3698 #ifdef	_LITTLE_ENDIAN
3699 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3700 #else
3701 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3702 #endif
3703 				/*
3704 				 * Least significant character of the serial
3705 				 * number shall appear as the last byte,
3706 				 * according to SBC-3 spec.
3707 				 * Count trailing spaces to determine the
3708 				 * necessary shift length.
3709 				 */
3710 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3711 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3712 					if (*(p - j) != '\0' &&
3713 					    *(p - j) != '\040')
3714 						break;
3715 				}
3716 
3717 				/*
3718 				 * Shift SN string right, so that the last
3719 				 * non-blank character would appear in last
3720 				 * byte of SN field in the page.
3721 				 * 'j' is the shift length.
3722 				 */
3723 				for (i = 0;
3724 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3725 				    i++, p--)
3726 					*p = *(p - j);
3727 
3728 				/*
3729 				 * Add leading spaces - same number as the
3730 				 * shift size
3731 				 */
3732 				for (; j > 0; j--)
3733 					page_buf[4 + j - 1] = '\040';
3734 
3735 				count = MIN(bp->b_bcount,
3736 				    SATA_ID_SERIAL_LEN + 4);
3737 				bcopy(page_buf, bp->b_un.b_addr, count);
3738 				break;
3739 
3740 			case INQUIRY_BDC_PAGE:
3741 				/*
3742 				 * Request for Block Device Characteristics
3743 				 * page.  Set-up the page.
3744 				 */
3745 				page_buf[0] = peripheral_device_type;
3746 				page_buf[1] = INQUIRY_BDC_PAGE;
3747 				page_buf[2] = 0;
3748 				/* remaining page length */
3749 				page_buf[3] = SATA_ID_BDC_LEN;
3750 
3751 				rate = sdinfo->satadrv_id.ai_medrotrate;
3752 				page_buf[4] = (rate >> 8) & 0xff;
3753 				page_buf[5] = rate & 0xff;
3754 				page_buf[6] = 0;
3755 				page_buf[7] = sdinfo->satadrv_id.
3756 				    ai_nomformfactor & 0xf;
3757 
3758 				count = MIN(bp->b_bcount,
3759 				    SATA_ID_BDC_LEN + 4);
3760 				bcopy(page_buf, bp->b_un.b_addr, count);
3761 				break;
3762 
3763 			case INQUIRY_ATA_INFO_PAGE:
3764 				/*
3765 				 * Request for ATA Information page.
3766 				 */
3767 				page_buf[0] = peripheral_device_type;
3768 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3769 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3770 				    0xff;
3771 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3772 				/* page_buf[4-7] reserved */
3773 #ifdef  _LITTLE_ENDIAN
3774 				bcopy("ATA     ", &page_buf[8], 8);
3775 				swab(sdinfo->satadrv_id.ai_model,
3776 				    &page_buf[16], 16);
3777 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3778 				    "    ", 4) == 0) {
3779 					swab(sdinfo->satadrv_id.ai_fw,
3780 					    &page_buf[32], 4);
3781 				} else {
3782 					swab(&sdinfo->satadrv_id.ai_fw[4],
3783 					    &page_buf[32], 4);
3784 				}
3785 #else   /* _LITTLE_ENDIAN */
3786 				bcopy("ATA     ", &page_buf[8], 8);
3787 				bcopy(sdinfo->satadrv_id.ai_model,
3788 				    &page_buf[16], 16);
3789 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3790 				    "    ", 4) == 0) {
3791 					bcopy(sdinfo->satadrv_id.ai_fw,
3792 					    &page_buf[32], 4);
3793 				} else {
3794 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3795 					    &page_buf[32], 4);
3796 				}
3797 #endif  /* _LITTLE_ENDIAN */
3798 				/*
3799 				 * page_buf[36-55] which defines the device
3800 				 * signature is not defined at this
3801 				 * time.
3802 				 */
3803 
3804 				/* Set the command code */
3805 				if (sdinfo->satadrv_type ==
3806 				    SATA_DTYPE_ATADISK) {
3807 					page_buf[56] = SATAC_ID_DEVICE;
3808 				} else if (sdinfo->satadrv_type ==
3809 				    SATA_DTYPE_ATAPI) {
3810 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3811 				}
3812 				/*
3813 				 * If the command code, page_buf[56], is not
3814 				 * zero and if one of the identify commands
3815 				 * succeeds, return the identify data.
3816 				 */
3817 				if ((page_buf[56] != 0) &&
3818 				    (sata_fetch_device_identify_data(
3819 				    spx->txlt_sata_hba_inst, sdinfo) ==
3820 				    SATA_SUCCESS)) {
3821 					bcopy(&sdinfo->satadrv_id,
3822 					    &page_buf[60], sizeof (sata_id_t));
3823 				}
3824 
3825 				/* Need to copy out the page_buf to bp */
3826 				count = MIN(bp->b_bcount,
3827 				    SATA_ID_ATA_INFO_LEN + 4);
3828 				bcopy(page_buf, bp->b_un.b_addr, count);
3829 				break;
3830 
3831 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3832 				/*
3833 				 * We may want to implement this page, when
3834 				 * identifiers are common for SATA devices
3835 				 * But not now.
3836 				 */
3837 				/*FALLTHROUGH*/
3838 
3839 			default:
3840 				/* Request for unsupported VPD page */
3841 				*scsipkt->pkt_scbp = STATUS_CHECK;
3842 				sense = sata_arq_sense(spx);
3843 				sense->es_key = KEY_ILLEGAL_REQUEST;
3844 				sense->es_add_code =
3845 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3846 				goto done;
3847 			}
3848 		}
3849 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3850 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3851 		    scsipkt->pkt_cdbp[4] - count : 0;
3852 	}
3853 done:
3854 	mutex_exit(cport_mutex);
3855 
3856 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3857 	    "Scsi_pkt completion reason %x\n",
3858 	    scsipkt->pkt_reason);
3859 
3860 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3861 	    scsipkt->pkt_comp != NULL) {
3862 		/* scsi callback required */
3863 		if (servicing_interrupt()) {
3864 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3865 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3866 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3867 				return (TRAN_BUSY);
3868 			}
3869 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3870 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3871 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3872 			/* Scheduling the callback failed */
3873 			return (TRAN_BUSY);
3874 		}
3875 	}
3876 	return (TRAN_ACCEPT);
3877 }
3878 
3879 /*
3880  * SATA translate command: Request Sense.
3881  *
3882  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3883  * At the moment this is an emulated command (ATA version for SATA hard disks).
3884  * May be translated into Check Power Mode command in the future.
3885  *
3886  * Note: There is a mismatch between already implemented Informational
3887  * Exception Mode Select page 0x1C and this function.
3888  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3889  * NO SENSE and set additional sense code to the exception code - this is not
3890  * implemented here.
3891  */
3892 static int
3893 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3894 {
3895 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3896 	struct scsi_extended_sense sense;
3897 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3898 	sata_drive_info_t *sdinfo;
3899 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3900 	int rval, reason, power_state = 0;
3901 	kmutex_t *cport_mutex;
3902 
3903 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3904 	mutex_enter(cport_mutex);
3905 
3906 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3907 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3908 		mutex_exit(cport_mutex);
3909 		return (rval);
3910 	}
3911 
3912 	scsipkt->pkt_reason = CMD_CMPLT;
3913 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3914 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3915 	*scsipkt->pkt_scbp = STATUS_GOOD;
3916 
3917 	/*
3918 	 * when CONTROL field's NACA bit == 1
3919 	 * return ILLEGAL_REQUEST
3920 	 */
3921 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3922 		mutex_exit(cport_mutex);
3923 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3924 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3925 	}
3926 
3927 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3928 	    &spx->txlt_sata_pkt->satapkt_device);
3929 	ASSERT(sdinfo != NULL);
3930 
3931 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3932 
3933 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3934 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3935 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3936 	if (sata_hba_start(spx, &rval) != 0) {
3937 		mutex_exit(cport_mutex);
3938 		return (rval);
3939 	}
3940 	if (scmd->satacmd_error_reg != 0) {
3941 		mutex_exit(cport_mutex);
3942 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3943 		    SD_SCSI_ASC_NO_ADD_SENSE));
3944 	}
3945 
3946 	switch (scmd->satacmd_sec_count_lsb) {
3947 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3948 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3949 			power_state = SATA_POWER_STOPPED;
3950 		else {
3951 			power_state = SATA_POWER_STANDBY;
3952 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3953 		}
3954 		break;
3955 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3956 		power_state = SATA_POWER_IDLE;
3957 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3958 		break;
3959 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3960 	default:		  /* 0x40, 0x41 active mode */
3961 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3962 			power_state = SATA_POWER_IDLE;
3963 		else {
3964 			power_state = SATA_POWER_ACTIVE;
3965 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3966 		}
3967 		break;
3968 	}
3969 
3970 	mutex_exit(cport_mutex);
3971 
3972 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3973 		/*
3974 		 * Because it is fully emulated command storing data
3975 		 * programatically in the specified buffer, release
3976 		 * preallocated DMA resources before storing data in the buffer,
3977 		 * so no unwanted DMA sync would take place.
3978 		 */
3979 		int count = MIN(bp->b_bcount,
3980 		    sizeof (struct scsi_extended_sense));
3981 		sata_scsi_dmafree(NULL, scsipkt);
3982 		bzero(&sense, sizeof (struct scsi_extended_sense));
3983 		sense.es_valid = 0;	/* Valid LBA */
3984 		sense.es_class = 7;	/* Response code 0x70 - current err */
3985 		sense.es_key = KEY_NO_SENSE;
3986 		sense.es_add_len = 6;	/* Additional length */
3987 		/* Copy no more than requested */
3988 		bcopy(&sense, bp->b_un.b_addr, count);
3989 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3990 		scsipkt->pkt_resid = 0;
3991 		switch (power_state) {
3992 		case SATA_POWER_IDLE:
3993 		case SATA_POWER_STANDBY:
3994 			sense.es_add_code =
3995 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3996 			break;
3997 		case SATA_POWER_STOPPED:
3998 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3999 			break;
4000 		case SATA_POWER_ACTIVE:
4001 		default:
4002 			break;
4003 		}
4004 	}
4005 
4006 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4007 	    "Scsi_pkt completion reason %x\n",
4008 	    scsipkt->pkt_reason);
4009 
4010 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4011 	    scsipkt->pkt_comp != NULL) {
4012 		/* scsi callback required */
4013 		if (servicing_interrupt()) {
4014 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4015 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4016 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4017 				return (TRAN_BUSY);
4018 			}
4019 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4020 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4021 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4022 			/* Scheduling the callback failed */
4023 			return (TRAN_BUSY);
4024 		}
4025 	}
4026 	return (TRAN_ACCEPT);
4027 }
4028 
4029 /*
4030  * SATA translate command: Test Unit Ready
4031  * (ATA version for SATA hard disks).
4032  * It is translated into the Check Power Mode command.
4033  *
4034  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4035  */
4036 static int
4037 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4038 {
4039 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4040 	struct scsi_extended_sense *sense;
4041 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4042 	sata_drive_info_t *sdinfo;
4043 	int power_state;
4044 	int rval, reason;
4045 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4046 
4047 	mutex_enter(cport_mutex);
4048 
4049 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4050 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4051 		mutex_exit(cport_mutex);
4052 		return (rval);
4053 	}
4054 
4055 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4056 	    &spx->txlt_sata_pkt->satapkt_device);
4057 	ASSERT(sdinfo != NULL);
4058 
4059 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4060 
4061 	/* send CHECK POWER MODE command */
4062 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4063 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4064 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4065 	if (sata_hba_start(spx, &rval) != 0) {
4066 		mutex_exit(cport_mutex);
4067 		return (rval);
4068 	}
4069 
4070 	if (scmd->satacmd_error_reg != 0) {
4071 		mutex_exit(cport_mutex);
4072 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4073 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4074 	}
4075 
4076 	power_state = scmd->satacmd_sec_count_lsb;
4077 
4078 	/*
4079 	 * return NOT READY when device in STOPPED mode
4080 	 */
4081 	if (power_state == SATA_PWRMODE_STANDBY &&
4082 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4083 		*scsipkt->pkt_scbp = STATUS_CHECK;
4084 		sense = sata_arq_sense(spx);
4085 		sense->es_key = KEY_NOT_READY;
4086 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4087 	} else {
4088 		/*
4089 		 * For other power mode, return GOOD status
4090 		 */
4091 		*scsipkt->pkt_scbp = STATUS_GOOD;
4092 	}
4093 
4094 	scsipkt->pkt_reason = CMD_CMPLT;
4095 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4096 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4097 
4098 	mutex_exit(cport_mutex);
4099 
4100 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4101 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4102 
4103 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4104 	    scsipkt->pkt_comp != NULL) {
4105 		/* scsi callback required */
4106 		if (servicing_interrupt()) {
4107 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4108 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4109 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4110 				return (TRAN_BUSY);
4111 			}
4112 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4113 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4114 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4115 			/* Scheduling the callback failed */
4116 			return (TRAN_BUSY);
4117 		}
4118 	}
4119 
4120 	return (TRAN_ACCEPT);
4121 }
4122 
4123 /*
4124  * SATA translate command: Start Stop Unit
4125  * Translation depends on a command:
4126  *
4127  * Power condition bits will be supported
4128  * and the power level should be maintained by SATL,
4129  * When SATL received a command, it will check the
4130  * power level firstly, and return the status according
4131  * to SAT2 v2.6 and SAT-2 Standby Modifications
4132  *
4133  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4134  * -----------------------------------------------------------------------
4135  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4136  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4137  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4138  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4139  *
4140  *	Unload Media / NOT SUPPORTED YET
4141  *	Load Media / NOT SUPPROTED YET
4142  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4143  *
4144  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4145  * appropriate values in scsi_pkt fields.
4146  */
4147 static int
4148 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4149 {
4150 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4151 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4152 	int rval, reason;
4153 	sata_drive_info_t *sdinfo;
4154 	sata_id_t *sata_id;
4155 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4156 
4157 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4158 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4159 
4160 	mutex_enter(cport_mutex);
4161 
4162 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4163 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4164 		mutex_exit(cport_mutex);
4165 		return (rval);
4166 	}
4167 
4168 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4169 		/* IMMED bit - not supported */
4170 		mutex_exit(cport_mutex);
4171 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4172 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4173 	}
4174 
4175 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4176 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4177 
4178 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4179 	    &spx->txlt_sata_pkt->satapkt_device);
4180 	ASSERT(sdinfo != NULL);
4181 	sata_id = &sdinfo->satadrv_id;
4182 
4183 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4184 	case 0:
4185 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4186 			/* Load/Unload Media - invalid request */
4187 			goto err_out;
4188 		}
4189 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4190 			/* Start Unit */
4191 			sata_build_read_verify_cmd(scmd, 1, 5);
4192 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4193 			/* Transfer command to HBA */
4194 			if (sata_hba_start(spx, &rval) != 0) {
4195 				/* Pkt not accepted for execution */
4196 				mutex_exit(cport_mutex);
4197 				return (rval);
4198 			}
4199 			if (scmd->satacmd_error_reg != 0) {
4200 				goto err_out;
4201 			}
4202 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4203 		} else {
4204 			/* Stop Unit */
4205 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4206 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4207 			if (sata_hba_start(spx, &rval) != 0) {
4208 				mutex_exit(cport_mutex);
4209 				return (rval);
4210 			} else {
4211 				if (scmd->satacmd_error_reg != 0) {
4212 					goto err_out;
4213 				}
4214 			}
4215 			/* ata standby immediate command */
4216 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4217 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4218 			if (sata_hba_start(spx, &rval) != 0) {
4219 				mutex_exit(cport_mutex);
4220 				return (rval);
4221 			}
4222 			if (scmd->satacmd_error_reg != 0) {
4223 				goto err_out;
4224 			}
4225 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4226 		}
4227 		break;
4228 	case 0x1:
4229 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4230 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4231 		if (sata_hba_start(spx, &rval) != 0) {
4232 			mutex_exit(cport_mutex);
4233 			return (rval);
4234 		}
4235 		if (scmd->satacmd_error_reg != 0) {
4236 			goto err_out;
4237 		}
4238 		sata_build_read_verify_cmd(scmd, 1, 5);
4239 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4240 		/* Transfer command to HBA */
4241 		if (sata_hba_start(spx, &rval) != 0) {
4242 			/* Pkt not accepted for execution */
4243 			mutex_exit(cport_mutex);
4244 			return (rval);
4245 		} else {
4246 			if (scmd->satacmd_error_reg != 0) {
4247 				goto err_out;
4248 			}
4249 		}
4250 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4251 		break;
4252 	case 0x2:
4253 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4254 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4255 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4256 			if (sata_hba_start(spx, &rval) != 0) {
4257 				mutex_exit(cport_mutex);
4258 				return (rval);
4259 			}
4260 			if (scmd->satacmd_error_reg != 0) {
4261 				goto err_out;
4262 			}
4263 		}
4264 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4265 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4266 		if (sata_hba_start(spx, &rval) != 0) {
4267 			mutex_exit(cport_mutex);
4268 			return (rval);
4269 		}
4270 		if (scmd->satacmd_error_reg != 0) {
4271 			goto err_out;
4272 		}
4273 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4274 			/*
4275 			 *  POWER CONDITION MODIFIER bit set
4276 			 *  to 0x1 or larger it will be handled
4277 			 *  on the same way as bit = 0x1
4278 			 */
4279 			if (!(sata_id->ai_cmdset84 &
4280 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4281 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4282 				break;
4283 			}
4284 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4285 			scmd->satacmd_features_reg = 0x44;
4286 			scmd->satacmd_lba_low_lsb = 0x4c;
4287 			scmd->satacmd_lba_mid_lsb = 0x4e;
4288 			scmd->satacmd_lba_high_lsb = 0x55;
4289 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4290 			if (sata_hba_start(spx, &rval) != 0) {
4291 				mutex_exit(cport_mutex);
4292 				return (rval);
4293 			}
4294 			if (scmd->satacmd_error_reg != 0) {
4295 				goto err_out;
4296 			}
4297 		}
4298 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4299 		break;
4300 	case 0x3:
4301 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4302 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4303 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4304 			if (sata_hba_start(spx, &rval) != 0) {
4305 				mutex_exit(cport_mutex);
4306 				return (rval);
4307 			}
4308 			if (scmd->satacmd_error_reg != 0) {
4309 				goto err_out;
4310 			}
4311 		}
4312 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4313 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4314 		if (sata_hba_start(spx, &rval) != 0) {
4315 			mutex_exit(cport_mutex);
4316 			return (rval);
4317 		}
4318 		if (scmd->satacmd_error_reg != 0) {
4319 			goto err_out;
4320 		}
4321 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4322 		break;
4323 	case 0x7:
4324 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4325 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4326 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4327 		if (sata_hba_start(spx, &rval) != 0) {
4328 			mutex_exit(cport_mutex);
4329 			return (rval);
4330 		}
4331 		if (scmd->satacmd_error_reg != 0) {
4332 			goto err_out;
4333 		}
4334 		switch (scmd->satacmd_sec_count_lsb) {
4335 		case SATA_PWRMODE_STANDBY:
4336 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4337 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4338 			    sdinfo->satadrv_standby_timer);
4339 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4340 			if (sata_hba_start(spx, &rval) != 0) {
4341 				mutex_exit(cport_mutex);
4342 				return (rval);
4343 			} else {
4344 				if (scmd->satacmd_error_reg != 0) {
4345 					goto err_out;
4346 				}
4347 			}
4348 			break;
4349 		case SATA_PWRMODE_IDLE:
4350 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4351 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4352 			    sdinfo->satadrv_standby_timer);
4353 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4354 			if (sata_hba_start(spx, &rval) != 0) {
4355 				mutex_exit(cport_mutex);
4356 				return (rval);
4357 			} else {
4358 				if (scmd->satacmd_error_reg != 0) {
4359 					goto err_out;
4360 				}
4361 			}
4362 			break;
4363 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4364 		case SATA_PWRMODE_ACTIVE_SPINUP:
4365 		case SATA_PWRMODE_ACTIVE:
4366 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4367 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4368 			    sdinfo->satadrv_standby_timer);
4369 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4370 			if (sata_hba_start(spx, &rval) != 0) {
4371 				mutex_exit(cport_mutex);
4372 				return (rval);
4373 			}
4374 			if (scmd->satacmd_error_reg != 0) {
4375 				goto err_out;
4376 			}
4377 			sata_build_read_verify_cmd(scmd, 1, 5);
4378 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4379 			if (sata_hba_start(spx, &rval) != 0) {
4380 				mutex_exit(cport_mutex);
4381 				return (rval);
4382 			}
4383 			if (scmd->satacmd_error_reg != 0) {
4384 				goto err_out;
4385 			}
4386 			break;
4387 		default:
4388 			goto err_out;
4389 		}
4390 		break;
4391 	case 0xb:
4392 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4393 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4394 			mutex_exit(cport_mutex);
4395 			return (sata_txlt_check_condition(spx,
4396 			    KEY_ILLEGAL_REQUEST,
4397 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4398 		}
4399 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4400 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4401 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4402 			if (sata_hba_start(spx, &rval) != 0) {
4403 				mutex_exit(cport_mutex);
4404 				return (rval);
4405 			}
4406 			if (scmd->satacmd_error_reg != 0) {
4407 				goto err_out;
4408 			}
4409 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4410 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4411 			if (sata_hba_start(spx, &rval) != 0) {
4412 				mutex_exit(cport_mutex);
4413 				return (rval);
4414 			}
4415 			if (scmd->satacmd_error_reg != 0) {
4416 				goto err_out;
4417 			}
4418 		}
4419 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4420 		break;
4421 	default:
4422 err_out:
4423 		mutex_exit(cport_mutex);
4424 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4425 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4426 	}
4427 
4428 	/*
4429 	 * Since it was a synchronous command,
4430 	 * a callback function will be called directly.
4431 	 */
4432 	mutex_exit(cport_mutex);
4433 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4434 	    "synchronous execution status %x\n",
4435 	    spx->txlt_sata_pkt->satapkt_reason);
4436 
4437 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4438 	    scsipkt->pkt_comp != NULL) {
4439 		sata_set_arq_data(spx->txlt_sata_pkt);
4440 		if (servicing_interrupt()) {
4441 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4442 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4443 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4444 				return (TRAN_BUSY);
4445 			}
4446 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4447 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4448 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4449 			/* Scheduling the callback failed */
4450 			return (TRAN_BUSY);
4451 		}
4452 	}
4453 	else
4454 
4455 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4456 
4457 	return (TRAN_ACCEPT);
4458 
4459 }
4460 
4461 /*
4462  * SATA translate command:  Read Capacity.
4463  * Emulated command for SATA disks.
4464  * Capacity is retrieved from cached Idenifty Device data.
4465  * Identify Device data shows effective disk capacity, not the native
4466  * capacity, which may be limitted by Set Max Address command.
4467  * This is ATA version for SATA hard disks.
4468  *
4469  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4470  */
4471 static int
4472 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4473 {
4474 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4475 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4476 	sata_drive_info_t *sdinfo;
4477 	uint64_t val;
4478 	uchar_t *rbuf;
4479 	int rval, reason;
4480 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4481 
4482 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4483 	    "sata_txlt_read_capacity: ", NULL);
4484 
4485 	mutex_enter(cport_mutex);
4486 
4487 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4488 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4489 		mutex_exit(cport_mutex);
4490 		return (rval);
4491 	}
4492 
4493 	scsipkt->pkt_reason = CMD_CMPLT;
4494 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4495 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4496 	*scsipkt->pkt_scbp = STATUS_GOOD;
4497 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4498 		/*
4499 		 * Because it is fully emulated command storing data
4500 		 * programatically in the specified buffer, release
4501 		 * preallocated DMA resources before storing data in the buffer,
4502 		 * so no unwanted DMA sync would take place.
4503 		 */
4504 		sata_scsi_dmafree(NULL, scsipkt);
4505 
4506 		sdinfo = sata_get_device_info(
4507 		    spx->txlt_sata_hba_inst,
4508 		    &spx->txlt_sata_pkt->satapkt_device);
4509 
4510 		/*
4511 		 * As per SBC-3, the "returned LBA" is either the highest
4512 		 * addressable LBA or 0xffffffff, whichever is smaller.
4513 		 */
4514 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4515 
4516 		rbuf = (uchar_t *)bp->b_un.b_addr;
4517 		/* Need to swap endians to match scsi format */
4518 		rbuf[0] = (val >> 24) & 0xff;
4519 		rbuf[1] = (val >> 16) & 0xff;
4520 		rbuf[2] = (val >> 8) & 0xff;
4521 		rbuf[3] = val & 0xff;
4522 		/* block size - always 512 bytes, for now */
4523 		rbuf[4] = 0;
4524 		rbuf[5] = 0;
4525 		rbuf[6] = 0x02;
4526 		rbuf[7] = 0;
4527 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4528 		scsipkt->pkt_resid = 0;
4529 
4530 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4531 		    sdinfo->satadrv_capacity -1);
4532 	}
4533 	mutex_exit(cport_mutex);
4534 	/*
4535 	 * If a callback was requested, do it now.
4536 	 */
4537 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4538 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4539 
4540 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4541 	    scsipkt->pkt_comp != NULL) {
4542 		/* scsi callback required */
4543 		if (servicing_interrupt()) {
4544 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4545 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4546 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4547 				return (TRAN_BUSY);
4548 			}
4549 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4550 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4551 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4552 			/* Scheduling the callback failed */
4553 			return (TRAN_BUSY);
4554 		}
4555 	}
4556 
4557 	return (TRAN_ACCEPT);
4558 }
4559 
4560 /*
4561  * SATA translate command:  Read Capacity (16).
4562  * Emulated command for SATA disks.
4563  * Info is retrieved from cached Identify Device data.
4564  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4565  *
4566  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4567  */
4568 static int
4569 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4570 {
4571 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4572 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4573 	sata_drive_info_t *sdinfo;
4574 	uint64_t val;
4575 	uint16_t l2p_exp;
4576 	uchar_t *rbuf;
4577 	int rval, reason;
4578 #define	TPE	0x80
4579 #define	TPRZ	0x40
4580 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4581 
4582 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4583 	    "sata_txlt_read_capacity: ", NULL);
4584 
4585 	mutex_enter(cport_mutex);
4586 
4587 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4588 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4589 		mutex_exit(cport_mutex);
4590 		return (rval);
4591 	}
4592 
4593 	scsipkt->pkt_reason = CMD_CMPLT;
4594 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4595 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4596 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4597 		/*
4598 		 * Because it is fully emulated command storing data
4599 		 * programatically in the specified buffer, release
4600 		 * preallocated DMA resources before storing data in the buffer,
4601 		 * so no unwanted DMA sync would take place.
4602 		 */
4603 		sata_scsi_dmafree(NULL, scsipkt);
4604 
4605 		/* Check SERVICE ACTION field */
4606 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4607 		    SSVC_ACTION_READ_CAPACITY_G4) {
4608 			mutex_exit(cport_mutex);
4609 			return (sata_txlt_check_condition(spx,
4610 			    KEY_ILLEGAL_REQUEST,
4611 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4612 		}
4613 
4614 		/* Check LBA field */
4615 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4616 		    (scsipkt->pkt_cdbp[3] != 0) ||
4617 		    (scsipkt->pkt_cdbp[4] != 0) ||
4618 		    (scsipkt->pkt_cdbp[5] != 0) ||
4619 		    (scsipkt->pkt_cdbp[6] != 0) ||
4620 		    (scsipkt->pkt_cdbp[7] != 0) ||
4621 		    (scsipkt->pkt_cdbp[8] != 0) ||
4622 		    (scsipkt->pkt_cdbp[9] != 0)) {
4623 			mutex_exit(cport_mutex);
4624 			return (sata_txlt_check_condition(spx,
4625 			    KEY_ILLEGAL_REQUEST,
4626 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4627 		}
4628 
4629 		/* Check PMI bit */
4630 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4631 			mutex_exit(cport_mutex);
4632 			return (sata_txlt_check_condition(spx,
4633 			    KEY_ILLEGAL_REQUEST,
4634 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4635 		}
4636 
4637 		*scsipkt->pkt_scbp = STATUS_GOOD;
4638 
4639 		sdinfo = sata_get_device_info(
4640 		    spx->txlt_sata_hba_inst,
4641 		    &spx->txlt_sata_pkt->satapkt_device);
4642 
4643 		/* last logical block address */
4644 		val = MIN(sdinfo->satadrv_capacity - 1,
4645 		    SCSI_READ_CAPACITY16_MAX_LBA);
4646 
4647 		/* logical to physical block size exponent */
4648 		l2p_exp = 0;
4649 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4650 			/* physical/logical sector size word is valid */
4651 
4652 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4653 			    SATA_L2PS_HAS_MULT) {
4654 				/* multiple logical sectors per phys sectors */
4655 				l2p_exp =
4656 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4657 				    SATA_L2PS_EXP_MASK;
4658 			}
4659 		}
4660 
4661 		rbuf = (uchar_t *)bp->b_un.b_addr;
4662 		bzero(rbuf, bp->b_bcount);
4663 
4664 		/* returned logical block address */
4665 		rbuf[0] = (val >> 56) & 0xff;
4666 		rbuf[1] = (val >> 48) & 0xff;
4667 		rbuf[2] = (val >> 40) & 0xff;
4668 		rbuf[3] = (val >> 32) & 0xff;
4669 		rbuf[4] = (val >> 24) & 0xff;
4670 		rbuf[5] = (val >> 16) & 0xff;
4671 		rbuf[6] = (val >> 8) & 0xff;
4672 		rbuf[7] = val & 0xff;
4673 
4674 		/* logical block length in bytes = 512 (for now) */
4675 		/* rbuf[8] = 0; */
4676 		/* rbuf[9] = 0; */
4677 		rbuf[10] = 0x02;
4678 		/* rbuf[11] = 0; */
4679 
4680 		/* p_type, prot_en, unspecified by SAT-2 */
4681 		/* rbuf[12] = 0; */
4682 
4683 		/* p_i_exponent, undefined by SAT-2 */
4684 		/* logical blocks per physical block exponent */
4685 		rbuf[13] = l2p_exp;
4686 
4687 		/* lowest aligned logical block address = 0 (for now) */
4688 		/* tpe and tprz as defined in T10/10-079 r0 */
4689 		if (sdinfo->satadrv_id.ai_addsupported &
4690 		    SATA_DETERMINISTIC_READ) {
4691 			if (sdinfo->satadrv_id.ai_addsupported &
4692 			    SATA_READ_ZERO) {
4693 				rbuf[14] |= TPRZ;
4694 			} else {
4695 				rbuf[14] |= TPE;
4696 			}
4697 		}
4698 		/* rbuf[15] = 0; */
4699 
4700 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4701 		scsipkt->pkt_resid = 0;
4702 
4703 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4704 		    sdinfo->satadrv_capacity -1);
4705 	}
4706 
4707 	mutex_exit(cport_mutex);
4708 
4709 	/*
4710 	 * If a callback was requested, do it now.
4711 	 */
4712 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4713 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4714 
4715 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4716 	    scsipkt->pkt_comp != NULL) {
4717 		/* scsi callback required */
4718 		if (servicing_interrupt()) {
4719 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4720 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4721 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4722 				return (TRAN_BUSY);
4723 			}
4724 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4725 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4726 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4727 			/* Scheduling the callback failed */
4728 			return (TRAN_BUSY);
4729 		}
4730 	}
4731 
4732 	return (TRAN_ACCEPT);
4733 }
4734 
4735 /*
4736  * Translate command: UNMAP
4737  *
4738  * The function cannot be called in interrupt context since it may sleep.
4739  */
4740 static int
4741 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4742 {
4743 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4744 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4745 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4746 	uint16_t count = 0;
4747 	int synch;
4748 	int rval, reason;
4749 	int i, x;
4750 	int bdlen = 0;
4751 	int ranges = 0;
4752 	int paramlen = 8;
4753 	uint8_t *data, *tmpbd;
4754 	sata_drive_info_t *sdinfo;
4755 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4756 #define	TRIM	0x1
4757 
4758 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4759 	    "sata_txlt_unmap: ", NULL);
4760 
4761 	mutex_enter(cport_mutex);
4762 
4763 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4764 	    &spx->txlt_sata_pkt->satapkt_device);
4765 	if (sdinfo != NULL) {
4766 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4767 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4768 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4769 		    sdinfo->satadrv_id.ai_maxcount);
4770 	}
4771 
4772 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4773 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4774 		mutex_exit(cport_mutex);
4775 		return (rval);
4776 	}
4777 
4778 	/*
4779 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4780 	 * Start by getting the block descriptor data length by subtracting
4781 	 * the 8 byte parameter list header from the parameter list length.
4782 	 * The block descriptor size has to be a multiple of 16 bytes.
4783 	 */
4784 	bdlen = scsipkt->pkt_cdbp[7];
4785 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4786 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4787 	    (bdlen > (bp->b_bcount - paramlen))) {
4788 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4789 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4790 		mutex_exit(cport_mutex);
4791 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4792 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4793 	}
4794 	/*
4795 	 * If there are no parameter data or block descriptors, it is not
4796 	 * considered an error so just complete the command without sending
4797 	 * TRIM.
4798 	 */
4799 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4800 	    (bp->b_bcount == 0)) {
4801 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4802 		    "sata_txlt_unmap: no parameter data or block descriptors",
4803 		    NULL);
4804 		mutex_exit(cport_mutex);
4805 		return (sata_txlt_unmap_nodata_cmd(spx));
4806 	}
4807 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4808 	data = kmem_zalloc(bdlen, KM_SLEEP);
4809 
4810 	/*
4811 	 * Loop through all the UNMAP block descriptors and convert the data
4812 	 * into TRIM format.
4813 	 */
4814 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4815 		/* get range length */
4816 		data[x] = tmpbd[i+7];
4817 		data[x+1] = tmpbd[i+6];
4818 		/* get LBA */
4819 		data[x+2] = tmpbd[i+5];
4820 		data[x+3] = tmpbd[i+4];
4821 		data[x+4] = tmpbd[i+3];
4822 		data[x+5] = tmpbd[i+2];
4823 		data[x+6] = tmpbd[i+11];
4824 		data[x+7] = tmpbd[i+10];
4825 
4826 		ranges++;
4827 	}
4828 
4829 	/*
4830 	 * The TRIM command expects the data buffer to be a multiple of
4831 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4832 	 * may be too small.  Free the original DMA resources and create a
4833 	 * local buffer.
4834 	 */
4835 	sata_common_free_dma_rsrcs(spx);
4836 
4837 	/*
4838 	 * Get count of 512-byte blocks of range entries.  The length
4839 	 * of a range entry is 8 bytes which means one count has 64 range
4840 	 * entries.
4841 	 */
4842 	count = (ranges + 63)/64;
4843 
4844 	/* Allocate a buffer that is a multiple of 512 bytes. */
4845 	mutex_exit(cport_mutex);
4846 	bp = sata_alloc_local_buffer(spx, count * 512);
4847 	if (bp == NULL) {
4848 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4849 		    "sata_txlt_unmap: "
4850 		    "cannot allocate buffer for TRIM command", NULL);
4851 		kmem_free(data, bdlen);
4852 		return (TRAN_BUSY);
4853 	}
4854 	bp_mapin(bp); /* make data buffer accessible */
4855 	mutex_enter(cport_mutex);
4856 
4857 	bzero(bp->b_un.b_addr, bp->b_bcount);
4858 	bcopy(data, bp->b_un.b_addr, x);
4859 	kmem_free(data, bdlen);
4860 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4861 	    DDI_DMA_SYNC_FORDEV);
4862 	ASSERT(rval == DDI_SUCCESS);
4863 
4864 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4865 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4866 	scmd->satacmd_cmd_reg = SATAC_DSM;
4867 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4868 	scmd->satacmd_sec_count_lsb = count & 0xff;
4869 	scmd->satacmd_features_reg = TRIM;
4870 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4871 	scmd->satacmd_status_reg = 0;
4872 	scmd->satacmd_error_reg = 0;
4873 
4874 	/* Start processing command */
4875 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4876 		spx->txlt_sata_pkt->satapkt_comp =
4877 		    sata_txlt_unmap_completion;
4878 		synch = FALSE;
4879 	} else {
4880 		synch = TRUE;
4881 	}
4882 
4883 	if (sata_hba_start(spx, &rval) != 0) {
4884 		mutex_exit(cport_mutex);
4885 		return (rval);
4886 	}
4887 
4888 	mutex_exit(cport_mutex);
4889 
4890 	if (synch) {
4891 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4892 	}
4893 
4894 	return (TRAN_ACCEPT);
4895 }
4896 
4897 /*
4898  * SATA translate command: Mode Sense.
4899  * Translated into appropriate SATA command or emulated.
4900  * Saved Values Page Control (03) are not supported.
4901  *
4902  * NOTE: only caching mode sense page is currently implemented.
4903  *
4904  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4905  */
4906 
4907 #define	LLBAA	0x10	/* Long LBA Accepted */
4908 
4909 static int
4910 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4911 {
4912 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4913 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4914 	sata_drive_info_t *sdinfo;
4915 	sata_id_t *sata_id;
4916 	struct scsi_extended_sense *sense;
4917 	int 		len, bdlen, count, alc_len;
4918 	int		pc;	/* Page Control code */
4919 	uint8_t		*buf;	/* mode sense buffer */
4920 	int		rval, reason;
4921 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4922 
4923 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4924 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4925 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4926 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4927 
4928 	if (servicing_interrupt()) {
4929 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4930 		if (buf == NULL) {
4931 			return (TRAN_BUSY);
4932 		}
4933 	} else {
4934 		buf = kmem_zalloc(1024, KM_SLEEP);
4935 	}
4936 
4937 	mutex_enter(cport_mutex);
4938 
4939 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4940 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4941 		mutex_exit(cport_mutex);
4942 		kmem_free(buf, 1024);
4943 		return (rval);
4944 	}
4945 
4946 	scsipkt->pkt_reason = CMD_CMPLT;
4947 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4948 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4949 
4950 	pc = scsipkt->pkt_cdbp[2] >> 6;
4951 
4952 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4953 		/*
4954 		 * Because it is fully emulated command storing data
4955 		 * programatically in the specified buffer, release
4956 		 * preallocated DMA resources before storing data in the buffer,
4957 		 * so no unwanted DMA sync would take place.
4958 		 */
4959 		sata_scsi_dmafree(NULL, scsipkt);
4960 
4961 		len = 0;
4962 		bdlen = 0;
4963 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4964 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4965 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4966 				bdlen = 16;
4967 			else
4968 				bdlen = 8;
4969 		}
4970 		/* Build mode parameter header */
4971 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4972 			/* 4-byte mode parameter header */
4973 			buf[len++] = 0;		/* mode data length */
4974 			buf[len++] = 0;		/* medium type */
4975 			buf[len++] = 0;		/* dev-specific param */
4976 			buf[len++] = bdlen;	/* Block Descriptor length */
4977 		} else {
4978 			/* 8-byte mode parameter header */
4979 			buf[len++] = 0;		/* mode data length */
4980 			buf[len++] = 0;
4981 			buf[len++] = 0;		/* medium type */
4982 			buf[len++] = 0;		/* dev-specific param */
4983 			if (bdlen == 16)
4984 				buf[len++] = 1;	/* long lba descriptor */
4985 			else
4986 				buf[len++] = 0;
4987 			buf[len++] = 0;
4988 			buf[len++] = 0;		/* Block Descriptor length */
4989 			buf[len++] = bdlen;
4990 		}
4991 
4992 		sdinfo = sata_get_device_info(
4993 		    spx->txlt_sata_hba_inst,
4994 		    &spx->txlt_sata_pkt->satapkt_device);
4995 
4996 		/* Build block descriptor only if not disabled (DBD) */
4997 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4998 			/* Block descriptor - direct-access device format */
4999 			if (bdlen == 8) {
5000 				/* build regular block descriptor */
5001 				buf[len++] =
5002 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5003 				buf[len++] =
5004 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5005 				buf[len++] =
5006 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5007 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5008 				buf[len++] = 0; /* density code */
5009 				buf[len++] = 0;
5010 				if (sdinfo->satadrv_type ==
5011 				    SATA_DTYPE_ATADISK)
5012 					buf[len++] = 2;
5013 				else
5014 					/* ATAPI */
5015 					buf[len++] = 8;
5016 				buf[len++] = 0;
5017 			} else if (bdlen == 16) {
5018 				/* Long LBA Accepted */
5019 				/* build long lba block descriptor */
5020 #ifndef __lock_lint
5021 				buf[len++] =
5022 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5023 				buf[len++] =
5024 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5025 				buf[len++] =
5026 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5027 				buf[len++] =
5028 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5029 #endif
5030 				buf[len++] =
5031 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5032 				buf[len++] =
5033 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5034 				buf[len++] =
5035 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5036 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5037 				buf[len++] = 0;
5038 				buf[len++] = 0; /* density code */
5039 				buf[len++] = 0;
5040 				buf[len++] = 0;
5041 				if (sdinfo->satadrv_type ==
5042 				    SATA_DTYPE_ATADISK)
5043 					buf[len++] = 2;
5044 				else
5045 					/* ATAPI */
5046 					buf[len++] = 8;
5047 				buf[len++] = 0;
5048 			}
5049 		}
5050 
5051 		sata_id = &sdinfo->satadrv_id;
5052 
5053 		/*
5054 		 * Add requested pages.
5055 		 * Page 3 and 4 are obsolete and we are not supporting them.
5056 		 * We deal now with:
5057 		 * caching (read/write cache control).
5058 		 * We should eventually deal with following mode pages:
5059 		 * error recovery  (0x01),
5060 		 * power condition (0x1a),
5061 		 * exception control page (enables SMART) (0x1c),
5062 		 * enclosure management (ses),
5063 		 * protocol-specific port mode (port control).
5064 		 */
5065 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5066 		case MODEPAGE_RW_ERRRECOV:
5067 			/* DAD_MODE_ERR_RECOV */
5068 			/* R/W recovery */
5069 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5070 			break;
5071 		case MODEPAGE_CACHING:
5072 			/* DAD_MODE_CACHE */
5073 			/* Reject not supported request for saved parameters */
5074 			if (pc == 3) {
5075 				*scsipkt->pkt_scbp = STATUS_CHECK;
5076 				sense = sata_arq_sense(spx);
5077 				sense->es_key = KEY_ILLEGAL_REQUEST;
5078 				sense->es_add_code =
5079 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5080 				goto done;
5081 			}
5082 
5083 			/* caching */
5084 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5085 			break;
5086 		case MODEPAGE_INFO_EXCPT:
5087 			/* exception cntrl */
5088 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5089 				len += sata_build_msense_page_1c(sdinfo, pc,
5090 				    buf+len);
5091 			}
5092 			else
5093 				goto err;
5094 			break;
5095 		case MODEPAGE_POWER_COND:
5096 			/* DAD_MODE_POWER_COND */
5097 			/* power condition */
5098 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5099 			break;
5100 
5101 		case MODEPAGE_ACOUSTIC_MANAG:
5102 			/* acoustic management */
5103 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5104 			break;
5105 		case MODEPAGE_ALLPAGES:
5106 			/* all pages */
5107 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5108 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5109 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5110 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5111 				len += sata_build_msense_page_1c(sdinfo, pc,
5112 				    buf+len);
5113 			}
5114 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5115 			break;
5116 		default:
5117 		err:
5118 			/* Invalid request */
5119 			*scsipkt->pkt_scbp = STATUS_CHECK;
5120 			sense = sata_arq_sense(spx);
5121 			sense->es_key = KEY_ILLEGAL_REQUEST;
5122 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5123 			goto done;
5124 		}
5125 
5126 		/* fix total mode data length */
5127 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5128 			/* 4-byte mode parameter header */
5129 			buf[0] = len - 1;	/* mode data length */
5130 		} else {
5131 			buf[0] = (len -2) >> 8;
5132 			buf[1] = (len -2) & 0xff;
5133 		}
5134 
5135 
5136 		/* Check allocation length */
5137 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5138 			alc_len = scsipkt->pkt_cdbp[4];
5139 		} else {
5140 			alc_len = scsipkt->pkt_cdbp[7];
5141 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5142 		}
5143 		/*
5144 		 * We do not check for possible parameters truncation
5145 		 * (alc_len < len) assuming that the target driver works
5146 		 * correctly. Just avoiding overrun.
5147 		 * Copy no more than requested and possible, buffer-wise.
5148 		 */
5149 		count = MIN(alc_len, len);
5150 		count = MIN(bp->b_bcount, count);
5151 		bcopy(buf, bp->b_un.b_addr, count);
5152 
5153 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5154 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5155 	}
5156 	*scsipkt->pkt_scbp = STATUS_GOOD;
5157 done:
5158 	mutex_exit(cport_mutex);
5159 	(void) kmem_free(buf, 1024);
5160 
5161 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5162 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5163 
5164 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5165 	    scsipkt->pkt_comp != NULL) {
5166 		/* scsi callback required */
5167 		if (servicing_interrupt()) {
5168 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5169 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5170 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5171 				return (TRAN_BUSY);
5172 			}
5173 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5174 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5175 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5176 			/* Scheduling the callback failed */
5177 			return (TRAN_BUSY);
5178 		}
5179 	}
5180 
5181 	return (TRAN_ACCEPT);
5182 }
5183 
5184 
5185 /*
5186  * SATA translate command: Mode Select.
5187  * Translated into appropriate SATA command or emulated.
5188  * Saving parameters is not supported.
5189  * Changing device capacity is not supported (although theoretically
5190  * possible by executing SET FEATURES/SET MAX ADDRESS)
5191  *
5192  * Assumption is that the target driver is working correctly.
5193  *
5194  * More than one SATA command may be executed to perform operations specified
5195  * by mode select pages. The first error terminates further execution.
5196  * Operations performed successully are not backed-up in such case.
5197  *
5198  * NOTE: Implemented pages:
5199  * - caching page
5200  * - informational exception page
5201  * - acoustic management page
5202  * - power condition page
5203  * Caching setup is remembered so it could be re-stored in case of
5204  * an unexpected device reset.
5205  *
5206  * Returns TRAN_XXXX.
5207  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5208  */
5209 
5210 static int
5211 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5212 {
5213 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5214 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5215 	struct scsi_extended_sense *sense;
5216 	int len, pagelen, count, pllen;
5217 	uint8_t *buf;	/* mode select buffer */
5218 	int rval, stat, reason;
5219 	uint_t nointr_flag;
5220 	int dmod = 0;
5221 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5222 
5223 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5224 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5225 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5226 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5227 
5228 	mutex_enter(cport_mutex);
5229 
5230 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5231 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5232 		mutex_exit(cport_mutex);
5233 		return (rval);
5234 	}
5235 
5236 	rval = TRAN_ACCEPT;
5237 
5238 	scsipkt->pkt_reason = CMD_CMPLT;
5239 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5240 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5241 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5242 
5243 	/* Reject not supported request */
5244 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5245 		*scsipkt->pkt_scbp = STATUS_CHECK;
5246 		sense = sata_arq_sense(spx);
5247 		sense->es_key = KEY_ILLEGAL_REQUEST;
5248 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5249 		goto done;
5250 	}
5251 
5252 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5253 		pllen = scsipkt->pkt_cdbp[4];
5254 	} else {
5255 		pllen = scsipkt->pkt_cdbp[7];
5256 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5257 	}
5258 
5259 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5260 
5261 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5262 		buf = (uint8_t *)bp->b_un.b_addr;
5263 		count = MIN(bp->b_bcount, pllen);
5264 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5265 		scsipkt->pkt_resid = 0;
5266 		pllen = count;
5267 
5268 		/*
5269 		 * Check the header to skip the block descriptor(s) - we
5270 		 * do not support setting device capacity.
5271 		 * Existing macros do not recognize long LBA dscriptor,
5272 		 * hence manual calculation.
5273 		 */
5274 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5275 			/* 6-bytes CMD, 4 bytes header */
5276 			if (count <= 4)
5277 				goto done;		/* header only */
5278 			len = buf[3] + 4;
5279 		} else {
5280 			/* 10-bytes CMD, 8 bytes header */
5281 			if (count <= 8)
5282 				goto done;		/* header only */
5283 			len = buf[6];
5284 			len = (len << 8) + buf[7] + 8;
5285 		}
5286 		if (len >= count)
5287 			goto done;	/* header + descriptor(s) only */
5288 
5289 		pllen -= len;		/* remaining data length */
5290 
5291 		/*
5292 		 * We may be executing SATA command and want to execute it
5293 		 * in SYNCH mode, regardless of scsi_pkt setting.
5294 		 * Save scsi_pkt setting and indicate SYNCH mode
5295 		 */
5296 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5297 		    scsipkt->pkt_comp != NULL) {
5298 			scsipkt->pkt_flags |= FLAG_NOINTR;
5299 		}
5300 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5301 
5302 		/*
5303 		 * len is now the offset to a first mode select page
5304 		 * Process all pages
5305 		 */
5306 		while (pllen > 0) {
5307 			switch ((int)buf[len]) {
5308 			case MODEPAGE_CACHING:
5309 				/* No support for SP (saving) */
5310 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5311 					*scsipkt->pkt_scbp = STATUS_CHECK;
5312 					sense = sata_arq_sense(spx);
5313 					sense->es_key = KEY_ILLEGAL_REQUEST;
5314 					sense->es_add_code =
5315 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5316 					goto done;
5317 				}
5318 				stat = sata_mode_select_page_8(spx,
5319 				    (struct mode_cache_scsi3 *)&buf[len],
5320 				    pllen, &pagelen, &rval, &dmod);
5321 				/*
5322 				 * The pagelen value indicates the number of
5323 				 * parameter bytes already processed.
5324 				 * The rval is the return value from
5325 				 * sata_tran_start().
5326 				 * The stat indicates the overall status of
5327 				 * the operation(s).
5328 				 */
5329 				if (stat != SATA_SUCCESS)
5330 					/*
5331 					 * Page processing did not succeed -
5332 					 * all error info is already set-up,
5333 					 * just return
5334 					 */
5335 					pllen = 0; /* this breaks the loop */
5336 				else {
5337 					len += pagelen;
5338 					pllen -= pagelen;
5339 				}
5340 				break;
5341 
5342 			case MODEPAGE_INFO_EXCPT:
5343 				stat = sata_mode_select_page_1c(spx,
5344 				    (struct mode_info_excpt_page *)&buf[len],
5345 				    pllen, &pagelen, &rval, &dmod);
5346 				/*
5347 				 * The pagelen value indicates the number of
5348 				 * parameter bytes already processed.
5349 				 * The rval is the return value from
5350 				 * sata_tran_start().
5351 				 * The stat indicates the overall status of
5352 				 * the operation(s).
5353 				 */
5354 				if (stat != SATA_SUCCESS)
5355 					/*
5356 					 * Page processing did not succeed -
5357 					 * all error info is already set-up,
5358 					 * just return
5359 					 */
5360 					pllen = 0; /* this breaks the loop */
5361 				else {
5362 					len += pagelen;
5363 					pllen -= pagelen;
5364 				}
5365 				break;
5366 
5367 			case MODEPAGE_ACOUSTIC_MANAG:
5368 				stat = sata_mode_select_page_30(spx,
5369 				    (struct mode_acoustic_management *)
5370 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5371 				/*
5372 				 * The pagelen value indicates the number of
5373 				 * parameter bytes already processed.
5374 				 * The rval is the return value from
5375 				 * sata_tran_start().
5376 				 * The stat indicates the overall status of
5377 				 * the operation(s).
5378 				 */
5379 				if (stat != SATA_SUCCESS)
5380 					/*
5381 					 * Page processing did not succeed -
5382 					 * all error info is already set-up,
5383 					 * just return
5384 					 */
5385 					pllen = 0; /* this breaks the loop */
5386 				else {
5387 					len += pagelen;
5388 					pllen -= pagelen;
5389 				}
5390 
5391 				break;
5392 			case MODEPAGE_POWER_COND:
5393 				stat = sata_mode_select_page_1a(spx,
5394 				    (struct mode_info_power_cond *)&buf[len],
5395 				    pllen, &pagelen, &rval, &dmod);
5396 				/*
5397 				 * The pagelen value indicates the number of
5398 				 * parameter bytes already processed.
5399 				 * The rval is the return value from
5400 				 * sata_tran_start().
5401 				 * The stat indicates the overall status of
5402 				 * the operation(s).
5403 				 */
5404 				if (stat != SATA_SUCCESS)
5405 					/*
5406 					 * Page processing did not succeed -
5407 					 * all error info is already set-up,
5408 					 * just return
5409 					 */
5410 					pllen = 0; /* this breaks the loop */
5411 				else {
5412 					len += pagelen;
5413 					pllen -= pagelen;
5414 				}
5415 				break;
5416 			default:
5417 				*scsipkt->pkt_scbp = STATUS_CHECK;
5418 				sense = sata_arq_sense(spx);
5419 				sense->es_key = KEY_ILLEGAL_REQUEST;
5420 				sense->es_add_code =
5421 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5422 				goto done;
5423 			}
5424 		}
5425 	}
5426 done:
5427 	mutex_exit(cport_mutex);
5428 	/*
5429 	 * If device parameters were modified, fetch and store the new
5430 	 * Identify Device data. Since port mutex could have been released
5431 	 * for accessing HBA driver, we need to re-check device existence.
5432 	 */
5433 	if (dmod != 0) {
5434 		sata_drive_info_t new_sdinfo, *sdinfo;
5435 		int rv = 0;
5436 
5437 		/*
5438 		 * Following statement has to be changed if this function is
5439 		 * used for devices other than SATA hard disks.
5440 		 */
5441 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5442 
5443 		new_sdinfo.satadrv_addr =
5444 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5445 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5446 		    &new_sdinfo);
5447 
5448 		mutex_enter(cport_mutex);
5449 		/*
5450 		 * Since port mutex could have been released when
5451 		 * accessing HBA driver, we need to re-check that the
5452 		 * framework still holds the device info structure.
5453 		 */
5454 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5455 		    &spx->txlt_sata_pkt->satapkt_device);
5456 		if (sdinfo != NULL) {
5457 			/*
5458 			 * Device still has info structure in the
5459 			 * sata framework. Copy newly fetched info
5460 			 */
5461 			if (rv == 0) {
5462 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5463 				sata_save_drive_settings(sdinfo);
5464 			} else {
5465 				/*
5466 				 * Could not fetch new data - invalidate
5467 				 * sata_drive_info. That makes device
5468 				 * unusable.
5469 				 */
5470 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5471 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5472 			}
5473 		}
5474 		if (rv != 0 || sdinfo == NULL) {
5475 			/*
5476 			 * This changes the overall mode select completion
5477 			 * reason to a failed one !!!!!
5478 			 */
5479 			*scsipkt->pkt_scbp = STATUS_CHECK;
5480 			sense = sata_arq_sense(spx);
5481 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5482 			rval = TRAN_ACCEPT;
5483 		}
5484 		mutex_exit(cport_mutex);
5485 	}
5486 	/* Restore the scsi pkt flags */
5487 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5488 	scsipkt->pkt_flags |= nointr_flag;
5489 
5490 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5491 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5492 
5493 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5494 	    scsipkt->pkt_comp != NULL) {
5495 		/* scsi callback required */
5496 		if (servicing_interrupt()) {
5497 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5498 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5499 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5500 				return (TRAN_BUSY);
5501 			}
5502 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5503 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5504 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5505 			/* Scheduling the callback failed */
5506 			return (TRAN_BUSY);
5507 		}
5508 	}
5509 
5510 	return (rval);
5511 }
5512 
5513 /*
5514  * Translate command: ATA Pass Through
5515  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5516  * PIO Data-Out protocols.  Also supports CK_COND bit.
5517  *
5518  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5519  * described in Table 111 of SAT-2 (Draft 9).
5520  */
5521 static  int
5522 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5523 {
5524 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5525 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5526 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5527 	int extend;
5528 	uint64_t lba;
5529 	uint16_t feature, sec_count;
5530 	int t_len, synch;
5531 	int rval, reason;
5532 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5533 
5534 	mutex_enter(cport_mutex);
5535 
5536 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5537 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5538 		mutex_exit(cport_mutex);
5539 		return (rval);
5540 	}
5541 
5542 	/* T_DIR bit */
5543 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5544 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5545 	else
5546 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5547 
5548 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5549 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5550 		mutex_exit(cport_mutex);
5551 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5552 	}
5553 
5554 	/* OFFLINE field. If non-zero, invalid command (for now). */
5555 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5556 		mutex_exit(cport_mutex);
5557 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5558 	}
5559 
5560 	/* PROTOCOL field */
5561 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5562 	case SATL_APT_P_HW_RESET:
5563 	case SATL_APT_P_SRST:
5564 	case SATL_APT_P_DMA:
5565 	case SATL_APT_P_DMA_QUEUED:
5566 	case SATL_APT_P_DEV_DIAG:
5567 	case SATL_APT_P_DEV_RESET:
5568 	case SATL_APT_P_UDMA_IN:
5569 	case SATL_APT_P_UDMA_OUT:
5570 	case SATL_APT_P_FPDMA:
5571 	case SATL_APT_P_RET_RESP:
5572 		/* Not yet implemented */
5573 	default:
5574 		mutex_exit(cport_mutex);
5575 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5576 
5577 	case SATL_APT_P_NON_DATA:
5578 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5579 		break;
5580 
5581 	case SATL_APT_P_PIO_DATA_IN:
5582 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5583 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5584 			mutex_exit(cport_mutex);
5585 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5586 		}
5587 
5588 		/* if there is a buffer, release its DMA resources */
5589 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5590 			sata_scsi_dmafree(NULL, scsipkt);
5591 		} else {
5592 			/* if there is no buffer, how do you PIO in? */
5593 			mutex_exit(cport_mutex);
5594 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5595 		}
5596 
5597 		break;
5598 
5599 	case SATL_APT_P_PIO_DATA_OUT:
5600 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5601 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5602 			mutex_exit(cport_mutex);
5603 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5604 		}
5605 
5606 		/* if there is a buffer, release its DMA resources */
5607 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5608 			sata_scsi_dmafree(NULL, scsipkt);
5609 		} else {
5610 			/* if there is no buffer, how do you PIO out? */
5611 			mutex_exit(cport_mutex);
5612 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5613 		}
5614 
5615 		break;
5616 	}
5617 
5618 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5619 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5620 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5621 		feature = scsipkt->pkt_cdbp[3];
5622 
5623 		sec_count = scsipkt->pkt_cdbp[4];
5624 
5625 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5626 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5627 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5628 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5629 
5630 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5631 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5632 
5633 		break;
5634 
5635 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5636 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5637 			extend = 1;
5638 
5639 			feature = scsipkt->pkt_cdbp[3];
5640 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5641 
5642 			sec_count = scsipkt->pkt_cdbp[5];
5643 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5644 
5645 			lba = scsipkt->pkt_cdbp[11];
5646 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5647 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5648 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5649 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5650 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5651 
5652 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5653 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5654 		} else {
5655 			feature = scsipkt->pkt_cdbp[3];
5656 
5657 			sec_count = scsipkt->pkt_cdbp[5];
5658 
5659 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5660 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5661 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5662 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5663 
5664 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5665 			    0xf0;
5666 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5667 		}
5668 
5669 		break;
5670 	}
5671 
5672 	/* CK_COND bit */
5673 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5674 		if (extend) {
5675 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5676 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5677 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5678 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5679 		}
5680 
5681 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5682 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5683 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5684 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5685 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5686 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5687 	}
5688 
5689 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5690 	if (extend) {
5691 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5692 
5693 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5694 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5695 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5696 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5697 		scmd->satacmd_lba_high_msb = lba >> 40;
5698 	} else {
5699 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5700 
5701 		scmd->satacmd_features_reg_ext = 0;
5702 		scmd->satacmd_sec_count_msb = 0;
5703 		scmd->satacmd_lba_low_msb = 0;
5704 		scmd->satacmd_lba_mid_msb = 0;
5705 		scmd->satacmd_lba_high_msb = 0;
5706 	}
5707 
5708 	scmd->satacmd_features_reg = feature & 0xff;
5709 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5710 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5711 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5712 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5713 
5714 	/* Determine transfer length */
5715 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5716 	case 1:
5717 		t_len = feature;
5718 		break;
5719 	case 2:
5720 		t_len = sec_count;
5721 		break;
5722 	default:
5723 		t_len = 0;
5724 		break;
5725 	}
5726 
5727 	/* Adjust transfer length for the Byte Block bit */
5728 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5729 		t_len *= SATA_DISK_SECTOR_SIZE;
5730 
5731 	/* Start processing command */
5732 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5733 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5734 		synch = FALSE;
5735 	} else {
5736 		synch = TRUE;
5737 	}
5738 
5739 	if (sata_hba_start(spx, &rval) != 0) {
5740 		mutex_exit(cport_mutex);
5741 		return (rval);
5742 	}
5743 
5744 	mutex_exit(cport_mutex);
5745 
5746 	if (synch) {
5747 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5748 	}
5749 
5750 	return (TRAN_ACCEPT);
5751 }
5752 
5753 /*
5754  * Translate command: Log Sense
5755  */
5756 static 	int
5757 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5758 {
5759 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5760 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5761 	sata_drive_info_t *sdinfo;
5762 	struct scsi_extended_sense *sense;
5763 	int 		len, count, alc_len;
5764 	int		pc;	/* Page Control code */
5765 	int		page_code;	/* Page code */
5766 	uint8_t		*buf;	/* log sense buffer */
5767 	int		rval, reason;
5768 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5769 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5770 
5771 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5772 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5773 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5774 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5775 
5776 	if (servicing_interrupt()) {
5777 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5778 		if (buf == NULL) {
5779 			return (TRAN_BUSY);
5780 		}
5781 	} else {
5782 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5783 	}
5784 
5785 	mutex_enter(cport_mutex);
5786 
5787 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5788 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5789 		mutex_exit(cport_mutex);
5790 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5791 		return (rval);
5792 	}
5793 
5794 	scsipkt->pkt_reason = CMD_CMPLT;
5795 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5796 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5797 
5798 	pc = scsipkt->pkt_cdbp[2] >> 6;
5799 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5800 
5801 	/* Reject not supported request for all but cumulative values */
5802 	switch (pc) {
5803 	case PC_CUMULATIVE_VALUES:
5804 		break;
5805 	default:
5806 		*scsipkt->pkt_scbp = STATUS_CHECK;
5807 		sense = sata_arq_sense(spx);
5808 		sense->es_key = KEY_ILLEGAL_REQUEST;
5809 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5810 		goto done;
5811 	}
5812 
5813 	switch (page_code) {
5814 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5815 	case PAGE_CODE_SELF_TEST_RESULTS:
5816 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5817 	case PAGE_CODE_SMART_READ_DATA:
5818 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5819 		break;
5820 	default:
5821 		*scsipkt->pkt_scbp = STATUS_CHECK;
5822 		sense = sata_arq_sense(spx);
5823 		sense->es_key = KEY_ILLEGAL_REQUEST;
5824 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5825 		goto done;
5826 	}
5827 
5828 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5829 		/*
5830 		 * Because log sense uses local buffers for data retrieval from
5831 		 * the devices and sets the data programatically in the
5832 		 * original specified buffer, release preallocated DMA
5833 		 * resources before storing data in the original buffer,
5834 		 * so no unwanted DMA sync would take place.
5835 		 */
5836 		sata_id_t *sata_id;
5837 
5838 		sata_scsi_dmafree(NULL, scsipkt);
5839 
5840 		len = 0;
5841 
5842 		/* Build log parameter header */
5843 		buf[len++] = page_code;	/* page code as in the CDB */
5844 		buf[len++] = 0;		/* reserved */
5845 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5846 		buf[len++] = 0;		/* (LSB) */
5847 
5848 		sdinfo = sata_get_device_info(
5849 		    spx->txlt_sata_hba_inst,
5850 		    &spx->txlt_sata_pkt->satapkt_device);
5851 
5852 		/*
5853 		 * Add requested pages.
5854 		 */
5855 		switch (page_code) {
5856 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5857 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5858 			break;
5859 		case PAGE_CODE_SELF_TEST_RESULTS:
5860 			sata_id = &sdinfo->satadrv_id;
5861 			if ((! (sata_id->ai_cmdset84 &
5862 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5863 			    (! (sata_id->ai_features87 &
5864 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5865 				*scsipkt->pkt_scbp = STATUS_CHECK;
5866 				sense = sata_arq_sense(spx);
5867 				sense->es_key = KEY_ILLEGAL_REQUEST;
5868 				sense->es_add_code =
5869 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5870 
5871 				goto done;
5872 			}
5873 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5874 			    spx->txlt_sata_hba_inst);
5875 			break;
5876 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5877 			sata_id = &sdinfo->satadrv_id;
5878 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5879 				*scsipkt->pkt_scbp = STATUS_CHECK;
5880 				sense = sata_arq_sense(spx);
5881 				sense->es_key = KEY_ILLEGAL_REQUEST;
5882 				sense->es_add_code =
5883 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5884 
5885 				goto done;
5886 			}
5887 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5888 				*scsipkt->pkt_scbp = STATUS_CHECK;
5889 				sense = sata_arq_sense(spx);
5890 				sense->es_key = KEY_ABORTED_COMMAND;
5891 				sense->es_add_code =
5892 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5893 				sense->es_qual_code =
5894 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5895 
5896 				goto done;
5897 			}
5898 
5899 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5900 			    spx->txlt_sata_hba_inst);
5901 			break;
5902 		case PAGE_CODE_SMART_READ_DATA:
5903 			sata_id = &sdinfo->satadrv_id;
5904 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5905 				*scsipkt->pkt_scbp = STATUS_CHECK;
5906 				sense = sata_arq_sense(spx);
5907 				sense->es_key = KEY_ILLEGAL_REQUEST;
5908 				sense->es_add_code =
5909 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5910 
5911 				goto done;
5912 			}
5913 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5914 				*scsipkt->pkt_scbp = STATUS_CHECK;
5915 				sense = sata_arq_sense(spx);
5916 				sense->es_key = KEY_ABORTED_COMMAND;
5917 				sense->es_add_code =
5918 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5919 				sense->es_qual_code =
5920 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5921 
5922 				goto done;
5923 			}
5924 
5925 			/* This page doesn't include a page header */
5926 			len = sata_build_lsense_page_30(sdinfo, buf,
5927 			    spx->txlt_sata_hba_inst);
5928 			goto no_header;
5929 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5930 			sata_id = &sdinfo->satadrv_id;
5931 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5932 				*scsipkt->pkt_scbp = STATUS_CHECK;
5933 				sense = sata_arq_sense(spx);
5934 				sense->es_key = KEY_ILLEGAL_REQUEST;
5935 				sense->es_add_code =
5936 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5937 
5938 				goto done;
5939 			}
5940 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5941 				*scsipkt->pkt_scbp = STATUS_CHECK;
5942 				sense = sata_arq_sense(spx);
5943 				sense->es_key = KEY_ABORTED_COMMAND;
5944 				sense->es_add_code =
5945 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5946 				sense->es_qual_code =
5947 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5948 
5949 				goto done;
5950 			}
5951 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5952 			goto no_header;
5953 		default:
5954 			/* Invalid request */
5955 			*scsipkt->pkt_scbp = STATUS_CHECK;
5956 			sense = sata_arq_sense(spx);
5957 			sense->es_key = KEY_ILLEGAL_REQUEST;
5958 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5959 			goto done;
5960 		}
5961 
5962 		/* set parameter log sense data length */
5963 		buf[2] = len >> 8;	/* log sense length (MSB) */
5964 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5965 
5966 		len += SCSI_LOG_PAGE_HDR_LEN;
5967 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5968 
5969 no_header:
5970 		/* Check allocation length */
5971 		alc_len = scsipkt->pkt_cdbp[7];
5972 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5973 
5974 		/*
5975 		 * We do not check for possible parameters truncation
5976 		 * (alc_len < len) assuming that the target driver works
5977 		 * correctly. Just avoiding overrun.
5978 		 * Copy no more than requested and possible, buffer-wise.
5979 		 */
5980 		count = MIN(alc_len, len);
5981 		count = MIN(bp->b_bcount, count);
5982 		bcopy(buf, bp->b_un.b_addr, count);
5983 
5984 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5985 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5986 	}
5987 	*scsipkt->pkt_scbp = STATUS_GOOD;
5988 done:
5989 	mutex_exit(cport_mutex);
5990 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5991 
5992 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5993 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5994 
5995 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5996 	    scsipkt->pkt_comp != NULL) {
5997 		/* scsi callback required */
5998 		if (servicing_interrupt()) {
5999 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6000 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6001 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6002 				return (TRAN_BUSY);
6003 			}
6004 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6005 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6006 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6007 			/* Scheduling the callback failed */
6008 			return (TRAN_BUSY);
6009 		}
6010 	}
6011 
6012 	return (TRAN_ACCEPT);
6013 }
6014 
6015 /*
6016  * Translate command: Log Select
6017  * Not implemented at this time - returns invalid command response.
6018  */
6019 static	int
6020 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6021 {
6022 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6023 	    "sata_txlt_log_select\n", NULL);
6024 
6025 	return (sata_txlt_invalid_command(spx));
6026 }
6027 
6028 
6029 /*
6030  * Translate command: Read (various types).
6031  * Translated into appropriate type of ATA READ command
6032  * for SATA hard disks.
6033  * Both the device capabilities and requested operation mode are
6034  * considered.
6035  *
6036  * Following scsi cdb fields are ignored:
6037  * rdprotect, dpo, fua, fua_nv, group_number.
6038  *
6039  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6040  * enable variable sata_func_enable), the capability of the controller and
6041  * capability of a device are checked and if both support queueing, read
6042  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6043  * command rather than plain READ_XXX command.
6044  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6045  * both the controller and device suport such functionality, the read
6046  * request will be translated to READ_FPDMA_QUEUED command.
6047  * In both cases the maximum queue depth is derived as minimum of:
6048  * HBA capability,device capability and sata_max_queue_depth variable setting.
6049  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6050  * used to pass max queue depth value, and the maximum possible queue depth
6051  * is 32.
6052  *
6053  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6054  * appropriate values in scsi_pkt fields.
6055  */
6056 static int
6057 sata_txlt_read(sata_pkt_txlate_t *spx)
6058 {
6059 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6060 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6061 	sata_drive_info_t *sdinfo;
6062 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6063 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6064 	uint16_t sec_count;
6065 	uint64_t lba;
6066 	int rval, reason;
6067 	int synch;
6068 
6069 	mutex_enter(cport_mutex);
6070 
6071 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6072 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6073 		mutex_exit(cport_mutex);
6074 		return (rval);
6075 	}
6076 
6077 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6078 	    &spx->txlt_sata_pkt->satapkt_device);
6079 
6080 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6081 	/*
6082 	 * Extract LBA and sector count from scsi CDB.
6083 	 */
6084 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6085 	case SCMD_READ:
6086 		/* 6-byte scsi read cmd : 0x08 */
6087 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6088 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6089 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6090 		sec_count = scsipkt->pkt_cdbp[4];
6091 		/* sec_count 0 will be interpreted as 256 by a device */
6092 		break;
6093 	case SCMD_READ_G1:
6094 		/* 10-bytes scsi read command : 0x28 */
6095 		lba = scsipkt->pkt_cdbp[2];
6096 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6097 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6098 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6099 		sec_count = scsipkt->pkt_cdbp[7];
6100 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6101 		break;
6102 	case SCMD_READ_G5:
6103 		/* 12-bytes scsi read command : 0xA8 */
6104 		lba = scsipkt->pkt_cdbp[2];
6105 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6106 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6107 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6108 		sec_count = scsipkt->pkt_cdbp[6];
6109 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6110 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6111 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6112 		break;
6113 	case SCMD_READ_G4:
6114 		/* 16-bytes scsi read command : 0x88 */
6115 		lba = scsipkt->pkt_cdbp[2];
6116 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6117 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6118 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6119 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6120 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6121 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6122 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6123 		sec_count = scsipkt->pkt_cdbp[10];
6124 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6125 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6126 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6127 		break;
6128 	default:
6129 		/* Unsupported command */
6130 		mutex_exit(cport_mutex);
6131 		return (sata_txlt_invalid_command(spx));
6132 	}
6133 
6134 	/*
6135 	 * Check if specified address exceeds device capacity
6136 	 */
6137 	if ((lba >= sdinfo->satadrv_capacity) ||
6138 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6139 		/* LBA out of range */
6140 		mutex_exit(cport_mutex);
6141 		return (sata_txlt_lba_out_of_range(spx));
6142 	}
6143 
6144 	/*
6145 	 * For zero-length transfer, emulate good completion of the command
6146 	 * (reasons for rejecting the command were already checked).
6147 	 * No DMA resources were allocated.
6148 	 */
6149 	if (spx->txlt_dma_cookie_list == NULL) {
6150 		mutex_exit(cport_mutex);
6151 		return (sata_emul_rw_completion(spx));
6152 	}
6153 
6154 	/*
6155 	 * Build cmd block depending on the device capability and
6156 	 * requested operation mode.
6157 	 * Do not bother with non-dma mode - we are working only with
6158 	 * devices supporting DMA.
6159 	 */
6160 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6161 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6162 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6163 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6164 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6165 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6166 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6167 #ifndef __lock_lint
6168 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6169 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6170 		scmd->satacmd_lba_high_msb = lba >> 40;
6171 #endif
6172 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6173 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6174 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6175 	}
6176 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6177 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6178 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6179 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6180 	scmd->satacmd_features_reg = 0;
6181 	scmd->satacmd_status_reg = 0;
6182 	scmd->satacmd_error_reg = 0;
6183 
6184 	/*
6185 	 * Check if queueing commands should be used and switch
6186 	 * to appropriate command if possible
6187 	 */
6188 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6189 		boolean_t using_queuing;
6190 
6191 		/* Queuing supported by controller and device? */
6192 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6193 		    (sdinfo->satadrv_features_support &
6194 		    SATA_DEV_F_NCQ) &&
6195 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6196 		    SATA_CTLF_NCQ)) {
6197 			using_queuing = B_TRUE;
6198 
6199 			/* NCQ supported - use FPDMA READ */
6200 			scmd->satacmd_cmd_reg =
6201 			    SATAC_READ_FPDMA_QUEUED;
6202 			scmd->satacmd_features_reg_ext =
6203 			    scmd->satacmd_sec_count_msb;
6204 			scmd->satacmd_sec_count_msb = 0;
6205 		} else if ((sdinfo->satadrv_features_support &
6206 		    SATA_DEV_F_TCQ) &&
6207 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6208 		    SATA_CTLF_QCMD)) {
6209 			using_queuing = B_TRUE;
6210 
6211 			/* Legacy queueing */
6212 			if (sdinfo->satadrv_features_support &
6213 			    SATA_DEV_F_LBA48) {
6214 				scmd->satacmd_cmd_reg =
6215 				    SATAC_READ_DMA_QUEUED_EXT;
6216 				scmd->satacmd_features_reg_ext =
6217 				    scmd->satacmd_sec_count_msb;
6218 				scmd->satacmd_sec_count_msb = 0;
6219 			} else {
6220 				scmd->satacmd_cmd_reg =
6221 				    SATAC_READ_DMA_QUEUED;
6222 			}
6223 		} else	/* NCQ nor legacy queuing not supported */
6224 			using_queuing = B_FALSE;
6225 
6226 		/*
6227 		 * If queuing, the sector count goes in the features register
6228 		 * and the secount count will contain the tag.
6229 		 */
6230 		if (using_queuing) {
6231 			scmd->satacmd_features_reg =
6232 			    scmd->satacmd_sec_count_lsb;
6233 			scmd->satacmd_sec_count_lsb = 0;
6234 			scmd->satacmd_flags.sata_queued = B_TRUE;
6235 
6236 			/* Set-up maximum queue depth */
6237 			scmd->satacmd_flags.sata_max_queue_depth =
6238 			    sdinfo->satadrv_max_queue_depth - 1;
6239 		} else if (sdinfo->satadrv_features_enabled &
6240 		    SATA_DEV_F_E_UNTAGGED_QING) {
6241 			/*
6242 			 * Although NCQ/TCQ is not enabled, untagged queuing
6243 			 * may be still used.
6244 			 * Set-up the maximum untagged queue depth.
6245 			 * Use controller's queue depth from sata_hba_tran.
6246 			 * SATA HBA drivers may ignore this value and rely on
6247 			 * the internal limits.For drivers that do not
6248 			 * ignore untaged queue depth, limit the value to
6249 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6250 			 * largest value that can be passed via
6251 			 * satacmd_flags.sata_max_queue_depth.
6252 			 */
6253 			scmd->satacmd_flags.sata_max_queue_depth =
6254 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6255 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6256 
6257 		} else {
6258 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6259 		}
6260 	} else
6261 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6262 
6263 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6264 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6265 	    scmd->satacmd_cmd_reg, lba, sec_count);
6266 
6267 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6268 		/* Need callback function */
6269 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6270 		synch = FALSE;
6271 	} else
6272 		synch = TRUE;
6273 
6274 	/* Transfer command to HBA */
6275 	if (sata_hba_start(spx, &rval) != 0) {
6276 		/* Pkt not accepted for execution */
6277 		mutex_exit(cport_mutex);
6278 		return (rval);
6279 	}
6280 	mutex_exit(cport_mutex);
6281 	/*
6282 	 * If execution is non-synchronous,
6283 	 * a callback function will handle potential errors, translate
6284 	 * the response and will do a callback to a target driver.
6285 	 * If it was synchronous, check execution status using the same
6286 	 * framework callback.
6287 	 */
6288 	if (synch) {
6289 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6290 		    "synchronous execution status %x\n",
6291 		    spx->txlt_sata_pkt->satapkt_reason);
6292 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6293 	}
6294 	return (TRAN_ACCEPT);
6295 }
6296 
6297 
6298 /*
6299  * SATA translate command: Write (various types)
6300  * Translated into appropriate type of ATA WRITE command
6301  * for SATA hard disks.
6302  * Both the device capabilities and requested operation mode are
6303  * considered.
6304  *
6305  * Following scsi cdb fields are ignored:
6306  * rwprotect, dpo, fua, fua_nv, group_number.
6307  *
6308  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6309  * enable variable sata_func_enable), the capability of the controller and
6310  * capability of a device are checked and if both support queueing, write
6311  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6312  * command rather than plain WRITE_XXX command.
6313  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6314  * both the controller and device suport such functionality, the write
6315  * request will be translated to WRITE_FPDMA_QUEUED command.
6316  * In both cases the maximum queue depth is derived as minimum of:
6317  * HBA capability,device capability and sata_max_queue_depth variable setting.
6318  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6319  * used to pass max queue depth value, and the maximum possible queue depth
6320  * is 32.
6321  *
6322  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6323  * appropriate values in scsi_pkt fields.
6324  */
6325 static int
6326 sata_txlt_write(sata_pkt_txlate_t *spx)
6327 {
6328 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6329 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6330 	sata_drive_info_t *sdinfo;
6331 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6332 	uint16_t sec_count;
6333 	uint64_t lba;
6334 	int rval, reason;
6335 	int synch;
6336 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6337 
6338 	mutex_enter(cport_mutex);
6339 
6340 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6341 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6342 		mutex_exit(cport_mutex);
6343 		return (rval);
6344 	}
6345 
6346 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6347 	    &spx->txlt_sata_pkt->satapkt_device);
6348 
6349 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6350 	/*
6351 	 * Extract LBA and sector count from scsi CDB
6352 	 */
6353 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6354 	case SCMD_WRITE:
6355 		/* 6-byte scsi read cmd : 0x0A */
6356 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6357 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6358 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6359 		sec_count = scsipkt->pkt_cdbp[4];
6360 		/* sec_count 0 will be interpreted as 256 by a device */
6361 		break;
6362 	case SCMD_WRITE_G1:
6363 		/* 10-bytes scsi write command : 0x2A */
6364 		lba = scsipkt->pkt_cdbp[2];
6365 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6366 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6367 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6368 		sec_count = scsipkt->pkt_cdbp[7];
6369 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6370 		break;
6371 	case SCMD_WRITE_G5:
6372 		/* 12-bytes scsi read command : 0xAA */
6373 		lba = scsipkt->pkt_cdbp[2];
6374 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6375 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6376 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6377 		sec_count = scsipkt->pkt_cdbp[6];
6378 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6379 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6380 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6381 		break;
6382 	case SCMD_WRITE_G4:
6383 		/* 16-bytes scsi write command : 0x8A */
6384 		lba = scsipkt->pkt_cdbp[2];
6385 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6386 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6387 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6388 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6389 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6390 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6391 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6392 		sec_count = scsipkt->pkt_cdbp[10];
6393 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6394 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6395 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6396 		break;
6397 	default:
6398 		/* Unsupported command */
6399 		mutex_exit(cport_mutex);
6400 		return (sata_txlt_invalid_command(spx));
6401 	}
6402 
6403 	/*
6404 	 * Check if specified address and length exceeds device capacity
6405 	 */
6406 	if ((lba >= sdinfo->satadrv_capacity) ||
6407 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6408 		/* LBA out of range */
6409 		mutex_exit(cport_mutex);
6410 		return (sata_txlt_lba_out_of_range(spx));
6411 	}
6412 
6413 	/*
6414 	 * For zero-length transfer, emulate good completion of the command
6415 	 * (reasons for rejecting the command were already checked).
6416 	 * No DMA resources were allocated.
6417 	 */
6418 	if (spx->txlt_dma_cookie_list == NULL) {
6419 		mutex_exit(cport_mutex);
6420 		return (sata_emul_rw_completion(spx));
6421 	}
6422 
6423 	/*
6424 	 * Build cmd block depending on the device capability and
6425 	 * requested operation mode.
6426 	 * Do not bother with non-dma mode- we are working only with
6427 	 * devices supporting DMA.
6428 	 */
6429 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6430 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6431 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6432 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6433 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6434 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6435 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6436 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6437 #ifndef __lock_lint
6438 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6439 		scmd->satacmd_lba_high_msb = lba >> 40;
6440 #endif
6441 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6442 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6443 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6444 	}
6445 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6446 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6447 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6448 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6449 	scmd->satacmd_features_reg = 0;
6450 	scmd->satacmd_status_reg = 0;
6451 	scmd->satacmd_error_reg = 0;
6452 
6453 	/*
6454 	 * Check if queueing commands should be used and switch
6455 	 * to appropriate command if possible
6456 	 */
6457 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6458 		boolean_t using_queuing;
6459 
6460 		/* Queuing supported by controller and device? */
6461 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6462 		    (sdinfo->satadrv_features_support &
6463 		    SATA_DEV_F_NCQ) &&
6464 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6465 		    SATA_CTLF_NCQ)) {
6466 			using_queuing = B_TRUE;
6467 
6468 			/* NCQ supported - use FPDMA WRITE */
6469 			scmd->satacmd_cmd_reg =
6470 			    SATAC_WRITE_FPDMA_QUEUED;
6471 			scmd->satacmd_features_reg_ext =
6472 			    scmd->satacmd_sec_count_msb;
6473 			scmd->satacmd_sec_count_msb = 0;
6474 		} else if ((sdinfo->satadrv_features_support &
6475 		    SATA_DEV_F_TCQ) &&
6476 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6477 		    SATA_CTLF_QCMD)) {
6478 			using_queuing = B_TRUE;
6479 
6480 			/* Legacy queueing */
6481 			if (sdinfo->satadrv_features_support &
6482 			    SATA_DEV_F_LBA48) {
6483 				scmd->satacmd_cmd_reg =
6484 				    SATAC_WRITE_DMA_QUEUED_EXT;
6485 				scmd->satacmd_features_reg_ext =
6486 				    scmd->satacmd_sec_count_msb;
6487 				scmd->satacmd_sec_count_msb = 0;
6488 			} else {
6489 				scmd->satacmd_cmd_reg =
6490 				    SATAC_WRITE_DMA_QUEUED;
6491 			}
6492 		} else	/*  NCQ nor legacy queuing not supported */
6493 			using_queuing = B_FALSE;
6494 
6495 		if (using_queuing) {
6496 			scmd->satacmd_features_reg =
6497 			    scmd->satacmd_sec_count_lsb;
6498 			scmd->satacmd_sec_count_lsb = 0;
6499 			scmd->satacmd_flags.sata_queued = B_TRUE;
6500 			/* Set-up maximum queue depth */
6501 			scmd->satacmd_flags.sata_max_queue_depth =
6502 			    sdinfo->satadrv_max_queue_depth - 1;
6503 		} else if (sdinfo->satadrv_features_enabled &
6504 		    SATA_DEV_F_E_UNTAGGED_QING) {
6505 			/*
6506 			 * Although NCQ/TCQ is not enabled, untagged queuing
6507 			 * may be still used.
6508 			 * Set-up the maximum untagged queue depth.
6509 			 * Use controller's queue depth from sata_hba_tran.
6510 			 * SATA HBA drivers may ignore this value and rely on
6511 			 * the internal limits. For drivera that do not
6512 			 * ignore untaged queue depth, limit the value to
6513 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6514 			 * largest value that can be passed via
6515 			 * satacmd_flags.sata_max_queue_depth.
6516 			 */
6517 			scmd->satacmd_flags.sata_max_queue_depth =
6518 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6519 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6520 
6521 		} else {
6522 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6523 		}
6524 	} else
6525 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6526 
6527 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6528 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6529 	    scmd->satacmd_cmd_reg, lba, sec_count);
6530 
6531 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6532 		/* Need callback function */
6533 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6534 		synch = FALSE;
6535 	} else
6536 		synch = TRUE;
6537 
6538 	/* Transfer command to HBA */
6539 	if (sata_hba_start(spx, &rval) != 0) {
6540 		/* Pkt not accepted for execution */
6541 		mutex_exit(cport_mutex);
6542 		return (rval);
6543 	}
6544 	mutex_exit(cport_mutex);
6545 
6546 	/*
6547 	 * If execution is non-synchronous,
6548 	 * a callback function will handle potential errors, translate
6549 	 * the response and will do a callback to a target driver.
6550 	 * If it was synchronous, check execution status using the same
6551 	 * framework callback.
6552 	 */
6553 	if (synch) {
6554 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6555 		    "synchronous execution status %x\n",
6556 		    spx->txlt_sata_pkt->satapkt_reason);
6557 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6558 	}
6559 	return (TRAN_ACCEPT);
6560 }
6561 
6562 
6563 /*
6564  * Implements SCSI SBC WRITE BUFFER command download microcode option
6565  */
6566 static int
6567 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6568 {
6569 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6570 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6571 
6572 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6573 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6574 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6575 
6576 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6577 	struct scsi_extended_sense *sense;
6578 	int rval, mode, sector_count, reason;
6579 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6580 
6581 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6582 
6583 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6584 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6585 
6586 	mutex_enter(cport_mutex);
6587 
6588 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6589 	    TRAN_ACCEPT) {
6590 		mutex_exit(cport_mutex);
6591 		return (rval);
6592 	}
6593 
6594 	/* Use synchronous mode */
6595 	spx->txlt_sata_pkt->satapkt_op_mode
6596 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6597 
6598 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6599 
6600 	scsipkt->pkt_reason = CMD_CMPLT;
6601 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6602 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6603 
6604 	/*
6605 	 * The SCSI to ATA translation specification only calls
6606 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6607 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6608 	 * ATA 8 (draft) got rid of download microcode for temp
6609 	 * and it is even optional for ATA 7, so it may be aborted.
6610 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6611 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6612 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6613 	 * sectors.  Thus the offset really doesn't buy us anything.
6614 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6615 	 * is revised, this can be revisisted.
6616 	 */
6617 	/* Reject not supported request */
6618 	switch (mode) {
6619 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6620 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6621 		break;
6622 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6623 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6624 		break;
6625 	default:
6626 		goto bad_param;
6627 	}
6628 
6629 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6630 
6631 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6632 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6633 		goto bad_param;
6634 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6635 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6636 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6637 	scmd->satacmd_lba_mid_lsb = 0;
6638 	scmd->satacmd_lba_high_lsb = 0;
6639 	scmd->satacmd_device_reg = 0;
6640 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6641 	scmd->satacmd_addr_type = 0;
6642 
6643 	/* Transfer command to HBA */
6644 	if (sata_hba_start(spx, &rval) != 0) {
6645 		/* Pkt not accepted for execution */
6646 		mutex_exit(cport_mutex);
6647 		return (rval);
6648 	}
6649 
6650 	mutex_exit(cport_mutex);
6651 
6652 	/* Then we need synchronous check the status of the disk */
6653 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6654 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6655 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6656 		scsipkt->pkt_reason = CMD_CMPLT;
6657 
6658 		/* Download commmand succeed, so probe and identify device */
6659 		sata_reidentify_device(spx);
6660 	} else {
6661 		/* Something went wrong, microcode download command failed */
6662 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6663 		*scsipkt->pkt_scbp = STATUS_CHECK;
6664 		sense = sata_arq_sense(spx);
6665 		switch (sata_pkt->satapkt_reason) {
6666 		case SATA_PKT_PORT_ERROR:
6667 			/*
6668 			 * We have no device data. Assume no data transfered.
6669 			 */
6670 			sense->es_key = KEY_HARDWARE_ERROR;
6671 			break;
6672 
6673 		case SATA_PKT_DEV_ERROR:
6674 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6675 			    SATA_STATUS_ERR) {
6676 				/*
6677 				 * determine dev error reason from error
6678 				 * reg content
6679 				 */
6680 				sata_decode_device_error(spx, sense);
6681 				break;
6682 			}
6683 			/* No extended sense key - no info available */
6684 			break;
6685 
6686 		case SATA_PKT_TIMEOUT:
6687 			scsipkt->pkt_reason = CMD_TIMEOUT;
6688 			scsipkt->pkt_statistics |=
6689 			    STAT_TIMEOUT | STAT_DEV_RESET;
6690 			/* No extended sense key ? */
6691 			break;
6692 
6693 		case SATA_PKT_ABORTED:
6694 			scsipkt->pkt_reason = CMD_ABORTED;
6695 			scsipkt->pkt_statistics |= STAT_ABORTED;
6696 			/* No extended sense key ? */
6697 			break;
6698 
6699 		case SATA_PKT_RESET:
6700 			/* pkt aborted by an explicit reset from a host */
6701 			scsipkt->pkt_reason = CMD_RESET;
6702 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6703 			break;
6704 
6705 		default:
6706 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6707 			    "sata_txlt_nodata_cmd_completion: "
6708 			    "invalid packet completion reason %d",
6709 			    sata_pkt->satapkt_reason));
6710 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6711 			break;
6712 		}
6713 
6714 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6715 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6716 
6717 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6718 			/* scsi callback required */
6719 			scsi_hba_pkt_comp(scsipkt);
6720 	}
6721 	return (TRAN_ACCEPT);
6722 
6723 bad_param:
6724 	mutex_exit(cport_mutex);
6725 	*scsipkt->pkt_scbp = STATUS_CHECK;
6726 	sense = sata_arq_sense(spx);
6727 	sense->es_key = KEY_ILLEGAL_REQUEST;
6728 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6729 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6730 	    scsipkt->pkt_comp != NULL) {
6731 		/* scsi callback required */
6732 		if (servicing_interrupt()) {
6733 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6734 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6735 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6736 				return (TRAN_BUSY);
6737 			}
6738 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6739 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6740 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6741 			/* Scheduling the callback failed */
6742 			return (TRAN_BUSY);
6743 		}
6744 	}
6745 	return (rval);
6746 }
6747 
6748 /*
6749  * Re-identify device after doing a firmware download.
6750  */
6751 static void
6752 sata_reidentify_device(sata_pkt_txlate_t *spx)
6753 {
6754 #define	DOWNLOAD_WAIT_TIME_SECS	60
6755 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6756 	int rval;
6757 	int retry_cnt;
6758 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6759 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6760 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6761 	sata_drive_info_t *sdinfo;
6762 
6763 	/*
6764 	 * Before returning good status, probe device.
6765 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6766 	 * The assumption is that the new microcode is applied by the
6767 	 * device. It is a caller responsibility to verify this.
6768 	 */
6769 	for (retry_cnt = 0;
6770 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6771 	    retry_cnt++) {
6772 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6773 
6774 		if (rval == SATA_SUCCESS) { /* Set default features */
6775 			sdinfo = sata_get_device_info(sata_hba_inst,
6776 			    &sata_device);
6777 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6778 			    SATA_SUCCESS) {
6779 				/* retry */
6780 				rval = sata_initialize_device(sata_hba_inst,
6781 				    sdinfo);
6782 				if (rval == SATA_RETRY)
6783 					sata_log(sata_hba_inst, CE_WARN,
6784 					    "SATA device at port %d pmport %d -"
6785 					    " default device features could not"
6786 					    " be set. Device may not operate "
6787 					    "as expected.",
6788 					    sata_device.satadev_addr.cport,
6789 					    sata_device.satadev_addr.pmport);
6790 			}
6791 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6792 				scsi_hba_pkt_comp(scsipkt);
6793 			return;
6794 		} else if (rval == SATA_RETRY) {
6795 			delay(drv_usectohz(1000000 *
6796 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6797 			continue;
6798 		} else	/* failed - no reason to retry */
6799 			break;
6800 	}
6801 
6802 	/*
6803 	 * Something went wrong, device probing failed.
6804 	 */
6805 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6806 	    "Cannot probe device after downloading microcode\n"));
6807 
6808 	/* Reset device to force retrying the probe. */
6809 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6810 	    (SATA_DIP(sata_hba_inst), &sata_device);
6811 
6812 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6813 		scsi_hba_pkt_comp(scsipkt);
6814 }
6815 
6816 
6817 /*
6818  * Translate command: Synchronize Cache.
6819  * Translates into Flush Cache command for SATA hard disks.
6820  *
6821  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6822  * appropriate values in scsi_pkt fields.
6823  */
6824 static 	int
6825 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6826 {
6827 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6828 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6829 	int rval, reason;
6830 	int synch;
6831 
6832 	mutex_enter(cport_mutex);
6833 
6834 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6835 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6836 		mutex_exit(cport_mutex);
6837 		return (rval);
6838 	}
6839 
6840 	scmd->satacmd_addr_type = 0;
6841 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6842 	scmd->satacmd_device_reg = 0;
6843 	scmd->satacmd_sec_count_lsb = 0;
6844 	scmd->satacmd_lba_low_lsb = 0;
6845 	scmd->satacmd_lba_mid_lsb = 0;
6846 	scmd->satacmd_lba_high_lsb = 0;
6847 	scmd->satacmd_features_reg = 0;
6848 	scmd->satacmd_status_reg = 0;
6849 	scmd->satacmd_error_reg = 0;
6850 
6851 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6852 	    "sata_txlt_synchronize_cache\n", NULL);
6853 
6854 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6855 		/* Need to set-up a callback function */
6856 		spx->txlt_sata_pkt->satapkt_comp =
6857 		    sata_txlt_nodata_cmd_completion;
6858 		synch = FALSE;
6859 	} else
6860 		synch = TRUE;
6861 
6862 	/* Transfer command to HBA */
6863 	if (sata_hba_start(spx, &rval) != 0) {
6864 		/* Pkt not accepted for execution */
6865 		mutex_exit(cport_mutex);
6866 		return (rval);
6867 	}
6868 	mutex_exit(cport_mutex);
6869 
6870 	/*
6871 	 * If execution non-synchronous, it had to be completed
6872 	 * a callback function will handle potential errors, translate
6873 	 * the response and will do a callback to a target driver.
6874 	 * If it was synchronous, check status, using the same
6875 	 * framework callback.
6876 	 */
6877 	if (synch) {
6878 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6879 		    "synchronous execution status %x\n",
6880 		    spx->txlt_sata_pkt->satapkt_reason);
6881 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6882 	}
6883 	return (TRAN_ACCEPT);
6884 }
6885 
6886 
6887 /*
6888  * Send pkt to SATA HBA driver
6889  *
6890  * This function may be called only if the operation is requested by scsi_pkt,
6891  * i.e. scsi_pkt is not NULL.
6892  *
6893  * This function has to be called with cport mutex held. It does release
6894  * the mutex when it calls HBA driver sata_tran_start function and
6895  * re-acquires it afterwards.
6896  *
6897  * If return value is 0, pkt was accepted, -1 otherwise
6898  * rval is set to appropriate sata_scsi_start return value.
6899  *
6900  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6901  * have called the sata_pkt callback function for this packet.
6902  *
6903  * The scsi callback has to be performed by the caller of this routine.
6904  */
6905 static int
6906 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6907 {
6908 	int stat;
6909 	uint8_t cport = SATA_TXLT_CPORT(spx);
6910 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6911 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6912 	sata_drive_info_t *sdinfo;
6913 	sata_pmult_info_t *pminfo;
6914 	sata_pmport_info_t *pmportinfo = NULL;
6915 	sata_device_t *sata_device = NULL;
6916 	uint8_t cmd;
6917 	struct sata_cmd_flags cmd_flags;
6918 
6919 	ASSERT(spx->txlt_sata_pkt != NULL);
6920 
6921 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6922 
6923 	sdinfo = sata_get_device_info(sata_hba_inst,
6924 	    &spx->txlt_sata_pkt->satapkt_device);
6925 	ASSERT(sdinfo != NULL);
6926 
6927 	/* Clear device reset state? */
6928 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6929 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6930 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6931 
6932 		/*
6933 		 * Get the pmult_info of the its parent port multiplier, all
6934 		 * sub-devices share a common device reset flags on in
6935 		 * pmult_info.
6936 		 */
6937 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6938 		pmportinfo = pminfo->pmult_dev_port[pmport];
6939 		ASSERT(pminfo != NULL);
6940 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6941 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6942 			    sata_clear_dev_reset = B_TRUE;
6943 			pminfo->pmult_event_flags &=
6944 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6945 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6946 			    "sata_hba_start: clearing device reset state"
6947 			    "on pmult.\n", NULL);
6948 		}
6949 	} else {
6950 		if (sdinfo->satadrv_event_flags &
6951 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6952 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6953 			    sata_clear_dev_reset = B_TRUE;
6954 			sdinfo->satadrv_event_flags &=
6955 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6956 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6957 			    "sata_hba_start: clearing device reset state\n",
6958 			    NULL);
6959 		}
6960 	}
6961 
6962 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6963 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6964 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6965 
6966 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6967 
6968 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6969 	    "Sata cmd 0x%2x\n", cmd);
6970 
6971 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6972 	    spx->txlt_sata_pkt);
6973 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6974 	/*
6975 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6976 	 * with the sata callback, the sata_pkt could be already destroyed
6977 	 * by the time we check ther return status from the hba_start()
6978 	 * function, because sata_scsi_destroy_pkt() could have been already
6979 	 * called (perhaps in the interrupt context). So, in such case, there
6980 	 * should be no references to it. In other cases, sata_pkt still
6981 	 * exists.
6982 	 */
6983 	if (stat == SATA_TRAN_ACCEPTED) {
6984 		/*
6985 		 * pkt accepted for execution.
6986 		 * If it was executed synchronously, it is already completed
6987 		 * and pkt completion_reason indicates completion status.
6988 		 */
6989 		*rval = TRAN_ACCEPT;
6990 		return (0);
6991 	}
6992 
6993 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6994 	switch (stat) {
6995 	case SATA_TRAN_QUEUE_FULL:
6996 		/*
6997 		 * Controller detected queue full condition.
6998 		 */
6999 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7000 		    "sata_hba_start: queue full\n", NULL);
7001 
7002 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7003 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7004 
7005 		*rval = TRAN_BUSY;
7006 		break;
7007 
7008 	case SATA_TRAN_PORT_ERROR:
7009 		/*
7010 		 * Communication/link with device or general port error
7011 		 * detected before pkt execution begun.
7012 		 */
7013 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7014 		    SATA_ADDR_CPORT ||
7015 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7016 		    SATA_ADDR_DCPORT)
7017 			sata_log(sata_hba_inst, CE_CONT,
7018 			    "SATA port %d error",
7019 			    sata_device->satadev_addr.cport);
7020 		else
7021 			sata_log(sata_hba_inst, CE_CONT,
7022 			    "SATA port %d:%d error\n",
7023 			    sata_device->satadev_addr.cport,
7024 			    sata_device->satadev_addr.pmport);
7025 
7026 		/*
7027 		 * Update the port/device structure.
7028 		 * sata_pkt should be still valid. Since port error is
7029 		 * returned, sata_device content should reflect port
7030 		 * state - it means, that sata address have been changed,
7031 		 * because original packet's sata address refered to a device
7032 		 * attached to some port.
7033 		 */
7034 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7035 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7036 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7037 			mutex_enter(&pmportinfo->pmport_mutex);
7038 			sata_update_pmport_info(sata_hba_inst, sata_device);
7039 			mutex_exit(&pmportinfo->pmport_mutex);
7040 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7041 		} else {
7042 			sata_update_port_info(sata_hba_inst, sata_device);
7043 		}
7044 
7045 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7046 		*rval = TRAN_FATAL_ERROR;
7047 		break;
7048 
7049 	case SATA_TRAN_CMD_UNSUPPORTED:
7050 		/*
7051 		 * Command rejected by HBA as unsupported. It was HBA driver
7052 		 * that rejected the command, command was not sent to
7053 		 * an attached device.
7054 		 */
7055 		if ((sdinfo != NULL) &&
7056 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7057 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7058 			    "sat_hba_start: cmd 0x%2x rejected "
7059 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7060 
7061 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7062 		(void) sata_txlt_invalid_command(spx);
7063 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7064 
7065 		*rval = TRAN_ACCEPT;
7066 		break;
7067 
7068 	case SATA_TRAN_BUSY:
7069 		/*
7070 		 * Command rejected by HBA because other operation prevents
7071 		 * accepting the packet, or device is in RESET condition.
7072 		 */
7073 		if (sdinfo != NULL) {
7074 			sdinfo->satadrv_state =
7075 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7076 
7077 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7078 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7079 				    "sata_hba_start: cmd 0x%2x rejected "
7080 				    "because of device reset condition\n",
7081 				    cmd);
7082 			} else {
7083 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7084 				    "sata_hba_start: cmd 0x%2x rejected "
7085 				    "with SATA_TRAN_BUSY status\n",
7086 				    cmd);
7087 			}
7088 		}
7089 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7090 		*rval = TRAN_BUSY;
7091 		break;
7092 
7093 	default:
7094 		/* Unrecognized HBA response */
7095 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7096 		    "sata_hba_start: unrecognized HBA response "
7097 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7098 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7099 		*rval = TRAN_FATAL_ERROR;
7100 		break;
7101 	}
7102 
7103 	/*
7104 	 * If we got here, the packet was rejected.
7105 	 * Check if we need to remember reset state clearing request
7106 	 */
7107 	if (cmd_flags.sata_clear_dev_reset) {
7108 		/*
7109 		 * Check if device is still configured - it may have
7110 		 * disapeared from the configuration
7111 		 */
7112 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7113 		if (sdinfo != NULL) {
7114 			/*
7115 			 * Restore the flag that requests clearing of
7116 			 * the device reset state,
7117 			 * so the next sata packet may carry it to HBA.
7118 			 */
7119 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7120 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7121 				pminfo->pmult_event_flags |=
7122 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7123 			} else {
7124 				sdinfo->satadrv_event_flags |=
7125 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7126 			}
7127 		}
7128 	}
7129 	return (-1);
7130 }
7131 
7132 /*
7133  * Scsi response setup for invalid LBA
7134  *
7135  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7136  */
7137 static int
7138 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7139 {
7140 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7141 	struct scsi_extended_sense *sense;
7142 
7143 	scsipkt->pkt_reason = CMD_CMPLT;
7144 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7145 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7146 	*scsipkt->pkt_scbp = STATUS_CHECK;
7147 
7148 	*scsipkt->pkt_scbp = STATUS_CHECK;
7149 	sense = sata_arq_sense(spx);
7150 	sense->es_key = KEY_ILLEGAL_REQUEST;
7151 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7152 
7153 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7154 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7155 
7156 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7157 	    scsipkt->pkt_comp != NULL) {
7158 		/* scsi callback required */
7159 		if (servicing_interrupt()) {
7160 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7161 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7162 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7163 				return (TRAN_BUSY);
7164 			}
7165 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7166 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7167 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7168 			/* Scheduling the callback failed */
7169 			return (TRAN_BUSY);
7170 		}
7171 	}
7172 	return (TRAN_ACCEPT);
7173 }
7174 
7175 
7176 /*
7177  * Analyze device status and error registers and translate them into
7178  * appropriate scsi sense codes.
7179  * NOTE: non-packet commands only for now
7180  */
7181 static void
7182 sata_decode_device_error(sata_pkt_txlate_t *spx,
7183     struct scsi_extended_sense *sense)
7184 {
7185 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7186 
7187 	ASSERT(sense != NULL);
7188 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7189 	    SATA_STATUS_ERR);
7190 
7191 
7192 	if (err_reg & SATA_ERROR_ICRC) {
7193 		sense->es_key = KEY_ABORTED_COMMAND;
7194 		sense->es_add_code = 0x08; /* Communication failure */
7195 		return;
7196 	}
7197 
7198 	if (err_reg & SATA_ERROR_UNC) {
7199 		sense->es_key = KEY_MEDIUM_ERROR;
7200 		/* Information bytes (LBA) need to be set by a caller */
7201 		return;
7202 	}
7203 
7204 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7205 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7206 		sense->es_key = KEY_UNIT_ATTENTION;
7207 		sense->es_add_code = 0x3a; /* No media present */
7208 		return;
7209 	}
7210 
7211 	if (err_reg & SATA_ERROR_IDNF) {
7212 		if (err_reg & SATA_ERROR_ABORT) {
7213 			sense->es_key = KEY_ABORTED_COMMAND;
7214 		} else {
7215 			sense->es_key = KEY_ILLEGAL_REQUEST;
7216 			sense->es_add_code = 0x21; /* LBA out of range */
7217 		}
7218 		return;
7219 	}
7220 
7221 	if (err_reg & SATA_ERROR_ABORT) {
7222 		ASSERT(spx->txlt_sata_pkt != NULL);
7223 		sense->es_key = KEY_ABORTED_COMMAND;
7224 		return;
7225 	}
7226 }
7227 
7228 /*
7229  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7230  */
7231 static void
7232 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7233 {
7234 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7235 
7236 	*lba = 0;
7237 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7238 		*lba = sata_cmd->satacmd_lba_high_msb;
7239 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7240 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7241 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7242 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7243 	}
7244 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7245 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7246 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7247 }
7248 
7249 /*
7250  * This is fixed sense format - if LBA exceeds the info field size,
7251  * no valid info will be returned (valid bit in extended sense will
7252  * be set to 0).
7253  */
7254 static struct scsi_extended_sense *
7255 sata_arq_sense(sata_pkt_txlate_t *spx)
7256 {
7257 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7258 	struct scsi_arq_status *arqs;
7259 	struct scsi_extended_sense *sense;
7260 
7261 	/* Fill ARQ sense data */
7262 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7263 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7264 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7265 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7266 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7267 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7268 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7269 	arqs->sts_rqpkt_resid = 0;
7270 	sense = &arqs->sts_sensedata;
7271 	bzero(sense, sizeof (struct scsi_extended_sense));
7272 	sata_fixed_sense_data_preset(sense);
7273 	return (sense);
7274 }
7275 
7276 /*
7277  * ATA Pass Through support
7278  * Sets flags indicating that an invalid value was found in some
7279  * field in the command.  It could be something illegal according to
7280  * the SAT-2 spec or it could be a feature that is not (yet?)
7281  * supported.
7282  */
7283 static int
7284 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7285 {
7286 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7287 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7288 
7289 	scsipkt->pkt_reason = CMD_CMPLT;
7290 	*scsipkt->pkt_scbp = STATUS_CHECK;
7291 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7292 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7293 
7294 	sense = sata_arq_sense(spx);
7295 	sense->es_key = KEY_ILLEGAL_REQUEST;
7296 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7297 
7298 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7299 	    scsipkt->pkt_comp != NULL) {
7300 		/* scsi callback required */
7301 		if (servicing_interrupt()) {
7302 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7303 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7304 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7305 				return (TRAN_BUSY);
7306 			}
7307 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7308 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7309 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7310 			/* Scheduling the callback failed */
7311 			return (TRAN_BUSY);
7312 		}
7313 	}
7314 
7315 	return (TRAN_ACCEPT);
7316 }
7317 
7318 /*
7319  * The UNMAP command considers it not to be an error if the parameter length
7320  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7321  * to do so just complete the command.
7322  */
7323 static int
7324 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7325 {
7326 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7327 
7328 	scsipkt->pkt_reason = CMD_CMPLT;
7329 	*scsipkt->pkt_scbp = STATUS_GOOD;
7330 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7331 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7332 
7333 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7334 	    scsipkt->pkt_comp != NULL) {
7335 		/* scsi callback required */
7336 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7337 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7338 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7339 			/* Scheduling the callback failed */
7340 			return (TRAN_BUSY);
7341 		}
7342 	}
7343 
7344 	return (TRAN_ACCEPT);
7345 }
7346 
7347 /*
7348  * Emulated SATA Read/Write command completion for zero-length requests.
7349  * This request always succedes, so in synchronous mode it always returns
7350  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7351  * callback cannot be scheduled.
7352  */
7353 static int
7354 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7355 {
7356 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7357 
7358 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7359 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7360 	scsipkt->pkt_reason = CMD_CMPLT;
7361 	*scsipkt->pkt_scbp = STATUS_GOOD;
7362 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7363 		/* scsi callback required - have to schedule it */
7364 		if (servicing_interrupt()) {
7365 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7366 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7367 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7368 				return (TRAN_BUSY);
7369 			}
7370 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7371 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7372 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7373 			/* Scheduling the callback failed */
7374 			return (TRAN_BUSY);
7375 		}
7376 	}
7377 	return (TRAN_ACCEPT);
7378 }
7379 
7380 
7381 /*
7382  * Translate completion status of SATA read/write commands into scsi response.
7383  * pkt completion_reason is checked to determine the completion status.
7384  * Do scsi callback if necessary.
7385  *
7386  * Note: this function may be called also for synchronously executed
7387  * commands.
7388  * This function may be used only if scsi_pkt is non-NULL.
7389  */
7390 static void
7391 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7392 {
7393 	sata_pkt_txlate_t *spx =
7394 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7395 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7396 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7397 	struct scsi_extended_sense *sense;
7398 	uint64_t lba;
7399 	struct buf *bp;
7400 	int rval;
7401 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7402 		/* Normal completion */
7403 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7404 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7405 		scsipkt->pkt_reason = CMD_CMPLT;
7406 		*scsipkt->pkt_scbp = STATUS_GOOD;
7407 		if (spx->txlt_tmp_buf != NULL) {
7408 			/* Temporary buffer was used */
7409 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7410 			if (bp->b_flags & B_READ) {
7411 				rval = ddi_dma_sync(
7412 				    spx->txlt_buf_dma_handle, 0, 0,
7413 				    DDI_DMA_SYNC_FORCPU);
7414 				ASSERT(rval == DDI_SUCCESS);
7415 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7416 				    bp->b_bcount);
7417 			}
7418 		}
7419 	} else {
7420 		/*
7421 		 * Something went wrong - analyze return
7422 		 */
7423 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7424 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7425 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7426 		*scsipkt->pkt_scbp = STATUS_CHECK;
7427 		sense = sata_arq_sense(spx);
7428 		ASSERT(sense != NULL);
7429 
7430 		/*
7431 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7432 		 * extract from device registers the failing LBA.
7433 		 */
7434 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7435 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7436 			    (scmd->satacmd_lba_mid_msb != 0 ||
7437 			    scmd->satacmd_lba_high_msb != 0)) {
7438 				/*
7439 				 * We have problem reporting this cmd LBA
7440 				 * in fixed sense data format, because of
7441 				 * the size of the scsi LBA fields.
7442 				 */
7443 				sense->es_valid = 0;
7444 			} else {
7445 				sata_extract_error_lba(spx, &lba);
7446 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7447 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7448 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7449 				sense->es_info_4 = lba & 0xFF;
7450 			}
7451 		} else {
7452 			/* Invalid extended sense info */
7453 			sense->es_valid = 0;
7454 		}
7455 
7456 		switch (sata_pkt->satapkt_reason) {
7457 		case SATA_PKT_PORT_ERROR:
7458 			/* We may want to handle DEV GONE state as well */
7459 			/*
7460 			 * We have no device data. Assume no data transfered.
7461 			 */
7462 			sense->es_key = KEY_HARDWARE_ERROR;
7463 			break;
7464 
7465 		case SATA_PKT_DEV_ERROR:
7466 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7467 			    SATA_STATUS_ERR) {
7468 				/*
7469 				 * determine dev error reason from error
7470 				 * reg content
7471 				 */
7472 				sata_decode_device_error(spx, sense);
7473 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7474 					switch (scmd->satacmd_cmd_reg) {
7475 					case SATAC_READ_DMA:
7476 					case SATAC_READ_DMA_EXT:
7477 					case SATAC_READ_DMA_QUEUED:
7478 					case SATAC_READ_DMA_QUEUED_EXT:
7479 					case SATAC_READ_FPDMA_QUEUED:
7480 						/* Unrecovered read error */
7481 						sense->es_add_code =
7482 						    SD_SCSI_ASC_UNREC_READ_ERR;
7483 						break;
7484 					case SATAC_WRITE_DMA:
7485 					case SATAC_WRITE_DMA_EXT:
7486 					case SATAC_WRITE_DMA_QUEUED:
7487 					case SATAC_WRITE_DMA_QUEUED_EXT:
7488 					case SATAC_WRITE_FPDMA_QUEUED:
7489 						/* Write error */
7490 						sense->es_add_code =
7491 						    SD_SCSI_ASC_WRITE_ERR;
7492 						break;
7493 					default:
7494 						/* Internal error */
7495 						SATA_LOG_D((
7496 						    spx->txlt_sata_hba_inst,
7497 						    CE_WARN,
7498 						    "sata_txlt_rw_completion :"
7499 						    "internal error - invalid "
7500 						    "command 0x%2x",
7501 						    scmd->satacmd_cmd_reg));
7502 						break;
7503 					}
7504 				}
7505 				break;
7506 			}
7507 			/* No extended sense key - no info available */
7508 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7509 			break;
7510 
7511 		case SATA_PKT_TIMEOUT:
7512 			scsipkt->pkt_reason = CMD_TIMEOUT;
7513 			scsipkt->pkt_statistics |=
7514 			    STAT_TIMEOUT | STAT_DEV_RESET;
7515 			sense->es_key = KEY_ABORTED_COMMAND;
7516 			break;
7517 
7518 		case SATA_PKT_ABORTED:
7519 			scsipkt->pkt_reason = CMD_ABORTED;
7520 			scsipkt->pkt_statistics |= STAT_ABORTED;
7521 			sense->es_key = KEY_ABORTED_COMMAND;
7522 			break;
7523 
7524 		case SATA_PKT_RESET:
7525 			scsipkt->pkt_reason = CMD_RESET;
7526 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7527 			sense->es_key = KEY_ABORTED_COMMAND;
7528 			break;
7529 
7530 		default:
7531 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7532 			    "sata_txlt_rw_completion: "
7533 			    "invalid packet completion reason"));
7534 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7535 			break;
7536 		}
7537 	}
7538 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7539 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7540 
7541 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7542 		/* scsi callback required */
7543 		scsi_hba_pkt_comp(scsipkt);
7544 }
7545 
7546 
7547 /*
7548  * Translate completion status of non-data commands (i.e. commands returning
7549  * no data).
7550  * pkt completion_reason is checked to determine the completion status.
7551  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7552  *
7553  * Note: this function may be called also for synchronously executed
7554  * commands.
7555  * This function may be used only if scsi_pkt is non-NULL.
7556  */
7557 
7558 static	void
7559 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7560 {
7561 	sata_pkt_txlate_t *spx =
7562 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7563 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7564 
7565 	sata_set_arq_data(sata_pkt);
7566 
7567 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7568 		/* scsi callback required */
7569 		scsi_hba_pkt_comp(scsipkt);
7570 }
7571 
7572 /*
7573  * Completion handler for ATA Pass Through command
7574  */
7575 static void
7576 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7577 {
7578 	sata_pkt_txlate_t *spx =
7579 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7580 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7581 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7582 	struct buf *bp;
7583 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7584 
7585 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7586 		/* Normal completion */
7587 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7588 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7589 		scsipkt->pkt_reason = CMD_CMPLT;
7590 		*scsipkt->pkt_scbp = STATUS_GOOD;
7591 
7592 		/*
7593 		 * If the command has CK_COND set
7594 		 */
7595 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7596 			*scsipkt->pkt_scbp = STATUS_CHECK;
7597 			sata_fill_ata_return_desc(sata_pkt,
7598 			    KEY_RECOVERABLE_ERROR,
7599 			    SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7600 		}
7601 
7602 		if (spx->txlt_tmp_buf != NULL) {
7603 			/* Temporary buffer was used */
7604 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7605 			if (bp->b_flags & B_READ) {
7606 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7607 				    bp->b_bcount);
7608 			}
7609 		}
7610 	} else {
7611 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7612 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7613 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7614 		*scsipkt->pkt_scbp = STATUS_CHECK;
7615 
7616 		/*
7617 		 * If DF or ERR was set, the HBA should have copied out the
7618 		 * status and error registers to the satacmd structure.
7619 		 */
7620 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7621 			sense_key = KEY_HARDWARE_ERROR;
7622 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7623 			addl_sense_qual = 0;
7624 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7625 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7626 				sense_key = KEY_NOT_READY;
7627 				addl_sense_code =
7628 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7629 				addl_sense_qual = 0;
7630 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7631 				sense_key = KEY_MEDIUM_ERROR;
7632 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7633 				addl_sense_qual = 0;
7634 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7635 				sense_key = KEY_DATA_PROTECT;
7636 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7637 				addl_sense_qual = 0;
7638 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7639 				sense_key = KEY_ILLEGAL_REQUEST;
7640 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7641 				addl_sense_qual = 0;
7642 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7643 				sense_key = KEY_ABORTED_COMMAND;
7644 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7645 				addl_sense_qual = 0;
7646 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7647 				sense_key = KEY_UNIT_ATTENTION;
7648 				addl_sense_code =
7649 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7650 				addl_sense_qual = 0;
7651 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7652 				sense_key = KEY_UNIT_ATTENTION;
7653 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7654 				addl_sense_qual = 0;
7655 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7656 				sense_key = KEY_ABORTED_COMMAND;
7657 				addl_sense_code =
7658 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7659 				addl_sense_qual = 0;
7660 			}
7661 		}
7662 
7663 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7664 		    addl_sense_qual);
7665 	}
7666 
7667 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7668 		/* scsi callback required */
7669 		scsi_hba_pkt_comp(scsipkt);
7670 }
7671 
7672 /*
7673  * Completion handler for unmap translation command
7674  */
7675 static void
7676 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7677 {
7678 	sata_pkt_txlate_t *spx =
7679 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7680 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7681 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7682 	struct buf *bp;
7683 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7684 
7685 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7686 		/* Normal completion */
7687 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7688 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7689 		scsipkt->pkt_reason = CMD_CMPLT;
7690 		*scsipkt->pkt_scbp = STATUS_GOOD;
7691 
7692 		if (spx->txlt_tmp_buf != NULL) {
7693 			/* Temporary buffer was used */
7694 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7695 			if (bp->b_flags & B_READ) {
7696 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7697 				    bp->b_bcount);
7698 			}
7699 		}
7700 	} else {
7701 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7702 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7703 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7704 		*scsipkt->pkt_scbp = STATUS_CHECK;
7705 
7706 		/*
7707 		 * If DF or ERR was set, the HBA should have copied out the
7708 		 * status and error registers to the satacmd structure.
7709 		 */
7710 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7711 			sense_key = KEY_HARDWARE_ERROR;
7712 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7713 			addl_sense_qual = 0;
7714 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7715 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7716 				sense_key = KEY_NOT_READY;
7717 				addl_sense_code =
7718 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7719 				addl_sense_qual = 0;
7720 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7721 				sense_key = KEY_MEDIUM_ERROR;
7722 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7723 				addl_sense_qual = 0;
7724 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7725 				sense_key = KEY_DATA_PROTECT;
7726 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7727 				addl_sense_qual = 0;
7728 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7729 				sense_key = KEY_ILLEGAL_REQUEST;
7730 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7731 				addl_sense_qual = 0;
7732 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7733 				sense_key = KEY_ABORTED_COMMAND;
7734 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7735 				addl_sense_qual = 0;
7736 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7737 				sense_key = KEY_UNIT_ATTENTION;
7738 				addl_sense_code =
7739 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7740 				addl_sense_qual = 0;
7741 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7742 				sense_key = KEY_UNIT_ATTENTION;
7743 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7744 				addl_sense_qual = 0;
7745 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7746 				sense_key = KEY_ABORTED_COMMAND;
7747 				addl_sense_code =
7748 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7749 				addl_sense_qual = 0;
7750 			}
7751 		}
7752 
7753 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7754 		    addl_sense_qual);
7755 	}
7756 
7757 	sata_free_local_buffer(spx);
7758 
7759 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7760 		/* scsi callback required */
7761 		scsi_hba_pkt_comp(scsipkt);
7762 }
7763 
7764 /*
7765  *
7766  */
7767 static void
7768 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7769     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7770 {
7771 	sata_pkt_txlate_t *spx =
7772 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7773 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7774 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7775 	struct sata_apt_sense_data *apt_sd =
7776 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7777 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7778 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7779 	    &(apt_sd->apt_sd_sense);
7780 	int extend = 0;
7781 
7782 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7783 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7784 		extend = 1;
7785 
7786 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7787 
7788 	/* update the residual count */
7789 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7790 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7791 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7792 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7793 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7794 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7795 	    sizeof (struct sata_apt_sense_data);
7796 
7797 	/*
7798 	 * Fill in the Descriptor sense header
7799 	 */
7800 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7801 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7802 	sds->ds_class = CLASS_EXTENDED_SENSE;
7803 	sds->ds_key = sense_key & 0xf;
7804 	sds->ds_add_code = addl_sense_code;
7805 	sds->ds_qual_code = addl_sense_qual;
7806 	sds->ds_addl_sense_length =
7807 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7808 
7809 	/*
7810 	 * Fill in the ATA Return descriptor sense data
7811 	 */
7812 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7813 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7814 	ata_ret_desc->ars_addl_length = 0xc;
7815 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7816 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7817 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7818 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7819 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7820 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7821 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7822 
7823 	if (extend == 1) {
7824 		ata_ret_desc->ars_extend = 1;
7825 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7826 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7827 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7828 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7829 	} else {
7830 		ata_ret_desc->ars_extend = 0;
7831 		ata_ret_desc->ars_sec_count_msb = 0;
7832 		ata_ret_desc->ars_lba_low_msb = 0;
7833 		ata_ret_desc->ars_lba_mid_msb = 0;
7834 		ata_ret_desc->ars_lba_high_msb = 0;
7835 	}
7836 }
7837 
7838 static	void
7839 sata_set_arq_data(sata_pkt_t *sata_pkt)
7840 {
7841 	sata_pkt_txlate_t *spx =
7842 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7843 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7844 	struct scsi_extended_sense *sense;
7845 
7846 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7847 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7848 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7849 		/* Normal completion */
7850 		scsipkt->pkt_reason = CMD_CMPLT;
7851 		*scsipkt->pkt_scbp = STATUS_GOOD;
7852 	} else {
7853 		/* Something went wrong */
7854 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7855 		*scsipkt->pkt_scbp = STATUS_CHECK;
7856 		sense = sata_arq_sense(spx);
7857 		switch (sata_pkt->satapkt_reason) {
7858 		case SATA_PKT_PORT_ERROR:
7859 			/*
7860 			 * We have no device data. Assume no data transfered.
7861 			 */
7862 			sense->es_key = KEY_HARDWARE_ERROR;
7863 			break;
7864 
7865 		case SATA_PKT_DEV_ERROR:
7866 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7867 			    SATA_STATUS_ERR) {
7868 				/*
7869 				 * determine dev error reason from error
7870 				 * reg content
7871 				 */
7872 				sata_decode_device_error(spx, sense);
7873 				break;
7874 			}
7875 			/* No extended sense key - no info available */
7876 			break;
7877 
7878 		case SATA_PKT_TIMEOUT:
7879 			scsipkt->pkt_reason = CMD_TIMEOUT;
7880 			scsipkt->pkt_statistics |=
7881 			    STAT_TIMEOUT | STAT_DEV_RESET;
7882 			/* No extended sense key ? */
7883 			break;
7884 
7885 		case SATA_PKT_ABORTED:
7886 			scsipkt->pkt_reason = CMD_ABORTED;
7887 			scsipkt->pkt_statistics |= STAT_ABORTED;
7888 			/* No extended sense key ? */
7889 			break;
7890 
7891 		case SATA_PKT_RESET:
7892 			/* pkt aborted by an explicit reset from a host */
7893 			scsipkt->pkt_reason = CMD_RESET;
7894 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7895 			break;
7896 
7897 		default:
7898 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7899 			    "sata_txlt_nodata_cmd_completion: "
7900 			    "invalid packet completion reason %d",
7901 			    sata_pkt->satapkt_reason));
7902 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7903 			break;
7904 		}
7905 
7906 	}
7907 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7908 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7909 }
7910 
7911 
7912 /*
7913  * Build Mode sense R/W recovery page
7914  * NOT IMPLEMENTED
7915  */
7916 
7917 static int
7918 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7919 {
7920 #ifndef __lock_lint
7921 	_NOTE(ARGUNUSED(sdinfo))
7922 	_NOTE(ARGUNUSED(pcntrl))
7923 	_NOTE(ARGUNUSED(buf))
7924 #endif
7925 	return (0);
7926 }
7927 
7928 /*
7929  * Build Mode sense caching page  -  scsi-3 implementation.
7930  * Page length distinguishes previous format from scsi-3 format.
7931  * buf must have space for 0x12 bytes.
7932  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7933  *
7934  */
7935 static int
7936 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7937 {
7938 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7939 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7940 
7941 	/*
7942 	 * Most of the fields are set to 0, being not supported and/or disabled
7943 	 */
7944 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7945 
7946 	/* Saved paramters not supported */
7947 	if (pcntrl == 3)
7948 		return (0);
7949 	if (pcntrl == 0 || pcntrl == 2) {
7950 		/*
7951 		 * For now treat current and default parameters as same
7952 		 * That may have to change, if target driver will complain
7953 		 */
7954 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
7955 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7956 
7957 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7958 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
7959 			page->dra = 1;		/* Read Ahead disabled */
7960 			page->rcd = 1;		/* Read Cache disabled */
7961 		}
7962 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
7963 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
7964 			page->wce = 1;		/* Write Cache enabled */
7965 	} else {
7966 		/* Changeable parameters */
7967 		page->mode_page.code = MODEPAGE_CACHING;
7968 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7969 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
7970 			page->dra = 1;
7971 			page->rcd = 1;
7972 		}
7973 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
7974 			page->wce = 1;
7975 	}
7976 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7977 	    sizeof (struct mode_page));
7978 }
7979 
7980 /*
7981  * Build Mode sense exception cntrl page
7982  */
7983 static int
7984 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7985 {
7986 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
7987 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7988 
7989 	/*
7990 	 * Most of the fields are set to 0, being not supported and/or disabled
7991 	 */
7992 	bzero(buf, PAGELENGTH_INFO_EXCPT);
7993 
7994 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
7995 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
7996 
7997 	/* Indicate that this is page is saveable */
7998 	page->mode_page.ps = 1;
7999 
8000 	/*
8001 	 * We will return the same data for default, current and saved page.
8002 	 * The only changeable bit is dexcpt and that bit is required
8003 	 * by the ATA specification to be preserved across power cycles.
8004 	 */
8005 	if (pcntrl != 1) {
8006 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8007 		page->mrie = MRIE_ONLY_ON_REQUEST;
8008 	}
8009 	else
8010 		page->dexcpt = 1;	/* Only changeable parameter */
8011 
8012 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8013 }
8014 
8015 
8016 static int
8017 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8018 {
8019 	struct mode_acoustic_management *page =
8020 	    (struct mode_acoustic_management *)buf;
8021 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8022 
8023 	/*
8024 	 * Most of the fields are set to 0, being not supported and/or disabled
8025 	 */
8026 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8027 
8028 	switch (pcntrl) {
8029 	case P_CNTRL_DEFAULT:
8030 		/*  default paramters not supported */
8031 		return (0);
8032 
8033 	case P_CNTRL_CURRENT:
8034 	case P_CNTRL_SAVED:
8035 		/* Saved and current are supported and are identical */
8036 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8037 		page->mode_page.length =
8038 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8039 		page->mode_page.ps = 1;
8040 
8041 		/* Word 83 indicates if feature is supported */
8042 		/* If feature is not supported */
8043 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8044 			page->acoustic_manag_enable =
8045 			    ACOUSTIC_DISABLED;
8046 		} else {
8047 			page->acoustic_manag_enable =
8048 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8049 			    != 0);
8050 			/* Word 94 inidicates the value */
8051 #ifdef	_LITTLE_ENDIAN
8052 			page->acoustic_manag_level =
8053 			    (uchar_t)sata_id->ai_acoustic;
8054 			page->vendor_recommended_value =
8055 			    sata_id->ai_acoustic >> 8;
8056 #else
8057 			page->acoustic_manag_level =
8058 			    sata_id->ai_acoustic >> 8;
8059 			page->vendor_recommended_value =
8060 			    (uchar_t)sata_id->ai_acoustic;
8061 #endif
8062 		}
8063 		break;
8064 
8065 	case P_CNTRL_CHANGEABLE:
8066 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8067 		page->mode_page.length =
8068 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8069 		page->mode_page.ps = 1;
8070 
8071 		/* Word 83 indicates if the feature is supported */
8072 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8073 			page->acoustic_manag_enable =
8074 			    ACOUSTIC_ENABLED;
8075 			page->acoustic_manag_level = 0xff;
8076 		}
8077 		break;
8078 	}
8079 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8080 	    sizeof (struct mode_page));
8081 }
8082 
8083 
8084 /*
8085  * Build Mode sense power condition page.
8086  */
8087 static int
8088 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8089 {
8090 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8091 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8092 
8093 	/*
8094 	 * Most of the fields are set to 0, being not supported and/or disabled
8095 	 * power condition page length was 0x0a
8096 	 */
8097 	bzero(buf, sizeof (struct mode_info_power_cond));
8098 
8099 	if (pcntrl == P_CNTRL_DEFAULT) {
8100 		/*  default paramters not supported */
8101 		return (0);
8102 	}
8103 
8104 	page->mode_page.code = MODEPAGE_POWER_COND;
8105 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8106 
8107 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8108 		page->standby = 1;
8109 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8110 		    sizeof (uchar_t) * 4);
8111 	}
8112 
8113 	return (sizeof (struct mode_info_power_cond));
8114 }
8115 
8116 /*
8117  * Process mode select caching page 8 (scsi3 format only).
8118  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8119  * if these features are supported by the device. If these features are not
8120  * supported, the command will be terminated with STATUS_CHECK.
8121  * This function fails only if the SET FEATURE command sent to
8122  * the device fails. The page format is not verified, assuming that the
8123  * target driver operates correctly - if parameters length is too short,
8124  * we just drop the page.
8125  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8126  * setting have to be changed.
8127  * SET FEATURE command is executed synchronously, i.e. we wait here until
8128  * it is completed, regardless of the scsi pkt directives.
8129  *
8130  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8131  * changing DRA will change RCD.
8132  *
8133  * More than one SATA command may be executed to perform operations specified
8134  * by mode select pages. The first error terminates further execution.
8135  * Operations performed successully are not backed-up in such case.
8136  *
8137  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8138  * If operation resulted in changing device setup, dmod flag should be set to
8139  * one (1). If parameters were not changed, dmod flag should be set to 0.
8140  * Upon return, if operation required sending command to the device, the rval
8141  * should be set to the value returned by sata_hba_start. If operation
8142  * did not require device access, rval should be set to TRAN_ACCEPT.
8143  * The pagelen should be set to the length of the page.
8144  *
8145  * This function has to be called with a port mutex held.
8146  *
8147  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8148  */
8149 int
8150 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8151     int parmlen, int *pagelen, int *rval, int *dmod)
8152 {
8153 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8154 	sata_drive_info_t *sdinfo;
8155 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8156 	sata_id_t *sata_id;
8157 	struct scsi_extended_sense *sense;
8158 	int wce, dra;	/* Current settings */
8159 
8160 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8161 	    &spx->txlt_sata_pkt->satapkt_device);
8162 	sata_id = &sdinfo->satadrv_id;
8163 	*dmod = 0;
8164 
8165 	/* Verify parameters length. If too short, drop it */
8166 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8167 	    sizeof (struct mode_page)) > parmlen) {
8168 		*scsipkt->pkt_scbp = STATUS_CHECK;
8169 		sense = sata_arq_sense(spx);
8170 		sense->es_key = KEY_ILLEGAL_REQUEST;
8171 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8172 		*pagelen = parmlen;
8173 		*rval = TRAN_ACCEPT;
8174 		return (SATA_FAILURE);
8175 	}
8176 
8177 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8178 
8179 	/* Current setting of Read Ahead (and Read Cache) */
8180 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8181 		dra = 0;	/* 0 == not disabled */
8182 	else
8183 		dra = 1;
8184 	/* Current setting of Write Cache */
8185 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8186 		wce = 1;
8187 	else
8188 		wce = 0;
8189 
8190 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8191 		/* nothing to do */
8192 		*rval = TRAN_ACCEPT;
8193 		return (SATA_SUCCESS);
8194 	}
8195 
8196 	/*
8197 	 * Need to flip some setting
8198 	 * Set-up Internal SET FEATURES command(s)
8199 	 */
8200 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8201 	scmd->satacmd_addr_type = 0;
8202 	scmd->satacmd_device_reg = 0;
8203 	scmd->satacmd_status_reg = 0;
8204 	scmd->satacmd_error_reg = 0;
8205 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8206 	if (page->dra != dra || page->rcd != dra) {
8207 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8208 			/* Need to flip read ahead setting */
8209 			if (dra == 0)
8210 				/* Disable read ahead / read cache */
8211 				scmd->satacmd_features_reg =
8212 				    SATAC_SF_DISABLE_READ_AHEAD;
8213 			else
8214 				/* Enable read ahead  / read cache */
8215 				scmd->satacmd_features_reg =
8216 				    SATAC_SF_ENABLE_READ_AHEAD;
8217 
8218 			/* Transfer command to HBA */
8219 			if (sata_hba_start(spx, rval) != 0)
8220 				/*
8221 				 * Pkt not accepted for execution.
8222 				 */
8223 				return (SATA_FAILURE);
8224 
8225 			*dmod = 1;
8226 
8227 			/* Now process return */
8228 			if (spx->txlt_sata_pkt->satapkt_reason !=
8229 			    SATA_PKT_COMPLETED) {
8230 				goto failure;	/* Terminate */
8231 			}
8232 		} else {
8233 			*scsipkt->pkt_scbp = STATUS_CHECK;
8234 			sense = sata_arq_sense(spx);
8235 			sense->es_key = KEY_ILLEGAL_REQUEST;
8236 			sense->es_add_code =
8237 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8238 			*pagelen = parmlen;
8239 			*rval = TRAN_ACCEPT;
8240 			return (SATA_FAILURE);
8241 		}
8242 	}
8243 
8244 	/* Note that the packet is not removed, so it could be re-used */
8245 	if (page->wce != wce) {
8246 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8247 			/* Need to flip Write Cache setting */
8248 			if (page->wce == 1)
8249 				/* Enable write cache */
8250 				scmd->satacmd_features_reg =
8251 				    SATAC_SF_ENABLE_WRITE_CACHE;
8252 			else
8253 				/* Disable write cache */
8254 				scmd->satacmd_features_reg =
8255 				    SATAC_SF_DISABLE_WRITE_CACHE;
8256 
8257 			/* Transfer command to HBA */
8258 			if (sata_hba_start(spx, rval) != 0)
8259 				/*
8260 				 * Pkt not accepted for execution.
8261 				 */
8262 				return (SATA_FAILURE);
8263 
8264 			*dmod = 1;
8265 
8266 			/* Now process return */
8267 			if (spx->txlt_sata_pkt->satapkt_reason !=
8268 			    SATA_PKT_COMPLETED) {
8269 				goto failure;
8270 			}
8271 		} else {
8272 			*scsipkt->pkt_scbp = STATUS_CHECK;
8273 			sense = sata_arq_sense(spx);
8274 			sense->es_key = KEY_ILLEGAL_REQUEST;
8275 			sense->es_add_code =
8276 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8277 			*pagelen = parmlen;
8278 			*rval = TRAN_ACCEPT;
8279 			return (SATA_FAILURE);
8280 		}
8281 	}
8282 	return (SATA_SUCCESS);
8283 
8284 failure:
8285 	sata_xlate_errors(spx);
8286 
8287 	return (SATA_FAILURE);
8288 }
8289 
8290 /*
8291  * Process mode select informational exceptions control page 0x1c
8292  *
8293  * The only changeable bit is dexcpt (disable exceptions).
8294  * MRIE (method of reporting informational exceptions) must be
8295  * "only on request".
8296  * This page applies to informational exceptions that report
8297  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8298  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8299  * Informational exception conditions occur as the result of background scan
8300  * errors, background self-test errors, or vendor specific events within a
8301  * logical unit. An informational exception condition may occur asynchronous
8302  * to any commands.
8303  *
8304  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8305  * If operation resulted in changing device setup, dmod flag should be set to
8306  * one (1). If parameters were not changed, dmod flag should be set to 0.
8307  * Upon return, if operation required sending command to the device, the rval
8308  * should be set to the value returned by sata_hba_start. If operation
8309  * did not require device access, rval should be set to TRAN_ACCEPT.
8310  * The pagelen should be set to the length of the page.
8311  *
8312  * This function has to be called with a port mutex held.
8313  *
8314  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8315  *
8316  * Cannot be called in the interrupt context.
8317  */
8318 static	int
8319 sata_mode_select_page_1c(
8320 	sata_pkt_txlate_t *spx,
8321 	struct mode_info_excpt_page *page,
8322 	int parmlen,
8323 	int *pagelen,
8324 	int *rval,
8325 	int *dmod)
8326 {
8327 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8328 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8329 	sata_drive_info_t *sdinfo;
8330 	sata_id_t *sata_id;
8331 	struct scsi_extended_sense *sense;
8332 
8333 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8334 	    &spx->txlt_sata_pkt->satapkt_device);
8335 	sata_id = &sdinfo->satadrv_id;
8336 
8337 	*dmod = 0;
8338 
8339 	/* Verify parameters length. If too short, drop it */
8340 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8341 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8342 		*scsipkt->pkt_scbp = STATUS_CHECK;
8343 		sense = sata_arq_sense(spx);
8344 		sense->es_key = KEY_ILLEGAL_REQUEST;
8345 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8346 		*pagelen = parmlen;
8347 		*rval = TRAN_ACCEPT;
8348 		return (SATA_FAILURE);
8349 	}
8350 
8351 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8352 
8353 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8354 		*scsipkt->pkt_scbp = STATUS_CHECK;
8355 		sense = sata_arq_sense(spx);
8356 		sense->es_key = KEY_ILLEGAL_REQUEST;
8357 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8358 		*pagelen = parmlen;
8359 		*rval = TRAN_ACCEPT;
8360 		return (SATA_FAILURE);
8361 	}
8362 
8363 	/* If already in the state requested, we are done */
8364 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8365 		/* nothing to do */
8366 		*rval = TRAN_ACCEPT;
8367 		return (SATA_SUCCESS);
8368 	}
8369 
8370 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8371 
8372 	/* Build SMART_ENABLE or SMART_DISABLE command */
8373 	scmd->satacmd_addr_type = 0;		/* N/A */
8374 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8375 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8376 	scmd->satacmd_features_reg = page->dexcpt ?
8377 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8378 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8379 	scmd->satacmd_cmd_reg = SATAC_SMART;
8380 
8381 	/* Transfer command to HBA */
8382 	if (sata_hba_start(spx, rval) != 0)
8383 		/*
8384 		 * Pkt not accepted for execution.
8385 		 */
8386 		return (SATA_FAILURE);
8387 
8388 	*dmod = 1;	/* At least may have been modified */
8389 
8390 	/* Now process return */
8391 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8392 		return (SATA_SUCCESS);
8393 
8394 	/* Packet did not complete successfully */
8395 	sata_xlate_errors(spx);
8396 
8397 	return (SATA_FAILURE);
8398 }
8399 
8400 /*
8401  * Process mode select acoustic management control page 0x30
8402  *
8403  *
8404  * This function has to be called with a port mutex held.
8405  *
8406  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8407  *
8408  * Cannot be called in the interrupt context.
8409  */
8410 int
8411 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8412     mode_acoustic_management *page, int parmlen, int *pagelen,
8413     int *rval, int *dmod)
8414 {
8415 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8416 	sata_drive_info_t *sdinfo;
8417 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8418 	sata_id_t *sata_id;
8419 	struct scsi_extended_sense *sense;
8420 
8421 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8422 	    &spx->txlt_sata_pkt->satapkt_device);
8423 	sata_id = &sdinfo->satadrv_id;
8424 	*dmod = 0;
8425 
8426 	/* If parmlen is too short or the feature is not supported, drop it */
8427 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8428 	    sizeof (struct mode_page)) > parmlen) ||
8429 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8430 		*scsipkt->pkt_scbp = STATUS_CHECK;
8431 		sense = sata_arq_sense(spx);
8432 		sense->es_key = KEY_ILLEGAL_REQUEST;
8433 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8434 		*pagelen = parmlen;
8435 		*rval = TRAN_ACCEPT;
8436 		return (SATA_FAILURE);
8437 	}
8438 
8439 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8440 	    sizeof (struct mode_page);
8441 
8442 	/*
8443 	 * We can enable and disable acoustice management and
8444 	 * set the acoustic management level.
8445 	 */
8446 
8447 	/*
8448 	 * Set-up Internal SET FEATURES command(s)
8449 	 */
8450 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8451 	scmd->satacmd_addr_type = 0;
8452 	scmd->satacmd_device_reg = 0;
8453 	scmd->satacmd_status_reg = 0;
8454 	scmd->satacmd_error_reg = 0;
8455 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8456 	if (page->acoustic_manag_enable) {
8457 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8458 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8459 	} else {	/* disabling acoustic management */
8460 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8461 	}
8462 
8463 	/* Transfer command to HBA */
8464 	if (sata_hba_start(spx, rval) != 0)
8465 		/*
8466 		 * Pkt not accepted for execution.
8467 		 */
8468 		return (SATA_FAILURE);
8469 
8470 	/* Now process return */
8471 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8472 		sata_xlate_errors(spx);
8473 		return (SATA_FAILURE);
8474 	}
8475 
8476 	*dmod = 1;
8477 
8478 	return (SATA_SUCCESS);
8479 }
8480 
8481 /*
8482  * Process mode select power condition page 0x1a
8483  *
8484  * This function has to be called with a port mutex held.
8485  *
8486  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8487  *
8488  * Cannot be called in the interrupt context.
8489  */
8490 int
8491 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8492     mode_info_power_cond *page, int parmlen, int *pagelen,
8493     int *rval, int *dmod)
8494 {
8495 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8496 	sata_drive_info_t *sdinfo;
8497 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8498 	sata_id_t *sata_id;
8499 	struct scsi_extended_sense *sense;
8500 	uint8_t ata_count;
8501 	int i, len;
8502 
8503 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8504 	    &spx->txlt_sata_pkt->satapkt_device);
8505 	sata_id = &sdinfo->satadrv_id;
8506 	*dmod = 0;
8507 
8508 	len = sizeof (struct mode_info_power_cond);
8509 	len += sizeof (struct mode_page);
8510 
8511 	/* If parmlen is too short or the feature is not supported, drop it */
8512 	if ((len < parmlen) || (page->idle == 1) ||
8513 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8514 		*scsipkt->pkt_scbp = STATUS_CHECK;
8515 		sense = sata_arq_sense(spx);
8516 		sense->es_key = KEY_ILLEGAL_REQUEST;
8517 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8518 		*pagelen = parmlen;
8519 		*rval = TRAN_ACCEPT;
8520 		return (SATA_FAILURE);
8521 	}
8522 
8523 	*pagelen = len;
8524 
8525 	/*
8526 	 * Set-up Internal STANDBY command(s)
8527 	 */
8528 	if (page->standby == 0)
8529 		goto out;
8530 
8531 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8532 
8533 	scmd->satacmd_addr_type = 0;
8534 	scmd->satacmd_sec_count_lsb = ata_count;
8535 	scmd->satacmd_lba_low_lsb = 0;
8536 	scmd->satacmd_lba_mid_lsb = 0;
8537 	scmd->satacmd_lba_high_lsb = 0;
8538 	scmd->satacmd_features_reg = 0;
8539 	scmd->satacmd_device_reg = 0;
8540 	scmd->satacmd_status_reg = 0;
8541 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8542 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8543 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8544 
8545 	/* Transfer command to HBA */
8546 	if (sata_hba_start(spx, rval) != 0) {
8547 		return (SATA_FAILURE);
8548 	} else {
8549 		if ((scmd->satacmd_error_reg != 0) ||
8550 		    (spx->txlt_sata_pkt->satapkt_reason !=
8551 		    SATA_PKT_COMPLETED)) {
8552 			sata_xlate_errors(spx);
8553 			return (SATA_FAILURE);
8554 		}
8555 	}
8556 
8557 	for (i = 0; i < 4; i++) {
8558 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8559 	}
8560 out:
8561 	*dmod = 1;
8562 	return (SATA_SUCCESS);
8563 }
8564 
8565 /*
8566  * sata_build_lsense_page0() is used to create the
8567  * SCSI LOG SENSE page 0 (supported log pages)
8568  *
8569  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8570  * (supported log pages, self-test results, informational exceptions
8571  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8572  *
8573  * Takes a sata_drive_info t * and the address of a buffer
8574  * in which to create the page information.
8575  *
8576  * Returns the number of bytes valid in the buffer.
8577  */
8578 static	int
8579 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8580 {
8581 	struct log_parameter *lpp = (struct log_parameter *)buf;
8582 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8583 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8584 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8585 
8586 	lpp->param_code[0] = 0;
8587 	lpp->param_code[1] = 0;
8588 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8589 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8590 
8591 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8592 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8593 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8594 			++num_pages_supported;
8595 		}
8596 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8597 		++num_pages_supported;
8598 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8599 		++num_pages_supported;
8600 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8601 		++num_pages_supported;
8602 	}
8603 
8604 	lpp->param_len = num_pages_supported;
8605 
8606 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8607 	    num_pages_supported);
8608 }
8609 
8610 /*
8611  * sata_build_lsense_page_10() is used to create the
8612  * SCSI LOG SENSE page 0x10 (self-test results)
8613  *
8614  * Takes a sata_drive_info t * and the address of a buffer
8615  * in which to create the page information as well as a sata_hba_inst_t *.
8616  *
8617  * Returns the number of bytes valid in the buffer.
8618  *
8619  * Note: Self test and SMART data is accessible in device log pages.
8620  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8621  * of data can be transferred by a single command), or by the General Purpose
8622  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8623  * - approximately 33MB - can be transferred by a single command.
8624  * The SCT Command response (either error or command) is the same for both
8625  * the SMART and GPL methods of issuing commands.
8626  * This function uses READ LOG EXT command when drive supports LBA48, and
8627  * SMART READ command otherwise.
8628  *
8629  * Since above commands are executed in a synchronous mode, this function
8630  * should not be called in an interrupt context.
8631  */
8632 static	int
8633 sata_build_lsense_page_10(
8634 	sata_drive_info_t *sdinfo,
8635 	uint8_t *buf,
8636 	sata_hba_inst_t *sata_hba_inst)
8637 {
8638 	struct log_parameter *lpp = (struct log_parameter *)buf;
8639 	int rval;
8640 
8641 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8642 		struct smart_ext_selftest_log *ext_selftest_log;
8643 
8644 		ext_selftest_log = kmem_zalloc(
8645 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8646 
8647 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8648 		    ext_selftest_log, 0);
8649 		if (rval == 0) {
8650 			int index, start_index;
8651 			struct smart_ext_selftest_log_entry *entry;
8652 			static const struct smart_ext_selftest_log_entry empty =
8653 			    {0};
8654 			uint16_t block_num;
8655 			int count;
8656 			boolean_t only_one_block = B_FALSE;
8657 
8658 			index = ext_selftest_log->
8659 			    smart_ext_selftest_log_index[0];
8660 			index |= ext_selftest_log->
8661 			    smart_ext_selftest_log_index[1] << 8;
8662 			if (index == 0)
8663 				goto out;
8664 
8665 			--index;	/* Correct for 0 origin */
8666 			start_index = index;	/* remember where we started */
8667 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8668 			if (block_num != 0) {
8669 				rval = sata_ext_smart_selftest_read_log(
8670 				    sata_hba_inst, sdinfo, ext_selftest_log,
8671 				    block_num);
8672 				if (rval != 0)
8673 					goto out;
8674 			}
8675 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8676 			entry =
8677 			    &ext_selftest_log->
8678 			    smart_ext_selftest_log_entries[index];
8679 
8680 			for (count = 1;
8681 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8682 			    ++count) {
8683 				uint8_t status;
8684 				uint8_t code;
8685 				uint8_t sense_key;
8686 				uint8_t add_sense_code;
8687 				uint8_t add_sense_code_qual;
8688 
8689 				/* If this is an unused entry, we are done */
8690 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8691 					/* Broken firmware on some disks */
8692 					if (index + 1 ==
8693 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8694 						--entry;
8695 						--index;
8696 						if (bcmp(entry, &empty,
8697 						    sizeof (empty)) == 0)
8698 							goto out;
8699 					} else
8700 						goto out;
8701 				}
8702 
8703 				if (only_one_block &&
8704 				    start_index == index)
8705 					goto out;
8706 
8707 				lpp->param_code[0] = 0;
8708 				lpp->param_code[1] = count;
8709 				lpp->param_ctrl_flags =
8710 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8711 				lpp->param_len =
8712 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8713 
8714 				status = entry->smart_ext_selftest_log_status;
8715 				status >>= 4;
8716 				switch (status) {
8717 				case 0:
8718 				default:
8719 					sense_key = KEY_NO_SENSE;
8720 					add_sense_code =
8721 					    SD_SCSI_ASC_NO_ADD_SENSE;
8722 					add_sense_code_qual = 0;
8723 					break;
8724 				case 1:
8725 					sense_key = KEY_ABORTED_COMMAND;
8726 					add_sense_code =
8727 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8728 					add_sense_code_qual = SCSI_COMPONENT_81;
8729 					break;
8730 				case 2:
8731 					sense_key = KEY_ABORTED_COMMAND;
8732 					add_sense_code =
8733 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8734 					add_sense_code_qual = SCSI_COMPONENT_82;
8735 					break;
8736 				case 3:
8737 					sense_key = KEY_ABORTED_COMMAND;
8738 					add_sense_code =
8739 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8740 					add_sense_code_qual = SCSI_COMPONENT_83;
8741 					break;
8742 				case 4:
8743 					sense_key = KEY_HARDWARE_ERROR;
8744 					add_sense_code =
8745 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8746 					add_sense_code_qual = SCSI_COMPONENT_84;
8747 					break;
8748 				case 5:
8749 					sense_key = KEY_HARDWARE_ERROR;
8750 					add_sense_code =
8751 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8752 					add_sense_code_qual = SCSI_COMPONENT_85;
8753 					break;
8754 				case 6:
8755 					sense_key = KEY_HARDWARE_ERROR;
8756 					add_sense_code =
8757 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8758 					add_sense_code_qual = SCSI_COMPONENT_86;
8759 					break;
8760 				case 7:
8761 					sense_key = KEY_MEDIUM_ERROR;
8762 					add_sense_code =
8763 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8764 					add_sense_code_qual = SCSI_COMPONENT_87;
8765 					break;
8766 				case 8:
8767 					sense_key = KEY_HARDWARE_ERROR;
8768 					add_sense_code =
8769 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8770 					add_sense_code_qual = SCSI_COMPONENT_88;
8771 					break;
8772 				}
8773 				code = 0;	/* unspecified */
8774 				status |= (code << 4);
8775 				lpp->param_values[0] = status;
8776 				lpp->param_values[1] = 0; /* unspecified */
8777 				lpp->param_values[2] = entry->
8778 				    smart_ext_selftest_log_timestamp[1];
8779 				lpp->param_values[3] = entry->
8780 				    smart_ext_selftest_log_timestamp[0];
8781 				if (status != 0) {
8782 					lpp->param_values[4] = 0;
8783 					lpp->param_values[5] = 0;
8784 					lpp->param_values[6] = entry->
8785 					    smart_ext_selftest_log_failing_lba
8786 					    [5];
8787 					lpp->param_values[7] = entry->
8788 					    smart_ext_selftest_log_failing_lba
8789 					    [4];
8790 					lpp->param_values[8] = entry->
8791 					    smart_ext_selftest_log_failing_lba
8792 					    [3];
8793 					lpp->param_values[9] = entry->
8794 					    smart_ext_selftest_log_failing_lba
8795 					    [2];
8796 					lpp->param_values[10] = entry->
8797 					    smart_ext_selftest_log_failing_lba
8798 					    [1];
8799 					lpp->param_values[11] = entry->
8800 					    smart_ext_selftest_log_failing_lba
8801 					    [0];
8802 				} else {	/* No bad block address */
8803 					lpp->param_values[4] = 0xff;
8804 					lpp->param_values[5] = 0xff;
8805 					lpp->param_values[6] = 0xff;
8806 					lpp->param_values[7] = 0xff;
8807 					lpp->param_values[8] = 0xff;
8808 					lpp->param_values[9] = 0xff;
8809 					lpp->param_values[10] = 0xff;
8810 					lpp->param_values[11] = 0xff;
8811 				}
8812 
8813 				lpp->param_values[12] = sense_key;
8814 				lpp->param_values[13] = add_sense_code;
8815 				lpp->param_values[14] = add_sense_code_qual;
8816 				lpp->param_values[15] = 0; /* undefined */
8817 
8818 				lpp = (struct log_parameter *)
8819 				    (((uint8_t *)lpp) +
8820 				    SCSI_LOG_PARAM_HDR_LEN +
8821 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8822 
8823 				--index;	/* Back up to previous entry */
8824 				if (index < 0) {
8825 					if (block_num > 0) {
8826 						--block_num;
8827 					} else {
8828 						struct read_log_ext_directory
8829 						    logdir;
8830 
8831 						rval =
8832 						    sata_read_log_ext_directory(
8833 						    sata_hba_inst, sdinfo,
8834 						    &logdir);
8835 						if (rval == -1)
8836 							goto out;
8837 						if ((logdir.read_log_ext_vers
8838 						    [0] == 0) &&
8839 						    (logdir.read_log_ext_vers
8840 						    [1] == 0))
8841 							goto out;
8842 						block_num =
8843 						    logdir.read_log_ext_nblks
8844 						    [EXT_SMART_SELFTEST_LOG_PAGE
8845 						    - 1][0];
8846 						block_num |= logdir.
8847 						    read_log_ext_nblks
8848 						    [EXT_SMART_SELFTEST_LOG_PAGE
8849 						    - 1][1] << 8;
8850 						--block_num;
8851 						only_one_block =
8852 						    (block_num == 0);
8853 					}
8854 					rval = sata_ext_smart_selftest_read_log(
8855 					    sata_hba_inst, sdinfo,
8856 					    ext_selftest_log, block_num);
8857 					if (rval != 0)
8858 						goto out;
8859 
8860 					index =
8861 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8862 					    1;
8863 				}
8864 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8865 				entry = &ext_selftest_log->
8866 				    smart_ext_selftest_log_entries[index];
8867 			}
8868 		}
8869 out:
8870 		kmem_free(ext_selftest_log,
8871 		    sizeof (struct smart_ext_selftest_log));
8872 	} else {
8873 		struct smart_selftest_log *selftest_log;
8874 
8875 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8876 		    KM_SLEEP);
8877 
8878 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8879 		    selftest_log);
8880 
8881 		if (rval == 0) {
8882 			int index;
8883 			int count;
8884 			struct smart_selftest_log_entry *entry;
8885 			static const struct smart_selftest_log_entry empty =
8886 			    { 0 };
8887 
8888 			index = selftest_log->smart_selftest_log_index;
8889 			if (index == 0)
8890 				goto done;
8891 			--index;	/* Correct for 0 origin */
8892 			entry = &selftest_log->
8893 			    smart_selftest_log_entries[index];
8894 			for (count = 1;
8895 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8896 			    ++count) {
8897 				uint8_t status;
8898 				uint8_t code;
8899 				uint8_t sense_key;
8900 				uint8_t add_sense_code;
8901 				uint8_t add_sense_code_qual;
8902 
8903 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8904 					goto done;
8905 
8906 				lpp->param_code[0] = 0;
8907 				lpp->param_code[1] = count;
8908 				lpp->param_ctrl_flags =
8909 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8910 				lpp->param_len =
8911 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8912 
8913 				status = entry->smart_selftest_log_status;
8914 				status >>= 4;
8915 				switch (status) {
8916 				case 0:
8917 				default:
8918 					sense_key = KEY_NO_SENSE;
8919 					add_sense_code =
8920 					    SD_SCSI_ASC_NO_ADD_SENSE;
8921 					break;
8922 				case 1:
8923 					sense_key = KEY_ABORTED_COMMAND;
8924 					add_sense_code =
8925 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8926 					add_sense_code_qual = SCSI_COMPONENT_81;
8927 					break;
8928 				case 2:
8929 					sense_key = KEY_ABORTED_COMMAND;
8930 					add_sense_code =
8931 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8932 					add_sense_code_qual = SCSI_COMPONENT_82;
8933 					break;
8934 				case 3:
8935 					sense_key = KEY_ABORTED_COMMAND;
8936 					add_sense_code =
8937 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8938 					add_sense_code_qual = SCSI_COMPONENT_83;
8939 					break;
8940 				case 4:
8941 					sense_key = KEY_HARDWARE_ERROR;
8942 					add_sense_code =
8943 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8944 					add_sense_code_qual = SCSI_COMPONENT_84;
8945 					break;
8946 				case 5:
8947 					sense_key = KEY_HARDWARE_ERROR;
8948 					add_sense_code =
8949 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8950 					add_sense_code_qual = SCSI_COMPONENT_85;
8951 					break;
8952 				case 6:
8953 					sense_key = KEY_HARDWARE_ERROR;
8954 					add_sense_code =
8955 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8956 					add_sense_code_qual = SCSI_COMPONENT_86;
8957 					break;
8958 				case 7:
8959 					sense_key = KEY_MEDIUM_ERROR;
8960 					add_sense_code =
8961 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8962 					add_sense_code_qual = SCSI_COMPONENT_87;
8963 					break;
8964 				case 8:
8965 					sense_key = KEY_HARDWARE_ERROR;
8966 					add_sense_code =
8967 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8968 					add_sense_code_qual = SCSI_COMPONENT_88;
8969 					break;
8970 				}
8971 				code = 0;	/* unspecified */
8972 				status |= (code << 4);
8973 				lpp->param_values[0] = status;
8974 				lpp->param_values[1] = 0; /* unspecified */
8975 				lpp->param_values[2] = entry->
8976 				    smart_selftest_log_timestamp[1];
8977 				lpp->param_values[3] = entry->
8978 				    smart_selftest_log_timestamp[0];
8979 				if (status != 0) {
8980 					lpp->param_values[4] = 0;
8981 					lpp->param_values[5] = 0;
8982 					lpp->param_values[6] = 0;
8983 					lpp->param_values[7] = 0;
8984 					lpp->param_values[8] = entry->
8985 					    smart_selftest_log_failing_lba[3];
8986 					lpp->param_values[9] = entry->
8987 					    smart_selftest_log_failing_lba[2];
8988 					lpp->param_values[10] = entry->
8989 					    smart_selftest_log_failing_lba[1];
8990 					lpp->param_values[11] = entry->
8991 					    smart_selftest_log_failing_lba[0];
8992 				} else {	/* No block address */
8993 					lpp->param_values[4] = 0xff;
8994 					lpp->param_values[5] = 0xff;
8995 					lpp->param_values[6] = 0xff;
8996 					lpp->param_values[7] = 0xff;
8997 					lpp->param_values[8] = 0xff;
8998 					lpp->param_values[9] = 0xff;
8999 					lpp->param_values[10] = 0xff;
9000 					lpp->param_values[11] = 0xff;
9001 				}
9002 				lpp->param_values[12] = sense_key;
9003 				lpp->param_values[13] = add_sense_code;
9004 				lpp->param_values[14] = add_sense_code_qual;
9005 				lpp->param_values[15] = 0; /* undefined */
9006 
9007 				lpp = (struct log_parameter *)
9008 				    (((uint8_t *)lpp) +
9009 				    SCSI_LOG_PARAM_HDR_LEN +
9010 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9011 				--index;	/* back up to previous entry */
9012 				if (index < 0) {
9013 					index =
9014 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9015 				}
9016 				entry = &selftest_log->
9017 				    smart_selftest_log_entries[index];
9018 			}
9019 		}
9020 done:
9021 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9022 	}
9023 
9024 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9025 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9026 }
9027 
9028 /*
9029  * sata_build_lsense_page_2f() is used to create the
9030  * SCSI LOG SENSE page 0x2f (informational exceptions)
9031  *
9032  * Takes a sata_drive_info t * and the address of a buffer
9033  * in which to create the page information as well as a sata_hba_inst_t *.
9034  *
9035  * Returns the number of bytes valid in the buffer.
9036  *
9037  * Because it invokes function(s) that send synchronously executed command
9038  * to the HBA, it cannot be called in the interrupt context.
9039  */
9040 static	int
9041 sata_build_lsense_page_2f(
9042 	sata_drive_info_t *sdinfo,
9043 	uint8_t *buf,
9044 	sata_hba_inst_t *sata_hba_inst)
9045 {
9046 	struct log_parameter *lpp = (struct log_parameter *)buf;
9047 	int rval;
9048 	uint8_t *smart_data;
9049 	uint8_t temp;
9050 	sata_id_t *sata_id;
9051 #define	SMART_NO_TEMP	0xff
9052 
9053 	lpp->param_code[0] = 0;
9054 	lpp->param_code[1] = 0;
9055 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9056 
9057 	/* Now get the SMART status w.r.t. threshold exceeded */
9058 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9059 	switch (rval) {
9060 	case 1:
9061 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9062 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9063 		break;
9064 	case 0:
9065 	case -1:	/* failed to get data */
9066 		lpp->param_values[0] = 0;	/* No failure predicted */
9067 		lpp->param_values[1] = 0;
9068 		break;
9069 #if defined(SATA_DEBUG)
9070 	default:
9071 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9072 		/* NOTREACHED */
9073 #endif
9074 	}
9075 
9076 	sata_id = &sdinfo->satadrv_id;
9077 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9078 		temp = SMART_NO_TEMP;
9079 	else {
9080 		/* Now get the temperature */
9081 		smart_data = kmem_zalloc(512, KM_SLEEP);
9082 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9083 		    SCT_STATUS_LOG_PAGE, 1);
9084 		if (rval == -1)
9085 			temp = SMART_NO_TEMP;
9086 		else {
9087 			temp = smart_data[200];
9088 			if (temp & 0x80) {
9089 				if (temp & 0x7f)
9090 					temp = 0;
9091 				else
9092 					temp = SMART_NO_TEMP;
9093 			}
9094 		}
9095 		kmem_free(smart_data, 512);
9096 	}
9097 
9098 	lpp->param_values[2] = temp;	/* most recent temperature */
9099 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9100 
9101 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9102 
9103 
9104 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9105 }
9106 
9107 /*
9108  * sata_build_lsense_page_30() is used to create the
9109  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9110  *
9111  * Takes a sata_drive_info t * and the address of a buffer
9112  * in which to create the page information as well as a sata_hba_inst_t *.
9113  *
9114  * Returns the number of bytes valid in the buffer.
9115  */
9116 static int
9117 sata_build_lsense_page_30(
9118 	sata_drive_info_t *sdinfo,
9119 	uint8_t *buf,
9120 	sata_hba_inst_t *sata_hba_inst)
9121 {
9122 	struct smart_data *smart_data = (struct smart_data *)buf;
9123 	int rval;
9124 
9125 	/* Now do the SMART READ DATA */
9126 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9127 	if (rval == -1)
9128 		return (0);
9129 
9130 	return (sizeof (struct smart_data));
9131 }
9132 
9133 /*
9134  * sata_build_lsense_page_0e() is used to create the
9135  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9136  *
9137  * Date of Manufacture (0x0001)
9138  *	YEAR = "0000"
9139  *	WEEK = "00"
9140  * Accounting Date (0x0002)
9141  *	6 ASCII space character(20h)
9142  * Specified cycle count over device lifetime
9143  *	VALUE - THRESH - the delta between max and min;
9144  * Accumulated start-stop cycles
9145  *	VALUE - WORST - the accumulated cycles;
9146  *
9147  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9148  *
9149  * Takes a sata_drive_info t * and the address of a buffer
9150  * in which to create the page information as well as a sata_hba_inst_t *.
9151  *
9152  * Returns the number of bytes valid in the buffer.
9153  */
9154 static	int
9155 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9156 	sata_pkt_txlate_t *spx)
9157 {
9158 	struct start_stop_cycle_counter_log *log_page;
9159 	int i, rval, index;
9160 	uint8_t smart_data[512], id, value, worst, thresh;
9161 	uint32_t max_count, cycles;
9162 
9163 	/* Now do the SMART READ DATA */
9164 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9165 	    (struct smart_data *)smart_data);
9166 	if (rval == -1)
9167 		return (0);
9168 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9169 		index = (i * 12) + 2;
9170 		id = smart_data[index];
9171 		if (id != SMART_START_STOP_COUNT_ID)
9172 			continue;
9173 		else {
9174 			thresh = smart_data[index + 2];
9175 			value = smart_data[index + 3];
9176 			worst = smart_data[index + 4];
9177 			break;
9178 		}
9179 	}
9180 	if (id != SMART_START_STOP_COUNT_ID)
9181 		return (0);
9182 	max_count = value - thresh;
9183 	cycles = value - worst;
9184 
9185 	log_page = (struct start_stop_cycle_counter_log *)buf;
9186 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9187 	log_page->code = 0x0e;
9188 	log_page->page_len_low = 0x24;
9189 
9190 	log_page->manufactor_date_low = 0x1;
9191 	log_page->param_1.fmt_link = 0x1; /* 01b */
9192 	log_page->param_len_1 = 0x06;
9193 	for (i = 0; i < 4; i++) {
9194 		log_page->year_manu[i] = 0x30;
9195 		if (i < 2)
9196 			log_page->week_manu[i] = 0x30;
9197 	}
9198 
9199 	log_page->account_date_low = 0x02;
9200 	log_page->param_2.fmt_link = 0x01; /* 01b */
9201 	log_page->param_len_2 = 0x06;
9202 	for (i = 0; i < 4; i++) {
9203 		log_page->year_account[i] = 0x20;
9204 		if (i < 2)
9205 			log_page->week_account[i] = 0x20;
9206 	}
9207 
9208 	log_page->lifetime_code_low = 0x03;
9209 	log_page->param_3.fmt_link = 0x03; /* 11b */
9210 	log_page->param_len_3 = 0x04;
9211 	/* VALUE - THRESH - the delta between max and min */
9212 	log_page->cycle_code_low = 0x04;
9213 	log_page->param_4.fmt_link = 0x03; /* 11b */
9214 	log_page->param_len_4 = 0x04;
9215 	/* WORST - THRESH - the distance from 'now' to min */
9216 
9217 	for (i = 0; i < 4; i++) {
9218 		log_page->cycle_lifetime[i] =
9219 		    (max_count >> (8 * (3 - i))) & 0xff;
9220 		log_page->cycle_accumulated[i] =
9221 		    (cycles >> (8 * (3 - i))) & 0xff;
9222 	}
9223 
9224 	return (sizeof (struct start_stop_cycle_counter_log));
9225 }
9226 
9227 /*
9228  * This function was used for build a ATA read verify sector command
9229  */
9230 static void
9231 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9232 {
9233 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9234 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9235 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9236 
9237 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9238 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9239 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9240 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9241 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9242 	scmd->satacmd_features_reg = 0;
9243 	scmd->satacmd_status_reg = 0;
9244 	scmd->satacmd_error_reg = 0;
9245 }
9246 
9247 /*
9248  * This function was used for building an ATA
9249  * command, and only command register need to
9250  * be defined, other register will be zero or na.
9251  */
9252 static void
9253 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9254 {
9255 	scmd->satacmd_addr_type = 0;
9256 	scmd->satacmd_cmd_reg = cmd;
9257 	scmd->satacmd_device_reg = 0;
9258 	scmd->satacmd_sec_count_lsb = 0;
9259 	scmd->satacmd_lba_low_lsb = 0;
9260 	scmd->satacmd_lba_mid_lsb = 0;
9261 	scmd->satacmd_lba_high_lsb = 0;
9262 	scmd->satacmd_features_reg = 0;
9263 	scmd->satacmd_status_reg = 0;
9264 	scmd->satacmd_error_reg = 0;
9265 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9266 }
9267 
9268 /*
9269  * This function was used for changing the standby
9270  * timer format from SCSI to ATA.
9271  */
9272 static uint8_t
9273 sata_get_standby_timer(uint8_t *timer)
9274 {
9275 	uint32_t i = 0, count = 0;
9276 	uint8_t ata_count;
9277 
9278 	for (i = 0; i < 4; i++) {
9279 		count = count << 8 | timer[i];
9280 	}
9281 
9282 	if (count == 0)
9283 		return (0);
9284 
9285 	if (count >= 1 && count <= 12000)
9286 		ata_count = (count -1) / 50 + 1;
9287 	else if (count > 12000 && count <= 12600)
9288 		ata_count = 0xfc;
9289 	else if (count > 12601 && count <= 12750)
9290 		ata_count = 0xff;
9291 	else if (count > 12750 && count <= 17999)
9292 		ata_count = 0xf1;
9293 	else if (count > 18000 && count <= 198000)
9294 		ata_count = count / 18000 + 240;
9295 	else
9296 		ata_count = 0xfd;
9297 	return (ata_count);
9298 }
9299 
9300 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9301 
9302 /*
9303  * Start command for ATAPI device.
9304  * This function processes scsi_pkt requests.
9305  * Now CD/DVD, tape and ATAPI disk devices are supported.
9306  * Most commands are packet without any translation into Packet Command.
9307  * Some may be trapped and executed as SATA commands (not clear which one).
9308  *
9309  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9310  * execution).
9311  * Returns other TRAN_XXXX codes if command is not accepted or completed
9312  * (see return values for sata_hba_start()).
9313  *
9314  * Note:
9315  * Inquiry cdb format differs between transport version 2 and 3.
9316  * However, the transport version 3 devices that were checked did not adhere
9317  * to the specification (ignored MSB of the allocation length). Therefore,
9318  * the transport version is not checked, but Inquiry allocation length is
9319  * truncated to 255 bytes if the original allocation length set-up by the
9320  * target driver is greater than 255 bytes.
9321  */
9322 static int
9323 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9324 {
9325 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9326 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9327 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9328 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9329 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9330 	    &spx->txlt_sata_pkt->satapkt_device);
9331 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9332 	int cdblen;
9333 	int rval, reason;
9334 	int synch;
9335 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9336 
9337 	mutex_enter(cport_mutex);
9338 
9339 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9340 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9341 		mutex_exit(cport_mutex);
9342 		return (rval);
9343 	}
9344 
9345 	/*
9346 	 * ATAPI device executes some ATA commands in addition to those
9347 	 * commands sent via PACKET command. These ATA commands may be
9348 	 * executed by the regular SATA translation functions. None needs
9349 	 * to be captured now.
9350 	 *
9351 	 * Commands sent via PACKET command include:
9352 	 *	MMC command set for ATAPI CD/DVD device
9353 	 *	SSC command set for ATAPI TAPE device
9354 	 *	SBC command set for ATAPI disk device
9355 	 *
9356 	 */
9357 
9358 	/* Check the size of cdb */
9359 
9360 	switch (GETGROUP(cdbp)) {
9361 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9362 		/*
9363 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9364 		 * therefore require special handling.  Return failure, for now.
9365 		 */
9366 		mutex_exit(cport_mutex);
9367 		return (TRAN_BADPKT);
9368 
9369 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9370 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9371 		/* obtain length from the scsi_pkt */
9372 		cdblen = scsipkt->pkt_cdblen;
9373 		break;
9374 
9375 	default:
9376 		/* CDB's length is statically known, per SPC-4 */
9377 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9378 		break;
9379 	}
9380 
9381 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9382 		sata_log(NULL, CE_WARN,
9383 		    "sata: invalid ATAPI cdb length %d",
9384 		    cdblen);
9385 		mutex_exit(cport_mutex);
9386 		return (TRAN_BADPKT);
9387 	}
9388 
9389 	SATAATAPITRACE(spx, cdblen);
9390 
9391 	/*
9392 	 * For non-read/write commands we need to
9393 	 * map buffer
9394 	 */
9395 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9396 	case SCMD_READ:
9397 	case SCMD_READ_G1:
9398 	case SCMD_READ_G5:
9399 	case SCMD_READ_G4:
9400 	case SCMD_WRITE:
9401 	case SCMD_WRITE_G1:
9402 	case SCMD_WRITE_G5:
9403 	case SCMD_WRITE_G4:
9404 		break;
9405 	default:
9406 		if (bp != NULL) {
9407 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9408 				bp_mapin(bp);
9409 		}
9410 		break;
9411 	}
9412 	/*
9413 	 * scmd->satacmd_flags.sata_data_direction default -
9414 	 * SATA_DIR_NODATA_XFER - is set by
9415 	 * sata_txlt_generic_pkt_info().
9416 	 */
9417 	if (scmd->satacmd_bp) {
9418 		if (scmd->satacmd_bp->b_flags & B_READ) {
9419 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9420 		} else {
9421 			scmd->satacmd_flags.sata_data_direction =
9422 			    SATA_DIR_WRITE;
9423 		}
9424 	}
9425 
9426 	/*
9427 	 * Set up ATAPI packet command.
9428 	 */
9429 
9430 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9431 
9432 	/* Copy cdb into sata_cmd */
9433 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9434 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9435 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9436 
9437 	/* See note in the command header */
9438 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9439 		if (scmd->satacmd_acdb[3] != 0)
9440 			scmd->satacmd_acdb[4] = 255;
9441 	}
9442 
9443 #ifdef SATA_DEBUG
9444 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9445 		uint8_t *p = scmd->satacmd_acdb;
9446 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9447 
9448 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9449 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9450 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9451 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9452 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9453 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9454 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9455 	}
9456 #endif
9457 
9458 	/*
9459 	 * Preset request sense data to NO SENSE.
9460 	 * If there is no way to get error information via Request Sense,
9461 	 * the packet request sense data would not have to be modified by HBA,
9462 	 * but it could be returned as is.
9463 	 */
9464 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9465 	sata_fixed_sense_data_preset(
9466 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9467 
9468 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9469 		/* Need callback function */
9470 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9471 		synch = FALSE;
9472 	} else
9473 		synch = TRUE;
9474 
9475 	/* Transfer command to HBA */
9476 	if (sata_hba_start(spx, &rval) != 0) {
9477 		/* Pkt not accepted for execution */
9478 		mutex_exit(cport_mutex);
9479 		return (rval);
9480 	}
9481 	mutex_exit(cport_mutex);
9482 	/*
9483 	 * If execution is non-synchronous,
9484 	 * a callback function will handle potential errors, translate
9485 	 * the response and will do a callback to a target driver.
9486 	 * If it was synchronous, use the same framework callback to check
9487 	 * an execution status.
9488 	 */
9489 	if (synch) {
9490 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9491 		    "synchronous execution status %x\n",
9492 		    spx->txlt_sata_pkt->satapkt_reason);
9493 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9494 	}
9495 	return (TRAN_ACCEPT);
9496 }
9497 
9498 
9499 /*
9500  * ATAPI Packet command completion.
9501  *
9502  * Failure of the command passed via Packet command are considered device
9503  * error. SATA HBA driver would have to retrieve error data (via Request
9504  * Sense command delivered via error retrieval sata packet) and copy it
9505  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9506  */
9507 static void
9508 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9509 {
9510 	sata_pkt_txlate_t *spx =
9511 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9512 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9513 	struct scsi_extended_sense *sense;
9514 	struct buf *bp;
9515 	int rval;
9516 
9517 #ifdef SATA_DEBUG
9518 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9519 #endif
9520 
9521 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9522 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9523 
9524 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9525 		/* Normal completion */
9526 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9527 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9528 		scsipkt->pkt_reason = CMD_CMPLT;
9529 		*scsipkt->pkt_scbp = STATUS_GOOD;
9530 		if (spx->txlt_tmp_buf != NULL) {
9531 			/* Temporary buffer was used */
9532 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9533 			if (bp->b_flags & B_READ) {
9534 				rval = ddi_dma_sync(
9535 				    spx->txlt_buf_dma_handle, 0, 0,
9536 				    DDI_DMA_SYNC_FORCPU);
9537 				ASSERT(rval == DDI_SUCCESS);
9538 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9539 				    bp->b_bcount);
9540 			}
9541 		}
9542 	} else {
9543 		/*
9544 		 * Something went wrong - analyze return
9545 		 */
9546 		*scsipkt->pkt_scbp = STATUS_CHECK;
9547 		sense = sata_arq_sense(spx);
9548 
9549 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9550 			/*
9551 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9552 			 * Under this condition ERR bit is set for ATA command,
9553 			 * and CHK bit set for ATAPI command.
9554 			 *
9555 			 * Please check st_intr & sdintr about how pkt_reason
9556 			 * is used.
9557 			 */
9558 			scsipkt->pkt_reason = CMD_CMPLT;
9559 
9560 			/*
9561 			 * We may not have ARQ data if there was a double
9562 			 * error. But sense data in sata packet was pre-set
9563 			 * with NO SENSE so it is valid even if HBA could
9564 			 * not retrieve a real sense data.
9565 			 * Just copy this sense data into scsi pkt sense area.
9566 			 */
9567 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9568 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9569 #ifdef SATA_DEBUG
9570 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9571 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9572 				    "sata_txlt_atapi_completion: %02x\n"
9573 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9574 				    "          %02x %02x %02x %02x %02x %02x "
9575 				    "          %02x %02x %02x %02x %02x %02x\n",
9576 				    scsipkt->pkt_reason,
9577 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9578 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9579 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9580 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9581 				    rqsp[16], rqsp[17]);
9582 			}
9583 #endif
9584 		} else {
9585 			switch (sata_pkt->satapkt_reason) {
9586 			case SATA_PKT_PORT_ERROR:
9587 				/*
9588 				 * We have no device data.
9589 				 */
9590 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9591 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9592 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9593 				    STATE_GOT_STATUS);
9594 				sense->es_key = KEY_HARDWARE_ERROR;
9595 				break;
9596 
9597 			case SATA_PKT_TIMEOUT:
9598 				scsipkt->pkt_reason = CMD_TIMEOUT;
9599 				scsipkt->pkt_statistics |=
9600 				    STAT_TIMEOUT | STAT_DEV_RESET;
9601 				/*
9602 				 * Need to check if HARDWARE_ERROR/
9603 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9604 				 * appropriate.
9605 				 */
9606 				break;
9607 
9608 			case SATA_PKT_ABORTED:
9609 				scsipkt->pkt_reason = CMD_ABORTED;
9610 				scsipkt->pkt_statistics |= STAT_ABORTED;
9611 				/* Should we set key COMMAND_ABPRTED? */
9612 				break;
9613 
9614 			case SATA_PKT_RESET:
9615 				scsipkt->pkt_reason = CMD_RESET;
9616 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9617 				/*
9618 				 * May be we should set Unit Attention /
9619 				 * Reset. Perhaps the same should be
9620 				 * returned for disks....
9621 				 */
9622 				sense->es_key = KEY_UNIT_ATTENTION;
9623 				sense->es_add_code = SD_SCSI_ASC_RESET;
9624 				break;
9625 
9626 			default:
9627 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9628 				    "sata_txlt_atapi_completion: "
9629 				    "invalid packet completion reason"));
9630 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9631 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9632 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9633 				    STATE_GOT_STATUS);
9634 				break;
9635 			}
9636 		}
9637 	}
9638 
9639 	SATAATAPITRACE(spx, 0);
9640 
9641 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9642 	    scsipkt->pkt_comp != NULL) {
9643 		/* scsi callback required */
9644 		(*scsipkt->pkt_comp)(scsipkt);
9645 	}
9646 }
9647 
9648 /*
9649  * Set up error retrieval sata command for ATAPI Packet Command error data
9650  * recovery.
9651  *
9652  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9653  * returns SATA_FAILURE otherwise.
9654  */
9655 
9656 static int
9657 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9658 {
9659 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9660 	sata_cmd_t *scmd;
9661 	struct buf *bp;
9662 
9663 	/*
9664 	 * Allocate dma-able buffer error data.
9665 	 * Buffer allocation will take care of buffer alignment and other DMA
9666 	 * attributes.
9667 	 */
9668 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9669 	if (bp == NULL) {
9670 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9671 		    "sata_get_err_retrieval_pkt: "
9672 		    "cannot allocate buffer for error data", NULL);
9673 		return (SATA_FAILURE);
9674 	}
9675 	bp_mapin(bp); /* make data buffer accessible */
9676 
9677 	/* Operation modes are up to the caller */
9678 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9679 
9680 	/* Synchronous mode, no callback - may be changed by the caller */
9681 	spkt->satapkt_comp = NULL;
9682 	spkt->satapkt_time = sata_default_pkt_time;
9683 
9684 	scmd = &spkt->satapkt_cmd;
9685 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9686 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9687 
9688 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9689 
9690 	/*
9691 	 * Set-up acdb. Request Sense CDB (packet command content) is
9692 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9693 	 * it is transfered into packet FIS).
9694 	 */
9695 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9696 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9697 	/* Following zeroing of pad bytes may not be necessary */
9698 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9699 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9700 
9701 	/*
9702 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9703 	 * before accessing it. Handle is in usual place in translate struct.
9704 	 */
9705 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9706 
9707 	/*
9708 	 * Preset request sense data to NO SENSE.
9709 	 * Here it is redundant, only for a symetry with scsi-originated
9710 	 * packets. It should not be used for anything but debugging.
9711 	 */
9712 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9713 	sata_fixed_sense_data_preset(
9714 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9715 
9716 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9717 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9718 
9719 	return (SATA_SUCCESS);
9720 }
9721 
9722 /*
9723  * Set-up ATAPI packet command.
9724  * Data transfer direction has to be set-up in sata_cmd structure prior to
9725  * calling this function.
9726  *
9727  * Returns void
9728  */
9729 
9730 static void
9731 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9732 {
9733 	scmd->satacmd_addr_type = 0;		/* N/A */
9734 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9735 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9736 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9737 	scmd->satacmd_lba_high_lsb =
9738 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9739 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9740 
9741 	/*
9742 	 * We want all data to be transfered via DMA.
9743 	 * But specify it only if drive supports DMA and DMA mode is
9744 	 * selected - some drives are sensitive about it.
9745 	 * Hopefully it wil work for all drives....
9746 	 */
9747 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9748 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9749 
9750 	/*
9751 	 * Features register requires special care for devices that use
9752 	 * Serial ATA bridge - they need an explicit specification of
9753 	 * the data transfer direction for Packet DMA commands.
9754 	 * Setting this bit is harmless if DMA is not used.
9755 	 *
9756 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9757 	 * spec they follow.
9758 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9759 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9760 	 * ATA/ATAPI-7 support is explicitly indicated.
9761 	 */
9762 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9763 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9764 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9765 		/*
9766 		 * Specification of major version is valid and version 7
9767 		 * is supported. It does automatically imply that all
9768 		 * spec features are supported. For now, we assume that
9769 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9770 		 */
9771 		if ((sdinfo->satadrv_id.ai_dirdma &
9772 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9773 			if (scmd->satacmd_flags.sata_data_direction ==
9774 			    SATA_DIR_READ)
9775 			scmd->satacmd_features_reg |=
9776 			    SATA_ATAPI_F_DATA_DIR_READ;
9777 		}
9778 	}
9779 }
9780 
9781 
9782 #ifdef SATA_DEBUG
9783 
9784 /* Display 18 bytes of Inquiry data */
9785 static void
9786 sata_show_inqry_data(uint8_t *buf)
9787 {
9788 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9789 	uint8_t *p;
9790 
9791 	cmn_err(CE_NOTE, "Inquiry data:");
9792 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9793 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9794 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9795 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9796 	    SATA_ATAPI_TRANS_VERSION(inq));
9797 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9798 	    inq->inq_rdf, inq->inq_aenc);
9799 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9800 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9801 	p = (uint8_t *)inq->inq_vid;
9802 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9803 	    "%02x %02x %02x %02x",
9804 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9805 	p = (uint8_t *)inq->inq_vid;
9806 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9807 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9808 
9809 	p = (uint8_t *)inq->inq_pid;
9810 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9811 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9812 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9813 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9814 	p = (uint8_t *)inq->inq_pid;
9815 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9816 	    "%c %c %c %c %c %c %c %c",
9817 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9818 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9819 
9820 	p = (uint8_t *)inq->inq_revision;
9821 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9822 	    p[0], p[1], p[2], p[3]);
9823 	p = (uint8_t *)inq->inq_revision;
9824 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9825 	    p[0], p[1], p[2], p[3]);
9826 
9827 }
9828 
9829 
9830 static void
9831 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9832 {
9833 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9834 
9835 	if (scsi_pkt == NULL)
9836 		return;
9837 	if (count != 0) {
9838 		/* saving cdb */
9839 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9840 		    SATA_ATAPI_MAX_CDB_LEN);
9841 		bcopy(scsi_pkt->pkt_cdbp,
9842 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9843 	} else {
9844 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9845 		    sts_sensedata,
9846 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9847 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9848 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9849 		    scsi_pkt->pkt_reason;
9850 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9851 		    spx->txlt_sata_pkt->satapkt_reason;
9852 
9853 		if (++sata_atapi_trace_index >= 64)
9854 			sata_atapi_trace_index = 0;
9855 	}
9856 }
9857 
9858 #endif
9859 
9860 /*
9861  * Fetch inquiry data from ATAPI device
9862  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9863  *
9864  * Note:
9865  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9866  * where the caller expects to see the inquiry data.
9867  *
9868  */
9869 
9870 static int
9871 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9872     sata_address_t *saddr, struct scsi_inquiry *inq)
9873 {
9874 	sata_pkt_txlate_t *spx;
9875 	sata_pkt_t *spkt;
9876 	struct buf *bp;
9877 	sata_drive_info_t *sdinfo;
9878 	sata_cmd_t *scmd;
9879 	int rval;
9880 	uint8_t *rqsp;
9881 	dev_info_t *dip = SATA_DIP(sata_hba);
9882 #ifdef SATA_DEBUG
9883 	char msg_buf[MAXPATHLEN];
9884 #endif
9885 	kmutex_t *cport_mutex;
9886 
9887 	ASSERT(sata_hba != NULL);
9888 
9889 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9890 	spx->txlt_sata_hba_inst = sata_hba;
9891 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9892 	spkt = sata_pkt_alloc(spx, NULL);
9893 	if (spkt == NULL) {
9894 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9895 		return (SATA_FAILURE);
9896 	}
9897 	/* address is needed now */
9898 	spkt->satapkt_device.satadev_addr = *saddr;
9899 
9900 	/* scsi_inquiry size buffer */
9901 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9902 	if (bp == NULL) {
9903 		sata_pkt_free(spx);
9904 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9905 		SATA_LOG_D((sata_hba, CE_WARN,
9906 		    "sata_get_atapi_inquiry_data: "
9907 		    "cannot allocate data buffer"));
9908 		return (SATA_FAILURE);
9909 	}
9910 	bp_mapin(bp); /* make data buffer accessible */
9911 
9912 	scmd = &spkt->satapkt_cmd;
9913 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9914 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9915 
9916 	/* Use synchronous mode */
9917 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9918 	spkt->satapkt_comp = NULL;
9919 	spkt->satapkt_time = sata_default_pkt_time;
9920 
9921 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9922 
9923 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9924 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9925 
9926 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9927 	mutex_enter(cport_mutex);
9928 	sdinfo = sata_get_device_info(sata_hba,
9929 	    &spx->txlt_sata_pkt->satapkt_device);
9930 	if (sdinfo == NULL) {
9931 		/* we have to be carefull about the disapearing device */
9932 		mutex_exit(cport_mutex);
9933 		rval = SATA_FAILURE;
9934 		goto cleanup;
9935 	}
9936 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9937 
9938 	/*
9939 	 * Set-up acdb. This works for atapi transport version 2 and later.
9940 	 */
9941 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9942 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9943 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
9944 	scmd->satacmd_acdb[1] = 0x00;
9945 	scmd->satacmd_acdb[2] = 0x00;
9946 	scmd->satacmd_acdb[3] = 0x00;
9947 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9948 	scmd->satacmd_acdb[5] = 0x00;
9949 
9950 	sata_fixed_sense_data_preset(
9951 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9952 
9953 	/* Transfer command to HBA */
9954 	if (sata_hba_start(spx, &rval) != 0) {
9955 		/* Pkt not accepted for execution */
9956 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9957 		    "sata_get_atapi_inquiry_data: "
9958 		    "Packet not accepted for execution - ret: %02x", rval);
9959 		mutex_exit(cport_mutex);
9960 		rval = SATA_FAILURE;
9961 		goto cleanup;
9962 	}
9963 	mutex_exit(cport_mutex);
9964 
9965 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9966 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9967 		    "sata_get_atapi_inquiry_data: "
9968 		    "Packet completed successfully - ret: %02x", rval);
9969 		if (spx->txlt_buf_dma_handle != NULL) {
9970 			/*
9971 			 * Sync buffer. Handle is in usual place in translate
9972 			 * struct.
9973 			 */
9974 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
9975 			    DDI_DMA_SYNC_FORCPU);
9976 			ASSERT(rval == DDI_SUCCESS);
9977 		}
9978 
9979 		if (sata_check_for_dma_error(dip, spx)) {
9980 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
9981 			rval = SATA_FAILURE;
9982 		} else {
9983 			/*
9984 			 * Normal completion - copy data into caller's buffer
9985 			 */
9986 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
9987 			    sizeof (struct scsi_inquiry));
9988 #ifdef SATA_DEBUG
9989 			if (sata_debug_flags & SATA_DBG_ATAPI) {
9990 				sata_show_inqry_data((uint8_t *)inq);
9991 			}
9992 #endif
9993 			rval = SATA_SUCCESS;
9994 		}
9995 	} else {
9996 		/*
9997 		 * Something went wrong - analyze return - check rqsense data
9998 		 */
9999 		rval = SATA_FAILURE;
10000 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10001 			/*
10002 			 * ARQ data hopefull show something other than NO SENSE
10003 			 */
10004 			rqsp = scmd->satacmd_rqsense;
10005 #ifdef SATA_DEBUG
10006 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10007 				msg_buf[0] = '\0';
10008 				(void) snprintf(msg_buf, MAXPATHLEN,
10009 				    "ATAPI packet completion reason: %02x\n"
10010 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10011 				    "          %02x %02x %02x %02x %02x %02x\n"
10012 				    "          %02x %02x %02x %02x %02x %02x",
10013 				    spkt->satapkt_reason,
10014 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10015 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10016 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10017 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10018 				    rqsp[16], rqsp[17]);
10019 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10020 				    "%s", msg_buf);
10021 			}
10022 #endif
10023 		} else {
10024 			switch (spkt->satapkt_reason) {
10025 			case SATA_PKT_PORT_ERROR:
10026 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10027 				    "sata_get_atapi_inquiry_data: "
10028 				    "packet reason: port error", NULL);
10029 				break;
10030 
10031 			case SATA_PKT_TIMEOUT:
10032 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10033 				    "sata_get_atapi_inquiry_data: "
10034 				    "packet reason: timeout", NULL);
10035 				break;
10036 
10037 			case SATA_PKT_ABORTED:
10038 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10039 				    "sata_get_atapi_inquiry_data: "
10040 				    "packet reason: aborted", NULL);
10041 				break;
10042 
10043 			case SATA_PKT_RESET:
10044 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10045 				    "sata_get_atapi_inquiry_data: "
10046 				    "packet reason: reset\n", NULL);
10047 				break;
10048 			default:
10049 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10050 				    "sata_get_atapi_inquiry_data: "
10051 				    "invalid packet reason: %02x\n",
10052 				    spkt->satapkt_reason);
10053 				break;
10054 			}
10055 		}
10056 	}
10057 cleanup:
10058 	sata_free_local_buffer(spx);
10059 	sata_pkt_free(spx);
10060 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10061 	return (rval);
10062 }
10063 
10064 
10065 
10066 
10067 
10068 #if 0
10069 #ifdef SATA_DEBUG
10070 
10071 /*
10072  * Test ATAPI packet command.
10073  * Single threaded test: send packet command in synch mode, process completion
10074  *
10075  */
10076 static void
10077 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10078 {
10079 	sata_pkt_txlate_t *spx;
10080 	sata_pkt_t *spkt;
10081 	struct buf *bp;
10082 	sata_device_t sata_device;
10083 	sata_drive_info_t *sdinfo;
10084 	sata_cmd_t *scmd;
10085 	int rval;
10086 	uint8_t *rqsp;
10087 
10088 	ASSERT(sata_hba_inst != NULL);
10089 	sata_device.satadev_addr.cport = cport;
10090 	sata_device.satadev_addr.pmport = 0;
10091 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10092 	sata_device.satadev_rev = SATA_DEVICE_REV;
10093 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10094 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10095 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10096 	if (sdinfo == NULL) {
10097 		sata_log(sata_hba_inst, CE_WARN,
10098 		    "sata_test_atapi_packet_command: "
10099 		    "no device info for cport %d",
10100 		    sata_device.satadev_addr.cport);
10101 		return;
10102 	}
10103 
10104 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10105 	spx->txlt_sata_hba_inst = sata_hba_inst;
10106 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10107 	spkt = sata_pkt_alloc(spx, NULL);
10108 	if (spkt == NULL) {
10109 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10110 		return;
10111 	}
10112 	/* address is needed now */
10113 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10114 
10115 	/* 1024k buffer */
10116 	bp = sata_alloc_local_buffer(spx, 1024);
10117 	if (bp == NULL) {
10118 		sata_pkt_free(spx);
10119 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10120 		sata_log(sata_hba_inst, CE_WARN,
10121 		    "sata_test_atapi_packet_command: "
10122 		    "cannot allocate data buffer");
10123 		return;
10124 	}
10125 	bp_mapin(bp); /* make data buffer accessible */
10126 
10127 	scmd = &spkt->satapkt_cmd;
10128 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10129 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10130 
10131 	/* Use synchronous mode */
10132 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10133 
10134 	/* Synchronous mode, no callback - may be changed by the caller */
10135 	spkt->satapkt_comp = NULL;
10136 	spkt->satapkt_time = sata_default_pkt_time;
10137 
10138 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10139 
10140 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10141 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10142 
10143 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10144 
10145 	/* Set-up acdb. */
10146 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10147 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10148 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10149 	scmd->satacmd_acdb[1] = 0x00;
10150 	scmd->satacmd_acdb[2] = 0x00;
10151 	scmd->satacmd_acdb[3] = 0x00;
10152 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10153 	scmd->satacmd_acdb[5] = 0x00;
10154 
10155 	sata_fixed_sense_data_preset(
10156 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10157 
10158 	/* Transfer command to HBA */
10159 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10160 	if (sata_hba_start(spx, &rval) != 0) {
10161 		/* Pkt not accepted for execution */
10162 		sata_log(sata_hba_inst, CE_WARN,
10163 		    "sata_test_atapi_packet_command: "
10164 		    "Packet not accepted for execution - ret: %02x", rval);
10165 		mutex_exit(
10166 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10167 		goto cleanup;
10168 	}
10169 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10170 
10171 	if (spx->txlt_buf_dma_handle != NULL) {
10172 		/*
10173 		 * Sync buffer. Handle is in usual place in translate struct.
10174 		 */
10175 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10176 		    DDI_DMA_SYNC_FORCPU);
10177 		ASSERT(rval == DDI_SUCCESS);
10178 	}
10179 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10180 		sata_log(sata_hba_inst, CE_WARN,
10181 		    "sata_test_atapi_packet_command: "
10182 		    "Packet completed successfully");
10183 		/*
10184 		 * Normal completion - show inquiry data
10185 		 */
10186 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10187 	} else {
10188 		/*
10189 		 * Something went wrong - analyze return - check rqsense data
10190 		 */
10191 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10192 			/*
10193 			 * ARQ data hopefull show something other than NO SENSE
10194 			 */
10195 			rqsp = scmd->satacmd_rqsense;
10196 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10197 			    "ATAPI packet completion reason: %02x\n"
10198 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10199 			    "          %02x %02x %02x %02x %02x %02x "
10200 			    "          %02x %02x %02x %02x %02x %02x\n",
10201 			    spkt->satapkt_reason,
10202 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10203 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10204 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10205 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10206 			    rqsp[16], rqsp[17]);
10207 		} else {
10208 			switch (spkt->satapkt_reason) {
10209 			case SATA_PKT_PORT_ERROR:
10210 				sata_log(sata_hba_inst, CE_WARN,
10211 				    "sata_test_atapi_packet_command: "
10212 				    "packet reason: port error\n");
10213 				break;
10214 
10215 			case SATA_PKT_TIMEOUT:
10216 				sata_log(sata_hba_inst, CE_WARN,
10217 				    "sata_test_atapi_packet_command: "
10218 				    "packet reason: timeout\n");
10219 				break;
10220 
10221 			case SATA_PKT_ABORTED:
10222 				sata_log(sata_hba_inst, CE_WARN,
10223 				    "sata_test_atapi_packet_command: "
10224 				    "packet reason: aborted\n");
10225 				break;
10226 
10227 			case SATA_PKT_RESET:
10228 				sata_log(sata_hba_inst, CE_WARN,
10229 				    "sata_test_atapi_packet_command: "
10230 				    "packet reason: reset\n");
10231 				break;
10232 			default:
10233 				sata_log(sata_hba_inst, CE_WARN,
10234 				    "sata_test_atapi_packet_command: "
10235 				    "invalid packet reason: %02x\n",
10236 				    spkt->satapkt_reason);
10237 				break;
10238 			}
10239 		}
10240 	}
10241 cleanup:
10242 	sata_free_local_buffer(spx);
10243 	sata_pkt_free(spx);
10244 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10245 }
10246 
10247 #endif /* SATA_DEBUG */
10248 #endif /* 1 */
10249 
10250 
10251 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10252 
10253 /*
10254  * Validate sata_tran info
10255  * SATA_FAILURE returns if structure is inconsistent or structure revision
10256  * does not match one used by the framework.
10257  *
10258  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10259  * required function pointers.
10260  * Returns SATA_FAILURE otherwise.
10261  */
10262 static int
10263 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10264 {
10265 	/*
10266 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10267 	 * of the SATA interface.
10268 	 */
10269 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10270 		sata_log(NULL, CE_WARN,
10271 		    "sata: invalid sata_hba_tran version %d for driver %s",
10272 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10273 		return (SATA_FAILURE);
10274 	}
10275 
10276 	if (dip != sata_tran->sata_tran_hba_dip) {
10277 		SATA_LOG_D((NULL, CE_WARN,
10278 		    "sata: inconsistent sata_tran_hba_dip "
10279 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10280 		return (SATA_FAILURE);
10281 	}
10282 
10283 	if (sata_tran->sata_tran_probe_port == NULL ||
10284 	    sata_tran->sata_tran_start == NULL ||
10285 	    sata_tran->sata_tran_abort == NULL ||
10286 	    sata_tran->sata_tran_reset_dport == NULL ||
10287 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10288 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10289 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10290 	    NULL) {
10291 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10292 		    "required functions"));
10293 	}
10294 	return (SATA_SUCCESS);
10295 }
10296 
10297 /*
10298  * Remove HBA instance from sata_hba_list.
10299  */
10300 static void
10301 sata_remove_hba_instance(dev_info_t *dip)
10302 {
10303 	sata_hba_inst_t	*sata_hba_inst;
10304 
10305 	mutex_enter(&sata_mutex);
10306 	for (sata_hba_inst = sata_hba_list;
10307 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10308 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10309 		if (sata_hba_inst->satahba_dip == dip)
10310 			break;
10311 	}
10312 
10313 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10314 #ifdef SATA_DEBUG
10315 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10316 		    "unknown HBA instance\n");
10317 #endif
10318 		ASSERT(FALSE);
10319 	}
10320 	if (sata_hba_inst == sata_hba_list) {
10321 		sata_hba_list = sata_hba_inst->satahba_next;
10322 		if (sata_hba_list) {
10323 			sata_hba_list->satahba_prev =
10324 			    (struct sata_hba_inst *)NULL;
10325 		}
10326 		if (sata_hba_inst == sata_hba_list_tail) {
10327 			sata_hba_list_tail = NULL;
10328 		}
10329 	} else if (sata_hba_inst == sata_hba_list_tail) {
10330 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10331 		if (sata_hba_list_tail) {
10332 			sata_hba_list_tail->satahba_next =
10333 			    (struct sata_hba_inst *)NULL;
10334 		}
10335 	} else {
10336 		sata_hba_inst->satahba_prev->satahba_next =
10337 		    sata_hba_inst->satahba_next;
10338 		sata_hba_inst->satahba_next->satahba_prev =
10339 		    sata_hba_inst->satahba_prev;
10340 	}
10341 	mutex_exit(&sata_mutex);
10342 }
10343 
10344 /*
10345  * Probe all SATA ports of the specified HBA instance.
10346  * The assumption is that there are no target and attachment point minor nodes
10347  * created by the boot subsystems, so we do not need to prune device tree.
10348  *
10349  * This function is called only from sata_hba_attach(). It does not have to
10350  * be protected by controller mutex, because the hba_attached flag is not set
10351  * yet and no one would be touching this HBA instance other than this thread.
10352  * Determines if port is active and what type of the device is attached
10353  * (if any). Allocates necessary structures for each port.
10354  *
10355  * An AP (Attachement Point) node is created for each SATA device port even
10356  * when there is no device attached.
10357  */
10358 
10359 static 	void
10360 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10361 {
10362 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10363 	int			ncport;
10364 	sata_cport_info_t 	*cportinfo;
10365 	sata_drive_info_t	*drive;
10366 	sata_device_t		sata_device;
10367 	int			rval;
10368 	dev_t			minor_number;
10369 	char			name[16];
10370 	clock_t			start_time, cur_time;
10371 
10372 	/*
10373 	 * Probe controller ports first, to find port status and
10374 	 * any port multiplier attached.
10375 	 */
10376 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10377 		/* allocate cport structure */
10378 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10379 		ASSERT(cportinfo != NULL);
10380 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10381 
10382 		mutex_enter(&cportinfo->cport_mutex);
10383 
10384 		cportinfo->cport_addr.cport = ncport;
10385 		cportinfo->cport_addr.pmport = 0;
10386 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10387 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10388 		cportinfo->cport_state |= SATA_STATE_PROBING;
10389 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10390 
10391 		/*
10392 		 * Regardless if a port is usable or not, create
10393 		 * an attachment point
10394 		 */
10395 		mutex_exit(&cportinfo->cport_mutex);
10396 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10397 		    ncport, 0, SATA_ADDR_CPORT);
10398 		(void) sprintf(name, "%d", ncport);
10399 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10400 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10401 		    DDI_SUCCESS) {
10402 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10403 			    "cannot create SATA attachment point for port %d",
10404 			    ncport);
10405 		}
10406 
10407 		/* Probe port */
10408 		start_time = ddi_get_lbolt();
10409 	reprobe_cport:
10410 		sata_device.satadev_addr.cport = ncport;
10411 		sata_device.satadev_addr.pmport = 0;
10412 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10413 		sata_device.satadev_rev = SATA_DEVICE_REV;
10414 
10415 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10416 		    (dip, &sata_device);
10417 
10418 		mutex_enter(&cportinfo->cport_mutex);
10419 		cportinfo->cport_scr = sata_device.satadev_scr;
10420 		if (rval != SATA_SUCCESS) {
10421 			/* Something went wrong? Fail the port */
10422 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10423 			mutex_exit(&cportinfo->cport_mutex);
10424 			continue;
10425 		}
10426 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10427 		cportinfo->cport_state |= SATA_STATE_PROBED;
10428 		cportinfo->cport_dev_type = sata_device.satadev_type;
10429 
10430 		cportinfo->cport_state |= SATA_STATE_READY;
10431 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10432 			mutex_exit(&cportinfo->cport_mutex);
10433 			continue;
10434 		}
10435 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10436 			/*
10437 			 * There is some device attached.
10438 			 * Allocate device info structure
10439 			 */
10440 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10441 				mutex_exit(&cportinfo->cport_mutex);
10442 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10443 				    kmem_zalloc(sizeof (sata_drive_info_t),
10444 				    KM_SLEEP);
10445 				mutex_enter(&cportinfo->cport_mutex);
10446 			}
10447 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10448 			drive->satadrv_addr = cportinfo->cport_addr;
10449 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10450 			drive->satadrv_type = cportinfo->cport_dev_type;
10451 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10452 
10453 			mutex_exit(&cportinfo->cport_mutex);
10454 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10455 			    SATA_SUCCESS) {
10456 				/*
10457 				 * Plugged device was not correctly identified.
10458 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10459 				 */
10460 				cur_time = ddi_get_lbolt();
10461 				if ((cur_time - start_time) <
10462 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10463 					/* sleep for a while */
10464 					delay(drv_usectohz(
10465 					    SATA_DEV_RETRY_DLY));
10466 					goto reprobe_cport;
10467 				}
10468 			}
10469 		} else { /* SATA_DTYPE_PMULT */
10470 			mutex_exit(&cportinfo->cport_mutex);
10471 
10472 			/* Allocate sata_pmult_info and sata_pmport_info */
10473 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10474 			    SATA_SUCCESS)
10475 				continue;
10476 
10477 			/* Log the information of the port multiplier */
10478 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10479 
10480 			/* Probe its pmports */
10481 			sata_probe_pmports(sata_hba_inst, ncport);
10482 		}
10483 	}
10484 }
10485 
10486 /*
10487  * Probe all device ports behind a port multiplier.
10488  *
10489  * PMult-related structure should be allocated before by sata_alloc_pmult().
10490  *
10491  * NOTE1: Only called from sata_probe_ports()
10492  * NOTE2: No mutex should be hold.
10493  */
10494 static void
10495 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10496 {
10497 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10498 	sata_pmult_info_t	*pmultinfo = NULL;
10499 	sata_pmport_info_t 	*pmportinfo = NULL;
10500 	sata_drive_info_t	*drive = NULL;
10501 	sata_device_t		sata_device;
10502 
10503 	clock_t			start_time, cur_time;
10504 	int			npmport;
10505 	int			rval;
10506 
10507 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10508 
10509 	/* Probe Port Multiplier ports */
10510 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10511 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10512 		start_time = ddi_get_lbolt();
10513 reprobe_pmport:
10514 		sata_device.satadev_addr.cport = ncport;
10515 		sata_device.satadev_addr.pmport = npmport;
10516 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10517 		sata_device.satadev_rev = SATA_DEVICE_REV;
10518 
10519 		/* Let HBA driver probe it. */
10520 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10521 		    (dip, &sata_device);
10522 		mutex_enter(&pmportinfo->pmport_mutex);
10523 
10524 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10525 
10526 		if (rval != SATA_SUCCESS) {
10527 			pmportinfo->pmport_state =
10528 			    SATA_PSTATE_FAILED;
10529 			mutex_exit(&pmportinfo->pmport_mutex);
10530 			continue;
10531 		}
10532 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10533 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10534 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10535 
10536 		pmportinfo->pmport_state |= SATA_STATE_READY;
10537 		if (pmportinfo->pmport_dev_type ==
10538 		    SATA_DTYPE_NONE) {
10539 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10540 			    "no device found at port %d:%d", ncport, npmport);
10541 			mutex_exit(&pmportinfo->pmport_mutex);
10542 			continue;
10543 		}
10544 		/* Port multipliers cannot be chained */
10545 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10546 		/*
10547 		 * There is something attached to Port
10548 		 * Multiplier device port
10549 		 * Allocate device info structure
10550 		 */
10551 		if (pmportinfo->pmport_sata_drive == NULL) {
10552 			mutex_exit(&pmportinfo->pmport_mutex);
10553 			pmportinfo->pmport_sata_drive =
10554 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10555 			mutex_enter(&pmportinfo->pmport_mutex);
10556 		}
10557 		drive = pmportinfo->pmport_sata_drive;
10558 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10559 		drive->satadrv_addr.pmport = npmport;
10560 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10561 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10562 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10563 
10564 		mutex_exit(&pmportinfo->pmport_mutex);
10565 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10566 
10567 		if (rval != SATA_SUCCESS) {
10568 			/*
10569 			 * Plugged device was not correctly identified.
10570 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10571 			 */
10572 			cur_time = ddi_get_lbolt();
10573 			if ((cur_time - start_time) < drv_usectohz(
10574 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10575 				/* sleep for a while */
10576 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10577 				goto reprobe_pmport;
10578 			}
10579 		}
10580 	}
10581 }
10582 
10583 /*
10584  * Add SATA device for specified HBA instance & port (SCSI target
10585  * device nodes).
10586  * This function is called (indirectly) only from sata_hba_attach().
10587  * A target node is created when there is a supported type device attached,
10588  * but may be removed if it cannot be put online.
10589  *
10590  * This function cannot be called from an interrupt context.
10591  *
10592  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10593  *
10594  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10595  * device identification failed - adding a device could be retried.
10596  *
10597  */
10598 static 	int
10599 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10600     sata_device_t *sata_device)
10601 {
10602 	sata_cport_info_t 	*cportinfo;
10603 	sata_pmult_info_t	*pminfo;
10604 	sata_pmport_info_t	*pmportinfo;
10605 	dev_info_t		*cdip;		/* child dip */
10606 	sata_address_t		*saddr = &sata_device->satadev_addr;
10607 	uint8_t			cport, pmport;
10608 	int			rval;
10609 
10610 	cport = saddr->cport;
10611 	pmport = saddr->pmport;
10612 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10613 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10614 
10615 	/*
10616 	 * Some device is attached to a controller port.
10617 	 * We rely on controllers distinquishing between no-device,
10618 	 * attached port multiplier and other kind of attached device.
10619 	 * We need to get Identify Device data and determine
10620 	 * positively the dev type before trying to attach
10621 	 * the target driver.
10622 	 */
10623 	sata_device->satadev_rev = SATA_DEVICE_REV;
10624 	switch (saddr->qual) {
10625 	case SATA_ADDR_CPORT:
10626 		/*
10627 		 * Add a non-port-multiplier device at controller port.
10628 		 */
10629 		saddr->qual = SATA_ADDR_DCPORT;
10630 
10631 		rval = sata_probe_device(sata_hba_inst, sata_device);
10632 		if (rval != SATA_SUCCESS ||
10633 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10634 			return (SATA_FAILURE);
10635 
10636 		mutex_enter(&cportinfo->cport_mutex);
10637 		sata_show_drive_info(sata_hba_inst,
10638 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10639 
10640 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10641 			/*
10642 			 * Could not determine device type or
10643 			 * a device is not supported.
10644 			 * Degrade this device to unknown.
10645 			 */
10646 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10647 			mutex_exit(&cportinfo->cport_mutex);
10648 			return (SATA_SUCCESS);
10649 		}
10650 		cportinfo->cport_dev_type = sata_device->satadev_type;
10651 		cportinfo->cport_tgtnode_clean = B_TRUE;
10652 		mutex_exit(&cportinfo->cport_mutex);
10653 
10654 		/*
10655 		 * Initialize device to the desired state. Even if it
10656 		 * fails, the device will still attach but syslog
10657 		 * will show the warning.
10658 		 */
10659 		if (sata_initialize_device(sata_hba_inst,
10660 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10661 			/* Retry */
10662 			rval = sata_initialize_device(sata_hba_inst,
10663 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10664 
10665 			if (rval == SATA_RETRY)
10666 				sata_log(sata_hba_inst, CE_WARN,
10667 				    "SATA device at port %d - "
10668 				    "default device features could not be set."
10669 				    " Device may not operate as expected.",
10670 				    cport);
10671 		}
10672 
10673 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10674 		if (cdip == NULL) {
10675 			/*
10676 			 * Attaching target node failed.
10677 			 * We retain sata_drive_info structure...
10678 			 */
10679 			return (SATA_SUCCESS);
10680 		}
10681 
10682 		mutex_enter(&cportinfo->cport_mutex);
10683 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10684 		    satadrv_state = SATA_STATE_READY;
10685 		mutex_exit(&cportinfo->cport_mutex);
10686 
10687 		break;
10688 
10689 	case SATA_ADDR_PMPORT:
10690 		saddr->qual = SATA_ADDR_DPMPORT;
10691 
10692 		mutex_enter(&cportinfo->cport_mutex);
10693 		/* It must be a Port Multiplier at the controller port */
10694 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10695 
10696 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10697 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10698 		mutex_exit(&cportinfo->cport_mutex);
10699 
10700 		rval = sata_probe_device(sata_hba_inst, sata_device);
10701 		if (rval != SATA_SUCCESS ||
10702 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10703 			return (SATA_FAILURE);
10704 		}
10705 
10706 		mutex_enter(&pmportinfo->pmport_mutex);
10707 		sata_show_drive_info(sata_hba_inst,
10708 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10709 
10710 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10711 			/*
10712 			 * Could not determine device type.
10713 			 * Degrade this device to unknown.
10714 			 */
10715 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10716 			mutex_exit(&pmportinfo->pmport_mutex);
10717 			return (SATA_SUCCESS);
10718 		}
10719 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10720 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10721 		mutex_exit(&pmportinfo->pmport_mutex);
10722 
10723 		/*
10724 		 * Initialize device to the desired state.
10725 		 * Even if it fails, the device will still
10726 		 * attach but syslog will show the warning.
10727 		 */
10728 		if (sata_initialize_device(sata_hba_inst,
10729 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10730 			/* Retry */
10731 			rval = sata_initialize_device(sata_hba_inst,
10732 			    pmportinfo->pmport_sata_drive);
10733 
10734 			if (rval == SATA_RETRY)
10735 				sata_log(sata_hba_inst, CE_WARN,
10736 				    "SATA device at port %d:%d - "
10737 				    "default device features could not be set."
10738 				    " Device may not operate as expected.",
10739 				    cport, pmport);
10740 		}
10741 
10742 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10743 		if (cdip == NULL) {
10744 			/*
10745 			 * Attaching target node failed.
10746 			 * We retain sata_drive_info structure...
10747 			 */
10748 			return (SATA_SUCCESS);
10749 		}
10750 		mutex_enter(&pmportinfo->pmport_mutex);
10751 		pmportinfo->pmport_sata_drive->satadrv_state |=
10752 		    SATA_STATE_READY;
10753 		mutex_exit(&pmportinfo->pmport_mutex);
10754 
10755 		break;
10756 
10757 	default:
10758 		return (SATA_FAILURE);
10759 	}
10760 
10761 	return (SATA_SUCCESS);
10762 }
10763 
10764 /*
10765  * Clean up target node at specific address.
10766  *
10767  * NOTE: No Mutex should be hold.
10768  */
10769 static int
10770 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10771     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10772 {
10773 	uint8_t cport, pmport, qual;
10774 	dev_info_t *tdip;
10775 
10776 	cport = sata_device->satadev_addr.cport;
10777 	pmport = sata_device->satadev_addr.pmport;
10778 	qual = sata_device->satadev_addr.qual;
10779 
10780 	if (qual == SATA_ADDR_DCPORT) {
10781 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10782 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10783 	} else {
10784 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10785 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10786 		    cport, pmport));
10787 	}
10788 
10789 	/* We are addressing attached device, not a port */
10790 	sata_device->satadev_addr.qual =
10791 	    sdinfo->satadrv_addr.qual;
10792 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10793 	    &sata_device->satadev_addr);
10794 	if (tdip != NULL && ndi_devi_offline(tdip,
10795 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10796 		/*
10797 		 * Problem :
10798 		 * The target node remained attached.
10799 		 * This happens when the device file was open
10800 		 * or a node was waiting for resources.
10801 		 * Cannot do anything about it.
10802 		 */
10803 		if (qual == SATA_ADDR_DCPORT) {
10804 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10805 			    "sata_hba_ioctl: disconnect: could "
10806 			    "not unconfigure device before "
10807 			    "disconnecting the SATA port %d",
10808 			    cport));
10809 		} else {
10810 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10811 			    "sata_hba_ioctl: disconnect: could "
10812 			    "not unconfigure device before "
10813 			    "disconnecting the SATA port %d:%d",
10814 			    cport, pmport));
10815 		}
10816 		/*
10817 		 * Set DEVICE REMOVED state in the target
10818 		 * node. It will prevent access to the device
10819 		 * even when a new device is attached, until
10820 		 * the old target node is released, removed and
10821 		 * recreated for a new  device.
10822 		 */
10823 		sata_set_device_removed(tdip);
10824 
10825 		/*
10826 		 * Instruct event daemon to try the target
10827 		 * node cleanup later.
10828 		 */
10829 		sata_set_target_node_cleanup(
10830 		    sata_hba_inst, &sata_device->satadev_addr);
10831 	}
10832 
10833 
10834 	return (SATA_SUCCESS);
10835 }
10836 
10837 
10838 /*
10839  * Create scsi target node for attached device, create node properties and
10840  * attach the node.
10841  * The node could be removed if the device onlining fails.
10842  *
10843  * A dev_info_t pointer is returned if operation is successful, NULL is
10844  * returned otherwise.
10845  */
10846 
10847 static dev_info_t *
10848 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10849 			sata_address_t *sata_addr)
10850 {
10851 	dev_info_t *cdip = NULL;
10852 	int rval;
10853 	char *nname = NULL;
10854 	char **compatible = NULL;
10855 	int ncompatible;
10856 	struct scsi_inquiry inq;
10857 	sata_device_t sata_device;
10858 	sata_drive_info_t *sdinfo;
10859 	int target;
10860 	int i;
10861 
10862 	sata_device.satadev_rev = SATA_DEVICE_REV;
10863 	sata_device.satadev_addr = *sata_addr;
10864 
10865 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10866 
10867 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10868 
10869 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10870 	    sata_addr->pmport, sata_addr->qual);
10871 
10872 	if (sdinfo == NULL) {
10873 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10874 		    sata_addr->cport)));
10875 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10876 		    "sata_create_target_node: no sdinfo for target %x",
10877 		    target));
10878 		return (NULL);
10879 	}
10880 
10881 	/*
10882 	 * create or get scsi inquiry data, expected by
10883 	 * scsi_hba_nodename_compatible_get()
10884 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10885 	 * ATAPI devices respond directly to Inquiry request.
10886 	 */
10887 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10888 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10889 		    (uint8_t *)&inq);
10890 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10891 		    sata_addr->cport)));
10892 	} else { /* Assume supported ATAPI device */
10893 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10894 		    sata_addr->cport)));
10895 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10896 		    &inq) == SATA_FAILURE)
10897 			return (NULL);
10898 		/*
10899 		 * Save supported ATAPI transport version
10900 		 */
10901 		sdinfo->satadrv_atapi_trans_ver =
10902 		    SATA_ATAPI_TRANS_VERSION(&inq);
10903 	}
10904 
10905 	/* determine the node name and compatible */
10906 	scsi_hba_nodename_compatible_get(&inq, NULL,
10907 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10908 
10909 #ifdef SATA_DEBUG
10910 	if (sata_debug_flags & SATA_DBG_NODES) {
10911 		if (nname == NULL) {
10912 			cmn_err(CE_NOTE, "sata_create_target_node: "
10913 			    "cannot determine nodename for target %d\n",
10914 			    target);
10915 		} else {
10916 			cmn_err(CE_WARN, "sata_create_target_node: "
10917 			    "target %d nodename: %s\n", target, nname);
10918 		}
10919 		if (compatible == NULL) {
10920 			cmn_err(CE_WARN,
10921 			    "sata_create_target_node: no compatible name\n");
10922 		} else {
10923 			for (i = 0; i < ncompatible; i++) {
10924 				cmn_err(CE_WARN, "sata_create_target_node: "
10925 				    "compatible name: %s\n", compatible[i]);
10926 			}
10927 		}
10928 	}
10929 #endif
10930 
10931 	/* if nodename can't be determined, log error and exit */
10932 	if (nname == NULL) {
10933 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10934 		    "sata_create_target_node: cannot determine nodename "
10935 		    "for target %d\n", target));
10936 		scsi_hba_nodename_compatible_free(nname, compatible);
10937 		return (NULL);
10938 	}
10939 	/*
10940 	 * Create scsi target node
10941 	 */
10942 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10943 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10944 	    "device-type", "scsi");
10945 
10946 	if (rval != DDI_PROP_SUCCESS) {
10947 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10948 		    "updating device_type prop failed %d", rval));
10949 		goto fail;
10950 	}
10951 
10952 	/*
10953 	 * Create target node properties: target & lun
10954 	 */
10955 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10956 	if (rval != DDI_PROP_SUCCESS) {
10957 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10958 		    "updating target prop failed %d", rval));
10959 		goto fail;
10960 	}
10961 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
10962 	if (rval != DDI_PROP_SUCCESS) {
10963 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10964 		    "updating target prop failed %d", rval));
10965 		goto fail;
10966 	}
10967 
10968 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10969 		/*
10970 		 * Add "variant" property
10971 		 */
10972 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10973 		    "variant", "atapi");
10974 		if (rval != DDI_PROP_SUCCESS) {
10975 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10976 			    "sata_create_target_node: variant atapi "
10977 			    "property could not be created: %d", rval));
10978 			goto fail;
10979 		}
10980 	}
10981 	/* decorate the node with compatible */
10982 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
10983 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
10984 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10985 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
10986 		    (void *)cdip));
10987 		goto fail;
10988 	}
10989 
10990 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10991 		/*
10992 		 * Add "sata-phy" property
10993 		 */
10994 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
10995 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
10996 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10997 			    "sata_create_target_node: failed to create "
10998 			    "\"sata-phy\" property: port %d",
10999 			    sata_addr->cport));
11000 		}
11001 	}
11002 
11003 
11004 	/*
11005 	 * Now, try to attach the driver. If probing of the device fails,
11006 	 * the target node may be removed
11007 	 */
11008 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11009 
11010 	scsi_hba_nodename_compatible_free(nname, compatible);
11011 
11012 	if (rval == NDI_SUCCESS)
11013 		return (cdip);
11014 
11015 	/* target node was removed - are we sure? */
11016 	return (NULL);
11017 
11018 fail:
11019 	scsi_hba_nodename_compatible_free(nname, compatible);
11020 	ddi_prop_remove_all(cdip);
11021 	rval = ndi_devi_free(cdip);
11022 	if (rval != NDI_SUCCESS) {
11023 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11024 		    "node removal failed %d", rval));
11025 	}
11026 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11027 	    "cannot create target node for SATA device at port %d",
11028 	    sata_addr->cport);
11029 	return (NULL);
11030 }
11031 
11032 /*
11033  * Remove a target node.
11034  */
11035 static void
11036 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11037 			sata_address_t *sata_addr)
11038 {
11039 	dev_info_t *tdip;
11040 	uint8_t cport = sata_addr->cport;
11041 	uint8_t pmport = sata_addr->pmport;
11042 	uint8_t qual = sata_addr->qual;
11043 
11044 	/* Note the sata daemon uses the address of the port/pmport */
11045 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11046 
11047 	/* Remove target node */
11048 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11049 	if (tdip != NULL) {
11050 		/*
11051 		 * Target node exists.  Unconfigure device
11052 		 * then remove the target node (one ndi
11053 		 * operation).
11054 		 */
11055 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11056 			/*
11057 			 * PROBLEM - no device, but target node remained. This
11058 			 * happens when the file was open or node was waiting
11059 			 * for resources.
11060 			 */
11061 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11062 			    "sata_remove_target_node: "
11063 			    "Failed to remove target node for "
11064 			    "detached SATA device."));
11065 			/*
11066 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11067 			 * re-check first that the node still exists.
11068 			 */
11069 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11070 			    cport, pmport);
11071 			if (tdip != NULL) {
11072 				sata_set_device_removed(tdip);
11073 				/*
11074 				 * Instruct event daemon to retry the cleanup
11075 				 * later.
11076 				 */
11077 				sata_set_target_node_cleanup(sata_hba_inst,
11078 				    sata_addr);
11079 			}
11080 		}
11081 
11082 		if (qual == SATA_ADDR_CPORT)
11083 			sata_log(sata_hba_inst, CE_WARN,
11084 			    "SATA device detached at port %d", cport);
11085 		else
11086 			sata_log(sata_hba_inst, CE_WARN,
11087 			    "SATA device detached at port %d:%d",
11088 			    cport, pmport);
11089 	}
11090 #ifdef SATA_DEBUG
11091 	else {
11092 		if (qual == SATA_ADDR_CPORT)
11093 			sata_log(sata_hba_inst, CE_WARN,
11094 			    "target node not found at port %d", cport);
11095 		else
11096 			sata_log(sata_hba_inst, CE_WARN,
11097 			    "target node not found at port %d:%d",
11098 			    cport, pmport);
11099 	}
11100 #endif
11101 }
11102 
11103 
11104 /*
11105  * Re-probe sata port, check for a device and attach info
11106  * structures when necessary. Identify Device data is fetched, if possible.
11107  * Assumption: sata address is already validated.
11108  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11109  * the presence of a device and its type.
11110  *
11111  * flag arg specifies that the function should try multiple times to identify
11112  * device type and to initialize it, or it should return immediately on failure.
11113  * SATA_DEV_IDENTIFY_RETRY - retry
11114  * SATA_DEV_IDENTIFY_NORETRY - no retry
11115  *
11116  * SATA_FAILURE is returned if one of the operations failed.
11117  *
11118  * This function cannot be called in interrupt context - it may sleep.
11119  *
11120  * Note: Port multiplier is supported.
11121  */
11122 static int
11123 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11124     int flag)
11125 {
11126 	sata_cport_info_t *cportinfo;
11127 	sata_pmult_info_t *pmultinfo;
11128 	sata_drive_info_t *sdinfo, *osdinfo;
11129 	boolean_t init_device = B_FALSE;
11130 	int prev_device_type = SATA_DTYPE_NONE;
11131 	int prev_device_settings = 0;
11132 	int prev_device_state = 0;
11133 	clock_t start_time;
11134 	int retry = B_FALSE;
11135 	uint8_t cport = sata_device->satadev_addr.cport;
11136 	int rval_probe, rval_init;
11137 
11138 	/*
11139 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11140 	 */
11141 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11142 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11143 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11144 
11145 	/* We only care about host sata cport for now */
11146 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11147 	    sata_device->satadev_addr.cport);
11148 
11149 	/*
11150 	 * If a port multiplier was previously attached (we have no idea it
11151 	 * still there or not), sata_reprobe_pmult() will handle it.
11152 	 */
11153 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11154 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11155 
11156 	/* Store sata_drive_info when a non-pmult device was attached. */
11157 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11158 	if (osdinfo != NULL) {
11159 		/*
11160 		 * We are re-probing port with a previously attached device.
11161 		 * Save previous device type and settings.
11162 		 */
11163 		prev_device_type = cportinfo->cport_dev_type;
11164 		prev_device_settings = osdinfo->satadrv_settings;
11165 		prev_device_state = osdinfo->satadrv_state;
11166 	}
11167 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11168 		start_time = ddi_get_lbolt();
11169 		retry = B_TRUE;
11170 	}
11171 retry_probe:
11172 
11173 	/* probe port */
11174 	mutex_enter(&cportinfo->cport_mutex);
11175 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11176 	cportinfo->cport_state |= SATA_STATE_PROBING;
11177 	mutex_exit(&cportinfo->cport_mutex);
11178 
11179 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11180 	    (SATA_DIP(sata_hba_inst), sata_device);
11181 
11182 	mutex_enter(&cportinfo->cport_mutex);
11183 	if (rval_probe != SATA_SUCCESS) {
11184 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11185 		mutex_exit(&cportinfo->cport_mutex);
11186 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11187 		    "SATA port %d probing failed",
11188 		    cportinfo->cport_addr.cport));
11189 		return (SATA_FAILURE);
11190 	}
11191 
11192 	/*
11193 	 * update sata port state and set device type
11194 	 */
11195 	sata_update_port_info(sata_hba_inst, sata_device);
11196 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11197 
11198 	/*
11199 	 * Sanity check - Port is active? Is the link active?
11200 	 * Is there any device attached?
11201 	 */
11202 	if ((cportinfo->cport_state &
11203 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11204 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11205 	    SATA_PORT_DEVLINK_UP) {
11206 		/*
11207 		 * Port in non-usable state or no link active/no device.
11208 		 * Free info structure if necessary (direct attached drive
11209 		 * only, for now!
11210 		 */
11211 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11212 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11213 		/* Add here differentiation for device attached or not */
11214 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11215 		mutex_exit(&cportinfo->cport_mutex);
11216 		if (sdinfo != NULL)
11217 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11218 		return (SATA_SUCCESS);
11219 	}
11220 
11221 	cportinfo->cport_state |= SATA_STATE_READY;
11222 	cportinfo->cport_state |= SATA_STATE_PROBED;
11223 
11224 	cportinfo->cport_dev_type = sata_device->satadev_type;
11225 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11226 
11227 	/*
11228 	 * If we are re-probing the port, there may be
11229 	 * sata_drive_info structure attached
11230 	 */
11231 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11232 
11233 		/*
11234 		 * There is no device, so remove device info structure,
11235 		 * if necessary.
11236 		 */
11237 		/* Device change: Drive -> None */
11238 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11239 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11240 		if (sdinfo != NULL) {
11241 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11242 			sata_log(sata_hba_inst, CE_WARN,
11243 			    "SATA device detached "
11244 			    "from port %d", cportinfo->cport_addr.cport);
11245 		}
11246 		mutex_exit(&cportinfo->cport_mutex);
11247 		return (SATA_SUCCESS);
11248 
11249 	}
11250 
11251 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11252 
11253 		/* Device (may) change: Drive -> Drive */
11254 		if (sdinfo == NULL) {
11255 			/*
11256 			 * There is some device attached, but there is
11257 			 * no sata_drive_info structure - allocate one
11258 			 */
11259 			mutex_exit(&cportinfo->cport_mutex);
11260 			sdinfo = kmem_zalloc(
11261 			    sizeof (sata_drive_info_t), KM_SLEEP);
11262 			mutex_enter(&cportinfo->cport_mutex);
11263 			/*
11264 			 * Recheck, that the port state did not change when we
11265 			 * released mutex.
11266 			 */
11267 			if (cportinfo->cport_state & SATA_STATE_READY) {
11268 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11269 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11270 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11271 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11272 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11273 			} else {
11274 				/*
11275 				 * Port is not in ready state, we
11276 				 * cannot attach a device.
11277 				 */
11278 				mutex_exit(&cportinfo->cport_mutex);
11279 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11280 				return (SATA_SUCCESS);
11281 			}
11282 			/*
11283 			 * Since we are adding device, presumably new one,
11284 			 * indicate that it  should be initalized,
11285 			 * as well as some internal framework states).
11286 			 */
11287 			init_device = B_TRUE;
11288 		}
11289 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11290 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11291 	} else {
11292 		/* Device change: Drive -> PMult */
11293 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11294 		if (sdinfo != NULL) {
11295 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11296 			sata_log(sata_hba_inst, CE_WARN,
11297 			    "SATA device detached "
11298 			    "from port %d", cportinfo->cport_addr.cport);
11299 		}
11300 
11301 		sata_log(sata_hba_inst, CE_WARN,
11302 		    "SATA port multiplier detected at port %d",
11303 		    cportinfo->cport_addr.cport);
11304 
11305 		mutex_exit(&cportinfo->cport_mutex);
11306 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11307 		    SATA_SUCCESS)
11308 			return (SATA_FAILURE);
11309 		sata_show_pmult_info(sata_hba_inst, sata_device);
11310 		mutex_enter(&cportinfo->cport_mutex);
11311 
11312 		/*
11313 		 * Mark all the port multiplier port behind the port
11314 		 * multiplier behind with link events, so that the sata daemon
11315 		 * will update their status.
11316 		 */
11317 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11318 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11319 		mutex_exit(&cportinfo->cport_mutex);
11320 		return (SATA_SUCCESS);
11321 	}
11322 	mutex_exit(&cportinfo->cport_mutex);
11323 
11324 	/*
11325 	 * Figure out what kind of device we are really
11326 	 * dealing with. Failure of identifying device does not fail this
11327 	 * function.
11328 	 */
11329 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11330 	rval_init = SATA_FAILURE;
11331 	mutex_enter(&cportinfo->cport_mutex);
11332 	if (rval_probe == SATA_SUCCESS) {
11333 		/*
11334 		 * If we are dealing with the same type of a device as before,
11335 		 * restore its settings flags.
11336 		 */
11337 		if (osdinfo != NULL &&
11338 		    sata_device->satadev_type == prev_device_type)
11339 			sdinfo->satadrv_settings = prev_device_settings;
11340 
11341 		mutex_exit(&cportinfo->cport_mutex);
11342 		rval_init = SATA_SUCCESS;
11343 		/* Set initial device features, if necessary */
11344 		if (init_device == B_TRUE) {
11345 			rval_init = sata_initialize_device(sata_hba_inst,
11346 			    sdinfo);
11347 		}
11348 		if (rval_init == SATA_SUCCESS)
11349 			return (rval_init);
11350 		/* else we will retry if retry was asked for */
11351 
11352 	} else {
11353 		/*
11354 		 * If there was some device info before we probe the device,
11355 		 * restore previous device setting, so we can retry from scratch
11356 		 * later. Providing, of course, that device has not disapear
11357 		 * during probing process.
11358 		 */
11359 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11360 			if (osdinfo != NULL) {
11361 				cportinfo->cport_dev_type = prev_device_type;
11362 				sdinfo->satadrv_type = prev_device_type;
11363 				sdinfo->satadrv_state = prev_device_state;
11364 			}
11365 		} else {
11366 			/* device is gone */
11367 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11368 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11369 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11370 			mutex_exit(&cportinfo->cport_mutex);
11371 			return (SATA_SUCCESS);
11372 		}
11373 		mutex_exit(&cportinfo->cport_mutex);
11374 	}
11375 
11376 	if (retry) {
11377 		clock_t cur_time = ddi_get_lbolt();
11378 		/*
11379 		 * A device was not successfully identified or initialized.
11380 		 * Track retry time for device identification.
11381 		 */
11382 		if ((cur_time - start_time) <
11383 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11384 			/* sleep for a while */
11385 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11386 			goto retry_probe;
11387 		}
11388 		/* else no more retries */
11389 		mutex_enter(&cportinfo->cport_mutex);
11390 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11391 			if (rval_init == SATA_RETRY) {
11392 				/*
11393 				 * Setting drive features have failed, but
11394 				 * because the drive is still accessible,
11395 				 * keep it and emit a warning message.
11396 				 */
11397 				sata_log(sata_hba_inst, CE_WARN,
11398 				    "SATA device at port %d - desired "
11399 				    "drive features could not be set. "
11400 				    "Device may not operate as expected.",
11401 				    cportinfo->cport_addr.cport);
11402 			} else {
11403 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11404 				    satadrv_state = SATA_DSTATE_FAILED;
11405 			}
11406 		}
11407 		mutex_exit(&cportinfo->cport_mutex);
11408 	}
11409 	return (SATA_SUCCESS);
11410 }
11411 
11412 /*
11413  * Reprobe a controller port that connected to a port multiplier.
11414  *
11415  * NOTE: No Mutex should be hold.
11416  */
11417 static int
11418 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11419     int flag)
11420 {
11421 	_NOTE(ARGUNUSED(flag))
11422 	sata_cport_info_t *cportinfo;
11423 	sata_pmult_info_t *pmultinfo;
11424 	uint8_t cport = sata_device->satadev_addr.cport;
11425 	int rval_probe;
11426 
11427 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11428 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11429 
11430 	/* probe port */
11431 	mutex_enter(&cportinfo->cport_mutex);
11432 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11433 	cportinfo->cport_state |= SATA_STATE_PROBING;
11434 	mutex_exit(&cportinfo->cport_mutex);
11435 
11436 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11437 	    (SATA_DIP(sata_hba_inst), sata_device);
11438 
11439 	mutex_enter(&cportinfo->cport_mutex);
11440 	if (rval_probe != SATA_SUCCESS) {
11441 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11442 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11443 		    "SATA port %d probing failed", cport));
11444 		sata_log(sata_hba_inst, CE_WARN,
11445 		    "SATA port multiplier detached at port %d", cport);
11446 		mutex_exit(&cportinfo->cport_mutex);
11447 		sata_free_pmult(sata_hba_inst, sata_device);
11448 		return (SATA_FAILURE);
11449 	}
11450 
11451 	/*
11452 	 * update sata port state and set device type
11453 	 */
11454 	sata_update_port_info(sata_hba_inst, sata_device);
11455 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11456 	cportinfo->cport_state |= SATA_STATE_PROBED;
11457 
11458 	/*
11459 	 * Sanity check - Port is active? Is the link active?
11460 	 * Is there any device attached?
11461 	 */
11462 	if ((cportinfo->cport_state &
11463 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11464 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11465 	    SATA_PORT_DEVLINK_UP ||
11466 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11467 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11468 		mutex_exit(&cportinfo->cport_mutex);
11469 		sata_free_pmult(sata_hba_inst, sata_device);
11470 		sata_log(sata_hba_inst, CE_WARN,
11471 		    "SATA port multiplier detached at port %d", cport);
11472 		return (SATA_SUCCESS);
11473 	}
11474 
11475 	/*
11476 	 * Device changed: PMult -> Non-PMult
11477 	 *
11478 	 * This situation is uncommon, most possibly being caused by errors
11479 	 * after which the port multiplier is not correct initialized and
11480 	 * recognized. In that case the new device will be marked as unknown
11481 	 * and will not be automatically probed in this routine. Instead
11482 	 * system administrator could manually restart it via cfgadm(1M).
11483 	 */
11484 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11485 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11486 		mutex_exit(&cportinfo->cport_mutex);
11487 		sata_free_pmult(sata_hba_inst, sata_device);
11488 		sata_log(sata_hba_inst, CE_WARN,
11489 		    "SATA port multiplier detached at port %d", cport);
11490 		return (SATA_FAILURE);
11491 	}
11492 
11493 	/*
11494 	 * Now we know it is a port multiplier. However, if this is not the
11495 	 * previously attached port multiplier - they may have different
11496 	 * pmport numbers - we need to re-allocate data structures for every
11497 	 * pmport and drive.
11498 	 *
11499 	 * Port multipliers of the same model have identical values in these
11500 	 * registers, so it is still necessary to update the information of
11501 	 * all drives attached to the previous port multiplier afterwards.
11502 	 */
11503 	/* Device changed: PMult -> another PMult */
11504 	mutex_exit(&cportinfo->cport_mutex);
11505 	sata_free_pmult(sata_hba_inst, sata_device);
11506 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11507 		return (SATA_FAILURE);
11508 	mutex_enter(&cportinfo->cport_mutex);
11509 
11510 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11511 	    "SATA port multiplier [changed] at port %d", cport);
11512 	sata_log(sata_hba_inst, CE_WARN,
11513 	    "SATA port multiplier detected at port %d", cport);
11514 
11515 	/*
11516 	 * Mark all the port multiplier port behind the port
11517 	 * multiplier behind with link events, so that the sata daemon
11518 	 * will update their status.
11519 	 */
11520 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11521 	mutex_exit(&cportinfo->cport_mutex);
11522 
11523 	return (SATA_SUCCESS);
11524 }
11525 
11526 /*
11527  * Re-probe a port multiplier port, check for a device and attach info
11528  * structures when necessary. Identify Device data is fetched, if possible.
11529  * Assumption: sata address is already validated as port multiplier port.
11530  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11531  * the presence of a device and its type.
11532  *
11533  * flag arg specifies that the function should try multiple times to identify
11534  * device type and to initialize it, or it should return immediately on failure.
11535  * SATA_DEV_IDENTIFY_RETRY - retry
11536  * SATA_DEV_IDENTIFY_NORETRY - no retry
11537  *
11538  * SATA_FAILURE is returned if one of the operations failed.
11539  *
11540  * This function cannot be called in interrupt context - it may sleep.
11541  *
11542  * NOTE: Should be only called by sata_probe_port() in case target port is a
11543  *       port multiplier port.
11544  * NOTE: No Mutex should be hold.
11545  */
11546 static int
11547 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11548     int flag)
11549 {
11550 	sata_cport_info_t *cportinfo = NULL;
11551 	sata_pmport_info_t *pmportinfo = NULL;
11552 	sata_drive_info_t *sdinfo, *osdinfo;
11553 	sata_device_t sdevice;
11554 	boolean_t init_device = B_FALSE;
11555 	int prev_device_type = SATA_DTYPE_NONE;
11556 	int prev_device_settings = 0;
11557 	int prev_device_state = 0;
11558 	clock_t start_time;
11559 	uint8_t cport = sata_device->satadev_addr.cport;
11560 	uint8_t pmport = sata_device->satadev_addr.pmport;
11561 	int rval;
11562 
11563 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11564 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11565 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11566 
11567 	if (osdinfo != NULL) {
11568 		/*
11569 		 * We are re-probing port with a previously attached device.
11570 		 * Save previous device type and settings.
11571 		 */
11572 		prev_device_type = pmportinfo->pmport_dev_type;
11573 		prev_device_settings = osdinfo->satadrv_settings;
11574 		prev_device_state = osdinfo->satadrv_state;
11575 	}
11576 
11577 	start_time = ddi_get_lbolt();
11578 
11579 	/* check parent status */
11580 	mutex_enter(&cportinfo->cport_mutex);
11581 	if ((cportinfo->cport_state &
11582 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11583 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11584 	    SATA_PORT_DEVLINK_UP) {
11585 		mutex_exit(&cportinfo->cport_mutex);
11586 		return (SATA_FAILURE);
11587 	}
11588 	mutex_exit(&cportinfo->cport_mutex);
11589 
11590 retry_probe_pmport:
11591 
11592 	/* probe port */
11593 	mutex_enter(&pmportinfo->pmport_mutex);
11594 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11595 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11596 	mutex_exit(&pmportinfo->pmport_mutex);
11597 
11598 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11599 	    (SATA_DIP(sata_hba_inst), sata_device);
11600 
11601 	/* might need retry because we cannot touch registers. */
11602 	if (rval == SATA_FAILURE) {
11603 		mutex_enter(&pmportinfo->pmport_mutex);
11604 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11605 		mutex_exit(&pmportinfo->pmport_mutex);
11606 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11607 		    "SATA port %d:%d probing failed",
11608 		    cport, pmport));
11609 		return (SATA_FAILURE);
11610 	} else if (rval == SATA_RETRY) {
11611 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11612 		    "SATA port %d:%d probing failed, retrying...",
11613 		    cport, pmport));
11614 		clock_t cur_time = ddi_get_lbolt();
11615 		/*
11616 		 * A device was not successfully identified or initialized.
11617 		 * Track retry time for device identification.
11618 		 */
11619 		if ((cur_time - start_time) <
11620 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11621 			/* sleep for a while */
11622 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11623 			goto retry_probe_pmport;
11624 		} else {
11625 			mutex_enter(&pmportinfo->pmport_mutex);
11626 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11627 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11628 				    satadrv_state = SATA_DSTATE_FAILED;
11629 			mutex_exit(&pmportinfo->pmport_mutex);
11630 			return (SATA_SUCCESS);
11631 		}
11632 	}
11633 
11634 	/*
11635 	 * Sanity check - Controller port is active? Is the link active?
11636 	 * Is it still a port multiplier?
11637 	 */
11638 	if ((cportinfo->cport_state &
11639 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11640 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11641 	    SATA_PORT_DEVLINK_UP ||
11642 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11643 		/*
11644 		 * Port in non-usable state or no link active/no
11645 		 * device. Free info structure.
11646 		 */
11647 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11648 
11649 		sdevice.satadev_addr.cport = cport;
11650 		sdevice.satadev_addr.pmport = pmport;
11651 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11652 		mutex_exit(&cportinfo->cport_mutex);
11653 
11654 		sata_free_pmult(sata_hba_inst, &sdevice);
11655 		return (SATA_FAILURE);
11656 	}
11657 
11658 	/* SATA_SUCCESS NOW */
11659 	/*
11660 	 * update sata port state and set device type
11661 	 */
11662 	mutex_enter(&pmportinfo->pmport_mutex);
11663 	sata_update_pmport_info(sata_hba_inst, sata_device);
11664 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11665 
11666 	/*
11667 	 * Sanity check - Port is active? Is the link active?
11668 	 * Is there any device attached?
11669 	 */
11670 	if ((pmportinfo->pmport_state &
11671 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11672 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11673 	    SATA_PORT_DEVLINK_UP) {
11674 		/*
11675 		 * Port in non-usable state or no link active/no device.
11676 		 * Free info structure if necessary (direct attached drive
11677 		 * only, for now!
11678 		 */
11679 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11680 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11681 		/* Add here differentiation for device attached or not */
11682 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11683 		mutex_exit(&pmportinfo->pmport_mutex);
11684 		if (sdinfo != NULL)
11685 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11686 		return (SATA_SUCCESS);
11687 	}
11688 
11689 	pmportinfo->pmport_state |= SATA_STATE_READY;
11690 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11691 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11692 
11693 	/*
11694 	 * If we are re-probing the port, there may be
11695 	 * sata_drive_info structure attached
11696 	 * (or sata_pm_info, if PMult is supported).
11697 	 */
11698 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11699 		/*
11700 		 * There is no device, so remove device info structure,
11701 		 * if necessary.
11702 		 */
11703 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11704 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11705 		if (sdinfo != NULL) {
11706 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11707 			sata_log(sata_hba_inst, CE_WARN,
11708 			    "SATA device detached from port %d:%d",
11709 			    cport, pmport);
11710 		}
11711 		mutex_exit(&pmportinfo->pmport_mutex);
11712 		return (SATA_SUCCESS);
11713 	}
11714 
11715 	/* this should not be a pmult */
11716 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11717 	if (sdinfo == NULL) {
11718 		/*
11719 		 * There is some device attached, but there is
11720 		 * no sata_drive_info structure - allocate one
11721 		 */
11722 		mutex_exit(&pmportinfo->pmport_mutex);
11723 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11724 		    KM_SLEEP);
11725 		mutex_enter(&pmportinfo->pmport_mutex);
11726 		/*
11727 		 * Recheck, that the port state did not change when we
11728 		 * released mutex.
11729 		 */
11730 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11731 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11732 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11733 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11734 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11735 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11736 		} else {
11737 			/*
11738 			 * Port is not in ready state, we
11739 			 * cannot attach a device.
11740 			 */
11741 			mutex_exit(&pmportinfo->pmport_mutex);
11742 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11743 			return (SATA_SUCCESS);
11744 		}
11745 		/*
11746 		 * Since we are adding device, presumably new one,
11747 		 * indicate that it  should be initalized,
11748 		 * as well as some internal framework states).
11749 		 */
11750 		init_device = B_TRUE;
11751 	}
11752 
11753 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11754 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11755 
11756 	mutex_exit(&pmportinfo->pmport_mutex);
11757 	/*
11758 	 * Figure out what kind of device we are really
11759 	 * dealing with.
11760 	 */
11761 	rval = sata_probe_device(sata_hba_inst, sata_device);
11762 
11763 	mutex_enter(&pmportinfo->pmport_mutex);
11764 	if (rval == SATA_SUCCESS) {
11765 		/*
11766 		 * If we are dealing with the same type of a device as before,
11767 		 * restore its settings flags.
11768 		 */
11769 		if (osdinfo != NULL &&
11770 		    sata_device->satadev_type == prev_device_type)
11771 			sdinfo->satadrv_settings = prev_device_settings;
11772 
11773 		mutex_exit(&pmportinfo->pmport_mutex);
11774 		/* Set initial device features, if necessary */
11775 		if (init_device == B_TRUE) {
11776 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11777 		}
11778 		if (rval == SATA_SUCCESS)
11779 			return (rval);
11780 	} else {
11781 		/*
11782 		 * If there was some device info before we probe the device,
11783 		 * restore previous device setting, so we can retry from scratch
11784 		 * later. Providing, of course, that device has not disappeared
11785 		 * during probing process.
11786 		 */
11787 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11788 			if (osdinfo != NULL) {
11789 				pmportinfo->pmport_dev_type = prev_device_type;
11790 				sdinfo->satadrv_type = prev_device_type;
11791 				sdinfo->satadrv_state = prev_device_state;
11792 			}
11793 		} else {
11794 			/* device is gone */
11795 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11796 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11797 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11798 			mutex_exit(&pmportinfo->pmport_mutex);
11799 			return (SATA_SUCCESS);
11800 		}
11801 		mutex_exit(&pmportinfo->pmport_mutex);
11802 	}
11803 
11804 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11805 		clock_t cur_time = ddi_get_lbolt();
11806 		/*
11807 		 * A device was not successfully identified or initialized.
11808 		 * Track retry time for device identification.
11809 		 */
11810 		if ((cur_time - start_time) <
11811 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11812 			/* sleep for a while */
11813 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11814 			goto retry_probe_pmport;
11815 		} else {
11816 			mutex_enter(&pmportinfo->pmport_mutex);
11817 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11818 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11819 				    satadrv_state = SATA_DSTATE_FAILED;
11820 			mutex_exit(&pmportinfo->pmport_mutex);
11821 		}
11822 	}
11823 	return (SATA_SUCCESS);
11824 }
11825 
11826 /*
11827  * Allocated related structure for a port multiplier and its device ports
11828  *
11829  * Port multiplier should be ready and probed, and related information like
11830  * the number of the device ports should be store in sata_device_t.
11831  *
11832  * NOTE: No Mutex should be hold.
11833  */
11834 static int
11835 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11836 {
11837 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11838 	sata_cport_info_t *cportinfo = NULL;
11839 	sata_pmult_info_t *pmultinfo = NULL;
11840 	sata_pmport_info_t *pmportinfo = NULL;
11841 	sata_device_t sd;
11842 	dev_t minor_number;
11843 	char name[16];
11844 	uint8_t cport = sata_device->satadev_addr.cport;
11845 	int rval;
11846 	int npmport;
11847 
11848 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11849 
11850 	/* This function might be called while a port-mult is hot-plugged. */
11851 	mutex_enter(&cportinfo->cport_mutex);
11852 
11853 	/* dev_type's not updated when get called from sata_reprobe_port() */
11854 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11855 		/* Create a pmult_info structure */
11856 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11857 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11858 	}
11859 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11860 
11861 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11862 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11863 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11864 
11865 	/*
11866 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11867 	 * The HBA driver should initialize and register the port multiplier,
11868 	 * sata_register_pmult() will fill following fields,
11869 	 *   + sata_pmult_info.pmult_gscr
11870 	 *   + sata_pmult_info.pmult_num_dev_ports
11871 	 */
11872 	sd.satadev_addr = sata_device->satadev_addr;
11873 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11874 	mutex_exit(&cportinfo->cport_mutex);
11875 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11876 	    (SATA_DIP(sata_hba_inst), &sd);
11877 	mutex_enter(&cportinfo->cport_mutex);
11878 
11879 	if (rval != SATA_SUCCESS ||
11880 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11881 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11882 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11883 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11884 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11885 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11886 		mutex_exit(&cportinfo->cport_mutex);
11887 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11888 		    "sata_alloc_pmult: failed to initialize pmult "
11889 		    "at port %d.", cport)
11890 		return (SATA_FAILURE);
11891 	}
11892 
11893 	/* Initialize pmport_info structure */
11894 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11895 	    npmport++) {
11896 
11897 		/* if everything is allocated, skip */
11898 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11899 			continue;
11900 
11901 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11902 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11903 		mutex_exit(&cportinfo->cport_mutex);
11904 
11905 		mutex_enter(&pmportinfo->pmport_mutex);
11906 		pmportinfo->pmport_addr.cport = cport;
11907 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11908 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11909 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11910 		mutex_exit(&pmportinfo->pmport_mutex);
11911 
11912 		mutex_enter(&cportinfo->cport_mutex);
11913 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11914 
11915 		/* Create an attachment point */
11916 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11917 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11918 		(void) sprintf(name, "%d.%d", cport, npmport);
11919 
11920 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11921 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11922 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11923 			    "cannot create SATA attachment point for "
11924 			    "port %d:%d", cport, npmport);
11925 		}
11926 	}
11927 
11928 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11929 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11930 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11931 
11932 	mutex_exit(&cportinfo->cport_mutex);
11933 	return (SATA_SUCCESS);
11934 }
11935 
11936 /*
11937  * Free data structures when a port multiplier is removed.
11938  *
11939  * NOTE: No Mutex should be hold.
11940  */
11941 static void
11942 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11943 {
11944 	sata_cport_info_t *cportinfo;
11945 	sata_pmult_info_t *pmultinfo;
11946 	sata_pmport_info_t *pmportinfo;
11947 	sata_device_t pmport_device;
11948 	sata_drive_info_t *sdinfo;
11949 	dev_info_t *tdip;
11950 	char name[16];
11951 	uint8_t cport = sata_device->satadev_addr.cport;
11952 	int npmport;
11953 
11954 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11955 
11956 	/* This function might be called while port-mult is hot plugged. */
11957 	mutex_enter(&cportinfo->cport_mutex);
11958 
11959 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11960 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11961 	ASSERT(pmultinfo != NULL);
11962 
11963 	/* Free pmport_info structure */
11964 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11965 	    npmport++) {
11966 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
11967 		if (pmportinfo == NULL)
11968 			continue;
11969 		mutex_exit(&cportinfo->cport_mutex);
11970 
11971 		mutex_enter(&pmportinfo->pmport_mutex);
11972 		sdinfo = pmportinfo->pmport_sata_drive;
11973 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11974 		mutex_exit(&pmportinfo->pmport_mutex);
11975 
11976 		/* Remove attachment point. */
11977 		name[0] = '\0';
11978 		(void) sprintf(name, "%d.%d", cport, npmport);
11979 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
11980 		sata_log(sata_hba_inst, CE_NOTE,
11981 		    "Remove attachment point of port %d:%d",
11982 		    cport, npmport);
11983 
11984 		/*
11985 		 * Rumove target node
11986 		 */
11987 		bzero(&pmport_device, sizeof (sata_device_t));
11988 		pmport_device.satadev_rev = SATA_DEVICE_REV;
11989 		pmport_device.satadev_addr.cport = cport;
11990 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
11991 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
11992 
11993 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11994 		    &(pmport_device.satadev_addr));
11995 		if (tdip != NULL && ndi_devi_offline(tdip,
11996 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11997 			/*
11998 			 * Problem :
11999 			 * The target node remained attached.
12000 			 * This happens when the device file was open
12001 			 * or a node was waiting for resources.
12002 			 * Cannot do anything about it.
12003 			 */
12004 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12005 			    "sata_free_pmult: could not unconfigure device "
12006 			    "before disconnecting the SATA port %d:%d",
12007 			    cport, npmport));
12008 
12009 			/*
12010 			 * Set DEVICE REMOVED state in the target
12011 			 * node. It will prevent access to the device
12012 			 * even when a new device is attached, until
12013 			 * the old target node is released, removed and
12014 			 * recreated for a new  device.
12015 			 */
12016 			sata_set_device_removed(tdip);
12017 
12018 			/*
12019 			 * Instruct event daemon to try the target
12020 			 * node cleanup later.
12021 			 */
12022 			sata_set_target_node_cleanup(
12023 			    sata_hba_inst, &(pmport_device.satadev_addr));
12024 
12025 		}
12026 		mutex_enter(&cportinfo->cport_mutex);
12027 
12028 		/*
12029 		 * Add here differentiation for device attached or not
12030 		 */
12031 		if (sdinfo != NULL)  {
12032 			sata_log(sata_hba_inst, CE_WARN,
12033 			    "SATA device detached from port %d:%d",
12034 			    cport, npmport);
12035 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12036 		}
12037 
12038 		mutex_destroy(&pmportinfo->pmport_mutex);
12039 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12040 	}
12041 
12042 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12043 
12044 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12045 
12046 	sata_log(sata_hba_inst, CE_WARN,
12047 	    "SATA port multiplier detached at port %d", cport);
12048 
12049 	mutex_exit(&cportinfo->cport_mutex);
12050 }
12051 
12052 /*
12053  * Initialize device
12054  * Specified device is initialized to a default state.
12055  *
12056  * Returns SATA_SUCCESS if all device features are set successfully,
12057  * SATA_RETRY if device is accessible but device features were not set
12058  * successfully, and SATA_FAILURE otherwise.
12059  */
12060 static int
12061 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12062     sata_drive_info_t *sdinfo)
12063 {
12064 	int rval;
12065 
12066 	sata_save_drive_settings(sdinfo);
12067 
12068 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12069 
12070 	sata_init_write_cache_mode(sdinfo);
12071 
12072 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12073 
12074 	/* Determine current data transfer mode */
12075 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12076 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12077 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12078 	    SATA_VALIDINFO_88) != 0 &&
12079 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12080 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12081 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12082 	    SATA_MDMA_SEL_MASK) != 0) {
12083 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12084 	} else
12085 		/* DMA supported, not no DMA transfer mode is selected !? */
12086 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12087 
12088 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12089 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12090 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12091 	else
12092 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12093 
12094 	return (rval);
12095 }
12096 
12097 
12098 /*
12099  * Initialize write cache mode.
12100  *
12101  * The default write cache setting for SATA HDD is provided by sata_write_cache
12102  * static variable. ATAPI CD/DVDs devices have write cache default is
12103  * determined by sata_atapicdvd_write_cache static variable.
12104  * ATAPI tape devices have write cache default is determined by
12105  * sata_atapitape_write_cache static variable.
12106  * ATAPI disk devices have write cache default is determined by
12107  * sata_atapidisk_write_cache static variable.
12108  * 1 - enable
12109  * 0 - disable
12110  * any other value - current drive setting
12111  *
12112  * Although there is not reason to disable write cache on CD/DVD devices,
12113  * tape devices and ATAPI disk devices, the default setting control is provided
12114  * for the maximun flexibility.
12115  *
12116  * In the future, it may be overridden by the
12117  * disk-write-cache-enable property setting, if it is defined.
12118  * Returns SATA_SUCCESS if all device features are set successfully,
12119  * SATA_FAILURE otherwise.
12120  */
12121 static void
12122 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12123 {
12124 	switch (sdinfo->satadrv_type) {
12125 	case SATA_DTYPE_ATADISK:
12126 		if (sata_write_cache == 1)
12127 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12128 		else if (sata_write_cache == 0)
12129 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12130 		/*
12131 		 * When sata_write_cache value is not 0 or 1,
12132 		 * a current setting of the drive's write cache is used.
12133 		 */
12134 		break;
12135 	case SATA_DTYPE_ATAPICD:
12136 		if (sata_atapicdvd_write_cache == 1)
12137 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12138 		else if (sata_atapicdvd_write_cache == 0)
12139 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12140 		/*
12141 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12142 		 * a current setting of the drive's write cache is used.
12143 		 */
12144 		break;
12145 	case SATA_DTYPE_ATAPITAPE:
12146 		if (sata_atapitape_write_cache == 1)
12147 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12148 		else if (sata_atapitape_write_cache == 0)
12149 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12150 		/*
12151 		 * When sata_atapitape_write_cache value is not 0 or 1,
12152 		 * a current setting of the drive's write cache is used.
12153 		 */
12154 		break;
12155 	case SATA_DTYPE_ATAPIDISK:
12156 		if (sata_atapidisk_write_cache == 1)
12157 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12158 		else if (sata_atapidisk_write_cache == 0)
12159 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12160 		/*
12161 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12162 		 * a current setting of the drive's write cache is used.
12163 		 */
12164 		break;
12165 	}
12166 }
12167 
12168 
12169 /*
12170  * Validate sata address.
12171  * Specified cport, pmport and qualifier has to match
12172  * passed sata_scsi configuration info.
12173  * The presence of an attached device is not verified.
12174  *
12175  * Returns 0 when address is valid, -1 otherwise.
12176  */
12177 static int
12178 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12179 	int pmport, int qual)
12180 {
12181 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12182 		goto invalid_address;
12183 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12184 		goto invalid_address;
12185 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12186 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12187 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12188 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12189 		goto invalid_address;
12190 
12191 	return (0);
12192 
12193 invalid_address:
12194 	return (-1);
12195 
12196 }
12197 
12198 /*
12199  * Validate scsi address
12200  * SCSI target address is translated into SATA cport/pmport and compared
12201  * with a controller port/device configuration. LUN has to be 0.
12202  * Returns 0 if a scsi target refers to an attached device,
12203  * returns 1 if address is valid but no valid device is attached,
12204  * returns 2 if address is valid but device type is unknown (not valid device),
12205  * returns -1 if bad address or device is of an unsupported type.
12206  * Upon return sata_device argument is set.
12207  *
12208  * Port multiplier is supported now.
12209  */
12210 static int
12211 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12212 	struct scsi_address *ap, sata_device_t *sata_device)
12213 {
12214 	int cport, pmport, qual, rval;
12215 
12216 	rval = -1;	/* Invalid address */
12217 	if (ap->a_lun != 0)
12218 		goto out;
12219 
12220 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12221 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12222 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12223 
12224 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12225 		goto out;
12226 
12227 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12228 	    0) {
12229 
12230 		sata_cport_info_t *cportinfo;
12231 		sata_pmult_info_t *pmultinfo;
12232 		sata_drive_info_t *sdinfo = NULL;
12233 
12234 		sata_device->satadev_addr.qual = qual;
12235 		sata_device->satadev_addr.cport = cport;
12236 		sata_device->satadev_addr.pmport = pmport;
12237 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12238 
12239 		rval = 1;	/* Valid sata address */
12240 
12241 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12242 		if (qual == SATA_ADDR_DCPORT) {
12243 			if (cportinfo == NULL ||
12244 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12245 				goto out;
12246 
12247 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12248 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12249 			    sdinfo != NULL) {
12250 				rval = 2;
12251 				goto out;
12252 			}
12253 
12254 			if ((cportinfo->cport_dev_type &
12255 			    SATA_VALID_DEV_TYPE) == 0) {
12256 				rval = -1;
12257 				goto out;
12258 			}
12259 
12260 		} else if (qual == SATA_ADDR_DPMPORT) {
12261 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12262 			if (pmultinfo == NULL) {
12263 				rval = -1;
12264 				goto out;
12265 			}
12266 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12267 			    NULL ||
12268 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12269 			    pmport) == SATA_DTYPE_NONE)
12270 				goto out;
12271 
12272 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12273 			    pmport);
12274 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12275 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12276 				rval = 2;
12277 				goto out;
12278 			}
12279 
12280 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12281 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12282 				rval = -1;
12283 				goto out;
12284 			}
12285 
12286 		} else {
12287 			rval = -1;
12288 			goto out;
12289 		}
12290 		if ((sdinfo == NULL) ||
12291 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12292 			goto out;
12293 
12294 		sata_device->satadev_type = sdinfo->satadrv_type;
12295 
12296 		return (0);
12297 	}
12298 out:
12299 	if (rval > 0) {
12300 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12301 		    "sata_validate_scsi_address: no valid target %x lun %x",
12302 		    ap->a_target, ap->a_lun);
12303 	}
12304 	return (rval);
12305 }
12306 
12307 /*
12308  * Find dip corresponding to passed device number
12309  *
12310  * Returns NULL if invalid device number is passed or device cannot be found,
12311  * Returns dip is device is found.
12312  */
12313 static dev_info_t *
12314 sata_devt_to_devinfo(dev_t dev)
12315 {
12316 	dev_info_t *dip;
12317 #ifndef __lock_lint
12318 	struct devnames *dnp;
12319 	major_t major = getmajor(dev);
12320 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12321 
12322 	if (major >= devcnt)
12323 		return (NULL);
12324 
12325 	dnp = &devnamesp[major];
12326 	LOCK_DEV_OPS(&(dnp->dn_lock));
12327 	dip = dnp->dn_head;
12328 	while (dip && (ddi_get_instance(dip) != instance)) {
12329 		dip = ddi_get_next(dip);
12330 	}
12331 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12332 #endif
12333 
12334 	return (dip);
12335 }
12336 
12337 
12338 /*
12339  * Probe device.
12340  * This function issues Identify Device command and initializes local
12341  * sata_drive_info structure if the device can be identified.
12342  * The device type is determined by examining Identify Device
12343  * command response.
12344  * If the sata_hba_inst has linked drive info structure for this
12345  * device address, the Identify Device data is stored into sata_drive_info
12346  * structure linked to the port info structure.
12347  *
12348  * sata_device has to refer to the valid sata port(s) for HBA described
12349  * by sata_hba_inst structure.
12350  *
12351  * Returns:
12352  *	SATA_SUCCESS if device type was successfully probed and port-linked
12353  *		drive info structure was updated;
12354  * 	SATA_FAILURE if there is no device, or device was not probed
12355  *		successully;
12356  *	SATA_RETRY if device probe can be retried later.
12357  * If a device cannot be identified, sata_device's dev_state and dev_type
12358  * fields are set to unknown.
12359  * There are no retries in this function. Any retries should be managed by
12360  * the caller.
12361  */
12362 
12363 
12364 static int
12365 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12366 {
12367 	sata_pmport_info_t *pmportinfo;
12368 	sata_drive_info_t *sdinfo;
12369 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12370 	int rval;
12371 
12372 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12373 	    sata_device->satadev_addr.cport) &
12374 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12375 
12376 	sata_device->satadev_type = SATA_DTYPE_NONE;
12377 
12378 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12379 	    sata_device->satadev_addr.cport)));
12380 
12381 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12382 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12383 		    sata_device->satadev_addr.cport,
12384 		    sata_device->satadev_addr.pmport);
12385 		ASSERT(pmportinfo != NULL);
12386 	}
12387 
12388 	/* Get pointer to port-linked sata device info structure */
12389 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12390 	if (sdinfo != NULL) {
12391 		sdinfo->satadrv_state &=
12392 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12393 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12394 	} else {
12395 		/* No device to probe */
12396 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12397 		    sata_device->satadev_addr.cport)));
12398 		sata_device->satadev_type = SATA_DTYPE_NONE;
12399 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12400 		return (SATA_FAILURE);
12401 	}
12402 	/*
12403 	 * Need to issue both types of identify device command and
12404 	 * determine device type by examining retreived data/status.
12405 	 * First, ATA Identify Device.
12406 	 */
12407 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12408 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12409 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12410 	    sata_device->satadev_addr.cport)));
12411 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12412 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12413 	if (rval == SATA_RETRY) {
12414 		/* We may try to check for ATAPI device */
12415 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12416 			/*
12417 			 * HBA supports ATAPI - try to issue Identify Packet
12418 			 * Device command.
12419 			 */
12420 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12421 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12422 		}
12423 	}
12424 	if (rval == SATA_SUCCESS) {
12425 		/*
12426 		 * Got something responding positively to ATA Identify Device
12427 		 * or to Identify Packet Device cmd.
12428 		 * Save last used device type.
12429 		 */
12430 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12431 
12432 		/* save device info, if possible */
12433 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12434 		    sata_device->satadev_addr.cport)));
12435 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12436 		if (sdinfo == NULL) {
12437 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12438 			    sata_device->satadev_addr.cport)));
12439 			return (SATA_FAILURE);
12440 		}
12441 		/*
12442 		 * Copy drive info into the port-linked drive info structure.
12443 		 */
12444 		*sdinfo = new_sdinfo;
12445 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12446 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12447 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12448 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12449 			    sata_device->satadev_addr.cport) =
12450 			    sdinfo->satadrv_type;
12451 		else { /* SATA_ADDR_DPMPORT */
12452 			mutex_enter(&pmportinfo->pmport_mutex);
12453 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12454 			    sata_device->satadev_addr.cport,
12455 			    sata_device->satadev_addr.pmport) =
12456 			    sdinfo->satadrv_type;
12457 			mutex_exit(&pmportinfo->pmport_mutex);
12458 		}
12459 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12460 		    sata_device->satadev_addr.cport)));
12461 		return (SATA_SUCCESS);
12462 	}
12463 
12464 	/*
12465 	 * It may be SATA_RETRY or SATA_FAILURE return.
12466 	 * Looks like we cannot determine the device type at this time.
12467 	 */
12468 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12469 	    sata_device->satadev_addr.cport)));
12470 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12471 	if (sdinfo != NULL) {
12472 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12473 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12474 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12475 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12476 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12477 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12478 			    sata_device->satadev_addr.cport) =
12479 			    SATA_DTYPE_UNKNOWN;
12480 		else {
12481 			/* SATA_ADDR_DPMPORT */
12482 			mutex_enter(&pmportinfo->pmport_mutex);
12483 			if ((SATA_PMULT_INFO(sata_hba_inst,
12484 			    sata_device->satadev_addr.cport) != NULL) &&
12485 			    (SATA_PMPORT_INFO(sata_hba_inst,
12486 			    sata_device->satadev_addr.cport,
12487 			    sata_device->satadev_addr.pmport) != NULL))
12488 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12489 				    sata_device->satadev_addr.cport,
12490 				    sata_device->satadev_addr.pmport) =
12491 				    SATA_DTYPE_UNKNOWN;
12492 			mutex_exit(&pmportinfo->pmport_mutex);
12493 		}
12494 	}
12495 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12496 	    sata_device->satadev_addr.cport)));
12497 	return (rval);
12498 }
12499 
12500 
12501 /*
12502  * Get pointer to sata_drive_info structure.
12503  *
12504  * The sata_device has to contain address (cport, pmport and qualifier) for
12505  * specified sata_scsi structure.
12506  *
12507  * Returns NULL if device address is not valid for this HBA configuration.
12508  * Otherwise, returns a pointer to sata_drive_info structure.
12509  *
12510  * This function should be called with a port mutex held.
12511  */
12512 static sata_drive_info_t *
12513 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12514     sata_device_t *sata_device)
12515 {
12516 	uint8_t cport = sata_device->satadev_addr.cport;
12517 	uint8_t pmport = sata_device->satadev_addr.pmport;
12518 	uint8_t qual = sata_device->satadev_addr.qual;
12519 
12520 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12521 		return (NULL);
12522 
12523 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12524 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12525 		/* Port not probed yet */
12526 		return (NULL);
12527 
12528 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12529 		return (NULL);
12530 
12531 	if (qual == SATA_ADDR_DCPORT) {
12532 		/* Request for a device on a controller port */
12533 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12534 		    SATA_DTYPE_PMULT)
12535 			/* Port multiplier attached */
12536 			return (NULL);
12537 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12538 	}
12539 	if (qual == SATA_ADDR_DPMPORT) {
12540 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12541 		    SATA_DTYPE_PMULT)
12542 			return (NULL);
12543 
12544 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12545 			return (NULL);
12546 
12547 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12548 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12549 			/* Port multiplier port not probed yet */
12550 			return (NULL);
12551 
12552 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12553 	}
12554 
12555 	/* we should not get here */
12556 	return (NULL);
12557 }
12558 
12559 
12560 /*
12561  * sata_identify_device.
12562  * Send Identify Device command to SATA HBA driver.
12563  * If command executes successfully, update sata_drive_info structure pointed
12564  * to by sdinfo argument, including Identify Device data.
12565  * If command fails, invalidate data in sata_drive_info.
12566  *
12567  * Cannot be called from interrupt level.
12568  *
12569  * Returns:
12570  * SATA_SUCCESS if the device was identified as a supported device,
12571  * SATA_RETRY if the device was not identified but could be retried,
12572  * SATA_FAILURE if the device was not identified and identify attempt
12573  *	should not be retried.
12574  */
12575 static int
12576 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12577     sata_drive_info_t *sdinfo)
12578 {
12579 	uint16_t cfg_word;
12580 	int rval;
12581 
12582 	/* fetch device identify data */
12583 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12584 	    sdinfo)) != SATA_SUCCESS)
12585 		goto fail_unknown;
12586 
12587 	cfg_word = sdinfo->satadrv_id.ai_config;
12588 
12589 	/* Set the correct device type */
12590 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12591 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12592 	} else if (cfg_word == SATA_CFA_TYPE) {
12593 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12594 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12595 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12596 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12597 		case SATA_ATAPI_CDROM_DEV:
12598 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12599 			break;
12600 		case SATA_ATAPI_SQACC_DEV:
12601 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12602 			break;
12603 		case SATA_ATAPI_DIRACC_DEV:
12604 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12605 			break;
12606 		case SATA_ATAPI_PROC_DEV:
12607 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12608 			break;
12609 		default:
12610 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12611 		}
12612 	} else {
12613 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12614 	}
12615 
12616 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12617 		if (sdinfo->satadrv_capacity == 0) {
12618 			/* Non-LBA disk. Too bad... */
12619 			sata_log(sata_hba_inst, CE_WARN,
12620 			    "SATA disk device at port %d does not support LBA",
12621 			    sdinfo->satadrv_addr.cport);
12622 			rval = SATA_FAILURE;
12623 			goto fail_unknown;
12624 		}
12625 	}
12626 #if 0
12627 	/* Left for historical reason */
12628 	/*
12629 	 * Some initial version of SATA spec indicated that at least
12630 	 * UDMA mode 4 has to be supported. It is not metioned in
12631 	 * SerialATA 2.6, so this restriction is removed.
12632 	 */
12633 	/* Check for Ultra DMA modes 6 through 0 being supported */
12634 	for (i = 6; i >= 0; --i) {
12635 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12636 			break;
12637 	}
12638 
12639 	/*
12640 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12641 	 * higher are not supported by the device, fail this
12642 	 * device.
12643 	 */
12644 	if (i < 4) {
12645 		/* No required Ultra DMA mode supported */
12646 		sata_log(sata_hba_inst, CE_WARN,
12647 		    "SATA disk device at port %d does not support UDMA "
12648 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12649 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12650 		    "mode 4 or higher required, %d supported", i));
12651 		rval = SATA_FAILURE;
12652 		goto fail_unknown;
12653 	}
12654 #endif
12655 
12656 	/*
12657 	 * For Disk devices, if it doesn't support UDMA mode, we would
12658 	 * like to return failure directly.
12659 	 */
12660 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12661 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12662 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12663 		sata_log(sata_hba_inst, CE_WARN,
12664 		    "SATA disk device at port %d does not support UDMA",
12665 		    sdinfo->satadrv_addr.cport);
12666 		rval = SATA_FAILURE;
12667 		goto fail_unknown;
12668 	}
12669 
12670 	return (SATA_SUCCESS);
12671 
12672 fail_unknown:
12673 	/* Invalidate sata_drive_info ? */
12674 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12675 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12676 	return (rval);
12677 }
12678 
12679 /*
12680  * Log/display device information
12681  */
12682 static void
12683 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12684     sata_drive_info_t *sdinfo)
12685 {
12686 	int valid_version;
12687 	char msg_buf[MAXPATHLEN];
12688 	int i;
12689 
12690 	/* Show HBA path */
12691 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12692 
12693 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12694 
12695 	switch (sdinfo->satadrv_type) {
12696 	case SATA_DTYPE_ATADISK:
12697 		(void) sprintf(msg_buf, "SATA disk device at");
12698 		break;
12699 
12700 	case SATA_DTYPE_ATAPICD:
12701 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12702 		break;
12703 
12704 	case SATA_DTYPE_ATAPITAPE:
12705 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12706 		break;
12707 
12708 	case SATA_DTYPE_ATAPIDISK:
12709 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12710 		break;
12711 
12712 	case SATA_DTYPE_ATAPIPROC:
12713 		(void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12714 		break;
12715 
12716 	case SATA_DTYPE_UNKNOWN:
12717 		(void) sprintf(msg_buf,
12718 		    "Unsupported SATA device type (cfg 0x%x) at ",
12719 		    sdinfo->satadrv_id.ai_config);
12720 		break;
12721 	}
12722 
12723 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12724 		cmn_err(CE_CONT, "?\t%s port %d\n",
12725 		    msg_buf, sdinfo->satadrv_addr.cport);
12726 	else
12727 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12728 		    msg_buf, sdinfo->satadrv_addr.cport,
12729 		    sdinfo->satadrv_addr.pmport);
12730 
12731 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12732 	    sizeof (sdinfo->satadrv_id.ai_model));
12733 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12734 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12735 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12736 
12737 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12738 	    sizeof (sdinfo->satadrv_id.ai_fw));
12739 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12740 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12741 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12742 
12743 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12744 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12745 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12746 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12747 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12748 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12749 	} else {
12750 		/*
12751 		 * Some drives do not implement serial number and may
12752 		 * violate the spec by providing spaces rather than zeros
12753 		 * in serial number field. Scan the buffer to detect it.
12754 		 */
12755 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12756 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12757 				break;
12758 		}
12759 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12760 			cmn_err(CE_CONT, "?\tserial number - none\n");
12761 		} else {
12762 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12763 		}
12764 	}
12765 
12766 #ifdef SATA_DEBUG
12767 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12768 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12769 		int i;
12770 		for (i = 14; i >= 2; i--) {
12771 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12772 				valid_version = i;
12773 				break;
12774 			}
12775 		}
12776 		cmn_err(CE_CONT,
12777 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12778 		    valid_version,
12779 		    sdinfo->satadrv_id.ai_majorversion,
12780 		    sdinfo->satadrv_id.ai_minorversion);
12781 	}
12782 #endif
12783 	/* Log some info */
12784 	cmn_err(CE_CONT, "?\tsupported features:\n");
12785 	msg_buf[0] = '\0';
12786 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12787 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12788 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12789 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12790 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12791 	}
12792 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12793 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12794 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12795 		(void) strlcat(msg_buf, ", Native Command Queueing",
12796 		    MAXPATHLEN);
12797 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12798 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12799 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12800 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12801 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12802 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12803 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12804 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12805 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12806 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12807 		cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12808 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12809 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12810 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12811 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12812 	if (sdinfo->satadrv_features_support &
12813 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12814 		msg_buf[0] = '\0';
12815 		(void) snprintf(msg_buf, MAXPATHLEN,
12816 		    "Supported queue depth %d",
12817 		    sdinfo->satadrv_queue_depth);
12818 		if (!(sata_func_enable &
12819 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12820 			(void) strlcat(msg_buf,
12821 			    " - queueing disabled globally", MAXPATHLEN);
12822 		else if (sdinfo->satadrv_queue_depth >
12823 		    sdinfo->satadrv_max_queue_depth) {
12824 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12825 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12826 			    (int)sdinfo->satadrv_max_queue_depth);
12827 		}
12828 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12829 	}
12830 
12831 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12832 #ifdef __i386
12833 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12834 		    sdinfo->satadrv_capacity);
12835 #else
12836 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12837 		    sdinfo->satadrv_capacity);
12838 #endif
12839 		cmn_err(CE_CONT, "?%s", msg_buf);
12840 	}
12841 }
12842 
12843 /*
12844  * Log/display port multiplier information
12845  * No Mutex should be hold.
12846  */
12847 static void
12848 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12849     sata_device_t *sata_device)
12850 {
12851 	_NOTE(ARGUNUSED(sata_hba_inst))
12852 
12853 	int cport = sata_device->satadev_addr.cport;
12854 	sata_pmult_info_t *pmultinfo;
12855 	char msg_buf[MAXPATHLEN];
12856 	uint32_t gscr0, gscr1, gscr2, gscr64;
12857 
12858 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12859 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12860 	if (pmultinfo == NULL) {
12861 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12862 		return;
12863 	}
12864 
12865 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12866 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12867 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12868 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12869 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12870 
12871 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12872 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12873 
12874 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12875 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12876 	cmn_err(CE_CONT, "?%s", msg_buf);
12877 
12878 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12879 	if (gscr1 & (1 << 3))
12880 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12881 	else if (gscr1 & (1 << 2))
12882 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12883 	else if (gscr1 & (1 << 1))
12884 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12885 	else
12886 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12887 	cmn_err(CE_CONT, "?%s", msg_buf);
12888 
12889 	(void) strcpy(msg_buf, "\tSupport ");
12890 	if (gscr64 & (1 << 3))
12891 		(void) strlcat(msg_buf, "Asy-Notif, ",
12892 		    MAXPATHLEN);
12893 	if (gscr64 & (1 << 2))
12894 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12895 	if (gscr64 & (1 << 1))
12896 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12897 	if (gscr64 & (1 << 0))
12898 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12899 	if ((gscr64 & 0xf) == 0)
12900 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12901 	cmn_err(CE_CONT, "?%s", msg_buf);
12902 
12903 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12904 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12905 	cmn_err(CE_CONT, "?%s", msg_buf);
12906 }
12907 
12908 /*
12909  * sata_save_drive_settings extracts current setting of the device and stores
12910  * it for future reference, in case the device setup would need to be restored
12911  * after the device reset.
12912  *
12913  * For all devices read ahead and write cache settings are saved, if the
12914  * device supports these features at all.
12915  * For ATAPI devices the Removable Media Status Notification setting is saved.
12916  */
12917 static void
12918 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12919 {
12920 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12921 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12922 
12923 		/* Current setting of Read Ahead (and Read Cache) */
12924 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12925 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12926 		else
12927 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12928 
12929 		/* Current setting of Write Cache */
12930 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12931 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12932 		else
12933 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12934 	}
12935 
12936 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12937 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12938 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12939 		else
12940 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12941 	}
12942 }
12943 
12944 
12945 /*
12946  * sata_check_capacity function determines a disk capacity
12947  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12948  *
12949  * NOTE: CHS mode is not supported! If a device does not support LBA,
12950  * this function is not called.
12951  *
12952  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12953  */
12954 static uint64_t
12955 sata_check_capacity(sata_drive_info_t *sdinfo)
12956 {
12957 	uint64_t capacity = 0;
12958 	int i;
12959 
12960 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
12961 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
12962 		/* Capacity valid only for LBA-addressable disk devices */
12963 		return (0);
12964 
12965 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
12966 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
12967 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
12968 		/* LBA48 mode supported and enabled */
12969 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
12970 		    SATA_DEV_F_LBA28;
12971 		for (i = 3;  i >= 0;  --i) {
12972 			capacity <<= 16;
12973 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
12974 		}
12975 	} else {
12976 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
12977 		capacity <<= 16;
12978 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
12979 		if (capacity >= 0x1000000)
12980 			/* LBA28 mode */
12981 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
12982 	}
12983 	return (capacity);
12984 }
12985 
12986 
12987 /*
12988  * Allocate consistent buffer for DMA transfer
12989  *
12990  * Cannot be called from interrupt level or with mutex held - it may sleep.
12991  *
12992  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
12993  */
12994 static struct buf *
12995 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
12996 {
12997 	struct scsi_address ap;
12998 	struct buf *bp;
12999 	ddi_dma_attr_t	cur_dma_attr;
13000 
13001 	ASSERT(spx->txlt_sata_pkt != NULL);
13002 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13003 	ap.a_target = SATA_TO_SCSI_TARGET(
13004 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13005 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13006 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13007 	ap.a_lun = 0;
13008 
13009 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13010 	    B_READ, SLEEP_FUNC, NULL);
13011 
13012 	if (bp != NULL) {
13013 		/* Allocate DMA resources for this buffer */
13014 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13015 		/*
13016 		 * We use a local version of the dma_attr, to account
13017 		 * for a device addressing limitations.
13018 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13019 		 * will cause dma attributes to be adjusted to a lowest
13020 		 * acceptable level.
13021 		 */
13022 		sata_adjust_dma_attr(NULL,
13023 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13024 
13025 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13026 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13027 			scsi_free_consistent_buf(bp);
13028 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13029 			bp = NULL;
13030 		}
13031 	}
13032 	return (bp);
13033 }
13034 
13035 /*
13036  * Release local buffer (consistent buffer for DMA transfer) allocated
13037  * via sata_alloc_local_buffer().
13038  */
13039 static void
13040 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13041 {
13042 	ASSERT(spx->txlt_sata_pkt != NULL);
13043 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13044 
13045 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13046 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13047 
13048 	sata_common_free_dma_rsrcs(spx);
13049 
13050 	/* Free buffer */
13051 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13052 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13053 }
13054 
13055 /*
13056  * Allocate sata_pkt
13057  * Pkt structure version and embedded strcutures version are initialized.
13058  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13059  *
13060  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13061  * callback argument determines if it can sleep or not.
13062  * Hence, it should not be called from interrupt context.
13063  *
13064  * If successful, non-NULL pointer to a sata pkt is returned.
13065  * Upon failure, NULL pointer is returned.
13066  */
13067 static sata_pkt_t *
13068 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13069 {
13070 	sata_pkt_t *spkt;
13071 	int kmsflag;
13072 
13073 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13074 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13075 	if (spkt == NULL) {
13076 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13077 		    "sata_pkt_alloc: failed"));
13078 		return (NULL);
13079 	}
13080 	spkt->satapkt_rev = SATA_PKT_REV;
13081 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13082 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13083 	spkt->satapkt_framework_private = spx;
13084 	spx->txlt_sata_pkt = spkt;
13085 	return (spkt);
13086 }
13087 
13088 /*
13089  * Free sata pkt allocated via sata_pkt_alloc()
13090  */
13091 static void
13092 sata_pkt_free(sata_pkt_txlate_t *spx)
13093 {
13094 	ASSERT(spx->txlt_sata_pkt != NULL);
13095 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13096 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13097 	spx->txlt_sata_pkt = NULL;
13098 }
13099 
13100 
13101 /*
13102  * Adjust DMA attributes.
13103  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13104  * from 8 bits to 16 bits, depending on a command being used.
13105  * Limiting max block count arbitrarily to 256 for all read/write
13106  * commands may affects performance, so check both the device and
13107  * controller capability before adjusting dma attributes.
13108  */
13109 void
13110 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13111     ddi_dma_attr_t *adj_dma_attr)
13112 {
13113 	uint32_t count_max;
13114 
13115 	/* Copy original attributes */
13116 	*adj_dma_attr = *dma_attr;
13117 	/*
13118 	 * Things to consider: device addressing capability,
13119 	 * "excessive" controller DMA capabilities.
13120 	 * If a device is being probed/initialized, there are
13121 	 * no device info - use default limits then.
13122 	 */
13123 	if (sdinfo == NULL) {
13124 		count_max = dma_attr->dma_attr_granular * 0x100;
13125 		if (dma_attr->dma_attr_count_max > count_max)
13126 			adj_dma_attr->dma_attr_count_max = count_max;
13127 		if (dma_attr->dma_attr_maxxfer > count_max)
13128 			adj_dma_attr->dma_attr_maxxfer = count_max;
13129 		return;
13130 	}
13131 
13132 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13133 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13134 			/*
13135 			 * 16-bit sector count may be used - we rely on
13136 			 * the assumption that only read and write cmds
13137 			 * will request more than 256 sectors worth of data
13138 			 */
13139 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13140 		} else {
13141 			/*
13142 			 * 8-bit sector count will be used - default limits
13143 			 * for dma attributes
13144 			 */
13145 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13146 		}
13147 		/*
13148 		 * Adjust controler dma attributes, if necessary
13149 		 */
13150 		if (dma_attr->dma_attr_count_max > count_max)
13151 			adj_dma_attr->dma_attr_count_max = count_max;
13152 		if (dma_attr->dma_attr_maxxfer > count_max)
13153 			adj_dma_attr->dma_attr_maxxfer = count_max;
13154 	}
13155 }
13156 
13157 
13158 /*
13159  * Allocate DMA resources for the buffer
13160  * This function handles initial DMA resource allocation as well as
13161  * DMA window shift and may be called repeatedly for the same DMA window
13162  * until all DMA cookies in the DMA window are processed.
13163  * To guarantee that there is always a coherent set of cookies to process
13164  * by SATA HBA driver (observing alignment, device granularity, etc.),
13165  * the number of slots for DMA cookies is equal to lesser of  a number of
13166  * cookies in a DMA window and a max number of scatter/gather entries.
13167  *
13168  * Returns DDI_SUCCESS upon successful operation.
13169  * Return failure code of a failing command or DDI_FAILURE when
13170  * internal cleanup failed.
13171  */
13172 static int
13173 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13174     int (*callback)(caddr_t), caddr_t arg,
13175     ddi_dma_attr_t *cur_dma_attr)
13176 {
13177 	int	rval;
13178 	off_t	offset;
13179 	size_t	size;
13180 	int	max_sg_len, req_len, i;
13181 	uint_t	dma_flags;
13182 	struct buf	*bp;
13183 	uint64_t	cur_txfer_len;
13184 
13185 
13186 	ASSERT(spx->txlt_sata_pkt != NULL);
13187 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13188 	ASSERT(bp != NULL);
13189 
13190 
13191 	if (spx->txlt_buf_dma_handle == NULL) {
13192 		/*
13193 		 * No DMA resources allocated so far - this is a first call
13194 		 * for this sata pkt.
13195 		 */
13196 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13197 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13198 
13199 		if (rval != DDI_SUCCESS) {
13200 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13201 			    "sata_dma_buf_setup: no buf DMA resources %x",
13202 			    rval));
13203 			return (rval);
13204 		}
13205 
13206 		if (bp->b_flags & B_READ)
13207 			dma_flags = DDI_DMA_READ;
13208 		else
13209 			dma_flags = DDI_DMA_WRITE;
13210 
13211 		if (flags & PKT_CONSISTENT)
13212 			dma_flags |= DDI_DMA_CONSISTENT;
13213 
13214 		if (flags & PKT_DMA_PARTIAL)
13215 			dma_flags |= DDI_DMA_PARTIAL;
13216 
13217 		/*
13218 		 * Check buffer alignment and size against dma attributes
13219 		 * Consider dma_attr_align only. There may be requests
13220 		 * with the size lower than device granularity, but they
13221 		 * will not read/write from/to the device, so no adjustment
13222 		 * is necessary. The dma_attr_minxfer theoretically should
13223 		 * be considered, but no HBA driver is checking it.
13224 		 */
13225 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13226 		    cur_dma_attr->dma_attr_align)) {
13227 			rval = ddi_dma_buf_bind_handle(
13228 			    spx->txlt_buf_dma_handle,
13229 			    bp, dma_flags, callback, arg,
13230 			    &spx->txlt_dma_cookie,
13231 			    &spx->txlt_curwin_num_dma_cookies);
13232 		} else { /* Buffer is not aligned */
13233 
13234 			int	(*ddicallback)(caddr_t);
13235 			size_t	bufsz;
13236 
13237 			/* Check id sleeping is allowed */
13238 			ddicallback = (callback == NULL_FUNC) ?
13239 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13240 
13241 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13242 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13243 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13244 
13245 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13246 				/*
13247 				 * CPU will need to access data in the buffer
13248 				 * (for copying) so map it.
13249 				 */
13250 				bp_mapin(bp);
13251 
13252 			ASSERT(spx->txlt_tmp_buf == NULL);
13253 
13254 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13255 			rval = ddi_dma_mem_alloc(
13256 			    spx->txlt_buf_dma_handle,
13257 			    bp->b_bcount,
13258 			    &sata_acc_attr,
13259 			    DDI_DMA_STREAMING,
13260 			    ddicallback, NULL,
13261 			    &spx->txlt_tmp_buf,
13262 			    &bufsz,
13263 			    &spx->txlt_tmp_buf_handle);
13264 
13265 			if (rval != DDI_SUCCESS) {
13266 				/* DMA mapping failed */
13267 				(void) ddi_dma_free_handle(
13268 				    &spx->txlt_buf_dma_handle);
13269 				spx->txlt_buf_dma_handle = NULL;
13270 #ifdef SATA_DEBUG
13271 				mbuffail_count++;
13272 #endif
13273 				SATADBG1(SATA_DBG_DMA_SETUP,
13274 				    spx->txlt_sata_hba_inst,
13275 				    "sata_dma_buf_setup: "
13276 				    "buf dma mem alloc failed %x\n", rval);
13277 				return (rval);
13278 			}
13279 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13280 			    cur_dma_attr->dma_attr_align));
13281 
13282 #ifdef SATA_DEBUG
13283 			mbuf_count++;
13284 
13285 			if (bp->b_bcount != bufsz)
13286 				/*
13287 				 * This will require special handling, because
13288 				 * DMA cookies will be based on the temporary
13289 				 * buffer size, not the original buffer
13290 				 * b_bcount, so the residue may have to
13291 				 * be counted differently.
13292 				 */
13293 				SATADBG2(SATA_DBG_DMA_SETUP,
13294 				    spx->txlt_sata_hba_inst,
13295 				    "sata_dma_buf_setup: bp size %x != "
13296 				    "bufsz %x\n", bp->b_bcount, bufsz);
13297 #endif
13298 			if (dma_flags & DDI_DMA_WRITE) {
13299 				/*
13300 				 * Write operation - copy data into
13301 				 * an aligned temporary buffer. Buffer will be
13302 				 * synced for device by ddi_dma_addr_bind_handle
13303 				 */
13304 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13305 				    bp->b_bcount);
13306 			}
13307 
13308 			rval = ddi_dma_addr_bind_handle(
13309 			    spx->txlt_buf_dma_handle,
13310 			    NULL,
13311 			    spx->txlt_tmp_buf,
13312 			    bufsz, dma_flags, ddicallback, 0,
13313 			    &spx->txlt_dma_cookie,
13314 			    &spx->txlt_curwin_num_dma_cookies);
13315 		}
13316 
13317 		switch (rval) {
13318 		case DDI_DMA_PARTIAL_MAP:
13319 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13320 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13321 			/*
13322 			 * Partial DMA mapping.
13323 			 * Retrieve number of DMA windows for this request.
13324 			 */
13325 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13326 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13327 				if (spx->txlt_tmp_buf != NULL) {
13328 					ddi_dma_mem_free(
13329 					    &spx->txlt_tmp_buf_handle);
13330 					spx->txlt_tmp_buf = NULL;
13331 				}
13332 				(void) ddi_dma_unbind_handle(
13333 				    spx->txlt_buf_dma_handle);
13334 				(void) ddi_dma_free_handle(
13335 				    &spx->txlt_buf_dma_handle);
13336 				spx->txlt_buf_dma_handle = NULL;
13337 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13338 				    "sata_dma_buf_setup: numwin failed\n"));
13339 				return (DDI_FAILURE);
13340 			}
13341 			SATADBG2(SATA_DBG_DMA_SETUP,
13342 			    spx->txlt_sata_hba_inst,
13343 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13344 			    spx->txlt_num_dma_win,
13345 			    spx->txlt_curwin_num_dma_cookies);
13346 			spx->txlt_cur_dma_win = 0;
13347 			break;
13348 
13349 		case DDI_DMA_MAPPED:
13350 			/* DMA fully mapped */
13351 			spx->txlt_num_dma_win = 1;
13352 			spx->txlt_cur_dma_win = 0;
13353 			SATADBG1(SATA_DBG_DMA_SETUP,
13354 			    spx->txlt_sata_hba_inst,
13355 			    "sata_dma_buf_setup: windows: 1 "
13356 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13357 			break;
13358 
13359 		default:
13360 			/* DMA mapping failed */
13361 			if (spx->txlt_tmp_buf != NULL) {
13362 				ddi_dma_mem_free(
13363 				    &spx->txlt_tmp_buf_handle);
13364 				spx->txlt_tmp_buf = NULL;
13365 			}
13366 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13367 			spx->txlt_buf_dma_handle = NULL;
13368 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13369 			    "sata_dma_buf_setup: buf dma handle binding "
13370 			    "failed %x\n", rval));
13371 			return (rval);
13372 		}
13373 		spx->txlt_curwin_processed_dma_cookies = 0;
13374 		spx->txlt_dma_cookie_list = NULL;
13375 	} else {
13376 		/*
13377 		 * DMA setup is reused. Check if we need to process more
13378 		 * cookies in current window, or to get next window, if any.
13379 		 */
13380 
13381 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13382 		    spx->txlt_curwin_num_dma_cookies);
13383 
13384 		if (spx->txlt_curwin_processed_dma_cookies ==
13385 		    spx->txlt_curwin_num_dma_cookies) {
13386 			/*
13387 			 * All cookies from current DMA window were processed.
13388 			 * Get next DMA window.
13389 			 */
13390 			spx->txlt_cur_dma_win++;
13391 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13392 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13393 				    spx->txlt_cur_dma_win, &offset, &size,
13394 				    &spx->txlt_dma_cookie,
13395 				    &spx->txlt_curwin_num_dma_cookies);
13396 				spx->txlt_curwin_processed_dma_cookies = 0;
13397 			} else {
13398 				/* No more windows! End of request! */
13399 				/* What to do? - panic for now */
13400 				ASSERT(spx->txlt_cur_dma_win >=
13401 				    spx->txlt_num_dma_win);
13402 
13403 				spx->txlt_curwin_num_dma_cookies = 0;
13404 				spx->txlt_curwin_processed_dma_cookies = 0;
13405 				spx->txlt_sata_pkt->
13406 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13407 				return (DDI_SUCCESS);
13408 			}
13409 		}
13410 	}
13411 	/* There better be at least one DMA cookie outstanding */
13412 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13413 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13414 
13415 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13416 		/* The default cookie slot was used in previous run */
13417 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13418 		spx->txlt_dma_cookie_list = NULL;
13419 		spx->txlt_dma_cookie_list_len = 0;
13420 	}
13421 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13422 		/*
13423 		 * Processing a new DMA window - set-up dma cookies list.
13424 		 * We may reuse previously allocated cookie array if it is
13425 		 * possible.
13426 		 */
13427 		if (spx->txlt_dma_cookie_list != NULL &&
13428 		    spx->txlt_dma_cookie_list_len <
13429 		    spx->txlt_curwin_num_dma_cookies) {
13430 			/*
13431 			 * New DMA window contains more cookies than
13432 			 * the previous one. We need larger cookie list - free
13433 			 * the old one.
13434 			 */
13435 			(void) kmem_free(spx->txlt_dma_cookie_list,
13436 			    spx->txlt_dma_cookie_list_len *
13437 			    sizeof (ddi_dma_cookie_t));
13438 			spx->txlt_dma_cookie_list = NULL;
13439 			spx->txlt_dma_cookie_list_len = 0;
13440 		}
13441 		if (spx->txlt_dma_cookie_list == NULL) {
13442 			/*
13443 			 * Calculate lesser of number of cookies in this
13444 			 * DMA window and number of s/g entries.
13445 			 */
13446 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13447 			req_len = MIN(max_sg_len,
13448 			    spx->txlt_curwin_num_dma_cookies);
13449 
13450 			/* Allocate new dma cookie array if necessary */
13451 			if (req_len == 1) {
13452 				/* Only one cookie - no need for a list */
13453 				spx->txlt_dma_cookie_list =
13454 				    &spx->txlt_dma_cookie;
13455 				spx->txlt_dma_cookie_list_len = 1;
13456 			} else {
13457 				/*
13458 				 * More than one cookie - try to allocate space.
13459 				 */
13460 				spx->txlt_dma_cookie_list = kmem_zalloc(
13461 				    sizeof (ddi_dma_cookie_t) * req_len,
13462 				    callback == NULL_FUNC ? KM_NOSLEEP :
13463 				    KM_SLEEP);
13464 				if (spx->txlt_dma_cookie_list == NULL) {
13465 					SATADBG1(SATA_DBG_DMA_SETUP,
13466 					    spx->txlt_sata_hba_inst,
13467 					    "sata_dma_buf_setup: cookie list "
13468 					    "allocation failed\n", NULL);
13469 					/*
13470 					 * We could not allocate space for
13471 					 * neccessary number of dma cookies in
13472 					 * this window, so we fail this request.
13473 					 * Next invocation would try again to
13474 					 * allocate space for cookie list.
13475 					 * Note:Packet residue was not modified.
13476 					 */
13477 					return (DDI_DMA_NORESOURCES);
13478 				} else {
13479 					spx->txlt_dma_cookie_list_len = req_len;
13480 				}
13481 			}
13482 		}
13483 		/*
13484 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13485 		 * First cookie was already fetched.
13486 		 */
13487 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13488 		cur_txfer_len =
13489 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13490 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13491 		spx->txlt_curwin_processed_dma_cookies++;
13492 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13493 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13494 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13495 			    &spx->txlt_dma_cookie_list[i]);
13496 			cur_txfer_len +=
13497 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13498 			spx->txlt_curwin_processed_dma_cookies++;
13499 			spx->txlt_sata_pkt->
13500 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13501 		}
13502 	} else {
13503 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13504 		    "sata_dma_buf_setup: sliding within DMA window, "
13505 		    "cur cookie %d, total cookies %d\n",
13506 		    spx->txlt_curwin_processed_dma_cookies,
13507 		    spx->txlt_curwin_num_dma_cookies);
13508 
13509 		/*
13510 		 * Not all cookies from the current dma window were used because
13511 		 * of s/g limitation.
13512 		 * There is no need to re-size the list - it was set at
13513 		 * optimal size, or only default entry is used (s/g = 1).
13514 		 */
13515 		if (spx->txlt_dma_cookie_list == NULL) {
13516 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13517 			spx->txlt_dma_cookie_list_len = 1;
13518 		}
13519 		/*
13520 		 * Since we are processing remaining cookies in a DMA window,
13521 		 * there may be less of them than the number of entries in the
13522 		 * current dma cookie list.
13523 		 */
13524 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13525 		    (spx->txlt_curwin_num_dma_cookies -
13526 		    spx->txlt_curwin_processed_dma_cookies));
13527 
13528 		/* Fetch the next batch of cookies */
13529 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13530 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13531 			    &spx->txlt_dma_cookie_list[i]);
13532 			cur_txfer_len +=
13533 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13534 			spx->txlt_sata_pkt->
13535 			    satapkt_cmd.satacmd_num_dma_cookies++;
13536 			spx->txlt_curwin_processed_dma_cookies++;
13537 		}
13538 	}
13539 
13540 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13541 
13542 	/* Point sata_cmd to the cookie list */
13543 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13544 	    &spx->txlt_dma_cookie_list[0];
13545 
13546 	/* Remember number of DMA cookies passed in sata packet */
13547 	spx->txlt_num_dma_cookies =
13548 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13549 
13550 	ASSERT(cur_txfer_len != 0);
13551 	if (cur_txfer_len <= bp->b_bcount)
13552 		spx->txlt_total_residue -= cur_txfer_len;
13553 	else {
13554 		/*
13555 		 * Temporary DMA buffer has been padded by
13556 		 * ddi_dma_mem_alloc()!
13557 		 * This requires special handling, because DMA cookies are
13558 		 * based on the temporary buffer size, not the b_bcount,
13559 		 * and we have extra bytes to transfer - but the packet
13560 		 * residue has to stay correct because we will copy only
13561 		 * the requested number of bytes.
13562 		 */
13563 		spx->txlt_total_residue -= bp->b_bcount;
13564 	}
13565 
13566 	return (DDI_SUCCESS);
13567 }
13568 
13569 /*
13570  * Common routine for releasing DMA resources
13571  */
13572 static void
13573 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13574 {
13575 	if (spx->txlt_buf_dma_handle != NULL) {
13576 		if (spx->txlt_tmp_buf != NULL)  {
13577 			/*
13578 			 * Intermediate DMA buffer was allocated.
13579 			 * Free allocated buffer and associated access handle.
13580 			 */
13581 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13582 			spx->txlt_tmp_buf = NULL;
13583 		}
13584 		/*
13585 		 * Free DMA resources - cookies and handles
13586 		 */
13587 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13588 		if (spx->txlt_dma_cookie_list != NULL) {
13589 			if (spx->txlt_dma_cookie_list !=
13590 			    &spx->txlt_dma_cookie) {
13591 				(void) kmem_free(spx->txlt_dma_cookie_list,
13592 				    spx->txlt_dma_cookie_list_len *
13593 				    sizeof (ddi_dma_cookie_t));
13594 				spx->txlt_dma_cookie_list = NULL;
13595 			}
13596 		}
13597 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13598 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13599 		spx->txlt_buf_dma_handle = NULL;
13600 	}
13601 }
13602 
13603 /*
13604  * Free DMA resources
13605  * Used by the HBA driver to release DMA resources that it does not use.
13606  *
13607  * Returns Void
13608  */
13609 void
13610 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13611 {
13612 	sata_pkt_txlate_t *spx;
13613 
13614 	if (sata_pkt == NULL)
13615 		return;
13616 
13617 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13618 
13619 	sata_common_free_dma_rsrcs(spx);
13620 }
13621 
13622 /*
13623  * Fetch Device Identify data.
13624  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13625  * command to a device and get the device identify data.
13626  * The device_info structure has to be set to device type (for selecting proper
13627  * device identify command).
13628  *
13629  * Returns:
13630  * SATA_SUCCESS if cmd succeeded
13631  * SATA_RETRY if cmd was rejected and could be retried,
13632  * SATA_FAILURE if cmd failed and should not be retried (port error)
13633  *
13634  * Cannot be called in an interrupt context.
13635  */
13636 
13637 static int
13638 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13639     sata_drive_info_t *sdinfo)
13640 {
13641 	struct buf *bp;
13642 	sata_pkt_t *spkt;
13643 	sata_cmd_t *scmd;
13644 	sata_pkt_txlate_t *spx;
13645 	int rval;
13646 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13647 
13648 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13649 	spx->txlt_sata_hba_inst = sata_hba_inst;
13650 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13651 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13652 	if (spkt == NULL) {
13653 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13654 		return (SATA_RETRY); /* may retry later */
13655 	}
13656 	/* address is needed now */
13657 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13658 
13659 	/*
13660 	 * Allocate buffer for Identify Data return data
13661 	 */
13662 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13663 	if (bp == NULL) {
13664 		sata_pkt_free(spx);
13665 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13666 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13667 		    "sata_fetch_device_identify_data: "
13668 		    "cannot allocate buffer for ID"));
13669 		return (SATA_RETRY); /* may retry later */
13670 	}
13671 
13672 	/* Fill sata_pkt */
13673 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13674 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13675 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13676 	/* Synchronous mode, no callback */
13677 	spkt->satapkt_comp = NULL;
13678 	/* Timeout 30s */
13679 	spkt->satapkt_time = sata_default_pkt_time;
13680 
13681 	scmd = &spkt->satapkt_cmd;
13682 	scmd->satacmd_bp = bp;
13683 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13684 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13685 
13686 	/* Build Identify Device cmd in the sata_pkt */
13687 	scmd->satacmd_addr_type = 0;		/* N/A */
13688 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13689 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13690 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13691 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13692 	scmd->satacmd_features_reg = 0;		/* N/A */
13693 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13694 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13695 		/* Identify Packet Device cmd */
13696 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13697 	} else {
13698 		/* Identify Device cmd - mandatory for all other devices */
13699 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13700 	}
13701 
13702 	/* Send pkt to SATA HBA driver */
13703 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13704 
13705 #ifdef SATA_INJECT_FAULTS
13706 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13707 #endif
13708 
13709 	if (rval == SATA_TRAN_ACCEPTED &&
13710 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13711 		if (spx->txlt_buf_dma_handle != NULL) {
13712 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13713 			    DDI_DMA_SYNC_FORKERNEL);
13714 			ASSERT(rval == DDI_SUCCESS);
13715 			if (sata_check_for_dma_error(dip, spx)) {
13716 				ddi_fm_service_impact(dip,
13717 				    DDI_SERVICE_UNAFFECTED);
13718 				rval = SATA_RETRY;
13719 				goto fail;
13720 			}
13721 
13722 		}
13723 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13724 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13725 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13726 			    "SATA disk device at port %d - "
13727 			    "partial Identify Data",
13728 			    sdinfo->satadrv_addr.cport));
13729 			rval = SATA_RETRY; /* may retry later */
13730 			goto fail;
13731 		}
13732 		/* Update sata_drive_info */
13733 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13734 		    sizeof (sata_id_t));
13735 
13736 		sdinfo->satadrv_features_support = 0;
13737 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13738 			/*
13739 			 * Retrieve capacity (disks only) and addressing mode
13740 			 */
13741 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13742 		} else {
13743 			/*
13744 			 * For ATAPI devices one would have to issue
13745 			 * Get Capacity cmd for media capacity. Not here.
13746 			 */
13747 			sdinfo->satadrv_capacity = 0;
13748 			/*
13749 			 * Check what cdb length is supported
13750 			 */
13751 			if ((sdinfo->satadrv_id.ai_config &
13752 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13753 				sdinfo->satadrv_atapi_cdb_len = 16;
13754 			else
13755 				sdinfo->satadrv_atapi_cdb_len = 12;
13756 		}
13757 		/* Setup supported features flags */
13758 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13759 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13760 
13761 		/* Check for SATA GEN and NCQ support */
13762 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13763 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13764 			/* SATA compliance */
13765 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13766 				sdinfo->satadrv_features_support |=
13767 				    SATA_DEV_F_NCQ;
13768 			if (sdinfo->satadrv_id.ai_satacap &
13769 			    (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13770 				if (sdinfo->satadrv_id.ai_satacap &
13771 				    SATA_3_SPEED)
13772 					sdinfo->satadrv_features_support |=
13773 					    SATA_DEV_F_SATA3;
13774 				if (sdinfo->satadrv_id.ai_satacap &
13775 				    SATA_2_SPEED)
13776 					sdinfo->satadrv_features_support |=
13777 					    SATA_DEV_F_SATA2;
13778 				if (sdinfo->satadrv_id.ai_satacap &
13779 				    SATA_1_SPEED)
13780 					sdinfo->satadrv_features_support |=
13781 					    SATA_DEV_F_SATA1;
13782 			} else {
13783 				sdinfo->satadrv_features_support |=
13784 				    SATA_DEV_F_SATA1;
13785 			}
13786 		}
13787 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13788 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13789 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13790 
13791 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13792 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13793 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13794 			++sdinfo->satadrv_queue_depth;
13795 			/* Adjust according to controller capabilities */
13796 			sdinfo->satadrv_max_queue_depth = MIN(
13797 			    sdinfo->satadrv_queue_depth,
13798 			    SATA_QDEPTH(sata_hba_inst));
13799 			/* Adjust according to global queue depth limit */
13800 			sdinfo->satadrv_max_queue_depth = MIN(
13801 			    sdinfo->satadrv_max_queue_depth,
13802 			    sata_current_max_qdepth);
13803 			if (sdinfo->satadrv_max_queue_depth == 0)
13804 				sdinfo->satadrv_max_queue_depth = 1;
13805 		} else
13806 			sdinfo->satadrv_max_queue_depth = 1;
13807 
13808 		rval = SATA_SUCCESS;
13809 	} else {
13810 		/*
13811 		 * Woops, no Identify Data.
13812 		 */
13813 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13814 			rval = SATA_RETRY; /* may retry later */
13815 		} else if (rval == SATA_TRAN_ACCEPTED) {
13816 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13817 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13818 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13819 			    spkt->satapkt_reason == SATA_PKT_RESET)
13820 				rval = SATA_RETRY; /* may retry later */
13821 			else
13822 				rval = SATA_FAILURE;
13823 		} else {
13824 			rval = SATA_FAILURE;
13825 		}
13826 	}
13827 fail:
13828 	/* Free allocated resources */
13829 	sata_free_local_buffer(spx);
13830 	sata_pkt_free(spx);
13831 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13832 
13833 	return (rval);
13834 }
13835 
13836 
13837 /*
13838  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13839  * UDMA mode is checked first, followed by MWDMA mode.
13840  * set correctly, so this function is setting it to the highest supported level.
13841  * Older SATA spec required that the device supports at least DMA 4 mode and
13842  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13843  * restriction has been removed.
13844  *
13845  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13846  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13847  *
13848  * NOTE: This function should be called only if DMA mode is supported.
13849  */
13850 static int
13851 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13852 {
13853 	sata_pkt_t *spkt;
13854 	sata_cmd_t *scmd;
13855 	sata_pkt_txlate_t *spx;
13856 	int i, mode;
13857 	uint8_t subcmd;
13858 	int rval = SATA_SUCCESS;
13859 
13860 	ASSERT(sdinfo != NULL);
13861 	ASSERT(sata_hba_inst != NULL);
13862 
13863 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13864 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13865 		/* Find highest Ultra DMA mode supported */
13866 		for (mode = 6; mode >= 0; --mode) {
13867 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13868 				break;
13869 		}
13870 #if 0
13871 		/* Left for historical reasons */
13872 		/*
13873 		 * Some initial version of SATA spec indicated that at least
13874 		 * UDMA mode 4 has to be supported. It is not mentioned in
13875 		 * SerialATA 2.6, so this restriction is removed.
13876 		 */
13877 		if (mode < 4)
13878 			return (SATA_FAILURE);
13879 #endif
13880 
13881 		/*
13882 		 * For disk, we're still going to set DMA mode whatever is
13883 		 * selected by default
13884 		 *
13885 		 * We saw an old maxtor sata drive will select Ultra DMA and
13886 		 * Multi-Word DMA simultaneouly by default, which is going
13887 		 * to cause DMA command timed out, so we need to select DMA
13888 		 * mode even when it's already done by default
13889 		 */
13890 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13891 
13892 			/* Find UDMA mode currently selected */
13893 			for (i = 6; i >= 0; --i) {
13894 				if (sdinfo->satadrv_id.ai_ultradma &
13895 				    (1 << (i + 8)))
13896 					break;
13897 			}
13898 			if (i >= mode)
13899 				/* Nothing to do */
13900 				return (SATA_SUCCESS);
13901 		}
13902 
13903 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13904 
13905 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13906 		/* Find highest MultiWord DMA mode supported */
13907 		for (mode = 2; mode >= 0; --mode) {
13908 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13909 				break;
13910 		}
13911 
13912 		/*
13913 		 * For disk, We're still going to set DMA mode whatever is
13914 		 * selected by default
13915 		 *
13916 		 * We saw an old maxtor sata drive will select Ultra DMA and
13917 		 * Multi-Word DMA simultaneouly by default, which is going
13918 		 * to cause DMA command timed out, so we need to select DMA
13919 		 * mode even when it's already done by default
13920 		 */
13921 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13922 
13923 			/* Find highest MultiWord DMA mode selected */
13924 			for (i = 2; i >= 0; --i) {
13925 				if (sdinfo->satadrv_id.ai_dworddma &
13926 				    (1 << (i + 8)))
13927 					break;
13928 			}
13929 			if (i >= mode)
13930 				/* Nothing to do */
13931 				return (SATA_SUCCESS);
13932 		}
13933 
13934 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13935 	} else
13936 		return (SATA_SUCCESS);
13937 
13938 	/*
13939 	 * Set DMA mode via SET FEATURES COMMAND.
13940 	 * Prepare packet for SET FEATURES COMMAND.
13941 	 */
13942 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13943 	spx->txlt_sata_hba_inst = sata_hba_inst;
13944 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13945 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13946 	if (spkt == NULL) {
13947 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13948 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
13949 		rval = SATA_FAILURE;
13950 		goto done;
13951 	}
13952 	/* Fill sata_pkt */
13953 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13954 	/* Timeout 30s */
13955 	spkt->satapkt_time = sata_default_pkt_time;
13956 	/* Synchronous mode, no callback, interrupts */
13957 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13958 	spkt->satapkt_comp = NULL;
13959 	scmd = &spkt->satapkt_cmd;
13960 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13961 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13962 	scmd->satacmd_addr_type = 0;
13963 	scmd->satacmd_device_reg = 0;
13964 	scmd->satacmd_status_reg = 0;
13965 	scmd->satacmd_error_reg = 0;
13966 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
13967 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
13968 	scmd->satacmd_sec_count_lsb = subcmd | mode;
13969 
13970 	/* Transfer command to HBA */
13971 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
13972 	    spkt) != SATA_TRAN_ACCEPTED ||
13973 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13974 		/* Pkt execution failed */
13975 		rval = SATA_FAILURE;
13976 	}
13977 done:
13978 
13979 	/* Free allocated resources */
13980 	if (spkt != NULL)
13981 		sata_pkt_free(spx);
13982 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
13983 
13984 	return (rval);
13985 }
13986 
13987 
13988 /*
13989  * Set device caching mode.
13990  * One of the following operations should be specified:
13991  * SATAC_SF_ENABLE_READ_AHEAD
13992  * SATAC_SF_DISABLE_READ_AHEAD
13993  * SATAC_SF_ENABLE_WRITE_CACHE
13994  * SATAC_SF_DISABLE_WRITE_CACHE
13995  *
13996  * If operation fails, system log messgage is emitted.
13997  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
13998  * command was sent but did not succeed, and SATA_FAILURE otherwise.
13999  */
14000 
14001 static int
14002 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14003     int cache_op)
14004 {
14005 	sata_pkt_t *spkt;
14006 	sata_cmd_t *scmd;
14007 	sata_pkt_txlate_t *spx;
14008 	int rval = SATA_SUCCESS;
14009 	int hba_rval;
14010 	char *infop;
14011 
14012 	ASSERT(sdinfo != NULL);
14013 	ASSERT(sata_hba_inst != NULL);
14014 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14015 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14016 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14017 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14018 
14019 
14020 	/* Prepare packet for SET FEATURES COMMAND */
14021 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14022 	spx->txlt_sata_hba_inst = sata_hba_inst;
14023 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14024 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14025 	if (spkt == NULL) {
14026 		rval = SATA_FAILURE;
14027 		goto failure;
14028 	}
14029 	/* Fill sata_pkt */
14030 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14031 	/* Timeout 30s */
14032 	spkt->satapkt_time = sata_default_pkt_time;
14033 	/* Synchronous mode, no callback, interrupts */
14034 	spkt->satapkt_op_mode =
14035 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14036 	spkt->satapkt_comp = NULL;
14037 	scmd = &spkt->satapkt_cmd;
14038 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14039 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14040 	scmd->satacmd_addr_type = 0;
14041 	scmd->satacmd_device_reg = 0;
14042 	scmd->satacmd_status_reg = 0;
14043 	scmd->satacmd_error_reg = 0;
14044 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14045 	scmd->satacmd_features_reg = cache_op;
14046 
14047 	/* Transfer command to HBA */
14048 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14049 	    SATA_DIP(sata_hba_inst), spkt);
14050 
14051 #ifdef SATA_INJECT_FAULTS
14052 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14053 #endif
14054 
14055 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14056 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14057 		/* Pkt execution failed */
14058 		switch (cache_op) {
14059 		case SATAC_SF_ENABLE_READ_AHEAD:
14060 			infop = "enabling read ahead failed";
14061 			break;
14062 		case SATAC_SF_DISABLE_READ_AHEAD:
14063 			infop = "disabling read ahead failed";
14064 			break;
14065 		case SATAC_SF_ENABLE_WRITE_CACHE:
14066 			infop = "enabling write cache failed";
14067 			break;
14068 		case SATAC_SF_DISABLE_WRITE_CACHE:
14069 			infop = "disabling write cache failed";
14070 			break;
14071 		}
14072 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14073 		rval = SATA_RETRY;
14074 	}
14075 failure:
14076 	/* Free allocated resources */
14077 	if (spkt != NULL)
14078 		sata_pkt_free(spx);
14079 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14080 	return (rval);
14081 }
14082 
14083 /*
14084  * Set Removable Media Status Notification (enable/disable)
14085  * state == 0 , disable
14086  * state != 0 , enable
14087  *
14088  * If operation fails, system log messgage is emitted.
14089  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14090  */
14091 
14092 static int
14093 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14094     int state)
14095 {
14096 	sata_pkt_t *spkt;
14097 	sata_cmd_t *scmd;
14098 	sata_pkt_txlate_t *spx;
14099 	int rval = SATA_SUCCESS;
14100 	char *infop;
14101 
14102 	ASSERT(sdinfo != NULL);
14103 	ASSERT(sata_hba_inst != NULL);
14104 
14105 	/* Prepare packet for SET FEATURES COMMAND */
14106 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14107 	spx->txlt_sata_hba_inst = sata_hba_inst;
14108 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14109 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14110 	if (spkt == NULL) {
14111 		rval = SATA_FAILURE;
14112 		goto failure;
14113 	}
14114 	/* Fill sata_pkt */
14115 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14116 	/* Timeout 30s */
14117 	spkt->satapkt_time = sata_default_pkt_time;
14118 	/* Synchronous mode, no callback, interrupts */
14119 	spkt->satapkt_op_mode =
14120 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14121 	spkt->satapkt_comp = NULL;
14122 	scmd = &spkt->satapkt_cmd;
14123 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14124 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14125 	scmd->satacmd_addr_type = 0;
14126 	scmd->satacmd_device_reg = 0;
14127 	scmd->satacmd_status_reg = 0;
14128 	scmd->satacmd_error_reg = 0;
14129 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14130 	if (state == 0)
14131 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14132 	else
14133 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14134 
14135 	/* Transfer command to HBA */
14136 	if (((*SATA_START_FUNC(sata_hba_inst))(
14137 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14138 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14139 		/* Pkt execution failed */
14140 		if (state == 0)
14141 			infop = "disabling Removable Media Status "
14142 			    "Notification failed";
14143 		else
14144 			infop = "enabling Removable Media Status "
14145 			    "Notification failed";
14146 
14147 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14148 		rval = SATA_FAILURE;
14149 	}
14150 failure:
14151 	/* Free allocated resources */
14152 	if (spkt != NULL)
14153 		sata_pkt_free(spx);
14154 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14155 	return (rval);
14156 }
14157 
14158 
14159 /*
14160  * Update state and copy port ss* values from passed sata_device structure.
14161  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14162  * configuration struct.
14163  *
14164  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14165  * regardless of the state in device argument.
14166  *
14167  * Port mutex should be held while calling this function.
14168  */
14169 static void
14170 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14171     sata_device_t *sata_device)
14172 {
14173 	sata_cport_info_t *cportinfo;
14174 
14175 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14176 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14177 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14178 		    sata_device->satadev_addr.cport)
14179 			return;
14180 
14181 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14182 		    sata_device->satadev_addr.cport);
14183 
14184 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14185 		cportinfo->cport_scr = sata_device->satadev_scr;
14186 
14187 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14188 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14189 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14190 		cportinfo->cport_state |=
14191 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14192 	}
14193 }
14194 
14195 void
14196 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14197     sata_device_t *sata_device)
14198 {
14199 	sata_pmport_info_t *pmportinfo;
14200 
14201 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14202 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14203 	    SATA_NUM_PMPORTS(sata_hba_inst,
14204 	    sata_device->satadev_addr.cport) <
14205 	    sata_device->satadev_addr.pmport) {
14206 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14207 		    "sata_update_port_info: error address %p.",
14208 		    &sata_device->satadev_addr);
14209 		return;
14210 	}
14211 
14212 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14213 	    sata_device->satadev_addr.cport,
14214 	    sata_device->satadev_addr.pmport);
14215 
14216 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14217 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14218 
14219 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14220 	pmportinfo->pmport_state &=
14221 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14222 	pmportinfo->pmport_state |=
14223 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14224 }
14225 
14226 /*
14227  * Extract SATA port specification from an IOCTL argument.
14228  *
14229  * This function return the port the user land send us as is, unless it
14230  * cannot retrieve port spec, then -1 is returned.
14231  *
14232  * Support port multiplier.
14233  */
14234 static int32_t
14235 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14236 {
14237 	int32_t port;
14238 
14239 	/* Extract port number from nvpair in dca structure  */
14240 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14241 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14242 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14243 		    port));
14244 		port = -1;
14245 	}
14246 
14247 	return (port);
14248 }
14249 
14250 /*
14251  * Get dev_info_t pointer to the device node pointed to by port argument.
14252  * NOTE: target argument is a value used in ioctls to identify
14253  * the AP - it is not a sata_address.
14254  * It is a combination of cport, pmport and address qualifier, encodded same
14255  * way as a scsi target number.
14256  * At this moment it carries only cport number.
14257  *
14258  * PMult hotplug is supported now.
14259  *
14260  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14261  */
14262 
14263 static dev_info_t *
14264 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14265 {
14266 	dev_info_t	*cdip = NULL;
14267 	int		target, tgt;
14268 	int 		circ;
14269 	uint8_t		qual;
14270 
14271 	sata_hba_inst_t	*sata_hba_inst;
14272 	scsi_hba_tran_t *scsi_hba_tran;
14273 
14274 	/* Get target id */
14275 	scsi_hba_tran = ddi_get_driver_private(dip);
14276 	if (scsi_hba_tran == NULL)
14277 		return (NULL);
14278 
14279 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14280 
14281 	if (sata_hba_inst == NULL)
14282 		return (NULL);
14283 
14284 	/* Identify a port-mult by cport_info.cport_dev_type */
14285 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14286 		qual = SATA_ADDR_DPMPORT;
14287 	else
14288 		qual = SATA_ADDR_DCPORT;
14289 
14290 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14291 
14292 	/* Retrieve target dip */
14293 	ndi_devi_enter(dip, &circ);
14294 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14295 		dev_info_t *next = ddi_get_next_sibling(cdip);
14296 
14297 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14298 		    DDI_PROP_DONTPASS, "target", -1);
14299 		if (tgt == -1) {
14300 			/*
14301 			 * This is actually an error condition, but not
14302 			 * a fatal one. Just continue the search.
14303 			 */
14304 			cdip = next;
14305 			continue;
14306 		}
14307 
14308 		if (tgt == target)
14309 			break;
14310 
14311 		cdip = next;
14312 	}
14313 	ndi_devi_exit(dip, circ);
14314 
14315 	return (cdip);
14316 }
14317 
14318 /*
14319  * Get dev_info_t pointer to the device node pointed to by port argument.
14320  * NOTE: target argument is a value used in ioctls to identify
14321  * the AP - it is not a sata_address.
14322  * It is a combination of cport, pmport and address qualifier, encoded same
14323  * way as a scsi target number.
14324  *
14325  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14326  */
14327 
14328 static dev_info_t *
14329 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14330 {
14331 	dev_info_t	*cdip = NULL;
14332 	int		target, tgt;
14333 	int 		circ;
14334 
14335 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14336 
14337 	ndi_devi_enter(dip, &circ);
14338 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14339 		dev_info_t *next = ddi_get_next_sibling(cdip);
14340 
14341 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14342 		    DDI_PROP_DONTPASS, "target", -1);
14343 		if (tgt == -1) {
14344 			/*
14345 			 * This is actually an error condition, but not
14346 			 * a fatal one. Just continue the search.
14347 			 */
14348 			cdip = next;
14349 			continue;
14350 		}
14351 
14352 		if (tgt == target)
14353 			break;
14354 
14355 		cdip = next;
14356 	}
14357 	ndi_devi_exit(dip, circ);
14358 
14359 	return (cdip);
14360 }
14361 
14362 /*
14363  * Process sata port disconnect request.
14364  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14365  * before this request. Nevertheless, if a device is still configured,
14366  * we need to attempt to offline and unconfigure device.
14367  * Regardless of the unconfigure operation results the port is marked as
14368  * deactivated and no access to the attached device is possible.
14369  * If the target node remains because unconfigure operation failed, its state
14370  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14371  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14372  * the device and remove old target node.
14373  *
14374  * This function invokes sata_hba_inst->satahba_tran->
14375  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14376  * If successful, the device structure (if any) attached to the specified port
14377  * is removed and state of the port marked appropriately.
14378  * Failure of the port_deactivate may keep port in the physically active state,
14379  * or may fail the port.
14380  *
14381  * NOTE: Port multiplier is supported.
14382  */
14383 
14384 static int
14385 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14386     sata_device_t *sata_device)
14387 {
14388 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14389 	sata_cport_info_t *cportinfo = NULL;
14390 	sata_pmport_info_t *pmportinfo = NULL;
14391 	sata_pmult_info_t *pmultinfo = NULL;
14392 	sata_device_t subsdevice;
14393 	int cport, pmport, qual;
14394 	int rval = SATA_SUCCESS;
14395 	int npmport = 0;
14396 	int rv = 0;
14397 
14398 	cport = sata_device->satadev_addr.cport;
14399 	pmport = sata_device->satadev_addr.pmport;
14400 	qual = sata_device->satadev_addr.qual;
14401 
14402 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14403 	if (qual == SATA_ADDR_DCPORT)
14404 		qual = SATA_ADDR_CPORT;
14405 	else
14406 		qual = SATA_ADDR_PMPORT;
14407 
14408 	/*
14409 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14410 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14411 	 * Do the sanity check.
14412 	 */
14413 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14414 		/* No physical port deactivation supported. */
14415 		return (EINVAL);
14416 	}
14417 
14418 	/* Check the current state of the port */
14419 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14420 	    (SATA_DIP(sata_hba_inst), sata_device);
14421 
14422 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14423 
14424 	/*
14425 	 * Processing port mulitiplier
14426 	 */
14427 	if (qual == SATA_ADDR_CPORT &&
14428 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14429 		mutex_enter(&cportinfo->cport_mutex);
14430 
14431 		/* Check controller port status */
14432 		sata_update_port_info(sata_hba_inst, sata_device);
14433 		if (rval != SATA_SUCCESS ||
14434 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14435 			/*
14436 			 * Device port status is unknown or it is in failed
14437 			 * state
14438 			 */
14439 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14440 			    SATA_PSTATE_FAILED;
14441 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14442 			    "sata_hba_ioctl: connect: failed to deactivate "
14443 			    "SATA port %d", cport);
14444 			mutex_exit(&cportinfo->cport_mutex);
14445 			return (EIO);
14446 		}
14447 
14448 		/* Disconnect all sub-devices. */
14449 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14450 		if (pmultinfo != NULL) {
14451 
14452 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14453 			    sata_hba_inst, cport); npmport ++) {
14454 				subsdinfo = SATA_PMPORT_DRV_INFO(
14455 				    sata_hba_inst, cport, npmport);
14456 				if (subsdinfo == NULL)
14457 					continue;
14458 
14459 				subsdevice.satadev_addr = subsdinfo->
14460 				    satadrv_addr;
14461 
14462 				mutex_exit(&cportinfo->cport_mutex);
14463 				if (sata_ioctl_disconnect(sata_hba_inst,
14464 				    &subsdevice) == SATA_SUCCESS) {
14465 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14466 					"[Remove] device at port %d:%d "
14467 					"successfully.", cport, npmport);
14468 				}
14469 				mutex_enter(&cportinfo->cport_mutex);
14470 			}
14471 		}
14472 
14473 		/* Disconnect the port multiplier */
14474 		cportinfo->cport_state &= ~SATA_STATE_READY;
14475 		mutex_exit(&cportinfo->cport_mutex);
14476 
14477 		sata_device->satadev_addr.qual = qual;
14478 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14479 		    (SATA_DIP(sata_hba_inst), sata_device);
14480 
14481 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14482 		    SE_NO_HINT);
14483 
14484 		mutex_enter(&cportinfo->cport_mutex);
14485 		sata_update_port_info(sata_hba_inst, sata_device);
14486 		if (rval != SATA_SUCCESS &&
14487 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14488 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14489 			rv = EIO;
14490 		} else {
14491 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14492 		}
14493 		mutex_exit(&cportinfo->cport_mutex);
14494 
14495 		return (rv);
14496 	}
14497 
14498 	/*
14499 	 * Process non-port-multiplier device - it could be a drive connected
14500 	 * to a port multiplier port or a controller port.
14501 	 */
14502 	if (qual == SATA_ADDR_PMPORT) {
14503 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14504 		mutex_enter(&pmportinfo->pmport_mutex);
14505 		sata_update_pmport_info(sata_hba_inst, sata_device);
14506 		if (rval != SATA_SUCCESS ||
14507 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14508 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14509 			    SATA_PSTATE_FAILED;
14510 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14511 			    "sata_hba_ioctl: connect: failed to deactivate "
14512 			    "SATA port %d:%d", cport, pmport);
14513 			mutex_exit(&pmportinfo->pmport_mutex);
14514 			return (EIO);
14515 		}
14516 
14517 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14518 			sdinfo = pmportinfo->pmport_sata_drive;
14519 			ASSERT(sdinfo != NULL);
14520 		}
14521 
14522 		/*
14523 		 * Set port's dev_state to not ready - this will disable
14524 		 * an access to a potentially attached device.
14525 		 */
14526 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14527 
14528 		/* Remove and release sata_drive info structure. */
14529 		if (sdinfo != NULL) {
14530 			if ((sdinfo->satadrv_type &
14531 			    SATA_VALID_DEV_TYPE) != 0) {
14532 				/*
14533 				 * If a target node exists, try to offline
14534 				 * a device and remove target node.
14535 				 */
14536 				mutex_exit(&pmportinfo->pmport_mutex);
14537 				(void) sata_offline_device(sata_hba_inst,
14538 				    sata_device, sdinfo);
14539 				mutex_enter(&pmportinfo->pmport_mutex);
14540 			}
14541 
14542 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14543 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14544 			(void) kmem_free((void *)sdinfo,
14545 			    sizeof (sata_drive_info_t));
14546 		}
14547 		mutex_exit(&pmportinfo->pmport_mutex);
14548 
14549 	} else if (qual == SATA_ADDR_CPORT) {
14550 		mutex_enter(&cportinfo->cport_mutex);
14551 		sata_update_port_info(sata_hba_inst, sata_device);
14552 		if (rval != SATA_SUCCESS ||
14553 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14554 			/*
14555 			 * Device port status is unknown or it is in failed
14556 			 * state
14557 			 */
14558 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14559 			    SATA_PSTATE_FAILED;
14560 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14561 			    "sata_hba_ioctl: connect: failed to deactivate "
14562 			    "SATA port %d", cport);
14563 			mutex_exit(&cportinfo->cport_mutex);
14564 			return (EIO);
14565 		}
14566 
14567 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14568 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14569 			ASSERT(pmultinfo != NULL);
14570 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14571 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14572 			ASSERT(sdinfo != NULL);
14573 		}
14574 		cportinfo->cport_state &= ~SATA_STATE_READY;
14575 
14576 		if (sdinfo != NULL) {
14577 			if ((sdinfo->satadrv_type &
14578 			    SATA_VALID_DEV_TYPE) != 0) {
14579 				/*
14580 				 * If a target node exists, try to offline
14581 				 * a device and remove target node.
14582 				 */
14583 				mutex_exit(&cportinfo->cport_mutex);
14584 				(void) sata_offline_device(sata_hba_inst,
14585 				    sata_device, sdinfo);
14586 				mutex_enter(&cportinfo->cport_mutex);
14587 			}
14588 
14589 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14590 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14591 			(void) kmem_free((void *)sdinfo,
14592 			    sizeof (sata_drive_info_t));
14593 		}
14594 		mutex_exit(&cportinfo->cport_mutex);
14595 	}
14596 
14597 	/* Just ask HBA driver to deactivate port */
14598 	sata_device->satadev_addr.qual = qual;
14599 
14600 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14601 	    (SATA_DIP(sata_hba_inst), sata_device);
14602 
14603 	/*
14604 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14605 	 * without the hint (to force listener to investivate the state).
14606 	 */
14607 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14608 	    SE_NO_HINT);
14609 
14610 	if (qual == SATA_ADDR_PMPORT) {
14611 		mutex_enter(&pmportinfo->pmport_mutex);
14612 		sata_update_pmport_info(sata_hba_inst, sata_device);
14613 
14614 		if (rval != SATA_SUCCESS &&
14615 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14616 			/*
14617 			 * Port deactivation failure - do not change port
14618 			 * state unless the state returned by HBA indicates a
14619 			 * port failure.
14620 			 *
14621 			 * NOTE: device structures were released, so devices
14622 			 * now are invisible! Port reset is needed to
14623 			 * re-enumerate devices.
14624 			 */
14625 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14626 			rv = EIO;
14627 		} else {
14628 			/*
14629 			 * Deactivation succeded. From now on the sata framework
14630 			 * will not care what is happening to the device, until
14631 			 * the port is activated again.
14632 			 */
14633 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14634 		}
14635 		mutex_exit(&pmportinfo->pmport_mutex);
14636 	} else if (qual == SATA_ADDR_CPORT) {
14637 		mutex_enter(&cportinfo->cport_mutex);
14638 		sata_update_port_info(sata_hba_inst, sata_device);
14639 
14640 		if (rval != SATA_SUCCESS &&
14641 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14642 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14643 			rv = EIO;
14644 		} else {
14645 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14646 		}
14647 		mutex_exit(&cportinfo->cport_mutex);
14648 	}
14649 
14650 	return (rv);
14651 }
14652 
14653 
14654 
14655 /*
14656  * Process sata port connect request
14657  * The sata cfgadm pluging will invoke this operation only if port was found
14658  * in the disconnect state (failed state is also treated as the disconnected
14659  * state).
14660  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14661  * sata_tran_hotplug_ops->sata_tran_port_activate().
14662  * If successful and a device is found attached to the port,
14663  * the initialization sequence is executed to attach a device structure to
14664  * a port structure. The state of the port and a device would be set
14665  * appropriately.
14666  * The device is not set in configured state (system-wise) by this operation.
14667  *
14668  * Note, that activating the port may generate link events,
14669  * so it is important that following processing and the
14670  * event processing does not interfere with each other!
14671  *
14672  * This operation may remove port failed state and will
14673  * try to make port active and in good standing.
14674  *
14675  * NOTE: Port multiplier is supported.
14676  */
14677 
14678 static int
14679 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14680     sata_device_t *sata_device)
14681 {
14682 	sata_pmport_info_t	*pmportinfo = NULL;
14683 	uint8_t cport, pmport, qual;
14684 	int rv = 0;
14685 
14686 	cport = sata_device->satadev_addr.cport;
14687 	pmport = sata_device->satadev_addr.pmport;
14688 	qual = sata_device->satadev_addr.qual;
14689 
14690 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14691 	if (qual == SATA_ADDR_DCPORT)
14692 		qual = SATA_ADDR_CPORT;
14693 	else
14694 		qual = SATA_ADDR_PMPORT;
14695 
14696 	if (qual == SATA_ADDR_PMPORT)
14697 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14698 
14699 	/*
14700 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14701 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14702 	 * Perform sanity check now.
14703 	 */
14704 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14705 		/* No physical port activation supported. */
14706 		return (EINVAL);
14707 	}
14708 
14709 	/* Just ask HBA driver to activate port */
14710 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14711 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14712 		/*
14713 		 * Port activation failure.
14714 		 */
14715 		if (qual == SATA_ADDR_CPORT) {
14716 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14717 			    cport)->cport_mutex);
14718 			sata_update_port_info(sata_hba_inst, sata_device);
14719 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14720 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14721 				    SATA_PSTATE_FAILED;
14722 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14723 				    "sata_hba_ioctl: connect: failed to "
14724 				    "activate SATA port %d", cport);
14725 			}
14726 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14727 			    cport)->cport_mutex);
14728 		} else { /* port multiplier device port */
14729 			mutex_enter(&pmportinfo->pmport_mutex);
14730 			sata_update_pmport_info(sata_hba_inst, sata_device);
14731 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14732 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14733 				    pmport) = SATA_PSTATE_FAILED;
14734 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14735 				    "sata_hba_ioctl: connect: failed to "
14736 				    "activate SATA port %d:%d", cport, pmport);
14737 			}
14738 			mutex_exit(&pmportinfo->pmport_mutex);
14739 		}
14740 		return (EIO);
14741 	}
14742 
14743 	/* Virgin port state - will be updated by the port re-probe. */
14744 	if (qual == SATA_ADDR_CPORT) {
14745 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14746 		    cport)->cport_mutex);
14747 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14748 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14749 		    cport)->cport_mutex);
14750 	} else { /* port multiplier device port */
14751 		mutex_enter(&pmportinfo->pmport_mutex);
14752 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14753 		mutex_exit(&pmportinfo->pmport_mutex);
14754 	}
14755 
14756 	/*
14757 	 * Probe the port to find its state and attached device.
14758 	 */
14759 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14760 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14761 		rv = EIO;
14762 
14763 	/*
14764 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14765 	 * without the hint
14766 	 */
14767 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14768 	    SE_NO_HINT);
14769 
14770 	/*
14771 	 * If there is a device attached to the port, emit
14772 	 * a message.
14773 	 */
14774 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14775 
14776 		if (qual == SATA_ADDR_CPORT) {
14777 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14778 				sata_log(sata_hba_inst, CE_WARN,
14779 				    "SATA port multiplier detected "
14780 				    "at port %d", cport);
14781 			} else {
14782 				sata_log(sata_hba_inst, CE_WARN,
14783 				    "SATA device detected at port %d", cport);
14784 				if (sata_device->satadev_type ==
14785 				    SATA_DTYPE_UNKNOWN) {
14786 				/*
14787 				 * A device was not successfully identified
14788 				 */
14789 				sata_log(sata_hba_inst, CE_WARN,
14790 				    "Could not identify SATA "
14791 				    "device at port %d", cport);
14792 				}
14793 			}
14794 		} else { /* port multiplier device port */
14795 			sata_log(sata_hba_inst, CE_WARN,
14796 			    "SATA device detected at port %d:%d",
14797 			    cport, pmport);
14798 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14799 				/*
14800 				 * A device was not successfully identified
14801 				 */
14802 				sata_log(sata_hba_inst, CE_WARN,
14803 				    "Could not identify SATA "
14804 				    "device at port %d:%d", cport, pmport);
14805 			}
14806 		}
14807 	}
14808 
14809 	return (rv);
14810 }
14811 
14812 
14813 /*
14814  * Process sata device unconfigure request.
14815  * The unconfigure operation uses generic nexus operation to
14816  * offline a device. It leaves a target device node attached.
14817  * and obviously sata_drive_info attached as well, because
14818  * from the hardware point of view nothing has changed.
14819  */
14820 static int
14821 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14822     sata_device_t *sata_device)
14823 {
14824 	int rv = 0;
14825 	dev_info_t *tdip;
14826 
14827 	/* We are addressing attached device, not a port */
14828 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14829 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14830 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14831 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14832 
14833 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14834 	    &sata_device->satadev_addr)) != NULL) {
14835 
14836 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14837 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14838 			    "sata_hba_ioctl: unconfigure: "
14839 			    "failed to unconfigure device at SATA port %d:%d",
14840 			    sata_device->satadev_addr.cport,
14841 			    sata_device->satadev_addr.pmport));
14842 			rv = EIO;
14843 		}
14844 		/*
14845 		 * The target node devi_state should be marked with
14846 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14847 		 * This would be the indication for cfgadm that
14848 		 * the AP node occupant state is 'unconfigured'.
14849 		 */
14850 
14851 	} else {
14852 		/*
14853 		 * This would indicate a failure on the part of cfgadm
14854 		 * to detect correct state of the node prior to this
14855 		 * call - one cannot unconfigure non-existing device.
14856 		 */
14857 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14858 		    "sata_hba_ioctl: unconfigure: "
14859 		    "attempt to unconfigure non-existing device "
14860 		    "at SATA port %d:%d",
14861 		    sata_device->satadev_addr.cport,
14862 		    sata_device->satadev_addr.pmport));
14863 		rv = ENXIO;
14864 	}
14865 	return (rv);
14866 }
14867 
14868 /*
14869  * Process sata device configure request
14870  * If port is in a failed state, operation is aborted - one has to use
14871  * an explicit connect or port activate request to try to get a port into
14872  * non-failed mode. Port reset wil also work in such situation.
14873  * If the port is in disconnected (shutdown) state, the connect operation is
14874  * attempted prior to any other action.
14875  * When port is in the active state, there is a device attached and the target
14876  * node exists, a device was most likely offlined.
14877  * If target node does not exist, a new target node is created. In both cases
14878  * an attempt is made to online (configure) the device.
14879  *
14880  * NOTE: Port multiplier is supported.
14881  */
14882 static int
14883 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14884     sata_device_t *sata_device)
14885 {
14886 	int cport, pmport, qual;
14887 	int rval;
14888 	boolean_t target = B_TRUE;
14889 	sata_cport_info_t *cportinfo;
14890 	sata_pmport_info_t *pmportinfo = NULL;
14891 	dev_info_t *tdip;
14892 	sata_drive_info_t *sdinfo;
14893 
14894 	cport = sata_device->satadev_addr.cport;
14895 	pmport = sata_device->satadev_addr.pmport;
14896 	qual = sata_device->satadev_addr.qual;
14897 
14898 	/* Get current port state */
14899 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14900 	    (SATA_DIP(sata_hba_inst), sata_device);
14901 
14902 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14903 	if (qual == SATA_ADDR_DPMPORT) {
14904 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14905 		mutex_enter(&pmportinfo->pmport_mutex);
14906 		sata_update_pmport_info(sata_hba_inst, sata_device);
14907 		if (rval != SATA_SUCCESS ||
14908 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14909 			/*
14910 			 * Obviously, device on a failed port is not visible
14911 			 */
14912 			mutex_exit(&pmportinfo->pmport_mutex);
14913 			return (ENXIO);
14914 		}
14915 		mutex_exit(&pmportinfo->pmport_mutex);
14916 	} else {
14917 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14918 		    cport)->cport_mutex);
14919 		sata_update_port_info(sata_hba_inst, sata_device);
14920 		if (rval != SATA_SUCCESS ||
14921 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14922 			/*
14923 			 * Obviously, device on a failed port is not visible
14924 			 */
14925 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14926 			    cport)->cport_mutex);
14927 			return (ENXIO);
14928 		}
14929 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14930 		    cport)->cport_mutex);
14931 	}
14932 
14933 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14934 		/* need to activate port */
14935 		target = B_FALSE;
14936 
14937 		/* Sanity check */
14938 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14939 			return (ENXIO);
14940 
14941 		/* Just let HBA driver to activate port */
14942 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14943 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14944 			/*
14945 			 * Port activation failure - do not change port state
14946 			 * unless the state returned by HBA indicates a port
14947 			 * failure.
14948 			 */
14949 			if (qual == SATA_ADDR_DPMPORT) {
14950 				mutex_enter(&pmportinfo->pmport_mutex);
14951 				sata_update_pmport_info(sata_hba_inst,
14952 				    sata_device);
14953 				if (sata_device->satadev_state &
14954 				    SATA_PSTATE_FAILED)
14955 					pmportinfo->pmport_state =
14956 					    SATA_PSTATE_FAILED;
14957 				mutex_exit(&pmportinfo->pmport_mutex);
14958 			} else {
14959 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14960 				    cport)->cport_mutex);
14961 				sata_update_port_info(sata_hba_inst,
14962 				    sata_device);
14963 				if (sata_device->satadev_state &
14964 				    SATA_PSTATE_FAILED)
14965 					cportinfo->cport_state =
14966 					    SATA_PSTATE_FAILED;
14967 				mutex_exit(&SATA_CPORT_INFO(
14968 				    sata_hba_inst, cport)->cport_mutex);
14969 			}
14970 		}
14971 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14972 		    "sata_hba_ioctl: configure: "
14973 		    "failed to activate SATA port %d:%d",
14974 		    cport, pmport));
14975 		return (EIO);
14976 	}
14977 	/*
14978 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14979 	 * without the hint.
14980 	 */
14981 	sata_gen_sysevent(sata_hba_inst,
14982 	    &sata_device->satadev_addr, SE_NO_HINT);
14983 
14984 	/* Virgin port state */
14985 	if (qual == SATA_ADDR_DPMPORT) {
14986 		mutex_enter(&pmportinfo->pmport_mutex);
14987 		pmportinfo->pmport_state = 0;
14988 		mutex_exit(&pmportinfo->pmport_mutex);
14989 	} else {
14990 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14991 		    cport)-> cport_mutex);
14992 		cportinfo->cport_state = 0;
14993 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14994 		    cport)->cport_mutex);
14995 	}
14996 	/*
14997 	 * Always reprobe port, to get current device info.
14998 	 */
14999 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15000 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15001 		return (EIO);
15002 
15003 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15004 		if (qual == SATA_ADDR_DPMPORT) {
15005 			/*
15006 			 * That's the transition from "inactive" port
15007 			 * to active one with device attached.
15008 			 */
15009 			sata_log(sata_hba_inst, CE_WARN,
15010 			    "SATA device detected at port %d:%d",
15011 			    cport, pmport);
15012 		} else {
15013 			/*
15014 			 * When PM is attached to the cport and cport is
15015 			 * activated, every PM device port needs to be reprobed.
15016 			 * We need to emit message for all devices detected
15017 			 * at port multiplier's device ports.
15018 			 * Add such code here.
15019 			 * For now, just inform about device attached to
15020 			 * cport.
15021 			 */
15022 			sata_log(sata_hba_inst, CE_WARN,
15023 			    "SATA device detected at port %d", cport);
15024 		}
15025 	}
15026 
15027 	/*
15028 	 * This is where real configuration operation starts.
15029 	 *
15030 	 * When PM is attached to the cport and cport is activated,
15031 	 * devices attached PM device ports may have to be configured
15032 	 * explicitly. This may change when port multiplier is supported.
15033 	 * For now, configure only disks and other valid target devices.
15034 	 */
15035 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15036 		if (qual == SATA_ADDR_DCPORT) {
15037 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15038 				/*
15039 				 * A device was not successfully identified
15040 				 */
15041 				sata_log(sata_hba_inst, CE_WARN,
15042 				    "Could not identify SATA "
15043 				    "device at port %d", cport);
15044 			}
15045 		} else { /* port multiplier device port */
15046 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15047 				/*
15048 				 * A device was not successfully identified
15049 				 */
15050 				sata_log(sata_hba_inst, CE_WARN,
15051 				    "Could not identify SATA "
15052 				    "device at port %d:%d", cport, pmport);
15053 			}
15054 		}
15055 		return (ENXIO);		/* No device to configure */
15056 	}
15057 
15058 	/*
15059 	 * Here we may have a device in reset condition,
15060 	 * but because we are just configuring it, there is
15061 	 * no need to process the reset other than just
15062 	 * to clear device reset condition in the HBA driver.
15063 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15064 	 * cause a first command sent the HBA driver with the request
15065 	 * to clear device reset condition.
15066 	 */
15067 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15068 	if (qual == SATA_ADDR_DPMPORT)
15069 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15070 	else
15071 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15072 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15073 	if (sdinfo == NULL) {
15074 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15075 		return (ENXIO);
15076 	}
15077 	if (sdinfo->satadrv_event_flags &
15078 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15079 		sdinfo->satadrv_event_flags = 0;
15080 	}
15081 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15082 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15083 
15084 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15085 	    &sata_device->satadev_addr)) != NULL) {
15086 		/*
15087 		 * Target node exists. Verify, that it belongs
15088 		 * to existing, attached device and not to
15089 		 * a removed device.
15090 		 */
15091 		if (sata_check_device_removed(tdip) == B_TRUE) {
15092 			if (qual == SATA_ADDR_DPMPORT)
15093 				sata_log(sata_hba_inst, CE_WARN,
15094 				    "SATA device at port %d cannot be "
15095 				    "configured. "
15096 				    "Application(s) accessing "
15097 				    "previously attached device "
15098 				    "have to release it before newly "
15099 				    "inserted device can be made accessible.",
15100 				    cport);
15101 			else
15102 				sata_log(sata_hba_inst, CE_WARN,
15103 				    "SATA device at port %d:%d cannot be"
15104 				    "configured. "
15105 				    "Application(s) accessing "
15106 				    "previously attached device "
15107 				    "have to release it before newly "
15108 				    "inserted device can be made accessible.",
15109 				    cport, pmport);
15110 			return (EIO);
15111 		}
15112 		/*
15113 		 * Device was not removed and re-inserted.
15114 		 * Try to online it.
15115 		 */
15116 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15117 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15118 			    "sata_hba_ioctl: configure: "
15119 			    "onlining device at SATA port "
15120 			    "%d:%d failed", cport, pmport));
15121 			return (EIO);
15122 		}
15123 
15124 		if (qual == SATA_ADDR_DPMPORT) {
15125 			mutex_enter(&pmportinfo->pmport_mutex);
15126 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15127 			mutex_exit(&pmportinfo->pmport_mutex);
15128 		} else {
15129 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15130 			    cport)->cport_mutex);
15131 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15132 			mutex_exit(&SATA_CPORT_INFO(
15133 			    sata_hba_inst, cport)->cport_mutex);
15134 		}
15135 	} else {
15136 		/*
15137 		 * No target node - need to create a new target node.
15138 		 */
15139 		if (qual == SATA_ADDR_DPMPORT) {
15140 			mutex_enter(&pmportinfo->pmport_mutex);
15141 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15142 			mutex_exit(&pmportinfo->pmport_mutex);
15143 		} else {
15144 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15145 			    cport_mutex);
15146 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15147 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15148 			    cport_mutex);
15149 		}
15150 
15151 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15152 		    sata_hba_inst, &sata_device->satadev_addr);
15153 		if (tdip == NULL) {
15154 			/* Configure operation failed */
15155 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15156 			    "sata_hba_ioctl: configure: "
15157 			    "configuring SATA device at port %d:%d "
15158 			    "failed", cport, pmport));
15159 			return (EIO);
15160 		}
15161 	}
15162 	return (0);
15163 }
15164 
15165 
15166 /*
15167  * Process ioctl deactivate port request.
15168  * Arbitrarily unconfigure attached device, if any.
15169  * Even if the unconfigure fails, proceed with the
15170  * port deactivation.
15171  *
15172  * NOTE: Port Multiplier is supported now.
15173  */
15174 
15175 static int
15176 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15177     sata_device_t *sata_device)
15178 {
15179 	int cport, pmport, qual;
15180 	int rval, rv = 0;
15181 	int npmport;
15182 	sata_cport_info_t *cportinfo;
15183 	sata_pmport_info_t *pmportinfo;
15184 	sata_pmult_info_t *pmultinfo;
15185 	dev_info_t *tdip;
15186 	sata_drive_info_t *sdinfo = NULL;
15187 	sata_device_t subsdevice;
15188 
15189 	/* Sanity check */
15190 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15191 		return (ENOTSUP);
15192 
15193 	cport = sata_device->satadev_addr.cport;
15194 	pmport = sata_device->satadev_addr.pmport;
15195 	qual = sata_device->satadev_addr.qual;
15196 
15197 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15198 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15199 	if (qual == SATA_ADDR_DCPORT)
15200 		qual = SATA_ADDR_CPORT;
15201 	else
15202 		qual = SATA_ADDR_PMPORT;
15203 
15204 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15205 	if (qual == SATA_ADDR_PMPORT)
15206 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15207 
15208 	/*
15209 	 * Processing port multiplier
15210 	 */
15211 	if (qual == SATA_ADDR_CPORT &&
15212 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15213 		mutex_enter(&cportinfo->cport_mutex);
15214 
15215 		/* Deactivate all sub-deices */
15216 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15217 		if (pmultinfo != NULL) {
15218 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15219 			    sata_hba_inst, cport); npmport++) {
15220 
15221 				subsdevice.satadev_addr.cport = cport;
15222 				subsdevice.satadev_addr.pmport =
15223 				    (uint8_t)npmport;
15224 				subsdevice.satadev_addr.qual =
15225 				    SATA_ADDR_DPMPORT;
15226 
15227 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15228 				    "sata_hba_ioctl: deactivate: trying to "
15229 				    "deactivate SATA port %d:%d",
15230 				    cport, npmport);
15231 
15232 				mutex_exit(&cportinfo->cport_mutex);
15233 				if (sata_ioctl_deactivate(sata_hba_inst,
15234 				    &subsdevice) == SATA_SUCCESS) {
15235 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15236 					    "[Deactivate] device at port %d:%d "
15237 					    "successfully.", cport, npmport);
15238 				}
15239 				mutex_enter(&cportinfo->cport_mutex);
15240 			}
15241 		}
15242 
15243 		/* Deactivate the port multiplier now. */
15244 		cportinfo->cport_state &= ~SATA_STATE_READY;
15245 		mutex_exit(&cportinfo->cport_mutex);
15246 
15247 		sata_device->satadev_addr.qual = qual;
15248 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15249 		    (SATA_DIP(sata_hba_inst), sata_device);
15250 
15251 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15252 		    SE_NO_HINT);
15253 
15254 		mutex_enter(&cportinfo->cport_mutex);
15255 		sata_update_port_info(sata_hba_inst, sata_device);
15256 		if (rval != SATA_SUCCESS) {
15257 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15258 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15259 			}
15260 			rv = EIO;
15261 		} else {
15262 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15263 		}
15264 		mutex_exit(&cportinfo->cport_mutex);
15265 
15266 		return (rv);
15267 	}
15268 
15269 	/*
15270 	 * Process non-port-multiplier device - it could be a drive connected
15271 	 * to a port multiplier port or a controller port.
15272 	 */
15273 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15274 	if (qual == SATA_ADDR_CPORT) {
15275 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15276 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15277 			/* deal only with valid devices */
15278 			if ((cportinfo->cport_dev_type &
15279 			    SATA_VALID_DEV_TYPE) != 0)
15280 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15281 		}
15282 		cportinfo->cport_state &= ~SATA_STATE_READY;
15283 	} else {
15284 		/* Port multiplier device port */
15285 		mutex_enter(&pmportinfo->pmport_mutex);
15286 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15287 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15288 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15289 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15290 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15291 		mutex_exit(&pmportinfo->pmport_mutex);
15292 	}
15293 
15294 	if (sdinfo != NULL) {
15295 		/*
15296 		 * If a target node exists, try to offline a device and
15297 		 * to remove a target node.
15298 		 */
15299 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15300 		    cport_mutex);
15301 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15302 		    &sata_device->satadev_addr);
15303 		if (tdip != NULL) {
15304 			/* target node exist */
15305 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15306 			    "sata_hba_ioctl: port deactivate: "
15307 			    "target node exists.", NULL);
15308 
15309 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15310 			    NDI_SUCCESS) {
15311 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15312 				    "sata_hba_ioctl: port deactivate: "
15313 				    "failed to unconfigure device at port "
15314 				    "%d:%d before deactivating the port",
15315 				    cport, pmport));
15316 				/*
15317 				 * Set DEVICE REMOVED state in the target
15318 				 * node. It will prevent an access to
15319 				 * the device even when a new device is
15320 				 * attached, until the old target node is
15321 				 * released, removed and recreated for a new
15322 				 * device.
15323 				 */
15324 				sata_set_device_removed(tdip);
15325 
15326 				/*
15327 				 * Instruct the event daemon to try the
15328 				 * target node cleanup later.
15329 				 */
15330 				sata_set_target_node_cleanup(sata_hba_inst,
15331 				    &sata_device->satadev_addr);
15332 			}
15333 		}
15334 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15335 		    cport_mutex);
15336 		/*
15337 		 * In any case, remove and release sata_drive_info
15338 		 * structure.
15339 		 */
15340 		if (qual == SATA_ADDR_CPORT) {
15341 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15342 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15343 		} else { /* port multiplier device port */
15344 			mutex_enter(&pmportinfo->pmport_mutex);
15345 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15346 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15347 			mutex_exit(&pmportinfo->pmport_mutex);
15348 		}
15349 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15350 	}
15351 
15352 	if (qual == SATA_ADDR_CPORT) {
15353 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15354 		    SATA_STATE_PROBING);
15355 	} else if (qual == SATA_ADDR_PMPORT) {
15356 		mutex_enter(&pmportinfo->pmport_mutex);
15357 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15358 		    SATA_STATE_PROBING);
15359 		mutex_exit(&pmportinfo->pmport_mutex);
15360 	}
15361 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15362 
15363 	/* Just let HBA driver to deactivate port */
15364 	sata_device->satadev_addr.qual = qual;
15365 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15366 	    (SATA_DIP(sata_hba_inst), sata_device);
15367 
15368 	/*
15369 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15370 	 * without the hint
15371 	 */
15372 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15373 	    SE_NO_HINT);
15374 
15375 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15376 	sata_update_port_info(sata_hba_inst, sata_device);
15377 	if (qual == SATA_ADDR_CPORT) {
15378 		if (rval != SATA_SUCCESS) {
15379 			/*
15380 			 * Port deactivation failure - do not change port state
15381 			 * unless the state returned by HBA indicates a port
15382 			 * failure.
15383 			 */
15384 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15385 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15386 				    SATA_PSTATE_FAILED;
15387 			}
15388 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15389 			    "sata_hba_ioctl: port deactivate: "
15390 			    "cannot deactivate SATA port %d", cport));
15391 			rv = EIO;
15392 		} else {
15393 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15394 		}
15395 	} else {
15396 		mutex_enter(&pmportinfo->pmport_mutex);
15397 		if (rval != SATA_SUCCESS) {
15398 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15399 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15400 				    pmport) = SATA_PSTATE_FAILED;
15401 			}
15402 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15403 			    "sata_hba_ioctl: port deactivate: "
15404 			    "cannot deactivate SATA port %d:%d",
15405 			    cport, pmport));
15406 			rv = EIO;
15407 		} else {
15408 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15409 		}
15410 		mutex_exit(&pmportinfo->pmport_mutex);
15411 	}
15412 
15413 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15414 
15415 	return (rv);
15416 }
15417 
15418 /*
15419  * Process ioctl port activate request.
15420  *
15421  * NOTE: Port multiplier is supported now.
15422  */
15423 static int
15424 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15425     sata_device_t *sata_device)
15426 {
15427 	int cport, pmport, qual;
15428 	sata_cport_info_t *cportinfo;
15429 	sata_pmport_info_t *pmportinfo = NULL;
15430 	boolean_t dev_existed = B_TRUE;
15431 
15432 	/* Sanity check */
15433 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15434 		return (ENOTSUP);
15435 
15436 	cport = sata_device->satadev_addr.cport;
15437 	pmport = sata_device->satadev_addr.pmport;
15438 	qual = sata_device->satadev_addr.qual;
15439 
15440 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15441 
15442 	/*
15443 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15444 	 * is a device. But what we are dealing with is port/pmport.
15445 	 */
15446 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15447 	if (qual == SATA_ADDR_DCPORT)
15448 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15449 	else
15450 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15451 
15452 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15453 	if (qual == SATA_ADDR_PMPORT) {
15454 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15455 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15456 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15457 			dev_existed = B_FALSE;
15458 	} else { /* cport */
15459 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15460 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15461 			dev_existed = B_FALSE;
15462 	}
15463 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15464 
15465 	/* Just let HBA driver to activate port, if necessary */
15466 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15467 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15468 		/*
15469 		 * Port activation failure - do not change port state unless
15470 		 * the state returned by HBA indicates a port failure.
15471 		 */
15472 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15473 		    cport)->cport_mutex);
15474 		sata_update_port_info(sata_hba_inst, sata_device);
15475 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15476 			if (qual == SATA_ADDR_PMPORT) {
15477 				mutex_enter(&pmportinfo->pmport_mutex);
15478 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15479 				mutex_exit(&pmportinfo->pmport_mutex);
15480 			} else
15481 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15482 
15483 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15484 			    cport)->cport_mutex);
15485 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15486 			    "sata_hba_ioctl: port activate: cannot activate "
15487 			    "SATA port %d:%d", cport, pmport));
15488 			return (EIO);
15489 		}
15490 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15491 	}
15492 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15493 	if (qual == SATA_ADDR_PMPORT) {
15494 		mutex_enter(&pmportinfo->pmport_mutex);
15495 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15496 		mutex_exit(&pmportinfo->pmport_mutex);
15497 	} else
15498 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15499 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15500 
15501 	/*
15502 	 * Re-probe port to find its current state and possibly attached device.
15503 	 * Port re-probing may change the cportinfo device type if device is
15504 	 * found attached.
15505 	 * If port probing failed, the device type would be set to
15506 	 * SATA_DTYPE_NONE.
15507 	 */
15508 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15509 	    SATA_DEV_IDENTIFY_RETRY);
15510 
15511 	/*
15512 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15513 	 * without the hint.
15514 	 */
15515 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15516 	    SE_NO_HINT);
15517 
15518 	if (dev_existed == B_FALSE) {
15519 		if (qual == SATA_ADDR_PMPORT &&
15520 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15521 			/*
15522 			 * That's the transition from the "inactive" port state
15523 			 * or the active port without a device attached to the
15524 			 * active port state with a device attached.
15525 			 */
15526 			sata_log(sata_hba_inst, CE_WARN,
15527 			    "SATA device detected at port %d:%d",
15528 			    cport, pmport);
15529 		} else if (qual == SATA_ADDR_CPORT &&
15530 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15531 			/*
15532 			 * That's the transition from the "inactive" port state
15533 			 * or the active port without a device attached to the
15534 			 * active port state with a device attached.
15535 			 */
15536 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15537 				sata_log(sata_hba_inst, CE_WARN,
15538 				    "SATA device detected at port %d", cport);
15539 			} else {
15540 				sata_log(sata_hba_inst, CE_WARN,
15541 				    "SATA port multiplier detected at port %d",
15542 				    cport);
15543 			}
15544 		}
15545 	}
15546 	return (0);
15547 }
15548 
15549 
15550 
15551 /*
15552  * Process ioctl reset port request.
15553  *
15554  * NOTE: Port-Multiplier is supported.
15555  */
15556 static int
15557 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15558     sata_device_t *sata_device)
15559 {
15560 	int cport, pmport, qual;
15561 	int rv = 0;
15562 
15563 	cport = sata_device->satadev_addr.cport;
15564 	pmport = sata_device->satadev_addr.pmport;
15565 	qual = sata_device->satadev_addr.qual;
15566 
15567 	/*
15568 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15569 	 * is a device. But what we are dealing with is port/pmport.
15570 	 */
15571 	if (qual == SATA_ADDR_DCPORT)
15572 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15573 	else
15574 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15575 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15576 
15577 	/* Sanity check */
15578 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15579 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15580 		    "sata_hba_ioctl: sata_hba_tran missing required "
15581 		    "function sata_tran_reset_dport"));
15582 		return (ENOTSUP);
15583 	}
15584 
15585 	/* Ask HBA to reset port */
15586 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15587 	    sata_device) != SATA_SUCCESS) {
15588 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15589 		    "sata_hba_ioctl: reset port: failed %d:%d",
15590 		    cport, pmport));
15591 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15592 		    cport_mutex);
15593 		sata_update_port_info(sata_hba_inst, sata_device);
15594 		if (qual == SATA_ADDR_CPORT)
15595 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15596 			    SATA_PSTATE_FAILED;
15597 		else {
15598 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15599 			    pmport));
15600 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15601 			    SATA_PSTATE_FAILED;
15602 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15603 			    pmport));
15604 		}
15605 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15606 		    cport_mutex);
15607 		rv = EIO;
15608 	}
15609 
15610 	return (rv);
15611 }
15612 
15613 /*
15614  * Process ioctl reset device request.
15615  *
15616  * NOTE: Port multiplier is supported.
15617  */
15618 static int
15619 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15620     sata_device_t *sata_device)
15621 {
15622 	sata_drive_info_t *sdinfo = NULL;
15623 	sata_pmult_info_t *pmultinfo = NULL;
15624 	int cport, pmport;
15625 	int rv = 0;
15626 
15627 	/* Sanity check */
15628 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15629 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15630 		    "sata_hba_ioctl: sata_hba_tran missing required "
15631 		    "function sata_tran_reset_dport"));
15632 		return (ENOTSUP);
15633 	}
15634 
15635 	cport = sata_device->satadev_addr.cport;
15636 	pmport = sata_device->satadev_addr.pmport;
15637 
15638 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15639 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15640 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15641 		    SATA_DTYPE_PMULT)
15642 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15643 			    cport_devp.cport_sata_pmult;
15644 		else
15645 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15646 			    sata_device->satadev_addr.cport);
15647 	} else { /* port multiplier */
15648 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15649 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15650 		    sata_device->satadev_addr.cport,
15651 		    sata_device->satadev_addr.pmport);
15652 	}
15653 	if (sdinfo == NULL && pmultinfo == NULL) {
15654 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15655 		return (EINVAL);
15656 	}
15657 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15658 
15659 	/* Ask HBA to reset device */
15660 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15661 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15662 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15663 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15664 		    cport, pmport));
15665 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15666 		    cport_mutex);
15667 		sata_update_port_info(sata_hba_inst, sata_device);
15668 		/*
15669 		 * Device info structure remains attached. Another device reset
15670 		 * or port disconnect/connect and re-probing is
15671 		 * needed to change it's state
15672 		 */
15673 		if (sdinfo != NULL) {
15674 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15675 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15676 		} else if (pmultinfo != NULL) {
15677 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15678 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15679 		}
15680 
15681 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15682 		rv = EIO;
15683 	}
15684 	/*
15685 	 * If attached device was a port multiplier, some extra processing
15686 	 * may be needed to bring it back. SATA specification requies a
15687 	 * mandatory software reset on host port to reliably enumerate a port
15688 	 * multiplier, the HBA driver should handle that after reset
15689 	 * operation.
15690 	 */
15691 	return (rv);
15692 }
15693 
15694 
15695 /*
15696  * Process ioctl reset all request.
15697  */
15698 static int
15699 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15700 {
15701 	sata_device_t sata_device;
15702 	int rv = 0;
15703 	int tcport;
15704 
15705 	sata_device.satadev_rev = SATA_DEVICE_REV;
15706 
15707 	/*
15708 	 * There is no protection here for configured devices.
15709 	 */
15710 	/* Sanity check */
15711 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15712 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15713 		    "sata_hba_ioctl: sata_hba_tran missing required "
15714 		    "function sata_tran_reset_dport"));
15715 		return (ENOTSUP);
15716 	}
15717 
15718 	/*
15719 	 * Need to lock all ports, not just one.
15720 	 * If any port is locked by event processing, fail the whole operation.
15721 	 * One port is already locked, but for simplicity lock it again.
15722 	 */
15723 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15724 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15725 		    cport_mutex);
15726 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15727 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15728 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15729 			    cport_mutex);
15730 			rv = EBUSY;
15731 			break;
15732 		} else {
15733 			/*
15734 			 * It is enough to lock cport in command-based
15735 			 * switching mode.
15736 			 */
15737 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15738 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15739 		}
15740 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15741 		    cport_mutex);
15742 	}
15743 
15744 	if (rv == 0) {
15745 		/*
15746 		 * All cports were successfully locked.
15747 		 * Reset main SATA controller.
15748 		 * Set the device address to port 0, to have a valid device
15749 		 * address.
15750 		 */
15751 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15752 		sata_device.satadev_addr.cport = 0;
15753 		sata_device.satadev_addr.pmport = 0;
15754 
15755 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15756 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15757 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15758 			    "sata_hba_ioctl: reset controller failed"));
15759 			return (EIO);
15760 		}
15761 	}
15762 	/*
15763 	 * Unlock all ports
15764 	 */
15765 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15766 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15767 		    cport_mutex);
15768 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15769 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15770 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15771 		    cport_mutex);
15772 	}
15773 
15774 	/*
15775 	 * This operation returns EFAULT if either reset
15776 	 * controller failed or a re-probing of any port failed.
15777 	 */
15778 	return (rv);
15779 }
15780 
15781 
15782 /*
15783  * Process ioctl port self test request.
15784  *
15785  * NOTE: Port multiplier code is not completed nor tested.
15786  */
15787 static int
15788 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15789     sata_device_t *sata_device)
15790 {
15791 	int cport, pmport, qual;
15792 	int rv = 0;
15793 
15794 	/* Sanity check */
15795 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15796 		return (ENOTSUP);
15797 
15798 	cport = sata_device->satadev_addr.cport;
15799 	pmport = sata_device->satadev_addr.pmport;
15800 	qual = sata_device->satadev_addr.qual;
15801 
15802 	/*
15803 	 * There is no protection here for a configured
15804 	 * device attached to this port.
15805 	 */
15806 
15807 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15808 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15809 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15810 		    "sata_hba_ioctl: port selftest: "
15811 		    "failed port %d:%d", cport, pmport));
15812 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15813 		    cport_mutex);
15814 		sata_update_port_info(sata_hba_inst, sata_device);
15815 		if (qual == SATA_ADDR_CPORT)
15816 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15817 			    SATA_PSTATE_FAILED;
15818 		else { /* port multiplier device port */
15819 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15820 			    cport, pmport));
15821 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15822 			    SATA_PSTATE_FAILED;
15823 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15824 			    cport, pmport));
15825 		}
15826 
15827 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15828 		    cport_mutex);
15829 		return (EIO);
15830 	}
15831 	/*
15832 	 * Beacuse the port was reset in the course of testing, it should be
15833 	 * re-probed and attached device state should be restored. At this
15834 	 * point the port state is unknown - it's state is HBA-specific.
15835 	 * Force port re-probing to get it into a known state.
15836 	 */
15837 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15838 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15839 		rv = EIO;
15840 	return (rv);
15841 }
15842 
15843 
15844 /*
15845  * sata_cfgadm_state:
15846  * Use the sata port state and state of the target node to figure out
15847  * the cfgadm_state.
15848  *
15849  * The port argument is a value with encoded cport,
15850  * pmport and address qualifier, in the same manner as a scsi target number.
15851  * SCSI_TO_SATA_CPORT macro extracts cport number,
15852  * SCSI_TO_SATA_PMPORT extracts pmport number and
15853  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15854  *
15855  * Port multiplier is supported.
15856  */
15857 
15858 static void
15859 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15860     devctl_ap_state_t *ap_state)
15861 {
15862 	uint8_t		cport, pmport, qual;
15863 	uint32_t	port_state, pmult_state;
15864 	uint32_t	dev_type;
15865 	sata_drive_info_t *sdinfo;
15866 
15867 	cport = SCSI_TO_SATA_CPORT(port);
15868 	pmport = SCSI_TO_SATA_PMPORT(port);
15869 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15870 
15871 	/* Check cport state */
15872 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15873 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15874 	    port_state & SATA_PSTATE_FAILED) {
15875 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15876 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15877 		if (port_state & SATA_PSTATE_FAILED)
15878 			ap_state->ap_condition = AP_COND_FAILED;
15879 		else
15880 			ap_state->ap_condition = AP_COND_UNKNOWN;
15881 
15882 		return;
15883 	}
15884 
15885 	/* cport state is okay. Now check pmport state */
15886 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15887 		/* Sanity check */
15888 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15889 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15890 		    cport, pmport) == NULL)
15891 			return;
15892 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15893 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15894 		    port_state & SATA_PSTATE_FAILED) {
15895 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15896 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15897 			if (port_state & SATA_PSTATE_FAILED)
15898 				ap_state->ap_condition = AP_COND_FAILED;
15899 			else
15900 				ap_state->ap_condition = AP_COND_UNKNOWN;
15901 
15902 			return;
15903 		}
15904 	}
15905 
15906 	/* Port is enabled and ready */
15907 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15908 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15909 	else
15910 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15911 
15912 	switch (dev_type) {
15913 	case SATA_DTYPE_NONE:
15914 	{
15915 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15916 		ap_state->ap_condition = AP_COND_OK;
15917 		/* No device attached */
15918 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15919 		break;
15920 	}
15921 	case SATA_DTYPE_PMULT:
15922 	{
15923 		/* Need to check port multiplier state */
15924 		ASSERT(qual == SATA_ADDR_DCPORT);
15925 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15926 		    pmult_state;
15927 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15928 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15929 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15930 			if (pmult_state & SATA_PSTATE_FAILED)
15931 				ap_state->ap_condition = AP_COND_FAILED;
15932 			else
15933 				ap_state->ap_condition = AP_COND_UNKNOWN;
15934 
15935 			return;
15936 		}
15937 
15938 		/* Port multiplier is not configurable */
15939 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15940 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15941 		ap_state->ap_condition = AP_COND_OK;
15942 		break;
15943 	}
15944 
15945 	case SATA_DTYPE_ATADISK:
15946 	case SATA_DTYPE_ATAPICD:
15947 	case SATA_DTYPE_ATAPITAPE:
15948 	case SATA_DTYPE_ATAPIDISK:
15949 	{
15950 		dev_info_t *tdip = NULL;
15951 		dev_info_t *dip = NULL;
15952 		int circ;
15953 
15954 		dip = SATA_DIP(sata_hba_inst);
15955 		tdip = sata_get_target_dip(dip, cport, pmport);
15956 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15957 		if (tdip != NULL) {
15958 			ndi_devi_enter(dip, &circ);
15959 			mutex_enter(&(DEVI(tdip)->devi_lock));
15960 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
15961 				/*
15962 				 * There could be the case where previously
15963 				 * configured and opened device was removed
15964 				 * and unknown device was plugged.
15965 				 * In such case we want to show a device, and
15966 				 * its configured or unconfigured state but
15967 				 * indicate unusable condition untill the
15968 				 * old target node is released and removed.
15969 				 */
15970 				ap_state->ap_condition = AP_COND_UNUSABLE;
15971 			} else {
15972 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
15973 				    cport));
15974 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15975 				    cport);
15976 				if (sdinfo != NULL) {
15977 					if ((sdinfo->satadrv_state &
15978 					    SATA_DSTATE_FAILED) != 0)
15979 						ap_state->ap_condition =
15980 						    AP_COND_FAILED;
15981 					else
15982 						ap_state->ap_condition =
15983 						    AP_COND_OK;
15984 				} else {
15985 					ap_state->ap_condition =
15986 					    AP_COND_UNKNOWN;
15987 				}
15988 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
15989 				    cport));
15990 			}
15991 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
15992 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
15993 				ap_state->ap_ostate =
15994 				    AP_OSTATE_UNCONFIGURED;
15995 			} else {
15996 				ap_state->ap_ostate =
15997 				    AP_OSTATE_CONFIGURED;
15998 			}
15999 			mutex_exit(&(DEVI(tdip)->devi_lock));
16000 			ndi_devi_exit(dip, circ);
16001 		} else {
16002 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16003 			ap_state->ap_condition = AP_COND_UNKNOWN;
16004 		}
16005 		break;
16006 	}
16007 	case SATA_DTYPE_ATAPIPROC:
16008 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16009 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16010 		ap_state->ap_condition = AP_COND_OK;
16011 		break;
16012 	default:
16013 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16014 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16015 		ap_state->ap_condition = AP_COND_UNKNOWN;
16016 		/*
16017 		 * This is actually internal error condition (non fatal),
16018 		 * because we have already checked all defined device types.
16019 		 */
16020 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16021 		    "sata_cfgadm_state: Internal error: "
16022 		    "unknown device type"));
16023 		break;
16024 	}
16025 }
16026 
16027 
16028 /*
16029  * Process ioctl get device path request.
16030  *
16031  * NOTE: Port multiplier has no target dip. Devices connected to port
16032  * multiplier have target node attached to the HBA node. The only difference
16033  * between them and the directly-attached device node is a target address.
16034  */
16035 static int
16036 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16037     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16038 {
16039 	char path[MAXPATHLEN];
16040 	uint32_t size;
16041 	dev_info_t *tdip;
16042 
16043 	(void) strcpy(path, "/devices");
16044 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16045 	    &sata_device->satadev_addr)) == NULL) {
16046 		/*
16047 		 * No such device. If this is a request for a size, do not
16048 		 * return EINVAL for non-existing target, because cfgadm
16049 		 * will then indicate a meaningless ioctl failure.
16050 		 * If this is a request for a path, indicate invalid
16051 		 * argument.
16052 		 */
16053 		if (ioc->get_size == 0)
16054 			return (EINVAL);
16055 	} else {
16056 		(void) ddi_pathname(tdip, path + strlen(path));
16057 	}
16058 	size = strlen(path) + 1;
16059 
16060 	if (ioc->get_size != 0) {
16061 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16062 		    mode) != 0)
16063 			return (EFAULT);
16064 	} else {
16065 		if (ioc->bufsiz != size)
16066 			return (EINVAL);
16067 
16068 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16069 		    mode) != 0)
16070 			return (EFAULT);
16071 	}
16072 	return (0);
16073 }
16074 
16075 /*
16076  * Process ioctl get attachment point type request.
16077  *
16078  * NOTE: Port multiplier is supported.
16079  */
16080 static	int
16081 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16082     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16083 {
16084 	uint32_t	type_len;
16085 	const char	*ap_type;
16086 	int		dev_type;
16087 
16088 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16089 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16090 		    sata_device->satadev_addr.cport);
16091 	else /* pmport */
16092 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16093 		    sata_device->satadev_addr.cport,
16094 		    sata_device->satadev_addr.pmport);
16095 
16096 	switch (dev_type) {
16097 	case SATA_DTYPE_NONE:
16098 		ap_type = "port";
16099 		break;
16100 
16101 	case SATA_DTYPE_ATADISK:
16102 	case SATA_DTYPE_ATAPIDISK:
16103 		ap_type = "disk";
16104 		break;
16105 
16106 	case SATA_DTYPE_ATAPICD:
16107 		ap_type = "cd/dvd";
16108 		break;
16109 
16110 	case SATA_DTYPE_ATAPITAPE:
16111 		ap_type = "tape";
16112 		break;
16113 
16114 	case SATA_DTYPE_ATAPIPROC:
16115 		ap_type = "processor";
16116 		break;
16117 
16118 	case SATA_DTYPE_PMULT:
16119 		ap_type = "sata-pmult";
16120 		break;
16121 
16122 	case SATA_DTYPE_UNKNOWN:
16123 		ap_type = "unknown";
16124 		break;
16125 
16126 	default:
16127 		ap_type = "unsupported";
16128 		break;
16129 
16130 	} /* end of dev_type switch */
16131 
16132 	type_len = strlen(ap_type) + 1;
16133 
16134 	if (ioc->get_size) {
16135 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16136 		    mode) != 0)
16137 			return (EFAULT);
16138 	} else {
16139 		if (ioc->bufsiz != type_len)
16140 			return (EINVAL);
16141 
16142 		if (ddi_copyout((void *)ap_type, ioc->buf,
16143 		    ioc->bufsiz, mode) != 0)
16144 			return (EFAULT);
16145 	}
16146 	return (0);
16147 
16148 }
16149 
16150 /*
16151  * Process ioctl get device model info request.
16152  * This operation should return to cfgadm the device model
16153  * information string
16154  *
16155  * NOTE: Port multiplier is supported.
16156  */
16157 static	int
16158 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16159     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16160 {
16161 	sata_drive_info_t *sdinfo;
16162 	uint32_t info_len;
16163 	char ap_info[SATA_ID_MODEL_LEN + 1];
16164 
16165 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16166 	    sata_device->satadev_addr.cport)->cport_mutex);
16167 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16168 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16169 		    sata_device->satadev_addr.cport);
16170 	else /* port multiplier */
16171 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16172 		    sata_device->satadev_addr.cport,
16173 		    sata_device->satadev_addr.pmport);
16174 	if (sdinfo == NULL) {
16175 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16176 		    sata_device->satadev_addr.cport)->cport_mutex);
16177 		return (EINVAL);
16178 	}
16179 
16180 #ifdef	_LITTLE_ENDIAN
16181 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16182 #else	/* _LITTLE_ENDIAN */
16183 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16184 #endif	/* _LITTLE_ENDIAN */
16185 
16186 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16187 	    sata_device->satadev_addr.cport)->cport_mutex);
16188 
16189 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16190 
16191 	info_len = strlen(ap_info) + 1;
16192 
16193 	if (ioc->get_size) {
16194 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16195 		    mode) != 0)
16196 			return (EFAULT);
16197 	} else {
16198 		if (ioc->bufsiz < info_len)
16199 			return (EINVAL);
16200 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16201 		    mode) != 0)
16202 			return (EFAULT);
16203 	}
16204 	return (0);
16205 }
16206 
16207 
16208 /*
16209  * Process ioctl get device firmware revision info request.
16210  * This operation should return to cfgadm the device firmware revision
16211  * information string
16212  *
16213  * Port multiplier is supported.
16214  */
16215 static	int
16216 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16217     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16218 {
16219 	sata_drive_info_t *sdinfo;
16220 	uint32_t info_len;
16221 	char ap_info[SATA_ID_FW_LEN + 1];
16222 
16223 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16224 	    sata_device->satadev_addr.cport)->cport_mutex);
16225 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16226 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16227 		    sata_device->satadev_addr.cport);
16228 	else /* port multiplier */
16229 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16230 		    sata_device->satadev_addr.cport,
16231 		    sata_device->satadev_addr.pmport);
16232 	if (sdinfo == NULL) {
16233 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16234 		    sata_device->satadev_addr.cport)->cport_mutex);
16235 		return (EINVAL);
16236 	}
16237 
16238 #ifdef	_LITTLE_ENDIAN
16239 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16240 #else	/* _LITTLE_ENDIAN */
16241 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16242 #endif	/* _LITTLE_ENDIAN */
16243 
16244 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16245 	    sata_device->satadev_addr.cport)->cport_mutex);
16246 
16247 	ap_info[SATA_ID_FW_LEN] = '\0';
16248 
16249 	info_len = strlen(ap_info) + 1;
16250 
16251 	if (ioc->get_size) {
16252 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16253 		    mode) != 0)
16254 			return (EFAULT);
16255 	} else {
16256 		if (ioc->bufsiz < info_len)
16257 			return (EINVAL);
16258 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16259 		    mode) != 0)
16260 			return (EFAULT);
16261 	}
16262 	return (0);
16263 }
16264 
16265 
16266 /*
16267  * Process ioctl get device serial number info request.
16268  * This operation should return to cfgadm the device serial number string.
16269  *
16270  * NOTE: Port multiplier is supported.
16271  */
16272 static	int
16273 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16274     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16275 {
16276 	sata_drive_info_t *sdinfo;
16277 	uint32_t info_len;
16278 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16279 
16280 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16281 	    sata_device->satadev_addr.cport)->cport_mutex);
16282 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16283 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16284 		    sata_device->satadev_addr.cport);
16285 	else /* port multiplier */
16286 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16287 		    sata_device->satadev_addr.cport,
16288 		    sata_device->satadev_addr.pmport);
16289 	if (sdinfo == NULL) {
16290 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16291 		    sata_device->satadev_addr.cport)->cport_mutex);
16292 		return (EINVAL);
16293 	}
16294 
16295 #ifdef	_LITTLE_ENDIAN
16296 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16297 #else	/* _LITTLE_ENDIAN */
16298 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16299 #endif	/* _LITTLE_ENDIAN */
16300 
16301 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16302 	    sata_device->satadev_addr.cport)->cport_mutex);
16303 
16304 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16305 
16306 	info_len = strlen(ap_info) + 1;
16307 
16308 	if (ioc->get_size) {
16309 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16310 		    mode) != 0)
16311 			return (EFAULT);
16312 	} else {
16313 		if (ioc->bufsiz < info_len)
16314 			return (EINVAL);
16315 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16316 		    mode) != 0)
16317 			return (EFAULT);
16318 	}
16319 	return (0);
16320 }
16321 
16322 
16323 /*
16324  * Preset scsi extended sense data (to NO SENSE)
16325  * First 18 bytes of the sense data are preset to current valid sense
16326  * with a key NO SENSE data.
16327  *
16328  * Returns void
16329  */
16330 static void
16331 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16332 {
16333 	sense->es_valid = 1;		/* Valid sense */
16334 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16335 	sense->es_key = KEY_NO_SENSE;
16336 	sense->es_info_1 = 0;
16337 	sense->es_info_2 = 0;
16338 	sense->es_info_3 = 0;
16339 	sense->es_info_4 = 0;
16340 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16341 	sense->es_cmd_info[0] = 0;
16342 	sense->es_cmd_info[1] = 0;
16343 	sense->es_cmd_info[2] = 0;
16344 	sense->es_cmd_info[3] = 0;
16345 	sense->es_add_code = 0;
16346 	sense->es_qual_code = 0;
16347 }
16348 
16349 /*
16350  * Register a legacy cmdk-style devid for the target (disk) device.
16351  *
16352  * Note: This function is called only when the HBA devinfo node has the
16353  * property "use-cmdk-devid-format" set. This property indicates that
16354  * devid compatible with old cmdk (target) driver is to be generated
16355  * for any target device attached to this controller. This will take
16356  * precedence over the devid generated by sd (target) driver.
16357  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16358  */
16359 static void
16360 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16361 {
16362 	char	*hwid;
16363 	int	modlen;
16364 	int	serlen;
16365 	int	rval;
16366 	ddi_devid_t	devid;
16367 
16368 	/*
16369 	 * device ID is a concatanation of model number, "=", serial number.
16370 	 */
16371 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16372 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16373 	    sizeof (sdinfo->satadrv_id.ai_model));
16374 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16375 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16376 	if (modlen == 0)
16377 		goto err;
16378 	hwid[modlen++] = '=';
16379 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16380 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16381 	swab(&hwid[modlen], &hwid[modlen],
16382 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16383 	serlen = sata_check_modser(&hwid[modlen],
16384 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16385 	if (serlen == 0)
16386 		goto err;
16387 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16388 
16389 	/* initialize/register devid */
16390 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16391 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16392 		rval = ddi_devid_register(dip, devid);
16393 		/*
16394 		 * Free up the allocated devid buffer.
16395 		 * NOTE: This doesn't mean unregistering devid.
16396 		 */
16397 		ddi_devid_free(devid);
16398 	}
16399 
16400 	if (rval != DDI_SUCCESS)
16401 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16402 		    " on port %d", sdinfo->satadrv_addr.cport);
16403 err:
16404 	kmem_free(hwid, LEGACY_HWID_LEN);
16405 }
16406 
16407 /*
16408  * valid model/serial string must contain a non-zero non-space characters.
16409  * trim trailing spaces/NULLs.
16410  */
16411 static int
16412 sata_check_modser(char *buf, int buf_len)
16413 {
16414 	boolean_t ret;
16415 	char *s;
16416 	int i;
16417 	int tb;
16418 	char ch;
16419 
16420 	ret = B_FALSE;
16421 	s = buf;
16422 	for (i = 0; i < buf_len; i++) {
16423 		ch = *s++;
16424 		if (ch != ' ' && ch != '\0')
16425 			tb = i + 1;
16426 		if (ch != ' ' && ch != '\0' && ch != '0')
16427 			ret = B_TRUE;
16428 	}
16429 
16430 	if (ret == B_FALSE)
16431 		return (0); /* invalid string */
16432 
16433 	return (tb); /* return length */
16434 }
16435 
16436 /*
16437  * sata_set_drive_features function compares current device features setting
16438  * with the saved device features settings and, if there is a difference,
16439  * it restores device features setting to the previously saved state.
16440  * It also arbitrarily tries to select the highest supported DMA mode.
16441  * Device Identify or Identify Packet Device data has to be current.
16442  * At the moment read ahead and write cache are considered for all devices.
16443  * For atapi devices, Removable Media Status Notification is set in addition
16444  * to common features.
16445  *
16446  * This function cannot be called in the interrupt context (it may sleep).
16447  *
16448  * The input argument sdinfo should point to the drive info structure
16449  * to be updated after features are set. Note, that only
16450  * device (packet) identify data is updated, not the flags indicating the
16451  * supported features.
16452  *
16453  * Returns SATA_SUCCESS if successful or there was nothing to do.
16454  * Device Identify data in the drive info structure pointed to by the sdinfo
16455  * arguments is updated even when no features were set or changed.
16456  *
16457  * Returns SATA_FAILURE if device features could not be set or DMA mode
16458  * for a disk cannot be set and device identify data cannot be fetched.
16459  *
16460  * Returns SATA_RETRY if device features could not be set (other than disk
16461  * DMA mode) but the device identify data was fetched successfully.
16462  *
16463  * Note: This function may fail the port, making it inaccessible.
16464  * In such case the explicit port disconnect/connect or physical device
16465  * detach/attach is required to re-evaluate port state again.
16466  */
16467 
16468 static int
16469 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16470     sata_drive_info_t *sdinfo, int restore)
16471 {
16472 	int rval = SATA_SUCCESS;
16473 	int rval_set;
16474 	sata_drive_info_t new_sdinfo;
16475 	char *finfo = "sata_set_drive_features: cannot";
16476 	char *finfox;
16477 	int cache_op;
16478 
16479 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16480 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16481 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16482 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16483 		/*
16484 		 * Cannot get device identification - caller may retry later
16485 		 */
16486 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16487 		    "%s fetch device identify data\n", finfo);
16488 		return (SATA_FAILURE);
16489 	}
16490 	finfox = (restore != 0) ? " restore device features" :
16491 	    " initialize device features\n";
16492 
16493 	switch (sdinfo->satadrv_type) {
16494 	case SATA_DTYPE_ATADISK:
16495 		/* Arbitrarily set UDMA mode */
16496 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16497 		    SATA_SUCCESS) {
16498 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16499 			    "%s set UDMA mode\n", finfo));
16500 			return (SATA_FAILURE);
16501 		}
16502 		break;
16503 	case SATA_DTYPE_ATAPICD:
16504 	case SATA_DTYPE_ATAPITAPE:
16505 	case SATA_DTYPE_ATAPIDISK:
16506 		/*  Set Removable Media Status Notification, if necessary */
16507 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16508 		    restore != 0) {
16509 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16510 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16511 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16512 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16513 				/* Current setting does not match saved one */
16514 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16515 				    sdinfo->satadrv_settings &
16516 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16517 					rval = SATA_FAILURE;
16518 			}
16519 		}
16520 		/*
16521 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16522 		 * we want to use DMA transfer mode whenever possible.
16523 		 * Some devices require explicit setting of the DMA mode.
16524 		 */
16525 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16526 			/* Set highest supported DMA mode */
16527 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16528 			    SATA_SUCCESS) {
16529 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16530 				    "%s set UDMA mode\n", finfo));
16531 				rval = SATA_FAILURE;
16532 			}
16533 		}
16534 		break;
16535 	}
16536 
16537 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16538 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16539 		/*
16540 		 * neither READ AHEAD nor WRITE CACHE is supported
16541 		 * - do nothing
16542 		 */
16543 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16544 		    "settable features not supported\n", NULL);
16545 		goto update_sdinfo;
16546 	}
16547 
16548 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16549 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16550 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16551 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16552 		/*
16553 		 * both READ AHEAD and WRITE CACHE are enabled
16554 		 * - Nothing to do
16555 		 */
16556 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16557 		    "no device features to set\n", NULL);
16558 		goto update_sdinfo;
16559 	}
16560 
16561 	cache_op = 0;
16562 
16563 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16564 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16565 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16566 			/* Enable read ahead / read cache */
16567 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16568 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16569 			    "enabling read cache\n", NULL);
16570 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16571 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16572 			/* Disable read ahead  / read cache */
16573 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16574 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16575 			    "disabling read cache\n", NULL);
16576 		}
16577 
16578 		if (cache_op != 0) {
16579 			/* Try to set read cache mode */
16580 			rval_set = sata_set_cache_mode(sata_hba_inst,
16581 			    &new_sdinfo, cache_op);
16582 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16583 				rval = rval_set;
16584 		}
16585 	}
16586 
16587 	cache_op = 0;
16588 
16589 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16590 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16591 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16592 			/* Enable write cache */
16593 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16594 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16595 			    "enabling write cache\n", NULL);
16596 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16597 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16598 			/* Disable write cache */
16599 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16600 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16601 			    "disabling write cache\n", NULL);
16602 		}
16603 
16604 		if (cache_op != 0) {
16605 			/* Try to set write cache mode */
16606 			rval_set = sata_set_cache_mode(sata_hba_inst,
16607 			    &new_sdinfo, cache_op);
16608 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16609 				rval = rval_set;
16610 		}
16611 	}
16612 	if (rval != SATA_SUCCESS)
16613 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16614 		    "%s %s", finfo, finfox));
16615 
16616 update_sdinfo:
16617 	/*
16618 	 * We need to fetch Device Identify data again
16619 	 */
16620 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16621 		/*
16622 		 * Cannot get device identification - retry later
16623 		 */
16624 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16625 		    "%s re-fetch device identify data\n", finfo));
16626 		rval = SATA_FAILURE;
16627 	}
16628 	/* Copy device sata info. */
16629 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16630 
16631 	return (rval);
16632 }
16633 
16634 
16635 /*
16636  *
16637  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16638  * unable to determine.
16639  *
16640  * Cannot be called in an interrupt context.
16641  *
16642  * Called by sata_build_lsense_page_2f()
16643  */
16644 
16645 static int
16646 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16647     sata_drive_info_t *sdinfo)
16648 {
16649 	sata_pkt_t *spkt;
16650 	sata_cmd_t *scmd;
16651 	sata_pkt_txlate_t *spx;
16652 	int rval;
16653 
16654 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16655 	spx->txlt_sata_hba_inst = sata_hba_inst;
16656 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16657 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16658 	if (spkt == NULL) {
16659 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16660 		return (-1);
16661 	}
16662 	/* address is needed now */
16663 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16664 
16665 
16666 	/* Fill sata_pkt */
16667 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16668 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16669 	/* Synchronous mode, no callback */
16670 	spkt->satapkt_comp = NULL;
16671 	/* Timeout 30s */
16672 	spkt->satapkt_time = sata_default_pkt_time;
16673 
16674 	scmd = &spkt->satapkt_cmd;
16675 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16676 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16677 
16678 	/* Set up which registers need to be returned */
16679 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16680 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16681 
16682 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16683 	scmd->satacmd_addr_type = 0;		/* N/A */
16684 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16685 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16686 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16687 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16688 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16689 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16690 	scmd->satacmd_cmd_reg = SATAC_SMART;
16691 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16692 	    sdinfo->satadrv_addr.cport)));
16693 
16694 
16695 	/* Send pkt to SATA HBA driver */
16696 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16697 	    SATA_TRAN_ACCEPTED ||
16698 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16699 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16700 		    sdinfo->satadrv_addr.cport)));
16701 		/*
16702 		 * Whoops, no SMART RETURN STATUS
16703 		 */
16704 		rval = -1;
16705 	} else {
16706 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16707 		    sdinfo->satadrv_addr.cport)));
16708 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16709 			rval = -1;
16710 			goto fail;
16711 		}
16712 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16713 			rval = -1;
16714 			goto fail;
16715 		}
16716 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16717 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16718 			rval = 0;
16719 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16720 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16721 			rval = 1;
16722 		else {
16723 			rval = -1;
16724 			goto fail;
16725 		}
16726 	}
16727 fail:
16728 	/* Free allocated resources */
16729 	sata_pkt_free(spx);
16730 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16731 
16732 	return (rval);
16733 }
16734 
16735 /*
16736  *
16737  * Returns 0 if succeeded, -1 otherwise
16738  *
16739  * Cannot be called in an interrupt context.
16740  *
16741  */
16742 static int
16743 sata_fetch_smart_data(
16744 	sata_hba_inst_t *sata_hba_inst,
16745 	sata_drive_info_t *sdinfo,
16746 	struct smart_data *smart_data)
16747 {
16748 	sata_pkt_t *spkt;
16749 	sata_cmd_t *scmd;
16750 	sata_pkt_txlate_t *spx;
16751 	int rval;
16752 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16753 
16754 #if ! defined(lint)
16755 	ASSERT(sizeof (struct smart_data) == 512);
16756 #endif
16757 
16758 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16759 	spx->txlt_sata_hba_inst = sata_hba_inst;
16760 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16761 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16762 	if (spkt == NULL) {
16763 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16764 		return (-1);
16765 	}
16766 	/* address is needed now */
16767 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16768 
16769 
16770 	/* Fill sata_pkt */
16771 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16772 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16773 	/* Synchronous mode, no callback */
16774 	spkt->satapkt_comp = NULL;
16775 	/* Timeout 30s */
16776 	spkt->satapkt_time = sata_default_pkt_time;
16777 
16778 	scmd = &spkt->satapkt_cmd;
16779 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16780 
16781 	/*
16782 	 * Allocate buffer for SMART data
16783 	 */
16784 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16785 	    sizeof (struct smart_data));
16786 	if (scmd->satacmd_bp == NULL) {
16787 		sata_pkt_free(spx);
16788 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16789 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16790 		    "sata_fetch_smart_data: "
16791 		    "cannot allocate buffer"));
16792 		return (-1);
16793 	}
16794 
16795 
16796 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16797 	scmd->satacmd_addr_type = 0;		/* N/A */
16798 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16799 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16800 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16801 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16802 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16803 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16804 	scmd->satacmd_cmd_reg = SATAC_SMART;
16805 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16806 	    sdinfo->satadrv_addr.cport)));
16807 
16808 	/* Send pkt to SATA HBA driver */
16809 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16810 	    SATA_TRAN_ACCEPTED ||
16811 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16812 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16813 		    sdinfo->satadrv_addr.cport)));
16814 		/*
16815 		 * Whoops, no SMART DATA available
16816 		 */
16817 		rval = -1;
16818 		goto fail;
16819 	} else {
16820 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16821 		    sdinfo->satadrv_addr.cport)));
16822 		if (spx->txlt_buf_dma_handle != NULL) {
16823 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16824 			    DDI_DMA_SYNC_FORKERNEL);
16825 			ASSERT(rval == DDI_SUCCESS);
16826 			if (sata_check_for_dma_error(dip, spx)) {
16827 				ddi_fm_service_impact(dip,
16828 				    DDI_SERVICE_UNAFFECTED);
16829 				rval = -1;
16830 				goto fail;
16831 			}
16832 		}
16833 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16834 		    sizeof (struct smart_data));
16835 	}
16836 
16837 fail:
16838 	/* Free allocated resources */
16839 	sata_free_local_buffer(spx);
16840 	sata_pkt_free(spx);
16841 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16842 
16843 	return (rval);
16844 }
16845 
16846 /*
16847  * Used by LOG SENSE page 0x10
16848  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16849  * Note: cannot be called in the interrupt context.
16850  *
16851  * return 0 for success, -1 otherwise
16852  *
16853  */
16854 static int
16855 sata_ext_smart_selftest_read_log(
16856 	sata_hba_inst_t *sata_hba_inst,
16857 	sata_drive_info_t *sdinfo,
16858 	struct smart_ext_selftest_log *ext_selftest_log,
16859 	uint16_t block_num)
16860 {
16861 	sata_pkt_txlate_t *spx;
16862 	sata_pkt_t *spkt;
16863 	sata_cmd_t *scmd;
16864 	int rval;
16865 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16866 
16867 #if ! defined(lint)
16868 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16869 #endif
16870 
16871 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16872 	spx->txlt_sata_hba_inst = sata_hba_inst;
16873 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16874 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16875 	if (spkt == NULL) {
16876 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16877 		return (-1);
16878 	}
16879 	/* address is needed now */
16880 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16881 
16882 
16883 	/* Fill sata_pkt */
16884 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16885 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16886 	/* Synchronous mode, no callback */
16887 	spkt->satapkt_comp = NULL;
16888 	/* Timeout 30s */
16889 	spkt->satapkt_time = sata_default_pkt_time;
16890 
16891 	scmd = &spkt->satapkt_cmd;
16892 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16893 
16894 	/*
16895 	 * Allocate buffer for SMART extended self-test log
16896 	 */
16897 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16898 	    sizeof (struct smart_ext_selftest_log));
16899 	if (scmd->satacmd_bp == NULL) {
16900 		sata_pkt_free(spx);
16901 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16902 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16903 		    "sata_ext_smart_selftest_log: "
16904 		    "cannot allocate buffer"));
16905 		return (-1);
16906 	}
16907 
16908 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16909 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16910 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16911 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16912 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16913 	scmd->satacmd_lba_low_msb = 0;
16914 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16915 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16916 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16917 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16918 
16919 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16920 	    sdinfo->satadrv_addr.cport)));
16921 
16922 	/* Send pkt to SATA HBA driver */
16923 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16924 	    SATA_TRAN_ACCEPTED ||
16925 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16926 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16927 		    sdinfo->satadrv_addr.cport)));
16928 
16929 		/*
16930 		 * Whoops, no SMART selftest log info available
16931 		 */
16932 		rval = -1;
16933 		goto fail;
16934 	} else {
16935 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16936 		    sdinfo->satadrv_addr.cport)));
16937 
16938 		if (spx->txlt_buf_dma_handle != NULL) {
16939 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16940 			    DDI_DMA_SYNC_FORKERNEL);
16941 			ASSERT(rval == DDI_SUCCESS);
16942 			if (sata_check_for_dma_error(dip, spx)) {
16943 				ddi_fm_service_impact(dip,
16944 				    DDI_SERVICE_UNAFFECTED);
16945 				rval = -1;
16946 				goto fail;
16947 			}
16948 		}
16949 		bcopy(scmd->satacmd_bp->b_un.b_addr,
16950 		    (uint8_t *)ext_selftest_log,
16951 		    sizeof (struct smart_ext_selftest_log));
16952 		rval = 0;
16953 	}
16954 
16955 fail:
16956 	/* Free allocated resources */
16957 	sata_free_local_buffer(spx);
16958 	sata_pkt_free(spx);
16959 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16960 
16961 	return (rval);
16962 }
16963 
16964 /*
16965  * Returns 0 for success, -1 otherwise
16966  *
16967  * SMART self-test log data is returned in buffer pointed to by selftest_log
16968  */
16969 static int
16970 sata_smart_selftest_log(
16971 	sata_hba_inst_t *sata_hba_inst,
16972 	sata_drive_info_t *sdinfo,
16973 	struct smart_selftest_log *selftest_log)
16974 {
16975 	sata_pkt_t *spkt;
16976 	sata_cmd_t *scmd;
16977 	sata_pkt_txlate_t *spx;
16978 	int rval;
16979 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16980 
16981 #if ! defined(lint)
16982 	ASSERT(sizeof (struct smart_selftest_log) == 512);
16983 #endif
16984 
16985 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16986 	spx->txlt_sata_hba_inst = sata_hba_inst;
16987 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16988 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16989 	if (spkt == NULL) {
16990 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16991 		return (-1);
16992 	}
16993 	/* address is needed now */
16994 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16995 
16996 
16997 	/* Fill sata_pkt */
16998 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16999 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17000 	/* Synchronous mode, no callback */
17001 	spkt->satapkt_comp = NULL;
17002 	/* Timeout 30s */
17003 	spkt->satapkt_time = sata_default_pkt_time;
17004 
17005 	scmd = &spkt->satapkt_cmd;
17006 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17007 
17008 	/*
17009 	 * Allocate buffer for SMART SELFTEST LOG
17010 	 */
17011 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17012 	    sizeof (struct smart_selftest_log));
17013 	if (scmd->satacmd_bp == NULL) {
17014 		sata_pkt_free(spx);
17015 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17016 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17017 		    "sata_smart_selftest_log: "
17018 		    "cannot allocate buffer"));
17019 		return (-1);
17020 	}
17021 
17022 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17023 	scmd->satacmd_addr_type = 0;		/* N/A */
17024 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17025 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17026 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17027 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17028 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17029 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17030 	scmd->satacmd_cmd_reg = SATAC_SMART;
17031 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17032 	    sdinfo->satadrv_addr.cport)));
17033 
17034 	/* Send pkt to SATA HBA driver */
17035 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17036 	    SATA_TRAN_ACCEPTED ||
17037 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17038 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17039 		    sdinfo->satadrv_addr.cport)));
17040 		/*
17041 		 * Whoops, no SMART DATA available
17042 		 */
17043 		rval = -1;
17044 		goto fail;
17045 	} else {
17046 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17047 		    sdinfo->satadrv_addr.cport)));
17048 		if (spx->txlt_buf_dma_handle != NULL) {
17049 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17050 			    DDI_DMA_SYNC_FORKERNEL);
17051 			ASSERT(rval == DDI_SUCCESS);
17052 			if (sata_check_for_dma_error(dip, spx)) {
17053 				ddi_fm_service_impact(dip,
17054 				    DDI_SERVICE_UNAFFECTED);
17055 				rval = -1;
17056 				goto fail;
17057 			}
17058 		}
17059 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17060 		    sizeof (struct smart_selftest_log));
17061 		rval = 0;
17062 	}
17063 
17064 fail:
17065 	/* Free allocated resources */
17066 	sata_free_local_buffer(spx);
17067 	sata_pkt_free(spx);
17068 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17069 
17070 	return (rval);
17071 }
17072 
17073 
17074 /*
17075  * Returns 0 for success, -1 otherwise
17076  *
17077  * SMART READ LOG data is returned in buffer pointed to by smart_log
17078  */
17079 static int
17080 sata_smart_read_log(
17081 	sata_hba_inst_t *sata_hba_inst,
17082 	sata_drive_info_t *sdinfo,
17083 	uint8_t *smart_log,		/* where the data should be returned */
17084 	uint8_t which_log,		/* which log should be returned */
17085 	uint8_t log_size)		/* # of 512 bytes in log */
17086 {
17087 	sata_pkt_t *spkt;
17088 	sata_cmd_t *scmd;
17089 	sata_pkt_txlate_t *spx;
17090 	int rval;
17091 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17092 
17093 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17094 	spx->txlt_sata_hba_inst = sata_hba_inst;
17095 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17096 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17097 	if (spkt == NULL) {
17098 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17099 		return (-1);
17100 	}
17101 	/* address is needed now */
17102 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17103 
17104 
17105 	/* Fill sata_pkt */
17106 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17107 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17108 	/* Synchronous mode, no callback */
17109 	spkt->satapkt_comp = NULL;
17110 	/* Timeout 30s */
17111 	spkt->satapkt_time = sata_default_pkt_time;
17112 
17113 	scmd = &spkt->satapkt_cmd;
17114 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17115 
17116 	/*
17117 	 * Allocate buffer for SMART READ LOG
17118 	 */
17119 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17120 	if (scmd->satacmd_bp == NULL) {
17121 		sata_pkt_free(spx);
17122 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17123 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17124 		    "sata_smart_read_log: " "cannot allocate buffer"));
17125 		return (-1);
17126 	}
17127 
17128 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17129 	scmd->satacmd_addr_type = 0;		/* N/A */
17130 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17131 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17132 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17133 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17134 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17135 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17136 	scmd->satacmd_cmd_reg = SATAC_SMART;
17137 
17138 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17139 	    sdinfo->satadrv_addr.cport)));
17140 
17141 	/* Send pkt to SATA HBA driver */
17142 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17143 	    SATA_TRAN_ACCEPTED ||
17144 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17145 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17146 		    sdinfo->satadrv_addr.cport)));
17147 
17148 		/*
17149 		 * Whoops, no SMART DATA available
17150 		 */
17151 		rval = -1;
17152 		goto fail;
17153 	} else {
17154 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17155 		    sdinfo->satadrv_addr.cport)));
17156 
17157 		if (spx->txlt_buf_dma_handle != NULL) {
17158 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17159 			    DDI_DMA_SYNC_FORKERNEL);
17160 			ASSERT(rval == DDI_SUCCESS);
17161 			if (sata_check_for_dma_error(dip, spx)) {
17162 				ddi_fm_service_impact(dip,
17163 				    DDI_SERVICE_UNAFFECTED);
17164 				rval = -1;
17165 				goto fail;
17166 			}
17167 		}
17168 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17169 		rval = 0;
17170 	}
17171 
17172 fail:
17173 	/* Free allocated resources */
17174 	sata_free_local_buffer(spx);
17175 	sata_pkt_free(spx);
17176 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17177 
17178 	return (rval);
17179 }
17180 
17181 /*
17182  * Used by LOG SENSE page 0x10
17183  *
17184  * return 0 for success, -1 otherwise
17185  *
17186  */
17187 static int
17188 sata_read_log_ext_directory(
17189 	sata_hba_inst_t *sata_hba_inst,
17190 	sata_drive_info_t *sdinfo,
17191 	struct read_log_ext_directory *logdir)
17192 {
17193 	sata_pkt_txlate_t *spx;
17194 	sata_pkt_t *spkt;
17195 	sata_cmd_t *scmd;
17196 	int rval;
17197 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17198 
17199 #if ! defined(lint)
17200 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17201 #endif
17202 
17203 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17204 	spx->txlt_sata_hba_inst = sata_hba_inst;
17205 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17206 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17207 	if (spkt == NULL) {
17208 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17209 		return (-1);
17210 	}
17211 
17212 	/* Fill sata_pkt */
17213 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17214 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17215 	/* Synchronous mode, no callback */
17216 	spkt->satapkt_comp = NULL;
17217 	/* Timeout 30s */
17218 	spkt->satapkt_time = sata_default_pkt_time;
17219 
17220 	scmd = &spkt->satapkt_cmd;
17221 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17222 
17223 	/*
17224 	 * Allocate buffer for SMART READ LOG EXTENDED command
17225 	 */
17226 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17227 	    sizeof (struct read_log_ext_directory));
17228 	if (scmd->satacmd_bp == NULL) {
17229 		sata_pkt_free(spx);
17230 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17231 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17232 		    "sata_read_log_ext_directory: "
17233 		    "cannot allocate buffer"));
17234 		return (-1);
17235 	}
17236 
17237 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17238 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17239 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17240 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17241 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17242 	scmd->satacmd_lba_low_msb = 0;
17243 	scmd->satacmd_lba_mid_lsb = 0;
17244 	scmd->satacmd_lba_mid_msb = 0;
17245 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17246 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17247 
17248 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17249 	    sdinfo->satadrv_addr.cport)));
17250 
17251 	/* Send pkt to SATA HBA driver */
17252 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17253 	    SATA_TRAN_ACCEPTED ||
17254 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17255 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17256 		    sdinfo->satadrv_addr.cport)));
17257 		/*
17258 		 * Whoops, no SMART selftest log info available
17259 		 */
17260 		rval = -1;
17261 		goto fail;
17262 	} else {
17263 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17264 		    sdinfo->satadrv_addr.cport)));
17265 		if (spx->txlt_buf_dma_handle != NULL) {
17266 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17267 			    DDI_DMA_SYNC_FORKERNEL);
17268 			ASSERT(rval == DDI_SUCCESS);
17269 			if (sata_check_for_dma_error(dip, spx)) {
17270 				ddi_fm_service_impact(dip,
17271 				    DDI_SERVICE_UNAFFECTED);
17272 				rval = -1;
17273 				goto fail;
17274 			}
17275 		}
17276 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17277 		    sizeof (struct read_log_ext_directory));
17278 		rval = 0;
17279 	}
17280 
17281 fail:
17282 	/* Free allocated resources */
17283 	sata_free_local_buffer(spx);
17284 	sata_pkt_free(spx);
17285 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17286 
17287 	return (rval);
17288 }
17289 
17290 /*
17291  * Set up error retrieval sata command for NCQ command error data
17292  * recovery.
17293  *
17294  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17295  * returns SATA_FAILURE otherwise.
17296  */
17297 static int
17298 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17299 {
17300 #ifndef __lock_lint
17301 	_NOTE(ARGUNUSED(sdinfo))
17302 #endif
17303 
17304 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17305 	sata_cmd_t *scmd;
17306 	struct buf *bp;
17307 
17308 	/* Operation modes are up to the caller */
17309 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17310 
17311 	/* Synchronous mode, no callback - may be changed by the caller */
17312 	spkt->satapkt_comp = NULL;
17313 	spkt->satapkt_time = sata_default_pkt_time;
17314 
17315 	scmd = &spkt->satapkt_cmd;
17316 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17317 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17318 
17319 	/*
17320 	 * Allocate dma_able buffer error data.
17321 	 * Buffer allocation will take care of buffer alignment and other DMA
17322 	 * attributes.
17323 	 */
17324 	bp = sata_alloc_local_buffer(spx,
17325 	    sizeof (struct sata_ncq_error_recovery_page));
17326 	if (bp == NULL)
17327 		return (SATA_FAILURE);
17328 
17329 	bp_mapin(bp); /* make data buffer accessible */
17330 	scmd->satacmd_bp = bp;
17331 
17332 	/*
17333 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17334 	 * before accessing it. Handle is in usual place in translate struct.
17335 	 */
17336 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17337 
17338 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17339 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17340 
17341 	return (SATA_SUCCESS);
17342 }
17343 
17344 /*
17345  * sata_xlate_errors() is used to translate (S)ATA error
17346  * information to SCSI information returned in the SCSI
17347  * packet.
17348  */
17349 static void
17350 sata_xlate_errors(sata_pkt_txlate_t *spx)
17351 {
17352 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17353 	struct scsi_extended_sense *sense;
17354 
17355 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17356 	*scsipkt->pkt_scbp = STATUS_CHECK;
17357 	sense = sata_arq_sense(spx);
17358 
17359 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17360 	case SATA_PKT_PORT_ERROR:
17361 		/*
17362 		 * We have no device data. Assume no data transfered.
17363 		 */
17364 		sense->es_key = KEY_HARDWARE_ERROR;
17365 		break;
17366 
17367 	case SATA_PKT_DEV_ERROR:
17368 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17369 		    SATA_STATUS_ERR) {
17370 			/*
17371 			 * determine dev error reason from error
17372 			 * reg content
17373 			 */
17374 			sata_decode_device_error(spx, sense);
17375 			break;
17376 		}
17377 		/* No extended sense key - no info available */
17378 		break;
17379 
17380 	case SATA_PKT_TIMEOUT:
17381 		scsipkt->pkt_reason = CMD_TIMEOUT;
17382 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17383 		/* No extended sense key */
17384 		break;
17385 
17386 	case SATA_PKT_ABORTED:
17387 		scsipkt->pkt_reason = CMD_ABORTED;
17388 		scsipkt->pkt_statistics |= STAT_ABORTED;
17389 		/* No extended sense key */
17390 		break;
17391 
17392 	case SATA_PKT_RESET:
17393 		/*
17394 		 * pkt aborted either by an explicit reset request from
17395 		 * a host, or due to error recovery
17396 		 */
17397 		scsipkt->pkt_reason = CMD_RESET;
17398 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17399 		break;
17400 
17401 	default:
17402 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17403 		break;
17404 	}
17405 }
17406 
17407 
17408 
17409 
17410 /*
17411  * Log sata message
17412  * dev pathname msg line preceeds the logged message.
17413  */
17414 
17415 static	void
17416 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17417 {
17418 	char pathname[128];
17419 	dev_info_t *dip = NULL;
17420 	va_list ap;
17421 
17422 	mutex_enter(&sata_log_mutex);
17423 
17424 	va_start(ap, fmt);
17425 	(void) vsprintf(sata_log_buf, fmt, ap);
17426 	va_end(ap);
17427 
17428 	if (sata_hba_inst != NULL) {
17429 		dip = SATA_DIP(sata_hba_inst);
17430 		(void) ddi_pathname(dip, pathname);
17431 	} else {
17432 		pathname[0] = 0;
17433 	}
17434 	if (level == CE_CONT) {
17435 		if (sata_debug_flags == 0)
17436 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17437 		else
17438 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17439 	} else {
17440 		if (level != CE_NOTE) {
17441 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17442 		} else if (sata_msg) {
17443 			cmn_err(level, "%s:\n %s", pathname,
17444 			    sata_log_buf);
17445 		}
17446 	}
17447 
17448 	/* sata trace debug */
17449 	sata_trace_debug(dip, sata_log_buf);
17450 
17451 	mutex_exit(&sata_log_mutex);
17452 }
17453 
17454 
17455 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17456 
17457 /*
17458  * Start or terminate the thread, depending on flag arg and current state
17459  */
17460 static void
17461 sata_event_thread_control(int startstop)
17462 {
17463 	static 	int sata_event_thread_terminating = 0;
17464 	static 	int sata_event_thread_starting = 0;
17465 	int i;
17466 
17467 	mutex_enter(&sata_event_mutex);
17468 
17469 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17470 	    sata_event_thread_terminating == 1)) {
17471 		mutex_exit(&sata_event_mutex);
17472 		return;
17473 	}
17474 	if (startstop == 1 && sata_event_thread_starting == 1) {
17475 		mutex_exit(&sata_event_mutex);
17476 		return;
17477 	}
17478 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17479 		sata_event_thread_starting = 1;
17480 		/* wait til terminate operation completes */
17481 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17482 		while (sata_event_thread_terminating == 1) {
17483 			if (i-- <= 0) {
17484 				sata_event_thread_starting = 0;
17485 				mutex_exit(&sata_event_mutex);
17486 #ifdef SATA_DEBUG
17487 				cmn_err(CE_WARN, "sata_event_thread_control: "
17488 				    "timeout waiting for thread to terminate");
17489 #endif
17490 				return;
17491 			}
17492 			mutex_exit(&sata_event_mutex);
17493 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17494 			mutex_enter(&sata_event_mutex);
17495 		}
17496 	}
17497 	if (startstop == 1) {
17498 		if (sata_event_thread == NULL) {
17499 			sata_event_thread = thread_create(NULL, 0,
17500 			    (void (*)())sata_event_daemon,
17501 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17502 		}
17503 		sata_event_thread_starting = 0;
17504 		mutex_exit(&sata_event_mutex);
17505 		return;
17506 	}
17507 
17508 	/*
17509 	 * If we got here, thread may need to be terminated
17510 	 */
17511 	if (sata_event_thread != NULL) {
17512 		int i;
17513 		/* Signal event thread to go away */
17514 		sata_event_thread_terminating = 1;
17515 		sata_event_thread_terminate = 1;
17516 		cv_signal(&sata_event_cv);
17517 		/*
17518 		 * Wait til daemon terminates.
17519 		 */
17520 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17521 		while (sata_event_thread_terminate == 1) {
17522 			mutex_exit(&sata_event_mutex);
17523 			if (i-- <= 0) {
17524 				/* Daemon did not go away !!! */
17525 #ifdef SATA_DEBUG
17526 				cmn_err(CE_WARN, "sata_event_thread_control: "
17527 				    "cannot terminate event daemon thread");
17528 #endif
17529 				mutex_enter(&sata_event_mutex);
17530 				break;
17531 			}
17532 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17533 			mutex_enter(&sata_event_mutex);
17534 		}
17535 		sata_event_thread_terminating = 0;
17536 	}
17537 	ASSERT(sata_event_thread_terminating == 0);
17538 	ASSERT(sata_event_thread_starting == 0);
17539 	mutex_exit(&sata_event_mutex);
17540 }
17541 
17542 
17543 /*
17544  * SATA HBA event notification function.
17545  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17546  * a port and/or device state or a controller itself.
17547  * Events for different addresses/addr types cannot be combined.
17548  * A warning message is generated for each event type.
17549  * Events are not processed by this function, so only the
17550  * event flag(s)is set for an affected entity and the event thread is
17551  * waken up. Event daemon thread processes all events.
17552  *
17553  * NOTE: Since more than one event may be reported at the same time, one
17554  * cannot determine a sequence of events when opposite event are reported, eg.
17555  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17556  * is taking precedence over reported events, i.e. may cause ignoring some
17557  * events.
17558  */
17559 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17560 
17561 void
17562 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17563 {
17564 	sata_hba_inst_t *sata_hba_inst = NULL;
17565 	sata_address_t *saddr;
17566 	sata_pmult_info_t *pmultinfo;
17567 	sata_drive_info_t *sdinfo;
17568 	sata_port_stats_t *pstats;
17569 	sata_cport_info_t *cportinfo;
17570 	sata_pmport_info_t *pmportinfo;
17571 	int cport, pmport;
17572 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17573 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17574 	char *lcp;
17575 	static char *err_msg_evnt_1 =
17576 	    "sata_hba_event_notify: invalid port event 0x%x ";
17577 	static char *err_msg_evnt_2 =
17578 	    "sata_hba_event_notify: invalid device event 0x%x ";
17579 	int linkevent;
17580 
17581 	/*
17582 	 * There is a possibility that an event will be generated on HBA
17583 	 * that has not completed attachment or is detaching. We still want
17584 	 * to process events until HBA is detached.
17585 	 */
17586 	mutex_enter(&sata_mutex);
17587 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17588 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17589 		if (SATA_DIP(sata_hba_inst) == dip)
17590 			if (sata_hba_inst->satahba_attached == 1)
17591 				break;
17592 	}
17593 	mutex_exit(&sata_mutex);
17594 	if (sata_hba_inst == NULL)
17595 		/* HBA not attached */
17596 		return;
17597 
17598 	ASSERT(sata_device != NULL);
17599 
17600 	/*
17601 	 * Validate address before - do not proceed with invalid address.
17602 	 */
17603 	saddr = &sata_device->satadev_addr;
17604 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17605 		return;
17606 
17607 	cport = saddr->cport;
17608 	pmport = saddr->pmport;
17609 
17610 	buf1[0] = buf2[0] = '\0';
17611 
17612 	/*
17613 	 * If event relates to port or device, check port state.
17614 	 * Port has to be initialized, or we cannot accept an event.
17615 	 */
17616 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17617 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17618 		mutex_enter(&sata_hba_inst->satahba_mutex);
17619 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17620 		mutex_exit(&sata_hba_inst->satahba_mutex);
17621 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17622 			return;
17623 	}
17624 
17625 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17626 	    SATA_ADDR_DPMPORT)) != 0) {
17627 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17628 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17629 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17630 			    "is attached to port %d, ignore pmult/pmport "
17631 			    "event 0x%x", cportinfo->cport_dev_type,
17632 			    cport, event));
17633 			return;
17634 		}
17635 
17636 		mutex_enter(&cportinfo->cport_mutex);
17637 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17638 		mutex_exit(&cportinfo->cport_mutex);
17639 
17640 		/*
17641 		 * The daemon might be processing attachment of port
17642 		 * multiplier, in that case we should ignore events on its
17643 		 * sub-devices.
17644 		 *
17645 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17646 		 * The pmport_state is checked by sata daemon.
17647 		 */
17648 		if (pmultinfo == NULL ||
17649 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17650 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17651 			    "sata_hba_event_notify: pmult is not"
17652 			    "available at port %d:%d, ignore event 0x%x",
17653 			    cport, pmport, event));
17654 			return;
17655 		}
17656 	}
17657 
17658 	if ((saddr->qual &
17659 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17660 
17661 		mutex_enter(&cportinfo->cport_mutex);
17662 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17663 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17664 			    "sata_hba_event_notify: invalid/"
17665 			    "un-implemented port %d:%d (%d ports), "
17666 			    "ignore event 0x%x", cport, pmport,
17667 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17668 			mutex_exit(&cportinfo->cport_mutex);
17669 			return;
17670 		}
17671 		mutex_exit(&cportinfo->cport_mutex);
17672 
17673 		mutex_enter(&sata_hba_inst->satahba_mutex);
17674 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17675 		    cport, pmport);
17676 		mutex_exit(&sata_hba_inst->satahba_mutex);
17677 
17678 		/* pmport is implemented/valid? */
17679 		if (pmportinfo == NULL) {
17680 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17681 			    "sata_hba_event_notify: invalid/"
17682 			    "un-implemented port %d:%d, ignore "
17683 			    "event 0x%x", cport, pmport, event));
17684 			return;
17685 		}
17686 	}
17687 
17688 	/*
17689 	 * Events refer to devices, ports and controllers - each has
17690 	 * unique address. Events for different addresses cannot be combined.
17691 	 */
17692 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17693 
17694 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17695 
17696 		/* qualify this event(s) */
17697 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17698 			/* Invalid event for the device port */
17699 			(void) sprintf(buf2, err_msg_evnt_1,
17700 			    event & SATA_EVNT_PORT_EVENTS);
17701 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17702 			goto event_info;
17703 		}
17704 		if (saddr->qual == SATA_ADDR_CPORT) {
17705 			/* Controller's device port event */
17706 
17707 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17708 			    cport_event_flags |=
17709 			    event & SATA_EVNT_PORT_EVENTS;
17710 			pstats =
17711 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17712 			    cport_stats;
17713 		} else {
17714 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17715 			mutex_enter(&pmportinfo->pmport_mutex);
17716 			/* Port multiplier's device port event */
17717 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17718 			    pmport_event_flags |=
17719 			    event & SATA_EVNT_PORT_EVENTS;
17720 			pstats =
17721 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17722 			    pmport_stats;
17723 			mutex_exit(&pmportinfo->pmport_mutex);
17724 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17725 		}
17726 
17727 		/*
17728 		 * Add to statistics and log the message. We have to do it
17729 		 * here rather than in the event daemon, because there may be
17730 		 * multiple events occuring before they are processed.
17731 		 */
17732 		linkevent = event &
17733 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17734 		if (linkevent) {
17735 			if (linkevent == (SATA_EVNT_LINK_LOST |
17736 			    SATA_EVNT_LINK_ESTABLISHED)) {
17737 				/* This is likely event combination */
17738 				(void) strlcat(buf1, "link lost/established, ",
17739 				    SATA_EVENT_MAX_MSG_LENGTH);
17740 
17741 				if (pstats->link_lost < 0xffffffffffffffffULL)
17742 					pstats->link_lost++;
17743 				if (pstats->link_established <
17744 				    0xffffffffffffffffULL)
17745 					pstats->link_established++;
17746 				linkevent = 0;
17747 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17748 				(void) strlcat(buf1, "link lost, ",
17749 				    SATA_EVENT_MAX_MSG_LENGTH);
17750 
17751 				if (pstats->link_lost < 0xffffffffffffffffULL)
17752 					pstats->link_lost++;
17753 			} else {
17754 				(void) strlcat(buf1, "link established, ",
17755 				    SATA_EVENT_MAX_MSG_LENGTH);
17756 				if (pstats->link_established <
17757 				    0xffffffffffffffffULL)
17758 					pstats->link_established++;
17759 			}
17760 		}
17761 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17762 			(void) strlcat(buf1, "device attached, ",
17763 			    SATA_EVENT_MAX_MSG_LENGTH);
17764 			if (pstats->device_attached < 0xffffffffffffffffULL)
17765 				pstats->device_attached++;
17766 		}
17767 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17768 			(void) strlcat(buf1, "device detached, ",
17769 			    SATA_EVENT_MAX_MSG_LENGTH);
17770 			if (pstats->device_detached < 0xffffffffffffffffULL)
17771 				pstats->device_detached++;
17772 		}
17773 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17774 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17775 			    "port %d power level changed", cport);
17776 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17777 				pstats->port_pwr_changed++;
17778 		}
17779 
17780 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17781 			/* There should be no other events for this address */
17782 			(void) sprintf(buf2, err_msg_evnt_1,
17783 			    event & ~SATA_EVNT_PORT_EVENTS);
17784 		}
17785 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17786 
17787 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17788 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17789 
17790 		/* qualify this event */
17791 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17792 			/* Invalid event for a device */
17793 			(void) sprintf(buf2, err_msg_evnt_2,
17794 			    event & SATA_EVNT_DEVICE_RESET);
17795 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17796 			goto event_info;
17797 		}
17798 		/* drive event */
17799 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17800 		if (sdinfo != NULL) {
17801 			if (event & SATA_EVNT_DEVICE_RESET) {
17802 				(void) strlcat(buf1, "device reset, ",
17803 				    SATA_EVENT_MAX_MSG_LENGTH);
17804 				if (sdinfo->satadrv_stats.drive_reset <
17805 				    0xffffffffffffffffULL)
17806 					sdinfo->satadrv_stats.drive_reset++;
17807 				sdinfo->satadrv_event_flags |=
17808 				    SATA_EVNT_DEVICE_RESET;
17809 			}
17810 		}
17811 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17812 			/* Invalid event for a device */
17813 			(void) sprintf(buf2, err_msg_evnt_2,
17814 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17815 		}
17816 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17817 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17818 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17819 
17820 		/* qualify this event */
17821 		if ((event & (SATA_EVNT_DEVICE_RESET |
17822 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17823 			/* Invalid event for a port multiplier */
17824 			(void) sprintf(buf2, err_msg_evnt_2,
17825 			    event & SATA_EVNT_DEVICE_RESET);
17826 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17827 			goto event_info;
17828 		}
17829 
17830 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17831 
17832 		if (event & SATA_EVNT_DEVICE_RESET) {
17833 
17834 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17835 			    "[Reset] port-mult on cport %d", cport);
17836 			pmultinfo->pmult_event_flags |=
17837 			    SATA_EVNT_DEVICE_RESET;
17838 			(void) strlcat(buf1, "pmult reset, ",
17839 			    SATA_EVENT_MAX_MSG_LENGTH);
17840 		}
17841 
17842 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17843 
17844 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17845 			    "pmult link changed on cport %d", cport);
17846 			pmultinfo->pmult_event_flags |=
17847 			    SATA_EVNT_PMULT_LINK_CHANGED;
17848 			(void) strlcat(buf1, "pmult link changed, ",
17849 			    SATA_EVENT_MAX_MSG_LENGTH);
17850 		}
17851 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17852 
17853 	} else {
17854 		if (saddr->qual != SATA_ADDR_NULL) {
17855 			/* Wrong address qualifier */
17856 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17857 			    "sata_hba_event_notify: invalid address 0x%x",
17858 			    *(uint32_t *)saddr));
17859 			return;
17860 		}
17861 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17862 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17863 			/* Invalid event for the controller */
17864 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17865 			    "sata_hba_event_notify: invalid event 0x%x for "
17866 			    "controller",
17867 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17868 			return;
17869 		}
17870 		buf1[0] = '\0';
17871 		/* This may be a frequent and not interesting event */
17872 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17873 		    "controller power level changed\n", NULL);
17874 
17875 		mutex_enter(&sata_hba_inst->satahba_mutex);
17876 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17877 		    0xffffffffffffffffULL)
17878 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17879 
17880 		sata_hba_inst->satahba_event_flags |=
17881 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17882 		mutex_exit(&sata_hba_inst->satahba_mutex);
17883 	}
17884 	/*
17885 	 * If we got here, there is something to do with this HBA
17886 	 * instance.
17887 	 */
17888 	mutex_enter(&sata_hba_inst->satahba_mutex);
17889 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17890 	mutex_exit(&sata_hba_inst->satahba_mutex);
17891 	mutex_enter(&sata_mutex);
17892 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17893 	mutex_exit(&sata_mutex);
17894 
17895 	/* Tickle event thread */
17896 	mutex_enter(&sata_event_mutex);
17897 	if (sata_event_thread_active == 0)
17898 		cv_signal(&sata_event_cv);
17899 	mutex_exit(&sata_event_mutex);
17900 
17901 event_info:
17902 	if (buf1[0] != '\0') {
17903 		lcp = strrchr(buf1, ',');
17904 		if (lcp != NULL)
17905 			*lcp = '\0';
17906 	}
17907 	if (saddr->qual == SATA_ADDR_CPORT ||
17908 	    saddr->qual == SATA_ADDR_DCPORT) {
17909 		if (buf1[0] != '\0') {
17910 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17911 			    cport, buf1);
17912 		}
17913 		if (buf2[0] != '\0') {
17914 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17915 			    cport, buf2);
17916 		}
17917 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17918 	    saddr->qual == SATA_ADDR_DPMPORT) {
17919 		if (buf1[0] != '\0') {
17920 			sata_log(sata_hba_inst, CE_NOTE,
17921 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17922 		}
17923 		if (buf2[0] != '\0') {
17924 			sata_log(sata_hba_inst, CE_NOTE,
17925 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17926 		}
17927 	}
17928 }
17929 
17930 
17931 /*
17932  * Event processing thread.
17933  * Arg is a pointer to the sata_hba_list pointer.
17934  * It is not really needed, because sata_hba_list is global and static
17935  */
17936 static void
17937 sata_event_daemon(void *arg)
17938 {
17939 #ifndef __lock_lint
17940 	_NOTE(ARGUNUSED(arg))
17941 #endif
17942 	sata_hba_inst_t *sata_hba_inst;
17943 	clock_t delta;
17944 
17945 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17946 	    "SATA event daemon started\n", NULL);
17947 loop:
17948 	/*
17949 	 * Process events here. Walk through all registered HBAs
17950 	 */
17951 	mutex_enter(&sata_mutex);
17952 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17953 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17954 		ASSERT(sata_hba_inst != NULL);
17955 		mutex_enter(&sata_hba_inst->satahba_mutex);
17956 		if (sata_hba_inst->satahba_attached == 0 ||
17957 		    (sata_hba_inst->satahba_event_flags &
17958 		    SATA_EVNT_SKIP) != 0) {
17959 			mutex_exit(&sata_hba_inst->satahba_mutex);
17960 			continue;
17961 		}
17962 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
17963 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
17964 			mutex_exit(&sata_hba_inst->satahba_mutex);
17965 			mutex_exit(&sata_mutex);
17966 			/* Got the controller with pending event */
17967 			sata_process_controller_events(sata_hba_inst);
17968 			/*
17969 			 * Since global mutex was released, there is a
17970 			 * possibility that HBA list has changed, so start
17971 			 * over from the top. Just processed controller
17972 			 * will be passed-over because of the SKIP flag.
17973 			 */
17974 			goto loop;
17975 		}
17976 		mutex_exit(&sata_hba_inst->satahba_mutex);
17977 	}
17978 	/* Clear SKIP flag in all controllers */
17979 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17980 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17981 		mutex_enter(&sata_hba_inst->satahba_mutex);
17982 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
17983 		mutex_exit(&sata_hba_inst->satahba_mutex);
17984 	}
17985 	mutex_exit(&sata_mutex);
17986 
17987 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17988 	    "SATA EVENT DAEMON suspending itself", NULL);
17989 
17990 #ifdef SATA_DEBUG
17991 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
17992 		sata_log(sata_hba_inst, CE_WARN,
17993 		    "SATA EVENTS PROCESSING DISABLED\n");
17994 		thread_exit(); /* Daemon will not run again */
17995 	}
17996 #endif
17997 	mutex_enter(&sata_event_mutex);
17998 	sata_event_thread_active = 0;
17999 	mutex_exit(&sata_event_mutex);
18000 	/*
18001 	 * Go to sleep/suspend itself and wake up either because new event or
18002 	 * wait timeout. Exit if there is a termination request (driver
18003 	 * unload).
18004 	 */
18005 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18006 	do {
18007 		mutex_enter(&sata_event_mutex);
18008 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18009 		    delta, TR_CLOCK_TICK);
18010 
18011 		if (sata_event_thread_active != 0) {
18012 			mutex_exit(&sata_event_mutex);
18013 			continue;
18014 		}
18015 
18016 		/* Check if it is time to go away */
18017 		if (sata_event_thread_terminate == 1) {
18018 			/*
18019 			 * It is up to the thread setting above flag to make
18020 			 * sure that this thread is not killed prematurely.
18021 			 */
18022 			sata_event_thread_terminate = 0;
18023 			sata_event_thread = NULL;
18024 			mutex_exit(&sata_event_mutex);
18025 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18026 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18027 			thread_exit();  { _NOTE(NOT_REACHED) }
18028 		}
18029 		mutex_exit(&sata_event_mutex);
18030 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18031 
18032 	mutex_enter(&sata_event_mutex);
18033 	sata_event_thread_active = 1;
18034 	mutex_exit(&sata_event_mutex);
18035 
18036 	mutex_enter(&sata_mutex);
18037 	sata_event_pending &= ~SATA_EVNT_MAIN;
18038 	mutex_exit(&sata_mutex);
18039 
18040 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18041 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18042 
18043 	goto loop;
18044 }
18045 
18046 /*
18047  * Specific HBA instance event processing.
18048  *
18049  * NOTE: At the moment, device event processing is limited to hard disks
18050  * only.
18051  * Port multiplier is supported now.
18052  */
18053 static void
18054 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18055 {
18056 	int ncport;
18057 	uint32_t event_flags;
18058 	sata_address_t *saddr;
18059 	sata_cport_info_t *cportinfo;
18060 	sata_pmult_info_t *pmultinfo;
18061 
18062 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18063 	    "Processing controller %d event(s)",
18064 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18065 
18066 	mutex_enter(&sata_hba_inst->satahba_mutex);
18067 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18068 	event_flags = sata_hba_inst->satahba_event_flags;
18069 	mutex_exit(&sata_hba_inst->satahba_mutex);
18070 	/*
18071 	 * Process controller power change first
18072 	 * HERE
18073 	 */
18074 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18075 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18076 
18077 	/*
18078 	 * Search through ports/devices to identify affected port/device.
18079 	 * We may have to process events for more than one port/device.
18080 	 */
18081 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18082 		/*
18083 		 * Not all ports may be processed in attach by the time we
18084 		 * get an event. Check if port info is initialized.
18085 		 */
18086 		mutex_enter(&sata_hba_inst->satahba_mutex);
18087 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18088 		mutex_exit(&sata_hba_inst->satahba_mutex);
18089 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18090 			continue;
18091 
18092 		/* We have initialized controller port info */
18093 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18094 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18095 		    cport_event_flags;
18096 		/* Check if port was locked by IOCTL processing */
18097 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18098 			/*
18099 			 * We ignore port events because port is busy
18100 			 * with AP control processing. Set again
18101 			 * controller and main event flag, so that
18102 			 * events may be processed by the next daemon
18103 			 * run.
18104 			 */
18105 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18106 			mutex_enter(&sata_hba_inst->satahba_mutex);
18107 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18108 			mutex_exit(&sata_hba_inst->satahba_mutex);
18109 			mutex_enter(&sata_mutex);
18110 			sata_event_pending |= SATA_EVNT_MAIN;
18111 			mutex_exit(&sata_mutex);
18112 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18113 			    "Event processing postponed until "
18114 			    "AP control processing completes",
18115 			    NULL);
18116 			/* Check other ports */
18117 			continue;
18118 		} else {
18119 			/*
18120 			 * Set BSY flag so that AP control would not
18121 			 * interfere with events processing for
18122 			 * this port.
18123 			 */
18124 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18125 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18126 		}
18127 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18128 
18129 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18130 
18131 		if ((event_flags &
18132 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18133 			/*
18134 			 * Got port event.
18135 			 * We need some hierarchy of event processing as they
18136 			 * are affecting each other:
18137 			 * 1. port failed
18138 			 * 2. device detached/attached
18139 			 * 3. link events - link events may trigger device
18140 			 *    detached or device attached events in some
18141 			 *    circumstances.
18142 			 * 4. port power level changed
18143 			 */
18144 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18145 				sata_process_port_failed_event(sata_hba_inst,
18146 				    saddr);
18147 			}
18148 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18149 				sata_process_device_detached(sata_hba_inst,
18150 				    saddr);
18151 			}
18152 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18153 				sata_process_device_attached(sata_hba_inst,
18154 				    saddr);
18155 			}
18156 			if (event_flags &
18157 			    (SATA_EVNT_LINK_ESTABLISHED |
18158 			    SATA_EVNT_LINK_LOST)) {
18159 				sata_process_port_link_events(sata_hba_inst,
18160 				    saddr);
18161 			}
18162 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18163 				sata_process_port_pwr_change(sata_hba_inst,
18164 				    saddr);
18165 			}
18166 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18167 				sata_process_target_node_cleanup(
18168 				    sata_hba_inst, saddr);
18169 			}
18170 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18171 				sata_process_device_autoonline(
18172 				    sata_hba_inst, saddr);
18173 			}
18174 		}
18175 
18176 
18177 		/*
18178 		 * Scan port multiplier and all its sub-ports event flags.
18179 		 * The events are marked by
18180 		 * (1) sata_pmult_info.pmult_event_flags
18181 		 * (2) sata_pmport_info.pmport_event_flags
18182 		 */
18183 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18184 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18185 			/*
18186 			 * There should be another extra check: this
18187 			 * port multiplier still exists?
18188 			 */
18189 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18190 			    ncport);
18191 
18192 			if (pmultinfo != NULL) {
18193 				mutex_exit(&(SATA_CPORT_MUTEX(
18194 				    sata_hba_inst, ncport)));
18195 				sata_process_pmult_events(
18196 				    sata_hba_inst, ncport);
18197 				mutex_enter(&(SATA_CPORT_MUTEX(
18198 				    sata_hba_inst, ncport)));
18199 			} else {
18200 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18201 				    "Port-multiplier is gone. "
18202 				    "Ignore all sub-device events "
18203 				    "at port %d.", ncport);
18204 			}
18205 		}
18206 
18207 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18208 		    SATA_DTYPE_NONE) &&
18209 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18210 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18211 			    satadrv_event_flags &
18212 			    (SATA_EVNT_DEVICE_RESET |
18213 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18214 				/* Have device event */
18215 				sata_process_device_reset(sata_hba_inst,
18216 				    saddr);
18217 			}
18218 		}
18219 		/* Release PORT_BUSY flag */
18220 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18221 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18222 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18223 
18224 	} /* End of loop through the controller SATA ports */
18225 }
18226 
18227 /*
18228  * Specific port multiplier instance event processing. At the moment, device
18229  * event processing is limited to link/attach event only.
18230  *
18231  * NOTE: power management event is not supported yet.
18232  */
18233 static void
18234 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18235 {
18236 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18237 	sata_pmult_info_t *pmultinfo;
18238 	sata_pmport_info_t *pmportinfo;
18239 	sata_address_t *saddr;
18240 	sata_device_t sata_device;
18241 	uint32_t event_flags;
18242 	int npmport;
18243 	int rval;
18244 
18245 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18246 	    "Processing pmult event(s) on cport %d of controller %d",
18247 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18248 
18249 	/* First process events on port multiplier */
18250 	mutex_enter(&cportinfo->cport_mutex);
18251 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18252 	event_flags = pmultinfo->pmult_event_flags;
18253 
18254 	/*
18255 	 * Reset event (of port multiplier) has higher priority because the
18256 	 * port multiplier itself might be failed or removed after reset.
18257 	 */
18258 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18259 		/*
18260 		 * The status of the sub-links are uncertain,
18261 		 * so mark all sub-ports as RESET
18262 		 */
18263 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18264 		    sata_hba_inst, cport); npmport ++) {
18265 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18266 			    cport, npmport);
18267 			if (pmportinfo == NULL) {
18268 				/* That's weird. */
18269 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18270 				    "sata_hba_event_notify: "
18271 				    "invalid/un-implemented "
18272 				    "port %d:%d (%d ports), ",
18273 				    cport, npmport, SATA_NUM_PMPORTS(
18274 				    sata_hba_inst, cport)));
18275 				continue;
18276 			}
18277 
18278 			mutex_enter(&pmportinfo->pmport_mutex);
18279 
18280 			/* Mark all pmport to unknow state. */
18281 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18282 			/* Mark all pmports with link events. */
18283 			pmportinfo->pmport_event_flags =
18284 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18285 			mutex_exit(&pmportinfo->pmport_mutex);
18286 		}
18287 
18288 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18289 		/*
18290 		 * We need probe the port multiplier to know what has
18291 		 * happened.
18292 		 */
18293 		bzero(&sata_device, sizeof (sata_device_t));
18294 		sata_device.satadev_rev = SATA_DEVICE_REV;
18295 		sata_device.satadev_addr.cport = cport;
18296 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18297 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18298 
18299 		mutex_exit(&cportinfo->cport_mutex);
18300 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18301 		    (SATA_DIP(sata_hba_inst), &sata_device);
18302 		mutex_enter(&cportinfo->cport_mutex);
18303 		if (rval != SATA_SUCCESS) {
18304 			/* Something went wrong? Fail the port */
18305 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18306 			mutex_exit(&cportinfo->cport_mutex);
18307 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18308 			    "SATA port %d probing failed", cport));
18309 
18310 			/* PMult structure must be released.  */
18311 			sata_free_pmult(sata_hba_inst, &sata_device);
18312 			return;
18313 		}
18314 
18315 		sata_update_port_info(sata_hba_inst, &sata_device);
18316 
18317 		/*
18318 		 * Sanity check - Port is active? Is the link active?
18319 		 * The device is still a port multiplier?
18320 		 */
18321 		if ((cportinfo->cport_state &
18322 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18323 		    ((cportinfo->cport_scr.sstatus &
18324 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18325 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18326 			mutex_exit(&cportinfo->cport_mutex);
18327 
18328 			/* PMult structure must be released.  */
18329 			sata_free_pmult(sata_hba_inst, &sata_device);
18330 			return;
18331 		}
18332 
18333 		/* Probed succeed, set port ready. */
18334 		cportinfo->cport_state |=
18335 		    SATA_STATE_PROBED | SATA_STATE_READY;
18336 	}
18337 
18338 	/* Release port multiplier event flags. */
18339 	pmultinfo->pmult_event_flags &=
18340 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18341 	mutex_exit(&cportinfo->cport_mutex);
18342 
18343 	/*
18344 	 * Check all sub-links.
18345 	 */
18346 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18347 	    npmport ++) {
18348 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18349 		mutex_enter(&pmportinfo->pmport_mutex);
18350 		event_flags = pmportinfo->pmport_event_flags;
18351 		mutex_exit(&pmportinfo->pmport_mutex);
18352 		saddr = &pmportinfo->pmport_addr;
18353 
18354 		if ((event_flags &
18355 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18356 			/*
18357 			 * Got port multiplier port event.
18358 			 * We need some hierarchy of event processing as they
18359 			 * are affecting each other:
18360 			 * 1. device detached/attached
18361 			 * 2. link events - link events may trigger device
18362 			 *    detached or device attached events in some
18363 			 *    circumstances.
18364 			 */
18365 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18366 				sata_process_pmdevice_detached(sata_hba_inst,
18367 				    saddr);
18368 			}
18369 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18370 				sata_process_pmdevice_attached(sata_hba_inst,
18371 				    saddr);
18372 			}
18373 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18374 			    event_flags & SATA_EVNT_LINK_LOST) {
18375 				sata_process_pmport_link_events(sata_hba_inst,
18376 				    saddr);
18377 			}
18378 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18379 				sata_process_target_node_cleanup(
18380 				    sata_hba_inst, saddr);
18381 			}
18382 		}
18383 
18384 		/* Checking drive event(s). */
18385 		mutex_enter(&pmportinfo->pmport_mutex);
18386 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18387 		    pmportinfo->pmport_sata_drive != NULL) {
18388 			event_flags = pmportinfo->pmport_sata_drive->
18389 			    satadrv_event_flags;
18390 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18391 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18392 
18393 				/* Have device event */
18394 				sata_process_pmdevice_reset(sata_hba_inst,
18395 				    saddr);
18396 			}
18397 		}
18398 		mutex_exit(&pmportinfo->pmport_mutex);
18399 
18400 		/* Release PORT_BUSY flag */
18401 		mutex_enter(&cportinfo->cport_mutex);
18402 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18403 		mutex_exit(&cportinfo->cport_mutex);
18404 	}
18405 
18406 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18407 	    "[DONE] pmult event(s) on cport %d of controller %d",
18408 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18409 }
18410 
18411 /*
18412  * Process HBA power level change reported by HBA driver.
18413  * Not implemented at this time - event is ignored.
18414  */
18415 static void
18416 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18417 {
18418 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18419 	    "Processing controller power level change", NULL);
18420 
18421 	/* Ignoring it for now */
18422 	mutex_enter(&sata_hba_inst->satahba_mutex);
18423 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18424 	mutex_exit(&sata_hba_inst->satahba_mutex);
18425 }
18426 
18427 /*
18428  * Process port power level change reported by HBA driver.
18429  * Not implemented at this time - event is ignored.
18430  */
18431 static void
18432 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18433     sata_address_t *saddr)
18434 {
18435 	sata_cport_info_t *cportinfo;
18436 
18437 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18438 	    "Processing port power level change", NULL);
18439 
18440 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18441 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18442 	/* Reset event flag */
18443 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18444 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18445 }
18446 
18447 /*
18448  * Process port failure reported by HBA driver.
18449  * cports support only - no pmports.
18450  */
18451 static void
18452 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18453     sata_address_t *saddr)
18454 {
18455 	sata_cport_info_t *cportinfo;
18456 
18457 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18458 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18459 	/* Reset event flag first */
18460 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18461 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18462 	if ((cportinfo->cport_state &
18463 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18464 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18465 		    cport_mutex);
18466 		return;
18467 	}
18468 	/* Fail the port */
18469 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18470 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18471 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18472 }
18473 
18474 /*
18475  * Device Reset Event processing.
18476  * The sequence is managed by 3 stage flags:
18477  * - reset event reported,
18478  * - reset event being processed,
18479  * - request to clear device reset state.
18480  *
18481  * NOTE: This function has to be entered with cport mutex held. It exits with
18482  * mutex held as well, but can release mutex during the processing.
18483  */
18484 static void
18485 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18486     sata_address_t *saddr)
18487 {
18488 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18489 	sata_drive_info_t *sdinfo;
18490 	sata_cport_info_t *cportinfo;
18491 	sata_device_t sata_device;
18492 	int rval_probe, rval_set;
18493 
18494 	/* We only care about host sata cport for now */
18495 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18496 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18497 	/*
18498 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18499 	 * state, ignore reset event.
18500 	 */
18501 	if (((cportinfo->cport_state &
18502 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18503 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18504 		sdinfo->satadrv_event_flags &=
18505 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18506 		return;
18507 	}
18508 
18509 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18510 	    SATA_DTYPE_PMULT)) {
18511 		/*
18512 		 * Should not happened: this is already handled in
18513 		 * sata_hba_event_notify()
18514 		 */
18515 		mutex_exit(&cportinfo->cport_mutex);
18516 		goto done;
18517 	}
18518 
18519 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18520 	    SATA_VALID_DEV_TYPE) == 0) {
18521 		/*
18522 		 * This should not happen - coding error.
18523 		 * But we can recover, so do not panic, just clean up
18524 		 * and if in debug mode, log the message.
18525 		 */
18526 #ifdef SATA_DEBUG
18527 		sata_log(sata_hba_inst, CE_WARN,
18528 		    "sata_process_device_reset: "
18529 		    "Invalid device type with sdinfo!", NULL);
18530 #endif
18531 		sdinfo->satadrv_event_flags = 0;
18532 		return;
18533 	}
18534 
18535 #ifdef SATA_DEBUG
18536 	if ((sdinfo->satadrv_event_flags &
18537 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18538 		/* Nothing to do */
18539 		/* Something is weird - why we are processing dev reset? */
18540 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18541 		    "No device reset event!!!!", NULL);
18542 
18543 		return;
18544 	}
18545 	if ((sdinfo->satadrv_event_flags &
18546 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18547 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18548 		/* Something is weird - new device reset event */
18549 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18550 		    "Overlapping device reset events!", NULL);
18551 	}
18552 #endif
18553 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18554 	    "Processing port %d device reset", saddr->cport);
18555 
18556 	/* Clear event flag */
18557 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18558 
18559 	/* It seems that we always need to check the port state first */
18560 	sata_device.satadev_rev = SATA_DEVICE_REV;
18561 	sata_device.satadev_addr = *saddr;
18562 	/*
18563 	 * We have to exit mutex, because the HBA probe port function may
18564 	 * block on its own mutex.
18565 	 */
18566 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18567 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18568 	    (SATA_DIP(sata_hba_inst), &sata_device);
18569 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18570 	sata_update_port_info(sata_hba_inst, &sata_device);
18571 	if (rval_probe != SATA_SUCCESS) {
18572 		/* Something went wrong? Fail the port */
18573 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18574 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18575 		if (sdinfo != NULL)
18576 			sdinfo->satadrv_event_flags = 0;
18577 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18578 		    cport_mutex);
18579 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18580 		    "SATA port %d probing failed",
18581 		    saddr->cport));
18582 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18583 		    saddr->cport)->cport_mutex);
18584 		return;
18585 	}
18586 	if ((sata_device.satadev_scr.sstatus  &
18587 	    SATA_PORT_DEVLINK_UP_MASK) !=
18588 	    SATA_PORT_DEVLINK_UP ||
18589 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18590 		/*
18591 		 * No device to process, anymore. Some other event processing
18592 		 * would or have already performed port info cleanup.
18593 		 * To be safe (HBA may need it), request clearing device
18594 		 * reset condition.
18595 		 */
18596 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18597 		if (sdinfo != NULL) {
18598 			sdinfo->satadrv_event_flags &=
18599 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18600 			sdinfo->satadrv_event_flags |=
18601 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18602 		}
18603 		return;
18604 	}
18605 
18606 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18607 	if (sdinfo == NULL) {
18608 		return;
18609 	}
18610 	if ((sdinfo->satadrv_event_flags &
18611 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18612 		/*
18613 		 * Start tracking time for device feature restoration and
18614 		 * identification. Save current time (lbolt value).
18615 		 */
18616 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18617 	}
18618 	/* Mark device reset processing as active */
18619 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18620 
18621 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18622 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18623 
18624 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18625 
18626 	if (rval_set  != SATA_SUCCESS) {
18627 		/*
18628 		 * Restoring drive setting failed.
18629 		 * Probe the port first, to check if the port state has changed
18630 		 */
18631 		sata_device.satadev_rev = SATA_DEVICE_REV;
18632 		sata_device.satadev_addr = *saddr;
18633 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18634 		/* probe port */
18635 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18636 		    (SATA_DIP(sata_hba_inst), &sata_device);
18637 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18638 		    cport_mutex);
18639 		if (rval_probe == SATA_SUCCESS &&
18640 		    (sata_device.satadev_state &
18641 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18642 		    (sata_device.satadev_scr.sstatus  &
18643 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18644 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18645 			/*
18646 			 * We may retry this a bit later - in-process reset
18647 			 * condition should be already set.
18648 			 * Track retry time for device identification.
18649 			 */
18650 			if ((cportinfo->cport_dev_type &
18651 			    SATA_VALID_DEV_TYPE) != 0 &&
18652 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18653 			    sdinfo->satadrv_reset_time != 0) {
18654 				clock_t cur_time = ddi_get_lbolt();
18655 				/*
18656 				 * If the retry time limit was not
18657 				 * exceeded, retry.
18658 				 */
18659 				if ((cur_time - sdinfo->satadrv_reset_time) <
18660 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18661 					mutex_enter(
18662 					    &sata_hba_inst->satahba_mutex);
18663 					sata_hba_inst->satahba_event_flags |=
18664 					    SATA_EVNT_MAIN;
18665 					mutex_exit(
18666 					    &sata_hba_inst->satahba_mutex);
18667 					mutex_enter(&sata_mutex);
18668 					sata_event_pending |= SATA_EVNT_MAIN;
18669 					mutex_exit(&sata_mutex);
18670 					return;
18671 				}
18672 				if (rval_set == SATA_RETRY) {
18673 					/*
18674 					 * Setting drive features failed, but
18675 					 * the drive is still accessible,
18676 					 * so emit a warning message before
18677 					 * return.
18678 					 */
18679 					mutex_exit(&SATA_CPORT_INFO(
18680 					    sata_hba_inst,
18681 					    saddr->cport)->cport_mutex);
18682 					goto done;
18683 				}
18684 			}
18685 			/* Fail the drive */
18686 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18687 
18688 			sata_log(sata_hba_inst, CE_WARN,
18689 			    "SATA device at port %d - device failed",
18690 			    saddr->cport);
18691 
18692 			DTRACE_PROBE(port_failed_f);
18693 		}
18694 		/*
18695 		 * No point of retrying - device failed or some other event
18696 		 * processing or already did or will do port info cleanup.
18697 		 * To be safe (HBA may need it),
18698 		 * request clearing device reset condition.
18699 		 */
18700 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18701 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18702 		sdinfo->satadrv_reset_time = 0;
18703 		return;
18704 	}
18705 done:
18706 	/*
18707 	 * If setting of drive features failed, but the drive is still
18708 	 * accessible, emit a warning message.
18709 	 */
18710 	if (rval_set == SATA_RETRY) {
18711 		sata_log(sata_hba_inst, CE_WARN,
18712 		    "SATA device at port %d - desired setting could not be "
18713 		    "restored after reset. Device may not operate as expected.",
18714 		    saddr->cport);
18715 	}
18716 	/*
18717 	 * Raise the flag indicating that the next sata command could
18718 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18719 	 * reset is reported.
18720 	 */
18721 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18722 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18723 		sdinfo->satadrv_reset_time = 0;
18724 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18725 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18726 			sdinfo->satadrv_event_flags &=
18727 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18728 			sdinfo->satadrv_event_flags |=
18729 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18730 		}
18731 	}
18732 }
18733 
18734 
18735 /*
18736  * Port Multiplier Port Device Reset Event processing.
18737  *
18738  * NOTE: This function has to be entered with pmport mutex held. It exits with
18739  * mutex held as well, but can release mutex during the processing.
18740  */
18741 static void
18742 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18743     sata_address_t *saddr)
18744 {
18745 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18746 	sata_drive_info_t *sdinfo = NULL;
18747 	sata_cport_info_t *cportinfo = NULL;
18748 	sata_pmport_info_t *pmportinfo = NULL;
18749 	sata_pmult_info_t *pminfo = NULL;
18750 	sata_device_t sata_device;
18751 	uint8_t cport = saddr->cport;
18752 	uint8_t pmport = saddr->pmport;
18753 	int rval;
18754 
18755 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18756 	    "Processing drive reset at port %d:%d", cport, pmport);
18757 
18758 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18759 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18760 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18761 
18762 	/*
18763 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18764 	 * state, ignore reset event.
18765 	 */
18766 	if (((cportinfo->cport_state &
18767 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18768 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18769 		sdinfo->satadrv_event_flags &=
18770 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18771 		return;
18772 	}
18773 
18774 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18775 		/*
18776 		 * This should not happen - coding error.
18777 		 * But we can recover, so do not panic, just clean up
18778 		 * and if in debug mode, log the message.
18779 		 */
18780 #ifdef SATA_DEBUG
18781 		sata_log(sata_hba_inst, CE_WARN,
18782 		    "sata_process_pmdevice_reset: "
18783 		    "Invalid device type with sdinfo!", NULL);
18784 #endif
18785 		sdinfo->satadrv_event_flags = 0;
18786 		return;
18787 	}
18788 
18789 #ifdef SATA_DEBUG
18790 	if ((sdinfo->satadrv_event_flags &
18791 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18792 		/* Nothing to do */
18793 		/* Something is weird - why we are processing dev reset? */
18794 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18795 		    "No device reset event!!!!", NULL);
18796 
18797 		return;
18798 	}
18799 	if ((sdinfo->satadrv_event_flags &
18800 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18801 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18802 		/* Something is weird - new device reset event */
18803 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18804 		    "Overlapping device reset events!", NULL);
18805 	}
18806 #endif
18807 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18808 	    "Processing port %d:%d device reset", cport, pmport);
18809 
18810 	/* Clear event flag */
18811 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18812 
18813 	/* It seems that we always need to check the port state first */
18814 	sata_device.satadev_rev = SATA_DEVICE_REV;
18815 	sata_device.satadev_addr = *saddr;
18816 	/*
18817 	 * We have to exit mutex, because the HBA probe port function may
18818 	 * block on its own mutex.
18819 	 */
18820 	mutex_exit(&pmportinfo->pmport_mutex);
18821 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18822 	    (SATA_DIP(sata_hba_inst), &sata_device);
18823 	mutex_enter(&pmportinfo->pmport_mutex);
18824 
18825 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18826 	if (rval != SATA_SUCCESS) {
18827 		/* Something went wrong? Fail the port */
18828 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18829 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18830 		    saddr->pmport);
18831 		if (sdinfo != NULL)
18832 			sdinfo->satadrv_event_flags = 0;
18833 		mutex_exit(&pmportinfo->pmport_mutex);
18834 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18835 		    "SATA port %d:%d probing failed",
18836 		    saddr->cport, saddr->pmport));
18837 		mutex_enter(&pmportinfo->pmport_mutex);
18838 		return;
18839 	}
18840 	if ((sata_device.satadev_scr.sstatus  &
18841 	    SATA_PORT_DEVLINK_UP_MASK) !=
18842 	    SATA_PORT_DEVLINK_UP ||
18843 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18844 		/*
18845 		 * No device to process, anymore. Some other event processing
18846 		 * would or have already performed port info cleanup.
18847 		 * To be safe (HBA may need it), request clearing device
18848 		 * reset condition.
18849 		 */
18850 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18851 		    saddr->pmport);
18852 		if (sdinfo != NULL) {
18853 			sdinfo->satadrv_event_flags &=
18854 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18855 			/* must clear flags on cport */
18856 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18857 			    saddr->cport);
18858 			pminfo->pmult_event_flags |=
18859 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18860 		}
18861 		return;
18862 	}
18863 
18864 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18865 	    saddr->pmport);
18866 	if (sdinfo == NULL) {
18867 		return;
18868 	}
18869 	if ((sdinfo->satadrv_event_flags &
18870 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18871 		/*
18872 		 * Start tracking time for device feature restoration and
18873 		 * identification. Save current time (lbolt value).
18874 		 */
18875 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18876 	}
18877 	/* Mark device reset processing as active */
18878 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18879 
18880 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18881 	mutex_exit(&pmportinfo->pmport_mutex);
18882 
18883 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18884 	    SATA_FAILURE) {
18885 		/*
18886 		 * Restoring drive setting failed.
18887 		 * Probe the port first, to check if the port state has changed
18888 		 */
18889 		sata_device.satadev_rev = SATA_DEVICE_REV;
18890 		sata_device.satadev_addr = *saddr;
18891 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18892 
18893 		/* probe port */
18894 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18895 		    (SATA_DIP(sata_hba_inst), &sata_device);
18896 		mutex_enter(&pmportinfo->pmport_mutex);
18897 		if (rval == SATA_SUCCESS &&
18898 		    (sata_device.satadev_state &
18899 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18900 		    (sata_device.satadev_scr.sstatus  &
18901 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18902 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18903 			/*
18904 			 * We may retry this a bit later - in-process reset
18905 			 * condition should be already set.
18906 			 * Track retry time for device identification.
18907 			 */
18908 			if ((pmportinfo->pmport_dev_type &
18909 			    SATA_VALID_DEV_TYPE) != 0 &&
18910 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18911 			    sdinfo->satadrv_reset_time != 0) {
18912 				clock_t cur_time = ddi_get_lbolt();
18913 				/*
18914 				 * If the retry time limit was not
18915 				 * exceeded, retry.
18916 				 */
18917 				if ((cur_time - sdinfo->satadrv_reset_time) <
18918 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18919 					mutex_enter(
18920 					    &sata_hba_inst->satahba_mutex);
18921 					sata_hba_inst->satahba_event_flags |=
18922 					    SATA_EVNT_MAIN;
18923 					mutex_exit(
18924 					    &sata_hba_inst->satahba_mutex);
18925 					mutex_enter(&sata_mutex);
18926 					sata_event_pending |= SATA_EVNT_MAIN;
18927 					mutex_exit(&sata_mutex);
18928 					return;
18929 				}
18930 			}
18931 			/* Fail the drive */
18932 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18933 
18934 			sata_log(sata_hba_inst, CE_WARN,
18935 			    "SATA device at port %d:%d - device failed",
18936 			    saddr->cport, saddr->pmport);
18937 		} else {
18938 			/*
18939 			 * No point of retrying - some other event processing
18940 			 * would or already did port info cleanup.
18941 			 * To be safe (HBA may need it),
18942 			 * request clearing device reset condition.
18943 			 */
18944 			sdinfo->satadrv_event_flags |=
18945 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18946 		}
18947 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18948 		sdinfo->satadrv_reset_time = 0;
18949 		return;
18950 	}
18951 	/*
18952 	 * Raise the flag indicating that the next sata command could
18953 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18954 	 * reset is reported.
18955 	 */
18956 	mutex_enter(&pmportinfo->pmport_mutex);
18957 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18958 		sdinfo->satadrv_reset_time = 0;
18959 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
18960 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18961 			sdinfo->satadrv_event_flags &=
18962 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18963 			/* must clear flags on cport */
18964 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18965 			    saddr->cport);
18966 			pminfo->pmult_event_flags |=
18967 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18968 		}
18969 	}
18970 }
18971 
18972 /*
18973  * Port Link Events processing.
18974  * Every link established event may involve device reset (due to
18975  * COMRESET signal, equivalent of the hard reset) so arbitrarily
18976  * set device reset event for an attached device (if any).
18977  * If the port is in SHUTDOWN or FAILED state, ignore link events.
18978  *
18979  * The link established event processing varies, depending on the state
18980  * of the target node, HBA hotplugging capabilities, state of the port.
18981  * If the link is not active, the link established event is ignored.
18982  * If HBA cannot detect device attachment and there is no target node,
18983  * the link established event triggers device attach event processing.
18984  * Else, link established event triggers device reset event processing.
18985  *
18986  * The link lost event processing varies, depending on a HBA hotplugging
18987  * capability and the state of the port (link active or not active).
18988  * If the link is active, the lost link event is ignored.
18989  * If HBA cannot detect device removal, the lost link event triggers
18990  * device detached event processing after link lost timeout.
18991  * Else, the event is ignored.
18992  *
18993  * NOTE: Port multiplier ports events are handled by
18994  * sata_process_pmport_link_events();
18995  */
18996 static void
18997 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
18998     sata_address_t *saddr)
18999 {
19000 	sata_device_t sata_device;
19001 	sata_cport_info_t *cportinfo;
19002 	sata_drive_info_t *sdinfo;
19003 	uint32_t event_flags;
19004 	int rval;
19005 
19006 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19007 	    "Processing port %d link event(s)", saddr->cport);
19008 
19009 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19010 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19011 	event_flags = cportinfo->cport_event_flags;
19012 
19013 	/* Reset event flags first */
19014 	cportinfo->cport_event_flags &=
19015 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19016 
19017 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19018 	if ((cportinfo->cport_state &
19019 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19020 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19021 		    cport_mutex);
19022 		return;
19023 	}
19024 
19025 	/*
19026 	 * For the sanity sake get current port state.
19027 	 * Set device address only. Other sata_device fields should be
19028 	 * set by HBA driver.
19029 	 */
19030 	sata_device.satadev_rev = SATA_DEVICE_REV;
19031 	sata_device.satadev_addr = *saddr;
19032 	/*
19033 	 * We have to exit mutex, because the HBA probe port function may
19034 	 * block on its own mutex.
19035 	 */
19036 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19037 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19038 	    (SATA_DIP(sata_hba_inst), &sata_device);
19039 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19040 	sata_update_port_info(sata_hba_inst, &sata_device);
19041 	if (rval != SATA_SUCCESS) {
19042 		/* Something went wrong? Fail the port */
19043 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19044 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19045 		    cport_mutex);
19046 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19047 		    "SATA port %d probing failed",
19048 		    saddr->cport));
19049 		/*
19050 		 * We may want to release device info structure, but
19051 		 * it is not necessary.
19052 		 */
19053 		return;
19054 	} else {
19055 		/* port probed successfully */
19056 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19057 	}
19058 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19059 
19060 		if ((sata_device.satadev_scr.sstatus &
19061 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19062 			/* Ignore event */
19063 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19064 			    "Ignoring port %d link established event - "
19065 			    "link down",
19066 			    saddr->cport);
19067 			goto linklost;
19068 		}
19069 
19070 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19071 		    "Processing port %d link established event",
19072 		    saddr->cport);
19073 
19074 		/*
19075 		 * For the sanity sake check if a device is attached - check
19076 		 * return state of a port probing.
19077 		 */
19078 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19079 			/*
19080 			 * HBA port probe indicated that there is a device
19081 			 * attached. Check if the framework had device info
19082 			 * structure attached for this device.
19083 			 */
19084 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19085 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19086 				    NULL);
19087 
19088 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19089 				if ((sdinfo->satadrv_type &
19090 				    SATA_VALID_DEV_TYPE) != 0) {
19091 					/*
19092 					 * Dev info structure is present.
19093 					 * If dev_type is set to known type in
19094 					 * the framework's drive info struct
19095 					 * then the device existed before and
19096 					 * the link was probably lost
19097 					 * momentarily - in such case
19098 					 * we may want to check device
19099 					 * identity.
19100 					 * Identity check is not supported now.
19101 					 *
19102 					 * Link established event
19103 					 * triggers device reset event.
19104 					 */
19105 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19106 					    satadrv_event_flags |=
19107 					    SATA_EVNT_DEVICE_RESET;
19108 				}
19109 			} else if (cportinfo->cport_dev_type ==
19110 			    SATA_DTYPE_NONE) {
19111 				/*
19112 				 * We got new device attached! If HBA does not
19113 				 * generate device attached events, trigger it
19114 				 * here.
19115 				 */
19116 				if (!(SATA_FEATURES(sata_hba_inst) &
19117 				    SATA_CTLF_HOTPLUG)) {
19118 					cportinfo->cport_event_flags |=
19119 					    SATA_EVNT_DEVICE_ATTACHED;
19120 				}
19121 			}
19122 			/* Reset link lost timeout */
19123 			cportinfo->cport_link_lost_time = 0;
19124 		}
19125 	}
19126 linklost:
19127 	if (event_flags & SATA_EVNT_LINK_LOST) {
19128 		if ((sata_device.satadev_scr.sstatus &
19129 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19130 			/* Ignore event */
19131 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19132 			    "Ignoring port %d link lost event - link is up",
19133 			    saddr->cport);
19134 			goto done;
19135 		}
19136 #ifdef SATA_DEBUG
19137 		if (cportinfo->cport_link_lost_time == 0) {
19138 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19139 			    "Processing port %d link lost event",
19140 			    saddr->cport);
19141 		}
19142 #endif
19143 		/*
19144 		 * When HBA cannot generate device attached/detached events,
19145 		 * we need to track link lost time and eventually generate
19146 		 * device detach event.
19147 		 */
19148 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19149 			/* We are tracking link lost time */
19150 			if (cportinfo->cport_link_lost_time == 0) {
19151 				/* save current time (lbolt value) */
19152 				cportinfo->cport_link_lost_time =
19153 				    ddi_get_lbolt();
19154 				/* just keep link lost event */
19155 				cportinfo->cport_event_flags |=
19156 				    SATA_EVNT_LINK_LOST;
19157 			} else {
19158 				clock_t cur_time = ddi_get_lbolt();
19159 				if ((cur_time -
19160 				    cportinfo->cport_link_lost_time) >=
19161 				    drv_usectohz(
19162 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19163 					/* trigger device detach event */
19164 					cportinfo->cport_event_flags |=
19165 					    SATA_EVNT_DEVICE_DETACHED;
19166 					cportinfo->cport_link_lost_time = 0;
19167 					SATADBG1(SATA_DBG_EVENTS,
19168 					    sata_hba_inst,
19169 					    "Triggering port %d "
19170 					    "device detached event",
19171 					    saddr->cport);
19172 				} else {
19173 					/* keep link lost event */
19174 					cportinfo->cport_event_flags |=
19175 					    SATA_EVNT_LINK_LOST;
19176 				}
19177 			}
19178 		}
19179 		/*
19180 		 * We could change port state to disable/delay access to
19181 		 * the attached device until the link is recovered.
19182 		 */
19183 	}
19184 done:
19185 	event_flags = cportinfo->cport_event_flags;
19186 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19187 	if (event_flags != 0) {
19188 		mutex_enter(&sata_hba_inst->satahba_mutex);
19189 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19190 		mutex_exit(&sata_hba_inst->satahba_mutex);
19191 		mutex_enter(&sata_mutex);
19192 		sata_event_pending |= SATA_EVNT_MAIN;
19193 		mutex_exit(&sata_mutex);
19194 	}
19195 }
19196 
19197 /*
19198  * Port Multiplier Port Link Events processing.
19199  */
19200 static void
19201 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19202     sata_address_t *saddr)
19203 {
19204 	sata_device_t sata_device;
19205 	sata_pmport_info_t *pmportinfo = NULL;
19206 	sata_drive_info_t *sdinfo = NULL;
19207 	uint32_t event_flags;
19208 	uint8_t cport = saddr->cport;
19209 	uint8_t pmport = saddr->pmport;
19210 	int rval;
19211 
19212 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19213 	    "Processing port %d:%d link event(s)",
19214 	    cport, pmport);
19215 
19216 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19217 	mutex_enter(&pmportinfo->pmport_mutex);
19218 	event_flags = pmportinfo->pmport_event_flags;
19219 
19220 	/* Reset event flags first */
19221 	pmportinfo->pmport_event_flags &=
19222 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19223 
19224 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19225 	if ((pmportinfo->pmport_state &
19226 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19227 		mutex_exit(&pmportinfo->pmport_mutex);
19228 		return;
19229 	}
19230 
19231 	/*
19232 	 * For the sanity sake get current port state.
19233 	 * Set device address only. Other sata_device fields should be
19234 	 * set by HBA driver.
19235 	 */
19236 	sata_device.satadev_rev = SATA_DEVICE_REV;
19237 	sata_device.satadev_addr = *saddr;
19238 	/*
19239 	 * We have to exit mutex, because the HBA probe port function may
19240 	 * block on its own mutex.
19241 	 */
19242 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19243 	    saddr->pmport));
19244 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19245 	    (SATA_DIP(sata_hba_inst), &sata_device);
19246 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19247 	    saddr->pmport));
19248 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19249 	if (rval != SATA_SUCCESS) {
19250 		/* Something went wrong? Fail the port */
19251 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19252 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19253 		    saddr->pmport));
19254 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19255 		    "SATA port %d:%d probing failed",
19256 		    saddr->cport, saddr->pmport));
19257 		/*
19258 		 * We may want to release device info structure, but
19259 		 * it is not necessary.
19260 		 */
19261 		return;
19262 	} else {
19263 		/* port probed successfully */
19264 		pmportinfo->pmport_state |=
19265 		    SATA_STATE_PROBED | SATA_STATE_READY;
19266 	}
19267 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19268 	    saddr->cport, saddr->pmport));
19269 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19270 	    saddr->cport, saddr->pmport));
19271 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19272 
19273 		if ((sata_device.satadev_scr.sstatus &
19274 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19275 			/* Ignore event */
19276 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19277 			    "Ignoring port %d:%d link established event - "
19278 			    "link down",
19279 			    saddr->cport, saddr->pmport);
19280 			goto linklost;
19281 		}
19282 
19283 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19284 		    "Processing port %d:%d link established event",
19285 		    cport, pmport);
19286 
19287 		/*
19288 		 * For the sanity sake check if a device is attached - check
19289 		 * return state of a port probing.
19290 		 */
19291 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19292 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19293 			/*
19294 			 * HBA port probe indicated that there is a device
19295 			 * attached. Check if the framework had device info
19296 			 * structure attached for this device.
19297 			 */
19298 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19299 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19300 				    NULL);
19301 
19302 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19303 				if ((sdinfo->satadrv_type &
19304 				    SATA_VALID_DEV_TYPE) != 0) {
19305 					/*
19306 					 * Dev info structure is present.
19307 					 * If dev_type is set to known type in
19308 					 * the framework's drive info struct
19309 					 * then the device existed before and
19310 					 * the link was probably lost
19311 					 * momentarily - in such case
19312 					 * we may want to check device
19313 					 * identity.
19314 					 * Identity check is not supported now.
19315 					 *
19316 					 * Link established event
19317 					 * triggers device reset event.
19318 					 */
19319 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19320 					    satadrv_event_flags |=
19321 					    SATA_EVNT_DEVICE_RESET;
19322 				}
19323 			} else if (pmportinfo->pmport_dev_type ==
19324 			    SATA_DTYPE_NONE) {
19325 				/*
19326 				 * We got new device attached! If HBA does not
19327 				 * generate device attached events, trigger it
19328 				 * here.
19329 				 */
19330 				if (!(SATA_FEATURES(sata_hba_inst) &
19331 				    SATA_CTLF_HOTPLUG)) {
19332 					pmportinfo->pmport_event_flags |=
19333 					    SATA_EVNT_DEVICE_ATTACHED;
19334 				}
19335 			}
19336 			/* Reset link lost timeout */
19337 			pmportinfo->pmport_link_lost_time = 0;
19338 		}
19339 	}
19340 linklost:
19341 	if (event_flags & SATA_EVNT_LINK_LOST) {
19342 #ifdef SATA_DEBUG
19343 		if (pmportinfo->pmport_link_lost_time == 0) {
19344 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19345 			    "Processing port %d:%d link lost event",
19346 			    saddr->cport, saddr->pmport);
19347 		}
19348 #endif
19349 		if ((sata_device.satadev_scr.sstatus &
19350 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19351 			/* Ignore event */
19352 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19353 			    "Ignoring port %d:%d link lost event - link is up",
19354 			    saddr->cport, saddr->pmport);
19355 			goto done;
19356 		}
19357 		/*
19358 		 * When HBA cannot generate device attached/detached events,
19359 		 * we need to track link lost time and eventually generate
19360 		 * device detach event.
19361 		 */
19362 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19363 			/* We are tracking link lost time */
19364 			if (pmportinfo->pmport_link_lost_time == 0) {
19365 				/* save current time (lbolt value) */
19366 				pmportinfo->pmport_link_lost_time =
19367 				    ddi_get_lbolt();
19368 				/* just keep link lost event */
19369 				pmportinfo->pmport_event_flags |=
19370 				    SATA_EVNT_LINK_LOST;
19371 			} else {
19372 				clock_t cur_time = ddi_get_lbolt();
19373 				if ((cur_time -
19374 				    pmportinfo->pmport_link_lost_time) >=
19375 				    drv_usectohz(
19376 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19377 					/* trigger device detach event */
19378 					pmportinfo->pmport_event_flags |=
19379 					    SATA_EVNT_DEVICE_DETACHED;
19380 					pmportinfo->pmport_link_lost_time = 0;
19381 					SATADBG2(SATA_DBG_EVENTS,
19382 					    sata_hba_inst,
19383 					    "Triggering port %d:%d "
19384 					    "device detached event",
19385 					    saddr->cport, saddr->pmport);
19386 				} else {
19387 					/* keep link lost event */
19388 					pmportinfo->pmport_event_flags |=
19389 					    SATA_EVNT_LINK_LOST;
19390 				}
19391 			}
19392 		}
19393 		/*
19394 		 * We could change port state to disable/delay access to
19395 		 * the attached device until the link is recovered.
19396 		 */
19397 	}
19398 done:
19399 	event_flags = pmportinfo->pmport_event_flags;
19400 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19401 	    saddr->pmport));
19402 	if (event_flags != 0) {
19403 		mutex_enter(&sata_hba_inst->satahba_mutex);
19404 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19405 		mutex_exit(&sata_hba_inst->satahba_mutex);
19406 		mutex_enter(&sata_mutex);
19407 		sata_event_pending |= SATA_EVNT_MAIN;
19408 		mutex_exit(&sata_mutex);
19409 	}
19410 }
19411 
19412 /*
19413  * Device Detached Event processing.
19414  * Port is probed to find if a device is really gone. If so,
19415  * the device info structure is detached from the SATA port info structure
19416  * and released.
19417  * Port status is updated.
19418  *
19419  * NOTE: Port multiplier ports events are handled by
19420  * sata_process_pmdevice_detached()
19421  */
19422 static void
19423 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19424     sata_address_t *saddr)
19425 {
19426 	sata_cport_info_t *cportinfo;
19427 	sata_pmport_info_t *pmportinfo;
19428 	sata_drive_info_t *sdevinfo;
19429 	sata_device_t sata_device;
19430 	sata_address_t pmport_addr;
19431 	char name[16];
19432 	uint8_t cport = saddr->cport;
19433 	int npmport;
19434 	int rval;
19435 
19436 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19437 	    "Processing port %d device detached", saddr->cport);
19438 
19439 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19440 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19441 	/* Clear event flag */
19442 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19443 
19444 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19445 	if ((cportinfo->cport_state &
19446 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19447 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19448 		    cport_mutex);
19449 		return;
19450 	}
19451 	/* For sanity, re-probe the port */
19452 	sata_device.satadev_rev = SATA_DEVICE_REV;
19453 	sata_device.satadev_addr = *saddr;
19454 
19455 	/*
19456 	 * We have to exit mutex, because the HBA probe port function may
19457 	 * block on its own mutex.
19458 	 */
19459 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19460 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19461 	    (SATA_DIP(sata_hba_inst), &sata_device);
19462 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19463 	sata_update_port_info(sata_hba_inst, &sata_device);
19464 	if (rval != SATA_SUCCESS) {
19465 		/* Something went wrong? Fail the port */
19466 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19467 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19468 		    cport_mutex);
19469 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19470 		    "SATA port %d probing failed",
19471 		    saddr->cport));
19472 		/*
19473 		 * We may want to release device info structure, but
19474 		 * it is not necessary.
19475 		 */
19476 		return;
19477 	} else {
19478 		/* port probed successfully */
19479 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19480 	}
19481 	/*
19482 	 * Check if a device is still attached. For sanity, check also
19483 	 * link status - if no link, there is no device.
19484 	 */
19485 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19486 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19487 	    SATA_DTYPE_NONE) {
19488 		/*
19489 		 * Device is still attached - ignore detach event.
19490 		 */
19491 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19492 		    cport_mutex);
19493 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19494 		    "Ignoring detach - device still attached to port %d",
19495 		    sata_device.satadev_addr.cport);
19496 		return;
19497 	}
19498 	/*
19499 	 * We need to detach and release device info structure here
19500 	 */
19501 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19502 		/*
19503 		 * A port-multiplier is removed.
19504 		 *
19505 		 * Calling sata_process_pmdevice_detached() does not work
19506 		 * here. The port multiplier is gone, so we cannot probe
19507 		 * sub-port any more and all pmult-related data structure must
19508 		 * be de-allocated immediately. Following structure of every
19509 		 * implemented sub-port behind the pmult are required to
19510 		 * released.
19511 		 *
19512 		 *   - attachment point
19513 		 *   - target node
19514 		 *   - sata_drive_info
19515 		 *   - sata_pmport_info
19516 		 */
19517 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19518 		    cport); npmport ++) {
19519 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19520 			    sata_hba_inst,
19521 			    "Detaching target node at port %d:%d",
19522 			    cport, npmport);
19523 
19524 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19525 
19526 			/* Remove attachment point. */
19527 			name[0] = '\0';
19528 			(void) sprintf(name, "%d.%d", cport, npmport);
19529 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19530 			sata_log(sata_hba_inst, CE_NOTE,
19531 			    "Remove attachment point of port %d:%d",
19532 			    cport, npmport);
19533 
19534 			/* Remove target node */
19535 			pmport_addr.cport = cport;
19536 			pmport_addr.pmport = (uint8_t)npmport;
19537 			pmport_addr.qual = SATA_ADDR_PMPORT;
19538 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19539 
19540 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19541 
19542 			/* Release sata_pmport_info & sata_drive_info. */
19543 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19544 			    cport, npmport);
19545 			ASSERT(pmportinfo != NULL);
19546 
19547 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19548 			if (sdevinfo != NULL) {
19549 				(void) kmem_free((void *) sdevinfo,
19550 				    sizeof (sata_drive_info_t));
19551 			}
19552 
19553 			/* Release sata_pmport_info at last */
19554 			(void) kmem_free((void *) pmportinfo,
19555 			    sizeof (sata_pmport_info_t));
19556 		}
19557 
19558 		/* Finally, release sata_pmult_info */
19559 		(void) kmem_free((void *)
19560 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19561 		    sizeof (sata_pmult_info_t));
19562 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19563 
19564 		sata_log(sata_hba_inst, CE_WARN,
19565 		    "SATA port-multiplier detached at port %d", cport);
19566 
19567 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19568 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19569 		    saddr->cport)->cport_mutex);
19570 	} else {
19571 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19572 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19573 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19574 			(void) kmem_free((void *)sdevinfo,
19575 			    sizeof (sata_drive_info_t));
19576 		}
19577 		sata_log(sata_hba_inst, CE_WARN,
19578 		    "SATA device detached at port %d", cport);
19579 
19580 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19581 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19582 		    saddr->cport)->cport_mutex);
19583 
19584 		/*
19585 		 * Try to offline a device and remove target node
19586 		 * if it still exists
19587 		 */
19588 		sata_remove_target_node(sata_hba_inst, saddr);
19589 	}
19590 
19591 
19592 	/*
19593 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19594 	 * with the hint: SE_HINT_REMOVE
19595 	 */
19596 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19597 }
19598 
19599 /*
19600  * Port Multiplier Port Device Deattached Event processing.
19601  *
19602  * NOTE: No Mutex should be hold.
19603  */
19604 static void
19605 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19606     sata_address_t *saddr)
19607 {
19608 	sata_pmport_info_t *pmportinfo;
19609 	sata_drive_info_t *sdevinfo;
19610 	sata_device_t sata_device;
19611 	int rval;
19612 	uint8_t cport, pmport;
19613 
19614 	cport = saddr->cport;
19615 	pmport = saddr->pmport;
19616 
19617 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19618 	    "Processing port %d:%d device detached",
19619 	    cport, pmport);
19620 
19621 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19622 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19623 
19624 	/* Clear event flag */
19625 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19626 
19627 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19628 	if ((pmportinfo->pmport_state &
19629 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19630 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19631 		return;
19632 	}
19633 	/* For sanity, re-probe the port */
19634 	sata_device.satadev_rev = SATA_DEVICE_REV;
19635 	sata_device.satadev_addr = *saddr;
19636 
19637 	/*
19638 	 * We have to exit mutex, because the HBA probe port function may
19639 	 * block on its own mutex.
19640 	 */
19641 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19642 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19643 	    (SATA_DIP(sata_hba_inst), &sata_device);
19644 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19645 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19646 	if (rval != SATA_SUCCESS) {
19647 		/* Something went wrong? Fail the port */
19648 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19649 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19650 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19651 		    "SATA port %d:%d probing failed",
19652 		    saddr->pmport));
19653 		/*
19654 		 * We may want to release device info structure, but
19655 		 * it is not necessary.
19656 		 */
19657 		return;
19658 	} else {
19659 		/* port probed successfully */
19660 		pmportinfo->pmport_state |=
19661 		    SATA_STATE_PROBED | SATA_STATE_READY;
19662 	}
19663 	/*
19664 	 * Check if a device is still attached. For sanity, check also
19665 	 * link status - if no link, there is no device.
19666 	 */
19667 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19668 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19669 	    SATA_DTYPE_NONE) {
19670 		/*
19671 		 * Device is still attached - ignore detach event.
19672 		 */
19673 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19674 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19675 		    "Ignoring detach - device still attached to port %d",
19676 		    sata_device.satadev_addr.pmport);
19677 		return;
19678 	}
19679 	/*
19680 	 * We need to detach and release device info structure here
19681 	 */
19682 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19683 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19684 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19685 		(void) kmem_free((void *)sdevinfo,
19686 		    sizeof (sata_drive_info_t));
19687 	}
19688 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19689 	/*
19690 	 * Device cannot be reached anymore, even if the target node may be
19691 	 * still present.
19692 	 */
19693 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19694 
19695 	/*
19696 	 * Try to offline a device and remove target node if it still exists
19697 	 */
19698 	sata_remove_target_node(sata_hba_inst, saddr);
19699 
19700 	/*
19701 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19702 	 * with the hint: SE_HINT_REMOVE
19703 	 */
19704 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19705 }
19706 
19707 
19708 /*
19709  * Device Attached Event processing.
19710  * Port state is checked to verify that a device is really attached. If so,
19711  * the device info structure is created and attached to the SATA port info
19712  * structure.
19713  *
19714  * If attached device cannot be identified or set-up, the retry for the
19715  * attach processing is set-up. Subsequent daemon run would try again to
19716  * identify the device, until the time limit is reached
19717  * (SATA_DEV_IDENTIFY_TIMEOUT).
19718  *
19719  * This function cannot be called in interrupt context (it may sleep).
19720  *
19721  * NOTE: Port multiplier ports events are handled by
19722  * sata_process_pmdevice_attached()
19723  */
19724 static void
19725 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19726     sata_address_t *saddr)
19727 {
19728 	sata_cport_info_t *cportinfo = NULL;
19729 	sata_drive_info_t *sdevinfo = NULL;
19730 	sata_pmult_info_t *pmultinfo = NULL;
19731 	sata_pmport_info_t *pmportinfo = NULL;
19732 	sata_device_t sata_device;
19733 	dev_info_t *tdip;
19734 	uint32_t event_flags = 0, pmult_event_flags = 0;
19735 	int rval;
19736 	int npmport;
19737 
19738 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19739 	    "Processing port %d device attached", saddr->cport);
19740 
19741 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19742 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19743 
19744 	/* Clear attach event flag first */
19745 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19746 
19747 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19748 	if ((cportinfo->cport_state &
19749 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19750 		cportinfo->cport_dev_attach_time = 0;
19751 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19752 		    cport_mutex);
19753 		return;
19754 	}
19755 
19756 	/*
19757 	 * If the sata_drive_info structure is found attached to the port info,
19758 	 * despite the fact the device was removed and now it is re-attached,
19759 	 * the old drive info structure was not removed.
19760 	 * Arbitrarily release device info structure.
19761 	 */
19762 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19763 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19764 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19765 		(void) kmem_free((void *)sdevinfo,
19766 		    sizeof (sata_drive_info_t));
19767 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19768 		    "Arbitrarily detaching old device info.", NULL);
19769 	}
19770 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19771 
19772 	/* For sanity, re-probe the port */
19773 	sata_device.satadev_rev = SATA_DEVICE_REV;
19774 	sata_device.satadev_addr = *saddr;
19775 
19776 	/*
19777 	 * We have to exit mutex, because the HBA probe port function may
19778 	 * block on its own mutex.
19779 	 */
19780 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19781 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19782 	    (SATA_DIP(sata_hba_inst), &sata_device);
19783 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19784 	sata_update_port_info(sata_hba_inst, &sata_device);
19785 	if (rval != SATA_SUCCESS) {
19786 		/* Something went wrong? Fail the port */
19787 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19788 		cportinfo->cport_dev_attach_time = 0;
19789 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19790 		    cport_mutex);
19791 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19792 		    "SATA port %d probing failed",
19793 		    saddr->cport));
19794 		return;
19795 	} else {
19796 		/* port probed successfully */
19797 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19798 	}
19799 	/*
19800 	 * Check if a device is still attached. For sanity, check also
19801 	 * link status - if no link, there is no device.
19802 	 */
19803 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19804 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19805 	    SATA_DTYPE_NONE) {
19806 		/*
19807 		 * No device - ignore attach event.
19808 		 */
19809 		cportinfo->cport_dev_attach_time = 0;
19810 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19811 		    cport_mutex);
19812 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19813 		    "Ignoring attach - no device connected to port %d",
19814 		    sata_device.satadev_addr.cport);
19815 		return;
19816 	}
19817 
19818 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19819 	/*
19820 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19821 	 * with the hint: SE_HINT_INSERT
19822 	 */
19823 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19824 
19825 	/*
19826 	 * Port reprobing will take care of the creation of the device
19827 	 * info structure and determination of the device type.
19828 	 */
19829 	sata_device.satadev_addr = *saddr;
19830 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19831 	    SATA_DEV_IDENTIFY_NORETRY);
19832 
19833 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19834 	    cport_mutex);
19835 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19836 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19837 		/* Some device is attached to the port */
19838 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19839 			/*
19840 			 * A device was not successfully attached.
19841 			 * Track retry time for device identification.
19842 			 */
19843 			if (cportinfo->cport_dev_attach_time != 0) {
19844 				clock_t cur_time = ddi_get_lbolt();
19845 				/*
19846 				 * If the retry time limit was not exceeded,
19847 				 * reinstate attach event.
19848 				 */
19849 				if ((cur_time -
19850 				    cportinfo->cport_dev_attach_time) <
19851 				    drv_usectohz(
19852 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19853 					/* OK, restore attach event */
19854 					cportinfo->cport_event_flags |=
19855 					    SATA_EVNT_DEVICE_ATTACHED;
19856 				} else {
19857 					/* Timeout - cannot identify device */
19858 					cportinfo->cport_dev_attach_time = 0;
19859 					sata_log(sata_hba_inst,
19860 					    CE_WARN,
19861 					    "Could not identify SATA device "
19862 					    "at port %d",
19863 					    saddr->cport);
19864 				}
19865 			} else {
19866 				/*
19867 				 * Start tracking time for device
19868 				 * identification.
19869 				 * Save current time (lbolt value).
19870 				 */
19871 				cportinfo->cport_dev_attach_time =
19872 				    ddi_get_lbolt();
19873 				/* Restore attach event */
19874 				cportinfo->cport_event_flags |=
19875 				    SATA_EVNT_DEVICE_ATTACHED;
19876 			}
19877 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19878 			cportinfo->cport_dev_attach_time = 0;
19879 			sata_log(sata_hba_inst, CE_NOTE,
19880 			    "SATA port-multiplier detected at port %d",
19881 			    saddr->cport);
19882 
19883 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19884 				/* Log the info of new port multiplier */
19885 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19886 				    saddr->cport)->cport_mutex);
19887 				sata_show_pmult_info(sata_hba_inst,
19888 				    &sata_device);
19889 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19890 				    saddr->cport)->cport_mutex);
19891 			}
19892 
19893 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19894 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19895 			for (npmport = 0; npmport <
19896 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19897 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19898 				    saddr->cport, npmport);
19899 				ASSERT(pmportinfo != NULL);
19900 
19901 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19902 				    saddr->cport)->cport_mutex);
19903 				mutex_enter(&pmportinfo->pmport_mutex);
19904 				/* Marked all pmports with link events. */
19905 				pmportinfo->pmport_event_flags =
19906 				    SATA_EVNT_LINK_ESTABLISHED;
19907 				pmult_event_flags |=
19908 				    pmportinfo->pmport_event_flags;
19909 				mutex_exit(&pmportinfo->pmport_mutex);
19910 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19911 				    saddr->cport)->cport_mutex);
19912 			}
19913 			/* Auto-online is not available for PMult now. */
19914 
19915 		} else {
19916 			/*
19917 			 * If device was successfully attached, the subsequent
19918 			 * action depends on a state of the
19919 			 * sata_auto_online variable. If it is set to zero.
19920 			 * an explicit 'configure' command will be needed to
19921 			 * configure it. If its value is non-zero, we will
19922 			 * attempt to online (configure) the device.
19923 			 * First, log the message indicating that a device
19924 			 * was attached.
19925 			 */
19926 			cportinfo->cport_dev_attach_time = 0;
19927 			sata_log(sata_hba_inst, CE_WARN,
19928 			    "SATA device detected at port %d", saddr->cport);
19929 
19930 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19931 				sata_drive_info_t new_sdinfo;
19932 
19933 				/* Log device info data */
19934 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19935 				    cportinfo));
19936 				sata_show_drive_info(sata_hba_inst,
19937 				    &new_sdinfo);
19938 			}
19939 
19940 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19941 			    saddr->cport)->cport_mutex);
19942 
19943 			/*
19944 			 * Make sure that there is no target node for that
19945 			 * device. If so, release it. It should not happen,
19946 			 * unless we had problem removing the node when
19947 			 * device was detached.
19948 			 */
19949 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19950 			    saddr->cport, saddr->pmport);
19951 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19952 			    saddr->cport)->cport_mutex);
19953 			if (tdip != NULL) {
19954 
19955 #ifdef SATA_DEBUG
19956 				if ((cportinfo->cport_event_flags &
19957 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19958 					sata_log(sata_hba_inst, CE_WARN,
19959 					    "sata_process_device_attached: "
19960 					    "old device target node exists!");
19961 #endif
19962 				/*
19963 				 * target node exists - try to unconfigure
19964 				 * device and remove the node.
19965 				 */
19966 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19967 				    saddr->cport)->cport_mutex);
19968 				rval = ndi_devi_offline(tdip,
19969 				    NDI_DEVI_REMOVE);
19970 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19971 				    saddr->cport)->cport_mutex);
19972 
19973 				if (rval == NDI_SUCCESS) {
19974 					cportinfo->cport_event_flags &=
19975 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19976 					cportinfo->cport_tgtnode_clean = B_TRUE;
19977 				} else {
19978 					/*
19979 					 * PROBLEM - the target node remained
19980 					 * and it belongs to a previously
19981 					 * attached device.
19982 					 * This happens when the file was open
19983 					 * or the node was waiting for
19984 					 * resources at the time the
19985 					 * associated device was removed.
19986 					 * Instruct event daemon to retry the
19987 					 * cleanup later.
19988 					 */
19989 					sata_log(sata_hba_inst,
19990 					    CE_WARN,
19991 					    "Application(s) accessing "
19992 					    "previously attached SATA "
19993 					    "device have to release "
19994 					    "it before newly inserted "
19995 					    "device can be made accessible.",
19996 					    saddr->cport);
19997 					cportinfo->cport_event_flags |=
19998 					    SATA_EVNT_TARGET_NODE_CLEANUP;
19999 					cportinfo->cport_tgtnode_clean =
20000 					    B_FALSE;
20001 				}
20002 			}
20003 			if (sata_auto_online != 0) {
20004 				cportinfo->cport_event_flags |=
20005 				    SATA_EVNT_AUTOONLINE_DEVICE;
20006 			}
20007 
20008 		}
20009 	} else {
20010 		cportinfo->cport_dev_attach_time = 0;
20011 	}
20012 
20013 	event_flags = cportinfo->cport_event_flags;
20014 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20015 	if (event_flags != 0 || pmult_event_flags != 0) {
20016 		mutex_enter(&sata_hba_inst->satahba_mutex);
20017 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20018 		mutex_exit(&sata_hba_inst->satahba_mutex);
20019 		mutex_enter(&sata_mutex);
20020 		sata_event_pending |= SATA_EVNT_MAIN;
20021 		mutex_exit(&sata_mutex);
20022 	}
20023 }
20024 
20025 /*
20026  * Port Multiplier Port Device Attached Event processing.
20027  *
20028  * NOTE: No Mutex should be hold.
20029  */
20030 static void
20031 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20032     sata_address_t *saddr)
20033 {
20034 	sata_pmport_info_t *pmportinfo;
20035 	sata_drive_info_t *sdinfo;
20036 	sata_device_t sata_device;
20037 	dev_info_t *tdip;
20038 	uint32_t event_flags;
20039 	uint8_t cport = saddr->cport;
20040 	uint8_t pmport = saddr->pmport;
20041 	int rval;
20042 
20043 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20044 	    "Processing port %d:%d device attached", cport, pmport);
20045 
20046 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20047 
20048 	mutex_enter(&pmportinfo->pmport_mutex);
20049 
20050 	/* Clear attach event flag first */
20051 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20052 
20053 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20054 	if ((pmportinfo->pmport_state &
20055 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20056 		pmportinfo->pmport_dev_attach_time = 0;
20057 		mutex_exit(&pmportinfo->pmport_mutex);
20058 		return;
20059 	}
20060 
20061 	/*
20062 	 * If the sata_drive_info structure is found attached to the port info,
20063 	 * despite the fact the device was removed and now it is re-attached,
20064 	 * the old drive info structure was not removed.
20065 	 * Arbitrarily release device info structure.
20066 	 */
20067 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20068 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20069 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20070 		(void) kmem_free((void *)sdinfo,
20071 		    sizeof (sata_drive_info_t));
20072 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20073 		    "Arbitrarily detaching old device info.", NULL);
20074 	}
20075 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20076 
20077 	/* For sanity, re-probe the port */
20078 	sata_device.satadev_rev = SATA_DEVICE_REV;
20079 	sata_device.satadev_addr = *saddr;
20080 
20081 	/*
20082 	 * We have to exit mutex, because the HBA probe port function may
20083 	 * block on its own mutex.
20084 	 */
20085 	mutex_exit(&pmportinfo->pmport_mutex);
20086 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20087 	    (SATA_DIP(sata_hba_inst), &sata_device);
20088 	mutex_enter(&pmportinfo->pmport_mutex);
20089 
20090 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20091 	if (rval != SATA_SUCCESS) {
20092 		/* Something went wrong? Fail the port */
20093 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20094 		pmportinfo->pmport_dev_attach_time = 0;
20095 		mutex_exit(&pmportinfo->pmport_mutex);
20096 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20097 		    "SATA port %d:%d probing failed", cport, pmport));
20098 		return;
20099 	} else {
20100 		/* pmport probed successfully */
20101 		pmportinfo->pmport_state |=
20102 		    SATA_STATE_PROBED | SATA_STATE_READY;
20103 	}
20104 	/*
20105 	 * Check if a device is still attached. For sanity, check also
20106 	 * link status - if no link, there is no device.
20107 	 */
20108 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20109 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20110 	    SATA_DTYPE_NONE) {
20111 		/*
20112 		 * No device - ignore attach event.
20113 		 */
20114 		pmportinfo->pmport_dev_attach_time = 0;
20115 		mutex_exit(&pmportinfo->pmport_mutex);
20116 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20117 		    "Ignoring attach - no device connected to port %d:%d",
20118 		    cport, pmport);
20119 		return;
20120 	}
20121 
20122 	mutex_exit(&pmportinfo->pmport_mutex);
20123 	/*
20124 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20125 	 * with the hint: SE_HINT_INSERT
20126 	 */
20127 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20128 
20129 	/*
20130 	 * Port reprobing will take care of the creation of the device
20131 	 * info structure and determination of the device type.
20132 	 */
20133 	sata_device.satadev_addr = *saddr;
20134 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20135 	    SATA_DEV_IDENTIFY_NORETRY);
20136 
20137 	mutex_enter(&pmportinfo->pmport_mutex);
20138 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20139 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20140 		/* Some device is attached to the port */
20141 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20142 			/*
20143 			 * A device was not successfully attached.
20144 			 * Track retry time for device identification.
20145 			 */
20146 			if (pmportinfo->pmport_dev_attach_time != 0) {
20147 				clock_t cur_time = ddi_get_lbolt();
20148 				/*
20149 				 * If the retry time limit was not exceeded,
20150 				 * reinstate attach event.
20151 				 */
20152 				if ((cur_time -
20153 				    pmportinfo->pmport_dev_attach_time) <
20154 				    drv_usectohz(
20155 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20156 					/* OK, restore attach event */
20157 					pmportinfo->pmport_event_flags |=
20158 					    SATA_EVNT_DEVICE_ATTACHED;
20159 				} else {
20160 					/* Timeout - cannot identify device */
20161 					pmportinfo->pmport_dev_attach_time = 0;
20162 					sata_log(sata_hba_inst, CE_WARN,
20163 					    "Could not identify SATA device "
20164 					    "at port %d:%d",
20165 					    cport, pmport);
20166 				}
20167 			} else {
20168 				/*
20169 				 * Start tracking time for device
20170 				 * identification.
20171 				 * Save current time (lbolt value).
20172 				 */
20173 				pmportinfo->pmport_dev_attach_time =
20174 				    ddi_get_lbolt();
20175 				/* Restore attach event */
20176 				pmportinfo->pmport_event_flags |=
20177 				    SATA_EVNT_DEVICE_ATTACHED;
20178 			}
20179 		} else {
20180 			/*
20181 			 * If device was successfully attached, the subsequent
20182 			 * action depends on a state of the
20183 			 * sata_auto_online variable. If it is set to zero.
20184 			 * an explicit 'configure' command will be needed to
20185 			 * configure it. If its value is non-zero, we will
20186 			 * attempt to online (configure) the device.
20187 			 * First, log the message indicating that a device
20188 			 * was attached.
20189 			 */
20190 			pmportinfo->pmport_dev_attach_time = 0;
20191 			sata_log(sata_hba_inst, CE_WARN,
20192 			    "SATA device detected at port %d:%d",
20193 			    cport, pmport);
20194 
20195 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20196 				sata_drive_info_t new_sdinfo;
20197 
20198 				/* Log device info data */
20199 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20200 				    pmportinfo));
20201 				sata_show_drive_info(sata_hba_inst,
20202 				    &new_sdinfo);
20203 			}
20204 
20205 			mutex_exit(&pmportinfo->pmport_mutex);
20206 
20207 			/*
20208 			 * Make sure that there is no target node for that
20209 			 * device. If so, release it. It should not happen,
20210 			 * unless we had problem removing the node when
20211 			 * device was detached.
20212 			 */
20213 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20214 			    saddr->cport, saddr->pmport);
20215 			mutex_enter(&pmportinfo->pmport_mutex);
20216 			if (tdip != NULL) {
20217 
20218 #ifdef SATA_DEBUG
20219 				if ((pmportinfo->pmport_event_flags &
20220 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20221 					sata_log(sata_hba_inst, CE_WARN,
20222 					    "sata_process_device_attached: "
20223 					    "old device target node exists!");
20224 #endif
20225 				/*
20226 				 * target node exists - try to unconfigure
20227 				 * device and remove the node.
20228 				 */
20229 				mutex_exit(&pmportinfo->pmport_mutex);
20230 				rval = ndi_devi_offline(tdip,
20231 				    NDI_DEVI_REMOVE);
20232 				mutex_enter(&pmportinfo->pmport_mutex);
20233 
20234 				if (rval == NDI_SUCCESS) {
20235 					pmportinfo->pmport_event_flags &=
20236 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20237 					pmportinfo->pmport_tgtnode_clean =
20238 					    B_TRUE;
20239 				} else {
20240 					/*
20241 					 * PROBLEM - the target node remained
20242 					 * and it belongs to a previously
20243 					 * attached device.
20244 					 * This happens when the file was open
20245 					 * or the node was waiting for
20246 					 * resources at the time the
20247 					 * associated device was removed.
20248 					 * Instruct event daemon to retry the
20249 					 * cleanup later.
20250 					 */
20251 					sata_log(sata_hba_inst,
20252 					    CE_WARN,
20253 					    "Application(s) accessing "
20254 					    "previously attached SATA "
20255 					    "device have to release "
20256 					    "it before newly inserted "
20257 					    "device can be made accessible."
20258 					    "at port %d:%d",
20259 					    cport, pmport);
20260 					pmportinfo->pmport_event_flags |=
20261 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20262 					pmportinfo->pmport_tgtnode_clean =
20263 					    B_FALSE;
20264 				}
20265 			}
20266 			if (sata_auto_online != 0) {
20267 				pmportinfo->pmport_event_flags |=
20268 				    SATA_EVNT_AUTOONLINE_DEVICE;
20269 			}
20270 
20271 		}
20272 	} else {
20273 		pmportinfo->pmport_dev_attach_time = 0;
20274 	}
20275 
20276 	event_flags = pmportinfo->pmport_event_flags;
20277 	mutex_exit(&pmportinfo->pmport_mutex);
20278 	if (event_flags != 0) {
20279 		mutex_enter(&sata_hba_inst->satahba_mutex);
20280 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20281 		mutex_exit(&sata_hba_inst->satahba_mutex);
20282 		mutex_enter(&sata_mutex);
20283 		sata_event_pending |= SATA_EVNT_MAIN;
20284 		mutex_exit(&sata_mutex);
20285 	}
20286 
20287 	/* clear the reset_in_progress events */
20288 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20289 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20290 			/* must clear flags on cport */
20291 			sata_pmult_info_t *pminfo =
20292 			    SATA_PMULT_INFO(sata_hba_inst,
20293 			    saddr->cport);
20294 			pminfo->pmult_event_flags |=
20295 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20296 		}
20297 	}
20298 }
20299 
20300 /*
20301  * Device Target Node Cleanup Event processing.
20302  * If the target node associated with a sata port device is in
20303  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20304  * If the target node cannot be removed, the event flag is left intact,
20305  * so that event daemon may re-run this function later.
20306  *
20307  * This function cannot be called in interrupt context (it may sleep).
20308  *
20309  * NOTE: Processes cport events only, not port multiplier ports.
20310  */
20311 static void
20312 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20313     sata_address_t *saddr)
20314 {
20315 	sata_cport_info_t *cportinfo;
20316 	dev_info_t *tdip;
20317 
20318 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20319 	    "Processing port %d device target node cleanup", saddr->cport);
20320 
20321 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20322 
20323 	/*
20324 	 * Check if there is target node for that device and it is in the
20325 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20326 	 */
20327 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20328 	    saddr->pmport);
20329 	if (tdip != NULL) {
20330 		/*
20331 		 * target node exists - check if it is target node of
20332 		 * a removed device.
20333 		 */
20334 		if (sata_check_device_removed(tdip) == B_TRUE) {
20335 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20336 			    "sata_process_target_node_cleanup: "
20337 			    "old device target node exists!", NULL);
20338 			/*
20339 			 * Unconfigure and remove the target node
20340 			 */
20341 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20342 			    NDI_SUCCESS) {
20343 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20344 				    saddr->cport)->cport_mutex);
20345 				cportinfo->cport_event_flags &=
20346 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20347 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20348 				    saddr->cport)->cport_mutex);
20349 				return;
20350 			}
20351 			/*
20352 			 * Event daemon will retry the cleanup later.
20353 			 */
20354 			mutex_enter(&sata_hba_inst->satahba_mutex);
20355 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20356 			mutex_exit(&sata_hba_inst->satahba_mutex);
20357 			mutex_enter(&sata_mutex);
20358 			sata_event_pending |= SATA_EVNT_MAIN;
20359 			mutex_exit(&sata_mutex);
20360 		}
20361 	} else {
20362 		if (saddr->qual == SATA_ADDR_CPORT ||
20363 		    saddr->qual == SATA_ADDR_DCPORT) {
20364 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20365 			    saddr->cport)->cport_mutex);
20366 			cportinfo->cport_event_flags &=
20367 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20368 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20369 			    saddr->cport)->cport_mutex);
20370 		} else {
20371 			/* sanity check */
20372 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20373 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20374 			    saddr->cport) == NULL)
20375 				return;
20376 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20377 			    saddr->pmport) == NULL)
20378 				return;
20379 
20380 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20381 			    saddr->cport, saddr->pmport)->pmport_mutex);
20382 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20383 			    saddr->pmport)->pmport_event_flags &=
20384 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20385 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20386 			    saddr->cport, saddr->pmport)->pmport_mutex);
20387 		}
20388 	}
20389 }
20390 
20391 /*
20392  * Device AutoOnline Event processing.
20393  * If attached device is to be onlined, an attempt is made to online this
20394  * device, but only if there is no lingering (old) target node present.
20395  * If the device cannot be onlined, the event flag is left intact,
20396  * so that event daemon may re-run this function later.
20397  *
20398  * This function cannot be called in interrupt context (it may sleep).
20399  *
20400  * NOTE: Processes cport events only, not port multiplier ports.
20401  */
20402 static void
20403 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20404     sata_address_t *saddr)
20405 {
20406 	sata_cport_info_t *cportinfo;
20407 	sata_drive_info_t *sdinfo;
20408 	sata_device_t sata_device;
20409 	dev_info_t *tdip;
20410 
20411 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20412 	    "Processing port %d attached device auto-onlining", saddr->cport);
20413 
20414 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20415 
20416 	/*
20417 	 * Check if device is present and recognized. If not, reset event.
20418 	 */
20419 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20420 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20421 		/* Nothing to online */
20422 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20423 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20424 		    saddr->cport)->cport_mutex);
20425 		return;
20426 	}
20427 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20428 
20429 	/*
20430 	 * Check if there is target node for this device and if it is in the
20431 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20432 	 * the event for later processing.
20433 	 */
20434 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20435 	    saddr->pmport);
20436 	if (tdip != NULL) {
20437 		/*
20438 		 * target node exists - check if it is target node of
20439 		 * a removed device.
20440 		 */
20441 		if (sata_check_device_removed(tdip) == B_TRUE) {
20442 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20443 			    "sata_process_device_autoonline: "
20444 			    "old device target node exists!", NULL);
20445 			/*
20446 			 * Event daemon will retry device onlining later.
20447 			 */
20448 			mutex_enter(&sata_hba_inst->satahba_mutex);
20449 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20450 			mutex_exit(&sata_hba_inst->satahba_mutex);
20451 			mutex_enter(&sata_mutex);
20452 			sata_event_pending |= SATA_EVNT_MAIN;
20453 			mutex_exit(&sata_mutex);
20454 			return;
20455 		}
20456 		/*
20457 		 * If the target node is not in the 'removed" state, assume
20458 		 * that it belongs to this device. There is nothing more to do,
20459 		 * but reset the event.
20460 		 */
20461 	} else {
20462 
20463 		/*
20464 		 * Try to online the device
20465 		 * If there is any reset-related event, remove it. We are
20466 		 * configuring the device and no state restoring is needed.
20467 		 */
20468 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20469 		    saddr->cport)->cport_mutex);
20470 		sata_device.satadev_addr = *saddr;
20471 		if (saddr->qual == SATA_ADDR_CPORT)
20472 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20473 		else
20474 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20475 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20476 		if (sdinfo != NULL) {
20477 			if (sdinfo->satadrv_event_flags &
20478 			    (SATA_EVNT_DEVICE_RESET |
20479 			    SATA_EVNT_INPROC_DEVICE_RESET))
20480 				sdinfo->satadrv_event_flags = 0;
20481 			sdinfo->satadrv_event_flags |=
20482 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20483 
20484 			/* Need to create a new target node. */
20485 			cportinfo->cport_tgtnode_clean = B_TRUE;
20486 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20487 			    saddr->cport)->cport_mutex);
20488 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20489 			    sata_hba_inst, &sata_device.satadev_addr);
20490 			if (tdip == NULL) {
20491 				/*
20492 				 * Configure (onlining) failed.
20493 				 * We will NOT retry
20494 				 */
20495 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20496 				    "sata_process_device_autoonline: "
20497 				    "configuring SATA device at port %d failed",
20498 				    saddr->cport));
20499 			}
20500 		} else {
20501 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20502 			    saddr->cport)->cport_mutex);
20503 		}
20504 
20505 	}
20506 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20507 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20508 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20509 	    saddr->cport)->cport_mutex);
20510 }
20511 
20512 
20513 static void
20514 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20515     int hint)
20516 {
20517 	char ap[MAXPATHLEN];
20518 	nvlist_t *ev_attr_list = NULL;
20519 	int err;
20520 
20521 	/* Allocate and build sysevent attribute list */
20522 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20523 	if (err != 0) {
20524 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20525 		    "sata_gen_sysevent: "
20526 		    "cannot allocate memory for sysevent attributes\n"));
20527 		return;
20528 	}
20529 	/* Add hint attribute */
20530 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20531 	if (err != 0) {
20532 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20533 		    "sata_gen_sysevent: "
20534 		    "failed to add DR_HINT attr for sysevent"));
20535 		nvlist_free(ev_attr_list);
20536 		return;
20537 	}
20538 	/*
20539 	 * Add AP attribute.
20540 	 * Get controller pathname and convert it into AP pathname by adding
20541 	 * a target number.
20542 	 */
20543 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20544 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20545 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20546 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20547 
20548 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20549 	if (err != 0) {
20550 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20551 		    "sata_gen_sysevent: "
20552 		    "failed to add DR_AP_ID attr for sysevent"));
20553 		nvlist_free(ev_attr_list);
20554 		return;
20555 	}
20556 
20557 	/* Generate/log sysevent */
20558 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20559 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20560 	if (err != DDI_SUCCESS) {
20561 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20562 		    "sata_gen_sysevent: "
20563 		    "cannot log sysevent, err code %x\n", err));
20564 	}
20565 
20566 	nvlist_free(ev_attr_list);
20567 }
20568 
20569 
20570 
20571 
20572 /*
20573  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20574  */
20575 static void
20576 sata_set_device_removed(dev_info_t *tdip)
20577 {
20578 	int circ;
20579 
20580 	ASSERT(tdip != NULL);
20581 
20582 	ndi_devi_enter(tdip, &circ);
20583 	mutex_enter(&DEVI(tdip)->devi_lock);
20584 	DEVI_SET_DEVICE_REMOVED(tdip);
20585 	mutex_exit(&DEVI(tdip)->devi_lock);
20586 	ndi_devi_exit(tdip, circ);
20587 }
20588 
20589 
20590 /*
20591  * Set internal event instructing event daemon to try
20592  * to perform the target node cleanup.
20593  */
20594 static void
20595 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20596     sata_address_t *saddr)
20597 {
20598 	if (saddr->qual == SATA_ADDR_CPORT ||
20599 	    saddr->qual == SATA_ADDR_DCPORT) {
20600 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20601 		    saddr->cport)->cport_mutex);
20602 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20603 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20604 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20605 		    cport_tgtnode_clean = B_FALSE;
20606 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20607 		    saddr->cport)->cport_mutex);
20608 	} else {
20609 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20610 		    saddr->cport, saddr->pmport)->pmport_mutex);
20611 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20612 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20613 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20614 		    pmport_tgtnode_clean = B_FALSE;
20615 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20616 		    saddr->cport, saddr->pmport)->pmport_mutex);
20617 	}
20618 	mutex_enter(&sata_hba_inst->satahba_mutex);
20619 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20620 	mutex_exit(&sata_hba_inst->satahba_mutex);
20621 	mutex_enter(&sata_mutex);
20622 	sata_event_pending |= SATA_EVNT_MAIN;
20623 	mutex_exit(&sata_mutex);
20624 }
20625 
20626 
20627 /*
20628  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20629  * i.e. check if the target node state indicates that it belongs to a removed
20630  * device.
20631  *
20632  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20633  * B_FALSE otherwise.
20634  */
20635 static boolean_t
20636 sata_check_device_removed(dev_info_t *tdip)
20637 {
20638 	ASSERT(tdip != NULL);
20639 
20640 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20641 		return (B_TRUE);
20642 	else
20643 		return (B_FALSE);
20644 }
20645 
20646 
20647 /*
20648  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20649  */
20650 static boolean_t
20651 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20652 {
20653 	int fm_capability = ddi_fm_capable(dip);
20654 	ddi_fm_error_t de;
20655 
20656 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20657 		if (spx->txlt_buf_dma_handle != NULL) {
20658 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20659 			    DDI_FME_VERSION);
20660 			if (de.fme_status != DDI_SUCCESS)
20661 				return (B_TRUE);
20662 		}
20663 	}
20664 	return (B_FALSE);
20665 }
20666 
20667 
20668 /* ************************ FAULT INJECTTION **************************** */
20669 
20670 #ifdef SATA_INJECT_FAULTS
20671 
20672 static	uint32_t sata_fault_count = 0;
20673 static	uint32_t sata_fault_suspend_count = 0;
20674 
20675 /*
20676  * Inject sata pkt fault
20677  * It modifies returned values of the sata packet.
20678  * It returns immediately if:
20679  * pkt fault injection is not enabled (via sata_inject_fault,
20680  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20681  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20682  * pkt is not directed to specified fault controller/device
20683  * (sata_fault_ctrl_dev and sata_fault_device).
20684  * If fault controller is not specified, fault injection applies to all
20685  * controllers and devices.
20686  *
20687  * First argument is the pointer to the executed sata packet.
20688  * Second argument is a pointer to a value returned by the HBA tran_start
20689  * function.
20690  * Third argument specifies injected error. Injected sata packet faults
20691  * are the satapkt_reason values.
20692  * SATA_PKT_BUSY		-1	Not completed, busy
20693  * SATA_PKT_DEV_ERROR		1	Device reported error
20694  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20695  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20696  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20697  * SATA_PKT_ABORTED		5	Aborted by request
20698  * SATA_PKT_TIMEOUT		6	Operation timeut
20699  * SATA_PKT_RESET		7	Aborted by reset request
20700  *
20701  * Additional global variables affecting the execution:
20702  *
20703  * sata_inject_fault_count variable specifies number of times in row the
20704  * error is injected. Value of -1 specifies permanent fault, ie. every time
20705  * the fault injection point is reached, the fault is injected and a pause
20706  * between fault injection specified by sata_inject_fault_pause_count is
20707  * ignored). Fault injection routine decrements sata_inject_fault_count
20708  * (if greater than zero) until it reaches 0. No fault is injected when
20709  * sata_inject_fault_count is 0 (zero).
20710  *
20711  * sata_inject_fault_pause_count variable specifies number of times a fault
20712  * injection is bypassed (pause between fault injections).
20713  * If set to 0, a fault is injected only a number of times specified by
20714  * sata_inject_fault_count.
20715  *
20716  * The fault counts are static, so for periodic errors they have to be manually
20717  * reset to start repetition sequence from scratch.
20718  * If the original value returned by the HBA tran_start function is not
20719  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20720  * is injected (to avoid masking real problems);
20721  *
20722  * NOTE: In its current incarnation, this function should be invoked only for
20723  * commands executed in SYNCHRONOUS mode.
20724  */
20725 
20726 
20727 static void
20728 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20729 {
20730 
20731 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20732 		return;
20733 
20734 	if (sata_inject_fault_count == 0)
20735 		return;
20736 
20737 	if (fault == 0)
20738 		return;
20739 
20740 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20741 		return;
20742 
20743 	if (sata_fault_ctrl != NULL) {
20744 		sata_pkt_txlate_t *spx =
20745 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20746 
20747 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20748 		    spx->txlt_sata_hba_inst->satahba_dip)
20749 			return;
20750 
20751 		if (sata_fault_device.satadev_addr.cport !=
20752 		    spkt->satapkt_device.satadev_addr.cport ||
20753 		    sata_fault_device.satadev_addr.pmport !=
20754 		    spkt->satapkt_device.satadev_addr.pmport ||
20755 		    sata_fault_device.satadev_addr.qual !=
20756 		    spkt->satapkt_device.satadev_addr.qual)
20757 			return;
20758 	}
20759 
20760 	/* Modify pkt return parameters */
20761 	if (*rval != SATA_TRAN_ACCEPTED ||
20762 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20763 		sata_fault_count = 0;
20764 		sata_fault_suspend_count = 0;
20765 		return;
20766 	}
20767 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20768 		/* Pause in the injection */
20769 		sata_fault_suspend_count -= 1;
20770 		return;
20771 	}
20772 
20773 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20774 		/*
20775 		 * Init inject fault cycle. If fault count is set to -1,
20776 		 * it is a permanent fault.
20777 		 */
20778 		if (sata_inject_fault_count != -1) {
20779 			sata_fault_count = sata_inject_fault_count;
20780 			sata_fault_suspend_count =
20781 			    sata_inject_fault_pause_count;
20782 			if (sata_fault_suspend_count == 0)
20783 				sata_inject_fault_count = 0;
20784 		}
20785 	}
20786 
20787 	if (sata_fault_count != 0)
20788 		sata_fault_count -= 1;
20789 
20790 	switch (fault) {
20791 	case SATA_PKT_BUSY:
20792 		*rval = SATA_TRAN_BUSY;
20793 		spkt->satapkt_reason = SATA_PKT_BUSY;
20794 		break;
20795 
20796 	case SATA_PKT_QUEUE_FULL:
20797 		*rval = SATA_TRAN_QUEUE_FULL;
20798 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20799 		break;
20800 
20801 	case SATA_PKT_CMD_UNSUPPORTED:
20802 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20803 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20804 		break;
20805 
20806 	case SATA_PKT_PORT_ERROR:
20807 		/* This is "rejected" command */
20808 		*rval = SATA_TRAN_PORT_ERROR;
20809 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20810 		/* Additional error setup could be done here - port state */
20811 		break;
20812 
20813 	case SATA_PKT_DEV_ERROR:
20814 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20815 		/*
20816 		 * Additional error setup could be done here
20817 		 */
20818 		break;
20819 
20820 	case SATA_PKT_ABORTED:
20821 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20822 		break;
20823 
20824 	case SATA_PKT_TIMEOUT:
20825 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20826 		/* Additional error setup could be done here */
20827 		break;
20828 
20829 	case SATA_PKT_RESET:
20830 		spkt->satapkt_reason = SATA_PKT_RESET;
20831 		/*
20832 		 * Additional error setup could be done here - device reset
20833 		 */
20834 		break;
20835 
20836 	default:
20837 		break;
20838 	}
20839 }
20840 
20841 #endif
20842 
20843 /*
20844  * SATA Trace Ring Buffer
20845  * ----------------------
20846  *
20847  * Overview
20848  *
20849  * The SATA trace ring buffer is a ring buffer created and managed by
20850  * the SATA framework module that can be used by any module or driver
20851  * within the SATA framework to store debug messages.
20852  *
20853  * Ring Buffer Interfaces:
20854  *
20855  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20856  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20857  *
20858  *	Note that the sata_trace_debug() interface was created to give
20859  *	consumers the flexibilty of sending debug messages to ring buffer
20860  *	as variable arguments.  Consumers can send type va_list debug
20861  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20862  *	and sata_vtrace_debug() relationship is similar to that of
20863  *	cmn_err(9F) and vcmn_err(9F).
20864  *
20865  * Below is a diagram of the SATA trace ring buffer interfaces and
20866  * sample consumers:
20867  *
20868  * +---------------------------------+
20869  * |    o  o  SATA Framework Module  |
20870  * | o  SATA  o     +------------------+      +------------------+
20871  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20872  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20873  * | o        o     +------------------+   |  +------------------+
20874  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20875  * |                        |        |        +------------------+
20876  * |           +------------------+  |
20877  * |           |SATA Debug Message|  |
20878  * |           +------------------+  |
20879  * +---------------------------------+
20880  *
20881  * Supporting Routines:
20882  *
20883  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20884  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20885  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20886  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20887  *
20888  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20889  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20890  * /etc/system to desired size in unit of bytes.
20891  *
20892  * The individual debug message size in the ring buffer is restricted
20893  * to DMSG_BUF_SIZE.
20894  */
20895 void
20896 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20897 {
20898 	sata_trace_dmsg_t *dmsg;
20899 
20900 	if (sata_debug_rbuf == NULL) {
20901 		return;
20902 	}
20903 
20904 	/*
20905 	 * If max size of ring buffer is smaller than size
20906 	 * required for one debug message then just return
20907 	 * since we have no room for the debug message.
20908 	 */
20909 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20910 		return;
20911 	}
20912 
20913 	mutex_enter(&sata_debug_rbuf->lock);
20914 
20915 	/* alloc or reuse on ring buffer */
20916 	dmsg = sata_trace_dmsg_alloc();
20917 
20918 	if (dmsg == NULL) {
20919 		/* resource allocation failed */
20920 		mutex_exit(&sata_debug_rbuf->lock);
20921 		return;
20922 	}
20923 
20924 	dmsg->dip = dip;
20925 	gethrestime(&dmsg->timestamp);
20926 
20927 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20928 
20929 	mutex_exit(&sata_debug_rbuf->lock);
20930 }
20931 
20932 void
20933 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20934 {
20935 	va_list ap;
20936 
20937 	va_start(ap, fmt);
20938 	sata_vtrace_debug(dip, fmt, ap);
20939 	va_end(ap);
20940 }
20941 
20942 /*
20943  * This routine is used to manage debug messages
20944  * on ring buffer.
20945  */
20946 static sata_trace_dmsg_t *
20947 sata_trace_dmsg_alloc(void)
20948 {
20949 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20950 
20951 	if (sata_debug_rbuf->looped == TRUE) {
20952 		sata_debug_rbuf->dmsgp = dmsg->next;
20953 		return (sata_debug_rbuf->dmsgp);
20954 	}
20955 
20956 	/*
20957 	 * If we're looping for the first time,
20958 	 * connect the ring.
20959 	 */
20960 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
20961 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
20962 		dmsg->next = sata_debug_rbuf->dmsgh;
20963 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
20964 		sata_debug_rbuf->looped = TRUE;
20965 		return (sata_debug_rbuf->dmsgp);
20966 	}
20967 
20968 	/* If we've gotten this far then memory allocation is needed */
20969 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
20970 	if (dmsg_alloc == NULL) {
20971 		sata_debug_rbuf->allocfailed++;
20972 		return (dmsg_alloc);
20973 	} else {
20974 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
20975 	}
20976 
20977 	if (sata_debug_rbuf->dmsgp != NULL) {
20978 		dmsg->next = dmsg_alloc;
20979 		sata_debug_rbuf->dmsgp = dmsg->next;
20980 		return (sata_debug_rbuf->dmsgp);
20981 	} else {
20982 		/*
20983 		 * We should only be here if we're initializing
20984 		 * the ring buffer.
20985 		 */
20986 		if (sata_debug_rbuf->dmsgh == NULL) {
20987 			sata_debug_rbuf->dmsgh = dmsg_alloc;
20988 		} else {
20989 			/* Something is wrong */
20990 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
20991 			return (NULL);
20992 		}
20993 
20994 		sata_debug_rbuf->dmsgp = dmsg_alloc;
20995 		return (sata_debug_rbuf->dmsgp);
20996 	}
20997 }
20998 
20999 
21000 /*
21001  * Free all messages on debug ring buffer.
21002  */
21003 static void
21004 sata_trace_dmsg_free(void)
21005 {
21006 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21007 
21008 	while (dmsg != NULL) {
21009 		dmsg_next = dmsg->next;
21010 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21011 
21012 		/*
21013 		 * If we've looped around the ring than we're done.
21014 		 */
21015 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
21016 			break;
21017 		} else {
21018 			dmsg = dmsg_next;
21019 		}
21020 	}
21021 }
21022 
21023 
21024 /*
21025  * This function can block
21026  */
21027 static void
21028 sata_trace_rbuf_alloc(void)
21029 {
21030 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21031 
21032 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21033 
21034 	if (dmsg_ring_size > 0) {
21035 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21036 	}
21037 }
21038 
21039 
21040 static void
21041 sata_trace_rbuf_free(void)
21042 {
21043 	sata_trace_dmsg_free();
21044 	mutex_destroy(&sata_debug_rbuf->lock);
21045 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21046 }
21047 
21048 /*
21049  * If SATA_DEBUG is not defined then this routine is called instead
21050  * of sata_log() via the SATA_LOG_D macro.
21051  */
21052 static void
21053 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21054     const char *fmt, ...)
21055 {
21056 #ifndef __lock_lint
21057 	_NOTE(ARGUNUSED(level))
21058 #endif
21059 
21060 	dev_info_t *dip = NULL;
21061 	va_list ap;
21062 
21063 	if (sata_hba_inst != NULL) {
21064 		dip = SATA_DIP(sata_hba_inst);
21065 	}
21066 
21067 	va_start(ap, fmt);
21068 	sata_vtrace_debug(dip, fmt, ap);
21069 	va_end(ap);
21070 }
21071