xref: /illumos-gate/usr/src/uts/common/io/sata/impl/sata.c (revision 49b7860084dbba18bc00b29413d6182197f9fe93)
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 2015 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_split_model splits the model ID into vendor and product IDs.
1956  * It assumes that a vendor ID cannot be longer than 8 characters, and
1957  * that vendor and product ID are separated by a whitespace.
1958  */
1959 void
1960 sata_split_model(char *model, char **vendor, char **product)
1961 {
1962 	int i, modlen;
1963 	char *vid, *pid;
1964 
1965 	/*
1966 	 * remove whitespace at the end of model
1967 	 */
1968 	for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1969 		if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1970 			model[i] = '\0';
1971 		else
1972 			break;
1973 
1974 	/*
1975 	 * try to split model into into vid/pid
1976 	 */
1977 	modlen = strlen(model);
1978 	for (i = 0, pid = model; i < modlen; i++, pid++)
1979 		if ((*pid == ' ') || (*pid == '\t'))
1980 			break;
1981 
1982 	/*
1983 	 * only use vid if it is less than 8 chars (as in SCSI)
1984 	 */
1985 	if (i < modlen && i <= 8) {
1986 		vid = model;
1987 		/*
1988 		 * terminate vid, establish pid
1989 		 */
1990 		*pid++ = '\0';
1991 	} else {
1992 		/*
1993 		 * vid will stay "ATA     "
1994 		 */
1995 		vid = NULL;
1996 		/*
1997 		 * model is all pid
1998 		 */
1999 		pid = model;
2000 	}
2001 
2002 	*vendor = vid;
2003 	*product = pid;
2004 }
2005 
2006 /*
2007  * sata_name_child is for composing the name of the node
2008  * the format of the name is "target,0".
2009  */
2010 static int
2011 sata_name_child(dev_info_t *dip, char *name, int namelen)
2012 {
2013 	int target;
2014 
2015 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2016 	    DDI_PROP_DONTPASS, "target", -1);
2017 	if (target == -1)
2018 		return (DDI_FAILURE);
2019 	(void) snprintf(name, namelen, "%x,0", target);
2020 	return (DDI_SUCCESS);
2021 }
2022 
2023 
2024 
2025 /* ****************** SCSA required entry points *********************** */
2026 
2027 /*
2028  * Implementation of scsi tran_tgt_init.
2029  * sata_scsi_tgt_init() initializes scsi_device structure
2030  *
2031  * If successful, DDI_SUCCESS is returned.
2032  * DDI_FAILURE is returned if addressed device does not exist
2033  */
2034 
2035 static int
2036 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2037     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2038 {
2039 #ifndef __lock_lint
2040 	_NOTE(ARGUNUSED(hba_dip))
2041 	_NOTE(ARGUNUSED(tgt_dip))
2042 #endif
2043 	sata_device_t		sata_device;
2044 	sata_drive_info_t	*sdinfo;
2045 	struct sata_id		*sid;
2046 	sata_hba_inst_t		*sata_hba_inst;
2047 	char			model[SATA_ID_MODEL_LEN + 1];
2048 	char			fw[SATA_ID_FW_LEN + 1];
2049 	char			*vid, *pid;
2050 
2051 	/*
2052 	 * Fail tran_tgt_init for .conf stub node
2053 	 */
2054 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2055 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2056 		ddi_set_name_addr(tgt_dip, NULL);
2057 		return (DDI_FAILURE);
2058 	}
2059 
2060 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2061 
2062 	/* Validate scsi device address */
2063 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2064 	    &sata_device) != 0)
2065 		return (DDI_FAILURE);
2066 
2067 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2068 	    sata_device.satadev_addr.cport)));
2069 
2070 	/* sata_device now contains a valid sata address */
2071 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2072 	if (sdinfo == NULL) {
2073 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2074 		    sata_device.satadev_addr.cport)));
2075 		return (DDI_FAILURE);
2076 	}
2077 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2078 	    sata_device.satadev_addr.cport)));
2079 
2080 	/*
2081 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2082 	 * the target disks.
2083 	 *
2084 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2085 	 * if we need to create cmdk-style devid for all the disk devices
2086 	 * attached to this controller. This property may have been set
2087 	 * from HBA driver's .conf file or by the HBA driver in its
2088 	 * attach(9F) function.
2089 	 */
2090 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2091 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2092 	    "use-cmdk-devid-format", 0) == 1)) {
2093 		/* register a legacy devid for this target node */
2094 		sata_target_devid_register(tgt_dip, sdinfo);
2095 	}
2096 
2097 
2098 	/*
2099 	 * 'Identify Device Data' does not always fit in standard SCSI
2100 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2101 	 * of information.
2102 	 */
2103 	sid = &sdinfo->satadrv_id;
2104 #ifdef	_LITTLE_ENDIAN
2105 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2106 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2107 #else	/* _LITTLE_ENDIAN */
2108 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2109 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2110 #endif	/* _LITTLE_ENDIAN */
2111 	model[SATA_ID_MODEL_LEN] = 0;
2112 	fw[SATA_ID_FW_LEN] = 0;
2113 
2114 	sata_split_model(model, &vid, &pid);
2115 
2116 	if (vid)
2117 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2118 		    vid, strlen(vid));
2119 	if (pid)
2120 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2121 		    pid, strlen(pid));
2122 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2123 	    fw, strlen(fw));
2124 
2125 	return (DDI_SUCCESS);
2126 }
2127 
2128 /*
2129  * Implementation of scsi tran_tgt_probe.
2130  * Probe target, by calling default scsi routine scsi_hba_probe()
2131  */
2132 static int
2133 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2134 {
2135 	sata_hba_inst_t *sata_hba_inst =
2136 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2137 	int rval;
2138 	uint32_t pm_cap;
2139 
2140 	rval = scsi_hba_probe(sd, callback);
2141 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2142 	    SATA_CAP_LOG_SENSE;
2143 
2144 	if (rval == SCSIPROBE_EXISTS) {
2145 		/*
2146 		 * Set property "pm-capable" on the target device node, so that
2147 		 * the target driver will not try to fetch scsi cycle counters
2148 		 * before enabling device power-management.
2149 		 */
2150 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2151 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2152 			sata_log(sata_hba_inst, CE_WARN,
2153 			    "SATA device at port %d: "
2154 			    "will not be power-managed ",
2155 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2156 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2157 			    "failure updating pm-capable property"));
2158 		}
2159 	}
2160 	return (rval);
2161 }
2162 
2163 /*
2164  * Implementation of scsi tran_tgt_free.
2165  * Release all resources allocated for scsi_device
2166  */
2167 static void
2168 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2169     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2170 {
2171 #ifndef __lock_lint
2172 	_NOTE(ARGUNUSED(hba_dip))
2173 #endif
2174 	sata_device_t		sata_device;
2175 	sata_drive_info_t	*sdinfo;
2176 	sata_hba_inst_t		*sata_hba_inst;
2177 	ddi_devid_t		devid;
2178 
2179 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2180 
2181 	/* Validate scsi device address */
2182 	/*
2183 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2184 	 * was a device at this address, so even if the sata framework internal
2185 	 * resources were alredy released because a device was detached,
2186 	 * this function should be executed as long as its actions do
2187 	 * not require the internal sata view of a device and the address
2188 	 * refers to a valid sata address.
2189 	 * Validating the address here means that we do not trust SCSA...
2190 	 */
2191 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2192 	    &sata_device) == -1)
2193 		return;
2194 
2195 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2196 	    sata_device.satadev_addr.cport)));
2197 
2198 	/* sata_device now should contain a valid sata address */
2199 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2200 	if (sdinfo == NULL) {
2201 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2202 		    sata_device.satadev_addr.cport)));
2203 		return;
2204 	}
2205 	/*
2206 	 * We did not allocate any resources in sata_scsi_tgt_init()
2207 	 * other than few properties.
2208 	 * Free them.
2209 	 */
2210 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2211 	    sata_device.satadev_addr.cport)));
2212 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2213 
2214 	/*
2215 	 * If devid was previously created but not freed up from
2216 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2217 	 */
2218 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2219 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2220 	    "use-cmdk-devid-format", 0) == 1) &&
2221 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2222 		ddi_devid_unregister(tgt_dip);
2223 		ddi_devid_free(devid);
2224 	}
2225 }
2226 
2227 /*
2228  * Implementation of scsi tran_init_pkt
2229  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2230  *
2231  * It seems that we should always allocate pkt, even if the address is
2232  * for non-existing device - just use some default for dma_attr.
2233  * The reason is that there is no way to communicate this to a caller here.
2234  * Subsequent call to sata_scsi_start may fail appropriately.
2235  * Simply returning NULL does not seem to discourage a target driver...
2236  *
2237  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2238  */
2239 static struct scsi_pkt *
2240 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2241     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2242     int (*callback)(caddr_t), caddr_t arg)
2243 {
2244 	sata_hba_inst_t *sata_hba_inst =
2245 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2246 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2247 	sata_device_t sata_device;
2248 	sata_drive_info_t *sdinfo;
2249 	sata_pkt_txlate_t *spx;
2250 	ddi_dma_attr_t cur_dma_attr;
2251 	int rval;
2252 	boolean_t new_pkt = B_TRUE;
2253 
2254 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2255 
2256 	/*
2257 	 * We need to translate the address, even if it could be
2258 	 * a bogus one, for a non-existing device
2259 	 */
2260 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2261 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2262 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2263 	sata_device.satadev_rev = SATA_DEVICE_REV;
2264 
2265 	if (pkt == NULL) {
2266 		/*
2267 		 * Have to allocate a brand new scsi packet.
2268 		 * We need to operate with auto request sense enabled.
2269 		 */
2270 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2271 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2272 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2273 
2274 		if (pkt == NULL)
2275 			return (NULL);
2276 
2277 		/* Fill scsi packet structure */
2278 		pkt->pkt_comp		= (void (*)())NULL;
2279 		pkt->pkt_time		= 0;
2280 		pkt->pkt_resid		= 0;
2281 		pkt->pkt_statistics	= 0;
2282 		pkt->pkt_reason		= 0;
2283 
2284 		/*
2285 		 * pkt_hba_private will point to sata pkt txlate structure
2286 		 */
2287 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2288 		bzero(spx, sizeof (sata_pkt_txlate_t));
2289 
2290 		spx->txlt_scsi_pkt = pkt;
2291 		spx->txlt_sata_hba_inst = sata_hba_inst;
2292 
2293 		/* Allocate sata_pkt */
2294 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2295 		if (spx->txlt_sata_pkt == NULL) {
2296 			/* Could not allocate sata pkt */
2297 			scsi_hba_pkt_free(ap, pkt);
2298 			return (NULL);
2299 		}
2300 		/* Set sata address */
2301 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2302 		    sata_device.satadev_addr;
2303 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2304 		    sata_device.satadev_rev;
2305 
2306 		if ((bp == NULL) || (bp->b_bcount == 0))
2307 			return (pkt);
2308 
2309 		spx->txlt_total_residue = bp->b_bcount;
2310 	} else {
2311 		new_pkt = B_FALSE;
2312 		/*
2313 		 * Packet was preallocated/initialized by previous call
2314 		 */
2315 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2316 
2317 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2318 			return (pkt);
2319 		}
2320 
2321 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2322 	}
2323 
2324 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2325 
2326 	/*
2327 	 * We use an adjusted version of the dma_attr, to account
2328 	 * for device addressing limitations.
2329 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2330 	 * happen when a device is not yet configured.
2331 	 */
2332 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2333 	    sata_device.satadev_addr.cport)));
2334 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2335 	    &spx->txlt_sata_pkt->satapkt_device);
2336 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2337 	sata_adjust_dma_attr(sdinfo,
2338 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2339 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2340 	    sata_device.satadev_addr.cport)));
2341 	/*
2342 	 * Allocate necessary DMA resources for the packet's data buffer
2343 	 * NOTE:
2344 	 * In case of read/write commands, DMA resource allocation here is
2345 	 * based on the premise that the transfer length specified in
2346 	 * the read/write scsi cdb will match exactly DMA resources -
2347 	 * returning correct packet residue is crucial.
2348 	 */
2349 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2350 	    &cur_dma_attr)) != DDI_SUCCESS) {
2351 		/*
2352 		 * If a DMA allocation request fails with
2353 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2354 		 * bioerror(9F) with bp and an error code of EFAULT.
2355 		 * If a DMA allocation request fails with
2356 		 * DDI_DMA_TOOBIG, indicate the error by calling
2357 		 * bioerror(9F) with bp and an error code of EINVAL.
2358 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2359 		 * Request may be repeated later - there is no real error.
2360 		 */
2361 		switch (rval) {
2362 		case DDI_DMA_NORESOURCES:
2363 			bioerror(bp, 0);
2364 			break;
2365 		case DDI_DMA_NOMAPPING:
2366 		case DDI_DMA_BADATTR:
2367 			bioerror(bp, EFAULT);
2368 			break;
2369 		case DDI_DMA_TOOBIG:
2370 		default:
2371 			bioerror(bp, EINVAL);
2372 			break;
2373 		}
2374 		goto fail;
2375 	}
2376 
2377 	if (sata_check_for_dma_error(dip, spx)) {
2378 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2379 		bioerror(bp, EFAULT);
2380 		goto fail;
2381 	}
2382 
2383 success:
2384 	/* Set number of bytes that are not yet accounted for */
2385 	pkt->pkt_resid = spx->txlt_total_residue;
2386 	ASSERT(pkt->pkt_resid >= 0);
2387 
2388 	return (pkt);
2389 
2390 fail:
2391 	if (new_pkt == B_TRUE) {
2392 		/*
2393 		 * Since this is a new packet, we can clean-up
2394 		 * everything
2395 		 */
2396 		sata_scsi_destroy_pkt(ap, pkt);
2397 	} else {
2398 		/*
2399 		 * This is a re-used packet. It will be target driver's
2400 		 * responsibility to eventually destroy it (which
2401 		 * will free allocated resources).
2402 		 * Here, we just "complete" the request, leaving
2403 		 * allocated resources intact, so the request may
2404 		 * be retried.
2405 		 */
2406 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2407 		sata_pkt_free(spx);
2408 	}
2409 	return (NULL);
2410 }
2411 
2412 /*
2413  * Implementation of scsi tran_start.
2414  * Translate scsi cmd into sata operation and return status.
2415  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2416  * are supported.
2417  * For SATA hard disks, supported scsi commands:
2418  * SCMD_INQUIRY
2419  * SCMD_TEST_UNIT_READY
2420  * SCMD_START_STOP
2421  * SCMD_READ_CAPACITY
2422  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2423  * SCMD_REQUEST_SENSE
2424  * SCMD_LOG_SENSE_G1
2425  * SCMD_LOG_SELECT_G1
2426  * SCMD_MODE_SENSE	(specific pages)
2427  * SCMD_MODE_SENSE_G1	(specific pages)
2428  * SCMD_MODE_SELECT	(specific pages)
2429  * SCMD_MODE_SELECT_G1	(specific pages)
2430  * SCMD_SYNCHRONIZE_CACHE
2431  * SCMD_SYNCHRONIZE_CACHE_G1
2432  * SCMD_READ
2433  * SCMD_READ_G1
2434  * SCMD_READ_G4
2435  * SCMD_READ_G5
2436  * SCMD_WRITE
2437  * SCMD_WRITE_BUFFER
2438  * SCMD_WRITE_G1
2439  * SCMD_WRITE_G4
2440  * SCMD_WRITE_G5
2441  * SCMD_SEEK		(noop)
2442  * SCMD_SDIAG
2443  *
2444  * All other commands are rejected as unsupported.
2445  *
2446  * Returns:
2447  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2448  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2449  * a callback could be scheduled.
2450  * TRAN_BADPKT if cmd was directed to invalid address.
2451  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2452  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2453  * was removed and there was no callback specified in scsi pkt.
2454  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2455  * framework was busy performing some other operation(s).
2456  *
2457  */
2458 static int
2459 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2460 {
2461 	sata_hba_inst_t *sata_hba_inst =
2462 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2463 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2464 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2465 	sata_drive_info_t *sdinfo;
2466 	struct buf *bp;
2467 	uint8_t cport, pmport;
2468 	boolean_t dev_gone = B_FALSE;
2469 	int rval;
2470 
2471 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2472 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2473 
2474 	ASSERT(spx != NULL &&
2475 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2476 
2477 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2478 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2479 
2480 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2481 
2482 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2483 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2484 		if (sdinfo == NULL ||
2485 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2486 		    cport_tgtnode_clean == B_FALSE ||
2487 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2488 			dev_gone = B_TRUE;
2489 		}
2490 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2491 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2492 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2493 		    cport) == NULL) {
2494 			dev_gone = B_TRUE;
2495 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2496 		    pmport) == NULL) {
2497 			dev_gone = B_TRUE;
2498 		} else {
2499 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2500 			    cport, pmport)));
2501 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2502 			if (sdinfo == NULL ||
2503 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2504 			    pmport_tgtnode_clean == B_FALSE ||
2505 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2506 				dev_gone = B_TRUE;
2507 			}
2508 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2509 			    cport, pmport)));
2510 		}
2511 	}
2512 
2513 	if (dev_gone == B_TRUE) {
2514 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2515 		pkt->pkt_reason = CMD_DEV_GONE;
2516 		/*
2517 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2518 		 * only in callback function (for normal requests) and
2519 		 * in the dump code path.
2520 		 * So, if the callback is available, we need to do
2521 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2522 		 */
2523 		if (pkt->pkt_comp != NULL) {
2524 			/* scsi callback required */
2525 			if (servicing_interrupt()) {
2526 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2527 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2528 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2529 				    NULL) {
2530 					return (TRAN_BUSY);
2531 				}
2532 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2533 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2534 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2535 				/* Scheduling the callback failed */
2536 				return (TRAN_BUSY);
2537 			}
2538 			return (TRAN_ACCEPT);
2539 		}
2540 		/* No callback available */
2541 		return (TRAN_FATAL_ERROR);
2542 	}
2543 
2544 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2545 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2546 		rval = sata_txlt_atapi(spx);
2547 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2548 		    "sata_scsi_start atapi: rval %d\n", rval);
2549 		return (rval);
2550 	}
2551 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2552 
2553 	/*
2554 	 * Checking for power state, if it was on
2555 	 * STOPPED state, then the drive is not capable
2556 	 * of processing media access command.  And
2557 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2558 	 * in the function for different power state.
2559 	 */
2560 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2561 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2562 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2563 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2564 		    SD_SCSI_ASC_LU_NOT_READY));
2565 	}
2566 
2567 	/* ATA Disk commands processing starts here */
2568 
2569 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2570 
2571 	switch (pkt->pkt_cdbp[0]) {
2572 
2573 	case SCMD_INQUIRY:
2574 		/* Mapped to identify device */
2575 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2576 			bp_mapin(bp);
2577 		rval = sata_txlt_inquiry(spx);
2578 		break;
2579 
2580 	case SCMD_TEST_UNIT_READY:
2581 		/*
2582 		 * SAT "SATA to ATA Translation" doc specifies translation
2583 		 * to ATA CHECK POWER MODE.
2584 		 */
2585 		rval = sata_txlt_test_unit_ready(spx);
2586 		break;
2587 
2588 	case SCMD_START_STOP:
2589 		/* Mapping depends on the command */
2590 		rval = sata_txlt_start_stop_unit(spx);
2591 		break;
2592 
2593 	case SCMD_READ_CAPACITY:
2594 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2595 			bp_mapin(bp);
2596 		rval = sata_txlt_read_capacity(spx);
2597 		break;
2598 
2599 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2600 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2601 			bp_mapin(bp);
2602 		rval = sata_txlt_read_capacity16(spx);
2603 		break;
2604 
2605 	case SCMD_REQUEST_SENSE:
2606 		/*
2607 		 * Always No Sense, since we force ARQ
2608 		 */
2609 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2610 			bp_mapin(bp);
2611 		rval = sata_txlt_request_sense(spx);
2612 		break;
2613 
2614 	case SCMD_LOG_SENSE_G1:
2615 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2616 			bp_mapin(bp);
2617 		rval = sata_txlt_log_sense(spx);
2618 		break;
2619 
2620 	case SCMD_LOG_SELECT_G1:
2621 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2622 			bp_mapin(bp);
2623 		rval = sata_txlt_log_select(spx);
2624 		break;
2625 
2626 	case SCMD_MODE_SENSE:
2627 	case SCMD_MODE_SENSE_G1:
2628 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2629 			bp_mapin(bp);
2630 		rval = sata_txlt_mode_sense(spx);
2631 		break;
2632 
2633 
2634 	case SCMD_MODE_SELECT:
2635 	case SCMD_MODE_SELECT_G1:
2636 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2637 			bp_mapin(bp);
2638 		rval = sata_txlt_mode_select(spx);
2639 		break;
2640 
2641 	case SCMD_SYNCHRONIZE_CACHE:
2642 	case SCMD_SYNCHRONIZE_CACHE_G1:
2643 		rval = sata_txlt_synchronize_cache(spx);
2644 		break;
2645 
2646 	case SCMD_READ:
2647 	case SCMD_READ_G1:
2648 	case SCMD_READ_G4:
2649 	case SCMD_READ_G5:
2650 		rval = sata_txlt_read(spx);
2651 		break;
2652 	case SCMD_WRITE_BUFFER:
2653 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2654 			bp_mapin(bp);
2655 		rval = sata_txlt_write_buffer(spx);
2656 		break;
2657 
2658 	case SCMD_WRITE:
2659 	case SCMD_WRITE_G1:
2660 	case SCMD_WRITE_G4:
2661 	case SCMD_WRITE_G5:
2662 		rval = sata_txlt_write(spx);
2663 		break;
2664 
2665 	case SCMD_SEEK:
2666 		rval = sata_txlt_nodata_cmd_immediate(spx);
2667 		break;
2668 
2669 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2670 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2671 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2672 			bp_mapin(bp);
2673 		rval = sata_txlt_ata_pass_thru(spx);
2674 		break;
2675 
2676 		/* Other cases will be filed later */
2677 		/* postponed until phase 2 of the development */
2678 	case SPC3_CMD_UNMAP:
2679 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2680 			bp_mapin(bp);
2681 		rval = sata_txlt_unmap(spx);
2682 		break;
2683 	default:
2684 		rval = sata_txlt_invalid_command(spx);
2685 		break;
2686 	}
2687 
2688 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2689 	    "sata_scsi_start: rval %d\n", rval);
2690 
2691 	return (rval);
2692 }
2693 
2694 /*
2695  * Implementation of scsi tran_abort.
2696  * Abort specific pkt or all packets.
2697  *
2698  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2699  *
2700  * May be called from an interrupt level.
2701  */
2702 static int
2703 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2704 {
2705 	sata_hba_inst_t *sata_hba_inst =
2706 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2707 	sata_device_t	sata_device;
2708 	sata_pkt_t	*sata_pkt;
2709 
2710 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2711 	    "sata_scsi_abort: %s at target: 0x%x\n",
2712 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2713 
2714 	/* Validate address */
2715 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2716 		/* Invalid address */
2717 		return (0);
2718 
2719 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2720 	    sata_device.satadev_addr.cport)));
2721 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2722 		/* invalid address */
2723 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2724 		    sata_device.satadev_addr.cport)));
2725 		return (0);
2726 	}
2727 	if (scsi_pkt == NULL) {
2728 		/*
2729 		 * Abort all packets.
2730 		 * Although we do not have specific packet, we still need
2731 		 * dummy packet structure to pass device address to HBA.
2732 		 * Allocate one, without sleeping. Fail if pkt cannot be
2733 		 * allocated.
2734 		 */
2735 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2736 		if (sata_pkt == NULL) {
2737 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2738 			    sata_device.satadev_addr.cport)));
2739 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2740 			    "could not allocate sata_pkt"));
2741 			return (0);
2742 		}
2743 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2744 		sata_pkt->satapkt_device = sata_device;
2745 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2746 	} else {
2747 		if (scsi_pkt->pkt_ha_private == NULL) {
2748 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2749 			    sata_device.satadev_addr.cport)));
2750 			return (0); /* Bad scsi pkt */
2751 		}
2752 		/* extract pointer to sata pkt */
2753 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2754 		    txlt_sata_pkt;
2755 	}
2756 
2757 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2758 	    sata_device.satadev_addr.cport)));
2759 	/* Send abort request to HBA */
2760 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2761 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2762 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2763 	    SATA_SUCCESS) {
2764 		if (scsi_pkt == NULL)
2765 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2766 		/* Success */
2767 		return (1);
2768 	}
2769 	/* Else, something did not go right */
2770 	if (scsi_pkt == NULL)
2771 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2772 	/* Failure */
2773 	return (0);
2774 }
2775 
2776 
2777 /*
2778  * Implementation of scsi tran_reset.
2779  * RESET_ALL request is translated into port reset.
2780  * RESET_TARGET requests is translated into a device reset,
2781  * RESET_LUN request is accepted only for LUN 0 and translated into
2782  * device reset.
2783  * The target reset should cause all HBA active and queued packets to
2784  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2785  * the return. HBA should report reset event for the device.
2786  *
2787  * Returns 1 upon success, 0 upon failure.
2788  */
2789 static int
2790 sata_scsi_reset(struct scsi_address *ap, int level)
2791 {
2792 	sata_hba_inst_t	*sata_hba_inst =
2793 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2794 	sata_device_t	sata_device;
2795 	int		val;
2796 
2797 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2798 	    "sata_scsi_reset: level %d target: 0x%x\n",
2799 	    level, ap->a_target);
2800 
2801 	/* Validate address */
2802 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2803 	if (val == -1)
2804 		/* Invalid address */
2805 		return (0);
2806 
2807 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2808 	    sata_device.satadev_addr.cport)));
2809 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2810 		/* invalid address */
2811 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2812 		    sata_device.satadev_addr.cport)));
2813 		return (0);
2814 	}
2815 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2816 	    sata_device.satadev_addr.cport)));
2817 	if (level == RESET_ALL) {
2818 		/* port reset */
2819 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2820 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2821 		else
2822 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2823 
2824 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2825 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2826 			return (1);
2827 		else
2828 			return (0);
2829 
2830 	} else if (val == 0 &&
2831 	    (level == RESET_TARGET || level == RESET_LUN)) {
2832 		/* reset device (device attached) */
2833 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2834 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2835 			return (1);
2836 		else
2837 			return (0);
2838 	}
2839 	return (0);
2840 }
2841 
2842 
2843 /*
2844  * Implementation of scsi tran_getcap (get transport/device capabilities).
2845  * Supported capabilities for SATA hard disks:
2846  * auto-rqsense		(always supported)
2847  * tagged-qing		(supported if HBA supports it)
2848  * untagged-qing	(could be supported if disk supports it, but because
2849  *			 caching behavior allowing untagged queuing actually
2850  *			 results in reduced performance.  sd tries to throttle
2851  *			 back to only 3 outstanding commands, which may
2852  *			 work for real SCSI disks, but with read ahead
2853  *			 caching, having more than 1 outstanding command
2854  *			 results in cache thrashing.)
2855  * sector_size
2856  * dma_max
2857  * interconnect-type	(INTERCONNECT_SATA)
2858  *
2859  * Supported capabilities for ATAPI CD/DVD devices:
2860  * auto-rqsense		(always supported)
2861  * sector_size
2862  * dma_max
2863  * max-cdb-length
2864  * interconnect-type	(INTERCONNECT_SATA)
2865  *
2866  * Supported capabilities for ATAPI TAPE devices:
2867  * auto-rqsense		(always supported)
2868  * dma_max
2869  * max-cdb-length
2870  *
2871  * Supported capabilities for SATA ATAPI hard disks:
2872  * auto-rqsense		(always supported)
2873  * interconnect-type	(INTERCONNECT_SATA)
2874  * max-cdb-length
2875  *
2876  * Request for other capabilities is rejected as unsupported.
2877  *
2878  * Returns supported capability value, or -1 if capability is unsuppported or
2879  * the address is invalid - no device.
2880  */
2881 
2882 static int
2883 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2884 {
2885 
2886 	sata_hba_inst_t 	*sata_hba_inst =
2887 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2888 	sata_device_t		sata_device;
2889 	sata_drive_info_t	*sdinfo;
2890 	ddi_dma_attr_t		adj_dma_attr;
2891 	int 			rval;
2892 
2893 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2894 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2895 	    ap->a_target, cap);
2896 
2897 	/*
2898 	 * We want to process the capabilities on per port granularity.
2899 	 * So, we are specifically restricting ourselves to whom != 0
2900 	 * to exclude the controller wide handling.
2901 	 */
2902 	if (cap == NULL || whom == 0)
2903 		return (-1);
2904 
2905 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2906 		/* Invalid address */
2907 		return (-1);
2908 	}
2909 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2910 	    sata_device.satadev_addr.cport)));
2911 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2912 	    NULL) {
2913 		/* invalid address */
2914 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2915 		    sata_device.satadev_addr.cport)));
2916 		return (-1);
2917 	}
2918 
2919 	switch (scsi_hba_lookup_capstr(cap)) {
2920 	case SCSI_CAP_ARQ:
2921 		rval = 1;		/* ARQ supported, turned on */
2922 		break;
2923 
2924 	case SCSI_CAP_SECTOR_SIZE:
2925 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2926 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2927 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2928 			rval = SATA_ATAPI_SECTOR_SIZE;
2929 		else rval = -1;
2930 		break;
2931 
2932 	/*
2933 	 * untagged queuing cause a performance inversion because of
2934 	 * the way sd operates.  Because of this reason we do not
2935 	 * use it when available.
2936 	 */
2937 	case SCSI_CAP_UNTAGGED_QING:
2938 		if (sdinfo->satadrv_features_enabled &
2939 		    SATA_DEV_F_E_UNTAGGED_QING)
2940 			rval = 1;	/* Untagged queuing available */
2941 		else
2942 			rval = -1;	/* Untagged queuing not available */
2943 		break;
2944 
2945 	case SCSI_CAP_TAGGED_QING:
2946 		if ((sdinfo->satadrv_features_enabled &
2947 		    SATA_DEV_F_E_TAGGED_QING) &&
2948 		    (sdinfo->satadrv_max_queue_depth > 1))
2949 			rval = 1;	/* Tagged queuing available */
2950 		else
2951 			rval = -1;	/* Tagged queuing not available */
2952 		break;
2953 
2954 	case SCSI_CAP_DMA_MAX:
2955 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2956 		    &adj_dma_attr);
2957 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2958 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2959 		break;
2960 
2961 	case SCSI_CAP_INTERCONNECT_TYPE:
2962 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2963 		break;
2964 
2965 	case SCSI_CAP_CDB_LEN:
2966 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2967 			rval = sdinfo->satadrv_atapi_cdb_len;
2968 		else
2969 			rval = -1;
2970 		break;
2971 
2972 	default:
2973 		rval = -1;
2974 		break;
2975 	}
2976 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2977 	    sata_device.satadev_addr.cport)));
2978 	return (rval);
2979 }
2980 
2981 /*
2982  * Implementation of scsi tran_setcap
2983  *
2984  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2985  *
2986  */
2987 static int
2988 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2989 {
2990 	sata_hba_inst_t	*sata_hba_inst =
2991 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2992 	sata_device_t	sata_device;
2993 	sata_drive_info_t	*sdinfo;
2994 	int		rval;
2995 
2996 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2997 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2998 
2999 	/*
3000 	 * We want to process the capabilities on per port granularity.
3001 	 * So, we are specifically restricting ourselves to whom != 0
3002 	 * to exclude the controller wide handling.
3003 	 */
3004 	if (cap == NULL || whom == 0) {
3005 		return (-1);
3006 	}
3007 
3008 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3009 		/* Invalid address */
3010 		return (-1);
3011 	}
3012 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3013 	    sata_device.satadev_addr.cport)));
3014 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3015 	    &sata_device)) == NULL) {
3016 		/* invalid address */
3017 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3018 		    sata_device.satadev_addr.cport)));
3019 		return (-1);
3020 	}
3021 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3022 	    sata_device.satadev_addr.cport)));
3023 
3024 	switch (scsi_hba_lookup_capstr(cap)) {
3025 	case SCSI_CAP_ARQ:
3026 	case SCSI_CAP_SECTOR_SIZE:
3027 	case SCSI_CAP_DMA_MAX:
3028 	case SCSI_CAP_INTERCONNECT_TYPE:
3029 		rval = 0;
3030 		break;
3031 	case SCSI_CAP_UNTAGGED_QING:
3032 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3033 			rval = 1;
3034 			if (value == 1) {
3035 				sdinfo->satadrv_features_enabled |=
3036 				    SATA_DEV_F_E_UNTAGGED_QING;
3037 			} else if (value == 0) {
3038 				sdinfo->satadrv_features_enabled &=
3039 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3040 			} else {
3041 				rval = -1;
3042 			}
3043 		} else {
3044 			rval = 0;
3045 		}
3046 		break;
3047 	case SCSI_CAP_TAGGED_QING:
3048 		/* This can TCQ or NCQ */
3049 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3050 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3051 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3052 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3053 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3054 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3055 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3056 			rval = 1;
3057 			if (value == 1) {
3058 				sdinfo->satadrv_features_enabled |=
3059 				    SATA_DEV_F_E_TAGGED_QING;
3060 			} else if (value == 0) {
3061 				sdinfo->satadrv_features_enabled &=
3062 				    ~SATA_DEV_F_E_TAGGED_QING;
3063 			} else {
3064 				rval = -1;
3065 			}
3066 		} else {
3067 			rval = 0;
3068 		}
3069 		break;
3070 	default:
3071 		rval = -1;
3072 		break;
3073 	}
3074 	return (rval);
3075 }
3076 
3077 /*
3078  * Implementations of scsi tran_destroy_pkt.
3079  * Free resources allocated by sata_scsi_init_pkt()
3080  */
3081 static void
3082 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3083 {
3084 	sata_pkt_txlate_t *spx;
3085 
3086 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3087 
3088 	sata_common_free_dma_rsrcs(spx);
3089 
3090 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3091 	sata_pkt_free(spx);
3092 
3093 	scsi_hba_pkt_free(ap, pkt);
3094 }
3095 
3096 /*
3097  * Implementation of scsi tran_dmafree.
3098  * Free DMA resources allocated by sata_scsi_init_pkt()
3099  */
3100 
3101 static void
3102 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3103 {
3104 #ifndef __lock_lint
3105 	_NOTE(ARGUNUSED(ap))
3106 #endif
3107 	sata_pkt_txlate_t *spx;
3108 
3109 	ASSERT(pkt != NULL);
3110 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3111 
3112 	sata_common_free_dma_rsrcs(spx);
3113 }
3114 
3115 /*
3116  * Implementation of scsi tran_sync_pkt.
3117  *
3118  * The assumption below is that pkt is unique - there is no need to check ap
3119  *
3120  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3121  * into/from the real buffer.
3122  */
3123 static void
3124 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3125 {
3126 #ifndef __lock_lint
3127 	_NOTE(ARGUNUSED(ap))
3128 #endif
3129 	int rval;
3130 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3131 	struct buf *bp;
3132 	int direction;
3133 
3134 	ASSERT(spx != NULL);
3135 	if (spx->txlt_buf_dma_handle != NULL) {
3136 		direction = spx->txlt_sata_pkt->
3137 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3138 		if (spx->txlt_sata_pkt != NULL &&
3139 		    direction != SATA_DIR_NODATA_XFER) {
3140 			if (spx->txlt_tmp_buf != NULL) {
3141 				/* Intermediate DMA buffer used */
3142 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3143 
3144 				if (direction & SATA_DIR_WRITE) {
3145 					bcopy(bp->b_un.b_addr,
3146 					    spx->txlt_tmp_buf, bp->b_bcount);
3147 				}
3148 			}
3149 			/* Sync the buffer for device or for CPU */
3150 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3151 			    (direction & SATA_DIR_WRITE) ?
3152 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3153 			ASSERT(rval == DDI_SUCCESS);
3154 			if (spx->txlt_tmp_buf != NULL &&
3155 			    !(direction & SATA_DIR_WRITE)) {
3156 				/* Intermediate DMA buffer used for read */
3157 				bcopy(spx->txlt_tmp_buf,
3158 				    bp->b_un.b_addr, bp->b_bcount);
3159 			}
3160 
3161 		}
3162 	}
3163 }
3164 
3165 
3166 
3167 /* *******************  SATA - SCSI Translation functions **************** */
3168 /*
3169  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3170  * translation.
3171  */
3172 
3173 /*
3174  * Checks if a device exists and can be access and translates common
3175  * scsi_pkt data to sata_pkt data.
3176  *
3177  * Flag argument indicates that a non-read/write ATA command may be sent
3178  * to HBA in arbitrary SYNC mode to execute this packet.
3179  *
3180  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3181  * sata_pkt was set-up.
3182  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3183  * exist and pkt_comp callback was scheduled.
3184  * Returns other TRAN_XXXXX values when error occured and command should be
3185  * rejected with the returned TRAN_XXXXX value.
3186  *
3187  * This function should be called with port mutex held.
3188  */
3189 static int
3190 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3191 {
3192 	sata_drive_info_t *sdinfo;
3193 	sata_device_t sata_device;
3194 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3195 		SATA_DIR_NODATA_XFER,
3196 		/* all other values to 0/FALSE */
3197 	};
3198 	/*
3199 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3200 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3201 	 * indicates that the scsi packet was not accepted (the reason will not
3202 	 * be checked by the scsi target driver).
3203 	 * To make debugging easier, we set pkt_reason to know value here.
3204 	 * It may be changed later when different completion reason is
3205 	 * determined.
3206 	 */
3207 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3208 	*reason = CMD_TRAN_ERR;
3209 
3210 	/* Validate address */
3211 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3212 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3213 
3214 	case -1:
3215 		/* Invalid address or invalid device type */
3216 		return (TRAN_BADPKT);
3217 	case 2:
3218 		/*
3219 		 * Valid address but device type is unknown - Chack if it is
3220 		 * in the reset state and therefore in an indeterminate state.
3221 		 */
3222 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3223 		    &spx->txlt_sata_pkt->satapkt_device);
3224 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3225 		    (SATA_EVNT_DEVICE_RESET |
3226 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3227 			if (!ddi_in_panic()) {
3228 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3229 				*reason = CMD_INCOMPLETE;
3230 				SATADBG1(SATA_DBG_SCSI_IF,
3231 				    spx->txlt_sata_hba_inst,
3232 				    "sata_scsi_start: rejecting command "
3233 				    "because of device reset state\n", NULL);
3234 				return (TRAN_BUSY);
3235 			}
3236 		}
3237 		/* FALLTHROUGH */
3238 	case 1:
3239 		/* valid address but no valid device - it has disappeared */
3240 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3241 		*reason = CMD_DEV_GONE;
3242 		/*
3243 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3244 		 * only in callback function (for normal requests) and
3245 		 * in the dump code path.
3246 		 * So, if the callback is available, we need to do
3247 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3248 		 */
3249 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3250 			/* scsi callback required */
3251 			if (servicing_interrupt()) {
3252 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3253 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3254 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3255 				    NULL) {
3256 					return (TRAN_BUSY);
3257 				}
3258 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3259 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3260 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3261 				/* Scheduling the callback failed */
3262 				return (TRAN_BUSY);
3263 			}
3264 
3265 			return (TRAN_ACCEPT);
3266 		}
3267 		return (TRAN_FATAL_ERROR);
3268 	default:
3269 		/* all OK; pkt reason will be overwritten later */
3270 		break;
3271 	}
3272 	/*
3273 	 * If pkt is to be executed in polling mode and a command will not be
3274 	 * emulated in SATA module (requires sending a non-read/write ATA
3275 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3276 	 * interrupt context and not in the panic dump, then reject the packet
3277 	 * to avoid a possible interrupt stack overrun or hang caused by
3278 	 * a potentially blocked interrupt.
3279 	 */
3280 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3281 	    servicing_interrupt() && !ddi_in_panic()) {
3282 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3283 		    "sata_scsi_start: rejecting synchronous command because "
3284 		    "of interrupt context\n", NULL);
3285 		return (TRAN_BUSY);
3286 	}
3287 
3288 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3289 	    &spx->txlt_sata_pkt->satapkt_device);
3290 
3291 	/*
3292 	 * If device is in reset condition, reject the packet with
3293 	 * TRAN_BUSY, unless:
3294 	 * 1. system is panicking (dumping)
3295 	 * In such case only one thread is running and there is no way to
3296 	 * process reset.
3297 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3298 	 * Some cfgadm operations involve drive commands, so reset condition
3299 	 * needs to be ignored for IOCTL operations.
3300 	 */
3301 	if ((sdinfo->satadrv_event_flags &
3302 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3303 
3304 		if (!ddi_in_panic() &&
3305 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3306 		    sata_device.satadev_addr.cport) &
3307 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3308 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3309 			*reason = CMD_INCOMPLETE;
3310 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3311 			    "sata_scsi_start: rejecting command because "
3312 			    "of device reset state\n", NULL);
3313 			return (TRAN_BUSY);
3314 		}
3315 	}
3316 
3317 	/*
3318 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3319 	 * sata_scsi_pkt_init() because pkt init had to work also with
3320 	 * non-existing devices.
3321 	 * Now we know that the packet was set-up for a real device, so its
3322 	 * type is known.
3323 	 */
3324 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3325 
3326 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3327 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3328 	    sata_device.satadev_addr.cport)->cport_event_flags &
3329 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3330 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3331 		    sata_ignore_dev_reset = B_TRUE;
3332 	}
3333 	/*
3334 	 * At this point the generic translation routine determined that the
3335 	 * scsi packet should be accepted. Packet completion reason may be
3336 	 * changed later when a different completion reason is determined.
3337 	 */
3338 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3339 	*reason = CMD_CMPLT;
3340 
3341 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3342 		/* Synchronous execution */
3343 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3344 		    SATA_OPMODE_POLLING;
3345 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3346 		    sata_ignore_dev_reset = ddi_in_panic();
3347 	} else {
3348 		/* Asynchronous execution */
3349 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3350 		    SATA_OPMODE_INTERRUPTS;
3351 	}
3352 	/* Convert queuing information */
3353 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3354 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3355 		    B_TRUE;
3356 	else if (spx->txlt_scsi_pkt->pkt_flags &
3357 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3358 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3359 		    B_TRUE;
3360 
3361 	/* Always limit pkt time */
3362 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3363 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3364 	else
3365 		/* Pass on scsi_pkt time */
3366 		spx->txlt_sata_pkt->satapkt_time =
3367 		    spx->txlt_scsi_pkt->pkt_time;
3368 
3369 	return (TRAN_ACCEPT);
3370 }
3371 
3372 
3373 /*
3374  * Translate ATA Identify Device data to SCSI Inquiry data.
3375  * This function may be called only for ATA devices.
3376  * This function should not be called for ATAPI devices - they
3377  * respond directly to SCSI Inquiry command.
3378  *
3379  * SATA Identify Device data has to be valid in sata_drive_info.
3380  * Buffer has to accomodate the inquiry length (36 bytes).
3381  *
3382  * This function should be called with a port mutex held.
3383  */
3384 static	void
3385 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3386     sata_drive_info_t *sdinfo, uint8_t *buf)
3387 {
3388 
3389 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3390 	struct sata_id *sid = &sdinfo->satadrv_id;
3391 
3392 	/* Start with a nice clean slate */
3393 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3394 
3395 	/*
3396 	 * Rely on the dev_type for setting paripheral qualifier.
3397 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3398 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3399 	 * ATAPI Inquiry may provide more data to the target driver.
3400 	 */
3401 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3402 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3403 
3404 	/* CFA type device is not a removable media device */
3405 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3406 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3407 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3408 	inq->inq_iso = 0;	/* ISO version */
3409 	inq->inq_ecma = 0;	/* ECMA version */
3410 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3411 	inq->inq_aenc = 0;	/* Async event notification cap. */
3412 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3413 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3414 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3415 	inq->inq_len = 31;	/* Additional length */
3416 	inq->inq_dualp = 0;	/* dual port device - NO */
3417 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3418 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3419 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3420 				/*
3421 				 * Queuing support - controller has to
3422 				 * support some sort of command queuing.
3423 				 */
3424 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3425 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3426 	else
3427 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3428 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3429 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3430 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3431 
3432 #ifdef	_LITTLE_ENDIAN
3433 	/* Swap text fields to match SCSI format */
3434 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3435 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3436 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3437 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3438 	else
3439 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3440 #else	/* _LITTLE_ENDIAN */
3441 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3442 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3443 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3444 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3445 	else
3446 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3447 #endif	/* _LITTLE_ENDIAN */
3448 }
3449 
3450 
3451 /*
3452  * Scsi response set up for invalid command (command not supported)
3453  *
3454  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3455  */
3456 static int
3457 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3458 {
3459 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3460 	struct scsi_extended_sense *sense;
3461 
3462 	scsipkt->pkt_reason = CMD_CMPLT;
3463 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3464 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3465 
3466 	*scsipkt->pkt_scbp = STATUS_CHECK;
3467 
3468 	sense = sata_arq_sense(spx);
3469 	sense->es_key = KEY_ILLEGAL_REQUEST;
3470 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3471 
3472 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3473 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3474 
3475 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3476 	    scsipkt->pkt_comp != NULL) {
3477 		/* scsi callback required */
3478 		if (servicing_interrupt()) {
3479 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3480 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3481 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3482 				return (TRAN_BUSY);
3483 			}
3484 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3485 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3486 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3487 			/* Scheduling the callback failed */
3488 			return (TRAN_BUSY);
3489 		}
3490 	}
3491 	return (TRAN_ACCEPT);
3492 }
3493 
3494 /*
3495  * Scsi response set up for check condition with special sense key
3496  * and additional sense code.
3497  *
3498  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3499  */
3500 static int
3501 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3502 {
3503 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3504 	int cport = SATA_TXLT_CPORT(spx);
3505 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3506 	struct scsi_extended_sense *sense;
3507 
3508 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3509 	scsipkt->pkt_reason = CMD_CMPLT;
3510 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3511 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3512 
3513 	*scsipkt->pkt_scbp = STATUS_CHECK;
3514 
3515 	sense = sata_arq_sense(spx);
3516 	sense->es_key = key;
3517 	sense->es_add_code = code;
3518 
3519 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3520 
3521 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3522 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3523 
3524 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3525 	    scsipkt->pkt_comp != NULL) {
3526 		/* scsi callback required */
3527 		if (servicing_interrupt()) {
3528 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3529 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3530 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3531 				return (TRAN_BUSY);
3532 			}
3533 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3534 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3535 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3536 			/* Scheduling the callback failed */
3537 			return (TRAN_BUSY);
3538 		}
3539 	}
3540 	return (TRAN_ACCEPT);
3541 }
3542 
3543 /*
3544  * Scsi response setup for
3545  * emulated non-data command that requires no action/return data
3546  *
3547  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3548  */
3549 static	int
3550 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3551 {
3552 	int rval;
3553 	int reason;
3554 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3555 
3556 	mutex_enter(cport_mutex);
3557 
3558 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3559 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3560 		mutex_exit(cport_mutex);
3561 		return (rval);
3562 	}
3563 	mutex_exit(cport_mutex);
3564 
3565 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3566 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3567 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3568 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3569 
3570 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3571 	    "Scsi_pkt completion reason %x\n",
3572 	    spx->txlt_scsi_pkt->pkt_reason);
3573 
3574 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3575 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3576 		/* scsi callback required */
3577 		if (servicing_interrupt()) {
3578 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3579 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3580 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3581 				return (TRAN_BUSY);
3582 			}
3583 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3584 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3585 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3586 			/* Scheduling the callback failed */
3587 			return (TRAN_BUSY);
3588 		}
3589 	}
3590 	return (TRAN_ACCEPT);
3591 }
3592 
3593 
3594 /*
3595  * SATA translate command: Inquiry / Identify Device
3596  * Use cached Identify Device data for now, rather than issuing actual
3597  * Device Identify cmd request. If device is detached and re-attached,
3598  * asynchronous event processing should fetch and refresh Identify Device
3599  * data.
3600  * VPD pages supported now:
3601  * Vital Product Data page
3602  * Unit Serial Number page
3603  * Block Device Characteristics Page
3604  * ATA Information Page
3605  *
3606  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3607  */
3608 
3609 #define	EVPD			1	/* Extended Vital Product Data flag */
3610 #define	CMDDT			2	/* Command Support Data - Obsolete */
3611 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3612 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3613 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3614 					/* Code */
3615 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3616 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3617 
3618 static int
3619 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3620 {
3621 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3622 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3623 	sata_drive_info_t *sdinfo;
3624 	struct scsi_extended_sense *sense;
3625 	int count;
3626 	uint8_t *p;
3627 	int i, j;
3628 	uint8_t page_buf[1024]; /* Max length */
3629 	int rval, reason;
3630 	ushort_t rate;
3631 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3632 
3633 	mutex_enter(cport_mutex);
3634 
3635 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3636 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3637 		mutex_exit(cport_mutex);
3638 		return (rval);
3639 	}
3640 
3641 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3642 	    &spx->txlt_sata_pkt->satapkt_device);
3643 
3644 	ASSERT(sdinfo != NULL);
3645 
3646 	scsipkt->pkt_reason = CMD_CMPLT;
3647 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3648 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3649 
3650 	/* Reject not supported request */
3651 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3652 		*scsipkt->pkt_scbp = STATUS_CHECK;
3653 		sense = sata_arq_sense(spx);
3654 		sense->es_key = KEY_ILLEGAL_REQUEST;
3655 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3656 		goto done;
3657 	}
3658 
3659 	/* Valid Inquiry request */
3660 	*scsipkt->pkt_scbp = STATUS_GOOD;
3661 
3662 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3663 
3664 		/*
3665 		 * Because it is fully emulated command storing data
3666 		 * programatically in the specified buffer, release
3667 		 * preallocated DMA resources before storing data in the buffer,
3668 		 * so no unwanted DMA sync would take place.
3669 		 */
3670 		sata_scsi_dmafree(NULL, scsipkt);
3671 
3672 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3673 			/* Standard Inquiry Data request */
3674 			struct scsi_inquiry inq;
3675 			unsigned int bufsize;
3676 
3677 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3678 			    sdinfo, (uint8_t *)&inq);
3679 			/* Copy no more than requested */
3680 			count = MIN(bp->b_bcount,
3681 			    sizeof (struct scsi_inquiry));
3682 			bufsize = scsipkt->pkt_cdbp[4];
3683 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3684 			count = MIN(count, bufsize);
3685 			bcopy(&inq, bp->b_un.b_addr, count);
3686 
3687 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3688 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3689 			    bufsize - count : 0;
3690 		} else {
3691 			/*
3692 			 * peripheral_qualifier = 0;
3693 			 *
3694 			 * We are dealing only with HD and will be
3695 			 * dealing with CD/DVD devices soon
3696 			 */
3697 			uint8_t peripheral_device_type =
3698 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3699 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3700 
3701 			bzero(page_buf, sizeof (page_buf));
3702 
3703 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3704 			case INQUIRY_SUP_VPD_PAGE:
3705 				/*
3706 				 * Request for supported Vital Product Data
3707 				 * pages.
3708 				 */
3709 				page_buf[0] = peripheral_device_type;
3710 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3711 				page_buf[2] = 0;
3712 				page_buf[3] = 4; /* page length */
3713 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3714 				page_buf[5] = INQUIRY_USN_PAGE;
3715 				page_buf[6] = INQUIRY_BDC_PAGE;
3716 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3717 				/* Copy no more than requested */
3718 				count = MIN(bp->b_bcount, 8);
3719 				bcopy(page_buf, bp->b_un.b_addr, count);
3720 				break;
3721 
3722 			case INQUIRY_USN_PAGE:
3723 				/*
3724 				 * Request for Unit Serial Number page.
3725 				 * Set-up the page.
3726 				 */
3727 				page_buf[0] = peripheral_device_type;
3728 				page_buf[1] = INQUIRY_USN_PAGE;
3729 				page_buf[2] = 0;
3730 				/* remaining page length */
3731 				page_buf[3] = SATA_ID_SERIAL_LEN;
3732 
3733 				/*
3734 				 * Copy serial number from Identify Device data
3735 				 * words into the inquiry page and swap bytes
3736 				 * when necessary.
3737 				 */
3738 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3739 #ifdef	_LITTLE_ENDIAN
3740 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3741 #else
3742 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3743 #endif
3744 				/*
3745 				 * Least significant character of the serial
3746 				 * number shall appear as the last byte,
3747 				 * according to SBC-3 spec.
3748 				 * Count trailing spaces to determine the
3749 				 * necessary shift length.
3750 				 */
3751 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3752 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3753 					if (*(p - j) != '\0' &&
3754 					    *(p - j) != '\040')
3755 						break;
3756 				}
3757 
3758 				/*
3759 				 * Shift SN string right, so that the last
3760 				 * non-blank character would appear in last
3761 				 * byte of SN field in the page.
3762 				 * 'j' is the shift length.
3763 				 */
3764 				for (i = 0;
3765 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3766 				    i++, p--)
3767 					*p = *(p - j);
3768 
3769 				/*
3770 				 * Add leading spaces - same number as the
3771 				 * shift size
3772 				 */
3773 				for (; j > 0; j--)
3774 					page_buf[4 + j - 1] = '\040';
3775 
3776 				count = MIN(bp->b_bcount,
3777 				    SATA_ID_SERIAL_LEN + 4);
3778 				bcopy(page_buf, bp->b_un.b_addr, count);
3779 				break;
3780 
3781 			case INQUIRY_BDC_PAGE:
3782 				/*
3783 				 * Request for Block Device Characteristics
3784 				 * page.  Set-up the page.
3785 				 */
3786 				page_buf[0] = peripheral_device_type;
3787 				page_buf[1] = INQUIRY_BDC_PAGE;
3788 				page_buf[2] = 0;
3789 				/* remaining page length */
3790 				page_buf[3] = SATA_ID_BDC_LEN;
3791 
3792 				rate = sdinfo->satadrv_id.ai_medrotrate;
3793 				page_buf[4] = (rate >> 8) & 0xff;
3794 				page_buf[5] = rate & 0xff;
3795 				page_buf[6] = 0;
3796 				page_buf[7] = sdinfo->satadrv_id.
3797 				    ai_nomformfactor & 0xf;
3798 
3799 				count = MIN(bp->b_bcount,
3800 				    SATA_ID_BDC_LEN + 4);
3801 				bcopy(page_buf, bp->b_un.b_addr, count);
3802 				break;
3803 
3804 			case INQUIRY_ATA_INFO_PAGE:
3805 				/*
3806 				 * Request for ATA Information page.
3807 				 */
3808 				page_buf[0] = peripheral_device_type;
3809 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3810 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3811 				    0xff;
3812 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3813 				/* page_buf[4-7] reserved */
3814 #ifdef  _LITTLE_ENDIAN
3815 				bcopy("ATA     ", &page_buf[8], 8);
3816 				swab(sdinfo->satadrv_id.ai_model,
3817 				    &page_buf[16], 16);
3818 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3819 				    "    ", 4) == 0) {
3820 					swab(sdinfo->satadrv_id.ai_fw,
3821 					    &page_buf[32], 4);
3822 				} else {
3823 					swab(&sdinfo->satadrv_id.ai_fw[4],
3824 					    &page_buf[32], 4);
3825 				}
3826 #else   /* _LITTLE_ENDIAN */
3827 				bcopy("ATA     ", &page_buf[8], 8);
3828 				bcopy(sdinfo->satadrv_id.ai_model,
3829 				    &page_buf[16], 16);
3830 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3831 				    "    ", 4) == 0) {
3832 					bcopy(sdinfo->satadrv_id.ai_fw,
3833 					    &page_buf[32], 4);
3834 				} else {
3835 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3836 					    &page_buf[32], 4);
3837 				}
3838 #endif  /* _LITTLE_ENDIAN */
3839 				/*
3840 				 * page_buf[36-55] which defines the device
3841 				 * signature is not defined at this
3842 				 * time.
3843 				 */
3844 
3845 				/* Set the command code */
3846 				if (sdinfo->satadrv_type ==
3847 				    SATA_DTYPE_ATADISK) {
3848 					page_buf[56] = SATAC_ID_DEVICE;
3849 				} else if (sdinfo->satadrv_type ==
3850 				    SATA_DTYPE_ATAPI) {
3851 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3852 				}
3853 				/*
3854 				 * If the command code, page_buf[56], is not
3855 				 * zero and if one of the identify commands
3856 				 * succeeds, return the identify data.
3857 				 */
3858 				if ((page_buf[56] != 0) &&
3859 				    (sata_fetch_device_identify_data(
3860 				    spx->txlt_sata_hba_inst, sdinfo) ==
3861 				    SATA_SUCCESS)) {
3862 					bcopy(&sdinfo->satadrv_id,
3863 					    &page_buf[60], sizeof (sata_id_t));
3864 				}
3865 
3866 				/* Need to copy out the page_buf to bp */
3867 				count = MIN(bp->b_bcount,
3868 				    SATA_ID_ATA_INFO_LEN + 4);
3869 				bcopy(page_buf, bp->b_un.b_addr, count);
3870 				break;
3871 
3872 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3873 				/*
3874 				 * We may want to implement this page, when
3875 				 * identifiers are common for SATA devices
3876 				 * But not now.
3877 				 */
3878 				/*FALLTHROUGH*/
3879 
3880 			default:
3881 				/* Request for unsupported VPD page */
3882 				*scsipkt->pkt_scbp = STATUS_CHECK;
3883 				sense = sata_arq_sense(spx);
3884 				sense->es_key = KEY_ILLEGAL_REQUEST;
3885 				sense->es_add_code =
3886 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3887 				goto done;
3888 			}
3889 		}
3890 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3891 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3892 		    scsipkt->pkt_cdbp[4] - count : 0;
3893 	}
3894 done:
3895 	mutex_exit(cport_mutex);
3896 
3897 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3898 	    "Scsi_pkt completion reason %x\n",
3899 	    scsipkt->pkt_reason);
3900 
3901 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3902 	    scsipkt->pkt_comp != NULL) {
3903 		/* scsi callback required */
3904 		if (servicing_interrupt()) {
3905 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3906 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3907 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3908 				return (TRAN_BUSY);
3909 			}
3910 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3911 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3912 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3913 			/* Scheduling the callback failed */
3914 			return (TRAN_BUSY);
3915 		}
3916 	}
3917 	return (TRAN_ACCEPT);
3918 }
3919 
3920 /*
3921  * SATA translate command: Request Sense.
3922  *
3923  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3924  * At the moment this is an emulated command (ATA version for SATA hard disks).
3925  * May be translated into Check Power Mode command in the future.
3926  *
3927  * Note: There is a mismatch between already implemented Informational
3928  * Exception Mode Select page 0x1C and this function.
3929  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3930  * NO SENSE and set additional sense code to the exception code - this is not
3931  * implemented here.
3932  */
3933 static int
3934 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3935 {
3936 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3937 	struct scsi_extended_sense sense;
3938 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3939 	sata_drive_info_t *sdinfo;
3940 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3941 	int rval, reason, power_state = 0;
3942 	kmutex_t *cport_mutex;
3943 
3944 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3945 	mutex_enter(cport_mutex);
3946 
3947 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3948 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3949 		mutex_exit(cport_mutex);
3950 		return (rval);
3951 	}
3952 
3953 	scsipkt->pkt_reason = CMD_CMPLT;
3954 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3955 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3956 	*scsipkt->pkt_scbp = STATUS_GOOD;
3957 
3958 	/*
3959 	 * when CONTROL field's NACA bit == 1
3960 	 * return ILLEGAL_REQUEST
3961 	 */
3962 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3963 		mutex_exit(cport_mutex);
3964 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3965 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3966 	}
3967 
3968 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3969 	    &spx->txlt_sata_pkt->satapkt_device);
3970 	ASSERT(sdinfo != NULL);
3971 
3972 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3973 
3974 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3975 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3976 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3977 	if (sata_hba_start(spx, &rval) != 0) {
3978 		mutex_exit(cport_mutex);
3979 		return (rval);
3980 	}
3981 	if (scmd->satacmd_error_reg != 0) {
3982 		mutex_exit(cport_mutex);
3983 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3984 		    SD_SCSI_ASC_NO_ADD_SENSE));
3985 	}
3986 
3987 	switch (scmd->satacmd_sec_count_lsb) {
3988 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3989 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3990 			power_state = SATA_POWER_STOPPED;
3991 		else {
3992 			power_state = SATA_POWER_STANDBY;
3993 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3994 		}
3995 		break;
3996 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3997 		power_state = SATA_POWER_IDLE;
3998 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3999 		break;
4000 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4001 	default:		  /* 0x40, 0x41 active mode */
4002 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4003 			power_state = SATA_POWER_IDLE;
4004 		else {
4005 			power_state = SATA_POWER_ACTIVE;
4006 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4007 		}
4008 		break;
4009 	}
4010 
4011 	mutex_exit(cport_mutex);
4012 
4013 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4014 		/*
4015 		 * Because it is fully emulated command storing data
4016 		 * programatically in the specified buffer, release
4017 		 * preallocated DMA resources before storing data in the buffer,
4018 		 * so no unwanted DMA sync would take place.
4019 		 */
4020 		int count = MIN(bp->b_bcount,
4021 		    sizeof (struct scsi_extended_sense));
4022 		sata_scsi_dmafree(NULL, scsipkt);
4023 		bzero(&sense, sizeof (struct scsi_extended_sense));
4024 		sense.es_valid = 0;	/* Valid LBA */
4025 		sense.es_class = 7;	/* Response code 0x70 - current err */
4026 		sense.es_key = KEY_NO_SENSE;
4027 		sense.es_add_len = 6;	/* Additional length */
4028 		/* Copy no more than requested */
4029 		bcopy(&sense, bp->b_un.b_addr, count);
4030 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4031 		scsipkt->pkt_resid = 0;
4032 		switch (power_state) {
4033 		case SATA_POWER_IDLE:
4034 		case SATA_POWER_STANDBY:
4035 			sense.es_add_code =
4036 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4037 			break;
4038 		case SATA_POWER_STOPPED:
4039 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4040 			break;
4041 		case SATA_POWER_ACTIVE:
4042 		default:
4043 			break;
4044 		}
4045 	}
4046 
4047 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4048 	    "Scsi_pkt completion reason %x\n",
4049 	    scsipkt->pkt_reason);
4050 
4051 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4052 	    scsipkt->pkt_comp != NULL) {
4053 		/* scsi callback required */
4054 		if (servicing_interrupt()) {
4055 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4056 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4057 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4058 				return (TRAN_BUSY);
4059 			}
4060 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4061 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4062 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4063 			/* Scheduling the callback failed */
4064 			return (TRAN_BUSY);
4065 		}
4066 	}
4067 	return (TRAN_ACCEPT);
4068 }
4069 
4070 /*
4071  * SATA translate command: Test Unit Ready
4072  * (ATA version for SATA hard disks).
4073  * It is translated into the Check Power Mode command.
4074  *
4075  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4076  */
4077 static int
4078 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4079 {
4080 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4081 	struct scsi_extended_sense *sense;
4082 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4083 	sata_drive_info_t *sdinfo;
4084 	int power_state;
4085 	int rval, reason;
4086 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4087 
4088 	mutex_enter(cport_mutex);
4089 
4090 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4091 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4092 		mutex_exit(cport_mutex);
4093 		return (rval);
4094 	}
4095 
4096 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4097 	    &spx->txlt_sata_pkt->satapkt_device);
4098 	ASSERT(sdinfo != NULL);
4099 
4100 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4101 
4102 	/* send CHECK POWER MODE command */
4103 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4104 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4105 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4106 	if (sata_hba_start(spx, &rval) != 0) {
4107 		mutex_exit(cport_mutex);
4108 		return (rval);
4109 	}
4110 
4111 	if (scmd->satacmd_error_reg != 0) {
4112 		mutex_exit(cport_mutex);
4113 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4114 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4115 	}
4116 
4117 	power_state = scmd->satacmd_sec_count_lsb;
4118 
4119 	/*
4120 	 * return NOT READY when device in STOPPED mode
4121 	 */
4122 	if (power_state == SATA_PWRMODE_STANDBY &&
4123 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4124 		*scsipkt->pkt_scbp = STATUS_CHECK;
4125 		sense = sata_arq_sense(spx);
4126 		sense->es_key = KEY_NOT_READY;
4127 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4128 	} else {
4129 		/*
4130 		 * For other power mode, return GOOD status
4131 		 */
4132 		*scsipkt->pkt_scbp = STATUS_GOOD;
4133 	}
4134 
4135 	scsipkt->pkt_reason = CMD_CMPLT;
4136 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4137 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4138 
4139 	mutex_exit(cport_mutex);
4140 
4141 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4142 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4143 
4144 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4145 	    scsipkt->pkt_comp != NULL) {
4146 		/* scsi callback required */
4147 		if (servicing_interrupt()) {
4148 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4149 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4150 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4151 				return (TRAN_BUSY);
4152 			}
4153 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4154 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4155 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4156 			/* Scheduling the callback failed */
4157 			return (TRAN_BUSY);
4158 		}
4159 	}
4160 
4161 	return (TRAN_ACCEPT);
4162 }
4163 
4164 /*
4165  * SATA translate command: Start Stop Unit
4166  * Translation depends on a command:
4167  *
4168  * Power condition bits will be supported
4169  * and the power level should be maintained by SATL,
4170  * When SATL received a command, it will check the
4171  * power level firstly, and return the status according
4172  * to SAT2 v2.6 and SAT-2 Standby Modifications
4173  *
4174  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4175  * -----------------------------------------------------------------------
4176  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4177  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4178  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4179  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4180  *
4181  *	Unload Media / NOT SUPPORTED YET
4182  *	Load Media / NOT SUPPROTED YET
4183  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4184  *
4185  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4186  * appropriate values in scsi_pkt fields.
4187  */
4188 static int
4189 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4190 {
4191 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4192 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4193 	int rval, reason;
4194 	sata_drive_info_t *sdinfo;
4195 	sata_id_t *sata_id;
4196 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4197 
4198 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4199 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4200 
4201 	mutex_enter(cport_mutex);
4202 
4203 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4204 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4205 		mutex_exit(cport_mutex);
4206 		return (rval);
4207 	}
4208 
4209 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4210 		/* IMMED bit - not supported */
4211 		mutex_exit(cport_mutex);
4212 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4213 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4214 	}
4215 
4216 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4217 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4218 
4219 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4220 	    &spx->txlt_sata_pkt->satapkt_device);
4221 	ASSERT(sdinfo != NULL);
4222 	sata_id = &sdinfo->satadrv_id;
4223 
4224 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4225 	case 0:
4226 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4227 			/* Load/Unload Media - invalid request */
4228 			goto err_out;
4229 		}
4230 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4231 			/* Start Unit */
4232 			sata_build_read_verify_cmd(scmd, 1, 5);
4233 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4234 			/* Transfer command to HBA */
4235 			if (sata_hba_start(spx, &rval) != 0) {
4236 				/* Pkt not accepted for execution */
4237 				mutex_exit(cport_mutex);
4238 				return (rval);
4239 			}
4240 			if (scmd->satacmd_error_reg != 0) {
4241 				goto err_out;
4242 			}
4243 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4244 		} else {
4245 			/* Stop Unit */
4246 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4247 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4248 			if (sata_hba_start(spx, &rval) != 0) {
4249 				mutex_exit(cport_mutex);
4250 				return (rval);
4251 			} else {
4252 				if (scmd->satacmd_error_reg != 0) {
4253 					goto err_out;
4254 				}
4255 			}
4256 			/* ata standby immediate command */
4257 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4258 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4259 			if (sata_hba_start(spx, &rval) != 0) {
4260 				mutex_exit(cport_mutex);
4261 				return (rval);
4262 			}
4263 			if (scmd->satacmd_error_reg != 0) {
4264 				goto err_out;
4265 			}
4266 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4267 		}
4268 		break;
4269 	case 0x1:
4270 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4271 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4272 		if (sata_hba_start(spx, &rval) != 0) {
4273 			mutex_exit(cport_mutex);
4274 			return (rval);
4275 		}
4276 		if (scmd->satacmd_error_reg != 0) {
4277 			goto err_out;
4278 		}
4279 		sata_build_read_verify_cmd(scmd, 1, 5);
4280 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4281 		/* Transfer command to HBA */
4282 		if (sata_hba_start(spx, &rval) != 0) {
4283 			/* Pkt not accepted for execution */
4284 			mutex_exit(cport_mutex);
4285 			return (rval);
4286 		} else {
4287 			if (scmd->satacmd_error_reg != 0) {
4288 				goto err_out;
4289 			}
4290 		}
4291 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4292 		break;
4293 	case 0x2:
4294 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4295 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4296 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4297 			if (sata_hba_start(spx, &rval) != 0) {
4298 				mutex_exit(cport_mutex);
4299 				return (rval);
4300 			}
4301 			if (scmd->satacmd_error_reg != 0) {
4302 				goto err_out;
4303 			}
4304 		}
4305 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4306 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4307 		if (sata_hba_start(spx, &rval) != 0) {
4308 			mutex_exit(cport_mutex);
4309 			return (rval);
4310 		}
4311 		if (scmd->satacmd_error_reg != 0) {
4312 			goto err_out;
4313 		}
4314 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4315 			/*
4316 			 *  POWER CONDITION MODIFIER bit set
4317 			 *  to 0x1 or larger it will be handled
4318 			 *  on the same way as bit = 0x1
4319 			 */
4320 			if (!(sata_id->ai_cmdset84 &
4321 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4322 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4323 				break;
4324 			}
4325 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4326 			scmd->satacmd_features_reg = 0x44;
4327 			scmd->satacmd_lba_low_lsb = 0x4c;
4328 			scmd->satacmd_lba_mid_lsb = 0x4e;
4329 			scmd->satacmd_lba_high_lsb = 0x55;
4330 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4331 			if (sata_hba_start(spx, &rval) != 0) {
4332 				mutex_exit(cport_mutex);
4333 				return (rval);
4334 			}
4335 			if (scmd->satacmd_error_reg != 0) {
4336 				goto err_out;
4337 			}
4338 		}
4339 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4340 		break;
4341 	case 0x3:
4342 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4343 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4345 			if (sata_hba_start(spx, &rval) != 0) {
4346 				mutex_exit(cport_mutex);
4347 				return (rval);
4348 			}
4349 			if (scmd->satacmd_error_reg != 0) {
4350 				goto err_out;
4351 			}
4352 		}
4353 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4354 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4355 		if (sata_hba_start(spx, &rval) != 0) {
4356 			mutex_exit(cport_mutex);
4357 			return (rval);
4358 		}
4359 		if (scmd->satacmd_error_reg != 0) {
4360 			goto err_out;
4361 		}
4362 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4363 		break;
4364 	case 0x7:
4365 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4366 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4367 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4368 		if (sata_hba_start(spx, &rval) != 0) {
4369 			mutex_exit(cport_mutex);
4370 			return (rval);
4371 		}
4372 		if (scmd->satacmd_error_reg != 0) {
4373 			goto err_out;
4374 		}
4375 		switch (scmd->satacmd_sec_count_lsb) {
4376 		case SATA_PWRMODE_STANDBY:
4377 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4378 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4379 			    sdinfo->satadrv_standby_timer);
4380 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4381 			if (sata_hba_start(spx, &rval) != 0) {
4382 				mutex_exit(cport_mutex);
4383 				return (rval);
4384 			} else {
4385 				if (scmd->satacmd_error_reg != 0) {
4386 					goto err_out;
4387 				}
4388 			}
4389 			break;
4390 		case SATA_PWRMODE_IDLE:
4391 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4392 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4393 			    sdinfo->satadrv_standby_timer);
4394 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4395 			if (sata_hba_start(spx, &rval) != 0) {
4396 				mutex_exit(cport_mutex);
4397 				return (rval);
4398 			} else {
4399 				if (scmd->satacmd_error_reg != 0) {
4400 					goto err_out;
4401 				}
4402 			}
4403 			break;
4404 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4405 		case SATA_PWRMODE_ACTIVE_SPINUP:
4406 		case SATA_PWRMODE_ACTIVE:
4407 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4408 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4409 			    sdinfo->satadrv_standby_timer);
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 			sata_build_read_verify_cmd(scmd, 1, 5);
4419 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4420 			if (sata_hba_start(spx, &rval) != 0) {
4421 				mutex_exit(cport_mutex);
4422 				return (rval);
4423 			}
4424 			if (scmd->satacmd_error_reg != 0) {
4425 				goto err_out;
4426 			}
4427 			break;
4428 		default:
4429 			goto err_out;
4430 		}
4431 		break;
4432 	case 0xb:
4433 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4434 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4435 			mutex_exit(cport_mutex);
4436 			return (sata_txlt_check_condition(spx,
4437 			    KEY_ILLEGAL_REQUEST,
4438 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4439 		}
4440 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4441 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4442 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4443 			if (sata_hba_start(spx, &rval) != 0) {
4444 				mutex_exit(cport_mutex);
4445 				return (rval);
4446 			}
4447 			if (scmd->satacmd_error_reg != 0) {
4448 				goto err_out;
4449 			}
4450 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4451 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4452 			if (sata_hba_start(spx, &rval) != 0) {
4453 				mutex_exit(cport_mutex);
4454 				return (rval);
4455 			}
4456 			if (scmd->satacmd_error_reg != 0) {
4457 				goto err_out;
4458 			}
4459 		}
4460 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4461 		break;
4462 	default:
4463 err_out:
4464 		mutex_exit(cport_mutex);
4465 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4466 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4467 	}
4468 
4469 	/*
4470 	 * Since it was a synchronous command,
4471 	 * a callback function will be called directly.
4472 	 */
4473 	mutex_exit(cport_mutex);
4474 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4475 	    "synchronous execution status %x\n",
4476 	    spx->txlt_sata_pkt->satapkt_reason);
4477 
4478 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4479 	    scsipkt->pkt_comp != NULL) {
4480 		sata_set_arq_data(spx->txlt_sata_pkt);
4481 		if (servicing_interrupt()) {
4482 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4483 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4484 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4485 				return (TRAN_BUSY);
4486 			}
4487 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4488 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4489 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4490 			/* Scheduling the callback failed */
4491 			return (TRAN_BUSY);
4492 		}
4493 	}
4494 	else
4495 
4496 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4497 
4498 	return (TRAN_ACCEPT);
4499 
4500 }
4501 
4502 /*
4503  * SATA translate command:  Read Capacity.
4504  * Emulated command for SATA disks.
4505  * Capacity is retrieved from cached Idenifty Device data.
4506  * Identify Device data shows effective disk capacity, not the native
4507  * capacity, which may be limitted by Set Max Address command.
4508  * This is ATA version for SATA hard disks.
4509  *
4510  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4511  */
4512 static int
4513 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4514 {
4515 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4516 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4517 	sata_drive_info_t *sdinfo;
4518 	uint64_t val;
4519 	uchar_t *rbuf;
4520 	int rval, reason;
4521 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4522 
4523 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4524 	    "sata_txlt_read_capacity: ", NULL);
4525 
4526 	mutex_enter(cport_mutex);
4527 
4528 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4529 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4530 		mutex_exit(cport_mutex);
4531 		return (rval);
4532 	}
4533 
4534 	scsipkt->pkt_reason = CMD_CMPLT;
4535 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4536 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4537 	*scsipkt->pkt_scbp = STATUS_GOOD;
4538 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4539 		/*
4540 		 * Because it is fully emulated command storing data
4541 		 * programatically in the specified buffer, release
4542 		 * preallocated DMA resources before storing data in the buffer,
4543 		 * so no unwanted DMA sync would take place.
4544 		 */
4545 		sata_scsi_dmafree(NULL, scsipkt);
4546 
4547 		sdinfo = sata_get_device_info(
4548 		    spx->txlt_sata_hba_inst,
4549 		    &spx->txlt_sata_pkt->satapkt_device);
4550 
4551 		/*
4552 		 * As per SBC-3, the "returned LBA" is either the highest
4553 		 * addressable LBA or 0xffffffff, whichever is smaller.
4554 		 */
4555 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4556 
4557 		rbuf = (uchar_t *)bp->b_un.b_addr;
4558 		/* Need to swap endians to match scsi format */
4559 		rbuf[0] = (val >> 24) & 0xff;
4560 		rbuf[1] = (val >> 16) & 0xff;
4561 		rbuf[2] = (val >> 8) & 0xff;
4562 		rbuf[3] = val & 0xff;
4563 		/* block size - always 512 bytes, for now */
4564 		rbuf[4] = 0;
4565 		rbuf[5] = 0;
4566 		rbuf[6] = 0x02;
4567 		rbuf[7] = 0;
4568 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4569 		scsipkt->pkt_resid = 0;
4570 
4571 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4572 		    sdinfo->satadrv_capacity -1);
4573 	}
4574 	mutex_exit(cport_mutex);
4575 	/*
4576 	 * If a callback was requested, do it now.
4577 	 */
4578 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4579 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4580 
4581 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4582 	    scsipkt->pkt_comp != NULL) {
4583 		/* scsi callback required */
4584 		if (servicing_interrupt()) {
4585 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4586 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4587 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4588 				return (TRAN_BUSY);
4589 			}
4590 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4591 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4592 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4593 			/* Scheduling the callback failed */
4594 			return (TRAN_BUSY);
4595 		}
4596 	}
4597 
4598 	return (TRAN_ACCEPT);
4599 }
4600 
4601 /*
4602  * SATA translate command:  Read Capacity (16).
4603  * Emulated command for SATA disks.
4604  * Info is retrieved from cached Identify Device data.
4605  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4606  *
4607  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4608  */
4609 static int
4610 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4611 {
4612 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4613 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4614 	sata_drive_info_t *sdinfo;
4615 	uint64_t val;
4616 	uint16_t l2p_exp;
4617 	uchar_t *rbuf;
4618 	int rval, reason;
4619 #define	TPE	0x80
4620 #define	TPRZ	0x40
4621 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4622 
4623 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4624 	    "sata_txlt_read_capacity: ", NULL);
4625 
4626 	mutex_enter(cport_mutex);
4627 
4628 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4629 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4630 		mutex_exit(cport_mutex);
4631 		return (rval);
4632 	}
4633 
4634 	scsipkt->pkt_reason = CMD_CMPLT;
4635 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4636 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4637 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4638 		/*
4639 		 * Because it is fully emulated command storing data
4640 		 * programatically in the specified buffer, release
4641 		 * preallocated DMA resources before storing data in the buffer,
4642 		 * so no unwanted DMA sync would take place.
4643 		 */
4644 		sata_scsi_dmafree(NULL, scsipkt);
4645 
4646 		/* Check SERVICE ACTION field */
4647 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4648 		    SSVC_ACTION_READ_CAPACITY_G4) {
4649 			mutex_exit(cport_mutex);
4650 			return (sata_txlt_check_condition(spx,
4651 			    KEY_ILLEGAL_REQUEST,
4652 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4653 		}
4654 
4655 		/* Check LBA field */
4656 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4657 		    (scsipkt->pkt_cdbp[3] != 0) ||
4658 		    (scsipkt->pkt_cdbp[4] != 0) ||
4659 		    (scsipkt->pkt_cdbp[5] != 0) ||
4660 		    (scsipkt->pkt_cdbp[6] != 0) ||
4661 		    (scsipkt->pkt_cdbp[7] != 0) ||
4662 		    (scsipkt->pkt_cdbp[8] != 0) ||
4663 		    (scsipkt->pkt_cdbp[9] != 0)) {
4664 			mutex_exit(cport_mutex);
4665 			return (sata_txlt_check_condition(spx,
4666 			    KEY_ILLEGAL_REQUEST,
4667 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4668 		}
4669 
4670 		/* Check PMI bit */
4671 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4672 			mutex_exit(cport_mutex);
4673 			return (sata_txlt_check_condition(spx,
4674 			    KEY_ILLEGAL_REQUEST,
4675 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4676 		}
4677 
4678 		*scsipkt->pkt_scbp = STATUS_GOOD;
4679 
4680 		sdinfo = sata_get_device_info(
4681 		    spx->txlt_sata_hba_inst,
4682 		    &spx->txlt_sata_pkt->satapkt_device);
4683 
4684 		/* last logical block address */
4685 		val = MIN(sdinfo->satadrv_capacity - 1,
4686 		    SCSI_READ_CAPACITY16_MAX_LBA);
4687 
4688 		/* logical to physical block size exponent */
4689 		l2p_exp = 0;
4690 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4691 			/* physical/logical sector size word is valid */
4692 
4693 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4694 			    SATA_L2PS_HAS_MULT) {
4695 				/* multiple logical sectors per phys sectors */
4696 				l2p_exp =
4697 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4698 				    SATA_L2PS_EXP_MASK;
4699 			}
4700 		}
4701 
4702 		rbuf = (uchar_t *)bp->b_un.b_addr;
4703 		bzero(rbuf, bp->b_bcount);
4704 
4705 		/* returned logical block address */
4706 		rbuf[0] = (val >> 56) & 0xff;
4707 		rbuf[1] = (val >> 48) & 0xff;
4708 		rbuf[2] = (val >> 40) & 0xff;
4709 		rbuf[3] = (val >> 32) & 0xff;
4710 		rbuf[4] = (val >> 24) & 0xff;
4711 		rbuf[5] = (val >> 16) & 0xff;
4712 		rbuf[6] = (val >> 8) & 0xff;
4713 		rbuf[7] = val & 0xff;
4714 
4715 		/* logical block length in bytes = 512 (for now) */
4716 		/* rbuf[8] = 0; */
4717 		/* rbuf[9] = 0; */
4718 		rbuf[10] = 0x02;
4719 		/* rbuf[11] = 0; */
4720 
4721 		/* p_type, prot_en, unspecified by SAT-2 */
4722 		/* rbuf[12] = 0; */
4723 
4724 		/* p_i_exponent, undefined by SAT-2 */
4725 		/* logical blocks per physical block exponent */
4726 		rbuf[13] = l2p_exp;
4727 
4728 		/* lowest aligned logical block address = 0 (for now) */
4729 		/* tpe and tprz as defined in T10/10-079 r0 */
4730 		if (sdinfo->satadrv_id.ai_addsupported &
4731 		    SATA_DETERMINISTIC_READ) {
4732 			if (sdinfo->satadrv_id.ai_addsupported &
4733 			    SATA_READ_ZERO) {
4734 				rbuf[14] |= TPRZ;
4735 			} else {
4736 				rbuf[14] |= TPE;
4737 			}
4738 		}
4739 		/* rbuf[15] = 0; */
4740 
4741 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4742 		scsipkt->pkt_resid = 0;
4743 
4744 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4745 		    sdinfo->satadrv_capacity -1);
4746 	}
4747 
4748 	mutex_exit(cport_mutex);
4749 
4750 	/*
4751 	 * If a callback was requested, do it now.
4752 	 */
4753 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4754 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4755 
4756 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4757 	    scsipkt->pkt_comp != NULL) {
4758 		/* scsi callback required */
4759 		if (servicing_interrupt()) {
4760 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4761 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4762 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4763 				return (TRAN_BUSY);
4764 			}
4765 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4766 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4767 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4768 			/* Scheduling the callback failed */
4769 			return (TRAN_BUSY);
4770 		}
4771 	}
4772 
4773 	return (TRAN_ACCEPT);
4774 }
4775 
4776 /*
4777  * Translate command: UNMAP
4778  *
4779  * The function cannot be called in interrupt context since it may sleep.
4780  */
4781 static int
4782 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4783 {
4784 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4785 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4786 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4787 	uint16_t count = 0;
4788 	int synch;
4789 	int rval, reason;
4790 	int i, x;
4791 	int bdlen = 0;
4792 	int ranges = 0;
4793 	int paramlen = 8;
4794 	uint8_t *data, *tmpbd;
4795 	sata_drive_info_t *sdinfo;
4796 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4797 #define	TRIM	0x1
4798 
4799 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4800 	    "sata_txlt_unmap: ", NULL);
4801 
4802 	mutex_enter(cport_mutex);
4803 
4804 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4805 	    &spx->txlt_sata_pkt->satapkt_device);
4806 	if (sdinfo != NULL) {
4807 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4808 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4809 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4810 		    sdinfo->satadrv_id.ai_maxcount);
4811 	}
4812 
4813 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4814 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4815 		mutex_exit(cport_mutex);
4816 		return (rval);
4817 	}
4818 
4819 	/*
4820 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4821 	 * Start by getting the block descriptor data length by subtracting
4822 	 * the 8 byte parameter list header from the parameter list length.
4823 	 * The block descriptor size has to be a multiple of 16 bytes.
4824 	 */
4825 	bdlen = scsipkt->pkt_cdbp[7];
4826 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4827 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4828 	    (bdlen > (bp->b_bcount - paramlen))) {
4829 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4830 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4831 		mutex_exit(cport_mutex);
4832 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4833 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4834 	}
4835 	/*
4836 	 * If there are no parameter data or block descriptors, it is not
4837 	 * considered an error so just complete the command without sending
4838 	 * TRIM.
4839 	 */
4840 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4841 	    (bp->b_bcount == 0)) {
4842 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4843 		    "sata_txlt_unmap: no parameter data or block descriptors",
4844 		    NULL);
4845 		mutex_exit(cport_mutex);
4846 		return (sata_txlt_unmap_nodata_cmd(spx));
4847 	}
4848 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4849 	data = kmem_zalloc(bdlen, KM_SLEEP);
4850 
4851 	/*
4852 	 * Loop through all the UNMAP block descriptors and convert the data
4853 	 * into TRIM format.
4854 	 */
4855 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4856 		/* get range length */
4857 		data[x] = tmpbd[i+7];
4858 		data[x+1] = tmpbd[i+6];
4859 		/* get LBA */
4860 		data[x+2] = tmpbd[i+5];
4861 		data[x+3] = tmpbd[i+4];
4862 		data[x+4] = tmpbd[i+3];
4863 		data[x+5] = tmpbd[i+2];
4864 		data[x+6] = tmpbd[i+11];
4865 		data[x+7] = tmpbd[i+10];
4866 
4867 		ranges++;
4868 	}
4869 
4870 	/*
4871 	 * The TRIM command expects the data buffer to be a multiple of
4872 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4873 	 * may be too small.  Free the original DMA resources and create a
4874 	 * local buffer.
4875 	 */
4876 	sata_common_free_dma_rsrcs(spx);
4877 
4878 	/*
4879 	 * Get count of 512-byte blocks of range entries.  The length
4880 	 * of a range entry is 8 bytes which means one count has 64 range
4881 	 * entries.
4882 	 */
4883 	count = (ranges + 63)/64;
4884 
4885 	/* Allocate a buffer that is a multiple of 512 bytes. */
4886 	mutex_exit(cport_mutex);
4887 	bp = sata_alloc_local_buffer(spx, count * 512);
4888 	if (bp == NULL) {
4889 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4890 		    "sata_txlt_unmap: "
4891 		    "cannot allocate buffer for TRIM command", NULL);
4892 		kmem_free(data, bdlen);
4893 		return (TRAN_BUSY);
4894 	}
4895 	bp_mapin(bp); /* make data buffer accessible */
4896 	mutex_enter(cport_mutex);
4897 
4898 	bzero(bp->b_un.b_addr, bp->b_bcount);
4899 	bcopy(data, bp->b_un.b_addr, x);
4900 	kmem_free(data, bdlen);
4901 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4902 	    DDI_DMA_SYNC_FORDEV);
4903 	ASSERT(rval == DDI_SUCCESS);
4904 
4905 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4906 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4907 	scmd->satacmd_cmd_reg = SATAC_DSM;
4908 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4909 	scmd->satacmd_sec_count_lsb = count & 0xff;
4910 	scmd->satacmd_features_reg = TRIM;
4911 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4912 	scmd->satacmd_status_reg = 0;
4913 	scmd->satacmd_error_reg = 0;
4914 
4915 	/* Start processing command */
4916 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4917 		spx->txlt_sata_pkt->satapkt_comp =
4918 		    sata_txlt_unmap_completion;
4919 		synch = FALSE;
4920 	} else {
4921 		synch = TRUE;
4922 	}
4923 
4924 	if (sata_hba_start(spx, &rval) != 0) {
4925 		mutex_exit(cport_mutex);
4926 		return (rval);
4927 	}
4928 
4929 	mutex_exit(cport_mutex);
4930 
4931 	if (synch) {
4932 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4933 	}
4934 
4935 	return (TRAN_ACCEPT);
4936 }
4937 
4938 /*
4939  * SATA translate command: Mode Sense.
4940  * Translated into appropriate SATA command or emulated.
4941  * Saved Values Page Control (03) are not supported.
4942  *
4943  * NOTE: only caching mode sense page is currently implemented.
4944  *
4945  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4946  */
4947 
4948 #define	LLBAA	0x10	/* Long LBA Accepted */
4949 
4950 static int
4951 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4952 {
4953 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4954 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4955 	sata_drive_info_t *sdinfo;
4956 	sata_id_t *sata_id;
4957 	struct scsi_extended_sense *sense;
4958 	int 		len, bdlen, count, alc_len;
4959 	int		pc;	/* Page Control code */
4960 	uint8_t		*buf;	/* mode sense buffer */
4961 	int		rval, reason;
4962 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4963 
4964 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4965 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4966 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4967 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4968 
4969 	if (servicing_interrupt()) {
4970 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4971 		if (buf == NULL) {
4972 			return (TRAN_BUSY);
4973 		}
4974 	} else {
4975 		buf = kmem_zalloc(1024, KM_SLEEP);
4976 	}
4977 
4978 	mutex_enter(cport_mutex);
4979 
4980 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4981 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4982 		mutex_exit(cport_mutex);
4983 		kmem_free(buf, 1024);
4984 		return (rval);
4985 	}
4986 
4987 	scsipkt->pkt_reason = CMD_CMPLT;
4988 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4989 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4990 
4991 	pc = scsipkt->pkt_cdbp[2] >> 6;
4992 
4993 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4994 		/*
4995 		 * Because it is fully emulated command storing data
4996 		 * programatically in the specified buffer, release
4997 		 * preallocated DMA resources before storing data in the buffer,
4998 		 * so no unwanted DMA sync would take place.
4999 		 */
5000 		sata_scsi_dmafree(NULL, scsipkt);
5001 
5002 		len = 0;
5003 		bdlen = 0;
5004 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
5005 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5006 			    (scsipkt->pkt_cdbp[1] & LLBAA))
5007 				bdlen = 16;
5008 			else
5009 				bdlen = 8;
5010 		}
5011 		/* Build mode parameter header */
5012 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5013 			/* 4-byte mode parameter header */
5014 			buf[len++] = 0;		/* mode data length */
5015 			buf[len++] = 0;		/* medium type */
5016 			buf[len++] = 0;		/* dev-specific param */
5017 			buf[len++] = bdlen;	/* Block Descriptor length */
5018 		} else {
5019 			/* 8-byte mode parameter header */
5020 			buf[len++] = 0;		/* mode data length */
5021 			buf[len++] = 0;
5022 			buf[len++] = 0;		/* medium type */
5023 			buf[len++] = 0;		/* dev-specific param */
5024 			if (bdlen == 16)
5025 				buf[len++] = 1;	/* long lba descriptor */
5026 			else
5027 				buf[len++] = 0;
5028 			buf[len++] = 0;
5029 			buf[len++] = 0;		/* Block Descriptor length */
5030 			buf[len++] = bdlen;
5031 		}
5032 
5033 		sdinfo = sata_get_device_info(
5034 		    spx->txlt_sata_hba_inst,
5035 		    &spx->txlt_sata_pkt->satapkt_device);
5036 
5037 		/* Build block descriptor only if not disabled (DBD) */
5038 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5039 			/* Block descriptor - direct-access device format */
5040 			if (bdlen == 8) {
5041 				/* build regular block descriptor */
5042 				buf[len++] =
5043 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5044 				buf[len++] =
5045 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5046 				buf[len++] =
5047 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5048 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5049 				buf[len++] = 0; /* density code */
5050 				buf[len++] = 0;
5051 				if (sdinfo->satadrv_type ==
5052 				    SATA_DTYPE_ATADISK)
5053 					buf[len++] = 2;
5054 				else
5055 					/* ATAPI */
5056 					buf[len++] = 8;
5057 				buf[len++] = 0;
5058 			} else if (bdlen == 16) {
5059 				/* Long LBA Accepted */
5060 				/* build long lba block descriptor */
5061 #ifndef __lock_lint
5062 				buf[len++] =
5063 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5064 				buf[len++] =
5065 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5066 				buf[len++] =
5067 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5068 				buf[len++] =
5069 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5070 #endif
5071 				buf[len++] =
5072 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5073 				buf[len++] =
5074 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5075 				buf[len++] =
5076 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5077 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5078 				buf[len++] = 0;
5079 				buf[len++] = 0; /* density code */
5080 				buf[len++] = 0;
5081 				buf[len++] = 0;
5082 				if (sdinfo->satadrv_type ==
5083 				    SATA_DTYPE_ATADISK)
5084 					buf[len++] = 2;
5085 				else
5086 					/* ATAPI */
5087 					buf[len++] = 8;
5088 				buf[len++] = 0;
5089 			}
5090 		}
5091 
5092 		sata_id = &sdinfo->satadrv_id;
5093 
5094 		/*
5095 		 * Add requested pages.
5096 		 * Page 3 and 4 are obsolete and we are not supporting them.
5097 		 * We deal now with:
5098 		 * caching (read/write cache control).
5099 		 * We should eventually deal with following mode pages:
5100 		 * error recovery  (0x01),
5101 		 * power condition (0x1a),
5102 		 * exception control page (enables SMART) (0x1c),
5103 		 * enclosure management (ses),
5104 		 * protocol-specific port mode (port control).
5105 		 */
5106 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5107 		case MODEPAGE_RW_ERRRECOV:
5108 			/* DAD_MODE_ERR_RECOV */
5109 			/* R/W recovery */
5110 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5111 			break;
5112 		case MODEPAGE_CACHING:
5113 			/* DAD_MODE_CACHE */
5114 			/* Reject not supported request for saved parameters */
5115 			if (pc == 3) {
5116 				*scsipkt->pkt_scbp = STATUS_CHECK;
5117 				sense = sata_arq_sense(spx);
5118 				sense->es_key = KEY_ILLEGAL_REQUEST;
5119 				sense->es_add_code =
5120 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5121 				goto done;
5122 			}
5123 
5124 			/* caching */
5125 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5126 			break;
5127 		case MODEPAGE_INFO_EXCPT:
5128 			/* exception cntrl */
5129 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5130 				len += sata_build_msense_page_1c(sdinfo, pc,
5131 				    buf+len);
5132 			}
5133 			else
5134 				goto err;
5135 			break;
5136 		case MODEPAGE_POWER_COND:
5137 			/* DAD_MODE_POWER_COND */
5138 			/* power condition */
5139 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5140 			break;
5141 
5142 		case MODEPAGE_ACOUSTIC_MANAG:
5143 			/* acoustic management */
5144 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5145 			break;
5146 		case MODEPAGE_ALLPAGES:
5147 			/* all pages */
5148 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5149 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5150 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5151 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5152 				len += sata_build_msense_page_1c(sdinfo, pc,
5153 				    buf+len);
5154 			}
5155 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5156 			break;
5157 		default:
5158 		err:
5159 			/* Invalid request */
5160 			*scsipkt->pkt_scbp = STATUS_CHECK;
5161 			sense = sata_arq_sense(spx);
5162 			sense->es_key = KEY_ILLEGAL_REQUEST;
5163 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5164 			goto done;
5165 		}
5166 
5167 		/* fix total mode data length */
5168 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5169 			/* 4-byte mode parameter header */
5170 			buf[0] = len - 1;	/* mode data length */
5171 		} else {
5172 			buf[0] = (len -2) >> 8;
5173 			buf[1] = (len -2) & 0xff;
5174 		}
5175 
5176 
5177 		/* Check allocation length */
5178 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5179 			alc_len = scsipkt->pkt_cdbp[4];
5180 		} else {
5181 			alc_len = scsipkt->pkt_cdbp[7];
5182 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5183 		}
5184 		/*
5185 		 * We do not check for possible parameters truncation
5186 		 * (alc_len < len) assuming that the target driver works
5187 		 * correctly. Just avoiding overrun.
5188 		 * Copy no more than requested and possible, buffer-wise.
5189 		 */
5190 		count = MIN(alc_len, len);
5191 		count = MIN(bp->b_bcount, count);
5192 		bcopy(buf, bp->b_un.b_addr, count);
5193 
5194 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5195 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5196 	}
5197 	*scsipkt->pkt_scbp = STATUS_GOOD;
5198 done:
5199 	mutex_exit(cport_mutex);
5200 	(void) kmem_free(buf, 1024);
5201 
5202 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5203 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5204 
5205 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5206 	    scsipkt->pkt_comp != NULL) {
5207 		/* scsi callback required */
5208 		if (servicing_interrupt()) {
5209 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5210 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5211 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5212 				return (TRAN_BUSY);
5213 			}
5214 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5215 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5216 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5217 			/* Scheduling the callback failed */
5218 			return (TRAN_BUSY);
5219 		}
5220 	}
5221 
5222 	return (TRAN_ACCEPT);
5223 }
5224 
5225 
5226 /*
5227  * SATA translate command: Mode Select.
5228  * Translated into appropriate SATA command or emulated.
5229  * Saving parameters is not supported.
5230  * Changing device capacity is not supported (although theoretically
5231  * possible by executing SET FEATURES/SET MAX ADDRESS)
5232  *
5233  * Assumption is that the target driver is working correctly.
5234  *
5235  * More than one SATA command may be executed to perform operations specified
5236  * by mode select pages. The first error terminates further execution.
5237  * Operations performed successully are not backed-up in such case.
5238  *
5239  * NOTE: Implemented pages:
5240  * - caching page
5241  * - informational exception page
5242  * - acoustic management page
5243  * - power condition page
5244  * Caching setup is remembered so it could be re-stored in case of
5245  * an unexpected device reset.
5246  *
5247  * Returns TRAN_XXXX.
5248  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5249  */
5250 
5251 static int
5252 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5253 {
5254 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5255 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5256 	struct scsi_extended_sense *sense;
5257 	int len, pagelen, count, pllen;
5258 	uint8_t *buf;	/* mode select buffer */
5259 	int rval, stat, reason;
5260 	uint_t nointr_flag;
5261 	int dmod = 0;
5262 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5263 
5264 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5265 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5266 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5267 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5268 
5269 	mutex_enter(cport_mutex);
5270 
5271 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5272 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5273 		mutex_exit(cport_mutex);
5274 		return (rval);
5275 	}
5276 
5277 	rval = TRAN_ACCEPT;
5278 
5279 	scsipkt->pkt_reason = CMD_CMPLT;
5280 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5281 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5282 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5283 
5284 	/* Reject not supported request */
5285 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5286 		*scsipkt->pkt_scbp = STATUS_CHECK;
5287 		sense = sata_arq_sense(spx);
5288 		sense->es_key = KEY_ILLEGAL_REQUEST;
5289 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5290 		goto done;
5291 	}
5292 
5293 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5294 		pllen = scsipkt->pkt_cdbp[4];
5295 	} else {
5296 		pllen = scsipkt->pkt_cdbp[7];
5297 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5298 	}
5299 
5300 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5301 
5302 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5303 		buf = (uint8_t *)bp->b_un.b_addr;
5304 		count = MIN(bp->b_bcount, pllen);
5305 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5306 		scsipkt->pkt_resid = 0;
5307 		pllen = count;
5308 
5309 		/*
5310 		 * Check the header to skip the block descriptor(s) - we
5311 		 * do not support setting device capacity.
5312 		 * Existing macros do not recognize long LBA dscriptor,
5313 		 * hence manual calculation.
5314 		 */
5315 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5316 			/* 6-bytes CMD, 4 bytes header */
5317 			if (count <= 4)
5318 				goto done;		/* header only */
5319 			len = buf[3] + 4;
5320 		} else {
5321 			/* 10-bytes CMD, 8 bytes header */
5322 			if (count <= 8)
5323 				goto done;		/* header only */
5324 			len = buf[6];
5325 			len = (len << 8) + buf[7] + 8;
5326 		}
5327 		if (len >= count)
5328 			goto done;	/* header + descriptor(s) only */
5329 
5330 		pllen -= len;		/* remaining data length */
5331 
5332 		/*
5333 		 * We may be executing SATA command and want to execute it
5334 		 * in SYNCH mode, regardless of scsi_pkt setting.
5335 		 * Save scsi_pkt setting and indicate SYNCH mode
5336 		 */
5337 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5338 		    scsipkt->pkt_comp != NULL) {
5339 			scsipkt->pkt_flags |= FLAG_NOINTR;
5340 		}
5341 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5342 
5343 		/*
5344 		 * len is now the offset to a first mode select page
5345 		 * Process all pages
5346 		 */
5347 		while (pllen > 0) {
5348 			switch ((int)buf[len]) {
5349 			case MODEPAGE_CACHING:
5350 				/* No support for SP (saving) */
5351 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5352 					*scsipkt->pkt_scbp = STATUS_CHECK;
5353 					sense = sata_arq_sense(spx);
5354 					sense->es_key = KEY_ILLEGAL_REQUEST;
5355 					sense->es_add_code =
5356 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5357 					goto done;
5358 				}
5359 				stat = sata_mode_select_page_8(spx,
5360 				    (struct mode_cache_scsi3 *)&buf[len],
5361 				    pllen, &pagelen, &rval, &dmod);
5362 				/*
5363 				 * The pagelen value indicates the number of
5364 				 * parameter bytes already processed.
5365 				 * The rval is the return value from
5366 				 * sata_tran_start().
5367 				 * The stat indicates the overall status of
5368 				 * the operation(s).
5369 				 */
5370 				if (stat != SATA_SUCCESS)
5371 					/*
5372 					 * Page processing did not succeed -
5373 					 * all error info is already set-up,
5374 					 * just return
5375 					 */
5376 					pllen = 0; /* this breaks the loop */
5377 				else {
5378 					len += pagelen;
5379 					pllen -= pagelen;
5380 				}
5381 				break;
5382 
5383 			case MODEPAGE_INFO_EXCPT:
5384 				stat = sata_mode_select_page_1c(spx,
5385 				    (struct mode_info_excpt_page *)&buf[len],
5386 				    pllen, &pagelen, &rval, &dmod);
5387 				/*
5388 				 * The pagelen value indicates the number of
5389 				 * parameter bytes already processed.
5390 				 * The rval is the return value from
5391 				 * sata_tran_start().
5392 				 * The stat indicates the overall status of
5393 				 * the operation(s).
5394 				 */
5395 				if (stat != SATA_SUCCESS)
5396 					/*
5397 					 * Page processing did not succeed -
5398 					 * all error info is already set-up,
5399 					 * just return
5400 					 */
5401 					pllen = 0; /* this breaks the loop */
5402 				else {
5403 					len += pagelen;
5404 					pllen -= pagelen;
5405 				}
5406 				break;
5407 
5408 			case MODEPAGE_ACOUSTIC_MANAG:
5409 				stat = sata_mode_select_page_30(spx,
5410 				    (struct mode_acoustic_management *)
5411 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5412 				/*
5413 				 * The pagelen value indicates the number of
5414 				 * parameter bytes already processed.
5415 				 * The rval is the return value from
5416 				 * sata_tran_start().
5417 				 * The stat indicates the overall status of
5418 				 * the operation(s).
5419 				 */
5420 				if (stat != SATA_SUCCESS)
5421 					/*
5422 					 * Page processing did not succeed -
5423 					 * all error info is already set-up,
5424 					 * just return
5425 					 */
5426 					pllen = 0; /* this breaks the loop */
5427 				else {
5428 					len += pagelen;
5429 					pllen -= pagelen;
5430 				}
5431 
5432 				break;
5433 			case MODEPAGE_POWER_COND:
5434 				stat = sata_mode_select_page_1a(spx,
5435 				    (struct mode_info_power_cond *)&buf[len],
5436 				    pllen, &pagelen, &rval, &dmod);
5437 				/*
5438 				 * The pagelen value indicates the number of
5439 				 * parameter bytes already processed.
5440 				 * The rval is the return value from
5441 				 * sata_tran_start().
5442 				 * The stat indicates the overall status of
5443 				 * the operation(s).
5444 				 */
5445 				if (stat != SATA_SUCCESS)
5446 					/*
5447 					 * Page processing did not succeed -
5448 					 * all error info is already set-up,
5449 					 * just return
5450 					 */
5451 					pllen = 0; /* this breaks the loop */
5452 				else {
5453 					len += pagelen;
5454 					pllen -= pagelen;
5455 				}
5456 				break;
5457 			default:
5458 				*scsipkt->pkt_scbp = STATUS_CHECK;
5459 				sense = sata_arq_sense(spx);
5460 				sense->es_key = KEY_ILLEGAL_REQUEST;
5461 				sense->es_add_code =
5462 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5463 				goto done;
5464 			}
5465 		}
5466 	}
5467 done:
5468 	mutex_exit(cport_mutex);
5469 	/*
5470 	 * If device parameters were modified, fetch and store the new
5471 	 * Identify Device data. Since port mutex could have been released
5472 	 * for accessing HBA driver, we need to re-check device existence.
5473 	 */
5474 	if (dmod != 0) {
5475 		sata_drive_info_t new_sdinfo, *sdinfo;
5476 		int rv = 0;
5477 
5478 		/*
5479 		 * Following statement has to be changed if this function is
5480 		 * used for devices other than SATA hard disks.
5481 		 */
5482 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5483 
5484 		new_sdinfo.satadrv_addr =
5485 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5486 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5487 		    &new_sdinfo);
5488 
5489 		mutex_enter(cport_mutex);
5490 		/*
5491 		 * Since port mutex could have been released when
5492 		 * accessing HBA driver, we need to re-check that the
5493 		 * framework still holds the device info structure.
5494 		 */
5495 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5496 		    &spx->txlt_sata_pkt->satapkt_device);
5497 		if (sdinfo != NULL) {
5498 			/*
5499 			 * Device still has info structure in the
5500 			 * sata framework. Copy newly fetched info
5501 			 */
5502 			if (rv == 0) {
5503 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5504 				sata_save_drive_settings(sdinfo);
5505 			} else {
5506 				/*
5507 				 * Could not fetch new data - invalidate
5508 				 * sata_drive_info. That makes device
5509 				 * unusable.
5510 				 */
5511 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5512 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5513 			}
5514 		}
5515 		if (rv != 0 || sdinfo == NULL) {
5516 			/*
5517 			 * This changes the overall mode select completion
5518 			 * reason to a failed one !!!!!
5519 			 */
5520 			*scsipkt->pkt_scbp = STATUS_CHECK;
5521 			sense = sata_arq_sense(spx);
5522 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5523 			rval = TRAN_ACCEPT;
5524 		}
5525 		mutex_exit(cport_mutex);
5526 	}
5527 	/* Restore the scsi pkt flags */
5528 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5529 	scsipkt->pkt_flags |= nointr_flag;
5530 
5531 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5532 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5533 
5534 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5535 	    scsipkt->pkt_comp != NULL) {
5536 		/* scsi callback required */
5537 		if (servicing_interrupt()) {
5538 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5539 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5540 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5541 				return (TRAN_BUSY);
5542 			}
5543 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5544 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5545 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5546 			/* Scheduling the callback failed */
5547 			return (TRAN_BUSY);
5548 		}
5549 	}
5550 
5551 	return (rval);
5552 }
5553 
5554 /*
5555  * Translate command: ATA Pass Through
5556  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5557  * PIO Data-Out protocols.  Also supports CK_COND bit.
5558  *
5559  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5560  * described in Table 111 of SAT-2 (Draft 9).
5561  */
5562 static  int
5563 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5564 {
5565 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5566 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5567 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5568 	int extend;
5569 	uint64_t lba;
5570 	uint16_t feature, sec_count;
5571 	int t_len, synch;
5572 	int rval, reason;
5573 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5574 
5575 	mutex_enter(cport_mutex);
5576 
5577 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5578 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5579 		mutex_exit(cport_mutex);
5580 		return (rval);
5581 	}
5582 
5583 	/* T_DIR bit */
5584 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5585 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5586 	else
5587 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5588 
5589 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5590 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5591 		mutex_exit(cport_mutex);
5592 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5593 	}
5594 
5595 	/* OFFLINE field. If non-zero, invalid command (for now). */
5596 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5597 		mutex_exit(cport_mutex);
5598 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5599 	}
5600 
5601 	/* PROTOCOL field */
5602 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5603 	case SATL_APT_P_HW_RESET:
5604 	case SATL_APT_P_SRST:
5605 	case SATL_APT_P_DMA:
5606 	case SATL_APT_P_DMA_QUEUED:
5607 	case SATL_APT_P_DEV_DIAG:
5608 	case SATL_APT_P_DEV_RESET:
5609 	case SATL_APT_P_UDMA_IN:
5610 	case SATL_APT_P_UDMA_OUT:
5611 	case SATL_APT_P_FPDMA:
5612 	case SATL_APT_P_RET_RESP:
5613 		/* Not yet implemented */
5614 	default:
5615 		mutex_exit(cport_mutex);
5616 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5617 
5618 	case SATL_APT_P_NON_DATA:
5619 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5620 		break;
5621 
5622 	case SATL_APT_P_PIO_DATA_IN:
5623 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5624 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5625 			mutex_exit(cport_mutex);
5626 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5627 		}
5628 
5629 		/* if there is a buffer, release its DMA resources */
5630 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5631 			sata_scsi_dmafree(NULL, scsipkt);
5632 		} else {
5633 			/* if there is no buffer, how do you PIO in? */
5634 			mutex_exit(cport_mutex);
5635 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5636 		}
5637 
5638 		break;
5639 
5640 	case SATL_APT_P_PIO_DATA_OUT:
5641 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5642 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5643 			mutex_exit(cport_mutex);
5644 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5645 		}
5646 
5647 		/* if there is a buffer, release its DMA resources */
5648 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5649 			sata_scsi_dmafree(NULL, scsipkt);
5650 		} else {
5651 			/* if there is no buffer, how do you PIO out? */
5652 			mutex_exit(cport_mutex);
5653 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5654 		}
5655 
5656 		break;
5657 	}
5658 
5659 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5660 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5661 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5662 		feature = scsipkt->pkt_cdbp[3];
5663 
5664 		sec_count = scsipkt->pkt_cdbp[4];
5665 
5666 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5667 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5668 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5669 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5670 
5671 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5672 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5673 
5674 		break;
5675 
5676 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5677 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5678 			extend = 1;
5679 
5680 			feature = scsipkt->pkt_cdbp[3];
5681 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5682 
5683 			sec_count = scsipkt->pkt_cdbp[5];
5684 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5685 
5686 			lba = scsipkt->pkt_cdbp[11];
5687 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5688 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5689 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5690 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5691 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5692 
5693 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5694 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5695 		} else {
5696 			feature = scsipkt->pkt_cdbp[3];
5697 
5698 			sec_count = scsipkt->pkt_cdbp[5];
5699 
5700 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5701 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5702 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5703 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5704 
5705 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5706 			    0xf0;
5707 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5708 		}
5709 
5710 		break;
5711 	}
5712 
5713 	/* CK_COND bit */
5714 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5715 		if (extend) {
5716 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5717 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5718 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5719 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5720 		}
5721 
5722 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5723 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5724 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5725 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5726 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5727 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5728 	}
5729 
5730 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5731 	if (extend) {
5732 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5733 
5734 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5735 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5736 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5737 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5738 		scmd->satacmd_lba_high_msb = lba >> 40;
5739 	} else {
5740 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5741 
5742 		scmd->satacmd_features_reg_ext = 0;
5743 		scmd->satacmd_sec_count_msb = 0;
5744 		scmd->satacmd_lba_low_msb = 0;
5745 		scmd->satacmd_lba_mid_msb = 0;
5746 		scmd->satacmd_lba_high_msb = 0;
5747 	}
5748 
5749 	scmd->satacmd_features_reg = feature & 0xff;
5750 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5751 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5752 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5753 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5754 
5755 	/* Determine transfer length */
5756 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5757 	case 1:
5758 		t_len = feature;
5759 		break;
5760 	case 2:
5761 		t_len = sec_count;
5762 		break;
5763 	default:
5764 		t_len = 0;
5765 		break;
5766 	}
5767 
5768 	/* Adjust transfer length for the Byte Block bit */
5769 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5770 		t_len *= SATA_DISK_SECTOR_SIZE;
5771 
5772 	/* Start processing command */
5773 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5774 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5775 		synch = FALSE;
5776 	} else {
5777 		synch = TRUE;
5778 	}
5779 
5780 	if (sata_hba_start(spx, &rval) != 0) {
5781 		mutex_exit(cport_mutex);
5782 		return (rval);
5783 	}
5784 
5785 	mutex_exit(cport_mutex);
5786 
5787 	if (synch) {
5788 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5789 	}
5790 
5791 	return (TRAN_ACCEPT);
5792 }
5793 
5794 /*
5795  * Translate command: Log Sense
5796  */
5797 static 	int
5798 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5799 {
5800 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5801 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5802 	sata_drive_info_t *sdinfo;
5803 	struct scsi_extended_sense *sense;
5804 	int 		len, count, alc_len;
5805 	int		pc;	/* Page Control code */
5806 	int		page_code;	/* Page code */
5807 	uint8_t		*buf;	/* log sense buffer */
5808 	int		rval, reason;
5809 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5810 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5811 
5812 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5813 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5814 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5815 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5816 
5817 	if (servicing_interrupt()) {
5818 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5819 		if (buf == NULL) {
5820 			return (TRAN_BUSY);
5821 		}
5822 	} else {
5823 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5824 	}
5825 
5826 	mutex_enter(cport_mutex);
5827 
5828 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5829 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5830 		mutex_exit(cport_mutex);
5831 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5832 		return (rval);
5833 	}
5834 
5835 	scsipkt->pkt_reason = CMD_CMPLT;
5836 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5837 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5838 
5839 	pc = scsipkt->pkt_cdbp[2] >> 6;
5840 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5841 
5842 	/* Reject not supported request for all but cumulative values */
5843 	switch (pc) {
5844 	case PC_CUMULATIVE_VALUES:
5845 		break;
5846 	default:
5847 		*scsipkt->pkt_scbp = STATUS_CHECK;
5848 		sense = sata_arq_sense(spx);
5849 		sense->es_key = KEY_ILLEGAL_REQUEST;
5850 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5851 		goto done;
5852 	}
5853 
5854 	switch (page_code) {
5855 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5856 	case PAGE_CODE_SELF_TEST_RESULTS:
5857 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5858 	case PAGE_CODE_SMART_READ_DATA:
5859 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5860 		break;
5861 	default:
5862 		*scsipkt->pkt_scbp = STATUS_CHECK;
5863 		sense = sata_arq_sense(spx);
5864 		sense->es_key = KEY_ILLEGAL_REQUEST;
5865 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5866 		goto done;
5867 	}
5868 
5869 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5870 		/*
5871 		 * Because log sense uses local buffers for data retrieval from
5872 		 * the devices and sets the data programatically in the
5873 		 * original specified buffer, release preallocated DMA
5874 		 * resources before storing data in the original buffer,
5875 		 * so no unwanted DMA sync would take place.
5876 		 */
5877 		sata_id_t *sata_id;
5878 
5879 		sata_scsi_dmafree(NULL, scsipkt);
5880 
5881 		len = 0;
5882 
5883 		/* Build log parameter header */
5884 		buf[len++] = page_code;	/* page code as in the CDB */
5885 		buf[len++] = 0;		/* reserved */
5886 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5887 		buf[len++] = 0;		/* (LSB) */
5888 
5889 		sdinfo = sata_get_device_info(
5890 		    spx->txlt_sata_hba_inst,
5891 		    &spx->txlt_sata_pkt->satapkt_device);
5892 
5893 		/*
5894 		 * Add requested pages.
5895 		 */
5896 		switch (page_code) {
5897 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5898 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5899 			break;
5900 		case PAGE_CODE_SELF_TEST_RESULTS:
5901 			sata_id = &sdinfo->satadrv_id;
5902 			if ((! (sata_id->ai_cmdset84 &
5903 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5904 			    (! (sata_id->ai_features87 &
5905 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5906 				*scsipkt->pkt_scbp = STATUS_CHECK;
5907 				sense = sata_arq_sense(spx);
5908 				sense->es_key = KEY_ILLEGAL_REQUEST;
5909 				sense->es_add_code =
5910 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5911 
5912 				goto done;
5913 			}
5914 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5915 			    spx->txlt_sata_hba_inst);
5916 			break;
5917 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5918 			sata_id = &sdinfo->satadrv_id;
5919 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5920 				*scsipkt->pkt_scbp = STATUS_CHECK;
5921 				sense = sata_arq_sense(spx);
5922 				sense->es_key = KEY_ILLEGAL_REQUEST;
5923 				sense->es_add_code =
5924 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5925 
5926 				goto done;
5927 			}
5928 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5929 				*scsipkt->pkt_scbp = STATUS_CHECK;
5930 				sense = sata_arq_sense(spx);
5931 				sense->es_key = KEY_ABORTED_COMMAND;
5932 				sense->es_add_code =
5933 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5934 				sense->es_qual_code =
5935 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5936 
5937 				goto done;
5938 			}
5939 
5940 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5941 			    spx->txlt_sata_hba_inst);
5942 			break;
5943 		case PAGE_CODE_SMART_READ_DATA:
5944 			sata_id = &sdinfo->satadrv_id;
5945 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5946 				*scsipkt->pkt_scbp = STATUS_CHECK;
5947 				sense = sata_arq_sense(spx);
5948 				sense->es_key = KEY_ILLEGAL_REQUEST;
5949 				sense->es_add_code =
5950 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5951 
5952 				goto done;
5953 			}
5954 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5955 				*scsipkt->pkt_scbp = STATUS_CHECK;
5956 				sense = sata_arq_sense(spx);
5957 				sense->es_key = KEY_ABORTED_COMMAND;
5958 				sense->es_add_code =
5959 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5960 				sense->es_qual_code =
5961 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5962 
5963 				goto done;
5964 			}
5965 
5966 			/* This page doesn't include a page header */
5967 			len = sata_build_lsense_page_30(sdinfo, buf,
5968 			    spx->txlt_sata_hba_inst);
5969 			goto no_header;
5970 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5971 			sata_id = &sdinfo->satadrv_id;
5972 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5973 				*scsipkt->pkt_scbp = STATUS_CHECK;
5974 				sense = sata_arq_sense(spx);
5975 				sense->es_key = KEY_ILLEGAL_REQUEST;
5976 				sense->es_add_code =
5977 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5978 
5979 				goto done;
5980 			}
5981 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5982 				*scsipkt->pkt_scbp = STATUS_CHECK;
5983 				sense = sata_arq_sense(spx);
5984 				sense->es_key = KEY_ABORTED_COMMAND;
5985 				sense->es_add_code =
5986 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5987 				sense->es_qual_code =
5988 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5989 
5990 				goto done;
5991 			}
5992 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5993 			goto no_header;
5994 		default:
5995 			/* Invalid request */
5996 			*scsipkt->pkt_scbp = STATUS_CHECK;
5997 			sense = sata_arq_sense(spx);
5998 			sense->es_key = KEY_ILLEGAL_REQUEST;
5999 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6000 			goto done;
6001 		}
6002 
6003 		/* set parameter log sense data length */
6004 		buf[2] = len >> 8;	/* log sense length (MSB) */
6005 		buf[3] = len & 0xff;	/* log sense length (LSB) */
6006 
6007 		len += SCSI_LOG_PAGE_HDR_LEN;
6008 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6009 
6010 no_header:
6011 		/* Check allocation length */
6012 		alc_len = scsipkt->pkt_cdbp[7];
6013 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6014 
6015 		/*
6016 		 * We do not check for possible parameters truncation
6017 		 * (alc_len < len) assuming that the target driver works
6018 		 * correctly. Just avoiding overrun.
6019 		 * Copy no more than requested and possible, buffer-wise.
6020 		 */
6021 		count = MIN(alc_len, len);
6022 		count = MIN(bp->b_bcount, count);
6023 		bcopy(buf, bp->b_un.b_addr, count);
6024 
6025 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
6026 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6027 	}
6028 	*scsipkt->pkt_scbp = STATUS_GOOD;
6029 done:
6030 	mutex_exit(cport_mutex);
6031 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6032 
6033 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6034 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6035 
6036 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6037 	    scsipkt->pkt_comp != NULL) {
6038 		/* scsi callback required */
6039 		if (servicing_interrupt()) {
6040 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6041 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6042 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6043 				return (TRAN_BUSY);
6044 			}
6045 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6046 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6047 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6048 			/* Scheduling the callback failed */
6049 			return (TRAN_BUSY);
6050 		}
6051 	}
6052 
6053 	return (TRAN_ACCEPT);
6054 }
6055 
6056 /*
6057  * Translate command: Log Select
6058  * Not implemented at this time - returns invalid command response.
6059  */
6060 static	int
6061 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6062 {
6063 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6064 	    "sata_txlt_log_select\n", NULL);
6065 
6066 	return (sata_txlt_invalid_command(spx));
6067 }
6068 
6069 
6070 /*
6071  * Translate command: Read (various types).
6072  * Translated into appropriate type of ATA READ command
6073  * for SATA hard disks.
6074  * Both the device capabilities and requested operation mode are
6075  * considered.
6076  *
6077  * Following scsi cdb fields are ignored:
6078  * rdprotect, dpo, fua, fua_nv, group_number.
6079  *
6080  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6081  * enable variable sata_func_enable), the capability of the controller and
6082  * capability of a device are checked and if both support queueing, read
6083  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6084  * command rather than plain READ_XXX command.
6085  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6086  * both the controller and device suport such functionality, the read
6087  * request will be translated to READ_FPDMA_QUEUED command.
6088  * In both cases the maximum queue depth is derived as minimum of:
6089  * HBA capability,device capability and sata_max_queue_depth variable setting.
6090  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6091  * used to pass max queue depth value, and the maximum possible queue depth
6092  * is 32.
6093  *
6094  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6095  * appropriate values in scsi_pkt fields.
6096  */
6097 static int
6098 sata_txlt_read(sata_pkt_txlate_t *spx)
6099 {
6100 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6101 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6102 	sata_drive_info_t *sdinfo;
6103 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6104 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6105 	uint16_t sec_count;
6106 	uint64_t lba;
6107 	int rval, reason;
6108 	int synch;
6109 
6110 	mutex_enter(cport_mutex);
6111 
6112 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6113 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6114 		mutex_exit(cport_mutex);
6115 		return (rval);
6116 	}
6117 
6118 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6119 	    &spx->txlt_sata_pkt->satapkt_device);
6120 
6121 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6122 	/*
6123 	 * Extract LBA and sector count from scsi CDB.
6124 	 */
6125 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6126 	case SCMD_READ:
6127 		/* 6-byte scsi read cmd : 0x08 */
6128 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6129 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6130 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6131 		sec_count = scsipkt->pkt_cdbp[4];
6132 		/* sec_count 0 will be interpreted as 256 by a device */
6133 		break;
6134 	case SCMD_READ_G1:
6135 		/* 10-bytes scsi read command : 0x28 */
6136 		lba = scsipkt->pkt_cdbp[2];
6137 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6138 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6139 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6140 		sec_count = scsipkt->pkt_cdbp[7];
6141 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6142 		break;
6143 	case SCMD_READ_G5:
6144 		/* 12-bytes scsi read command : 0xA8 */
6145 		lba = scsipkt->pkt_cdbp[2];
6146 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6147 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6148 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6149 		sec_count = scsipkt->pkt_cdbp[6];
6150 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6151 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6152 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6153 		break;
6154 	case SCMD_READ_G4:
6155 		/* 16-bytes scsi read command : 0x88 */
6156 		lba = scsipkt->pkt_cdbp[2];
6157 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6158 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6159 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6160 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6161 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6162 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6163 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6164 		sec_count = scsipkt->pkt_cdbp[10];
6165 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6166 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6167 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6168 		break;
6169 	default:
6170 		/* Unsupported command */
6171 		mutex_exit(cport_mutex);
6172 		return (sata_txlt_invalid_command(spx));
6173 	}
6174 
6175 	/*
6176 	 * Check if specified address exceeds device capacity
6177 	 */
6178 	if ((lba >= sdinfo->satadrv_capacity) ||
6179 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6180 		/* LBA out of range */
6181 		mutex_exit(cport_mutex);
6182 		return (sata_txlt_lba_out_of_range(spx));
6183 	}
6184 
6185 	/*
6186 	 * For zero-length transfer, emulate good completion of the command
6187 	 * (reasons for rejecting the command were already checked).
6188 	 * No DMA resources were allocated.
6189 	 */
6190 	if (spx->txlt_dma_cookie_list == NULL) {
6191 		mutex_exit(cport_mutex);
6192 		return (sata_emul_rw_completion(spx));
6193 	}
6194 
6195 	/*
6196 	 * Build cmd block depending on the device capability and
6197 	 * requested operation mode.
6198 	 * Do not bother with non-dma mode - we are working only with
6199 	 * devices supporting DMA.
6200 	 */
6201 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6202 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6203 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6204 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6205 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6206 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6207 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6208 #ifndef __lock_lint
6209 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6210 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6211 		scmd->satacmd_lba_high_msb = lba >> 40;
6212 #endif
6213 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6214 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6215 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6216 	}
6217 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6218 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6219 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6220 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6221 	scmd->satacmd_features_reg = 0;
6222 	scmd->satacmd_status_reg = 0;
6223 	scmd->satacmd_error_reg = 0;
6224 
6225 	/*
6226 	 * Check if queueing commands should be used and switch
6227 	 * to appropriate command if possible
6228 	 */
6229 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6230 		boolean_t using_queuing;
6231 
6232 		/* Queuing supported by controller and device? */
6233 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6234 		    (sdinfo->satadrv_features_support &
6235 		    SATA_DEV_F_NCQ) &&
6236 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6237 		    SATA_CTLF_NCQ)) {
6238 			using_queuing = B_TRUE;
6239 
6240 			/* NCQ supported - use FPDMA READ */
6241 			scmd->satacmd_cmd_reg =
6242 			    SATAC_READ_FPDMA_QUEUED;
6243 			scmd->satacmd_features_reg_ext =
6244 			    scmd->satacmd_sec_count_msb;
6245 			scmd->satacmd_sec_count_msb = 0;
6246 		} else if ((sdinfo->satadrv_features_support &
6247 		    SATA_DEV_F_TCQ) &&
6248 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6249 		    SATA_CTLF_QCMD)) {
6250 			using_queuing = B_TRUE;
6251 
6252 			/* Legacy queueing */
6253 			if (sdinfo->satadrv_features_support &
6254 			    SATA_DEV_F_LBA48) {
6255 				scmd->satacmd_cmd_reg =
6256 				    SATAC_READ_DMA_QUEUED_EXT;
6257 				scmd->satacmd_features_reg_ext =
6258 				    scmd->satacmd_sec_count_msb;
6259 				scmd->satacmd_sec_count_msb = 0;
6260 			} else {
6261 				scmd->satacmd_cmd_reg =
6262 				    SATAC_READ_DMA_QUEUED;
6263 			}
6264 		} else	/* NCQ nor legacy queuing not supported */
6265 			using_queuing = B_FALSE;
6266 
6267 		/*
6268 		 * If queuing, the sector count goes in the features register
6269 		 * and the secount count will contain the tag.
6270 		 */
6271 		if (using_queuing) {
6272 			scmd->satacmd_features_reg =
6273 			    scmd->satacmd_sec_count_lsb;
6274 			scmd->satacmd_sec_count_lsb = 0;
6275 			scmd->satacmd_flags.sata_queued = B_TRUE;
6276 
6277 			/* Set-up maximum queue depth */
6278 			scmd->satacmd_flags.sata_max_queue_depth =
6279 			    sdinfo->satadrv_max_queue_depth - 1;
6280 		} else if (sdinfo->satadrv_features_enabled &
6281 		    SATA_DEV_F_E_UNTAGGED_QING) {
6282 			/*
6283 			 * Although NCQ/TCQ is not enabled, untagged queuing
6284 			 * may be still used.
6285 			 * Set-up the maximum untagged queue depth.
6286 			 * Use controller's queue depth from sata_hba_tran.
6287 			 * SATA HBA drivers may ignore this value and rely on
6288 			 * the internal limits.For drivers that do not
6289 			 * ignore untaged queue depth, limit the value to
6290 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6291 			 * largest value that can be passed via
6292 			 * satacmd_flags.sata_max_queue_depth.
6293 			 */
6294 			scmd->satacmd_flags.sata_max_queue_depth =
6295 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6296 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6297 
6298 		} else {
6299 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6300 		}
6301 	} else
6302 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6303 
6304 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6305 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6306 	    scmd->satacmd_cmd_reg, lba, sec_count);
6307 
6308 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6309 		/* Need callback function */
6310 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6311 		synch = FALSE;
6312 	} else
6313 		synch = TRUE;
6314 
6315 	/* Transfer command to HBA */
6316 	if (sata_hba_start(spx, &rval) != 0) {
6317 		/* Pkt not accepted for execution */
6318 		mutex_exit(cport_mutex);
6319 		return (rval);
6320 	}
6321 	mutex_exit(cport_mutex);
6322 	/*
6323 	 * If execution is non-synchronous,
6324 	 * a callback function will handle potential errors, translate
6325 	 * the response and will do a callback to a target driver.
6326 	 * If it was synchronous, check execution status using the same
6327 	 * framework callback.
6328 	 */
6329 	if (synch) {
6330 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6331 		    "synchronous execution status %x\n",
6332 		    spx->txlt_sata_pkt->satapkt_reason);
6333 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6334 	}
6335 	return (TRAN_ACCEPT);
6336 }
6337 
6338 
6339 /*
6340  * SATA translate command: Write (various types)
6341  * Translated into appropriate type of ATA WRITE command
6342  * for SATA hard disks.
6343  * Both the device capabilities and requested operation mode are
6344  * considered.
6345  *
6346  * Following scsi cdb fields are ignored:
6347  * rwprotect, dpo, fua, fua_nv, group_number.
6348  *
6349  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6350  * enable variable sata_func_enable), the capability of the controller and
6351  * capability of a device are checked and if both support queueing, write
6352  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6353  * command rather than plain WRITE_XXX command.
6354  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6355  * both the controller and device suport such functionality, the write
6356  * request will be translated to WRITE_FPDMA_QUEUED command.
6357  * In both cases the maximum queue depth is derived as minimum of:
6358  * HBA capability,device capability and sata_max_queue_depth variable setting.
6359  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6360  * used to pass max queue depth value, and the maximum possible queue depth
6361  * is 32.
6362  *
6363  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6364  * appropriate values in scsi_pkt fields.
6365  */
6366 static int
6367 sata_txlt_write(sata_pkt_txlate_t *spx)
6368 {
6369 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6370 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6371 	sata_drive_info_t *sdinfo;
6372 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6373 	uint16_t sec_count;
6374 	uint64_t lba;
6375 	int rval, reason;
6376 	int synch;
6377 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6378 
6379 	mutex_enter(cport_mutex);
6380 
6381 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6382 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6383 		mutex_exit(cport_mutex);
6384 		return (rval);
6385 	}
6386 
6387 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6388 	    &spx->txlt_sata_pkt->satapkt_device);
6389 
6390 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6391 	/*
6392 	 * Extract LBA and sector count from scsi CDB
6393 	 */
6394 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6395 	case SCMD_WRITE:
6396 		/* 6-byte scsi read cmd : 0x0A */
6397 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6398 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6399 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6400 		sec_count = scsipkt->pkt_cdbp[4];
6401 		/* sec_count 0 will be interpreted as 256 by a device */
6402 		break;
6403 	case SCMD_WRITE_G1:
6404 		/* 10-bytes scsi write command : 0x2A */
6405 		lba = scsipkt->pkt_cdbp[2];
6406 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6407 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6408 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6409 		sec_count = scsipkt->pkt_cdbp[7];
6410 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6411 		break;
6412 	case SCMD_WRITE_G5:
6413 		/* 12-bytes scsi read command : 0xAA */
6414 		lba = scsipkt->pkt_cdbp[2];
6415 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6416 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6417 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6418 		sec_count = scsipkt->pkt_cdbp[6];
6419 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6420 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6421 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6422 		break;
6423 	case SCMD_WRITE_G4:
6424 		/* 16-bytes scsi write command : 0x8A */
6425 		lba = scsipkt->pkt_cdbp[2];
6426 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6427 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6428 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6429 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6430 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6431 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6432 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6433 		sec_count = scsipkt->pkt_cdbp[10];
6434 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6435 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6436 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6437 		break;
6438 	default:
6439 		/* Unsupported command */
6440 		mutex_exit(cport_mutex);
6441 		return (sata_txlt_invalid_command(spx));
6442 	}
6443 
6444 	/*
6445 	 * Check if specified address and length exceeds device capacity
6446 	 */
6447 	if ((lba >= sdinfo->satadrv_capacity) ||
6448 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6449 		/* LBA out of range */
6450 		mutex_exit(cport_mutex);
6451 		return (sata_txlt_lba_out_of_range(spx));
6452 	}
6453 
6454 	/*
6455 	 * For zero-length transfer, emulate good completion of the command
6456 	 * (reasons for rejecting the command were already checked).
6457 	 * No DMA resources were allocated.
6458 	 */
6459 	if (spx->txlt_dma_cookie_list == NULL) {
6460 		mutex_exit(cport_mutex);
6461 		return (sata_emul_rw_completion(spx));
6462 	}
6463 
6464 	/*
6465 	 * Build cmd block depending on the device capability and
6466 	 * requested operation mode.
6467 	 * Do not bother with non-dma mode- we are working only with
6468 	 * devices supporting DMA.
6469 	 */
6470 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6471 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6472 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6473 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6474 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6475 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6476 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6477 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6478 #ifndef __lock_lint
6479 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6480 		scmd->satacmd_lba_high_msb = lba >> 40;
6481 #endif
6482 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6483 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6484 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6485 	}
6486 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6487 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6488 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6489 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6490 	scmd->satacmd_features_reg = 0;
6491 	scmd->satacmd_status_reg = 0;
6492 	scmd->satacmd_error_reg = 0;
6493 
6494 	/*
6495 	 * Check if queueing commands should be used and switch
6496 	 * to appropriate command if possible
6497 	 */
6498 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6499 		boolean_t using_queuing;
6500 
6501 		/* Queuing supported by controller and device? */
6502 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6503 		    (sdinfo->satadrv_features_support &
6504 		    SATA_DEV_F_NCQ) &&
6505 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6506 		    SATA_CTLF_NCQ)) {
6507 			using_queuing = B_TRUE;
6508 
6509 			/* NCQ supported - use FPDMA WRITE */
6510 			scmd->satacmd_cmd_reg =
6511 			    SATAC_WRITE_FPDMA_QUEUED;
6512 			scmd->satacmd_features_reg_ext =
6513 			    scmd->satacmd_sec_count_msb;
6514 			scmd->satacmd_sec_count_msb = 0;
6515 		} else if ((sdinfo->satadrv_features_support &
6516 		    SATA_DEV_F_TCQ) &&
6517 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6518 		    SATA_CTLF_QCMD)) {
6519 			using_queuing = B_TRUE;
6520 
6521 			/* Legacy queueing */
6522 			if (sdinfo->satadrv_features_support &
6523 			    SATA_DEV_F_LBA48) {
6524 				scmd->satacmd_cmd_reg =
6525 				    SATAC_WRITE_DMA_QUEUED_EXT;
6526 				scmd->satacmd_features_reg_ext =
6527 				    scmd->satacmd_sec_count_msb;
6528 				scmd->satacmd_sec_count_msb = 0;
6529 			} else {
6530 				scmd->satacmd_cmd_reg =
6531 				    SATAC_WRITE_DMA_QUEUED;
6532 			}
6533 		} else	/*  NCQ nor legacy queuing not supported */
6534 			using_queuing = B_FALSE;
6535 
6536 		if (using_queuing) {
6537 			scmd->satacmd_features_reg =
6538 			    scmd->satacmd_sec_count_lsb;
6539 			scmd->satacmd_sec_count_lsb = 0;
6540 			scmd->satacmd_flags.sata_queued = B_TRUE;
6541 			/* Set-up maximum queue depth */
6542 			scmd->satacmd_flags.sata_max_queue_depth =
6543 			    sdinfo->satadrv_max_queue_depth - 1;
6544 		} else if (sdinfo->satadrv_features_enabled &
6545 		    SATA_DEV_F_E_UNTAGGED_QING) {
6546 			/*
6547 			 * Although NCQ/TCQ is not enabled, untagged queuing
6548 			 * may be still used.
6549 			 * Set-up the maximum untagged queue depth.
6550 			 * Use controller's queue depth from sata_hba_tran.
6551 			 * SATA HBA drivers may ignore this value and rely on
6552 			 * the internal limits. For drivera that do not
6553 			 * ignore untaged queue depth, limit the value to
6554 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6555 			 * largest value that can be passed via
6556 			 * satacmd_flags.sata_max_queue_depth.
6557 			 */
6558 			scmd->satacmd_flags.sata_max_queue_depth =
6559 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6560 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6561 
6562 		} else {
6563 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6564 		}
6565 	} else
6566 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6567 
6568 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6569 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6570 	    scmd->satacmd_cmd_reg, lba, sec_count);
6571 
6572 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6573 		/* Need callback function */
6574 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6575 		synch = FALSE;
6576 	} else
6577 		synch = TRUE;
6578 
6579 	/* Transfer command to HBA */
6580 	if (sata_hba_start(spx, &rval) != 0) {
6581 		/* Pkt not accepted for execution */
6582 		mutex_exit(cport_mutex);
6583 		return (rval);
6584 	}
6585 	mutex_exit(cport_mutex);
6586 
6587 	/*
6588 	 * If execution is non-synchronous,
6589 	 * a callback function will handle potential errors, translate
6590 	 * the response and will do a callback to a target driver.
6591 	 * If it was synchronous, check execution status using the same
6592 	 * framework callback.
6593 	 */
6594 	if (synch) {
6595 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6596 		    "synchronous execution status %x\n",
6597 		    spx->txlt_sata_pkt->satapkt_reason);
6598 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6599 	}
6600 	return (TRAN_ACCEPT);
6601 }
6602 
6603 
6604 /*
6605  * Implements SCSI SBC WRITE BUFFER command download microcode option
6606  */
6607 static int
6608 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6609 {
6610 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6611 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6612 
6613 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6614 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6615 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6616 
6617 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6618 	struct scsi_extended_sense *sense;
6619 	int rval, mode, sector_count, reason;
6620 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6621 
6622 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6623 
6624 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6625 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6626 
6627 	mutex_enter(cport_mutex);
6628 
6629 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6630 	    TRAN_ACCEPT) {
6631 		mutex_exit(cport_mutex);
6632 		return (rval);
6633 	}
6634 
6635 	/* Use synchronous mode */
6636 	spx->txlt_sata_pkt->satapkt_op_mode
6637 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6638 
6639 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6640 
6641 	scsipkt->pkt_reason = CMD_CMPLT;
6642 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6643 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6644 
6645 	/*
6646 	 * The SCSI to ATA translation specification only calls
6647 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6648 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6649 	 * ATA 8 (draft) got rid of download microcode for temp
6650 	 * and it is even optional for ATA 7, so it may be aborted.
6651 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6652 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6653 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6654 	 * sectors.  Thus the offset really doesn't buy us anything.
6655 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6656 	 * is revised, this can be revisisted.
6657 	 */
6658 	/* Reject not supported request */
6659 	switch (mode) {
6660 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6661 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6662 		break;
6663 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6664 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6665 		break;
6666 	default:
6667 		goto bad_param;
6668 	}
6669 
6670 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6671 
6672 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6673 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6674 		goto bad_param;
6675 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6676 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6677 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6678 	scmd->satacmd_lba_mid_lsb = 0;
6679 	scmd->satacmd_lba_high_lsb = 0;
6680 	scmd->satacmd_device_reg = 0;
6681 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6682 	scmd->satacmd_addr_type = 0;
6683 
6684 	/* Transfer command to HBA */
6685 	if (sata_hba_start(spx, &rval) != 0) {
6686 		/* Pkt not accepted for execution */
6687 		mutex_exit(cport_mutex);
6688 		return (rval);
6689 	}
6690 
6691 	mutex_exit(cport_mutex);
6692 
6693 	/* Then we need synchronous check the status of the disk */
6694 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6695 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6696 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6697 		scsipkt->pkt_reason = CMD_CMPLT;
6698 
6699 		/* Download commmand succeed, so probe and identify device */
6700 		sata_reidentify_device(spx);
6701 	} else {
6702 		/* Something went wrong, microcode download command failed */
6703 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6704 		*scsipkt->pkt_scbp = STATUS_CHECK;
6705 		sense = sata_arq_sense(spx);
6706 		switch (sata_pkt->satapkt_reason) {
6707 		case SATA_PKT_PORT_ERROR:
6708 			/*
6709 			 * We have no device data. Assume no data transfered.
6710 			 */
6711 			sense->es_key = KEY_HARDWARE_ERROR;
6712 			break;
6713 
6714 		case SATA_PKT_DEV_ERROR:
6715 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6716 			    SATA_STATUS_ERR) {
6717 				/*
6718 				 * determine dev error reason from error
6719 				 * reg content
6720 				 */
6721 				sata_decode_device_error(spx, sense);
6722 				break;
6723 			}
6724 			/* No extended sense key - no info available */
6725 			break;
6726 
6727 		case SATA_PKT_TIMEOUT:
6728 			scsipkt->pkt_reason = CMD_TIMEOUT;
6729 			scsipkt->pkt_statistics |=
6730 			    STAT_TIMEOUT | STAT_DEV_RESET;
6731 			/* No extended sense key ? */
6732 			break;
6733 
6734 		case SATA_PKT_ABORTED:
6735 			scsipkt->pkt_reason = CMD_ABORTED;
6736 			scsipkt->pkt_statistics |= STAT_ABORTED;
6737 			/* No extended sense key ? */
6738 			break;
6739 
6740 		case SATA_PKT_RESET:
6741 			/* pkt aborted by an explicit reset from a host */
6742 			scsipkt->pkt_reason = CMD_RESET;
6743 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6744 			break;
6745 
6746 		default:
6747 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6748 			    "sata_txlt_nodata_cmd_completion: "
6749 			    "invalid packet completion reason %d",
6750 			    sata_pkt->satapkt_reason));
6751 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6752 			break;
6753 		}
6754 
6755 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6756 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6757 
6758 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6759 			/* scsi callback required */
6760 			scsi_hba_pkt_comp(scsipkt);
6761 	}
6762 	return (TRAN_ACCEPT);
6763 
6764 bad_param:
6765 	mutex_exit(cport_mutex);
6766 	*scsipkt->pkt_scbp = STATUS_CHECK;
6767 	sense = sata_arq_sense(spx);
6768 	sense->es_key = KEY_ILLEGAL_REQUEST;
6769 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6770 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6771 	    scsipkt->pkt_comp != NULL) {
6772 		/* scsi callback required */
6773 		if (servicing_interrupt()) {
6774 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6775 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6776 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6777 				return (TRAN_BUSY);
6778 			}
6779 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6780 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6781 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6782 			/* Scheduling the callback failed */
6783 			return (TRAN_BUSY);
6784 		}
6785 	}
6786 	return (rval);
6787 }
6788 
6789 /*
6790  * Re-identify device after doing a firmware download.
6791  */
6792 static void
6793 sata_reidentify_device(sata_pkt_txlate_t *spx)
6794 {
6795 #define	DOWNLOAD_WAIT_TIME_SECS	60
6796 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6797 	int rval;
6798 	int retry_cnt;
6799 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6800 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6801 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6802 	sata_drive_info_t *sdinfo;
6803 
6804 	/*
6805 	 * Before returning good status, probe device.
6806 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6807 	 * The assumption is that the new microcode is applied by the
6808 	 * device. It is a caller responsibility to verify this.
6809 	 */
6810 	for (retry_cnt = 0;
6811 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6812 	    retry_cnt++) {
6813 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6814 
6815 		if (rval == SATA_SUCCESS) { /* Set default features */
6816 			sdinfo = sata_get_device_info(sata_hba_inst,
6817 			    &sata_device);
6818 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6819 			    SATA_SUCCESS) {
6820 				/* retry */
6821 				rval = sata_initialize_device(sata_hba_inst,
6822 				    sdinfo);
6823 				if (rval == SATA_RETRY)
6824 					sata_log(sata_hba_inst, CE_WARN,
6825 					    "SATA device at port %d pmport %d -"
6826 					    " default device features could not"
6827 					    " be set. Device may not operate "
6828 					    "as expected.",
6829 					    sata_device.satadev_addr.cport,
6830 					    sata_device.satadev_addr.pmport);
6831 			}
6832 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6833 				scsi_hba_pkt_comp(scsipkt);
6834 			return;
6835 		} else if (rval == SATA_RETRY) {
6836 			delay(drv_usectohz(1000000 *
6837 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6838 			continue;
6839 		} else	/* failed - no reason to retry */
6840 			break;
6841 	}
6842 
6843 	/*
6844 	 * Something went wrong, device probing failed.
6845 	 */
6846 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6847 	    "Cannot probe device after downloading microcode\n"));
6848 
6849 	/* Reset device to force retrying the probe. */
6850 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6851 	    (SATA_DIP(sata_hba_inst), &sata_device);
6852 
6853 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6854 		scsi_hba_pkt_comp(scsipkt);
6855 }
6856 
6857 
6858 /*
6859  * Translate command: Synchronize Cache.
6860  * Translates into Flush Cache command for SATA hard disks.
6861  *
6862  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6863  * appropriate values in scsi_pkt fields.
6864  */
6865 static 	int
6866 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6867 {
6868 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6869 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6870 	int rval, reason;
6871 	int synch;
6872 
6873 	mutex_enter(cport_mutex);
6874 
6875 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6876 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6877 		mutex_exit(cport_mutex);
6878 		return (rval);
6879 	}
6880 
6881 	scmd->satacmd_addr_type = 0;
6882 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6883 	scmd->satacmd_device_reg = 0;
6884 	scmd->satacmd_sec_count_lsb = 0;
6885 	scmd->satacmd_lba_low_lsb = 0;
6886 	scmd->satacmd_lba_mid_lsb = 0;
6887 	scmd->satacmd_lba_high_lsb = 0;
6888 	scmd->satacmd_features_reg = 0;
6889 	scmd->satacmd_status_reg = 0;
6890 	scmd->satacmd_error_reg = 0;
6891 
6892 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6893 	    "sata_txlt_synchronize_cache\n", NULL);
6894 
6895 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6896 		/* Need to set-up a callback function */
6897 		spx->txlt_sata_pkt->satapkt_comp =
6898 		    sata_txlt_nodata_cmd_completion;
6899 		synch = FALSE;
6900 	} else
6901 		synch = TRUE;
6902 
6903 	/* Transfer command to HBA */
6904 	if (sata_hba_start(spx, &rval) != 0) {
6905 		/* Pkt not accepted for execution */
6906 		mutex_exit(cport_mutex);
6907 		return (rval);
6908 	}
6909 	mutex_exit(cport_mutex);
6910 
6911 	/*
6912 	 * If execution non-synchronous, it had to be completed
6913 	 * a callback function will handle potential errors, translate
6914 	 * the response and will do a callback to a target driver.
6915 	 * If it was synchronous, check status, using the same
6916 	 * framework callback.
6917 	 */
6918 	if (synch) {
6919 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6920 		    "synchronous execution status %x\n",
6921 		    spx->txlt_sata_pkt->satapkt_reason);
6922 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6923 	}
6924 	return (TRAN_ACCEPT);
6925 }
6926 
6927 
6928 /*
6929  * Send pkt to SATA HBA driver
6930  *
6931  * This function may be called only if the operation is requested by scsi_pkt,
6932  * i.e. scsi_pkt is not NULL.
6933  *
6934  * This function has to be called with cport mutex held. It does release
6935  * the mutex when it calls HBA driver sata_tran_start function and
6936  * re-acquires it afterwards.
6937  *
6938  * If return value is 0, pkt was accepted, -1 otherwise
6939  * rval is set to appropriate sata_scsi_start return value.
6940  *
6941  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6942  * have called the sata_pkt callback function for this packet.
6943  *
6944  * The scsi callback has to be performed by the caller of this routine.
6945  */
6946 static int
6947 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6948 {
6949 	int stat;
6950 	uint8_t cport = SATA_TXLT_CPORT(spx);
6951 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6952 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6953 	sata_drive_info_t *sdinfo;
6954 	sata_pmult_info_t *pminfo;
6955 	sata_pmport_info_t *pmportinfo = NULL;
6956 	sata_device_t *sata_device = NULL;
6957 	uint8_t cmd;
6958 	struct sata_cmd_flags cmd_flags;
6959 
6960 	ASSERT(spx->txlt_sata_pkt != NULL);
6961 
6962 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6963 
6964 	sdinfo = sata_get_device_info(sata_hba_inst,
6965 	    &spx->txlt_sata_pkt->satapkt_device);
6966 	ASSERT(sdinfo != NULL);
6967 
6968 	/* Clear device reset state? */
6969 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6970 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6971 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6972 
6973 		/*
6974 		 * Get the pmult_info of the its parent port multiplier, all
6975 		 * sub-devices share a common device reset flags on in
6976 		 * pmult_info.
6977 		 */
6978 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6979 		pmportinfo = pminfo->pmult_dev_port[pmport];
6980 		ASSERT(pminfo != NULL);
6981 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6982 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6983 			    sata_clear_dev_reset = B_TRUE;
6984 			pminfo->pmult_event_flags &=
6985 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6986 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6987 			    "sata_hba_start: clearing device reset state"
6988 			    "on pmult.\n", NULL);
6989 		}
6990 	} else {
6991 		if (sdinfo->satadrv_event_flags &
6992 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6993 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6994 			    sata_clear_dev_reset = B_TRUE;
6995 			sdinfo->satadrv_event_flags &=
6996 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6997 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6998 			    "sata_hba_start: clearing device reset state\n",
6999 			    NULL);
7000 		}
7001 	}
7002 
7003 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7004 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7005 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
7006 
7007 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7008 
7009 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7010 	    "Sata cmd 0x%2x\n", cmd);
7011 
7012 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7013 	    spx->txlt_sata_pkt);
7014 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7015 	/*
7016 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7017 	 * with the sata callback, the sata_pkt could be already destroyed
7018 	 * by the time we check ther return status from the hba_start()
7019 	 * function, because sata_scsi_destroy_pkt() could have been already
7020 	 * called (perhaps in the interrupt context). So, in such case, there
7021 	 * should be no references to it. In other cases, sata_pkt still
7022 	 * exists.
7023 	 */
7024 	if (stat == SATA_TRAN_ACCEPTED) {
7025 		/*
7026 		 * pkt accepted for execution.
7027 		 * If it was executed synchronously, it is already completed
7028 		 * and pkt completion_reason indicates completion status.
7029 		 */
7030 		*rval = TRAN_ACCEPT;
7031 		return (0);
7032 	}
7033 
7034 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7035 	switch (stat) {
7036 	case SATA_TRAN_QUEUE_FULL:
7037 		/*
7038 		 * Controller detected queue full condition.
7039 		 */
7040 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7041 		    "sata_hba_start: queue full\n", NULL);
7042 
7043 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7044 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7045 
7046 		*rval = TRAN_BUSY;
7047 		break;
7048 
7049 	case SATA_TRAN_PORT_ERROR:
7050 		/*
7051 		 * Communication/link with device or general port error
7052 		 * detected before pkt execution begun.
7053 		 */
7054 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7055 		    SATA_ADDR_CPORT ||
7056 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7057 		    SATA_ADDR_DCPORT)
7058 			sata_log(sata_hba_inst, CE_CONT,
7059 			    "SATA port %d error",
7060 			    sata_device->satadev_addr.cport);
7061 		else
7062 			sata_log(sata_hba_inst, CE_CONT,
7063 			    "SATA port %d:%d error\n",
7064 			    sata_device->satadev_addr.cport,
7065 			    sata_device->satadev_addr.pmport);
7066 
7067 		/*
7068 		 * Update the port/device structure.
7069 		 * sata_pkt should be still valid. Since port error is
7070 		 * returned, sata_device content should reflect port
7071 		 * state - it means, that sata address have been changed,
7072 		 * because original packet's sata address refered to a device
7073 		 * attached to some port.
7074 		 */
7075 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7076 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7077 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7078 			mutex_enter(&pmportinfo->pmport_mutex);
7079 			sata_update_pmport_info(sata_hba_inst, sata_device);
7080 			mutex_exit(&pmportinfo->pmport_mutex);
7081 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7082 		} else {
7083 			sata_update_port_info(sata_hba_inst, sata_device);
7084 		}
7085 
7086 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7087 		*rval = TRAN_FATAL_ERROR;
7088 		break;
7089 
7090 	case SATA_TRAN_CMD_UNSUPPORTED:
7091 		/*
7092 		 * Command rejected by HBA as unsupported. It was HBA driver
7093 		 * that rejected the command, command was not sent to
7094 		 * an attached device.
7095 		 */
7096 		if ((sdinfo != NULL) &&
7097 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7098 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7099 			    "sat_hba_start: cmd 0x%2x rejected "
7100 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7101 
7102 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7103 		(void) sata_txlt_invalid_command(spx);
7104 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7105 
7106 		*rval = TRAN_ACCEPT;
7107 		break;
7108 
7109 	case SATA_TRAN_BUSY:
7110 		/*
7111 		 * Command rejected by HBA because other operation prevents
7112 		 * accepting the packet, or device is in RESET condition.
7113 		 */
7114 		if (sdinfo != NULL) {
7115 			sdinfo->satadrv_state =
7116 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7117 
7118 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7119 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7120 				    "sata_hba_start: cmd 0x%2x rejected "
7121 				    "because of device reset condition\n",
7122 				    cmd);
7123 			} else {
7124 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7125 				    "sata_hba_start: cmd 0x%2x rejected "
7126 				    "with SATA_TRAN_BUSY status\n",
7127 				    cmd);
7128 			}
7129 		}
7130 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7131 		*rval = TRAN_BUSY;
7132 		break;
7133 
7134 	default:
7135 		/* Unrecognized HBA response */
7136 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7137 		    "sata_hba_start: unrecognized HBA response "
7138 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7139 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7140 		*rval = TRAN_FATAL_ERROR;
7141 		break;
7142 	}
7143 
7144 	/*
7145 	 * If we got here, the packet was rejected.
7146 	 * Check if we need to remember reset state clearing request
7147 	 */
7148 	if (cmd_flags.sata_clear_dev_reset) {
7149 		/*
7150 		 * Check if device is still configured - it may have
7151 		 * disapeared from the configuration
7152 		 */
7153 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7154 		if (sdinfo != NULL) {
7155 			/*
7156 			 * Restore the flag that requests clearing of
7157 			 * the device reset state,
7158 			 * so the next sata packet may carry it to HBA.
7159 			 */
7160 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7161 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7162 				pminfo->pmult_event_flags |=
7163 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7164 			} else {
7165 				sdinfo->satadrv_event_flags |=
7166 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7167 			}
7168 		}
7169 	}
7170 	return (-1);
7171 }
7172 
7173 /*
7174  * Scsi response setup for invalid LBA
7175  *
7176  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7177  */
7178 static int
7179 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7180 {
7181 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7182 	struct scsi_extended_sense *sense;
7183 
7184 	scsipkt->pkt_reason = CMD_CMPLT;
7185 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7186 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7187 	*scsipkt->pkt_scbp = STATUS_CHECK;
7188 
7189 	*scsipkt->pkt_scbp = STATUS_CHECK;
7190 	sense = sata_arq_sense(spx);
7191 	sense->es_key = KEY_ILLEGAL_REQUEST;
7192 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7193 
7194 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7195 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7196 
7197 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7198 	    scsipkt->pkt_comp != NULL) {
7199 		/* scsi callback required */
7200 		if (servicing_interrupt()) {
7201 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7202 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7203 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7204 				return (TRAN_BUSY);
7205 			}
7206 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7207 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7208 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7209 			/* Scheduling the callback failed */
7210 			return (TRAN_BUSY);
7211 		}
7212 	}
7213 	return (TRAN_ACCEPT);
7214 }
7215 
7216 
7217 /*
7218  * Analyze device status and error registers and translate them into
7219  * appropriate scsi sense codes.
7220  * NOTE: non-packet commands only for now
7221  */
7222 static void
7223 sata_decode_device_error(sata_pkt_txlate_t *spx,
7224     struct scsi_extended_sense *sense)
7225 {
7226 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7227 
7228 	ASSERT(sense != NULL);
7229 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7230 	    SATA_STATUS_ERR);
7231 
7232 
7233 	if (err_reg & SATA_ERROR_ICRC) {
7234 		sense->es_key = KEY_ABORTED_COMMAND;
7235 		sense->es_add_code = 0x08; /* Communication failure */
7236 		return;
7237 	}
7238 
7239 	if (err_reg & SATA_ERROR_UNC) {
7240 		sense->es_key = KEY_MEDIUM_ERROR;
7241 		/* Information bytes (LBA) need to be set by a caller */
7242 		return;
7243 	}
7244 
7245 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7246 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7247 		sense->es_key = KEY_UNIT_ATTENTION;
7248 		sense->es_add_code = 0x3a; /* No media present */
7249 		return;
7250 	}
7251 
7252 	if (err_reg & SATA_ERROR_IDNF) {
7253 		if (err_reg & SATA_ERROR_ABORT) {
7254 			sense->es_key = KEY_ABORTED_COMMAND;
7255 		} else {
7256 			sense->es_key = KEY_ILLEGAL_REQUEST;
7257 			sense->es_add_code = 0x21; /* LBA out of range */
7258 		}
7259 		return;
7260 	}
7261 
7262 	if (err_reg & SATA_ERROR_ABORT) {
7263 		ASSERT(spx->txlt_sata_pkt != NULL);
7264 		sense->es_key = KEY_ABORTED_COMMAND;
7265 		return;
7266 	}
7267 }
7268 
7269 /*
7270  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7271  */
7272 static void
7273 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7274 {
7275 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7276 
7277 	*lba = 0;
7278 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7279 		*lba = sata_cmd->satacmd_lba_high_msb;
7280 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7281 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7282 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7283 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7284 	}
7285 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7286 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7287 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7288 }
7289 
7290 /*
7291  * This is fixed sense format - if LBA exceeds the info field size,
7292  * no valid info will be returned (valid bit in extended sense will
7293  * be set to 0).
7294  */
7295 static struct scsi_extended_sense *
7296 sata_arq_sense(sata_pkt_txlate_t *spx)
7297 {
7298 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7299 	struct scsi_arq_status *arqs;
7300 	struct scsi_extended_sense *sense;
7301 
7302 	/* Fill ARQ sense data */
7303 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7304 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7305 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7306 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7307 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7308 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7309 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7310 	arqs->sts_rqpkt_resid = 0;
7311 	sense = &arqs->sts_sensedata;
7312 	bzero(sense, sizeof (struct scsi_extended_sense));
7313 	sata_fixed_sense_data_preset(sense);
7314 	return (sense);
7315 }
7316 
7317 /*
7318  * ATA Pass Through support
7319  * Sets flags indicating that an invalid value was found in some
7320  * field in the command.  It could be something illegal according to
7321  * the SAT-2 spec or it could be a feature that is not (yet?)
7322  * supported.
7323  */
7324 static int
7325 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7326 {
7327 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7328 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7329 
7330 	scsipkt->pkt_reason = CMD_CMPLT;
7331 	*scsipkt->pkt_scbp = STATUS_CHECK;
7332 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7333 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7334 
7335 	sense = sata_arq_sense(spx);
7336 	sense->es_key = KEY_ILLEGAL_REQUEST;
7337 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7338 
7339 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7340 	    scsipkt->pkt_comp != NULL) {
7341 		/* scsi callback required */
7342 		if (servicing_interrupt()) {
7343 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7344 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7345 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7346 				return (TRAN_BUSY);
7347 			}
7348 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7349 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7350 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7351 			/* Scheduling the callback failed */
7352 			return (TRAN_BUSY);
7353 		}
7354 	}
7355 
7356 	return (TRAN_ACCEPT);
7357 }
7358 
7359 /*
7360  * The UNMAP command considers it not to be an error if the parameter length
7361  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7362  * to do so just complete the command.
7363  */
7364 static int
7365 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7366 {
7367 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7368 
7369 	scsipkt->pkt_reason = CMD_CMPLT;
7370 	*scsipkt->pkt_scbp = STATUS_GOOD;
7371 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7372 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7373 
7374 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7375 	    scsipkt->pkt_comp != NULL) {
7376 		/* scsi callback required */
7377 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7378 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7379 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7380 			/* Scheduling the callback failed */
7381 			return (TRAN_BUSY);
7382 		}
7383 	}
7384 
7385 	return (TRAN_ACCEPT);
7386 }
7387 
7388 /*
7389  * Emulated SATA Read/Write command completion for zero-length requests.
7390  * This request always succedes, so in synchronous mode it always returns
7391  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7392  * callback cannot be scheduled.
7393  */
7394 static int
7395 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7396 {
7397 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7398 
7399 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7400 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7401 	scsipkt->pkt_reason = CMD_CMPLT;
7402 	*scsipkt->pkt_scbp = STATUS_GOOD;
7403 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7404 		/* scsi callback required - have to schedule it */
7405 		if (servicing_interrupt()) {
7406 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7407 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7408 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7409 				return (TRAN_BUSY);
7410 			}
7411 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7412 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7413 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7414 			/* Scheduling the callback failed */
7415 			return (TRAN_BUSY);
7416 		}
7417 	}
7418 	return (TRAN_ACCEPT);
7419 }
7420 
7421 
7422 /*
7423  * Translate completion status of SATA read/write commands into scsi response.
7424  * pkt completion_reason is checked to determine the completion status.
7425  * Do scsi callback if necessary.
7426  *
7427  * Note: this function may be called also for synchronously executed
7428  * commands.
7429  * This function may be used only if scsi_pkt is non-NULL.
7430  */
7431 static void
7432 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7433 {
7434 	sata_pkt_txlate_t *spx =
7435 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7436 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7437 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7438 	struct scsi_extended_sense *sense;
7439 	uint64_t lba;
7440 	struct buf *bp;
7441 	int rval;
7442 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7443 		/* Normal completion */
7444 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7445 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7446 		scsipkt->pkt_reason = CMD_CMPLT;
7447 		*scsipkt->pkt_scbp = STATUS_GOOD;
7448 		if (spx->txlt_tmp_buf != NULL) {
7449 			/* Temporary buffer was used */
7450 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7451 			if (bp->b_flags & B_READ) {
7452 				rval = ddi_dma_sync(
7453 				    spx->txlt_buf_dma_handle, 0, 0,
7454 				    DDI_DMA_SYNC_FORCPU);
7455 				ASSERT(rval == DDI_SUCCESS);
7456 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7457 				    bp->b_bcount);
7458 			}
7459 		}
7460 	} else {
7461 		/*
7462 		 * Something went wrong - analyze return
7463 		 */
7464 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7465 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7466 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7467 		*scsipkt->pkt_scbp = STATUS_CHECK;
7468 		sense = sata_arq_sense(spx);
7469 		ASSERT(sense != NULL);
7470 
7471 		/*
7472 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7473 		 * extract from device registers the failing LBA.
7474 		 */
7475 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7476 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7477 			    (scmd->satacmd_lba_mid_msb != 0 ||
7478 			    scmd->satacmd_lba_high_msb != 0)) {
7479 				/*
7480 				 * We have problem reporting this cmd LBA
7481 				 * in fixed sense data format, because of
7482 				 * the size of the scsi LBA fields.
7483 				 */
7484 				sense->es_valid = 0;
7485 			} else {
7486 				sata_extract_error_lba(spx, &lba);
7487 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7488 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7489 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7490 				sense->es_info_4 = lba & 0xFF;
7491 			}
7492 		} else {
7493 			/* Invalid extended sense info */
7494 			sense->es_valid = 0;
7495 		}
7496 
7497 		switch (sata_pkt->satapkt_reason) {
7498 		case SATA_PKT_PORT_ERROR:
7499 			/* We may want to handle DEV GONE state as well */
7500 			/*
7501 			 * We have no device data. Assume no data transfered.
7502 			 */
7503 			sense->es_key = KEY_HARDWARE_ERROR;
7504 			break;
7505 
7506 		case SATA_PKT_DEV_ERROR:
7507 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7508 			    SATA_STATUS_ERR) {
7509 				/*
7510 				 * determine dev error reason from error
7511 				 * reg content
7512 				 */
7513 				sata_decode_device_error(spx, sense);
7514 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7515 					switch (scmd->satacmd_cmd_reg) {
7516 					case SATAC_READ_DMA:
7517 					case SATAC_READ_DMA_EXT:
7518 					case SATAC_READ_DMA_QUEUED:
7519 					case SATAC_READ_DMA_QUEUED_EXT:
7520 					case SATAC_READ_FPDMA_QUEUED:
7521 						/* Unrecovered read error */
7522 						sense->es_add_code =
7523 						    SD_SCSI_ASC_UNREC_READ_ERR;
7524 						break;
7525 					case SATAC_WRITE_DMA:
7526 					case SATAC_WRITE_DMA_EXT:
7527 					case SATAC_WRITE_DMA_QUEUED:
7528 					case SATAC_WRITE_DMA_QUEUED_EXT:
7529 					case SATAC_WRITE_FPDMA_QUEUED:
7530 						/* Write error */
7531 						sense->es_add_code =
7532 						    SD_SCSI_ASC_WRITE_ERR;
7533 						break;
7534 					default:
7535 						/* Internal error */
7536 						SATA_LOG_D((
7537 						    spx->txlt_sata_hba_inst,
7538 						    CE_WARN,
7539 						    "sata_txlt_rw_completion :"
7540 						    "internal error - invalid "
7541 						    "command 0x%2x",
7542 						    scmd->satacmd_cmd_reg));
7543 						break;
7544 					}
7545 				}
7546 				break;
7547 			}
7548 			/* No extended sense key - no info available */
7549 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7550 			break;
7551 
7552 		case SATA_PKT_TIMEOUT:
7553 			scsipkt->pkt_reason = CMD_TIMEOUT;
7554 			scsipkt->pkt_statistics |=
7555 			    STAT_TIMEOUT | STAT_DEV_RESET;
7556 			sense->es_key = KEY_ABORTED_COMMAND;
7557 			break;
7558 
7559 		case SATA_PKT_ABORTED:
7560 			scsipkt->pkt_reason = CMD_ABORTED;
7561 			scsipkt->pkt_statistics |= STAT_ABORTED;
7562 			sense->es_key = KEY_ABORTED_COMMAND;
7563 			break;
7564 
7565 		case SATA_PKT_RESET:
7566 			scsipkt->pkt_reason = CMD_RESET;
7567 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7568 			sense->es_key = KEY_ABORTED_COMMAND;
7569 			break;
7570 
7571 		default:
7572 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7573 			    "sata_txlt_rw_completion: "
7574 			    "invalid packet completion reason"));
7575 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7576 			break;
7577 		}
7578 	}
7579 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7580 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7581 
7582 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7583 		/* scsi callback required */
7584 		scsi_hba_pkt_comp(scsipkt);
7585 }
7586 
7587 
7588 /*
7589  * Translate completion status of non-data commands (i.e. commands returning
7590  * no data).
7591  * pkt completion_reason is checked to determine the completion status.
7592  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7593  *
7594  * Note: this function may be called also for synchronously executed
7595  * commands.
7596  * This function may be used only if scsi_pkt is non-NULL.
7597  */
7598 
7599 static	void
7600 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7601 {
7602 	sata_pkt_txlate_t *spx =
7603 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7604 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7605 
7606 	sata_set_arq_data(sata_pkt);
7607 
7608 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7609 		/* scsi callback required */
7610 		scsi_hba_pkt_comp(scsipkt);
7611 }
7612 
7613 /*
7614  * Completion handler for ATA Pass Through command
7615  */
7616 static void
7617 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7618 {
7619 	sata_pkt_txlate_t *spx =
7620 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7621 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7622 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7623 	struct buf *bp;
7624 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7625 
7626 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7627 		/* Normal completion */
7628 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7629 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7630 		scsipkt->pkt_reason = CMD_CMPLT;
7631 		*scsipkt->pkt_scbp = STATUS_GOOD;
7632 
7633 		/*
7634 		 * If the command has CK_COND set
7635 		 */
7636 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7637 			*scsipkt->pkt_scbp = STATUS_CHECK;
7638 			sata_fill_ata_return_desc(sata_pkt,
7639 			    KEY_RECOVERABLE_ERROR,
7640 			    SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7641 		}
7642 
7643 		if (spx->txlt_tmp_buf != NULL) {
7644 			/* Temporary buffer was used */
7645 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7646 			if (bp->b_flags & B_READ) {
7647 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7648 				    bp->b_bcount);
7649 			}
7650 		}
7651 	} else {
7652 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7653 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7654 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7655 		*scsipkt->pkt_scbp = STATUS_CHECK;
7656 
7657 		/*
7658 		 * If DF or ERR was set, the HBA should have copied out the
7659 		 * status and error registers to the satacmd structure.
7660 		 */
7661 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7662 			sense_key = KEY_HARDWARE_ERROR;
7663 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7664 			addl_sense_qual = 0;
7665 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7666 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7667 				sense_key = KEY_NOT_READY;
7668 				addl_sense_code =
7669 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7670 				addl_sense_qual = 0;
7671 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7672 				sense_key = KEY_MEDIUM_ERROR;
7673 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7674 				addl_sense_qual = 0;
7675 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7676 				sense_key = KEY_DATA_PROTECT;
7677 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7678 				addl_sense_qual = 0;
7679 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7680 				sense_key = KEY_ILLEGAL_REQUEST;
7681 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7682 				addl_sense_qual = 0;
7683 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7684 				sense_key = KEY_ABORTED_COMMAND;
7685 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7686 				addl_sense_qual = 0;
7687 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7688 				sense_key = KEY_UNIT_ATTENTION;
7689 				addl_sense_code =
7690 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7691 				addl_sense_qual = 0;
7692 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7693 				sense_key = KEY_UNIT_ATTENTION;
7694 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7695 				addl_sense_qual = 0;
7696 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7697 				sense_key = KEY_ABORTED_COMMAND;
7698 				addl_sense_code =
7699 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7700 				addl_sense_qual = 0;
7701 			}
7702 		}
7703 
7704 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7705 		    addl_sense_qual);
7706 	}
7707 
7708 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7709 		/* scsi callback required */
7710 		scsi_hba_pkt_comp(scsipkt);
7711 }
7712 
7713 /*
7714  * Completion handler for unmap translation command
7715  */
7716 static void
7717 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7718 {
7719 	sata_pkt_txlate_t *spx =
7720 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7721 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7722 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7723 	struct buf *bp;
7724 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7725 
7726 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7727 		/* Normal completion */
7728 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7729 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7730 		scsipkt->pkt_reason = CMD_CMPLT;
7731 		*scsipkt->pkt_scbp = STATUS_GOOD;
7732 
7733 		if (spx->txlt_tmp_buf != NULL) {
7734 			/* Temporary buffer was used */
7735 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7736 			if (bp->b_flags & B_READ) {
7737 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7738 				    bp->b_bcount);
7739 			}
7740 		}
7741 	} else {
7742 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7743 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7744 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7745 		*scsipkt->pkt_scbp = STATUS_CHECK;
7746 
7747 		/*
7748 		 * If DF or ERR was set, the HBA should have copied out the
7749 		 * status and error registers to the satacmd structure.
7750 		 */
7751 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7752 			sense_key = KEY_HARDWARE_ERROR;
7753 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7754 			addl_sense_qual = 0;
7755 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7756 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7757 				sense_key = KEY_NOT_READY;
7758 				addl_sense_code =
7759 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7760 				addl_sense_qual = 0;
7761 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7762 				sense_key = KEY_MEDIUM_ERROR;
7763 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7764 				addl_sense_qual = 0;
7765 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7766 				sense_key = KEY_DATA_PROTECT;
7767 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7768 				addl_sense_qual = 0;
7769 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7770 				sense_key = KEY_ILLEGAL_REQUEST;
7771 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7772 				addl_sense_qual = 0;
7773 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7774 				sense_key = KEY_ABORTED_COMMAND;
7775 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7776 				addl_sense_qual = 0;
7777 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7778 				sense_key = KEY_UNIT_ATTENTION;
7779 				addl_sense_code =
7780 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7781 				addl_sense_qual = 0;
7782 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7783 				sense_key = KEY_UNIT_ATTENTION;
7784 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7785 				addl_sense_qual = 0;
7786 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7787 				sense_key = KEY_ABORTED_COMMAND;
7788 				addl_sense_code =
7789 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7790 				addl_sense_qual = 0;
7791 			}
7792 		}
7793 
7794 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7795 		    addl_sense_qual);
7796 	}
7797 
7798 	sata_free_local_buffer(spx);
7799 
7800 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7801 		/* scsi callback required */
7802 		scsi_hba_pkt_comp(scsipkt);
7803 }
7804 
7805 /*
7806  *
7807  */
7808 static void
7809 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7810     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7811 {
7812 	sata_pkt_txlate_t *spx =
7813 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7814 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7815 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7816 	struct sata_apt_sense_data *apt_sd =
7817 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7818 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7819 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7820 	    &(apt_sd->apt_sd_sense);
7821 	int extend = 0;
7822 
7823 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7824 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7825 		extend = 1;
7826 
7827 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7828 
7829 	/* update the residual count */
7830 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7831 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7832 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7833 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7834 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7835 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7836 	    sizeof (struct sata_apt_sense_data);
7837 
7838 	/*
7839 	 * Fill in the Descriptor sense header
7840 	 */
7841 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7842 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7843 	sds->ds_class = CLASS_EXTENDED_SENSE;
7844 	sds->ds_key = sense_key & 0xf;
7845 	sds->ds_add_code = addl_sense_code;
7846 	sds->ds_qual_code = addl_sense_qual;
7847 	sds->ds_addl_sense_length =
7848 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7849 
7850 	/*
7851 	 * Fill in the ATA Return descriptor sense data
7852 	 */
7853 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7854 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7855 	ata_ret_desc->ars_addl_length = 0xc;
7856 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7857 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7858 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7859 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7860 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7861 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7862 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7863 
7864 	if (extend == 1) {
7865 		ata_ret_desc->ars_extend = 1;
7866 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7867 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7868 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7869 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7870 	} else {
7871 		ata_ret_desc->ars_extend = 0;
7872 		ata_ret_desc->ars_sec_count_msb = 0;
7873 		ata_ret_desc->ars_lba_low_msb = 0;
7874 		ata_ret_desc->ars_lba_mid_msb = 0;
7875 		ata_ret_desc->ars_lba_high_msb = 0;
7876 	}
7877 }
7878 
7879 static	void
7880 sata_set_arq_data(sata_pkt_t *sata_pkt)
7881 {
7882 	sata_pkt_txlate_t *spx =
7883 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7884 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7885 	struct scsi_extended_sense *sense;
7886 
7887 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7888 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7889 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7890 		/* Normal completion */
7891 		scsipkt->pkt_reason = CMD_CMPLT;
7892 		*scsipkt->pkt_scbp = STATUS_GOOD;
7893 	} else {
7894 		/* Something went wrong */
7895 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7896 		*scsipkt->pkt_scbp = STATUS_CHECK;
7897 		sense = sata_arq_sense(spx);
7898 		switch (sata_pkt->satapkt_reason) {
7899 		case SATA_PKT_PORT_ERROR:
7900 			/*
7901 			 * We have no device data. Assume no data transfered.
7902 			 */
7903 			sense->es_key = KEY_HARDWARE_ERROR;
7904 			break;
7905 
7906 		case SATA_PKT_DEV_ERROR:
7907 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7908 			    SATA_STATUS_ERR) {
7909 				/*
7910 				 * determine dev error reason from error
7911 				 * reg content
7912 				 */
7913 				sata_decode_device_error(spx, sense);
7914 				break;
7915 			}
7916 			/* No extended sense key - no info available */
7917 			break;
7918 
7919 		case SATA_PKT_TIMEOUT:
7920 			scsipkt->pkt_reason = CMD_TIMEOUT;
7921 			scsipkt->pkt_statistics |=
7922 			    STAT_TIMEOUT | STAT_DEV_RESET;
7923 			/* No extended sense key ? */
7924 			break;
7925 
7926 		case SATA_PKT_ABORTED:
7927 			scsipkt->pkt_reason = CMD_ABORTED;
7928 			scsipkt->pkt_statistics |= STAT_ABORTED;
7929 			/* No extended sense key ? */
7930 			break;
7931 
7932 		case SATA_PKT_RESET:
7933 			/* pkt aborted by an explicit reset from a host */
7934 			scsipkt->pkt_reason = CMD_RESET;
7935 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7936 			break;
7937 
7938 		default:
7939 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7940 			    "sata_txlt_nodata_cmd_completion: "
7941 			    "invalid packet completion reason %d",
7942 			    sata_pkt->satapkt_reason));
7943 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7944 			break;
7945 		}
7946 
7947 	}
7948 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7949 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7950 }
7951 
7952 
7953 /*
7954  * Build Mode sense R/W recovery page
7955  * NOT IMPLEMENTED
7956  */
7957 
7958 static int
7959 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7960 {
7961 #ifndef __lock_lint
7962 	_NOTE(ARGUNUSED(sdinfo))
7963 	_NOTE(ARGUNUSED(pcntrl))
7964 	_NOTE(ARGUNUSED(buf))
7965 #endif
7966 	return (0);
7967 }
7968 
7969 /*
7970  * Build Mode sense caching page  -  scsi-3 implementation.
7971  * Page length distinguishes previous format from scsi-3 format.
7972  * buf must have space for 0x12 bytes.
7973  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7974  *
7975  */
7976 static int
7977 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7978 {
7979 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7980 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7981 
7982 	/*
7983 	 * Most of the fields are set to 0, being not supported and/or disabled
7984 	 */
7985 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7986 
7987 	/* Saved paramters not supported */
7988 	if (pcntrl == 3)
7989 		return (0);
7990 	if (pcntrl == 0 || pcntrl == 2) {
7991 		/*
7992 		 * For now treat current and default parameters as same
7993 		 * That may have to change, if target driver will complain
7994 		 */
7995 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
7996 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7997 
7998 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7999 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8000 			page->dra = 1;		/* Read Ahead disabled */
8001 			page->rcd = 1;		/* Read Cache disabled */
8002 		}
8003 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8004 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
8005 			page->wce = 1;		/* Write Cache enabled */
8006 	} else {
8007 		/* Changeable parameters */
8008 		page->mode_page.code = MODEPAGE_CACHING;
8009 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8010 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8011 			page->dra = 1;
8012 			page->rcd = 1;
8013 		}
8014 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8015 			page->wce = 1;
8016 	}
8017 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8018 	    sizeof (struct mode_page));
8019 }
8020 
8021 /*
8022  * Build Mode sense exception cntrl page
8023  */
8024 static int
8025 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8026 {
8027 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8028 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8029 
8030 	/*
8031 	 * Most of the fields are set to 0, being not supported and/or disabled
8032 	 */
8033 	bzero(buf, PAGELENGTH_INFO_EXCPT);
8034 
8035 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
8036 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8037 
8038 	/* Indicate that this is page is saveable */
8039 	page->mode_page.ps = 1;
8040 
8041 	/*
8042 	 * We will return the same data for default, current and saved page.
8043 	 * The only changeable bit is dexcpt and that bit is required
8044 	 * by the ATA specification to be preserved across power cycles.
8045 	 */
8046 	if (pcntrl != 1) {
8047 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8048 		page->mrie = MRIE_ONLY_ON_REQUEST;
8049 	}
8050 	else
8051 		page->dexcpt = 1;	/* Only changeable parameter */
8052 
8053 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8054 }
8055 
8056 
8057 static int
8058 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8059 {
8060 	struct mode_acoustic_management *page =
8061 	    (struct mode_acoustic_management *)buf;
8062 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8063 
8064 	/*
8065 	 * Most of the fields are set to 0, being not supported and/or disabled
8066 	 */
8067 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8068 
8069 	switch (pcntrl) {
8070 	case P_CNTRL_DEFAULT:
8071 		/*  default paramters not supported */
8072 		return (0);
8073 
8074 	case P_CNTRL_CURRENT:
8075 	case P_CNTRL_SAVED:
8076 		/* Saved and current are supported and are identical */
8077 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8078 		page->mode_page.length =
8079 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8080 		page->mode_page.ps = 1;
8081 
8082 		/* Word 83 indicates if feature is supported */
8083 		/* If feature is not supported */
8084 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8085 			page->acoustic_manag_enable =
8086 			    ACOUSTIC_DISABLED;
8087 		} else {
8088 			page->acoustic_manag_enable =
8089 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8090 			    != 0);
8091 			/* Word 94 inidicates the value */
8092 #ifdef	_LITTLE_ENDIAN
8093 			page->acoustic_manag_level =
8094 			    (uchar_t)sata_id->ai_acoustic;
8095 			page->vendor_recommended_value =
8096 			    sata_id->ai_acoustic >> 8;
8097 #else
8098 			page->acoustic_manag_level =
8099 			    sata_id->ai_acoustic >> 8;
8100 			page->vendor_recommended_value =
8101 			    (uchar_t)sata_id->ai_acoustic;
8102 #endif
8103 		}
8104 		break;
8105 
8106 	case P_CNTRL_CHANGEABLE:
8107 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8108 		page->mode_page.length =
8109 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8110 		page->mode_page.ps = 1;
8111 
8112 		/* Word 83 indicates if the feature is supported */
8113 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8114 			page->acoustic_manag_enable =
8115 			    ACOUSTIC_ENABLED;
8116 			page->acoustic_manag_level = 0xff;
8117 		}
8118 		break;
8119 	}
8120 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8121 	    sizeof (struct mode_page));
8122 }
8123 
8124 
8125 /*
8126  * Build Mode sense power condition page.
8127  */
8128 static int
8129 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8130 {
8131 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8132 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8133 
8134 	/*
8135 	 * Most of the fields are set to 0, being not supported and/or disabled
8136 	 * power condition page length was 0x0a
8137 	 */
8138 	bzero(buf, sizeof (struct mode_info_power_cond));
8139 
8140 	if (pcntrl == P_CNTRL_DEFAULT) {
8141 		/*  default paramters not supported */
8142 		return (0);
8143 	}
8144 
8145 	page->mode_page.code = MODEPAGE_POWER_COND;
8146 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8147 
8148 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8149 		page->standby = 1;
8150 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8151 		    sizeof (uchar_t) * 4);
8152 	}
8153 
8154 	return (sizeof (struct mode_info_power_cond));
8155 }
8156 
8157 /*
8158  * Process mode select caching page 8 (scsi3 format only).
8159  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8160  * if these features are supported by the device. If these features are not
8161  * supported, the command will be terminated with STATUS_CHECK.
8162  * This function fails only if the SET FEATURE command sent to
8163  * the device fails. The page format is not verified, assuming that the
8164  * target driver operates correctly - if parameters length is too short,
8165  * we just drop the page.
8166  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8167  * setting have to be changed.
8168  * SET FEATURE command is executed synchronously, i.e. we wait here until
8169  * it is completed, regardless of the scsi pkt directives.
8170  *
8171  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8172  * changing DRA will change RCD.
8173  *
8174  * More than one SATA command may be executed to perform operations specified
8175  * by mode select pages. The first error terminates further execution.
8176  * Operations performed successully are not backed-up in such case.
8177  *
8178  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8179  * If operation resulted in changing device setup, dmod flag should be set to
8180  * one (1). If parameters were not changed, dmod flag should be set to 0.
8181  * Upon return, if operation required sending command to the device, the rval
8182  * should be set to the value returned by sata_hba_start. If operation
8183  * did not require device access, rval should be set to TRAN_ACCEPT.
8184  * The pagelen should be set to the length of the page.
8185  *
8186  * This function has to be called with a port mutex held.
8187  *
8188  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8189  */
8190 int
8191 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8192     int parmlen, int *pagelen, int *rval, int *dmod)
8193 {
8194 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8195 	sata_drive_info_t *sdinfo;
8196 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8197 	sata_id_t *sata_id;
8198 	struct scsi_extended_sense *sense;
8199 	int wce, dra;	/* Current settings */
8200 
8201 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8202 	    &spx->txlt_sata_pkt->satapkt_device);
8203 	sata_id = &sdinfo->satadrv_id;
8204 	*dmod = 0;
8205 
8206 	/* Verify parameters length. If too short, drop it */
8207 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8208 	    sizeof (struct mode_page)) > parmlen) {
8209 		*scsipkt->pkt_scbp = STATUS_CHECK;
8210 		sense = sata_arq_sense(spx);
8211 		sense->es_key = KEY_ILLEGAL_REQUEST;
8212 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8213 		*pagelen = parmlen;
8214 		*rval = TRAN_ACCEPT;
8215 		return (SATA_FAILURE);
8216 	}
8217 
8218 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8219 
8220 	/* Current setting of Read Ahead (and Read Cache) */
8221 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8222 		dra = 0;	/* 0 == not disabled */
8223 	else
8224 		dra = 1;
8225 	/* Current setting of Write Cache */
8226 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8227 		wce = 1;
8228 	else
8229 		wce = 0;
8230 
8231 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8232 		/* nothing to do */
8233 		*rval = TRAN_ACCEPT;
8234 		return (SATA_SUCCESS);
8235 	}
8236 
8237 	/*
8238 	 * Need to flip some setting
8239 	 * Set-up Internal SET FEATURES command(s)
8240 	 */
8241 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8242 	scmd->satacmd_addr_type = 0;
8243 	scmd->satacmd_device_reg = 0;
8244 	scmd->satacmd_status_reg = 0;
8245 	scmd->satacmd_error_reg = 0;
8246 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8247 	if (page->dra != dra || page->rcd != dra) {
8248 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8249 			/* Need to flip read ahead setting */
8250 			if (dra == 0)
8251 				/* Disable read ahead / read cache */
8252 				scmd->satacmd_features_reg =
8253 				    SATAC_SF_DISABLE_READ_AHEAD;
8254 			else
8255 				/* Enable read ahead  / read cache */
8256 				scmd->satacmd_features_reg =
8257 				    SATAC_SF_ENABLE_READ_AHEAD;
8258 
8259 			/* Transfer command to HBA */
8260 			if (sata_hba_start(spx, rval) != 0)
8261 				/*
8262 				 * Pkt not accepted for execution.
8263 				 */
8264 				return (SATA_FAILURE);
8265 
8266 			*dmod = 1;
8267 
8268 			/* Now process return */
8269 			if (spx->txlt_sata_pkt->satapkt_reason !=
8270 			    SATA_PKT_COMPLETED) {
8271 				goto failure;	/* Terminate */
8272 			}
8273 		} else {
8274 			*scsipkt->pkt_scbp = STATUS_CHECK;
8275 			sense = sata_arq_sense(spx);
8276 			sense->es_key = KEY_ILLEGAL_REQUEST;
8277 			sense->es_add_code =
8278 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8279 			*pagelen = parmlen;
8280 			*rval = TRAN_ACCEPT;
8281 			return (SATA_FAILURE);
8282 		}
8283 	}
8284 
8285 	/* Note that the packet is not removed, so it could be re-used */
8286 	if (page->wce != wce) {
8287 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8288 			/* Need to flip Write Cache setting */
8289 			if (page->wce == 1)
8290 				/* Enable write cache */
8291 				scmd->satacmd_features_reg =
8292 				    SATAC_SF_ENABLE_WRITE_CACHE;
8293 			else
8294 				/* Disable write cache */
8295 				scmd->satacmd_features_reg =
8296 				    SATAC_SF_DISABLE_WRITE_CACHE;
8297 
8298 			/* Transfer command to HBA */
8299 			if (sata_hba_start(spx, rval) != 0)
8300 				/*
8301 				 * Pkt not accepted for execution.
8302 				 */
8303 				return (SATA_FAILURE);
8304 
8305 			*dmod = 1;
8306 
8307 			/* Now process return */
8308 			if (spx->txlt_sata_pkt->satapkt_reason !=
8309 			    SATA_PKT_COMPLETED) {
8310 				goto failure;
8311 			}
8312 		} else {
8313 			*scsipkt->pkt_scbp = STATUS_CHECK;
8314 			sense = sata_arq_sense(spx);
8315 			sense->es_key = KEY_ILLEGAL_REQUEST;
8316 			sense->es_add_code =
8317 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8318 			*pagelen = parmlen;
8319 			*rval = TRAN_ACCEPT;
8320 			return (SATA_FAILURE);
8321 		}
8322 	}
8323 	return (SATA_SUCCESS);
8324 
8325 failure:
8326 	sata_xlate_errors(spx);
8327 
8328 	return (SATA_FAILURE);
8329 }
8330 
8331 /*
8332  * Process mode select informational exceptions control page 0x1c
8333  *
8334  * The only changeable bit is dexcpt (disable exceptions).
8335  * MRIE (method of reporting informational exceptions) must be
8336  * "only on request".
8337  * This page applies to informational exceptions that report
8338  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8339  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8340  * Informational exception conditions occur as the result of background scan
8341  * errors, background self-test errors, or vendor specific events within a
8342  * logical unit. An informational exception condition may occur asynchronous
8343  * to any commands.
8344  *
8345  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8346  * If operation resulted in changing device setup, dmod flag should be set to
8347  * one (1). If parameters were not changed, dmod flag should be set to 0.
8348  * Upon return, if operation required sending command to the device, the rval
8349  * should be set to the value returned by sata_hba_start. If operation
8350  * did not require device access, rval should be set to TRAN_ACCEPT.
8351  * The pagelen should be set to the length of the page.
8352  *
8353  * This function has to be called with a port mutex held.
8354  *
8355  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8356  *
8357  * Cannot be called in the interrupt context.
8358  */
8359 static	int
8360 sata_mode_select_page_1c(
8361 	sata_pkt_txlate_t *spx,
8362 	struct mode_info_excpt_page *page,
8363 	int parmlen,
8364 	int *pagelen,
8365 	int *rval,
8366 	int *dmod)
8367 {
8368 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8369 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8370 	sata_drive_info_t *sdinfo;
8371 	sata_id_t *sata_id;
8372 	struct scsi_extended_sense *sense;
8373 
8374 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8375 	    &spx->txlt_sata_pkt->satapkt_device);
8376 	sata_id = &sdinfo->satadrv_id;
8377 
8378 	*dmod = 0;
8379 
8380 	/* Verify parameters length. If too short, drop it */
8381 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8382 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8383 		*scsipkt->pkt_scbp = STATUS_CHECK;
8384 		sense = sata_arq_sense(spx);
8385 		sense->es_key = KEY_ILLEGAL_REQUEST;
8386 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8387 		*pagelen = parmlen;
8388 		*rval = TRAN_ACCEPT;
8389 		return (SATA_FAILURE);
8390 	}
8391 
8392 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8393 
8394 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8395 		*scsipkt->pkt_scbp = STATUS_CHECK;
8396 		sense = sata_arq_sense(spx);
8397 		sense->es_key = KEY_ILLEGAL_REQUEST;
8398 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8399 		*pagelen = parmlen;
8400 		*rval = TRAN_ACCEPT;
8401 		return (SATA_FAILURE);
8402 	}
8403 
8404 	/* If already in the state requested, we are done */
8405 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8406 		/* nothing to do */
8407 		*rval = TRAN_ACCEPT;
8408 		return (SATA_SUCCESS);
8409 	}
8410 
8411 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8412 
8413 	/* Build SMART_ENABLE or SMART_DISABLE command */
8414 	scmd->satacmd_addr_type = 0;		/* N/A */
8415 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8416 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8417 	scmd->satacmd_features_reg = page->dexcpt ?
8418 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8419 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8420 	scmd->satacmd_cmd_reg = SATAC_SMART;
8421 
8422 	/* Transfer command to HBA */
8423 	if (sata_hba_start(spx, rval) != 0)
8424 		/*
8425 		 * Pkt not accepted for execution.
8426 		 */
8427 		return (SATA_FAILURE);
8428 
8429 	*dmod = 1;	/* At least may have been modified */
8430 
8431 	/* Now process return */
8432 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8433 		return (SATA_SUCCESS);
8434 
8435 	/* Packet did not complete successfully */
8436 	sata_xlate_errors(spx);
8437 
8438 	return (SATA_FAILURE);
8439 }
8440 
8441 /*
8442  * Process mode select acoustic management control page 0x30
8443  *
8444  *
8445  * This function has to be called with a port mutex held.
8446  *
8447  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8448  *
8449  * Cannot be called in the interrupt context.
8450  */
8451 int
8452 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8453     mode_acoustic_management *page, int parmlen, int *pagelen,
8454     int *rval, int *dmod)
8455 {
8456 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8457 	sata_drive_info_t *sdinfo;
8458 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8459 	sata_id_t *sata_id;
8460 	struct scsi_extended_sense *sense;
8461 
8462 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8463 	    &spx->txlt_sata_pkt->satapkt_device);
8464 	sata_id = &sdinfo->satadrv_id;
8465 	*dmod = 0;
8466 
8467 	/* If parmlen is too short or the feature is not supported, drop it */
8468 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8469 	    sizeof (struct mode_page)) > parmlen) ||
8470 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8471 		*scsipkt->pkt_scbp = STATUS_CHECK;
8472 		sense = sata_arq_sense(spx);
8473 		sense->es_key = KEY_ILLEGAL_REQUEST;
8474 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8475 		*pagelen = parmlen;
8476 		*rval = TRAN_ACCEPT;
8477 		return (SATA_FAILURE);
8478 	}
8479 
8480 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8481 	    sizeof (struct mode_page);
8482 
8483 	/*
8484 	 * We can enable and disable acoustice management and
8485 	 * set the acoustic management level.
8486 	 */
8487 
8488 	/*
8489 	 * Set-up Internal SET FEATURES command(s)
8490 	 */
8491 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8492 	scmd->satacmd_addr_type = 0;
8493 	scmd->satacmd_device_reg = 0;
8494 	scmd->satacmd_status_reg = 0;
8495 	scmd->satacmd_error_reg = 0;
8496 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8497 	if (page->acoustic_manag_enable) {
8498 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8499 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8500 	} else {	/* disabling acoustic management */
8501 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8502 	}
8503 
8504 	/* Transfer command to HBA */
8505 	if (sata_hba_start(spx, rval) != 0)
8506 		/*
8507 		 * Pkt not accepted for execution.
8508 		 */
8509 		return (SATA_FAILURE);
8510 
8511 	/* Now process return */
8512 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8513 		sata_xlate_errors(spx);
8514 		return (SATA_FAILURE);
8515 	}
8516 
8517 	*dmod = 1;
8518 
8519 	return (SATA_SUCCESS);
8520 }
8521 
8522 /*
8523  * Process mode select power condition page 0x1a
8524  *
8525  * This function has to be called with a port mutex held.
8526  *
8527  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8528  *
8529  * Cannot be called in the interrupt context.
8530  */
8531 int
8532 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8533     mode_info_power_cond *page, int parmlen, int *pagelen,
8534     int *rval, int *dmod)
8535 {
8536 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8537 	sata_drive_info_t *sdinfo;
8538 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8539 	sata_id_t *sata_id;
8540 	struct scsi_extended_sense *sense;
8541 	uint8_t ata_count;
8542 	int i, len;
8543 
8544 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8545 	    &spx->txlt_sata_pkt->satapkt_device);
8546 	sata_id = &sdinfo->satadrv_id;
8547 	*dmod = 0;
8548 
8549 	len = sizeof (struct mode_info_power_cond);
8550 	len += sizeof (struct mode_page);
8551 
8552 	/* If parmlen is too short or the feature is not supported, drop it */
8553 	if ((len < parmlen) || (page->idle == 1) ||
8554 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8555 		*scsipkt->pkt_scbp = STATUS_CHECK;
8556 		sense = sata_arq_sense(spx);
8557 		sense->es_key = KEY_ILLEGAL_REQUEST;
8558 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8559 		*pagelen = parmlen;
8560 		*rval = TRAN_ACCEPT;
8561 		return (SATA_FAILURE);
8562 	}
8563 
8564 	*pagelen = len;
8565 
8566 	/*
8567 	 * Set-up Internal STANDBY command(s)
8568 	 */
8569 	if (page->standby == 0)
8570 		goto out;
8571 
8572 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8573 
8574 	scmd->satacmd_addr_type = 0;
8575 	scmd->satacmd_sec_count_lsb = ata_count;
8576 	scmd->satacmd_lba_low_lsb = 0;
8577 	scmd->satacmd_lba_mid_lsb = 0;
8578 	scmd->satacmd_lba_high_lsb = 0;
8579 	scmd->satacmd_features_reg = 0;
8580 	scmd->satacmd_device_reg = 0;
8581 	scmd->satacmd_status_reg = 0;
8582 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8583 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8584 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8585 
8586 	/* Transfer command to HBA */
8587 	if (sata_hba_start(spx, rval) != 0) {
8588 		return (SATA_FAILURE);
8589 	} else {
8590 		if ((scmd->satacmd_error_reg != 0) ||
8591 		    (spx->txlt_sata_pkt->satapkt_reason !=
8592 		    SATA_PKT_COMPLETED)) {
8593 			sata_xlate_errors(spx);
8594 			return (SATA_FAILURE);
8595 		}
8596 	}
8597 
8598 	for (i = 0; i < 4; i++) {
8599 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8600 	}
8601 out:
8602 	*dmod = 1;
8603 	return (SATA_SUCCESS);
8604 }
8605 
8606 /*
8607  * sata_build_lsense_page0() is used to create the
8608  * SCSI LOG SENSE page 0 (supported log pages)
8609  *
8610  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8611  * (supported log pages, self-test results, informational exceptions
8612  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8613  *
8614  * Takes a sata_drive_info t * and the address of a buffer
8615  * in which to create the page information.
8616  *
8617  * Returns the number of bytes valid in the buffer.
8618  */
8619 static	int
8620 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8621 {
8622 	struct log_parameter *lpp = (struct log_parameter *)buf;
8623 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8624 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8625 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8626 
8627 	lpp->param_code[0] = 0;
8628 	lpp->param_code[1] = 0;
8629 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8630 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8631 
8632 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8633 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8634 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8635 			++num_pages_supported;
8636 		}
8637 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8638 		++num_pages_supported;
8639 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8640 		++num_pages_supported;
8641 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8642 		++num_pages_supported;
8643 	}
8644 
8645 	lpp->param_len = num_pages_supported;
8646 
8647 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8648 	    num_pages_supported);
8649 }
8650 
8651 /*
8652  * sata_build_lsense_page_10() is used to create the
8653  * SCSI LOG SENSE page 0x10 (self-test results)
8654  *
8655  * Takes a sata_drive_info t * and the address of a buffer
8656  * in which to create the page information as well as a sata_hba_inst_t *.
8657  *
8658  * Returns the number of bytes valid in the buffer.
8659  *
8660  * Note: Self test and SMART data is accessible in device log pages.
8661  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8662  * of data can be transferred by a single command), or by the General Purpose
8663  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8664  * - approximately 33MB - can be transferred by a single command.
8665  * The SCT Command response (either error or command) is the same for both
8666  * the SMART and GPL methods of issuing commands.
8667  * This function uses READ LOG EXT command when drive supports LBA48, and
8668  * SMART READ command otherwise.
8669  *
8670  * Since above commands are executed in a synchronous mode, this function
8671  * should not be called in an interrupt context.
8672  */
8673 static	int
8674 sata_build_lsense_page_10(
8675 	sata_drive_info_t *sdinfo,
8676 	uint8_t *buf,
8677 	sata_hba_inst_t *sata_hba_inst)
8678 {
8679 	struct log_parameter *lpp = (struct log_parameter *)buf;
8680 	int rval;
8681 
8682 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8683 		struct smart_ext_selftest_log *ext_selftest_log;
8684 
8685 		ext_selftest_log = kmem_zalloc(
8686 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8687 
8688 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8689 		    ext_selftest_log, 0);
8690 		if (rval == 0) {
8691 			int index, start_index;
8692 			struct smart_ext_selftest_log_entry *entry;
8693 			static const struct smart_ext_selftest_log_entry empty =
8694 			    {0};
8695 			uint16_t block_num;
8696 			int count;
8697 			boolean_t only_one_block = B_FALSE;
8698 
8699 			index = ext_selftest_log->
8700 			    smart_ext_selftest_log_index[0];
8701 			index |= ext_selftest_log->
8702 			    smart_ext_selftest_log_index[1] << 8;
8703 			if (index == 0)
8704 				goto out;
8705 
8706 			--index;	/* Correct for 0 origin */
8707 			start_index = index;	/* remember where we started */
8708 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8709 			if (block_num != 0) {
8710 				rval = sata_ext_smart_selftest_read_log(
8711 				    sata_hba_inst, sdinfo, ext_selftest_log,
8712 				    block_num);
8713 				if (rval != 0)
8714 					goto out;
8715 			}
8716 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8717 			entry =
8718 			    &ext_selftest_log->
8719 			    smart_ext_selftest_log_entries[index];
8720 
8721 			for (count = 1;
8722 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8723 			    ++count) {
8724 				uint8_t status;
8725 				uint8_t code;
8726 				uint8_t sense_key;
8727 				uint8_t add_sense_code;
8728 				uint8_t add_sense_code_qual;
8729 
8730 				/* If this is an unused entry, we are done */
8731 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8732 					/* Broken firmware on some disks */
8733 					if (index + 1 ==
8734 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8735 						--entry;
8736 						--index;
8737 						if (bcmp(entry, &empty,
8738 						    sizeof (empty)) == 0)
8739 							goto out;
8740 					} else
8741 						goto out;
8742 				}
8743 
8744 				if (only_one_block &&
8745 				    start_index == index)
8746 					goto out;
8747 
8748 				lpp->param_code[0] = 0;
8749 				lpp->param_code[1] = count;
8750 				lpp->param_ctrl_flags =
8751 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8752 				lpp->param_len =
8753 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8754 
8755 				status = entry->smart_ext_selftest_log_status;
8756 				status >>= 4;
8757 				switch (status) {
8758 				case 0:
8759 				default:
8760 					sense_key = KEY_NO_SENSE;
8761 					add_sense_code =
8762 					    SD_SCSI_ASC_NO_ADD_SENSE;
8763 					add_sense_code_qual = 0;
8764 					break;
8765 				case 1:
8766 					sense_key = KEY_ABORTED_COMMAND;
8767 					add_sense_code =
8768 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8769 					add_sense_code_qual = SCSI_COMPONENT_81;
8770 					break;
8771 				case 2:
8772 					sense_key = KEY_ABORTED_COMMAND;
8773 					add_sense_code =
8774 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8775 					add_sense_code_qual = SCSI_COMPONENT_82;
8776 					break;
8777 				case 3:
8778 					sense_key = KEY_ABORTED_COMMAND;
8779 					add_sense_code =
8780 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8781 					add_sense_code_qual = SCSI_COMPONENT_83;
8782 					break;
8783 				case 4:
8784 					sense_key = KEY_HARDWARE_ERROR;
8785 					add_sense_code =
8786 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8787 					add_sense_code_qual = SCSI_COMPONENT_84;
8788 					break;
8789 				case 5:
8790 					sense_key = KEY_HARDWARE_ERROR;
8791 					add_sense_code =
8792 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8793 					add_sense_code_qual = SCSI_COMPONENT_85;
8794 					break;
8795 				case 6:
8796 					sense_key = KEY_HARDWARE_ERROR;
8797 					add_sense_code =
8798 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8799 					add_sense_code_qual = SCSI_COMPONENT_86;
8800 					break;
8801 				case 7:
8802 					sense_key = KEY_MEDIUM_ERROR;
8803 					add_sense_code =
8804 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8805 					add_sense_code_qual = SCSI_COMPONENT_87;
8806 					break;
8807 				case 8:
8808 					sense_key = KEY_HARDWARE_ERROR;
8809 					add_sense_code =
8810 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8811 					add_sense_code_qual = SCSI_COMPONENT_88;
8812 					break;
8813 				}
8814 				code = 0;	/* unspecified */
8815 				status |= (code << 4);
8816 				lpp->param_values[0] = status;
8817 				lpp->param_values[1] = 0; /* unspecified */
8818 				lpp->param_values[2] = entry->
8819 				    smart_ext_selftest_log_timestamp[1];
8820 				lpp->param_values[3] = entry->
8821 				    smart_ext_selftest_log_timestamp[0];
8822 				if (status != 0) {
8823 					lpp->param_values[4] = 0;
8824 					lpp->param_values[5] = 0;
8825 					lpp->param_values[6] = entry->
8826 					    smart_ext_selftest_log_failing_lba
8827 					    [5];
8828 					lpp->param_values[7] = entry->
8829 					    smart_ext_selftest_log_failing_lba
8830 					    [4];
8831 					lpp->param_values[8] = entry->
8832 					    smart_ext_selftest_log_failing_lba
8833 					    [3];
8834 					lpp->param_values[9] = entry->
8835 					    smart_ext_selftest_log_failing_lba
8836 					    [2];
8837 					lpp->param_values[10] = entry->
8838 					    smart_ext_selftest_log_failing_lba
8839 					    [1];
8840 					lpp->param_values[11] = entry->
8841 					    smart_ext_selftest_log_failing_lba
8842 					    [0];
8843 				} else {	/* No bad block address */
8844 					lpp->param_values[4] = 0xff;
8845 					lpp->param_values[5] = 0xff;
8846 					lpp->param_values[6] = 0xff;
8847 					lpp->param_values[7] = 0xff;
8848 					lpp->param_values[8] = 0xff;
8849 					lpp->param_values[9] = 0xff;
8850 					lpp->param_values[10] = 0xff;
8851 					lpp->param_values[11] = 0xff;
8852 				}
8853 
8854 				lpp->param_values[12] = sense_key;
8855 				lpp->param_values[13] = add_sense_code;
8856 				lpp->param_values[14] = add_sense_code_qual;
8857 				lpp->param_values[15] = 0; /* undefined */
8858 
8859 				lpp = (struct log_parameter *)
8860 				    (((uint8_t *)lpp) +
8861 				    SCSI_LOG_PARAM_HDR_LEN +
8862 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8863 
8864 				--index;	/* Back up to previous entry */
8865 				if (index < 0) {
8866 					if (block_num > 0) {
8867 						--block_num;
8868 					} else {
8869 						struct read_log_ext_directory
8870 						    logdir;
8871 
8872 						rval =
8873 						    sata_read_log_ext_directory(
8874 						    sata_hba_inst, sdinfo,
8875 						    &logdir);
8876 						if (rval == -1)
8877 							goto out;
8878 						if ((logdir.read_log_ext_vers
8879 						    [0] == 0) &&
8880 						    (logdir.read_log_ext_vers
8881 						    [1] == 0))
8882 							goto out;
8883 						block_num =
8884 						    logdir.read_log_ext_nblks
8885 						    [EXT_SMART_SELFTEST_LOG_PAGE
8886 						    - 1][0];
8887 						block_num |= logdir.
8888 						    read_log_ext_nblks
8889 						    [EXT_SMART_SELFTEST_LOG_PAGE
8890 						    - 1][1] << 8;
8891 						--block_num;
8892 						only_one_block =
8893 						    (block_num == 0);
8894 					}
8895 					rval = sata_ext_smart_selftest_read_log(
8896 					    sata_hba_inst, sdinfo,
8897 					    ext_selftest_log, block_num);
8898 					if (rval != 0)
8899 						goto out;
8900 
8901 					index =
8902 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8903 					    1;
8904 				}
8905 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8906 				entry = &ext_selftest_log->
8907 				    smart_ext_selftest_log_entries[index];
8908 			}
8909 		}
8910 out:
8911 		kmem_free(ext_selftest_log,
8912 		    sizeof (struct smart_ext_selftest_log));
8913 	} else {
8914 		struct smart_selftest_log *selftest_log;
8915 
8916 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8917 		    KM_SLEEP);
8918 
8919 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8920 		    selftest_log);
8921 
8922 		if (rval == 0) {
8923 			int index;
8924 			int count;
8925 			struct smart_selftest_log_entry *entry;
8926 			static const struct smart_selftest_log_entry empty =
8927 			    { 0 };
8928 
8929 			index = selftest_log->smart_selftest_log_index;
8930 			if (index == 0)
8931 				goto done;
8932 			--index;	/* Correct for 0 origin */
8933 			entry = &selftest_log->
8934 			    smart_selftest_log_entries[index];
8935 			for (count = 1;
8936 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8937 			    ++count) {
8938 				uint8_t status;
8939 				uint8_t code;
8940 				uint8_t sense_key;
8941 				uint8_t add_sense_code;
8942 				uint8_t add_sense_code_qual;
8943 
8944 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8945 					goto done;
8946 
8947 				lpp->param_code[0] = 0;
8948 				lpp->param_code[1] = count;
8949 				lpp->param_ctrl_flags =
8950 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8951 				lpp->param_len =
8952 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8953 
8954 				status = entry->smart_selftest_log_status;
8955 				status >>= 4;
8956 				switch (status) {
8957 				case 0:
8958 				default:
8959 					sense_key = KEY_NO_SENSE;
8960 					add_sense_code =
8961 					    SD_SCSI_ASC_NO_ADD_SENSE;
8962 					break;
8963 				case 1:
8964 					sense_key = KEY_ABORTED_COMMAND;
8965 					add_sense_code =
8966 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8967 					add_sense_code_qual = SCSI_COMPONENT_81;
8968 					break;
8969 				case 2:
8970 					sense_key = KEY_ABORTED_COMMAND;
8971 					add_sense_code =
8972 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8973 					add_sense_code_qual = SCSI_COMPONENT_82;
8974 					break;
8975 				case 3:
8976 					sense_key = KEY_ABORTED_COMMAND;
8977 					add_sense_code =
8978 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8979 					add_sense_code_qual = SCSI_COMPONENT_83;
8980 					break;
8981 				case 4:
8982 					sense_key = KEY_HARDWARE_ERROR;
8983 					add_sense_code =
8984 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8985 					add_sense_code_qual = SCSI_COMPONENT_84;
8986 					break;
8987 				case 5:
8988 					sense_key = KEY_HARDWARE_ERROR;
8989 					add_sense_code =
8990 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8991 					add_sense_code_qual = SCSI_COMPONENT_85;
8992 					break;
8993 				case 6:
8994 					sense_key = KEY_HARDWARE_ERROR;
8995 					add_sense_code =
8996 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8997 					add_sense_code_qual = SCSI_COMPONENT_86;
8998 					break;
8999 				case 7:
9000 					sense_key = KEY_MEDIUM_ERROR;
9001 					add_sense_code =
9002 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9003 					add_sense_code_qual = SCSI_COMPONENT_87;
9004 					break;
9005 				case 8:
9006 					sense_key = KEY_HARDWARE_ERROR;
9007 					add_sense_code =
9008 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9009 					add_sense_code_qual = SCSI_COMPONENT_88;
9010 					break;
9011 				}
9012 				code = 0;	/* unspecified */
9013 				status |= (code << 4);
9014 				lpp->param_values[0] = status;
9015 				lpp->param_values[1] = 0; /* unspecified */
9016 				lpp->param_values[2] = entry->
9017 				    smart_selftest_log_timestamp[1];
9018 				lpp->param_values[3] = entry->
9019 				    smart_selftest_log_timestamp[0];
9020 				if (status != 0) {
9021 					lpp->param_values[4] = 0;
9022 					lpp->param_values[5] = 0;
9023 					lpp->param_values[6] = 0;
9024 					lpp->param_values[7] = 0;
9025 					lpp->param_values[8] = entry->
9026 					    smart_selftest_log_failing_lba[3];
9027 					lpp->param_values[9] = entry->
9028 					    smart_selftest_log_failing_lba[2];
9029 					lpp->param_values[10] = entry->
9030 					    smart_selftest_log_failing_lba[1];
9031 					lpp->param_values[11] = entry->
9032 					    smart_selftest_log_failing_lba[0];
9033 				} else {	/* No block address */
9034 					lpp->param_values[4] = 0xff;
9035 					lpp->param_values[5] = 0xff;
9036 					lpp->param_values[6] = 0xff;
9037 					lpp->param_values[7] = 0xff;
9038 					lpp->param_values[8] = 0xff;
9039 					lpp->param_values[9] = 0xff;
9040 					lpp->param_values[10] = 0xff;
9041 					lpp->param_values[11] = 0xff;
9042 				}
9043 				lpp->param_values[12] = sense_key;
9044 				lpp->param_values[13] = add_sense_code;
9045 				lpp->param_values[14] = add_sense_code_qual;
9046 				lpp->param_values[15] = 0; /* undefined */
9047 
9048 				lpp = (struct log_parameter *)
9049 				    (((uint8_t *)lpp) +
9050 				    SCSI_LOG_PARAM_HDR_LEN +
9051 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9052 				--index;	/* back up to previous entry */
9053 				if (index < 0) {
9054 					index =
9055 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9056 				}
9057 				entry = &selftest_log->
9058 				    smart_selftest_log_entries[index];
9059 			}
9060 		}
9061 done:
9062 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9063 	}
9064 
9065 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9066 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9067 }
9068 
9069 /*
9070  * sata_build_lsense_page_2f() is used to create the
9071  * SCSI LOG SENSE page 0x2f (informational exceptions)
9072  *
9073  * Takes a sata_drive_info t * and the address of a buffer
9074  * in which to create the page information as well as a sata_hba_inst_t *.
9075  *
9076  * Returns the number of bytes valid in the buffer.
9077  *
9078  * Because it invokes function(s) that send synchronously executed command
9079  * to the HBA, it cannot be called in the interrupt context.
9080  */
9081 static	int
9082 sata_build_lsense_page_2f(
9083 	sata_drive_info_t *sdinfo,
9084 	uint8_t *buf,
9085 	sata_hba_inst_t *sata_hba_inst)
9086 {
9087 	struct log_parameter *lpp = (struct log_parameter *)buf;
9088 	int rval;
9089 	uint8_t *smart_data;
9090 	uint8_t temp;
9091 	sata_id_t *sata_id;
9092 #define	SMART_NO_TEMP	0xff
9093 
9094 	lpp->param_code[0] = 0;
9095 	lpp->param_code[1] = 0;
9096 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9097 
9098 	/* Now get the SMART status w.r.t. threshold exceeded */
9099 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9100 	switch (rval) {
9101 	case 1:
9102 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9103 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9104 		break;
9105 	case 0:
9106 	case -1:	/* failed to get data */
9107 		lpp->param_values[0] = 0;	/* No failure predicted */
9108 		lpp->param_values[1] = 0;
9109 		break;
9110 #if defined(SATA_DEBUG)
9111 	default:
9112 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9113 		/* NOTREACHED */
9114 #endif
9115 	}
9116 
9117 	sata_id = &sdinfo->satadrv_id;
9118 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9119 		temp = SMART_NO_TEMP;
9120 	else {
9121 		/* Now get the temperature */
9122 		smart_data = kmem_zalloc(512, KM_SLEEP);
9123 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9124 		    SCT_STATUS_LOG_PAGE, 1);
9125 		if (rval == -1)
9126 			temp = SMART_NO_TEMP;
9127 		else {
9128 			temp = smart_data[200];
9129 			if (temp & 0x80) {
9130 				if (temp & 0x7f)
9131 					temp = 0;
9132 				else
9133 					temp = SMART_NO_TEMP;
9134 			}
9135 		}
9136 		kmem_free(smart_data, 512);
9137 	}
9138 
9139 	lpp->param_values[2] = temp;	/* most recent temperature */
9140 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9141 
9142 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9143 
9144 
9145 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9146 }
9147 
9148 /*
9149  * sata_build_lsense_page_30() is used to create the
9150  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9151  *
9152  * Takes a sata_drive_info t * and the address of a buffer
9153  * in which to create the page information as well as a sata_hba_inst_t *.
9154  *
9155  * Returns the number of bytes valid in the buffer.
9156  */
9157 static int
9158 sata_build_lsense_page_30(
9159 	sata_drive_info_t *sdinfo,
9160 	uint8_t *buf,
9161 	sata_hba_inst_t *sata_hba_inst)
9162 {
9163 	struct smart_data *smart_data = (struct smart_data *)buf;
9164 	int rval;
9165 
9166 	/* Now do the SMART READ DATA */
9167 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9168 	if (rval == -1)
9169 		return (0);
9170 
9171 	return (sizeof (struct smart_data));
9172 }
9173 
9174 /*
9175  * sata_build_lsense_page_0e() is used to create the
9176  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9177  *
9178  * Date of Manufacture (0x0001)
9179  *	YEAR = "0000"
9180  *	WEEK = "00"
9181  * Accounting Date (0x0002)
9182  *	6 ASCII space character(20h)
9183  * Specified cycle count over device lifetime
9184  *	VALUE - THRESH - the delta between max and min;
9185  * Accumulated start-stop cycles
9186  *	VALUE - WORST - the accumulated cycles;
9187  *
9188  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9189  *
9190  * Takes a sata_drive_info t * and the address of a buffer
9191  * in which to create the page information as well as a sata_hba_inst_t *.
9192  *
9193  * Returns the number of bytes valid in the buffer.
9194  */
9195 static	int
9196 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9197 	sata_pkt_txlate_t *spx)
9198 {
9199 	struct start_stop_cycle_counter_log *log_page;
9200 	int i, rval, index;
9201 	uint8_t smart_data[512], id, value, worst, thresh;
9202 	uint32_t max_count, cycles;
9203 
9204 	/* Now do the SMART READ DATA */
9205 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9206 	    (struct smart_data *)smart_data);
9207 	if (rval == -1)
9208 		return (0);
9209 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9210 		index = (i * 12) + 2;
9211 		id = smart_data[index];
9212 		if (id != SMART_START_STOP_COUNT_ID)
9213 			continue;
9214 		else {
9215 			thresh = smart_data[index + 2];
9216 			value = smart_data[index + 3];
9217 			worst = smart_data[index + 4];
9218 			break;
9219 		}
9220 	}
9221 	if (id != SMART_START_STOP_COUNT_ID)
9222 		return (0);
9223 	max_count = value - thresh;
9224 	cycles = value - worst;
9225 
9226 	log_page = (struct start_stop_cycle_counter_log *)buf;
9227 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9228 	log_page->code = 0x0e;
9229 	log_page->page_len_low = 0x24;
9230 
9231 	log_page->manufactor_date_low = 0x1;
9232 	log_page->param_1.fmt_link = 0x1; /* 01b */
9233 	log_page->param_len_1 = 0x06;
9234 	for (i = 0; i < 4; i++) {
9235 		log_page->year_manu[i] = 0x30;
9236 		if (i < 2)
9237 			log_page->week_manu[i] = 0x30;
9238 	}
9239 
9240 	log_page->account_date_low = 0x02;
9241 	log_page->param_2.fmt_link = 0x01; /* 01b */
9242 	log_page->param_len_2 = 0x06;
9243 	for (i = 0; i < 4; i++) {
9244 		log_page->year_account[i] = 0x20;
9245 		if (i < 2)
9246 			log_page->week_account[i] = 0x20;
9247 	}
9248 
9249 	log_page->lifetime_code_low = 0x03;
9250 	log_page->param_3.fmt_link = 0x03; /* 11b */
9251 	log_page->param_len_3 = 0x04;
9252 	/* VALUE - THRESH - the delta between max and min */
9253 	log_page->cycle_code_low = 0x04;
9254 	log_page->param_4.fmt_link = 0x03; /* 11b */
9255 	log_page->param_len_4 = 0x04;
9256 	/* WORST - THRESH - the distance from 'now' to min */
9257 
9258 	for (i = 0; i < 4; i++) {
9259 		log_page->cycle_lifetime[i] =
9260 		    (max_count >> (8 * (3 - i))) & 0xff;
9261 		log_page->cycle_accumulated[i] =
9262 		    (cycles >> (8 * (3 - i))) & 0xff;
9263 	}
9264 
9265 	return (sizeof (struct start_stop_cycle_counter_log));
9266 }
9267 
9268 /*
9269  * This function was used for build a ATA read verify sector command
9270  */
9271 static void
9272 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9273 {
9274 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9275 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9276 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9277 
9278 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9279 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9280 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9281 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9282 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9283 	scmd->satacmd_features_reg = 0;
9284 	scmd->satacmd_status_reg = 0;
9285 	scmd->satacmd_error_reg = 0;
9286 }
9287 
9288 /*
9289  * This function was used for building an ATA
9290  * command, and only command register need to
9291  * be defined, other register will be zero or na.
9292  */
9293 static void
9294 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9295 {
9296 	scmd->satacmd_addr_type = 0;
9297 	scmd->satacmd_cmd_reg = cmd;
9298 	scmd->satacmd_device_reg = 0;
9299 	scmd->satacmd_sec_count_lsb = 0;
9300 	scmd->satacmd_lba_low_lsb = 0;
9301 	scmd->satacmd_lba_mid_lsb = 0;
9302 	scmd->satacmd_lba_high_lsb = 0;
9303 	scmd->satacmd_features_reg = 0;
9304 	scmd->satacmd_status_reg = 0;
9305 	scmd->satacmd_error_reg = 0;
9306 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9307 }
9308 
9309 /*
9310  * This function was used for changing the standby
9311  * timer format from SCSI to ATA.
9312  */
9313 static uint8_t
9314 sata_get_standby_timer(uint8_t *timer)
9315 {
9316 	uint32_t i = 0, count = 0;
9317 	uint8_t ata_count;
9318 
9319 	for (i = 0; i < 4; i++) {
9320 		count = count << 8 | timer[i];
9321 	}
9322 
9323 	if (count == 0)
9324 		return (0);
9325 
9326 	if (count >= 1 && count <= 12000)
9327 		ata_count = (count -1) / 50 + 1;
9328 	else if (count > 12000 && count <= 12600)
9329 		ata_count = 0xfc;
9330 	else if (count > 12601 && count <= 12750)
9331 		ata_count = 0xff;
9332 	else if (count > 12750 && count <= 17999)
9333 		ata_count = 0xf1;
9334 	else if (count > 18000 && count <= 198000)
9335 		ata_count = count / 18000 + 240;
9336 	else
9337 		ata_count = 0xfd;
9338 	return (ata_count);
9339 }
9340 
9341 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9342 
9343 /*
9344  * Start command for ATAPI device.
9345  * This function processes scsi_pkt requests.
9346  * Now CD/DVD, tape and ATAPI disk devices are supported.
9347  * Most commands are packet without any translation into Packet Command.
9348  * Some may be trapped and executed as SATA commands (not clear which one).
9349  *
9350  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9351  * execution).
9352  * Returns other TRAN_XXXX codes if command is not accepted or completed
9353  * (see return values for sata_hba_start()).
9354  *
9355  * Note:
9356  * Inquiry cdb format differs between transport version 2 and 3.
9357  * However, the transport version 3 devices that were checked did not adhere
9358  * to the specification (ignored MSB of the allocation length). Therefore,
9359  * the transport version is not checked, but Inquiry allocation length is
9360  * truncated to 255 bytes if the original allocation length set-up by the
9361  * target driver is greater than 255 bytes.
9362  */
9363 static int
9364 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9365 {
9366 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9367 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9368 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9369 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9370 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9371 	    &spx->txlt_sata_pkt->satapkt_device);
9372 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9373 	int cdblen;
9374 	int rval, reason;
9375 	int synch;
9376 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9377 
9378 	mutex_enter(cport_mutex);
9379 
9380 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9381 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9382 		mutex_exit(cport_mutex);
9383 		return (rval);
9384 	}
9385 
9386 	/*
9387 	 * ATAPI device executes some ATA commands in addition to those
9388 	 * commands sent via PACKET command. These ATA commands may be
9389 	 * executed by the regular SATA translation functions. None needs
9390 	 * to be captured now.
9391 	 *
9392 	 * Commands sent via PACKET command include:
9393 	 *	MMC command set for ATAPI CD/DVD device
9394 	 *	SSC command set for ATAPI TAPE device
9395 	 *	SBC command set for ATAPI disk device
9396 	 *
9397 	 */
9398 
9399 	/* Check the size of cdb */
9400 
9401 	switch (GETGROUP(cdbp)) {
9402 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9403 		/*
9404 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9405 		 * therefore require special handling.  Return failure, for now.
9406 		 */
9407 		mutex_exit(cport_mutex);
9408 		return (TRAN_BADPKT);
9409 
9410 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9411 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9412 		/* obtain length from the scsi_pkt */
9413 		cdblen = scsipkt->pkt_cdblen;
9414 		break;
9415 
9416 	default:
9417 		/* CDB's length is statically known, per SPC-4 */
9418 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9419 		break;
9420 	}
9421 
9422 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9423 		sata_log(NULL, CE_WARN,
9424 		    "sata: invalid ATAPI cdb length %d",
9425 		    cdblen);
9426 		mutex_exit(cport_mutex);
9427 		return (TRAN_BADPKT);
9428 	}
9429 
9430 	SATAATAPITRACE(spx, cdblen);
9431 
9432 	/*
9433 	 * For non-read/write commands we need to
9434 	 * map buffer
9435 	 */
9436 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9437 	case SCMD_READ:
9438 	case SCMD_READ_G1:
9439 	case SCMD_READ_G5:
9440 	case SCMD_READ_G4:
9441 	case SCMD_WRITE:
9442 	case SCMD_WRITE_G1:
9443 	case SCMD_WRITE_G5:
9444 	case SCMD_WRITE_G4:
9445 		break;
9446 	default:
9447 		if (bp != NULL) {
9448 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9449 				bp_mapin(bp);
9450 		}
9451 		break;
9452 	}
9453 	/*
9454 	 * scmd->satacmd_flags.sata_data_direction default -
9455 	 * SATA_DIR_NODATA_XFER - is set by
9456 	 * sata_txlt_generic_pkt_info().
9457 	 */
9458 	if (scmd->satacmd_bp) {
9459 		if (scmd->satacmd_bp->b_flags & B_READ) {
9460 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9461 		} else {
9462 			scmd->satacmd_flags.sata_data_direction =
9463 			    SATA_DIR_WRITE;
9464 		}
9465 	}
9466 
9467 	/*
9468 	 * Set up ATAPI packet command.
9469 	 */
9470 
9471 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9472 
9473 	/* Copy cdb into sata_cmd */
9474 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9475 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9476 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9477 
9478 	/* See note in the command header */
9479 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9480 		if (scmd->satacmd_acdb[3] != 0)
9481 			scmd->satacmd_acdb[4] = 255;
9482 	}
9483 
9484 #ifdef SATA_DEBUG
9485 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9486 		uint8_t *p = scmd->satacmd_acdb;
9487 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9488 
9489 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9490 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9491 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9492 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9493 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9494 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9495 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9496 	}
9497 #endif
9498 
9499 	/*
9500 	 * Preset request sense data to NO SENSE.
9501 	 * If there is no way to get error information via Request Sense,
9502 	 * the packet request sense data would not have to be modified by HBA,
9503 	 * but it could be returned as is.
9504 	 */
9505 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9506 	sata_fixed_sense_data_preset(
9507 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9508 
9509 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9510 		/* Need callback function */
9511 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9512 		synch = FALSE;
9513 	} else
9514 		synch = TRUE;
9515 
9516 	/* Transfer command to HBA */
9517 	if (sata_hba_start(spx, &rval) != 0) {
9518 		/* Pkt not accepted for execution */
9519 		mutex_exit(cport_mutex);
9520 		return (rval);
9521 	}
9522 	mutex_exit(cport_mutex);
9523 	/*
9524 	 * If execution is non-synchronous,
9525 	 * a callback function will handle potential errors, translate
9526 	 * the response and will do a callback to a target driver.
9527 	 * If it was synchronous, use the same framework callback to check
9528 	 * an execution status.
9529 	 */
9530 	if (synch) {
9531 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9532 		    "synchronous execution status %x\n",
9533 		    spx->txlt_sata_pkt->satapkt_reason);
9534 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9535 	}
9536 	return (TRAN_ACCEPT);
9537 }
9538 
9539 
9540 /*
9541  * ATAPI Packet command completion.
9542  *
9543  * Failure of the command passed via Packet command are considered device
9544  * error. SATA HBA driver would have to retrieve error data (via Request
9545  * Sense command delivered via error retrieval sata packet) and copy it
9546  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9547  */
9548 static void
9549 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9550 {
9551 	sata_pkt_txlate_t *spx =
9552 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9553 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9554 	struct scsi_extended_sense *sense;
9555 	struct buf *bp;
9556 	int rval;
9557 
9558 #ifdef SATA_DEBUG
9559 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9560 #endif
9561 
9562 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9563 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9564 
9565 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9566 		/* Normal completion */
9567 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9568 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9569 		scsipkt->pkt_reason = CMD_CMPLT;
9570 		*scsipkt->pkt_scbp = STATUS_GOOD;
9571 		if (spx->txlt_tmp_buf != NULL) {
9572 			/* Temporary buffer was used */
9573 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9574 			if (bp->b_flags & B_READ) {
9575 				rval = ddi_dma_sync(
9576 				    spx->txlt_buf_dma_handle, 0, 0,
9577 				    DDI_DMA_SYNC_FORCPU);
9578 				ASSERT(rval == DDI_SUCCESS);
9579 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9580 				    bp->b_bcount);
9581 			}
9582 		}
9583 	} else {
9584 		/*
9585 		 * Something went wrong - analyze return
9586 		 */
9587 		*scsipkt->pkt_scbp = STATUS_CHECK;
9588 		sense = sata_arq_sense(spx);
9589 
9590 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9591 			/*
9592 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9593 			 * Under this condition ERR bit is set for ATA command,
9594 			 * and CHK bit set for ATAPI command.
9595 			 *
9596 			 * Please check st_intr & sdintr about how pkt_reason
9597 			 * is used.
9598 			 */
9599 			scsipkt->pkt_reason = CMD_CMPLT;
9600 
9601 			/*
9602 			 * We may not have ARQ data if there was a double
9603 			 * error. But sense data in sata packet was pre-set
9604 			 * with NO SENSE so it is valid even if HBA could
9605 			 * not retrieve a real sense data.
9606 			 * Just copy this sense data into scsi pkt sense area.
9607 			 */
9608 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9609 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9610 #ifdef SATA_DEBUG
9611 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9612 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9613 				    "sata_txlt_atapi_completion: %02x\n"
9614 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9615 				    "          %02x %02x %02x %02x %02x %02x "
9616 				    "          %02x %02x %02x %02x %02x %02x\n",
9617 				    scsipkt->pkt_reason,
9618 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9619 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9620 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9621 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9622 				    rqsp[16], rqsp[17]);
9623 			}
9624 #endif
9625 		} else {
9626 			switch (sata_pkt->satapkt_reason) {
9627 			case SATA_PKT_PORT_ERROR:
9628 				/*
9629 				 * We have no device data.
9630 				 */
9631 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9632 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9633 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9634 				    STATE_GOT_STATUS);
9635 				sense->es_key = KEY_HARDWARE_ERROR;
9636 				break;
9637 
9638 			case SATA_PKT_TIMEOUT:
9639 				scsipkt->pkt_reason = CMD_TIMEOUT;
9640 				scsipkt->pkt_statistics |=
9641 				    STAT_TIMEOUT | STAT_DEV_RESET;
9642 				/*
9643 				 * Need to check if HARDWARE_ERROR/
9644 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9645 				 * appropriate.
9646 				 */
9647 				break;
9648 
9649 			case SATA_PKT_ABORTED:
9650 				scsipkt->pkt_reason = CMD_ABORTED;
9651 				scsipkt->pkt_statistics |= STAT_ABORTED;
9652 				/* Should we set key COMMAND_ABPRTED? */
9653 				break;
9654 
9655 			case SATA_PKT_RESET:
9656 				scsipkt->pkt_reason = CMD_RESET;
9657 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9658 				/*
9659 				 * May be we should set Unit Attention /
9660 				 * Reset. Perhaps the same should be
9661 				 * returned for disks....
9662 				 */
9663 				sense->es_key = KEY_UNIT_ATTENTION;
9664 				sense->es_add_code = SD_SCSI_ASC_RESET;
9665 				break;
9666 
9667 			default:
9668 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9669 				    "sata_txlt_atapi_completion: "
9670 				    "invalid packet completion reason"));
9671 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9672 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9673 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9674 				    STATE_GOT_STATUS);
9675 				break;
9676 			}
9677 		}
9678 	}
9679 
9680 	SATAATAPITRACE(spx, 0);
9681 
9682 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9683 	    scsipkt->pkt_comp != NULL) {
9684 		/* scsi callback required */
9685 		(*scsipkt->pkt_comp)(scsipkt);
9686 	}
9687 }
9688 
9689 /*
9690  * Set up error retrieval sata command for ATAPI Packet Command error data
9691  * recovery.
9692  *
9693  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9694  * returns SATA_FAILURE otherwise.
9695  */
9696 
9697 static int
9698 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9699 {
9700 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9701 	sata_cmd_t *scmd;
9702 	struct buf *bp;
9703 
9704 	/*
9705 	 * Allocate dma-able buffer error data.
9706 	 * Buffer allocation will take care of buffer alignment and other DMA
9707 	 * attributes.
9708 	 */
9709 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9710 	if (bp == NULL) {
9711 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9712 		    "sata_get_err_retrieval_pkt: "
9713 		    "cannot allocate buffer for error data", NULL);
9714 		return (SATA_FAILURE);
9715 	}
9716 	bp_mapin(bp); /* make data buffer accessible */
9717 
9718 	/* Operation modes are up to the caller */
9719 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9720 
9721 	/* Synchronous mode, no callback - may be changed by the caller */
9722 	spkt->satapkt_comp = NULL;
9723 	spkt->satapkt_time = sata_default_pkt_time;
9724 
9725 	scmd = &spkt->satapkt_cmd;
9726 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9727 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9728 
9729 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9730 
9731 	/*
9732 	 * Set-up acdb. Request Sense CDB (packet command content) is
9733 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9734 	 * it is transfered into packet FIS).
9735 	 */
9736 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9737 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9738 	/* Following zeroing of pad bytes may not be necessary */
9739 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9740 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9741 
9742 	/*
9743 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9744 	 * before accessing it. Handle is in usual place in translate struct.
9745 	 */
9746 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9747 
9748 	/*
9749 	 * Preset request sense data to NO SENSE.
9750 	 * Here it is redundant, only for a symetry with scsi-originated
9751 	 * packets. It should not be used for anything but debugging.
9752 	 */
9753 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9754 	sata_fixed_sense_data_preset(
9755 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9756 
9757 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9758 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9759 
9760 	return (SATA_SUCCESS);
9761 }
9762 
9763 /*
9764  * Set-up ATAPI packet command.
9765  * Data transfer direction has to be set-up in sata_cmd structure prior to
9766  * calling this function.
9767  *
9768  * Returns void
9769  */
9770 
9771 static void
9772 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9773 {
9774 	scmd->satacmd_addr_type = 0;		/* N/A */
9775 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9776 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9777 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9778 	scmd->satacmd_lba_high_lsb =
9779 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9780 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9781 
9782 	/*
9783 	 * We want all data to be transfered via DMA.
9784 	 * But specify it only if drive supports DMA and DMA mode is
9785 	 * selected - some drives are sensitive about it.
9786 	 * Hopefully it wil work for all drives....
9787 	 */
9788 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9789 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9790 
9791 	/*
9792 	 * Features register requires special care for devices that use
9793 	 * Serial ATA bridge - they need an explicit specification of
9794 	 * the data transfer direction for Packet DMA commands.
9795 	 * Setting this bit is harmless if DMA is not used.
9796 	 *
9797 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9798 	 * spec they follow.
9799 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9800 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9801 	 * ATA/ATAPI-7 support is explicitly indicated.
9802 	 */
9803 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9804 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9805 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9806 		/*
9807 		 * Specification of major version is valid and version 7
9808 		 * is supported. It does automatically imply that all
9809 		 * spec features are supported. For now, we assume that
9810 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9811 		 */
9812 		if ((sdinfo->satadrv_id.ai_dirdma &
9813 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9814 			if (scmd->satacmd_flags.sata_data_direction ==
9815 			    SATA_DIR_READ)
9816 			scmd->satacmd_features_reg |=
9817 			    SATA_ATAPI_F_DATA_DIR_READ;
9818 		}
9819 	}
9820 }
9821 
9822 
9823 #ifdef SATA_DEBUG
9824 
9825 /* Display 18 bytes of Inquiry data */
9826 static void
9827 sata_show_inqry_data(uint8_t *buf)
9828 {
9829 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9830 	uint8_t *p;
9831 
9832 	cmn_err(CE_NOTE, "Inquiry data:");
9833 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9834 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9835 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9836 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9837 	    SATA_ATAPI_TRANS_VERSION(inq));
9838 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9839 	    inq->inq_rdf, inq->inq_aenc);
9840 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9841 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9842 	p = (uint8_t *)inq->inq_vid;
9843 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9844 	    "%02x %02x %02x %02x",
9845 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9846 	p = (uint8_t *)inq->inq_vid;
9847 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9848 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9849 
9850 	p = (uint8_t *)inq->inq_pid;
9851 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9852 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9853 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9854 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9855 	p = (uint8_t *)inq->inq_pid;
9856 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9857 	    "%c %c %c %c %c %c %c %c",
9858 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9859 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9860 
9861 	p = (uint8_t *)inq->inq_revision;
9862 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9863 	    p[0], p[1], p[2], p[3]);
9864 	p = (uint8_t *)inq->inq_revision;
9865 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9866 	    p[0], p[1], p[2], p[3]);
9867 
9868 }
9869 
9870 
9871 static void
9872 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9873 {
9874 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9875 
9876 	if (scsi_pkt == NULL)
9877 		return;
9878 	if (count != 0) {
9879 		/* saving cdb */
9880 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9881 		    SATA_ATAPI_MAX_CDB_LEN);
9882 		bcopy(scsi_pkt->pkt_cdbp,
9883 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9884 	} else {
9885 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9886 		    sts_sensedata,
9887 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9888 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9889 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9890 		    scsi_pkt->pkt_reason;
9891 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9892 		    spx->txlt_sata_pkt->satapkt_reason;
9893 
9894 		if (++sata_atapi_trace_index >= 64)
9895 			sata_atapi_trace_index = 0;
9896 	}
9897 }
9898 
9899 #endif
9900 
9901 /*
9902  * Fetch inquiry data from ATAPI device
9903  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9904  *
9905  * Note:
9906  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9907  * where the caller expects to see the inquiry data.
9908  *
9909  */
9910 
9911 static int
9912 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9913     sata_address_t *saddr, struct scsi_inquiry *inq)
9914 {
9915 	sata_pkt_txlate_t *spx;
9916 	sata_pkt_t *spkt;
9917 	struct buf *bp;
9918 	sata_drive_info_t *sdinfo;
9919 	sata_cmd_t *scmd;
9920 	int rval;
9921 	uint8_t *rqsp;
9922 	dev_info_t *dip = SATA_DIP(sata_hba);
9923 #ifdef SATA_DEBUG
9924 	char msg_buf[MAXPATHLEN];
9925 #endif
9926 	kmutex_t *cport_mutex;
9927 
9928 	ASSERT(sata_hba != NULL);
9929 
9930 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9931 	spx->txlt_sata_hba_inst = sata_hba;
9932 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9933 	spkt = sata_pkt_alloc(spx, NULL);
9934 	if (spkt == NULL) {
9935 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9936 		return (SATA_FAILURE);
9937 	}
9938 	/* address is needed now */
9939 	spkt->satapkt_device.satadev_addr = *saddr;
9940 
9941 	/* scsi_inquiry size buffer */
9942 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9943 	if (bp == NULL) {
9944 		sata_pkt_free(spx);
9945 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9946 		SATA_LOG_D((sata_hba, CE_WARN,
9947 		    "sata_get_atapi_inquiry_data: "
9948 		    "cannot allocate data buffer"));
9949 		return (SATA_FAILURE);
9950 	}
9951 	bp_mapin(bp); /* make data buffer accessible */
9952 
9953 	scmd = &spkt->satapkt_cmd;
9954 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9955 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9956 
9957 	/* Use synchronous mode */
9958 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9959 	spkt->satapkt_comp = NULL;
9960 	spkt->satapkt_time = sata_default_pkt_time;
9961 
9962 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9963 
9964 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9965 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9966 
9967 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9968 	mutex_enter(cport_mutex);
9969 	sdinfo = sata_get_device_info(sata_hba,
9970 	    &spx->txlt_sata_pkt->satapkt_device);
9971 	if (sdinfo == NULL) {
9972 		/* we have to be carefull about the disapearing device */
9973 		mutex_exit(cport_mutex);
9974 		rval = SATA_FAILURE;
9975 		goto cleanup;
9976 	}
9977 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9978 
9979 	/*
9980 	 * Set-up acdb. This works for atapi transport version 2 and later.
9981 	 */
9982 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9983 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9984 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
9985 	scmd->satacmd_acdb[1] = 0x00;
9986 	scmd->satacmd_acdb[2] = 0x00;
9987 	scmd->satacmd_acdb[3] = 0x00;
9988 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9989 	scmd->satacmd_acdb[5] = 0x00;
9990 
9991 	sata_fixed_sense_data_preset(
9992 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9993 
9994 	/* Transfer command to HBA */
9995 	if (sata_hba_start(spx, &rval) != 0) {
9996 		/* Pkt not accepted for execution */
9997 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9998 		    "sata_get_atapi_inquiry_data: "
9999 		    "Packet not accepted for execution - ret: %02x", rval);
10000 		mutex_exit(cport_mutex);
10001 		rval = SATA_FAILURE;
10002 		goto cleanup;
10003 	}
10004 	mutex_exit(cport_mutex);
10005 
10006 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10007 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10008 		    "sata_get_atapi_inquiry_data: "
10009 		    "Packet completed successfully - ret: %02x", rval);
10010 		if (spx->txlt_buf_dma_handle != NULL) {
10011 			/*
10012 			 * Sync buffer. Handle is in usual place in translate
10013 			 * struct.
10014 			 */
10015 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10016 			    DDI_DMA_SYNC_FORCPU);
10017 			ASSERT(rval == DDI_SUCCESS);
10018 		}
10019 
10020 		if (sata_check_for_dma_error(dip, spx)) {
10021 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10022 			rval = SATA_FAILURE;
10023 		} else {
10024 			/*
10025 			 * Normal completion - copy data into caller's buffer
10026 			 */
10027 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10028 			    sizeof (struct scsi_inquiry));
10029 #ifdef SATA_DEBUG
10030 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10031 				sata_show_inqry_data((uint8_t *)inq);
10032 			}
10033 #endif
10034 			rval = SATA_SUCCESS;
10035 		}
10036 	} else {
10037 		/*
10038 		 * Something went wrong - analyze return - check rqsense data
10039 		 */
10040 		rval = SATA_FAILURE;
10041 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10042 			/*
10043 			 * ARQ data hopefull show something other than NO SENSE
10044 			 */
10045 			rqsp = scmd->satacmd_rqsense;
10046 #ifdef SATA_DEBUG
10047 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10048 				msg_buf[0] = '\0';
10049 				(void) snprintf(msg_buf, MAXPATHLEN,
10050 				    "ATAPI packet completion reason: %02x\n"
10051 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10052 				    "          %02x %02x %02x %02x %02x %02x\n"
10053 				    "          %02x %02x %02x %02x %02x %02x",
10054 				    spkt->satapkt_reason,
10055 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10056 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10057 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10058 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10059 				    rqsp[16], rqsp[17]);
10060 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10061 				    "%s", msg_buf);
10062 			}
10063 #endif
10064 		} else {
10065 			switch (spkt->satapkt_reason) {
10066 			case SATA_PKT_PORT_ERROR:
10067 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10068 				    "sata_get_atapi_inquiry_data: "
10069 				    "packet reason: port error", NULL);
10070 				break;
10071 
10072 			case SATA_PKT_TIMEOUT:
10073 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10074 				    "sata_get_atapi_inquiry_data: "
10075 				    "packet reason: timeout", NULL);
10076 				break;
10077 
10078 			case SATA_PKT_ABORTED:
10079 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10080 				    "sata_get_atapi_inquiry_data: "
10081 				    "packet reason: aborted", NULL);
10082 				break;
10083 
10084 			case SATA_PKT_RESET:
10085 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10086 				    "sata_get_atapi_inquiry_data: "
10087 				    "packet reason: reset\n", NULL);
10088 				break;
10089 			default:
10090 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10091 				    "sata_get_atapi_inquiry_data: "
10092 				    "invalid packet reason: %02x\n",
10093 				    spkt->satapkt_reason);
10094 				break;
10095 			}
10096 		}
10097 	}
10098 cleanup:
10099 	sata_free_local_buffer(spx);
10100 	sata_pkt_free(spx);
10101 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10102 	return (rval);
10103 }
10104 
10105 
10106 
10107 
10108 
10109 #if 0
10110 #ifdef SATA_DEBUG
10111 
10112 /*
10113  * Test ATAPI packet command.
10114  * Single threaded test: send packet command in synch mode, process completion
10115  *
10116  */
10117 static void
10118 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10119 {
10120 	sata_pkt_txlate_t *spx;
10121 	sata_pkt_t *spkt;
10122 	struct buf *bp;
10123 	sata_device_t sata_device;
10124 	sata_drive_info_t *sdinfo;
10125 	sata_cmd_t *scmd;
10126 	int rval;
10127 	uint8_t *rqsp;
10128 
10129 	ASSERT(sata_hba_inst != NULL);
10130 	sata_device.satadev_addr.cport = cport;
10131 	sata_device.satadev_addr.pmport = 0;
10132 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10133 	sata_device.satadev_rev = SATA_DEVICE_REV;
10134 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10135 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10136 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10137 	if (sdinfo == NULL) {
10138 		sata_log(sata_hba_inst, CE_WARN,
10139 		    "sata_test_atapi_packet_command: "
10140 		    "no device info for cport %d",
10141 		    sata_device.satadev_addr.cport);
10142 		return;
10143 	}
10144 
10145 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10146 	spx->txlt_sata_hba_inst = sata_hba_inst;
10147 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10148 	spkt = sata_pkt_alloc(spx, NULL);
10149 	if (spkt == NULL) {
10150 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10151 		return;
10152 	}
10153 	/* address is needed now */
10154 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10155 
10156 	/* 1024k buffer */
10157 	bp = sata_alloc_local_buffer(spx, 1024);
10158 	if (bp == NULL) {
10159 		sata_pkt_free(spx);
10160 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10161 		sata_log(sata_hba_inst, CE_WARN,
10162 		    "sata_test_atapi_packet_command: "
10163 		    "cannot allocate data buffer");
10164 		return;
10165 	}
10166 	bp_mapin(bp); /* make data buffer accessible */
10167 
10168 	scmd = &spkt->satapkt_cmd;
10169 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10170 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10171 
10172 	/* Use synchronous mode */
10173 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10174 
10175 	/* Synchronous mode, no callback - may be changed by the caller */
10176 	spkt->satapkt_comp = NULL;
10177 	spkt->satapkt_time = sata_default_pkt_time;
10178 
10179 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10180 
10181 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10182 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10183 
10184 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10185 
10186 	/* Set-up acdb. */
10187 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10188 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10189 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10190 	scmd->satacmd_acdb[1] = 0x00;
10191 	scmd->satacmd_acdb[2] = 0x00;
10192 	scmd->satacmd_acdb[3] = 0x00;
10193 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10194 	scmd->satacmd_acdb[5] = 0x00;
10195 
10196 	sata_fixed_sense_data_preset(
10197 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10198 
10199 	/* Transfer command to HBA */
10200 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10201 	if (sata_hba_start(spx, &rval) != 0) {
10202 		/* Pkt not accepted for execution */
10203 		sata_log(sata_hba_inst, CE_WARN,
10204 		    "sata_test_atapi_packet_command: "
10205 		    "Packet not accepted for execution - ret: %02x", rval);
10206 		mutex_exit(
10207 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10208 		goto cleanup;
10209 	}
10210 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10211 
10212 	if (spx->txlt_buf_dma_handle != NULL) {
10213 		/*
10214 		 * Sync buffer. Handle is in usual place in translate struct.
10215 		 */
10216 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10217 		    DDI_DMA_SYNC_FORCPU);
10218 		ASSERT(rval == DDI_SUCCESS);
10219 	}
10220 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10221 		sata_log(sata_hba_inst, CE_WARN,
10222 		    "sata_test_atapi_packet_command: "
10223 		    "Packet completed successfully");
10224 		/*
10225 		 * Normal completion - show inquiry data
10226 		 */
10227 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10228 	} else {
10229 		/*
10230 		 * Something went wrong - analyze return - check rqsense data
10231 		 */
10232 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10233 			/*
10234 			 * ARQ data hopefull show something other than NO SENSE
10235 			 */
10236 			rqsp = scmd->satacmd_rqsense;
10237 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10238 			    "ATAPI packet completion reason: %02x\n"
10239 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10240 			    "          %02x %02x %02x %02x %02x %02x "
10241 			    "          %02x %02x %02x %02x %02x %02x\n",
10242 			    spkt->satapkt_reason,
10243 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10244 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10245 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10246 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10247 			    rqsp[16], rqsp[17]);
10248 		} else {
10249 			switch (spkt->satapkt_reason) {
10250 			case SATA_PKT_PORT_ERROR:
10251 				sata_log(sata_hba_inst, CE_WARN,
10252 				    "sata_test_atapi_packet_command: "
10253 				    "packet reason: port error\n");
10254 				break;
10255 
10256 			case SATA_PKT_TIMEOUT:
10257 				sata_log(sata_hba_inst, CE_WARN,
10258 				    "sata_test_atapi_packet_command: "
10259 				    "packet reason: timeout\n");
10260 				break;
10261 
10262 			case SATA_PKT_ABORTED:
10263 				sata_log(sata_hba_inst, CE_WARN,
10264 				    "sata_test_atapi_packet_command: "
10265 				    "packet reason: aborted\n");
10266 				break;
10267 
10268 			case SATA_PKT_RESET:
10269 				sata_log(sata_hba_inst, CE_WARN,
10270 				    "sata_test_atapi_packet_command: "
10271 				    "packet reason: reset\n");
10272 				break;
10273 			default:
10274 				sata_log(sata_hba_inst, CE_WARN,
10275 				    "sata_test_atapi_packet_command: "
10276 				    "invalid packet reason: %02x\n",
10277 				    spkt->satapkt_reason);
10278 				break;
10279 			}
10280 		}
10281 	}
10282 cleanup:
10283 	sata_free_local_buffer(spx);
10284 	sata_pkt_free(spx);
10285 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10286 }
10287 
10288 #endif /* SATA_DEBUG */
10289 #endif /* 1 */
10290 
10291 
10292 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10293 
10294 /*
10295  * Validate sata_tran info
10296  * SATA_FAILURE returns if structure is inconsistent or structure revision
10297  * does not match one used by the framework.
10298  *
10299  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10300  * required function pointers.
10301  * Returns SATA_FAILURE otherwise.
10302  */
10303 static int
10304 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10305 {
10306 	/*
10307 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10308 	 * of the SATA interface.
10309 	 */
10310 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10311 		sata_log(NULL, CE_WARN,
10312 		    "sata: invalid sata_hba_tran version %d for driver %s",
10313 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10314 		return (SATA_FAILURE);
10315 	}
10316 
10317 	if (dip != sata_tran->sata_tran_hba_dip) {
10318 		SATA_LOG_D((NULL, CE_WARN,
10319 		    "sata: inconsistent sata_tran_hba_dip "
10320 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10321 		return (SATA_FAILURE);
10322 	}
10323 
10324 	if (sata_tran->sata_tran_probe_port == NULL ||
10325 	    sata_tran->sata_tran_start == NULL ||
10326 	    sata_tran->sata_tran_abort == NULL ||
10327 	    sata_tran->sata_tran_reset_dport == NULL ||
10328 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10329 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10330 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10331 	    NULL) {
10332 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10333 		    "required functions"));
10334 	}
10335 	return (SATA_SUCCESS);
10336 }
10337 
10338 /*
10339  * Remove HBA instance from sata_hba_list.
10340  */
10341 static void
10342 sata_remove_hba_instance(dev_info_t *dip)
10343 {
10344 	sata_hba_inst_t	*sata_hba_inst;
10345 
10346 	mutex_enter(&sata_mutex);
10347 	for (sata_hba_inst = sata_hba_list;
10348 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10349 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10350 		if (sata_hba_inst->satahba_dip == dip)
10351 			break;
10352 	}
10353 
10354 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10355 #ifdef SATA_DEBUG
10356 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10357 		    "unknown HBA instance\n");
10358 #endif
10359 		ASSERT(FALSE);
10360 	}
10361 	if (sata_hba_inst == sata_hba_list) {
10362 		sata_hba_list = sata_hba_inst->satahba_next;
10363 		if (sata_hba_list) {
10364 			sata_hba_list->satahba_prev =
10365 			    (struct sata_hba_inst *)NULL;
10366 		}
10367 		if (sata_hba_inst == sata_hba_list_tail) {
10368 			sata_hba_list_tail = NULL;
10369 		}
10370 	} else if (sata_hba_inst == sata_hba_list_tail) {
10371 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10372 		if (sata_hba_list_tail) {
10373 			sata_hba_list_tail->satahba_next =
10374 			    (struct sata_hba_inst *)NULL;
10375 		}
10376 	} else {
10377 		sata_hba_inst->satahba_prev->satahba_next =
10378 		    sata_hba_inst->satahba_next;
10379 		sata_hba_inst->satahba_next->satahba_prev =
10380 		    sata_hba_inst->satahba_prev;
10381 	}
10382 	mutex_exit(&sata_mutex);
10383 }
10384 
10385 /*
10386  * Probe all SATA ports of the specified HBA instance.
10387  * The assumption is that there are no target and attachment point minor nodes
10388  * created by the boot subsystems, so we do not need to prune device tree.
10389  *
10390  * This function is called only from sata_hba_attach(). It does not have to
10391  * be protected by controller mutex, because the hba_attached flag is not set
10392  * yet and no one would be touching this HBA instance other than this thread.
10393  * Determines if port is active and what type of the device is attached
10394  * (if any). Allocates necessary structures for each port.
10395  *
10396  * An AP (Attachement Point) node is created for each SATA device port even
10397  * when there is no device attached.
10398  */
10399 
10400 static 	void
10401 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10402 {
10403 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10404 	int			ncport;
10405 	sata_cport_info_t 	*cportinfo;
10406 	sata_drive_info_t	*drive;
10407 	sata_device_t		sata_device;
10408 	int			rval;
10409 	dev_t			minor_number;
10410 	char			name[16];
10411 	clock_t			start_time, cur_time;
10412 
10413 	/*
10414 	 * Probe controller ports first, to find port status and
10415 	 * any port multiplier attached.
10416 	 */
10417 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10418 		/* allocate cport structure */
10419 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10420 		ASSERT(cportinfo != NULL);
10421 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10422 
10423 		mutex_enter(&cportinfo->cport_mutex);
10424 
10425 		cportinfo->cport_addr.cport = ncport;
10426 		cportinfo->cport_addr.pmport = 0;
10427 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10428 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10429 		cportinfo->cport_state |= SATA_STATE_PROBING;
10430 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10431 
10432 		/*
10433 		 * Regardless if a port is usable or not, create
10434 		 * an attachment point
10435 		 */
10436 		mutex_exit(&cportinfo->cport_mutex);
10437 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10438 		    ncport, 0, SATA_ADDR_CPORT);
10439 		(void) sprintf(name, "%d", ncport);
10440 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10441 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10442 		    DDI_SUCCESS) {
10443 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10444 			    "cannot create SATA attachment point for port %d",
10445 			    ncport);
10446 		}
10447 
10448 		/* Probe port */
10449 		start_time = ddi_get_lbolt();
10450 	reprobe_cport:
10451 		sata_device.satadev_addr.cport = ncport;
10452 		sata_device.satadev_addr.pmport = 0;
10453 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10454 		sata_device.satadev_rev = SATA_DEVICE_REV;
10455 
10456 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10457 		    (dip, &sata_device);
10458 
10459 		mutex_enter(&cportinfo->cport_mutex);
10460 		cportinfo->cport_scr = sata_device.satadev_scr;
10461 		if (rval != SATA_SUCCESS) {
10462 			/* Something went wrong? Fail the port */
10463 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10464 			mutex_exit(&cportinfo->cport_mutex);
10465 			continue;
10466 		}
10467 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10468 		cportinfo->cport_state |= SATA_STATE_PROBED;
10469 		cportinfo->cport_dev_type = sata_device.satadev_type;
10470 
10471 		cportinfo->cport_state |= SATA_STATE_READY;
10472 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10473 			mutex_exit(&cportinfo->cport_mutex);
10474 			continue;
10475 		}
10476 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10477 			/*
10478 			 * There is some device attached.
10479 			 * Allocate device info structure
10480 			 */
10481 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10482 				mutex_exit(&cportinfo->cport_mutex);
10483 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10484 				    kmem_zalloc(sizeof (sata_drive_info_t),
10485 				    KM_SLEEP);
10486 				mutex_enter(&cportinfo->cport_mutex);
10487 			}
10488 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10489 			drive->satadrv_addr = cportinfo->cport_addr;
10490 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10491 			drive->satadrv_type = cportinfo->cport_dev_type;
10492 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10493 
10494 			mutex_exit(&cportinfo->cport_mutex);
10495 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10496 			    SATA_SUCCESS) {
10497 				/*
10498 				 * Plugged device was not correctly identified.
10499 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10500 				 */
10501 				cur_time = ddi_get_lbolt();
10502 				if ((cur_time - start_time) <
10503 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10504 					/* sleep for a while */
10505 					delay(drv_usectohz(
10506 					    SATA_DEV_RETRY_DLY));
10507 					goto reprobe_cport;
10508 				}
10509 			}
10510 		} else { /* SATA_DTYPE_PMULT */
10511 			mutex_exit(&cportinfo->cport_mutex);
10512 
10513 			/* Allocate sata_pmult_info and sata_pmport_info */
10514 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10515 			    SATA_SUCCESS)
10516 				continue;
10517 
10518 			/* Log the information of the port multiplier */
10519 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10520 
10521 			/* Probe its pmports */
10522 			sata_probe_pmports(sata_hba_inst, ncport);
10523 		}
10524 	}
10525 }
10526 
10527 /*
10528  * Probe all device ports behind a port multiplier.
10529  *
10530  * PMult-related structure should be allocated before by sata_alloc_pmult().
10531  *
10532  * NOTE1: Only called from sata_probe_ports()
10533  * NOTE2: No mutex should be hold.
10534  */
10535 static void
10536 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10537 {
10538 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10539 	sata_pmult_info_t	*pmultinfo = NULL;
10540 	sata_pmport_info_t 	*pmportinfo = NULL;
10541 	sata_drive_info_t	*drive = NULL;
10542 	sata_device_t		sata_device;
10543 
10544 	clock_t			start_time, cur_time;
10545 	int			npmport;
10546 	int			rval;
10547 
10548 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10549 
10550 	/* Probe Port Multiplier ports */
10551 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10552 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10553 		start_time = ddi_get_lbolt();
10554 reprobe_pmport:
10555 		sata_device.satadev_addr.cport = ncport;
10556 		sata_device.satadev_addr.pmport = npmport;
10557 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10558 		sata_device.satadev_rev = SATA_DEVICE_REV;
10559 
10560 		/* Let HBA driver probe it. */
10561 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10562 		    (dip, &sata_device);
10563 		mutex_enter(&pmportinfo->pmport_mutex);
10564 
10565 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10566 
10567 		if (rval != SATA_SUCCESS) {
10568 			pmportinfo->pmport_state =
10569 			    SATA_PSTATE_FAILED;
10570 			mutex_exit(&pmportinfo->pmport_mutex);
10571 			continue;
10572 		}
10573 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10574 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10575 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10576 
10577 		pmportinfo->pmport_state |= SATA_STATE_READY;
10578 		if (pmportinfo->pmport_dev_type ==
10579 		    SATA_DTYPE_NONE) {
10580 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10581 			    "no device found at port %d:%d", ncport, npmport);
10582 			mutex_exit(&pmportinfo->pmport_mutex);
10583 			continue;
10584 		}
10585 		/* Port multipliers cannot be chained */
10586 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10587 		/*
10588 		 * There is something attached to Port
10589 		 * Multiplier device port
10590 		 * Allocate device info structure
10591 		 */
10592 		if (pmportinfo->pmport_sata_drive == NULL) {
10593 			mutex_exit(&pmportinfo->pmport_mutex);
10594 			pmportinfo->pmport_sata_drive =
10595 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10596 			mutex_enter(&pmportinfo->pmport_mutex);
10597 		}
10598 		drive = pmportinfo->pmport_sata_drive;
10599 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10600 		drive->satadrv_addr.pmport = npmport;
10601 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10602 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10603 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10604 
10605 		mutex_exit(&pmportinfo->pmport_mutex);
10606 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10607 
10608 		if (rval != SATA_SUCCESS) {
10609 			/*
10610 			 * Plugged device was not correctly identified.
10611 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10612 			 */
10613 			cur_time = ddi_get_lbolt();
10614 			if ((cur_time - start_time) < drv_usectohz(
10615 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10616 				/* sleep for a while */
10617 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10618 				goto reprobe_pmport;
10619 			}
10620 		}
10621 	}
10622 }
10623 
10624 /*
10625  * Add SATA device for specified HBA instance & port (SCSI target
10626  * device nodes).
10627  * This function is called (indirectly) only from sata_hba_attach().
10628  * A target node is created when there is a supported type device attached,
10629  * but may be removed if it cannot be put online.
10630  *
10631  * This function cannot be called from an interrupt context.
10632  *
10633  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10634  *
10635  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10636  * device identification failed - adding a device could be retried.
10637  *
10638  */
10639 static 	int
10640 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10641     sata_device_t *sata_device)
10642 {
10643 	sata_cport_info_t 	*cportinfo;
10644 	sata_pmult_info_t	*pminfo;
10645 	sata_pmport_info_t	*pmportinfo;
10646 	dev_info_t		*cdip;		/* child dip */
10647 	sata_address_t		*saddr = &sata_device->satadev_addr;
10648 	uint8_t			cport, pmport;
10649 	int			rval;
10650 
10651 	cport = saddr->cport;
10652 	pmport = saddr->pmport;
10653 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10654 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10655 
10656 	/*
10657 	 * Some device is attached to a controller port.
10658 	 * We rely on controllers distinquishing between no-device,
10659 	 * attached port multiplier and other kind of attached device.
10660 	 * We need to get Identify Device data and determine
10661 	 * positively the dev type before trying to attach
10662 	 * the target driver.
10663 	 */
10664 	sata_device->satadev_rev = SATA_DEVICE_REV;
10665 	switch (saddr->qual) {
10666 	case SATA_ADDR_CPORT:
10667 		/*
10668 		 * Add a non-port-multiplier device at controller port.
10669 		 */
10670 		saddr->qual = SATA_ADDR_DCPORT;
10671 
10672 		rval = sata_probe_device(sata_hba_inst, sata_device);
10673 		if (rval != SATA_SUCCESS ||
10674 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10675 			return (SATA_FAILURE);
10676 
10677 		mutex_enter(&cportinfo->cport_mutex);
10678 		sata_show_drive_info(sata_hba_inst,
10679 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10680 
10681 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10682 			/*
10683 			 * Could not determine device type or
10684 			 * a device is not supported.
10685 			 * Degrade this device to unknown.
10686 			 */
10687 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10688 			mutex_exit(&cportinfo->cport_mutex);
10689 			return (SATA_SUCCESS);
10690 		}
10691 		cportinfo->cport_dev_type = sata_device->satadev_type;
10692 		cportinfo->cport_tgtnode_clean = B_TRUE;
10693 		mutex_exit(&cportinfo->cport_mutex);
10694 
10695 		/*
10696 		 * Initialize device to the desired state. Even if it
10697 		 * fails, the device will still attach but syslog
10698 		 * will show the warning.
10699 		 */
10700 		if (sata_initialize_device(sata_hba_inst,
10701 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10702 			/* Retry */
10703 			rval = sata_initialize_device(sata_hba_inst,
10704 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10705 
10706 			if (rval == SATA_RETRY)
10707 				sata_log(sata_hba_inst, CE_WARN,
10708 				    "SATA device at port %d - "
10709 				    "default device features could not be set."
10710 				    " Device may not operate as expected.",
10711 				    cport);
10712 		}
10713 
10714 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10715 		if (cdip == NULL) {
10716 			/*
10717 			 * Attaching target node failed.
10718 			 * We retain sata_drive_info structure...
10719 			 */
10720 			return (SATA_SUCCESS);
10721 		}
10722 
10723 		mutex_enter(&cportinfo->cport_mutex);
10724 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10725 		    satadrv_state = SATA_STATE_READY;
10726 		mutex_exit(&cportinfo->cport_mutex);
10727 
10728 		break;
10729 
10730 	case SATA_ADDR_PMPORT:
10731 		saddr->qual = SATA_ADDR_DPMPORT;
10732 
10733 		mutex_enter(&cportinfo->cport_mutex);
10734 		/* It must be a Port Multiplier at the controller port */
10735 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10736 
10737 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10738 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10739 		mutex_exit(&cportinfo->cport_mutex);
10740 
10741 		rval = sata_probe_device(sata_hba_inst, sata_device);
10742 		if (rval != SATA_SUCCESS ||
10743 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10744 			return (SATA_FAILURE);
10745 		}
10746 
10747 		mutex_enter(&pmportinfo->pmport_mutex);
10748 		sata_show_drive_info(sata_hba_inst,
10749 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10750 
10751 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10752 			/*
10753 			 * Could not determine device type.
10754 			 * Degrade this device to unknown.
10755 			 */
10756 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10757 			mutex_exit(&pmportinfo->pmport_mutex);
10758 			return (SATA_SUCCESS);
10759 		}
10760 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10761 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10762 		mutex_exit(&pmportinfo->pmport_mutex);
10763 
10764 		/*
10765 		 * Initialize device to the desired state.
10766 		 * Even if it fails, the device will still
10767 		 * attach but syslog will show the warning.
10768 		 */
10769 		if (sata_initialize_device(sata_hba_inst,
10770 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10771 			/* Retry */
10772 			rval = sata_initialize_device(sata_hba_inst,
10773 			    pmportinfo->pmport_sata_drive);
10774 
10775 			if (rval == SATA_RETRY)
10776 				sata_log(sata_hba_inst, CE_WARN,
10777 				    "SATA device at port %d:%d - "
10778 				    "default device features could not be set."
10779 				    " Device may not operate as expected.",
10780 				    cport, pmport);
10781 		}
10782 
10783 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10784 		if (cdip == NULL) {
10785 			/*
10786 			 * Attaching target node failed.
10787 			 * We retain sata_drive_info structure...
10788 			 */
10789 			return (SATA_SUCCESS);
10790 		}
10791 		mutex_enter(&pmportinfo->pmport_mutex);
10792 		pmportinfo->pmport_sata_drive->satadrv_state |=
10793 		    SATA_STATE_READY;
10794 		mutex_exit(&pmportinfo->pmport_mutex);
10795 
10796 		break;
10797 
10798 	default:
10799 		return (SATA_FAILURE);
10800 	}
10801 
10802 	return (SATA_SUCCESS);
10803 }
10804 
10805 /*
10806  * Clean up target node at specific address.
10807  *
10808  * NOTE: No Mutex should be hold.
10809  */
10810 static int
10811 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10812     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10813 {
10814 	uint8_t cport, pmport, qual;
10815 	dev_info_t *tdip;
10816 
10817 	cport = sata_device->satadev_addr.cport;
10818 	pmport = sata_device->satadev_addr.pmport;
10819 	qual = sata_device->satadev_addr.qual;
10820 
10821 	if (qual == SATA_ADDR_DCPORT) {
10822 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10823 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10824 	} else {
10825 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10826 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10827 		    cport, pmport));
10828 	}
10829 
10830 	/* We are addressing attached device, not a port */
10831 	sata_device->satadev_addr.qual =
10832 	    sdinfo->satadrv_addr.qual;
10833 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10834 	    &sata_device->satadev_addr);
10835 	if (tdip != NULL && ndi_devi_offline(tdip,
10836 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10837 		/*
10838 		 * Problem :
10839 		 * The target node remained attached.
10840 		 * This happens when the device file was open
10841 		 * or a node was waiting for resources.
10842 		 * Cannot do anything about it.
10843 		 */
10844 		if (qual == SATA_ADDR_DCPORT) {
10845 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10846 			    "sata_hba_ioctl: disconnect: could "
10847 			    "not unconfigure device before "
10848 			    "disconnecting the SATA port %d",
10849 			    cport));
10850 		} else {
10851 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10852 			    "sata_hba_ioctl: disconnect: could "
10853 			    "not unconfigure device before "
10854 			    "disconnecting the SATA port %d:%d",
10855 			    cport, pmport));
10856 		}
10857 		/*
10858 		 * Set DEVICE REMOVED state in the target
10859 		 * node. It will prevent access to the device
10860 		 * even when a new device is attached, until
10861 		 * the old target node is released, removed and
10862 		 * recreated for a new  device.
10863 		 */
10864 		sata_set_device_removed(tdip);
10865 
10866 		/*
10867 		 * Instruct event daemon to try the target
10868 		 * node cleanup later.
10869 		 */
10870 		sata_set_target_node_cleanup(
10871 		    sata_hba_inst, &sata_device->satadev_addr);
10872 	}
10873 
10874 
10875 	return (SATA_SUCCESS);
10876 }
10877 
10878 
10879 /*
10880  * Create scsi target node for attached device, create node properties and
10881  * attach the node.
10882  * The node could be removed if the device onlining fails.
10883  *
10884  * A dev_info_t pointer is returned if operation is successful, NULL is
10885  * returned otherwise.
10886  */
10887 
10888 static dev_info_t *
10889 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10890 			sata_address_t *sata_addr)
10891 {
10892 	dev_info_t *cdip = NULL;
10893 	int rval;
10894 	char *nname = NULL;
10895 	char **compatible = NULL;
10896 	int ncompatible;
10897 	struct scsi_inquiry inq;
10898 	sata_device_t sata_device;
10899 	sata_drive_info_t *sdinfo;
10900 	int target;
10901 	int i;
10902 
10903 	sata_device.satadev_rev = SATA_DEVICE_REV;
10904 	sata_device.satadev_addr = *sata_addr;
10905 
10906 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10907 
10908 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10909 
10910 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10911 	    sata_addr->pmport, sata_addr->qual);
10912 
10913 	if (sdinfo == NULL) {
10914 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10915 		    sata_addr->cport)));
10916 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10917 		    "sata_create_target_node: no sdinfo for target %x",
10918 		    target));
10919 		return (NULL);
10920 	}
10921 
10922 	/*
10923 	 * create or get scsi inquiry data, expected by
10924 	 * scsi_hba_nodename_compatible_get()
10925 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10926 	 * ATAPI devices respond directly to Inquiry request.
10927 	 */
10928 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10929 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10930 		    (uint8_t *)&inq);
10931 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10932 		    sata_addr->cport)));
10933 	} else { /* Assume supported ATAPI device */
10934 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10935 		    sata_addr->cport)));
10936 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10937 		    &inq) == SATA_FAILURE)
10938 			return (NULL);
10939 		/*
10940 		 * Save supported ATAPI transport version
10941 		 */
10942 		sdinfo->satadrv_atapi_trans_ver =
10943 		    SATA_ATAPI_TRANS_VERSION(&inq);
10944 	}
10945 
10946 	/* determine the node name and compatible */
10947 	scsi_hba_nodename_compatible_get(&inq, NULL,
10948 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10949 
10950 #ifdef SATA_DEBUG
10951 	if (sata_debug_flags & SATA_DBG_NODES) {
10952 		if (nname == NULL) {
10953 			cmn_err(CE_NOTE, "sata_create_target_node: "
10954 			    "cannot determine nodename for target %d\n",
10955 			    target);
10956 		} else {
10957 			cmn_err(CE_WARN, "sata_create_target_node: "
10958 			    "target %d nodename: %s\n", target, nname);
10959 		}
10960 		if (compatible == NULL) {
10961 			cmn_err(CE_WARN,
10962 			    "sata_create_target_node: no compatible name\n");
10963 		} else {
10964 			for (i = 0; i < ncompatible; i++) {
10965 				cmn_err(CE_WARN, "sata_create_target_node: "
10966 				    "compatible name: %s\n", compatible[i]);
10967 			}
10968 		}
10969 	}
10970 #endif
10971 
10972 	/* if nodename can't be determined, log error and exit */
10973 	if (nname == NULL) {
10974 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10975 		    "sata_create_target_node: cannot determine nodename "
10976 		    "for target %d\n", target));
10977 		scsi_hba_nodename_compatible_free(nname, compatible);
10978 		return (NULL);
10979 	}
10980 	/*
10981 	 * Create scsi target node
10982 	 */
10983 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10984 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10985 	    "device-type", "scsi");
10986 
10987 	if (rval != DDI_PROP_SUCCESS) {
10988 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10989 		    "updating device_type prop failed %d", rval));
10990 		goto fail;
10991 	}
10992 
10993 	/*
10994 	 * Create target node properties: target & lun
10995 	 */
10996 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10997 	if (rval != DDI_PROP_SUCCESS) {
10998 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10999 		    "updating target prop failed %d", rval));
11000 		goto fail;
11001 	}
11002 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11003 	if (rval != DDI_PROP_SUCCESS) {
11004 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11005 		    "updating target prop failed %d", rval));
11006 		goto fail;
11007 	}
11008 
11009 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11010 		/*
11011 		 * Add "variant" property
11012 		 */
11013 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11014 		    "variant", "atapi");
11015 		if (rval != DDI_PROP_SUCCESS) {
11016 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11017 			    "sata_create_target_node: variant atapi "
11018 			    "property could not be created: %d", rval));
11019 			goto fail;
11020 		}
11021 	}
11022 	/* decorate the node with compatible */
11023 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11024 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
11025 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11026 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
11027 		    (void *)cdip));
11028 		goto fail;
11029 	}
11030 
11031 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11032 		/*
11033 		 * Add "sata-phy" property
11034 		 */
11035 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11036 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11037 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11038 			    "sata_create_target_node: failed to create "
11039 			    "\"sata-phy\" property: port %d",
11040 			    sata_addr->cport));
11041 		}
11042 	}
11043 
11044 
11045 	/*
11046 	 * Now, try to attach the driver. If probing of the device fails,
11047 	 * the target node may be removed
11048 	 */
11049 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11050 
11051 	scsi_hba_nodename_compatible_free(nname, compatible);
11052 
11053 	if (rval == NDI_SUCCESS)
11054 		return (cdip);
11055 
11056 	/* target node was removed - are we sure? */
11057 	return (NULL);
11058 
11059 fail:
11060 	scsi_hba_nodename_compatible_free(nname, compatible);
11061 	ddi_prop_remove_all(cdip);
11062 	rval = ndi_devi_free(cdip);
11063 	if (rval != NDI_SUCCESS) {
11064 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11065 		    "node removal failed %d", rval));
11066 	}
11067 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11068 	    "cannot create target node for SATA device at port %d",
11069 	    sata_addr->cport);
11070 	return (NULL);
11071 }
11072 
11073 /*
11074  * Remove a target node.
11075  */
11076 static void
11077 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11078 			sata_address_t *sata_addr)
11079 {
11080 	dev_info_t *tdip;
11081 	uint8_t cport = sata_addr->cport;
11082 	uint8_t pmport = sata_addr->pmport;
11083 	uint8_t qual = sata_addr->qual;
11084 
11085 	/* Note the sata daemon uses the address of the port/pmport */
11086 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11087 
11088 	/* Remove target node */
11089 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11090 	if (tdip != NULL) {
11091 		/*
11092 		 * Target node exists.  Unconfigure device
11093 		 * then remove the target node (one ndi
11094 		 * operation).
11095 		 */
11096 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11097 			/*
11098 			 * PROBLEM - no device, but target node remained. This
11099 			 * happens when the file was open or node was waiting
11100 			 * for resources.
11101 			 */
11102 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11103 			    "sata_remove_target_node: "
11104 			    "Failed to remove target node for "
11105 			    "detached SATA device."));
11106 			/*
11107 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11108 			 * re-check first that the node still exists.
11109 			 */
11110 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11111 			    cport, pmport);
11112 			if (tdip != NULL) {
11113 				sata_set_device_removed(tdip);
11114 				/*
11115 				 * Instruct event daemon to retry the cleanup
11116 				 * later.
11117 				 */
11118 				sata_set_target_node_cleanup(sata_hba_inst,
11119 				    sata_addr);
11120 			}
11121 		}
11122 
11123 		if (qual == SATA_ADDR_CPORT)
11124 			sata_log(sata_hba_inst, CE_WARN,
11125 			    "SATA device detached at port %d", cport);
11126 		else
11127 			sata_log(sata_hba_inst, CE_WARN,
11128 			    "SATA device detached at port %d:%d",
11129 			    cport, pmport);
11130 	}
11131 #ifdef SATA_DEBUG
11132 	else {
11133 		if (qual == SATA_ADDR_CPORT)
11134 			sata_log(sata_hba_inst, CE_WARN,
11135 			    "target node not found at port %d", cport);
11136 		else
11137 			sata_log(sata_hba_inst, CE_WARN,
11138 			    "target node not found at port %d:%d",
11139 			    cport, pmport);
11140 	}
11141 #endif
11142 }
11143 
11144 
11145 /*
11146  * Re-probe sata port, check for a device and attach info
11147  * structures when necessary. Identify Device data is fetched, if possible.
11148  * Assumption: sata address is already validated.
11149  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11150  * the presence of a device and its type.
11151  *
11152  * flag arg specifies that the function should try multiple times to identify
11153  * device type and to initialize it, or it should return immediately on failure.
11154  * SATA_DEV_IDENTIFY_RETRY - retry
11155  * SATA_DEV_IDENTIFY_NORETRY - no retry
11156  *
11157  * SATA_FAILURE is returned if one of the operations failed.
11158  *
11159  * This function cannot be called in interrupt context - it may sleep.
11160  *
11161  * Note: Port multiplier is supported.
11162  */
11163 static int
11164 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11165     int flag)
11166 {
11167 	sata_cport_info_t *cportinfo;
11168 	sata_pmult_info_t *pmultinfo;
11169 	sata_drive_info_t *sdinfo, *osdinfo;
11170 	boolean_t init_device = B_FALSE;
11171 	int prev_device_type = SATA_DTYPE_NONE;
11172 	int prev_device_settings = 0;
11173 	int prev_device_state = 0;
11174 	clock_t start_time;
11175 	int retry = B_FALSE;
11176 	uint8_t cport = sata_device->satadev_addr.cport;
11177 	int rval_probe, rval_init;
11178 
11179 	/*
11180 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11181 	 */
11182 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11183 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11184 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11185 
11186 	/* We only care about host sata cport for now */
11187 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11188 	    sata_device->satadev_addr.cport);
11189 
11190 	/*
11191 	 * If a port multiplier was previously attached (we have no idea it
11192 	 * still there or not), sata_reprobe_pmult() will handle it.
11193 	 */
11194 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11195 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11196 
11197 	/* Store sata_drive_info when a non-pmult device was attached. */
11198 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11199 	if (osdinfo != NULL) {
11200 		/*
11201 		 * We are re-probing port with a previously attached device.
11202 		 * Save previous device type and settings.
11203 		 */
11204 		prev_device_type = cportinfo->cport_dev_type;
11205 		prev_device_settings = osdinfo->satadrv_settings;
11206 		prev_device_state = osdinfo->satadrv_state;
11207 	}
11208 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11209 		start_time = ddi_get_lbolt();
11210 		retry = B_TRUE;
11211 	}
11212 retry_probe:
11213 
11214 	/* probe port */
11215 	mutex_enter(&cportinfo->cport_mutex);
11216 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11217 	cportinfo->cport_state |= SATA_STATE_PROBING;
11218 	mutex_exit(&cportinfo->cport_mutex);
11219 
11220 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11221 	    (SATA_DIP(sata_hba_inst), sata_device);
11222 
11223 	mutex_enter(&cportinfo->cport_mutex);
11224 	if (rval_probe != SATA_SUCCESS) {
11225 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11226 		mutex_exit(&cportinfo->cport_mutex);
11227 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11228 		    "SATA port %d probing failed",
11229 		    cportinfo->cport_addr.cport));
11230 		return (SATA_FAILURE);
11231 	}
11232 
11233 	/*
11234 	 * update sata port state and set device type
11235 	 */
11236 	sata_update_port_info(sata_hba_inst, sata_device);
11237 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11238 
11239 	/*
11240 	 * Sanity check - Port is active? Is the link active?
11241 	 * Is there any device attached?
11242 	 */
11243 	if ((cportinfo->cport_state &
11244 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11245 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11246 	    SATA_PORT_DEVLINK_UP) {
11247 		/*
11248 		 * Port in non-usable state or no link active/no device.
11249 		 * Free info structure if necessary (direct attached drive
11250 		 * only, for now!
11251 		 */
11252 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11253 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11254 		/* Add here differentiation for device attached or not */
11255 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11256 		mutex_exit(&cportinfo->cport_mutex);
11257 		if (sdinfo != NULL)
11258 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11259 		return (SATA_SUCCESS);
11260 	}
11261 
11262 	cportinfo->cport_state |= SATA_STATE_READY;
11263 	cportinfo->cport_state |= SATA_STATE_PROBED;
11264 
11265 	cportinfo->cport_dev_type = sata_device->satadev_type;
11266 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11267 
11268 	/*
11269 	 * If we are re-probing the port, there may be
11270 	 * sata_drive_info structure attached
11271 	 */
11272 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11273 
11274 		/*
11275 		 * There is no device, so remove device info structure,
11276 		 * if necessary.
11277 		 */
11278 		/* Device change: Drive -> None */
11279 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11280 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11281 		if (sdinfo != NULL) {
11282 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11283 			sata_log(sata_hba_inst, CE_WARN,
11284 			    "SATA device detached "
11285 			    "from port %d", cportinfo->cport_addr.cport);
11286 		}
11287 		mutex_exit(&cportinfo->cport_mutex);
11288 		return (SATA_SUCCESS);
11289 
11290 	}
11291 
11292 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11293 
11294 		/* Device (may) change: Drive -> Drive */
11295 		if (sdinfo == NULL) {
11296 			/*
11297 			 * There is some device attached, but there is
11298 			 * no sata_drive_info structure - allocate one
11299 			 */
11300 			mutex_exit(&cportinfo->cport_mutex);
11301 			sdinfo = kmem_zalloc(
11302 			    sizeof (sata_drive_info_t), KM_SLEEP);
11303 			mutex_enter(&cportinfo->cport_mutex);
11304 			/*
11305 			 * Recheck, that the port state did not change when we
11306 			 * released mutex.
11307 			 */
11308 			if (cportinfo->cport_state & SATA_STATE_READY) {
11309 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11310 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11311 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11312 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11313 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11314 			} else {
11315 				/*
11316 				 * Port is not in ready state, we
11317 				 * cannot attach a device.
11318 				 */
11319 				mutex_exit(&cportinfo->cport_mutex);
11320 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11321 				return (SATA_SUCCESS);
11322 			}
11323 			/*
11324 			 * Since we are adding device, presumably new one,
11325 			 * indicate that it  should be initalized,
11326 			 * as well as some internal framework states).
11327 			 */
11328 			init_device = B_TRUE;
11329 		}
11330 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11331 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11332 	} else {
11333 		/* Device change: Drive -> PMult */
11334 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11335 		if (sdinfo != NULL) {
11336 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11337 			sata_log(sata_hba_inst, CE_WARN,
11338 			    "SATA device detached "
11339 			    "from port %d", cportinfo->cport_addr.cport);
11340 		}
11341 
11342 		sata_log(sata_hba_inst, CE_WARN,
11343 		    "SATA port multiplier detected at port %d",
11344 		    cportinfo->cport_addr.cport);
11345 
11346 		mutex_exit(&cportinfo->cport_mutex);
11347 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11348 		    SATA_SUCCESS)
11349 			return (SATA_FAILURE);
11350 		sata_show_pmult_info(sata_hba_inst, sata_device);
11351 		mutex_enter(&cportinfo->cport_mutex);
11352 
11353 		/*
11354 		 * Mark all the port multiplier port behind the port
11355 		 * multiplier behind with link events, so that the sata daemon
11356 		 * will update their status.
11357 		 */
11358 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11359 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11360 		mutex_exit(&cportinfo->cport_mutex);
11361 		return (SATA_SUCCESS);
11362 	}
11363 	mutex_exit(&cportinfo->cport_mutex);
11364 
11365 	/*
11366 	 * Figure out what kind of device we are really
11367 	 * dealing with. Failure of identifying device does not fail this
11368 	 * function.
11369 	 */
11370 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11371 	rval_init = SATA_FAILURE;
11372 	mutex_enter(&cportinfo->cport_mutex);
11373 	if (rval_probe == SATA_SUCCESS) {
11374 		/*
11375 		 * If we are dealing with the same type of a device as before,
11376 		 * restore its settings flags.
11377 		 */
11378 		if (osdinfo != NULL &&
11379 		    sata_device->satadev_type == prev_device_type)
11380 			sdinfo->satadrv_settings = prev_device_settings;
11381 
11382 		mutex_exit(&cportinfo->cport_mutex);
11383 		rval_init = SATA_SUCCESS;
11384 		/* Set initial device features, if necessary */
11385 		if (init_device == B_TRUE) {
11386 			rval_init = sata_initialize_device(sata_hba_inst,
11387 			    sdinfo);
11388 		}
11389 		if (rval_init == SATA_SUCCESS)
11390 			return (rval_init);
11391 		/* else we will retry if retry was asked for */
11392 
11393 	} else {
11394 		/*
11395 		 * If there was some device info before we probe the device,
11396 		 * restore previous device setting, so we can retry from scratch
11397 		 * later. Providing, of course, that device has not disapear
11398 		 * during probing process.
11399 		 */
11400 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11401 			if (osdinfo != NULL) {
11402 				cportinfo->cport_dev_type = prev_device_type;
11403 				sdinfo->satadrv_type = prev_device_type;
11404 				sdinfo->satadrv_state = prev_device_state;
11405 			}
11406 		} else {
11407 			/* device is gone */
11408 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11409 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11410 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11411 			mutex_exit(&cportinfo->cport_mutex);
11412 			return (SATA_SUCCESS);
11413 		}
11414 		mutex_exit(&cportinfo->cport_mutex);
11415 	}
11416 
11417 	if (retry) {
11418 		clock_t cur_time = ddi_get_lbolt();
11419 		/*
11420 		 * A device was not successfully identified or initialized.
11421 		 * Track retry time for device identification.
11422 		 */
11423 		if ((cur_time - start_time) <
11424 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11425 			/* sleep for a while */
11426 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11427 			goto retry_probe;
11428 		}
11429 		/* else no more retries */
11430 		mutex_enter(&cportinfo->cport_mutex);
11431 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11432 			if (rval_init == SATA_RETRY) {
11433 				/*
11434 				 * Setting drive features have failed, but
11435 				 * because the drive is still accessible,
11436 				 * keep it and emit a warning message.
11437 				 */
11438 				sata_log(sata_hba_inst, CE_WARN,
11439 				    "SATA device at port %d - desired "
11440 				    "drive features could not be set. "
11441 				    "Device may not operate as expected.",
11442 				    cportinfo->cport_addr.cport);
11443 			} else {
11444 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11445 				    satadrv_state = SATA_DSTATE_FAILED;
11446 			}
11447 		}
11448 		mutex_exit(&cportinfo->cport_mutex);
11449 	}
11450 	return (SATA_SUCCESS);
11451 }
11452 
11453 /*
11454  * Reprobe a controller port that connected to a port multiplier.
11455  *
11456  * NOTE: No Mutex should be hold.
11457  */
11458 static int
11459 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11460     int flag)
11461 {
11462 	_NOTE(ARGUNUSED(flag))
11463 	sata_cport_info_t *cportinfo;
11464 	sata_pmult_info_t *pmultinfo;
11465 	uint8_t cport = sata_device->satadev_addr.cport;
11466 	int rval_probe;
11467 
11468 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11469 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11470 
11471 	/* probe port */
11472 	mutex_enter(&cportinfo->cport_mutex);
11473 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11474 	cportinfo->cport_state |= SATA_STATE_PROBING;
11475 	mutex_exit(&cportinfo->cport_mutex);
11476 
11477 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11478 	    (SATA_DIP(sata_hba_inst), sata_device);
11479 
11480 	mutex_enter(&cportinfo->cport_mutex);
11481 	if (rval_probe != SATA_SUCCESS) {
11482 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11483 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11484 		    "SATA port %d probing failed", cport));
11485 		sata_log(sata_hba_inst, CE_WARN,
11486 		    "SATA port multiplier detached at port %d", cport);
11487 		mutex_exit(&cportinfo->cport_mutex);
11488 		sata_free_pmult(sata_hba_inst, sata_device);
11489 		return (SATA_FAILURE);
11490 	}
11491 
11492 	/*
11493 	 * update sata port state and set device type
11494 	 */
11495 	sata_update_port_info(sata_hba_inst, sata_device);
11496 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11497 	cportinfo->cport_state |= SATA_STATE_PROBED;
11498 
11499 	/*
11500 	 * Sanity check - Port is active? Is the link active?
11501 	 * Is there any device attached?
11502 	 */
11503 	if ((cportinfo->cport_state &
11504 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11505 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11506 	    SATA_PORT_DEVLINK_UP ||
11507 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11508 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11509 		mutex_exit(&cportinfo->cport_mutex);
11510 		sata_free_pmult(sata_hba_inst, sata_device);
11511 		sata_log(sata_hba_inst, CE_WARN,
11512 		    "SATA port multiplier detached at port %d", cport);
11513 		return (SATA_SUCCESS);
11514 	}
11515 
11516 	/*
11517 	 * Device changed: PMult -> Non-PMult
11518 	 *
11519 	 * This situation is uncommon, most possibly being caused by errors
11520 	 * after which the port multiplier is not correct initialized and
11521 	 * recognized. In that case the new device will be marked as unknown
11522 	 * and will not be automatically probed in this routine. Instead
11523 	 * system administrator could manually restart it via cfgadm(1M).
11524 	 */
11525 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11526 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11527 		mutex_exit(&cportinfo->cport_mutex);
11528 		sata_free_pmult(sata_hba_inst, sata_device);
11529 		sata_log(sata_hba_inst, CE_WARN,
11530 		    "SATA port multiplier detached at port %d", cport);
11531 		return (SATA_FAILURE);
11532 	}
11533 
11534 	/*
11535 	 * Now we know it is a port multiplier. However, if this is not the
11536 	 * previously attached port multiplier - they may have different
11537 	 * pmport numbers - we need to re-allocate data structures for every
11538 	 * pmport and drive.
11539 	 *
11540 	 * Port multipliers of the same model have identical values in these
11541 	 * registers, so it is still necessary to update the information of
11542 	 * all drives attached to the previous port multiplier afterwards.
11543 	 */
11544 	/* Device changed: PMult -> another PMult */
11545 	mutex_exit(&cportinfo->cport_mutex);
11546 	sata_free_pmult(sata_hba_inst, sata_device);
11547 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11548 		return (SATA_FAILURE);
11549 	mutex_enter(&cportinfo->cport_mutex);
11550 
11551 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11552 	    "SATA port multiplier [changed] at port %d", cport);
11553 	sata_log(sata_hba_inst, CE_WARN,
11554 	    "SATA port multiplier detected at port %d", cport);
11555 
11556 	/*
11557 	 * Mark all the port multiplier port behind the port
11558 	 * multiplier behind with link events, so that the sata daemon
11559 	 * will update their status.
11560 	 */
11561 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11562 	mutex_exit(&cportinfo->cport_mutex);
11563 
11564 	return (SATA_SUCCESS);
11565 }
11566 
11567 /*
11568  * Re-probe a port multiplier port, check for a device and attach info
11569  * structures when necessary. Identify Device data is fetched, if possible.
11570  * Assumption: sata address is already validated as port multiplier port.
11571  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11572  * the presence of a device and its type.
11573  *
11574  * flag arg specifies that the function should try multiple times to identify
11575  * device type and to initialize it, or it should return immediately on failure.
11576  * SATA_DEV_IDENTIFY_RETRY - retry
11577  * SATA_DEV_IDENTIFY_NORETRY - no retry
11578  *
11579  * SATA_FAILURE is returned if one of the operations failed.
11580  *
11581  * This function cannot be called in interrupt context - it may sleep.
11582  *
11583  * NOTE: Should be only called by sata_probe_port() in case target port is a
11584  *       port multiplier port.
11585  * NOTE: No Mutex should be hold.
11586  */
11587 static int
11588 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11589     int flag)
11590 {
11591 	sata_cport_info_t *cportinfo = NULL;
11592 	sata_pmport_info_t *pmportinfo = NULL;
11593 	sata_drive_info_t *sdinfo, *osdinfo;
11594 	sata_device_t sdevice;
11595 	boolean_t init_device = B_FALSE;
11596 	int prev_device_type = SATA_DTYPE_NONE;
11597 	int prev_device_settings = 0;
11598 	int prev_device_state = 0;
11599 	clock_t start_time;
11600 	uint8_t cport = sata_device->satadev_addr.cport;
11601 	uint8_t pmport = sata_device->satadev_addr.pmport;
11602 	int rval;
11603 
11604 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11605 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11606 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11607 
11608 	if (osdinfo != NULL) {
11609 		/*
11610 		 * We are re-probing port with a previously attached device.
11611 		 * Save previous device type and settings.
11612 		 */
11613 		prev_device_type = pmportinfo->pmport_dev_type;
11614 		prev_device_settings = osdinfo->satadrv_settings;
11615 		prev_device_state = osdinfo->satadrv_state;
11616 	}
11617 
11618 	start_time = ddi_get_lbolt();
11619 
11620 	/* check parent status */
11621 	mutex_enter(&cportinfo->cport_mutex);
11622 	if ((cportinfo->cport_state &
11623 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11624 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11625 	    SATA_PORT_DEVLINK_UP) {
11626 		mutex_exit(&cportinfo->cport_mutex);
11627 		return (SATA_FAILURE);
11628 	}
11629 	mutex_exit(&cportinfo->cport_mutex);
11630 
11631 retry_probe_pmport:
11632 
11633 	/* probe port */
11634 	mutex_enter(&pmportinfo->pmport_mutex);
11635 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11636 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11637 	mutex_exit(&pmportinfo->pmport_mutex);
11638 
11639 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11640 	    (SATA_DIP(sata_hba_inst), sata_device);
11641 
11642 	/* might need retry because we cannot touch registers. */
11643 	if (rval == SATA_FAILURE) {
11644 		mutex_enter(&pmportinfo->pmport_mutex);
11645 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11646 		mutex_exit(&pmportinfo->pmport_mutex);
11647 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11648 		    "SATA port %d:%d probing failed",
11649 		    cport, pmport));
11650 		return (SATA_FAILURE);
11651 	} else if (rval == SATA_RETRY) {
11652 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11653 		    "SATA port %d:%d probing failed, retrying...",
11654 		    cport, pmport));
11655 		clock_t cur_time = ddi_get_lbolt();
11656 		/*
11657 		 * A device was not successfully identified or initialized.
11658 		 * Track retry time for device identification.
11659 		 */
11660 		if ((cur_time - start_time) <
11661 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11662 			/* sleep for a while */
11663 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11664 			goto retry_probe_pmport;
11665 		} else {
11666 			mutex_enter(&pmportinfo->pmport_mutex);
11667 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11668 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11669 				    satadrv_state = SATA_DSTATE_FAILED;
11670 			mutex_exit(&pmportinfo->pmport_mutex);
11671 			return (SATA_SUCCESS);
11672 		}
11673 	}
11674 
11675 	/*
11676 	 * Sanity check - Controller port is active? Is the link active?
11677 	 * Is it still a port multiplier?
11678 	 */
11679 	if ((cportinfo->cport_state &
11680 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11681 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11682 	    SATA_PORT_DEVLINK_UP ||
11683 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11684 		/*
11685 		 * Port in non-usable state or no link active/no
11686 		 * device. Free info structure.
11687 		 */
11688 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11689 
11690 		sdevice.satadev_addr.cport = cport;
11691 		sdevice.satadev_addr.pmport = pmport;
11692 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11693 		mutex_exit(&cportinfo->cport_mutex);
11694 
11695 		sata_free_pmult(sata_hba_inst, &sdevice);
11696 		return (SATA_FAILURE);
11697 	}
11698 
11699 	/* SATA_SUCCESS NOW */
11700 	/*
11701 	 * update sata port state and set device type
11702 	 */
11703 	mutex_enter(&pmportinfo->pmport_mutex);
11704 	sata_update_pmport_info(sata_hba_inst, sata_device);
11705 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11706 
11707 	/*
11708 	 * Sanity check - Port is active? Is the link active?
11709 	 * Is there any device attached?
11710 	 */
11711 	if ((pmportinfo->pmport_state &
11712 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11713 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11714 	    SATA_PORT_DEVLINK_UP) {
11715 		/*
11716 		 * Port in non-usable state or no link active/no device.
11717 		 * Free info structure if necessary (direct attached drive
11718 		 * only, for now!
11719 		 */
11720 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11721 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11722 		/* Add here differentiation for device attached or not */
11723 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11724 		mutex_exit(&pmportinfo->pmport_mutex);
11725 		if (sdinfo != NULL)
11726 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11727 		return (SATA_SUCCESS);
11728 	}
11729 
11730 	pmportinfo->pmport_state |= SATA_STATE_READY;
11731 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11732 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11733 
11734 	/*
11735 	 * If we are re-probing the port, there may be
11736 	 * sata_drive_info structure attached
11737 	 * (or sata_pm_info, if PMult is supported).
11738 	 */
11739 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11740 		/*
11741 		 * There is no device, so remove device info structure,
11742 		 * if necessary.
11743 		 */
11744 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11745 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11746 		if (sdinfo != NULL) {
11747 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11748 			sata_log(sata_hba_inst, CE_WARN,
11749 			    "SATA device detached from port %d:%d",
11750 			    cport, pmport);
11751 		}
11752 		mutex_exit(&pmportinfo->pmport_mutex);
11753 		return (SATA_SUCCESS);
11754 	}
11755 
11756 	/* this should not be a pmult */
11757 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11758 	if (sdinfo == NULL) {
11759 		/*
11760 		 * There is some device attached, but there is
11761 		 * no sata_drive_info structure - allocate one
11762 		 */
11763 		mutex_exit(&pmportinfo->pmport_mutex);
11764 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11765 		    KM_SLEEP);
11766 		mutex_enter(&pmportinfo->pmport_mutex);
11767 		/*
11768 		 * Recheck, that the port state did not change when we
11769 		 * released mutex.
11770 		 */
11771 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11772 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11773 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11774 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11775 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11776 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11777 		} else {
11778 			/*
11779 			 * Port is not in ready state, we
11780 			 * cannot attach a device.
11781 			 */
11782 			mutex_exit(&pmportinfo->pmport_mutex);
11783 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11784 			return (SATA_SUCCESS);
11785 		}
11786 		/*
11787 		 * Since we are adding device, presumably new one,
11788 		 * indicate that it  should be initalized,
11789 		 * as well as some internal framework states).
11790 		 */
11791 		init_device = B_TRUE;
11792 	}
11793 
11794 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11795 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11796 
11797 	mutex_exit(&pmportinfo->pmport_mutex);
11798 	/*
11799 	 * Figure out what kind of device we are really
11800 	 * dealing with.
11801 	 */
11802 	rval = sata_probe_device(sata_hba_inst, sata_device);
11803 
11804 	mutex_enter(&pmportinfo->pmport_mutex);
11805 	if (rval == SATA_SUCCESS) {
11806 		/*
11807 		 * If we are dealing with the same type of a device as before,
11808 		 * restore its settings flags.
11809 		 */
11810 		if (osdinfo != NULL &&
11811 		    sata_device->satadev_type == prev_device_type)
11812 			sdinfo->satadrv_settings = prev_device_settings;
11813 
11814 		mutex_exit(&pmportinfo->pmport_mutex);
11815 		/* Set initial device features, if necessary */
11816 		if (init_device == B_TRUE) {
11817 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11818 		}
11819 		if (rval == SATA_SUCCESS)
11820 			return (rval);
11821 	} else {
11822 		/*
11823 		 * If there was some device info before we probe the device,
11824 		 * restore previous device setting, so we can retry from scratch
11825 		 * later. Providing, of course, that device has not disappeared
11826 		 * during probing process.
11827 		 */
11828 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11829 			if (osdinfo != NULL) {
11830 				pmportinfo->pmport_dev_type = prev_device_type;
11831 				sdinfo->satadrv_type = prev_device_type;
11832 				sdinfo->satadrv_state = prev_device_state;
11833 			}
11834 		} else {
11835 			/* device is gone */
11836 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11837 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11838 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11839 			mutex_exit(&pmportinfo->pmport_mutex);
11840 			return (SATA_SUCCESS);
11841 		}
11842 		mutex_exit(&pmportinfo->pmport_mutex);
11843 	}
11844 
11845 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11846 		clock_t cur_time = ddi_get_lbolt();
11847 		/*
11848 		 * A device was not successfully identified or initialized.
11849 		 * Track retry time for device identification.
11850 		 */
11851 		if ((cur_time - start_time) <
11852 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11853 			/* sleep for a while */
11854 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11855 			goto retry_probe_pmport;
11856 		} else {
11857 			mutex_enter(&pmportinfo->pmport_mutex);
11858 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11859 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11860 				    satadrv_state = SATA_DSTATE_FAILED;
11861 			mutex_exit(&pmportinfo->pmport_mutex);
11862 		}
11863 	}
11864 	return (SATA_SUCCESS);
11865 }
11866 
11867 /*
11868  * Allocated related structure for a port multiplier and its device ports
11869  *
11870  * Port multiplier should be ready and probed, and related information like
11871  * the number of the device ports should be store in sata_device_t.
11872  *
11873  * NOTE: No Mutex should be hold.
11874  */
11875 static int
11876 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11877 {
11878 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11879 	sata_cport_info_t *cportinfo = NULL;
11880 	sata_pmult_info_t *pmultinfo = NULL;
11881 	sata_pmport_info_t *pmportinfo = NULL;
11882 	sata_device_t sd;
11883 	dev_t minor_number;
11884 	char name[16];
11885 	uint8_t cport = sata_device->satadev_addr.cport;
11886 	int rval;
11887 	int npmport;
11888 
11889 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11890 
11891 	/* This function might be called while a port-mult is hot-plugged. */
11892 	mutex_enter(&cportinfo->cport_mutex);
11893 
11894 	/* dev_type's not updated when get called from sata_reprobe_port() */
11895 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11896 		/* Create a pmult_info structure */
11897 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11898 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11899 	}
11900 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11901 
11902 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11903 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11904 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11905 
11906 	/*
11907 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11908 	 * The HBA driver should initialize and register the port multiplier,
11909 	 * sata_register_pmult() will fill following fields,
11910 	 *   + sata_pmult_info.pmult_gscr
11911 	 *   + sata_pmult_info.pmult_num_dev_ports
11912 	 */
11913 	sd.satadev_addr = sata_device->satadev_addr;
11914 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11915 	mutex_exit(&cportinfo->cport_mutex);
11916 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11917 	    (SATA_DIP(sata_hba_inst), &sd);
11918 	mutex_enter(&cportinfo->cport_mutex);
11919 
11920 	if (rval != SATA_SUCCESS ||
11921 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11922 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11923 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11924 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11925 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11926 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11927 		mutex_exit(&cportinfo->cport_mutex);
11928 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11929 		    "sata_alloc_pmult: failed to initialize pmult "
11930 		    "at port %d.", cport)
11931 		return (SATA_FAILURE);
11932 	}
11933 
11934 	/* Initialize pmport_info structure */
11935 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11936 	    npmport++) {
11937 
11938 		/* if everything is allocated, skip */
11939 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11940 			continue;
11941 
11942 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11943 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11944 		mutex_exit(&cportinfo->cport_mutex);
11945 
11946 		mutex_enter(&pmportinfo->pmport_mutex);
11947 		pmportinfo->pmport_addr.cport = cport;
11948 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11949 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11950 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11951 		mutex_exit(&pmportinfo->pmport_mutex);
11952 
11953 		mutex_enter(&cportinfo->cport_mutex);
11954 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11955 
11956 		/* Create an attachment point */
11957 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11958 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11959 		(void) sprintf(name, "%d.%d", cport, npmport);
11960 
11961 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11962 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11963 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11964 			    "cannot create SATA attachment point for "
11965 			    "port %d:%d", cport, npmport);
11966 		}
11967 	}
11968 
11969 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11970 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11971 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11972 
11973 	mutex_exit(&cportinfo->cport_mutex);
11974 	return (SATA_SUCCESS);
11975 }
11976 
11977 /*
11978  * Free data structures when a port multiplier is removed.
11979  *
11980  * NOTE: No Mutex should be hold.
11981  */
11982 static void
11983 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11984 {
11985 	sata_cport_info_t *cportinfo;
11986 	sata_pmult_info_t *pmultinfo;
11987 	sata_pmport_info_t *pmportinfo;
11988 	sata_device_t pmport_device;
11989 	sata_drive_info_t *sdinfo;
11990 	dev_info_t *tdip;
11991 	char name[16];
11992 	uint8_t cport = sata_device->satadev_addr.cport;
11993 	int npmport;
11994 
11995 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11996 
11997 	/* This function might be called while port-mult is hot plugged. */
11998 	mutex_enter(&cportinfo->cport_mutex);
11999 
12000 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12001 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12002 	ASSERT(pmultinfo != NULL);
12003 
12004 	/* Free pmport_info structure */
12005 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12006 	    npmport++) {
12007 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12008 		if (pmportinfo == NULL)
12009 			continue;
12010 		mutex_exit(&cportinfo->cport_mutex);
12011 
12012 		mutex_enter(&pmportinfo->pmport_mutex);
12013 		sdinfo = pmportinfo->pmport_sata_drive;
12014 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12015 		mutex_exit(&pmportinfo->pmport_mutex);
12016 
12017 		/* Remove attachment point. */
12018 		name[0] = '\0';
12019 		(void) sprintf(name, "%d.%d", cport, npmport);
12020 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12021 		sata_log(sata_hba_inst, CE_NOTE,
12022 		    "Remove attachment point of port %d:%d",
12023 		    cport, npmport);
12024 
12025 		/*
12026 		 * Rumove target node
12027 		 */
12028 		bzero(&pmport_device, sizeof (sata_device_t));
12029 		pmport_device.satadev_rev = SATA_DEVICE_REV;
12030 		pmport_device.satadev_addr.cport = cport;
12031 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12032 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12033 
12034 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12035 		    &(pmport_device.satadev_addr));
12036 		if (tdip != NULL && ndi_devi_offline(tdip,
12037 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12038 			/*
12039 			 * Problem :
12040 			 * The target node remained attached.
12041 			 * This happens when the device file was open
12042 			 * or a node was waiting for resources.
12043 			 * Cannot do anything about it.
12044 			 */
12045 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12046 			    "sata_free_pmult: could not unconfigure device "
12047 			    "before disconnecting the SATA port %d:%d",
12048 			    cport, npmport));
12049 
12050 			/*
12051 			 * Set DEVICE REMOVED state in the target
12052 			 * node. It will prevent access to the device
12053 			 * even when a new device is attached, until
12054 			 * the old target node is released, removed and
12055 			 * recreated for a new  device.
12056 			 */
12057 			sata_set_device_removed(tdip);
12058 
12059 			/*
12060 			 * Instruct event daemon to try the target
12061 			 * node cleanup later.
12062 			 */
12063 			sata_set_target_node_cleanup(
12064 			    sata_hba_inst, &(pmport_device.satadev_addr));
12065 
12066 		}
12067 		mutex_enter(&cportinfo->cport_mutex);
12068 
12069 		/*
12070 		 * Add here differentiation for device attached or not
12071 		 */
12072 		if (sdinfo != NULL)  {
12073 			sata_log(sata_hba_inst, CE_WARN,
12074 			    "SATA device detached from port %d:%d",
12075 			    cport, npmport);
12076 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12077 		}
12078 
12079 		mutex_destroy(&pmportinfo->pmport_mutex);
12080 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12081 	}
12082 
12083 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12084 
12085 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12086 
12087 	sata_log(sata_hba_inst, CE_WARN,
12088 	    "SATA port multiplier detached at port %d", cport);
12089 
12090 	mutex_exit(&cportinfo->cport_mutex);
12091 }
12092 
12093 /*
12094  * Initialize device
12095  * Specified device is initialized to a default state.
12096  *
12097  * Returns SATA_SUCCESS if all device features are set successfully,
12098  * SATA_RETRY if device is accessible but device features were not set
12099  * successfully, and SATA_FAILURE otherwise.
12100  */
12101 static int
12102 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12103     sata_drive_info_t *sdinfo)
12104 {
12105 	int rval;
12106 
12107 	sata_save_drive_settings(sdinfo);
12108 
12109 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12110 
12111 	sata_init_write_cache_mode(sdinfo);
12112 
12113 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12114 
12115 	/* Determine current data transfer mode */
12116 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12117 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12118 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12119 	    SATA_VALIDINFO_88) != 0 &&
12120 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12121 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12122 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12123 	    SATA_MDMA_SEL_MASK) != 0) {
12124 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12125 	} else
12126 		/* DMA supported, not no DMA transfer mode is selected !? */
12127 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12128 
12129 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12130 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12131 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12132 	else
12133 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12134 
12135 	return (rval);
12136 }
12137 
12138 
12139 /*
12140  * Initialize write cache mode.
12141  *
12142  * The default write cache setting for SATA HDD is provided by sata_write_cache
12143  * static variable. ATAPI CD/DVDs devices have write cache default is
12144  * determined by sata_atapicdvd_write_cache static variable.
12145  * ATAPI tape devices have write cache default is determined by
12146  * sata_atapitape_write_cache static variable.
12147  * ATAPI disk devices have write cache default is determined by
12148  * sata_atapidisk_write_cache static variable.
12149  * 1 - enable
12150  * 0 - disable
12151  * any other value - current drive setting
12152  *
12153  * Although there is not reason to disable write cache on CD/DVD devices,
12154  * tape devices and ATAPI disk devices, the default setting control is provided
12155  * for the maximun flexibility.
12156  *
12157  * In the future, it may be overridden by the
12158  * disk-write-cache-enable property setting, if it is defined.
12159  * Returns SATA_SUCCESS if all device features are set successfully,
12160  * SATA_FAILURE otherwise.
12161  */
12162 static void
12163 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12164 {
12165 	switch (sdinfo->satadrv_type) {
12166 	case SATA_DTYPE_ATADISK:
12167 		if (sata_write_cache == 1)
12168 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12169 		else if (sata_write_cache == 0)
12170 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12171 		/*
12172 		 * When sata_write_cache value is not 0 or 1,
12173 		 * a current setting of the drive's write cache is used.
12174 		 */
12175 		break;
12176 	case SATA_DTYPE_ATAPICD:
12177 		if (sata_atapicdvd_write_cache == 1)
12178 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12179 		else if (sata_atapicdvd_write_cache == 0)
12180 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12181 		/*
12182 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12183 		 * a current setting of the drive's write cache is used.
12184 		 */
12185 		break;
12186 	case SATA_DTYPE_ATAPITAPE:
12187 		if (sata_atapitape_write_cache == 1)
12188 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12189 		else if (sata_atapitape_write_cache == 0)
12190 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12191 		/*
12192 		 * When sata_atapitape_write_cache value is not 0 or 1,
12193 		 * a current setting of the drive's write cache is used.
12194 		 */
12195 		break;
12196 	case SATA_DTYPE_ATAPIDISK:
12197 		if (sata_atapidisk_write_cache == 1)
12198 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12199 		else if (sata_atapidisk_write_cache == 0)
12200 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12201 		/*
12202 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12203 		 * a current setting of the drive's write cache is used.
12204 		 */
12205 		break;
12206 	}
12207 }
12208 
12209 
12210 /*
12211  * Validate sata address.
12212  * Specified cport, pmport and qualifier has to match
12213  * passed sata_scsi configuration info.
12214  * The presence of an attached device is not verified.
12215  *
12216  * Returns 0 when address is valid, -1 otherwise.
12217  */
12218 static int
12219 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12220 	int pmport, int qual)
12221 {
12222 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12223 		goto invalid_address;
12224 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12225 		goto invalid_address;
12226 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12227 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12228 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12229 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12230 		goto invalid_address;
12231 
12232 	return (0);
12233 
12234 invalid_address:
12235 	return (-1);
12236 
12237 }
12238 
12239 /*
12240  * Validate scsi address
12241  * SCSI target address is translated into SATA cport/pmport and compared
12242  * with a controller port/device configuration. LUN has to be 0.
12243  * Returns 0 if a scsi target refers to an attached device,
12244  * returns 1 if address is valid but no valid device is attached,
12245  * returns 2 if address is valid but device type is unknown (not valid device),
12246  * returns -1 if bad address or device is of an unsupported type.
12247  * Upon return sata_device argument is set.
12248  *
12249  * Port multiplier is supported now.
12250  */
12251 static int
12252 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12253 	struct scsi_address *ap, sata_device_t *sata_device)
12254 {
12255 	int cport, pmport, qual, rval;
12256 
12257 	rval = -1;	/* Invalid address */
12258 	if (ap->a_lun != 0)
12259 		goto out;
12260 
12261 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12262 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12263 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12264 
12265 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12266 		goto out;
12267 
12268 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12269 	    0) {
12270 
12271 		sata_cport_info_t *cportinfo;
12272 		sata_pmult_info_t *pmultinfo;
12273 		sata_drive_info_t *sdinfo = NULL;
12274 
12275 		sata_device->satadev_addr.qual = qual;
12276 		sata_device->satadev_addr.cport = cport;
12277 		sata_device->satadev_addr.pmport = pmport;
12278 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12279 
12280 		rval = 1;	/* Valid sata address */
12281 
12282 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12283 		if (qual == SATA_ADDR_DCPORT) {
12284 			if (cportinfo == NULL ||
12285 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12286 				goto out;
12287 
12288 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12289 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12290 			    sdinfo != NULL) {
12291 				rval = 2;
12292 				goto out;
12293 			}
12294 
12295 			if ((cportinfo->cport_dev_type &
12296 			    SATA_VALID_DEV_TYPE) == 0) {
12297 				rval = -1;
12298 				goto out;
12299 			}
12300 
12301 		} else if (qual == SATA_ADDR_DPMPORT) {
12302 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12303 			if (pmultinfo == NULL) {
12304 				rval = -1;
12305 				goto out;
12306 			}
12307 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12308 			    NULL ||
12309 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12310 			    pmport) == SATA_DTYPE_NONE)
12311 				goto out;
12312 
12313 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12314 			    pmport);
12315 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12316 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12317 				rval = 2;
12318 				goto out;
12319 			}
12320 
12321 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12322 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12323 				rval = -1;
12324 				goto out;
12325 			}
12326 
12327 		} else {
12328 			rval = -1;
12329 			goto out;
12330 		}
12331 		if ((sdinfo == NULL) ||
12332 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12333 			goto out;
12334 
12335 		sata_device->satadev_type = sdinfo->satadrv_type;
12336 
12337 		return (0);
12338 	}
12339 out:
12340 	if (rval > 0) {
12341 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12342 		    "sata_validate_scsi_address: no valid target %x lun %x",
12343 		    ap->a_target, ap->a_lun);
12344 	}
12345 	return (rval);
12346 }
12347 
12348 /*
12349  * Find dip corresponding to passed device number
12350  *
12351  * Returns NULL if invalid device number is passed or device cannot be found,
12352  * Returns dip is device is found.
12353  */
12354 static dev_info_t *
12355 sata_devt_to_devinfo(dev_t dev)
12356 {
12357 	dev_info_t *dip;
12358 #ifndef __lock_lint
12359 	struct devnames *dnp;
12360 	major_t major = getmajor(dev);
12361 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12362 
12363 	if (major >= devcnt)
12364 		return (NULL);
12365 
12366 	dnp = &devnamesp[major];
12367 	LOCK_DEV_OPS(&(dnp->dn_lock));
12368 	dip = dnp->dn_head;
12369 	while (dip && (ddi_get_instance(dip) != instance)) {
12370 		dip = ddi_get_next(dip);
12371 	}
12372 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12373 #endif
12374 
12375 	return (dip);
12376 }
12377 
12378 
12379 /*
12380  * Probe device.
12381  * This function issues Identify Device command and initializes local
12382  * sata_drive_info structure if the device can be identified.
12383  * The device type is determined by examining Identify Device
12384  * command response.
12385  * If the sata_hba_inst has linked drive info structure for this
12386  * device address, the Identify Device data is stored into sata_drive_info
12387  * structure linked to the port info structure.
12388  *
12389  * sata_device has to refer to the valid sata port(s) for HBA described
12390  * by sata_hba_inst structure.
12391  *
12392  * Returns:
12393  *	SATA_SUCCESS if device type was successfully probed and port-linked
12394  *		drive info structure was updated;
12395  * 	SATA_FAILURE if there is no device, or device was not probed
12396  *		successully;
12397  *	SATA_RETRY if device probe can be retried later.
12398  * If a device cannot be identified, sata_device's dev_state and dev_type
12399  * fields are set to unknown.
12400  * There are no retries in this function. Any retries should be managed by
12401  * the caller.
12402  */
12403 
12404 
12405 static int
12406 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12407 {
12408 	sata_pmport_info_t *pmportinfo;
12409 	sata_drive_info_t *sdinfo;
12410 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12411 	int rval;
12412 
12413 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12414 	    sata_device->satadev_addr.cport) &
12415 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12416 
12417 	sata_device->satadev_type = SATA_DTYPE_NONE;
12418 
12419 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12420 	    sata_device->satadev_addr.cport)));
12421 
12422 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12423 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12424 		    sata_device->satadev_addr.cport,
12425 		    sata_device->satadev_addr.pmport);
12426 		ASSERT(pmportinfo != NULL);
12427 	}
12428 
12429 	/* Get pointer to port-linked sata device info structure */
12430 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12431 	if (sdinfo != NULL) {
12432 		sdinfo->satadrv_state &=
12433 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12434 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12435 	} else {
12436 		/* No device to probe */
12437 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12438 		    sata_device->satadev_addr.cport)));
12439 		sata_device->satadev_type = SATA_DTYPE_NONE;
12440 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12441 		return (SATA_FAILURE);
12442 	}
12443 	/*
12444 	 * Need to issue both types of identify device command and
12445 	 * determine device type by examining retreived data/status.
12446 	 * First, ATA Identify Device.
12447 	 */
12448 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12449 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12450 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12451 	    sata_device->satadev_addr.cport)));
12452 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12453 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12454 	if (rval == SATA_RETRY) {
12455 		/* We may try to check for ATAPI device */
12456 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12457 			/*
12458 			 * HBA supports ATAPI - try to issue Identify Packet
12459 			 * Device command.
12460 			 */
12461 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12462 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12463 		}
12464 	}
12465 	if (rval == SATA_SUCCESS) {
12466 		/*
12467 		 * Got something responding positively to ATA Identify Device
12468 		 * or to Identify Packet Device cmd.
12469 		 * Save last used device type.
12470 		 */
12471 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12472 
12473 		/* save device info, if possible */
12474 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12475 		    sata_device->satadev_addr.cport)));
12476 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12477 		if (sdinfo == NULL) {
12478 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12479 			    sata_device->satadev_addr.cport)));
12480 			return (SATA_FAILURE);
12481 		}
12482 		/*
12483 		 * Copy drive info into the port-linked drive info structure.
12484 		 */
12485 		*sdinfo = new_sdinfo;
12486 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12487 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12488 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12489 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12490 			    sata_device->satadev_addr.cport) =
12491 			    sdinfo->satadrv_type;
12492 		else { /* SATA_ADDR_DPMPORT */
12493 			mutex_enter(&pmportinfo->pmport_mutex);
12494 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12495 			    sata_device->satadev_addr.cport,
12496 			    sata_device->satadev_addr.pmport) =
12497 			    sdinfo->satadrv_type;
12498 			mutex_exit(&pmportinfo->pmport_mutex);
12499 		}
12500 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12501 		    sata_device->satadev_addr.cport)));
12502 		return (SATA_SUCCESS);
12503 	}
12504 
12505 	/*
12506 	 * It may be SATA_RETRY or SATA_FAILURE return.
12507 	 * Looks like we cannot determine the device type at this time.
12508 	 */
12509 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12510 	    sata_device->satadev_addr.cport)));
12511 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12512 	if (sdinfo != NULL) {
12513 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12514 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12515 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12516 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12517 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12518 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12519 			    sata_device->satadev_addr.cport) =
12520 			    SATA_DTYPE_UNKNOWN;
12521 		else {
12522 			/* SATA_ADDR_DPMPORT */
12523 			mutex_enter(&pmportinfo->pmport_mutex);
12524 			if ((SATA_PMULT_INFO(sata_hba_inst,
12525 			    sata_device->satadev_addr.cport) != NULL) &&
12526 			    (SATA_PMPORT_INFO(sata_hba_inst,
12527 			    sata_device->satadev_addr.cport,
12528 			    sata_device->satadev_addr.pmport) != NULL))
12529 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12530 				    sata_device->satadev_addr.cport,
12531 				    sata_device->satadev_addr.pmport) =
12532 				    SATA_DTYPE_UNKNOWN;
12533 			mutex_exit(&pmportinfo->pmport_mutex);
12534 		}
12535 	}
12536 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12537 	    sata_device->satadev_addr.cport)));
12538 	return (rval);
12539 }
12540 
12541 
12542 /*
12543  * Get pointer to sata_drive_info structure.
12544  *
12545  * The sata_device has to contain address (cport, pmport and qualifier) for
12546  * specified sata_scsi structure.
12547  *
12548  * Returns NULL if device address is not valid for this HBA configuration.
12549  * Otherwise, returns a pointer to sata_drive_info structure.
12550  *
12551  * This function should be called with a port mutex held.
12552  */
12553 static sata_drive_info_t *
12554 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12555     sata_device_t *sata_device)
12556 {
12557 	uint8_t cport = sata_device->satadev_addr.cport;
12558 	uint8_t pmport = sata_device->satadev_addr.pmport;
12559 	uint8_t qual = sata_device->satadev_addr.qual;
12560 
12561 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12562 		return (NULL);
12563 
12564 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12565 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12566 		/* Port not probed yet */
12567 		return (NULL);
12568 
12569 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12570 		return (NULL);
12571 
12572 	if (qual == SATA_ADDR_DCPORT) {
12573 		/* Request for a device on a controller port */
12574 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12575 		    SATA_DTYPE_PMULT)
12576 			/* Port multiplier attached */
12577 			return (NULL);
12578 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12579 	}
12580 	if (qual == SATA_ADDR_DPMPORT) {
12581 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12582 		    SATA_DTYPE_PMULT)
12583 			return (NULL);
12584 
12585 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12586 			return (NULL);
12587 
12588 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12589 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12590 			/* Port multiplier port not probed yet */
12591 			return (NULL);
12592 
12593 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12594 	}
12595 
12596 	/* we should not get here */
12597 	return (NULL);
12598 }
12599 
12600 
12601 /*
12602  * sata_identify_device.
12603  * Send Identify Device command to SATA HBA driver.
12604  * If command executes successfully, update sata_drive_info structure pointed
12605  * to by sdinfo argument, including Identify Device data.
12606  * If command fails, invalidate data in sata_drive_info.
12607  *
12608  * Cannot be called from interrupt level.
12609  *
12610  * Returns:
12611  * SATA_SUCCESS if the device was identified as a supported device,
12612  * SATA_RETRY if the device was not identified but could be retried,
12613  * SATA_FAILURE if the device was not identified and identify attempt
12614  *	should not be retried.
12615  */
12616 static int
12617 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12618     sata_drive_info_t *sdinfo)
12619 {
12620 	uint16_t cfg_word;
12621 	int rval;
12622 
12623 	/* fetch device identify data */
12624 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12625 	    sdinfo)) != SATA_SUCCESS)
12626 		goto fail_unknown;
12627 
12628 	cfg_word = sdinfo->satadrv_id.ai_config;
12629 
12630 	/* Set the correct device type */
12631 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12632 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12633 	} else if (cfg_word == SATA_CFA_TYPE) {
12634 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12635 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12636 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12637 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12638 		case SATA_ATAPI_CDROM_DEV:
12639 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12640 			break;
12641 		case SATA_ATAPI_SQACC_DEV:
12642 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12643 			break;
12644 		case SATA_ATAPI_DIRACC_DEV:
12645 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12646 			break;
12647 		case SATA_ATAPI_PROC_DEV:
12648 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12649 			break;
12650 		default:
12651 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12652 		}
12653 	} else {
12654 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12655 	}
12656 
12657 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12658 		if (sdinfo->satadrv_capacity == 0) {
12659 			/* Non-LBA disk. Too bad... */
12660 			sata_log(sata_hba_inst, CE_WARN,
12661 			    "SATA disk device at port %d does not support LBA",
12662 			    sdinfo->satadrv_addr.cport);
12663 			rval = SATA_FAILURE;
12664 			goto fail_unknown;
12665 		}
12666 	}
12667 #if 0
12668 	/* Left for historical reason */
12669 	/*
12670 	 * Some initial version of SATA spec indicated that at least
12671 	 * UDMA mode 4 has to be supported. It is not metioned in
12672 	 * SerialATA 2.6, so this restriction is removed.
12673 	 */
12674 	/* Check for Ultra DMA modes 6 through 0 being supported */
12675 	for (i = 6; i >= 0; --i) {
12676 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12677 			break;
12678 	}
12679 
12680 	/*
12681 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12682 	 * higher are not supported by the device, fail this
12683 	 * device.
12684 	 */
12685 	if (i < 4) {
12686 		/* No required Ultra DMA mode supported */
12687 		sata_log(sata_hba_inst, CE_WARN,
12688 		    "SATA disk device at port %d does not support UDMA "
12689 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12690 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12691 		    "mode 4 or higher required, %d supported", i));
12692 		rval = SATA_FAILURE;
12693 		goto fail_unknown;
12694 	}
12695 #endif
12696 
12697 	/*
12698 	 * For Disk devices, if it doesn't support UDMA mode, we would
12699 	 * like to return failure directly.
12700 	 */
12701 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12702 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12703 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12704 		sata_log(sata_hba_inst, CE_WARN,
12705 		    "SATA disk device at port %d does not support UDMA",
12706 		    sdinfo->satadrv_addr.cport);
12707 		rval = SATA_FAILURE;
12708 		goto fail_unknown;
12709 	}
12710 
12711 	return (SATA_SUCCESS);
12712 
12713 fail_unknown:
12714 	/* Invalidate sata_drive_info ? */
12715 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12716 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12717 	return (rval);
12718 }
12719 
12720 /*
12721  * Log/display device information
12722  */
12723 static void
12724 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12725     sata_drive_info_t *sdinfo)
12726 {
12727 	int valid_version;
12728 	char msg_buf[MAXPATHLEN];
12729 	int i;
12730 
12731 	/* Show HBA path */
12732 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12733 
12734 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12735 
12736 	switch (sdinfo->satadrv_type) {
12737 	case SATA_DTYPE_ATADISK:
12738 		(void) sprintf(msg_buf, "SATA disk device at");
12739 		break;
12740 
12741 	case SATA_DTYPE_ATAPICD:
12742 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12743 		break;
12744 
12745 	case SATA_DTYPE_ATAPITAPE:
12746 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12747 		break;
12748 
12749 	case SATA_DTYPE_ATAPIDISK:
12750 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12751 		break;
12752 
12753 	case SATA_DTYPE_ATAPIPROC:
12754 		(void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12755 		break;
12756 
12757 	case SATA_DTYPE_UNKNOWN:
12758 		(void) sprintf(msg_buf,
12759 		    "Unsupported SATA device type (cfg 0x%x) at ",
12760 		    sdinfo->satadrv_id.ai_config);
12761 		break;
12762 	}
12763 
12764 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12765 		cmn_err(CE_CONT, "?\t%s port %d\n",
12766 		    msg_buf, sdinfo->satadrv_addr.cport);
12767 	else
12768 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12769 		    msg_buf, sdinfo->satadrv_addr.cport,
12770 		    sdinfo->satadrv_addr.pmport);
12771 
12772 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12773 	    sizeof (sdinfo->satadrv_id.ai_model));
12774 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12775 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12776 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12777 
12778 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12779 	    sizeof (sdinfo->satadrv_id.ai_fw));
12780 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12781 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12782 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12783 
12784 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12785 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12786 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12787 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12788 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12789 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12790 	} else {
12791 		/*
12792 		 * Some drives do not implement serial number and may
12793 		 * violate the spec by providing spaces rather than zeros
12794 		 * in serial number field. Scan the buffer to detect it.
12795 		 */
12796 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12797 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12798 				break;
12799 		}
12800 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12801 			cmn_err(CE_CONT, "?\tserial number - none\n");
12802 		} else {
12803 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12804 		}
12805 	}
12806 
12807 #ifdef SATA_DEBUG
12808 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12809 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12810 		int i;
12811 		for (i = 14; i >= 2; i--) {
12812 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12813 				valid_version = i;
12814 				break;
12815 			}
12816 		}
12817 		cmn_err(CE_CONT,
12818 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12819 		    valid_version,
12820 		    sdinfo->satadrv_id.ai_majorversion,
12821 		    sdinfo->satadrv_id.ai_minorversion);
12822 	}
12823 #endif
12824 	/* Log some info */
12825 	cmn_err(CE_CONT, "?\tsupported features:\n");
12826 	msg_buf[0] = '\0';
12827 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12828 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12829 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12830 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12831 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12832 	}
12833 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12834 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12835 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12836 		(void) strlcat(msg_buf, ", Native Command Queueing",
12837 		    MAXPATHLEN);
12838 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12839 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12840 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12841 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12842 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12843 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12844 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12845 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12846 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12847 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12848 		cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12849 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12850 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12851 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12852 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12853 	if (sdinfo->satadrv_features_support &
12854 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12855 		msg_buf[0] = '\0';
12856 		(void) snprintf(msg_buf, MAXPATHLEN,
12857 		    "Supported queue depth %d",
12858 		    sdinfo->satadrv_queue_depth);
12859 		if (!(sata_func_enable &
12860 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12861 			(void) strlcat(msg_buf,
12862 			    " - queueing disabled globally", MAXPATHLEN);
12863 		else if (sdinfo->satadrv_queue_depth >
12864 		    sdinfo->satadrv_max_queue_depth) {
12865 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12866 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12867 			    (int)sdinfo->satadrv_max_queue_depth);
12868 		}
12869 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12870 	}
12871 
12872 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12873 #ifdef __i386
12874 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12875 		    sdinfo->satadrv_capacity);
12876 #else
12877 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12878 		    sdinfo->satadrv_capacity);
12879 #endif
12880 		cmn_err(CE_CONT, "?%s", msg_buf);
12881 	}
12882 }
12883 
12884 /*
12885  * Log/display port multiplier information
12886  * No Mutex should be hold.
12887  */
12888 static void
12889 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12890     sata_device_t *sata_device)
12891 {
12892 	_NOTE(ARGUNUSED(sata_hba_inst))
12893 
12894 	int cport = sata_device->satadev_addr.cport;
12895 	sata_pmult_info_t *pmultinfo;
12896 	char msg_buf[MAXPATHLEN];
12897 	uint32_t gscr0, gscr1, gscr2, gscr64;
12898 
12899 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12900 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12901 	if (pmultinfo == NULL) {
12902 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12903 		return;
12904 	}
12905 
12906 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12907 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12908 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12909 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12910 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12911 
12912 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12913 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12914 
12915 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12916 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12917 	cmn_err(CE_CONT, "?%s", msg_buf);
12918 
12919 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12920 	if (gscr1 & (1 << 3))
12921 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12922 	else if (gscr1 & (1 << 2))
12923 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12924 	else if (gscr1 & (1 << 1))
12925 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12926 	else
12927 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12928 	cmn_err(CE_CONT, "?%s", msg_buf);
12929 
12930 	(void) strcpy(msg_buf, "\tSupport ");
12931 	if (gscr64 & (1 << 3))
12932 		(void) strlcat(msg_buf, "Asy-Notif, ",
12933 		    MAXPATHLEN);
12934 	if (gscr64 & (1 << 2))
12935 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12936 	if (gscr64 & (1 << 1))
12937 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12938 	if (gscr64 & (1 << 0))
12939 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12940 	if ((gscr64 & 0xf) == 0)
12941 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12942 	cmn_err(CE_CONT, "?%s", msg_buf);
12943 
12944 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12945 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12946 	cmn_err(CE_CONT, "?%s", msg_buf);
12947 }
12948 
12949 /*
12950  * sata_save_drive_settings extracts current setting of the device and stores
12951  * it for future reference, in case the device setup would need to be restored
12952  * after the device reset.
12953  *
12954  * For all devices read ahead and write cache settings are saved, if the
12955  * device supports these features at all.
12956  * For ATAPI devices the Removable Media Status Notification setting is saved.
12957  */
12958 static void
12959 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12960 {
12961 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12962 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12963 
12964 		/* Current setting of Read Ahead (and Read Cache) */
12965 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12966 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12967 		else
12968 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12969 
12970 		/* Current setting of Write Cache */
12971 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12972 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12973 		else
12974 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12975 	}
12976 
12977 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12978 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12979 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12980 		else
12981 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12982 	}
12983 }
12984 
12985 
12986 /*
12987  * sata_check_capacity function determines a disk capacity
12988  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12989  *
12990  * NOTE: CHS mode is not supported! If a device does not support LBA,
12991  * this function is not called.
12992  *
12993  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12994  */
12995 static uint64_t
12996 sata_check_capacity(sata_drive_info_t *sdinfo)
12997 {
12998 	uint64_t capacity = 0;
12999 	int i;
13000 
13001 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13002 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13003 		/* Capacity valid only for LBA-addressable disk devices */
13004 		return (0);
13005 
13006 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13007 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13008 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13009 		/* LBA48 mode supported and enabled */
13010 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13011 		    SATA_DEV_F_LBA28;
13012 		for (i = 3;  i >= 0;  --i) {
13013 			capacity <<= 16;
13014 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13015 		}
13016 	} else {
13017 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
13018 		capacity <<= 16;
13019 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
13020 		if (capacity >= 0x1000000)
13021 			/* LBA28 mode */
13022 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13023 	}
13024 	return (capacity);
13025 }
13026 
13027 
13028 /*
13029  * Allocate consistent buffer for DMA transfer
13030  *
13031  * Cannot be called from interrupt level or with mutex held - it may sleep.
13032  *
13033  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13034  */
13035 static struct buf *
13036 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13037 {
13038 	struct scsi_address ap;
13039 	struct buf *bp;
13040 	ddi_dma_attr_t	cur_dma_attr;
13041 
13042 	ASSERT(spx->txlt_sata_pkt != NULL);
13043 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13044 	ap.a_target = SATA_TO_SCSI_TARGET(
13045 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13046 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13047 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13048 	ap.a_lun = 0;
13049 
13050 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13051 	    B_READ, SLEEP_FUNC, NULL);
13052 
13053 	if (bp != NULL) {
13054 		/* Allocate DMA resources for this buffer */
13055 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13056 		/*
13057 		 * We use a local version of the dma_attr, to account
13058 		 * for a device addressing limitations.
13059 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13060 		 * will cause dma attributes to be adjusted to a lowest
13061 		 * acceptable level.
13062 		 */
13063 		sata_adjust_dma_attr(NULL,
13064 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13065 
13066 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13067 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13068 			scsi_free_consistent_buf(bp);
13069 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13070 			bp = NULL;
13071 		}
13072 	}
13073 	return (bp);
13074 }
13075 
13076 /*
13077  * Release local buffer (consistent buffer for DMA transfer) allocated
13078  * via sata_alloc_local_buffer().
13079  */
13080 static void
13081 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13082 {
13083 	ASSERT(spx->txlt_sata_pkt != NULL);
13084 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13085 
13086 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13087 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13088 
13089 	sata_common_free_dma_rsrcs(spx);
13090 
13091 	/* Free buffer */
13092 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13093 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13094 }
13095 
13096 /*
13097  * Allocate sata_pkt
13098  * Pkt structure version and embedded strcutures version are initialized.
13099  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13100  *
13101  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13102  * callback argument determines if it can sleep or not.
13103  * Hence, it should not be called from interrupt context.
13104  *
13105  * If successful, non-NULL pointer to a sata pkt is returned.
13106  * Upon failure, NULL pointer is returned.
13107  */
13108 static sata_pkt_t *
13109 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13110 {
13111 	sata_pkt_t *spkt;
13112 	int kmsflag;
13113 
13114 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13115 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13116 	if (spkt == NULL) {
13117 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13118 		    "sata_pkt_alloc: failed"));
13119 		return (NULL);
13120 	}
13121 	spkt->satapkt_rev = SATA_PKT_REV;
13122 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13123 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13124 	spkt->satapkt_framework_private = spx;
13125 	spx->txlt_sata_pkt = spkt;
13126 	return (spkt);
13127 }
13128 
13129 /*
13130  * Free sata pkt allocated via sata_pkt_alloc()
13131  */
13132 static void
13133 sata_pkt_free(sata_pkt_txlate_t *spx)
13134 {
13135 	ASSERT(spx->txlt_sata_pkt != NULL);
13136 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13137 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13138 	spx->txlt_sata_pkt = NULL;
13139 }
13140 
13141 
13142 /*
13143  * Adjust DMA attributes.
13144  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13145  * from 8 bits to 16 bits, depending on a command being used.
13146  * Limiting max block count arbitrarily to 256 for all read/write
13147  * commands may affects performance, so check both the device and
13148  * controller capability before adjusting dma attributes.
13149  */
13150 void
13151 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13152     ddi_dma_attr_t *adj_dma_attr)
13153 {
13154 	uint32_t count_max;
13155 
13156 	/* Copy original attributes */
13157 	*adj_dma_attr = *dma_attr;
13158 	/*
13159 	 * Things to consider: device addressing capability,
13160 	 * "excessive" controller DMA capabilities.
13161 	 * If a device is being probed/initialized, there are
13162 	 * no device info - use default limits then.
13163 	 */
13164 	if (sdinfo == NULL) {
13165 		count_max = dma_attr->dma_attr_granular * 0x100;
13166 		if (dma_attr->dma_attr_count_max > count_max)
13167 			adj_dma_attr->dma_attr_count_max = count_max;
13168 		if (dma_attr->dma_attr_maxxfer > count_max)
13169 			adj_dma_attr->dma_attr_maxxfer = count_max;
13170 		return;
13171 	}
13172 
13173 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13174 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13175 			/*
13176 			 * 16-bit sector count may be used - we rely on
13177 			 * the assumption that only read and write cmds
13178 			 * will request more than 256 sectors worth of data
13179 			 */
13180 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13181 		} else {
13182 			/*
13183 			 * 8-bit sector count will be used - default limits
13184 			 * for dma attributes
13185 			 */
13186 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13187 		}
13188 		/*
13189 		 * Adjust controler dma attributes, if necessary
13190 		 */
13191 		if (dma_attr->dma_attr_count_max > count_max)
13192 			adj_dma_attr->dma_attr_count_max = count_max;
13193 		if (dma_attr->dma_attr_maxxfer > count_max)
13194 			adj_dma_attr->dma_attr_maxxfer = count_max;
13195 	}
13196 }
13197 
13198 
13199 /*
13200  * Allocate DMA resources for the buffer
13201  * This function handles initial DMA resource allocation as well as
13202  * DMA window shift and may be called repeatedly for the same DMA window
13203  * until all DMA cookies in the DMA window are processed.
13204  * To guarantee that there is always a coherent set of cookies to process
13205  * by SATA HBA driver (observing alignment, device granularity, etc.),
13206  * the number of slots for DMA cookies is equal to lesser of  a number of
13207  * cookies in a DMA window and a max number of scatter/gather entries.
13208  *
13209  * Returns DDI_SUCCESS upon successful operation.
13210  * Return failure code of a failing command or DDI_FAILURE when
13211  * internal cleanup failed.
13212  */
13213 static int
13214 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13215     int (*callback)(caddr_t), caddr_t arg,
13216     ddi_dma_attr_t *cur_dma_attr)
13217 {
13218 	int	rval;
13219 	off_t	offset;
13220 	size_t	size;
13221 	int	max_sg_len, req_len, i;
13222 	uint_t	dma_flags;
13223 	struct buf	*bp;
13224 	uint64_t	cur_txfer_len;
13225 
13226 
13227 	ASSERT(spx->txlt_sata_pkt != NULL);
13228 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13229 	ASSERT(bp != NULL);
13230 
13231 
13232 	if (spx->txlt_buf_dma_handle == NULL) {
13233 		/*
13234 		 * No DMA resources allocated so far - this is a first call
13235 		 * for this sata pkt.
13236 		 */
13237 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13238 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13239 
13240 		if (rval != DDI_SUCCESS) {
13241 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13242 			    "sata_dma_buf_setup: no buf DMA resources %x",
13243 			    rval));
13244 			return (rval);
13245 		}
13246 
13247 		if (bp->b_flags & B_READ)
13248 			dma_flags = DDI_DMA_READ;
13249 		else
13250 			dma_flags = DDI_DMA_WRITE;
13251 
13252 		if (flags & PKT_CONSISTENT)
13253 			dma_flags |= DDI_DMA_CONSISTENT;
13254 
13255 		if (flags & PKT_DMA_PARTIAL)
13256 			dma_flags |= DDI_DMA_PARTIAL;
13257 
13258 		/*
13259 		 * Check buffer alignment and size against dma attributes
13260 		 * Consider dma_attr_align only. There may be requests
13261 		 * with the size lower than device granularity, but they
13262 		 * will not read/write from/to the device, so no adjustment
13263 		 * is necessary. The dma_attr_minxfer theoretically should
13264 		 * be considered, but no HBA driver is checking it.
13265 		 */
13266 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13267 		    cur_dma_attr->dma_attr_align)) {
13268 			rval = ddi_dma_buf_bind_handle(
13269 			    spx->txlt_buf_dma_handle,
13270 			    bp, dma_flags, callback, arg,
13271 			    &spx->txlt_dma_cookie,
13272 			    &spx->txlt_curwin_num_dma_cookies);
13273 		} else { /* Buffer is not aligned */
13274 
13275 			int	(*ddicallback)(caddr_t);
13276 			size_t	bufsz;
13277 
13278 			/* Check id sleeping is allowed */
13279 			ddicallback = (callback == NULL_FUNC) ?
13280 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13281 
13282 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13283 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13284 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13285 
13286 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13287 				/*
13288 				 * CPU will need to access data in the buffer
13289 				 * (for copying) so map it.
13290 				 */
13291 				bp_mapin(bp);
13292 
13293 			ASSERT(spx->txlt_tmp_buf == NULL);
13294 
13295 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13296 			rval = ddi_dma_mem_alloc(
13297 			    spx->txlt_buf_dma_handle,
13298 			    bp->b_bcount,
13299 			    &sata_acc_attr,
13300 			    DDI_DMA_STREAMING,
13301 			    ddicallback, NULL,
13302 			    &spx->txlt_tmp_buf,
13303 			    &bufsz,
13304 			    &spx->txlt_tmp_buf_handle);
13305 
13306 			if (rval != DDI_SUCCESS) {
13307 				/* DMA mapping failed */
13308 				(void) ddi_dma_free_handle(
13309 				    &spx->txlt_buf_dma_handle);
13310 				spx->txlt_buf_dma_handle = NULL;
13311 #ifdef SATA_DEBUG
13312 				mbuffail_count++;
13313 #endif
13314 				SATADBG1(SATA_DBG_DMA_SETUP,
13315 				    spx->txlt_sata_hba_inst,
13316 				    "sata_dma_buf_setup: "
13317 				    "buf dma mem alloc failed %x\n", rval);
13318 				return (rval);
13319 			}
13320 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13321 			    cur_dma_attr->dma_attr_align));
13322 
13323 #ifdef SATA_DEBUG
13324 			mbuf_count++;
13325 
13326 			if (bp->b_bcount != bufsz)
13327 				/*
13328 				 * This will require special handling, because
13329 				 * DMA cookies will be based on the temporary
13330 				 * buffer size, not the original buffer
13331 				 * b_bcount, so the residue may have to
13332 				 * be counted differently.
13333 				 */
13334 				SATADBG2(SATA_DBG_DMA_SETUP,
13335 				    spx->txlt_sata_hba_inst,
13336 				    "sata_dma_buf_setup: bp size %x != "
13337 				    "bufsz %x\n", bp->b_bcount, bufsz);
13338 #endif
13339 			if (dma_flags & DDI_DMA_WRITE) {
13340 				/*
13341 				 * Write operation - copy data into
13342 				 * an aligned temporary buffer. Buffer will be
13343 				 * synced for device by ddi_dma_addr_bind_handle
13344 				 */
13345 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13346 				    bp->b_bcount);
13347 			}
13348 
13349 			rval = ddi_dma_addr_bind_handle(
13350 			    spx->txlt_buf_dma_handle,
13351 			    NULL,
13352 			    spx->txlt_tmp_buf,
13353 			    bufsz, dma_flags, ddicallback, 0,
13354 			    &spx->txlt_dma_cookie,
13355 			    &spx->txlt_curwin_num_dma_cookies);
13356 		}
13357 
13358 		switch (rval) {
13359 		case DDI_DMA_PARTIAL_MAP:
13360 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13361 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13362 			/*
13363 			 * Partial DMA mapping.
13364 			 * Retrieve number of DMA windows for this request.
13365 			 */
13366 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13367 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13368 				if (spx->txlt_tmp_buf != NULL) {
13369 					ddi_dma_mem_free(
13370 					    &spx->txlt_tmp_buf_handle);
13371 					spx->txlt_tmp_buf = NULL;
13372 				}
13373 				(void) ddi_dma_unbind_handle(
13374 				    spx->txlt_buf_dma_handle);
13375 				(void) ddi_dma_free_handle(
13376 				    &spx->txlt_buf_dma_handle);
13377 				spx->txlt_buf_dma_handle = NULL;
13378 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13379 				    "sata_dma_buf_setup: numwin failed\n"));
13380 				return (DDI_FAILURE);
13381 			}
13382 			SATADBG2(SATA_DBG_DMA_SETUP,
13383 			    spx->txlt_sata_hba_inst,
13384 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13385 			    spx->txlt_num_dma_win,
13386 			    spx->txlt_curwin_num_dma_cookies);
13387 			spx->txlt_cur_dma_win = 0;
13388 			break;
13389 
13390 		case DDI_DMA_MAPPED:
13391 			/* DMA fully mapped */
13392 			spx->txlt_num_dma_win = 1;
13393 			spx->txlt_cur_dma_win = 0;
13394 			SATADBG1(SATA_DBG_DMA_SETUP,
13395 			    spx->txlt_sata_hba_inst,
13396 			    "sata_dma_buf_setup: windows: 1 "
13397 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13398 			break;
13399 
13400 		default:
13401 			/* DMA mapping failed */
13402 			if (spx->txlt_tmp_buf != NULL) {
13403 				ddi_dma_mem_free(
13404 				    &spx->txlt_tmp_buf_handle);
13405 				spx->txlt_tmp_buf = NULL;
13406 			}
13407 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13408 			spx->txlt_buf_dma_handle = NULL;
13409 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13410 			    "sata_dma_buf_setup: buf dma handle binding "
13411 			    "failed %x\n", rval));
13412 			return (rval);
13413 		}
13414 		spx->txlt_curwin_processed_dma_cookies = 0;
13415 		spx->txlt_dma_cookie_list = NULL;
13416 	} else {
13417 		/*
13418 		 * DMA setup is reused. Check if we need to process more
13419 		 * cookies in current window, or to get next window, if any.
13420 		 */
13421 
13422 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13423 		    spx->txlt_curwin_num_dma_cookies);
13424 
13425 		if (spx->txlt_curwin_processed_dma_cookies ==
13426 		    spx->txlt_curwin_num_dma_cookies) {
13427 			/*
13428 			 * All cookies from current DMA window were processed.
13429 			 * Get next DMA window.
13430 			 */
13431 			spx->txlt_cur_dma_win++;
13432 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13433 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13434 				    spx->txlt_cur_dma_win, &offset, &size,
13435 				    &spx->txlt_dma_cookie,
13436 				    &spx->txlt_curwin_num_dma_cookies);
13437 				spx->txlt_curwin_processed_dma_cookies = 0;
13438 			} else {
13439 				/* No more windows! End of request! */
13440 				/* What to do? - panic for now */
13441 				ASSERT(spx->txlt_cur_dma_win >=
13442 				    spx->txlt_num_dma_win);
13443 
13444 				spx->txlt_curwin_num_dma_cookies = 0;
13445 				spx->txlt_curwin_processed_dma_cookies = 0;
13446 				spx->txlt_sata_pkt->
13447 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13448 				return (DDI_SUCCESS);
13449 			}
13450 		}
13451 	}
13452 	/* There better be at least one DMA cookie outstanding */
13453 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13454 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13455 
13456 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13457 		/* The default cookie slot was used in previous run */
13458 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13459 		spx->txlt_dma_cookie_list = NULL;
13460 		spx->txlt_dma_cookie_list_len = 0;
13461 	}
13462 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13463 		/*
13464 		 * Processing a new DMA window - set-up dma cookies list.
13465 		 * We may reuse previously allocated cookie array if it is
13466 		 * possible.
13467 		 */
13468 		if (spx->txlt_dma_cookie_list != NULL &&
13469 		    spx->txlt_dma_cookie_list_len <
13470 		    spx->txlt_curwin_num_dma_cookies) {
13471 			/*
13472 			 * New DMA window contains more cookies than
13473 			 * the previous one. We need larger cookie list - free
13474 			 * the old one.
13475 			 */
13476 			(void) kmem_free(spx->txlt_dma_cookie_list,
13477 			    spx->txlt_dma_cookie_list_len *
13478 			    sizeof (ddi_dma_cookie_t));
13479 			spx->txlt_dma_cookie_list = NULL;
13480 			spx->txlt_dma_cookie_list_len = 0;
13481 		}
13482 		if (spx->txlt_dma_cookie_list == NULL) {
13483 			/*
13484 			 * Calculate lesser of number of cookies in this
13485 			 * DMA window and number of s/g entries.
13486 			 */
13487 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13488 			req_len = MIN(max_sg_len,
13489 			    spx->txlt_curwin_num_dma_cookies);
13490 
13491 			/* Allocate new dma cookie array if necessary */
13492 			if (req_len == 1) {
13493 				/* Only one cookie - no need for a list */
13494 				spx->txlt_dma_cookie_list =
13495 				    &spx->txlt_dma_cookie;
13496 				spx->txlt_dma_cookie_list_len = 1;
13497 			} else {
13498 				/*
13499 				 * More than one cookie - try to allocate space.
13500 				 */
13501 				spx->txlt_dma_cookie_list = kmem_zalloc(
13502 				    sizeof (ddi_dma_cookie_t) * req_len,
13503 				    callback == NULL_FUNC ? KM_NOSLEEP :
13504 				    KM_SLEEP);
13505 				if (spx->txlt_dma_cookie_list == NULL) {
13506 					SATADBG1(SATA_DBG_DMA_SETUP,
13507 					    spx->txlt_sata_hba_inst,
13508 					    "sata_dma_buf_setup: cookie list "
13509 					    "allocation failed\n", NULL);
13510 					/*
13511 					 * We could not allocate space for
13512 					 * neccessary number of dma cookies in
13513 					 * this window, so we fail this request.
13514 					 * Next invocation would try again to
13515 					 * allocate space for cookie list.
13516 					 * Note:Packet residue was not modified.
13517 					 */
13518 					return (DDI_DMA_NORESOURCES);
13519 				} else {
13520 					spx->txlt_dma_cookie_list_len = req_len;
13521 				}
13522 			}
13523 		}
13524 		/*
13525 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13526 		 * First cookie was already fetched.
13527 		 */
13528 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13529 		cur_txfer_len =
13530 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13531 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13532 		spx->txlt_curwin_processed_dma_cookies++;
13533 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13534 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13535 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13536 			    &spx->txlt_dma_cookie_list[i]);
13537 			cur_txfer_len +=
13538 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13539 			spx->txlt_curwin_processed_dma_cookies++;
13540 			spx->txlt_sata_pkt->
13541 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13542 		}
13543 	} else {
13544 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13545 		    "sata_dma_buf_setup: sliding within DMA window, "
13546 		    "cur cookie %d, total cookies %d\n",
13547 		    spx->txlt_curwin_processed_dma_cookies,
13548 		    spx->txlt_curwin_num_dma_cookies);
13549 
13550 		/*
13551 		 * Not all cookies from the current dma window were used because
13552 		 * of s/g limitation.
13553 		 * There is no need to re-size the list - it was set at
13554 		 * optimal size, or only default entry is used (s/g = 1).
13555 		 */
13556 		if (spx->txlt_dma_cookie_list == NULL) {
13557 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13558 			spx->txlt_dma_cookie_list_len = 1;
13559 		}
13560 		/*
13561 		 * Since we are processing remaining cookies in a DMA window,
13562 		 * there may be less of them than the number of entries in the
13563 		 * current dma cookie list.
13564 		 */
13565 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13566 		    (spx->txlt_curwin_num_dma_cookies -
13567 		    spx->txlt_curwin_processed_dma_cookies));
13568 
13569 		/* Fetch the next batch of cookies */
13570 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13571 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13572 			    &spx->txlt_dma_cookie_list[i]);
13573 			cur_txfer_len +=
13574 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13575 			spx->txlt_sata_pkt->
13576 			    satapkt_cmd.satacmd_num_dma_cookies++;
13577 			spx->txlt_curwin_processed_dma_cookies++;
13578 		}
13579 	}
13580 
13581 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13582 
13583 	/* Point sata_cmd to the cookie list */
13584 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13585 	    &spx->txlt_dma_cookie_list[0];
13586 
13587 	/* Remember number of DMA cookies passed in sata packet */
13588 	spx->txlt_num_dma_cookies =
13589 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13590 
13591 	ASSERT(cur_txfer_len != 0);
13592 	if (cur_txfer_len <= bp->b_bcount)
13593 		spx->txlt_total_residue -= cur_txfer_len;
13594 	else {
13595 		/*
13596 		 * Temporary DMA buffer has been padded by
13597 		 * ddi_dma_mem_alloc()!
13598 		 * This requires special handling, because DMA cookies are
13599 		 * based on the temporary buffer size, not the b_bcount,
13600 		 * and we have extra bytes to transfer - but the packet
13601 		 * residue has to stay correct because we will copy only
13602 		 * the requested number of bytes.
13603 		 */
13604 		spx->txlt_total_residue -= bp->b_bcount;
13605 	}
13606 
13607 	return (DDI_SUCCESS);
13608 }
13609 
13610 /*
13611  * Common routine for releasing DMA resources
13612  */
13613 static void
13614 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13615 {
13616 	if (spx->txlt_buf_dma_handle != NULL) {
13617 		if (spx->txlt_tmp_buf != NULL)  {
13618 			/*
13619 			 * Intermediate DMA buffer was allocated.
13620 			 * Free allocated buffer and associated access handle.
13621 			 */
13622 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13623 			spx->txlt_tmp_buf = NULL;
13624 		}
13625 		/*
13626 		 * Free DMA resources - cookies and handles
13627 		 */
13628 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13629 		if (spx->txlt_dma_cookie_list != NULL) {
13630 			if (spx->txlt_dma_cookie_list !=
13631 			    &spx->txlt_dma_cookie) {
13632 				(void) kmem_free(spx->txlt_dma_cookie_list,
13633 				    spx->txlt_dma_cookie_list_len *
13634 				    sizeof (ddi_dma_cookie_t));
13635 				spx->txlt_dma_cookie_list = NULL;
13636 			}
13637 		}
13638 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13639 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13640 		spx->txlt_buf_dma_handle = NULL;
13641 	}
13642 }
13643 
13644 /*
13645  * Free DMA resources
13646  * Used by the HBA driver to release DMA resources that it does not use.
13647  *
13648  * Returns Void
13649  */
13650 void
13651 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13652 {
13653 	sata_pkt_txlate_t *spx;
13654 
13655 	if (sata_pkt == NULL)
13656 		return;
13657 
13658 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13659 
13660 	sata_common_free_dma_rsrcs(spx);
13661 }
13662 
13663 /*
13664  * Fetch Device Identify data.
13665  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13666  * command to a device and get the device identify data.
13667  * The device_info structure has to be set to device type (for selecting proper
13668  * device identify command).
13669  *
13670  * Returns:
13671  * SATA_SUCCESS if cmd succeeded
13672  * SATA_RETRY if cmd was rejected and could be retried,
13673  * SATA_FAILURE if cmd failed and should not be retried (port error)
13674  *
13675  * Cannot be called in an interrupt context.
13676  */
13677 
13678 static int
13679 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13680     sata_drive_info_t *sdinfo)
13681 {
13682 	struct buf *bp;
13683 	sata_pkt_t *spkt;
13684 	sata_cmd_t *scmd;
13685 	sata_pkt_txlate_t *spx;
13686 	int rval;
13687 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13688 
13689 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13690 	spx->txlt_sata_hba_inst = sata_hba_inst;
13691 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13692 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13693 	if (spkt == NULL) {
13694 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13695 		return (SATA_RETRY); /* may retry later */
13696 	}
13697 	/* address is needed now */
13698 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13699 
13700 	/*
13701 	 * Allocate buffer for Identify Data return data
13702 	 */
13703 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13704 	if (bp == NULL) {
13705 		sata_pkt_free(spx);
13706 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13707 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13708 		    "sata_fetch_device_identify_data: "
13709 		    "cannot allocate buffer for ID"));
13710 		return (SATA_RETRY); /* may retry later */
13711 	}
13712 
13713 	/* Fill sata_pkt */
13714 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13715 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13716 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13717 	/* Synchronous mode, no callback */
13718 	spkt->satapkt_comp = NULL;
13719 	/* Timeout 30s */
13720 	spkt->satapkt_time = sata_default_pkt_time;
13721 
13722 	scmd = &spkt->satapkt_cmd;
13723 	scmd->satacmd_bp = bp;
13724 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13725 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13726 
13727 	/* Build Identify Device cmd in the sata_pkt */
13728 	scmd->satacmd_addr_type = 0;		/* N/A */
13729 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13730 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13731 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13732 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13733 	scmd->satacmd_features_reg = 0;		/* N/A */
13734 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13735 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13736 		/* Identify Packet Device cmd */
13737 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13738 	} else {
13739 		/* Identify Device cmd - mandatory for all other devices */
13740 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13741 	}
13742 
13743 	/* Send pkt to SATA HBA driver */
13744 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13745 
13746 #ifdef SATA_INJECT_FAULTS
13747 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13748 #endif
13749 
13750 	if (rval == SATA_TRAN_ACCEPTED &&
13751 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13752 		if (spx->txlt_buf_dma_handle != NULL) {
13753 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13754 			    DDI_DMA_SYNC_FORKERNEL);
13755 			ASSERT(rval == DDI_SUCCESS);
13756 			if (sata_check_for_dma_error(dip, spx)) {
13757 				ddi_fm_service_impact(dip,
13758 				    DDI_SERVICE_UNAFFECTED);
13759 				rval = SATA_RETRY;
13760 				goto fail;
13761 			}
13762 
13763 		}
13764 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13765 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13766 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13767 			    "SATA disk device at port %d - "
13768 			    "partial Identify Data",
13769 			    sdinfo->satadrv_addr.cport));
13770 			rval = SATA_RETRY; /* may retry later */
13771 			goto fail;
13772 		}
13773 		/* Update sata_drive_info */
13774 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13775 		    sizeof (sata_id_t));
13776 
13777 		sdinfo->satadrv_features_support = 0;
13778 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13779 			/*
13780 			 * Retrieve capacity (disks only) and addressing mode
13781 			 */
13782 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13783 		} else {
13784 			/*
13785 			 * For ATAPI devices one would have to issue
13786 			 * Get Capacity cmd for media capacity. Not here.
13787 			 */
13788 			sdinfo->satadrv_capacity = 0;
13789 			/*
13790 			 * Check what cdb length is supported
13791 			 */
13792 			if ((sdinfo->satadrv_id.ai_config &
13793 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13794 				sdinfo->satadrv_atapi_cdb_len = 16;
13795 			else
13796 				sdinfo->satadrv_atapi_cdb_len = 12;
13797 		}
13798 		/* Setup supported features flags */
13799 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13800 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13801 
13802 		/* Check for SATA GEN and NCQ support */
13803 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13804 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13805 			/* SATA compliance */
13806 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13807 				sdinfo->satadrv_features_support |=
13808 				    SATA_DEV_F_NCQ;
13809 			if (sdinfo->satadrv_id.ai_satacap &
13810 			    (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13811 				if (sdinfo->satadrv_id.ai_satacap &
13812 				    SATA_3_SPEED)
13813 					sdinfo->satadrv_features_support |=
13814 					    SATA_DEV_F_SATA3;
13815 				if (sdinfo->satadrv_id.ai_satacap &
13816 				    SATA_2_SPEED)
13817 					sdinfo->satadrv_features_support |=
13818 					    SATA_DEV_F_SATA2;
13819 				if (sdinfo->satadrv_id.ai_satacap &
13820 				    SATA_1_SPEED)
13821 					sdinfo->satadrv_features_support |=
13822 					    SATA_DEV_F_SATA1;
13823 			} else {
13824 				sdinfo->satadrv_features_support |=
13825 				    SATA_DEV_F_SATA1;
13826 			}
13827 		}
13828 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13829 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13830 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13831 
13832 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13833 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13834 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13835 			++sdinfo->satadrv_queue_depth;
13836 			/* Adjust according to controller capabilities */
13837 			sdinfo->satadrv_max_queue_depth = MIN(
13838 			    sdinfo->satadrv_queue_depth,
13839 			    SATA_QDEPTH(sata_hba_inst));
13840 			/* Adjust according to global queue depth limit */
13841 			sdinfo->satadrv_max_queue_depth = MIN(
13842 			    sdinfo->satadrv_max_queue_depth,
13843 			    sata_current_max_qdepth);
13844 			if (sdinfo->satadrv_max_queue_depth == 0)
13845 				sdinfo->satadrv_max_queue_depth = 1;
13846 		} else
13847 			sdinfo->satadrv_max_queue_depth = 1;
13848 
13849 		rval = SATA_SUCCESS;
13850 	} else {
13851 		/*
13852 		 * Woops, no Identify Data.
13853 		 */
13854 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13855 			rval = SATA_RETRY; /* may retry later */
13856 		} else if (rval == SATA_TRAN_ACCEPTED) {
13857 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13858 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13859 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13860 			    spkt->satapkt_reason == SATA_PKT_RESET)
13861 				rval = SATA_RETRY; /* may retry later */
13862 			else
13863 				rval = SATA_FAILURE;
13864 		} else {
13865 			rval = SATA_FAILURE;
13866 		}
13867 	}
13868 fail:
13869 	/* Free allocated resources */
13870 	sata_free_local_buffer(spx);
13871 	sata_pkt_free(spx);
13872 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13873 
13874 	return (rval);
13875 }
13876 
13877 
13878 /*
13879  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13880  * UDMA mode is checked first, followed by MWDMA mode.
13881  * set correctly, so this function is setting it to the highest supported level.
13882  * Older SATA spec required that the device supports at least DMA 4 mode and
13883  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13884  * restriction has been removed.
13885  *
13886  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13887  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13888  *
13889  * NOTE: This function should be called only if DMA mode is supported.
13890  */
13891 static int
13892 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13893 {
13894 	sata_pkt_t *spkt;
13895 	sata_cmd_t *scmd;
13896 	sata_pkt_txlate_t *spx;
13897 	int i, mode;
13898 	uint8_t subcmd;
13899 	int rval = SATA_SUCCESS;
13900 
13901 	ASSERT(sdinfo != NULL);
13902 	ASSERT(sata_hba_inst != NULL);
13903 
13904 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13905 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13906 		/* Find highest Ultra DMA mode supported */
13907 		for (mode = 6; mode >= 0; --mode) {
13908 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13909 				break;
13910 		}
13911 #if 0
13912 		/* Left for historical reasons */
13913 		/*
13914 		 * Some initial version of SATA spec indicated that at least
13915 		 * UDMA mode 4 has to be supported. It is not mentioned in
13916 		 * SerialATA 2.6, so this restriction is removed.
13917 		 */
13918 		if (mode < 4)
13919 			return (SATA_FAILURE);
13920 #endif
13921 
13922 		/*
13923 		 * For disk, we're still going to set DMA mode whatever is
13924 		 * selected by default
13925 		 *
13926 		 * We saw an old maxtor sata drive will select Ultra DMA and
13927 		 * Multi-Word DMA simultaneouly by default, which is going
13928 		 * to cause DMA command timed out, so we need to select DMA
13929 		 * mode even when it's already done by default
13930 		 */
13931 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13932 
13933 			/* Find UDMA mode currently selected */
13934 			for (i = 6; i >= 0; --i) {
13935 				if (sdinfo->satadrv_id.ai_ultradma &
13936 				    (1 << (i + 8)))
13937 					break;
13938 			}
13939 			if (i >= mode)
13940 				/* Nothing to do */
13941 				return (SATA_SUCCESS);
13942 		}
13943 
13944 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13945 
13946 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13947 		/* Find highest MultiWord DMA mode supported */
13948 		for (mode = 2; mode >= 0; --mode) {
13949 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13950 				break;
13951 		}
13952 
13953 		/*
13954 		 * For disk, We're still going to set DMA mode whatever is
13955 		 * selected by default
13956 		 *
13957 		 * We saw an old maxtor sata drive will select Ultra DMA and
13958 		 * Multi-Word DMA simultaneouly by default, which is going
13959 		 * to cause DMA command timed out, so we need to select DMA
13960 		 * mode even when it's already done by default
13961 		 */
13962 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13963 
13964 			/* Find highest MultiWord DMA mode selected */
13965 			for (i = 2; i >= 0; --i) {
13966 				if (sdinfo->satadrv_id.ai_dworddma &
13967 				    (1 << (i + 8)))
13968 					break;
13969 			}
13970 			if (i >= mode)
13971 				/* Nothing to do */
13972 				return (SATA_SUCCESS);
13973 		}
13974 
13975 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13976 	} else
13977 		return (SATA_SUCCESS);
13978 
13979 	/*
13980 	 * Set DMA mode via SET FEATURES COMMAND.
13981 	 * Prepare packet for SET FEATURES COMMAND.
13982 	 */
13983 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13984 	spx->txlt_sata_hba_inst = sata_hba_inst;
13985 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13986 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13987 	if (spkt == NULL) {
13988 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13989 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
13990 		rval = SATA_FAILURE;
13991 		goto done;
13992 	}
13993 	/* Fill sata_pkt */
13994 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13995 	/* Timeout 30s */
13996 	spkt->satapkt_time = sata_default_pkt_time;
13997 	/* Synchronous mode, no callback, interrupts */
13998 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13999 	spkt->satapkt_comp = NULL;
14000 	scmd = &spkt->satapkt_cmd;
14001 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14002 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14003 	scmd->satacmd_addr_type = 0;
14004 	scmd->satacmd_device_reg = 0;
14005 	scmd->satacmd_status_reg = 0;
14006 	scmd->satacmd_error_reg = 0;
14007 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14008 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14009 	scmd->satacmd_sec_count_lsb = subcmd | mode;
14010 
14011 	/* Transfer command to HBA */
14012 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14013 	    spkt) != SATA_TRAN_ACCEPTED ||
14014 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14015 		/* Pkt execution failed */
14016 		rval = SATA_FAILURE;
14017 	}
14018 done:
14019 
14020 	/* Free allocated resources */
14021 	if (spkt != NULL)
14022 		sata_pkt_free(spx);
14023 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14024 
14025 	return (rval);
14026 }
14027 
14028 
14029 /*
14030  * Set device caching mode.
14031  * One of the following operations should be specified:
14032  * SATAC_SF_ENABLE_READ_AHEAD
14033  * SATAC_SF_DISABLE_READ_AHEAD
14034  * SATAC_SF_ENABLE_WRITE_CACHE
14035  * SATAC_SF_DISABLE_WRITE_CACHE
14036  *
14037  * If operation fails, system log messgage is emitted.
14038  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14039  * command was sent but did not succeed, and SATA_FAILURE otherwise.
14040  */
14041 
14042 static int
14043 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14044     int cache_op)
14045 {
14046 	sata_pkt_t *spkt;
14047 	sata_cmd_t *scmd;
14048 	sata_pkt_txlate_t *spx;
14049 	int rval = SATA_SUCCESS;
14050 	int hba_rval;
14051 	char *infop;
14052 
14053 	ASSERT(sdinfo != NULL);
14054 	ASSERT(sata_hba_inst != NULL);
14055 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14056 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14057 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14058 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14059 
14060 
14061 	/* Prepare packet for SET FEATURES COMMAND */
14062 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14063 	spx->txlt_sata_hba_inst = sata_hba_inst;
14064 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14065 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14066 	if (spkt == NULL) {
14067 		rval = SATA_FAILURE;
14068 		goto failure;
14069 	}
14070 	/* Fill sata_pkt */
14071 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14072 	/* Timeout 30s */
14073 	spkt->satapkt_time = sata_default_pkt_time;
14074 	/* Synchronous mode, no callback, interrupts */
14075 	spkt->satapkt_op_mode =
14076 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14077 	spkt->satapkt_comp = NULL;
14078 	scmd = &spkt->satapkt_cmd;
14079 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14080 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14081 	scmd->satacmd_addr_type = 0;
14082 	scmd->satacmd_device_reg = 0;
14083 	scmd->satacmd_status_reg = 0;
14084 	scmd->satacmd_error_reg = 0;
14085 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14086 	scmd->satacmd_features_reg = cache_op;
14087 
14088 	/* Transfer command to HBA */
14089 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14090 	    SATA_DIP(sata_hba_inst), spkt);
14091 
14092 #ifdef SATA_INJECT_FAULTS
14093 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14094 #endif
14095 
14096 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14097 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14098 		/* Pkt execution failed */
14099 		switch (cache_op) {
14100 		case SATAC_SF_ENABLE_READ_AHEAD:
14101 			infop = "enabling read ahead failed";
14102 			break;
14103 		case SATAC_SF_DISABLE_READ_AHEAD:
14104 			infop = "disabling read ahead failed";
14105 			break;
14106 		case SATAC_SF_ENABLE_WRITE_CACHE:
14107 			infop = "enabling write cache failed";
14108 			break;
14109 		case SATAC_SF_DISABLE_WRITE_CACHE:
14110 			infop = "disabling write cache failed";
14111 			break;
14112 		}
14113 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14114 		rval = SATA_RETRY;
14115 	}
14116 failure:
14117 	/* Free allocated resources */
14118 	if (spkt != NULL)
14119 		sata_pkt_free(spx);
14120 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14121 	return (rval);
14122 }
14123 
14124 /*
14125  * Set Removable Media Status Notification (enable/disable)
14126  * state == 0 , disable
14127  * state != 0 , enable
14128  *
14129  * If operation fails, system log messgage is emitted.
14130  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14131  */
14132 
14133 static int
14134 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14135     int state)
14136 {
14137 	sata_pkt_t *spkt;
14138 	sata_cmd_t *scmd;
14139 	sata_pkt_txlate_t *spx;
14140 	int rval = SATA_SUCCESS;
14141 	char *infop;
14142 
14143 	ASSERT(sdinfo != NULL);
14144 	ASSERT(sata_hba_inst != NULL);
14145 
14146 	/* Prepare packet for SET FEATURES COMMAND */
14147 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14148 	spx->txlt_sata_hba_inst = sata_hba_inst;
14149 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14150 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14151 	if (spkt == NULL) {
14152 		rval = SATA_FAILURE;
14153 		goto failure;
14154 	}
14155 	/* Fill sata_pkt */
14156 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14157 	/* Timeout 30s */
14158 	spkt->satapkt_time = sata_default_pkt_time;
14159 	/* Synchronous mode, no callback, interrupts */
14160 	spkt->satapkt_op_mode =
14161 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14162 	spkt->satapkt_comp = NULL;
14163 	scmd = &spkt->satapkt_cmd;
14164 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14165 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14166 	scmd->satacmd_addr_type = 0;
14167 	scmd->satacmd_device_reg = 0;
14168 	scmd->satacmd_status_reg = 0;
14169 	scmd->satacmd_error_reg = 0;
14170 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14171 	if (state == 0)
14172 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14173 	else
14174 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14175 
14176 	/* Transfer command to HBA */
14177 	if (((*SATA_START_FUNC(sata_hba_inst))(
14178 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14179 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14180 		/* Pkt execution failed */
14181 		if (state == 0)
14182 			infop = "disabling Removable Media Status "
14183 			    "Notification failed";
14184 		else
14185 			infop = "enabling Removable Media Status "
14186 			    "Notification failed";
14187 
14188 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14189 		rval = SATA_FAILURE;
14190 	}
14191 failure:
14192 	/* Free allocated resources */
14193 	if (spkt != NULL)
14194 		sata_pkt_free(spx);
14195 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14196 	return (rval);
14197 }
14198 
14199 
14200 /*
14201  * Update state and copy port ss* values from passed sata_device structure.
14202  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14203  * configuration struct.
14204  *
14205  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14206  * regardless of the state in device argument.
14207  *
14208  * Port mutex should be held while calling this function.
14209  */
14210 static void
14211 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14212     sata_device_t *sata_device)
14213 {
14214 	sata_cport_info_t *cportinfo;
14215 
14216 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14217 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14218 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14219 		    sata_device->satadev_addr.cport)
14220 			return;
14221 
14222 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14223 		    sata_device->satadev_addr.cport);
14224 
14225 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14226 		cportinfo->cport_scr = sata_device->satadev_scr;
14227 
14228 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14229 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14230 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14231 		cportinfo->cport_state |=
14232 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14233 	}
14234 }
14235 
14236 void
14237 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14238     sata_device_t *sata_device)
14239 {
14240 	sata_pmport_info_t *pmportinfo;
14241 
14242 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14243 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14244 	    SATA_NUM_PMPORTS(sata_hba_inst,
14245 	    sata_device->satadev_addr.cport) <
14246 	    sata_device->satadev_addr.pmport) {
14247 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14248 		    "sata_update_port_info: error address %p.",
14249 		    &sata_device->satadev_addr);
14250 		return;
14251 	}
14252 
14253 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14254 	    sata_device->satadev_addr.cport,
14255 	    sata_device->satadev_addr.pmport);
14256 
14257 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14258 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14259 
14260 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14261 	pmportinfo->pmport_state &=
14262 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14263 	pmportinfo->pmport_state |=
14264 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14265 }
14266 
14267 /*
14268  * Extract SATA port specification from an IOCTL argument.
14269  *
14270  * This function return the port the user land send us as is, unless it
14271  * cannot retrieve port spec, then -1 is returned.
14272  *
14273  * Support port multiplier.
14274  */
14275 static int32_t
14276 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14277 {
14278 	int32_t port;
14279 
14280 	/* Extract port number from nvpair in dca structure  */
14281 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14282 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14283 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14284 		    port));
14285 		port = -1;
14286 	}
14287 
14288 	return (port);
14289 }
14290 
14291 /*
14292  * Get dev_info_t pointer to the device node pointed to by port argument.
14293  * NOTE: target argument is a value used in ioctls to identify
14294  * the AP - it is not a sata_address.
14295  * It is a combination of cport, pmport and address qualifier, encodded same
14296  * way as a scsi target number.
14297  * At this moment it carries only cport number.
14298  *
14299  * PMult hotplug is supported now.
14300  *
14301  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14302  */
14303 
14304 static dev_info_t *
14305 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14306 {
14307 	dev_info_t	*cdip = NULL;
14308 	int		target, tgt;
14309 	int 		circ;
14310 	uint8_t		qual;
14311 
14312 	sata_hba_inst_t	*sata_hba_inst;
14313 	scsi_hba_tran_t *scsi_hba_tran;
14314 
14315 	/* Get target id */
14316 	scsi_hba_tran = ddi_get_driver_private(dip);
14317 	if (scsi_hba_tran == NULL)
14318 		return (NULL);
14319 
14320 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14321 
14322 	if (sata_hba_inst == NULL)
14323 		return (NULL);
14324 
14325 	/* Identify a port-mult by cport_info.cport_dev_type */
14326 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14327 		qual = SATA_ADDR_DPMPORT;
14328 	else
14329 		qual = SATA_ADDR_DCPORT;
14330 
14331 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14332 
14333 	/* Retrieve target dip */
14334 	ndi_devi_enter(dip, &circ);
14335 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14336 		dev_info_t *next = ddi_get_next_sibling(cdip);
14337 
14338 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14339 		    DDI_PROP_DONTPASS, "target", -1);
14340 		if (tgt == -1) {
14341 			/*
14342 			 * This is actually an error condition, but not
14343 			 * a fatal one. Just continue the search.
14344 			 */
14345 			cdip = next;
14346 			continue;
14347 		}
14348 
14349 		if (tgt == target)
14350 			break;
14351 
14352 		cdip = next;
14353 	}
14354 	ndi_devi_exit(dip, circ);
14355 
14356 	return (cdip);
14357 }
14358 
14359 /*
14360  * Get dev_info_t pointer to the device node pointed to by port argument.
14361  * NOTE: target argument is a value used in ioctls to identify
14362  * the AP - it is not a sata_address.
14363  * It is a combination of cport, pmport and address qualifier, encoded same
14364  * way as a scsi target number.
14365  *
14366  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14367  */
14368 
14369 static dev_info_t *
14370 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14371 {
14372 	dev_info_t	*cdip = NULL;
14373 	int		target, tgt;
14374 	int 		circ;
14375 
14376 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14377 
14378 	ndi_devi_enter(dip, &circ);
14379 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14380 		dev_info_t *next = ddi_get_next_sibling(cdip);
14381 
14382 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14383 		    DDI_PROP_DONTPASS, "target", -1);
14384 		if (tgt == -1) {
14385 			/*
14386 			 * This is actually an error condition, but not
14387 			 * a fatal one. Just continue the search.
14388 			 */
14389 			cdip = next;
14390 			continue;
14391 		}
14392 
14393 		if (tgt == target)
14394 			break;
14395 
14396 		cdip = next;
14397 	}
14398 	ndi_devi_exit(dip, circ);
14399 
14400 	return (cdip);
14401 }
14402 
14403 /*
14404  * Process sata port disconnect request.
14405  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14406  * before this request. Nevertheless, if a device is still configured,
14407  * we need to attempt to offline and unconfigure device.
14408  * Regardless of the unconfigure operation results the port is marked as
14409  * deactivated and no access to the attached device is possible.
14410  * If the target node remains because unconfigure operation failed, its state
14411  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14412  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14413  * the device and remove old target node.
14414  *
14415  * This function invokes sata_hba_inst->satahba_tran->
14416  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14417  * If successful, the device structure (if any) attached to the specified port
14418  * is removed and state of the port marked appropriately.
14419  * Failure of the port_deactivate may keep port in the physically active state,
14420  * or may fail the port.
14421  *
14422  * NOTE: Port multiplier is supported.
14423  */
14424 
14425 static int
14426 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14427     sata_device_t *sata_device)
14428 {
14429 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14430 	sata_cport_info_t *cportinfo = NULL;
14431 	sata_pmport_info_t *pmportinfo = NULL;
14432 	sata_pmult_info_t *pmultinfo = NULL;
14433 	sata_device_t subsdevice;
14434 	int cport, pmport, qual;
14435 	int rval = SATA_SUCCESS;
14436 	int npmport = 0;
14437 	int rv = 0;
14438 
14439 	cport = sata_device->satadev_addr.cport;
14440 	pmport = sata_device->satadev_addr.pmport;
14441 	qual = sata_device->satadev_addr.qual;
14442 
14443 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14444 	if (qual == SATA_ADDR_DCPORT)
14445 		qual = SATA_ADDR_CPORT;
14446 	else
14447 		qual = SATA_ADDR_PMPORT;
14448 
14449 	/*
14450 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14451 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14452 	 * Do the sanity check.
14453 	 */
14454 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14455 		/* No physical port deactivation supported. */
14456 		return (EINVAL);
14457 	}
14458 
14459 	/* Check the current state of the port */
14460 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14461 	    (SATA_DIP(sata_hba_inst), sata_device);
14462 
14463 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14464 
14465 	/*
14466 	 * Processing port mulitiplier
14467 	 */
14468 	if (qual == SATA_ADDR_CPORT &&
14469 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14470 		mutex_enter(&cportinfo->cport_mutex);
14471 
14472 		/* Check controller port status */
14473 		sata_update_port_info(sata_hba_inst, sata_device);
14474 		if (rval != SATA_SUCCESS ||
14475 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14476 			/*
14477 			 * Device port status is unknown or it is in failed
14478 			 * state
14479 			 */
14480 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14481 			    SATA_PSTATE_FAILED;
14482 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14483 			    "sata_hba_ioctl: connect: failed to deactivate "
14484 			    "SATA port %d", cport);
14485 			mutex_exit(&cportinfo->cport_mutex);
14486 			return (EIO);
14487 		}
14488 
14489 		/* Disconnect all sub-devices. */
14490 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14491 		if (pmultinfo != NULL) {
14492 
14493 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14494 			    sata_hba_inst, cport); npmport ++) {
14495 				subsdinfo = SATA_PMPORT_DRV_INFO(
14496 				    sata_hba_inst, cport, npmport);
14497 				if (subsdinfo == NULL)
14498 					continue;
14499 
14500 				subsdevice.satadev_addr = subsdinfo->
14501 				    satadrv_addr;
14502 
14503 				mutex_exit(&cportinfo->cport_mutex);
14504 				if (sata_ioctl_disconnect(sata_hba_inst,
14505 				    &subsdevice) == SATA_SUCCESS) {
14506 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14507 					"[Remove] device at port %d:%d "
14508 					"successfully.", cport, npmport);
14509 				}
14510 				mutex_enter(&cportinfo->cport_mutex);
14511 			}
14512 		}
14513 
14514 		/* Disconnect the port multiplier */
14515 		cportinfo->cport_state &= ~SATA_STATE_READY;
14516 		mutex_exit(&cportinfo->cport_mutex);
14517 
14518 		sata_device->satadev_addr.qual = qual;
14519 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14520 		    (SATA_DIP(sata_hba_inst), sata_device);
14521 
14522 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14523 		    SE_NO_HINT);
14524 
14525 		mutex_enter(&cportinfo->cport_mutex);
14526 		sata_update_port_info(sata_hba_inst, sata_device);
14527 		if (rval != SATA_SUCCESS &&
14528 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14529 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14530 			rv = EIO;
14531 		} else {
14532 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14533 		}
14534 		mutex_exit(&cportinfo->cport_mutex);
14535 
14536 		return (rv);
14537 	}
14538 
14539 	/*
14540 	 * Process non-port-multiplier device - it could be a drive connected
14541 	 * to a port multiplier port or a controller port.
14542 	 */
14543 	if (qual == SATA_ADDR_PMPORT) {
14544 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14545 		mutex_enter(&pmportinfo->pmport_mutex);
14546 		sata_update_pmport_info(sata_hba_inst, sata_device);
14547 		if (rval != SATA_SUCCESS ||
14548 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14549 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14550 			    SATA_PSTATE_FAILED;
14551 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14552 			    "sata_hba_ioctl: connect: failed to deactivate "
14553 			    "SATA port %d:%d", cport, pmport);
14554 			mutex_exit(&pmportinfo->pmport_mutex);
14555 			return (EIO);
14556 		}
14557 
14558 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14559 			sdinfo = pmportinfo->pmport_sata_drive;
14560 			ASSERT(sdinfo != NULL);
14561 		}
14562 
14563 		/*
14564 		 * Set port's dev_state to not ready - this will disable
14565 		 * an access to a potentially attached device.
14566 		 */
14567 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14568 
14569 		/* Remove and release sata_drive info structure. */
14570 		if (sdinfo != NULL) {
14571 			if ((sdinfo->satadrv_type &
14572 			    SATA_VALID_DEV_TYPE) != 0) {
14573 				/*
14574 				 * If a target node exists, try to offline
14575 				 * a device and remove target node.
14576 				 */
14577 				mutex_exit(&pmportinfo->pmport_mutex);
14578 				(void) sata_offline_device(sata_hba_inst,
14579 				    sata_device, sdinfo);
14580 				mutex_enter(&pmportinfo->pmport_mutex);
14581 			}
14582 
14583 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14584 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14585 			(void) kmem_free((void *)sdinfo,
14586 			    sizeof (sata_drive_info_t));
14587 		}
14588 		mutex_exit(&pmportinfo->pmport_mutex);
14589 
14590 	} else if (qual == SATA_ADDR_CPORT) {
14591 		mutex_enter(&cportinfo->cport_mutex);
14592 		sata_update_port_info(sata_hba_inst, sata_device);
14593 		if (rval != SATA_SUCCESS ||
14594 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14595 			/*
14596 			 * Device port status is unknown or it is in failed
14597 			 * state
14598 			 */
14599 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14600 			    SATA_PSTATE_FAILED;
14601 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14602 			    "sata_hba_ioctl: connect: failed to deactivate "
14603 			    "SATA port %d", cport);
14604 			mutex_exit(&cportinfo->cport_mutex);
14605 			return (EIO);
14606 		}
14607 
14608 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14609 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14610 			ASSERT(pmultinfo != NULL);
14611 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14612 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14613 			ASSERT(sdinfo != NULL);
14614 		}
14615 		cportinfo->cport_state &= ~SATA_STATE_READY;
14616 
14617 		if (sdinfo != NULL) {
14618 			if ((sdinfo->satadrv_type &
14619 			    SATA_VALID_DEV_TYPE) != 0) {
14620 				/*
14621 				 * If a target node exists, try to offline
14622 				 * a device and remove target node.
14623 				 */
14624 				mutex_exit(&cportinfo->cport_mutex);
14625 				(void) sata_offline_device(sata_hba_inst,
14626 				    sata_device, sdinfo);
14627 				mutex_enter(&cportinfo->cport_mutex);
14628 			}
14629 
14630 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14631 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14632 			(void) kmem_free((void *)sdinfo,
14633 			    sizeof (sata_drive_info_t));
14634 		}
14635 		mutex_exit(&cportinfo->cport_mutex);
14636 	}
14637 
14638 	/* Just ask HBA driver to deactivate port */
14639 	sata_device->satadev_addr.qual = qual;
14640 
14641 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14642 	    (SATA_DIP(sata_hba_inst), sata_device);
14643 
14644 	/*
14645 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14646 	 * without the hint (to force listener to investivate the state).
14647 	 */
14648 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14649 	    SE_NO_HINT);
14650 
14651 	if (qual == SATA_ADDR_PMPORT) {
14652 		mutex_enter(&pmportinfo->pmport_mutex);
14653 		sata_update_pmport_info(sata_hba_inst, sata_device);
14654 
14655 		if (rval != SATA_SUCCESS &&
14656 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14657 			/*
14658 			 * Port deactivation failure - do not change port
14659 			 * state unless the state returned by HBA indicates a
14660 			 * port failure.
14661 			 *
14662 			 * NOTE: device structures were released, so devices
14663 			 * now are invisible! Port reset is needed to
14664 			 * re-enumerate devices.
14665 			 */
14666 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14667 			rv = EIO;
14668 		} else {
14669 			/*
14670 			 * Deactivation succeded. From now on the sata framework
14671 			 * will not care what is happening to the device, until
14672 			 * the port is activated again.
14673 			 */
14674 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14675 		}
14676 		mutex_exit(&pmportinfo->pmport_mutex);
14677 	} else if (qual == SATA_ADDR_CPORT) {
14678 		mutex_enter(&cportinfo->cport_mutex);
14679 		sata_update_port_info(sata_hba_inst, sata_device);
14680 
14681 		if (rval != SATA_SUCCESS &&
14682 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14683 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14684 			rv = EIO;
14685 		} else {
14686 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14687 		}
14688 		mutex_exit(&cportinfo->cport_mutex);
14689 	}
14690 
14691 	return (rv);
14692 }
14693 
14694 
14695 
14696 /*
14697  * Process sata port connect request
14698  * The sata cfgadm pluging will invoke this operation only if port was found
14699  * in the disconnect state (failed state is also treated as the disconnected
14700  * state).
14701  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14702  * sata_tran_hotplug_ops->sata_tran_port_activate().
14703  * If successful and a device is found attached to the port,
14704  * the initialization sequence is executed to attach a device structure to
14705  * a port structure. The state of the port and a device would be set
14706  * appropriately.
14707  * The device is not set in configured state (system-wise) by this operation.
14708  *
14709  * Note, that activating the port may generate link events,
14710  * so it is important that following processing and the
14711  * event processing does not interfere with each other!
14712  *
14713  * This operation may remove port failed state and will
14714  * try to make port active and in good standing.
14715  *
14716  * NOTE: Port multiplier is supported.
14717  */
14718 
14719 static int
14720 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14721     sata_device_t *sata_device)
14722 {
14723 	sata_pmport_info_t	*pmportinfo = NULL;
14724 	uint8_t cport, pmport, qual;
14725 	int rv = 0;
14726 
14727 	cport = sata_device->satadev_addr.cport;
14728 	pmport = sata_device->satadev_addr.pmport;
14729 	qual = sata_device->satadev_addr.qual;
14730 
14731 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14732 	if (qual == SATA_ADDR_DCPORT)
14733 		qual = SATA_ADDR_CPORT;
14734 	else
14735 		qual = SATA_ADDR_PMPORT;
14736 
14737 	if (qual == SATA_ADDR_PMPORT)
14738 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14739 
14740 	/*
14741 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14742 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14743 	 * Perform sanity check now.
14744 	 */
14745 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14746 		/* No physical port activation supported. */
14747 		return (EINVAL);
14748 	}
14749 
14750 	/* Just ask HBA driver to activate port */
14751 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14752 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14753 		/*
14754 		 * Port activation failure.
14755 		 */
14756 		if (qual == SATA_ADDR_CPORT) {
14757 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14758 			    cport)->cport_mutex);
14759 			sata_update_port_info(sata_hba_inst, sata_device);
14760 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14761 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14762 				    SATA_PSTATE_FAILED;
14763 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14764 				    "sata_hba_ioctl: connect: failed to "
14765 				    "activate SATA port %d", cport);
14766 			}
14767 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14768 			    cport)->cport_mutex);
14769 		} else { /* port multiplier device port */
14770 			mutex_enter(&pmportinfo->pmport_mutex);
14771 			sata_update_pmport_info(sata_hba_inst, sata_device);
14772 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14773 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14774 				    pmport) = SATA_PSTATE_FAILED;
14775 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14776 				    "sata_hba_ioctl: connect: failed to "
14777 				    "activate SATA port %d:%d", cport, pmport);
14778 			}
14779 			mutex_exit(&pmportinfo->pmport_mutex);
14780 		}
14781 		return (EIO);
14782 	}
14783 
14784 	/* Virgin port state - will be updated by the port re-probe. */
14785 	if (qual == SATA_ADDR_CPORT) {
14786 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14787 		    cport)->cport_mutex);
14788 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14789 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14790 		    cport)->cport_mutex);
14791 	} else { /* port multiplier device port */
14792 		mutex_enter(&pmportinfo->pmport_mutex);
14793 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14794 		mutex_exit(&pmportinfo->pmport_mutex);
14795 	}
14796 
14797 	/*
14798 	 * Probe the port to find its state and attached device.
14799 	 */
14800 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14801 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14802 		rv = EIO;
14803 
14804 	/*
14805 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14806 	 * without the hint
14807 	 */
14808 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14809 	    SE_NO_HINT);
14810 
14811 	/*
14812 	 * If there is a device attached to the port, emit
14813 	 * a message.
14814 	 */
14815 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14816 
14817 		if (qual == SATA_ADDR_CPORT) {
14818 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14819 				sata_log(sata_hba_inst, CE_WARN,
14820 				    "SATA port multiplier detected "
14821 				    "at port %d", cport);
14822 			} else {
14823 				sata_log(sata_hba_inst, CE_WARN,
14824 				    "SATA device detected at port %d", cport);
14825 				if (sata_device->satadev_type ==
14826 				    SATA_DTYPE_UNKNOWN) {
14827 				/*
14828 				 * A device was not successfully identified
14829 				 */
14830 				sata_log(sata_hba_inst, CE_WARN,
14831 				    "Could not identify SATA "
14832 				    "device at port %d", cport);
14833 				}
14834 			}
14835 		} else { /* port multiplier device port */
14836 			sata_log(sata_hba_inst, CE_WARN,
14837 			    "SATA device detected at port %d:%d",
14838 			    cport, pmport);
14839 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14840 				/*
14841 				 * A device was not successfully identified
14842 				 */
14843 				sata_log(sata_hba_inst, CE_WARN,
14844 				    "Could not identify SATA "
14845 				    "device at port %d:%d", cport, pmport);
14846 			}
14847 		}
14848 	}
14849 
14850 	return (rv);
14851 }
14852 
14853 
14854 /*
14855  * Process sata device unconfigure request.
14856  * The unconfigure operation uses generic nexus operation to
14857  * offline a device. It leaves a target device node attached.
14858  * and obviously sata_drive_info attached as well, because
14859  * from the hardware point of view nothing has changed.
14860  */
14861 static int
14862 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14863     sata_device_t *sata_device)
14864 {
14865 	int rv = 0;
14866 	dev_info_t *tdip;
14867 
14868 	/* We are addressing attached device, not a port */
14869 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14870 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14871 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14872 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14873 
14874 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14875 	    &sata_device->satadev_addr)) != NULL) {
14876 
14877 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14878 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14879 			    "sata_hba_ioctl: unconfigure: "
14880 			    "failed to unconfigure device at SATA port %d:%d",
14881 			    sata_device->satadev_addr.cport,
14882 			    sata_device->satadev_addr.pmport));
14883 			rv = EIO;
14884 		}
14885 		/*
14886 		 * The target node devi_state should be marked with
14887 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14888 		 * This would be the indication for cfgadm that
14889 		 * the AP node occupant state is 'unconfigured'.
14890 		 */
14891 
14892 	} else {
14893 		/*
14894 		 * This would indicate a failure on the part of cfgadm
14895 		 * to detect correct state of the node prior to this
14896 		 * call - one cannot unconfigure non-existing device.
14897 		 */
14898 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14899 		    "sata_hba_ioctl: unconfigure: "
14900 		    "attempt to unconfigure non-existing device "
14901 		    "at SATA port %d:%d",
14902 		    sata_device->satadev_addr.cport,
14903 		    sata_device->satadev_addr.pmport));
14904 		rv = ENXIO;
14905 	}
14906 	return (rv);
14907 }
14908 
14909 /*
14910  * Process sata device configure request
14911  * If port is in a failed state, operation is aborted - one has to use
14912  * an explicit connect or port activate request to try to get a port into
14913  * non-failed mode. Port reset wil also work in such situation.
14914  * If the port is in disconnected (shutdown) state, the connect operation is
14915  * attempted prior to any other action.
14916  * When port is in the active state, there is a device attached and the target
14917  * node exists, a device was most likely offlined.
14918  * If target node does not exist, a new target node is created. In both cases
14919  * an attempt is made to online (configure) the device.
14920  *
14921  * NOTE: Port multiplier is supported.
14922  */
14923 static int
14924 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14925     sata_device_t *sata_device)
14926 {
14927 	int cport, pmport, qual;
14928 	int rval;
14929 	boolean_t target = B_TRUE;
14930 	sata_cport_info_t *cportinfo;
14931 	sata_pmport_info_t *pmportinfo = NULL;
14932 	dev_info_t *tdip;
14933 	sata_drive_info_t *sdinfo;
14934 
14935 	cport = sata_device->satadev_addr.cport;
14936 	pmport = sata_device->satadev_addr.pmport;
14937 	qual = sata_device->satadev_addr.qual;
14938 
14939 	/* Get current port state */
14940 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14941 	    (SATA_DIP(sata_hba_inst), sata_device);
14942 
14943 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14944 	if (qual == SATA_ADDR_DPMPORT) {
14945 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14946 		mutex_enter(&pmportinfo->pmport_mutex);
14947 		sata_update_pmport_info(sata_hba_inst, sata_device);
14948 		if (rval != SATA_SUCCESS ||
14949 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14950 			/*
14951 			 * Obviously, device on a failed port is not visible
14952 			 */
14953 			mutex_exit(&pmportinfo->pmport_mutex);
14954 			return (ENXIO);
14955 		}
14956 		mutex_exit(&pmportinfo->pmport_mutex);
14957 	} else {
14958 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14959 		    cport)->cport_mutex);
14960 		sata_update_port_info(sata_hba_inst, sata_device);
14961 		if (rval != SATA_SUCCESS ||
14962 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14963 			/*
14964 			 * Obviously, device on a failed port is not visible
14965 			 */
14966 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14967 			    cport)->cport_mutex);
14968 			return (ENXIO);
14969 		}
14970 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14971 		    cport)->cport_mutex);
14972 	}
14973 
14974 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14975 		/* need to activate port */
14976 		target = B_FALSE;
14977 
14978 		/* Sanity check */
14979 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14980 			return (ENXIO);
14981 
14982 		/* Just let HBA driver to activate port */
14983 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14984 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14985 			/*
14986 			 * Port activation failure - do not change port state
14987 			 * unless the state returned by HBA indicates a port
14988 			 * failure.
14989 			 */
14990 			if (qual == SATA_ADDR_DPMPORT) {
14991 				mutex_enter(&pmportinfo->pmport_mutex);
14992 				sata_update_pmport_info(sata_hba_inst,
14993 				    sata_device);
14994 				if (sata_device->satadev_state &
14995 				    SATA_PSTATE_FAILED)
14996 					pmportinfo->pmport_state =
14997 					    SATA_PSTATE_FAILED;
14998 				mutex_exit(&pmportinfo->pmport_mutex);
14999 			} else {
15000 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15001 				    cport)->cport_mutex);
15002 				sata_update_port_info(sata_hba_inst,
15003 				    sata_device);
15004 				if (sata_device->satadev_state &
15005 				    SATA_PSTATE_FAILED)
15006 					cportinfo->cport_state =
15007 					    SATA_PSTATE_FAILED;
15008 				mutex_exit(&SATA_CPORT_INFO(
15009 				    sata_hba_inst, cport)->cport_mutex);
15010 			}
15011 		}
15012 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15013 		    "sata_hba_ioctl: configure: "
15014 		    "failed to activate SATA port %d:%d",
15015 		    cport, pmport));
15016 		return (EIO);
15017 	}
15018 	/*
15019 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15020 	 * without the hint.
15021 	 */
15022 	sata_gen_sysevent(sata_hba_inst,
15023 	    &sata_device->satadev_addr, SE_NO_HINT);
15024 
15025 	/* Virgin port state */
15026 	if (qual == SATA_ADDR_DPMPORT) {
15027 		mutex_enter(&pmportinfo->pmport_mutex);
15028 		pmportinfo->pmport_state = 0;
15029 		mutex_exit(&pmportinfo->pmport_mutex);
15030 	} else {
15031 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15032 		    cport)-> cport_mutex);
15033 		cportinfo->cport_state = 0;
15034 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15035 		    cport)->cport_mutex);
15036 	}
15037 	/*
15038 	 * Always reprobe port, to get current device info.
15039 	 */
15040 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15041 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15042 		return (EIO);
15043 
15044 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15045 		if (qual == SATA_ADDR_DPMPORT) {
15046 			/*
15047 			 * That's the transition from "inactive" port
15048 			 * to active one with device attached.
15049 			 */
15050 			sata_log(sata_hba_inst, CE_WARN,
15051 			    "SATA device detected at port %d:%d",
15052 			    cport, pmport);
15053 		} else {
15054 			/*
15055 			 * When PM is attached to the cport and cport is
15056 			 * activated, every PM device port needs to be reprobed.
15057 			 * We need to emit message for all devices detected
15058 			 * at port multiplier's device ports.
15059 			 * Add such code here.
15060 			 * For now, just inform about device attached to
15061 			 * cport.
15062 			 */
15063 			sata_log(sata_hba_inst, CE_WARN,
15064 			    "SATA device detected at port %d", cport);
15065 		}
15066 	}
15067 
15068 	/*
15069 	 * This is where real configuration operation starts.
15070 	 *
15071 	 * When PM is attached to the cport and cport is activated,
15072 	 * devices attached PM device ports may have to be configured
15073 	 * explicitly. This may change when port multiplier is supported.
15074 	 * For now, configure only disks and other valid target devices.
15075 	 */
15076 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15077 		if (qual == SATA_ADDR_DCPORT) {
15078 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15079 				/*
15080 				 * A device was not successfully identified
15081 				 */
15082 				sata_log(sata_hba_inst, CE_WARN,
15083 				    "Could not identify SATA "
15084 				    "device at port %d", cport);
15085 			}
15086 		} else { /* port multiplier device port */
15087 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15088 				/*
15089 				 * A device was not successfully identified
15090 				 */
15091 				sata_log(sata_hba_inst, CE_WARN,
15092 				    "Could not identify SATA "
15093 				    "device at port %d:%d", cport, pmport);
15094 			}
15095 		}
15096 		return (ENXIO);		/* No device to configure */
15097 	}
15098 
15099 	/*
15100 	 * Here we may have a device in reset condition,
15101 	 * but because we are just configuring it, there is
15102 	 * no need to process the reset other than just
15103 	 * to clear device reset condition in the HBA driver.
15104 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15105 	 * cause a first command sent the HBA driver with the request
15106 	 * to clear device reset condition.
15107 	 */
15108 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15109 	if (qual == SATA_ADDR_DPMPORT)
15110 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15111 	else
15112 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15113 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15114 	if (sdinfo == NULL) {
15115 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15116 		return (ENXIO);
15117 	}
15118 	if (sdinfo->satadrv_event_flags &
15119 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15120 		sdinfo->satadrv_event_flags = 0;
15121 	}
15122 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15123 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15124 
15125 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15126 	    &sata_device->satadev_addr)) != NULL) {
15127 		/*
15128 		 * Target node exists. Verify, that it belongs
15129 		 * to existing, attached device and not to
15130 		 * a removed device.
15131 		 */
15132 		if (sata_check_device_removed(tdip) == B_TRUE) {
15133 			if (qual == SATA_ADDR_DPMPORT)
15134 				sata_log(sata_hba_inst, CE_WARN,
15135 				    "SATA device at port %d cannot be "
15136 				    "configured. "
15137 				    "Application(s) accessing "
15138 				    "previously attached device "
15139 				    "have to release it before newly "
15140 				    "inserted device can be made accessible.",
15141 				    cport);
15142 			else
15143 				sata_log(sata_hba_inst, CE_WARN,
15144 				    "SATA device at port %d:%d cannot be"
15145 				    "configured. "
15146 				    "Application(s) accessing "
15147 				    "previously attached device "
15148 				    "have to release it before newly "
15149 				    "inserted device can be made accessible.",
15150 				    cport, pmport);
15151 			return (EIO);
15152 		}
15153 		/*
15154 		 * Device was not removed and re-inserted.
15155 		 * Try to online it.
15156 		 */
15157 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15158 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15159 			    "sata_hba_ioctl: configure: "
15160 			    "onlining device at SATA port "
15161 			    "%d:%d failed", cport, pmport));
15162 			return (EIO);
15163 		}
15164 
15165 		if (qual == SATA_ADDR_DPMPORT) {
15166 			mutex_enter(&pmportinfo->pmport_mutex);
15167 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15168 			mutex_exit(&pmportinfo->pmport_mutex);
15169 		} else {
15170 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15171 			    cport)->cport_mutex);
15172 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15173 			mutex_exit(&SATA_CPORT_INFO(
15174 			    sata_hba_inst, cport)->cport_mutex);
15175 		}
15176 	} else {
15177 		/*
15178 		 * No target node - need to create a new target node.
15179 		 */
15180 		if (qual == SATA_ADDR_DPMPORT) {
15181 			mutex_enter(&pmportinfo->pmport_mutex);
15182 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15183 			mutex_exit(&pmportinfo->pmport_mutex);
15184 		} else {
15185 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15186 			    cport_mutex);
15187 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15188 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15189 			    cport_mutex);
15190 		}
15191 
15192 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15193 		    sata_hba_inst, &sata_device->satadev_addr);
15194 		if (tdip == NULL) {
15195 			/* Configure operation failed */
15196 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15197 			    "sata_hba_ioctl: configure: "
15198 			    "configuring SATA device at port %d:%d "
15199 			    "failed", cport, pmport));
15200 			return (EIO);
15201 		}
15202 	}
15203 	return (0);
15204 }
15205 
15206 
15207 /*
15208  * Process ioctl deactivate port request.
15209  * Arbitrarily unconfigure attached device, if any.
15210  * Even if the unconfigure fails, proceed with the
15211  * port deactivation.
15212  *
15213  * NOTE: Port Multiplier is supported now.
15214  */
15215 
15216 static int
15217 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15218     sata_device_t *sata_device)
15219 {
15220 	int cport, pmport, qual;
15221 	int rval, rv = 0;
15222 	int npmport;
15223 	sata_cport_info_t *cportinfo;
15224 	sata_pmport_info_t *pmportinfo;
15225 	sata_pmult_info_t *pmultinfo;
15226 	dev_info_t *tdip;
15227 	sata_drive_info_t *sdinfo = NULL;
15228 	sata_device_t subsdevice;
15229 
15230 	/* Sanity check */
15231 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15232 		return (ENOTSUP);
15233 
15234 	cport = sata_device->satadev_addr.cport;
15235 	pmport = sata_device->satadev_addr.pmport;
15236 	qual = sata_device->satadev_addr.qual;
15237 
15238 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15239 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15240 	if (qual == SATA_ADDR_DCPORT)
15241 		qual = SATA_ADDR_CPORT;
15242 	else
15243 		qual = SATA_ADDR_PMPORT;
15244 
15245 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15246 	if (qual == SATA_ADDR_PMPORT)
15247 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15248 
15249 	/*
15250 	 * Processing port multiplier
15251 	 */
15252 	if (qual == SATA_ADDR_CPORT &&
15253 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15254 		mutex_enter(&cportinfo->cport_mutex);
15255 
15256 		/* Deactivate all sub-deices */
15257 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15258 		if (pmultinfo != NULL) {
15259 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15260 			    sata_hba_inst, cport); npmport++) {
15261 
15262 				subsdevice.satadev_addr.cport = cport;
15263 				subsdevice.satadev_addr.pmport =
15264 				    (uint8_t)npmport;
15265 				subsdevice.satadev_addr.qual =
15266 				    SATA_ADDR_DPMPORT;
15267 
15268 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15269 				    "sata_hba_ioctl: deactivate: trying to "
15270 				    "deactivate SATA port %d:%d",
15271 				    cport, npmport);
15272 
15273 				mutex_exit(&cportinfo->cport_mutex);
15274 				if (sata_ioctl_deactivate(sata_hba_inst,
15275 				    &subsdevice) == SATA_SUCCESS) {
15276 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15277 					    "[Deactivate] device at port %d:%d "
15278 					    "successfully.", cport, npmport);
15279 				}
15280 				mutex_enter(&cportinfo->cport_mutex);
15281 			}
15282 		}
15283 
15284 		/* Deactivate the port multiplier now. */
15285 		cportinfo->cport_state &= ~SATA_STATE_READY;
15286 		mutex_exit(&cportinfo->cport_mutex);
15287 
15288 		sata_device->satadev_addr.qual = qual;
15289 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15290 		    (SATA_DIP(sata_hba_inst), sata_device);
15291 
15292 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15293 		    SE_NO_HINT);
15294 
15295 		mutex_enter(&cportinfo->cport_mutex);
15296 		sata_update_port_info(sata_hba_inst, sata_device);
15297 		if (rval != SATA_SUCCESS) {
15298 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15299 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15300 			}
15301 			rv = EIO;
15302 		} else {
15303 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15304 		}
15305 		mutex_exit(&cportinfo->cport_mutex);
15306 
15307 		return (rv);
15308 	}
15309 
15310 	/*
15311 	 * Process non-port-multiplier device - it could be a drive connected
15312 	 * to a port multiplier port or a controller port.
15313 	 */
15314 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15315 	if (qual == SATA_ADDR_CPORT) {
15316 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15317 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15318 			/* deal only with valid devices */
15319 			if ((cportinfo->cport_dev_type &
15320 			    SATA_VALID_DEV_TYPE) != 0)
15321 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15322 		}
15323 		cportinfo->cport_state &= ~SATA_STATE_READY;
15324 	} else {
15325 		/* Port multiplier device port */
15326 		mutex_enter(&pmportinfo->pmport_mutex);
15327 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15328 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15329 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15330 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15331 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15332 		mutex_exit(&pmportinfo->pmport_mutex);
15333 	}
15334 
15335 	if (sdinfo != NULL) {
15336 		/*
15337 		 * If a target node exists, try to offline a device and
15338 		 * to remove a target node.
15339 		 */
15340 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15341 		    cport_mutex);
15342 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15343 		    &sata_device->satadev_addr);
15344 		if (tdip != NULL) {
15345 			/* target node exist */
15346 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15347 			    "sata_hba_ioctl: port deactivate: "
15348 			    "target node exists.", NULL);
15349 
15350 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15351 			    NDI_SUCCESS) {
15352 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15353 				    "sata_hba_ioctl: port deactivate: "
15354 				    "failed to unconfigure device at port "
15355 				    "%d:%d before deactivating the port",
15356 				    cport, pmport));
15357 				/*
15358 				 * Set DEVICE REMOVED state in the target
15359 				 * node. It will prevent an access to
15360 				 * the device even when a new device is
15361 				 * attached, until the old target node is
15362 				 * released, removed and recreated for a new
15363 				 * device.
15364 				 */
15365 				sata_set_device_removed(tdip);
15366 
15367 				/*
15368 				 * Instruct the event daemon to try the
15369 				 * target node cleanup later.
15370 				 */
15371 				sata_set_target_node_cleanup(sata_hba_inst,
15372 				    &sata_device->satadev_addr);
15373 			}
15374 		}
15375 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15376 		    cport_mutex);
15377 		/*
15378 		 * In any case, remove and release sata_drive_info
15379 		 * structure.
15380 		 */
15381 		if (qual == SATA_ADDR_CPORT) {
15382 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15383 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15384 		} else { /* port multiplier device port */
15385 			mutex_enter(&pmportinfo->pmport_mutex);
15386 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15387 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15388 			mutex_exit(&pmportinfo->pmport_mutex);
15389 		}
15390 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15391 	}
15392 
15393 	if (qual == SATA_ADDR_CPORT) {
15394 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15395 		    SATA_STATE_PROBING);
15396 	} else if (qual == SATA_ADDR_PMPORT) {
15397 		mutex_enter(&pmportinfo->pmport_mutex);
15398 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15399 		    SATA_STATE_PROBING);
15400 		mutex_exit(&pmportinfo->pmport_mutex);
15401 	}
15402 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15403 
15404 	/* Just let HBA driver to deactivate port */
15405 	sata_device->satadev_addr.qual = qual;
15406 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15407 	    (SATA_DIP(sata_hba_inst), sata_device);
15408 
15409 	/*
15410 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15411 	 * without the hint
15412 	 */
15413 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15414 	    SE_NO_HINT);
15415 
15416 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15417 	sata_update_port_info(sata_hba_inst, sata_device);
15418 	if (qual == SATA_ADDR_CPORT) {
15419 		if (rval != SATA_SUCCESS) {
15420 			/*
15421 			 * Port deactivation failure - do not change port state
15422 			 * unless the state returned by HBA indicates a port
15423 			 * failure.
15424 			 */
15425 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15426 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15427 				    SATA_PSTATE_FAILED;
15428 			}
15429 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15430 			    "sata_hba_ioctl: port deactivate: "
15431 			    "cannot deactivate SATA port %d", cport));
15432 			rv = EIO;
15433 		} else {
15434 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15435 		}
15436 	} else {
15437 		mutex_enter(&pmportinfo->pmport_mutex);
15438 		if (rval != SATA_SUCCESS) {
15439 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15440 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15441 				    pmport) = SATA_PSTATE_FAILED;
15442 			}
15443 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15444 			    "sata_hba_ioctl: port deactivate: "
15445 			    "cannot deactivate SATA port %d:%d",
15446 			    cport, pmport));
15447 			rv = EIO;
15448 		} else {
15449 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15450 		}
15451 		mutex_exit(&pmportinfo->pmport_mutex);
15452 	}
15453 
15454 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15455 
15456 	return (rv);
15457 }
15458 
15459 /*
15460  * Process ioctl port activate request.
15461  *
15462  * NOTE: Port multiplier is supported now.
15463  */
15464 static int
15465 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15466     sata_device_t *sata_device)
15467 {
15468 	int cport, pmport, qual;
15469 	sata_cport_info_t *cportinfo;
15470 	sata_pmport_info_t *pmportinfo = NULL;
15471 	boolean_t dev_existed = B_TRUE;
15472 
15473 	/* Sanity check */
15474 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15475 		return (ENOTSUP);
15476 
15477 	cport = sata_device->satadev_addr.cport;
15478 	pmport = sata_device->satadev_addr.pmport;
15479 	qual = sata_device->satadev_addr.qual;
15480 
15481 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15482 
15483 	/*
15484 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15485 	 * is a device. But what we are dealing with is port/pmport.
15486 	 */
15487 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15488 	if (qual == SATA_ADDR_DCPORT)
15489 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15490 	else
15491 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15492 
15493 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15494 	if (qual == SATA_ADDR_PMPORT) {
15495 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15496 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15497 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15498 			dev_existed = B_FALSE;
15499 	} else { /* cport */
15500 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15501 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15502 			dev_existed = B_FALSE;
15503 	}
15504 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15505 
15506 	/* Just let HBA driver to activate port, if necessary */
15507 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15508 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15509 		/*
15510 		 * Port activation failure - do not change port state unless
15511 		 * the state returned by HBA indicates a port failure.
15512 		 */
15513 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15514 		    cport)->cport_mutex);
15515 		sata_update_port_info(sata_hba_inst, sata_device);
15516 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15517 			if (qual == SATA_ADDR_PMPORT) {
15518 				mutex_enter(&pmportinfo->pmport_mutex);
15519 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15520 				mutex_exit(&pmportinfo->pmport_mutex);
15521 			} else
15522 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15523 
15524 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15525 			    cport)->cport_mutex);
15526 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15527 			    "sata_hba_ioctl: port activate: cannot activate "
15528 			    "SATA port %d:%d", cport, pmport));
15529 			return (EIO);
15530 		}
15531 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15532 	}
15533 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15534 	if (qual == SATA_ADDR_PMPORT) {
15535 		mutex_enter(&pmportinfo->pmport_mutex);
15536 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15537 		mutex_exit(&pmportinfo->pmport_mutex);
15538 	} else
15539 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15540 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15541 
15542 	/*
15543 	 * Re-probe port to find its current state and possibly attached device.
15544 	 * Port re-probing may change the cportinfo device type if device is
15545 	 * found attached.
15546 	 * If port probing failed, the device type would be set to
15547 	 * SATA_DTYPE_NONE.
15548 	 */
15549 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15550 	    SATA_DEV_IDENTIFY_RETRY);
15551 
15552 	/*
15553 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15554 	 * without the hint.
15555 	 */
15556 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15557 	    SE_NO_HINT);
15558 
15559 	if (dev_existed == B_FALSE) {
15560 		if (qual == SATA_ADDR_PMPORT &&
15561 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15562 			/*
15563 			 * That's the transition from the "inactive" port state
15564 			 * or the active port without a device attached to the
15565 			 * active port state with a device attached.
15566 			 */
15567 			sata_log(sata_hba_inst, CE_WARN,
15568 			    "SATA device detected at port %d:%d",
15569 			    cport, pmport);
15570 		} else if (qual == SATA_ADDR_CPORT &&
15571 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15572 			/*
15573 			 * That's the transition from the "inactive" port state
15574 			 * or the active port without a device attached to the
15575 			 * active port state with a device attached.
15576 			 */
15577 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15578 				sata_log(sata_hba_inst, CE_WARN,
15579 				    "SATA device detected at port %d", cport);
15580 			} else {
15581 				sata_log(sata_hba_inst, CE_WARN,
15582 				    "SATA port multiplier detected at port %d",
15583 				    cport);
15584 			}
15585 		}
15586 	}
15587 	return (0);
15588 }
15589 
15590 
15591 
15592 /*
15593  * Process ioctl reset port request.
15594  *
15595  * NOTE: Port-Multiplier is supported.
15596  */
15597 static int
15598 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15599     sata_device_t *sata_device)
15600 {
15601 	int cport, pmport, qual;
15602 	int rv = 0;
15603 
15604 	cport = sata_device->satadev_addr.cport;
15605 	pmport = sata_device->satadev_addr.pmport;
15606 	qual = sata_device->satadev_addr.qual;
15607 
15608 	/*
15609 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15610 	 * is a device. But what we are dealing with is port/pmport.
15611 	 */
15612 	if (qual == SATA_ADDR_DCPORT)
15613 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15614 	else
15615 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15616 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15617 
15618 	/* Sanity check */
15619 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15620 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15621 		    "sata_hba_ioctl: sata_hba_tran missing required "
15622 		    "function sata_tran_reset_dport"));
15623 		return (ENOTSUP);
15624 	}
15625 
15626 	/* Ask HBA to reset port */
15627 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15628 	    sata_device) != SATA_SUCCESS) {
15629 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15630 		    "sata_hba_ioctl: reset port: failed %d:%d",
15631 		    cport, pmport));
15632 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15633 		    cport_mutex);
15634 		sata_update_port_info(sata_hba_inst, sata_device);
15635 		if (qual == SATA_ADDR_CPORT)
15636 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15637 			    SATA_PSTATE_FAILED;
15638 		else {
15639 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15640 			    pmport));
15641 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15642 			    SATA_PSTATE_FAILED;
15643 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15644 			    pmport));
15645 		}
15646 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15647 		    cport_mutex);
15648 		rv = EIO;
15649 	}
15650 
15651 	return (rv);
15652 }
15653 
15654 /*
15655  * Process ioctl reset device request.
15656  *
15657  * NOTE: Port multiplier is supported.
15658  */
15659 static int
15660 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15661     sata_device_t *sata_device)
15662 {
15663 	sata_drive_info_t *sdinfo = NULL;
15664 	sata_pmult_info_t *pmultinfo = NULL;
15665 	int cport, pmport;
15666 	int rv = 0;
15667 
15668 	/* Sanity check */
15669 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15670 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15671 		    "sata_hba_ioctl: sata_hba_tran missing required "
15672 		    "function sata_tran_reset_dport"));
15673 		return (ENOTSUP);
15674 	}
15675 
15676 	cport = sata_device->satadev_addr.cport;
15677 	pmport = sata_device->satadev_addr.pmport;
15678 
15679 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15680 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15681 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15682 		    SATA_DTYPE_PMULT)
15683 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15684 			    cport_devp.cport_sata_pmult;
15685 		else
15686 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15687 			    sata_device->satadev_addr.cport);
15688 	} else { /* port multiplier */
15689 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15690 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15691 		    sata_device->satadev_addr.cport,
15692 		    sata_device->satadev_addr.pmport);
15693 	}
15694 	if (sdinfo == NULL && pmultinfo == NULL) {
15695 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15696 		return (EINVAL);
15697 	}
15698 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15699 
15700 	/* Ask HBA to reset device */
15701 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15702 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15703 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15704 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15705 		    cport, pmport));
15706 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15707 		    cport_mutex);
15708 		sata_update_port_info(sata_hba_inst, sata_device);
15709 		/*
15710 		 * Device info structure remains attached. Another device reset
15711 		 * or port disconnect/connect and re-probing is
15712 		 * needed to change it's state
15713 		 */
15714 		if (sdinfo != NULL) {
15715 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15716 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15717 		} else if (pmultinfo != NULL) {
15718 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15719 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15720 		}
15721 
15722 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15723 		rv = EIO;
15724 	}
15725 	/*
15726 	 * If attached device was a port multiplier, some extra processing
15727 	 * may be needed to bring it back. SATA specification requies a
15728 	 * mandatory software reset on host port to reliably enumerate a port
15729 	 * multiplier, the HBA driver should handle that after reset
15730 	 * operation.
15731 	 */
15732 	return (rv);
15733 }
15734 
15735 
15736 /*
15737  * Process ioctl reset all request.
15738  */
15739 static int
15740 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15741 {
15742 	sata_device_t sata_device;
15743 	int rv = 0;
15744 	int tcport;
15745 
15746 	sata_device.satadev_rev = SATA_DEVICE_REV;
15747 
15748 	/*
15749 	 * There is no protection here for configured devices.
15750 	 */
15751 	/* Sanity check */
15752 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15753 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15754 		    "sata_hba_ioctl: sata_hba_tran missing required "
15755 		    "function sata_tran_reset_dport"));
15756 		return (ENOTSUP);
15757 	}
15758 
15759 	/*
15760 	 * Need to lock all ports, not just one.
15761 	 * If any port is locked by event processing, fail the whole operation.
15762 	 * One port is already locked, but for simplicity lock it again.
15763 	 */
15764 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15765 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15766 		    cport_mutex);
15767 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15768 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15769 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15770 			    cport_mutex);
15771 			rv = EBUSY;
15772 			break;
15773 		} else {
15774 			/*
15775 			 * It is enough to lock cport in command-based
15776 			 * switching mode.
15777 			 */
15778 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15779 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15780 		}
15781 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15782 		    cport_mutex);
15783 	}
15784 
15785 	if (rv == 0) {
15786 		/*
15787 		 * All cports were successfully locked.
15788 		 * Reset main SATA controller.
15789 		 * Set the device address to port 0, to have a valid device
15790 		 * address.
15791 		 */
15792 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15793 		sata_device.satadev_addr.cport = 0;
15794 		sata_device.satadev_addr.pmport = 0;
15795 
15796 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15797 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15798 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15799 			    "sata_hba_ioctl: reset controller failed"));
15800 			return (EIO);
15801 		}
15802 	}
15803 	/*
15804 	 * Unlock all ports
15805 	 */
15806 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15807 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15808 		    cport_mutex);
15809 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15810 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15811 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15812 		    cport_mutex);
15813 	}
15814 
15815 	/*
15816 	 * This operation returns EFAULT if either reset
15817 	 * controller failed or a re-probing of any port failed.
15818 	 */
15819 	return (rv);
15820 }
15821 
15822 
15823 /*
15824  * Process ioctl port self test request.
15825  *
15826  * NOTE: Port multiplier code is not completed nor tested.
15827  */
15828 static int
15829 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15830     sata_device_t *sata_device)
15831 {
15832 	int cport, pmport, qual;
15833 	int rv = 0;
15834 
15835 	/* Sanity check */
15836 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15837 		return (ENOTSUP);
15838 
15839 	cport = sata_device->satadev_addr.cport;
15840 	pmport = sata_device->satadev_addr.pmport;
15841 	qual = sata_device->satadev_addr.qual;
15842 
15843 	/*
15844 	 * There is no protection here for a configured
15845 	 * device attached to this port.
15846 	 */
15847 
15848 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15849 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15850 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15851 		    "sata_hba_ioctl: port selftest: "
15852 		    "failed port %d:%d", cport, pmport));
15853 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15854 		    cport_mutex);
15855 		sata_update_port_info(sata_hba_inst, sata_device);
15856 		if (qual == SATA_ADDR_CPORT)
15857 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15858 			    SATA_PSTATE_FAILED;
15859 		else { /* port multiplier device port */
15860 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15861 			    cport, pmport));
15862 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15863 			    SATA_PSTATE_FAILED;
15864 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15865 			    cport, pmport));
15866 		}
15867 
15868 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15869 		    cport_mutex);
15870 		return (EIO);
15871 	}
15872 	/*
15873 	 * Beacuse the port was reset in the course of testing, it should be
15874 	 * re-probed and attached device state should be restored. At this
15875 	 * point the port state is unknown - it's state is HBA-specific.
15876 	 * Force port re-probing to get it into a known state.
15877 	 */
15878 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15879 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15880 		rv = EIO;
15881 	return (rv);
15882 }
15883 
15884 
15885 /*
15886  * sata_cfgadm_state:
15887  * Use the sata port state and state of the target node to figure out
15888  * the cfgadm_state.
15889  *
15890  * The port argument is a value with encoded cport,
15891  * pmport and address qualifier, in the same manner as a scsi target number.
15892  * SCSI_TO_SATA_CPORT macro extracts cport number,
15893  * SCSI_TO_SATA_PMPORT extracts pmport number and
15894  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15895  *
15896  * Port multiplier is supported.
15897  */
15898 
15899 static void
15900 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15901     devctl_ap_state_t *ap_state)
15902 {
15903 	uint8_t		cport, pmport, qual;
15904 	uint32_t	port_state, pmult_state;
15905 	uint32_t	dev_type;
15906 	sata_drive_info_t *sdinfo;
15907 
15908 	cport = SCSI_TO_SATA_CPORT(port);
15909 	pmport = SCSI_TO_SATA_PMPORT(port);
15910 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15911 
15912 	/* Check cport state */
15913 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15914 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15915 	    port_state & SATA_PSTATE_FAILED) {
15916 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15917 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15918 		if (port_state & SATA_PSTATE_FAILED)
15919 			ap_state->ap_condition = AP_COND_FAILED;
15920 		else
15921 			ap_state->ap_condition = AP_COND_UNKNOWN;
15922 
15923 		return;
15924 	}
15925 
15926 	/* cport state is okay. Now check pmport state */
15927 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15928 		/* Sanity check */
15929 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15930 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15931 		    cport, pmport) == NULL)
15932 			return;
15933 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15934 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15935 		    port_state & SATA_PSTATE_FAILED) {
15936 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15937 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15938 			if (port_state & SATA_PSTATE_FAILED)
15939 				ap_state->ap_condition = AP_COND_FAILED;
15940 			else
15941 				ap_state->ap_condition = AP_COND_UNKNOWN;
15942 
15943 			return;
15944 		}
15945 	}
15946 
15947 	/* Port is enabled and ready */
15948 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15949 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15950 	else
15951 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15952 
15953 	switch (dev_type) {
15954 	case SATA_DTYPE_NONE:
15955 	{
15956 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15957 		ap_state->ap_condition = AP_COND_OK;
15958 		/* No device attached */
15959 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15960 		break;
15961 	}
15962 	case SATA_DTYPE_PMULT:
15963 	{
15964 		/* Need to check port multiplier state */
15965 		ASSERT(qual == SATA_ADDR_DCPORT);
15966 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15967 		    pmult_state;
15968 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15969 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15970 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15971 			if (pmult_state & SATA_PSTATE_FAILED)
15972 				ap_state->ap_condition = AP_COND_FAILED;
15973 			else
15974 				ap_state->ap_condition = AP_COND_UNKNOWN;
15975 
15976 			return;
15977 		}
15978 
15979 		/* Port multiplier is not configurable */
15980 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15981 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15982 		ap_state->ap_condition = AP_COND_OK;
15983 		break;
15984 	}
15985 
15986 	case SATA_DTYPE_ATADISK:
15987 	case SATA_DTYPE_ATAPICD:
15988 	case SATA_DTYPE_ATAPITAPE:
15989 	case SATA_DTYPE_ATAPIDISK:
15990 	{
15991 		dev_info_t *tdip = NULL;
15992 		dev_info_t *dip = NULL;
15993 		int circ;
15994 
15995 		dip = SATA_DIP(sata_hba_inst);
15996 		tdip = sata_get_target_dip(dip, cport, pmport);
15997 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15998 		if (tdip != NULL) {
15999 			ndi_devi_enter(dip, &circ);
16000 			mutex_enter(&(DEVI(tdip)->devi_lock));
16001 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16002 				/*
16003 				 * There could be the case where previously
16004 				 * configured and opened device was removed
16005 				 * and unknown device was plugged.
16006 				 * In such case we want to show a device, and
16007 				 * its configured or unconfigured state but
16008 				 * indicate unusable condition untill the
16009 				 * old target node is released and removed.
16010 				 */
16011 				ap_state->ap_condition = AP_COND_UNUSABLE;
16012 			} else {
16013 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16014 				    cport));
16015 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16016 				    cport);
16017 				if (sdinfo != NULL) {
16018 					if ((sdinfo->satadrv_state &
16019 					    SATA_DSTATE_FAILED) != 0)
16020 						ap_state->ap_condition =
16021 						    AP_COND_FAILED;
16022 					else
16023 						ap_state->ap_condition =
16024 						    AP_COND_OK;
16025 				} else {
16026 					ap_state->ap_condition =
16027 					    AP_COND_UNKNOWN;
16028 				}
16029 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16030 				    cport));
16031 			}
16032 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16033 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
16034 				ap_state->ap_ostate =
16035 				    AP_OSTATE_UNCONFIGURED;
16036 			} else {
16037 				ap_state->ap_ostate =
16038 				    AP_OSTATE_CONFIGURED;
16039 			}
16040 			mutex_exit(&(DEVI(tdip)->devi_lock));
16041 			ndi_devi_exit(dip, circ);
16042 		} else {
16043 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16044 			ap_state->ap_condition = AP_COND_UNKNOWN;
16045 		}
16046 		break;
16047 	}
16048 	case SATA_DTYPE_ATAPIPROC:
16049 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16050 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16051 		ap_state->ap_condition = AP_COND_OK;
16052 		break;
16053 	default:
16054 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16055 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16056 		ap_state->ap_condition = AP_COND_UNKNOWN;
16057 		/*
16058 		 * This is actually internal error condition (non fatal),
16059 		 * because we have already checked all defined device types.
16060 		 */
16061 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16062 		    "sata_cfgadm_state: Internal error: "
16063 		    "unknown device type"));
16064 		break;
16065 	}
16066 }
16067 
16068 
16069 /*
16070  * Process ioctl get device path request.
16071  *
16072  * NOTE: Port multiplier has no target dip. Devices connected to port
16073  * multiplier have target node attached to the HBA node. The only difference
16074  * between them and the directly-attached device node is a target address.
16075  */
16076 static int
16077 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16078     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16079 {
16080 	char path[MAXPATHLEN];
16081 	uint32_t size;
16082 	dev_info_t *tdip;
16083 
16084 	(void) strcpy(path, "/devices");
16085 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16086 	    &sata_device->satadev_addr)) == NULL) {
16087 		/*
16088 		 * No such device. If this is a request for a size, do not
16089 		 * return EINVAL for non-existing target, because cfgadm
16090 		 * will then indicate a meaningless ioctl failure.
16091 		 * If this is a request for a path, indicate invalid
16092 		 * argument.
16093 		 */
16094 		if (ioc->get_size == 0)
16095 			return (EINVAL);
16096 	} else {
16097 		(void) ddi_pathname(tdip, path + strlen(path));
16098 	}
16099 	size = strlen(path) + 1;
16100 
16101 	if (ioc->get_size != 0) {
16102 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16103 		    mode) != 0)
16104 			return (EFAULT);
16105 	} else {
16106 		if (ioc->bufsiz != size)
16107 			return (EINVAL);
16108 
16109 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16110 		    mode) != 0)
16111 			return (EFAULT);
16112 	}
16113 	return (0);
16114 }
16115 
16116 /*
16117  * Process ioctl get attachment point type request.
16118  *
16119  * NOTE: Port multiplier is supported.
16120  */
16121 static	int
16122 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16123     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16124 {
16125 	uint32_t	type_len;
16126 	const char	*ap_type;
16127 	int		dev_type;
16128 
16129 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16130 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16131 		    sata_device->satadev_addr.cport);
16132 	else /* pmport */
16133 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16134 		    sata_device->satadev_addr.cport,
16135 		    sata_device->satadev_addr.pmport);
16136 
16137 	switch (dev_type) {
16138 	case SATA_DTYPE_NONE:
16139 		ap_type = "port";
16140 		break;
16141 
16142 	case SATA_DTYPE_ATADISK:
16143 	case SATA_DTYPE_ATAPIDISK:
16144 		ap_type = "disk";
16145 		break;
16146 
16147 	case SATA_DTYPE_ATAPICD:
16148 		ap_type = "cd/dvd";
16149 		break;
16150 
16151 	case SATA_DTYPE_ATAPITAPE:
16152 		ap_type = "tape";
16153 		break;
16154 
16155 	case SATA_DTYPE_ATAPIPROC:
16156 		ap_type = "processor";
16157 		break;
16158 
16159 	case SATA_DTYPE_PMULT:
16160 		ap_type = "sata-pmult";
16161 		break;
16162 
16163 	case SATA_DTYPE_UNKNOWN:
16164 		ap_type = "unknown";
16165 		break;
16166 
16167 	default:
16168 		ap_type = "unsupported";
16169 		break;
16170 
16171 	} /* end of dev_type switch */
16172 
16173 	type_len = strlen(ap_type) + 1;
16174 
16175 	if (ioc->get_size) {
16176 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16177 		    mode) != 0)
16178 			return (EFAULT);
16179 	} else {
16180 		if (ioc->bufsiz != type_len)
16181 			return (EINVAL);
16182 
16183 		if (ddi_copyout((void *)ap_type, ioc->buf,
16184 		    ioc->bufsiz, mode) != 0)
16185 			return (EFAULT);
16186 	}
16187 	return (0);
16188 
16189 }
16190 
16191 /*
16192  * Process ioctl get device model info request.
16193  * This operation should return to cfgadm the device model
16194  * information string
16195  *
16196  * NOTE: Port multiplier is supported.
16197  */
16198 static	int
16199 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16200     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16201 {
16202 	sata_drive_info_t *sdinfo;
16203 	uint32_t info_len;
16204 	char ap_info[SATA_ID_MODEL_LEN + 1];
16205 
16206 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16207 	    sata_device->satadev_addr.cport)->cport_mutex);
16208 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16209 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16210 		    sata_device->satadev_addr.cport);
16211 	else /* port multiplier */
16212 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16213 		    sata_device->satadev_addr.cport,
16214 		    sata_device->satadev_addr.pmport);
16215 	if (sdinfo == NULL) {
16216 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16217 		    sata_device->satadev_addr.cport)->cport_mutex);
16218 		return (EINVAL);
16219 	}
16220 
16221 #ifdef	_LITTLE_ENDIAN
16222 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16223 #else	/* _LITTLE_ENDIAN */
16224 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16225 #endif	/* _LITTLE_ENDIAN */
16226 
16227 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16228 	    sata_device->satadev_addr.cport)->cport_mutex);
16229 
16230 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16231 
16232 	info_len = strlen(ap_info) + 1;
16233 
16234 	if (ioc->get_size) {
16235 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16236 		    mode) != 0)
16237 			return (EFAULT);
16238 	} else {
16239 		if (ioc->bufsiz < info_len)
16240 			return (EINVAL);
16241 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16242 		    mode) != 0)
16243 			return (EFAULT);
16244 	}
16245 	return (0);
16246 }
16247 
16248 
16249 /*
16250  * Process ioctl get device firmware revision info request.
16251  * This operation should return to cfgadm the device firmware revision
16252  * information string
16253  *
16254  * Port multiplier is supported.
16255  */
16256 static	int
16257 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16258     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16259 {
16260 	sata_drive_info_t *sdinfo;
16261 	uint32_t info_len;
16262 	char ap_info[SATA_ID_FW_LEN + 1];
16263 
16264 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16265 	    sata_device->satadev_addr.cport)->cport_mutex);
16266 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16267 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16268 		    sata_device->satadev_addr.cport);
16269 	else /* port multiplier */
16270 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16271 		    sata_device->satadev_addr.cport,
16272 		    sata_device->satadev_addr.pmport);
16273 	if (sdinfo == NULL) {
16274 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16275 		    sata_device->satadev_addr.cport)->cport_mutex);
16276 		return (EINVAL);
16277 	}
16278 
16279 #ifdef	_LITTLE_ENDIAN
16280 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16281 #else	/* _LITTLE_ENDIAN */
16282 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16283 #endif	/* _LITTLE_ENDIAN */
16284 
16285 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16286 	    sata_device->satadev_addr.cport)->cport_mutex);
16287 
16288 	ap_info[SATA_ID_FW_LEN] = '\0';
16289 
16290 	info_len = strlen(ap_info) + 1;
16291 
16292 	if (ioc->get_size) {
16293 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16294 		    mode) != 0)
16295 			return (EFAULT);
16296 	} else {
16297 		if (ioc->bufsiz < info_len)
16298 			return (EINVAL);
16299 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16300 		    mode) != 0)
16301 			return (EFAULT);
16302 	}
16303 	return (0);
16304 }
16305 
16306 
16307 /*
16308  * Process ioctl get device serial number info request.
16309  * This operation should return to cfgadm the device serial number string.
16310  *
16311  * NOTE: Port multiplier is supported.
16312  */
16313 static	int
16314 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16315     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16316 {
16317 	sata_drive_info_t *sdinfo;
16318 	uint32_t info_len;
16319 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16320 
16321 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16322 	    sata_device->satadev_addr.cport)->cport_mutex);
16323 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16324 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16325 		    sata_device->satadev_addr.cport);
16326 	else /* port multiplier */
16327 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16328 		    sata_device->satadev_addr.cport,
16329 		    sata_device->satadev_addr.pmport);
16330 	if (sdinfo == NULL) {
16331 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16332 		    sata_device->satadev_addr.cport)->cport_mutex);
16333 		return (EINVAL);
16334 	}
16335 
16336 #ifdef	_LITTLE_ENDIAN
16337 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16338 #else	/* _LITTLE_ENDIAN */
16339 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16340 #endif	/* _LITTLE_ENDIAN */
16341 
16342 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16343 	    sata_device->satadev_addr.cport)->cport_mutex);
16344 
16345 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16346 
16347 	info_len = strlen(ap_info) + 1;
16348 
16349 	if (ioc->get_size) {
16350 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16351 		    mode) != 0)
16352 			return (EFAULT);
16353 	} else {
16354 		if (ioc->bufsiz < info_len)
16355 			return (EINVAL);
16356 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16357 		    mode) != 0)
16358 			return (EFAULT);
16359 	}
16360 	return (0);
16361 }
16362 
16363 
16364 /*
16365  * Preset scsi extended sense data (to NO SENSE)
16366  * First 18 bytes of the sense data are preset to current valid sense
16367  * with a key NO SENSE data.
16368  *
16369  * Returns void
16370  */
16371 static void
16372 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16373 {
16374 	sense->es_valid = 1;		/* Valid sense */
16375 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16376 	sense->es_key = KEY_NO_SENSE;
16377 	sense->es_info_1 = 0;
16378 	sense->es_info_2 = 0;
16379 	sense->es_info_3 = 0;
16380 	sense->es_info_4 = 0;
16381 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16382 	sense->es_cmd_info[0] = 0;
16383 	sense->es_cmd_info[1] = 0;
16384 	sense->es_cmd_info[2] = 0;
16385 	sense->es_cmd_info[3] = 0;
16386 	sense->es_add_code = 0;
16387 	sense->es_qual_code = 0;
16388 }
16389 
16390 /*
16391  * Register a legacy cmdk-style devid for the target (disk) device.
16392  *
16393  * Note: This function is called only when the HBA devinfo node has the
16394  * property "use-cmdk-devid-format" set. This property indicates that
16395  * devid compatible with old cmdk (target) driver is to be generated
16396  * for any target device attached to this controller. This will take
16397  * precedence over the devid generated by sd (target) driver.
16398  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16399  */
16400 static void
16401 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16402 {
16403 	char	*hwid;
16404 	int	modlen;
16405 	int	serlen;
16406 	int	rval;
16407 	ddi_devid_t	devid;
16408 
16409 	/*
16410 	 * device ID is a concatanation of model number, "=", serial number.
16411 	 */
16412 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16413 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16414 	    sizeof (sdinfo->satadrv_id.ai_model));
16415 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16416 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16417 	if (modlen == 0)
16418 		goto err;
16419 	hwid[modlen++] = '=';
16420 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16421 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16422 	swab(&hwid[modlen], &hwid[modlen],
16423 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16424 	serlen = sata_check_modser(&hwid[modlen],
16425 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16426 	if (serlen == 0)
16427 		goto err;
16428 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16429 
16430 	/* initialize/register devid */
16431 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16432 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16433 		rval = ddi_devid_register(dip, devid);
16434 		/*
16435 		 * Free up the allocated devid buffer.
16436 		 * NOTE: This doesn't mean unregistering devid.
16437 		 */
16438 		ddi_devid_free(devid);
16439 	}
16440 
16441 	if (rval != DDI_SUCCESS)
16442 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16443 		    " on port %d", sdinfo->satadrv_addr.cport);
16444 err:
16445 	kmem_free(hwid, LEGACY_HWID_LEN);
16446 }
16447 
16448 /*
16449  * valid model/serial string must contain a non-zero non-space characters.
16450  * trim trailing spaces/NULLs.
16451  */
16452 static int
16453 sata_check_modser(char *buf, int buf_len)
16454 {
16455 	boolean_t ret;
16456 	char *s;
16457 	int i;
16458 	int tb;
16459 	char ch;
16460 
16461 	ret = B_FALSE;
16462 	s = buf;
16463 	for (i = 0; i < buf_len; i++) {
16464 		ch = *s++;
16465 		if (ch != ' ' && ch != '\0')
16466 			tb = i + 1;
16467 		if (ch != ' ' && ch != '\0' && ch != '0')
16468 			ret = B_TRUE;
16469 	}
16470 
16471 	if (ret == B_FALSE)
16472 		return (0); /* invalid string */
16473 
16474 	return (tb); /* return length */
16475 }
16476 
16477 /*
16478  * sata_set_drive_features function compares current device features setting
16479  * with the saved device features settings and, if there is a difference,
16480  * it restores device features setting to the previously saved state.
16481  * It also arbitrarily tries to select the highest supported DMA mode.
16482  * Device Identify or Identify Packet Device data has to be current.
16483  * At the moment read ahead and write cache are considered for all devices.
16484  * For atapi devices, Removable Media Status Notification is set in addition
16485  * to common features.
16486  *
16487  * This function cannot be called in the interrupt context (it may sleep).
16488  *
16489  * The input argument sdinfo should point to the drive info structure
16490  * to be updated after features are set. Note, that only
16491  * device (packet) identify data is updated, not the flags indicating the
16492  * supported features.
16493  *
16494  * Returns SATA_SUCCESS if successful or there was nothing to do.
16495  * Device Identify data in the drive info structure pointed to by the sdinfo
16496  * arguments is updated even when no features were set or changed.
16497  *
16498  * Returns SATA_FAILURE if device features could not be set or DMA mode
16499  * for a disk cannot be set and device identify data cannot be fetched.
16500  *
16501  * Returns SATA_RETRY if device features could not be set (other than disk
16502  * DMA mode) but the device identify data was fetched successfully.
16503  *
16504  * Note: This function may fail the port, making it inaccessible.
16505  * In such case the explicit port disconnect/connect or physical device
16506  * detach/attach is required to re-evaluate port state again.
16507  */
16508 
16509 static int
16510 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16511     sata_drive_info_t *sdinfo, int restore)
16512 {
16513 	int rval = SATA_SUCCESS;
16514 	int rval_set;
16515 	sata_drive_info_t new_sdinfo;
16516 	char *finfo = "sata_set_drive_features: cannot";
16517 	char *finfox;
16518 	int cache_op;
16519 
16520 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16521 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16522 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16523 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16524 		/*
16525 		 * Cannot get device identification - caller may retry later
16526 		 */
16527 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16528 		    "%s fetch device identify data\n", finfo);
16529 		return (SATA_FAILURE);
16530 	}
16531 	finfox = (restore != 0) ? " restore device features" :
16532 	    " initialize device features\n";
16533 
16534 	switch (sdinfo->satadrv_type) {
16535 	case SATA_DTYPE_ATADISK:
16536 		/* Arbitrarily set UDMA mode */
16537 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16538 		    SATA_SUCCESS) {
16539 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16540 			    "%s set UDMA mode\n", finfo));
16541 			return (SATA_FAILURE);
16542 		}
16543 		break;
16544 	case SATA_DTYPE_ATAPICD:
16545 	case SATA_DTYPE_ATAPITAPE:
16546 	case SATA_DTYPE_ATAPIDISK:
16547 		/*  Set Removable Media Status Notification, if necessary */
16548 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16549 		    restore != 0) {
16550 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16551 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16552 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16553 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16554 				/* Current setting does not match saved one */
16555 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16556 				    sdinfo->satadrv_settings &
16557 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16558 					rval = SATA_FAILURE;
16559 			}
16560 		}
16561 		/*
16562 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16563 		 * we want to use DMA transfer mode whenever possible.
16564 		 * Some devices require explicit setting of the DMA mode.
16565 		 */
16566 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16567 			/* Set highest supported DMA mode */
16568 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16569 			    SATA_SUCCESS) {
16570 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16571 				    "%s set UDMA mode\n", finfo));
16572 				rval = SATA_FAILURE;
16573 			}
16574 		}
16575 		break;
16576 	}
16577 
16578 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16579 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16580 		/*
16581 		 * neither READ AHEAD nor WRITE CACHE is supported
16582 		 * - do nothing
16583 		 */
16584 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16585 		    "settable features not supported\n", NULL);
16586 		goto update_sdinfo;
16587 	}
16588 
16589 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16590 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16591 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16592 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16593 		/*
16594 		 * both READ AHEAD and WRITE CACHE are enabled
16595 		 * - Nothing to do
16596 		 */
16597 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16598 		    "no device features to set\n", NULL);
16599 		goto update_sdinfo;
16600 	}
16601 
16602 	cache_op = 0;
16603 
16604 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16605 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16606 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16607 			/* Enable read ahead / read cache */
16608 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16609 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16610 			    "enabling read cache\n", NULL);
16611 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16612 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16613 			/* Disable read ahead  / read cache */
16614 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16615 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16616 			    "disabling read cache\n", NULL);
16617 		}
16618 
16619 		if (cache_op != 0) {
16620 			/* Try to set read cache mode */
16621 			rval_set = sata_set_cache_mode(sata_hba_inst,
16622 			    &new_sdinfo, cache_op);
16623 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16624 				rval = rval_set;
16625 		}
16626 	}
16627 
16628 	cache_op = 0;
16629 
16630 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16631 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16632 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16633 			/* Enable write cache */
16634 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16635 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16636 			    "enabling write cache\n", NULL);
16637 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16638 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16639 			/* Disable write cache */
16640 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16641 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16642 			    "disabling write cache\n", NULL);
16643 		}
16644 
16645 		if (cache_op != 0) {
16646 			/* Try to set write cache mode */
16647 			rval_set = sata_set_cache_mode(sata_hba_inst,
16648 			    &new_sdinfo, cache_op);
16649 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16650 				rval = rval_set;
16651 		}
16652 	}
16653 	if (rval != SATA_SUCCESS)
16654 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16655 		    "%s %s", finfo, finfox));
16656 
16657 update_sdinfo:
16658 	/*
16659 	 * We need to fetch Device Identify data again
16660 	 */
16661 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16662 		/*
16663 		 * Cannot get device identification - retry later
16664 		 */
16665 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16666 		    "%s re-fetch device identify data\n", finfo));
16667 		rval = SATA_FAILURE;
16668 	}
16669 	/* Copy device sata info. */
16670 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16671 
16672 	return (rval);
16673 }
16674 
16675 
16676 /*
16677  *
16678  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16679  * unable to determine.
16680  *
16681  * Cannot be called in an interrupt context.
16682  *
16683  * Called by sata_build_lsense_page_2f()
16684  */
16685 
16686 static int
16687 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16688     sata_drive_info_t *sdinfo)
16689 {
16690 	sata_pkt_t *spkt;
16691 	sata_cmd_t *scmd;
16692 	sata_pkt_txlate_t *spx;
16693 	int rval;
16694 
16695 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16696 	spx->txlt_sata_hba_inst = sata_hba_inst;
16697 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16698 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16699 	if (spkt == NULL) {
16700 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16701 		return (-1);
16702 	}
16703 	/* address is needed now */
16704 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16705 
16706 
16707 	/* Fill sata_pkt */
16708 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16709 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16710 	/* Synchronous mode, no callback */
16711 	spkt->satapkt_comp = NULL;
16712 	/* Timeout 30s */
16713 	spkt->satapkt_time = sata_default_pkt_time;
16714 
16715 	scmd = &spkt->satapkt_cmd;
16716 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16717 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16718 
16719 	/* Set up which registers need to be returned */
16720 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16721 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16722 
16723 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16724 	scmd->satacmd_addr_type = 0;		/* N/A */
16725 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16726 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16727 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16728 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16729 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16730 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16731 	scmd->satacmd_cmd_reg = SATAC_SMART;
16732 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16733 	    sdinfo->satadrv_addr.cport)));
16734 
16735 
16736 	/* Send pkt to SATA HBA driver */
16737 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16738 	    SATA_TRAN_ACCEPTED ||
16739 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16740 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16741 		    sdinfo->satadrv_addr.cport)));
16742 		/*
16743 		 * Whoops, no SMART RETURN STATUS
16744 		 */
16745 		rval = -1;
16746 	} else {
16747 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16748 		    sdinfo->satadrv_addr.cport)));
16749 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16750 			rval = -1;
16751 			goto fail;
16752 		}
16753 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16754 			rval = -1;
16755 			goto fail;
16756 		}
16757 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16758 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16759 			rval = 0;
16760 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16761 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16762 			rval = 1;
16763 		else {
16764 			rval = -1;
16765 			goto fail;
16766 		}
16767 	}
16768 fail:
16769 	/* Free allocated resources */
16770 	sata_pkt_free(spx);
16771 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16772 
16773 	return (rval);
16774 }
16775 
16776 /*
16777  *
16778  * Returns 0 if succeeded, -1 otherwise
16779  *
16780  * Cannot be called in an interrupt context.
16781  *
16782  */
16783 static int
16784 sata_fetch_smart_data(
16785 	sata_hba_inst_t *sata_hba_inst,
16786 	sata_drive_info_t *sdinfo,
16787 	struct smart_data *smart_data)
16788 {
16789 	sata_pkt_t *spkt;
16790 	sata_cmd_t *scmd;
16791 	sata_pkt_txlate_t *spx;
16792 	int rval;
16793 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16794 
16795 #if ! defined(lint)
16796 	ASSERT(sizeof (struct smart_data) == 512);
16797 #endif
16798 
16799 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16800 	spx->txlt_sata_hba_inst = sata_hba_inst;
16801 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16802 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16803 	if (spkt == NULL) {
16804 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16805 		return (-1);
16806 	}
16807 	/* address is needed now */
16808 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16809 
16810 
16811 	/* Fill sata_pkt */
16812 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16813 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16814 	/* Synchronous mode, no callback */
16815 	spkt->satapkt_comp = NULL;
16816 	/* Timeout 30s */
16817 	spkt->satapkt_time = sata_default_pkt_time;
16818 
16819 	scmd = &spkt->satapkt_cmd;
16820 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16821 
16822 	/*
16823 	 * Allocate buffer for SMART data
16824 	 */
16825 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16826 	    sizeof (struct smart_data));
16827 	if (scmd->satacmd_bp == NULL) {
16828 		sata_pkt_free(spx);
16829 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16830 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16831 		    "sata_fetch_smart_data: "
16832 		    "cannot allocate buffer"));
16833 		return (-1);
16834 	}
16835 
16836 
16837 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16838 	scmd->satacmd_addr_type = 0;		/* N/A */
16839 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16840 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16841 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16842 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16843 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16844 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16845 	scmd->satacmd_cmd_reg = SATAC_SMART;
16846 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16847 	    sdinfo->satadrv_addr.cport)));
16848 
16849 	/* Send pkt to SATA HBA driver */
16850 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16851 	    SATA_TRAN_ACCEPTED ||
16852 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16853 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16854 		    sdinfo->satadrv_addr.cport)));
16855 		/*
16856 		 * Whoops, no SMART DATA available
16857 		 */
16858 		rval = -1;
16859 		goto fail;
16860 	} else {
16861 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16862 		    sdinfo->satadrv_addr.cport)));
16863 		if (spx->txlt_buf_dma_handle != NULL) {
16864 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16865 			    DDI_DMA_SYNC_FORKERNEL);
16866 			ASSERT(rval == DDI_SUCCESS);
16867 			if (sata_check_for_dma_error(dip, spx)) {
16868 				ddi_fm_service_impact(dip,
16869 				    DDI_SERVICE_UNAFFECTED);
16870 				rval = -1;
16871 				goto fail;
16872 			}
16873 		}
16874 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16875 		    sizeof (struct smart_data));
16876 	}
16877 
16878 fail:
16879 	/* Free allocated resources */
16880 	sata_free_local_buffer(spx);
16881 	sata_pkt_free(spx);
16882 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16883 
16884 	return (rval);
16885 }
16886 
16887 /*
16888  * Used by LOG SENSE page 0x10
16889  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16890  * Note: cannot be called in the interrupt context.
16891  *
16892  * return 0 for success, -1 otherwise
16893  *
16894  */
16895 static int
16896 sata_ext_smart_selftest_read_log(
16897 	sata_hba_inst_t *sata_hba_inst,
16898 	sata_drive_info_t *sdinfo,
16899 	struct smart_ext_selftest_log *ext_selftest_log,
16900 	uint16_t block_num)
16901 {
16902 	sata_pkt_txlate_t *spx;
16903 	sata_pkt_t *spkt;
16904 	sata_cmd_t *scmd;
16905 	int rval;
16906 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16907 
16908 #if ! defined(lint)
16909 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16910 #endif
16911 
16912 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16913 	spx->txlt_sata_hba_inst = sata_hba_inst;
16914 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16915 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16916 	if (spkt == NULL) {
16917 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16918 		return (-1);
16919 	}
16920 	/* address is needed now */
16921 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16922 
16923 
16924 	/* Fill sata_pkt */
16925 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16926 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16927 	/* Synchronous mode, no callback */
16928 	spkt->satapkt_comp = NULL;
16929 	/* Timeout 30s */
16930 	spkt->satapkt_time = sata_default_pkt_time;
16931 
16932 	scmd = &spkt->satapkt_cmd;
16933 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16934 
16935 	/*
16936 	 * Allocate buffer for SMART extended self-test log
16937 	 */
16938 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16939 	    sizeof (struct smart_ext_selftest_log));
16940 	if (scmd->satacmd_bp == NULL) {
16941 		sata_pkt_free(spx);
16942 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16943 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16944 		    "sata_ext_smart_selftest_log: "
16945 		    "cannot allocate buffer"));
16946 		return (-1);
16947 	}
16948 
16949 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16950 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16951 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16952 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16953 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16954 	scmd->satacmd_lba_low_msb = 0;
16955 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16956 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16957 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16958 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16959 
16960 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16961 	    sdinfo->satadrv_addr.cport)));
16962 
16963 	/* Send pkt to SATA HBA driver */
16964 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16965 	    SATA_TRAN_ACCEPTED ||
16966 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16967 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16968 		    sdinfo->satadrv_addr.cport)));
16969 
16970 		/*
16971 		 * Whoops, no SMART selftest log info available
16972 		 */
16973 		rval = -1;
16974 		goto fail;
16975 	} else {
16976 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16977 		    sdinfo->satadrv_addr.cport)));
16978 
16979 		if (spx->txlt_buf_dma_handle != NULL) {
16980 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16981 			    DDI_DMA_SYNC_FORKERNEL);
16982 			ASSERT(rval == DDI_SUCCESS);
16983 			if (sata_check_for_dma_error(dip, spx)) {
16984 				ddi_fm_service_impact(dip,
16985 				    DDI_SERVICE_UNAFFECTED);
16986 				rval = -1;
16987 				goto fail;
16988 			}
16989 		}
16990 		bcopy(scmd->satacmd_bp->b_un.b_addr,
16991 		    (uint8_t *)ext_selftest_log,
16992 		    sizeof (struct smart_ext_selftest_log));
16993 		rval = 0;
16994 	}
16995 
16996 fail:
16997 	/* Free allocated resources */
16998 	sata_free_local_buffer(spx);
16999 	sata_pkt_free(spx);
17000 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17001 
17002 	return (rval);
17003 }
17004 
17005 /*
17006  * Returns 0 for success, -1 otherwise
17007  *
17008  * SMART self-test log data is returned in buffer pointed to by selftest_log
17009  */
17010 static int
17011 sata_smart_selftest_log(
17012 	sata_hba_inst_t *sata_hba_inst,
17013 	sata_drive_info_t *sdinfo,
17014 	struct smart_selftest_log *selftest_log)
17015 {
17016 	sata_pkt_t *spkt;
17017 	sata_cmd_t *scmd;
17018 	sata_pkt_txlate_t *spx;
17019 	int rval;
17020 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17021 
17022 #if ! defined(lint)
17023 	ASSERT(sizeof (struct smart_selftest_log) == 512);
17024 #endif
17025 
17026 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17027 	spx->txlt_sata_hba_inst = sata_hba_inst;
17028 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17029 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17030 	if (spkt == NULL) {
17031 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17032 		return (-1);
17033 	}
17034 	/* address is needed now */
17035 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17036 
17037 
17038 	/* Fill sata_pkt */
17039 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17040 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17041 	/* Synchronous mode, no callback */
17042 	spkt->satapkt_comp = NULL;
17043 	/* Timeout 30s */
17044 	spkt->satapkt_time = sata_default_pkt_time;
17045 
17046 	scmd = &spkt->satapkt_cmd;
17047 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17048 
17049 	/*
17050 	 * Allocate buffer for SMART SELFTEST LOG
17051 	 */
17052 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17053 	    sizeof (struct smart_selftest_log));
17054 	if (scmd->satacmd_bp == NULL) {
17055 		sata_pkt_free(spx);
17056 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17057 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17058 		    "sata_smart_selftest_log: "
17059 		    "cannot allocate buffer"));
17060 		return (-1);
17061 	}
17062 
17063 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17064 	scmd->satacmd_addr_type = 0;		/* N/A */
17065 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17066 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17067 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17068 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17069 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17070 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17071 	scmd->satacmd_cmd_reg = SATAC_SMART;
17072 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17073 	    sdinfo->satadrv_addr.cport)));
17074 
17075 	/* Send pkt to SATA HBA driver */
17076 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17077 	    SATA_TRAN_ACCEPTED ||
17078 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17079 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17080 		    sdinfo->satadrv_addr.cport)));
17081 		/*
17082 		 * Whoops, no SMART DATA available
17083 		 */
17084 		rval = -1;
17085 		goto fail;
17086 	} else {
17087 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17088 		    sdinfo->satadrv_addr.cport)));
17089 		if (spx->txlt_buf_dma_handle != NULL) {
17090 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17091 			    DDI_DMA_SYNC_FORKERNEL);
17092 			ASSERT(rval == DDI_SUCCESS);
17093 			if (sata_check_for_dma_error(dip, spx)) {
17094 				ddi_fm_service_impact(dip,
17095 				    DDI_SERVICE_UNAFFECTED);
17096 				rval = -1;
17097 				goto fail;
17098 			}
17099 		}
17100 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17101 		    sizeof (struct smart_selftest_log));
17102 		rval = 0;
17103 	}
17104 
17105 fail:
17106 	/* Free allocated resources */
17107 	sata_free_local_buffer(spx);
17108 	sata_pkt_free(spx);
17109 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17110 
17111 	return (rval);
17112 }
17113 
17114 
17115 /*
17116  * Returns 0 for success, -1 otherwise
17117  *
17118  * SMART READ LOG data is returned in buffer pointed to by smart_log
17119  */
17120 static int
17121 sata_smart_read_log(
17122 	sata_hba_inst_t *sata_hba_inst,
17123 	sata_drive_info_t *sdinfo,
17124 	uint8_t *smart_log,		/* where the data should be returned */
17125 	uint8_t which_log,		/* which log should be returned */
17126 	uint8_t log_size)		/* # of 512 bytes in log */
17127 {
17128 	sata_pkt_t *spkt;
17129 	sata_cmd_t *scmd;
17130 	sata_pkt_txlate_t *spx;
17131 	int rval;
17132 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17133 
17134 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17135 	spx->txlt_sata_hba_inst = sata_hba_inst;
17136 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17137 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17138 	if (spkt == NULL) {
17139 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17140 		return (-1);
17141 	}
17142 	/* address is needed now */
17143 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17144 
17145 
17146 	/* Fill sata_pkt */
17147 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17148 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17149 	/* Synchronous mode, no callback */
17150 	spkt->satapkt_comp = NULL;
17151 	/* Timeout 30s */
17152 	spkt->satapkt_time = sata_default_pkt_time;
17153 
17154 	scmd = &spkt->satapkt_cmd;
17155 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17156 
17157 	/*
17158 	 * Allocate buffer for SMART READ LOG
17159 	 */
17160 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17161 	if (scmd->satacmd_bp == NULL) {
17162 		sata_pkt_free(spx);
17163 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17164 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17165 		    "sata_smart_read_log: " "cannot allocate buffer"));
17166 		return (-1);
17167 	}
17168 
17169 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17170 	scmd->satacmd_addr_type = 0;		/* N/A */
17171 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17172 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17173 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17174 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17175 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17176 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17177 	scmd->satacmd_cmd_reg = SATAC_SMART;
17178 
17179 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17180 	    sdinfo->satadrv_addr.cport)));
17181 
17182 	/* Send pkt to SATA HBA driver */
17183 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17184 	    SATA_TRAN_ACCEPTED ||
17185 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17186 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17187 		    sdinfo->satadrv_addr.cport)));
17188 
17189 		/*
17190 		 * Whoops, no SMART DATA available
17191 		 */
17192 		rval = -1;
17193 		goto fail;
17194 	} else {
17195 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17196 		    sdinfo->satadrv_addr.cport)));
17197 
17198 		if (spx->txlt_buf_dma_handle != NULL) {
17199 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17200 			    DDI_DMA_SYNC_FORKERNEL);
17201 			ASSERT(rval == DDI_SUCCESS);
17202 			if (sata_check_for_dma_error(dip, spx)) {
17203 				ddi_fm_service_impact(dip,
17204 				    DDI_SERVICE_UNAFFECTED);
17205 				rval = -1;
17206 				goto fail;
17207 			}
17208 		}
17209 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17210 		rval = 0;
17211 	}
17212 
17213 fail:
17214 	/* Free allocated resources */
17215 	sata_free_local_buffer(spx);
17216 	sata_pkt_free(spx);
17217 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17218 
17219 	return (rval);
17220 }
17221 
17222 /*
17223  * Used by LOG SENSE page 0x10
17224  *
17225  * return 0 for success, -1 otherwise
17226  *
17227  */
17228 static int
17229 sata_read_log_ext_directory(
17230 	sata_hba_inst_t *sata_hba_inst,
17231 	sata_drive_info_t *sdinfo,
17232 	struct read_log_ext_directory *logdir)
17233 {
17234 	sata_pkt_txlate_t *spx;
17235 	sata_pkt_t *spkt;
17236 	sata_cmd_t *scmd;
17237 	int rval;
17238 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17239 
17240 #if ! defined(lint)
17241 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17242 #endif
17243 
17244 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17245 	spx->txlt_sata_hba_inst = sata_hba_inst;
17246 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17247 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17248 	if (spkt == NULL) {
17249 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17250 		return (-1);
17251 	}
17252 
17253 	/* Fill sata_pkt */
17254 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17255 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17256 	/* Synchronous mode, no callback */
17257 	spkt->satapkt_comp = NULL;
17258 	/* Timeout 30s */
17259 	spkt->satapkt_time = sata_default_pkt_time;
17260 
17261 	scmd = &spkt->satapkt_cmd;
17262 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17263 
17264 	/*
17265 	 * Allocate buffer for SMART READ LOG EXTENDED command
17266 	 */
17267 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17268 	    sizeof (struct read_log_ext_directory));
17269 	if (scmd->satacmd_bp == NULL) {
17270 		sata_pkt_free(spx);
17271 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17272 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17273 		    "sata_read_log_ext_directory: "
17274 		    "cannot allocate buffer"));
17275 		return (-1);
17276 	}
17277 
17278 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17279 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17280 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17281 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17282 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17283 	scmd->satacmd_lba_low_msb = 0;
17284 	scmd->satacmd_lba_mid_lsb = 0;
17285 	scmd->satacmd_lba_mid_msb = 0;
17286 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17287 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17288 
17289 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17290 	    sdinfo->satadrv_addr.cport)));
17291 
17292 	/* Send pkt to SATA HBA driver */
17293 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17294 	    SATA_TRAN_ACCEPTED ||
17295 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17296 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17297 		    sdinfo->satadrv_addr.cport)));
17298 		/*
17299 		 * Whoops, no SMART selftest log info available
17300 		 */
17301 		rval = -1;
17302 		goto fail;
17303 	} else {
17304 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17305 		    sdinfo->satadrv_addr.cport)));
17306 		if (spx->txlt_buf_dma_handle != NULL) {
17307 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17308 			    DDI_DMA_SYNC_FORKERNEL);
17309 			ASSERT(rval == DDI_SUCCESS);
17310 			if (sata_check_for_dma_error(dip, spx)) {
17311 				ddi_fm_service_impact(dip,
17312 				    DDI_SERVICE_UNAFFECTED);
17313 				rval = -1;
17314 				goto fail;
17315 			}
17316 		}
17317 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17318 		    sizeof (struct read_log_ext_directory));
17319 		rval = 0;
17320 	}
17321 
17322 fail:
17323 	/* Free allocated resources */
17324 	sata_free_local_buffer(spx);
17325 	sata_pkt_free(spx);
17326 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17327 
17328 	return (rval);
17329 }
17330 
17331 /*
17332  * Set up error retrieval sata command for NCQ command error data
17333  * recovery.
17334  *
17335  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17336  * returns SATA_FAILURE otherwise.
17337  */
17338 static int
17339 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17340 {
17341 #ifndef __lock_lint
17342 	_NOTE(ARGUNUSED(sdinfo))
17343 #endif
17344 
17345 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17346 	sata_cmd_t *scmd;
17347 	struct buf *bp;
17348 
17349 	/* Operation modes are up to the caller */
17350 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17351 
17352 	/* Synchronous mode, no callback - may be changed by the caller */
17353 	spkt->satapkt_comp = NULL;
17354 	spkt->satapkt_time = sata_default_pkt_time;
17355 
17356 	scmd = &spkt->satapkt_cmd;
17357 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17358 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17359 
17360 	/*
17361 	 * Allocate dma_able buffer error data.
17362 	 * Buffer allocation will take care of buffer alignment and other DMA
17363 	 * attributes.
17364 	 */
17365 	bp = sata_alloc_local_buffer(spx,
17366 	    sizeof (struct sata_ncq_error_recovery_page));
17367 	if (bp == NULL)
17368 		return (SATA_FAILURE);
17369 
17370 	bp_mapin(bp); /* make data buffer accessible */
17371 	scmd->satacmd_bp = bp;
17372 
17373 	/*
17374 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17375 	 * before accessing it. Handle is in usual place in translate struct.
17376 	 */
17377 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17378 
17379 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17380 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17381 
17382 	return (SATA_SUCCESS);
17383 }
17384 
17385 /*
17386  * sata_xlate_errors() is used to translate (S)ATA error
17387  * information to SCSI information returned in the SCSI
17388  * packet.
17389  */
17390 static void
17391 sata_xlate_errors(sata_pkt_txlate_t *spx)
17392 {
17393 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17394 	struct scsi_extended_sense *sense;
17395 
17396 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17397 	*scsipkt->pkt_scbp = STATUS_CHECK;
17398 	sense = sata_arq_sense(spx);
17399 
17400 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17401 	case SATA_PKT_PORT_ERROR:
17402 		/*
17403 		 * We have no device data. Assume no data transfered.
17404 		 */
17405 		sense->es_key = KEY_HARDWARE_ERROR;
17406 		break;
17407 
17408 	case SATA_PKT_DEV_ERROR:
17409 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17410 		    SATA_STATUS_ERR) {
17411 			/*
17412 			 * determine dev error reason from error
17413 			 * reg content
17414 			 */
17415 			sata_decode_device_error(spx, sense);
17416 			break;
17417 		}
17418 		/* No extended sense key - no info available */
17419 		break;
17420 
17421 	case SATA_PKT_TIMEOUT:
17422 		scsipkt->pkt_reason = CMD_TIMEOUT;
17423 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17424 		/* No extended sense key */
17425 		break;
17426 
17427 	case SATA_PKT_ABORTED:
17428 		scsipkt->pkt_reason = CMD_ABORTED;
17429 		scsipkt->pkt_statistics |= STAT_ABORTED;
17430 		/* No extended sense key */
17431 		break;
17432 
17433 	case SATA_PKT_RESET:
17434 		/*
17435 		 * pkt aborted either by an explicit reset request from
17436 		 * a host, or due to error recovery
17437 		 */
17438 		scsipkt->pkt_reason = CMD_RESET;
17439 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17440 		break;
17441 
17442 	default:
17443 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17444 		break;
17445 	}
17446 }
17447 
17448 
17449 
17450 
17451 /*
17452  * Log sata message
17453  * dev pathname msg line preceeds the logged message.
17454  */
17455 
17456 static	void
17457 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17458 {
17459 	char pathname[128];
17460 	dev_info_t *dip = NULL;
17461 	va_list ap;
17462 
17463 	mutex_enter(&sata_log_mutex);
17464 
17465 	va_start(ap, fmt);
17466 	(void) vsprintf(sata_log_buf, fmt, ap);
17467 	va_end(ap);
17468 
17469 	if (sata_hba_inst != NULL) {
17470 		dip = SATA_DIP(sata_hba_inst);
17471 		(void) ddi_pathname(dip, pathname);
17472 	} else {
17473 		pathname[0] = 0;
17474 	}
17475 	if (level == CE_CONT) {
17476 		if (sata_debug_flags == 0)
17477 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17478 		else
17479 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17480 	} else {
17481 		if (level != CE_NOTE) {
17482 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17483 		} else if (sata_msg) {
17484 			cmn_err(level, "%s:\n %s", pathname,
17485 			    sata_log_buf);
17486 		}
17487 	}
17488 
17489 	/* sata trace debug */
17490 	sata_trace_debug(dip, sata_log_buf);
17491 
17492 	mutex_exit(&sata_log_mutex);
17493 }
17494 
17495 
17496 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17497 
17498 /*
17499  * Start or terminate the thread, depending on flag arg and current state
17500  */
17501 static void
17502 sata_event_thread_control(int startstop)
17503 {
17504 	static 	int sata_event_thread_terminating = 0;
17505 	static 	int sata_event_thread_starting = 0;
17506 	int i;
17507 
17508 	mutex_enter(&sata_event_mutex);
17509 
17510 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17511 	    sata_event_thread_terminating == 1)) {
17512 		mutex_exit(&sata_event_mutex);
17513 		return;
17514 	}
17515 	if (startstop == 1 && sata_event_thread_starting == 1) {
17516 		mutex_exit(&sata_event_mutex);
17517 		return;
17518 	}
17519 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17520 		sata_event_thread_starting = 1;
17521 		/* wait til terminate operation completes */
17522 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17523 		while (sata_event_thread_terminating == 1) {
17524 			if (i-- <= 0) {
17525 				sata_event_thread_starting = 0;
17526 				mutex_exit(&sata_event_mutex);
17527 #ifdef SATA_DEBUG
17528 				cmn_err(CE_WARN, "sata_event_thread_control: "
17529 				    "timeout waiting for thread to terminate");
17530 #endif
17531 				return;
17532 			}
17533 			mutex_exit(&sata_event_mutex);
17534 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17535 			mutex_enter(&sata_event_mutex);
17536 		}
17537 	}
17538 	if (startstop == 1) {
17539 		if (sata_event_thread == NULL) {
17540 			sata_event_thread = thread_create(NULL, 0,
17541 			    (void (*)())sata_event_daemon,
17542 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17543 		}
17544 		sata_event_thread_starting = 0;
17545 		mutex_exit(&sata_event_mutex);
17546 		return;
17547 	}
17548 
17549 	/*
17550 	 * If we got here, thread may need to be terminated
17551 	 */
17552 	if (sata_event_thread != NULL) {
17553 		int i;
17554 		/* Signal event thread to go away */
17555 		sata_event_thread_terminating = 1;
17556 		sata_event_thread_terminate = 1;
17557 		cv_signal(&sata_event_cv);
17558 		/*
17559 		 * Wait til daemon terminates.
17560 		 */
17561 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17562 		while (sata_event_thread_terminate == 1) {
17563 			mutex_exit(&sata_event_mutex);
17564 			if (i-- <= 0) {
17565 				/* Daemon did not go away !!! */
17566 #ifdef SATA_DEBUG
17567 				cmn_err(CE_WARN, "sata_event_thread_control: "
17568 				    "cannot terminate event daemon thread");
17569 #endif
17570 				mutex_enter(&sata_event_mutex);
17571 				break;
17572 			}
17573 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17574 			mutex_enter(&sata_event_mutex);
17575 		}
17576 		sata_event_thread_terminating = 0;
17577 	}
17578 	ASSERT(sata_event_thread_terminating == 0);
17579 	ASSERT(sata_event_thread_starting == 0);
17580 	mutex_exit(&sata_event_mutex);
17581 }
17582 
17583 
17584 /*
17585  * SATA HBA event notification function.
17586  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17587  * a port and/or device state or a controller itself.
17588  * Events for different addresses/addr types cannot be combined.
17589  * A warning message is generated for each event type.
17590  * Events are not processed by this function, so only the
17591  * event flag(s)is set for an affected entity and the event thread is
17592  * waken up. Event daemon thread processes all events.
17593  *
17594  * NOTE: Since more than one event may be reported at the same time, one
17595  * cannot determine a sequence of events when opposite event are reported, eg.
17596  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17597  * is taking precedence over reported events, i.e. may cause ignoring some
17598  * events.
17599  */
17600 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17601 
17602 void
17603 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17604 {
17605 	sata_hba_inst_t *sata_hba_inst = NULL;
17606 	sata_address_t *saddr;
17607 	sata_pmult_info_t *pmultinfo;
17608 	sata_drive_info_t *sdinfo;
17609 	sata_port_stats_t *pstats;
17610 	sata_cport_info_t *cportinfo;
17611 	sata_pmport_info_t *pmportinfo;
17612 	int cport, pmport;
17613 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17614 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17615 	char *lcp;
17616 	static char *err_msg_evnt_1 =
17617 	    "sata_hba_event_notify: invalid port event 0x%x ";
17618 	static char *err_msg_evnt_2 =
17619 	    "sata_hba_event_notify: invalid device event 0x%x ";
17620 	int linkevent;
17621 
17622 	/*
17623 	 * There is a possibility that an event will be generated on HBA
17624 	 * that has not completed attachment or is detaching. We still want
17625 	 * to process events until HBA is detached.
17626 	 */
17627 	mutex_enter(&sata_mutex);
17628 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17629 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17630 		if (SATA_DIP(sata_hba_inst) == dip)
17631 			if (sata_hba_inst->satahba_attached == 1)
17632 				break;
17633 	}
17634 	mutex_exit(&sata_mutex);
17635 	if (sata_hba_inst == NULL)
17636 		/* HBA not attached */
17637 		return;
17638 
17639 	ASSERT(sata_device != NULL);
17640 
17641 	/*
17642 	 * Validate address before - do not proceed with invalid address.
17643 	 */
17644 	saddr = &sata_device->satadev_addr;
17645 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17646 		return;
17647 
17648 	cport = saddr->cport;
17649 	pmport = saddr->pmport;
17650 
17651 	buf1[0] = buf2[0] = '\0';
17652 
17653 	/*
17654 	 * If event relates to port or device, check port state.
17655 	 * Port has to be initialized, or we cannot accept an event.
17656 	 */
17657 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17658 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17659 		mutex_enter(&sata_hba_inst->satahba_mutex);
17660 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17661 		mutex_exit(&sata_hba_inst->satahba_mutex);
17662 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17663 			return;
17664 	}
17665 
17666 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17667 	    SATA_ADDR_DPMPORT)) != 0) {
17668 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17669 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17670 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17671 			    "is attached to port %d, ignore pmult/pmport "
17672 			    "event 0x%x", cportinfo->cport_dev_type,
17673 			    cport, event));
17674 			return;
17675 		}
17676 
17677 		mutex_enter(&cportinfo->cport_mutex);
17678 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17679 		mutex_exit(&cportinfo->cport_mutex);
17680 
17681 		/*
17682 		 * The daemon might be processing attachment of port
17683 		 * multiplier, in that case we should ignore events on its
17684 		 * sub-devices.
17685 		 *
17686 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17687 		 * The pmport_state is checked by sata daemon.
17688 		 */
17689 		if (pmultinfo == NULL ||
17690 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17691 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17692 			    "sata_hba_event_notify: pmult is not"
17693 			    "available at port %d:%d, ignore event 0x%x",
17694 			    cport, pmport, event));
17695 			return;
17696 		}
17697 	}
17698 
17699 	if ((saddr->qual &
17700 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17701 
17702 		mutex_enter(&cportinfo->cport_mutex);
17703 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17704 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17705 			    "sata_hba_event_notify: invalid/"
17706 			    "un-implemented port %d:%d (%d ports), "
17707 			    "ignore event 0x%x", cport, pmport,
17708 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17709 			mutex_exit(&cportinfo->cport_mutex);
17710 			return;
17711 		}
17712 		mutex_exit(&cportinfo->cport_mutex);
17713 
17714 		mutex_enter(&sata_hba_inst->satahba_mutex);
17715 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17716 		    cport, pmport);
17717 		mutex_exit(&sata_hba_inst->satahba_mutex);
17718 
17719 		/* pmport is implemented/valid? */
17720 		if (pmportinfo == NULL) {
17721 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17722 			    "sata_hba_event_notify: invalid/"
17723 			    "un-implemented port %d:%d, ignore "
17724 			    "event 0x%x", cport, pmport, event));
17725 			return;
17726 		}
17727 	}
17728 
17729 	/*
17730 	 * Events refer to devices, ports and controllers - each has
17731 	 * unique address. Events for different addresses cannot be combined.
17732 	 */
17733 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17734 
17735 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17736 
17737 		/* qualify this event(s) */
17738 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17739 			/* Invalid event for the device port */
17740 			(void) sprintf(buf2, err_msg_evnt_1,
17741 			    event & SATA_EVNT_PORT_EVENTS);
17742 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17743 			goto event_info;
17744 		}
17745 		if (saddr->qual == SATA_ADDR_CPORT) {
17746 			/* Controller's device port event */
17747 
17748 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17749 			    cport_event_flags |=
17750 			    event & SATA_EVNT_PORT_EVENTS;
17751 			pstats =
17752 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17753 			    cport_stats;
17754 		} else {
17755 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17756 			mutex_enter(&pmportinfo->pmport_mutex);
17757 			/* Port multiplier's device port event */
17758 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17759 			    pmport_event_flags |=
17760 			    event & SATA_EVNT_PORT_EVENTS;
17761 			pstats =
17762 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17763 			    pmport_stats;
17764 			mutex_exit(&pmportinfo->pmport_mutex);
17765 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17766 		}
17767 
17768 		/*
17769 		 * Add to statistics and log the message. We have to do it
17770 		 * here rather than in the event daemon, because there may be
17771 		 * multiple events occuring before they are processed.
17772 		 */
17773 		linkevent = event &
17774 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17775 		if (linkevent) {
17776 			if (linkevent == (SATA_EVNT_LINK_LOST |
17777 			    SATA_EVNT_LINK_ESTABLISHED)) {
17778 				/* This is likely event combination */
17779 				(void) strlcat(buf1, "link lost/established, ",
17780 				    SATA_EVENT_MAX_MSG_LENGTH);
17781 
17782 				if (pstats->link_lost < 0xffffffffffffffffULL)
17783 					pstats->link_lost++;
17784 				if (pstats->link_established <
17785 				    0xffffffffffffffffULL)
17786 					pstats->link_established++;
17787 				linkevent = 0;
17788 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17789 				(void) strlcat(buf1, "link lost, ",
17790 				    SATA_EVENT_MAX_MSG_LENGTH);
17791 
17792 				if (pstats->link_lost < 0xffffffffffffffffULL)
17793 					pstats->link_lost++;
17794 			} else {
17795 				(void) strlcat(buf1, "link established, ",
17796 				    SATA_EVENT_MAX_MSG_LENGTH);
17797 				if (pstats->link_established <
17798 				    0xffffffffffffffffULL)
17799 					pstats->link_established++;
17800 			}
17801 		}
17802 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17803 			(void) strlcat(buf1, "device attached, ",
17804 			    SATA_EVENT_MAX_MSG_LENGTH);
17805 			if (pstats->device_attached < 0xffffffffffffffffULL)
17806 				pstats->device_attached++;
17807 		}
17808 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17809 			(void) strlcat(buf1, "device detached, ",
17810 			    SATA_EVENT_MAX_MSG_LENGTH);
17811 			if (pstats->device_detached < 0xffffffffffffffffULL)
17812 				pstats->device_detached++;
17813 		}
17814 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17815 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17816 			    "port %d power level changed", cport);
17817 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17818 				pstats->port_pwr_changed++;
17819 		}
17820 
17821 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17822 			/* There should be no other events for this address */
17823 			(void) sprintf(buf2, err_msg_evnt_1,
17824 			    event & ~SATA_EVNT_PORT_EVENTS);
17825 		}
17826 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17827 
17828 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17829 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17830 
17831 		/* qualify this event */
17832 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17833 			/* Invalid event for a device */
17834 			(void) sprintf(buf2, err_msg_evnt_2,
17835 			    event & SATA_EVNT_DEVICE_RESET);
17836 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17837 			goto event_info;
17838 		}
17839 		/* drive event */
17840 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17841 		if (sdinfo != NULL) {
17842 			if (event & SATA_EVNT_DEVICE_RESET) {
17843 				(void) strlcat(buf1, "device reset, ",
17844 				    SATA_EVENT_MAX_MSG_LENGTH);
17845 				if (sdinfo->satadrv_stats.drive_reset <
17846 				    0xffffffffffffffffULL)
17847 					sdinfo->satadrv_stats.drive_reset++;
17848 				sdinfo->satadrv_event_flags |=
17849 				    SATA_EVNT_DEVICE_RESET;
17850 			}
17851 		}
17852 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17853 			/* Invalid event for a device */
17854 			(void) sprintf(buf2, err_msg_evnt_2,
17855 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17856 		}
17857 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17858 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17859 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17860 
17861 		/* qualify this event */
17862 		if ((event & (SATA_EVNT_DEVICE_RESET |
17863 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17864 			/* Invalid event for a port multiplier */
17865 			(void) sprintf(buf2, err_msg_evnt_2,
17866 			    event & SATA_EVNT_DEVICE_RESET);
17867 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17868 			goto event_info;
17869 		}
17870 
17871 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17872 
17873 		if (event & SATA_EVNT_DEVICE_RESET) {
17874 
17875 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17876 			    "[Reset] port-mult on cport %d", cport);
17877 			pmultinfo->pmult_event_flags |=
17878 			    SATA_EVNT_DEVICE_RESET;
17879 			(void) strlcat(buf1, "pmult reset, ",
17880 			    SATA_EVENT_MAX_MSG_LENGTH);
17881 		}
17882 
17883 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17884 
17885 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17886 			    "pmult link changed on cport %d", cport);
17887 			pmultinfo->pmult_event_flags |=
17888 			    SATA_EVNT_PMULT_LINK_CHANGED;
17889 			(void) strlcat(buf1, "pmult link changed, ",
17890 			    SATA_EVENT_MAX_MSG_LENGTH);
17891 		}
17892 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17893 
17894 	} else {
17895 		if (saddr->qual != SATA_ADDR_NULL) {
17896 			/* Wrong address qualifier */
17897 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17898 			    "sata_hba_event_notify: invalid address 0x%x",
17899 			    *(uint32_t *)saddr));
17900 			return;
17901 		}
17902 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17903 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17904 			/* Invalid event for the controller */
17905 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17906 			    "sata_hba_event_notify: invalid event 0x%x for "
17907 			    "controller",
17908 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17909 			return;
17910 		}
17911 		buf1[0] = '\0';
17912 		/* This may be a frequent and not interesting event */
17913 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17914 		    "controller power level changed\n", NULL);
17915 
17916 		mutex_enter(&sata_hba_inst->satahba_mutex);
17917 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17918 		    0xffffffffffffffffULL)
17919 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17920 
17921 		sata_hba_inst->satahba_event_flags |=
17922 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17923 		mutex_exit(&sata_hba_inst->satahba_mutex);
17924 	}
17925 	/*
17926 	 * If we got here, there is something to do with this HBA
17927 	 * instance.
17928 	 */
17929 	mutex_enter(&sata_hba_inst->satahba_mutex);
17930 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17931 	mutex_exit(&sata_hba_inst->satahba_mutex);
17932 	mutex_enter(&sata_mutex);
17933 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17934 	mutex_exit(&sata_mutex);
17935 
17936 	/* Tickle event thread */
17937 	mutex_enter(&sata_event_mutex);
17938 	if (sata_event_thread_active == 0)
17939 		cv_signal(&sata_event_cv);
17940 	mutex_exit(&sata_event_mutex);
17941 
17942 event_info:
17943 	if (buf1[0] != '\0') {
17944 		lcp = strrchr(buf1, ',');
17945 		if (lcp != NULL)
17946 			*lcp = '\0';
17947 	}
17948 	if (saddr->qual == SATA_ADDR_CPORT ||
17949 	    saddr->qual == SATA_ADDR_DCPORT) {
17950 		if (buf1[0] != '\0') {
17951 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17952 			    cport, buf1);
17953 		}
17954 		if (buf2[0] != '\0') {
17955 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17956 			    cport, buf2);
17957 		}
17958 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17959 	    saddr->qual == SATA_ADDR_DPMPORT) {
17960 		if (buf1[0] != '\0') {
17961 			sata_log(sata_hba_inst, CE_NOTE,
17962 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17963 		}
17964 		if (buf2[0] != '\0') {
17965 			sata_log(sata_hba_inst, CE_NOTE,
17966 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17967 		}
17968 	}
17969 }
17970 
17971 
17972 /*
17973  * Event processing thread.
17974  * Arg is a pointer to the sata_hba_list pointer.
17975  * It is not really needed, because sata_hba_list is global and static
17976  */
17977 static void
17978 sata_event_daemon(void *arg)
17979 {
17980 #ifndef __lock_lint
17981 	_NOTE(ARGUNUSED(arg))
17982 #endif
17983 	sata_hba_inst_t *sata_hba_inst;
17984 	clock_t delta;
17985 
17986 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17987 	    "SATA event daemon started\n", NULL);
17988 loop:
17989 	/*
17990 	 * Process events here. Walk through all registered HBAs
17991 	 */
17992 	mutex_enter(&sata_mutex);
17993 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17994 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17995 		ASSERT(sata_hba_inst != NULL);
17996 		mutex_enter(&sata_hba_inst->satahba_mutex);
17997 		if (sata_hba_inst->satahba_attached == 0 ||
17998 		    (sata_hba_inst->satahba_event_flags &
17999 		    SATA_EVNT_SKIP) != 0) {
18000 			mutex_exit(&sata_hba_inst->satahba_mutex);
18001 			continue;
18002 		}
18003 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18004 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18005 			mutex_exit(&sata_hba_inst->satahba_mutex);
18006 			mutex_exit(&sata_mutex);
18007 			/* Got the controller with pending event */
18008 			sata_process_controller_events(sata_hba_inst);
18009 			/*
18010 			 * Since global mutex was released, there is a
18011 			 * possibility that HBA list has changed, so start
18012 			 * over from the top. Just processed controller
18013 			 * will be passed-over because of the SKIP flag.
18014 			 */
18015 			goto loop;
18016 		}
18017 		mutex_exit(&sata_hba_inst->satahba_mutex);
18018 	}
18019 	/* Clear SKIP flag in all controllers */
18020 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18021 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18022 		mutex_enter(&sata_hba_inst->satahba_mutex);
18023 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18024 		mutex_exit(&sata_hba_inst->satahba_mutex);
18025 	}
18026 	mutex_exit(&sata_mutex);
18027 
18028 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18029 	    "SATA EVENT DAEMON suspending itself", NULL);
18030 
18031 #ifdef SATA_DEBUG
18032 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18033 		sata_log(sata_hba_inst, CE_WARN,
18034 		    "SATA EVENTS PROCESSING DISABLED\n");
18035 		thread_exit(); /* Daemon will not run again */
18036 	}
18037 #endif
18038 	mutex_enter(&sata_event_mutex);
18039 	sata_event_thread_active = 0;
18040 	mutex_exit(&sata_event_mutex);
18041 	/*
18042 	 * Go to sleep/suspend itself and wake up either because new event or
18043 	 * wait timeout. Exit if there is a termination request (driver
18044 	 * unload).
18045 	 */
18046 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18047 	do {
18048 		mutex_enter(&sata_event_mutex);
18049 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18050 		    delta, TR_CLOCK_TICK);
18051 
18052 		if (sata_event_thread_active != 0) {
18053 			mutex_exit(&sata_event_mutex);
18054 			continue;
18055 		}
18056 
18057 		/* Check if it is time to go away */
18058 		if (sata_event_thread_terminate == 1) {
18059 			/*
18060 			 * It is up to the thread setting above flag to make
18061 			 * sure that this thread is not killed prematurely.
18062 			 */
18063 			sata_event_thread_terminate = 0;
18064 			sata_event_thread = NULL;
18065 			mutex_exit(&sata_event_mutex);
18066 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18067 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18068 			thread_exit();  { _NOTE(NOT_REACHED) }
18069 		}
18070 		mutex_exit(&sata_event_mutex);
18071 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18072 
18073 	mutex_enter(&sata_event_mutex);
18074 	sata_event_thread_active = 1;
18075 	mutex_exit(&sata_event_mutex);
18076 
18077 	mutex_enter(&sata_mutex);
18078 	sata_event_pending &= ~SATA_EVNT_MAIN;
18079 	mutex_exit(&sata_mutex);
18080 
18081 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18082 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18083 
18084 	goto loop;
18085 }
18086 
18087 /*
18088  * Specific HBA instance event processing.
18089  *
18090  * NOTE: At the moment, device event processing is limited to hard disks
18091  * only.
18092  * Port multiplier is supported now.
18093  */
18094 static void
18095 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18096 {
18097 	int ncport;
18098 	uint32_t event_flags;
18099 	sata_address_t *saddr;
18100 	sata_cport_info_t *cportinfo;
18101 	sata_pmult_info_t *pmultinfo;
18102 
18103 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18104 	    "Processing controller %d event(s)",
18105 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18106 
18107 	mutex_enter(&sata_hba_inst->satahba_mutex);
18108 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18109 	event_flags = sata_hba_inst->satahba_event_flags;
18110 	mutex_exit(&sata_hba_inst->satahba_mutex);
18111 	/*
18112 	 * Process controller power change first
18113 	 * HERE
18114 	 */
18115 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18116 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18117 
18118 	/*
18119 	 * Search through ports/devices to identify affected port/device.
18120 	 * We may have to process events for more than one port/device.
18121 	 */
18122 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18123 		/*
18124 		 * Not all ports may be processed in attach by the time we
18125 		 * get an event. Check if port info is initialized.
18126 		 */
18127 		mutex_enter(&sata_hba_inst->satahba_mutex);
18128 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18129 		mutex_exit(&sata_hba_inst->satahba_mutex);
18130 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18131 			continue;
18132 
18133 		/* We have initialized controller port info */
18134 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18135 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18136 		    cport_event_flags;
18137 		/* Check if port was locked by IOCTL processing */
18138 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18139 			/*
18140 			 * We ignore port events because port is busy
18141 			 * with AP control processing. Set again
18142 			 * controller and main event flag, so that
18143 			 * events may be processed by the next daemon
18144 			 * run.
18145 			 */
18146 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18147 			mutex_enter(&sata_hba_inst->satahba_mutex);
18148 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18149 			mutex_exit(&sata_hba_inst->satahba_mutex);
18150 			mutex_enter(&sata_mutex);
18151 			sata_event_pending |= SATA_EVNT_MAIN;
18152 			mutex_exit(&sata_mutex);
18153 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18154 			    "Event processing postponed until "
18155 			    "AP control processing completes",
18156 			    NULL);
18157 			/* Check other ports */
18158 			continue;
18159 		} else {
18160 			/*
18161 			 * Set BSY flag so that AP control would not
18162 			 * interfere with events processing for
18163 			 * this port.
18164 			 */
18165 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18166 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18167 		}
18168 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18169 
18170 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18171 
18172 		if ((event_flags &
18173 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18174 			/*
18175 			 * Got port event.
18176 			 * We need some hierarchy of event processing as they
18177 			 * are affecting each other:
18178 			 * 1. port failed
18179 			 * 2. device detached/attached
18180 			 * 3. link events - link events may trigger device
18181 			 *    detached or device attached events in some
18182 			 *    circumstances.
18183 			 * 4. port power level changed
18184 			 */
18185 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18186 				sata_process_port_failed_event(sata_hba_inst,
18187 				    saddr);
18188 			}
18189 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18190 				sata_process_device_detached(sata_hba_inst,
18191 				    saddr);
18192 			}
18193 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18194 				sata_process_device_attached(sata_hba_inst,
18195 				    saddr);
18196 			}
18197 			if (event_flags &
18198 			    (SATA_EVNT_LINK_ESTABLISHED |
18199 			    SATA_EVNT_LINK_LOST)) {
18200 				sata_process_port_link_events(sata_hba_inst,
18201 				    saddr);
18202 			}
18203 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18204 				sata_process_port_pwr_change(sata_hba_inst,
18205 				    saddr);
18206 			}
18207 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18208 				sata_process_target_node_cleanup(
18209 				    sata_hba_inst, saddr);
18210 			}
18211 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18212 				sata_process_device_autoonline(
18213 				    sata_hba_inst, saddr);
18214 			}
18215 		}
18216 
18217 
18218 		/*
18219 		 * Scan port multiplier and all its sub-ports event flags.
18220 		 * The events are marked by
18221 		 * (1) sata_pmult_info.pmult_event_flags
18222 		 * (2) sata_pmport_info.pmport_event_flags
18223 		 */
18224 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18225 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18226 			/*
18227 			 * There should be another extra check: this
18228 			 * port multiplier still exists?
18229 			 */
18230 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18231 			    ncport);
18232 
18233 			if (pmultinfo != NULL) {
18234 				mutex_exit(&(SATA_CPORT_MUTEX(
18235 				    sata_hba_inst, ncport)));
18236 				sata_process_pmult_events(
18237 				    sata_hba_inst, ncport);
18238 				mutex_enter(&(SATA_CPORT_MUTEX(
18239 				    sata_hba_inst, ncport)));
18240 			} else {
18241 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18242 				    "Port-multiplier is gone. "
18243 				    "Ignore all sub-device events "
18244 				    "at port %d.", ncport);
18245 			}
18246 		}
18247 
18248 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18249 		    SATA_DTYPE_NONE) &&
18250 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18251 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18252 			    satadrv_event_flags &
18253 			    (SATA_EVNT_DEVICE_RESET |
18254 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18255 				/* Have device event */
18256 				sata_process_device_reset(sata_hba_inst,
18257 				    saddr);
18258 			}
18259 		}
18260 		/* Release PORT_BUSY flag */
18261 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18262 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18263 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18264 
18265 	} /* End of loop through the controller SATA ports */
18266 }
18267 
18268 /*
18269  * Specific port multiplier instance event processing. At the moment, device
18270  * event processing is limited to link/attach event only.
18271  *
18272  * NOTE: power management event is not supported yet.
18273  */
18274 static void
18275 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18276 {
18277 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18278 	sata_pmult_info_t *pmultinfo;
18279 	sata_pmport_info_t *pmportinfo;
18280 	sata_address_t *saddr;
18281 	sata_device_t sata_device;
18282 	uint32_t event_flags;
18283 	int npmport;
18284 	int rval;
18285 
18286 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18287 	    "Processing pmult event(s) on cport %d of controller %d",
18288 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18289 
18290 	/* First process events on port multiplier */
18291 	mutex_enter(&cportinfo->cport_mutex);
18292 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18293 	event_flags = pmultinfo->pmult_event_flags;
18294 
18295 	/*
18296 	 * Reset event (of port multiplier) has higher priority because the
18297 	 * port multiplier itself might be failed or removed after reset.
18298 	 */
18299 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18300 		/*
18301 		 * The status of the sub-links are uncertain,
18302 		 * so mark all sub-ports as RESET
18303 		 */
18304 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18305 		    sata_hba_inst, cport); npmport ++) {
18306 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18307 			    cport, npmport);
18308 			if (pmportinfo == NULL) {
18309 				/* That's weird. */
18310 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18311 				    "sata_hba_event_notify: "
18312 				    "invalid/un-implemented "
18313 				    "port %d:%d (%d ports), ",
18314 				    cport, npmport, SATA_NUM_PMPORTS(
18315 				    sata_hba_inst, cport)));
18316 				continue;
18317 			}
18318 
18319 			mutex_enter(&pmportinfo->pmport_mutex);
18320 
18321 			/* Mark all pmport to unknow state. */
18322 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18323 			/* Mark all pmports with link events. */
18324 			pmportinfo->pmport_event_flags =
18325 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18326 			mutex_exit(&pmportinfo->pmport_mutex);
18327 		}
18328 
18329 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18330 		/*
18331 		 * We need probe the port multiplier to know what has
18332 		 * happened.
18333 		 */
18334 		bzero(&sata_device, sizeof (sata_device_t));
18335 		sata_device.satadev_rev = SATA_DEVICE_REV;
18336 		sata_device.satadev_addr.cport = cport;
18337 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18338 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18339 
18340 		mutex_exit(&cportinfo->cport_mutex);
18341 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18342 		    (SATA_DIP(sata_hba_inst), &sata_device);
18343 		mutex_enter(&cportinfo->cport_mutex);
18344 		if (rval != SATA_SUCCESS) {
18345 			/* Something went wrong? Fail the port */
18346 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18347 			mutex_exit(&cportinfo->cport_mutex);
18348 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18349 			    "SATA port %d probing failed", cport));
18350 
18351 			/* PMult structure must be released.  */
18352 			sata_free_pmult(sata_hba_inst, &sata_device);
18353 			return;
18354 		}
18355 
18356 		sata_update_port_info(sata_hba_inst, &sata_device);
18357 
18358 		/*
18359 		 * Sanity check - Port is active? Is the link active?
18360 		 * The device is still a port multiplier?
18361 		 */
18362 		if ((cportinfo->cport_state &
18363 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18364 		    ((cportinfo->cport_scr.sstatus &
18365 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18366 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18367 			mutex_exit(&cportinfo->cport_mutex);
18368 
18369 			/* PMult structure must be released.  */
18370 			sata_free_pmult(sata_hba_inst, &sata_device);
18371 			return;
18372 		}
18373 
18374 		/* Probed succeed, set port ready. */
18375 		cportinfo->cport_state |=
18376 		    SATA_STATE_PROBED | SATA_STATE_READY;
18377 	}
18378 
18379 	/* Release port multiplier event flags. */
18380 	pmultinfo->pmult_event_flags &=
18381 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18382 	mutex_exit(&cportinfo->cport_mutex);
18383 
18384 	/*
18385 	 * Check all sub-links.
18386 	 */
18387 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18388 	    npmport ++) {
18389 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18390 		mutex_enter(&pmportinfo->pmport_mutex);
18391 		event_flags = pmportinfo->pmport_event_flags;
18392 		mutex_exit(&pmportinfo->pmport_mutex);
18393 		saddr = &pmportinfo->pmport_addr;
18394 
18395 		if ((event_flags &
18396 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18397 			/*
18398 			 * Got port multiplier port event.
18399 			 * We need some hierarchy of event processing as they
18400 			 * are affecting each other:
18401 			 * 1. device detached/attached
18402 			 * 2. link events - link events may trigger device
18403 			 *    detached or device attached events in some
18404 			 *    circumstances.
18405 			 */
18406 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18407 				sata_process_pmdevice_detached(sata_hba_inst,
18408 				    saddr);
18409 			}
18410 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18411 				sata_process_pmdevice_attached(sata_hba_inst,
18412 				    saddr);
18413 			}
18414 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18415 			    event_flags & SATA_EVNT_LINK_LOST) {
18416 				sata_process_pmport_link_events(sata_hba_inst,
18417 				    saddr);
18418 			}
18419 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18420 				sata_process_target_node_cleanup(
18421 				    sata_hba_inst, saddr);
18422 			}
18423 		}
18424 
18425 		/* Checking drive event(s). */
18426 		mutex_enter(&pmportinfo->pmport_mutex);
18427 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18428 		    pmportinfo->pmport_sata_drive != NULL) {
18429 			event_flags = pmportinfo->pmport_sata_drive->
18430 			    satadrv_event_flags;
18431 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18432 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18433 
18434 				/* Have device event */
18435 				sata_process_pmdevice_reset(sata_hba_inst,
18436 				    saddr);
18437 			}
18438 		}
18439 		mutex_exit(&pmportinfo->pmport_mutex);
18440 
18441 		/* Release PORT_BUSY flag */
18442 		mutex_enter(&cportinfo->cport_mutex);
18443 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18444 		mutex_exit(&cportinfo->cport_mutex);
18445 	}
18446 
18447 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18448 	    "[DONE] pmult event(s) on cport %d of controller %d",
18449 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18450 }
18451 
18452 /*
18453  * Process HBA power level change reported by HBA driver.
18454  * Not implemented at this time - event is ignored.
18455  */
18456 static void
18457 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18458 {
18459 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18460 	    "Processing controller power level change", NULL);
18461 
18462 	/* Ignoring it for now */
18463 	mutex_enter(&sata_hba_inst->satahba_mutex);
18464 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18465 	mutex_exit(&sata_hba_inst->satahba_mutex);
18466 }
18467 
18468 /*
18469  * Process port power level change reported by HBA driver.
18470  * Not implemented at this time - event is ignored.
18471  */
18472 static void
18473 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18474     sata_address_t *saddr)
18475 {
18476 	sata_cport_info_t *cportinfo;
18477 
18478 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18479 	    "Processing port power level change", NULL);
18480 
18481 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18482 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18483 	/* Reset event flag */
18484 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18485 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18486 }
18487 
18488 /*
18489  * Process port failure reported by HBA driver.
18490  * cports support only - no pmports.
18491  */
18492 static void
18493 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18494     sata_address_t *saddr)
18495 {
18496 	sata_cport_info_t *cportinfo;
18497 
18498 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18499 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18500 	/* Reset event flag first */
18501 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18502 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18503 	if ((cportinfo->cport_state &
18504 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18505 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18506 		    cport_mutex);
18507 		return;
18508 	}
18509 	/* Fail the port */
18510 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18511 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18512 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18513 }
18514 
18515 /*
18516  * Device Reset Event processing.
18517  * The sequence is managed by 3 stage flags:
18518  * - reset event reported,
18519  * - reset event being processed,
18520  * - request to clear device reset state.
18521  *
18522  * NOTE: This function has to be entered with cport mutex held. It exits with
18523  * mutex held as well, but can release mutex during the processing.
18524  */
18525 static void
18526 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18527     sata_address_t *saddr)
18528 {
18529 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18530 	sata_drive_info_t *sdinfo;
18531 	sata_cport_info_t *cportinfo;
18532 	sata_device_t sata_device;
18533 	int rval_probe, rval_set;
18534 
18535 	/* We only care about host sata cport for now */
18536 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18537 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18538 	/*
18539 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18540 	 * state, ignore reset event.
18541 	 */
18542 	if (((cportinfo->cport_state &
18543 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18544 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18545 		sdinfo->satadrv_event_flags &=
18546 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18547 		return;
18548 	}
18549 
18550 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18551 	    SATA_DTYPE_PMULT)) {
18552 		/*
18553 		 * Should not happened: this is already handled in
18554 		 * sata_hba_event_notify()
18555 		 */
18556 		mutex_exit(&cportinfo->cport_mutex);
18557 		goto done;
18558 	}
18559 
18560 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18561 	    SATA_VALID_DEV_TYPE) == 0) {
18562 		/*
18563 		 * This should not happen - coding error.
18564 		 * But we can recover, so do not panic, just clean up
18565 		 * and if in debug mode, log the message.
18566 		 */
18567 #ifdef SATA_DEBUG
18568 		sata_log(sata_hba_inst, CE_WARN,
18569 		    "sata_process_device_reset: "
18570 		    "Invalid device type with sdinfo!", NULL);
18571 #endif
18572 		sdinfo->satadrv_event_flags = 0;
18573 		return;
18574 	}
18575 
18576 #ifdef SATA_DEBUG
18577 	if ((sdinfo->satadrv_event_flags &
18578 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18579 		/* Nothing to do */
18580 		/* Something is weird - why we are processing dev reset? */
18581 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18582 		    "No device reset event!!!!", NULL);
18583 
18584 		return;
18585 	}
18586 	if ((sdinfo->satadrv_event_flags &
18587 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18588 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18589 		/* Something is weird - new device reset event */
18590 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18591 		    "Overlapping device reset events!", NULL);
18592 	}
18593 #endif
18594 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18595 	    "Processing port %d device reset", saddr->cport);
18596 
18597 	/* Clear event flag */
18598 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18599 
18600 	/* It seems that we always need to check the port state first */
18601 	sata_device.satadev_rev = SATA_DEVICE_REV;
18602 	sata_device.satadev_addr = *saddr;
18603 	/*
18604 	 * We have to exit mutex, because the HBA probe port function may
18605 	 * block on its own mutex.
18606 	 */
18607 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18608 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18609 	    (SATA_DIP(sata_hba_inst), &sata_device);
18610 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18611 	sata_update_port_info(sata_hba_inst, &sata_device);
18612 	if (rval_probe != SATA_SUCCESS) {
18613 		/* Something went wrong? Fail the port */
18614 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18615 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18616 		if (sdinfo != NULL)
18617 			sdinfo->satadrv_event_flags = 0;
18618 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18619 		    cport_mutex);
18620 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18621 		    "SATA port %d probing failed",
18622 		    saddr->cport));
18623 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18624 		    saddr->cport)->cport_mutex);
18625 		return;
18626 	}
18627 	if ((sata_device.satadev_scr.sstatus  &
18628 	    SATA_PORT_DEVLINK_UP_MASK) !=
18629 	    SATA_PORT_DEVLINK_UP ||
18630 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18631 		/*
18632 		 * No device to process, anymore. Some other event processing
18633 		 * would or have already performed port info cleanup.
18634 		 * To be safe (HBA may need it), request clearing device
18635 		 * reset condition.
18636 		 */
18637 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18638 		if (sdinfo != NULL) {
18639 			sdinfo->satadrv_event_flags &=
18640 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18641 			sdinfo->satadrv_event_flags |=
18642 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18643 		}
18644 		return;
18645 	}
18646 
18647 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18648 	if (sdinfo == NULL) {
18649 		return;
18650 	}
18651 	if ((sdinfo->satadrv_event_flags &
18652 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18653 		/*
18654 		 * Start tracking time for device feature restoration and
18655 		 * identification. Save current time (lbolt value).
18656 		 */
18657 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18658 	}
18659 	/* Mark device reset processing as active */
18660 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18661 
18662 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18663 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18664 
18665 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18666 
18667 	if (rval_set  != SATA_SUCCESS) {
18668 		/*
18669 		 * Restoring drive setting failed.
18670 		 * Probe the port first, to check if the port state has changed
18671 		 */
18672 		sata_device.satadev_rev = SATA_DEVICE_REV;
18673 		sata_device.satadev_addr = *saddr;
18674 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18675 		/* probe port */
18676 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18677 		    (SATA_DIP(sata_hba_inst), &sata_device);
18678 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18679 		    cport_mutex);
18680 		if (rval_probe == SATA_SUCCESS &&
18681 		    (sata_device.satadev_state &
18682 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18683 		    (sata_device.satadev_scr.sstatus  &
18684 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18685 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18686 			/*
18687 			 * We may retry this a bit later - in-process reset
18688 			 * condition should be already set.
18689 			 * Track retry time for device identification.
18690 			 */
18691 			if ((cportinfo->cport_dev_type &
18692 			    SATA_VALID_DEV_TYPE) != 0 &&
18693 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18694 			    sdinfo->satadrv_reset_time != 0) {
18695 				clock_t cur_time = ddi_get_lbolt();
18696 				/*
18697 				 * If the retry time limit was not
18698 				 * exceeded, retry.
18699 				 */
18700 				if ((cur_time - sdinfo->satadrv_reset_time) <
18701 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18702 					mutex_enter(
18703 					    &sata_hba_inst->satahba_mutex);
18704 					sata_hba_inst->satahba_event_flags |=
18705 					    SATA_EVNT_MAIN;
18706 					mutex_exit(
18707 					    &sata_hba_inst->satahba_mutex);
18708 					mutex_enter(&sata_mutex);
18709 					sata_event_pending |= SATA_EVNT_MAIN;
18710 					mutex_exit(&sata_mutex);
18711 					return;
18712 				}
18713 				if (rval_set == SATA_RETRY) {
18714 					/*
18715 					 * Setting drive features failed, but
18716 					 * the drive is still accessible,
18717 					 * so emit a warning message before
18718 					 * return.
18719 					 */
18720 					mutex_exit(&SATA_CPORT_INFO(
18721 					    sata_hba_inst,
18722 					    saddr->cport)->cport_mutex);
18723 					goto done;
18724 				}
18725 			}
18726 			/* Fail the drive */
18727 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18728 
18729 			sata_log(sata_hba_inst, CE_WARN,
18730 			    "SATA device at port %d - device failed",
18731 			    saddr->cport);
18732 
18733 			DTRACE_PROBE(port_failed_f);
18734 		}
18735 		/*
18736 		 * No point of retrying - device failed or some other event
18737 		 * processing or already did or will do port info cleanup.
18738 		 * To be safe (HBA may need it),
18739 		 * request clearing device reset condition.
18740 		 */
18741 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18742 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18743 		sdinfo->satadrv_reset_time = 0;
18744 		return;
18745 	}
18746 done:
18747 	/*
18748 	 * If setting of drive features failed, but the drive is still
18749 	 * accessible, emit a warning message.
18750 	 */
18751 	if (rval_set == SATA_RETRY) {
18752 		sata_log(sata_hba_inst, CE_WARN,
18753 		    "SATA device at port %d - desired setting could not be "
18754 		    "restored after reset. Device may not operate as expected.",
18755 		    saddr->cport);
18756 	}
18757 	/*
18758 	 * Raise the flag indicating that the next sata command could
18759 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18760 	 * reset is reported.
18761 	 */
18762 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18763 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18764 		sdinfo->satadrv_reset_time = 0;
18765 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18766 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18767 			sdinfo->satadrv_event_flags &=
18768 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18769 			sdinfo->satadrv_event_flags |=
18770 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18771 		}
18772 	}
18773 }
18774 
18775 
18776 /*
18777  * Port Multiplier Port Device Reset Event processing.
18778  *
18779  * NOTE: This function has to be entered with pmport mutex held. It exits with
18780  * mutex held as well, but can release mutex during the processing.
18781  */
18782 static void
18783 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18784     sata_address_t *saddr)
18785 {
18786 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18787 	sata_drive_info_t *sdinfo = NULL;
18788 	sata_cport_info_t *cportinfo = NULL;
18789 	sata_pmport_info_t *pmportinfo = NULL;
18790 	sata_pmult_info_t *pminfo = NULL;
18791 	sata_device_t sata_device;
18792 	uint8_t cport = saddr->cport;
18793 	uint8_t pmport = saddr->pmport;
18794 	int rval;
18795 
18796 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18797 	    "Processing drive reset at port %d:%d", cport, pmport);
18798 
18799 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18800 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18801 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18802 
18803 	/*
18804 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18805 	 * state, ignore reset event.
18806 	 */
18807 	if (((cportinfo->cport_state &
18808 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18809 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18810 		sdinfo->satadrv_event_flags &=
18811 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18812 		return;
18813 	}
18814 
18815 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18816 		/*
18817 		 * This should not happen - coding error.
18818 		 * But we can recover, so do not panic, just clean up
18819 		 * and if in debug mode, log the message.
18820 		 */
18821 #ifdef SATA_DEBUG
18822 		sata_log(sata_hba_inst, CE_WARN,
18823 		    "sata_process_pmdevice_reset: "
18824 		    "Invalid device type with sdinfo!", NULL);
18825 #endif
18826 		sdinfo->satadrv_event_flags = 0;
18827 		return;
18828 	}
18829 
18830 #ifdef SATA_DEBUG
18831 	if ((sdinfo->satadrv_event_flags &
18832 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18833 		/* Nothing to do */
18834 		/* Something is weird - why we are processing dev reset? */
18835 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18836 		    "No device reset event!!!!", NULL);
18837 
18838 		return;
18839 	}
18840 	if ((sdinfo->satadrv_event_flags &
18841 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18842 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18843 		/* Something is weird - new device reset event */
18844 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18845 		    "Overlapping device reset events!", NULL);
18846 	}
18847 #endif
18848 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18849 	    "Processing port %d:%d device reset", cport, pmport);
18850 
18851 	/* Clear event flag */
18852 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18853 
18854 	/* It seems that we always need to check the port state first */
18855 	sata_device.satadev_rev = SATA_DEVICE_REV;
18856 	sata_device.satadev_addr = *saddr;
18857 	/*
18858 	 * We have to exit mutex, because the HBA probe port function may
18859 	 * block on its own mutex.
18860 	 */
18861 	mutex_exit(&pmportinfo->pmport_mutex);
18862 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18863 	    (SATA_DIP(sata_hba_inst), &sata_device);
18864 	mutex_enter(&pmportinfo->pmport_mutex);
18865 
18866 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18867 	if (rval != SATA_SUCCESS) {
18868 		/* Something went wrong? Fail the port */
18869 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18870 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18871 		    saddr->pmport);
18872 		if (sdinfo != NULL)
18873 			sdinfo->satadrv_event_flags = 0;
18874 		mutex_exit(&pmportinfo->pmport_mutex);
18875 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18876 		    "SATA port %d:%d probing failed",
18877 		    saddr->cport, saddr->pmport));
18878 		mutex_enter(&pmportinfo->pmport_mutex);
18879 		return;
18880 	}
18881 	if ((sata_device.satadev_scr.sstatus  &
18882 	    SATA_PORT_DEVLINK_UP_MASK) !=
18883 	    SATA_PORT_DEVLINK_UP ||
18884 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18885 		/*
18886 		 * No device to process, anymore. Some other event processing
18887 		 * would or have already performed port info cleanup.
18888 		 * To be safe (HBA may need it), request clearing device
18889 		 * reset condition.
18890 		 */
18891 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18892 		    saddr->pmport);
18893 		if (sdinfo != NULL) {
18894 			sdinfo->satadrv_event_flags &=
18895 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18896 			/* must clear flags on cport */
18897 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18898 			    saddr->cport);
18899 			pminfo->pmult_event_flags |=
18900 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18901 		}
18902 		return;
18903 	}
18904 
18905 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18906 	    saddr->pmport);
18907 	if (sdinfo == NULL) {
18908 		return;
18909 	}
18910 	if ((sdinfo->satadrv_event_flags &
18911 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18912 		/*
18913 		 * Start tracking time for device feature restoration and
18914 		 * identification. Save current time (lbolt value).
18915 		 */
18916 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18917 	}
18918 	/* Mark device reset processing as active */
18919 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18920 
18921 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18922 	mutex_exit(&pmportinfo->pmport_mutex);
18923 
18924 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18925 	    SATA_FAILURE) {
18926 		/*
18927 		 * Restoring drive setting failed.
18928 		 * Probe the port first, to check if the port state has changed
18929 		 */
18930 		sata_device.satadev_rev = SATA_DEVICE_REV;
18931 		sata_device.satadev_addr = *saddr;
18932 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18933 
18934 		/* probe port */
18935 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18936 		    (SATA_DIP(sata_hba_inst), &sata_device);
18937 		mutex_enter(&pmportinfo->pmport_mutex);
18938 		if (rval == SATA_SUCCESS &&
18939 		    (sata_device.satadev_state &
18940 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18941 		    (sata_device.satadev_scr.sstatus  &
18942 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18943 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18944 			/*
18945 			 * We may retry this a bit later - in-process reset
18946 			 * condition should be already set.
18947 			 * Track retry time for device identification.
18948 			 */
18949 			if ((pmportinfo->pmport_dev_type &
18950 			    SATA_VALID_DEV_TYPE) != 0 &&
18951 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18952 			    sdinfo->satadrv_reset_time != 0) {
18953 				clock_t cur_time = ddi_get_lbolt();
18954 				/*
18955 				 * If the retry time limit was not
18956 				 * exceeded, retry.
18957 				 */
18958 				if ((cur_time - sdinfo->satadrv_reset_time) <
18959 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18960 					mutex_enter(
18961 					    &sata_hba_inst->satahba_mutex);
18962 					sata_hba_inst->satahba_event_flags |=
18963 					    SATA_EVNT_MAIN;
18964 					mutex_exit(
18965 					    &sata_hba_inst->satahba_mutex);
18966 					mutex_enter(&sata_mutex);
18967 					sata_event_pending |= SATA_EVNT_MAIN;
18968 					mutex_exit(&sata_mutex);
18969 					return;
18970 				}
18971 			}
18972 			/* Fail the drive */
18973 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18974 
18975 			sata_log(sata_hba_inst, CE_WARN,
18976 			    "SATA device at port %d:%d - device failed",
18977 			    saddr->cport, saddr->pmport);
18978 		} else {
18979 			/*
18980 			 * No point of retrying - some other event processing
18981 			 * would or already did port info cleanup.
18982 			 * To be safe (HBA may need it),
18983 			 * request clearing device reset condition.
18984 			 */
18985 			sdinfo->satadrv_event_flags |=
18986 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18987 		}
18988 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18989 		sdinfo->satadrv_reset_time = 0;
18990 		return;
18991 	}
18992 	/*
18993 	 * Raise the flag indicating that the next sata command could
18994 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18995 	 * reset is reported.
18996 	 */
18997 	mutex_enter(&pmportinfo->pmport_mutex);
18998 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18999 		sdinfo->satadrv_reset_time = 0;
19000 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19001 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19002 			sdinfo->satadrv_event_flags &=
19003 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
19004 			/* must clear flags on cport */
19005 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
19006 			    saddr->cport);
19007 			pminfo->pmult_event_flags |=
19008 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19009 		}
19010 	}
19011 }
19012 
19013 /*
19014  * Port Link Events processing.
19015  * Every link established event may involve device reset (due to
19016  * COMRESET signal, equivalent of the hard reset) so arbitrarily
19017  * set device reset event for an attached device (if any).
19018  * If the port is in SHUTDOWN or FAILED state, ignore link events.
19019  *
19020  * The link established event processing varies, depending on the state
19021  * of the target node, HBA hotplugging capabilities, state of the port.
19022  * If the link is not active, the link established event is ignored.
19023  * If HBA cannot detect device attachment and there is no target node,
19024  * the link established event triggers device attach event processing.
19025  * Else, link established event triggers device reset event processing.
19026  *
19027  * The link lost event processing varies, depending on a HBA hotplugging
19028  * capability and the state of the port (link active or not active).
19029  * If the link is active, the lost link event is ignored.
19030  * If HBA cannot detect device removal, the lost link event triggers
19031  * device detached event processing after link lost timeout.
19032  * Else, the event is ignored.
19033  *
19034  * NOTE: Port multiplier ports events are handled by
19035  * sata_process_pmport_link_events();
19036  */
19037 static void
19038 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19039     sata_address_t *saddr)
19040 {
19041 	sata_device_t sata_device;
19042 	sata_cport_info_t *cportinfo;
19043 	sata_drive_info_t *sdinfo;
19044 	uint32_t event_flags;
19045 	int rval;
19046 
19047 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19048 	    "Processing port %d link event(s)", saddr->cport);
19049 
19050 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19051 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19052 	event_flags = cportinfo->cport_event_flags;
19053 
19054 	/* Reset event flags first */
19055 	cportinfo->cport_event_flags &=
19056 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19057 
19058 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19059 	if ((cportinfo->cport_state &
19060 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19061 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19062 		    cport_mutex);
19063 		return;
19064 	}
19065 
19066 	/*
19067 	 * For the sanity sake get current port state.
19068 	 * Set device address only. Other sata_device fields should be
19069 	 * set by HBA driver.
19070 	 */
19071 	sata_device.satadev_rev = SATA_DEVICE_REV;
19072 	sata_device.satadev_addr = *saddr;
19073 	/*
19074 	 * We have to exit mutex, because the HBA probe port function may
19075 	 * block on its own mutex.
19076 	 */
19077 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19078 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19079 	    (SATA_DIP(sata_hba_inst), &sata_device);
19080 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19081 	sata_update_port_info(sata_hba_inst, &sata_device);
19082 	if (rval != SATA_SUCCESS) {
19083 		/* Something went wrong? Fail the port */
19084 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19085 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19086 		    cport_mutex);
19087 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19088 		    "SATA port %d probing failed",
19089 		    saddr->cport));
19090 		/*
19091 		 * We may want to release device info structure, but
19092 		 * it is not necessary.
19093 		 */
19094 		return;
19095 	} else {
19096 		/* port probed successfully */
19097 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19098 	}
19099 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19100 
19101 		if ((sata_device.satadev_scr.sstatus &
19102 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19103 			/* Ignore event */
19104 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19105 			    "Ignoring port %d link established event - "
19106 			    "link down",
19107 			    saddr->cport);
19108 			goto linklost;
19109 		}
19110 
19111 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19112 		    "Processing port %d link established event",
19113 		    saddr->cport);
19114 
19115 		/*
19116 		 * For the sanity sake check if a device is attached - check
19117 		 * return state of a port probing.
19118 		 */
19119 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19120 			/*
19121 			 * HBA port probe indicated that there is a device
19122 			 * attached. Check if the framework had device info
19123 			 * structure attached for this device.
19124 			 */
19125 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19126 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19127 				    NULL);
19128 
19129 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19130 				if ((sdinfo->satadrv_type &
19131 				    SATA_VALID_DEV_TYPE) != 0) {
19132 					/*
19133 					 * Dev info structure is present.
19134 					 * If dev_type is set to known type in
19135 					 * the framework's drive info struct
19136 					 * then the device existed before and
19137 					 * the link was probably lost
19138 					 * momentarily - in such case
19139 					 * we may want to check device
19140 					 * identity.
19141 					 * Identity check is not supported now.
19142 					 *
19143 					 * Link established event
19144 					 * triggers device reset event.
19145 					 */
19146 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19147 					    satadrv_event_flags |=
19148 					    SATA_EVNT_DEVICE_RESET;
19149 				}
19150 			} else if (cportinfo->cport_dev_type ==
19151 			    SATA_DTYPE_NONE) {
19152 				/*
19153 				 * We got new device attached! If HBA does not
19154 				 * generate device attached events, trigger it
19155 				 * here.
19156 				 */
19157 				if (!(SATA_FEATURES(sata_hba_inst) &
19158 				    SATA_CTLF_HOTPLUG)) {
19159 					cportinfo->cport_event_flags |=
19160 					    SATA_EVNT_DEVICE_ATTACHED;
19161 				}
19162 			}
19163 			/* Reset link lost timeout */
19164 			cportinfo->cport_link_lost_time = 0;
19165 		}
19166 	}
19167 linklost:
19168 	if (event_flags & SATA_EVNT_LINK_LOST) {
19169 		if ((sata_device.satadev_scr.sstatus &
19170 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19171 			/* Ignore event */
19172 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19173 			    "Ignoring port %d link lost event - link is up",
19174 			    saddr->cport);
19175 			goto done;
19176 		}
19177 #ifdef SATA_DEBUG
19178 		if (cportinfo->cport_link_lost_time == 0) {
19179 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19180 			    "Processing port %d link lost event",
19181 			    saddr->cport);
19182 		}
19183 #endif
19184 		/*
19185 		 * When HBA cannot generate device attached/detached events,
19186 		 * we need to track link lost time and eventually generate
19187 		 * device detach event.
19188 		 */
19189 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19190 			/* We are tracking link lost time */
19191 			if (cportinfo->cport_link_lost_time == 0) {
19192 				/* save current time (lbolt value) */
19193 				cportinfo->cport_link_lost_time =
19194 				    ddi_get_lbolt();
19195 				/* just keep link lost event */
19196 				cportinfo->cport_event_flags |=
19197 				    SATA_EVNT_LINK_LOST;
19198 			} else {
19199 				clock_t cur_time = ddi_get_lbolt();
19200 				if ((cur_time -
19201 				    cportinfo->cport_link_lost_time) >=
19202 				    drv_usectohz(
19203 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19204 					/* trigger device detach event */
19205 					cportinfo->cport_event_flags |=
19206 					    SATA_EVNT_DEVICE_DETACHED;
19207 					cportinfo->cport_link_lost_time = 0;
19208 					SATADBG1(SATA_DBG_EVENTS,
19209 					    sata_hba_inst,
19210 					    "Triggering port %d "
19211 					    "device detached event",
19212 					    saddr->cport);
19213 				} else {
19214 					/* keep link lost event */
19215 					cportinfo->cport_event_flags |=
19216 					    SATA_EVNT_LINK_LOST;
19217 				}
19218 			}
19219 		}
19220 		/*
19221 		 * We could change port state to disable/delay access to
19222 		 * the attached device until the link is recovered.
19223 		 */
19224 	}
19225 done:
19226 	event_flags = cportinfo->cport_event_flags;
19227 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19228 	if (event_flags != 0) {
19229 		mutex_enter(&sata_hba_inst->satahba_mutex);
19230 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19231 		mutex_exit(&sata_hba_inst->satahba_mutex);
19232 		mutex_enter(&sata_mutex);
19233 		sata_event_pending |= SATA_EVNT_MAIN;
19234 		mutex_exit(&sata_mutex);
19235 	}
19236 }
19237 
19238 /*
19239  * Port Multiplier Port Link Events processing.
19240  */
19241 static void
19242 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19243     sata_address_t *saddr)
19244 {
19245 	sata_device_t sata_device;
19246 	sata_pmport_info_t *pmportinfo = NULL;
19247 	sata_drive_info_t *sdinfo = NULL;
19248 	uint32_t event_flags;
19249 	uint8_t cport = saddr->cport;
19250 	uint8_t pmport = saddr->pmport;
19251 	int rval;
19252 
19253 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19254 	    "Processing port %d:%d link event(s)",
19255 	    cport, pmport);
19256 
19257 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19258 	mutex_enter(&pmportinfo->pmport_mutex);
19259 	event_flags = pmportinfo->pmport_event_flags;
19260 
19261 	/* Reset event flags first */
19262 	pmportinfo->pmport_event_flags &=
19263 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19264 
19265 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19266 	if ((pmportinfo->pmport_state &
19267 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19268 		mutex_exit(&pmportinfo->pmport_mutex);
19269 		return;
19270 	}
19271 
19272 	/*
19273 	 * For the sanity sake get current port state.
19274 	 * Set device address only. Other sata_device fields should be
19275 	 * set by HBA driver.
19276 	 */
19277 	sata_device.satadev_rev = SATA_DEVICE_REV;
19278 	sata_device.satadev_addr = *saddr;
19279 	/*
19280 	 * We have to exit mutex, because the HBA probe port function may
19281 	 * block on its own mutex.
19282 	 */
19283 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19284 	    saddr->pmport));
19285 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19286 	    (SATA_DIP(sata_hba_inst), &sata_device);
19287 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19288 	    saddr->pmport));
19289 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19290 	if (rval != SATA_SUCCESS) {
19291 		/* Something went wrong? Fail the port */
19292 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19293 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19294 		    saddr->pmport));
19295 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19296 		    "SATA port %d:%d probing failed",
19297 		    saddr->cport, saddr->pmport));
19298 		/*
19299 		 * We may want to release device info structure, but
19300 		 * it is not necessary.
19301 		 */
19302 		return;
19303 	} else {
19304 		/* port probed successfully */
19305 		pmportinfo->pmport_state |=
19306 		    SATA_STATE_PROBED | SATA_STATE_READY;
19307 	}
19308 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19309 	    saddr->cport, saddr->pmport));
19310 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19311 	    saddr->cport, saddr->pmport));
19312 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19313 
19314 		if ((sata_device.satadev_scr.sstatus &
19315 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19316 			/* Ignore event */
19317 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19318 			    "Ignoring port %d:%d link established event - "
19319 			    "link down",
19320 			    saddr->cport, saddr->pmport);
19321 			goto linklost;
19322 		}
19323 
19324 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19325 		    "Processing port %d:%d link established event",
19326 		    cport, pmport);
19327 
19328 		/*
19329 		 * For the sanity sake check if a device is attached - check
19330 		 * return state of a port probing.
19331 		 */
19332 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19333 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19334 			/*
19335 			 * HBA port probe indicated that there is a device
19336 			 * attached. Check if the framework had device info
19337 			 * structure attached for this device.
19338 			 */
19339 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19340 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19341 				    NULL);
19342 
19343 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19344 				if ((sdinfo->satadrv_type &
19345 				    SATA_VALID_DEV_TYPE) != 0) {
19346 					/*
19347 					 * Dev info structure is present.
19348 					 * If dev_type is set to known type in
19349 					 * the framework's drive info struct
19350 					 * then the device existed before and
19351 					 * the link was probably lost
19352 					 * momentarily - in such case
19353 					 * we may want to check device
19354 					 * identity.
19355 					 * Identity check is not supported now.
19356 					 *
19357 					 * Link established event
19358 					 * triggers device reset event.
19359 					 */
19360 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19361 					    satadrv_event_flags |=
19362 					    SATA_EVNT_DEVICE_RESET;
19363 				}
19364 			} else if (pmportinfo->pmport_dev_type ==
19365 			    SATA_DTYPE_NONE) {
19366 				/*
19367 				 * We got new device attached! If HBA does not
19368 				 * generate device attached events, trigger it
19369 				 * here.
19370 				 */
19371 				if (!(SATA_FEATURES(sata_hba_inst) &
19372 				    SATA_CTLF_HOTPLUG)) {
19373 					pmportinfo->pmport_event_flags |=
19374 					    SATA_EVNT_DEVICE_ATTACHED;
19375 				}
19376 			}
19377 			/* Reset link lost timeout */
19378 			pmportinfo->pmport_link_lost_time = 0;
19379 		}
19380 	}
19381 linklost:
19382 	if (event_flags & SATA_EVNT_LINK_LOST) {
19383 #ifdef SATA_DEBUG
19384 		if (pmportinfo->pmport_link_lost_time == 0) {
19385 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19386 			    "Processing port %d:%d link lost event",
19387 			    saddr->cport, saddr->pmport);
19388 		}
19389 #endif
19390 		if ((sata_device.satadev_scr.sstatus &
19391 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19392 			/* Ignore event */
19393 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19394 			    "Ignoring port %d:%d link lost event - link is up",
19395 			    saddr->cport, saddr->pmport);
19396 			goto done;
19397 		}
19398 		/*
19399 		 * When HBA cannot generate device attached/detached events,
19400 		 * we need to track link lost time and eventually generate
19401 		 * device detach event.
19402 		 */
19403 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19404 			/* We are tracking link lost time */
19405 			if (pmportinfo->pmport_link_lost_time == 0) {
19406 				/* save current time (lbolt value) */
19407 				pmportinfo->pmport_link_lost_time =
19408 				    ddi_get_lbolt();
19409 				/* just keep link lost event */
19410 				pmportinfo->pmport_event_flags |=
19411 				    SATA_EVNT_LINK_LOST;
19412 			} else {
19413 				clock_t cur_time = ddi_get_lbolt();
19414 				if ((cur_time -
19415 				    pmportinfo->pmport_link_lost_time) >=
19416 				    drv_usectohz(
19417 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19418 					/* trigger device detach event */
19419 					pmportinfo->pmport_event_flags |=
19420 					    SATA_EVNT_DEVICE_DETACHED;
19421 					pmportinfo->pmport_link_lost_time = 0;
19422 					SATADBG2(SATA_DBG_EVENTS,
19423 					    sata_hba_inst,
19424 					    "Triggering port %d:%d "
19425 					    "device detached event",
19426 					    saddr->cport, saddr->pmport);
19427 				} else {
19428 					/* keep link lost event */
19429 					pmportinfo->pmport_event_flags |=
19430 					    SATA_EVNT_LINK_LOST;
19431 				}
19432 			}
19433 		}
19434 		/*
19435 		 * We could change port state to disable/delay access to
19436 		 * the attached device until the link is recovered.
19437 		 */
19438 	}
19439 done:
19440 	event_flags = pmportinfo->pmport_event_flags;
19441 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19442 	    saddr->pmport));
19443 	if (event_flags != 0) {
19444 		mutex_enter(&sata_hba_inst->satahba_mutex);
19445 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19446 		mutex_exit(&sata_hba_inst->satahba_mutex);
19447 		mutex_enter(&sata_mutex);
19448 		sata_event_pending |= SATA_EVNT_MAIN;
19449 		mutex_exit(&sata_mutex);
19450 	}
19451 }
19452 
19453 /*
19454  * Device Detached Event processing.
19455  * Port is probed to find if a device is really gone. If so,
19456  * the device info structure is detached from the SATA port info structure
19457  * and released.
19458  * Port status is updated.
19459  *
19460  * NOTE: Port multiplier ports events are handled by
19461  * sata_process_pmdevice_detached()
19462  */
19463 static void
19464 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19465     sata_address_t *saddr)
19466 {
19467 	sata_cport_info_t *cportinfo;
19468 	sata_pmport_info_t *pmportinfo;
19469 	sata_drive_info_t *sdevinfo;
19470 	sata_device_t sata_device;
19471 	sata_address_t pmport_addr;
19472 	char name[16];
19473 	uint8_t cport = saddr->cport;
19474 	int npmport;
19475 	int rval;
19476 
19477 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19478 	    "Processing port %d device detached", saddr->cport);
19479 
19480 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19481 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19482 	/* Clear event flag */
19483 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19484 
19485 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19486 	if ((cportinfo->cport_state &
19487 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19488 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19489 		    cport_mutex);
19490 		return;
19491 	}
19492 	/* For sanity, re-probe the port */
19493 	sata_device.satadev_rev = SATA_DEVICE_REV;
19494 	sata_device.satadev_addr = *saddr;
19495 
19496 	/*
19497 	 * We have to exit mutex, because the HBA probe port function may
19498 	 * block on its own mutex.
19499 	 */
19500 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19501 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19502 	    (SATA_DIP(sata_hba_inst), &sata_device);
19503 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19504 	sata_update_port_info(sata_hba_inst, &sata_device);
19505 	if (rval != SATA_SUCCESS) {
19506 		/* Something went wrong? Fail the port */
19507 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19508 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19509 		    cport_mutex);
19510 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19511 		    "SATA port %d probing failed",
19512 		    saddr->cport));
19513 		/*
19514 		 * We may want to release device info structure, but
19515 		 * it is not necessary.
19516 		 */
19517 		return;
19518 	} else {
19519 		/* port probed successfully */
19520 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19521 	}
19522 	/*
19523 	 * Check if a device is still attached. For sanity, check also
19524 	 * link status - if no link, there is no device.
19525 	 */
19526 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19527 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19528 	    SATA_DTYPE_NONE) {
19529 		/*
19530 		 * Device is still attached - ignore detach event.
19531 		 */
19532 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19533 		    cport_mutex);
19534 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19535 		    "Ignoring detach - device still attached to port %d",
19536 		    sata_device.satadev_addr.cport);
19537 		return;
19538 	}
19539 	/*
19540 	 * We need to detach and release device info structure here
19541 	 */
19542 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19543 		/*
19544 		 * A port-multiplier is removed.
19545 		 *
19546 		 * Calling sata_process_pmdevice_detached() does not work
19547 		 * here. The port multiplier is gone, so we cannot probe
19548 		 * sub-port any more and all pmult-related data structure must
19549 		 * be de-allocated immediately. Following structure of every
19550 		 * implemented sub-port behind the pmult are required to
19551 		 * released.
19552 		 *
19553 		 *   - attachment point
19554 		 *   - target node
19555 		 *   - sata_drive_info
19556 		 *   - sata_pmport_info
19557 		 */
19558 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19559 		    cport); npmport ++) {
19560 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19561 			    sata_hba_inst,
19562 			    "Detaching target node at port %d:%d",
19563 			    cport, npmport);
19564 
19565 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19566 
19567 			/* Remove attachment point. */
19568 			name[0] = '\0';
19569 			(void) sprintf(name, "%d.%d", cport, npmport);
19570 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19571 			sata_log(sata_hba_inst, CE_NOTE,
19572 			    "Remove attachment point of port %d:%d",
19573 			    cport, npmport);
19574 
19575 			/* Remove target node */
19576 			pmport_addr.cport = cport;
19577 			pmport_addr.pmport = (uint8_t)npmport;
19578 			pmport_addr.qual = SATA_ADDR_PMPORT;
19579 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19580 
19581 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19582 
19583 			/* Release sata_pmport_info & sata_drive_info. */
19584 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19585 			    cport, npmport);
19586 			ASSERT(pmportinfo != NULL);
19587 
19588 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19589 			if (sdevinfo != NULL) {
19590 				(void) kmem_free((void *) sdevinfo,
19591 				    sizeof (sata_drive_info_t));
19592 			}
19593 
19594 			/* Release sata_pmport_info at last */
19595 			(void) kmem_free((void *) pmportinfo,
19596 			    sizeof (sata_pmport_info_t));
19597 		}
19598 
19599 		/* Finally, release sata_pmult_info */
19600 		(void) kmem_free((void *)
19601 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19602 		    sizeof (sata_pmult_info_t));
19603 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19604 
19605 		sata_log(sata_hba_inst, CE_WARN,
19606 		    "SATA port-multiplier detached at port %d", cport);
19607 
19608 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19609 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19610 		    saddr->cport)->cport_mutex);
19611 	} else {
19612 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19613 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19614 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19615 			(void) kmem_free((void *)sdevinfo,
19616 			    sizeof (sata_drive_info_t));
19617 		}
19618 		sata_log(sata_hba_inst, CE_WARN,
19619 		    "SATA device detached at port %d", cport);
19620 
19621 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19622 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19623 		    saddr->cport)->cport_mutex);
19624 
19625 		/*
19626 		 * Try to offline a device and remove target node
19627 		 * if it still exists
19628 		 */
19629 		sata_remove_target_node(sata_hba_inst, saddr);
19630 	}
19631 
19632 
19633 	/*
19634 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19635 	 * with the hint: SE_HINT_REMOVE
19636 	 */
19637 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19638 }
19639 
19640 /*
19641  * Port Multiplier Port Device Deattached Event processing.
19642  *
19643  * NOTE: No Mutex should be hold.
19644  */
19645 static void
19646 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19647     sata_address_t *saddr)
19648 {
19649 	sata_pmport_info_t *pmportinfo;
19650 	sata_drive_info_t *sdevinfo;
19651 	sata_device_t sata_device;
19652 	int rval;
19653 	uint8_t cport, pmport;
19654 
19655 	cport = saddr->cport;
19656 	pmport = saddr->pmport;
19657 
19658 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19659 	    "Processing port %d:%d device detached",
19660 	    cport, pmport);
19661 
19662 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19663 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19664 
19665 	/* Clear event flag */
19666 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19667 
19668 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19669 	if ((pmportinfo->pmport_state &
19670 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19671 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19672 		return;
19673 	}
19674 	/* For sanity, re-probe the port */
19675 	sata_device.satadev_rev = SATA_DEVICE_REV;
19676 	sata_device.satadev_addr = *saddr;
19677 
19678 	/*
19679 	 * We have to exit mutex, because the HBA probe port function may
19680 	 * block on its own mutex.
19681 	 */
19682 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19683 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19684 	    (SATA_DIP(sata_hba_inst), &sata_device);
19685 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19686 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19687 	if (rval != SATA_SUCCESS) {
19688 		/* Something went wrong? Fail the port */
19689 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19690 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19691 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19692 		    "SATA port %d:%d probing failed",
19693 		    saddr->pmport));
19694 		/*
19695 		 * We may want to release device info structure, but
19696 		 * it is not necessary.
19697 		 */
19698 		return;
19699 	} else {
19700 		/* port probed successfully */
19701 		pmportinfo->pmport_state |=
19702 		    SATA_STATE_PROBED | SATA_STATE_READY;
19703 	}
19704 	/*
19705 	 * Check if a device is still attached. For sanity, check also
19706 	 * link status - if no link, there is no device.
19707 	 */
19708 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19709 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19710 	    SATA_DTYPE_NONE) {
19711 		/*
19712 		 * Device is still attached - ignore detach event.
19713 		 */
19714 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19715 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19716 		    "Ignoring detach - device still attached to port %d",
19717 		    sata_device.satadev_addr.pmport);
19718 		return;
19719 	}
19720 	/*
19721 	 * We need to detach and release device info structure here
19722 	 */
19723 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19724 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19725 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19726 		(void) kmem_free((void *)sdevinfo,
19727 		    sizeof (sata_drive_info_t));
19728 	}
19729 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19730 	/*
19731 	 * Device cannot be reached anymore, even if the target node may be
19732 	 * still present.
19733 	 */
19734 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19735 
19736 	/*
19737 	 * Try to offline a device and remove target node if it still exists
19738 	 */
19739 	sata_remove_target_node(sata_hba_inst, saddr);
19740 
19741 	/*
19742 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19743 	 * with the hint: SE_HINT_REMOVE
19744 	 */
19745 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19746 }
19747 
19748 
19749 /*
19750  * Device Attached Event processing.
19751  * Port state is checked to verify that a device is really attached. If so,
19752  * the device info structure is created and attached to the SATA port info
19753  * structure.
19754  *
19755  * If attached device cannot be identified or set-up, the retry for the
19756  * attach processing is set-up. Subsequent daemon run would try again to
19757  * identify the device, until the time limit is reached
19758  * (SATA_DEV_IDENTIFY_TIMEOUT).
19759  *
19760  * This function cannot be called in interrupt context (it may sleep).
19761  *
19762  * NOTE: Port multiplier ports events are handled by
19763  * sata_process_pmdevice_attached()
19764  */
19765 static void
19766 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19767     sata_address_t *saddr)
19768 {
19769 	sata_cport_info_t *cportinfo = NULL;
19770 	sata_drive_info_t *sdevinfo = NULL;
19771 	sata_pmult_info_t *pmultinfo = NULL;
19772 	sata_pmport_info_t *pmportinfo = NULL;
19773 	sata_device_t sata_device;
19774 	dev_info_t *tdip;
19775 	uint32_t event_flags = 0, pmult_event_flags = 0;
19776 	int rval;
19777 	int npmport;
19778 
19779 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19780 	    "Processing port %d device attached", saddr->cport);
19781 
19782 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19783 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19784 
19785 	/* Clear attach event flag first */
19786 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19787 
19788 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19789 	if ((cportinfo->cport_state &
19790 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19791 		cportinfo->cport_dev_attach_time = 0;
19792 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19793 		    cport_mutex);
19794 		return;
19795 	}
19796 
19797 	/*
19798 	 * If the sata_drive_info structure is found attached to the port info,
19799 	 * despite the fact the device was removed and now it is re-attached,
19800 	 * the old drive info structure was not removed.
19801 	 * Arbitrarily release device info structure.
19802 	 */
19803 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19804 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19805 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19806 		(void) kmem_free((void *)sdevinfo,
19807 		    sizeof (sata_drive_info_t));
19808 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19809 		    "Arbitrarily detaching old device info.", NULL);
19810 	}
19811 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19812 
19813 	/* For sanity, re-probe the port */
19814 	sata_device.satadev_rev = SATA_DEVICE_REV;
19815 	sata_device.satadev_addr = *saddr;
19816 
19817 	/*
19818 	 * We have to exit mutex, because the HBA probe port function may
19819 	 * block on its own mutex.
19820 	 */
19821 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19822 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19823 	    (SATA_DIP(sata_hba_inst), &sata_device);
19824 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19825 	sata_update_port_info(sata_hba_inst, &sata_device);
19826 	if (rval != SATA_SUCCESS) {
19827 		/* Something went wrong? Fail the port */
19828 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19829 		cportinfo->cport_dev_attach_time = 0;
19830 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19831 		    cport_mutex);
19832 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19833 		    "SATA port %d probing failed",
19834 		    saddr->cport));
19835 		return;
19836 	} else {
19837 		/* port probed successfully */
19838 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19839 	}
19840 	/*
19841 	 * Check if a device is still attached. For sanity, check also
19842 	 * link status - if no link, there is no device.
19843 	 */
19844 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19845 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19846 	    SATA_DTYPE_NONE) {
19847 		/*
19848 		 * No device - ignore attach event.
19849 		 */
19850 		cportinfo->cport_dev_attach_time = 0;
19851 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19852 		    cport_mutex);
19853 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19854 		    "Ignoring attach - no device connected to port %d",
19855 		    sata_device.satadev_addr.cport);
19856 		return;
19857 	}
19858 
19859 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19860 	/*
19861 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19862 	 * with the hint: SE_HINT_INSERT
19863 	 */
19864 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19865 
19866 	/*
19867 	 * Port reprobing will take care of the creation of the device
19868 	 * info structure and determination of the device type.
19869 	 */
19870 	sata_device.satadev_addr = *saddr;
19871 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19872 	    SATA_DEV_IDENTIFY_NORETRY);
19873 
19874 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19875 	    cport_mutex);
19876 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19877 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19878 		/* Some device is attached to the port */
19879 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19880 			/*
19881 			 * A device was not successfully attached.
19882 			 * Track retry time for device identification.
19883 			 */
19884 			if (cportinfo->cport_dev_attach_time != 0) {
19885 				clock_t cur_time = ddi_get_lbolt();
19886 				/*
19887 				 * If the retry time limit was not exceeded,
19888 				 * reinstate attach event.
19889 				 */
19890 				if ((cur_time -
19891 				    cportinfo->cport_dev_attach_time) <
19892 				    drv_usectohz(
19893 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19894 					/* OK, restore attach event */
19895 					cportinfo->cport_event_flags |=
19896 					    SATA_EVNT_DEVICE_ATTACHED;
19897 				} else {
19898 					/* Timeout - cannot identify device */
19899 					cportinfo->cport_dev_attach_time = 0;
19900 					sata_log(sata_hba_inst,
19901 					    CE_WARN,
19902 					    "Could not identify SATA device "
19903 					    "at port %d",
19904 					    saddr->cport);
19905 				}
19906 			} else {
19907 				/*
19908 				 * Start tracking time for device
19909 				 * identification.
19910 				 * Save current time (lbolt value).
19911 				 */
19912 				cportinfo->cport_dev_attach_time =
19913 				    ddi_get_lbolt();
19914 				/* Restore attach event */
19915 				cportinfo->cport_event_flags |=
19916 				    SATA_EVNT_DEVICE_ATTACHED;
19917 			}
19918 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19919 			cportinfo->cport_dev_attach_time = 0;
19920 			sata_log(sata_hba_inst, CE_NOTE,
19921 			    "SATA port-multiplier detected at port %d",
19922 			    saddr->cport);
19923 
19924 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19925 				/* Log the info of new port multiplier */
19926 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19927 				    saddr->cport)->cport_mutex);
19928 				sata_show_pmult_info(sata_hba_inst,
19929 				    &sata_device);
19930 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19931 				    saddr->cport)->cport_mutex);
19932 			}
19933 
19934 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19935 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19936 			for (npmport = 0; npmport <
19937 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19938 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19939 				    saddr->cport, npmport);
19940 				ASSERT(pmportinfo != NULL);
19941 
19942 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19943 				    saddr->cport)->cport_mutex);
19944 				mutex_enter(&pmportinfo->pmport_mutex);
19945 				/* Marked all pmports with link events. */
19946 				pmportinfo->pmport_event_flags =
19947 				    SATA_EVNT_LINK_ESTABLISHED;
19948 				pmult_event_flags |=
19949 				    pmportinfo->pmport_event_flags;
19950 				mutex_exit(&pmportinfo->pmport_mutex);
19951 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19952 				    saddr->cport)->cport_mutex);
19953 			}
19954 			/* Auto-online is not available for PMult now. */
19955 
19956 		} else {
19957 			/*
19958 			 * If device was successfully attached, the subsequent
19959 			 * action depends on a state of the
19960 			 * sata_auto_online variable. If it is set to zero.
19961 			 * an explicit 'configure' command will be needed to
19962 			 * configure it. If its value is non-zero, we will
19963 			 * attempt to online (configure) the device.
19964 			 * First, log the message indicating that a device
19965 			 * was attached.
19966 			 */
19967 			cportinfo->cport_dev_attach_time = 0;
19968 			sata_log(sata_hba_inst, CE_WARN,
19969 			    "SATA device detected at port %d", saddr->cport);
19970 
19971 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19972 				sata_drive_info_t new_sdinfo;
19973 
19974 				/* Log device info data */
19975 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19976 				    cportinfo));
19977 				sata_show_drive_info(sata_hba_inst,
19978 				    &new_sdinfo);
19979 			}
19980 
19981 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19982 			    saddr->cport)->cport_mutex);
19983 
19984 			/*
19985 			 * Make sure that there is no target node for that
19986 			 * device. If so, release it. It should not happen,
19987 			 * unless we had problem removing the node when
19988 			 * device was detached.
19989 			 */
19990 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19991 			    saddr->cport, saddr->pmport);
19992 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19993 			    saddr->cport)->cport_mutex);
19994 			if (tdip != NULL) {
19995 
19996 #ifdef SATA_DEBUG
19997 				if ((cportinfo->cport_event_flags &
19998 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19999 					sata_log(sata_hba_inst, CE_WARN,
20000 					    "sata_process_device_attached: "
20001 					    "old device target node exists!");
20002 #endif
20003 				/*
20004 				 * target node exists - try to unconfigure
20005 				 * device and remove the node.
20006 				 */
20007 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20008 				    saddr->cport)->cport_mutex);
20009 				rval = ndi_devi_offline(tdip,
20010 				    NDI_DEVI_REMOVE);
20011 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20012 				    saddr->cport)->cport_mutex);
20013 
20014 				if (rval == NDI_SUCCESS) {
20015 					cportinfo->cport_event_flags &=
20016 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20017 					cportinfo->cport_tgtnode_clean = B_TRUE;
20018 				} else {
20019 					/*
20020 					 * PROBLEM - the target node remained
20021 					 * and it belongs to a previously
20022 					 * attached device.
20023 					 * This happens when the file was open
20024 					 * or the node was waiting for
20025 					 * resources at the time the
20026 					 * associated device was removed.
20027 					 * Instruct event daemon to retry the
20028 					 * cleanup later.
20029 					 */
20030 					sata_log(sata_hba_inst,
20031 					    CE_WARN,
20032 					    "Application(s) accessing "
20033 					    "previously attached SATA "
20034 					    "device have to release "
20035 					    "it before newly inserted "
20036 					    "device can be made accessible.",
20037 					    saddr->cport);
20038 					cportinfo->cport_event_flags |=
20039 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20040 					cportinfo->cport_tgtnode_clean =
20041 					    B_FALSE;
20042 				}
20043 			}
20044 			if (sata_auto_online != 0) {
20045 				cportinfo->cport_event_flags |=
20046 				    SATA_EVNT_AUTOONLINE_DEVICE;
20047 			}
20048 
20049 		}
20050 	} else {
20051 		cportinfo->cport_dev_attach_time = 0;
20052 	}
20053 
20054 	event_flags = cportinfo->cport_event_flags;
20055 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20056 	if (event_flags != 0 || pmult_event_flags != 0) {
20057 		mutex_enter(&sata_hba_inst->satahba_mutex);
20058 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20059 		mutex_exit(&sata_hba_inst->satahba_mutex);
20060 		mutex_enter(&sata_mutex);
20061 		sata_event_pending |= SATA_EVNT_MAIN;
20062 		mutex_exit(&sata_mutex);
20063 	}
20064 }
20065 
20066 /*
20067  * Port Multiplier Port Device Attached Event processing.
20068  *
20069  * NOTE: No Mutex should be hold.
20070  */
20071 static void
20072 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20073     sata_address_t *saddr)
20074 {
20075 	sata_pmport_info_t *pmportinfo;
20076 	sata_drive_info_t *sdinfo;
20077 	sata_device_t sata_device;
20078 	dev_info_t *tdip;
20079 	uint32_t event_flags;
20080 	uint8_t cport = saddr->cport;
20081 	uint8_t pmport = saddr->pmport;
20082 	int rval;
20083 
20084 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20085 	    "Processing port %d:%d device attached", cport, pmport);
20086 
20087 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20088 
20089 	mutex_enter(&pmportinfo->pmport_mutex);
20090 
20091 	/* Clear attach event flag first */
20092 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20093 
20094 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20095 	if ((pmportinfo->pmport_state &
20096 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20097 		pmportinfo->pmport_dev_attach_time = 0;
20098 		mutex_exit(&pmportinfo->pmport_mutex);
20099 		return;
20100 	}
20101 
20102 	/*
20103 	 * If the sata_drive_info structure is found attached to the port info,
20104 	 * despite the fact the device was removed and now it is re-attached,
20105 	 * the old drive info structure was not removed.
20106 	 * Arbitrarily release device info structure.
20107 	 */
20108 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20109 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20110 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20111 		(void) kmem_free((void *)sdinfo,
20112 		    sizeof (sata_drive_info_t));
20113 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20114 		    "Arbitrarily detaching old device info.", NULL);
20115 	}
20116 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20117 
20118 	/* For sanity, re-probe the port */
20119 	sata_device.satadev_rev = SATA_DEVICE_REV;
20120 	sata_device.satadev_addr = *saddr;
20121 
20122 	/*
20123 	 * We have to exit mutex, because the HBA probe port function may
20124 	 * block on its own mutex.
20125 	 */
20126 	mutex_exit(&pmportinfo->pmport_mutex);
20127 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20128 	    (SATA_DIP(sata_hba_inst), &sata_device);
20129 	mutex_enter(&pmportinfo->pmport_mutex);
20130 
20131 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20132 	if (rval != SATA_SUCCESS) {
20133 		/* Something went wrong? Fail the port */
20134 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20135 		pmportinfo->pmport_dev_attach_time = 0;
20136 		mutex_exit(&pmportinfo->pmport_mutex);
20137 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20138 		    "SATA port %d:%d probing failed", cport, pmport));
20139 		return;
20140 	} else {
20141 		/* pmport probed successfully */
20142 		pmportinfo->pmport_state |=
20143 		    SATA_STATE_PROBED | SATA_STATE_READY;
20144 	}
20145 	/*
20146 	 * Check if a device is still attached. For sanity, check also
20147 	 * link status - if no link, there is no device.
20148 	 */
20149 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20150 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20151 	    SATA_DTYPE_NONE) {
20152 		/*
20153 		 * No device - ignore attach event.
20154 		 */
20155 		pmportinfo->pmport_dev_attach_time = 0;
20156 		mutex_exit(&pmportinfo->pmport_mutex);
20157 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20158 		    "Ignoring attach - no device connected to port %d:%d",
20159 		    cport, pmport);
20160 		return;
20161 	}
20162 
20163 	mutex_exit(&pmportinfo->pmport_mutex);
20164 	/*
20165 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20166 	 * with the hint: SE_HINT_INSERT
20167 	 */
20168 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20169 
20170 	/*
20171 	 * Port reprobing will take care of the creation of the device
20172 	 * info structure and determination of the device type.
20173 	 */
20174 	sata_device.satadev_addr = *saddr;
20175 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20176 	    SATA_DEV_IDENTIFY_NORETRY);
20177 
20178 	mutex_enter(&pmportinfo->pmport_mutex);
20179 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20180 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20181 		/* Some device is attached to the port */
20182 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20183 			/*
20184 			 * A device was not successfully attached.
20185 			 * Track retry time for device identification.
20186 			 */
20187 			if (pmportinfo->pmport_dev_attach_time != 0) {
20188 				clock_t cur_time = ddi_get_lbolt();
20189 				/*
20190 				 * If the retry time limit was not exceeded,
20191 				 * reinstate attach event.
20192 				 */
20193 				if ((cur_time -
20194 				    pmportinfo->pmport_dev_attach_time) <
20195 				    drv_usectohz(
20196 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20197 					/* OK, restore attach event */
20198 					pmportinfo->pmport_event_flags |=
20199 					    SATA_EVNT_DEVICE_ATTACHED;
20200 				} else {
20201 					/* Timeout - cannot identify device */
20202 					pmportinfo->pmport_dev_attach_time = 0;
20203 					sata_log(sata_hba_inst, CE_WARN,
20204 					    "Could not identify SATA device "
20205 					    "at port %d:%d",
20206 					    cport, pmport);
20207 				}
20208 			} else {
20209 				/*
20210 				 * Start tracking time for device
20211 				 * identification.
20212 				 * Save current time (lbolt value).
20213 				 */
20214 				pmportinfo->pmport_dev_attach_time =
20215 				    ddi_get_lbolt();
20216 				/* Restore attach event */
20217 				pmportinfo->pmport_event_flags |=
20218 				    SATA_EVNT_DEVICE_ATTACHED;
20219 			}
20220 		} else {
20221 			/*
20222 			 * If device was successfully attached, the subsequent
20223 			 * action depends on a state of the
20224 			 * sata_auto_online variable. If it is set to zero.
20225 			 * an explicit 'configure' command will be needed to
20226 			 * configure it. If its value is non-zero, we will
20227 			 * attempt to online (configure) the device.
20228 			 * First, log the message indicating that a device
20229 			 * was attached.
20230 			 */
20231 			pmportinfo->pmport_dev_attach_time = 0;
20232 			sata_log(sata_hba_inst, CE_WARN,
20233 			    "SATA device detected at port %d:%d",
20234 			    cport, pmport);
20235 
20236 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20237 				sata_drive_info_t new_sdinfo;
20238 
20239 				/* Log device info data */
20240 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20241 				    pmportinfo));
20242 				sata_show_drive_info(sata_hba_inst,
20243 				    &new_sdinfo);
20244 			}
20245 
20246 			mutex_exit(&pmportinfo->pmport_mutex);
20247 
20248 			/*
20249 			 * Make sure that there is no target node for that
20250 			 * device. If so, release it. It should not happen,
20251 			 * unless we had problem removing the node when
20252 			 * device was detached.
20253 			 */
20254 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20255 			    saddr->cport, saddr->pmport);
20256 			mutex_enter(&pmportinfo->pmport_mutex);
20257 			if (tdip != NULL) {
20258 
20259 #ifdef SATA_DEBUG
20260 				if ((pmportinfo->pmport_event_flags &
20261 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20262 					sata_log(sata_hba_inst, CE_WARN,
20263 					    "sata_process_device_attached: "
20264 					    "old device target node exists!");
20265 #endif
20266 				/*
20267 				 * target node exists - try to unconfigure
20268 				 * device and remove the node.
20269 				 */
20270 				mutex_exit(&pmportinfo->pmport_mutex);
20271 				rval = ndi_devi_offline(tdip,
20272 				    NDI_DEVI_REMOVE);
20273 				mutex_enter(&pmportinfo->pmport_mutex);
20274 
20275 				if (rval == NDI_SUCCESS) {
20276 					pmportinfo->pmport_event_flags &=
20277 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20278 					pmportinfo->pmport_tgtnode_clean =
20279 					    B_TRUE;
20280 				} else {
20281 					/*
20282 					 * PROBLEM - the target node remained
20283 					 * and it belongs to a previously
20284 					 * attached device.
20285 					 * This happens when the file was open
20286 					 * or the node was waiting for
20287 					 * resources at the time the
20288 					 * associated device was removed.
20289 					 * Instruct event daemon to retry the
20290 					 * cleanup later.
20291 					 */
20292 					sata_log(sata_hba_inst,
20293 					    CE_WARN,
20294 					    "Application(s) accessing "
20295 					    "previously attached SATA "
20296 					    "device have to release "
20297 					    "it before newly inserted "
20298 					    "device can be made accessible."
20299 					    "at port %d:%d",
20300 					    cport, pmport);
20301 					pmportinfo->pmport_event_flags |=
20302 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20303 					pmportinfo->pmport_tgtnode_clean =
20304 					    B_FALSE;
20305 				}
20306 			}
20307 			if (sata_auto_online != 0) {
20308 				pmportinfo->pmport_event_flags |=
20309 				    SATA_EVNT_AUTOONLINE_DEVICE;
20310 			}
20311 
20312 		}
20313 	} else {
20314 		pmportinfo->pmport_dev_attach_time = 0;
20315 	}
20316 
20317 	event_flags = pmportinfo->pmport_event_flags;
20318 	mutex_exit(&pmportinfo->pmport_mutex);
20319 	if (event_flags != 0) {
20320 		mutex_enter(&sata_hba_inst->satahba_mutex);
20321 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20322 		mutex_exit(&sata_hba_inst->satahba_mutex);
20323 		mutex_enter(&sata_mutex);
20324 		sata_event_pending |= SATA_EVNT_MAIN;
20325 		mutex_exit(&sata_mutex);
20326 	}
20327 
20328 	/* clear the reset_in_progress events */
20329 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20330 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20331 			/* must clear flags on cport */
20332 			sata_pmult_info_t *pminfo =
20333 			    SATA_PMULT_INFO(sata_hba_inst,
20334 			    saddr->cport);
20335 			pminfo->pmult_event_flags |=
20336 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20337 		}
20338 	}
20339 }
20340 
20341 /*
20342  * Device Target Node Cleanup Event processing.
20343  * If the target node associated with a sata port device is in
20344  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20345  * If the target node cannot be removed, the event flag is left intact,
20346  * so that event daemon may re-run this function later.
20347  *
20348  * This function cannot be called in interrupt context (it may sleep).
20349  *
20350  * NOTE: Processes cport events only, not port multiplier ports.
20351  */
20352 static void
20353 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20354     sata_address_t *saddr)
20355 {
20356 	sata_cport_info_t *cportinfo;
20357 	dev_info_t *tdip;
20358 
20359 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20360 	    "Processing port %d device target node cleanup", saddr->cport);
20361 
20362 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20363 
20364 	/*
20365 	 * Check if there is target node for that device and it is in the
20366 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20367 	 */
20368 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20369 	    saddr->pmport);
20370 	if (tdip != NULL) {
20371 		/*
20372 		 * target node exists - check if it is target node of
20373 		 * a removed device.
20374 		 */
20375 		if (sata_check_device_removed(tdip) == B_TRUE) {
20376 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20377 			    "sata_process_target_node_cleanup: "
20378 			    "old device target node exists!", NULL);
20379 			/*
20380 			 * Unconfigure and remove the target node
20381 			 */
20382 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20383 			    NDI_SUCCESS) {
20384 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20385 				    saddr->cport)->cport_mutex);
20386 				cportinfo->cport_event_flags &=
20387 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20388 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20389 				    saddr->cport)->cport_mutex);
20390 				return;
20391 			}
20392 			/*
20393 			 * Event daemon will retry the cleanup later.
20394 			 */
20395 			mutex_enter(&sata_hba_inst->satahba_mutex);
20396 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20397 			mutex_exit(&sata_hba_inst->satahba_mutex);
20398 			mutex_enter(&sata_mutex);
20399 			sata_event_pending |= SATA_EVNT_MAIN;
20400 			mutex_exit(&sata_mutex);
20401 		}
20402 	} else {
20403 		if (saddr->qual == SATA_ADDR_CPORT ||
20404 		    saddr->qual == SATA_ADDR_DCPORT) {
20405 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20406 			    saddr->cport)->cport_mutex);
20407 			cportinfo->cport_event_flags &=
20408 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20409 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20410 			    saddr->cport)->cport_mutex);
20411 		} else {
20412 			/* sanity check */
20413 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20414 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20415 			    saddr->cport) == NULL)
20416 				return;
20417 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20418 			    saddr->pmport) == NULL)
20419 				return;
20420 
20421 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20422 			    saddr->cport, saddr->pmport)->pmport_mutex);
20423 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20424 			    saddr->pmport)->pmport_event_flags &=
20425 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20426 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20427 			    saddr->cport, saddr->pmport)->pmport_mutex);
20428 		}
20429 	}
20430 }
20431 
20432 /*
20433  * Device AutoOnline Event processing.
20434  * If attached device is to be onlined, an attempt is made to online this
20435  * device, but only if there is no lingering (old) target node present.
20436  * If the device cannot be onlined, the event flag is left intact,
20437  * so that event daemon may re-run this function later.
20438  *
20439  * This function cannot be called in interrupt context (it may sleep).
20440  *
20441  * NOTE: Processes cport events only, not port multiplier ports.
20442  */
20443 static void
20444 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20445     sata_address_t *saddr)
20446 {
20447 	sata_cport_info_t *cportinfo;
20448 	sata_drive_info_t *sdinfo;
20449 	sata_device_t sata_device;
20450 	dev_info_t *tdip;
20451 
20452 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20453 	    "Processing port %d attached device auto-onlining", saddr->cport);
20454 
20455 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20456 
20457 	/*
20458 	 * Check if device is present and recognized. If not, reset event.
20459 	 */
20460 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20461 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20462 		/* Nothing to online */
20463 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20464 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20465 		    saddr->cport)->cport_mutex);
20466 		return;
20467 	}
20468 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20469 
20470 	/*
20471 	 * Check if there is target node for this device and if it is in the
20472 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20473 	 * the event for later processing.
20474 	 */
20475 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20476 	    saddr->pmport);
20477 	if (tdip != NULL) {
20478 		/*
20479 		 * target node exists - check if it is target node of
20480 		 * a removed device.
20481 		 */
20482 		if (sata_check_device_removed(tdip) == B_TRUE) {
20483 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20484 			    "sata_process_device_autoonline: "
20485 			    "old device target node exists!", NULL);
20486 			/*
20487 			 * Event daemon will retry device onlining later.
20488 			 */
20489 			mutex_enter(&sata_hba_inst->satahba_mutex);
20490 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20491 			mutex_exit(&sata_hba_inst->satahba_mutex);
20492 			mutex_enter(&sata_mutex);
20493 			sata_event_pending |= SATA_EVNT_MAIN;
20494 			mutex_exit(&sata_mutex);
20495 			return;
20496 		}
20497 		/*
20498 		 * If the target node is not in the 'removed" state, assume
20499 		 * that it belongs to this device. There is nothing more to do,
20500 		 * but reset the event.
20501 		 */
20502 	} else {
20503 
20504 		/*
20505 		 * Try to online the device
20506 		 * If there is any reset-related event, remove it. We are
20507 		 * configuring the device and no state restoring is needed.
20508 		 */
20509 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20510 		    saddr->cport)->cport_mutex);
20511 		sata_device.satadev_addr = *saddr;
20512 		if (saddr->qual == SATA_ADDR_CPORT)
20513 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20514 		else
20515 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20516 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20517 		if (sdinfo != NULL) {
20518 			if (sdinfo->satadrv_event_flags &
20519 			    (SATA_EVNT_DEVICE_RESET |
20520 			    SATA_EVNT_INPROC_DEVICE_RESET))
20521 				sdinfo->satadrv_event_flags = 0;
20522 			sdinfo->satadrv_event_flags |=
20523 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20524 
20525 			/* Need to create a new target node. */
20526 			cportinfo->cport_tgtnode_clean = B_TRUE;
20527 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20528 			    saddr->cport)->cport_mutex);
20529 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20530 			    sata_hba_inst, &sata_device.satadev_addr);
20531 			if (tdip == NULL) {
20532 				/*
20533 				 * Configure (onlining) failed.
20534 				 * We will NOT retry
20535 				 */
20536 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20537 				    "sata_process_device_autoonline: "
20538 				    "configuring SATA device at port %d failed",
20539 				    saddr->cport));
20540 			}
20541 		} else {
20542 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20543 			    saddr->cport)->cport_mutex);
20544 		}
20545 
20546 	}
20547 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20548 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20549 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20550 	    saddr->cport)->cport_mutex);
20551 }
20552 
20553 
20554 static void
20555 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20556     int hint)
20557 {
20558 	char ap[MAXPATHLEN];
20559 	nvlist_t *ev_attr_list = NULL;
20560 	int err;
20561 
20562 	/* Allocate and build sysevent attribute list */
20563 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20564 	if (err != 0) {
20565 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20566 		    "sata_gen_sysevent: "
20567 		    "cannot allocate memory for sysevent attributes\n"));
20568 		return;
20569 	}
20570 	/* Add hint attribute */
20571 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20572 	if (err != 0) {
20573 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20574 		    "sata_gen_sysevent: "
20575 		    "failed to add DR_HINT attr for sysevent"));
20576 		nvlist_free(ev_attr_list);
20577 		return;
20578 	}
20579 	/*
20580 	 * Add AP attribute.
20581 	 * Get controller pathname and convert it into AP pathname by adding
20582 	 * a target number.
20583 	 */
20584 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20585 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20586 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20587 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20588 
20589 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20590 	if (err != 0) {
20591 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20592 		    "sata_gen_sysevent: "
20593 		    "failed to add DR_AP_ID attr for sysevent"));
20594 		nvlist_free(ev_attr_list);
20595 		return;
20596 	}
20597 
20598 	/* Generate/log sysevent */
20599 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20600 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20601 	if (err != DDI_SUCCESS) {
20602 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20603 		    "sata_gen_sysevent: "
20604 		    "cannot log sysevent, err code %x\n", err));
20605 	}
20606 
20607 	nvlist_free(ev_attr_list);
20608 }
20609 
20610 
20611 
20612 
20613 /*
20614  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20615  */
20616 static void
20617 sata_set_device_removed(dev_info_t *tdip)
20618 {
20619 	int circ;
20620 
20621 	ASSERT(tdip != NULL);
20622 
20623 	ndi_devi_enter(tdip, &circ);
20624 	mutex_enter(&DEVI(tdip)->devi_lock);
20625 	DEVI_SET_DEVICE_REMOVED(tdip);
20626 	mutex_exit(&DEVI(tdip)->devi_lock);
20627 	ndi_devi_exit(tdip, circ);
20628 }
20629 
20630 
20631 /*
20632  * Set internal event instructing event daemon to try
20633  * to perform the target node cleanup.
20634  */
20635 static void
20636 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20637     sata_address_t *saddr)
20638 {
20639 	if (saddr->qual == SATA_ADDR_CPORT ||
20640 	    saddr->qual == SATA_ADDR_DCPORT) {
20641 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20642 		    saddr->cport)->cport_mutex);
20643 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20644 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20645 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20646 		    cport_tgtnode_clean = B_FALSE;
20647 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20648 		    saddr->cport)->cport_mutex);
20649 	} else {
20650 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20651 		    saddr->cport, saddr->pmport)->pmport_mutex);
20652 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20653 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20654 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20655 		    pmport_tgtnode_clean = B_FALSE;
20656 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20657 		    saddr->cport, saddr->pmport)->pmport_mutex);
20658 	}
20659 	mutex_enter(&sata_hba_inst->satahba_mutex);
20660 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20661 	mutex_exit(&sata_hba_inst->satahba_mutex);
20662 	mutex_enter(&sata_mutex);
20663 	sata_event_pending |= SATA_EVNT_MAIN;
20664 	mutex_exit(&sata_mutex);
20665 }
20666 
20667 
20668 /*
20669  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20670  * i.e. check if the target node state indicates that it belongs to a removed
20671  * device.
20672  *
20673  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20674  * B_FALSE otherwise.
20675  */
20676 static boolean_t
20677 sata_check_device_removed(dev_info_t *tdip)
20678 {
20679 	ASSERT(tdip != NULL);
20680 
20681 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20682 		return (B_TRUE);
20683 	else
20684 		return (B_FALSE);
20685 }
20686 
20687 
20688 /*
20689  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20690  */
20691 static boolean_t
20692 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20693 {
20694 	int fm_capability = ddi_fm_capable(dip);
20695 	ddi_fm_error_t de;
20696 
20697 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20698 		if (spx->txlt_buf_dma_handle != NULL) {
20699 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20700 			    DDI_FME_VERSION);
20701 			if (de.fme_status != DDI_SUCCESS)
20702 				return (B_TRUE);
20703 		}
20704 	}
20705 	return (B_FALSE);
20706 }
20707 
20708 
20709 /* ************************ FAULT INJECTTION **************************** */
20710 
20711 #ifdef SATA_INJECT_FAULTS
20712 
20713 static	uint32_t sata_fault_count = 0;
20714 static	uint32_t sata_fault_suspend_count = 0;
20715 
20716 /*
20717  * Inject sata pkt fault
20718  * It modifies returned values of the sata packet.
20719  * It returns immediately if:
20720  * pkt fault injection is not enabled (via sata_inject_fault,
20721  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20722  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20723  * pkt is not directed to specified fault controller/device
20724  * (sata_fault_ctrl_dev and sata_fault_device).
20725  * If fault controller is not specified, fault injection applies to all
20726  * controllers and devices.
20727  *
20728  * First argument is the pointer to the executed sata packet.
20729  * Second argument is a pointer to a value returned by the HBA tran_start
20730  * function.
20731  * Third argument specifies injected error. Injected sata packet faults
20732  * are the satapkt_reason values.
20733  * SATA_PKT_BUSY		-1	Not completed, busy
20734  * SATA_PKT_DEV_ERROR		1	Device reported error
20735  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20736  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20737  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20738  * SATA_PKT_ABORTED		5	Aborted by request
20739  * SATA_PKT_TIMEOUT		6	Operation timeut
20740  * SATA_PKT_RESET		7	Aborted by reset request
20741  *
20742  * Additional global variables affecting the execution:
20743  *
20744  * sata_inject_fault_count variable specifies number of times in row the
20745  * error is injected. Value of -1 specifies permanent fault, ie. every time
20746  * the fault injection point is reached, the fault is injected and a pause
20747  * between fault injection specified by sata_inject_fault_pause_count is
20748  * ignored). Fault injection routine decrements sata_inject_fault_count
20749  * (if greater than zero) until it reaches 0. No fault is injected when
20750  * sata_inject_fault_count is 0 (zero).
20751  *
20752  * sata_inject_fault_pause_count variable specifies number of times a fault
20753  * injection is bypassed (pause between fault injections).
20754  * If set to 0, a fault is injected only a number of times specified by
20755  * sata_inject_fault_count.
20756  *
20757  * The fault counts are static, so for periodic errors they have to be manually
20758  * reset to start repetition sequence from scratch.
20759  * If the original value returned by the HBA tran_start function is not
20760  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20761  * is injected (to avoid masking real problems);
20762  *
20763  * NOTE: In its current incarnation, this function should be invoked only for
20764  * commands executed in SYNCHRONOUS mode.
20765  */
20766 
20767 
20768 static void
20769 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20770 {
20771 
20772 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20773 		return;
20774 
20775 	if (sata_inject_fault_count == 0)
20776 		return;
20777 
20778 	if (fault == 0)
20779 		return;
20780 
20781 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20782 		return;
20783 
20784 	if (sata_fault_ctrl != NULL) {
20785 		sata_pkt_txlate_t *spx =
20786 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20787 
20788 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20789 		    spx->txlt_sata_hba_inst->satahba_dip)
20790 			return;
20791 
20792 		if (sata_fault_device.satadev_addr.cport !=
20793 		    spkt->satapkt_device.satadev_addr.cport ||
20794 		    sata_fault_device.satadev_addr.pmport !=
20795 		    spkt->satapkt_device.satadev_addr.pmport ||
20796 		    sata_fault_device.satadev_addr.qual !=
20797 		    spkt->satapkt_device.satadev_addr.qual)
20798 			return;
20799 	}
20800 
20801 	/* Modify pkt return parameters */
20802 	if (*rval != SATA_TRAN_ACCEPTED ||
20803 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20804 		sata_fault_count = 0;
20805 		sata_fault_suspend_count = 0;
20806 		return;
20807 	}
20808 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20809 		/* Pause in the injection */
20810 		sata_fault_suspend_count -= 1;
20811 		return;
20812 	}
20813 
20814 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20815 		/*
20816 		 * Init inject fault cycle. If fault count is set to -1,
20817 		 * it is a permanent fault.
20818 		 */
20819 		if (sata_inject_fault_count != -1) {
20820 			sata_fault_count = sata_inject_fault_count;
20821 			sata_fault_suspend_count =
20822 			    sata_inject_fault_pause_count;
20823 			if (sata_fault_suspend_count == 0)
20824 				sata_inject_fault_count = 0;
20825 		}
20826 	}
20827 
20828 	if (sata_fault_count != 0)
20829 		sata_fault_count -= 1;
20830 
20831 	switch (fault) {
20832 	case SATA_PKT_BUSY:
20833 		*rval = SATA_TRAN_BUSY;
20834 		spkt->satapkt_reason = SATA_PKT_BUSY;
20835 		break;
20836 
20837 	case SATA_PKT_QUEUE_FULL:
20838 		*rval = SATA_TRAN_QUEUE_FULL;
20839 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20840 		break;
20841 
20842 	case SATA_PKT_CMD_UNSUPPORTED:
20843 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20844 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20845 		break;
20846 
20847 	case SATA_PKT_PORT_ERROR:
20848 		/* This is "rejected" command */
20849 		*rval = SATA_TRAN_PORT_ERROR;
20850 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20851 		/* Additional error setup could be done here - port state */
20852 		break;
20853 
20854 	case SATA_PKT_DEV_ERROR:
20855 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20856 		/*
20857 		 * Additional error setup could be done here
20858 		 */
20859 		break;
20860 
20861 	case SATA_PKT_ABORTED:
20862 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20863 		break;
20864 
20865 	case SATA_PKT_TIMEOUT:
20866 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20867 		/* Additional error setup could be done here */
20868 		break;
20869 
20870 	case SATA_PKT_RESET:
20871 		spkt->satapkt_reason = SATA_PKT_RESET;
20872 		/*
20873 		 * Additional error setup could be done here - device reset
20874 		 */
20875 		break;
20876 
20877 	default:
20878 		break;
20879 	}
20880 }
20881 
20882 #endif
20883 
20884 /*
20885  * SATA Trace Ring Buffer
20886  * ----------------------
20887  *
20888  * Overview
20889  *
20890  * The SATA trace ring buffer is a ring buffer created and managed by
20891  * the SATA framework module that can be used by any module or driver
20892  * within the SATA framework to store debug messages.
20893  *
20894  * Ring Buffer Interfaces:
20895  *
20896  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20897  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20898  *
20899  *	Note that the sata_trace_debug() interface was created to give
20900  *	consumers the flexibilty of sending debug messages to ring buffer
20901  *	as variable arguments.  Consumers can send type va_list debug
20902  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20903  *	and sata_vtrace_debug() relationship is similar to that of
20904  *	cmn_err(9F) and vcmn_err(9F).
20905  *
20906  * Below is a diagram of the SATA trace ring buffer interfaces and
20907  * sample consumers:
20908  *
20909  * +---------------------------------+
20910  * |    o  o  SATA Framework Module  |
20911  * | o  SATA  o     +------------------+      +------------------+
20912  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20913  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20914  * | o        o     +------------------+   |  +------------------+
20915  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20916  * |                        |        |        +------------------+
20917  * |           +------------------+  |
20918  * |           |SATA Debug Message|  |
20919  * |           +------------------+  |
20920  * +---------------------------------+
20921  *
20922  * Supporting Routines:
20923  *
20924  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20925  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20926  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20927  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20928  *
20929  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20930  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20931  * /etc/system to desired size in unit of bytes.
20932  *
20933  * The individual debug message size in the ring buffer is restricted
20934  * to DMSG_BUF_SIZE.
20935  */
20936 void
20937 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20938 {
20939 	sata_trace_dmsg_t *dmsg;
20940 
20941 	if (sata_debug_rbuf == NULL) {
20942 		return;
20943 	}
20944 
20945 	/*
20946 	 * If max size of ring buffer is smaller than size
20947 	 * required for one debug message then just return
20948 	 * since we have no room for the debug message.
20949 	 */
20950 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20951 		return;
20952 	}
20953 
20954 	mutex_enter(&sata_debug_rbuf->lock);
20955 
20956 	/* alloc or reuse on ring buffer */
20957 	dmsg = sata_trace_dmsg_alloc();
20958 
20959 	if (dmsg == NULL) {
20960 		/* resource allocation failed */
20961 		mutex_exit(&sata_debug_rbuf->lock);
20962 		return;
20963 	}
20964 
20965 	dmsg->dip = dip;
20966 	gethrestime(&dmsg->timestamp);
20967 
20968 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20969 
20970 	mutex_exit(&sata_debug_rbuf->lock);
20971 }
20972 
20973 void
20974 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20975 {
20976 	va_list ap;
20977 
20978 	va_start(ap, fmt);
20979 	sata_vtrace_debug(dip, fmt, ap);
20980 	va_end(ap);
20981 }
20982 
20983 /*
20984  * This routine is used to manage debug messages
20985  * on ring buffer.
20986  */
20987 static sata_trace_dmsg_t *
20988 sata_trace_dmsg_alloc(void)
20989 {
20990 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20991 
20992 	if (sata_debug_rbuf->looped == TRUE) {
20993 		sata_debug_rbuf->dmsgp = dmsg->next;
20994 		return (sata_debug_rbuf->dmsgp);
20995 	}
20996 
20997 	/*
20998 	 * If we're looping for the first time,
20999 	 * connect the ring.
21000 	 */
21001 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21002 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21003 		dmsg->next = sata_debug_rbuf->dmsgh;
21004 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21005 		sata_debug_rbuf->looped = TRUE;
21006 		return (sata_debug_rbuf->dmsgp);
21007 	}
21008 
21009 	/* If we've gotten this far then memory allocation is needed */
21010 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21011 	if (dmsg_alloc == NULL) {
21012 		sata_debug_rbuf->allocfailed++;
21013 		return (dmsg_alloc);
21014 	} else {
21015 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21016 	}
21017 
21018 	if (sata_debug_rbuf->dmsgp != NULL) {
21019 		dmsg->next = dmsg_alloc;
21020 		sata_debug_rbuf->dmsgp = dmsg->next;
21021 		return (sata_debug_rbuf->dmsgp);
21022 	} else {
21023 		/*
21024 		 * We should only be here if we're initializing
21025 		 * the ring buffer.
21026 		 */
21027 		if (sata_debug_rbuf->dmsgh == NULL) {
21028 			sata_debug_rbuf->dmsgh = dmsg_alloc;
21029 		} else {
21030 			/* Something is wrong */
21031 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21032 			return (NULL);
21033 		}
21034 
21035 		sata_debug_rbuf->dmsgp = dmsg_alloc;
21036 		return (sata_debug_rbuf->dmsgp);
21037 	}
21038 }
21039 
21040 
21041 /*
21042  * Free all messages on debug ring buffer.
21043  */
21044 static void
21045 sata_trace_dmsg_free(void)
21046 {
21047 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21048 
21049 	while (dmsg != NULL) {
21050 		dmsg_next = dmsg->next;
21051 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21052 
21053 		/*
21054 		 * If we've looped around the ring than we're done.
21055 		 */
21056 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
21057 			break;
21058 		} else {
21059 			dmsg = dmsg_next;
21060 		}
21061 	}
21062 }
21063 
21064 
21065 /*
21066  * This function can block
21067  */
21068 static void
21069 sata_trace_rbuf_alloc(void)
21070 {
21071 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21072 
21073 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21074 
21075 	if (dmsg_ring_size > 0) {
21076 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21077 	}
21078 }
21079 
21080 
21081 static void
21082 sata_trace_rbuf_free(void)
21083 {
21084 	sata_trace_dmsg_free();
21085 	mutex_destroy(&sata_debug_rbuf->lock);
21086 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21087 }
21088 
21089 /*
21090  * If SATA_DEBUG is not defined then this routine is called instead
21091  * of sata_log() via the SATA_LOG_D macro.
21092  */
21093 static void
21094 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21095     const char *fmt, ...)
21096 {
21097 #ifndef __lock_lint
21098 	_NOTE(ARGUNUSED(level))
21099 #endif
21100 
21101 	dev_info_t *dip = NULL;
21102 	va_list ap;
21103 
21104 	if (sata_hba_inst != NULL) {
21105 		dip = SATA_DIP(sata_hba_inst);
21106 	}
21107 
21108 	va_start(ap, fmt);
21109 	sata_vtrace_debug(dip, fmt, ap);
21110 	va_end(ap);
21111 }
21112