xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision 41afdfa77f9af46beb3aaab2eccc0d9afe660d31)
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 
27 /*
28  * SATA Framework
29  * Generic SATA Host Adapter Implementation
30  */
31 
32 #include <sys/conf.h>
33 #include <sys/file.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/modctl.h>
37 #include <sys/cmn_err.h>
38 #include <sys/errno.h>
39 #include <sys/thread.h>
40 #include <sys/kstat.h>
41 #include <sys/note.h>
42 #include <sys/sysevent.h>
43 #include <sys/sysevent/eventdefs.h>
44 #include <sys/sysevent/dr.h>
45 #include <sys/taskq.h>
46 #include <sys/disp.h>
47 
48 #include <sys/sata/impl/sata.h>
49 #include <sys/sata/sata_hba.h>
50 #include <sys/sata/sata_defs.h>
51 #include <sys/sata/sata_cfgadm.h>
52 #include <sys/sata/sata_blacklist.h>
53 #include <sys/sata/sata_satl.h>
54 
55 #include <sys/scsi/impl/spc3_types.h>
56 
57 /*
58  * FMA header files
59  */
60 #include <sys/ddifm.h>
61 #include <sys/fm/protocol.h>
62 #include <sys/fm/util.h>
63 #include <sys/fm/io/ddi.h>
64 
65 /* Debug flags - defined in sata.h */
66 int	sata_debug_flags = 0;
67 int	sata_msg = 0;
68 
69 /*
70  * Flags enabling selected SATA HBA framework functionality
71  */
72 #define	SATA_ENABLE_QUEUING		1
73 #define	SATA_ENABLE_NCQ			2
74 #define	SATA_ENABLE_PROCESS_EVENTS	4
75 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
76 int sata_func_enable =
77 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
78 
79 /*
80  * Global variable setting default maximum queue depth (NCQ or TCQ)
81  * Note:minimum queue depth is 1
82  */
83 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
84 
85 /*
86  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
87  * initialization, using value from sata_max_queue_depth
88  * It is adjusted to minimum supported by the controller and by the device,
89  * if queueing is enabled.
90  */
91 static	int sata_current_max_qdepth;
92 
93 /*
94  * Global variable determining the default behavior after device hotpluggin.
95  * If non-zero, the hotplugged device is onlined (if possible) without explicit
96  * IOCTL request (AP_CONFIGURE).
97  * If zero, hotplugged device is identified, but not onlined.
98  * Enabling (AP_CONNECT) device port with an attached device does not result
99  * in device onlining regardless of the flag setting
100  */
101 int sata_auto_online = 0;
102 
103 #ifdef SATA_DEBUG
104 
105 #define	SATA_LOG_D(args)	sata_log args
106 uint64_t mbuf_count = 0;
107 uint64_t mbuffail_count = 0;
108 
109 sata_atapi_cmd_t sata_atapi_trace[64];
110 uint32_t sata_atapi_trace_index = 0;
111 int sata_atapi_trace_save = 1;
112 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
113 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
114     sata_save_atapi_trace(spx, count);
115 
116 #else
117 #define	SATA_LOG_D(args)	sata_trace_log args
118 #define	SATAATAPITRACE(spx, count)
119 #endif
120 
121 #if 0
122 static void
123 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
124 #endif
125 
126 #ifdef SATA_INJECT_FAULTS
127 
128 #define		SATA_INJECT_PKT_FAULT	1
129 uint32_t	sata_inject_fault = 0;
130 
131 uint32_t	sata_inject_fault_count = 0;
132 uint32_t	sata_inject_fault_pause_count = 0;
133 uint32_t	sata_fault_type = 0;
134 uint32_t	sata_fault_cmd = 0;
135 dev_info_t	*sata_fault_ctrl = NULL;
136 sata_device_t	sata_fault_device;
137 
138 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
139 
140 #endif
141 
142 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
143 
144 static char sata_rev_tag[] = {"1.46"};
145 
146 /*
147  * SATA cb_ops functions
148  */
149 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
150 static 	int sata_hba_close(dev_t, int, int, cred_t *);
151 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
152 
153 /*
154  * SCSA required entry points
155  */
156 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
157     scsi_hba_tran_t *, struct scsi_device *);
158 static	int sata_scsi_tgt_probe(struct scsi_device *,
159     int (*callback)(void));
160 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
161     scsi_hba_tran_t *, struct scsi_device *);
162 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
163 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
164 static 	int sata_scsi_reset(struct scsi_address *, int);
165 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
166 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
167 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
168     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
169     caddr_t);
170 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
171 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
172 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
173 
174 /*
175  * SATA HBA interface functions are defined in sata_hba.h header file
176  */
177 
178 /* Event processing functions */
179 static	void sata_event_daemon(void *);
180 static	void sata_event_thread_control(int);
181 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
182 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
183 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
184 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
185 static	void sata_process_port_failed_event(sata_hba_inst_t *,
186     sata_address_t *);
187 static	void sata_process_port_link_events(sata_hba_inst_t *,
188     sata_address_t *);
189 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
190     sata_address_t *);
191 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
192 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
193     sata_address_t *);
194 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
195 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
196     sata_address_t *);
197 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
198 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
199 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
200     sata_address_t *);
201 static	void sata_process_device_autoonline(sata_hba_inst_t *,
202     sata_address_t *saddr);
203 
204 /*
205  * Local translation functions
206  */
207 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
208 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
209 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
210 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
211 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
212 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
213 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
214 static	int sata_txlt_read(sata_pkt_txlate_t *);
215 static	int sata_txlt_write(sata_pkt_txlate_t *);
216 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
217 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
218 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
219 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
220 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
221 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
222 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
223 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
224 
225 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
226 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
227 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
228 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
229 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
230 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
231 static	void sata_txlt_rw_completion(sata_pkt_t *);
232 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
233 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
234 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
235 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
236 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
237 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
238     uint8_t);
239 static	struct scsi_extended_sense *sata_immediate_error_response(
240     sata_pkt_txlate_t *, int);
241 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
242 
243 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
244 static	void sata_txlt_atapi_completion(sata_pkt_t *);
245 
246 /*
247  * Local functions for ioctl
248  */
249 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
250 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
251     devctl_ap_state_t *);
252 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
253 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
254 static	dev_info_t *sata_devt_to_devinfo(dev_t);
255 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
256 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
257 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
258 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
259 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
260 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
264 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
266     sata_ioctl_data_t *, int mode);
267 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
268     sata_ioctl_data_t *, int mode);
269 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
270     sata_ioctl_data_t *, int mode);
271 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
272     sata_ioctl_data_t *, int mode);
273 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
274     sata_device_t *, sata_ioctl_data_t *, int mode);
275 
276 /*
277  * Local functions
278  */
279 static 	void sata_remove_hba_instance(dev_info_t *);
280 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
281 static 	void sata_probe_ports(sata_hba_inst_t *);
282 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
283 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
284 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
285 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
286 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
287 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
288 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
289 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
290     sata_drive_info_t *);
291 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
292     sata_address_t *);
293 static 	void sata_remove_target_node(sata_hba_inst_t *,
294     sata_address_t *);
295 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
296     struct scsi_address *, sata_device_t *);
297 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
298 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
299 static	void sata_pkt_free(sata_pkt_txlate_t *);
300 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
301     caddr_t, ddi_dma_attr_t *);
302 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
303 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
304 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
305     sata_device_t *);
306 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
307 static	void sata_reidentify_device(sata_pkt_txlate_t *);
308 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
309 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
310 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
311 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
312     ddi_dma_attr_t *);
313 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
314     sata_drive_info_t *);
315 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
316 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
317 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
318 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
319 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
320 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
321 static	int sata_set_drive_features(sata_hba_inst_t *,
322     sata_drive_info_t *, int flag);
323 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
324 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
325 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
326     uint8_t *);
327 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
328     struct scsi_inquiry *);
329 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
330 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
331 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
332 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
333 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
334 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
335     struct mode_cache_scsi3 *, int, int *, int *, int *);
336 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
337     struct mode_info_power_cond *, int, int *, int *, int *);
338 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
339     struct mode_info_excpt_page *, int, int *, int *, int *);
340 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
341     struct mode_acoustic_management *, int, int *, int *, int *);
342 
343 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
344 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
345     sata_hba_inst_t *);
346 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
347     sata_hba_inst_t *);
348 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
349     sata_hba_inst_t *);
350 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
351     sata_pkt_txlate_t *);
352 
353 static	void sata_set_arq_data(sata_pkt_t *);
354 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
355 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
356 static	uint8_t sata_get_standby_timer(uint8_t *timer);
357 
358 static	void sata_save_drive_settings(sata_drive_info_t *);
359 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
360 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
361 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
362 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
363 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
364     sata_drive_info_t *);
365 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
366     struct smart_data *);
367 static	int sata_smart_selftest_log(sata_hba_inst_t *,
368     sata_drive_info_t *,
369     struct smart_selftest_log *);
370 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
371     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
372 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
373     uint8_t *, uint8_t, uint8_t);
374 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
375     struct read_log_ext_directory *);
376 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
377 static	void sata_xlate_errors(sata_pkt_txlate_t *);
378 static	void sata_decode_device_error(sata_pkt_txlate_t *,
379     struct scsi_extended_sense *);
380 static	void sata_set_device_removed(dev_info_t *);
381 static	boolean_t sata_check_device_removed(dev_info_t *);
382 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
383 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
384     sata_drive_info_t *);
385 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
386     sata_drive_info_t *);
387 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
388 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
389 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
390 static  int sata_check_modser(char *, int);
391 
392 /*
393  * FMA
394  */
395 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
396 
397 
398 /*
399  * SATA Framework will ignore SATA HBA driver cb_ops structure and
400  * register following one with SCSA framework.
401  * Open & close are provided, so scsi framework will not use its own
402  */
403 static struct cb_ops sata_cb_ops = {
404 	sata_hba_open,			/* open */
405 	sata_hba_close,			/* close */
406 	nodev,				/* strategy */
407 	nodev,				/* print */
408 	nodev,				/* dump */
409 	nodev,				/* read */
410 	nodev,				/* write */
411 	sata_hba_ioctl,			/* ioctl */
412 	nodev,				/* devmap */
413 	nodev,				/* mmap */
414 	nodev,				/* segmap */
415 	nochpoll,			/* chpoll */
416 	ddi_prop_op,			/* cb_prop_op */
417 	0,				/* streamtab */
418 	D_NEW | D_MP,			/* cb_flag */
419 	CB_REV,				/* rev */
420 	nodev,				/* aread */
421 	nodev				/* awrite */
422 };
423 
424 
425 extern struct mod_ops mod_miscops;
426 extern uchar_t	scsi_cdb_size[];
427 
428 static struct modlmisc modlmisc = {
429 	&mod_miscops,			/* Type of module */
430 	"SATA Module"			/* module name */
431 };
432 
433 
434 static struct modlinkage modlinkage = {
435 	MODREV_1,
436 	(void *)&modlmisc,
437 	NULL
438 };
439 
440 /*
441  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
442  * i.e. when scsi_pkt has not timeout specified.
443  */
444 static int sata_default_pkt_time = 60;	/* 60 seconds */
445 
446 /*
447  * Intermediate buffer device access attributes - they are required,
448  * but not necessarily used.
449  */
450 static ddi_device_acc_attr_t sata_acc_attr = {
451 	DDI_DEVICE_ATTR_V0,
452 	DDI_STRUCTURE_LE_ACC,
453 	DDI_STRICTORDER_ACC
454 };
455 
456 
457 /*
458  * Mutexes protecting structures in multithreaded operations.
459  * Because events are relatively rare, a single global mutex protecting
460  * data structures should be sufficient. To increase performance, add
461  * separate mutex per each sata port and use global mutex only to protect
462  * common data structures.
463  */
464 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
465 static	kmutex_t sata_log_mutex;	/* protects log */
466 
467 static 	char sata_log_buf[256];
468 
469 /*
470  * sata trace debug
471  */
472 static	sata_trace_rbuf_t *sata_debug_rbuf;
473 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
474 static	void sata_trace_dmsg_free(void);
475 static	void sata_trace_rbuf_alloc(void);
476 static	void sata_trace_rbuf_free(void);
477 
478 int	dmsg_ring_size = DMSG_RING_SIZE;
479 
480 /* Default write cache setting for SATA hard disks */
481 int	sata_write_cache = 1;		/* enabled */
482 
483 /* Default write cache setting for SATA ATAPI CD/DVD */
484 int	sata_atapicdvd_write_cache = 1; /* enabled */
485 
486 /* Default write cache setting for SATA ATAPI tape */
487 int	sata_atapitape_write_cache = 1; /* enabled */
488 
489 /* Default write cache setting for SATA ATAPI disk */
490 int	sata_atapidisk_write_cache = 1;	/* enabled */
491 
492 /*
493  * Linked list of HBA instances
494  */
495 static 	sata_hba_inst_t *sata_hba_list = NULL;
496 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
497 /*
498  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
499  * structure and in sata soft state.
500  */
501 
502 /*
503  * Event daemon related variables
504  */
505 static 	kmutex_t sata_event_mutex;
506 static 	kcondvar_t sata_event_cv;
507 static 	kthread_t *sata_event_thread = NULL;
508 static 	int sata_event_thread_terminate = 0;
509 static 	int sata_event_pending = 0;
510 static 	int sata_event_thread_active = 0;
511 extern 	pri_t minclsyspri;
512 
513 /*
514  * NCQ error recovery command
515  */
516 static const sata_cmd_t sata_rle_cmd = {
517 	SATA_CMD_REV,
518 	NULL,
519 	{
520 		SATA_DIR_READ
521 	},
522 	ATA_ADDR_LBA48,
523 	0,
524 	0,
525 	0,
526 	0,
527 	0,
528 	1,
529 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
530 	0,
531 	0,
532 	0,
533 	SATAC_READ_LOG_EXT,
534 	0,
535 	0,
536 	0,
537 };
538 
539 /*
540  * ATAPI error recovery CDB
541  */
542 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
543 	SCMD_REQUEST_SENSE,
544 	0,			/* Only fixed RQ format is supported */
545 	0,
546 	0,
547 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
548 	0
549 };
550 
551 
552 /* Warlock directives */
553 
554 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
555 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
556 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
563 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
564 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
565 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
566 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
568     sata_hba_inst::satahba_scsi_tran))
569 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
570 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
571 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
572 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
573 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
574     sata_hba_inst::satahba_event_flags))
575 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
576     sata_cport_info::cport_devp))
577 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
578 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
579 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
580     sata_cport_info::cport_dev_type))
581 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
582 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
583     sata_cport_info::cport_state))
584 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
585 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
586     sata_pmport_info::pmport_state))
587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
588 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
589     sata_pmport_info::pmport_dev_type))
590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
591 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
592     sata_pmport_info::pmport_sata_drive))
593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
594     sata_pmport_info::pmport_tgtnode_clean))
595 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
596     sata_pmport_info::pmport_event_flags))
597 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
598 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
599 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
600 #ifdef SATA_DEBUG
601 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
602 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
603 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
605 #endif
606 
607 /* End of warlock directives */
608 
609 /* ************** loadable module configuration functions ************** */
610 
611 int
612 _init()
613 {
614 	int rval;
615 
616 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
617 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
618 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
619 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
620 	sata_trace_rbuf_alloc();
621 	if ((rval = mod_install(&modlinkage)) != 0) {
622 #ifdef SATA_DEBUG
623 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
624 #endif
625 		sata_trace_rbuf_free();
626 		mutex_destroy(&sata_log_mutex);
627 		cv_destroy(&sata_event_cv);
628 		mutex_destroy(&sata_event_mutex);
629 		mutex_destroy(&sata_mutex);
630 	}
631 	return (rval);
632 }
633 
634 int
635 _fini()
636 {
637 	int rval;
638 
639 	if ((rval = mod_remove(&modlinkage)) != 0)
640 		return (rval);
641 
642 	sata_trace_rbuf_free();
643 	mutex_destroy(&sata_log_mutex);
644 	cv_destroy(&sata_event_cv);
645 	mutex_destroy(&sata_event_mutex);
646 	mutex_destroy(&sata_mutex);
647 	return (rval);
648 }
649 
650 int
651 _info(struct modinfo *modinfop)
652 {
653 	return (mod_info(&modlinkage, modinfop));
654 }
655 
656 
657 
658 /* ********************* SATA HBA entry points ********************* */
659 
660 
661 /*
662  * Called by SATA HBA from _init().
663  * Registers HBA driver instance/sata framework pair with scsi framework, by
664  * calling scsi_hba_init().
665  *
666  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
667  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
668  * cb_ops pointer in SATA HBA driver dev_ops structure.
669  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
670  *
671  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
672  * driver.
673  */
674 int
675 sata_hba_init(struct modlinkage *modlp)
676 {
677 	int rval;
678 	struct dev_ops *hba_ops;
679 
680 	SATADBG1(SATA_DBG_HBA_IF, NULL,
681 	    "sata_hba_init: name %s \n",
682 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
683 	/*
684 	 * Fill-up cb_ops and dev_ops when necessary
685 	 */
686 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
687 	/*
688 	 * Provide pointer to SATA dev_ops
689 	 */
690 	hba_ops->devo_cb_ops = &sata_cb_ops;
691 
692 	/*
693 	 * Register SATA HBA with SCSI framework
694 	 */
695 	if ((rval = scsi_hba_init(modlp)) != 0) {
696 		SATADBG1(SATA_DBG_HBA_IF, NULL,
697 		    "sata_hba_init: scsi hba init failed\n", NULL);
698 		return (rval);
699 	}
700 
701 	return (0);
702 }
703 
704 
705 /* HBA attach stages */
706 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
707 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
708 #define	HBA_ATTACH_STAGE_SETUP		4
709 #define	HBA_ATTACH_STAGE_LINKED		8
710 
711 
712 /*
713  *
714  * Called from SATA HBA driver's attach routine to attach an instance of
715  * the HBA.
716  *
717  * For DDI_ATTACH command:
718  * sata_hba_inst structure is allocated here and initialized with pointers to
719  * SATA framework implementation of required scsi tran functions.
720  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
721  * to the soft structure (sata_hba_inst) allocated by SATA framework for
722  * SATA HBA instance related data.
723  * The scsi_tran's tran_hba_private field is used by SATA framework to
724  * store a pointer to per-HBA-instance of sata_hba_inst structure.
725  * The sata_hba_inst structure is cross-linked to scsi tran structure.
726  * Among other info, a pointer to sata_hba_tran structure is stored in
727  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
728  * linked together into the list, pointed to by sata_hba_list.
729  * On the first HBA instance attach the sata event thread is initialized.
730  * Attachment points are created for all SATA ports of the HBA being attached.
731  * All HBA instance's SATA ports are probed and type of plugged devices is
732  * determined. For each device of a supported type, a target node is created.
733  *
734  * DDI_SUCCESS is returned when attachment process is successful,
735  * DDI_FAILURE is returned otherwise.
736  *
737  * For DDI_RESUME command:
738  * Not implemented at this time (postponed until phase 2 of the development).
739  */
740 int
741 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
742     ddi_attach_cmd_t cmd)
743 {
744 	sata_hba_inst_t	*sata_hba_inst;
745 	scsi_hba_tran_t *scsi_tran = NULL;
746 	int hba_attach_state = 0;
747 	char taskq_name[MAXPATHLEN];
748 
749 	SATADBG3(SATA_DBG_HBA_IF, NULL,
750 	    "sata_hba_attach: node %s (%s%d)\n",
751 	    ddi_node_name(dip), ddi_driver_name(dip),
752 	    ddi_get_instance(dip));
753 
754 	if (cmd == DDI_RESUME) {
755 		/*
756 		 * Postponed until phase 2 of the development
757 		 */
758 		return (DDI_FAILURE);
759 	}
760 
761 	if (cmd != DDI_ATTACH) {
762 		return (DDI_FAILURE);
763 	}
764 
765 	/* cmd == DDI_ATTACH */
766 
767 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
768 		SATA_LOG_D((NULL, CE_WARN,
769 		    "sata_hba_attach: invalid sata_hba_tran"));
770 		return (DDI_FAILURE);
771 	}
772 	/*
773 	 * Allocate and initialize SCSI tran structure.
774 	 * SATA copy of tran_bus_config is provided to create port nodes.
775 	 */
776 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
777 	if (scsi_tran == NULL)
778 		return (DDI_FAILURE);
779 	/*
780 	 * Allocate soft structure for SATA HBA instance.
781 	 * There is a separate softstate for each HBA instance.
782 	 */
783 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
784 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
785 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
786 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
787 
788 	/*
789 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
790 	 * soft structure allocated by SATA framework for
791 	 * SATA HBA instance related data.
792 	 */
793 	scsi_tran->tran_hba_private	= sata_hba_inst;
794 	scsi_tran->tran_tgt_private	= NULL;
795 
796 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
797 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
798 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
799 
800 	scsi_tran->tran_start		= sata_scsi_start;
801 	scsi_tran->tran_reset		= sata_scsi_reset;
802 	scsi_tran->tran_abort		= sata_scsi_abort;
803 	scsi_tran->tran_getcap		= sata_scsi_getcap;
804 	scsi_tran->tran_setcap		= sata_scsi_setcap;
805 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
806 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
807 
808 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
809 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
810 
811 	scsi_tran->tran_reset_notify	= NULL;
812 	scsi_tran->tran_get_bus_addr	= NULL;
813 	scsi_tran->tran_quiesce		= NULL;
814 	scsi_tran->tran_unquiesce	= NULL;
815 	scsi_tran->tran_bus_reset	= NULL;
816 
817 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
818 	    scsi_tran, 0) != DDI_SUCCESS) {
819 #ifdef SATA_DEBUG
820 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
821 		    ddi_driver_name(dip), ddi_get_instance(dip));
822 #endif
823 		goto fail;
824 	}
825 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
826 
827 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
828 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
829 		    "sata", 1) != DDI_PROP_SUCCESS) {
830 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
831 			    "failed to create hba sata prop"));
832 			goto fail;
833 		}
834 	}
835 
836 	/*
837 	 * Save pointers in hba instance soft state.
838 	 */
839 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
840 	sata_hba_inst->satahba_tran = sata_tran;
841 	sata_hba_inst->satahba_dip = dip;
842 
843 	/*
844 	 * Create a task queue to handle emulated commands completion
845 	 * Use node name, dash, instance number as the queue name.
846 	 */
847 	taskq_name[0] = '\0';
848 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
849 	    sizeof (taskq_name));
850 	(void) snprintf(taskq_name + strlen(taskq_name),
851 	    sizeof (taskq_name) - strlen(taskq_name),
852 	    "-%d", DEVI(dip)->devi_instance);
853 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
854 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
855 	    TASKQ_DYNAMIC);
856 
857 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
858 
859 	/*
860 	 * Create events thread if not created yet.
861 	 */
862 	sata_event_thread_control(1);
863 
864 	/*
865 	 * Link this hba instance into the list.
866 	 */
867 	mutex_enter(&sata_mutex);
868 
869 	if (sata_hba_list == NULL) {
870 		/*
871 		 * The first instance of HBA is attached.
872 		 * Set current/active default maximum NCQ/TCQ queue depth for
873 		 * all SATA devices. It is done here and now, to eliminate the
874 		 * possibility of the dynamic, programatic modification of the
875 		 * queue depth via global (and public) sata_max_queue_depth
876 		 * variable (this would require special handling in HBA drivers)
877 		 */
878 		sata_current_max_qdepth = sata_max_queue_depth;
879 		if (sata_current_max_qdepth > 32)
880 			sata_current_max_qdepth = 32;
881 		else if (sata_current_max_qdepth < 1)
882 			sata_current_max_qdepth = 1;
883 	}
884 
885 	sata_hba_inst->satahba_next = NULL;
886 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
887 	if (sata_hba_list == NULL) {
888 		sata_hba_list = sata_hba_inst;
889 	}
890 	if (sata_hba_list_tail != NULL) {
891 		sata_hba_list_tail->satahba_next = sata_hba_inst;
892 	}
893 	sata_hba_list_tail = sata_hba_inst;
894 	mutex_exit(&sata_mutex);
895 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
896 
897 	/*
898 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
899 	 * SATA HBA driver should not use its own open/close entry points.
900 	 *
901 	 * Make sure that instance number doesn't overflow
902 	 * when forming minor numbers.
903 	 */
904 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
905 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
906 	    INST2DEVCTL(ddi_get_instance(dip)),
907 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
908 #ifdef SATA_DEBUG
909 		cmn_err(CE_WARN, "sata_hba_attach: "
910 		    "cannot create devctl minor node");
911 #endif
912 		goto fail;
913 	}
914 
915 
916 	/*
917 	 * Set-up kstats here, if necessary.
918 	 * (postponed until future phase of the development).
919 	 */
920 
921 	/*
922 	 * Indicate that HBA is attached. This will enable events processing
923 	 * for this HBA.
924 	 */
925 	sata_hba_inst->satahba_attached = 1;
926 	/*
927 	 * Probe controller ports. This operation will describe a current
928 	 * controller/port/multipliers/device configuration and will create
929 	 * attachment points.
930 	 * We may end-up with just a controller with no devices attached.
931 	 * For the ports with a supported device attached, device target nodes
932 	 * are created and devices are initialized.
933 	 */
934 	sata_probe_ports(sata_hba_inst);
935 
936 	return (DDI_SUCCESS);
937 
938 fail:
939 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
940 		(void) sata_remove_hba_instance(dip);
941 		if (sata_hba_list == NULL)
942 			sata_event_thread_control(0);
943 	}
944 
945 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
946 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
947 		taskq_destroy(sata_hba_inst->satahba_taskq);
948 	}
949 
950 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
951 		(void) scsi_hba_detach(dip);
952 
953 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
954 		mutex_destroy(&sata_hba_inst->satahba_mutex);
955 		kmem_free((void *)sata_hba_inst,
956 		    sizeof (struct sata_hba_inst));
957 		scsi_hba_tran_free(scsi_tran);
958 	}
959 
960 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
961 	    ddi_driver_name(dip), ddi_get_instance(dip));
962 
963 	return (DDI_FAILURE);
964 }
965 
966 
967 /*
968  * Called by SATA HBA from to detach an instance of the driver.
969  *
970  * For DDI_DETACH command:
971  * Free local structures allocated for SATA HBA instance during
972  * sata_hba_attach processing.
973  *
974  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
975  *
976  * For DDI_SUSPEND command:
977  * Not implemented at this time (postponed until phase 2 of the development)
978  * Returnd DDI_SUCCESS.
979  *
980  * When the last HBA instance is detached, the event daemon is terminated.
981  *
982  * NOTE: Port multiplier is supported.
983  */
984 int
985 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
986 {
987 	dev_info_t	*tdip;
988 	sata_hba_inst_t	*sata_hba_inst;
989 	scsi_hba_tran_t *scsi_hba_tran;
990 	sata_cport_info_t *cportinfo;
991 	sata_pmult_info_t *pminfo;
992 	sata_drive_info_t *sdinfo;
993 	sata_device_t	sdevice;
994 	int ncport, npmport;
995 
996 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
997 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
998 
999 	switch (cmd) {
1000 	case DDI_DETACH:
1001 
1002 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1003 			return (DDI_FAILURE);
1004 
1005 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1006 		if (sata_hba_inst == NULL)
1007 			return (DDI_FAILURE);
1008 
1009 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1010 			sata_hba_inst->satahba_attached = 1;
1011 			return (DDI_FAILURE);
1012 		}
1013 
1014 		/*
1015 		 * Free all target nodes - at this point
1016 		 * devices should be at least offlined
1017 		 * otherwise scsi_hba_detach() should not be called.
1018 		 */
1019 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1020 		    ncport++) {
1021 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1022 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1023 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1024 				if (sdinfo != NULL) {
1025 					tdip = sata_get_target_dip(dip,
1026 					    ncport, 0);
1027 					if (tdip != NULL) {
1028 						if (ndi_devi_offline(tdip,
1029 						    NDI_DEVI_REMOVE) !=
1030 						    NDI_SUCCESS) {
1031 							SATA_LOG_D((
1032 							    sata_hba_inst,
1033 							    CE_WARN,
1034 							    "sata_hba_detach: "
1035 							    "Target node not "
1036 							    "removed !"));
1037 							return (DDI_FAILURE);
1038 						}
1039 					}
1040 				}
1041 			} else { /* SATA_DTYPE_PMULT */
1042 				mutex_enter(&cportinfo->cport_mutex);
1043 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1044 
1045 				if (pminfo == NULL) {
1046 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1047 					    "sata_hba_detach: Port multiplier "
1048 					    "not ready yet!"));
1049 					mutex_exit(&cportinfo->cport_mutex);
1050 					return (DDI_FAILURE);
1051 				}
1052 
1053 				/*
1054 				 * Detach would fail if removal of any of the
1055 				 * target nodes is failed - albeit in that
1056 				 * case some of them may have been removed.
1057 				 */
1058 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1059 				    sata_hba_inst, ncport); npmport++) {
1060 					tdip = sata_get_target_dip(dip, ncport,
1061 					    npmport);
1062 					if (tdip != NULL) {
1063 						if (ndi_devi_offline(tdip,
1064 						    NDI_DEVI_REMOVE) !=
1065 						    NDI_SUCCESS) {
1066 							SATA_LOG_D((
1067 							    sata_hba_inst,
1068 							    CE_WARN,
1069 							    "sata_hba_detach: "
1070 							    "Target node not "
1071 							    "removed !"));
1072 							mutex_exit(&cportinfo->
1073 							    cport_mutex);
1074 							return (DDI_FAILURE);
1075 						}
1076 					}
1077 				}
1078 				mutex_exit(&cportinfo->cport_mutex);
1079 			}
1080 		}
1081 		/*
1082 		 * Disable sata event daemon processing for this HBA
1083 		 */
1084 		sata_hba_inst->satahba_attached = 0;
1085 
1086 		/*
1087 		 * Remove event daemon thread, if it is last HBA instance.
1088 		 */
1089 
1090 		mutex_enter(&sata_mutex);
1091 		if (sata_hba_list->satahba_next == NULL) {
1092 			mutex_exit(&sata_mutex);
1093 			sata_event_thread_control(0);
1094 			mutex_enter(&sata_mutex);
1095 		}
1096 		mutex_exit(&sata_mutex);
1097 
1098 		/* Remove this HBA instance from the HBA list */
1099 		sata_remove_hba_instance(dip);
1100 
1101 		/*
1102 		 * At this point there should be no target nodes attached.
1103 		 * Detach and destroy device and port info structures.
1104 		 */
1105 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1106 		    ncport++) {
1107 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1108 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1109 				sdinfo =
1110 				    cportinfo->cport_devp.cport_sata_drive;
1111 				if (sdinfo != NULL) {
1112 					/* Release device structure */
1113 					kmem_free(sdinfo,
1114 					    sizeof (sata_drive_info_t));
1115 				}
1116 				/* Release cport info */
1117 				mutex_destroy(&cportinfo->cport_mutex);
1118 				kmem_free(cportinfo,
1119 				    sizeof (sata_cport_info_t));
1120 			} else { /* SATA_DTYPE_PMULT */
1121 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1122 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1123 				sata_free_pmult(sata_hba_inst, &sdevice);
1124 			}
1125 		}
1126 
1127 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1128 
1129 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1130 
1131 		taskq_destroy(sata_hba_inst->satahba_taskq);
1132 
1133 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1134 		kmem_free((void *)sata_hba_inst,
1135 		    sizeof (struct sata_hba_inst));
1136 
1137 		return (DDI_SUCCESS);
1138 
1139 	case DDI_SUSPEND:
1140 		/*
1141 		 * Postponed until phase 2
1142 		 */
1143 		return (DDI_FAILURE);
1144 
1145 	default:
1146 		return (DDI_FAILURE);
1147 	}
1148 }
1149 
1150 
1151 /*
1152  * Called by an HBA drive from _fini() routine.
1153  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1154  */
1155 void
1156 sata_hba_fini(struct modlinkage *modlp)
1157 {
1158 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1159 	    "sata_hba_fini: name %s\n",
1160 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1161 
1162 	scsi_hba_fini(modlp);
1163 }
1164 
1165 
1166 /*
1167  * Default open and close routine for sata_hba framework.
1168  *
1169  */
1170 /*
1171  * Open devctl node.
1172  *
1173  * Returns:
1174  * 0 if node was open successfully, error code otherwise.
1175  *
1176  *
1177  */
1178 
1179 static int
1180 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1181 {
1182 #ifndef __lock_lint
1183 	_NOTE(ARGUNUSED(credp))
1184 #endif
1185 	int rv = 0;
1186 	dev_info_t *dip;
1187 	scsi_hba_tran_t *scsi_hba_tran;
1188 	sata_hba_inst_t	*sata_hba_inst;
1189 
1190 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1191 
1192 	if (otyp != OTYP_CHR)
1193 		return (EINVAL);
1194 
1195 	dip = sata_devt_to_devinfo(*devp);
1196 	if (dip == NULL)
1197 		return (ENXIO);
1198 
1199 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1200 		return (ENXIO);
1201 
1202 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1203 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1204 		return (ENXIO);
1205 
1206 	mutex_enter(&sata_mutex);
1207 	if (flags & FEXCL) {
1208 		if (sata_hba_inst->satahba_open_flag != 0) {
1209 			rv = EBUSY;
1210 		} else {
1211 			sata_hba_inst->satahba_open_flag =
1212 			    SATA_DEVCTL_EXOPENED;
1213 		}
1214 	} else {
1215 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1216 			rv = EBUSY;
1217 		} else {
1218 			sata_hba_inst->satahba_open_flag =
1219 			    SATA_DEVCTL_SOPENED;
1220 		}
1221 	}
1222 	mutex_exit(&sata_mutex);
1223 
1224 	return (rv);
1225 }
1226 
1227 
1228 /*
1229  * Close devctl node.
1230  * Returns:
1231  * 0 if node was closed successfully, error code otherwise.
1232  *
1233  */
1234 
1235 static int
1236 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1237 {
1238 #ifndef __lock_lint
1239 	_NOTE(ARGUNUSED(credp))
1240 	_NOTE(ARGUNUSED(flag))
1241 #endif
1242 	dev_info_t *dip;
1243 	scsi_hba_tran_t *scsi_hba_tran;
1244 	sata_hba_inst_t	*sata_hba_inst;
1245 
1246 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1247 
1248 	if (otyp != OTYP_CHR)
1249 		return (EINVAL);
1250 
1251 	dip = sata_devt_to_devinfo(dev);
1252 	if (dip == NULL)
1253 		return (ENXIO);
1254 
1255 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1256 		return (ENXIO);
1257 
1258 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1259 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1260 		return (ENXIO);
1261 
1262 	mutex_enter(&sata_mutex);
1263 	sata_hba_inst->satahba_open_flag = 0;
1264 	mutex_exit(&sata_mutex);
1265 	return (0);
1266 }
1267 
1268 
1269 
1270 /*
1271  * Standard IOCTL commands for SATA hotplugging.
1272  * Implemented DEVCTL_AP commands:
1273  * DEVCTL_AP_CONNECT
1274  * DEVCTL_AP_DISCONNECT
1275  * DEVCTL_AP_CONFIGURE
1276  * DEVCTL_UNCONFIGURE
1277  * DEVCTL_AP_CONTROL
1278  *
1279  * Commands passed to default ndi ioctl handler:
1280  * DEVCTL_DEVICE_GETSTATE
1281  * DEVCTL_DEVICE_ONLINE
1282  * DEVCTL_DEVICE_OFFLINE
1283  * DEVCTL_DEVICE_REMOVE
1284  * DEVCTL_DEVICE_INSERT
1285  * DEVCTL_BUS_GETSTATE
1286  *
1287  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1288  * if not.
1289  *
1290  * Returns:
1291  * 0 if successful,
1292  * error code if operation failed.
1293  *
1294  * Port Multiplier support is supported now.
1295  *
1296  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1297  */
1298 
1299 static int
1300 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1301     int *rvalp)
1302 {
1303 #ifndef __lock_lint
1304 	_NOTE(ARGUNUSED(credp))
1305 	_NOTE(ARGUNUSED(rvalp))
1306 #endif
1307 	int rv = 0;
1308 	int32_t	comp_port = -1;
1309 	dev_info_t *dip;
1310 	devctl_ap_state_t ap_state;
1311 	struct devctl_iocdata *dcp = NULL;
1312 	scsi_hba_tran_t *scsi_hba_tran;
1313 	sata_hba_inst_t *sata_hba_inst;
1314 	sata_device_t sata_device;
1315 	sata_cport_info_t *cportinfo;
1316 	int cport, pmport, qual;
1317 	int rval = SATA_SUCCESS;
1318 
1319 	dip = sata_devt_to_devinfo(dev);
1320 	if (dip == NULL)
1321 		return (ENXIO);
1322 
1323 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1324 		return (ENXIO);
1325 
1326 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1327 	if (sata_hba_inst == NULL)
1328 		return (ENXIO);
1329 
1330 	if (sata_hba_inst->satahba_tran == NULL)
1331 		return (ENXIO);
1332 
1333 	switch (cmd) {
1334 
1335 	case DEVCTL_DEVICE_GETSTATE:
1336 	case DEVCTL_DEVICE_ONLINE:
1337 	case DEVCTL_DEVICE_OFFLINE:
1338 	case DEVCTL_DEVICE_REMOVE:
1339 	case DEVCTL_BUS_GETSTATE:
1340 		/*
1341 		 * There may be more cases that we want to pass to default
1342 		 * handler rather than fail them.
1343 		 */
1344 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1345 	}
1346 
1347 	/* read devctl ioctl data */
1348 	if (cmd != DEVCTL_AP_CONTROL) {
1349 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1350 			return (EFAULT);
1351 
1352 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1353 		    -1) {
1354 			if (dcp)
1355 				ndi_dc_freehdl(dcp);
1356 			return (EINVAL);
1357 		}
1358 
1359 		/*
1360 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1361 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1362 		 */
1363 		cport = SCSI_TO_SATA_CPORT(comp_port);
1364 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1365 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1366 
1367 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1368 		    qual) != 0) {
1369 			ndi_dc_freehdl(dcp);
1370 			return (EINVAL);
1371 		}
1372 
1373 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1374 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1375 		    cport_mutex);
1376 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1377 			/*
1378 			 * Cannot process ioctl request now. Come back later.
1379 			 */
1380 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1381 			    cport_mutex);
1382 			ndi_dc_freehdl(dcp);
1383 			return (EBUSY);
1384 		}
1385 		/* Block event processing for this port */
1386 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1387 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1388 
1389 		sata_device.satadev_addr.cport = cport;
1390 		sata_device.satadev_addr.pmport = pmport;
1391 		sata_device.satadev_addr.qual = qual;
1392 		sata_device.satadev_rev = SATA_DEVICE_REV;
1393 	}
1394 
1395 	switch (cmd) {
1396 
1397 	case DEVCTL_AP_DISCONNECT:
1398 
1399 		/*
1400 		 * Normally, cfgadm sata plugin will try to offline
1401 		 * (unconfigure) device before this request. Nevertheless,
1402 		 * if a device is still configured, we need to
1403 		 * attempt to offline and unconfigure device first, and we will
1404 		 * deactivate the port regardless of the unconfigure
1405 		 * operation results.
1406 		 *
1407 		 */
1408 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1409 
1410 		break;
1411 
1412 	case DEVCTL_AP_UNCONFIGURE:
1413 
1414 		/*
1415 		 * The unconfigure operation uses generic nexus operation to
1416 		 * offline a device. It leaves a target device node attached.
1417 		 * and obviously sata_drive_info attached as well, because
1418 		 * from the hardware point of view nothing has changed.
1419 		 */
1420 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1421 		break;
1422 
1423 	case DEVCTL_AP_CONNECT:
1424 	{
1425 		/*
1426 		 * The sata cfgadm pluging will invoke this operation only if
1427 		 * port was found in the disconnect state (failed state
1428 		 * is also treated as the disconnected state).
1429 		 * If port activation is successful and a device is found
1430 		 * attached to the port, the initialization sequence is
1431 		 * executed to probe the port and attach
1432 		 * a device structure to a port structure. The device is not
1433 		 * set in configured state (system-wise) by this operation.
1434 		 */
1435 
1436 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1437 
1438 		break;
1439 	}
1440 
1441 	case DEVCTL_AP_CONFIGURE:
1442 	{
1443 		/*
1444 		 * A port may be in an active or shutdown state.
1445 		 * If port is in a failed state, operation is aborted.
1446 		 * If a port is in a shutdown state, sata_tran_port_activate()
1447 		 * is invoked prior to any other operation.
1448 		 *
1449 		 * Onlining the device involves creating a new target node.
1450 		 * If there is an old target node present (belonging to
1451 		 * previously removed device), the operation is aborted - the
1452 		 * old node has to be released and removed before configure
1453 		 * operation is attempted.
1454 		 */
1455 
1456 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1457 
1458 		break;
1459 	}
1460 
1461 	case DEVCTL_AP_GETSTATE:
1462 
1463 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1464 
1465 		ap_state.ap_last_change = (time_t)-1;
1466 		ap_state.ap_error_code = 0;
1467 		ap_state.ap_in_transition = 0;
1468 
1469 		/* Copy the return AP-state information to the user space */
1470 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1471 			rv = EFAULT;
1472 		}
1473 		break;
1474 
1475 	case DEVCTL_AP_CONTROL:
1476 	{
1477 		/*
1478 		 * Generic devctl for hardware specific functionality
1479 		 */
1480 		sata_ioctl_data_t	ioc;
1481 
1482 		ASSERT(dcp == NULL);
1483 
1484 		/* Copy in user ioctl data first */
1485 #ifdef _MULTI_DATAMODEL
1486 		if (ddi_model_convert_from(mode & FMODELS) ==
1487 		    DDI_MODEL_ILP32) {
1488 
1489 			sata_ioctl_data_32_t	ioc32;
1490 
1491 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1492 			    sizeof (ioc32), mode) != 0) {
1493 				rv = EFAULT;
1494 				break;
1495 			}
1496 			ioc.cmd 	= (uint_t)ioc32.cmd;
1497 			ioc.port	= (uint_t)ioc32.port;
1498 			ioc.get_size	= (uint_t)ioc32.get_size;
1499 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1500 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1501 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1502 		} else
1503 #endif /* _MULTI_DATAMODEL */
1504 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1505 		    mode) != 0) {
1506 			return (EFAULT);
1507 		}
1508 
1509 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1510 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1511 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1512 
1513 		/*
1514 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1515 		 * a 32-bit number.
1516 		 */
1517 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1518 			return (EINVAL);
1519 		}
1520 		/* validate address */
1521 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1522 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1523 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1524 
1525 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1526 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1527 		    cport, pmport, qual);
1528 
1529 		if (sata_validate_sata_address(sata_hba_inst, cport,
1530 		    pmport, qual) != 0)
1531 			return (EINVAL);
1532 
1533 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1534 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1535 		    cport_mutex);
1536 		/* Is the port locked by event processing daemon ? */
1537 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1538 			/*
1539 			 * Cannot process ioctl request now. Come back later
1540 			 */
1541 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1542 			    cport_mutex);
1543 			return (EBUSY);
1544 		}
1545 		/* Block event processing for this port */
1546 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1547 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1548 
1549 
1550 		sata_device.satadev_addr.cport = cport;
1551 		sata_device.satadev_addr.pmport = pmport;
1552 		sata_device.satadev_addr.qual = qual;
1553 		sata_device.satadev_rev = SATA_DEVICE_REV;
1554 
1555 		switch (ioc.cmd) {
1556 
1557 		case SATA_CFGA_RESET_PORT:
1558 			/*
1559 			 * There is no protection for configured device.
1560 			 */
1561 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1562 			break;
1563 
1564 		case SATA_CFGA_RESET_DEVICE:
1565 			/*
1566 			 * There is no protection for configured device.
1567 			 */
1568 			rv = sata_ioctl_reset_device(sata_hba_inst,
1569 			    &sata_device);
1570 			break;
1571 
1572 		case SATA_CFGA_RESET_ALL:
1573 			/*
1574 			 * There is no protection for configured devices.
1575 			 */
1576 			rv = sata_ioctl_reset_all(sata_hba_inst);
1577 			/*
1578 			 * We return here, because common return is for
1579 			 * a single port operation - we have already unlocked
1580 			 * all ports and no dc handle was allocated.
1581 			 */
1582 			return (rv);
1583 
1584 		case SATA_CFGA_PORT_DEACTIVATE:
1585 			/*
1586 			 * Arbitrarily unconfigure attached device, if any.
1587 			 * Even if the unconfigure fails, proceed with the
1588 			 * port deactivation.
1589 			 */
1590 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1591 
1592 			break;
1593 
1594 		case SATA_CFGA_PORT_ACTIVATE:
1595 
1596 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1597 			break;
1598 
1599 		case SATA_CFGA_PORT_SELF_TEST:
1600 
1601 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1602 			    &sata_device);
1603 			break;
1604 
1605 		case SATA_CFGA_GET_DEVICE_PATH:
1606 
1607 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1608 			    &sata_device, &ioc, mode);
1609 			break;
1610 
1611 		case SATA_CFGA_GET_AP_TYPE:
1612 
1613 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1614 			    &sata_device, &ioc, mode);
1615 			break;
1616 
1617 		case SATA_CFGA_GET_MODEL_INFO:
1618 
1619 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1620 			    &sata_device, &ioc, mode);
1621 			break;
1622 
1623 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1624 
1625 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1626 			    &sata_device, &ioc, mode);
1627 			break;
1628 
1629 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1630 
1631 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1632 			    &sata_device, &ioc, mode);
1633 			break;
1634 
1635 		default:
1636 			rv = EINVAL;
1637 			break;
1638 
1639 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1640 
1641 		break;
1642 	}
1643 
1644 	default:
1645 	{
1646 		/*
1647 		 * If we got here, we got an IOCTL that SATA HBA Framework
1648 		 * does not recognize. Pass ioctl to HBA driver, in case
1649 		 * it could process it.
1650 		 */
1651 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1652 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1653 
1654 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1655 		    "IOCTL 0x%2x not supported in SATA framework, "
1656 		    "passthrough to HBA", cmd);
1657 
1658 		if (sata_tran->sata_tran_ioctl == NULL) {
1659 			rv = EINVAL;
1660 			break;
1661 		}
1662 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1663 		if (rval != 0) {
1664 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1665 			    "IOCTL 0x%2x failed in HBA", cmd);
1666 			rv = rval;
1667 		}
1668 		break;
1669 	}
1670 
1671 	} /* End of main IOCTL switch */
1672 
1673 	if (dcp) {
1674 		ndi_dc_freehdl(dcp);
1675 	}
1676 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1677 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1678 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1679 
1680 	return (rv);
1681 }
1682 
1683 
1684 /*
1685  * Create error retrieval sata packet
1686  *
1687  * A sata packet is allocated and set-up to contain specified error retrieval
1688  * command and appropriate dma-able data buffer.
1689  * No association with any scsi packet is made and no callback routine is
1690  * specified.
1691  *
1692  * Returns a pointer to sata packet upon successful packet creation.
1693  * Returns NULL, if packet cannot be created.
1694  */
1695 sata_pkt_t *
1696 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1697     int pkt_type)
1698 {
1699 	sata_hba_inst_t	*sata_hba_inst;
1700 	sata_pkt_txlate_t *spx;
1701 	sata_pkt_t *spkt;
1702 	sata_drive_info_t *sdinfo;
1703 
1704 	mutex_enter(&sata_mutex);
1705 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1706 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1707 		if (SATA_DIP(sata_hba_inst) == dip)
1708 			break;
1709 	}
1710 	mutex_exit(&sata_mutex);
1711 	ASSERT(sata_hba_inst != NULL);
1712 
1713 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1714 	if (sdinfo == NULL) {
1715 		sata_log(sata_hba_inst, CE_WARN,
1716 		    "sata: error recovery request for non-attached device at "
1717 		    "cport %d", sata_device->satadev_addr.cport);
1718 		return (NULL);
1719 	}
1720 
1721 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1722 	spx->txlt_sata_hba_inst = sata_hba_inst;
1723 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1724 	spkt = sata_pkt_alloc(spx, NULL);
1725 	if (spkt == NULL) {
1726 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1727 		return (NULL);
1728 	}
1729 	/* address is needed now */
1730 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1731 
1732 	switch (pkt_type) {
1733 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1734 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1735 			if (sata_check_for_dma_error(dip, spx)) {
1736 				ddi_fm_service_impact(dip,
1737 				    DDI_SERVICE_UNAFFECTED);
1738 				break;
1739 			}
1740 			return (spkt);
1741 		}
1742 		break;
1743 
1744 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1745 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1746 			if (sata_check_for_dma_error(dip, spx)) {
1747 				ddi_fm_service_impact(dip,
1748 				    DDI_SERVICE_UNAFFECTED);
1749 				break;
1750 			}
1751 			return (spkt);
1752 		}
1753 		break;
1754 
1755 	default:
1756 		break;
1757 	}
1758 
1759 	sata_pkt_free(spx);
1760 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1761 	return (NULL);
1762 
1763 }
1764 
1765 
1766 /*
1767  * Free error retrieval sata packet
1768  *
1769  * Free sata packet and any associated resources allocated previously by
1770  * sata_get_error_retrieval_pkt().
1771  *
1772  * Void return.
1773  */
1774 void
1775 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1776 {
1777 	sata_pkt_txlate_t *spx =
1778 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1779 
1780 	ASSERT(sata_pkt != NULL);
1781 
1782 	sata_free_local_buffer(spx);
1783 	sata_pkt_free(spx);
1784 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1785 
1786 }
1787 
1788 /*
1789  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1790  *
1791  * No association with any scsi packet is made and no callback routine is
1792  * specified.
1793  *
1794  * Returns a pointer to sata packet upon successful packet creation.
1795  * Returns NULL, if packet cannot be created.
1796  *
1797  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1798  * only lower 32 bits are available currently.
1799  */
1800 sata_pkt_t *
1801 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1802     uint8_t regn, uint32_t regv, uint32_t type)
1803 {
1804 	sata_hba_inst_t	*sata_hba_inst;
1805 	sata_pkt_txlate_t *spx;
1806 	sata_pkt_t *spkt;
1807 	sata_cmd_t *scmd;
1808 
1809 	/* Only READ/WRITE commands are accepted. */
1810 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1811 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1812 
1813 	mutex_enter(&sata_mutex);
1814 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1815 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1816 		if (SATA_DIP(sata_hba_inst) == dip)
1817 			break;
1818 	}
1819 	mutex_exit(&sata_mutex);
1820 	ASSERT(sata_hba_inst != NULL);
1821 
1822 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1823 	spx->txlt_sata_hba_inst = sata_hba_inst;
1824 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1825 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1826 	if (spkt == NULL) {
1827 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1828 		return (NULL);
1829 	}
1830 
1831 	/*
1832 	 * NOTE: We need to send this command to the port multiplier,
1833 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1834 	 *
1835 	 * sata_device contains the address of actual target device, and the
1836 	 * pmport number in the command comes from the sata_device structure.
1837 	 */
1838 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1839 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1840 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1841 
1842 	/* Fill sata_pkt */
1843 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1844 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1845 	spkt->satapkt_time = 10; /* Timeout 10s */
1846 
1847 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1848 	scmd = &spkt->satapkt_cmd;
1849 	scmd->satacmd_features_reg = regn & 0xff;
1850 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1851 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1852 	scmd->satacmd_addr_type = 0;		/* N/A */
1853 
1854 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1855 
1856 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1857 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1858 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1859 		scmd->satacmd_flags.sata_special_regs = 1;
1860 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1861 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1862 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1863 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1864 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1865 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1866 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1867 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1868 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1869 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1870 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1871 	}
1872 
1873 	return (spkt);
1874 }
1875 
1876 /*
1877  * Free sata packet and any associated resources allocated previously by
1878  * sata_get_rdwr_pmult_pkt().
1879  *
1880  * Void return.
1881  */
1882 void
1883 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1884 {
1885 	sata_pkt_txlate_t *spx =
1886 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1887 
1888 	/* Free allocated resources */
1889 	sata_pkt_free(spx);
1890 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1891 }
1892 
1893 /*
1894  * Register a port multiplier to framework.
1895  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1896  * 2) Search in the blacklist and update the number of the device ports of the
1897  * port multiplier.
1898  *
1899  * Void return.
1900  */
1901 void
1902 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1903 {
1904 	sata_hba_inst_t *sata_hba_inst = NULL;
1905 	sata_pmult_info_t *pmultinfo;
1906 	sata_pmult_bl_t *blp;
1907 	int cport = sd->satadev_addr.cport;
1908 
1909 	mutex_enter(&sata_mutex);
1910 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1911 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1912 		if (SATA_DIP(sata_hba_inst) == dip)
1913 			if (sata_hba_inst->satahba_attached == 1)
1914 				break;
1915 	}
1916 	mutex_exit(&sata_mutex);
1917 	/* HBA not attached? */
1918 	if (sata_hba_inst == NULL)
1919 		return;
1920 
1921 	/* Number of pmports */
1922 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1923 
1924 	/* Check the blacklist */
1925 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1926 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1927 			continue;
1928 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1929 			continue;
1930 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1931 			continue;
1932 
1933 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1934 		sd->satadev_add_info = blp->bl_flags;
1935 		break;
1936 	}
1937 
1938 	/* Register the port multiplier GSCR */
1939 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1940 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1941 	if (pmultinfo != NULL) {
1942 		pmultinfo->pmult_gscr = *sg;
1943 		pmultinfo->pmult_num_dev_ports =
1944 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1945 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1946 		    "Port multiplier registered at port %d", cport);
1947 	}
1948 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1949 }
1950 
1951 /*
1952  * sata_name_child is for composing the name of the node
1953  * the format of the name is "target,0".
1954  */
1955 static int
1956 sata_name_child(dev_info_t *dip, char *name, int namelen)
1957 {
1958 	int target;
1959 
1960 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1961 	    DDI_PROP_DONTPASS, "target", -1);
1962 	if (target == -1)
1963 		return (DDI_FAILURE);
1964 	(void) snprintf(name, namelen, "%x,0", target);
1965 	return (DDI_SUCCESS);
1966 }
1967 
1968 
1969 
1970 /* ****************** SCSA required entry points *********************** */
1971 
1972 /*
1973  * Implementation of scsi tran_tgt_init.
1974  * sata_scsi_tgt_init() initializes scsi_device structure
1975  *
1976  * If successful, DDI_SUCCESS is returned.
1977  * DDI_FAILURE is returned if addressed device does not exist
1978  */
1979 
1980 static int
1981 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1982     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1983 {
1984 #ifndef __lock_lint
1985 	_NOTE(ARGUNUSED(hba_dip))
1986 	_NOTE(ARGUNUSED(tgt_dip))
1987 #endif
1988 	sata_device_t		sata_device;
1989 	sata_drive_info_t	*sdinfo;
1990 	struct sata_id		*sid;
1991 	sata_hba_inst_t		*sata_hba_inst;
1992 	char			model[SATA_ID_MODEL_LEN + 1];
1993 	char			fw[SATA_ID_FW_LEN + 1];
1994 	char			*vid, *pid;
1995 	int			i;
1996 
1997 	/*
1998 	 * Fail tran_tgt_init for .conf stub node
1999 	 */
2000 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2001 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2002 		ddi_set_name_addr(tgt_dip, NULL);
2003 		return (DDI_FAILURE);
2004 	}
2005 
2006 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2007 
2008 	/* Validate scsi device address */
2009 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2010 	    &sata_device) != 0)
2011 		return (DDI_FAILURE);
2012 
2013 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2014 	    sata_device.satadev_addr.cport)));
2015 
2016 	/* sata_device now contains a valid sata address */
2017 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2018 	if (sdinfo == NULL) {
2019 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2020 		    sata_device.satadev_addr.cport)));
2021 		return (DDI_FAILURE);
2022 	}
2023 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2024 	    sata_device.satadev_addr.cport)));
2025 
2026 	/*
2027 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2028 	 * the target disks.
2029 	 *
2030 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2031 	 * if we need to create cmdk-style devid for all the disk devices
2032 	 * attached to this controller. This property may have been set
2033 	 * from HBA driver's .conf file or by the HBA driver in its
2034 	 * attach(9F) function.
2035 	 */
2036 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2037 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2038 	    "use-cmdk-devid-format", 0) == 1)) {
2039 		/* register a legacy devid for this target node */
2040 		sata_target_devid_register(tgt_dip, sdinfo);
2041 	}
2042 
2043 
2044 	/*
2045 	 * 'Identify Device Data' does not always fit in standard SCSI
2046 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2047 	 * of information.
2048 	 */
2049 	sid = &sdinfo->satadrv_id;
2050 #ifdef	_LITTLE_ENDIAN
2051 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2052 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2053 #else	/* _LITTLE_ENDIAN */
2054 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2055 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2056 #endif	/* _LITTLE_ENDIAN */
2057 	model[SATA_ID_MODEL_LEN] = 0;
2058 	fw[SATA_ID_FW_LEN] = 0;
2059 
2060 	/* split model into into vid/pid */
2061 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2062 		if ((*pid == ' ') || (*pid == '\t'))
2063 			break;
2064 	if (i < SATA_ID_MODEL_LEN) {
2065 		vid = model;
2066 		*pid++ = 0;		/* terminate vid, establish pid */
2067 	} else {
2068 		vid = NULL;		/* vid will stay "ATA     " */
2069 		pid = model;		/* model is all pid */
2070 	}
2071 
2072 	if (vid)
2073 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2074 		    vid, strlen(vid));
2075 	if (pid)
2076 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2077 		    pid, strlen(pid));
2078 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2079 	    fw, strlen(fw));
2080 
2081 	return (DDI_SUCCESS);
2082 }
2083 
2084 /*
2085  * Implementation of scsi tran_tgt_probe.
2086  * Probe target, by calling default scsi routine scsi_hba_probe()
2087  */
2088 static int
2089 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2090 {
2091 	sata_hba_inst_t *sata_hba_inst =
2092 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2093 	int rval;
2094 	uint32_t pm_cap;
2095 
2096 	rval = scsi_hba_probe(sd, callback);
2097 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2098 	    SATA_CAP_LOG_SENSE;
2099 
2100 	if (rval == SCSIPROBE_EXISTS) {
2101 		/*
2102 		 * Set property "pm-capable" on the target device node, so that
2103 		 * the target driver will not try to fetch scsi cycle counters
2104 		 * before enabling device power-management.
2105 		 */
2106 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2107 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2108 			sata_log(sata_hba_inst, CE_WARN,
2109 			    "SATA device at port %d: "
2110 			    "will not be power-managed ",
2111 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2112 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2113 			    "failure updating pm-capable property"));
2114 		}
2115 	}
2116 	return (rval);
2117 }
2118 
2119 /*
2120  * Implementation of scsi tran_tgt_free.
2121  * Release all resources allocated for scsi_device
2122  */
2123 static void
2124 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2125     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2126 {
2127 #ifndef __lock_lint
2128 	_NOTE(ARGUNUSED(hba_dip))
2129 #endif
2130 	sata_device_t		sata_device;
2131 	sata_drive_info_t	*sdinfo;
2132 	sata_hba_inst_t		*sata_hba_inst;
2133 	ddi_devid_t		devid;
2134 
2135 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2136 
2137 	/* Validate scsi device address */
2138 	/*
2139 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2140 	 * was a device at this address, so even if the sata framework internal
2141 	 * resources were alredy released because a device was detached,
2142 	 * this function should be executed as long as its actions do
2143 	 * not require the internal sata view of a device and the address
2144 	 * refers to a valid sata address.
2145 	 * Validating the address here means that we do not trust SCSA...
2146 	 */
2147 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2148 	    &sata_device) == -1)
2149 		return;
2150 
2151 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2152 	    sata_device.satadev_addr.cport)));
2153 
2154 	/* sata_device now should contain a valid sata address */
2155 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2156 	if (sdinfo == NULL) {
2157 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2158 		    sata_device.satadev_addr.cport)));
2159 		return;
2160 	}
2161 	/*
2162 	 * We did not allocate any resources in sata_scsi_tgt_init()
2163 	 * other than few properties.
2164 	 * Free them.
2165 	 */
2166 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2167 	    sata_device.satadev_addr.cport)));
2168 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2169 
2170 	/*
2171 	 * If devid was previously created but not freed up from
2172 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2173 	 */
2174 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2175 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2176 	    "use-cmdk-devid-format", 0) == 1) &&
2177 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2178 		ddi_devid_unregister(tgt_dip);
2179 		ddi_devid_free(devid);
2180 	}
2181 }
2182 
2183 /*
2184  * Implementation of scsi tran_init_pkt
2185  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2186  *
2187  * It seems that we should always allocate pkt, even if the address is
2188  * for non-existing device - just use some default for dma_attr.
2189  * The reason is that there is no way to communicate this to a caller here.
2190  * Subsequent call to sata_scsi_start may fail appropriately.
2191  * Simply returning NULL does not seem to discourage a target driver...
2192  *
2193  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2194  */
2195 static struct scsi_pkt *
2196 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2197     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2198     int (*callback)(caddr_t), caddr_t arg)
2199 {
2200 	sata_hba_inst_t *sata_hba_inst =
2201 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2202 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2203 	sata_device_t sata_device;
2204 	sata_drive_info_t *sdinfo;
2205 	sata_pkt_txlate_t *spx;
2206 	ddi_dma_attr_t cur_dma_attr;
2207 	int rval;
2208 	boolean_t new_pkt = B_TRUE;
2209 
2210 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2211 
2212 	/*
2213 	 * We need to translate the address, even if it could be
2214 	 * a bogus one, for a non-existing device
2215 	 */
2216 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2217 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2218 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2219 	sata_device.satadev_rev = SATA_DEVICE_REV;
2220 
2221 	if (pkt == NULL) {
2222 		/*
2223 		 * Have to allocate a brand new scsi packet.
2224 		 * We need to operate with auto request sense enabled.
2225 		 */
2226 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2227 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2228 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2229 
2230 		if (pkt == NULL)
2231 			return (NULL);
2232 
2233 		/* Fill scsi packet structure */
2234 		pkt->pkt_comp		= (void (*)())NULL;
2235 		pkt->pkt_time		= 0;
2236 		pkt->pkt_resid		= 0;
2237 		pkt->pkt_statistics	= 0;
2238 		pkt->pkt_reason		= 0;
2239 
2240 		/*
2241 		 * pkt_hba_private will point to sata pkt txlate structure
2242 		 */
2243 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2244 		bzero(spx, sizeof (sata_pkt_txlate_t));
2245 
2246 		spx->txlt_scsi_pkt = pkt;
2247 		spx->txlt_sata_hba_inst = sata_hba_inst;
2248 
2249 		/* Allocate sata_pkt */
2250 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2251 		if (spx->txlt_sata_pkt == NULL) {
2252 			/* Could not allocate sata pkt */
2253 			scsi_hba_pkt_free(ap, pkt);
2254 			return (NULL);
2255 		}
2256 		/* Set sata address */
2257 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2258 		    sata_device.satadev_addr;
2259 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2260 		    sata_device.satadev_rev;
2261 
2262 		if ((bp == NULL) || (bp->b_bcount == 0))
2263 			return (pkt);
2264 
2265 		spx->txlt_total_residue = bp->b_bcount;
2266 	} else {
2267 		new_pkt = B_FALSE;
2268 		/*
2269 		 * Packet was preallocated/initialized by previous call
2270 		 */
2271 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2272 
2273 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2274 			return (pkt);
2275 		}
2276 
2277 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2278 	}
2279 
2280 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2281 
2282 	/*
2283 	 * We use an adjusted version of the dma_attr, to account
2284 	 * for device addressing limitations.
2285 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2286 	 * happen when a device is not yet configured.
2287 	 */
2288 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2289 	    sata_device.satadev_addr.cport)));
2290 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2291 	    &spx->txlt_sata_pkt->satapkt_device);
2292 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2293 	sata_adjust_dma_attr(sdinfo,
2294 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2295 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2296 	    sata_device.satadev_addr.cport)));
2297 	/*
2298 	 * Allocate necessary DMA resources for the packet's data buffer
2299 	 * NOTE:
2300 	 * In case of read/write commands, DMA resource allocation here is
2301 	 * based on the premise that the transfer length specified in
2302 	 * the read/write scsi cdb will match exactly DMA resources -
2303 	 * returning correct packet residue is crucial.
2304 	 */
2305 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2306 	    &cur_dma_attr)) != DDI_SUCCESS) {
2307 		/*
2308 		 * If a DMA allocation request fails with
2309 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2310 		 * bioerror(9F) with bp and an error code of EFAULT.
2311 		 * If a DMA allocation request fails with
2312 		 * DDI_DMA_TOOBIG, indicate the error by calling
2313 		 * bioerror(9F) with bp and an error code of EINVAL.
2314 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2315 		 * Request may be repeated later - there is no real error.
2316 		 */
2317 		switch (rval) {
2318 		case DDI_DMA_NORESOURCES:
2319 			bioerror(bp, 0);
2320 			break;
2321 		case DDI_DMA_NOMAPPING:
2322 		case DDI_DMA_BADATTR:
2323 			bioerror(bp, EFAULT);
2324 			break;
2325 		case DDI_DMA_TOOBIG:
2326 		default:
2327 			bioerror(bp, EINVAL);
2328 			break;
2329 		}
2330 		goto fail;
2331 	}
2332 
2333 	if (sata_check_for_dma_error(dip, spx)) {
2334 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2335 		bioerror(bp, EFAULT);
2336 		goto fail;
2337 	}
2338 
2339 success:
2340 	/* Set number of bytes that are not yet accounted for */
2341 	pkt->pkt_resid = spx->txlt_total_residue;
2342 	ASSERT(pkt->pkt_resid >= 0);
2343 
2344 	return (pkt);
2345 
2346 fail:
2347 	if (new_pkt == B_TRUE) {
2348 		/*
2349 		 * Since this is a new packet, we can clean-up
2350 		 * everything
2351 		 */
2352 		sata_scsi_destroy_pkt(ap, pkt);
2353 	} else {
2354 		/*
2355 		 * This is a re-used packet. It will be target driver's
2356 		 * responsibility to eventually destroy it (which
2357 		 * will free allocated resources).
2358 		 * Here, we just "complete" the request, leaving
2359 		 * allocated resources intact, so the request may
2360 		 * be retried.
2361 		 */
2362 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2363 		sata_pkt_free(spx);
2364 	}
2365 	return (NULL);
2366 }
2367 
2368 /*
2369  * Implementation of scsi tran_start.
2370  * Translate scsi cmd into sata operation and return status.
2371  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2372  * are supported.
2373  * For SATA hard disks, supported scsi commands:
2374  * SCMD_INQUIRY
2375  * SCMD_TEST_UNIT_READY
2376  * SCMD_START_STOP
2377  * SCMD_READ_CAPACITY
2378  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2379  * SCMD_REQUEST_SENSE
2380  * SCMD_LOG_SENSE_G1
2381  * SCMD_LOG_SELECT_G1
2382  * SCMD_MODE_SENSE	(specific pages)
2383  * SCMD_MODE_SENSE_G1	(specific pages)
2384  * SCMD_MODE_SELECT	(specific pages)
2385  * SCMD_MODE_SELECT_G1	(specific pages)
2386  * SCMD_SYNCHRONIZE_CACHE
2387  * SCMD_SYNCHRONIZE_CACHE_G1
2388  * SCMD_READ
2389  * SCMD_READ_G1
2390  * SCMD_READ_G4
2391  * SCMD_READ_G5
2392  * SCMD_WRITE
2393  * SCMD_WRITE_BUFFER
2394  * SCMD_WRITE_G1
2395  * SCMD_WRITE_G4
2396  * SCMD_WRITE_G5
2397  * SCMD_SEEK		(noop)
2398  * SCMD_SDIAG
2399  *
2400  * All other commands are rejected as unsupported.
2401  *
2402  * Returns:
2403  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2404  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2405  * a callback could be scheduled.
2406  * TRAN_BADPKT if cmd was directed to invalid address.
2407  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2408  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2409  * was removed and there was no callback specified in scsi pkt.
2410  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2411  * framework was busy performing some other operation(s).
2412  *
2413  */
2414 static int
2415 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2416 {
2417 	sata_hba_inst_t *sata_hba_inst =
2418 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2419 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2420 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2421 	sata_drive_info_t *sdinfo;
2422 	struct buf *bp;
2423 	uint8_t cport, pmport;
2424 	boolean_t dev_gone = B_FALSE;
2425 	int rval;
2426 
2427 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2428 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2429 
2430 	ASSERT(spx != NULL &&
2431 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2432 
2433 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2434 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2435 
2436 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2437 
2438 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2439 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2440 		if (sdinfo == NULL ||
2441 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2442 		    cport_tgtnode_clean == B_FALSE ||
2443 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2444 			dev_gone = B_TRUE;
2445 		}
2446 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2447 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2448 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2449 		    cport) == NULL) {
2450 			dev_gone = B_TRUE;
2451 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2452 		    pmport) == NULL) {
2453 			dev_gone = B_TRUE;
2454 		} else {
2455 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2456 			    cport, pmport)));
2457 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2458 			if (sdinfo == NULL ||
2459 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2460 			    pmport_tgtnode_clean == B_FALSE ||
2461 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2462 				dev_gone = B_TRUE;
2463 			}
2464 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2465 			    cport, pmport)));
2466 		}
2467 	}
2468 
2469 	if (dev_gone == B_TRUE) {
2470 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2471 		pkt->pkt_reason = CMD_DEV_GONE;
2472 		/*
2473 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2474 		 * only in callback function (for normal requests) and
2475 		 * in the dump code path.
2476 		 * So, if the callback is available, we need to do
2477 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2478 		 */
2479 		if (pkt->pkt_comp != NULL) {
2480 			/* scsi callback required */
2481 			if (servicing_interrupt()) {
2482 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2483 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2484 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2485 				    NULL) {
2486 					return (TRAN_BUSY);
2487 				}
2488 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2489 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2490 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2491 				/* Scheduling the callback failed */
2492 				return (TRAN_BUSY);
2493 			}
2494 			return (TRAN_ACCEPT);
2495 		}
2496 		/* No callback available */
2497 		return (TRAN_FATAL_ERROR);
2498 	}
2499 
2500 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2501 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2502 		rval = sata_txlt_atapi(spx);
2503 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2504 		    "sata_scsi_start atapi: rval %d\n", rval);
2505 		return (rval);
2506 	}
2507 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2508 
2509 	/*
2510 	 * Checking for power state, if it was on
2511 	 * STOPPED state, then the drive is not capable
2512 	 * of processing media access command.  And
2513 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2514 	 * in the function for different power state.
2515 	 */
2516 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2517 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2518 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2519 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2520 		    SD_SCSI_ASC_LU_NOT_READY));
2521 	}
2522 
2523 	/* ATA Disk commands processing starts here */
2524 
2525 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2526 
2527 	switch (pkt->pkt_cdbp[0]) {
2528 
2529 	case SCMD_INQUIRY:
2530 		/* Mapped to identify device */
2531 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2532 			bp_mapin(bp);
2533 		rval = sata_txlt_inquiry(spx);
2534 		break;
2535 
2536 	case SCMD_TEST_UNIT_READY:
2537 		/*
2538 		 * SAT "SATA to ATA Translation" doc specifies translation
2539 		 * to ATA CHECK POWER MODE.
2540 		 */
2541 		rval = sata_txlt_test_unit_ready(spx);
2542 		break;
2543 
2544 	case SCMD_START_STOP:
2545 		/* Mapping depends on the command */
2546 		rval = sata_txlt_start_stop_unit(spx);
2547 		break;
2548 
2549 	case SCMD_READ_CAPACITY:
2550 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2551 			bp_mapin(bp);
2552 		rval = sata_txlt_read_capacity(spx);
2553 		break;
2554 
2555 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2556 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2557 			bp_mapin(bp);
2558 		rval = sata_txlt_read_capacity16(spx);
2559 		break;
2560 
2561 	case SCMD_REQUEST_SENSE:
2562 		/*
2563 		 * Always No Sense, since we force ARQ
2564 		 */
2565 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2566 			bp_mapin(bp);
2567 		rval = sata_txlt_request_sense(spx);
2568 		break;
2569 
2570 	case SCMD_LOG_SENSE_G1:
2571 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2572 			bp_mapin(bp);
2573 		rval = sata_txlt_log_sense(spx);
2574 		break;
2575 
2576 	case SCMD_LOG_SELECT_G1:
2577 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2578 			bp_mapin(bp);
2579 		rval = sata_txlt_log_select(spx);
2580 		break;
2581 
2582 	case SCMD_MODE_SENSE:
2583 	case SCMD_MODE_SENSE_G1:
2584 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2585 			bp_mapin(bp);
2586 		rval = sata_txlt_mode_sense(spx);
2587 		break;
2588 
2589 
2590 	case SCMD_MODE_SELECT:
2591 	case SCMD_MODE_SELECT_G1:
2592 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2593 			bp_mapin(bp);
2594 		rval = sata_txlt_mode_select(spx);
2595 		break;
2596 
2597 	case SCMD_SYNCHRONIZE_CACHE:
2598 	case SCMD_SYNCHRONIZE_CACHE_G1:
2599 		rval = sata_txlt_synchronize_cache(spx);
2600 		break;
2601 
2602 	case SCMD_READ:
2603 	case SCMD_READ_G1:
2604 	case SCMD_READ_G4:
2605 	case SCMD_READ_G5:
2606 		rval = sata_txlt_read(spx);
2607 		break;
2608 	case SCMD_WRITE_BUFFER:
2609 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2610 			bp_mapin(bp);
2611 		rval = sata_txlt_write_buffer(spx);
2612 		break;
2613 
2614 	case SCMD_WRITE:
2615 	case SCMD_WRITE_G1:
2616 	case SCMD_WRITE_G4:
2617 	case SCMD_WRITE_G5:
2618 		rval = sata_txlt_write(spx);
2619 		break;
2620 
2621 	case SCMD_SEEK:
2622 		rval = sata_txlt_nodata_cmd_immediate(spx);
2623 		break;
2624 
2625 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2626 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2627 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2628 			bp_mapin(bp);
2629 		rval = sata_txlt_ata_pass_thru(spx);
2630 		break;
2631 
2632 		/* Other cases will be filed later */
2633 		/* postponed until phase 2 of the development */
2634 	case SPC3_CMD_UNMAP:
2635 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2636 			bp_mapin(bp);
2637 		rval = sata_txlt_unmap(spx);
2638 		break;
2639 	default:
2640 		rval = sata_txlt_invalid_command(spx);
2641 		break;
2642 	}
2643 
2644 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2645 	    "sata_scsi_start: rval %d\n", rval);
2646 
2647 	return (rval);
2648 }
2649 
2650 /*
2651  * Implementation of scsi tran_abort.
2652  * Abort specific pkt or all packets.
2653  *
2654  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2655  *
2656  * May be called from an interrupt level.
2657  */
2658 static int
2659 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2660 {
2661 	sata_hba_inst_t *sata_hba_inst =
2662 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2663 	sata_device_t	sata_device;
2664 	sata_pkt_t	*sata_pkt;
2665 
2666 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2667 	    "sata_scsi_abort: %s at target: 0x%x\n",
2668 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2669 
2670 	/* Validate address */
2671 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2672 		/* Invalid address */
2673 		return (0);
2674 
2675 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2676 	    sata_device.satadev_addr.cport)));
2677 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2678 		/* invalid address */
2679 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2680 		    sata_device.satadev_addr.cport)));
2681 		return (0);
2682 	}
2683 	if (scsi_pkt == NULL) {
2684 		/*
2685 		 * Abort all packets.
2686 		 * Although we do not have specific packet, we still need
2687 		 * dummy packet structure to pass device address to HBA.
2688 		 * Allocate one, without sleeping. Fail if pkt cannot be
2689 		 * allocated.
2690 		 */
2691 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2692 		if (sata_pkt == NULL) {
2693 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2694 			    sata_device.satadev_addr.cport)));
2695 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2696 			    "could not allocate sata_pkt"));
2697 			return (0);
2698 		}
2699 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2700 		sata_pkt->satapkt_device = sata_device;
2701 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2702 	} else {
2703 		if (scsi_pkt->pkt_ha_private == NULL) {
2704 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2705 			    sata_device.satadev_addr.cport)));
2706 			return (0); /* Bad scsi pkt */
2707 		}
2708 		/* extract pointer to sata pkt */
2709 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2710 		    txlt_sata_pkt;
2711 	}
2712 
2713 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2714 	    sata_device.satadev_addr.cport)));
2715 	/* Send abort request to HBA */
2716 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2717 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2718 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2719 	    SATA_SUCCESS) {
2720 		if (scsi_pkt == NULL)
2721 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2722 		/* Success */
2723 		return (1);
2724 	}
2725 	/* Else, something did not go right */
2726 	if (scsi_pkt == NULL)
2727 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2728 	/* Failure */
2729 	return (0);
2730 }
2731 
2732 
2733 /*
2734  * Implementation of scsi tran_reset.
2735  * RESET_ALL request is translated into port reset.
2736  * RESET_TARGET requests is translated into a device reset,
2737  * RESET_LUN request is accepted only for LUN 0 and translated into
2738  * device reset.
2739  * The target reset should cause all HBA active and queued packets to
2740  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2741  * the return. HBA should report reset event for the device.
2742  *
2743  * Returns 1 upon success, 0 upon failure.
2744  */
2745 static int
2746 sata_scsi_reset(struct scsi_address *ap, int level)
2747 {
2748 	sata_hba_inst_t	*sata_hba_inst =
2749 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2750 	sata_device_t	sata_device;
2751 	int		val;
2752 
2753 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2754 	    "sata_scsi_reset: level %d target: 0x%x\n",
2755 	    level, ap->a_target);
2756 
2757 	/* Validate address */
2758 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2759 	if (val == -1)
2760 		/* Invalid address */
2761 		return (0);
2762 
2763 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2764 	    sata_device.satadev_addr.cport)));
2765 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2766 		/* invalid address */
2767 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2768 		    sata_device.satadev_addr.cport)));
2769 		return (0);
2770 	}
2771 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2772 	    sata_device.satadev_addr.cport)));
2773 	if (level == RESET_ALL) {
2774 		/* port reset */
2775 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2776 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2777 		else
2778 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2779 
2780 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2781 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2782 			return (1);
2783 		else
2784 			return (0);
2785 
2786 	} else if (val == 0 &&
2787 	    (level == RESET_TARGET || level == RESET_LUN)) {
2788 		/* reset device (device attached) */
2789 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2790 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2791 			return (1);
2792 		else
2793 			return (0);
2794 	}
2795 	return (0);
2796 }
2797 
2798 
2799 /*
2800  * Implementation of scsi tran_getcap (get transport/device capabilities).
2801  * Supported capabilities for SATA hard disks:
2802  * auto-rqsense		(always supported)
2803  * tagged-qing		(supported if HBA supports it)
2804  * untagged-qing	(could be supported if disk supports it, but because
2805  *			 caching behavior allowing untagged queuing actually
2806  *			 results in reduced performance.  sd tries to throttle
2807  *			 back to only 3 outstanding commands, which may
2808  *			 work for real SCSI disks, but with read ahead
2809  *			 caching, having more than 1 outstanding command
2810  *			 results in cache thrashing.)
2811  * sector_size
2812  * dma_max
2813  * interconnect-type	(INTERCONNECT_SATA)
2814  *
2815  * Supported capabilities for ATAPI CD/DVD devices:
2816  * auto-rqsense		(always supported)
2817  * sector_size
2818  * dma_max
2819  * max-cdb-length
2820  * interconnect-type	(INTERCONNECT_SATA)
2821  *
2822  * Supported capabilities for ATAPI TAPE devices:
2823  * auto-rqsense		(always supported)
2824  * dma_max
2825  * max-cdb-length
2826  *
2827  * Supported capabilities for SATA ATAPI hard disks:
2828  * auto-rqsense		(always supported)
2829  * interconnect-type	(INTERCONNECT_SATA)
2830  * max-cdb-length
2831  *
2832  * Request for other capabilities is rejected as unsupported.
2833  *
2834  * Returns supported capability value, or -1 if capability is unsuppported or
2835  * the address is invalid - no device.
2836  */
2837 
2838 static int
2839 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2840 {
2841 
2842 	sata_hba_inst_t 	*sata_hba_inst =
2843 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2844 	sata_device_t		sata_device;
2845 	sata_drive_info_t	*sdinfo;
2846 	ddi_dma_attr_t		adj_dma_attr;
2847 	int 			rval;
2848 
2849 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2850 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2851 	    ap->a_target, cap);
2852 
2853 	/*
2854 	 * We want to process the capabilities on per port granularity.
2855 	 * So, we are specifically restricting ourselves to whom != 0
2856 	 * to exclude the controller wide handling.
2857 	 */
2858 	if (cap == NULL || whom == 0)
2859 		return (-1);
2860 
2861 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2862 		/* Invalid address */
2863 		return (-1);
2864 	}
2865 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2866 	    sata_device.satadev_addr.cport)));
2867 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2868 	    NULL) {
2869 		/* invalid address */
2870 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2871 		    sata_device.satadev_addr.cport)));
2872 		return (-1);
2873 	}
2874 
2875 	switch (scsi_hba_lookup_capstr(cap)) {
2876 	case SCSI_CAP_ARQ:
2877 		rval = 1;		/* ARQ supported, turned on */
2878 		break;
2879 
2880 	case SCSI_CAP_SECTOR_SIZE:
2881 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2882 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2883 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2884 			rval = SATA_ATAPI_SECTOR_SIZE;
2885 		else rval = -1;
2886 		break;
2887 
2888 	/*
2889 	 * untagged queuing cause a performance inversion because of
2890 	 * the way sd operates.  Because of this reason we do not
2891 	 * use it when available.
2892 	 */
2893 	case SCSI_CAP_UNTAGGED_QING:
2894 		if (sdinfo->satadrv_features_enabled &
2895 		    SATA_DEV_F_E_UNTAGGED_QING)
2896 			rval = 1;	/* Untagged queuing available */
2897 		else
2898 			rval = -1;	/* Untagged queuing not available */
2899 		break;
2900 
2901 	case SCSI_CAP_TAGGED_QING:
2902 		if ((sdinfo->satadrv_features_enabled &
2903 		    SATA_DEV_F_E_TAGGED_QING) &&
2904 		    (sdinfo->satadrv_max_queue_depth > 1))
2905 			rval = 1;	/* Tagged queuing available */
2906 		else
2907 			rval = -1;	/* Tagged queuing not available */
2908 		break;
2909 
2910 	case SCSI_CAP_DMA_MAX:
2911 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2912 		    &adj_dma_attr);
2913 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2914 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2915 		break;
2916 
2917 	case SCSI_CAP_INTERCONNECT_TYPE:
2918 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2919 		break;
2920 
2921 	case SCSI_CAP_CDB_LEN:
2922 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2923 			rval = sdinfo->satadrv_atapi_cdb_len;
2924 		else
2925 			rval = -1;
2926 		break;
2927 
2928 	default:
2929 		rval = -1;
2930 		break;
2931 	}
2932 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2933 	    sata_device.satadev_addr.cport)));
2934 	return (rval);
2935 }
2936 
2937 /*
2938  * Implementation of scsi tran_setcap
2939  *
2940  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2941  *
2942  */
2943 static int
2944 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2945 {
2946 	sata_hba_inst_t	*sata_hba_inst =
2947 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2948 	sata_device_t	sata_device;
2949 	sata_drive_info_t	*sdinfo;
2950 	int		rval;
2951 
2952 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2953 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2954 
2955 	/*
2956 	 * We want to process the capabilities on per port granularity.
2957 	 * So, we are specifically restricting ourselves to whom != 0
2958 	 * to exclude the controller wide handling.
2959 	 */
2960 	if (cap == NULL || whom == 0) {
2961 		return (-1);
2962 	}
2963 
2964 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2965 		/* Invalid address */
2966 		return (-1);
2967 	}
2968 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2969 	    sata_device.satadev_addr.cport)));
2970 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2971 	    &sata_device)) == NULL) {
2972 		/* invalid address */
2973 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2974 		    sata_device.satadev_addr.cport)));
2975 		return (-1);
2976 	}
2977 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2978 	    sata_device.satadev_addr.cport)));
2979 
2980 	switch (scsi_hba_lookup_capstr(cap)) {
2981 	case SCSI_CAP_ARQ:
2982 	case SCSI_CAP_SECTOR_SIZE:
2983 	case SCSI_CAP_DMA_MAX:
2984 	case SCSI_CAP_INTERCONNECT_TYPE:
2985 		rval = 0;
2986 		break;
2987 	case SCSI_CAP_UNTAGGED_QING:
2988 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2989 			rval = 1;
2990 			if (value == 1) {
2991 				sdinfo->satadrv_features_enabled |=
2992 				    SATA_DEV_F_E_UNTAGGED_QING;
2993 			} else if (value == 0) {
2994 				sdinfo->satadrv_features_enabled &=
2995 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2996 			} else {
2997 				rval = -1;
2998 			}
2999 		} else {
3000 			rval = 0;
3001 		}
3002 		break;
3003 	case SCSI_CAP_TAGGED_QING:
3004 		/* This can TCQ or NCQ */
3005 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3006 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3007 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3008 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3009 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3010 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3011 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3012 			rval = 1;
3013 			if (value == 1) {
3014 				sdinfo->satadrv_features_enabled |=
3015 				    SATA_DEV_F_E_TAGGED_QING;
3016 			} else if (value == 0) {
3017 				sdinfo->satadrv_features_enabled &=
3018 				    ~SATA_DEV_F_E_TAGGED_QING;
3019 			} else {
3020 				rval = -1;
3021 			}
3022 		} else {
3023 			rval = 0;
3024 		}
3025 		break;
3026 	default:
3027 		rval = -1;
3028 		break;
3029 	}
3030 	return (rval);
3031 }
3032 
3033 /*
3034  * Implementations of scsi tran_destroy_pkt.
3035  * Free resources allocated by sata_scsi_init_pkt()
3036  */
3037 static void
3038 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3039 {
3040 	sata_pkt_txlate_t *spx;
3041 
3042 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3043 
3044 	sata_common_free_dma_rsrcs(spx);
3045 
3046 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3047 	sata_pkt_free(spx);
3048 
3049 	scsi_hba_pkt_free(ap, pkt);
3050 }
3051 
3052 /*
3053  * Implementation of scsi tran_dmafree.
3054  * Free DMA resources allocated by sata_scsi_init_pkt()
3055  */
3056 
3057 static void
3058 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3059 {
3060 #ifndef __lock_lint
3061 	_NOTE(ARGUNUSED(ap))
3062 #endif
3063 	sata_pkt_txlate_t *spx;
3064 
3065 	ASSERT(pkt != NULL);
3066 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3067 
3068 	sata_common_free_dma_rsrcs(spx);
3069 }
3070 
3071 /*
3072  * Implementation of scsi tran_sync_pkt.
3073  *
3074  * The assumption below is that pkt is unique - there is no need to check ap
3075  *
3076  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3077  * into/from the real buffer.
3078  */
3079 static void
3080 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3081 {
3082 #ifndef __lock_lint
3083 	_NOTE(ARGUNUSED(ap))
3084 #endif
3085 	int rval;
3086 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3087 	struct buf *bp;
3088 	int direction;
3089 
3090 	ASSERT(spx != NULL);
3091 	if (spx->txlt_buf_dma_handle != NULL) {
3092 		direction = spx->txlt_sata_pkt->
3093 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3094 		if (spx->txlt_sata_pkt != NULL &&
3095 		    direction != SATA_DIR_NODATA_XFER) {
3096 			if (spx->txlt_tmp_buf != NULL) {
3097 				/* Intermediate DMA buffer used */
3098 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3099 
3100 				if (direction & SATA_DIR_WRITE) {
3101 					bcopy(bp->b_un.b_addr,
3102 					    spx->txlt_tmp_buf, bp->b_bcount);
3103 				}
3104 			}
3105 			/* Sync the buffer for device or for CPU */
3106 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3107 			    (direction & SATA_DIR_WRITE) ?
3108 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3109 			ASSERT(rval == DDI_SUCCESS);
3110 			if (spx->txlt_tmp_buf != NULL &&
3111 			    !(direction & SATA_DIR_WRITE)) {
3112 				/* Intermediate DMA buffer used for read */
3113 				bcopy(spx->txlt_tmp_buf,
3114 				    bp->b_un.b_addr, bp->b_bcount);
3115 			}
3116 
3117 		}
3118 	}
3119 }
3120 
3121 
3122 
3123 /* *******************  SATA - SCSI Translation functions **************** */
3124 /*
3125  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3126  * translation.
3127  */
3128 
3129 /*
3130  * Checks if a device exists and can be access and translates common
3131  * scsi_pkt data to sata_pkt data.
3132  *
3133  * Flag argument indicates that a non-read/write ATA command may be sent
3134  * to HBA in arbitrary SYNC mode to execute this packet.
3135  *
3136  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3137  * sata_pkt was set-up.
3138  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3139  * exist and pkt_comp callback was scheduled.
3140  * Returns other TRAN_XXXXX values when error occured and command should be
3141  * rejected with the returned TRAN_XXXXX value.
3142  *
3143  * This function should be called with port mutex held.
3144  */
3145 static int
3146 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3147 {
3148 	sata_drive_info_t *sdinfo;
3149 	sata_device_t sata_device;
3150 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3151 		SATA_DIR_NODATA_XFER,
3152 		/* all other values to 0/FALSE */
3153 	};
3154 	/*
3155 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3156 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3157 	 * indicates that the scsi packet was not accepted (the reason will not
3158 	 * be checked by the scsi target driver).
3159 	 * To make debugging easier, we set pkt_reason to know value here.
3160 	 * It may be changed later when different completion reason is
3161 	 * determined.
3162 	 */
3163 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3164 	*reason = CMD_TRAN_ERR;
3165 
3166 	/* Validate address */
3167 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3168 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3169 
3170 	case -1:
3171 		/* Invalid address or invalid device type */
3172 		return (TRAN_BADPKT);
3173 	case 2:
3174 		/*
3175 		 * Valid address but device type is unknown - Chack if it is
3176 		 * in the reset state and therefore in an indeterminate state.
3177 		 */
3178 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3179 		    &spx->txlt_sata_pkt->satapkt_device);
3180 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3181 		    (SATA_EVNT_DEVICE_RESET |
3182 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3183 			if (!ddi_in_panic()) {
3184 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3185 				*reason = CMD_INCOMPLETE;
3186 				SATADBG1(SATA_DBG_SCSI_IF,
3187 				    spx->txlt_sata_hba_inst,
3188 				    "sata_scsi_start: rejecting command "
3189 				    "because of device reset state\n", NULL);
3190 				return (TRAN_BUSY);
3191 			}
3192 		}
3193 		/* FALLTHROUGH */
3194 	case 1:
3195 		/* valid address but no valid device - it has disappeared */
3196 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3197 		*reason = CMD_DEV_GONE;
3198 		/*
3199 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3200 		 * only in callback function (for normal requests) and
3201 		 * in the dump code path.
3202 		 * So, if the callback is available, we need to do
3203 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3204 		 */
3205 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3206 			/* scsi callback required */
3207 			if (servicing_interrupt()) {
3208 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3209 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3210 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3211 				    NULL) {
3212 					return (TRAN_BUSY);
3213 				}
3214 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3215 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3216 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3217 				/* Scheduling the callback failed */
3218 				return (TRAN_BUSY);
3219 			}
3220 
3221 			return (TRAN_ACCEPT);
3222 		}
3223 		return (TRAN_FATAL_ERROR);
3224 	default:
3225 		/* all OK; pkt reason will be overwritten later */
3226 		break;
3227 	}
3228 	/*
3229 	 * If pkt is to be executed in polling mode and a command will not be
3230 	 * emulated in SATA module (requires sending a non-read/write ATA
3231 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3232 	 * interrupt context and not in the panic dump, then reject the packet
3233 	 * to avoid a possible interrupt stack overrun or hang caused by
3234 	 * a potentially blocked interrupt.
3235 	 */
3236 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3237 	    servicing_interrupt() && !ddi_in_panic()) {
3238 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3239 		    "sata_scsi_start: rejecting synchronous command because "
3240 		    "of interrupt context\n", NULL);
3241 		return (TRAN_BUSY);
3242 	}
3243 
3244 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3245 	    &spx->txlt_sata_pkt->satapkt_device);
3246 
3247 	/*
3248 	 * If device is in reset condition, reject the packet with
3249 	 * TRAN_BUSY, unless:
3250 	 * 1. system is panicking (dumping)
3251 	 * In such case only one thread is running and there is no way to
3252 	 * process reset.
3253 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3254 	 * Some cfgadm operations involve drive commands, so reset condition
3255 	 * needs to be ignored for IOCTL operations.
3256 	 */
3257 	if ((sdinfo->satadrv_event_flags &
3258 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3259 
3260 		if (!ddi_in_panic() &&
3261 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3262 		    sata_device.satadev_addr.cport) &
3263 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3264 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3265 			*reason = CMD_INCOMPLETE;
3266 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3267 			    "sata_scsi_start: rejecting command because "
3268 			    "of device reset state\n", NULL);
3269 			return (TRAN_BUSY);
3270 		}
3271 	}
3272 
3273 	/*
3274 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3275 	 * sata_scsi_pkt_init() because pkt init had to work also with
3276 	 * non-existing devices.
3277 	 * Now we know that the packet was set-up for a real device, so its
3278 	 * type is known.
3279 	 */
3280 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3281 
3282 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3283 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3284 	    sata_device.satadev_addr.cport)->cport_event_flags &
3285 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3286 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3287 		    sata_ignore_dev_reset = B_TRUE;
3288 	}
3289 	/*
3290 	 * At this point the generic translation routine determined that the
3291 	 * scsi packet should be accepted. Packet completion reason may be
3292 	 * changed later when a different completion reason is determined.
3293 	 */
3294 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3295 	*reason = CMD_CMPLT;
3296 
3297 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3298 		/* Synchronous execution */
3299 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3300 		    SATA_OPMODE_POLLING;
3301 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3302 		    sata_ignore_dev_reset = ddi_in_panic();
3303 	} else {
3304 		/* Asynchronous execution */
3305 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3306 		    SATA_OPMODE_INTERRUPTS;
3307 	}
3308 	/* Convert queuing information */
3309 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3310 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3311 		    B_TRUE;
3312 	else if (spx->txlt_scsi_pkt->pkt_flags &
3313 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3314 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3315 		    B_TRUE;
3316 
3317 	/* Always limit pkt time */
3318 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3319 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3320 	else
3321 		/* Pass on scsi_pkt time */
3322 		spx->txlt_sata_pkt->satapkt_time =
3323 		    spx->txlt_scsi_pkt->pkt_time;
3324 
3325 	return (TRAN_ACCEPT);
3326 }
3327 
3328 
3329 /*
3330  * Translate ATA Identify Device data to SCSI Inquiry data.
3331  * This function may be called only for ATA devices.
3332  * This function should not be called for ATAPI devices - they
3333  * respond directly to SCSI Inquiry command.
3334  *
3335  * SATA Identify Device data has to be valid in sata_drive_info.
3336  * Buffer has to accomodate the inquiry length (36 bytes).
3337  *
3338  * This function should be called with a port mutex held.
3339  */
3340 static	void
3341 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3342     sata_drive_info_t *sdinfo, uint8_t *buf)
3343 {
3344 
3345 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3346 	struct sata_id *sid = &sdinfo->satadrv_id;
3347 
3348 	/* Start with a nice clean slate */
3349 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3350 
3351 	/*
3352 	 * Rely on the dev_type for setting paripheral qualifier.
3353 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3354 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3355 	 * ATAPI Inquiry may provide more data to the target driver.
3356 	 */
3357 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3358 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3359 
3360 	/* CFA type device is not a removable media device */
3361 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3362 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3363 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3364 	inq->inq_iso = 0;	/* ISO version */
3365 	inq->inq_ecma = 0;	/* ECMA version */
3366 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3367 	inq->inq_aenc = 0;	/* Async event notification cap. */
3368 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3369 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3370 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3371 	inq->inq_len = 31;	/* Additional length */
3372 	inq->inq_dualp = 0;	/* dual port device - NO */
3373 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3374 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3375 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3376 				/*
3377 				 * Queuing support - controller has to
3378 				 * support some sort of command queuing.
3379 				 */
3380 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3381 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3382 	else
3383 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3384 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3385 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3386 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3387 
3388 #ifdef	_LITTLE_ENDIAN
3389 	/* Swap text fields to match SCSI format */
3390 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3391 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3392 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3393 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3394 	else
3395 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3396 #else	/* _LITTLE_ENDIAN */
3397 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3398 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3399 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3400 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3401 	else
3402 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3403 #endif	/* _LITTLE_ENDIAN */
3404 }
3405 
3406 
3407 /*
3408  * Scsi response set up for invalid command (command not supported)
3409  *
3410  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3411  */
3412 static int
3413 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3414 {
3415 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3416 	struct scsi_extended_sense *sense;
3417 
3418 	scsipkt->pkt_reason = CMD_CMPLT;
3419 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3420 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3421 
3422 	*scsipkt->pkt_scbp = STATUS_CHECK;
3423 
3424 	sense = sata_arq_sense(spx);
3425 	sense->es_key = KEY_ILLEGAL_REQUEST;
3426 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3427 
3428 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3429 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3430 
3431 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3432 	    scsipkt->pkt_comp != NULL) {
3433 		/* scsi callback required */
3434 		if (servicing_interrupt()) {
3435 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3436 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3437 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3438 				return (TRAN_BUSY);
3439 			}
3440 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3441 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3442 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3443 			/* Scheduling the callback failed */
3444 			return (TRAN_BUSY);
3445 		}
3446 	}
3447 	return (TRAN_ACCEPT);
3448 }
3449 
3450 /*
3451  * Scsi response set up for check condition with special sense key
3452  * and additional sense code.
3453  *
3454  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3455  */
3456 static int
3457 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3458 {
3459 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3460 	int cport = SATA_TXLT_CPORT(spx);
3461 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3462 	struct scsi_extended_sense *sense;
3463 
3464 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3465 	scsipkt->pkt_reason = CMD_CMPLT;
3466 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3467 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3468 
3469 	*scsipkt->pkt_scbp = STATUS_CHECK;
3470 
3471 	sense = sata_arq_sense(spx);
3472 	sense->es_key = key;
3473 	sense->es_add_code = code;
3474 
3475 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3476 
3477 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3478 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3479 
3480 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3481 	    scsipkt->pkt_comp != NULL) {
3482 		/* scsi callback required */
3483 		if (servicing_interrupt()) {
3484 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3485 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3486 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3487 				return (TRAN_BUSY);
3488 			}
3489 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3490 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3491 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3492 			/* Scheduling the callback failed */
3493 			return (TRAN_BUSY);
3494 		}
3495 	}
3496 	return (TRAN_ACCEPT);
3497 }
3498 
3499 /*
3500  * Scsi response setup for
3501  * emulated non-data command that requires no action/return data
3502  *
3503  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3504  */
3505 static	int
3506 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3507 {
3508 	int rval;
3509 	int reason;
3510 
3511 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3512 
3513 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3514 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3515 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3516 		return (rval);
3517 	}
3518 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3519 
3520 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3521 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3522 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3523 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3524 
3525 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3526 	    "Scsi_pkt completion reason %x\n",
3527 	    spx->txlt_scsi_pkt->pkt_reason);
3528 
3529 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3530 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3531 		/* scsi callback required */
3532 		if (servicing_interrupt()) {
3533 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3534 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3535 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3536 				return (TRAN_BUSY);
3537 			}
3538 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3539 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3540 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3541 			/* Scheduling the callback failed */
3542 			return (TRAN_BUSY);
3543 		}
3544 	}
3545 	return (TRAN_ACCEPT);
3546 }
3547 
3548 
3549 /*
3550  * SATA translate command: Inquiry / Identify Device
3551  * Use cached Identify Device data for now, rather than issuing actual
3552  * Device Identify cmd request. If device is detached and re-attached,
3553  * asynchronous event processing should fetch and refresh Identify Device
3554  * data.
3555  * Two VPD pages are supported now:
3556  * Vital Product Data page
3557  * Unit Serial Number page
3558  *
3559  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3560  */
3561 
3562 #define	EVPD			1	/* Extended Vital Product Data flag */
3563 #define	CMDDT			2	/* Command Support Data - Obsolete */
3564 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3565 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3566 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3567 					/* Code */
3568 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3569 
3570 static int
3571 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3572 {
3573 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3574 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3575 	sata_drive_info_t *sdinfo;
3576 	struct scsi_extended_sense *sense;
3577 	int count;
3578 	uint8_t *p;
3579 	int i, j;
3580 	uint8_t page_buf[0xff]; /* Max length */
3581 	int rval, reason;
3582 	ushort_t rate;
3583 
3584 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3585 
3586 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3587 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3588 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3589 		return (rval);
3590 	}
3591 
3592 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3593 	    &spx->txlt_sata_pkt->satapkt_device);
3594 
3595 	ASSERT(sdinfo != NULL);
3596 
3597 	scsipkt->pkt_reason = CMD_CMPLT;
3598 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3599 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3600 
3601 	/* Reject not supported request */
3602 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3603 		*scsipkt->pkt_scbp = STATUS_CHECK;
3604 		sense = sata_arq_sense(spx);
3605 		sense->es_key = KEY_ILLEGAL_REQUEST;
3606 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3607 		goto done;
3608 	}
3609 
3610 	/* Valid Inquiry request */
3611 	*scsipkt->pkt_scbp = STATUS_GOOD;
3612 
3613 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3614 
3615 		/*
3616 		 * Because it is fully emulated command storing data
3617 		 * programatically in the specified buffer, release
3618 		 * preallocated DMA resources before storing data in the buffer,
3619 		 * so no unwanted DMA sync would take place.
3620 		 */
3621 		sata_scsi_dmafree(NULL, scsipkt);
3622 
3623 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3624 			/* Standard Inquiry Data request */
3625 			struct scsi_inquiry inq;
3626 			unsigned int bufsize;
3627 
3628 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3629 			    sdinfo, (uint8_t *)&inq);
3630 			/* Copy no more than requested */
3631 			count = MIN(bp->b_bcount,
3632 			    sizeof (struct scsi_inquiry));
3633 			bufsize = scsipkt->pkt_cdbp[4];
3634 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3635 			count = MIN(count, bufsize);
3636 			bcopy(&inq, bp->b_un.b_addr, count);
3637 
3638 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3639 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3640 			    bufsize - count : 0;
3641 		} else {
3642 			/*
3643 			 * peripheral_qualifier = 0;
3644 			 *
3645 			 * We are dealing only with HD and will be
3646 			 * dealing with CD/DVD devices soon
3647 			 */
3648 			uint8_t peripheral_device_type =
3649 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3650 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3651 
3652 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3653 			case INQUIRY_SUP_VPD_PAGE:
3654 				/*
3655 				 * Request for suported Vital Product Data
3656 				 * pages - assuming only 2 page codes
3657 				 * supported.
3658 				 */
3659 				page_buf[0] = peripheral_device_type;
3660 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3661 				page_buf[2] = 0;
3662 				page_buf[3] = 3; /* page length */
3663 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3664 				page_buf[5] = INQUIRY_USN_PAGE;
3665 				page_buf[6] = INQUIRY_BDC_PAGE;
3666 				/* Copy no more than requested */
3667 				count = MIN(bp->b_bcount, 7);
3668 				bcopy(page_buf, bp->b_un.b_addr, count);
3669 				break;
3670 
3671 			case INQUIRY_USN_PAGE:
3672 				/*
3673 				 * Request for Unit Serial Number page.
3674 				 * Set-up the page.
3675 				 */
3676 				page_buf[0] = peripheral_device_type;
3677 				page_buf[1] = INQUIRY_USN_PAGE;
3678 				page_buf[2] = 0;
3679 				/* remaining page length */
3680 				page_buf[3] = SATA_ID_SERIAL_LEN;
3681 
3682 				/*
3683 				 * Copy serial number from Identify Device data
3684 				 * words into the inquiry page and swap bytes
3685 				 * when necessary.
3686 				 */
3687 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3688 #ifdef	_LITTLE_ENDIAN
3689 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3690 #else
3691 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3692 #endif
3693 				/*
3694 				 * Least significant character of the serial
3695 				 * number shall appear as the last byte,
3696 				 * according to SBC-3 spec.
3697 				 * Count trailing spaces to determine the
3698 				 * necessary shift length.
3699 				 */
3700 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3701 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3702 					if (*(p - j) != '\0' &&
3703 					    *(p - j) != '\040')
3704 						break;
3705 				}
3706 
3707 				/*
3708 				 * Shift SN string right, so that the last
3709 				 * non-blank character would appear in last
3710 				 * byte of SN field in the page.
3711 				 * 'j' is the shift length.
3712 				 */
3713 				for (i = 0;
3714 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3715 				    i++, p--)
3716 					*p = *(p - j);
3717 
3718 				/*
3719 				 * Add leading spaces - same number as the
3720 				 * shift size
3721 				 */
3722 				for (; j > 0; j--)
3723 					page_buf[4 + j - 1] = '\040';
3724 
3725 				count = MIN(bp->b_bcount,
3726 				    SATA_ID_SERIAL_LEN + 4);
3727 				bcopy(page_buf, bp->b_un.b_addr, count);
3728 				break;
3729 
3730 			case INQUIRY_BDC_PAGE:
3731 				/*
3732 				 * Request for Block Device Characteristics
3733 				 * page.  Set-up the page.
3734 				 */
3735 				page_buf[0] = peripheral_device_type;
3736 				page_buf[1] = INQUIRY_BDC_PAGE;
3737 				page_buf[2] = 0;
3738 				/* remaining page length */
3739 				page_buf[3] = SATA_ID_BDC_LEN;
3740 
3741 				rate = sdinfo->satadrv_id.ai_medrotrate;
3742 				page_buf[4] = (rate >> 8) & 0xff;
3743 				page_buf[5] = rate & 0xff;
3744 				page_buf[6] = 0;
3745 				page_buf[7] = sdinfo->satadrv_id.
3746 				    ai_nomformfactor & 0xf;
3747 
3748 				count = MIN(bp->b_bcount,
3749 				    SATA_ID_BDC_LEN + 4);
3750 				bcopy(page_buf, bp->b_un.b_addr, count);
3751 				break;
3752 
3753 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3754 				/*
3755 				 * We may want to implement this page, when
3756 				 * identifiers are common for SATA devices
3757 				 * But not now.
3758 				 */
3759 				/*FALLTHROUGH*/
3760 
3761 			default:
3762 				/* Request for unsupported VPD page */
3763 				*scsipkt->pkt_scbp = STATUS_CHECK;
3764 				sense = sata_arq_sense(spx);
3765 				sense->es_key = KEY_ILLEGAL_REQUEST;
3766 				sense->es_add_code =
3767 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3768 				goto done;
3769 			}
3770 		}
3771 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3772 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3773 		    scsipkt->pkt_cdbp[4] - count : 0;
3774 	}
3775 done:
3776 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3777 
3778 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3779 	    "Scsi_pkt completion reason %x\n",
3780 	    scsipkt->pkt_reason);
3781 
3782 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3783 	    scsipkt->pkt_comp != NULL) {
3784 		/* scsi callback required */
3785 		if (servicing_interrupt()) {
3786 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3787 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3788 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3789 				return (TRAN_BUSY);
3790 			}
3791 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3792 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3793 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3794 			/* Scheduling the callback failed */
3795 			return (TRAN_BUSY);
3796 		}
3797 	}
3798 	return (TRAN_ACCEPT);
3799 }
3800 
3801 /*
3802  * SATA translate command: Request Sense.
3803  *
3804  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3805  * At the moment this is an emulated command (ATA version for SATA hard disks).
3806  * May be translated into Check Power Mode command in the future.
3807  *
3808  * Note: There is a mismatch between already implemented Informational
3809  * Exception Mode Select page 0x1C and this function.
3810  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3811  * NO SENSE and set additional sense code to the exception code - this is not
3812  * implemented here.
3813  */
3814 static int
3815 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3816 {
3817 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3818 	struct scsi_extended_sense sense;
3819 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3820 	sata_drive_info_t *sdinfo;
3821 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3822 	int rval, reason, power_state = 0;
3823 
3824 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3825 
3826 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3827 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3828 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3829 		return (rval);
3830 	}
3831 
3832 	scsipkt->pkt_reason = CMD_CMPLT;
3833 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3834 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3835 	*scsipkt->pkt_scbp = STATUS_GOOD;
3836 
3837 	/*
3838 	 * when CONTROL field's NACA bit == 1
3839 	 * return ILLEGAL_REQUEST
3840 	 */
3841 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3842 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3843 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3844 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3845 	}
3846 
3847 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3848 	    &spx->txlt_sata_pkt->satapkt_device);
3849 	ASSERT(sdinfo != NULL);
3850 
3851 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3852 
3853 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3854 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3855 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3856 	if (sata_hba_start(spx, &rval) != 0) {
3857 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3858 		return (rval);
3859 	} else {
3860 		if (scmd->satacmd_error_reg != 0) {
3861 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3862 			return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3863 			    SD_SCSI_ASC_NO_ADD_SENSE));
3864 		}
3865 	}
3866 
3867 	switch (scmd->satacmd_sec_count_lsb) {
3868 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3869 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3870 			power_state = SATA_POWER_STOPPED;
3871 		else {
3872 			power_state = SATA_POWER_STANDBY;
3873 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3874 		}
3875 		break;
3876 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3877 		power_state = SATA_POWER_IDLE;
3878 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3879 		break;
3880 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3881 	default:		  /* 0x40, 0x41 active mode */
3882 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3883 			power_state = SATA_POWER_IDLE;
3884 		else {
3885 			power_state = SATA_POWER_ACTIVE;
3886 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3887 		}
3888 		break;
3889 	}
3890 
3891 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3892 
3893 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3894 		/*
3895 		 * Because it is fully emulated command storing data
3896 		 * programatically in the specified buffer, release
3897 		 * preallocated DMA resources before storing data in the buffer,
3898 		 * so no unwanted DMA sync would take place.
3899 		 */
3900 		int count = MIN(bp->b_bcount,
3901 		    sizeof (struct scsi_extended_sense));
3902 		sata_scsi_dmafree(NULL, scsipkt);
3903 		bzero(&sense, sizeof (struct scsi_extended_sense));
3904 		sense.es_valid = 0;	/* Valid LBA */
3905 		sense.es_class = 7;	/* Response code 0x70 - current err */
3906 		sense.es_key = KEY_NO_SENSE;
3907 		sense.es_add_len = 6;	/* Additional length */
3908 		/* Copy no more than requested */
3909 		bcopy(&sense, bp->b_un.b_addr, count);
3910 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3911 		scsipkt->pkt_resid = 0;
3912 		switch (power_state) {
3913 		case SATA_POWER_IDLE:
3914 		case SATA_POWER_STANDBY:
3915 			sense.es_add_code =
3916 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3917 			break;
3918 		case SATA_POWER_STOPPED:
3919 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3920 			break;
3921 		case SATA_POWER_ACTIVE:
3922 		default:
3923 			break;
3924 		}
3925 	}
3926 
3927 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3928 	    "Scsi_pkt completion reason %x\n",
3929 	    scsipkt->pkt_reason);
3930 
3931 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3932 	    scsipkt->pkt_comp != NULL) {
3933 		/* scsi callback required */
3934 		if (servicing_interrupt()) {
3935 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3936 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3937 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3938 				return (TRAN_BUSY);
3939 			}
3940 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3941 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3942 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3943 			/* Scheduling the callback failed */
3944 			return (TRAN_BUSY);
3945 		}
3946 	}
3947 	return (TRAN_ACCEPT);
3948 }
3949 
3950 /*
3951  * SATA translate command: Test Unit Ready
3952  * (ATA version for SATA hard disks).
3953  * It is translated into the Check Power Mode command.
3954  *
3955  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3956  */
3957 static int
3958 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3959 {
3960 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3961 	struct scsi_extended_sense *sense;
3962 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3963 	sata_drive_info_t *sdinfo;
3964 	int power_state;
3965 	int rval, reason;
3966 
3967 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3968 
3969 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3970 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3971 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3972 		return (rval);
3973 	}
3974 
3975 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3976 	    &spx->txlt_sata_pkt->satapkt_device);
3977 	ASSERT(sdinfo != NULL);
3978 
3979 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3980 
3981 	/* send CHECK POWER MODE command */
3982 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3983 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3984 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3985 	if (sata_hba_start(spx, &rval) != 0) {
3986 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3987 		return (rval);
3988 	} else {
3989 		if (scmd->satacmd_error_reg != 0) {
3990 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3991 			return (sata_txlt_check_condition(spx, KEY_NOT_READY,
3992 			    SD_SCSI_ASC_LU_NOT_RESPONSE));
3993 		}
3994 	}
3995 
3996 	power_state = scmd->satacmd_sec_count_lsb;
3997 
3998 	/*
3999 	 * return NOT READY when device in STOPPED mode
4000 	 */
4001 	if (power_state == SATA_PWRMODE_STANDBY &&
4002 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4003 		*scsipkt->pkt_scbp = STATUS_CHECK;
4004 		sense = sata_arq_sense(spx);
4005 		sense->es_key = KEY_NOT_READY;
4006 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4007 	} else {
4008 		/*
4009 		 * For other power mode, return GOOD status
4010 		 */
4011 		*scsipkt->pkt_scbp = STATUS_GOOD;
4012 	}
4013 
4014 	scsipkt->pkt_reason = CMD_CMPLT;
4015 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4016 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4017 
4018 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4019 
4020 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4021 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4022 
4023 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4024 	    scsipkt->pkt_comp != NULL) {
4025 		/* scsi callback required */
4026 		if (servicing_interrupt()) {
4027 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4028 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4029 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4030 				return (TRAN_BUSY);
4031 			}
4032 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4033 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4034 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4035 			/* Scheduling the callback failed */
4036 			return (TRAN_BUSY);
4037 		}
4038 	}
4039 
4040 	return (TRAN_ACCEPT);
4041 }
4042 
4043 /*
4044  * SATA translate command: Start Stop Unit
4045  * Translation depends on a command:
4046  *
4047  * Power condition bits will be supported
4048  * and the power level should be maintained by SATL,
4049  * When SATL received a command, it will check the
4050  * power level firstly, and return the status according
4051  * to SAT2 v2.6 and SAT-2 Standby Modifications
4052  *
4053  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4054  * -----------------------------------------------------------------------
4055  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4056  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4057  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4058  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4059  *
4060  *	Unload Media / NOT SUPPORTED YET
4061  *	Load Media / NOT SUPPROTED YET
4062  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4063  *
4064  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4065  * appropriate values in scsi_pkt fields.
4066  */
4067 static int
4068 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4069 {
4070 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4071 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4072 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4073 	int cport = SATA_TXLT_CPORT(spx);
4074 	int rval, reason;
4075 	sata_drive_info_t *sdinfo;
4076 	sata_id_t *sata_id;
4077 
4078 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4079 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4080 
4081 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
4082 
4083 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4084 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4085 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4086 		return (rval);
4087 	}
4088 
4089 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4090 		/* IMMED bit - not supported */
4091 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4092 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4093 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4094 	}
4095 
4096 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4097 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4098 
4099 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4100 	    &spx->txlt_sata_pkt->satapkt_device);
4101 	ASSERT(sdinfo != NULL);
4102 	sata_id = &sdinfo->satadrv_id;
4103 
4104 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4105 	case 0:
4106 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4107 			/* Load/Unload Media - invalid request */
4108 			goto err_out;
4109 		}
4110 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4111 			/* Start Unit */
4112 			sata_build_read_verify_cmd(scmd, 1, 5);
4113 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4114 			/* Transfer command to HBA */
4115 			if (sata_hba_start(spx, &rval) != 0) {
4116 				/* Pkt not accepted for execution */
4117 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4118 				return (rval);
4119 			} else {
4120 				if (scmd->satacmd_error_reg != 0) {
4121 					goto err_out;
4122 				}
4123 			}
4124 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4125 		} else {
4126 			/* Stop Unit */
4127 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4128 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4129 			if (sata_hba_start(spx, &rval) != 0) {
4130 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4131 				return (rval);
4132 			} else {
4133 				if (scmd->satacmd_error_reg != 0) {
4134 					goto err_out;
4135 				}
4136 			}
4137 			/* ata standby immediate command */
4138 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4139 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4140 			if (sata_hba_start(spx, &rval) != 0) {
4141 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4142 				return (rval);
4143 			} else {
4144 				if (scmd->satacmd_error_reg != 0) {
4145 					goto err_out;
4146 				}
4147 			}
4148 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4149 		}
4150 		break;
4151 	case 0x1:
4152 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4153 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4154 		if (sata_hba_start(spx, &rval) != 0) {
4155 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4156 			return (rval);
4157 		} else {
4158 			if (scmd->satacmd_error_reg != 0) {
4159 				goto err_out;
4160 			}
4161 		}
4162 		sata_build_read_verify_cmd(scmd, 1, 5);
4163 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4164 		/* Transfer command to HBA */
4165 		if (sata_hba_start(spx, &rval) != 0) {
4166 			/* Pkt not accepted for execution */
4167 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4168 			return (rval);
4169 		} else {
4170 			if (scmd->satacmd_error_reg != 0) {
4171 				goto err_out;
4172 			}
4173 		}
4174 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4175 		break;
4176 	case 0x2:
4177 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4178 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4179 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4180 			if (sata_hba_start(spx, &rval) != 0) {
4181 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4182 				return (rval);
4183 			} else {
4184 				if (scmd->satacmd_error_reg != 0) {
4185 					goto err_out;
4186 				}
4187 			}
4188 		}
4189 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4190 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4191 		if (sata_hba_start(spx, &rval) != 0) {
4192 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4193 			return (rval);
4194 		} else {
4195 			if (scmd->satacmd_error_reg != 0) {
4196 				goto err_out;
4197 			}
4198 		}
4199 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4200 			/*
4201 			 *  POWER CONDITION MODIFIER bit set
4202 			 *  to 0x1 or larger it will be handled
4203 			 *  on the same way as bit = 0x1
4204 			 */
4205 			if (!(sata_id->ai_cmdset84 &
4206 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4207 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4208 				break;
4209 			}
4210 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4211 			scmd->satacmd_features_reg = 0x44;
4212 			scmd->satacmd_lba_low_lsb = 0x4c;
4213 			scmd->satacmd_lba_mid_lsb = 0x4e;
4214 			scmd->satacmd_lba_high_lsb = 0x55;
4215 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4216 			if (sata_hba_start(spx, &rval) != 0) {
4217 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4218 				return (rval);
4219 			} else {
4220 				if (scmd->satacmd_error_reg != 0) {
4221 					goto err_out;
4222 				}
4223 			}
4224 		}
4225 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4226 		break;
4227 	case 0x3:
4228 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4229 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4230 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4231 			if (sata_hba_start(spx, &rval) != 0) {
4232 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4233 				return (rval);
4234 			} else {
4235 				if (scmd->satacmd_error_reg != 0) {
4236 					goto err_out;
4237 				}
4238 			}
4239 		}
4240 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4241 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4242 		if (sata_hba_start(spx, &rval) != 0) {
4243 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4244 			return (rval);
4245 		} else {
4246 			if (scmd->satacmd_error_reg != 0) {
4247 				goto err_out;
4248 			}
4249 		}
4250 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4251 		break;
4252 	case 0x7:
4253 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4254 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4255 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4256 		if (sata_hba_start(spx, &rval) != 0) {
4257 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4258 			return (rval);
4259 		} else {
4260 			if (scmd->satacmd_error_reg != 0) {
4261 				goto err_out;
4262 			}
4263 		}
4264 		switch (scmd->satacmd_sec_count_lsb) {
4265 		case SATA_PWRMODE_STANDBY:
4266 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4267 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4268 			    sdinfo->satadrv_standby_timer);
4269 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4270 			if (sata_hba_start(spx, &rval) != 0) {
4271 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4272 				return (rval);
4273 			} else {
4274 				if (scmd->satacmd_error_reg != 0) {
4275 					goto err_out;
4276 				}
4277 			}
4278 			break;
4279 		case SATA_PWRMODE_IDLE:
4280 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4281 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4282 			    sdinfo->satadrv_standby_timer);
4283 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4284 			if (sata_hba_start(spx, &rval) != 0) {
4285 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4286 				return (rval);
4287 			} else {
4288 				if (scmd->satacmd_error_reg != 0) {
4289 					goto err_out;
4290 				}
4291 			}
4292 			break;
4293 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4294 		case SATA_PWRMODE_ACTIVE_SPINUP:
4295 		case SATA_PWRMODE_ACTIVE:
4296 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4297 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4298 			    sdinfo->satadrv_standby_timer);
4299 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4300 			if (sata_hba_start(spx, &rval) != 0) {
4301 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4302 				return (rval);
4303 			} else {
4304 				if (scmd->satacmd_error_reg != 0) {
4305 					goto err_out;
4306 				}
4307 			}
4308 			sata_build_read_verify_cmd(scmd, 1, 5);
4309 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4310 			if (sata_hba_start(spx, &rval) != 0) {
4311 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4312 				return (rval);
4313 			} else {
4314 				if (scmd->satacmd_error_reg != 0) {
4315 					goto err_out;
4316 				}
4317 			}
4318 			break;
4319 		default:
4320 			goto err_out;
4321 		}
4322 		break;
4323 	case 0xb:
4324 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4325 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4326 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4327 			return (sata_txlt_check_condition(spx,
4328 			    KEY_ILLEGAL_REQUEST,
4329 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4330 		}
4331 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4332 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4333 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4334 			if (sata_hba_start(spx, &rval) != 0) {
4335 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4336 				return (rval);
4337 			} else {
4338 				if (scmd->satacmd_error_reg != 0) {
4339 					goto err_out;
4340 				}
4341 			}
4342 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4343 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 			if (sata_hba_start(spx, &rval) != 0) {
4345 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4346 				return (rval);
4347 			} else {
4348 				if (scmd->satacmd_error_reg != 0) {
4349 					goto err_out;
4350 				}
4351 			}
4352 		}
4353 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4354 		break;
4355 	default:
4356 err_out:
4357 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4358 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4359 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4360 	}
4361 
4362 	/*
4363 	 * Since it was a synchronous command,
4364 	 * a callback function will be called directly.
4365 	 */
4366 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4367 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4368 	    "synchronous execution status %x\n",
4369 	    spx->txlt_sata_pkt->satapkt_reason);
4370 
4371 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4372 	    scsipkt->pkt_comp != NULL) {
4373 		sata_set_arq_data(spx->txlt_sata_pkt);
4374 		if (servicing_interrupt()) {
4375 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4376 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4377 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4378 				return (TRAN_BUSY);
4379 			}
4380 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4381 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4382 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4383 			/* Scheduling the callback failed */
4384 			return (TRAN_BUSY);
4385 		}
4386 	}
4387 	else
4388 
4389 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4390 
4391 	return (TRAN_ACCEPT);
4392 
4393 }
4394 
4395 /*
4396  * SATA translate command:  Read Capacity.
4397  * Emulated command for SATA disks.
4398  * Capacity is retrieved from cached Idenifty Device data.
4399  * Identify Device data shows effective disk capacity, not the native
4400  * capacity, which may be limitted by Set Max Address command.
4401  * This is ATA version for SATA hard disks.
4402  *
4403  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4404  */
4405 static int
4406 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4407 {
4408 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4409 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4410 	sata_drive_info_t *sdinfo;
4411 	uint64_t val;
4412 	uchar_t *rbuf;
4413 	int rval, reason;
4414 
4415 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4416 	    "sata_txlt_read_capacity: ", NULL);
4417 
4418 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4419 
4420 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4421 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4422 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4423 		return (rval);
4424 	}
4425 
4426 	scsipkt->pkt_reason = CMD_CMPLT;
4427 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4428 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4429 	*scsipkt->pkt_scbp = STATUS_GOOD;
4430 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4431 		/*
4432 		 * Because it is fully emulated command storing data
4433 		 * programatically in the specified buffer, release
4434 		 * preallocated DMA resources before storing data in the buffer,
4435 		 * so no unwanted DMA sync would take place.
4436 		 */
4437 		sata_scsi_dmafree(NULL, scsipkt);
4438 
4439 		sdinfo = sata_get_device_info(
4440 		    spx->txlt_sata_hba_inst,
4441 		    &spx->txlt_sata_pkt->satapkt_device);
4442 
4443 		/*
4444 		 * As per SBC-3, the "returned LBA" is either the highest
4445 		 * addressable LBA or 0xffffffff, whichever is smaller.
4446 		 */
4447 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4448 
4449 		rbuf = (uchar_t *)bp->b_un.b_addr;
4450 		/* Need to swap endians to match scsi format */
4451 		rbuf[0] = (val >> 24) & 0xff;
4452 		rbuf[1] = (val >> 16) & 0xff;
4453 		rbuf[2] = (val >> 8) & 0xff;
4454 		rbuf[3] = val & 0xff;
4455 		/* block size - always 512 bytes, for now */
4456 		rbuf[4] = 0;
4457 		rbuf[5] = 0;
4458 		rbuf[6] = 0x02;
4459 		rbuf[7] = 0;
4460 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4461 		scsipkt->pkt_resid = 0;
4462 
4463 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4464 		    sdinfo->satadrv_capacity -1);
4465 	}
4466 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4467 	/*
4468 	 * If a callback was requested, do it now.
4469 	 */
4470 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4471 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4472 
4473 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4474 	    scsipkt->pkt_comp != NULL) {
4475 		/* scsi callback required */
4476 		if (servicing_interrupt()) {
4477 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4478 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4479 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4480 				return (TRAN_BUSY);
4481 			}
4482 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4483 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4484 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4485 			/* Scheduling the callback failed */
4486 			return (TRAN_BUSY);
4487 		}
4488 	}
4489 
4490 	return (TRAN_ACCEPT);
4491 }
4492 
4493 /*
4494  * SATA translate command:  Read Capacity (16).
4495  * Emulated command for SATA disks.
4496  * Info is retrieved from cached Identify Device data.
4497  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4498  *
4499  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4500  */
4501 static int
4502 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4503 {
4504 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4505 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4506 	sata_drive_info_t *sdinfo;
4507 	uint64_t val;
4508 	uint16_t l2p_exp;
4509 	uchar_t *rbuf;
4510 	int rval, reason;
4511 #define	TPE	0x80
4512 #define	TPRZ	0x40
4513 
4514 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4515 	    "sata_txlt_read_capacity: ", NULL);
4516 
4517 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4518 
4519 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4520 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4521 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4522 		return (rval);
4523 	}
4524 
4525 	scsipkt->pkt_reason = CMD_CMPLT;
4526 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4527 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4528 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4529 		/*
4530 		 * Because it is fully emulated command storing data
4531 		 * programatically in the specified buffer, release
4532 		 * preallocated DMA resources before storing data in the buffer,
4533 		 * so no unwanted DMA sync would take place.
4534 		 */
4535 		sata_scsi_dmafree(NULL, scsipkt);
4536 
4537 		/* Check SERVICE ACTION field */
4538 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4539 		    SSVC_ACTION_READ_CAPACITY_G4) {
4540 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4541 			return (sata_txlt_check_condition(spx,
4542 			    KEY_ILLEGAL_REQUEST,
4543 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4544 		}
4545 
4546 		/* Check LBA field */
4547 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4548 		    (scsipkt->pkt_cdbp[3] != 0) ||
4549 		    (scsipkt->pkt_cdbp[4] != 0) ||
4550 		    (scsipkt->pkt_cdbp[5] != 0) ||
4551 		    (scsipkt->pkt_cdbp[6] != 0) ||
4552 		    (scsipkt->pkt_cdbp[7] != 0) ||
4553 		    (scsipkt->pkt_cdbp[8] != 0) ||
4554 		    (scsipkt->pkt_cdbp[9] != 0)) {
4555 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4556 			return (sata_txlt_check_condition(spx,
4557 			    KEY_ILLEGAL_REQUEST,
4558 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4559 		}
4560 
4561 		/* Check PMI bit */
4562 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4563 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4564 			return (sata_txlt_check_condition(spx,
4565 			    KEY_ILLEGAL_REQUEST,
4566 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4567 		}
4568 
4569 		*scsipkt->pkt_scbp = STATUS_GOOD;
4570 
4571 		sdinfo = sata_get_device_info(
4572 		    spx->txlt_sata_hba_inst,
4573 		    &spx->txlt_sata_pkt->satapkt_device);
4574 
4575 		/* last logical block address */
4576 		val = MIN(sdinfo->satadrv_capacity - 1,
4577 		    SCSI_READ_CAPACITY16_MAX_LBA);
4578 
4579 		/* logical to physical block size exponent */
4580 		l2p_exp = 0;
4581 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4582 			/* physical/logical sector size word is valid */
4583 
4584 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4585 			    SATA_L2PS_HAS_MULT) {
4586 				/* multiple logical sectors per phys sectors */
4587 				l2p_exp =
4588 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4589 				    SATA_L2PS_EXP_MASK;
4590 			}
4591 		}
4592 
4593 		rbuf = (uchar_t *)bp->b_un.b_addr;
4594 		bzero(rbuf, bp->b_bcount);
4595 
4596 		/* returned logical block address */
4597 		rbuf[0] = (val >> 56) & 0xff;
4598 		rbuf[1] = (val >> 48) & 0xff;
4599 		rbuf[2] = (val >> 40) & 0xff;
4600 		rbuf[3] = (val >> 32) & 0xff;
4601 		rbuf[4] = (val >> 24) & 0xff;
4602 		rbuf[5] = (val >> 16) & 0xff;
4603 		rbuf[6] = (val >> 8) & 0xff;
4604 		rbuf[7] = val & 0xff;
4605 
4606 		/* logical block length in bytes = 512 (for now) */
4607 		/* rbuf[8] = 0; */
4608 		/* rbuf[9] = 0; */
4609 		rbuf[10] = 0x02;
4610 		/* rbuf[11] = 0; */
4611 
4612 		/* p_type, prot_en, unspecified by SAT-2 */
4613 		/* rbuf[12] = 0; */
4614 
4615 		/* p_i_exponent, undefined by SAT-2 */
4616 		/* logical blocks per physical block exponent */
4617 		rbuf[13] = l2p_exp;
4618 
4619 		/* lowest aligned logical block address = 0 (for now) */
4620 		/* tpe and tprz as defined in T10/10-079 r0 */
4621 		if (sdinfo->satadrv_id.ai_addsupported &
4622 		    SATA_DETERMINISTIC_READ) {
4623 			if (sdinfo->satadrv_id.ai_addsupported &
4624 			    SATA_READ_ZERO) {
4625 				rbuf[14] |= TPRZ;
4626 			} else {
4627 				rbuf[14] |= TPE;
4628 			}
4629 		}
4630 		/* rbuf[15] = 0; */
4631 
4632 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4633 		scsipkt->pkt_resid = 0;
4634 
4635 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4636 		    sdinfo->satadrv_capacity -1);
4637 	}
4638 
4639 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4640 
4641 	/*
4642 	 * If a callback was requested, do it now.
4643 	 */
4644 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4645 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4646 
4647 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4648 	    scsipkt->pkt_comp != NULL) {
4649 		/* scsi callback required */
4650 		if (servicing_interrupt()) {
4651 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4652 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4653 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4654 				return (TRAN_BUSY);
4655 			}
4656 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4657 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4658 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4659 			/* Scheduling the callback failed */
4660 			return (TRAN_BUSY);
4661 		}
4662 	}
4663 
4664 	return (TRAN_ACCEPT);
4665 }
4666 
4667 /*
4668  * Translate command: UNMAP
4669  *
4670  * The function cannot be called in interrupt context since it may sleep.
4671  */
4672 static int
4673 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4674 {
4675 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4676 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4677 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4678 	uint16_t count = 0;
4679 	int synch;
4680 	int rval, reason;
4681 	int i, x;
4682 	int bdlen = 0;
4683 	int ranges = 0;
4684 	int paramlen = 8;
4685 	uint8_t *data, *tmpbd;
4686 	sata_drive_info_t *sdinfo;
4687 #define	TRIM	0x1
4688 
4689 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4690 	    "sata_txlt_unmap: ", NULL);
4691 
4692 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4693 
4694 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4695 	    &spx->txlt_sata_pkt->satapkt_device);
4696 	if (sdinfo != NULL) {
4697 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4698 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4699 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4700 		    sdinfo->satadrv_id.ai_maxcount);
4701 	}
4702 
4703 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4704 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4705 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4706 		return (rval);
4707 	}
4708 
4709 	/*
4710 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4711 	 * Start by getting the block descriptor data length by subtracting
4712 	 * the 8 byte parameter list header from the parameter list length.
4713 	 * The block descriptor size has to be a multiple of 16 bytes.
4714 	 */
4715 	bdlen = scsipkt->pkt_cdbp[7];
4716 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4717 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4718 	    (bdlen > (bp->b_bcount - paramlen))) {
4719 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4720 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4721 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4722 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4723 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4724 	}
4725 	/*
4726 	 * If there are no parameter data or block descriptors, it is not
4727 	 * considered an error so just complete the command without sending
4728 	 * TRIM.
4729 	 */
4730 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4731 	    (bp->b_bcount == 0)) {
4732 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4733 		    "sata_txlt_unmap: no parameter data or block descriptors",
4734 		    NULL);
4735 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4736 		return (sata_txlt_unmap_nodata_cmd(spx));
4737 	}
4738 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4739 	data = kmem_zalloc(bdlen, KM_SLEEP);
4740 
4741 	/*
4742 	 * Loop through all the UNMAP block descriptors and convert the data
4743 	 * into TRIM format.
4744 	 */
4745 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4746 		/* get range length */
4747 		data[x] = tmpbd[i+7];
4748 		data[x+1] = tmpbd[i+6];
4749 		/* get LBA */
4750 		data[x+2] = tmpbd[i+5];
4751 		data[x+3] = tmpbd[i+4];
4752 		data[x+4] = tmpbd[i+3];
4753 		data[x+5] = tmpbd[i+2];
4754 		data[x+6] = tmpbd[i+11];
4755 		data[x+7] = tmpbd[i+10];
4756 
4757 		ranges++;
4758 	}
4759 
4760 	/*
4761 	 * The TRIM command expects the data buffer to be a multiple of
4762 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4763 	 * may be too small.  Free the original DMA resources and create a
4764 	 * local buffer.
4765 	 */
4766 	sata_common_free_dma_rsrcs(spx);
4767 
4768 	/*
4769 	 * Get count of 512-byte blocks of range entries.  The length
4770 	 * of a range entry is 8 bytes which means one count has 64 range
4771 	 * entries.
4772 	 */
4773 	count = (ranges + 63)/64;
4774 
4775 	/* Allocate a buffer that is a multiple of 512 bytes. */
4776 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4777 	bp = sata_alloc_local_buffer(spx, count * 512);
4778 	if (bp == NULL) {
4779 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4780 		    "sata_txlt_unmap: "
4781 		    "cannot allocate buffer for TRIM command", NULL);
4782 		kmem_free(data, bdlen);
4783 		return (TRAN_BUSY);
4784 	}
4785 	bp_mapin(bp); /* make data buffer accessible */
4786 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4787 
4788 	bzero(bp->b_un.b_addr, bp->b_bcount);
4789 	bcopy(data, bp->b_un.b_addr, x);
4790 	kmem_free(data, bdlen);
4791 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4792 	    DDI_DMA_SYNC_FORDEV);
4793 	ASSERT(rval == DDI_SUCCESS);
4794 
4795 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4796 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4797 	scmd->satacmd_cmd_reg = SATAC_DSM;
4798 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4799 	scmd->satacmd_sec_count_lsb = count & 0xff;
4800 	scmd->satacmd_features_reg = TRIM;
4801 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4802 	scmd->satacmd_status_reg = 0;
4803 	scmd->satacmd_error_reg = 0;
4804 
4805 	/* Start processing command */
4806 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4807 		spx->txlt_sata_pkt->satapkt_comp =
4808 		    sata_txlt_unmap_completion;
4809 		synch = FALSE;
4810 	} else {
4811 		synch = TRUE;
4812 	}
4813 
4814 	if (sata_hba_start(spx, &rval) != 0) {
4815 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4816 		return (rval);
4817 	}
4818 
4819 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4820 
4821 	if (synch) {
4822 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4823 	}
4824 
4825 	return (TRAN_ACCEPT);
4826 }
4827 
4828 /*
4829  * SATA translate command: Mode Sense.
4830  * Translated into appropriate SATA command or emulated.
4831  * Saved Values Page Control (03) are not supported.
4832  *
4833  * NOTE: only caching mode sense page is currently implemented.
4834  *
4835  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4836  */
4837 
4838 #define	LLBAA	0x10	/* Long LBA Accepted */
4839 
4840 static int
4841 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4842 {
4843 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4844 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4845 	sata_drive_info_t *sdinfo;
4846 	sata_id_t *sata_id;
4847 	struct scsi_extended_sense *sense;
4848 	int 		len, bdlen, count, alc_len;
4849 	int		pc;	/* Page Control code */
4850 	uint8_t		*buf;	/* mode sense buffer */
4851 	int		rval, reason;
4852 
4853 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4854 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4855 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4856 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4857 
4858 	if (servicing_interrupt()) {
4859 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4860 		if (buf == NULL) {
4861 			return (TRAN_BUSY);
4862 		}
4863 	} else {
4864 		buf = kmem_zalloc(1024, KM_SLEEP);
4865 	}
4866 
4867 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4868 
4869 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4870 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4871 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4872 		kmem_free(buf, 1024);
4873 		return (rval);
4874 	}
4875 
4876 	scsipkt->pkt_reason = CMD_CMPLT;
4877 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4878 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4879 
4880 	pc = scsipkt->pkt_cdbp[2] >> 6;
4881 
4882 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4883 		/*
4884 		 * Because it is fully emulated command storing data
4885 		 * programatically in the specified buffer, release
4886 		 * preallocated DMA resources before storing data in the buffer,
4887 		 * so no unwanted DMA sync would take place.
4888 		 */
4889 		sata_scsi_dmafree(NULL, scsipkt);
4890 
4891 		len = 0;
4892 		bdlen = 0;
4893 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4894 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4895 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4896 				bdlen = 16;
4897 			else
4898 				bdlen = 8;
4899 		}
4900 		/* Build mode parameter header */
4901 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4902 			/* 4-byte mode parameter header */
4903 			buf[len++] = 0;		/* mode data length */
4904 			buf[len++] = 0;		/* medium type */
4905 			buf[len++] = 0;		/* dev-specific param */
4906 			buf[len++] = bdlen;	/* Block Descriptor length */
4907 		} else {
4908 			/* 8-byte mode parameter header */
4909 			buf[len++] = 0;		/* mode data length */
4910 			buf[len++] = 0;
4911 			buf[len++] = 0;		/* medium type */
4912 			buf[len++] = 0;		/* dev-specific param */
4913 			if (bdlen == 16)
4914 				buf[len++] = 1;	/* long lba descriptor */
4915 			else
4916 				buf[len++] = 0;
4917 			buf[len++] = 0;
4918 			buf[len++] = 0;		/* Block Descriptor length */
4919 			buf[len++] = bdlen;
4920 		}
4921 
4922 		sdinfo = sata_get_device_info(
4923 		    spx->txlt_sata_hba_inst,
4924 		    &spx->txlt_sata_pkt->satapkt_device);
4925 
4926 		/* Build block descriptor only if not disabled (DBD) */
4927 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4928 			/* Block descriptor - direct-access device format */
4929 			if (bdlen == 8) {
4930 				/* build regular block descriptor */
4931 				buf[len++] =
4932 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4933 				buf[len++] =
4934 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4935 				buf[len++] =
4936 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4937 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4938 				buf[len++] = 0; /* density code */
4939 				buf[len++] = 0;
4940 				if (sdinfo->satadrv_type ==
4941 				    SATA_DTYPE_ATADISK)
4942 					buf[len++] = 2;
4943 				else
4944 					/* ATAPI */
4945 					buf[len++] = 8;
4946 				buf[len++] = 0;
4947 			} else if (bdlen == 16) {
4948 				/* Long LBA Accepted */
4949 				/* build long lba block descriptor */
4950 #ifndef __lock_lint
4951 				buf[len++] =
4952 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4953 				buf[len++] =
4954 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4955 				buf[len++] =
4956 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4957 				buf[len++] =
4958 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4959 #endif
4960 				buf[len++] =
4961 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4962 				buf[len++] =
4963 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4964 				buf[len++] =
4965 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4966 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4967 				buf[len++] = 0;
4968 				buf[len++] = 0; /* density code */
4969 				buf[len++] = 0;
4970 				buf[len++] = 0;
4971 				if (sdinfo->satadrv_type ==
4972 				    SATA_DTYPE_ATADISK)
4973 					buf[len++] = 2;
4974 				else
4975 					/* ATAPI */
4976 					buf[len++] = 8;
4977 				buf[len++] = 0;
4978 			}
4979 		}
4980 
4981 		sata_id = &sdinfo->satadrv_id;
4982 
4983 		/*
4984 		 * Add requested pages.
4985 		 * Page 3 and 4 are obsolete and we are not supporting them.
4986 		 * We deal now with:
4987 		 * caching (read/write cache control).
4988 		 * We should eventually deal with following mode pages:
4989 		 * error recovery  (0x01),
4990 		 * power condition (0x1a),
4991 		 * exception control page (enables SMART) (0x1c),
4992 		 * enclosure management (ses),
4993 		 * protocol-specific port mode (port control).
4994 		 */
4995 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4996 		case MODEPAGE_RW_ERRRECOV:
4997 			/* DAD_MODE_ERR_RECOV */
4998 			/* R/W recovery */
4999 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5000 			break;
5001 		case MODEPAGE_CACHING:
5002 			/* DAD_MODE_CACHE */
5003 			/* Reject not supported request for saved parameters */
5004 			if (pc == 3) {
5005 				*scsipkt->pkt_scbp = STATUS_CHECK;
5006 				sense = sata_arq_sense(spx);
5007 				sense->es_key = KEY_ILLEGAL_REQUEST;
5008 				sense->es_add_code =
5009 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5010 				goto done;
5011 			}
5012 
5013 			/* caching */
5014 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5015 			break;
5016 		case MODEPAGE_INFO_EXCPT:
5017 			/* exception cntrl */
5018 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5019 				len += sata_build_msense_page_1c(sdinfo, pc,
5020 				    buf+len);
5021 			}
5022 			else
5023 				goto err;
5024 			break;
5025 		case MODEPAGE_POWER_COND:
5026 			/* DAD_MODE_POWER_COND */
5027 			/* power condition */
5028 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5029 			break;
5030 
5031 		case MODEPAGE_ACOUSTIC_MANAG:
5032 			/* acoustic management */
5033 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5034 			break;
5035 		case MODEPAGE_ALLPAGES:
5036 			/* all pages */
5037 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5038 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5039 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5040 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5041 				len += sata_build_msense_page_1c(sdinfo, pc,
5042 				    buf+len);
5043 			}
5044 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5045 			break;
5046 		default:
5047 		err:
5048 			/* Invalid request */
5049 			*scsipkt->pkt_scbp = STATUS_CHECK;
5050 			sense = sata_arq_sense(spx);
5051 			sense->es_key = KEY_ILLEGAL_REQUEST;
5052 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5053 			goto done;
5054 		}
5055 
5056 		/* fix total mode data length */
5057 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5058 			/* 4-byte mode parameter header */
5059 			buf[0] = len - 1;	/* mode data length */
5060 		} else {
5061 			buf[0] = (len -2) >> 8;
5062 			buf[1] = (len -2) & 0xff;
5063 		}
5064 
5065 
5066 		/* Check allocation length */
5067 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5068 			alc_len = scsipkt->pkt_cdbp[4];
5069 		} else {
5070 			alc_len = scsipkt->pkt_cdbp[7];
5071 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5072 		}
5073 		/*
5074 		 * We do not check for possible parameters truncation
5075 		 * (alc_len < len) assuming that the target driver works
5076 		 * correctly. Just avoiding overrun.
5077 		 * Copy no more than requested and possible, buffer-wise.
5078 		 */
5079 		count = MIN(alc_len, len);
5080 		count = MIN(bp->b_bcount, count);
5081 		bcopy(buf, bp->b_un.b_addr, count);
5082 
5083 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5084 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5085 	}
5086 	*scsipkt->pkt_scbp = STATUS_GOOD;
5087 done:
5088 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5089 	(void) kmem_free(buf, 1024);
5090 
5091 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5092 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5093 
5094 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5095 	    scsipkt->pkt_comp != NULL) {
5096 		/* scsi callback required */
5097 		if (servicing_interrupt()) {
5098 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5099 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5100 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5101 				return (TRAN_BUSY);
5102 			}
5103 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5104 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5105 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5106 			/* Scheduling the callback failed */
5107 			return (TRAN_BUSY);
5108 		}
5109 	}
5110 
5111 	return (TRAN_ACCEPT);
5112 }
5113 
5114 
5115 /*
5116  * SATA translate command: Mode Select.
5117  * Translated into appropriate SATA command or emulated.
5118  * Saving parameters is not supported.
5119  * Changing device capacity is not supported (although theoretically
5120  * possible by executing SET FEATURES/SET MAX ADDRESS)
5121  *
5122  * Assumption is that the target driver is working correctly.
5123  *
5124  * More than one SATA command may be executed to perform operations specified
5125  * by mode select pages. The first error terminates further execution.
5126  * Operations performed successully are not backed-up in such case.
5127  *
5128  * NOTE: Implemented pages:
5129  * - caching page
5130  * - informational exception page
5131  * - acoustic management page
5132  * - power condition page
5133  * Caching setup is remembered so it could be re-stored in case of
5134  * an unexpected device reset.
5135  *
5136  * Returns TRAN_XXXX.
5137  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5138  */
5139 
5140 static int
5141 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5142 {
5143 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5144 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5145 	struct scsi_extended_sense *sense;
5146 	int len, pagelen, count, pllen;
5147 	uint8_t *buf;	/* mode select buffer */
5148 	int rval, stat, reason;
5149 	uint_t nointr_flag;
5150 	int dmod = 0;
5151 
5152 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5153 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5154 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5155 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5156 
5157 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5158 
5159 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5160 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5161 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5162 		return (rval);
5163 	}
5164 
5165 	rval = TRAN_ACCEPT;
5166 
5167 	scsipkt->pkt_reason = CMD_CMPLT;
5168 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5169 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5170 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5171 
5172 	/* Reject not supported request */
5173 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5174 		*scsipkt->pkt_scbp = STATUS_CHECK;
5175 		sense = sata_arq_sense(spx);
5176 		sense->es_key = KEY_ILLEGAL_REQUEST;
5177 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5178 		goto done;
5179 	}
5180 
5181 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5182 		pllen = scsipkt->pkt_cdbp[4];
5183 	} else {
5184 		pllen = scsipkt->pkt_cdbp[7];
5185 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5186 	}
5187 
5188 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5189 
5190 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5191 		buf = (uint8_t *)bp->b_un.b_addr;
5192 		count = MIN(bp->b_bcount, pllen);
5193 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5194 		scsipkt->pkt_resid = 0;
5195 		pllen = count;
5196 
5197 		/*
5198 		 * Check the header to skip the block descriptor(s) - we
5199 		 * do not support setting device capacity.
5200 		 * Existing macros do not recognize long LBA dscriptor,
5201 		 * hence manual calculation.
5202 		 */
5203 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5204 			/* 6-bytes CMD, 4 bytes header */
5205 			if (count <= 4)
5206 				goto done;		/* header only */
5207 			len = buf[3] + 4;
5208 		} else {
5209 			/* 10-bytes CMD, 8 bytes header */
5210 			if (count <= 8)
5211 				goto done;		/* header only */
5212 			len = buf[6];
5213 			len = (len << 8) + buf[7] + 8;
5214 		}
5215 		if (len >= count)
5216 			goto done;	/* header + descriptor(s) only */
5217 
5218 		pllen -= len;		/* remaining data length */
5219 
5220 		/*
5221 		 * We may be executing SATA command and want to execute it
5222 		 * in SYNCH mode, regardless of scsi_pkt setting.
5223 		 * Save scsi_pkt setting and indicate SYNCH mode
5224 		 */
5225 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5226 		    scsipkt->pkt_comp != NULL) {
5227 			scsipkt->pkt_flags |= FLAG_NOINTR;
5228 		}
5229 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5230 
5231 		/*
5232 		 * len is now the offset to a first mode select page
5233 		 * Process all pages
5234 		 */
5235 		while (pllen > 0) {
5236 			switch ((int)buf[len]) {
5237 			case MODEPAGE_CACHING:
5238 				/* No support for SP (saving) */
5239 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5240 					*scsipkt->pkt_scbp = STATUS_CHECK;
5241 					sense = sata_arq_sense(spx);
5242 					sense->es_key = KEY_ILLEGAL_REQUEST;
5243 					sense->es_add_code =
5244 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5245 					goto done;
5246 				}
5247 				stat = sata_mode_select_page_8(spx,
5248 				    (struct mode_cache_scsi3 *)&buf[len],
5249 				    pllen, &pagelen, &rval, &dmod);
5250 				/*
5251 				 * The pagelen value indicates the number of
5252 				 * parameter bytes already processed.
5253 				 * The rval is the return value from
5254 				 * sata_tran_start().
5255 				 * The stat indicates the overall status of
5256 				 * the operation(s).
5257 				 */
5258 				if (stat != SATA_SUCCESS)
5259 					/*
5260 					 * Page processing did not succeed -
5261 					 * all error info is already set-up,
5262 					 * just return
5263 					 */
5264 					pllen = 0; /* this breaks the loop */
5265 				else {
5266 					len += pagelen;
5267 					pllen -= pagelen;
5268 				}
5269 				break;
5270 
5271 			case MODEPAGE_INFO_EXCPT:
5272 				stat = sata_mode_select_page_1c(spx,
5273 				    (struct mode_info_excpt_page *)&buf[len],
5274 				    pllen, &pagelen, &rval, &dmod);
5275 				/*
5276 				 * The pagelen value indicates the number of
5277 				 * parameter bytes already processed.
5278 				 * The rval is the return value from
5279 				 * sata_tran_start().
5280 				 * The stat indicates the overall status of
5281 				 * the operation(s).
5282 				 */
5283 				if (stat != SATA_SUCCESS)
5284 					/*
5285 					 * Page processing did not succeed -
5286 					 * all error info is already set-up,
5287 					 * just return
5288 					 */
5289 					pllen = 0; /* this breaks the loop */
5290 				else {
5291 					len += pagelen;
5292 					pllen -= pagelen;
5293 				}
5294 				break;
5295 
5296 			case MODEPAGE_ACOUSTIC_MANAG:
5297 				stat = sata_mode_select_page_30(spx,
5298 				    (struct mode_acoustic_management *)
5299 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5300 				/*
5301 				 * The pagelen value indicates the number of
5302 				 * parameter bytes already processed.
5303 				 * The rval is the return value from
5304 				 * sata_tran_start().
5305 				 * The stat indicates the overall status of
5306 				 * the operation(s).
5307 				 */
5308 				if (stat != SATA_SUCCESS)
5309 					/*
5310 					 * Page processing did not succeed -
5311 					 * all error info is already set-up,
5312 					 * just return
5313 					 */
5314 					pllen = 0; /* this breaks the loop */
5315 				else {
5316 					len += pagelen;
5317 					pllen -= pagelen;
5318 				}
5319 
5320 				break;
5321 			case MODEPAGE_POWER_COND:
5322 				stat = sata_mode_select_page_1a(spx,
5323 				    (struct mode_info_power_cond *)&buf[len],
5324 				    pllen, &pagelen, &rval, &dmod);
5325 				/*
5326 				 * The pagelen value indicates the number of
5327 				 * parameter bytes already processed.
5328 				 * The rval is the return value from
5329 				 * sata_tran_start().
5330 				 * The stat indicates the overall status of
5331 				 * the operation(s).
5332 				 */
5333 				if (stat != SATA_SUCCESS)
5334 					/*
5335 					 * Page processing did not succeed -
5336 					 * all error info is already set-up,
5337 					 * just return
5338 					 */
5339 					pllen = 0; /* this breaks the loop */
5340 				else {
5341 					len += pagelen;
5342 					pllen -= pagelen;
5343 				}
5344 				break;
5345 			default:
5346 				*scsipkt->pkt_scbp = STATUS_CHECK;
5347 				sense = sata_arq_sense(spx);
5348 				sense->es_key = KEY_ILLEGAL_REQUEST;
5349 				sense->es_add_code =
5350 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5351 				goto done;
5352 			}
5353 		}
5354 	}
5355 done:
5356 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5357 	/*
5358 	 * If device parameters were modified, fetch and store the new
5359 	 * Identify Device data. Since port mutex could have been released
5360 	 * for accessing HBA driver, we need to re-check device existence.
5361 	 */
5362 	if (dmod != 0) {
5363 		sata_drive_info_t new_sdinfo, *sdinfo;
5364 		int rv = 0;
5365 
5366 		/*
5367 		 * Following statement has to be changed if this function is
5368 		 * used for devices other than SATA hard disks.
5369 		 */
5370 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5371 
5372 		new_sdinfo.satadrv_addr =
5373 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5374 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5375 		    &new_sdinfo);
5376 
5377 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5378 		/*
5379 		 * Since port mutex could have been released when
5380 		 * accessing HBA driver, we need to re-check that the
5381 		 * framework still holds the device info structure.
5382 		 */
5383 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5384 		    &spx->txlt_sata_pkt->satapkt_device);
5385 		if (sdinfo != NULL) {
5386 			/*
5387 			 * Device still has info structure in the
5388 			 * sata framework. Copy newly fetched info
5389 			 */
5390 			if (rv == 0) {
5391 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5392 				sata_save_drive_settings(sdinfo);
5393 			} else {
5394 				/*
5395 				 * Could not fetch new data - invalidate
5396 				 * sata_drive_info. That makes device
5397 				 * unusable.
5398 				 */
5399 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5400 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5401 			}
5402 		}
5403 		if (rv != 0 || sdinfo == NULL) {
5404 			/*
5405 			 * This changes the overall mode select completion
5406 			 * reason to a failed one !!!!!
5407 			 */
5408 			*scsipkt->pkt_scbp = STATUS_CHECK;
5409 			sense = sata_arq_sense(spx);
5410 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5411 			rval = TRAN_ACCEPT;
5412 		}
5413 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5414 	}
5415 	/* Restore the scsi pkt flags */
5416 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5417 	scsipkt->pkt_flags |= nointr_flag;
5418 
5419 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5420 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5421 
5422 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5423 	    scsipkt->pkt_comp != NULL) {
5424 		/* scsi callback required */
5425 		if (servicing_interrupt()) {
5426 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5427 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5428 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5429 				return (TRAN_BUSY);
5430 			}
5431 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5432 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5433 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5434 			/* Scheduling the callback failed */
5435 			return (TRAN_BUSY);
5436 		}
5437 	}
5438 
5439 	return (rval);
5440 }
5441 
5442 /*
5443  * Translate command: ATA Pass Through
5444  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5445  * PIO Data-Out protocols.  Also supports CK_COND bit.
5446  *
5447  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5448  * described in Table 111 of SAT-2 (Draft 9).
5449  */
5450 static  int
5451 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5452 {
5453 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5454 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5455 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5456 	int extend;
5457 	uint64_t lba;
5458 	uint16_t feature, sec_count;
5459 	int t_len, synch;
5460 	int rval, reason;
5461 
5462 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5463 
5464 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5465 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5466 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5467 		return (rval);
5468 	}
5469 
5470 	/* T_DIR bit */
5471 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5472 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5473 	else
5474 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5475 
5476 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5477 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5478 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5479 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5480 	}
5481 
5482 	/* OFFLINE field. If non-zero, invalid command (for now). */
5483 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5484 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5485 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5486 	}
5487 
5488 	/* PROTOCOL field */
5489 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5490 	case SATL_APT_P_HW_RESET:
5491 	case SATL_APT_P_SRST:
5492 	case SATL_APT_P_DMA:
5493 	case SATL_APT_P_DMA_QUEUED:
5494 	case SATL_APT_P_DEV_DIAG:
5495 	case SATL_APT_P_DEV_RESET:
5496 	case SATL_APT_P_UDMA_IN:
5497 	case SATL_APT_P_UDMA_OUT:
5498 	case SATL_APT_P_FPDMA:
5499 	case SATL_APT_P_RET_RESP:
5500 		/* Not yet implemented */
5501 	default:
5502 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5503 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5504 
5505 	case SATL_APT_P_NON_DATA:
5506 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5507 		break;
5508 
5509 	case SATL_APT_P_PIO_DATA_IN:
5510 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5511 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5512 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5513 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5514 		}
5515 
5516 		/* if there is a buffer, release its DMA resources */
5517 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5518 			sata_scsi_dmafree(NULL, scsipkt);
5519 		} else {
5520 			/* if there is no buffer, how do you PIO in? */
5521 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5522 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5523 		}
5524 
5525 		break;
5526 
5527 	case SATL_APT_P_PIO_DATA_OUT:
5528 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5529 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5530 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5531 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5532 		}
5533 
5534 		/* if there is a buffer, release its DMA resources */
5535 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5536 			sata_scsi_dmafree(NULL, scsipkt);
5537 		} else {
5538 			/* if there is no buffer, how do you PIO out? */
5539 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5540 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5541 		}
5542 
5543 		break;
5544 	}
5545 
5546 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5547 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5548 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5549 		feature = scsipkt->pkt_cdbp[3];
5550 
5551 		sec_count = scsipkt->pkt_cdbp[4];
5552 
5553 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5554 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5555 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5556 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5557 
5558 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5559 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5560 
5561 		break;
5562 
5563 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5564 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5565 			extend = 1;
5566 
5567 			feature = scsipkt->pkt_cdbp[3];
5568 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5569 
5570 			sec_count = scsipkt->pkt_cdbp[5];
5571 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5572 
5573 			lba = scsipkt->pkt_cdbp[11];
5574 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5575 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5576 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5577 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5578 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5579 
5580 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5581 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5582 		} else {
5583 			feature = scsipkt->pkt_cdbp[3];
5584 
5585 			sec_count = scsipkt->pkt_cdbp[5];
5586 
5587 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5588 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5589 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5590 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5591 
5592 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5593 			    0xf0;
5594 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5595 		}
5596 
5597 		break;
5598 	}
5599 
5600 	/* CK_COND bit */
5601 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5602 		if (extend) {
5603 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5604 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5605 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5606 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5607 		}
5608 
5609 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5610 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5611 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5612 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5613 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5614 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5615 	}
5616 
5617 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5618 	if (extend) {
5619 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5620 
5621 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5622 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5623 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5624 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5625 		scmd->satacmd_lba_high_msb = lba >> 40;
5626 	} else {
5627 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5628 
5629 		scmd->satacmd_features_reg_ext = 0;
5630 		scmd->satacmd_sec_count_msb = 0;
5631 		scmd->satacmd_lba_low_msb = 0;
5632 		scmd->satacmd_lba_mid_msb = 0;
5633 		scmd->satacmd_lba_high_msb = 0;
5634 	}
5635 
5636 	scmd->satacmd_features_reg = feature & 0xff;
5637 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5638 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5639 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5640 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5641 
5642 	/* Determine transfer length */
5643 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5644 	case 1:
5645 		t_len = feature;
5646 		break;
5647 	case 2:
5648 		t_len = sec_count;
5649 		break;
5650 	default:
5651 		t_len = 0;
5652 		break;
5653 	}
5654 
5655 	/* Adjust transfer length for the Byte Block bit */
5656 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5657 		t_len *= SATA_DISK_SECTOR_SIZE;
5658 
5659 	/* Start processing command */
5660 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5661 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5662 		synch = FALSE;
5663 	} else {
5664 		synch = TRUE;
5665 	}
5666 
5667 	if (sata_hba_start(spx, &rval) != 0) {
5668 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5669 		return (rval);
5670 	}
5671 
5672 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5673 
5674 	if (synch) {
5675 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5676 	}
5677 
5678 	return (TRAN_ACCEPT);
5679 }
5680 
5681 /*
5682  * Translate command: Log Sense
5683  */
5684 static 	int
5685 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5686 {
5687 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5688 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5689 	sata_drive_info_t *sdinfo;
5690 	struct scsi_extended_sense *sense;
5691 	int 		len, count, alc_len;
5692 	int		pc;	/* Page Control code */
5693 	int		page_code;	/* Page code */
5694 	uint8_t		*buf;	/* log sense buffer */
5695 	int		rval, reason;
5696 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5697 
5698 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5699 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5700 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5701 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5702 
5703 	if (servicing_interrupt()) {
5704 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5705 		if (buf == NULL) {
5706 			return (TRAN_BUSY);
5707 		}
5708 	} else {
5709 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5710 	}
5711 
5712 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5713 
5714 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5715 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5716 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5717 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5718 		return (rval);
5719 	}
5720 
5721 	scsipkt->pkt_reason = CMD_CMPLT;
5722 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5723 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5724 
5725 	pc = scsipkt->pkt_cdbp[2] >> 6;
5726 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5727 
5728 	/* Reject not supported request for all but cumulative values */
5729 	switch (pc) {
5730 	case PC_CUMULATIVE_VALUES:
5731 		break;
5732 	default:
5733 		*scsipkt->pkt_scbp = STATUS_CHECK;
5734 		sense = sata_arq_sense(spx);
5735 		sense->es_key = KEY_ILLEGAL_REQUEST;
5736 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5737 		goto done;
5738 	}
5739 
5740 	switch (page_code) {
5741 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5742 	case PAGE_CODE_SELF_TEST_RESULTS:
5743 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5744 	case PAGE_CODE_SMART_READ_DATA:
5745 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5746 		break;
5747 	default:
5748 		*scsipkt->pkt_scbp = STATUS_CHECK;
5749 		sense = sata_arq_sense(spx);
5750 		sense->es_key = KEY_ILLEGAL_REQUEST;
5751 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5752 		goto done;
5753 	}
5754 
5755 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5756 		/*
5757 		 * Because log sense uses local buffers for data retrieval from
5758 		 * the devices and sets the data programatically in the
5759 		 * original specified buffer, release preallocated DMA
5760 		 * resources before storing data in the original buffer,
5761 		 * so no unwanted DMA sync would take place.
5762 		 */
5763 		sata_id_t *sata_id;
5764 
5765 		sata_scsi_dmafree(NULL, scsipkt);
5766 
5767 		len = 0;
5768 
5769 		/* Build log parameter header */
5770 		buf[len++] = page_code;	/* page code as in the CDB */
5771 		buf[len++] = 0;		/* reserved */
5772 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5773 		buf[len++] = 0;		/* (LSB) */
5774 
5775 		sdinfo = sata_get_device_info(
5776 		    spx->txlt_sata_hba_inst,
5777 		    &spx->txlt_sata_pkt->satapkt_device);
5778 
5779 		/*
5780 		 * Add requested pages.
5781 		 */
5782 		switch (page_code) {
5783 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5784 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5785 			break;
5786 		case PAGE_CODE_SELF_TEST_RESULTS:
5787 			sata_id = &sdinfo->satadrv_id;
5788 			if ((! (sata_id->ai_cmdset84 &
5789 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5790 			    (! (sata_id->ai_features87 &
5791 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5792 				*scsipkt->pkt_scbp = STATUS_CHECK;
5793 				sense = sata_arq_sense(spx);
5794 				sense->es_key = KEY_ILLEGAL_REQUEST;
5795 				sense->es_add_code =
5796 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5797 
5798 				goto done;
5799 			}
5800 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5801 			    spx->txlt_sata_hba_inst);
5802 			break;
5803 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5804 			sata_id = &sdinfo->satadrv_id;
5805 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5806 				*scsipkt->pkt_scbp = STATUS_CHECK;
5807 				sense = sata_arq_sense(spx);
5808 				sense->es_key = KEY_ILLEGAL_REQUEST;
5809 				sense->es_add_code =
5810 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5811 
5812 				goto done;
5813 			}
5814 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5815 				*scsipkt->pkt_scbp = STATUS_CHECK;
5816 				sense = sata_arq_sense(spx);
5817 				sense->es_key = KEY_ABORTED_COMMAND;
5818 				sense->es_add_code =
5819 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5820 				sense->es_qual_code =
5821 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5822 
5823 				goto done;
5824 			}
5825 
5826 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5827 			    spx->txlt_sata_hba_inst);
5828 			break;
5829 		case PAGE_CODE_SMART_READ_DATA:
5830 			sata_id = &sdinfo->satadrv_id;
5831 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5832 				*scsipkt->pkt_scbp = STATUS_CHECK;
5833 				sense = sata_arq_sense(spx);
5834 				sense->es_key = KEY_ILLEGAL_REQUEST;
5835 				sense->es_add_code =
5836 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5837 
5838 				goto done;
5839 			}
5840 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5841 				*scsipkt->pkt_scbp = STATUS_CHECK;
5842 				sense = sata_arq_sense(spx);
5843 				sense->es_key = KEY_ABORTED_COMMAND;
5844 				sense->es_add_code =
5845 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5846 				sense->es_qual_code =
5847 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5848 
5849 				goto done;
5850 			}
5851 
5852 			/* This page doesn't include a page header */
5853 			len = sata_build_lsense_page_30(sdinfo, buf,
5854 			    spx->txlt_sata_hba_inst);
5855 			goto no_header;
5856 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5857 			sata_id = &sdinfo->satadrv_id;
5858 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5859 				*scsipkt->pkt_scbp = STATUS_CHECK;
5860 				sense = sata_arq_sense(spx);
5861 				sense->es_key = KEY_ILLEGAL_REQUEST;
5862 				sense->es_add_code =
5863 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5864 
5865 				goto done;
5866 			}
5867 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5868 				*scsipkt->pkt_scbp = STATUS_CHECK;
5869 				sense = sata_arq_sense(spx);
5870 				sense->es_key = KEY_ABORTED_COMMAND;
5871 				sense->es_add_code =
5872 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5873 				sense->es_qual_code =
5874 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5875 
5876 				goto done;
5877 			}
5878 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5879 			goto no_header;
5880 		default:
5881 			/* Invalid request */
5882 			*scsipkt->pkt_scbp = STATUS_CHECK;
5883 			sense = sata_arq_sense(spx);
5884 			sense->es_key = KEY_ILLEGAL_REQUEST;
5885 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5886 			goto done;
5887 		}
5888 
5889 		/* set parameter log sense data length */
5890 		buf[2] = len >> 8;	/* log sense length (MSB) */
5891 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5892 
5893 		len += SCSI_LOG_PAGE_HDR_LEN;
5894 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5895 
5896 no_header:
5897 		/* Check allocation length */
5898 		alc_len = scsipkt->pkt_cdbp[7];
5899 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5900 
5901 		/*
5902 		 * We do not check for possible parameters truncation
5903 		 * (alc_len < len) assuming that the target driver works
5904 		 * correctly. Just avoiding overrun.
5905 		 * Copy no more than requested and possible, buffer-wise.
5906 		 */
5907 		count = MIN(alc_len, len);
5908 		count = MIN(bp->b_bcount, count);
5909 		bcopy(buf, bp->b_un.b_addr, count);
5910 
5911 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5912 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5913 	}
5914 	*scsipkt->pkt_scbp = STATUS_GOOD;
5915 done:
5916 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5917 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5918 
5919 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5920 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5921 
5922 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5923 	    scsipkt->pkt_comp != NULL) {
5924 		/* scsi callback required */
5925 		if (servicing_interrupt()) {
5926 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5927 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5928 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5929 				return (TRAN_BUSY);
5930 			}
5931 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5932 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5933 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5934 			/* Scheduling the callback failed */
5935 			return (TRAN_BUSY);
5936 		}
5937 	}
5938 
5939 	return (TRAN_ACCEPT);
5940 }
5941 
5942 /*
5943  * Translate command: Log Select
5944  * Not implemented at this time - returns invalid command response.
5945  */
5946 static	int
5947 sata_txlt_log_select(sata_pkt_txlate_t *spx)
5948 {
5949 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5950 	    "sata_txlt_log_select\n", NULL);
5951 
5952 	return (sata_txlt_invalid_command(spx));
5953 }
5954 
5955 
5956 /*
5957  * Translate command: Read (various types).
5958  * Translated into appropriate type of ATA READ command
5959  * for SATA hard disks.
5960  * Both the device capabilities and requested operation mode are
5961  * considered.
5962  *
5963  * Following scsi cdb fields are ignored:
5964  * rdprotect, dpo, fua, fua_nv, group_number.
5965  *
5966  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5967  * enable variable sata_func_enable), the capability of the controller and
5968  * capability of a device are checked and if both support queueing, read
5969  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
5970  * command rather than plain READ_XXX command.
5971  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5972  * both the controller and device suport such functionality, the read
5973  * request will be translated to READ_FPDMA_QUEUED command.
5974  * In both cases the maximum queue depth is derived as minimum of:
5975  * HBA capability,device capability and sata_max_queue_depth variable setting.
5976  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5977  * used to pass max queue depth value, and the maximum possible queue depth
5978  * is 32.
5979  *
5980  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5981  * appropriate values in scsi_pkt fields.
5982  */
5983 static int
5984 sata_txlt_read(sata_pkt_txlate_t *spx)
5985 {
5986 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5987 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5988 	sata_drive_info_t *sdinfo;
5989 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5990 	int cport = SATA_TXLT_CPORT(spx);
5991 	uint16_t sec_count;
5992 	uint64_t lba;
5993 	int rval, reason;
5994 	int synch;
5995 
5996 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5997 
5998 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5999 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6000 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6001 		return (rval);
6002 	}
6003 
6004 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6005 	    &spx->txlt_sata_pkt->satapkt_device);
6006 
6007 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6008 	/*
6009 	 * Extract LBA and sector count from scsi CDB.
6010 	 */
6011 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6012 	case SCMD_READ:
6013 		/* 6-byte scsi read cmd : 0x08 */
6014 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6015 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6016 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6017 		sec_count = scsipkt->pkt_cdbp[4];
6018 		/* sec_count 0 will be interpreted as 256 by a device */
6019 		break;
6020 	case SCMD_READ_G1:
6021 		/* 10-bytes scsi read command : 0x28 */
6022 		lba = scsipkt->pkt_cdbp[2];
6023 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6024 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6025 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6026 		sec_count = scsipkt->pkt_cdbp[7];
6027 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6028 		break;
6029 	case SCMD_READ_G5:
6030 		/* 12-bytes scsi read command : 0xA8 */
6031 		lba = scsipkt->pkt_cdbp[2];
6032 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6033 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6034 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6035 		sec_count = scsipkt->pkt_cdbp[6];
6036 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6037 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6038 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6039 		break;
6040 	case SCMD_READ_G4:
6041 		/* 16-bytes scsi read command : 0x88 */
6042 		lba = scsipkt->pkt_cdbp[2];
6043 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6044 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6045 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6046 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6047 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6048 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6049 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6050 		sec_count = scsipkt->pkt_cdbp[10];
6051 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6052 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6053 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6054 		break;
6055 	default:
6056 		/* Unsupported command */
6057 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6058 		return (sata_txlt_invalid_command(spx));
6059 	}
6060 
6061 	/*
6062 	 * Check if specified address exceeds device capacity
6063 	 */
6064 	if ((lba >= sdinfo->satadrv_capacity) ||
6065 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6066 		/* LBA out of range */
6067 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6068 		return (sata_txlt_lba_out_of_range(spx));
6069 	}
6070 
6071 	/*
6072 	 * For zero-length transfer, emulate good completion of the command
6073 	 * (reasons for rejecting the command were already checked).
6074 	 * No DMA resources were allocated.
6075 	 */
6076 	if (spx->txlt_dma_cookie_list == NULL) {
6077 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6078 		return (sata_emul_rw_completion(spx));
6079 	}
6080 
6081 	/*
6082 	 * Build cmd block depending on the device capability and
6083 	 * requested operation mode.
6084 	 * Do not bother with non-dma mode - we are working only with
6085 	 * devices supporting DMA.
6086 	 */
6087 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6088 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6089 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6090 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6091 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6092 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6093 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6094 #ifndef __lock_lint
6095 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6096 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6097 		scmd->satacmd_lba_high_msb = lba >> 40;
6098 #endif
6099 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6100 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6101 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6102 	}
6103 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6104 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6105 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6106 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6107 	scmd->satacmd_features_reg = 0;
6108 	scmd->satacmd_status_reg = 0;
6109 	scmd->satacmd_error_reg = 0;
6110 
6111 	/*
6112 	 * Check if queueing commands should be used and switch
6113 	 * to appropriate command if possible
6114 	 */
6115 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6116 		boolean_t using_queuing;
6117 
6118 		/* Queuing supported by controller and device? */
6119 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6120 		    (sdinfo->satadrv_features_support &
6121 		    SATA_DEV_F_NCQ) &&
6122 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6123 		    SATA_CTLF_NCQ)) {
6124 			using_queuing = B_TRUE;
6125 
6126 			/* NCQ supported - use FPDMA READ */
6127 			scmd->satacmd_cmd_reg =
6128 			    SATAC_READ_FPDMA_QUEUED;
6129 			scmd->satacmd_features_reg_ext =
6130 			    scmd->satacmd_sec_count_msb;
6131 			scmd->satacmd_sec_count_msb = 0;
6132 		} else if ((sdinfo->satadrv_features_support &
6133 		    SATA_DEV_F_TCQ) &&
6134 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6135 		    SATA_CTLF_QCMD)) {
6136 			using_queuing = B_TRUE;
6137 
6138 			/* Legacy queueing */
6139 			if (sdinfo->satadrv_features_support &
6140 			    SATA_DEV_F_LBA48) {
6141 				scmd->satacmd_cmd_reg =
6142 				    SATAC_READ_DMA_QUEUED_EXT;
6143 				scmd->satacmd_features_reg_ext =
6144 				    scmd->satacmd_sec_count_msb;
6145 				scmd->satacmd_sec_count_msb = 0;
6146 			} else {
6147 				scmd->satacmd_cmd_reg =
6148 				    SATAC_READ_DMA_QUEUED;
6149 			}
6150 		} else	/* NCQ nor legacy queuing not supported */
6151 			using_queuing = B_FALSE;
6152 
6153 		/*
6154 		 * If queuing, the sector count goes in the features register
6155 		 * and the secount count will contain the tag.
6156 		 */
6157 		if (using_queuing) {
6158 			scmd->satacmd_features_reg =
6159 			    scmd->satacmd_sec_count_lsb;
6160 			scmd->satacmd_sec_count_lsb = 0;
6161 			scmd->satacmd_flags.sata_queued = B_TRUE;
6162 
6163 			/* Set-up maximum queue depth */
6164 			scmd->satacmd_flags.sata_max_queue_depth =
6165 			    sdinfo->satadrv_max_queue_depth - 1;
6166 		} else if (sdinfo->satadrv_features_enabled &
6167 		    SATA_DEV_F_E_UNTAGGED_QING) {
6168 			/*
6169 			 * Although NCQ/TCQ is not enabled, untagged queuing
6170 			 * may be still used.
6171 			 * Set-up the maximum untagged queue depth.
6172 			 * Use controller's queue depth from sata_hba_tran.
6173 			 * SATA HBA drivers may ignore this value and rely on
6174 			 * the internal limits.For drivers that do not
6175 			 * ignore untaged queue depth, limit the value to
6176 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6177 			 * largest value that can be passed via
6178 			 * satacmd_flags.sata_max_queue_depth.
6179 			 */
6180 			scmd->satacmd_flags.sata_max_queue_depth =
6181 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6182 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6183 
6184 		} else {
6185 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6186 		}
6187 	} else
6188 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6189 
6190 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6191 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6192 	    scmd->satacmd_cmd_reg, lba, sec_count);
6193 
6194 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6195 		/* Need callback function */
6196 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6197 		synch = FALSE;
6198 	} else
6199 		synch = TRUE;
6200 
6201 	/* Transfer command to HBA */
6202 	if (sata_hba_start(spx, &rval) != 0) {
6203 		/* Pkt not accepted for execution */
6204 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6205 		return (rval);
6206 	}
6207 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6208 	/*
6209 	 * If execution is non-synchronous,
6210 	 * a callback function will handle potential errors, translate
6211 	 * the response and will do a callback to a target driver.
6212 	 * If it was synchronous, check execution status using the same
6213 	 * framework callback.
6214 	 */
6215 	if (synch) {
6216 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6217 		    "synchronous execution status %x\n",
6218 		    spx->txlt_sata_pkt->satapkt_reason);
6219 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6220 	}
6221 	return (TRAN_ACCEPT);
6222 }
6223 
6224 
6225 /*
6226  * SATA translate command: Write (various types)
6227  * Translated into appropriate type of ATA WRITE command
6228  * for SATA hard disks.
6229  * Both the device capabilities and requested operation mode are
6230  * considered.
6231  *
6232  * Following scsi cdb fields are ignored:
6233  * rwprotect, dpo, fua, fua_nv, group_number.
6234  *
6235  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6236  * enable variable sata_func_enable), the capability of the controller and
6237  * capability of a device are checked and if both support queueing, write
6238  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6239  * command rather than plain WRITE_XXX command.
6240  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6241  * both the controller and device suport such functionality, the write
6242  * request will be translated to WRITE_FPDMA_QUEUED command.
6243  * In both cases the maximum queue depth is derived as minimum of:
6244  * HBA capability,device capability and sata_max_queue_depth variable setting.
6245  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6246  * used to pass max queue depth value, and the maximum possible queue depth
6247  * is 32.
6248  *
6249  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6250  * appropriate values in scsi_pkt fields.
6251  */
6252 static int
6253 sata_txlt_write(sata_pkt_txlate_t *spx)
6254 {
6255 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6256 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6257 	sata_drive_info_t *sdinfo;
6258 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6259 	int cport = SATA_TXLT_CPORT(spx);
6260 	uint16_t sec_count;
6261 	uint64_t lba;
6262 	int rval, reason;
6263 	int synch;
6264 
6265 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6266 
6267 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6268 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6269 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6270 		return (rval);
6271 	}
6272 
6273 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6274 	    &spx->txlt_sata_pkt->satapkt_device);
6275 
6276 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6277 	/*
6278 	 * Extract LBA and sector count from scsi CDB
6279 	 */
6280 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6281 	case SCMD_WRITE:
6282 		/* 6-byte scsi read cmd : 0x0A */
6283 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6284 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6285 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6286 		sec_count = scsipkt->pkt_cdbp[4];
6287 		/* sec_count 0 will be interpreted as 256 by a device */
6288 		break;
6289 	case SCMD_WRITE_G1:
6290 		/* 10-bytes scsi write command : 0x2A */
6291 		lba = scsipkt->pkt_cdbp[2];
6292 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6293 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6294 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6295 		sec_count = scsipkt->pkt_cdbp[7];
6296 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6297 		break;
6298 	case SCMD_WRITE_G5:
6299 		/* 12-bytes scsi read command : 0xAA */
6300 		lba = scsipkt->pkt_cdbp[2];
6301 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6302 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6303 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6304 		sec_count = scsipkt->pkt_cdbp[6];
6305 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6306 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6307 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6308 		break;
6309 	case SCMD_WRITE_G4:
6310 		/* 16-bytes scsi write command : 0x8A */
6311 		lba = scsipkt->pkt_cdbp[2];
6312 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6313 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6314 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6315 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6316 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6317 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6318 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6319 		sec_count = scsipkt->pkt_cdbp[10];
6320 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6321 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6322 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6323 		break;
6324 	default:
6325 		/* Unsupported command */
6326 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6327 		return (sata_txlt_invalid_command(spx));
6328 	}
6329 
6330 	/*
6331 	 * Check if specified address and length exceeds device capacity
6332 	 */
6333 	if ((lba >= sdinfo->satadrv_capacity) ||
6334 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6335 		/* LBA out of range */
6336 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6337 		return (sata_txlt_lba_out_of_range(spx));
6338 	}
6339 
6340 	/*
6341 	 * For zero-length transfer, emulate good completion of the command
6342 	 * (reasons for rejecting the command were already checked).
6343 	 * No DMA resources were allocated.
6344 	 */
6345 	if (spx->txlt_dma_cookie_list == NULL) {
6346 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6347 		return (sata_emul_rw_completion(spx));
6348 	}
6349 
6350 	/*
6351 	 * Build cmd block depending on the device capability and
6352 	 * requested operation mode.
6353 	 * Do not bother with non-dma mode- we are working only with
6354 	 * devices supporting DMA.
6355 	 */
6356 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6357 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6358 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6359 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6360 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6361 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6362 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6363 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6364 #ifndef __lock_lint
6365 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6366 		scmd->satacmd_lba_high_msb = lba >> 40;
6367 #endif
6368 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6369 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6370 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6371 	}
6372 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6373 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6374 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6375 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6376 	scmd->satacmd_features_reg = 0;
6377 	scmd->satacmd_status_reg = 0;
6378 	scmd->satacmd_error_reg = 0;
6379 
6380 	/*
6381 	 * Check if queueing commands should be used and switch
6382 	 * to appropriate command if possible
6383 	 */
6384 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6385 		boolean_t using_queuing;
6386 
6387 		/* Queuing supported by controller and device? */
6388 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6389 		    (sdinfo->satadrv_features_support &
6390 		    SATA_DEV_F_NCQ) &&
6391 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6392 		    SATA_CTLF_NCQ)) {
6393 			using_queuing = B_TRUE;
6394 
6395 			/* NCQ supported - use FPDMA WRITE */
6396 			scmd->satacmd_cmd_reg =
6397 			    SATAC_WRITE_FPDMA_QUEUED;
6398 			scmd->satacmd_features_reg_ext =
6399 			    scmd->satacmd_sec_count_msb;
6400 			scmd->satacmd_sec_count_msb = 0;
6401 		} else if ((sdinfo->satadrv_features_support &
6402 		    SATA_DEV_F_TCQ) &&
6403 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6404 		    SATA_CTLF_QCMD)) {
6405 			using_queuing = B_TRUE;
6406 
6407 			/* Legacy queueing */
6408 			if (sdinfo->satadrv_features_support &
6409 			    SATA_DEV_F_LBA48) {
6410 				scmd->satacmd_cmd_reg =
6411 				    SATAC_WRITE_DMA_QUEUED_EXT;
6412 				scmd->satacmd_features_reg_ext =
6413 				    scmd->satacmd_sec_count_msb;
6414 				scmd->satacmd_sec_count_msb = 0;
6415 			} else {
6416 				scmd->satacmd_cmd_reg =
6417 				    SATAC_WRITE_DMA_QUEUED;
6418 			}
6419 		} else	/*  NCQ nor legacy queuing not supported */
6420 			using_queuing = B_FALSE;
6421 
6422 		if (using_queuing) {
6423 			scmd->satacmd_features_reg =
6424 			    scmd->satacmd_sec_count_lsb;
6425 			scmd->satacmd_sec_count_lsb = 0;
6426 			scmd->satacmd_flags.sata_queued = B_TRUE;
6427 			/* Set-up maximum queue depth */
6428 			scmd->satacmd_flags.sata_max_queue_depth =
6429 			    sdinfo->satadrv_max_queue_depth - 1;
6430 		} else if (sdinfo->satadrv_features_enabled &
6431 		    SATA_DEV_F_E_UNTAGGED_QING) {
6432 			/*
6433 			 * Although NCQ/TCQ is not enabled, untagged queuing
6434 			 * may be still used.
6435 			 * Set-up the maximum untagged queue depth.
6436 			 * Use controller's queue depth from sata_hba_tran.
6437 			 * SATA HBA drivers may ignore this value and rely on
6438 			 * the internal limits. For drivera that do not
6439 			 * ignore untaged queue depth, limit the value to
6440 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6441 			 * largest value that can be passed via
6442 			 * satacmd_flags.sata_max_queue_depth.
6443 			 */
6444 			scmd->satacmd_flags.sata_max_queue_depth =
6445 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6446 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6447 
6448 		} else {
6449 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6450 		}
6451 	} else
6452 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6453 
6454 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6455 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6456 	    scmd->satacmd_cmd_reg, lba, sec_count);
6457 
6458 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6459 		/* Need callback function */
6460 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6461 		synch = FALSE;
6462 	} else
6463 		synch = TRUE;
6464 
6465 	/* Transfer command to HBA */
6466 	if (sata_hba_start(spx, &rval) != 0) {
6467 		/* Pkt not accepted for execution */
6468 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6469 		return (rval);
6470 	}
6471 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6472 
6473 	/*
6474 	 * If execution is non-synchronous,
6475 	 * a callback function will handle potential errors, translate
6476 	 * the response and will do a callback to a target driver.
6477 	 * If it was synchronous, check execution status using the same
6478 	 * framework callback.
6479 	 */
6480 	if (synch) {
6481 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6482 		    "synchronous execution status %x\n",
6483 		    spx->txlt_sata_pkt->satapkt_reason);
6484 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6485 	}
6486 	return (TRAN_ACCEPT);
6487 }
6488 
6489 
6490 /*
6491  * Implements SCSI SBC WRITE BUFFER command download microcode option
6492  */
6493 static int
6494 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6495 {
6496 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6497 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6498 
6499 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
6500 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6501 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6502 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6503 
6504 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6505 	struct scsi_extended_sense *sense;
6506 	int rval, mode, sector_count, reason;
6507 	int cport = SATA_TXLT_CPORT(spx);
6508 
6509 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6510 
6511 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6512 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6513 
6514 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6515 
6516 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6517 	    TRAN_ACCEPT) {
6518 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6519 		return (rval);
6520 	}
6521 
6522 	/* Use synchronous mode */
6523 	spx->txlt_sata_pkt->satapkt_op_mode
6524 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6525 
6526 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6527 
6528 	scsipkt->pkt_reason = CMD_CMPLT;
6529 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6530 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6531 
6532 	/*
6533 	 * The SCSI to ATA translation specification only calls
6534 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6535 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6536 	 * ATA 8 (draft) got rid of download microcode for temp
6537 	 * and it is even optional for ATA 7, so it may be aborted.
6538 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6539 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6540 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6541 	 * sectors.  Thus the offset really doesn't buy us anything.
6542 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6543 	 * is revised, this can be revisisted.
6544 	 */
6545 	/* Reject not supported request */
6546 	switch (mode) {
6547 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6548 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6549 		break;
6550 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6551 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6552 		break;
6553 	default:
6554 		goto bad_param;
6555 	}
6556 
6557 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6558 
6559 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6560 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6561 		goto bad_param;
6562 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6563 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6564 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6565 	scmd->satacmd_lba_mid_lsb = 0;
6566 	scmd->satacmd_lba_high_lsb = 0;
6567 	scmd->satacmd_device_reg = 0;
6568 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6569 	scmd->satacmd_addr_type = 0;
6570 
6571 	/* Transfer command to HBA */
6572 	if (sata_hba_start(spx, &rval) != 0) {
6573 		/* Pkt not accepted for execution */
6574 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
6575 		return (rval);
6576 	}
6577 
6578 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
6579 
6580 	/* Then we need synchronous check the status of the disk */
6581 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6582 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6583 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6584 		scsipkt->pkt_reason = CMD_CMPLT;
6585 
6586 		/* Download commmand succeed, so probe and identify device */
6587 		sata_reidentify_device(spx);
6588 	} else {
6589 		/* Something went wrong, microcode download command failed */
6590 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6591 		*scsipkt->pkt_scbp = STATUS_CHECK;
6592 		sense = sata_arq_sense(spx);
6593 		switch (sata_pkt->satapkt_reason) {
6594 		case SATA_PKT_PORT_ERROR:
6595 			/*
6596 			 * We have no device data. Assume no data transfered.
6597 			 */
6598 			sense->es_key = KEY_HARDWARE_ERROR;
6599 			break;
6600 
6601 		case SATA_PKT_DEV_ERROR:
6602 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6603 			    SATA_STATUS_ERR) {
6604 				/*
6605 				 * determine dev error reason from error
6606 				 * reg content
6607 				 */
6608 				sata_decode_device_error(spx, sense);
6609 				break;
6610 			}
6611 			/* No extended sense key - no info available */
6612 			break;
6613 
6614 		case SATA_PKT_TIMEOUT:
6615 			scsipkt->pkt_reason = CMD_TIMEOUT;
6616 			scsipkt->pkt_statistics |=
6617 			    STAT_TIMEOUT | STAT_DEV_RESET;
6618 			/* No extended sense key ? */
6619 			break;
6620 
6621 		case SATA_PKT_ABORTED:
6622 			scsipkt->pkt_reason = CMD_ABORTED;
6623 			scsipkt->pkt_statistics |= STAT_ABORTED;
6624 			/* No extended sense key ? */
6625 			break;
6626 
6627 		case SATA_PKT_RESET:
6628 			/* pkt aborted by an explicit reset from a host */
6629 			scsipkt->pkt_reason = CMD_RESET;
6630 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6631 			break;
6632 
6633 		default:
6634 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6635 			    "sata_txlt_nodata_cmd_completion: "
6636 			    "invalid packet completion reason %d",
6637 			    sata_pkt->satapkt_reason));
6638 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6639 			break;
6640 		}
6641 
6642 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6643 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6644 
6645 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6646 			/* scsi callback required */
6647 			scsi_hba_pkt_comp(scsipkt);
6648 	}
6649 	return (TRAN_ACCEPT);
6650 
6651 bad_param:
6652 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6653 	*scsipkt->pkt_scbp = STATUS_CHECK;
6654 	sense = sata_arq_sense(spx);
6655 	sense->es_key = KEY_ILLEGAL_REQUEST;
6656 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6657 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6658 	    scsipkt->pkt_comp != NULL) {
6659 		/* scsi callback required */
6660 		if (servicing_interrupt()) {
6661 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6662 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6663 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6664 				return (TRAN_BUSY);
6665 			}
6666 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6667 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6668 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6669 			/* Scheduling the callback failed */
6670 			return (TRAN_BUSY);
6671 		}
6672 	}
6673 	return (rval);
6674 }
6675 
6676 /*
6677  * Re-identify device after doing a firmware download.
6678  */
6679 static void
6680 sata_reidentify_device(sata_pkt_txlate_t *spx)
6681 {
6682 #define	DOWNLOAD_WAIT_TIME_SECS	60
6683 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6684 	int rval;
6685 	int retry_cnt;
6686 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6687 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6688 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6689 	sata_drive_info_t *sdinfo;
6690 
6691 	/*
6692 	 * Before returning good status, probe device.
6693 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6694 	 * The assumption is that the new microcode is applied by the
6695 	 * device. It is a caller responsibility to verify this.
6696 	 */
6697 	for (retry_cnt = 0;
6698 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6699 	    retry_cnt++) {
6700 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6701 
6702 		if (rval == SATA_SUCCESS) { /* Set default features */
6703 			sdinfo = sata_get_device_info(sata_hba_inst,
6704 			    &sata_device);
6705 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6706 			    SATA_SUCCESS) {
6707 				/* retry */
6708 				rval = sata_initialize_device(sata_hba_inst,
6709 				    sdinfo);
6710 				if (rval == SATA_RETRY)
6711 					sata_log(sata_hba_inst, CE_WARN,
6712 					    "SATA device at port %d pmport %d -"
6713 					    " default device features could not"
6714 					    " be set. Device may not operate "
6715 					    "as expected.",
6716 					    sata_device.satadev_addr.cport,
6717 					    sata_device.satadev_addr.pmport);
6718 			}
6719 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6720 				scsi_hba_pkt_comp(scsipkt);
6721 			return;
6722 		} else if (rval == SATA_RETRY) {
6723 			delay(drv_usectohz(1000000 *
6724 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6725 			continue;
6726 		} else	/* failed - no reason to retry */
6727 			break;
6728 	}
6729 
6730 	/*
6731 	 * Something went wrong, device probing failed.
6732 	 */
6733 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6734 	    "Cannot probe device after downloading microcode\n"));
6735 
6736 	/* Reset device to force retrying the probe. */
6737 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6738 	    (SATA_DIP(sata_hba_inst), &sata_device);
6739 
6740 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6741 		scsi_hba_pkt_comp(scsipkt);
6742 }
6743 
6744 
6745 /*
6746  * Translate command: Synchronize Cache.
6747  * Translates into Flush Cache command for SATA hard disks.
6748  *
6749  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6750  * appropriate values in scsi_pkt fields.
6751  */
6752 static 	int
6753 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6754 {
6755 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6756 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6757 	int cport = SATA_TXLT_CPORT(spx);
6758 	int rval, reason;
6759 	int synch;
6760 
6761 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6762 
6763 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6764 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6765 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6766 		return (rval);
6767 	}
6768 
6769 	scmd->satacmd_addr_type = 0;
6770 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6771 	scmd->satacmd_device_reg = 0;
6772 	scmd->satacmd_sec_count_lsb = 0;
6773 	scmd->satacmd_lba_low_lsb = 0;
6774 	scmd->satacmd_lba_mid_lsb = 0;
6775 	scmd->satacmd_lba_high_lsb = 0;
6776 	scmd->satacmd_features_reg = 0;
6777 	scmd->satacmd_status_reg = 0;
6778 	scmd->satacmd_error_reg = 0;
6779 
6780 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6781 	    "sata_txlt_synchronize_cache\n", NULL);
6782 
6783 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6784 		/* Need to set-up a callback function */
6785 		spx->txlt_sata_pkt->satapkt_comp =
6786 		    sata_txlt_nodata_cmd_completion;
6787 		synch = FALSE;
6788 	} else
6789 		synch = TRUE;
6790 
6791 	/* Transfer command to HBA */
6792 	if (sata_hba_start(spx, &rval) != 0) {
6793 		/* Pkt not accepted for execution */
6794 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6795 		return (rval);
6796 	}
6797 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6798 
6799 	/*
6800 	 * If execution non-synchronous, it had to be completed
6801 	 * a callback function will handle potential errors, translate
6802 	 * the response and will do a callback to a target driver.
6803 	 * If it was synchronous, check status, using the same
6804 	 * framework callback.
6805 	 */
6806 	if (synch) {
6807 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6808 		    "synchronous execution status %x\n",
6809 		    spx->txlt_sata_pkt->satapkt_reason);
6810 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6811 	}
6812 	return (TRAN_ACCEPT);
6813 }
6814 
6815 
6816 /*
6817  * Send pkt to SATA HBA driver
6818  *
6819  * This function may be called only if the operation is requested by scsi_pkt,
6820  * i.e. scsi_pkt is not NULL.
6821  *
6822  * This function has to be called with cport mutex held. It does release
6823  * the mutex when it calls HBA driver sata_tran_start function and
6824  * re-acquires it afterwards.
6825  *
6826  * If return value is 0, pkt was accepted, -1 otherwise
6827  * rval is set to appropriate sata_scsi_start return value.
6828  *
6829  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6830  * have called the sata_pkt callback function for this packet.
6831  *
6832  * The scsi callback has to be performed by the caller of this routine.
6833  */
6834 static int
6835 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6836 {
6837 	int stat;
6838 	uint8_t cport = SATA_TXLT_CPORT(spx);
6839 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6840 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6841 	sata_drive_info_t *sdinfo;
6842 	sata_pmult_info_t *pminfo;
6843 	sata_pmport_info_t *pmportinfo = NULL;
6844 	sata_device_t *sata_device = NULL;
6845 	uint8_t cmd;
6846 	struct sata_cmd_flags cmd_flags;
6847 
6848 	ASSERT(spx->txlt_sata_pkt != NULL);
6849 
6850 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6851 
6852 	sdinfo = sata_get_device_info(sata_hba_inst,
6853 	    &spx->txlt_sata_pkt->satapkt_device);
6854 	ASSERT(sdinfo != NULL);
6855 
6856 	/* Clear device reset state? */
6857 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6858 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6859 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6860 
6861 		/*
6862 		 * Get the pmult_info of the its parent port multiplier, all
6863 		 * sub-devices share a common device reset flags on in
6864 		 * pmult_info.
6865 		 */
6866 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6867 		pmportinfo = pminfo->pmult_dev_port[pmport];
6868 		ASSERT(pminfo != NULL);
6869 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6870 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6871 			    sata_clear_dev_reset = B_TRUE;
6872 			pminfo->pmult_event_flags &=
6873 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6874 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6875 			    "sata_hba_start: clearing device reset state"
6876 			    "on pmult.\n", NULL);
6877 		}
6878 	} else {
6879 		if (sdinfo->satadrv_event_flags &
6880 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6881 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6882 			    sata_clear_dev_reset = B_TRUE;
6883 			sdinfo->satadrv_event_flags &=
6884 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6885 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6886 			    "sata_hba_start: clearing device reset state\n",
6887 			    NULL);
6888 		}
6889 	}
6890 
6891 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6892 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6893 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6894 
6895 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6896 
6897 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6898 	    "Sata cmd 0x%2x\n", cmd);
6899 
6900 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6901 	    spx->txlt_sata_pkt);
6902 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6903 	/*
6904 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6905 	 * with the sata callback, the sata_pkt could be already destroyed
6906 	 * by the time we check ther return status from the hba_start()
6907 	 * function, because sata_scsi_destroy_pkt() could have been already
6908 	 * called (perhaps in the interrupt context). So, in such case, there
6909 	 * should be no references to it. In other cases, sata_pkt still
6910 	 * exists.
6911 	 */
6912 	if (stat == SATA_TRAN_ACCEPTED) {
6913 		/*
6914 		 * pkt accepted for execution.
6915 		 * If it was executed synchronously, it is already completed
6916 		 * and pkt completion_reason indicates completion status.
6917 		 */
6918 		*rval = TRAN_ACCEPT;
6919 		return (0);
6920 	}
6921 
6922 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6923 	switch (stat) {
6924 	case SATA_TRAN_QUEUE_FULL:
6925 		/*
6926 		 * Controller detected queue full condition.
6927 		 */
6928 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
6929 		    "sata_hba_start: queue full\n", NULL);
6930 
6931 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6932 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
6933 
6934 		*rval = TRAN_BUSY;
6935 		break;
6936 
6937 	case SATA_TRAN_PORT_ERROR:
6938 		/*
6939 		 * Communication/link with device or general port error
6940 		 * detected before pkt execution begun.
6941 		 */
6942 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6943 		    SATA_ADDR_CPORT ||
6944 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6945 		    SATA_ADDR_DCPORT)
6946 			sata_log(sata_hba_inst, CE_CONT,
6947 			    "SATA port %d error",
6948 			    sata_device->satadev_addr.cport);
6949 		else
6950 			sata_log(sata_hba_inst, CE_CONT,
6951 			    "SATA port %d:%d error\n",
6952 			    sata_device->satadev_addr.cport,
6953 			    sata_device->satadev_addr.pmport);
6954 
6955 		/*
6956 		 * Update the port/device structure.
6957 		 * sata_pkt should be still valid. Since port error is
6958 		 * returned, sata_device content should reflect port
6959 		 * state - it means, that sata address have been changed,
6960 		 * because original packet's sata address refered to a device
6961 		 * attached to some port.
6962 		 */
6963 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
6964 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
6965 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6966 			mutex_enter(&pmportinfo->pmport_mutex);
6967 			sata_update_pmport_info(sata_hba_inst, sata_device);
6968 			mutex_exit(&pmportinfo->pmport_mutex);
6969 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6970 		} else {
6971 			sata_update_port_info(sata_hba_inst, sata_device);
6972 		}
6973 
6974 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6975 		*rval = TRAN_FATAL_ERROR;
6976 		break;
6977 
6978 	case SATA_TRAN_CMD_UNSUPPORTED:
6979 		/*
6980 		 * Command rejected by HBA as unsupported. It was HBA driver
6981 		 * that rejected the command, command was not sent to
6982 		 * an attached device.
6983 		 */
6984 		if ((sdinfo != NULL) &&
6985 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
6986 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6987 			    "sat_hba_start: cmd 0x%2x rejected "
6988 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
6989 
6990 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6991 		(void) sata_txlt_invalid_command(spx);
6992 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6993 
6994 		*rval = TRAN_ACCEPT;
6995 		break;
6996 
6997 	case SATA_TRAN_BUSY:
6998 		/*
6999 		 * Command rejected by HBA because other operation prevents
7000 		 * accepting the packet, or device is in RESET condition.
7001 		 */
7002 		if (sdinfo != NULL) {
7003 			sdinfo->satadrv_state =
7004 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7005 
7006 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7007 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7008 				    "sata_hba_start: cmd 0x%2x rejected "
7009 				    "because of device reset condition\n",
7010 				    cmd);
7011 			} else {
7012 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7013 				    "sata_hba_start: cmd 0x%2x rejected "
7014 				    "with SATA_TRAN_BUSY status\n",
7015 				    cmd);
7016 			}
7017 		}
7018 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7019 		*rval = TRAN_BUSY;
7020 		break;
7021 
7022 	default:
7023 		/* Unrecognized HBA response */
7024 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7025 		    "sata_hba_start: unrecognized HBA response "
7026 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7027 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7028 		*rval = TRAN_FATAL_ERROR;
7029 		break;
7030 	}
7031 
7032 	/*
7033 	 * If we got here, the packet was rejected.
7034 	 * Check if we need to remember reset state clearing request
7035 	 */
7036 	if (cmd_flags.sata_clear_dev_reset) {
7037 		/*
7038 		 * Check if device is still configured - it may have
7039 		 * disapeared from the configuration
7040 		 */
7041 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7042 		if (sdinfo != NULL) {
7043 			/*
7044 			 * Restore the flag that requests clearing of
7045 			 * the device reset state,
7046 			 * so the next sata packet may carry it to HBA.
7047 			 */
7048 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7049 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7050 				pminfo->pmult_event_flags |=
7051 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7052 			} else {
7053 				sdinfo->satadrv_event_flags |=
7054 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7055 			}
7056 		}
7057 	}
7058 	return (-1);
7059 }
7060 
7061 /*
7062  * Scsi response setup for invalid LBA
7063  *
7064  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7065  */
7066 static int
7067 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7068 {
7069 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7070 	struct scsi_extended_sense *sense;
7071 
7072 	scsipkt->pkt_reason = CMD_CMPLT;
7073 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7074 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7075 	*scsipkt->pkt_scbp = STATUS_CHECK;
7076 
7077 	*scsipkt->pkt_scbp = STATUS_CHECK;
7078 	sense = sata_arq_sense(spx);
7079 	sense->es_key = KEY_ILLEGAL_REQUEST;
7080 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7081 
7082 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7083 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7084 
7085 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7086 	    scsipkt->pkt_comp != NULL) {
7087 		/* scsi callback required */
7088 		if (servicing_interrupt()) {
7089 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7090 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7091 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7092 				return (TRAN_BUSY);
7093 			}
7094 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7095 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7096 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7097 			/* Scheduling the callback failed */
7098 			return (TRAN_BUSY);
7099 		}
7100 	}
7101 	return (TRAN_ACCEPT);
7102 }
7103 
7104 
7105 /*
7106  * Analyze device status and error registers and translate them into
7107  * appropriate scsi sense codes.
7108  * NOTE: non-packet commands only for now
7109  */
7110 static void
7111 sata_decode_device_error(sata_pkt_txlate_t *spx,
7112     struct scsi_extended_sense *sense)
7113 {
7114 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7115 
7116 	ASSERT(sense != NULL);
7117 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7118 	    SATA_STATUS_ERR);
7119 
7120 
7121 	if (err_reg & SATA_ERROR_ICRC) {
7122 		sense->es_key = KEY_ABORTED_COMMAND;
7123 		sense->es_add_code = 0x08; /* Communication failure */
7124 		return;
7125 	}
7126 
7127 	if (err_reg & SATA_ERROR_UNC) {
7128 		sense->es_key = KEY_MEDIUM_ERROR;
7129 		/* Information bytes (LBA) need to be set by a caller */
7130 		return;
7131 	}
7132 
7133 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7134 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7135 		sense->es_key = KEY_UNIT_ATTENTION;
7136 		sense->es_add_code = 0x3a; /* No media present */
7137 		return;
7138 	}
7139 
7140 	if (err_reg & SATA_ERROR_IDNF) {
7141 		if (err_reg & SATA_ERROR_ABORT) {
7142 			sense->es_key = KEY_ABORTED_COMMAND;
7143 		} else {
7144 			sense->es_key = KEY_ILLEGAL_REQUEST;
7145 			sense->es_add_code = 0x21; /* LBA out of range */
7146 		}
7147 		return;
7148 	}
7149 
7150 	if (err_reg & SATA_ERROR_ABORT) {
7151 		ASSERT(spx->txlt_sata_pkt != NULL);
7152 		sense->es_key = KEY_ABORTED_COMMAND;
7153 		return;
7154 	}
7155 }
7156 
7157 /*
7158  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7159  */
7160 static void
7161 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7162 {
7163 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7164 
7165 	*lba = 0;
7166 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7167 		*lba = sata_cmd->satacmd_lba_high_msb;
7168 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7169 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7170 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7171 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7172 	}
7173 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7174 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7175 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7176 }
7177 
7178 /*
7179  * This is fixed sense format - if LBA exceeds the info field size,
7180  * no valid info will be returned (valid bit in extended sense will
7181  * be set to 0).
7182  */
7183 static struct scsi_extended_sense *
7184 sata_arq_sense(sata_pkt_txlate_t *spx)
7185 {
7186 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7187 	struct scsi_arq_status *arqs;
7188 	struct scsi_extended_sense *sense;
7189 
7190 	/* Fill ARQ sense data */
7191 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7192 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7193 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7194 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7195 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7196 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7197 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7198 	arqs->sts_rqpkt_resid = 0;
7199 	sense = &arqs->sts_sensedata;
7200 	bzero(sense, sizeof (struct scsi_extended_sense));
7201 	sata_fixed_sense_data_preset(sense);
7202 	return (sense);
7203 }
7204 
7205 /*
7206  * ATA Pass Through support
7207  * Sets flags indicating that an invalid value was found in some
7208  * field in the command.  It could be something illegal according to
7209  * the SAT-2 spec or it could be a feature that is not (yet?)
7210  * supported.
7211  */
7212 static int
7213 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7214 {
7215 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7216 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7217 
7218 	scsipkt->pkt_reason = CMD_CMPLT;
7219 	*scsipkt->pkt_scbp = STATUS_CHECK;
7220 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7221 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7222 
7223 	sense = sata_arq_sense(spx);
7224 	sense->es_key = KEY_ILLEGAL_REQUEST;
7225 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7226 
7227 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7228 	    scsipkt->pkt_comp != NULL) {
7229 		/* scsi callback required */
7230 		if (servicing_interrupt()) {
7231 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7232 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7233 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7234 				return (TRAN_BUSY);
7235 			}
7236 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7237 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7238 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7239 			/* Scheduling the callback failed */
7240 			return (TRAN_BUSY);
7241 		}
7242 	}
7243 
7244 	return (TRAN_ACCEPT);
7245 }
7246 
7247 /*
7248  * The UNMAP command considers it not to be an error if the parameter length
7249  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7250  * to do so just complete the command.
7251  */
7252 static int
7253 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7254 {
7255 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7256 
7257 	scsipkt->pkt_reason = CMD_CMPLT;
7258 	*scsipkt->pkt_scbp = STATUS_GOOD;
7259 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7260 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7261 
7262 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7263 	    scsipkt->pkt_comp != NULL) {
7264 		/* scsi callback required */
7265 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7266 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7267 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7268 			/* Scheduling the callback failed */
7269 			return (TRAN_BUSY);
7270 		}
7271 	}
7272 
7273 	return (TRAN_ACCEPT);
7274 }
7275 
7276 /*
7277  * Emulated SATA Read/Write command completion for zero-length requests.
7278  * This request always succedes, so in synchronous mode it always returns
7279  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7280  * callback cannot be scheduled.
7281  */
7282 static int
7283 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7284 {
7285 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7286 
7287 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7288 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7289 	scsipkt->pkt_reason = CMD_CMPLT;
7290 	*scsipkt->pkt_scbp = STATUS_GOOD;
7291 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7292 		/* scsi callback required - have to schedule it */
7293 		if (servicing_interrupt()) {
7294 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7295 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7296 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7297 				return (TRAN_BUSY);
7298 			}
7299 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7300 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7301 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7302 			/* Scheduling the callback failed */
7303 			return (TRAN_BUSY);
7304 		}
7305 	}
7306 	return (TRAN_ACCEPT);
7307 }
7308 
7309 
7310 /*
7311  * Translate completion status of SATA read/write commands into scsi response.
7312  * pkt completion_reason is checked to determine the completion status.
7313  * Do scsi callback if necessary.
7314  *
7315  * Note: this function may be called also for synchronously executed
7316  * commands.
7317  * This function may be used only if scsi_pkt is non-NULL.
7318  */
7319 static void
7320 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7321 {
7322 	sata_pkt_txlate_t *spx =
7323 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7324 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7325 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7326 	struct scsi_extended_sense *sense;
7327 	uint64_t lba;
7328 	struct buf *bp;
7329 	int rval;
7330 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7331 		/* Normal completion */
7332 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7333 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7334 		scsipkt->pkt_reason = CMD_CMPLT;
7335 		*scsipkt->pkt_scbp = STATUS_GOOD;
7336 		if (spx->txlt_tmp_buf != NULL) {
7337 			/* Temporary buffer was used */
7338 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7339 			if (bp->b_flags & B_READ) {
7340 				rval = ddi_dma_sync(
7341 				    spx->txlt_buf_dma_handle, 0, 0,
7342 				    DDI_DMA_SYNC_FORCPU);
7343 				ASSERT(rval == DDI_SUCCESS);
7344 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7345 				    bp->b_bcount);
7346 			}
7347 		}
7348 	} else {
7349 		/*
7350 		 * Something went wrong - analyze return
7351 		 */
7352 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7353 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7354 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7355 		*scsipkt->pkt_scbp = STATUS_CHECK;
7356 		sense = sata_arq_sense(spx);
7357 		ASSERT(sense != NULL);
7358 
7359 		/*
7360 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7361 		 * extract from device registers the failing LBA.
7362 		 */
7363 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7364 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7365 			    (scmd->satacmd_lba_mid_msb != 0 ||
7366 			    scmd->satacmd_lba_high_msb != 0)) {
7367 				/*
7368 				 * We have problem reporting this cmd LBA
7369 				 * in fixed sense data format, because of
7370 				 * the size of the scsi LBA fields.
7371 				 */
7372 				sense->es_valid = 0;
7373 			} else {
7374 				sata_extract_error_lba(spx, &lba);
7375 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7376 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7377 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7378 				sense->es_info_4 = lba & 0xFF;
7379 			}
7380 		} else {
7381 			/* Invalid extended sense info */
7382 			sense->es_valid = 0;
7383 		}
7384 
7385 		switch (sata_pkt->satapkt_reason) {
7386 		case SATA_PKT_PORT_ERROR:
7387 			/* We may want to handle DEV GONE state as well */
7388 			/*
7389 			 * We have no device data. Assume no data transfered.
7390 			 */
7391 			sense->es_key = KEY_HARDWARE_ERROR;
7392 			break;
7393 
7394 		case SATA_PKT_DEV_ERROR:
7395 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7396 			    SATA_STATUS_ERR) {
7397 				/*
7398 				 * determine dev error reason from error
7399 				 * reg content
7400 				 */
7401 				sata_decode_device_error(spx, sense);
7402 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7403 					switch (scmd->satacmd_cmd_reg) {
7404 					case SATAC_READ_DMA:
7405 					case SATAC_READ_DMA_EXT:
7406 					case SATAC_READ_DMA_QUEUED:
7407 					case SATAC_READ_DMA_QUEUED_EXT:
7408 					case SATAC_READ_FPDMA_QUEUED:
7409 						/* Unrecovered read error */
7410 						sense->es_add_code =
7411 						    SD_SCSI_ASC_UNREC_READ_ERR;
7412 						break;
7413 					case SATAC_WRITE_DMA:
7414 					case SATAC_WRITE_DMA_EXT:
7415 					case SATAC_WRITE_DMA_QUEUED:
7416 					case SATAC_WRITE_DMA_QUEUED_EXT:
7417 					case SATAC_WRITE_FPDMA_QUEUED:
7418 						/* Write error */
7419 						sense->es_add_code =
7420 						    SD_SCSI_ASC_WRITE_ERR;
7421 						break;
7422 					default:
7423 						/* Internal error */
7424 						SATA_LOG_D((
7425 						    spx->txlt_sata_hba_inst,
7426 						    CE_WARN,
7427 						    "sata_txlt_rw_completion :"
7428 						    "internal error - invalid "
7429 						    "command 0x%2x",
7430 						    scmd->satacmd_cmd_reg));
7431 						break;
7432 					}
7433 				}
7434 				break;
7435 			}
7436 			/* No extended sense key - no info available */
7437 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7438 			break;
7439 
7440 		case SATA_PKT_TIMEOUT:
7441 			scsipkt->pkt_reason = CMD_TIMEOUT;
7442 			scsipkt->pkt_statistics |=
7443 			    STAT_TIMEOUT | STAT_DEV_RESET;
7444 			sense->es_key = KEY_ABORTED_COMMAND;
7445 			break;
7446 
7447 		case SATA_PKT_ABORTED:
7448 			scsipkt->pkt_reason = CMD_ABORTED;
7449 			scsipkt->pkt_statistics |= STAT_ABORTED;
7450 			sense->es_key = KEY_ABORTED_COMMAND;
7451 			break;
7452 
7453 		case SATA_PKT_RESET:
7454 			scsipkt->pkt_reason = CMD_RESET;
7455 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7456 			sense->es_key = KEY_ABORTED_COMMAND;
7457 			break;
7458 
7459 		default:
7460 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7461 			    "sata_txlt_rw_completion: "
7462 			    "invalid packet completion reason"));
7463 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7464 			break;
7465 		}
7466 	}
7467 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7468 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7469 
7470 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7471 		/* scsi callback required */
7472 		scsi_hba_pkt_comp(scsipkt);
7473 }
7474 
7475 
7476 /*
7477  * Translate completion status of non-data commands (i.e. commands returning
7478  * no data).
7479  * pkt completion_reason is checked to determine the completion status.
7480  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7481  *
7482  * Note: this function may be called also for synchronously executed
7483  * commands.
7484  * This function may be used only if scsi_pkt is non-NULL.
7485  */
7486 
7487 static	void
7488 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7489 {
7490 	sata_pkt_txlate_t *spx =
7491 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7492 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7493 
7494 	sata_set_arq_data(sata_pkt);
7495 
7496 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7497 		/* scsi callback required */
7498 		scsi_hba_pkt_comp(scsipkt);
7499 }
7500 
7501 /*
7502  * Completion handler for ATA Pass Through command
7503  */
7504 static void
7505 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7506 {
7507 	sata_pkt_txlate_t *spx =
7508 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7509 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7510 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7511 	struct buf *bp;
7512 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7513 
7514 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7515 		/* Normal completion */
7516 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7517 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7518 		scsipkt->pkt_reason = CMD_CMPLT;
7519 		*scsipkt->pkt_scbp = STATUS_GOOD;
7520 
7521 		/*
7522 		 * If the command has CK_COND set
7523 		 */
7524 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7525 			*scsipkt->pkt_scbp = STATUS_CHECK;
7526 			sata_fill_ata_return_desc(sata_pkt,
7527 			    KEY_RECOVERABLE_ERROR,
7528 			    SD_SCSI_ASC_ATP_INFO_AVAIL, 0);
7529 		}
7530 
7531 		if (spx->txlt_tmp_buf != NULL) {
7532 			/* Temporary buffer was used */
7533 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7534 			if (bp->b_flags & B_READ) {
7535 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7536 				    bp->b_bcount);
7537 			}
7538 		}
7539 	} else {
7540 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7541 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7542 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7543 		*scsipkt->pkt_scbp = STATUS_CHECK;
7544 
7545 		/*
7546 		 * If DF or ERR was set, the HBA should have copied out the
7547 		 * status and error registers to the satacmd structure.
7548 		 */
7549 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7550 			sense_key = KEY_HARDWARE_ERROR;
7551 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7552 			addl_sense_qual = 0;
7553 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7554 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7555 				sense_key = KEY_NOT_READY;
7556 				addl_sense_code =
7557 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7558 				addl_sense_qual = 0;
7559 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7560 				sense_key = KEY_MEDIUM_ERROR;
7561 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7562 				addl_sense_qual = 0;
7563 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7564 				sense_key = KEY_DATA_PROTECT;
7565 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7566 				addl_sense_qual = 0;
7567 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7568 				sense_key = KEY_ILLEGAL_REQUEST;
7569 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7570 				addl_sense_qual = 0;
7571 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7572 				sense_key = KEY_ABORTED_COMMAND;
7573 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7574 				addl_sense_qual = 0;
7575 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7576 				sense_key = KEY_UNIT_ATTENTION;
7577 				addl_sense_code =
7578 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7579 				addl_sense_qual = 0;
7580 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7581 				sense_key = KEY_UNIT_ATTENTION;
7582 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7583 				addl_sense_qual = 0;
7584 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7585 				sense_key = KEY_ABORTED_COMMAND;
7586 				addl_sense_code =
7587 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7588 				addl_sense_qual = 0;
7589 			}
7590 		}
7591 
7592 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7593 		    addl_sense_qual);
7594 	}
7595 
7596 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7597 		/* scsi callback required */
7598 		scsi_hba_pkt_comp(scsipkt);
7599 }
7600 
7601 /*
7602  * Completion handler for unmap translation command
7603  */
7604 static void
7605 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7606 {
7607 	sata_pkt_txlate_t *spx =
7608 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7609 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7610 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7611 	struct buf *bp;
7612 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7613 
7614 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7615 		/* Normal completion */
7616 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7617 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7618 		scsipkt->pkt_reason = CMD_CMPLT;
7619 		*scsipkt->pkt_scbp = STATUS_GOOD;
7620 
7621 		if (spx->txlt_tmp_buf != NULL) {
7622 			/* Temporary buffer was used */
7623 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7624 			if (bp->b_flags & B_READ) {
7625 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7626 				    bp->b_bcount);
7627 			}
7628 		}
7629 	} else {
7630 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7631 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7632 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7633 		*scsipkt->pkt_scbp = STATUS_CHECK;
7634 
7635 		/*
7636 		 * If DF or ERR was set, the HBA should have copied out the
7637 		 * status and error registers to the satacmd structure.
7638 		 */
7639 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7640 			sense_key = KEY_HARDWARE_ERROR;
7641 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7642 			addl_sense_qual = 0;
7643 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7644 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7645 				sense_key = KEY_NOT_READY;
7646 				addl_sense_code =
7647 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7648 				addl_sense_qual = 0;
7649 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7650 				sense_key = KEY_MEDIUM_ERROR;
7651 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7652 				addl_sense_qual = 0;
7653 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7654 				sense_key = KEY_DATA_PROTECT;
7655 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7656 				addl_sense_qual = 0;
7657 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7658 				sense_key = KEY_ILLEGAL_REQUEST;
7659 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7660 				addl_sense_qual = 0;
7661 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7662 				sense_key = KEY_ABORTED_COMMAND;
7663 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7664 				addl_sense_qual = 0;
7665 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7666 				sense_key = KEY_UNIT_ATTENTION;
7667 				addl_sense_code =
7668 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7669 				addl_sense_qual = 0;
7670 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7671 				sense_key = KEY_UNIT_ATTENTION;
7672 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7673 				addl_sense_qual = 0;
7674 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7675 				sense_key = KEY_ABORTED_COMMAND;
7676 				addl_sense_code =
7677 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7678 				addl_sense_qual = 0;
7679 			}
7680 		}
7681 
7682 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7683 		    addl_sense_qual);
7684 	}
7685 
7686 	sata_free_local_buffer(spx);
7687 
7688 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7689 		/* scsi callback required */
7690 		scsi_hba_pkt_comp(scsipkt);
7691 }
7692 
7693 /*
7694  *
7695  */
7696 static void
7697 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7698     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7699 {
7700 	sata_pkt_txlate_t *spx =
7701 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7702 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7703 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7704 	struct sata_apt_sense_data *apt_sd =
7705 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7706 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7707 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7708 	    &(apt_sd->apt_sd_sense);
7709 	int extend = 0;
7710 
7711 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7712 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7713 		extend = 1;
7714 
7715 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7716 
7717 	/* update the residual count */
7718 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7719 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7720 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7721 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7722 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7723 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7724 	    sizeof (struct sata_apt_sense_data);
7725 
7726 	/*
7727 	 * Fill in the Descriptor sense header
7728 	 */
7729 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7730 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7731 	sds->ds_class = CLASS_EXTENDED_SENSE;
7732 	sds->ds_key = sense_key & 0xf;
7733 	sds->ds_add_code = addl_sense_code;
7734 	sds->ds_qual_code = addl_sense_qual;
7735 	sds->ds_addl_sense_length =
7736 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7737 
7738 	/*
7739 	 * Fill in the ATA Return descriptor sense data
7740 	 */
7741 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7742 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7743 	ata_ret_desc->ars_addl_length = 0xc;
7744 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7745 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7746 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7747 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7748 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7749 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7750 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7751 
7752 	if (extend == 1) {
7753 		ata_ret_desc->ars_extend = 1;
7754 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7755 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7756 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7757 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7758 	} else {
7759 		ata_ret_desc->ars_extend = 0;
7760 		ata_ret_desc->ars_sec_count_msb = 0;
7761 		ata_ret_desc->ars_lba_low_msb = 0;
7762 		ata_ret_desc->ars_lba_mid_msb = 0;
7763 		ata_ret_desc->ars_lba_high_msb = 0;
7764 	}
7765 }
7766 
7767 static	void
7768 sata_set_arq_data(sata_pkt_t *sata_pkt)
7769 {
7770 	sata_pkt_txlate_t *spx =
7771 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7772 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7773 	struct scsi_extended_sense *sense;
7774 
7775 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7776 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7777 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7778 		/* Normal completion */
7779 		scsipkt->pkt_reason = CMD_CMPLT;
7780 		*scsipkt->pkt_scbp = STATUS_GOOD;
7781 	} else {
7782 		/* Something went wrong */
7783 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7784 		*scsipkt->pkt_scbp = STATUS_CHECK;
7785 		sense = sata_arq_sense(spx);
7786 		switch (sata_pkt->satapkt_reason) {
7787 		case SATA_PKT_PORT_ERROR:
7788 			/*
7789 			 * We have no device data. Assume no data transfered.
7790 			 */
7791 			sense->es_key = KEY_HARDWARE_ERROR;
7792 			break;
7793 
7794 		case SATA_PKT_DEV_ERROR:
7795 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7796 			    SATA_STATUS_ERR) {
7797 				/*
7798 				 * determine dev error reason from error
7799 				 * reg content
7800 				 */
7801 				sata_decode_device_error(spx, sense);
7802 				break;
7803 			}
7804 			/* No extended sense key - no info available */
7805 			break;
7806 
7807 		case SATA_PKT_TIMEOUT:
7808 			scsipkt->pkt_reason = CMD_TIMEOUT;
7809 			scsipkt->pkt_statistics |=
7810 			    STAT_TIMEOUT | STAT_DEV_RESET;
7811 			/* No extended sense key ? */
7812 			break;
7813 
7814 		case SATA_PKT_ABORTED:
7815 			scsipkt->pkt_reason = CMD_ABORTED;
7816 			scsipkt->pkt_statistics |= STAT_ABORTED;
7817 			/* No extended sense key ? */
7818 			break;
7819 
7820 		case SATA_PKT_RESET:
7821 			/* pkt aborted by an explicit reset from a host */
7822 			scsipkt->pkt_reason = CMD_RESET;
7823 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7824 			break;
7825 
7826 		default:
7827 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7828 			    "sata_txlt_nodata_cmd_completion: "
7829 			    "invalid packet completion reason %d",
7830 			    sata_pkt->satapkt_reason));
7831 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7832 			break;
7833 		}
7834 
7835 	}
7836 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7837 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7838 }
7839 
7840 
7841 /*
7842  * Build Mode sense R/W recovery page
7843  * NOT IMPLEMENTED
7844  */
7845 
7846 static int
7847 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7848 {
7849 #ifndef __lock_lint
7850 	_NOTE(ARGUNUSED(sdinfo))
7851 	_NOTE(ARGUNUSED(pcntrl))
7852 	_NOTE(ARGUNUSED(buf))
7853 #endif
7854 	return (0);
7855 }
7856 
7857 /*
7858  * Build Mode sense caching page  -  scsi-3 implementation.
7859  * Page length distinguishes previous format from scsi-3 format.
7860  * buf must have space for 0x12 bytes.
7861  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7862  *
7863  */
7864 static int
7865 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7866 {
7867 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7868 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7869 
7870 	/*
7871 	 * Most of the fields are set to 0, being not supported and/or disabled
7872 	 */
7873 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7874 
7875 	/* Saved paramters not supported */
7876 	if (pcntrl == 3)
7877 		return (0);
7878 	if (pcntrl == 0 || pcntrl == 2) {
7879 		/*
7880 		 * For now treat current and default parameters as same
7881 		 * That may have to change, if target driver will complain
7882 		 */
7883 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
7884 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7885 
7886 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7887 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
7888 			page->dra = 1;		/* Read Ahead disabled */
7889 			page->rcd = 1;		/* Read Cache disabled */
7890 		}
7891 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
7892 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
7893 			page->wce = 1;		/* Write Cache enabled */
7894 	} else {
7895 		/* Changeable parameters */
7896 		page->mode_page.code = MODEPAGE_CACHING;
7897 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7898 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
7899 			page->dra = 1;
7900 			page->rcd = 1;
7901 		}
7902 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
7903 			page->wce = 1;
7904 	}
7905 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7906 	    sizeof (struct mode_page));
7907 }
7908 
7909 /*
7910  * Build Mode sense exception cntrl page
7911  */
7912 static int
7913 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7914 {
7915 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
7916 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7917 
7918 	/*
7919 	 * Most of the fields are set to 0, being not supported and/or disabled
7920 	 */
7921 	bzero(buf, PAGELENGTH_INFO_EXCPT);
7922 
7923 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
7924 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
7925 
7926 	/* Indicate that this is page is saveable */
7927 	page->mode_page.ps = 1;
7928 
7929 	/*
7930 	 * We will return the same data for default, current and saved page.
7931 	 * The only changeable bit is dexcpt and that bit is required
7932 	 * by the ATA specification to be preserved across power cycles.
7933 	 */
7934 	if (pcntrl != 1) {
7935 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
7936 		page->mrie = MRIE_ONLY_ON_REQUEST;
7937 	}
7938 	else
7939 		page->dexcpt = 1;	/* Only changeable parameter */
7940 
7941 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
7942 }
7943 
7944 
7945 static int
7946 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7947 {
7948 	struct mode_acoustic_management *page =
7949 	    (struct mode_acoustic_management *)buf;
7950 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7951 
7952 	/*
7953 	 * Most of the fields are set to 0, being not supported and/or disabled
7954 	 */
7955 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
7956 
7957 	switch (pcntrl) {
7958 	case P_CNTRL_DEFAULT:
7959 		/*  default paramters not supported */
7960 		return (0);
7961 
7962 	case P_CNTRL_CURRENT:
7963 	case P_CNTRL_SAVED:
7964 		/* Saved and current are supported and are identical */
7965 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
7966 		page->mode_page.length =
7967 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
7968 		page->mode_page.ps = 1;
7969 
7970 		/* Word 83 indicates if feature is supported */
7971 		/* If feature is not supported */
7972 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
7973 			page->acoustic_manag_enable =
7974 			    ACOUSTIC_DISABLED;
7975 		} else {
7976 			page->acoustic_manag_enable =
7977 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
7978 			    != 0);
7979 			/* Word 94 inidicates the value */
7980 #ifdef	_LITTLE_ENDIAN
7981 			page->acoustic_manag_level =
7982 			    (uchar_t)sata_id->ai_acoustic;
7983 			page->vendor_recommended_value =
7984 			    sata_id->ai_acoustic >> 8;
7985 #else
7986 			page->acoustic_manag_level =
7987 			    sata_id->ai_acoustic >> 8;
7988 			page->vendor_recommended_value =
7989 			    (uchar_t)sata_id->ai_acoustic;
7990 #endif
7991 		}
7992 		break;
7993 
7994 	case P_CNTRL_CHANGEABLE:
7995 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
7996 		page->mode_page.length =
7997 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
7998 		page->mode_page.ps = 1;
7999 
8000 		/* Word 83 indicates if the feature is supported */
8001 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8002 			page->acoustic_manag_enable =
8003 			    ACOUSTIC_ENABLED;
8004 			page->acoustic_manag_level = 0xff;
8005 		}
8006 		break;
8007 	}
8008 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8009 	    sizeof (struct mode_page));
8010 }
8011 
8012 
8013 /*
8014  * Build Mode sense power condition page.
8015  */
8016 static int
8017 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8018 {
8019 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8020 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8021 
8022 	/*
8023 	 * Most of the fields are set to 0, being not supported and/or disabled
8024 	 * power condition page length was 0x0a
8025 	 */
8026 	bzero(buf, sizeof (struct mode_info_power_cond));
8027 
8028 	if (pcntrl == P_CNTRL_DEFAULT) {
8029 		/*  default paramters not supported */
8030 		return (0);
8031 	}
8032 
8033 	page->mode_page.code = MODEPAGE_POWER_COND;
8034 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8035 
8036 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
8037 		page->standby = 1;
8038 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8039 		    sizeof (uchar_t) * 4);
8040 	}
8041 
8042 	return (sizeof (struct mode_info_power_cond));
8043 }
8044 
8045 /*
8046  * Process mode select caching page 8 (scsi3 format only).
8047  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8048  * if these features are supported by the device. If these features are not
8049  * supported, the command will be terminated with STATUS_CHECK.
8050  * This function fails only if the SET FEATURE command sent to
8051  * the device fails. The page format is not verified, assuming that the
8052  * target driver operates correctly - if parameters length is too short,
8053  * we just drop the page.
8054  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8055  * setting have to be changed.
8056  * SET FEATURE command is executed synchronously, i.e. we wait here until
8057  * it is completed, regardless of the scsi pkt directives.
8058  *
8059  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8060  * changing DRA will change RCD.
8061  *
8062  * More than one SATA command may be executed to perform operations specified
8063  * by mode select pages. The first error terminates further execution.
8064  * Operations performed successully are not backed-up in such case.
8065  *
8066  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8067  * If operation resulted in changing device setup, dmod flag should be set to
8068  * one (1). If parameters were not changed, dmod flag should be set to 0.
8069  * Upon return, if operation required sending command to the device, the rval
8070  * should be set to the value returned by sata_hba_start. If operation
8071  * did not require device access, rval should be set to TRAN_ACCEPT.
8072  * The pagelen should be set to the length of the page.
8073  *
8074  * This function has to be called with a port mutex held.
8075  *
8076  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8077  */
8078 int
8079 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8080     int parmlen, int *pagelen, int *rval, int *dmod)
8081 {
8082 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8083 	sata_drive_info_t *sdinfo;
8084 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8085 	sata_id_t *sata_id;
8086 	struct scsi_extended_sense *sense;
8087 	int wce, dra;	/* Current settings */
8088 
8089 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8090 	    &spx->txlt_sata_pkt->satapkt_device);
8091 	sata_id = &sdinfo->satadrv_id;
8092 	*dmod = 0;
8093 
8094 	/* Verify parameters length. If too short, drop it */
8095 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8096 	    sizeof (struct mode_page)) > parmlen) {
8097 		*scsipkt->pkt_scbp = STATUS_CHECK;
8098 		sense = sata_arq_sense(spx);
8099 		sense->es_key = KEY_ILLEGAL_REQUEST;
8100 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8101 		*pagelen = parmlen;
8102 		*rval = TRAN_ACCEPT;
8103 		return (SATA_FAILURE);
8104 	}
8105 
8106 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8107 
8108 	/* Current setting of Read Ahead (and Read Cache) */
8109 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8110 		dra = 0;	/* 0 == not disabled */
8111 	else
8112 		dra = 1;
8113 	/* Current setting of Write Cache */
8114 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8115 		wce = 1;
8116 	else
8117 		wce = 0;
8118 
8119 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8120 		/* nothing to do */
8121 		*rval = TRAN_ACCEPT;
8122 		return (SATA_SUCCESS);
8123 	}
8124 
8125 	/*
8126 	 * Need to flip some setting
8127 	 * Set-up Internal SET FEATURES command(s)
8128 	 */
8129 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8130 	scmd->satacmd_addr_type = 0;
8131 	scmd->satacmd_device_reg = 0;
8132 	scmd->satacmd_status_reg = 0;
8133 	scmd->satacmd_error_reg = 0;
8134 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8135 	if (page->dra != dra || page->rcd != dra) {
8136 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8137 			/* Need to flip read ahead setting */
8138 			if (dra == 0)
8139 				/* Disable read ahead / read cache */
8140 				scmd->satacmd_features_reg =
8141 				    SATAC_SF_DISABLE_READ_AHEAD;
8142 			else
8143 				/* Enable read ahead  / read cache */
8144 				scmd->satacmd_features_reg =
8145 				    SATAC_SF_ENABLE_READ_AHEAD;
8146 
8147 			/* Transfer command to HBA */
8148 			if (sata_hba_start(spx, rval) != 0)
8149 				/*
8150 				 * Pkt not accepted for execution.
8151 				 */
8152 				return (SATA_FAILURE);
8153 
8154 			*dmod = 1;
8155 
8156 			/* Now process return */
8157 			if (spx->txlt_sata_pkt->satapkt_reason !=
8158 			    SATA_PKT_COMPLETED) {
8159 				goto failure;	/* Terminate */
8160 			}
8161 		} else {
8162 			*scsipkt->pkt_scbp = STATUS_CHECK;
8163 			sense = sata_arq_sense(spx);
8164 			sense->es_key = KEY_ILLEGAL_REQUEST;
8165 			sense->es_add_code =
8166 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8167 			*pagelen = parmlen;
8168 			*rval = TRAN_ACCEPT;
8169 			return (SATA_FAILURE);
8170 		}
8171 	}
8172 
8173 	/* Note that the packet is not removed, so it could be re-used */
8174 	if (page->wce != wce) {
8175 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8176 			/* Need to flip Write Cache setting */
8177 			if (page->wce == 1)
8178 				/* Enable write cache */
8179 				scmd->satacmd_features_reg =
8180 				    SATAC_SF_ENABLE_WRITE_CACHE;
8181 			else
8182 				/* Disable write cache */
8183 				scmd->satacmd_features_reg =
8184 				    SATAC_SF_DISABLE_WRITE_CACHE;
8185 
8186 			/* Transfer command to HBA */
8187 			if (sata_hba_start(spx, rval) != 0)
8188 				/*
8189 				 * Pkt not accepted for execution.
8190 				 */
8191 				return (SATA_FAILURE);
8192 
8193 			*dmod = 1;
8194 
8195 			/* Now process return */
8196 			if (spx->txlt_sata_pkt->satapkt_reason !=
8197 			    SATA_PKT_COMPLETED) {
8198 				goto failure;
8199 			}
8200 		} else {
8201 			*scsipkt->pkt_scbp = STATUS_CHECK;
8202 			sense = sata_arq_sense(spx);
8203 			sense->es_key = KEY_ILLEGAL_REQUEST;
8204 			sense->es_add_code =
8205 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8206 			*pagelen = parmlen;
8207 			*rval = TRAN_ACCEPT;
8208 			return (SATA_FAILURE);
8209 		}
8210 	}
8211 	return (SATA_SUCCESS);
8212 
8213 failure:
8214 	sata_xlate_errors(spx);
8215 
8216 	return (SATA_FAILURE);
8217 }
8218 
8219 /*
8220  * Process mode select informational exceptions control page 0x1c
8221  *
8222  * The only changeable bit is dexcpt (disable exceptions).
8223  * MRIE (method of reporting informational exceptions) must be
8224  * "only on request".
8225  * This page applies to informational exceptions that report
8226  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8227  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8228  * Informational exception conditions occur as the result of background scan
8229  * errors, background self-test errors, or vendor specific events within a
8230  * logical unit. An informational exception condition may occur asynchronous
8231  * to any commands.
8232  *
8233  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8234  * If operation resulted in changing device setup, dmod flag should be set to
8235  * one (1). If parameters were not changed, dmod flag should be set to 0.
8236  * Upon return, if operation required sending command to the device, the rval
8237  * should be set to the value returned by sata_hba_start. If operation
8238  * did not require device access, rval should be set to TRAN_ACCEPT.
8239  * The pagelen should be set to the length of the page.
8240  *
8241  * This function has to be called with a port mutex held.
8242  *
8243  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8244  *
8245  * Cannot be called in the interrupt context.
8246  */
8247 static	int
8248 sata_mode_select_page_1c(
8249 	sata_pkt_txlate_t *spx,
8250 	struct mode_info_excpt_page *page,
8251 	int parmlen,
8252 	int *pagelen,
8253 	int *rval,
8254 	int *dmod)
8255 {
8256 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8257 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8258 	sata_drive_info_t *sdinfo;
8259 	sata_id_t *sata_id;
8260 	struct scsi_extended_sense *sense;
8261 
8262 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8263 	    &spx->txlt_sata_pkt->satapkt_device);
8264 	sata_id = &sdinfo->satadrv_id;
8265 
8266 	*dmod = 0;
8267 
8268 	/* Verify parameters length. If too short, drop it */
8269 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8270 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8271 		*scsipkt->pkt_scbp = STATUS_CHECK;
8272 		sense = sata_arq_sense(spx);
8273 		sense->es_key = KEY_ILLEGAL_REQUEST;
8274 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8275 		*pagelen = parmlen;
8276 		*rval = TRAN_ACCEPT;
8277 		return (SATA_FAILURE);
8278 	}
8279 
8280 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8281 
8282 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8283 		*scsipkt->pkt_scbp = STATUS_CHECK;
8284 		sense = sata_arq_sense(spx);
8285 		sense->es_key = KEY_ILLEGAL_REQUEST;
8286 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8287 		*pagelen = parmlen;
8288 		*rval = TRAN_ACCEPT;
8289 		return (SATA_FAILURE);
8290 	}
8291 
8292 	/* If already in the state requested, we are done */
8293 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8294 		/* nothing to do */
8295 		*rval = TRAN_ACCEPT;
8296 		return (SATA_SUCCESS);
8297 	}
8298 
8299 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8300 
8301 	/* Build SMART_ENABLE or SMART_DISABLE command */
8302 	scmd->satacmd_addr_type = 0;		/* N/A */
8303 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8304 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8305 	scmd->satacmd_features_reg = page->dexcpt ?
8306 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8307 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8308 	scmd->satacmd_cmd_reg = SATAC_SMART;
8309 
8310 	/* Transfer command to HBA */
8311 	if (sata_hba_start(spx, rval) != 0)
8312 		/*
8313 		 * Pkt not accepted for execution.
8314 		 */
8315 		return (SATA_FAILURE);
8316 
8317 	*dmod = 1;	/* At least may have been modified */
8318 
8319 	/* Now process return */
8320 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8321 		return (SATA_SUCCESS);
8322 
8323 	/* Packet did not complete successfully */
8324 	sata_xlate_errors(spx);
8325 
8326 	return (SATA_FAILURE);
8327 }
8328 
8329 /*
8330  * Process mode select acoustic management control page 0x30
8331  *
8332  *
8333  * This function has to be called with a port mutex held.
8334  *
8335  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8336  *
8337  * Cannot be called in the interrupt context.
8338  */
8339 int
8340 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8341     mode_acoustic_management *page, int parmlen, int *pagelen,
8342     int *rval, int *dmod)
8343 {
8344 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8345 	sata_drive_info_t *sdinfo;
8346 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8347 	sata_id_t *sata_id;
8348 	struct scsi_extended_sense *sense;
8349 
8350 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8351 	    &spx->txlt_sata_pkt->satapkt_device);
8352 	sata_id = &sdinfo->satadrv_id;
8353 	*dmod = 0;
8354 
8355 	/* If parmlen is too short or the feature is not supported, drop it */
8356 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8357 	    sizeof (struct mode_page)) > parmlen) ||
8358 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8359 		*scsipkt->pkt_scbp = STATUS_CHECK;
8360 		sense = sata_arq_sense(spx);
8361 		sense->es_key = KEY_ILLEGAL_REQUEST;
8362 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8363 		*pagelen = parmlen;
8364 		*rval = TRAN_ACCEPT;
8365 		return (SATA_FAILURE);
8366 	}
8367 
8368 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8369 	    sizeof (struct mode_page);
8370 
8371 	/*
8372 	 * We can enable and disable acoustice management and
8373 	 * set the acoustic management level.
8374 	 */
8375 
8376 	/*
8377 	 * Set-up Internal SET FEATURES command(s)
8378 	 */
8379 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8380 	scmd->satacmd_addr_type = 0;
8381 	scmd->satacmd_device_reg = 0;
8382 	scmd->satacmd_status_reg = 0;
8383 	scmd->satacmd_error_reg = 0;
8384 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8385 	if (page->acoustic_manag_enable) {
8386 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8387 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8388 	} else {	/* disabling acoustic management */
8389 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8390 	}
8391 
8392 	/* Transfer command to HBA */
8393 	if (sata_hba_start(spx, rval) != 0)
8394 		/*
8395 		 * Pkt not accepted for execution.
8396 		 */
8397 		return (SATA_FAILURE);
8398 
8399 	/* Now process return */
8400 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8401 		sata_xlate_errors(spx);
8402 		return (SATA_FAILURE);
8403 	}
8404 
8405 	*dmod = 1;
8406 
8407 	return (SATA_SUCCESS);
8408 }
8409 
8410 /*
8411  * Process mode select power condition page 0x1a
8412  *
8413  * This function has to be called with a port mutex held.
8414  *
8415  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8416  *
8417  * Cannot be called in the interrupt context.
8418  */
8419 int
8420 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8421     mode_info_power_cond *page, int parmlen, int *pagelen,
8422     int *rval, int *dmod)
8423 {
8424 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8425 	sata_drive_info_t *sdinfo;
8426 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8427 	sata_id_t *sata_id;
8428 	struct scsi_extended_sense *sense;
8429 	uint8_t ata_count;
8430 	int i, len;
8431 
8432 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8433 	    &spx->txlt_sata_pkt->satapkt_device);
8434 	sata_id = &sdinfo->satadrv_id;
8435 	*dmod = 0;
8436 
8437 	len = sizeof (struct mode_info_power_cond);
8438 	len += sizeof (struct mode_page);
8439 
8440 	/* If parmlen is too short or the feature is not supported, drop it */
8441 	if ((len < parmlen) || (page->idle == 1) ||
8442 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
8443 		*scsipkt->pkt_scbp = STATUS_CHECK;
8444 		sense = sata_arq_sense(spx);
8445 		sense->es_key = KEY_ILLEGAL_REQUEST;
8446 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8447 		*pagelen = parmlen;
8448 		*rval = TRAN_ACCEPT;
8449 		return (SATA_FAILURE);
8450 	}
8451 
8452 	*pagelen = len;
8453 
8454 	/*
8455 	 * Set-up Internal STANDBY command(s)
8456 	 */
8457 	if (page->standby == 0)
8458 		goto out;
8459 
8460 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8461 
8462 	scmd->satacmd_addr_type = 0;
8463 	scmd->satacmd_sec_count_lsb = ata_count;
8464 	scmd->satacmd_lba_low_lsb = 0;
8465 	scmd->satacmd_lba_mid_lsb = 0;
8466 	scmd->satacmd_lba_high_lsb = 0;
8467 	scmd->satacmd_features_reg = 0;
8468 	scmd->satacmd_device_reg = 0;
8469 	scmd->satacmd_status_reg = 0;
8470 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8471 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8472 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8473 
8474 	/* Transfer command to HBA */
8475 	if (sata_hba_start(spx, rval) != 0) {
8476 		return (SATA_FAILURE);
8477 	} else {
8478 		if ((scmd->satacmd_error_reg != 0) ||
8479 		    (spx->txlt_sata_pkt->satapkt_reason !=
8480 		    SATA_PKT_COMPLETED)) {
8481 			sata_xlate_errors(spx);
8482 			return (SATA_FAILURE);
8483 		}
8484 	}
8485 
8486 	for (i = 0; i < 4; i++) {
8487 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8488 	}
8489 out:
8490 	*dmod = 1;
8491 	return (SATA_SUCCESS);
8492 }
8493 
8494 /*
8495  * sata_build_lsense_page0() is used to create the
8496  * SCSI LOG SENSE page 0 (supported log pages)
8497  *
8498  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8499  * (supported log pages, self-test results, informational exceptions
8500  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8501  *
8502  * Takes a sata_drive_info t * and the address of a buffer
8503  * in which to create the page information.
8504  *
8505  * Returns the number of bytes valid in the buffer.
8506  */
8507 static	int
8508 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8509 {
8510 	struct log_parameter *lpp = (struct log_parameter *)buf;
8511 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8512 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8513 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8514 
8515 	lpp->param_code[0] = 0;
8516 	lpp->param_code[1] = 0;
8517 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8518 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8519 
8520 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8521 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8522 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8523 			++num_pages_supported;
8524 		}
8525 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8526 		++num_pages_supported;
8527 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8528 		++num_pages_supported;
8529 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8530 		++num_pages_supported;
8531 	}
8532 
8533 	lpp->param_len = num_pages_supported;
8534 
8535 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8536 	    num_pages_supported);
8537 }
8538 
8539 /*
8540  * sata_build_lsense_page_10() is used to create the
8541  * SCSI LOG SENSE page 0x10 (self-test results)
8542  *
8543  * Takes a sata_drive_info t * and the address of a buffer
8544  * in which to create the page information as well as a sata_hba_inst_t *.
8545  *
8546  * Returns the number of bytes valid in the buffer.
8547  *
8548  * Note: Self test and SMART data is accessible in device log pages.
8549  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8550  * of data can be transferred by a single command), or by the General Purpose
8551  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8552  * - approximately 33MB - can be transferred by a single command.
8553  * The SCT Command response (either error or command) is the same for both
8554  * the SMART and GPL methods of issuing commands.
8555  * This function uses READ LOG EXT command when drive supports LBA48, and
8556  * SMART READ command otherwise.
8557  *
8558  * Since above commands are executed in a synchronous mode, this function
8559  * should not be called in an interrupt context.
8560  */
8561 static	int
8562 sata_build_lsense_page_10(
8563 	sata_drive_info_t *sdinfo,
8564 	uint8_t *buf,
8565 	sata_hba_inst_t *sata_hba_inst)
8566 {
8567 	struct log_parameter *lpp = (struct log_parameter *)buf;
8568 	int rval;
8569 
8570 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8571 		struct smart_ext_selftest_log *ext_selftest_log;
8572 
8573 		ext_selftest_log = kmem_zalloc(
8574 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8575 
8576 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8577 		    ext_selftest_log, 0);
8578 		if (rval == 0) {
8579 			int index, start_index;
8580 			struct smart_ext_selftest_log_entry *entry;
8581 			static const struct smart_ext_selftest_log_entry empty =
8582 			    {0};
8583 			uint16_t block_num;
8584 			int count;
8585 			boolean_t only_one_block = B_FALSE;
8586 
8587 			index = ext_selftest_log->
8588 			    smart_ext_selftest_log_index[0];
8589 			index |= ext_selftest_log->
8590 			    smart_ext_selftest_log_index[1] << 8;
8591 			if (index == 0)
8592 				goto out;
8593 
8594 			--index;	/* Correct for 0 origin */
8595 			start_index = index;	/* remember where we started */
8596 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8597 			if (block_num != 0) {
8598 				rval = sata_ext_smart_selftest_read_log(
8599 				    sata_hba_inst, sdinfo, ext_selftest_log,
8600 				    block_num);
8601 				if (rval != 0)
8602 					goto out;
8603 			}
8604 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8605 			entry =
8606 			    &ext_selftest_log->
8607 			    smart_ext_selftest_log_entries[index];
8608 
8609 			for (count = 1;
8610 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8611 			    ++count) {
8612 				uint8_t status;
8613 				uint8_t code;
8614 				uint8_t sense_key;
8615 				uint8_t add_sense_code;
8616 				uint8_t add_sense_code_qual;
8617 
8618 				/* If this is an unused entry, we are done */
8619 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8620 					/* Broken firmware on some disks */
8621 					if (index + 1 ==
8622 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8623 						--entry;
8624 						--index;
8625 						if (bcmp(entry, &empty,
8626 						    sizeof (empty)) == 0)
8627 							goto out;
8628 					} else
8629 						goto out;
8630 				}
8631 
8632 				if (only_one_block &&
8633 				    start_index == index)
8634 					goto out;
8635 
8636 				lpp->param_code[0] = 0;
8637 				lpp->param_code[1] = count;
8638 				lpp->param_ctrl_flags =
8639 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8640 				lpp->param_len =
8641 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8642 
8643 				status = entry->smart_ext_selftest_log_status;
8644 				status >>= 4;
8645 				switch (status) {
8646 				case 0:
8647 				default:
8648 					sense_key = KEY_NO_SENSE;
8649 					add_sense_code =
8650 					    SD_SCSI_ASC_NO_ADD_SENSE;
8651 					add_sense_code_qual = 0;
8652 					break;
8653 				case 1:
8654 					sense_key = KEY_ABORTED_COMMAND;
8655 					add_sense_code =
8656 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8657 					add_sense_code_qual = SCSI_COMPONENT_81;
8658 					break;
8659 				case 2:
8660 					sense_key = KEY_ABORTED_COMMAND;
8661 					add_sense_code =
8662 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8663 					add_sense_code_qual = SCSI_COMPONENT_82;
8664 					break;
8665 				case 3:
8666 					sense_key = KEY_ABORTED_COMMAND;
8667 					add_sense_code =
8668 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8669 					add_sense_code_qual = SCSI_COMPONENT_83;
8670 					break;
8671 				case 4:
8672 					sense_key = KEY_HARDWARE_ERROR;
8673 					add_sense_code =
8674 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8675 					add_sense_code_qual = SCSI_COMPONENT_84;
8676 					break;
8677 				case 5:
8678 					sense_key = KEY_HARDWARE_ERROR;
8679 					add_sense_code =
8680 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8681 					add_sense_code_qual = SCSI_COMPONENT_85;
8682 					break;
8683 				case 6:
8684 					sense_key = KEY_HARDWARE_ERROR;
8685 					add_sense_code =
8686 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8687 					add_sense_code_qual = SCSI_COMPONENT_86;
8688 					break;
8689 				case 7:
8690 					sense_key = KEY_MEDIUM_ERROR;
8691 					add_sense_code =
8692 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8693 					add_sense_code_qual = SCSI_COMPONENT_87;
8694 					break;
8695 				case 8:
8696 					sense_key = KEY_HARDWARE_ERROR;
8697 					add_sense_code =
8698 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8699 					add_sense_code_qual = SCSI_COMPONENT_88;
8700 					break;
8701 				}
8702 				code = 0;	/* unspecified */
8703 				status |= (code << 4);
8704 				lpp->param_values[0] = status;
8705 				lpp->param_values[1] = 0; /* unspecified */
8706 				lpp->param_values[2] = entry->
8707 				    smart_ext_selftest_log_timestamp[1];
8708 				lpp->param_values[3] = entry->
8709 				    smart_ext_selftest_log_timestamp[0];
8710 				if (status != 0) {
8711 					lpp->param_values[4] = 0;
8712 					lpp->param_values[5] = 0;
8713 					lpp->param_values[6] = entry->
8714 					    smart_ext_selftest_log_failing_lba
8715 					    [5];
8716 					lpp->param_values[7] = entry->
8717 					    smart_ext_selftest_log_failing_lba
8718 					    [4];
8719 					lpp->param_values[8] = entry->
8720 					    smart_ext_selftest_log_failing_lba
8721 					    [3];
8722 					lpp->param_values[9] = entry->
8723 					    smart_ext_selftest_log_failing_lba
8724 					    [2];
8725 					lpp->param_values[10] = entry->
8726 					    smart_ext_selftest_log_failing_lba
8727 					    [1];
8728 					lpp->param_values[11] = entry->
8729 					    smart_ext_selftest_log_failing_lba
8730 					    [0];
8731 				} else {	/* No bad block address */
8732 					lpp->param_values[4] = 0xff;
8733 					lpp->param_values[5] = 0xff;
8734 					lpp->param_values[6] = 0xff;
8735 					lpp->param_values[7] = 0xff;
8736 					lpp->param_values[8] = 0xff;
8737 					lpp->param_values[9] = 0xff;
8738 					lpp->param_values[10] = 0xff;
8739 					lpp->param_values[11] = 0xff;
8740 				}
8741 
8742 				lpp->param_values[12] = sense_key;
8743 				lpp->param_values[13] = add_sense_code;
8744 				lpp->param_values[14] = add_sense_code_qual;
8745 				lpp->param_values[15] = 0; /* undefined */
8746 
8747 				lpp = (struct log_parameter *)
8748 				    (((uint8_t *)lpp) +
8749 				    SCSI_LOG_PARAM_HDR_LEN +
8750 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8751 
8752 				--index;	/* Back up to previous entry */
8753 				if (index < 0) {
8754 					if (block_num > 0) {
8755 						--block_num;
8756 					} else {
8757 						struct read_log_ext_directory
8758 						    logdir;
8759 
8760 						rval =
8761 						    sata_read_log_ext_directory(
8762 						    sata_hba_inst, sdinfo,
8763 						    &logdir);
8764 						if (rval == -1)
8765 							goto out;
8766 						if ((logdir.read_log_ext_vers
8767 						    [0] == 0) &&
8768 						    (logdir.read_log_ext_vers
8769 						    [1] == 0))
8770 							goto out;
8771 						block_num =
8772 						    logdir.read_log_ext_nblks
8773 						    [EXT_SMART_SELFTEST_LOG_PAGE
8774 						    - 1][0];
8775 						block_num |= logdir.
8776 						    read_log_ext_nblks
8777 						    [EXT_SMART_SELFTEST_LOG_PAGE
8778 						    - 1][1] << 8;
8779 						--block_num;
8780 						only_one_block =
8781 						    (block_num == 0);
8782 					}
8783 					rval = sata_ext_smart_selftest_read_log(
8784 					    sata_hba_inst, sdinfo,
8785 					    ext_selftest_log, block_num);
8786 					if (rval != 0)
8787 						goto out;
8788 
8789 					index =
8790 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8791 					    1;
8792 				}
8793 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8794 				entry = &ext_selftest_log->
8795 				    smart_ext_selftest_log_entries[index];
8796 			}
8797 		}
8798 out:
8799 		kmem_free(ext_selftest_log,
8800 		    sizeof (struct smart_ext_selftest_log));
8801 	} else {
8802 		struct smart_selftest_log *selftest_log;
8803 
8804 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8805 		    KM_SLEEP);
8806 
8807 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8808 		    selftest_log);
8809 
8810 		if (rval == 0) {
8811 			int index;
8812 			int count;
8813 			struct smart_selftest_log_entry *entry;
8814 			static const struct smart_selftest_log_entry empty =
8815 			    { 0 };
8816 
8817 			index = selftest_log->smart_selftest_log_index;
8818 			if (index == 0)
8819 				goto done;
8820 			--index;	/* Correct for 0 origin */
8821 			entry = &selftest_log->
8822 			    smart_selftest_log_entries[index];
8823 			for (count = 1;
8824 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8825 			    ++count) {
8826 				uint8_t status;
8827 				uint8_t code;
8828 				uint8_t sense_key;
8829 				uint8_t add_sense_code;
8830 				uint8_t add_sense_code_qual;
8831 
8832 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8833 					goto done;
8834 
8835 				lpp->param_code[0] = 0;
8836 				lpp->param_code[1] = count;
8837 				lpp->param_ctrl_flags =
8838 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8839 				lpp->param_len =
8840 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8841 
8842 				status = entry->smart_selftest_log_status;
8843 				status >>= 4;
8844 				switch (status) {
8845 				case 0:
8846 				default:
8847 					sense_key = KEY_NO_SENSE;
8848 					add_sense_code =
8849 					    SD_SCSI_ASC_NO_ADD_SENSE;
8850 					break;
8851 				case 1:
8852 					sense_key = KEY_ABORTED_COMMAND;
8853 					add_sense_code =
8854 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8855 					add_sense_code_qual = SCSI_COMPONENT_81;
8856 					break;
8857 				case 2:
8858 					sense_key = KEY_ABORTED_COMMAND;
8859 					add_sense_code =
8860 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8861 					add_sense_code_qual = SCSI_COMPONENT_82;
8862 					break;
8863 				case 3:
8864 					sense_key = KEY_ABORTED_COMMAND;
8865 					add_sense_code =
8866 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8867 					add_sense_code_qual = SCSI_COMPONENT_83;
8868 					break;
8869 				case 4:
8870 					sense_key = KEY_HARDWARE_ERROR;
8871 					add_sense_code =
8872 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8873 					add_sense_code_qual = SCSI_COMPONENT_84;
8874 					break;
8875 				case 5:
8876 					sense_key = KEY_HARDWARE_ERROR;
8877 					add_sense_code =
8878 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8879 					add_sense_code_qual = SCSI_COMPONENT_85;
8880 					break;
8881 				case 6:
8882 					sense_key = KEY_HARDWARE_ERROR;
8883 					add_sense_code =
8884 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8885 					add_sense_code_qual = SCSI_COMPONENT_86;
8886 					break;
8887 				case 7:
8888 					sense_key = KEY_MEDIUM_ERROR;
8889 					add_sense_code =
8890 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8891 					add_sense_code_qual = SCSI_COMPONENT_87;
8892 					break;
8893 				case 8:
8894 					sense_key = KEY_HARDWARE_ERROR;
8895 					add_sense_code =
8896 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8897 					add_sense_code_qual = SCSI_COMPONENT_88;
8898 					break;
8899 				}
8900 				code = 0;	/* unspecified */
8901 				status |= (code << 4);
8902 				lpp->param_values[0] = status;
8903 				lpp->param_values[1] = 0; /* unspecified */
8904 				lpp->param_values[2] = entry->
8905 				    smart_selftest_log_timestamp[1];
8906 				lpp->param_values[3] = entry->
8907 				    smart_selftest_log_timestamp[0];
8908 				if (status != 0) {
8909 					lpp->param_values[4] = 0;
8910 					lpp->param_values[5] = 0;
8911 					lpp->param_values[6] = 0;
8912 					lpp->param_values[7] = 0;
8913 					lpp->param_values[8] = entry->
8914 					    smart_selftest_log_failing_lba[3];
8915 					lpp->param_values[9] = entry->
8916 					    smart_selftest_log_failing_lba[2];
8917 					lpp->param_values[10] = entry->
8918 					    smart_selftest_log_failing_lba[1];
8919 					lpp->param_values[11] = entry->
8920 					    smart_selftest_log_failing_lba[0];
8921 				} else {	/* No block address */
8922 					lpp->param_values[4] = 0xff;
8923 					lpp->param_values[5] = 0xff;
8924 					lpp->param_values[6] = 0xff;
8925 					lpp->param_values[7] = 0xff;
8926 					lpp->param_values[8] = 0xff;
8927 					lpp->param_values[9] = 0xff;
8928 					lpp->param_values[10] = 0xff;
8929 					lpp->param_values[11] = 0xff;
8930 				}
8931 				lpp->param_values[12] = sense_key;
8932 				lpp->param_values[13] = add_sense_code;
8933 				lpp->param_values[14] = add_sense_code_qual;
8934 				lpp->param_values[15] = 0; /* undefined */
8935 
8936 				lpp = (struct log_parameter *)
8937 				    (((uint8_t *)lpp) +
8938 				    SCSI_LOG_PARAM_HDR_LEN +
8939 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8940 				--index;	/* back up to previous entry */
8941 				if (index < 0) {
8942 					index =
8943 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
8944 				}
8945 				entry = &selftest_log->
8946 				    smart_selftest_log_entries[index];
8947 			}
8948 		}
8949 done:
8950 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
8951 	}
8952 
8953 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
8954 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
8955 }
8956 
8957 /*
8958  * sata_build_lsense_page_2f() is used to create the
8959  * SCSI LOG SENSE page 0x2f (informational exceptions)
8960  *
8961  * Takes a sata_drive_info t * and the address of a buffer
8962  * in which to create the page information as well as a sata_hba_inst_t *.
8963  *
8964  * Returns the number of bytes valid in the buffer.
8965  *
8966  * Because it invokes function(s) that send synchronously executed command
8967  * to the HBA, it cannot be called in the interrupt context.
8968  */
8969 static	int
8970 sata_build_lsense_page_2f(
8971 	sata_drive_info_t *sdinfo,
8972 	uint8_t *buf,
8973 	sata_hba_inst_t *sata_hba_inst)
8974 {
8975 	struct log_parameter *lpp = (struct log_parameter *)buf;
8976 	int rval;
8977 	uint8_t *smart_data;
8978 	uint8_t temp;
8979 	sata_id_t *sata_id;
8980 #define	SMART_NO_TEMP	0xff
8981 
8982 	lpp->param_code[0] = 0;
8983 	lpp->param_code[1] = 0;
8984 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8985 
8986 	/* Now get the SMART status w.r.t. threshold exceeded */
8987 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
8988 	switch (rval) {
8989 	case 1:
8990 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
8991 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
8992 		break;
8993 	case 0:
8994 	case -1:	/* failed to get data */
8995 		lpp->param_values[0] = 0;	/* No failure predicted */
8996 		lpp->param_values[1] = 0;
8997 		break;
8998 #if defined(SATA_DEBUG)
8999 	default:
9000 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9001 		/* NOTREACHED */
9002 #endif
9003 	}
9004 
9005 	sata_id = &sdinfo->satadrv_id;
9006 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9007 		temp = SMART_NO_TEMP;
9008 	else {
9009 		/* Now get the temperature */
9010 		smart_data = kmem_zalloc(512, KM_SLEEP);
9011 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9012 		    SCT_STATUS_LOG_PAGE, 1);
9013 		if (rval == -1)
9014 			temp = SMART_NO_TEMP;
9015 		else {
9016 			temp = smart_data[200];
9017 			if (temp & 0x80) {
9018 				if (temp & 0x7f)
9019 					temp = 0;
9020 				else
9021 					temp = SMART_NO_TEMP;
9022 			}
9023 		}
9024 		kmem_free(smart_data, 512);
9025 	}
9026 
9027 	lpp->param_values[2] = temp;	/* most recent temperature */
9028 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9029 
9030 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9031 
9032 
9033 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9034 }
9035 
9036 /*
9037  * sata_build_lsense_page_30() is used to create the
9038  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9039  *
9040  * Takes a sata_drive_info t * and the address of a buffer
9041  * in which to create the page information as well as a sata_hba_inst_t *.
9042  *
9043  * Returns the number of bytes valid in the buffer.
9044  */
9045 static int
9046 sata_build_lsense_page_30(
9047 	sata_drive_info_t *sdinfo,
9048 	uint8_t *buf,
9049 	sata_hba_inst_t *sata_hba_inst)
9050 {
9051 	struct smart_data *smart_data = (struct smart_data *)buf;
9052 	int rval;
9053 
9054 	/* Now do the SMART READ DATA */
9055 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9056 	if (rval == -1)
9057 		return (0);
9058 
9059 	return (sizeof (struct smart_data));
9060 }
9061 
9062 /*
9063  * sata_build_lsense_page_0e() is used to create the
9064  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9065  *
9066  * Date of Manufacture (0x0001)
9067  *	YEAR = "0000"
9068  *	WEEK = "00"
9069  * Accounting Date (0x0002)
9070  *	6 ASCII space character(20h)
9071  * Specified cycle count over device lifetime
9072  *	VALUE - THRESH - the delta between max and min;
9073  * Accumulated start-stop cycles
9074  *	VALUE - WORST - the accumulated cycles;
9075  *
9076  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9077  *
9078  * Takes a sata_drive_info t * and the address of a buffer
9079  * in which to create the page information as well as a sata_hba_inst_t *.
9080  *
9081  * Returns the number of bytes valid in the buffer.
9082  */
9083 static	int
9084 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9085 	sata_pkt_txlate_t *spx)
9086 {
9087 	struct start_stop_cycle_counter_log *log_page;
9088 	int i, rval, index;
9089 	uint8_t smart_data[512], id, value, worst, thresh;
9090 	uint32_t max_count, cycles;
9091 
9092 	/* Now do the SMART READ DATA */
9093 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9094 	    (struct smart_data *)smart_data);
9095 	if (rval == -1)
9096 		return (0);
9097 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9098 		index = (i * 12) + 2;
9099 		id = smart_data[index];
9100 		if (id != SMART_START_STOP_COUNT_ID)
9101 			continue;
9102 		else {
9103 			thresh = smart_data[index + 2];
9104 			value = smart_data[index + 3];
9105 			worst = smart_data[index + 4];
9106 			break;
9107 		}
9108 	}
9109 	if (id != SMART_START_STOP_COUNT_ID)
9110 		return (0);
9111 	max_count = value - thresh;
9112 	cycles = value - worst;
9113 
9114 	log_page = (struct start_stop_cycle_counter_log *)buf;
9115 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9116 	log_page->code = 0x0e;
9117 	log_page->page_len_low = 0x24;
9118 
9119 	log_page->manufactor_date_low = 0x1;
9120 	log_page->param_1.fmt_link = 0x1; /* 01b */
9121 	log_page->param_len_1 = 0x06;
9122 	for (i = 0; i < 4; i++) {
9123 		log_page->year_manu[i] = 0x30;
9124 		if (i < 2)
9125 			log_page->week_manu[i] = 0x30;
9126 	}
9127 
9128 	log_page->account_date_low = 0x02;
9129 	log_page->param_2.fmt_link = 0x01; /* 01b */
9130 	log_page->param_len_2 = 0x06;
9131 	for (i = 0; i < 4; i++) {
9132 		log_page->year_account[i] = 0x20;
9133 		if (i < 2)
9134 			log_page->week_account[i] = 0x20;
9135 	}
9136 
9137 	log_page->lifetime_code_low = 0x03;
9138 	log_page->param_3.fmt_link = 0x03; /* 11b */
9139 	log_page->param_len_3 = 0x04;
9140 	/* VALUE - THRESH - the delta between max and min */
9141 	log_page->cycle_code_low = 0x04;
9142 	log_page->param_4.fmt_link = 0x03; /* 11b */
9143 	log_page->param_len_4 = 0x04;
9144 	/* WORST - THRESH - the distance from 'now' to min */
9145 
9146 	for (i = 0; i < 4; i++) {
9147 		log_page->cycle_lifetime[i] =
9148 		    (max_count >> (8 * (3 - i))) & 0xff;
9149 		log_page->cycle_accumulated[i] =
9150 		    (cycles >> (8 * (3 - i))) & 0xff;
9151 	}
9152 
9153 	return (sizeof (struct start_stop_cycle_counter_log));
9154 }
9155 
9156 /*
9157  * This function was used for build a ATA read verify sector command
9158  */
9159 static void
9160 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9161 {
9162 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9163 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9164 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9165 
9166 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9167 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9168 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9169 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9170 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9171 	scmd->satacmd_features_reg = 0;
9172 	scmd->satacmd_status_reg = 0;
9173 	scmd->satacmd_error_reg = 0;
9174 }
9175 
9176 /*
9177  * This function was used for building an ATA
9178  * command, and only command register need to
9179  * be defined, other register will be zero or na.
9180  */
9181 static void
9182 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9183 {
9184 	scmd->satacmd_addr_type = 0;
9185 	scmd->satacmd_cmd_reg = cmd;
9186 	scmd->satacmd_device_reg = 0;
9187 	scmd->satacmd_sec_count_lsb = 0;
9188 	scmd->satacmd_lba_low_lsb = 0;
9189 	scmd->satacmd_lba_mid_lsb = 0;
9190 	scmd->satacmd_lba_high_lsb = 0;
9191 	scmd->satacmd_features_reg = 0;
9192 	scmd->satacmd_status_reg = 0;
9193 	scmd->satacmd_error_reg = 0;
9194 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9195 }
9196 
9197 /*
9198  * This function was used for changing the standby
9199  * timer format from SCSI to ATA.
9200  */
9201 static uint8_t
9202 sata_get_standby_timer(uint8_t *timer)
9203 {
9204 	uint32_t i = 0, count = 0;
9205 	uint8_t ata_count;
9206 
9207 	for (i = 0; i < 4; i++) {
9208 		count = count << 8 | timer[i];
9209 	}
9210 
9211 	if (count == 0)
9212 		return (0);
9213 
9214 	if (count >= 1 && count <= 12000)
9215 		ata_count = (count -1) / 50 + 1;
9216 	else if (count > 12000 && count <= 12600)
9217 		ata_count = 0xfc;
9218 	else if (count > 12601 && count <= 12750)
9219 		ata_count = 0xff;
9220 	else if (count > 12750 && count <= 17999)
9221 		ata_count = 0xf1;
9222 	else if (count > 18000 && count <= 198000)
9223 		ata_count = count / 18000 + 240;
9224 	else
9225 		ata_count = 0xfd;
9226 	return (ata_count);
9227 }
9228 
9229 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9230 
9231 /*
9232  * Start command for ATAPI device.
9233  * This function processes scsi_pkt requests.
9234  * Now CD/DVD, tape and ATAPI disk devices are supported.
9235  * Most commands are packet without any translation into Packet Command.
9236  * Some may be trapped and executed as SATA commands (not clear which one).
9237  *
9238  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9239  * execution).
9240  * Returns other TRAN_XXXX codes if command is not accepted or completed
9241  * (see return values for sata_hba_start()).
9242  *
9243  * Note:
9244  * Inquiry cdb format differs between transport version 2 and 3.
9245  * However, the transport version 3 devices that were checked did not adhere
9246  * to the specification (ignored MSB of the allocation length). Therefore,
9247  * the transport version is not checked, but Inquiry allocation length is
9248  * truncated to 255 bytes if the original allocation length set-up by the
9249  * target driver is greater than 255 bytes.
9250  */
9251 static int
9252 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9253 {
9254 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9255 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9256 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9257 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9258 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9259 	    &spx->txlt_sata_pkt->satapkt_device);
9260 	int cport = SATA_TXLT_CPORT(spx);
9261 	int cdblen;
9262 	int rval, reason;
9263 	int synch;
9264 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9265 
9266 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
9267 
9268 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9269 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9270 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9271 		return (rval);
9272 	}
9273 
9274 	/*
9275 	 * ATAPI device executes some ATA commands in addition to those
9276 	 * commands sent via PACKET command. These ATA commands may be
9277 	 * executed by the regular SATA translation functions. None needs
9278 	 * to be captured now.
9279 	 *
9280 	 * Commands sent via PACKET command include:
9281 	 *	MMC command set for ATAPI CD/DVD device
9282 	 *	SSC command set for ATAPI TAPE device
9283 	 *	SBC command set for ATAPI disk device
9284 	 *
9285 	 */
9286 
9287 	/* Check the size of cdb */
9288 
9289 	switch (GETGROUP(cdbp)) {
9290 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9291 		/*
9292 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9293 		 * therefore require special handling.  Return failure, for now.
9294 		 */
9295 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9296 		return (TRAN_BADPKT);
9297 
9298 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9299 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9300 		/* obtain length from the scsi_pkt */
9301 		cdblen = scsipkt->pkt_cdblen;
9302 		break;
9303 
9304 	default:
9305 		/* CDB's length is statically known, per SPC-4 */
9306 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9307 		break;
9308 	}
9309 
9310 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9311 		sata_log(NULL, CE_WARN,
9312 		    "sata: invalid ATAPI cdb length %d",
9313 		    cdblen);
9314 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9315 		return (TRAN_BADPKT);
9316 	}
9317 
9318 	SATAATAPITRACE(spx, cdblen);
9319 
9320 	/*
9321 	 * For non-read/write commands we need to
9322 	 * map buffer
9323 	 */
9324 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9325 	case SCMD_READ:
9326 	case SCMD_READ_G1:
9327 	case SCMD_READ_G5:
9328 	case SCMD_READ_G4:
9329 	case SCMD_WRITE:
9330 	case SCMD_WRITE_G1:
9331 	case SCMD_WRITE_G5:
9332 	case SCMD_WRITE_G4:
9333 		break;
9334 	default:
9335 		if (bp != NULL) {
9336 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9337 				bp_mapin(bp);
9338 		}
9339 		break;
9340 	}
9341 	/*
9342 	 * scmd->satacmd_flags.sata_data_direction default -
9343 	 * SATA_DIR_NODATA_XFER - is set by
9344 	 * sata_txlt_generic_pkt_info().
9345 	 */
9346 	if (scmd->satacmd_bp) {
9347 		if (scmd->satacmd_bp->b_flags & B_READ) {
9348 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9349 		} else {
9350 			scmd->satacmd_flags.sata_data_direction =
9351 			    SATA_DIR_WRITE;
9352 		}
9353 	}
9354 
9355 	/*
9356 	 * Set up ATAPI packet command.
9357 	 */
9358 
9359 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9360 
9361 	/* Copy cdb into sata_cmd */
9362 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9363 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9364 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9365 
9366 	/* See note in the command header */
9367 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9368 		if (scmd->satacmd_acdb[3] != 0)
9369 			scmd->satacmd_acdb[4] = 255;
9370 	}
9371 
9372 #ifdef SATA_DEBUG
9373 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9374 		uint8_t *p = scmd->satacmd_acdb;
9375 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9376 
9377 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9378 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9379 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9380 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9381 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9382 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9383 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9384 	}
9385 #endif
9386 
9387 	/*
9388 	 * Preset request sense data to NO SENSE.
9389 	 * If there is no way to get error information via Request Sense,
9390 	 * the packet request sense data would not have to be modified by HBA,
9391 	 * but it could be returned as is.
9392 	 */
9393 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9394 	sata_fixed_sense_data_preset(
9395 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9396 
9397 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9398 		/* Need callback function */
9399 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9400 		synch = FALSE;
9401 	} else
9402 		synch = TRUE;
9403 
9404 	/* Transfer command to HBA */
9405 	if (sata_hba_start(spx, &rval) != 0) {
9406 		/* Pkt not accepted for execution */
9407 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
9408 		return (rval);
9409 	}
9410 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
9411 	/*
9412 	 * If execution is non-synchronous,
9413 	 * a callback function will handle potential errors, translate
9414 	 * the response and will do a callback to a target driver.
9415 	 * If it was synchronous, use the same framework callback to check
9416 	 * an execution status.
9417 	 */
9418 	if (synch) {
9419 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9420 		    "synchronous execution status %x\n",
9421 		    spx->txlt_sata_pkt->satapkt_reason);
9422 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9423 	}
9424 	return (TRAN_ACCEPT);
9425 }
9426 
9427 
9428 /*
9429  * ATAPI Packet command completion.
9430  *
9431  * Failure of the command passed via Packet command are considered device
9432  * error. SATA HBA driver would have to retrieve error data (via Request
9433  * Sense command delivered via error retrieval sata packet) and copy it
9434  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9435  */
9436 static void
9437 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9438 {
9439 	sata_pkt_txlate_t *spx =
9440 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9441 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9442 	struct scsi_extended_sense *sense;
9443 	struct buf *bp;
9444 	int rval;
9445 
9446 #ifdef SATA_DEBUG
9447 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9448 #endif
9449 
9450 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9451 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9452 
9453 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9454 		/* Normal completion */
9455 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9456 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9457 		scsipkt->pkt_reason = CMD_CMPLT;
9458 		*scsipkt->pkt_scbp = STATUS_GOOD;
9459 		if (spx->txlt_tmp_buf != NULL) {
9460 			/* Temporary buffer was used */
9461 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9462 			if (bp->b_flags & B_READ) {
9463 				rval = ddi_dma_sync(
9464 				    spx->txlt_buf_dma_handle, 0, 0,
9465 				    DDI_DMA_SYNC_FORCPU);
9466 				ASSERT(rval == DDI_SUCCESS);
9467 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9468 				    bp->b_bcount);
9469 			}
9470 		}
9471 	} else {
9472 		/*
9473 		 * Something went wrong - analyze return
9474 		 */
9475 		*scsipkt->pkt_scbp = STATUS_CHECK;
9476 		sense = sata_arq_sense(spx);
9477 
9478 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9479 			/*
9480 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9481 			 * Under this condition ERR bit is set for ATA command,
9482 			 * and CHK bit set for ATAPI command.
9483 			 *
9484 			 * Please check st_intr & sdintr about how pkt_reason
9485 			 * is used.
9486 			 */
9487 			scsipkt->pkt_reason = CMD_CMPLT;
9488 
9489 			/*
9490 			 * We may not have ARQ data if there was a double
9491 			 * error. But sense data in sata packet was pre-set
9492 			 * with NO SENSE so it is valid even if HBA could
9493 			 * not retrieve a real sense data.
9494 			 * Just copy this sense data into scsi pkt sense area.
9495 			 */
9496 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9497 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9498 #ifdef SATA_DEBUG
9499 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9500 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9501 				    "sata_txlt_atapi_completion: %02x\n"
9502 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9503 				    "          %02x %02x %02x %02x %02x %02x "
9504 				    "          %02x %02x %02x %02x %02x %02x\n",
9505 				    scsipkt->pkt_reason,
9506 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9507 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9508 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9509 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9510 				    rqsp[16], rqsp[17]);
9511 			}
9512 #endif
9513 		} else {
9514 			switch (sata_pkt->satapkt_reason) {
9515 			case SATA_PKT_PORT_ERROR:
9516 				/*
9517 				 * We have no device data.
9518 				 */
9519 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9520 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9521 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9522 				    STATE_GOT_STATUS);
9523 				sense->es_key = KEY_HARDWARE_ERROR;
9524 				break;
9525 
9526 			case SATA_PKT_TIMEOUT:
9527 				scsipkt->pkt_reason = CMD_TIMEOUT;
9528 				scsipkt->pkt_statistics |=
9529 				    STAT_TIMEOUT | STAT_DEV_RESET;
9530 				/*
9531 				 * Need to check if HARDWARE_ERROR/
9532 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9533 				 * appropriate.
9534 				 */
9535 				break;
9536 
9537 			case SATA_PKT_ABORTED:
9538 				scsipkt->pkt_reason = CMD_ABORTED;
9539 				scsipkt->pkt_statistics |= STAT_ABORTED;
9540 				/* Should we set key COMMAND_ABPRTED? */
9541 				break;
9542 
9543 			case SATA_PKT_RESET:
9544 				scsipkt->pkt_reason = CMD_RESET;
9545 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9546 				/*
9547 				 * May be we should set Unit Attention /
9548 				 * Reset. Perhaps the same should be
9549 				 * returned for disks....
9550 				 */
9551 				sense->es_key = KEY_UNIT_ATTENTION;
9552 				sense->es_add_code = SD_SCSI_ASC_RESET;
9553 				break;
9554 
9555 			default:
9556 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9557 				    "sata_txlt_atapi_completion: "
9558 				    "invalid packet completion reason"));
9559 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9560 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9561 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9562 				    STATE_GOT_STATUS);
9563 				break;
9564 			}
9565 		}
9566 	}
9567 
9568 	SATAATAPITRACE(spx, 0);
9569 
9570 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9571 	    scsipkt->pkt_comp != NULL) {
9572 		/* scsi callback required */
9573 		(*scsipkt->pkt_comp)(scsipkt);
9574 	}
9575 }
9576 
9577 /*
9578  * Set up error retrieval sata command for ATAPI Packet Command error data
9579  * recovery.
9580  *
9581  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9582  * returns SATA_FAILURE otherwise.
9583  */
9584 
9585 static int
9586 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9587 {
9588 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9589 	sata_cmd_t *scmd;
9590 	struct buf *bp;
9591 
9592 	/*
9593 	 * Allocate dma-able buffer error data.
9594 	 * Buffer allocation will take care of buffer alignment and other DMA
9595 	 * attributes.
9596 	 */
9597 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9598 	if (bp == NULL) {
9599 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9600 		    "sata_get_err_retrieval_pkt: "
9601 		    "cannot allocate buffer for error data", NULL);
9602 		return (SATA_FAILURE);
9603 	}
9604 	bp_mapin(bp); /* make data buffer accessible */
9605 
9606 	/* Operation modes are up to the caller */
9607 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9608 
9609 	/* Synchronous mode, no callback - may be changed by the caller */
9610 	spkt->satapkt_comp = NULL;
9611 	spkt->satapkt_time = sata_default_pkt_time;
9612 
9613 	scmd = &spkt->satapkt_cmd;
9614 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9615 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9616 
9617 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9618 
9619 	/*
9620 	 * Set-up acdb. Request Sense CDB (packet command content) is
9621 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9622 	 * it is transfered into packet FIS).
9623 	 */
9624 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9625 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9626 	/* Following zeroing of pad bytes may not be necessary */
9627 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9628 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9629 
9630 	/*
9631 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9632 	 * before accessing it. Handle is in usual place in translate struct.
9633 	 */
9634 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9635 
9636 	/*
9637 	 * Preset request sense data to NO SENSE.
9638 	 * Here it is redundant, only for a symetry with scsi-originated
9639 	 * packets. It should not be used for anything but debugging.
9640 	 */
9641 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9642 	sata_fixed_sense_data_preset(
9643 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9644 
9645 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9646 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9647 
9648 	return (SATA_SUCCESS);
9649 }
9650 
9651 /*
9652  * Set-up ATAPI packet command.
9653  * Data transfer direction has to be set-up in sata_cmd structure prior to
9654  * calling this function.
9655  *
9656  * Returns void
9657  */
9658 
9659 static void
9660 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9661 {
9662 	scmd->satacmd_addr_type = 0;		/* N/A */
9663 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9664 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9665 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9666 	scmd->satacmd_lba_high_lsb =
9667 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9668 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9669 
9670 	/*
9671 	 * We want all data to be transfered via DMA.
9672 	 * But specify it only if drive supports DMA and DMA mode is
9673 	 * selected - some drives are sensitive about it.
9674 	 * Hopefully it wil work for all drives....
9675 	 */
9676 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9677 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9678 
9679 	/*
9680 	 * Features register requires special care for devices that use
9681 	 * Serial ATA bridge - they need an explicit specification of
9682 	 * the data transfer direction for Packet DMA commands.
9683 	 * Setting this bit is harmless if DMA is not used.
9684 	 *
9685 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9686 	 * spec they follow.
9687 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9688 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9689 	 * ATA/ATAPI-7 support is explicitly indicated.
9690 	 */
9691 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9692 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9693 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9694 		/*
9695 		 * Specification of major version is valid and version 7
9696 		 * is supported. It does automatically imply that all
9697 		 * spec features are supported. For now, we assume that
9698 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9699 		 */
9700 		if ((sdinfo->satadrv_id.ai_dirdma &
9701 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9702 			if (scmd->satacmd_flags.sata_data_direction ==
9703 			    SATA_DIR_READ)
9704 			scmd->satacmd_features_reg |=
9705 			    SATA_ATAPI_F_DATA_DIR_READ;
9706 		}
9707 	}
9708 }
9709 
9710 
9711 #ifdef SATA_DEBUG
9712 
9713 /* Display 18 bytes of Inquiry data */
9714 static void
9715 sata_show_inqry_data(uint8_t *buf)
9716 {
9717 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9718 	uint8_t *p;
9719 
9720 	cmn_err(CE_NOTE, "Inquiry data:");
9721 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9722 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9723 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9724 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9725 	    SATA_ATAPI_TRANS_VERSION(inq));
9726 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9727 	    inq->inq_rdf, inq->inq_aenc);
9728 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9729 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9730 	p = (uint8_t *)inq->inq_vid;
9731 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9732 	    "%02x %02x %02x %02x",
9733 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9734 	p = (uint8_t *)inq->inq_vid;
9735 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9736 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9737 
9738 	p = (uint8_t *)inq->inq_pid;
9739 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9740 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9741 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9742 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9743 	p = (uint8_t *)inq->inq_pid;
9744 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9745 	    "%c %c %c %c %c %c %c %c",
9746 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9747 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9748 
9749 	p = (uint8_t *)inq->inq_revision;
9750 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9751 	    p[0], p[1], p[2], p[3]);
9752 	p = (uint8_t *)inq->inq_revision;
9753 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9754 	    p[0], p[1], p[2], p[3]);
9755 
9756 }
9757 
9758 
9759 static void
9760 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9761 {
9762 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9763 
9764 	if (scsi_pkt == NULL)
9765 		return;
9766 	if (count != 0) {
9767 		/* saving cdb */
9768 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9769 		    SATA_ATAPI_MAX_CDB_LEN);
9770 		bcopy(scsi_pkt->pkt_cdbp,
9771 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9772 	} else {
9773 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9774 		    sts_sensedata,
9775 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9776 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9777 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9778 		    scsi_pkt->pkt_reason;
9779 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9780 		    spx->txlt_sata_pkt->satapkt_reason;
9781 
9782 		if (++sata_atapi_trace_index >= 64)
9783 			sata_atapi_trace_index = 0;
9784 	}
9785 }
9786 
9787 #endif
9788 
9789 /*
9790  * Fetch inquiry data from ATAPI device
9791  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9792  *
9793  * Note:
9794  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9795  * where the caller expects to see the inquiry data.
9796  *
9797  */
9798 
9799 static int
9800 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9801     sata_address_t *saddr, struct scsi_inquiry *inq)
9802 {
9803 	sata_pkt_txlate_t *spx;
9804 	sata_pkt_t *spkt;
9805 	struct buf *bp;
9806 	sata_drive_info_t *sdinfo;
9807 	sata_cmd_t *scmd;
9808 	int rval;
9809 	uint8_t *rqsp;
9810 	dev_info_t *dip = SATA_DIP(sata_hba);
9811 #ifdef SATA_DEBUG
9812 	char msg_buf[MAXPATHLEN];
9813 #endif
9814 
9815 	ASSERT(sata_hba != NULL);
9816 
9817 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9818 	spx->txlt_sata_hba_inst = sata_hba;
9819 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9820 	spkt = sata_pkt_alloc(spx, NULL);
9821 	if (spkt == NULL) {
9822 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9823 		return (SATA_FAILURE);
9824 	}
9825 	/* address is needed now */
9826 	spkt->satapkt_device.satadev_addr = *saddr;
9827 
9828 	/* scsi_inquiry size buffer */
9829 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9830 	if (bp == NULL) {
9831 		sata_pkt_free(spx);
9832 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9833 		SATA_LOG_D((sata_hba, CE_WARN,
9834 		    "sata_get_atapi_inquiry_data: "
9835 		    "cannot allocate data buffer"));
9836 		return (SATA_FAILURE);
9837 	}
9838 	bp_mapin(bp); /* make data buffer accessible */
9839 
9840 	scmd = &spkt->satapkt_cmd;
9841 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9842 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9843 
9844 	/* Use synchronous mode */
9845 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9846 	spkt->satapkt_comp = NULL;
9847 	spkt->satapkt_time = sata_default_pkt_time;
9848 
9849 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9850 
9851 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9852 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9853 
9854 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
9855 	sdinfo = sata_get_device_info(sata_hba,
9856 	    &spx->txlt_sata_pkt->satapkt_device);
9857 	if (sdinfo == NULL) {
9858 		/* we have to be carefull about the disapearing device */
9859 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9860 		rval = SATA_FAILURE;
9861 		goto cleanup;
9862 	}
9863 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9864 
9865 	/*
9866 	 * Set-up acdb. This works for atapi transport version 2 and later.
9867 	 */
9868 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9869 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9870 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
9871 	scmd->satacmd_acdb[1] = 0x00;
9872 	scmd->satacmd_acdb[2] = 0x00;
9873 	scmd->satacmd_acdb[3] = 0x00;
9874 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9875 	scmd->satacmd_acdb[5] = 0x00;
9876 
9877 	sata_fixed_sense_data_preset(
9878 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9879 
9880 	/* Transfer command to HBA */
9881 	if (sata_hba_start(spx, &rval) != 0) {
9882 		/* Pkt not accepted for execution */
9883 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9884 		    "sata_get_atapi_inquiry_data: "
9885 		    "Packet not accepted for execution - ret: %02x", rval);
9886 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9887 		rval = SATA_FAILURE;
9888 		goto cleanup;
9889 	}
9890 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9891 
9892 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9893 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9894 		    "sata_get_atapi_inquiry_data: "
9895 		    "Packet completed successfully - ret: %02x", rval);
9896 		if (spx->txlt_buf_dma_handle != NULL) {
9897 			/*
9898 			 * Sync buffer. Handle is in usual place in translate
9899 			 * struct.
9900 			 */
9901 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
9902 			    DDI_DMA_SYNC_FORCPU);
9903 			ASSERT(rval == DDI_SUCCESS);
9904 		}
9905 
9906 		if (sata_check_for_dma_error(dip, spx)) {
9907 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
9908 			rval = SATA_FAILURE;
9909 		} else {
9910 			/*
9911 			 * Normal completion - copy data into caller's buffer
9912 			 */
9913 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
9914 			    sizeof (struct scsi_inquiry));
9915 #ifdef SATA_DEBUG
9916 			if (sata_debug_flags & SATA_DBG_ATAPI) {
9917 				sata_show_inqry_data((uint8_t *)inq);
9918 			}
9919 #endif
9920 			rval = SATA_SUCCESS;
9921 		}
9922 	} else {
9923 		/*
9924 		 * Something went wrong - analyze return - check rqsense data
9925 		 */
9926 		rval = SATA_FAILURE;
9927 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9928 			/*
9929 			 * ARQ data hopefull show something other than NO SENSE
9930 			 */
9931 			rqsp = scmd->satacmd_rqsense;
9932 #ifdef SATA_DEBUG
9933 			if (sata_debug_flags & SATA_DBG_ATAPI) {
9934 				msg_buf[0] = '\0';
9935 				(void) snprintf(msg_buf, MAXPATHLEN,
9936 				    "ATAPI packet completion reason: %02x\n"
9937 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
9938 				    "          %02x %02x %02x %02x %02x %02x\n"
9939 				    "          %02x %02x %02x %02x %02x %02x",
9940 				    spkt->satapkt_reason,
9941 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9942 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9943 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9944 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9945 				    rqsp[16], rqsp[17]);
9946 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9947 				    "%s", msg_buf);
9948 			}
9949 #endif
9950 		} else {
9951 			switch (spkt->satapkt_reason) {
9952 			case SATA_PKT_PORT_ERROR:
9953 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
9954 				    "sata_get_atapi_inquiry_data: "
9955 				    "packet reason: port error", NULL);
9956 				break;
9957 
9958 			case SATA_PKT_TIMEOUT:
9959 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
9960 				    "sata_get_atapi_inquiry_data: "
9961 				    "packet reason: timeout", NULL);
9962 				break;
9963 
9964 			case SATA_PKT_ABORTED:
9965 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
9966 				    "sata_get_atapi_inquiry_data: "
9967 				    "packet reason: aborted", NULL);
9968 				break;
9969 
9970 			case SATA_PKT_RESET:
9971 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
9972 				    "sata_get_atapi_inquiry_data: "
9973 				    "packet reason: reset\n", NULL);
9974 				break;
9975 			default:
9976 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
9977 				    "sata_get_atapi_inquiry_data: "
9978 				    "invalid packet reason: %02x\n",
9979 				    spkt->satapkt_reason);
9980 				break;
9981 			}
9982 		}
9983 	}
9984 cleanup:
9985 	sata_free_local_buffer(spx);
9986 	sata_pkt_free(spx);
9987 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
9988 	return (rval);
9989 }
9990 
9991 
9992 
9993 
9994 
9995 #if 0
9996 #ifdef SATA_DEBUG
9997 
9998 /*
9999  * Test ATAPI packet command.
10000  * Single threaded test: send packet command in synch mode, process completion
10001  *
10002  */
10003 static void
10004 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10005 {
10006 	sata_pkt_txlate_t *spx;
10007 	sata_pkt_t *spkt;
10008 	struct buf *bp;
10009 	sata_device_t sata_device;
10010 	sata_drive_info_t *sdinfo;
10011 	sata_cmd_t *scmd;
10012 	int rval;
10013 	uint8_t *rqsp;
10014 
10015 	ASSERT(sata_hba_inst != NULL);
10016 	sata_device.satadev_addr.cport = cport;
10017 	sata_device.satadev_addr.pmport = 0;
10018 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10019 	sata_device.satadev_rev = SATA_DEVICE_REV;
10020 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10021 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10022 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10023 	if (sdinfo == NULL) {
10024 		sata_log(sata_hba_inst, CE_WARN,
10025 		    "sata_test_atapi_packet_command: "
10026 		    "no device info for cport %d",
10027 		    sata_device.satadev_addr.cport);
10028 		return;
10029 	}
10030 
10031 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10032 	spx->txlt_sata_hba_inst = sata_hba_inst;
10033 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10034 	spkt = sata_pkt_alloc(spx, NULL);
10035 	if (spkt == NULL) {
10036 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10037 		return;
10038 	}
10039 	/* address is needed now */
10040 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10041 
10042 	/* 1024k buffer */
10043 	bp = sata_alloc_local_buffer(spx, 1024);
10044 	if (bp == NULL) {
10045 		sata_pkt_free(spx);
10046 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10047 		sata_log(sata_hba_inst, CE_WARN,
10048 		    "sata_test_atapi_packet_command: "
10049 		    "cannot allocate data buffer");
10050 		return;
10051 	}
10052 	bp_mapin(bp); /* make data buffer accessible */
10053 
10054 	scmd = &spkt->satapkt_cmd;
10055 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10056 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10057 
10058 	/* Use synchronous mode */
10059 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10060 
10061 	/* Synchronous mode, no callback - may be changed by the caller */
10062 	spkt->satapkt_comp = NULL;
10063 	spkt->satapkt_time = sata_default_pkt_time;
10064 
10065 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10066 
10067 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10068 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10069 
10070 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10071 
10072 	/* Set-up acdb. */
10073 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10074 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10075 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10076 	scmd->satacmd_acdb[1] = 0x00;
10077 	scmd->satacmd_acdb[2] = 0x00;
10078 	scmd->satacmd_acdb[3] = 0x00;
10079 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10080 	scmd->satacmd_acdb[5] = 0x00;
10081 
10082 	sata_fixed_sense_data_preset(
10083 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10084 
10085 	/* Transfer command to HBA */
10086 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10087 	if (sata_hba_start(spx, &rval) != 0) {
10088 		/* Pkt not accepted for execution */
10089 		sata_log(sata_hba_inst, CE_WARN,
10090 		    "sata_test_atapi_packet_command: "
10091 		    "Packet not accepted for execution - ret: %02x", rval);
10092 		mutex_exit(
10093 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10094 		goto cleanup;
10095 	}
10096 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10097 
10098 	if (spx->txlt_buf_dma_handle != NULL) {
10099 		/*
10100 		 * Sync buffer. Handle is in usual place in translate struct.
10101 		 */
10102 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10103 		    DDI_DMA_SYNC_FORCPU);
10104 		ASSERT(rval == DDI_SUCCESS);
10105 	}
10106 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10107 		sata_log(sata_hba_inst, CE_WARN,
10108 		    "sata_test_atapi_packet_command: "
10109 		    "Packet completed successfully");
10110 		/*
10111 		 * Normal completion - show inquiry data
10112 		 */
10113 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10114 	} else {
10115 		/*
10116 		 * Something went wrong - analyze return - check rqsense data
10117 		 */
10118 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10119 			/*
10120 			 * ARQ data hopefull show something other than NO SENSE
10121 			 */
10122 			rqsp = scmd->satacmd_rqsense;
10123 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10124 			    "ATAPI packet completion reason: %02x\n"
10125 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10126 			    "          %02x %02x %02x %02x %02x %02x "
10127 			    "          %02x %02x %02x %02x %02x %02x\n",
10128 			    spkt->satapkt_reason,
10129 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10130 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10131 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10132 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10133 			    rqsp[16], rqsp[17]);
10134 		} else {
10135 			switch (spkt->satapkt_reason) {
10136 			case SATA_PKT_PORT_ERROR:
10137 				sata_log(sata_hba_inst, CE_WARN,
10138 				    "sata_test_atapi_packet_command: "
10139 				    "packet reason: port error\n");
10140 				break;
10141 
10142 			case SATA_PKT_TIMEOUT:
10143 				sata_log(sata_hba_inst, CE_WARN,
10144 				    "sata_test_atapi_packet_command: "
10145 				    "packet reason: timeout\n");
10146 				break;
10147 
10148 			case SATA_PKT_ABORTED:
10149 				sata_log(sata_hba_inst, CE_WARN,
10150 				    "sata_test_atapi_packet_command: "
10151 				    "packet reason: aborted\n");
10152 				break;
10153 
10154 			case SATA_PKT_RESET:
10155 				sata_log(sata_hba_inst, CE_WARN,
10156 				    "sata_test_atapi_packet_command: "
10157 				    "packet reason: reset\n");
10158 				break;
10159 			default:
10160 				sata_log(sata_hba_inst, CE_WARN,
10161 				    "sata_test_atapi_packet_command: "
10162 				    "invalid packet reason: %02x\n",
10163 				    spkt->satapkt_reason);
10164 				break;
10165 			}
10166 		}
10167 	}
10168 cleanup:
10169 	sata_free_local_buffer(spx);
10170 	sata_pkt_free(spx);
10171 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10172 }
10173 
10174 #endif /* SATA_DEBUG */
10175 #endif /* 1 */
10176 
10177 
10178 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10179 
10180 /*
10181  * Validate sata_tran info
10182  * SATA_FAILURE returns if structure is inconsistent or structure revision
10183  * does not match one used by the framework.
10184  *
10185  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10186  * required function pointers.
10187  * Returns SATA_FAILURE otherwise.
10188  */
10189 static int
10190 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10191 {
10192 	/*
10193 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10194 	 * of the SATA interface.
10195 	 */
10196 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10197 		sata_log(NULL, CE_WARN,
10198 		    "sata: invalid sata_hba_tran version %d for driver %s",
10199 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10200 		return (SATA_FAILURE);
10201 	}
10202 
10203 	if (dip != sata_tran->sata_tran_hba_dip) {
10204 		SATA_LOG_D((NULL, CE_WARN,
10205 		    "sata: inconsistent sata_tran_hba_dip "
10206 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10207 		return (SATA_FAILURE);
10208 	}
10209 
10210 	if (sata_tran->sata_tran_probe_port == NULL ||
10211 	    sata_tran->sata_tran_start == NULL ||
10212 	    sata_tran->sata_tran_abort == NULL ||
10213 	    sata_tran->sata_tran_reset_dport == NULL ||
10214 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10215 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10216 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10217 	    NULL) {
10218 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10219 		    "required functions"));
10220 	}
10221 	return (SATA_SUCCESS);
10222 }
10223 
10224 /*
10225  * Remove HBA instance from sata_hba_list.
10226  */
10227 static void
10228 sata_remove_hba_instance(dev_info_t *dip)
10229 {
10230 	sata_hba_inst_t	*sata_hba_inst;
10231 
10232 	mutex_enter(&sata_mutex);
10233 	for (sata_hba_inst = sata_hba_list;
10234 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10235 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10236 		if (sata_hba_inst->satahba_dip == dip)
10237 			break;
10238 	}
10239 
10240 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10241 #ifdef SATA_DEBUG
10242 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10243 		    "unknown HBA instance\n");
10244 #endif
10245 		ASSERT(FALSE);
10246 	}
10247 	if (sata_hba_inst == sata_hba_list) {
10248 		sata_hba_list = sata_hba_inst->satahba_next;
10249 		if (sata_hba_list) {
10250 			sata_hba_list->satahba_prev =
10251 			    (struct sata_hba_inst *)NULL;
10252 		}
10253 		if (sata_hba_inst == sata_hba_list_tail) {
10254 			sata_hba_list_tail = NULL;
10255 		}
10256 	} else if (sata_hba_inst == sata_hba_list_tail) {
10257 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10258 		if (sata_hba_list_tail) {
10259 			sata_hba_list_tail->satahba_next =
10260 			    (struct sata_hba_inst *)NULL;
10261 		}
10262 	} else {
10263 		sata_hba_inst->satahba_prev->satahba_next =
10264 		    sata_hba_inst->satahba_next;
10265 		sata_hba_inst->satahba_next->satahba_prev =
10266 		    sata_hba_inst->satahba_prev;
10267 	}
10268 	mutex_exit(&sata_mutex);
10269 }
10270 
10271 /*
10272  * Probe all SATA ports of the specified HBA instance.
10273  * The assumption is that there are no target and attachment point minor nodes
10274  * created by the boot subsystems, so we do not need to prune device tree.
10275  *
10276  * This function is called only from sata_hba_attach(). It does not have to
10277  * be protected by controller mutex, because the hba_attached flag is not set
10278  * yet and no one would be touching this HBA instance other than this thread.
10279  * Determines if port is active and what type of the device is attached
10280  * (if any). Allocates necessary structures for each port.
10281  *
10282  * An AP (Attachement Point) node is created for each SATA device port even
10283  * when there is no device attached.
10284  */
10285 
10286 static 	void
10287 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10288 {
10289 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10290 	int			ncport;
10291 	sata_cport_info_t 	*cportinfo;
10292 	sata_drive_info_t	*drive;
10293 	sata_device_t		sata_device;
10294 	int			rval;
10295 	dev_t			minor_number;
10296 	char			name[16];
10297 	clock_t			start_time, cur_time;
10298 
10299 	/*
10300 	 * Probe controller ports first, to find port status and
10301 	 * any port multiplier attached.
10302 	 */
10303 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10304 		/* allocate cport structure */
10305 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10306 		ASSERT(cportinfo != NULL);
10307 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10308 
10309 		mutex_enter(&cportinfo->cport_mutex);
10310 
10311 		cportinfo->cport_addr.cport = ncport;
10312 		cportinfo->cport_addr.pmport = 0;
10313 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10314 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10315 		cportinfo->cport_state |= SATA_STATE_PROBING;
10316 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10317 
10318 		/*
10319 		 * Regardless if a port is usable or not, create
10320 		 * an attachment point
10321 		 */
10322 		mutex_exit(&cportinfo->cport_mutex);
10323 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10324 		    ncport, 0, SATA_ADDR_CPORT);
10325 		(void) sprintf(name, "%d", ncport);
10326 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10327 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10328 		    DDI_SUCCESS) {
10329 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10330 			    "cannot create SATA attachment point for port %d",
10331 			    ncport);
10332 		}
10333 
10334 		/* Probe port */
10335 		start_time = ddi_get_lbolt();
10336 	reprobe_cport:
10337 		sata_device.satadev_addr.cport = ncport;
10338 		sata_device.satadev_addr.pmport = 0;
10339 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10340 		sata_device.satadev_rev = SATA_DEVICE_REV;
10341 
10342 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10343 		    (dip, &sata_device);
10344 
10345 		mutex_enter(&cportinfo->cport_mutex);
10346 		cportinfo->cport_scr = sata_device.satadev_scr;
10347 		if (rval != SATA_SUCCESS) {
10348 			/* Something went wrong? Fail the port */
10349 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10350 			mutex_exit(&cportinfo->cport_mutex);
10351 			continue;
10352 		}
10353 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10354 		cportinfo->cport_state |= SATA_STATE_PROBED;
10355 		cportinfo->cport_dev_type = sata_device.satadev_type;
10356 
10357 		cportinfo->cport_state |= SATA_STATE_READY;
10358 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10359 			mutex_exit(&cportinfo->cport_mutex);
10360 			continue;
10361 		}
10362 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10363 			/*
10364 			 * There is some device attached.
10365 			 * Allocate device info structure
10366 			 */
10367 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10368 				mutex_exit(&cportinfo->cport_mutex);
10369 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10370 				    kmem_zalloc(sizeof (sata_drive_info_t),
10371 				    KM_SLEEP);
10372 				mutex_enter(&cportinfo->cport_mutex);
10373 			}
10374 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10375 			drive->satadrv_addr = cportinfo->cport_addr;
10376 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10377 			drive->satadrv_type = cportinfo->cport_dev_type;
10378 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10379 
10380 			mutex_exit(&cportinfo->cport_mutex);
10381 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10382 			    SATA_SUCCESS) {
10383 				/*
10384 				 * Plugged device was not correctly identified.
10385 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10386 				 */
10387 				cur_time = ddi_get_lbolt();
10388 				if ((cur_time - start_time) <
10389 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10390 					/* sleep for a while */
10391 					delay(drv_usectohz(
10392 					    SATA_DEV_RETRY_DLY));
10393 					goto reprobe_cport;
10394 				}
10395 			}
10396 		} else { /* SATA_DTYPE_PMULT */
10397 			mutex_exit(&cportinfo->cport_mutex);
10398 
10399 			/* Allocate sata_pmult_info and sata_pmport_info */
10400 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10401 			    SATA_SUCCESS)
10402 				continue;
10403 
10404 			/* Log the information of the port multiplier */
10405 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10406 
10407 			/* Probe its pmports */
10408 			sata_probe_pmports(sata_hba_inst, ncport);
10409 		}
10410 	}
10411 }
10412 
10413 /*
10414  * Probe all device ports behind a port multiplier.
10415  *
10416  * PMult-related structure should be allocated before by sata_alloc_pmult().
10417  *
10418  * NOTE1: Only called from sata_probe_ports()
10419  * NOTE2: No mutex should be hold.
10420  */
10421 static void
10422 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10423 {
10424 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10425 	sata_pmult_info_t	*pmultinfo = NULL;
10426 	sata_pmport_info_t 	*pmportinfo = NULL;
10427 	sata_drive_info_t	*drive = NULL;
10428 	sata_device_t		sata_device;
10429 
10430 	clock_t			start_time, cur_time;
10431 	int			npmport;
10432 	int			rval;
10433 
10434 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10435 
10436 	/* Probe Port Multiplier ports */
10437 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10438 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10439 		start_time = ddi_get_lbolt();
10440 reprobe_pmport:
10441 		sata_device.satadev_addr.cport = ncport;
10442 		sata_device.satadev_addr.pmport = npmport;
10443 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10444 		sata_device.satadev_rev = SATA_DEVICE_REV;
10445 
10446 		/* Let HBA driver probe it. */
10447 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10448 		    (dip, &sata_device);
10449 		mutex_enter(&pmportinfo->pmport_mutex);
10450 
10451 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10452 
10453 		if (rval != SATA_SUCCESS) {
10454 			pmportinfo->pmport_state =
10455 			    SATA_PSTATE_FAILED;
10456 			mutex_exit(&pmportinfo->pmport_mutex);
10457 			continue;
10458 		}
10459 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10460 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10461 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10462 
10463 		pmportinfo->pmport_state |= SATA_STATE_READY;
10464 		if (pmportinfo->pmport_dev_type ==
10465 		    SATA_DTYPE_NONE) {
10466 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10467 			    "no device found at port %d:%d", ncport, npmport);
10468 			mutex_exit(&pmportinfo->pmport_mutex);
10469 			continue;
10470 		}
10471 		/* Port multipliers cannot be chained */
10472 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10473 		/*
10474 		 * There is something attached to Port
10475 		 * Multiplier device port
10476 		 * Allocate device info structure
10477 		 */
10478 		if (pmportinfo->pmport_sata_drive == NULL) {
10479 			mutex_exit(&pmportinfo->pmport_mutex);
10480 			pmportinfo->pmport_sata_drive =
10481 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10482 			mutex_enter(&pmportinfo->pmport_mutex);
10483 		}
10484 		drive = pmportinfo->pmport_sata_drive;
10485 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10486 		drive->satadrv_addr.pmport = npmport;
10487 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10488 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10489 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10490 
10491 		mutex_exit(&pmportinfo->pmport_mutex);
10492 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10493 
10494 		if (rval != SATA_SUCCESS) {
10495 			/*
10496 			 * Plugged device was not correctly identified.
10497 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10498 			 */
10499 			cur_time = ddi_get_lbolt();
10500 			if ((cur_time - start_time) < drv_usectohz(
10501 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10502 				/* sleep for a while */
10503 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10504 				goto reprobe_pmport;
10505 			}
10506 		}
10507 	}
10508 }
10509 
10510 /*
10511  * Add SATA device for specified HBA instance & port (SCSI target
10512  * device nodes).
10513  * This function is called (indirectly) only from sata_hba_attach().
10514  * A target node is created when there is a supported type device attached,
10515  * but may be removed if it cannot be put online.
10516  *
10517  * This function cannot be called from an interrupt context.
10518  *
10519  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10520  *
10521  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10522  * device identification failed - adding a device could be retried.
10523  *
10524  */
10525 static 	int
10526 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10527     sata_device_t *sata_device)
10528 {
10529 	sata_cport_info_t 	*cportinfo;
10530 	sata_pmult_info_t	*pminfo;
10531 	sata_pmport_info_t	*pmportinfo;
10532 	dev_info_t		*cdip;		/* child dip */
10533 	sata_address_t		*saddr = &sata_device->satadev_addr;
10534 	uint8_t			cport, pmport;
10535 	int			rval;
10536 
10537 	cport = saddr->cport;
10538 	pmport = saddr->pmport;
10539 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10540 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10541 
10542 	/*
10543 	 * Some device is attached to a controller port.
10544 	 * We rely on controllers distinquishing between no-device,
10545 	 * attached port multiplier and other kind of attached device.
10546 	 * We need to get Identify Device data and determine
10547 	 * positively the dev type before trying to attach
10548 	 * the target driver.
10549 	 */
10550 	sata_device->satadev_rev = SATA_DEVICE_REV;
10551 	switch (saddr->qual) {
10552 	case SATA_ADDR_CPORT:
10553 		/*
10554 		 * Add a non-port-multiplier device at controller port.
10555 		 */
10556 		saddr->qual = SATA_ADDR_DCPORT;
10557 
10558 		rval = sata_probe_device(sata_hba_inst, sata_device);
10559 		if (rval != SATA_SUCCESS ||
10560 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10561 			return (SATA_FAILURE);
10562 
10563 		mutex_enter(&cportinfo->cport_mutex);
10564 		sata_show_drive_info(sata_hba_inst,
10565 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10566 
10567 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10568 			/*
10569 			 * Could not determine device type or
10570 			 * a device is not supported.
10571 			 * Degrade this device to unknown.
10572 			 */
10573 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10574 			mutex_exit(&cportinfo->cport_mutex);
10575 			return (SATA_SUCCESS);
10576 		}
10577 		cportinfo->cport_dev_type = sata_device->satadev_type;
10578 		cportinfo->cport_tgtnode_clean = B_TRUE;
10579 		mutex_exit(&cportinfo->cport_mutex);
10580 
10581 		/*
10582 		 * Initialize device to the desired state. Even if it
10583 		 * fails, the device will still attach but syslog
10584 		 * will show the warning.
10585 		 */
10586 		if (sata_initialize_device(sata_hba_inst,
10587 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10588 			/* Retry */
10589 			rval = sata_initialize_device(sata_hba_inst,
10590 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10591 
10592 			if (rval == SATA_RETRY)
10593 				sata_log(sata_hba_inst, CE_WARN,
10594 				    "SATA device at port %d - "
10595 				    "default device features could not be set."
10596 				    " Device may not operate as expected.",
10597 				    cport);
10598 		}
10599 
10600 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10601 		if (cdip == NULL) {
10602 			/*
10603 			 * Attaching target node failed.
10604 			 * We retain sata_drive_info structure...
10605 			 */
10606 			return (SATA_SUCCESS);
10607 		}
10608 
10609 		mutex_enter(&cportinfo->cport_mutex);
10610 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10611 		    satadrv_state = SATA_STATE_READY;
10612 		mutex_exit(&cportinfo->cport_mutex);
10613 
10614 		break;
10615 
10616 	case SATA_ADDR_PMPORT:
10617 		saddr->qual = SATA_ADDR_DPMPORT;
10618 
10619 		mutex_enter(&cportinfo->cport_mutex);
10620 		/* It must be a Port Multiplier at the controller port */
10621 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10622 
10623 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10624 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10625 		mutex_exit(&cportinfo->cport_mutex);
10626 
10627 		rval = sata_probe_device(sata_hba_inst, sata_device);
10628 		if (rval != SATA_SUCCESS ||
10629 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10630 			return (SATA_FAILURE);
10631 		}
10632 
10633 		mutex_enter(&pmportinfo->pmport_mutex);
10634 		sata_show_drive_info(sata_hba_inst,
10635 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10636 
10637 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10638 			/*
10639 			 * Could not determine device type.
10640 			 * Degrade this device to unknown.
10641 			 */
10642 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10643 			mutex_exit(&pmportinfo->pmport_mutex);
10644 			return (SATA_SUCCESS);
10645 		}
10646 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10647 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10648 		mutex_exit(&pmportinfo->pmport_mutex);
10649 
10650 		/*
10651 		 * Initialize device to the desired state.
10652 		 * Even if it fails, the device will still
10653 		 * attach but syslog will show the warning.
10654 		 */
10655 		if (sata_initialize_device(sata_hba_inst,
10656 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10657 			/* Retry */
10658 			rval = sata_initialize_device(sata_hba_inst,
10659 			    pmportinfo->pmport_sata_drive);
10660 
10661 			if (rval == SATA_RETRY)
10662 				sata_log(sata_hba_inst, CE_WARN,
10663 				    "SATA device at port %d:%d - "
10664 				    "default device features could not be set."
10665 				    " Device may not operate as expected.",
10666 				    cport, pmport);
10667 		}
10668 
10669 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10670 		if (cdip == NULL) {
10671 			/*
10672 			 * Attaching target node failed.
10673 			 * We retain sata_drive_info structure...
10674 			 */
10675 			return (SATA_SUCCESS);
10676 		}
10677 		mutex_enter(&pmportinfo->pmport_mutex);
10678 		pmportinfo->pmport_sata_drive->satadrv_state |=
10679 		    SATA_STATE_READY;
10680 		mutex_exit(&pmportinfo->pmport_mutex);
10681 
10682 		break;
10683 
10684 	default:
10685 		return (SATA_FAILURE);
10686 	}
10687 
10688 	return (SATA_SUCCESS);
10689 }
10690 
10691 /*
10692  * Clean up target node at specific address.
10693  *
10694  * NOTE: No Mutex should be hold.
10695  */
10696 static int
10697 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10698     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10699 {
10700 	uint8_t cport, pmport, qual;
10701 	dev_info_t *tdip;
10702 
10703 	cport = sata_device->satadev_addr.cport;
10704 	pmport = sata_device->satadev_addr.pmport;
10705 	qual = sata_device->satadev_addr.qual;
10706 
10707 	if (qual == SATA_ADDR_DCPORT) {
10708 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10709 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10710 	} else {
10711 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10712 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10713 		    cport, pmport));
10714 	}
10715 
10716 	/* We are addressing attached device, not a port */
10717 	sata_device->satadev_addr.qual =
10718 	    sdinfo->satadrv_addr.qual;
10719 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10720 	    &sata_device->satadev_addr);
10721 	if (tdip != NULL && ndi_devi_offline(tdip,
10722 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10723 		/*
10724 		 * Problem :
10725 		 * The target node remained attached.
10726 		 * This happens when the device file was open
10727 		 * or a node was waiting for resources.
10728 		 * Cannot do anything about it.
10729 		 */
10730 		if (qual == SATA_ADDR_DCPORT) {
10731 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10732 			    "sata_hba_ioctl: disconnect: could "
10733 			    "not unconfigure device before "
10734 			    "disconnecting the SATA port %d",
10735 			    cport));
10736 		} else {
10737 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10738 			    "sata_hba_ioctl: disconnect: could "
10739 			    "not unconfigure device before "
10740 			    "disconnecting the SATA port %d:%d",
10741 			    cport, pmport));
10742 		}
10743 		/*
10744 		 * Set DEVICE REMOVED state in the target
10745 		 * node. It will prevent access to the device
10746 		 * even when a new device is attached, until
10747 		 * the old target node is released, removed and
10748 		 * recreated for a new  device.
10749 		 */
10750 		sata_set_device_removed(tdip);
10751 
10752 		/*
10753 		 * Instruct event daemon to try the target
10754 		 * node cleanup later.
10755 		 */
10756 		sata_set_target_node_cleanup(
10757 		    sata_hba_inst, &sata_device->satadev_addr);
10758 	}
10759 
10760 
10761 	return (SATA_SUCCESS);
10762 }
10763 
10764 
10765 /*
10766  * Create scsi target node for attached device, create node properties and
10767  * attach the node.
10768  * The node could be removed if the device onlining fails.
10769  *
10770  * A dev_info_t pointer is returned if operation is successful, NULL is
10771  * returned otherwise.
10772  */
10773 
10774 static dev_info_t *
10775 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10776 			sata_address_t *sata_addr)
10777 {
10778 	dev_info_t *cdip = NULL;
10779 	int rval;
10780 	char *nname = NULL;
10781 	char **compatible = NULL;
10782 	int ncompatible;
10783 	struct scsi_inquiry inq;
10784 	sata_device_t sata_device;
10785 	sata_drive_info_t *sdinfo;
10786 	int target;
10787 	int i;
10788 
10789 	sata_device.satadev_rev = SATA_DEVICE_REV;
10790 	sata_device.satadev_addr = *sata_addr;
10791 
10792 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10793 
10794 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10795 
10796 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10797 	    sata_addr->pmport, sata_addr->qual);
10798 
10799 	if (sdinfo == NULL) {
10800 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10801 		    sata_addr->cport)));
10802 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10803 		    "sata_create_target_node: no sdinfo for target %x",
10804 		    target));
10805 		return (NULL);
10806 	}
10807 
10808 	/*
10809 	 * create or get scsi inquiry data, expected by
10810 	 * scsi_hba_nodename_compatible_get()
10811 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10812 	 * ATAPI devices respond directly to Inquiry request.
10813 	 */
10814 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10815 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10816 		    (uint8_t *)&inq);
10817 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10818 		    sata_addr->cport)));
10819 	} else { /* Assume supported ATAPI device */
10820 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10821 		    sata_addr->cport)));
10822 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10823 		    &inq) == SATA_FAILURE)
10824 			return (NULL);
10825 		/*
10826 		 * Save supported ATAPI transport version
10827 		 */
10828 		sdinfo->satadrv_atapi_trans_ver =
10829 		    SATA_ATAPI_TRANS_VERSION(&inq);
10830 	}
10831 
10832 	/* determine the node name and compatible */
10833 	scsi_hba_nodename_compatible_get(&inq, NULL,
10834 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10835 
10836 #ifdef SATA_DEBUG
10837 	if (sata_debug_flags & SATA_DBG_NODES) {
10838 		if (nname == NULL) {
10839 			cmn_err(CE_NOTE, "sata_create_target_node: "
10840 			    "cannot determine nodename for target %d\n",
10841 			    target);
10842 		} else {
10843 			cmn_err(CE_WARN, "sata_create_target_node: "
10844 			    "target %d nodename: %s\n", target, nname);
10845 		}
10846 		if (compatible == NULL) {
10847 			cmn_err(CE_WARN,
10848 			    "sata_create_target_node: no compatible name\n");
10849 		} else {
10850 			for (i = 0; i < ncompatible; i++) {
10851 				cmn_err(CE_WARN, "sata_create_target_node: "
10852 				    "compatible name: %s\n", compatible[i]);
10853 			}
10854 		}
10855 	}
10856 #endif
10857 
10858 	/* if nodename can't be determined, log error and exit */
10859 	if (nname == NULL) {
10860 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10861 		    "sata_create_target_node: cannot determine nodename "
10862 		    "for target %d\n", target));
10863 		scsi_hba_nodename_compatible_free(nname, compatible);
10864 		return (NULL);
10865 	}
10866 	/*
10867 	 * Create scsi target node
10868 	 */
10869 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10870 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10871 	    "device-type", "scsi");
10872 
10873 	if (rval != DDI_PROP_SUCCESS) {
10874 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10875 		    "updating device_type prop failed %d", rval));
10876 		goto fail;
10877 	}
10878 
10879 	/*
10880 	 * Create target node properties: target & lun
10881 	 */
10882 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10883 	if (rval != DDI_PROP_SUCCESS) {
10884 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10885 		    "updating target prop failed %d", rval));
10886 		goto fail;
10887 	}
10888 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
10889 	if (rval != DDI_PROP_SUCCESS) {
10890 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10891 		    "updating target prop failed %d", rval));
10892 		goto fail;
10893 	}
10894 
10895 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10896 		/*
10897 		 * Add "variant" property
10898 		 */
10899 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10900 		    "variant", "atapi");
10901 		if (rval != DDI_PROP_SUCCESS) {
10902 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10903 			    "sata_create_target_node: variant atapi "
10904 			    "property could not be created: %d", rval));
10905 			goto fail;
10906 		}
10907 	}
10908 	/* decorate the node with compatible */
10909 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
10910 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
10911 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10912 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
10913 		    (void *)cdip));
10914 		goto fail;
10915 	}
10916 
10917 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10918 		/*
10919 		 * Add "sata-phy" property
10920 		 */
10921 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
10922 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
10923 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10924 			    "sata_create_target_node: failed to create "
10925 			    "\"sata-phy\" property: port %d",
10926 			    sata_addr->cport));
10927 		}
10928 	}
10929 
10930 
10931 	/*
10932 	 * Now, try to attach the driver. If probing of the device fails,
10933 	 * the target node may be removed
10934 	 */
10935 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
10936 
10937 	scsi_hba_nodename_compatible_free(nname, compatible);
10938 
10939 	if (rval == NDI_SUCCESS)
10940 		return (cdip);
10941 
10942 	/* target node was removed - are we sure? */
10943 	return (NULL);
10944 
10945 fail:
10946 	scsi_hba_nodename_compatible_free(nname, compatible);
10947 	ddi_prop_remove_all(cdip);
10948 	rval = ndi_devi_free(cdip);
10949 	if (rval != NDI_SUCCESS) {
10950 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10951 		    "node removal failed %d", rval));
10952 	}
10953 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
10954 	    "cannot create target node for SATA device at port %d",
10955 	    sata_addr->cport);
10956 	return (NULL);
10957 }
10958 
10959 /*
10960  * Remove a target node.
10961  */
10962 static void
10963 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
10964 			sata_address_t *sata_addr)
10965 {
10966 	dev_info_t *tdip;
10967 	uint8_t cport = sata_addr->cport;
10968 	uint8_t pmport = sata_addr->pmport;
10969 	uint8_t qual = sata_addr->qual;
10970 
10971 	/* Note the sata daemon uses the address of the port/pmport */
10972 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10973 
10974 	/* Remove target node */
10975 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
10976 	if (tdip != NULL) {
10977 		/*
10978 		 * Target node exists.  Unconfigure device
10979 		 * then remove the target node (one ndi
10980 		 * operation).
10981 		 */
10982 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10983 			/*
10984 			 * PROBLEM - no device, but target node remained. This
10985 			 * happens when the file was open or node was waiting
10986 			 * for resources.
10987 			 */
10988 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10989 			    "sata_remove_target_node: "
10990 			    "Failed to remove target node for "
10991 			    "detached SATA device."));
10992 			/*
10993 			 * Set target node state to DEVI_DEVICE_REMOVED. But
10994 			 * re-check first that the node still exists.
10995 			 */
10996 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
10997 			    cport, pmport);
10998 			if (tdip != NULL) {
10999 				sata_set_device_removed(tdip);
11000 				/*
11001 				 * Instruct event daemon to retry the cleanup
11002 				 * later.
11003 				 */
11004 				sata_set_target_node_cleanup(sata_hba_inst,
11005 				    sata_addr);
11006 			}
11007 		}
11008 
11009 		if (qual == SATA_ADDR_CPORT)
11010 			sata_log(sata_hba_inst, CE_WARN,
11011 			    "SATA device detached at port %d", cport);
11012 		else
11013 			sata_log(sata_hba_inst, CE_WARN,
11014 			    "SATA device detached at port %d:%d",
11015 			    cport, pmport);
11016 	}
11017 #ifdef SATA_DEBUG
11018 	else {
11019 		if (qual == SATA_ADDR_CPORT)
11020 			sata_log(sata_hba_inst, CE_WARN,
11021 			    "target node not found at port %d", cport);
11022 		else
11023 			sata_log(sata_hba_inst, CE_WARN,
11024 			    "target node not found at port %d:%d",
11025 			    cport, pmport);
11026 	}
11027 #endif
11028 }
11029 
11030 
11031 /*
11032  * Re-probe sata port, check for a device and attach info
11033  * structures when necessary. Identify Device data is fetched, if possible.
11034  * Assumption: sata address is already validated.
11035  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11036  * the presence of a device and its type.
11037  *
11038  * flag arg specifies that the function should try multiple times to identify
11039  * device type and to initialize it, or it should return immediately on failure.
11040  * SATA_DEV_IDENTIFY_RETRY - retry
11041  * SATA_DEV_IDENTIFY_NORETRY - no retry
11042  *
11043  * SATA_FAILURE is returned if one of the operations failed.
11044  *
11045  * This function cannot be called in interrupt context - it may sleep.
11046  *
11047  * Note: Port multiplier is supported.
11048  */
11049 static int
11050 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11051     int flag)
11052 {
11053 	sata_cport_info_t *cportinfo;
11054 	sata_pmult_info_t *pmultinfo;
11055 	sata_drive_info_t *sdinfo, *osdinfo;
11056 	boolean_t init_device = B_FALSE;
11057 	int prev_device_type = SATA_DTYPE_NONE;
11058 	int prev_device_settings = 0;
11059 	int prev_device_state = 0;
11060 	clock_t start_time;
11061 	int retry = B_FALSE;
11062 	uint8_t cport = sata_device->satadev_addr.cport;
11063 	int rval_probe, rval_init;
11064 
11065 	/*
11066 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11067 	 */
11068 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11069 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11070 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11071 
11072 	/* We only care about host sata cport for now */
11073 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11074 	    sata_device->satadev_addr.cport);
11075 
11076 	/*
11077 	 * If a port multiplier was previously attached (we have no idea it
11078 	 * still there or not), sata_reprobe_pmult() will handle it.
11079 	 */
11080 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11081 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11082 
11083 	/* Store sata_drive_info when a non-pmult device was attached. */
11084 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11085 	if (osdinfo != NULL) {
11086 		/*
11087 		 * We are re-probing port with a previously attached device.
11088 		 * Save previous device type and settings.
11089 		 */
11090 		prev_device_type = cportinfo->cport_dev_type;
11091 		prev_device_settings = osdinfo->satadrv_settings;
11092 		prev_device_state = osdinfo->satadrv_state;
11093 	}
11094 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11095 		start_time = ddi_get_lbolt();
11096 		retry = B_TRUE;
11097 	}
11098 retry_probe:
11099 
11100 	/* probe port */
11101 	mutex_enter(&cportinfo->cport_mutex);
11102 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11103 	cportinfo->cport_state |= SATA_STATE_PROBING;
11104 	mutex_exit(&cportinfo->cport_mutex);
11105 
11106 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11107 	    (SATA_DIP(sata_hba_inst), sata_device);
11108 
11109 	mutex_enter(&cportinfo->cport_mutex);
11110 	if (rval_probe != SATA_SUCCESS) {
11111 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11112 		mutex_exit(&cportinfo->cport_mutex);
11113 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11114 		    "SATA port %d probing failed",
11115 		    cportinfo->cport_addr.cport));
11116 		return (SATA_FAILURE);
11117 	}
11118 
11119 	/*
11120 	 * update sata port state and set device type
11121 	 */
11122 	sata_update_port_info(sata_hba_inst, sata_device);
11123 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11124 
11125 	/*
11126 	 * Sanity check - Port is active? Is the link active?
11127 	 * Is there any device attached?
11128 	 */
11129 	if ((cportinfo->cport_state &
11130 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11131 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11132 	    SATA_PORT_DEVLINK_UP) {
11133 		/*
11134 		 * Port in non-usable state or no link active/no device.
11135 		 * Free info structure if necessary (direct attached drive
11136 		 * only, for now!
11137 		 */
11138 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11139 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11140 		/* Add here differentiation for device attached or not */
11141 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11142 		mutex_exit(&cportinfo->cport_mutex);
11143 		if (sdinfo != NULL)
11144 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11145 		return (SATA_SUCCESS);
11146 	}
11147 
11148 	cportinfo->cport_state |= SATA_STATE_READY;
11149 	cportinfo->cport_state |= SATA_STATE_PROBED;
11150 
11151 	cportinfo->cport_dev_type = sata_device->satadev_type;
11152 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11153 
11154 	/*
11155 	 * If we are re-probing the port, there may be
11156 	 * sata_drive_info structure attached
11157 	 */
11158 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11159 
11160 		/*
11161 		 * There is no device, so remove device info structure,
11162 		 * if necessary.
11163 		 */
11164 		/* Device change: Drive -> None */
11165 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11166 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11167 		if (sdinfo != NULL) {
11168 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11169 			sata_log(sata_hba_inst, CE_WARN,
11170 			    "SATA device detached "
11171 			    "from port %d", cportinfo->cport_addr.cport);
11172 		}
11173 		mutex_exit(&cportinfo->cport_mutex);
11174 		return (SATA_SUCCESS);
11175 
11176 	}
11177 
11178 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11179 
11180 		/* Device (may) change: Drive -> Drive */
11181 		if (sdinfo == NULL) {
11182 			/*
11183 			 * There is some device attached, but there is
11184 			 * no sata_drive_info structure - allocate one
11185 			 */
11186 			mutex_exit(&cportinfo->cport_mutex);
11187 			sdinfo = kmem_zalloc(
11188 			    sizeof (sata_drive_info_t), KM_SLEEP);
11189 			mutex_enter(&cportinfo->cport_mutex);
11190 			/*
11191 			 * Recheck, that the port state did not change when we
11192 			 * released mutex.
11193 			 */
11194 			if (cportinfo->cport_state & SATA_STATE_READY) {
11195 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11196 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11197 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11198 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11199 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11200 			} else {
11201 				/*
11202 				 * Port is not in ready state, we
11203 				 * cannot attach a device.
11204 				 */
11205 				mutex_exit(&cportinfo->cport_mutex);
11206 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11207 				return (SATA_SUCCESS);
11208 			}
11209 			/*
11210 			 * Since we are adding device, presumably new one,
11211 			 * indicate that it  should be initalized,
11212 			 * as well as some internal framework states).
11213 			 */
11214 			init_device = B_TRUE;
11215 		}
11216 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11217 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11218 	} else {
11219 		/* Device change: Drive -> PMult */
11220 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11221 		if (sdinfo != NULL) {
11222 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11223 			sata_log(sata_hba_inst, CE_WARN,
11224 			    "SATA device detached "
11225 			    "from port %d", cportinfo->cport_addr.cport);
11226 		}
11227 
11228 		sata_log(sata_hba_inst, CE_WARN,
11229 		    "SATA port multiplier detected at port %d",
11230 		    cportinfo->cport_addr.cport);
11231 
11232 		mutex_exit(&cportinfo->cport_mutex);
11233 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11234 		    SATA_SUCCESS)
11235 			return (SATA_FAILURE);
11236 		sata_show_pmult_info(sata_hba_inst, sata_device);
11237 		mutex_enter(&cportinfo->cport_mutex);
11238 
11239 		/*
11240 		 * Mark all the port multiplier port behind the port
11241 		 * multiplier behind with link events, so that the sata daemon
11242 		 * will update their status.
11243 		 */
11244 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11245 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11246 		mutex_exit(&cportinfo->cport_mutex);
11247 		return (SATA_SUCCESS);
11248 	}
11249 	mutex_exit(&cportinfo->cport_mutex);
11250 
11251 	/*
11252 	 * Figure out what kind of device we are really
11253 	 * dealing with. Failure of identifying device does not fail this
11254 	 * function.
11255 	 */
11256 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11257 	rval_init = SATA_FAILURE;
11258 	mutex_enter(&cportinfo->cport_mutex);
11259 	if (rval_probe == SATA_SUCCESS) {
11260 		/*
11261 		 * If we are dealing with the same type of a device as before,
11262 		 * restore its settings flags.
11263 		 */
11264 		if (osdinfo != NULL &&
11265 		    sata_device->satadev_type == prev_device_type)
11266 			sdinfo->satadrv_settings = prev_device_settings;
11267 
11268 		mutex_exit(&cportinfo->cport_mutex);
11269 		rval_init = SATA_SUCCESS;
11270 		/* Set initial device features, if necessary */
11271 		if (init_device == B_TRUE) {
11272 			rval_init = sata_initialize_device(sata_hba_inst,
11273 			    sdinfo);
11274 		}
11275 		if (rval_init == SATA_SUCCESS)
11276 			return (rval_init);
11277 		/* else we will retry if retry was asked for */
11278 
11279 	} else {
11280 		/*
11281 		 * If there was some device info before we probe the device,
11282 		 * restore previous device setting, so we can retry from scratch
11283 		 * later. Providing, of course, that device has not disapear
11284 		 * during probing process.
11285 		 */
11286 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11287 			if (osdinfo != NULL) {
11288 				cportinfo->cport_dev_type = prev_device_type;
11289 				sdinfo->satadrv_type = prev_device_type;
11290 				sdinfo->satadrv_state = prev_device_state;
11291 			}
11292 		} else {
11293 			/* device is gone */
11294 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11295 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11296 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11297 			mutex_exit(&cportinfo->cport_mutex);
11298 			return (SATA_SUCCESS);
11299 		}
11300 		mutex_exit(&cportinfo->cport_mutex);
11301 	}
11302 
11303 	if (retry) {
11304 		clock_t cur_time = ddi_get_lbolt();
11305 		/*
11306 		 * A device was not successfully identified or initialized.
11307 		 * Track retry time for device identification.
11308 		 */
11309 		if ((cur_time - start_time) <
11310 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11311 			/* sleep for a while */
11312 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11313 			goto retry_probe;
11314 		}
11315 		/* else no more retries */
11316 		mutex_enter(&cportinfo->cport_mutex);
11317 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11318 			if (rval_init == SATA_RETRY) {
11319 				/*
11320 				 * Setting drive features have failed, but
11321 				 * because the drive is still accessible,
11322 				 * keep it and emit a warning message.
11323 				 */
11324 				sata_log(sata_hba_inst, CE_WARN,
11325 				    "SATA device at port %d - desired "
11326 				    "drive features could not be set. "
11327 				    "Device may not operate as expected.",
11328 				    cportinfo->cport_addr.cport);
11329 			} else {
11330 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11331 				    satadrv_state = SATA_DSTATE_FAILED;
11332 			}
11333 		}
11334 		mutex_exit(&cportinfo->cport_mutex);
11335 	}
11336 	return (SATA_SUCCESS);
11337 }
11338 
11339 /*
11340  * Reprobe a controller port that connected to a port multiplier.
11341  *
11342  * NOTE: No Mutex should be hold.
11343  */
11344 static int
11345 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11346     int flag)
11347 {
11348 	_NOTE(ARGUNUSED(flag))
11349 	sata_cport_info_t *cportinfo;
11350 	sata_pmult_info_t *pmultinfo;
11351 	uint8_t cport = sata_device->satadev_addr.cport;
11352 	int rval_probe;
11353 
11354 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11355 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11356 
11357 	/* probe port */
11358 	mutex_enter(&cportinfo->cport_mutex);
11359 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11360 	cportinfo->cport_state |= SATA_STATE_PROBING;
11361 	mutex_exit(&cportinfo->cport_mutex);
11362 
11363 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11364 	    (SATA_DIP(sata_hba_inst), sata_device);
11365 
11366 	mutex_enter(&cportinfo->cport_mutex);
11367 	if (rval_probe != SATA_SUCCESS) {
11368 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11369 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11370 		    "SATA port %d probing failed", cport));
11371 		sata_log(sata_hba_inst, CE_WARN,
11372 		    "SATA port multiplier detached at port %d", cport);
11373 		mutex_exit(&cportinfo->cport_mutex);
11374 		sata_free_pmult(sata_hba_inst, sata_device);
11375 		return (SATA_FAILURE);
11376 	}
11377 
11378 	/*
11379 	 * update sata port state and set device type
11380 	 */
11381 	sata_update_port_info(sata_hba_inst, sata_device);
11382 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11383 	cportinfo->cport_state |= SATA_STATE_PROBED;
11384 
11385 	/*
11386 	 * Sanity check - Port is active? Is the link active?
11387 	 * Is there any device attached?
11388 	 */
11389 	if ((cportinfo->cport_state &
11390 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11391 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11392 	    SATA_PORT_DEVLINK_UP ||
11393 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11394 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11395 		mutex_exit(&cportinfo->cport_mutex);
11396 		sata_free_pmult(sata_hba_inst, sata_device);
11397 		sata_log(sata_hba_inst, CE_WARN,
11398 		    "SATA port multiplier detached at port %d", cport);
11399 		return (SATA_SUCCESS);
11400 	}
11401 
11402 	/*
11403 	 * Device changed: PMult -> Non-PMult
11404 	 *
11405 	 * This situation is uncommon, most possibly being caused by errors
11406 	 * after which the port multiplier is not correct initialized and
11407 	 * recognized. In that case the new device will be marked as unknown
11408 	 * and will not be automatically probed in this routine. Instead
11409 	 * system administrator could manually restart it via cfgadm(1M).
11410 	 */
11411 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11412 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11413 		mutex_exit(&cportinfo->cport_mutex);
11414 		sata_free_pmult(sata_hba_inst, sata_device);
11415 		sata_log(sata_hba_inst, CE_WARN,
11416 		    "SATA port multiplier detached at port %d", cport);
11417 		return (SATA_FAILURE);
11418 	}
11419 
11420 	/*
11421 	 * Now we know it is a port multiplier. However, if this is not the
11422 	 * previously attached port multiplier - they may have different
11423 	 * pmport numbers - we need to re-allocate data structures for every
11424 	 * pmport and drive.
11425 	 *
11426 	 * Port multipliers of the same model have identical values in these
11427 	 * registers, so it is still necessary to update the information of
11428 	 * all drives attached to the previous port multiplier afterwards.
11429 	 */
11430 	/* Device changed: PMult -> another PMult */
11431 	mutex_exit(&cportinfo->cport_mutex);
11432 	sata_free_pmult(sata_hba_inst, sata_device);
11433 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11434 		return (SATA_FAILURE);
11435 	mutex_enter(&cportinfo->cport_mutex);
11436 
11437 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11438 	    "SATA port multiplier [changed] at port %d", cport);
11439 	sata_log(sata_hba_inst, CE_WARN,
11440 	    "SATA port multiplier detected at port %d", cport);
11441 
11442 	/*
11443 	 * Mark all the port multiplier port behind the port
11444 	 * multiplier behind with link events, so that the sata daemon
11445 	 * will update their status.
11446 	 */
11447 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11448 	mutex_exit(&cportinfo->cport_mutex);
11449 
11450 	return (SATA_SUCCESS);
11451 }
11452 
11453 /*
11454  * Re-probe a port multiplier port, check for a device and attach info
11455  * structures when necessary. Identify Device data is fetched, if possible.
11456  * Assumption: sata address is already validated as port multiplier port.
11457  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11458  * the presence of a device and its type.
11459  *
11460  * flag arg specifies that the function should try multiple times to identify
11461  * device type and to initialize it, or it should return immediately on failure.
11462  * SATA_DEV_IDENTIFY_RETRY - retry
11463  * SATA_DEV_IDENTIFY_NORETRY - no retry
11464  *
11465  * SATA_FAILURE is returned if one of the operations failed.
11466  *
11467  * This function cannot be called in interrupt context - it may sleep.
11468  *
11469  * NOTE: Should be only called by sata_probe_port() in case target port is a
11470  *       port multiplier port.
11471  * NOTE: No Mutex should be hold.
11472  */
11473 static int
11474 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11475     int flag)
11476 {
11477 	sata_cport_info_t *cportinfo = NULL;
11478 	sata_pmport_info_t *pmportinfo = NULL;
11479 	sata_drive_info_t *sdinfo, *osdinfo;
11480 	sata_device_t sdevice;
11481 	boolean_t init_device = B_FALSE;
11482 	int prev_device_type = SATA_DTYPE_NONE;
11483 	int prev_device_settings = 0;
11484 	int prev_device_state = 0;
11485 	clock_t start_time;
11486 	uint8_t cport = sata_device->satadev_addr.cport;
11487 	uint8_t pmport = sata_device->satadev_addr.pmport;
11488 	int rval;
11489 
11490 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11491 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11492 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11493 
11494 	if (osdinfo != NULL) {
11495 		/*
11496 		 * We are re-probing port with a previously attached device.
11497 		 * Save previous device type and settings.
11498 		 */
11499 		prev_device_type = pmportinfo->pmport_dev_type;
11500 		prev_device_settings = osdinfo->satadrv_settings;
11501 		prev_device_state = osdinfo->satadrv_state;
11502 	}
11503 
11504 	start_time = ddi_get_lbolt();
11505 
11506 	/* check parent status */
11507 	mutex_enter(&cportinfo->cport_mutex);
11508 	if ((cportinfo->cport_state &
11509 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11510 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11511 	    SATA_PORT_DEVLINK_UP) {
11512 		mutex_exit(&cportinfo->cport_mutex);
11513 		return (SATA_FAILURE);
11514 	}
11515 	mutex_exit(&cportinfo->cport_mutex);
11516 
11517 retry_probe_pmport:
11518 
11519 	/* probe port */
11520 	mutex_enter(&pmportinfo->pmport_mutex);
11521 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11522 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11523 	mutex_exit(&pmportinfo->pmport_mutex);
11524 
11525 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11526 	    (SATA_DIP(sata_hba_inst), sata_device);
11527 
11528 	/* might need retry because we cannot touch registers. */
11529 	if (rval == SATA_FAILURE) {
11530 		mutex_enter(&pmportinfo->pmport_mutex);
11531 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11532 		mutex_exit(&pmportinfo->pmport_mutex);
11533 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11534 		    "SATA port %d:%d probing failed",
11535 		    cport, pmport));
11536 		return (SATA_FAILURE);
11537 	} else if (rval == SATA_RETRY) {
11538 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11539 		    "SATA port %d:%d probing failed, retrying...",
11540 		    cport, pmport));
11541 		clock_t cur_time = ddi_get_lbolt();
11542 		/*
11543 		 * A device was not successfully identified or initialized.
11544 		 * Track retry time for device identification.
11545 		 */
11546 		if ((cur_time - start_time) <
11547 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11548 			/* sleep for a while */
11549 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11550 			goto retry_probe_pmport;
11551 		} else {
11552 			mutex_enter(&pmportinfo->pmport_mutex);
11553 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11554 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11555 				    satadrv_state = SATA_DSTATE_FAILED;
11556 			mutex_exit(&pmportinfo->pmport_mutex);
11557 			return (SATA_SUCCESS);
11558 		}
11559 	}
11560 
11561 	/*
11562 	 * Sanity check - Controller port is active? Is the link active?
11563 	 * Is it still a port multiplier?
11564 	 */
11565 	if ((cportinfo->cport_state &
11566 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11567 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11568 	    SATA_PORT_DEVLINK_UP ||
11569 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11570 		/*
11571 		 * Port in non-usable state or no link active/no
11572 		 * device. Free info structure.
11573 		 */
11574 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11575 
11576 		sdevice.satadev_addr.cport = cport;
11577 		sdevice.satadev_addr.pmport = pmport;
11578 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11579 		mutex_exit(&cportinfo->cport_mutex);
11580 
11581 		sata_free_pmult(sata_hba_inst, &sdevice);
11582 		return (SATA_FAILURE);
11583 	}
11584 
11585 	/* SATA_SUCCESS NOW */
11586 	/*
11587 	 * update sata port state and set device type
11588 	 */
11589 	mutex_enter(&pmportinfo->pmport_mutex);
11590 	sata_update_pmport_info(sata_hba_inst, sata_device);
11591 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11592 
11593 	/*
11594 	 * Sanity check - Port is active? Is the link active?
11595 	 * Is there any device attached?
11596 	 */
11597 	if ((pmportinfo->pmport_state &
11598 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11599 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11600 	    SATA_PORT_DEVLINK_UP) {
11601 		/*
11602 		 * Port in non-usable state or no link active/no device.
11603 		 * Free info structure if necessary (direct attached drive
11604 		 * only, for now!
11605 		 */
11606 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11607 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11608 		/* Add here differentiation for device attached or not */
11609 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11610 		mutex_exit(&pmportinfo->pmport_mutex);
11611 		if (sdinfo != NULL)
11612 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11613 		return (SATA_SUCCESS);
11614 	}
11615 
11616 	pmportinfo->pmport_state |= SATA_STATE_READY;
11617 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11618 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11619 
11620 	/*
11621 	 * If we are re-probing the port, there may be
11622 	 * sata_drive_info structure attached
11623 	 * (or sata_pm_info, if PMult is supported).
11624 	 */
11625 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11626 		/*
11627 		 * There is no device, so remove device info structure,
11628 		 * if necessary.
11629 		 */
11630 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11631 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11632 		if (sdinfo != NULL) {
11633 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11634 			sata_log(sata_hba_inst, CE_WARN,
11635 			    "SATA device detached from port %d:%d",
11636 			    cport, pmport);
11637 		}
11638 		mutex_exit(&pmportinfo->pmport_mutex);
11639 		return (SATA_SUCCESS);
11640 	}
11641 
11642 	/* this should not be a pmult */
11643 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11644 	if (sdinfo == NULL) {
11645 		/*
11646 		 * There is some device attached, but there is
11647 		 * no sata_drive_info structure - allocate one
11648 		 */
11649 		mutex_exit(&pmportinfo->pmport_mutex);
11650 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11651 		    KM_SLEEP);
11652 		mutex_enter(&pmportinfo->pmport_mutex);
11653 		/*
11654 		 * Recheck, that the port state did not change when we
11655 		 * released mutex.
11656 		 */
11657 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11658 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11659 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11660 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11661 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11662 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11663 		} else {
11664 			/*
11665 			 * Port is not in ready state, we
11666 			 * cannot attach a device.
11667 			 */
11668 			mutex_exit(&pmportinfo->pmport_mutex);
11669 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11670 			return (SATA_SUCCESS);
11671 		}
11672 		/*
11673 		 * Since we are adding device, presumably new one,
11674 		 * indicate that it  should be initalized,
11675 		 * as well as some internal framework states).
11676 		 */
11677 		init_device = B_TRUE;
11678 	}
11679 
11680 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11681 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11682 
11683 	mutex_exit(&pmportinfo->pmport_mutex);
11684 	/*
11685 	 * Figure out what kind of device we are really
11686 	 * dealing with.
11687 	 */
11688 	rval = sata_probe_device(sata_hba_inst, sata_device);
11689 
11690 	mutex_enter(&pmportinfo->pmport_mutex);
11691 	if (rval == SATA_SUCCESS) {
11692 		/*
11693 		 * If we are dealing with the same type of a device as before,
11694 		 * restore its settings flags.
11695 		 */
11696 		if (osdinfo != NULL &&
11697 		    sata_device->satadev_type == prev_device_type)
11698 			sdinfo->satadrv_settings = prev_device_settings;
11699 
11700 		mutex_exit(&pmportinfo->pmport_mutex);
11701 		/* Set initial device features, if necessary */
11702 		if (init_device == B_TRUE) {
11703 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11704 		}
11705 		if (rval == SATA_SUCCESS)
11706 			return (rval);
11707 	} else {
11708 		/*
11709 		 * If there was some device info before we probe the device,
11710 		 * restore previous device setting, so we can retry from scratch
11711 		 * later. Providing, of course, that device has not disappeared
11712 		 * during probing process.
11713 		 */
11714 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11715 			if (osdinfo != NULL) {
11716 				pmportinfo->pmport_dev_type = prev_device_type;
11717 				sdinfo->satadrv_type = prev_device_type;
11718 				sdinfo->satadrv_state = prev_device_state;
11719 			}
11720 		} else {
11721 			/* device is gone */
11722 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11723 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11724 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11725 			mutex_exit(&pmportinfo->pmport_mutex);
11726 			return (SATA_SUCCESS);
11727 		}
11728 		mutex_exit(&pmportinfo->pmport_mutex);
11729 	}
11730 
11731 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11732 		clock_t cur_time = ddi_get_lbolt();
11733 		/*
11734 		 * A device was not successfully identified or initialized.
11735 		 * Track retry time for device identification.
11736 		 */
11737 		if ((cur_time - start_time) <
11738 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11739 			/* sleep for a while */
11740 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11741 			goto retry_probe_pmport;
11742 		} else {
11743 			mutex_enter(&pmportinfo->pmport_mutex);
11744 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11745 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11746 				    satadrv_state = SATA_DSTATE_FAILED;
11747 			mutex_exit(&pmportinfo->pmport_mutex);
11748 		}
11749 	}
11750 	return (SATA_SUCCESS);
11751 }
11752 
11753 /*
11754  * Allocated related structure for a port multiplier and its device ports
11755  *
11756  * Port multiplier should be ready and probed, and related information like
11757  * the number of the device ports should be store in sata_device_t.
11758  *
11759  * NOTE: No Mutex should be hold.
11760  */
11761 static int
11762 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11763 {
11764 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11765 	sata_cport_info_t *cportinfo = NULL;
11766 	sata_pmult_info_t *pmultinfo = NULL;
11767 	sata_pmport_info_t *pmportinfo = NULL;
11768 	sata_device_t sd;
11769 	dev_t minor_number;
11770 	char name[16];
11771 	uint8_t cport = sata_device->satadev_addr.cport;
11772 	int rval;
11773 	int npmport;
11774 
11775 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11776 
11777 	/* This function might be called while a port-mult is hot-plugged. */
11778 	mutex_enter(&cportinfo->cport_mutex);
11779 
11780 	/* dev_type's not updated when get called from sata_reprobe_port() */
11781 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11782 		/* Create a pmult_info structure */
11783 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11784 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11785 	}
11786 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11787 
11788 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11789 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11790 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11791 
11792 	/*
11793 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11794 	 * The HBA driver should initialize and register the port multiplier,
11795 	 * sata_register_pmult() will fill following fields,
11796 	 *   + sata_pmult_info.pmult_gscr
11797 	 *   + sata_pmult_info.pmult_num_dev_ports
11798 	 */
11799 	sd.satadev_addr = sata_device->satadev_addr;
11800 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11801 	mutex_exit(&cportinfo->cport_mutex);
11802 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11803 	    (SATA_DIP(sata_hba_inst), &sd);
11804 	mutex_enter(&cportinfo->cport_mutex);
11805 
11806 	if (rval != SATA_SUCCESS ||
11807 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11808 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11809 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11810 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11811 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11812 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11813 		mutex_exit(&cportinfo->cport_mutex);
11814 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11815 		    "sata_alloc_pmult: failed to initialize pmult "
11816 		    "at port %d.", cport)
11817 		return (SATA_FAILURE);
11818 	}
11819 
11820 	/* Initialize pmport_info structure */
11821 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11822 	    npmport++) {
11823 
11824 		/* if everything is allocated, skip */
11825 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11826 			continue;
11827 
11828 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11829 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11830 		mutex_exit(&cportinfo->cport_mutex);
11831 
11832 		mutex_enter(&pmportinfo->pmport_mutex);
11833 		pmportinfo->pmport_addr.cport = cport;
11834 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11835 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11836 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11837 		mutex_exit(&pmportinfo->pmport_mutex);
11838 
11839 		mutex_enter(&cportinfo->cport_mutex);
11840 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11841 
11842 		/* Create an attachment point */
11843 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11844 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11845 		(void) sprintf(name, "%d.%d", cport, npmport);
11846 
11847 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11848 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11849 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11850 			    "cannot create SATA attachment point for "
11851 			    "port %d:%d", cport, npmport);
11852 		}
11853 	}
11854 
11855 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11856 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11857 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11858 
11859 	mutex_exit(&cportinfo->cport_mutex);
11860 	return (SATA_SUCCESS);
11861 }
11862 
11863 /*
11864  * Free data structures when a port multiplier is removed.
11865  *
11866  * NOTE: No Mutex should be hold.
11867  */
11868 static void
11869 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11870 {
11871 	sata_cport_info_t *cportinfo;
11872 	sata_pmult_info_t *pmultinfo;
11873 	sata_pmport_info_t *pmportinfo;
11874 	sata_device_t pmport_device;
11875 	sata_drive_info_t *sdinfo;
11876 	dev_info_t *tdip;
11877 	char name[16];
11878 	uint8_t cport = sata_device->satadev_addr.cport;
11879 	int npmport;
11880 
11881 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11882 
11883 	/* This function might be called while port-mult is hot plugged. */
11884 	mutex_enter(&cportinfo->cport_mutex);
11885 
11886 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11887 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11888 	ASSERT(pmultinfo != NULL);
11889 
11890 	/* Free pmport_info structure */
11891 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11892 	    npmport++) {
11893 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
11894 		if (pmportinfo == NULL)
11895 			continue;
11896 		mutex_exit(&cportinfo->cport_mutex);
11897 
11898 		mutex_enter(&pmportinfo->pmport_mutex);
11899 		sdinfo = pmportinfo->pmport_sata_drive;
11900 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11901 		mutex_exit(&pmportinfo->pmport_mutex);
11902 
11903 		/* Remove attachment point. */
11904 		name[0] = '\0';
11905 		(void) sprintf(name, "%d.%d", cport, npmport);
11906 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
11907 		sata_log(sata_hba_inst, CE_NOTE,
11908 		    "Remove attachment point of port %d:%d",
11909 		    cport, npmport);
11910 
11911 		/*
11912 		 * Rumove target node
11913 		 */
11914 		bzero(&pmport_device, sizeof (sata_device_t));
11915 		pmport_device.satadev_rev = SATA_DEVICE_REV;
11916 		pmport_device.satadev_addr.cport = cport;
11917 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
11918 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
11919 
11920 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11921 		    &(pmport_device.satadev_addr));
11922 		if (tdip != NULL && ndi_devi_offline(tdip,
11923 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11924 			/*
11925 			 * Problem :
11926 			 * The target node remained attached.
11927 			 * This happens when the device file was open
11928 			 * or a node was waiting for resources.
11929 			 * Cannot do anything about it.
11930 			 */
11931 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11932 			    "sata_free_pmult: could not unconfigure device "
11933 			    "before disconnecting the SATA port %d:%d",
11934 			    cport, npmport));
11935 
11936 			/*
11937 			 * Set DEVICE REMOVED state in the target
11938 			 * node. It will prevent access to the device
11939 			 * even when a new device is attached, until
11940 			 * the old target node is released, removed and
11941 			 * recreated for a new  device.
11942 			 */
11943 			sata_set_device_removed(tdip);
11944 
11945 			/*
11946 			 * Instruct event daemon to try the target
11947 			 * node cleanup later.
11948 			 */
11949 			sata_set_target_node_cleanup(
11950 			    sata_hba_inst, &(pmport_device.satadev_addr));
11951 
11952 		}
11953 		mutex_enter(&cportinfo->cport_mutex);
11954 
11955 		/*
11956 		 * Add here differentiation for device attached or not
11957 		 */
11958 		if (sdinfo != NULL)  {
11959 			sata_log(sata_hba_inst, CE_WARN,
11960 			    "SATA device detached from port %d:%d",
11961 			    cport, npmport);
11962 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11963 		}
11964 
11965 		mutex_destroy(&pmportinfo->pmport_mutex);
11966 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
11967 	}
11968 
11969 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11970 
11971 	cportinfo->cport_devp.cport_sata_pmult = NULL;
11972 
11973 	sata_log(sata_hba_inst, CE_WARN,
11974 	    "SATA port multiplier detached at port %d", cport);
11975 
11976 	mutex_exit(&cportinfo->cport_mutex);
11977 }
11978 
11979 /*
11980  * Initialize device
11981  * Specified device is initialized to a default state.
11982  *
11983  * Returns SATA_SUCCESS if all device features are set successfully,
11984  * SATA_RETRY if device is accessible but device features were not set
11985  * successfully, and SATA_FAILURE otherwise.
11986  */
11987 static int
11988 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
11989     sata_drive_info_t *sdinfo)
11990 {
11991 	int rval;
11992 
11993 	sata_save_drive_settings(sdinfo);
11994 
11995 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
11996 
11997 	sata_init_write_cache_mode(sdinfo);
11998 
11999 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12000 
12001 	/* Determine current data transfer mode */
12002 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12003 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12004 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12005 	    SATA_VALIDINFO_88) != 0 &&
12006 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12007 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12008 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12009 	    SATA_MDMA_SEL_MASK) != 0) {
12010 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12011 	} else
12012 		/* DMA supported, not no DMA transfer mode is selected !? */
12013 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12014 
12015 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12016 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12017 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12018 	else
12019 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12020 
12021 	return (rval);
12022 }
12023 
12024 
12025 /*
12026  * Initialize write cache mode.
12027  *
12028  * The default write cache setting for SATA HDD is provided by sata_write_cache
12029  * static variable. ATAPI CD/DVDs devices have write cache default is
12030  * determined by sata_atapicdvd_write_cache static variable.
12031  * ATAPI tape devices have write cache default is determined by
12032  * sata_atapitape_write_cache static variable.
12033  * ATAPI disk devices have write cache default is determined by
12034  * sata_atapidisk_write_cache static variable.
12035  * 1 - enable
12036  * 0 - disable
12037  * any other value - current drive setting
12038  *
12039  * Although there is not reason to disable write cache on CD/DVD devices,
12040  * tape devices and ATAPI disk devices, the default setting control is provided
12041  * for the maximun flexibility.
12042  *
12043  * In the future, it may be overridden by the
12044  * disk-write-cache-enable property setting, if it is defined.
12045  * Returns SATA_SUCCESS if all device features are set successfully,
12046  * SATA_FAILURE otherwise.
12047  */
12048 static void
12049 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12050 {
12051 	switch (sdinfo->satadrv_type) {
12052 	case SATA_DTYPE_ATADISK:
12053 		if (sata_write_cache == 1)
12054 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12055 		else if (sata_write_cache == 0)
12056 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12057 		/*
12058 		 * When sata_write_cache value is not 0 or 1,
12059 		 * a current setting of the drive's write cache is used.
12060 		 */
12061 		break;
12062 	case SATA_DTYPE_ATAPICD:
12063 		if (sata_atapicdvd_write_cache == 1)
12064 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12065 		else if (sata_atapicdvd_write_cache == 0)
12066 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12067 		/*
12068 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12069 		 * a current setting of the drive's write cache is used.
12070 		 */
12071 		break;
12072 	case SATA_DTYPE_ATAPITAPE:
12073 		if (sata_atapitape_write_cache == 1)
12074 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12075 		else if (sata_atapitape_write_cache == 0)
12076 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12077 		/*
12078 		 * When sata_atapitape_write_cache value is not 0 or 1,
12079 		 * a current setting of the drive's write cache is used.
12080 		 */
12081 		break;
12082 	case SATA_DTYPE_ATAPIDISK:
12083 		if (sata_atapidisk_write_cache == 1)
12084 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12085 		else if (sata_atapidisk_write_cache == 0)
12086 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12087 		/*
12088 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12089 		 * a current setting of the drive's write cache is used.
12090 		 */
12091 		break;
12092 	}
12093 }
12094 
12095 
12096 /*
12097  * Validate sata address.
12098  * Specified cport, pmport and qualifier has to match
12099  * passed sata_scsi configuration info.
12100  * The presence of an attached device is not verified.
12101  *
12102  * Returns 0 when address is valid, -1 otherwise.
12103  */
12104 static int
12105 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12106 	int pmport, int qual)
12107 {
12108 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12109 		goto invalid_address;
12110 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12111 		goto invalid_address;
12112 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12113 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12114 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12115 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12116 		goto invalid_address;
12117 
12118 	return (0);
12119 
12120 invalid_address:
12121 	return (-1);
12122 
12123 }
12124 
12125 /*
12126  * Validate scsi address
12127  * SCSI target address is translated into SATA cport/pmport and compared
12128  * with a controller port/device configuration. LUN has to be 0.
12129  * Returns 0 if a scsi target refers to an attached device,
12130  * returns 1 if address is valid but no valid device is attached,
12131  * returns 2 if address is valid but device type is unknown (not valid device),
12132  * returns -1 if bad address or device is of an unsupported type.
12133  * Upon return sata_device argument is set.
12134  *
12135  * Port multiplier is supported now.
12136  */
12137 static int
12138 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12139 	struct scsi_address *ap, sata_device_t *sata_device)
12140 {
12141 	int cport, pmport, qual, rval;
12142 
12143 	rval = -1;	/* Invalid address */
12144 	if (ap->a_lun != 0)
12145 		goto out;
12146 
12147 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12148 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12149 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12150 
12151 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12152 		goto out;
12153 
12154 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12155 	    0) {
12156 
12157 		sata_cport_info_t *cportinfo;
12158 		sata_pmult_info_t *pmultinfo;
12159 		sata_drive_info_t *sdinfo = NULL;
12160 
12161 		sata_device->satadev_addr.qual = qual;
12162 		sata_device->satadev_addr.cport = cport;
12163 		sata_device->satadev_addr.pmport = pmport;
12164 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12165 
12166 		rval = 1;	/* Valid sata address */
12167 
12168 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12169 		if (qual == SATA_ADDR_DCPORT) {
12170 			if (cportinfo == NULL ||
12171 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12172 				goto out;
12173 
12174 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12175 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12176 			    sdinfo != NULL) {
12177 				rval = 2;
12178 				goto out;
12179 			}
12180 
12181 			if ((cportinfo->cport_dev_type &
12182 			    SATA_VALID_DEV_TYPE) == 0) {
12183 				rval = -1;
12184 				goto out;
12185 			}
12186 
12187 		} else if (qual == SATA_ADDR_DPMPORT) {
12188 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12189 			if (pmultinfo == NULL) {
12190 				rval = -1;
12191 				goto out;
12192 			}
12193 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12194 			    NULL ||
12195 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12196 			    pmport) == SATA_DTYPE_NONE)
12197 				goto out;
12198 
12199 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12200 			    pmport);
12201 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12202 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12203 				rval = 2;
12204 				goto out;
12205 			}
12206 
12207 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12208 			    pmport) && SATA_VALID_DEV_TYPE) == 0) {
12209 				rval = -1;
12210 				goto out;
12211 			}
12212 
12213 		} else {
12214 			rval = -1;
12215 			goto out;
12216 		}
12217 		if ((sdinfo == NULL) ||
12218 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12219 			goto out;
12220 
12221 		sata_device->satadev_type = sdinfo->satadrv_type;
12222 
12223 		return (0);
12224 	}
12225 out:
12226 	if (rval > 0) {
12227 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12228 		    "sata_validate_scsi_address: no valid target %x lun %x",
12229 		    ap->a_target, ap->a_lun);
12230 	}
12231 	return (rval);
12232 }
12233 
12234 /*
12235  * Find dip corresponding to passed device number
12236  *
12237  * Returns NULL if invalid device number is passed or device cannot be found,
12238  * Returns dip is device is found.
12239  */
12240 static dev_info_t *
12241 sata_devt_to_devinfo(dev_t dev)
12242 {
12243 	dev_info_t *dip;
12244 #ifndef __lock_lint
12245 	struct devnames *dnp;
12246 	major_t major = getmajor(dev);
12247 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12248 
12249 	if (major >= devcnt)
12250 		return (NULL);
12251 
12252 	dnp = &devnamesp[major];
12253 	LOCK_DEV_OPS(&(dnp->dn_lock));
12254 	dip = dnp->dn_head;
12255 	while (dip && (ddi_get_instance(dip) != instance)) {
12256 		dip = ddi_get_next(dip);
12257 	}
12258 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12259 #endif
12260 
12261 	return (dip);
12262 }
12263 
12264 
12265 /*
12266  * Probe device.
12267  * This function issues Identify Device command and initializes local
12268  * sata_drive_info structure if the device can be identified.
12269  * The device type is determined by examining Identify Device
12270  * command response.
12271  * If the sata_hba_inst has linked drive info structure for this
12272  * device address, the Identify Device data is stored into sata_drive_info
12273  * structure linked to the port info structure.
12274  *
12275  * sata_device has to refer to the valid sata port(s) for HBA described
12276  * by sata_hba_inst structure.
12277  *
12278  * Returns:
12279  *	SATA_SUCCESS if device type was successfully probed and port-linked
12280  *		drive info structure was updated;
12281  * 	SATA_FAILURE if there is no device, or device was not probed
12282  *		successully;
12283  *	SATA_RETRY if device probe can be retried later.
12284  * If a device cannot be identified, sata_device's dev_state and dev_type
12285  * fields are set to unknown.
12286  * There are no retries in this function. Any retries should be managed by
12287  * the caller.
12288  */
12289 
12290 
12291 static int
12292 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12293 {
12294 	sata_pmport_info_t *pmportinfo;
12295 	sata_drive_info_t *sdinfo;
12296 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12297 	int rval;
12298 
12299 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12300 	    sata_device->satadev_addr.cport) &
12301 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12302 
12303 	sata_device->satadev_type = SATA_DTYPE_NONE;
12304 
12305 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12306 	    sata_device->satadev_addr.cport)));
12307 
12308 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12309 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12310 		    sata_device->satadev_addr.cport,
12311 		    sata_device->satadev_addr.pmport);
12312 		ASSERT(pmportinfo != NULL);
12313 	}
12314 
12315 	/* Get pointer to port-linked sata device info structure */
12316 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12317 	if (sdinfo != NULL) {
12318 		sdinfo->satadrv_state &=
12319 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12320 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12321 	} else {
12322 		/* No device to probe */
12323 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12324 		    sata_device->satadev_addr.cport)));
12325 		sata_device->satadev_type = SATA_DTYPE_NONE;
12326 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12327 		return (SATA_FAILURE);
12328 	}
12329 	/*
12330 	 * Need to issue both types of identify device command and
12331 	 * determine device type by examining retreived data/status.
12332 	 * First, ATA Identify Device.
12333 	 */
12334 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12335 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12336 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12337 	    sata_device->satadev_addr.cport)));
12338 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12339 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12340 	if (rval == SATA_RETRY) {
12341 		/* We may try to check for ATAPI device */
12342 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12343 			/*
12344 			 * HBA supports ATAPI - try to issue Identify Packet
12345 			 * Device command.
12346 			 */
12347 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12348 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12349 		}
12350 	}
12351 	if (rval == SATA_SUCCESS) {
12352 		/*
12353 		 * Got something responding positively to ATA Identify Device
12354 		 * or to Identify Packet Device cmd.
12355 		 * Save last used device type.
12356 		 */
12357 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12358 
12359 		/* save device info, if possible */
12360 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12361 		    sata_device->satadev_addr.cport)));
12362 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12363 		if (sdinfo == NULL) {
12364 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12365 			    sata_device->satadev_addr.cport)));
12366 			return (SATA_FAILURE);
12367 		}
12368 		/*
12369 		 * Copy drive info into the port-linked drive info structure.
12370 		 */
12371 		*sdinfo = new_sdinfo;
12372 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12373 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12374 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12375 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12376 			    sata_device->satadev_addr.cport) =
12377 			    sdinfo->satadrv_type;
12378 		else { /* SATA_ADDR_DPMPORT */
12379 			mutex_enter(&pmportinfo->pmport_mutex);
12380 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12381 			    sata_device->satadev_addr.cport,
12382 			    sata_device->satadev_addr.pmport) =
12383 			    sdinfo->satadrv_type;
12384 			mutex_exit(&pmportinfo->pmport_mutex);
12385 		}
12386 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12387 		    sata_device->satadev_addr.cport)));
12388 		return (SATA_SUCCESS);
12389 	}
12390 
12391 	/*
12392 	 * It may be SATA_RETRY or SATA_FAILURE return.
12393 	 * Looks like we cannot determine the device type at this time.
12394 	 */
12395 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12396 	    sata_device->satadev_addr.cport)));
12397 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12398 	if (sdinfo != NULL) {
12399 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12400 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12401 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12402 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12403 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12404 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12405 			    sata_device->satadev_addr.cport) =
12406 			    SATA_DTYPE_UNKNOWN;
12407 		else {
12408 			/* SATA_ADDR_DPMPORT */
12409 			mutex_enter(&pmportinfo->pmport_mutex);
12410 			if ((SATA_PMULT_INFO(sata_hba_inst,
12411 			    sata_device->satadev_addr.cport) != NULL) &&
12412 			    (SATA_PMPORT_INFO(sata_hba_inst,
12413 			    sata_device->satadev_addr.cport,
12414 			    sata_device->satadev_addr.pmport) != NULL))
12415 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12416 				    sata_device->satadev_addr.cport,
12417 				    sata_device->satadev_addr.pmport) =
12418 				    SATA_DTYPE_UNKNOWN;
12419 			mutex_exit(&pmportinfo->pmport_mutex);
12420 		}
12421 	}
12422 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12423 	    sata_device->satadev_addr.cport)));
12424 	return (rval);
12425 }
12426 
12427 
12428 /*
12429  * Get pointer to sata_drive_info structure.
12430  *
12431  * The sata_device has to contain address (cport, pmport and qualifier) for
12432  * specified sata_scsi structure.
12433  *
12434  * Returns NULL if device address is not valid for this HBA configuration.
12435  * Otherwise, returns a pointer to sata_drive_info structure.
12436  *
12437  * This function should be called with a port mutex held.
12438  */
12439 static sata_drive_info_t *
12440 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12441     sata_device_t *sata_device)
12442 {
12443 	uint8_t cport = sata_device->satadev_addr.cport;
12444 	uint8_t pmport = sata_device->satadev_addr.pmport;
12445 	uint8_t qual = sata_device->satadev_addr.qual;
12446 
12447 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12448 		return (NULL);
12449 
12450 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12451 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12452 		/* Port not probed yet */
12453 		return (NULL);
12454 
12455 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12456 		return (NULL);
12457 
12458 	if (qual == SATA_ADDR_DCPORT) {
12459 		/* Request for a device on a controller port */
12460 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12461 		    SATA_DTYPE_PMULT)
12462 			/* Port multiplier attached */
12463 			return (NULL);
12464 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12465 	}
12466 	if (qual == SATA_ADDR_DPMPORT) {
12467 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12468 		    SATA_DTYPE_PMULT)
12469 			return (NULL);
12470 
12471 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12472 			return (NULL);
12473 
12474 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12475 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12476 			/* Port multiplier port not probed yet */
12477 			return (NULL);
12478 
12479 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12480 	}
12481 
12482 	/* we should not get here */
12483 	return (NULL);
12484 }
12485 
12486 
12487 /*
12488  * sata_identify_device.
12489  * Send Identify Device command to SATA HBA driver.
12490  * If command executes successfully, update sata_drive_info structure pointed
12491  * to by sdinfo argument, including Identify Device data.
12492  * If command fails, invalidate data in sata_drive_info.
12493  *
12494  * Cannot be called from interrupt level.
12495  *
12496  * Returns:
12497  * SATA_SUCCESS if the device was identified as a supported device,
12498  * SATA_RETRY if the device was not identified but could be retried,
12499  * SATA_FAILURE if the device was not identified and identify attempt
12500  *	should not be retried.
12501  */
12502 static int
12503 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12504     sata_drive_info_t *sdinfo)
12505 {
12506 	uint16_t cfg_word;
12507 	int rval;
12508 
12509 	/* fetch device identify data */
12510 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12511 	    sdinfo)) != SATA_SUCCESS)
12512 		goto fail_unknown;
12513 
12514 	cfg_word = sdinfo->satadrv_id.ai_config;
12515 
12516 	/* Set the correct device type */
12517 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12518 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12519 	} else if (cfg_word == SATA_CFA_TYPE) {
12520 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12521 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12522 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12523 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12524 		case SATA_ATAPI_CDROM_DEV:
12525 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12526 			break;
12527 		case SATA_ATAPI_SQACC_DEV:
12528 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12529 			break;
12530 		case SATA_ATAPI_DIRACC_DEV:
12531 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12532 			break;
12533 		default:
12534 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12535 		}
12536 	} else {
12537 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12538 	}
12539 
12540 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12541 		if (sdinfo->satadrv_capacity == 0) {
12542 			/* Non-LBA disk. Too bad... */
12543 			sata_log(sata_hba_inst, CE_WARN,
12544 			    "SATA disk device at port %d does not support LBA",
12545 			    sdinfo->satadrv_addr.cport);
12546 			rval = SATA_FAILURE;
12547 			goto fail_unknown;
12548 		}
12549 	}
12550 #if 0
12551 	/* Left for historical reason */
12552 	/*
12553 	 * Some initial version of SATA spec indicated that at least
12554 	 * UDMA mode 4 has to be supported. It is not metioned in
12555 	 * SerialATA 2.6, so this restriction is removed.
12556 	 */
12557 	/* Check for Ultra DMA modes 6 through 0 being supported */
12558 	for (i = 6; i >= 0; --i) {
12559 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12560 			break;
12561 	}
12562 
12563 	/*
12564 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12565 	 * higher are not supported by the device, fail this
12566 	 * device.
12567 	 */
12568 	if (i < 4) {
12569 		/* No required Ultra DMA mode supported */
12570 		sata_log(sata_hba_inst, CE_WARN,
12571 		    "SATA disk device at port %d does not support UDMA "
12572 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12573 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12574 		    "mode 4 or higher required, %d supported", i));
12575 		rval = SATA_FAILURE;
12576 		goto fail_unknown;
12577 	}
12578 #endif
12579 
12580 	/*
12581 	 * For Disk devices, if it doesn't support UDMA mode, we would
12582 	 * like to return failure directly.
12583 	 */
12584 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12585 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12586 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12587 		sata_log(sata_hba_inst, CE_WARN,
12588 		    "SATA disk device at port %d does not support UDMA",
12589 		    sdinfo->satadrv_addr.cport);
12590 		rval = SATA_FAILURE;
12591 		goto fail_unknown;
12592 	}
12593 
12594 	return (SATA_SUCCESS);
12595 
12596 fail_unknown:
12597 	/* Invalidate sata_drive_info ? */
12598 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12599 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12600 	return (rval);
12601 }
12602 
12603 /*
12604  * Log/display device information
12605  */
12606 static void
12607 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12608     sata_drive_info_t *sdinfo)
12609 {
12610 	int valid_version;
12611 	char msg_buf[MAXPATHLEN];
12612 	int i;
12613 
12614 	/* Show HBA path */
12615 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12616 
12617 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12618 
12619 	switch (sdinfo->satadrv_type) {
12620 	case SATA_DTYPE_ATADISK:
12621 		(void) sprintf(msg_buf, "SATA disk device at");
12622 		break;
12623 
12624 	case SATA_DTYPE_ATAPICD:
12625 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12626 		break;
12627 
12628 	case SATA_DTYPE_ATAPITAPE:
12629 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12630 		break;
12631 
12632 	case SATA_DTYPE_ATAPIDISK:
12633 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12634 		break;
12635 
12636 	case SATA_DTYPE_UNKNOWN:
12637 		(void) sprintf(msg_buf,
12638 		    "Unsupported SATA device type (cfg 0x%x) at ",
12639 		    sdinfo->satadrv_id.ai_config);
12640 		break;
12641 	}
12642 
12643 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12644 		cmn_err(CE_CONT, "?\t%s port %d\n",
12645 		    msg_buf, sdinfo->satadrv_addr.cport);
12646 	else
12647 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12648 		    msg_buf, sdinfo->satadrv_addr.cport,
12649 		    sdinfo->satadrv_addr.pmport);
12650 
12651 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12652 	    sizeof (sdinfo->satadrv_id.ai_model));
12653 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12654 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12655 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12656 
12657 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12658 	    sizeof (sdinfo->satadrv_id.ai_fw));
12659 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12660 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12661 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12662 
12663 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12664 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12665 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12666 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12667 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12668 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12669 	} else {
12670 		/*
12671 		 * Some drives do not implement serial number and may
12672 		 * violate the spec by providing spaces rather than zeros
12673 		 * in serial number field. Scan the buffer to detect it.
12674 		 */
12675 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12676 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12677 				break;
12678 		}
12679 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12680 			cmn_err(CE_CONT, "?\tserial number - none\n");
12681 		} else {
12682 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12683 		}
12684 	}
12685 
12686 #ifdef SATA_DEBUG
12687 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12688 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12689 		int i;
12690 		for (i = 14; i >= 2; i--) {
12691 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12692 				valid_version = i;
12693 				break;
12694 			}
12695 		}
12696 		cmn_err(CE_CONT,
12697 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12698 		    valid_version,
12699 		    sdinfo->satadrv_id.ai_majorversion,
12700 		    sdinfo->satadrv_id.ai_minorversion);
12701 	}
12702 #endif
12703 	/* Log some info */
12704 	cmn_err(CE_CONT, "?\tsupported features:\n");
12705 	msg_buf[0] = '\0';
12706 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12707 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12708 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12709 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12710 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12711 	}
12712 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12713 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12714 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12715 		(void) strlcat(msg_buf, ", Native Command Queueing",
12716 		    MAXPATHLEN);
12717 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12718 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12719 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12720 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12721 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12722 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12723 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12724 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12725 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12726 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12727 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12728 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12729 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12730 	if (sdinfo->satadrv_features_support &
12731 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12732 		msg_buf[0] = '\0';
12733 		(void) snprintf(msg_buf, MAXPATHLEN,
12734 		    "Supported queue depth %d",
12735 		    sdinfo->satadrv_queue_depth);
12736 		if (!(sata_func_enable &
12737 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12738 			(void) strlcat(msg_buf,
12739 			    " - queueing disabled globally", MAXPATHLEN);
12740 		else if (sdinfo->satadrv_queue_depth >
12741 		    sdinfo->satadrv_max_queue_depth) {
12742 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12743 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12744 			    (int)sdinfo->satadrv_max_queue_depth);
12745 		}
12746 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12747 	}
12748 
12749 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12750 #ifdef __i386
12751 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12752 		    sdinfo->satadrv_capacity);
12753 #else
12754 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12755 		    sdinfo->satadrv_capacity);
12756 #endif
12757 		cmn_err(CE_CONT, "?%s", msg_buf);
12758 	}
12759 }
12760 
12761 /*
12762  * Log/display port multiplier information
12763  * No Mutex should be hold.
12764  */
12765 static void
12766 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12767     sata_device_t *sata_device)
12768 {
12769 	_NOTE(ARGUNUSED(sata_hba_inst))
12770 
12771 	int cport = sata_device->satadev_addr.cport;
12772 	sata_pmult_info_t *pmultinfo;
12773 	char msg_buf[MAXPATHLEN];
12774 	uint32_t gscr0, gscr1, gscr2, gscr64;
12775 
12776 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12777 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12778 	if (pmultinfo == NULL) {
12779 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12780 		return;
12781 	}
12782 
12783 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12784 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12785 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12786 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12787 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12788 
12789 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12790 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12791 
12792 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12793 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12794 	cmn_err(CE_CONT, "?%s", msg_buf);
12795 
12796 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12797 	if (gscr1 & (1 << 3))
12798 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12799 	else if (gscr1 & (1 << 2))
12800 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12801 	else if (gscr1 & (1 << 1))
12802 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12803 	else
12804 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12805 	cmn_err(CE_CONT, "?%s", msg_buf);
12806 
12807 	(void) strcpy(msg_buf, "\tSupport ");
12808 	if (gscr64 & (1 << 3))
12809 		(void) strlcat(msg_buf, "Asy-Notif, ",
12810 		    MAXPATHLEN);
12811 	if (gscr64 & (1 << 2))
12812 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12813 	if (gscr64 & (1 << 1))
12814 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12815 	if (gscr64 & (1 << 0))
12816 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12817 	if ((gscr64 & 0xf) == 0)
12818 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12819 	cmn_err(CE_CONT, "?%s", msg_buf);
12820 
12821 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12822 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12823 	cmn_err(CE_CONT, "?%s", msg_buf);
12824 }
12825 
12826 /*
12827  * sata_save_drive_settings extracts current setting of the device and stores
12828  * it for future reference, in case the device setup would need to be restored
12829  * after the device reset.
12830  *
12831  * For all devices read ahead and write cache settings are saved, if the
12832  * device supports these features at all.
12833  * For ATAPI devices the Removable Media Status Notification setting is saved.
12834  */
12835 static void
12836 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12837 {
12838 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12839 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12840 
12841 		/* Current setting of Read Ahead (and Read Cache) */
12842 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12843 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12844 		else
12845 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12846 
12847 		/* Current setting of Write Cache */
12848 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12849 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12850 		else
12851 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12852 	}
12853 
12854 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12855 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12856 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12857 		else
12858 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12859 	}
12860 }
12861 
12862 
12863 /*
12864  * sata_check_capacity function determines a disk capacity
12865  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12866  *
12867  * NOTE: CHS mode is not supported! If a device does not support LBA,
12868  * this function is not called.
12869  *
12870  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12871  */
12872 static uint64_t
12873 sata_check_capacity(sata_drive_info_t *sdinfo)
12874 {
12875 	uint64_t capacity = 0;
12876 	int i;
12877 
12878 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
12879 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
12880 		/* Capacity valid only for LBA-addressable disk devices */
12881 		return (0);
12882 
12883 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
12884 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
12885 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
12886 		/* LBA48 mode supported and enabled */
12887 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
12888 		    SATA_DEV_F_LBA28;
12889 		for (i = 3;  i >= 0;  --i) {
12890 			capacity <<= 16;
12891 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
12892 		}
12893 	} else {
12894 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
12895 		capacity <<= 16;
12896 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
12897 		if (capacity >= 0x1000000)
12898 			/* LBA28 mode */
12899 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
12900 	}
12901 	return (capacity);
12902 }
12903 
12904 
12905 /*
12906  * Allocate consistent buffer for DMA transfer
12907  *
12908  * Cannot be called from interrupt level or with mutex held - it may sleep.
12909  *
12910  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
12911  */
12912 static struct buf *
12913 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
12914 {
12915 	struct scsi_address ap;
12916 	struct buf *bp;
12917 	ddi_dma_attr_t	cur_dma_attr;
12918 
12919 	ASSERT(spx->txlt_sata_pkt != NULL);
12920 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
12921 	ap.a_target = SATA_TO_SCSI_TARGET(
12922 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
12923 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
12924 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
12925 	ap.a_lun = 0;
12926 
12927 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
12928 	    B_READ, SLEEP_FUNC, NULL);
12929 
12930 	if (bp != NULL) {
12931 		/* Allocate DMA resources for this buffer */
12932 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
12933 		/*
12934 		 * We use a local version of the dma_attr, to account
12935 		 * for a device addressing limitations.
12936 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
12937 		 * will cause dma attributes to be adjusted to a lowest
12938 		 * acceptable level.
12939 		 */
12940 		sata_adjust_dma_attr(NULL,
12941 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
12942 
12943 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
12944 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
12945 			scsi_free_consistent_buf(bp);
12946 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
12947 			bp = NULL;
12948 		}
12949 	}
12950 	return (bp);
12951 }
12952 
12953 /*
12954  * Release local buffer (consistent buffer for DMA transfer) allocated
12955  * via sata_alloc_local_buffer().
12956  */
12957 static void
12958 sata_free_local_buffer(sata_pkt_txlate_t *spx)
12959 {
12960 	ASSERT(spx->txlt_sata_pkt != NULL);
12961 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
12962 
12963 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
12964 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
12965 
12966 	sata_common_free_dma_rsrcs(spx);
12967 
12968 	/* Free buffer */
12969 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
12970 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
12971 }
12972 
12973 /*
12974  * Allocate sata_pkt
12975  * Pkt structure version and embedded strcutures version are initialized.
12976  * sata_pkt and sata_pkt_txlate structures are cross-linked.
12977  *
12978  * Since this may be called in interrupt context by sata_scsi_init_pkt,
12979  * callback argument determines if it can sleep or not.
12980  * Hence, it should not be called from interrupt context.
12981  *
12982  * If successful, non-NULL pointer to a sata pkt is returned.
12983  * Upon failure, NULL pointer is returned.
12984  */
12985 static sata_pkt_t *
12986 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
12987 {
12988 	sata_pkt_t *spkt;
12989 	int kmsflag;
12990 
12991 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
12992 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
12993 	if (spkt == NULL) {
12994 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
12995 		    "sata_pkt_alloc: failed"));
12996 		return (NULL);
12997 	}
12998 	spkt->satapkt_rev = SATA_PKT_REV;
12999 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13000 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13001 	spkt->satapkt_framework_private = spx;
13002 	spx->txlt_sata_pkt = spkt;
13003 	return (spkt);
13004 }
13005 
13006 /*
13007  * Free sata pkt allocated via sata_pkt_alloc()
13008  */
13009 static void
13010 sata_pkt_free(sata_pkt_txlate_t *spx)
13011 {
13012 	ASSERT(spx->txlt_sata_pkt != NULL);
13013 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13014 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13015 	spx->txlt_sata_pkt = NULL;
13016 }
13017 
13018 
13019 /*
13020  * Adjust DMA attributes.
13021  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13022  * from 8 bits to 16 bits, depending on a command being used.
13023  * Limiting max block count arbitrarily to 256 for all read/write
13024  * commands may affects performance, so check both the device and
13025  * controller capability before adjusting dma attributes.
13026  */
13027 void
13028 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13029     ddi_dma_attr_t *adj_dma_attr)
13030 {
13031 	uint32_t count_max;
13032 
13033 	/* Copy original attributes */
13034 	*adj_dma_attr = *dma_attr;
13035 	/*
13036 	 * Things to consider: device addressing capability,
13037 	 * "excessive" controller DMA capabilities.
13038 	 * If a device is being probed/initialized, there are
13039 	 * no device info - use default limits then.
13040 	 */
13041 	if (sdinfo == NULL) {
13042 		count_max = dma_attr->dma_attr_granular * 0x100;
13043 		if (dma_attr->dma_attr_count_max > count_max)
13044 			adj_dma_attr->dma_attr_count_max = count_max;
13045 		if (dma_attr->dma_attr_maxxfer > count_max)
13046 			adj_dma_attr->dma_attr_maxxfer = count_max;
13047 		return;
13048 	}
13049 
13050 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13051 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13052 			/*
13053 			 * 16-bit sector count may be used - we rely on
13054 			 * the assumption that only read and write cmds
13055 			 * will request more than 256 sectors worth of data
13056 			 */
13057 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13058 		} else {
13059 			/*
13060 			 * 8-bit sector count will be used - default limits
13061 			 * for dma attributes
13062 			 */
13063 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13064 		}
13065 		/*
13066 		 * Adjust controler dma attributes, if necessary
13067 		 */
13068 		if (dma_attr->dma_attr_count_max > count_max)
13069 			adj_dma_attr->dma_attr_count_max = count_max;
13070 		if (dma_attr->dma_attr_maxxfer > count_max)
13071 			adj_dma_attr->dma_attr_maxxfer = count_max;
13072 	}
13073 }
13074 
13075 
13076 /*
13077  * Allocate DMA resources for the buffer
13078  * This function handles initial DMA resource allocation as well as
13079  * DMA window shift and may be called repeatedly for the same DMA window
13080  * until all DMA cookies in the DMA window are processed.
13081  * To guarantee that there is always a coherent set of cookies to process
13082  * by SATA HBA driver (observing alignment, device granularity, etc.),
13083  * the number of slots for DMA cookies is equal to lesser of  a number of
13084  * cookies in a DMA window and a max number of scatter/gather entries.
13085  *
13086  * Returns DDI_SUCCESS upon successful operation.
13087  * Return failure code of a failing command or DDI_FAILURE when
13088  * internal cleanup failed.
13089  */
13090 static int
13091 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13092     int (*callback)(caddr_t), caddr_t arg,
13093     ddi_dma_attr_t *cur_dma_attr)
13094 {
13095 	int	rval;
13096 	off_t	offset;
13097 	size_t	size;
13098 	int	max_sg_len, req_len, i;
13099 	uint_t	dma_flags;
13100 	struct buf	*bp;
13101 	uint64_t	cur_txfer_len;
13102 
13103 
13104 	ASSERT(spx->txlt_sata_pkt != NULL);
13105 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13106 	ASSERT(bp != NULL);
13107 
13108 
13109 	if (spx->txlt_buf_dma_handle == NULL) {
13110 		/*
13111 		 * No DMA resources allocated so far - this is a first call
13112 		 * for this sata pkt.
13113 		 */
13114 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13115 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13116 
13117 		if (rval != DDI_SUCCESS) {
13118 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13119 			    "sata_dma_buf_setup: no buf DMA resources %x",
13120 			    rval));
13121 			return (rval);
13122 		}
13123 
13124 		if (bp->b_flags & B_READ)
13125 			dma_flags = DDI_DMA_READ;
13126 		else
13127 			dma_flags = DDI_DMA_WRITE;
13128 
13129 		if (flags & PKT_CONSISTENT)
13130 			dma_flags |= DDI_DMA_CONSISTENT;
13131 
13132 		if (flags & PKT_DMA_PARTIAL)
13133 			dma_flags |= DDI_DMA_PARTIAL;
13134 
13135 		/*
13136 		 * Check buffer alignment and size against dma attributes
13137 		 * Consider dma_attr_align only. There may be requests
13138 		 * with the size lower than device granularity, but they
13139 		 * will not read/write from/to the device, so no adjustment
13140 		 * is necessary. The dma_attr_minxfer theoretically should
13141 		 * be considered, but no HBA driver is checking it.
13142 		 */
13143 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13144 		    cur_dma_attr->dma_attr_align)) {
13145 			rval = ddi_dma_buf_bind_handle(
13146 			    spx->txlt_buf_dma_handle,
13147 			    bp, dma_flags, callback, arg,
13148 			    &spx->txlt_dma_cookie,
13149 			    &spx->txlt_curwin_num_dma_cookies);
13150 		} else { /* Buffer is not aligned */
13151 
13152 			int	(*ddicallback)(caddr_t);
13153 			size_t	bufsz;
13154 
13155 			/* Check id sleeping is allowed */
13156 			ddicallback = (callback == NULL_FUNC) ?
13157 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13158 
13159 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13160 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13161 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13162 
13163 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13164 				/*
13165 				 * CPU will need to access data in the buffer
13166 				 * (for copying) so map it.
13167 				 */
13168 				bp_mapin(bp);
13169 
13170 			ASSERT(spx->txlt_tmp_buf == NULL);
13171 
13172 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13173 			rval = ddi_dma_mem_alloc(
13174 			    spx->txlt_buf_dma_handle,
13175 			    bp->b_bcount,
13176 			    &sata_acc_attr,
13177 			    DDI_DMA_STREAMING,
13178 			    ddicallback, NULL,
13179 			    &spx->txlt_tmp_buf,
13180 			    &bufsz,
13181 			    &spx->txlt_tmp_buf_handle);
13182 
13183 			if (rval != DDI_SUCCESS) {
13184 				/* DMA mapping failed */
13185 				(void) ddi_dma_free_handle(
13186 				    &spx->txlt_buf_dma_handle);
13187 				spx->txlt_buf_dma_handle = NULL;
13188 #ifdef SATA_DEBUG
13189 				mbuffail_count++;
13190 #endif
13191 				SATADBG1(SATA_DBG_DMA_SETUP,
13192 				    spx->txlt_sata_hba_inst,
13193 				    "sata_dma_buf_setup: "
13194 				    "buf dma mem alloc failed %x\n", rval);
13195 				return (rval);
13196 			}
13197 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13198 			    cur_dma_attr->dma_attr_align));
13199 
13200 #ifdef SATA_DEBUG
13201 			mbuf_count++;
13202 
13203 			if (bp->b_bcount != bufsz)
13204 				/*
13205 				 * This will require special handling, because
13206 				 * DMA cookies will be based on the temporary
13207 				 * buffer size, not the original buffer
13208 				 * b_bcount, so the residue may have to
13209 				 * be counted differently.
13210 				 */
13211 				SATADBG2(SATA_DBG_DMA_SETUP,
13212 				    spx->txlt_sata_hba_inst,
13213 				    "sata_dma_buf_setup: bp size %x != "
13214 				    "bufsz %x\n", bp->b_bcount, bufsz);
13215 #endif
13216 			if (dma_flags & DDI_DMA_WRITE) {
13217 				/*
13218 				 * Write operation - copy data into
13219 				 * an aligned temporary buffer. Buffer will be
13220 				 * synced for device by ddi_dma_addr_bind_handle
13221 				 */
13222 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13223 				    bp->b_bcount);
13224 			}
13225 
13226 			rval = ddi_dma_addr_bind_handle(
13227 			    spx->txlt_buf_dma_handle,
13228 			    NULL,
13229 			    spx->txlt_tmp_buf,
13230 			    bufsz, dma_flags, ddicallback, 0,
13231 			    &spx->txlt_dma_cookie,
13232 			    &spx->txlt_curwin_num_dma_cookies);
13233 		}
13234 
13235 		switch (rval) {
13236 		case DDI_DMA_PARTIAL_MAP:
13237 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13238 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13239 			/*
13240 			 * Partial DMA mapping.
13241 			 * Retrieve number of DMA windows for this request.
13242 			 */
13243 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13244 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13245 				if (spx->txlt_tmp_buf != NULL) {
13246 					ddi_dma_mem_free(
13247 					    &spx->txlt_tmp_buf_handle);
13248 					spx->txlt_tmp_buf = NULL;
13249 				}
13250 				(void) ddi_dma_unbind_handle(
13251 				    spx->txlt_buf_dma_handle);
13252 				(void) ddi_dma_free_handle(
13253 				    &spx->txlt_buf_dma_handle);
13254 				spx->txlt_buf_dma_handle = NULL;
13255 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13256 				    "sata_dma_buf_setup: numwin failed\n"));
13257 				return (DDI_FAILURE);
13258 			}
13259 			SATADBG2(SATA_DBG_DMA_SETUP,
13260 			    spx->txlt_sata_hba_inst,
13261 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13262 			    spx->txlt_num_dma_win,
13263 			    spx->txlt_curwin_num_dma_cookies);
13264 			spx->txlt_cur_dma_win = 0;
13265 			break;
13266 
13267 		case DDI_DMA_MAPPED:
13268 			/* DMA fully mapped */
13269 			spx->txlt_num_dma_win = 1;
13270 			spx->txlt_cur_dma_win = 0;
13271 			SATADBG1(SATA_DBG_DMA_SETUP,
13272 			    spx->txlt_sata_hba_inst,
13273 			    "sata_dma_buf_setup: windows: 1 "
13274 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13275 			break;
13276 
13277 		default:
13278 			/* DMA mapping failed */
13279 			if (spx->txlt_tmp_buf != NULL) {
13280 				ddi_dma_mem_free(
13281 				    &spx->txlt_tmp_buf_handle);
13282 				spx->txlt_tmp_buf = NULL;
13283 			}
13284 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13285 			spx->txlt_buf_dma_handle = NULL;
13286 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13287 			    "sata_dma_buf_setup: buf dma handle binding "
13288 			    "failed %x\n", rval));
13289 			return (rval);
13290 		}
13291 		spx->txlt_curwin_processed_dma_cookies = 0;
13292 		spx->txlt_dma_cookie_list = NULL;
13293 	} else {
13294 		/*
13295 		 * DMA setup is reused. Check if we need to process more
13296 		 * cookies in current window, or to get next window, if any.
13297 		 */
13298 
13299 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13300 		    spx->txlt_curwin_num_dma_cookies);
13301 
13302 		if (spx->txlt_curwin_processed_dma_cookies ==
13303 		    spx->txlt_curwin_num_dma_cookies) {
13304 			/*
13305 			 * All cookies from current DMA window were processed.
13306 			 * Get next DMA window.
13307 			 */
13308 			spx->txlt_cur_dma_win++;
13309 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13310 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13311 				    spx->txlt_cur_dma_win, &offset, &size,
13312 				    &spx->txlt_dma_cookie,
13313 				    &spx->txlt_curwin_num_dma_cookies);
13314 				spx->txlt_curwin_processed_dma_cookies = 0;
13315 			} else {
13316 				/* No more windows! End of request! */
13317 				/* What to do? - panic for now */
13318 				ASSERT(spx->txlt_cur_dma_win >=
13319 				    spx->txlt_num_dma_win);
13320 
13321 				spx->txlt_curwin_num_dma_cookies = 0;
13322 				spx->txlt_curwin_processed_dma_cookies = 0;
13323 				spx->txlt_sata_pkt->
13324 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13325 				return (DDI_SUCCESS);
13326 			}
13327 		}
13328 	}
13329 	/* There better be at least one DMA cookie outstanding */
13330 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13331 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13332 
13333 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13334 		/* The default cookie slot was used in previous run */
13335 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13336 		spx->txlt_dma_cookie_list = NULL;
13337 		spx->txlt_dma_cookie_list_len = 0;
13338 	}
13339 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13340 		/*
13341 		 * Processing a new DMA window - set-up dma cookies list.
13342 		 * We may reuse previously allocated cookie array if it is
13343 		 * possible.
13344 		 */
13345 		if (spx->txlt_dma_cookie_list != NULL &&
13346 		    spx->txlt_dma_cookie_list_len <
13347 		    spx->txlt_curwin_num_dma_cookies) {
13348 			/*
13349 			 * New DMA window contains more cookies than
13350 			 * the previous one. We need larger cookie list - free
13351 			 * the old one.
13352 			 */
13353 			(void) kmem_free(spx->txlt_dma_cookie_list,
13354 			    spx->txlt_dma_cookie_list_len *
13355 			    sizeof (ddi_dma_cookie_t));
13356 			spx->txlt_dma_cookie_list = NULL;
13357 			spx->txlt_dma_cookie_list_len = 0;
13358 		}
13359 		if (spx->txlt_dma_cookie_list == NULL) {
13360 			/*
13361 			 * Calculate lesser of number of cookies in this
13362 			 * DMA window and number of s/g entries.
13363 			 */
13364 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13365 			req_len = MIN(max_sg_len,
13366 			    spx->txlt_curwin_num_dma_cookies);
13367 
13368 			/* Allocate new dma cookie array if necessary */
13369 			if (req_len == 1) {
13370 				/* Only one cookie - no need for a list */
13371 				spx->txlt_dma_cookie_list =
13372 				    &spx->txlt_dma_cookie;
13373 				spx->txlt_dma_cookie_list_len = 1;
13374 			} else {
13375 				/*
13376 				 * More than one cookie - try to allocate space.
13377 				 */
13378 				spx->txlt_dma_cookie_list = kmem_zalloc(
13379 				    sizeof (ddi_dma_cookie_t) * req_len,
13380 				    callback == NULL_FUNC ? KM_NOSLEEP :
13381 				    KM_SLEEP);
13382 				if (spx->txlt_dma_cookie_list == NULL) {
13383 					SATADBG1(SATA_DBG_DMA_SETUP,
13384 					    spx->txlt_sata_hba_inst,
13385 					    "sata_dma_buf_setup: cookie list "
13386 					    "allocation failed\n", NULL);
13387 					/*
13388 					 * We could not allocate space for
13389 					 * neccessary number of dma cookies in
13390 					 * this window, so we fail this request.
13391 					 * Next invocation would try again to
13392 					 * allocate space for cookie list.
13393 					 * Note:Packet residue was not modified.
13394 					 */
13395 					return (DDI_DMA_NORESOURCES);
13396 				} else {
13397 					spx->txlt_dma_cookie_list_len = req_len;
13398 				}
13399 			}
13400 		}
13401 		/*
13402 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13403 		 * First cookie was already fetched.
13404 		 */
13405 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13406 		cur_txfer_len =
13407 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13408 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13409 		spx->txlt_curwin_processed_dma_cookies++;
13410 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13411 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13412 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13413 			    &spx->txlt_dma_cookie_list[i]);
13414 			cur_txfer_len +=
13415 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13416 			spx->txlt_curwin_processed_dma_cookies++;
13417 			spx->txlt_sata_pkt->
13418 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13419 		}
13420 	} else {
13421 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13422 		    "sata_dma_buf_setup: sliding within DMA window, "
13423 		    "cur cookie %d, total cookies %d\n",
13424 		    spx->txlt_curwin_processed_dma_cookies,
13425 		    spx->txlt_curwin_num_dma_cookies);
13426 
13427 		/*
13428 		 * Not all cookies from the current dma window were used because
13429 		 * of s/g limitation.
13430 		 * There is no need to re-size the list - it was set at
13431 		 * optimal size, or only default entry is used (s/g = 1).
13432 		 */
13433 		if (spx->txlt_dma_cookie_list == NULL) {
13434 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13435 			spx->txlt_dma_cookie_list_len = 1;
13436 		}
13437 		/*
13438 		 * Since we are processing remaining cookies in a DMA window,
13439 		 * there may be less of them than the number of entries in the
13440 		 * current dma cookie list.
13441 		 */
13442 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13443 		    (spx->txlt_curwin_num_dma_cookies -
13444 		    spx->txlt_curwin_processed_dma_cookies));
13445 
13446 		/* Fetch the next batch of cookies */
13447 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13448 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13449 			    &spx->txlt_dma_cookie_list[i]);
13450 			cur_txfer_len +=
13451 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13452 			spx->txlt_sata_pkt->
13453 			    satapkt_cmd.satacmd_num_dma_cookies++;
13454 			spx->txlt_curwin_processed_dma_cookies++;
13455 		}
13456 	}
13457 
13458 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13459 
13460 	/* Point sata_cmd to the cookie list */
13461 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13462 	    &spx->txlt_dma_cookie_list[0];
13463 
13464 	/* Remember number of DMA cookies passed in sata packet */
13465 	spx->txlt_num_dma_cookies =
13466 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13467 
13468 	ASSERT(cur_txfer_len != 0);
13469 	if (cur_txfer_len <= bp->b_bcount)
13470 		spx->txlt_total_residue -= cur_txfer_len;
13471 	else {
13472 		/*
13473 		 * Temporary DMA buffer has been padded by
13474 		 * ddi_dma_mem_alloc()!
13475 		 * This requires special handling, because DMA cookies are
13476 		 * based on the temporary buffer size, not the b_bcount,
13477 		 * and we have extra bytes to transfer - but the packet
13478 		 * residue has to stay correct because we will copy only
13479 		 * the requested number of bytes.
13480 		 */
13481 		spx->txlt_total_residue -= bp->b_bcount;
13482 	}
13483 
13484 	return (DDI_SUCCESS);
13485 }
13486 
13487 /*
13488  * Common routine for releasing DMA resources
13489  */
13490 static void
13491 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13492 {
13493 	if (spx->txlt_buf_dma_handle != NULL) {
13494 		if (spx->txlt_tmp_buf != NULL)  {
13495 			/*
13496 			 * Intermediate DMA buffer was allocated.
13497 			 * Free allocated buffer and associated access handle.
13498 			 */
13499 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13500 			spx->txlt_tmp_buf = NULL;
13501 		}
13502 		/*
13503 		 * Free DMA resources - cookies and handles
13504 		 */
13505 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13506 		if (spx->txlt_dma_cookie_list != NULL) {
13507 			if (spx->txlt_dma_cookie_list !=
13508 			    &spx->txlt_dma_cookie) {
13509 				(void) kmem_free(spx->txlt_dma_cookie_list,
13510 				    spx->txlt_dma_cookie_list_len *
13511 				    sizeof (ddi_dma_cookie_t));
13512 				spx->txlt_dma_cookie_list = NULL;
13513 			}
13514 		}
13515 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13516 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13517 		spx->txlt_buf_dma_handle = NULL;
13518 	}
13519 }
13520 
13521 /*
13522  * Free DMA resources
13523  * Used by the HBA driver to release DMA resources that it does not use.
13524  *
13525  * Returns Void
13526  */
13527 void
13528 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13529 {
13530 	sata_pkt_txlate_t *spx;
13531 
13532 	if (sata_pkt == NULL)
13533 		return;
13534 
13535 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13536 
13537 	sata_common_free_dma_rsrcs(spx);
13538 }
13539 
13540 /*
13541  * Fetch Device Identify data.
13542  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13543  * command to a device and get the device identify data.
13544  * The device_info structure has to be set to device type (for selecting proper
13545  * device identify command).
13546  *
13547  * Returns:
13548  * SATA_SUCCESS if cmd succeeded
13549  * SATA_RETRY if cmd was rejected and could be retried,
13550  * SATA_FAILURE if cmd failed and should not be retried (port error)
13551  *
13552  * Cannot be called in an interrupt context.
13553  */
13554 
13555 static int
13556 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13557     sata_drive_info_t *sdinfo)
13558 {
13559 	struct buf *bp;
13560 	sata_pkt_t *spkt;
13561 	sata_cmd_t *scmd;
13562 	sata_pkt_txlate_t *spx;
13563 	int rval;
13564 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13565 
13566 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13567 	spx->txlt_sata_hba_inst = sata_hba_inst;
13568 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13569 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13570 	if (spkt == NULL) {
13571 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13572 		return (SATA_RETRY); /* may retry later */
13573 	}
13574 	/* address is needed now */
13575 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13576 
13577 	/*
13578 	 * Allocate buffer for Identify Data return data
13579 	 */
13580 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13581 	if (bp == NULL) {
13582 		sata_pkt_free(spx);
13583 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13584 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13585 		    "sata_fetch_device_identify_data: "
13586 		    "cannot allocate buffer for ID"));
13587 		return (SATA_RETRY); /* may retry later */
13588 	}
13589 
13590 	/* Fill sata_pkt */
13591 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13592 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13593 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13594 	/* Synchronous mode, no callback */
13595 	spkt->satapkt_comp = NULL;
13596 	/* Timeout 30s */
13597 	spkt->satapkt_time = sata_default_pkt_time;
13598 
13599 	scmd = &spkt->satapkt_cmd;
13600 	scmd->satacmd_bp = bp;
13601 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13602 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13603 
13604 	/* Build Identify Device cmd in the sata_pkt */
13605 	scmd->satacmd_addr_type = 0;		/* N/A */
13606 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13607 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13608 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13609 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13610 	scmd->satacmd_features_reg = 0;		/* N/A */
13611 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13612 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13613 		/* Identify Packet Device cmd */
13614 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13615 	} else {
13616 		/* Identify Device cmd - mandatory for all other devices */
13617 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13618 	}
13619 
13620 	/* Send pkt to SATA HBA driver */
13621 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13622 
13623 #ifdef SATA_INJECT_FAULTS
13624 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13625 #endif
13626 
13627 	if (rval == SATA_TRAN_ACCEPTED &&
13628 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13629 		if (spx->txlt_buf_dma_handle != NULL) {
13630 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13631 			    DDI_DMA_SYNC_FORKERNEL);
13632 			ASSERT(rval == DDI_SUCCESS);
13633 			if (sata_check_for_dma_error(dip, spx)) {
13634 				ddi_fm_service_impact(dip,
13635 				    DDI_SERVICE_UNAFFECTED);
13636 				rval = SATA_RETRY;
13637 				goto fail;
13638 			}
13639 
13640 		}
13641 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13642 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13643 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13644 			    "SATA disk device at port %d - "
13645 			    "partial Identify Data",
13646 			    sdinfo->satadrv_addr.cport));
13647 			rval = SATA_RETRY; /* may retry later */
13648 			goto fail;
13649 		}
13650 		/* Update sata_drive_info */
13651 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13652 		    sizeof (sata_id_t));
13653 
13654 		sdinfo->satadrv_features_support = 0;
13655 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13656 			/*
13657 			 * Retrieve capacity (disks only) and addressing mode
13658 			 */
13659 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13660 		} else {
13661 			/*
13662 			 * For ATAPI devices one would have to issue
13663 			 * Get Capacity cmd for media capacity. Not here.
13664 			 */
13665 			sdinfo->satadrv_capacity = 0;
13666 			/*
13667 			 * Check what cdb length is supported
13668 			 */
13669 			if ((sdinfo->satadrv_id.ai_config &
13670 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13671 				sdinfo->satadrv_atapi_cdb_len = 16;
13672 			else
13673 				sdinfo->satadrv_atapi_cdb_len = 12;
13674 		}
13675 		/* Setup supported features flags */
13676 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13677 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13678 
13679 		/* Check for SATA GEN and NCQ support */
13680 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13681 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13682 			/* SATA compliance */
13683 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13684 				sdinfo->satadrv_features_support |=
13685 				    SATA_DEV_F_NCQ;
13686 			if (sdinfo->satadrv_id.ai_satacap &
13687 			    (SATA_1_SPEED | SATA_2_SPEED)) {
13688 				if (sdinfo->satadrv_id.ai_satacap &
13689 				    SATA_2_SPEED)
13690 					sdinfo->satadrv_features_support |=
13691 					    SATA_DEV_F_SATA2;
13692 				if (sdinfo->satadrv_id.ai_satacap &
13693 				    SATA_1_SPEED)
13694 					sdinfo->satadrv_features_support |=
13695 					    SATA_DEV_F_SATA1;
13696 			} else {
13697 				sdinfo->satadrv_features_support |=
13698 				    SATA_DEV_F_SATA1;
13699 			}
13700 		}
13701 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13702 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13703 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13704 
13705 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13706 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13707 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13708 			++sdinfo->satadrv_queue_depth;
13709 			/* Adjust according to controller capabilities */
13710 			sdinfo->satadrv_max_queue_depth = MIN(
13711 			    sdinfo->satadrv_queue_depth,
13712 			    SATA_QDEPTH(sata_hba_inst));
13713 			/* Adjust according to global queue depth limit */
13714 			sdinfo->satadrv_max_queue_depth = MIN(
13715 			    sdinfo->satadrv_max_queue_depth,
13716 			    sata_current_max_qdepth);
13717 			if (sdinfo->satadrv_max_queue_depth == 0)
13718 				sdinfo->satadrv_max_queue_depth = 1;
13719 		} else
13720 			sdinfo->satadrv_max_queue_depth = 1;
13721 
13722 		rval = SATA_SUCCESS;
13723 	} else {
13724 		/*
13725 		 * Woops, no Identify Data.
13726 		 */
13727 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13728 			rval = SATA_RETRY; /* may retry later */
13729 		} else if (rval == SATA_TRAN_ACCEPTED) {
13730 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13731 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13732 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13733 			    spkt->satapkt_reason == SATA_PKT_RESET)
13734 				rval = SATA_RETRY; /* may retry later */
13735 			else
13736 				rval = SATA_FAILURE;
13737 		} else {
13738 			rval = SATA_FAILURE;
13739 		}
13740 	}
13741 fail:
13742 	/* Free allocated resources */
13743 	sata_free_local_buffer(spx);
13744 	sata_pkt_free(spx);
13745 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13746 
13747 	return (rval);
13748 }
13749 
13750 
13751 /*
13752  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13753  * UDMA mode is checked first, followed by MWDMA mode.
13754  * set correctly, so this function is setting it to the highest supported level.
13755  * Older SATA spec required that the device supports at least DMA 4 mode and
13756  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13757  * restriction has been removed.
13758  *
13759  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13760  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13761  *
13762  * NOTE: This function should be called only if DMA mode is supported.
13763  */
13764 static int
13765 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13766 {
13767 	sata_pkt_t *spkt;
13768 	sata_cmd_t *scmd;
13769 	sata_pkt_txlate_t *spx;
13770 	int i, mode;
13771 	uint8_t subcmd;
13772 	int rval = SATA_SUCCESS;
13773 
13774 	ASSERT(sdinfo != NULL);
13775 	ASSERT(sata_hba_inst != NULL);
13776 
13777 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13778 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13779 		/* Find highest Ultra DMA mode supported */
13780 		for (mode = 6; mode >= 0; --mode) {
13781 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13782 				break;
13783 		}
13784 #if 0
13785 		/* Left for historical reasons */
13786 		/*
13787 		 * Some initial version of SATA spec indicated that at least
13788 		 * UDMA mode 4 has to be supported. It is not mentioned in
13789 		 * SerialATA 2.6, so this restriction is removed.
13790 		 */
13791 		if (mode < 4)
13792 			return (SATA_FAILURE);
13793 #endif
13794 
13795 		/*
13796 		 * For disk, we're still going to set DMA mode whatever is
13797 		 * selected by default
13798 		 *
13799 		 * We saw an old maxtor sata drive will select Ultra DMA and
13800 		 * Multi-Word DMA simultaneouly by default, which is going
13801 		 * to cause DMA command timed out, so we need to select DMA
13802 		 * mode even when it's already done by default
13803 		 */
13804 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13805 
13806 			/* Find UDMA mode currently selected */
13807 			for (i = 6; i >= 0; --i) {
13808 				if (sdinfo->satadrv_id.ai_ultradma &
13809 				    (1 << (i + 8)))
13810 					break;
13811 			}
13812 			if (i >= mode)
13813 				/* Nothing to do */
13814 				return (SATA_SUCCESS);
13815 		}
13816 
13817 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13818 
13819 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13820 		/* Find highest MultiWord DMA mode supported */
13821 		for (mode = 2; mode >= 0; --mode) {
13822 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13823 				break;
13824 		}
13825 
13826 		/*
13827 		 * For disk, We're still going to set DMA mode whatever is
13828 		 * selected by default
13829 		 *
13830 		 * We saw an old maxtor sata drive will select Ultra DMA and
13831 		 * Multi-Word DMA simultaneouly by default, which is going
13832 		 * to cause DMA command timed out, so we need to select DMA
13833 		 * mode even when it's already done by default
13834 		 */
13835 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13836 
13837 			/* Find highest MultiWord DMA mode selected */
13838 			for (i = 2; i >= 0; --i) {
13839 				if (sdinfo->satadrv_id.ai_dworddma &
13840 				    (1 << (i + 8)))
13841 					break;
13842 			}
13843 			if (i >= mode)
13844 				/* Nothing to do */
13845 				return (SATA_SUCCESS);
13846 		}
13847 
13848 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13849 	} else
13850 		return (SATA_SUCCESS);
13851 
13852 	/*
13853 	 * Set DMA mode via SET FEATURES COMMAND.
13854 	 * Prepare packet for SET FEATURES COMMAND.
13855 	 */
13856 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13857 	spx->txlt_sata_hba_inst = sata_hba_inst;
13858 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13859 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13860 	if (spkt == NULL) {
13861 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13862 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
13863 		rval = SATA_FAILURE;
13864 		goto done;
13865 	}
13866 	/* Fill sata_pkt */
13867 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13868 	/* Timeout 30s */
13869 	spkt->satapkt_time = sata_default_pkt_time;
13870 	/* Synchronous mode, no callback, interrupts */
13871 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13872 	spkt->satapkt_comp = NULL;
13873 	scmd = &spkt->satapkt_cmd;
13874 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13875 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13876 	scmd->satacmd_addr_type = 0;
13877 	scmd->satacmd_device_reg = 0;
13878 	scmd->satacmd_status_reg = 0;
13879 	scmd->satacmd_error_reg = 0;
13880 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
13881 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
13882 	scmd->satacmd_sec_count_lsb = subcmd | mode;
13883 
13884 	/* Transfer command to HBA */
13885 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
13886 	    spkt) != SATA_TRAN_ACCEPTED ||
13887 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13888 		/* Pkt execution failed */
13889 		rval = SATA_FAILURE;
13890 	}
13891 done:
13892 
13893 	/* Free allocated resources */
13894 	if (spkt != NULL)
13895 		sata_pkt_free(spx);
13896 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
13897 
13898 	return (rval);
13899 }
13900 
13901 
13902 /*
13903  * Set device caching mode.
13904  * One of the following operations should be specified:
13905  * SATAC_SF_ENABLE_READ_AHEAD
13906  * SATAC_SF_DISABLE_READ_AHEAD
13907  * SATAC_SF_ENABLE_WRITE_CACHE
13908  * SATAC_SF_DISABLE_WRITE_CACHE
13909  *
13910  * If operation fails, system log messgage is emitted.
13911  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
13912  * command was sent but did not succeed, and SATA_FAILURE otherwise.
13913  */
13914 
13915 static int
13916 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
13917     int cache_op)
13918 {
13919 	sata_pkt_t *spkt;
13920 	sata_cmd_t *scmd;
13921 	sata_pkt_txlate_t *spx;
13922 	int rval = SATA_SUCCESS;
13923 	int hba_rval;
13924 	char *infop;
13925 
13926 	ASSERT(sdinfo != NULL);
13927 	ASSERT(sata_hba_inst != NULL);
13928 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
13929 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
13930 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
13931 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
13932 
13933 
13934 	/* Prepare packet for SET FEATURES COMMAND */
13935 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13936 	spx->txlt_sata_hba_inst = sata_hba_inst;
13937 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13938 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13939 	if (spkt == NULL) {
13940 		rval = SATA_FAILURE;
13941 		goto failure;
13942 	}
13943 	/* Fill sata_pkt */
13944 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13945 	/* Timeout 30s */
13946 	spkt->satapkt_time = sata_default_pkt_time;
13947 	/* Synchronous mode, no callback, interrupts */
13948 	spkt->satapkt_op_mode =
13949 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13950 	spkt->satapkt_comp = NULL;
13951 	scmd = &spkt->satapkt_cmd;
13952 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13953 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13954 	scmd->satacmd_addr_type = 0;
13955 	scmd->satacmd_device_reg = 0;
13956 	scmd->satacmd_status_reg = 0;
13957 	scmd->satacmd_error_reg = 0;
13958 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
13959 	scmd->satacmd_features_reg = cache_op;
13960 
13961 	/* Transfer command to HBA */
13962 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
13963 	    SATA_DIP(sata_hba_inst), spkt);
13964 
13965 #ifdef SATA_INJECT_FAULTS
13966 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13967 #endif
13968 
13969 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
13970 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
13971 		/* Pkt execution failed */
13972 		switch (cache_op) {
13973 		case SATAC_SF_ENABLE_READ_AHEAD:
13974 			infop = "enabling read ahead failed";
13975 			break;
13976 		case SATAC_SF_DISABLE_READ_AHEAD:
13977 			infop = "disabling read ahead failed";
13978 			break;
13979 		case SATAC_SF_ENABLE_WRITE_CACHE:
13980 			infop = "enabling write cache failed";
13981 			break;
13982 		case SATAC_SF_DISABLE_WRITE_CACHE:
13983 			infop = "disabling write cache failed";
13984 			break;
13985 		}
13986 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
13987 		rval = SATA_RETRY;
13988 	}
13989 failure:
13990 	/* Free allocated resources */
13991 	if (spkt != NULL)
13992 		sata_pkt_free(spx);
13993 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
13994 	return (rval);
13995 }
13996 
13997 /*
13998  * Set Removable Media Status Notification (enable/disable)
13999  * state == 0 , disable
14000  * state != 0 , enable
14001  *
14002  * If operation fails, system log messgage is emitted.
14003  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14004  */
14005 
14006 static int
14007 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14008     int state)
14009 {
14010 	sata_pkt_t *spkt;
14011 	sata_cmd_t *scmd;
14012 	sata_pkt_txlate_t *spx;
14013 	int rval = SATA_SUCCESS;
14014 	char *infop;
14015 
14016 	ASSERT(sdinfo != NULL);
14017 	ASSERT(sata_hba_inst != NULL);
14018 
14019 	/* Prepare packet for SET FEATURES COMMAND */
14020 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14021 	spx->txlt_sata_hba_inst = sata_hba_inst;
14022 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14023 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14024 	if (spkt == NULL) {
14025 		rval = SATA_FAILURE;
14026 		goto failure;
14027 	}
14028 	/* Fill sata_pkt */
14029 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14030 	/* Timeout 30s */
14031 	spkt->satapkt_time = sata_default_pkt_time;
14032 	/* Synchronous mode, no callback, interrupts */
14033 	spkt->satapkt_op_mode =
14034 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14035 	spkt->satapkt_comp = NULL;
14036 	scmd = &spkt->satapkt_cmd;
14037 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14038 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14039 	scmd->satacmd_addr_type = 0;
14040 	scmd->satacmd_device_reg = 0;
14041 	scmd->satacmd_status_reg = 0;
14042 	scmd->satacmd_error_reg = 0;
14043 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14044 	if (state == 0)
14045 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14046 	else
14047 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14048 
14049 	/* Transfer command to HBA */
14050 	if (((*SATA_START_FUNC(sata_hba_inst))(
14051 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14052 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14053 		/* Pkt execution failed */
14054 		if (state == 0)
14055 			infop = "disabling Removable Media Status "
14056 			    "Notification failed";
14057 		else
14058 			infop = "enabling Removable Media Status "
14059 			    "Notification failed";
14060 
14061 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14062 		rval = SATA_FAILURE;
14063 	}
14064 failure:
14065 	/* Free allocated resources */
14066 	if (spkt != NULL)
14067 		sata_pkt_free(spx);
14068 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14069 	return (rval);
14070 }
14071 
14072 
14073 /*
14074  * Update state and copy port ss* values from passed sata_device structure.
14075  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14076  * configuration struct.
14077  *
14078  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14079  * regardless of the state in device argument.
14080  *
14081  * Port mutex should be held while calling this function.
14082  */
14083 static void
14084 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14085     sata_device_t *sata_device)
14086 {
14087 	sata_cport_info_t *cportinfo;
14088 
14089 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14090 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14091 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14092 		    sata_device->satadev_addr.cport)
14093 			return;
14094 
14095 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14096 		    sata_device->satadev_addr.cport);
14097 
14098 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14099 		cportinfo->cport_scr = sata_device->satadev_scr;
14100 
14101 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14102 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14103 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14104 		cportinfo->cport_state |=
14105 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14106 	}
14107 }
14108 
14109 void
14110 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14111     sata_device_t *sata_device)
14112 {
14113 	sata_pmport_info_t *pmportinfo;
14114 
14115 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14116 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14117 	    SATA_NUM_PMPORTS(sata_hba_inst,
14118 	    sata_device->satadev_addr.cport) <
14119 	    sata_device->satadev_addr.pmport) {
14120 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14121 		    "sata_update_port_info: error address %p.",
14122 		    &sata_device->satadev_addr);
14123 		return;
14124 	}
14125 
14126 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14127 	    sata_device->satadev_addr.cport,
14128 	    sata_device->satadev_addr.pmport);
14129 
14130 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14131 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14132 
14133 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14134 	pmportinfo->pmport_state &=
14135 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14136 	pmportinfo->pmport_state |=
14137 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14138 }
14139 
14140 /*
14141  * Extract SATA port specification from an IOCTL argument.
14142  *
14143  * This function return the port the user land send us as is, unless it
14144  * cannot retrieve port spec, then -1 is returned.
14145  *
14146  * Support port multiplier.
14147  */
14148 static int32_t
14149 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14150 {
14151 	int32_t port;
14152 
14153 	/* Extract port number from nvpair in dca structure  */
14154 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14155 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14156 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14157 		    port));
14158 		port = -1;
14159 	}
14160 
14161 	return (port);
14162 }
14163 
14164 /*
14165  * Get dev_info_t pointer to the device node pointed to by port argument.
14166  * NOTE: target argument is a value used in ioctls to identify
14167  * the AP - it is not a sata_address.
14168  * It is a combination of cport, pmport and address qualifier, encodded same
14169  * way as a scsi target number.
14170  * At this moment it carries only cport number.
14171  *
14172  * PMult hotplug is supported now.
14173  *
14174  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14175  */
14176 
14177 static dev_info_t *
14178 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14179 {
14180 	dev_info_t	*cdip = NULL;
14181 	int		target, tgt;
14182 	int 		circ;
14183 	uint8_t		qual;
14184 
14185 	sata_hba_inst_t	*sata_hba_inst;
14186 	scsi_hba_tran_t *scsi_hba_tran;
14187 
14188 	/* Get target id */
14189 	scsi_hba_tran = ddi_get_driver_private(dip);
14190 	if (scsi_hba_tran == NULL)
14191 		return (NULL);
14192 
14193 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14194 
14195 	if (sata_hba_inst == NULL)
14196 		return (NULL);
14197 
14198 	/* Identify a port-mult by cport_info.cport_dev_type */
14199 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14200 		qual = SATA_ADDR_DPMPORT;
14201 	else
14202 		qual = SATA_ADDR_DCPORT;
14203 
14204 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14205 
14206 	/* Retrieve target dip */
14207 	ndi_devi_enter(dip, &circ);
14208 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14209 		dev_info_t *next = ddi_get_next_sibling(cdip);
14210 
14211 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14212 		    DDI_PROP_DONTPASS, "target", -1);
14213 		if (tgt == -1) {
14214 			/*
14215 			 * This is actually an error condition, but not
14216 			 * a fatal one. Just continue the search.
14217 			 */
14218 			cdip = next;
14219 			continue;
14220 		}
14221 
14222 		if (tgt == target)
14223 			break;
14224 
14225 		cdip = next;
14226 	}
14227 	ndi_devi_exit(dip, circ);
14228 
14229 	return (cdip);
14230 }
14231 
14232 /*
14233  * Get dev_info_t pointer to the device node pointed to by port argument.
14234  * NOTE: target argument is a value used in ioctls to identify
14235  * the AP - it is not a sata_address.
14236  * It is a combination of cport, pmport and address qualifier, encoded same
14237  * way as a scsi target number.
14238  *
14239  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14240  */
14241 
14242 static dev_info_t *
14243 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14244 {
14245 	dev_info_t	*cdip = NULL;
14246 	int		target, tgt;
14247 	int 		circ;
14248 
14249 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14250 
14251 	ndi_devi_enter(dip, &circ);
14252 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14253 		dev_info_t *next = ddi_get_next_sibling(cdip);
14254 
14255 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14256 		    DDI_PROP_DONTPASS, "target", -1);
14257 		if (tgt == -1) {
14258 			/*
14259 			 * This is actually an error condition, but not
14260 			 * a fatal one. Just continue the search.
14261 			 */
14262 			cdip = next;
14263 			continue;
14264 		}
14265 
14266 		if (tgt == target)
14267 			break;
14268 
14269 		cdip = next;
14270 	}
14271 	ndi_devi_exit(dip, circ);
14272 
14273 	return (cdip);
14274 }
14275 
14276 /*
14277  * Process sata port disconnect request.
14278  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14279  * before this request. Nevertheless, if a device is still configured,
14280  * we need to attempt to offline and unconfigure device.
14281  * Regardless of the unconfigure operation results the port is marked as
14282  * deactivated and no access to the attached device is possible.
14283  * If the target node remains because unconfigure operation failed, its state
14284  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14285  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14286  * the device and remove old target node.
14287  *
14288  * This function invokes sata_hba_inst->satahba_tran->
14289  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14290  * If successful, the device structure (if any) attached to the specified port
14291  * is removed and state of the port marked appropriately.
14292  * Failure of the port_deactivate may keep port in the physically active state,
14293  * or may fail the port.
14294  *
14295  * NOTE: Port multiplier is supported.
14296  */
14297 
14298 static int
14299 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14300     sata_device_t *sata_device)
14301 {
14302 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14303 	sata_cport_info_t *cportinfo = NULL;
14304 	sata_pmport_info_t *pmportinfo = NULL;
14305 	sata_pmult_info_t *pmultinfo = NULL;
14306 	sata_device_t subsdevice;
14307 	int cport, pmport, qual;
14308 	int rval = SATA_SUCCESS;
14309 	int npmport = 0;
14310 	int rv = 0;
14311 
14312 	cport = sata_device->satadev_addr.cport;
14313 	pmport = sata_device->satadev_addr.pmport;
14314 	qual = sata_device->satadev_addr.qual;
14315 
14316 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14317 	if (qual == SATA_ADDR_DCPORT)
14318 		qual = SATA_ADDR_CPORT;
14319 	else
14320 		qual = SATA_ADDR_PMPORT;
14321 
14322 	/*
14323 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14324 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14325 	 * Do the sanity check.
14326 	 */
14327 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14328 		/* No physical port deactivation supported. */
14329 		return (EINVAL);
14330 	}
14331 
14332 	/* Check the current state of the port */
14333 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14334 	    (SATA_DIP(sata_hba_inst), sata_device);
14335 
14336 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14337 
14338 	/*
14339 	 * Processing port mulitiplier
14340 	 */
14341 	if (qual == SATA_ADDR_CPORT &&
14342 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14343 		mutex_enter(&cportinfo->cport_mutex);
14344 
14345 		/* Check controller port status */
14346 		sata_update_port_info(sata_hba_inst, sata_device);
14347 		if (rval != SATA_SUCCESS ||
14348 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14349 			/*
14350 			 * Device port status is unknown or it is in failed
14351 			 * state
14352 			 */
14353 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14354 			    SATA_PSTATE_FAILED;
14355 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14356 			    "sata_hba_ioctl: connect: failed to deactivate "
14357 			    "SATA port %d", cport);
14358 			mutex_exit(&cportinfo->cport_mutex);
14359 			return (EIO);
14360 		}
14361 
14362 		/* Disconnect all sub-devices. */
14363 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14364 		if (pmultinfo != NULL) {
14365 
14366 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14367 			    sata_hba_inst, cport); npmport ++) {
14368 				subsdinfo = SATA_PMPORT_DRV_INFO(
14369 				    sata_hba_inst, cport, npmport);
14370 				if (subsdinfo == NULL)
14371 					continue;
14372 
14373 				subsdevice.satadev_addr = subsdinfo->
14374 				    satadrv_addr;
14375 
14376 				mutex_exit(&cportinfo->cport_mutex);
14377 				if (sata_ioctl_disconnect(sata_hba_inst,
14378 				    &subsdevice) == SATA_SUCCESS) {
14379 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14380 					"[Remove] device at port %d:%d "
14381 					"successfully.", cport, npmport);
14382 				}
14383 				mutex_enter(&cportinfo->cport_mutex);
14384 			}
14385 		}
14386 
14387 		/* Disconnect the port multiplier */
14388 		cportinfo->cport_state &= ~SATA_STATE_READY;
14389 		mutex_exit(&cportinfo->cport_mutex);
14390 
14391 		sata_device->satadev_addr.qual = qual;
14392 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14393 		    (SATA_DIP(sata_hba_inst), sata_device);
14394 
14395 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14396 		    SE_NO_HINT);
14397 
14398 		mutex_enter(&cportinfo->cport_mutex);
14399 		sata_update_port_info(sata_hba_inst, sata_device);
14400 		if (rval != SATA_SUCCESS &&
14401 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14402 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14403 			rv = EIO;
14404 		} else {
14405 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14406 		}
14407 		mutex_exit(&cportinfo->cport_mutex);
14408 
14409 		return (rv);
14410 	}
14411 
14412 	/*
14413 	 * Process non-port-multiplier device - it could be a drive connected
14414 	 * to a port multiplier port or a controller port.
14415 	 */
14416 	if (qual == SATA_ADDR_PMPORT) {
14417 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14418 		mutex_enter(&pmportinfo->pmport_mutex);
14419 		sata_update_pmport_info(sata_hba_inst, sata_device);
14420 		if (rval != SATA_SUCCESS ||
14421 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14422 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14423 			    SATA_PSTATE_FAILED;
14424 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14425 			    "sata_hba_ioctl: connect: failed to deactivate "
14426 			    "SATA port %d:%d", cport, pmport);
14427 			mutex_exit(&pmportinfo->pmport_mutex);
14428 			return (EIO);
14429 		}
14430 
14431 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14432 			sdinfo = pmportinfo->pmport_sata_drive;
14433 			ASSERT(sdinfo != NULL);
14434 		}
14435 
14436 		/*
14437 		 * Set port's dev_state to not ready - this will disable
14438 		 * an access to a potentially attached device.
14439 		 */
14440 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14441 
14442 		/* Remove and release sata_drive info structure. */
14443 		if (sdinfo != NULL) {
14444 			if ((sdinfo->satadrv_type &
14445 			    SATA_VALID_DEV_TYPE) != 0) {
14446 				/*
14447 				 * If a target node exists, try to offline
14448 				 * a device and remove target node.
14449 				 */
14450 				mutex_exit(&pmportinfo->pmport_mutex);
14451 				(void) sata_offline_device(sata_hba_inst,
14452 				    sata_device, sdinfo);
14453 				mutex_enter(&pmportinfo->pmport_mutex);
14454 			}
14455 
14456 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14457 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14458 			(void) kmem_free((void *)sdinfo,
14459 			    sizeof (sata_drive_info_t));
14460 		}
14461 		mutex_exit(&pmportinfo->pmport_mutex);
14462 
14463 	} else if (qual == SATA_ADDR_CPORT) {
14464 		mutex_enter(&cportinfo->cport_mutex);
14465 		sata_update_port_info(sata_hba_inst, sata_device);
14466 		if (rval != SATA_SUCCESS ||
14467 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14468 			/*
14469 			 * Device port status is unknown or it is in failed
14470 			 * state
14471 			 */
14472 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14473 			    SATA_PSTATE_FAILED;
14474 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14475 			    "sata_hba_ioctl: connect: failed to deactivate "
14476 			    "SATA port %d", cport);
14477 			mutex_exit(&cportinfo->cport_mutex);
14478 			return (EIO);
14479 		}
14480 
14481 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14482 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14483 			ASSERT(pmultinfo != NULL);
14484 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14485 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14486 			ASSERT(sdinfo != NULL);
14487 		}
14488 		cportinfo->cport_state &= ~SATA_STATE_READY;
14489 
14490 		if (sdinfo != NULL) {
14491 			if ((sdinfo->satadrv_type &
14492 			    SATA_VALID_DEV_TYPE) != 0) {
14493 				/*
14494 				 * If a target node exists, try to offline
14495 				 * a device and remove target node.
14496 				 */
14497 				mutex_exit(&cportinfo->cport_mutex);
14498 				(void) sata_offline_device(sata_hba_inst,
14499 				    sata_device, sdinfo);
14500 				mutex_enter(&cportinfo->cport_mutex);
14501 			}
14502 
14503 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14504 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14505 			(void) kmem_free((void *)sdinfo,
14506 			    sizeof (sata_drive_info_t));
14507 		}
14508 		mutex_exit(&cportinfo->cport_mutex);
14509 	}
14510 
14511 	/* Just ask HBA driver to deactivate port */
14512 	sata_device->satadev_addr.qual = qual;
14513 
14514 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14515 	    (SATA_DIP(sata_hba_inst), sata_device);
14516 
14517 	/*
14518 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14519 	 * without the hint (to force listener to investivate the state).
14520 	 */
14521 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14522 	    SE_NO_HINT);
14523 
14524 	if (qual == SATA_ADDR_PMPORT) {
14525 		mutex_enter(&pmportinfo->pmport_mutex);
14526 		sata_update_pmport_info(sata_hba_inst, sata_device);
14527 
14528 		if (rval != SATA_SUCCESS &&
14529 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14530 			/*
14531 			 * Port deactivation failure - do not change port
14532 			 * state unless the state returned by HBA indicates a
14533 			 * port failure.
14534 			 *
14535 			 * NOTE: device structures were released, so devices
14536 			 * now are invisible! Port reset is needed to
14537 			 * re-enumerate devices.
14538 			 */
14539 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14540 			rv = EIO;
14541 		} else {
14542 			/*
14543 			 * Deactivation succeded. From now on the sata framework
14544 			 * will not care what is happening to the device, until
14545 			 * the port is activated again.
14546 			 */
14547 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14548 		}
14549 		mutex_exit(&pmportinfo->pmport_mutex);
14550 	} else if (qual == SATA_ADDR_CPORT) {
14551 		mutex_enter(&cportinfo->cport_mutex);
14552 		sata_update_port_info(sata_hba_inst, sata_device);
14553 
14554 		if (rval != SATA_SUCCESS &&
14555 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14556 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14557 			rv = EIO;
14558 		} else {
14559 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14560 		}
14561 		mutex_exit(&cportinfo->cport_mutex);
14562 	}
14563 
14564 	return (rv);
14565 }
14566 
14567 
14568 
14569 /*
14570  * Process sata port connect request
14571  * The sata cfgadm pluging will invoke this operation only if port was found
14572  * in the disconnect state (failed state is also treated as the disconnected
14573  * state).
14574  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14575  * sata_tran_hotplug_ops->sata_tran_port_activate().
14576  * If successful and a device is found attached to the port,
14577  * the initialization sequence is executed to attach a device structure to
14578  * a port structure. The state of the port and a device would be set
14579  * appropriately.
14580  * The device is not set in configured state (system-wise) by this operation.
14581  *
14582  * Note, that activating the port may generate link events,
14583  * so it is important that following processing and the
14584  * event processing does not interfere with each other!
14585  *
14586  * This operation may remove port failed state and will
14587  * try to make port active and in good standing.
14588  *
14589  * NOTE: Port multiplier is supported.
14590  */
14591 
14592 static int
14593 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14594     sata_device_t *sata_device)
14595 {
14596 	sata_pmport_info_t	*pmportinfo = NULL;
14597 	uint8_t cport, pmport, qual;
14598 	int rv = 0;
14599 
14600 	cport = sata_device->satadev_addr.cport;
14601 	pmport = sata_device->satadev_addr.pmport;
14602 	qual = sata_device->satadev_addr.qual;
14603 
14604 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14605 	if (qual == SATA_ADDR_DCPORT)
14606 		qual = SATA_ADDR_CPORT;
14607 	else
14608 		qual = SATA_ADDR_PMPORT;
14609 
14610 	if (qual == SATA_ADDR_PMPORT)
14611 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14612 
14613 	/*
14614 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14615 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14616 	 * Perform sanity check now.
14617 	 */
14618 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14619 		/* No physical port activation supported. */
14620 		return (EINVAL);
14621 	}
14622 
14623 	/* Just ask HBA driver to activate port */
14624 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14625 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14626 		/*
14627 		 * Port activation failure.
14628 		 */
14629 		if (qual == SATA_ADDR_CPORT) {
14630 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14631 			    cport)->cport_mutex);
14632 			sata_update_port_info(sata_hba_inst, sata_device);
14633 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14634 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14635 				    SATA_PSTATE_FAILED;
14636 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14637 				    "sata_hba_ioctl: connect: failed to "
14638 				    "activate SATA port %d", cport);
14639 			}
14640 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14641 			    cport)->cport_mutex);
14642 		} else { /* port multiplier device port */
14643 			mutex_enter(&pmportinfo->pmport_mutex);
14644 			sata_update_pmport_info(sata_hba_inst, sata_device);
14645 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14646 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14647 				    pmport) = SATA_PSTATE_FAILED;
14648 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14649 				    "sata_hba_ioctl: connect: failed to "
14650 				    "activate SATA port %d:%d", cport, pmport);
14651 			}
14652 			mutex_exit(&pmportinfo->pmport_mutex);
14653 		}
14654 		return (EIO);
14655 	}
14656 
14657 	/* Virgin port state - will be updated by the port re-probe. */
14658 	if (qual == SATA_ADDR_CPORT) {
14659 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14660 		    cport)->cport_mutex);
14661 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14662 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14663 		    cport)->cport_mutex);
14664 	} else { /* port multiplier device port */
14665 		mutex_enter(&pmportinfo->pmport_mutex);
14666 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14667 		mutex_exit(&pmportinfo->pmport_mutex);
14668 	}
14669 
14670 	/*
14671 	 * Probe the port to find its state and attached device.
14672 	 */
14673 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14674 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14675 		rv = EIO;
14676 
14677 	/*
14678 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14679 	 * without the hint
14680 	 */
14681 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14682 	    SE_NO_HINT);
14683 
14684 	/*
14685 	 * If there is a device attached to the port, emit
14686 	 * a message.
14687 	 */
14688 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14689 
14690 		if (qual == SATA_ADDR_CPORT) {
14691 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14692 				sata_log(sata_hba_inst, CE_WARN,
14693 				    "SATA port multiplier detected "
14694 				    "at port %d", cport);
14695 			} else {
14696 				sata_log(sata_hba_inst, CE_WARN,
14697 				    "SATA device detected at port %d", cport);
14698 				if (sata_device->satadev_type ==
14699 				    SATA_DTYPE_UNKNOWN) {
14700 				/*
14701 				 * A device was not successfully identified
14702 				 */
14703 				sata_log(sata_hba_inst, CE_WARN,
14704 				    "Could not identify SATA "
14705 				    "device at port %d", cport);
14706 				}
14707 			}
14708 		} else { /* port multiplier device port */
14709 			sata_log(sata_hba_inst, CE_WARN,
14710 			    "SATA device detected at port %d:%d",
14711 			    cport, pmport);
14712 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14713 				/*
14714 				 * A device was not successfully identified
14715 				 */
14716 				sata_log(sata_hba_inst, CE_WARN,
14717 				    "Could not identify SATA "
14718 				    "device at port %d:%d", cport, pmport);
14719 			}
14720 		}
14721 	}
14722 
14723 	return (rv);
14724 }
14725 
14726 
14727 /*
14728  * Process sata device unconfigure request.
14729  * The unconfigure operation uses generic nexus operation to
14730  * offline a device. It leaves a target device node attached.
14731  * and obviously sata_drive_info attached as well, because
14732  * from the hardware point of view nothing has changed.
14733  */
14734 static int
14735 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14736     sata_device_t *sata_device)
14737 {
14738 	int rv = 0;
14739 	dev_info_t *tdip;
14740 
14741 	/* We are addressing attached device, not a port */
14742 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14743 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14744 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14745 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14746 
14747 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14748 	    &sata_device->satadev_addr)) != NULL) {
14749 
14750 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14751 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14752 			    "sata_hba_ioctl: unconfigure: "
14753 			    "failed to unconfigure device at SATA port %d:%d",
14754 			    sata_device->satadev_addr.cport,
14755 			    sata_device->satadev_addr.pmport));
14756 			rv = EIO;
14757 		}
14758 		/*
14759 		 * The target node devi_state should be marked with
14760 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14761 		 * This would be the indication for cfgadm that
14762 		 * the AP node occupant state is 'unconfigured'.
14763 		 */
14764 
14765 	} else {
14766 		/*
14767 		 * This would indicate a failure on the part of cfgadm
14768 		 * to detect correct state of the node prior to this
14769 		 * call - one cannot unconfigure non-existing device.
14770 		 */
14771 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14772 		    "sata_hba_ioctl: unconfigure: "
14773 		    "attempt to unconfigure non-existing device "
14774 		    "at SATA port %d:%d",
14775 		    sata_device->satadev_addr.cport,
14776 		    sata_device->satadev_addr.pmport));
14777 		rv = ENXIO;
14778 	}
14779 	return (rv);
14780 }
14781 
14782 /*
14783  * Process sata device configure request
14784  * If port is in a failed state, operation is aborted - one has to use
14785  * an explicit connect or port activate request to try to get a port into
14786  * non-failed mode. Port reset wil also work in such situation.
14787  * If the port is in disconnected (shutdown) state, the connect operation is
14788  * attempted prior to any other action.
14789  * When port is in the active state, there is a device attached and the target
14790  * node exists, a device was most likely offlined.
14791  * If target node does not exist, a new target node is created. In both cases
14792  * an attempt is made to online (configure) the device.
14793  *
14794  * NOTE: Port multiplier is supported.
14795  */
14796 static int
14797 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14798     sata_device_t *sata_device)
14799 {
14800 	int cport, pmport, qual;
14801 	int rval;
14802 	boolean_t target = B_TRUE;
14803 	sata_cport_info_t *cportinfo;
14804 	sata_pmport_info_t *pmportinfo = NULL;
14805 	dev_info_t *tdip;
14806 	sata_drive_info_t *sdinfo;
14807 
14808 	cport = sata_device->satadev_addr.cport;
14809 	pmport = sata_device->satadev_addr.pmport;
14810 	qual = sata_device->satadev_addr.qual;
14811 
14812 	/* Get current port state */
14813 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14814 	    (SATA_DIP(sata_hba_inst), sata_device);
14815 
14816 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14817 	if (qual == SATA_ADDR_DPMPORT) {
14818 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14819 		mutex_enter(&pmportinfo->pmport_mutex);
14820 		sata_update_pmport_info(sata_hba_inst, sata_device);
14821 		if (rval != SATA_SUCCESS ||
14822 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14823 			/*
14824 			 * Obviously, device on a failed port is not visible
14825 			 */
14826 			mutex_exit(&pmportinfo->pmport_mutex);
14827 			return (ENXIO);
14828 		}
14829 		mutex_exit(&pmportinfo->pmport_mutex);
14830 	} else {
14831 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14832 		    cport)->cport_mutex);
14833 		sata_update_port_info(sata_hba_inst, sata_device);
14834 		if (rval != SATA_SUCCESS ||
14835 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14836 			/*
14837 			 * Obviously, device on a failed port is not visible
14838 			 */
14839 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14840 			    cport)->cport_mutex);
14841 			return (ENXIO);
14842 		}
14843 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14844 		    cport)->cport_mutex);
14845 	}
14846 
14847 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14848 		/* need to activate port */
14849 		target = B_FALSE;
14850 
14851 		/* Sanity check */
14852 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14853 			return (ENXIO);
14854 
14855 		/* Just let HBA driver to activate port */
14856 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14857 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14858 			/*
14859 			 * Port activation failure - do not change port state
14860 			 * unless the state returned by HBA indicates a port
14861 			 * failure.
14862 			 */
14863 			if (qual == SATA_ADDR_DPMPORT) {
14864 				mutex_enter(&pmportinfo->pmport_mutex);
14865 				sata_update_pmport_info(sata_hba_inst,
14866 				    sata_device);
14867 				if (sata_device->satadev_state &
14868 				    SATA_PSTATE_FAILED)
14869 					pmportinfo->pmport_state =
14870 					    SATA_PSTATE_FAILED;
14871 				mutex_exit(&pmportinfo->pmport_mutex);
14872 			} else {
14873 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14874 				    cport)->cport_mutex);
14875 				sata_update_port_info(sata_hba_inst,
14876 				    sata_device);
14877 				if (sata_device->satadev_state &
14878 				    SATA_PSTATE_FAILED)
14879 					cportinfo->cport_state =
14880 					    SATA_PSTATE_FAILED;
14881 				mutex_exit(&SATA_CPORT_INFO(
14882 				    sata_hba_inst, cport)->cport_mutex);
14883 			}
14884 		}
14885 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14886 		    "sata_hba_ioctl: configure: "
14887 		    "failed to activate SATA port %d:%d",
14888 		    cport, pmport));
14889 		return (EIO);
14890 	}
14891 	/*
14892 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14893 	 * without the hint.
14894 	 */
14895 	sata_gen_sysevent(sata_hba_inst,
14896 	    &sata_device->satadev_addr, SE_NO_HINT);
14897 
14898 	/* Virgin port state */
14899 	if (qual == SATA_ADDR_DPMPORT) {
14900 		mutex_enter(&pmportinfo->pmport_mutex);
14901 		pmportinfo->pmport_state = 0;
14902 		mutex_exit(&pmportinfo->pmport_mutex);
14903 	} else {
14904 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14905 		    cport)-> cport_mutex);
14906 		cportinfo->cport_state = 0;
14907 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14908 		    cport)->cport_mutex);
14909 	}
14910 	/*
14911 	 * Always reprobe port, to get current device info.
14912 	 */
14913 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14914 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14915 		return (EIO);
14916 
14917 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
14918 		if (qual == SATA_ADDR_DPMPORT) {
14919 			/*
14920 			 * That's the transition from "inactive" port
14921 			 * to active one with device attached.
14922 			 */
14923 			sata_log(sata_hba_inst, CE_WARN,
14924 			    "SATA device detected at port %d:%d",
14925 			    cport, pmport);
14926 		} else {
14927 			/*
14928 			 * When PM is attached to the cport and cport is
14929 			 * activated, every PM device port needs to be reprobed.
14930 			 * We need to emit message for all devices detected
14931 			 * at port multiplier's device ports.
14932 			 * Add such code here.
14933 			 * For now, just inform about device attached to
14934 			 * cport.
14935 			 */
14936 			sata_log(sata_hba_inst, CE_WARN,
14937 			    "SATA device detected at port %d", cport);
14938 		}
14939 	}
14940 
14941 	/*
14942 	 * This is where real configuration operation starts.
14943 	 *
14944 	 * When PM is attached to the cport and cport is activated,
14945 	 * devices attached PM device ports may have to be configured
14946 	 * explicitly. This may change when port multiplier is supported.
14947 	 * For now, configure only disks and other valid target devices.
14948 	 */
14949 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
14950 		if (qual == SATA_ADDR_DCPORT) {
14951 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14952 				/*
14953 				 * A device was not successfully identified
14954 				 */
14955 				sata_log(sata_hba_inst, CE_WARN,
14956 				    "Could not identify SATA "
14957 				    "device at port %d", cport);
14958 			}
14959 		} else { /* port multiplier device port */
14960 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14961 				/*
14962 				 * A device was not successfully identified
14963 				 */
14964 				sata_log(sata_hba_inst, CE_WARN,
14965 				    "Could not identify SATA "
14966 				    "device at port %d:%d", cport, pmport);
14967 			}
14968 		}
14969 		return (ENXIO);		/* No device to configure */
14970 	}
14971 
14972 	/*
14973 	 * Here we may have a device in reset condition,
14974 	 * but because we are just configuring it, there is
14975 	 * no need to process the reset other than just
14976 	 * to clear device reset condition in the HBA driver.
14977 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
14978 	 * cause a first command sent the HBA driver with the request
14979 	 * to clear device reset condition.
14980 	 */
14981 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14982 	if (qual == SATA_ADDR_DPMPORT)
14983 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14984 	else
14985 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14986 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
14987 	if (sdinfo == NULL) {
14988 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14989 		return (ENXIO);
14990 	}
14991 	if (sdinfo->satadrv_event_flags &
14992 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14993 		sdinfo->satadrv_event_flags = 0;
14994 	}
14995 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14996 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14997 
14998 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14999 	    &sata_device->satadev_addr)) != NULL) {
15000 		/*
15001 		 * Target node exists. Verify, that it belongs
15002 		 * to existing, attached device and not to
15003 		 * a removed device.
15004 		 */
15005 		if (sata_check_device_removed(tdip) == B_TRUE) {
15006 			if (qual == SATA_ADDR_DPMPORT)
15007 				sata_log(sata_hba_inst, CE_WARN,
15008 				    "SATA device at port %d cannot be "
15009 				    "configured. "
15010 				    "Application(s) accessing "
15011 				    "previously attached device "
15012 				    "have to release it before newly "
15013 				    "inserted device can be made accessible.",
15014 				    cport);
15015 			else
15016 				sata_log(sata_hba_inst, CE_WARN,
15017 				    "SATA device at port %d:%d cannot be"
15018 				    "configured. "
15019 				    "Application(s) accessing "
15020 				    "previously attached device "
15021 				    "have to release it before newly "
15022 				    "inserted device can be made accessible.",
15023 				    cport, pmport);
15024 			return (EIO);
15025 		}
15026 		/*
15027 		 * Device was not removed and re-inserted.
15028 		 * Try to online it.
15029 		 */
15030 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15031 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15032 			    "sata_hba_ioctl: configure: "
15033 			    "onlining device at SATA port "
15034 			    "%d:%d failed", cport, pmport));
15035 			return (EIO);
15036 		}
15037 
15038 		if (qual == SATA_ADDR_DPMPORT) {
15039 			mutex_enter(&pmportinfo->pmport_mutex);
15040 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15041 			mutex_exit(&pmportinfo->pmport_mutex);
15042 		} else {
15043 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15044 			    cport)->cport_mutex);
15045 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15046 			mutex_exit(&SATA_CPORT_INFO(
15047 			    sata_hba_inst, cport)->cport_mutex);
15048 		}
15049 	} else {
15050 		/*
15051 		 * No target node - need to create a new target node.
15052 		 */
15053 		if (qual == SATA_ADDR_DPMPORT) {
15054 			mutex_enter(&pmportinfo->pmport_mutex);
15055 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15056 			mutex_exit(&pmportinfo->pmport_mutex);
15057 		} else {
15058 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15059 			    cport_mutex);
15060 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15061 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15062 			    cport_mutex);
15063 		}
15064 
15065 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15066 		    sata_hba_inst, &sata_device->satadev_addr);
15067 		if (tdip == NULL) {
15068 			/* Configure operation failed */
15069 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15070 			    "sata_hba_ioctl: configure: "
15071 			    "configuring SATA device at port %d:%d "
15072 			    "failed", cport, pmport));
15073 			return (EIO);
15074 		}
15075 	}
15076 	return (0);
15077 }
15078 
15079 
15080 /*
15081  * Process ioctl deactivate port request.
15082  * Arbitrarily unconfigure attached device, if any.
15083  * Even if the unconfigure fails, proceed with the
15084  * port deactivation.
15085  *
15086  * NOTE: Port Multiplier is supported now.
15087  */
15088 
15089 static int
15090 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15091     sata_device_t *sata_device)
15092 {
15093 	int cport, pmport, qual;
15094 	int rval, rv = 0;
15095 	int npmport;
15096 	sata_cport_info_t *cportinfo;
15097 	sata_pmport_info_t *pmportinfo;
15098 	sata_pmult_info_t *pmultinfo;
15099 	dev_info_t *tdip;
15100 	sata_drive_info_t *sdinfo = NULL;
15101 	sata_device_t subsdevice;
15102 
15103 	/* Sanity check */
15104 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15105 		return (ENOTSUP);
15106 
15107 	cport = sata_device->satadev_addr.cport;
15108 	pmport = sata_device->satadev_addr.pmport;
15109 	qual = sata_device->satadev_addr.qual;
15110 
15111 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15112 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15113 	if (qual == SATA_ADDR_DCPORT)
15114 		qual = SATA_ADDR_CPORT;
15115 	else
15116 		qual = SATA_ADDR_PMPORT;
15117 
15118 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15119 	if (qual == SATA_ADDR_PMPORT)
15120 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15121 
15122 	/*
15123 	 * Processing port multiplier
15124 	 */
15125 	if (qual == SATA_ADDR_CPORT &&
15126 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15127 		mutex_enter(&cportinfo->cport_mutex);
15128 
15129 		/* Deactivate all sub-deices */
15130 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15131 		if (pmultinfo != NULL) {
15132 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15133 			    sata_hba_inst, cport); npmport++) {
15134 
15135 				subsdevice.satadev_addr.cport = cport;
15136 				subsdevice.satadev_addr.pmport =
15137 				    (uint8_t)npmport;
15138 				subsdevice.satadev_addr.qual =
15139 				    SATA_ADDR_DPMPORT;
15140 
15141 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15142 				    "sata_hba_ioctl: deactivate: trying to "
15143 				    "deactivate SATA port %d:%d",
15144 				    cport, npmport);
15145 
15146 				mutex_exit(&cportinfo->cport_mutex);
15147 				if (sata_ioctl_deactivate(sata_hba_inst,
15148 				    &subsdevice) == SATA_SUCCESS) {
15149 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15150 					    "[Deactivate] device at port %d:%d "
15151 					    "successfully.", cport, npmport);
15152 				}
15153 				mutex_enter(&cportinfo->cport_mutex);
15154 			}
15155 		}
15156 
15157 		/* Deactivate the port multiplier now. */
15158 		cportinfo->cport_state &= ~SATA_STATE_READY;
15159 		mutex_exit(&cportinfo->cport_mutex);
15160 
15161 		sata_device->satadev_addr.qual = qual;
15162 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15163 		    (SATA_DIP(sata_hba_inst), sata_device);
15164 
15165 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15166 		    SE_NO_HINT);
15167 
15168 		mutex_enter(&cportinfo->cport_mutex);
15169 		sata_update_port_info(sata_hba_inst, sata_device);
15170 		if (rval != SATA_SUCCESS) {
15171 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15172 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15173 			}
15174 			rv = EIO;
15175 		} else {
15176 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15177 		}
15178 		mutex_exit(&cportinfo->cport_mutex);
15179 
15180 		return (rv);
15181 	}
15182 
15183 	/*
15184 	 * Process non-port-multiplier device - it could be a drive connected
15185 	 * to a port multiplier port or a controller port.
15186 	 */
15187 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15188 	if (qual == SATA_ADDR_CPORT) {
15189 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15190 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15191 			/* deal only with valid devices */
15192 			if ((cportinfo->cport_dev_type &
15193 			    SATA_VALID_DEV_TYPE) != 0)
15194 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15195 		}
15196 		cportinfo->cport_state &= ~SATA_STATE_READY;
15197 	} else {
15198 		/* Port multiplier device port */
15199 		mutex_enter(&pmportinfo->pmport_mutex);
15200 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15201 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15202 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15203 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15204 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15205 		mutex_exit(&pmportinfo->pmport_mutex);
15206 	}
15207 
15208 	if (sdinfo != NULL) {
15209 		/*
15210 		 * If a target node exists, try to offline a device and
15211 		 * to remove a target node.
15212 		 */
15213 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15214 		    cport_mutex);
15215 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15216 		    &sata_device->satadev_addr);
15217 		if (tdip != NULL) {
15218 			/* target node exist */
15219 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15220 			    "sata_hba_ioctl: port deactivate: "
15221 			    "target node exists.", NULL);
15222 
15223 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15224 			    NDI_SUCCESS) {
15225 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15226 				    "sata_hba_ioctl: port deactivate: "
15227 				    "failed to unconfigure device at port "
15228 				    "%d:%d before deactivating the port",
15229 				    cport, pmport));
15230 				/*
15231 				 * Set DEVICE REMOVED state in the target
15232 				 * node. It will prevent an access to
15233 				 * the device even when a new device is
15234 				 * attached, until the old target node is
15235 				 * released, removed and recreated for a new
15236 				 * device.
15237 				 */
15238 				sata_set_device_removed(tdip);
15239 
15240 				/*
15241 				 * Instruct the event daemon to try the
15242 				 * target node cleanup later.
15243 				 */
15244 				sata_set_target_node_cleanup(sata_hba_inst,
15245 				    &sata_device->satadev_addr);
15246 			}
15247 		}
15248 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15249 		    cport_mutex);
15250 		/*
15251 		 * In any case, remove and release sata_drive_info
15252 		 * structure.
15253 		 */
15254 		if (qual == SATA_ADDR_CPORT) {
15255 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15256 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15257 		} else { /* port multiplier device port */
15258 			mutex_enter(&pmportinfo->pmport_mutex);
15259 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15260 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15261 			mutex_exit(&pmportinfo->pmport_mutex);
15262 		}
15263 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15264 	}
15265 
15266 	if (qual == SATA_ADDR_CPORT) {
15267 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15268 		    SATA_STATE_PROBING);
15269 	} else if (qual == SATA_ADDR_PMPORT) {
15270 		mutex_enter(&pmportinfo->pmport_mutex);
15271 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15272 		    SATA_STATE_PROBING);
15273 		mutex_exit(&pmportinfo->pmport_mutex);
15274 	}
15275 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15276 
15277 	/* Just let HBA driver to deactivate port */
15278 	sata_device->satadev_addr.qual = qual;
15279 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15280 	    (SATA_DIP(sata_hba_inst), sata_device);
15281 
15282 	/*
15283 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15284 	 * without the hint
15285 	 */
15286 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15287 	    SE_NO_HINT);
15288 
15289 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15290 	sata_update_port_info(sata_hba_inst, sata_device);
15291 	if (qual == SATA_ADDR_CPORT) {
15292 		if (rval != SATA_SUCCESS) {
15293 			/*
15294 			 * Port deactivation failure - do not change port state
15295 			 * unless the state returned by HBA indicates a port
15296 			 * failure.
15297 			 */
15298 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15299 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15300 				    SATA_PSTATE_FAILED;
15301 			}
15302 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15303 			    "sata_hba_ioctl: port deactivate: "
15304 			    "cannot deactivate SATA port %d", cport));
15305 			rv = EIO;
15306 		} else {
15307 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15308 		}
15309 	} else {
15310 		mutex_enter(&pmportinfo->pmport_mutex);
15311 		if (rval != SATA_SUCCESS) {
15312 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15313 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15314 				    pmport) = SATA_PSTATE_FAILED;
15315 			}
15316 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15317 			    "sata_hba_ioctl: port deactivate: "
15318 			    "cannot deactivate SATA port %d:%d",
15319 			    cport, pmport));
15320 			rv = EIO;
15321 		} else {
15322 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15323 		}
15324 		mutex_exit(&pmportinfo->pmport_mutex);
15325 	}
15326 
15327 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15328 
15329 	return (rv);
15330 }
15331 
15332 /*
15333  * Process ioctl port activate request.
15334  *
15335  * NOTE: Port multiplier is supported now.
15336  */
15337 static int
15338 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15339     sata_device_t *sata_device)
15340 {
15341 	int cport, pmport, qual;
15342 	sata_cport_info_t *cportinfo;
15343 	sata_pmport_info_t *pmportinfo = NULL;
15344 	boolean_t dev_existed = B_TRUE;
15345 
15346 	/* Sanity check */
15347 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15348 		return (ENOTSUP);
15349 
15350 	cport = sata_device->satadev_addr.cport;
15351 	pmport = sata_device->satadev_addr.pmport;
15352 	qual = sata_device->satadev_addr.qual;
15353 
15354 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15355 
15356 	/*
15357 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15358 	 * is a device. But what we are dealing with is port/pmport.
15359 	 */
15360 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15361 	if (qual == SATA_ADDR_DCPORT)
15362 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15363 	else
15364 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15365 
15366 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15367 	if (qual == SATA_ADDR_PMPORT) {
15368 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15369 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15370 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15371 			dev_existed = B_FALSE;
15372 	} else { /* cport */
15373 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15374 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15375 			dev_existed = B_FALSE;
15376 	}
15377 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15378 
15379 	/* Just let HBA driver to activate port, if necessary */
15380 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15381 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15382 		/*
15383 		 * Port activation failure - do not change port state unless
15384 		 * the state returned by HBA indicates a port failure.
15385 		 */
15386 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15387 		    cport)->cport_mutex);
15388 		sata_update_port_info(sata_hba_inst, sata_device);
15389 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15390 			if (qual == SATA_ADDR_PMPORT) {
15391 				mutex_enter(&pmportinfo->pmport_mutex);
15392 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15393 				mutex_exit(&pmportinfo->pmport_mutex);
15394 			} else
15395 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15396 
15397 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15398 			    cport)->cport_mutex);
15399 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15400 			    "sata_hba_ioctl: port activate: cannot activate "
15401 			    "SATA port %d:%d", cport, pmport));
15402 			return (EIO);
15403 		}
15404 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15405 	}
15406 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15407 	if (qual == SATA_ADDR_PMPORT) {
15408 		mutex_enter(&pmportinfo->pmport_mutex);
15409 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15410 		mutex_exit(&pmportinfo->pmport_mutex);
15411 	} else
15412 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15413 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15414 
15415 	/*
15416 	 * Re-probe port to find its current state and possibly attached device.
15417 	 * Port re-probing may change the cportinfo device type if device is
15418 	 * found attached.
15419 	 * If port probing failed, the device type would be set to
15420 	 * SATA_DTYPE_NONE.
15421 	 */
15422 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15423 	    SATA_DEV_IDENTIFY_RETRY);
15424 
15425 	/*
15426 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15427 	 * without the hint.
15428 	 */
15429 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15430 	    SE_NO_HINT);
15431 
15432 	if (dev_existed == B_FALSE) {
15433 		if (qual == SATA_ADDR_PMPORT &&
15434 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15435 			/*
15436 			 * That's the transition from the "inactive" port state
15437 			 * or the active port without a device attached to the
15438 			 * active port state with a device attached.
15439 			 */
15440 			sata_log(sata_hba_inst, CE_WARN,
15441 			    "SATA device detected at port %d:%d",
15442 			    cport, pmport);
15443 		} else if (qual == SATA_ADDR_CPORT &&
15444 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15445 			/*
15446 			 * That's the transition from the "inactive" port state
15447 			 * or the active port without a device attached to the
15448 			 * active port state with a device attached.
15449 			 */
15450 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15451 				sata_log(sata_hba_inst, CE_WARN,
15452 				    "SATA device detected at port %d", cport);
15453 			} else {
15454 				sata_log(sata_hba_inst, CE_WARN,
15455 				    "SATA port multiplier detected at port %d",
15456 				    cport);
15457 			}
15458 		}
15459 	}
15460 	return (0);
15461 }
15462 
15463 
15464 
15465 /*
15466  * Process ioctl reset port request.
15467  *
15468  * NOTE: Port-Multiplier is supported.
15469  */
15470 static int
15471 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15472     sata_device_t *sata_device)
15473 {
15474 	int cport, pmport, qual;
15475 	int rv = 0;
15476 
15477 	cport = sata_device->satadev_addr.cport;
15478 	pmport = sata_device->satadev_addr.pmport;
15479 	qual = sata_device->satadev_addr.qual;
15480 
15481 	/*
15482 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15483 	 * is a device. But what we are dealing with is port/pmport.
15484 	 */
15485 	if (qual == SATA_ADDR_DCPORT)
15486 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15487 	else
15488 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15489 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15490 
15491 	/* Sanity check */
15492 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15493 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15494 		    "sata_hba_ioctl: sata_hba_tran missing required "
15495 		    "function sata_tran_reset_dport"));
15496 		return (ENOTSUP);
15497 	}
15498 
15499 	/* Ask HBA to reset port */
15500 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15501 	    sata_device) != SATA_SUCCESS) {
15502 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15503 		    "sata_hba_ioctl: reset port: failed %d:%d",
15504 		    cport, pmport));
15505 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15506 		    cport_mutex);
15507 		sata_update_port_info(sata_hba_inst, sata_device);
15508 		if (qual == SATA_ADDR_CPORT)
15509 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15510 			    SATA_PSTATE_FAILED;
15511 		else {
15512 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15513 			    pmport));
15514 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15515 			    SATA_PSTATE_FAILED;
15516 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15517 			    pmport));
15518 		}
15519 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15520 		    cport_mutex);
15521 		rv = EIO;
15522 	}
15523 	/*
15524 	 * Beacuse the port was reset, it should be probed and
15525 	 * attached device reinitialized. At this point the
15526 	 * port state is unknown - it's state is HBA-specific.
15527 	 * Re-probe port to get its state.
15528 	 */
15529 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15530 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
15531 		rv = EIO;
15532 	}
15533 	return (rv);
15534 }
15535 
15536 /*
15537  * Process ioctl reset device request.
15538  *
15539  * NOTE: Port multiplier is supported.
15540  */
15541 static int
15542 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15543     sata_device_t *sata_device)
15544 {
15545 	sata_drive_info_t *sdinfo = NULL;
15546 	sata_pmult_info_t *pmultinfo = NULL;
15547 	int cport, pmport;
15548 	int rv = 0;
15549 
15550 	/* Sanity check */
15551 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15552 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15553 		    "sata_hba_ioctl: sata_hba_tran missing required "
15554 		    "function sata_tran_reset_dport"));
15555 		return (ENOTSUP);
15556 	}
15557 
15558 	cport = sata_device->satadev_addr.cport;
15559 	pmport = sata_device->satadev_addr.pmport;
15560 
15561 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15562 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15563 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15564 		    SATA_DTYPE_PMULT)
15565 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15566 			    cport_devp.cport_sata_pmult;
15567 		else
15568 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15569 			    sata_device->satadev_addr.cport);
15570 	} else { /* port multiplier */
15571 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15572 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15573 		    sata_device->satadev_addr.cport,
15574 		    sata_device->satadev_addr.pmport);
15575 	}
15576 	if (sdinfo == NULL && pmultinfo == NULL) {
15577 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15578 		return (EINVAL);
15579 	}
15580 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15581 
15582 	/* Ask HBA to reset device */
15583 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15584 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15585 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15586 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15587 		    cport, pmport));
15588 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15589 		    cport_mutex);
15590 		sata_update_port_info(sata_hba_inst, sata_device);
15591 		/*
15592 		 * Device info structure remains attached. Another device reset
15593 		 * or port disconnect/connect and re-probing is
15594 		 * needed to change it's state
15595 		 */
15596 		if (sdinfo != NULL) {
15597 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15598 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15599 		} else if (pmultinfo != NULL) {
15600 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15601 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15602 		}
15603 
15604 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15605 		rv = EIO;
15606 	}
15607 	/*
15608 	 * If attached device was a port multiplier, some extra processing
15609 	 * may be needed to bring it back. SATA specification requies a
15610 	 * mandatory software reset on host port to reliably enumerate a port
15611 	 * multiplier, the HBA driver should handle that after reset
15612 	 * operation.
15613 	 */
15614 	return (rv);
15615 }
15616 
15617 
15618 /*
15619  * Process ioctl reset all request.
15620  */
15621 static int
15622 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15623 {
15624 	sata_device_t sata_device;
15625 	int rv = 0;
15626 	int tcport;
15627 	int tpmport = 0;
15628 
15629 	sata_device.satadev_rev = SATA_DEVICE_REV;
15630 
15631 	/*
15632 	 * There is no protection here for configured devices.
15633 	 */
15634 	/* Sanity check */
15635 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15636 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15637 		    "sata_hba_ioctl: sata_hba_tran missing required "
15638 		    "function sata_tran_reset_dport"));
15639 		return (ENOTSUP);
15640 	}
15641 
15642 	/*
15643 	 * Need to lock all ports, not just one.
15644 	 * If any port is locked by event processing, fail the whole operation.
15645 	 * One port is already locked, but for simplicity lock it again.
15646 	 */
15647 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15648 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15649 		    cport_mutex);
15650 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15651 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15652 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15653 			    cport_mutex);
15654 			rv = EBUSY;
15655 			break;
15656 		} else {
15657 			/*
15658 			 * It is enough to lock cport in command-based
15659 			 * switching mode.
15660 			 */
15661 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15662 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15663 		}
15664 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15665 		    cport_mutex);
15666 	}
15667 
15668 	if (rv == 0) {
15669 		/*
15670 		 * All cports were successfully locked.
15671 		 * Reset main SATA controller.
15672 		 * Set the device address to port 0, to have a valid device
15673 		 * address.
15674 		 */
15675 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15676 		sata_device.satadev_addr.cport = 0;
15677 		sata_device.satadev_addr.pmport = 0;
15678 
15679 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15680 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15681 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15682 			    "sata_hba_ioctl: reset controller failed"));
15683 			return (EIO);
15684 		}
15685 		/*
15686 		 * Because ports were reset, port states are unknown.
15687 		 * They should be re-probed to get their state and
15688 		 * attached devices should be reinitialized.
15689 		 */
15690 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
15691 		    tcport++) {
15692 			sata_device.satadev_addr.cport = tcport;
15693 			sata_device.satadev_addr.pmport = tpmport;
15694 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
15695 
15696 			/*
15697 			 * The sata_reprobe_port() will mark a
15698 			 * SATA_EVNT_DEVICE_RESET event on the port
15699 			 * multiplier, all its sub-ports will be probed by
15700 			 * sata daemon afterwards.
15701 			 */
15702 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
15703 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15704 				rv = EIO;
15705 		}
15706 	}
15707 	/*
15708 	 * Unlock all ports
15709 	 */
15710 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15711 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15712 		    cport_mutex);
15713 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15714 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15715 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15716 		    cport_mutex);
15717 	}
15718 
15719 	/*
15720 	 * This operation returns EFAULT if either reset
15721 	 * controller failed or a re-probing of any port failed.
15722 	 */
15723 	return (rv);
15724 }
15725 
15726 
15727 /*
15728  * Process ioctl port self test request.
15729  *
15730  * NOTE: Port multiplier code is not completed nor tested.
15731  */
15732 static int
15733 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15734     sata_device_t *sata_device)
15735 {
15736 	int cport, pmport, qual;
15737 	int rv = 0;
15738 
15739 	/* Sanity check */
15740 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15741 		return (ENOTSUP);
15742 
15743 	cport = sata_device->satadev_addr.cport;
15744 	pmport = sata_device->satadev_addr.pmport;
15745 	qual = sata_device->satadev_addr.qual;
15746 
15747 	/*
15748 	 * There is no protection here for a configured
15749 	 * device attached to this port.
15750 	 */
15751 
15752 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15753 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15754 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15755 		    "sata_hba_ioctl: port selftest: "
15756 		    "failed port %d:%d", cport, pmport));
15757 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15758 		    cport_mutex);
15759 		sata_update_port_info(sata_hba_inst, sata_device);
15760 		if (qual == SATA_ADDR_CPORT)
15761 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15762 			    SATA_PSTATE_FAILED;
15763 		else { /* port multiplier device port */
15764 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15765 			    cport, pmport));
15766 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15767 			    SATA_PSTATE_FAILED;
15768 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15769 			    cport, pmport));
15770 		}
15771 
15772 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15773 		    cport_mutex);
15774 		return (EIO);
15775 	}
15776 	/*
15777 	 * Beacuse the port was reset in the course of testing, it should be
15778 	 * re-probed and attached device state should be restored. At this
15779 	 * point the port state is unknown - it's state is HBA-specific.
15780 	 * Force port re-probing to get it into a known state.
15781 	 */
15782 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15783 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15784 		rv = EIO;
15785 	return (rv);
15786 }
15787 
15788 
15789 /*
15790  * sata_cfgadm_state:
15791  * Use the sata port state and state of the target node to figure out
15792  * the cfgadm_state.
15793  *
15794  * The port argument is a value with encoded cport,
15795  * pmport and address qualifier, in the same manner as a scsi target number.
15796  * SCSI_TO_SATA_CPORT macro extracts cport number,
15797  * SCSI_TO_SATA_PMPORT extracts pmport number and
15798  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15799  *
15800  * Port multiplier is supported.
15801  */
15802 
15803 static void
15804 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15805     devctl_ap_state_t *ap_state)
15806 {
15807 	uint8_t		cport, pmport, qual;
15808 	uint32_t	port_state, pmult_state;
15809 	uint32_t	dev_type;
15810 	sata_drive_info_t *sdinfo;
15811 
15812 	cport = SCSI_TO_SATA_CPORT(port);
15813 	pmport = SCSI_TO_SATA_PMPORT(port);
15814 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15815 
15816 	/* Check cport state */
15817 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15818 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15819 	    port_state & SATA_PSTATE_FAILED) {
15820 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15821 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15822 		if (port_state & SATA_PSTATE_FAILED)
15823 			ap_state->ap_condition = AP_COND_FAILED;
15824 		else
15825 			ap_state->ap_condition = AP_COND_UNKNOWN;
15826 
15827 		return;
15828 	}
15829 
15830 	/* cport state is okay. Now check pmport state */
15831 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15832 		/* Sanity check */
15833 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15834 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15835 		    cport, pmport) == NULL)
15836 			return;
15837 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15838 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15839 		    port_state & SATA_PSTATE_FAILED) {
15840 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15841 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15842 			if (port_state & SATA_PSTATE_FAILED)
15843 				ap_state->ap_condition = AP_COND_FAILED;
15844 			else
15845 				ap_state->ap_condition = AP_COND_UNKNOWN;
15846 
15847 			return;
15848 		}
15849 	}
15850 
15851 	/* Port is enabled and ready */
15852 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15853 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15854 	else
15855 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15856 
15857 	switch (dev_type) {
15858 	case SATA_DTYPE_NONE:
15859 	{
15860 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15861 		ap_state->ap_condition = AP_COND_OK;
15862 		/* No device attached */
15863 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15864 		break;
15865 	}
15866 	case SATA_DTYPE_PMULT:
15867 	{
15868 		/* Need to check port multiplier state */
15869 		ASSERT(qual == SATA_ADDR_DCPORT);
15870 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15871 		    pmult_state;
15872 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15873 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15874 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15875 			if (pmult_state & SATA_PSTATE_FAILED)
15876 				ap_state->ap_condition = AP_COND_FAILED;
15877 			else
15878 				ap_state->ap_condition = AP_COND_UNKNOWN;
15879 
15880 			return;
15881 		}
15882 
15883 		/* Port multiplier is not configurable */
15884 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15885 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15886 		ap_state->ap_condition = AP_COND_OK;
15887 		break;
15888 	}
15889 
15890 	case SATA_DTYPE_ATADISK:
15891 	case SATA_DTYPE_ATAPICD:
15892 	case SATA_DTYPE_ATAPITAPE:
15893 	case SATA_DTYPE_ATAPIDISK:
15894 	{
15895 		dev_info_t *tdip = NULL;
15896 		dev_info_t *dip = NULL;
15897 		int circ;
15898 
15899 		dip = SATA_DIP(sata_hba_inst);
15900 		tdip = sata_get_target_dip(dip, cport, pmport);
15901 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15902 		if (tdip != NULL) {
15903 			ndi_devi_enter(dip, &circ);
15904 			mutex_enter(&(DEVI(tdip)->devi_lock));
15905 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
15906 				/*
15907 				 * There could be the case where previously
15908 				 * configured and opened device was removed
15909 				 * and unknown device was plugged.
15910 				 * In such case we want to show a device, and
15911 				 * its configured or unconfigured state but
15912 				 * indicate unusable condition untill the
15913 				 * old target node is released and removed.
15914 				 */
15915 				ap_state->ap_condition = AP_COND_UNUSABLE;
15916 			} else {
15917 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
15918 				    cport));
15919 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15920 				    cport);
15921 				if (sdinfo != NULL) {
15922 					if ((sdinfo->satadrv_state &
15923 					    SATA_DSTATE_FAILED) != 0)
15924 						ap_state->ap_condition =
15925 						    AP_COND_FAILED;
15926 					else
15927 						ap_state->ap_condition =
15928 						    AP_COND_OK;
15929 				} else {
15930 					ap_state->ap_condition =
15931 					    AP_COND_UNKNOWN;
15932 				}
15933 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
15934 				    cport));
15935 			}
15936 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
15937 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
15938 				ap_state->ap_ostate =
15939 				    AP_OSTATE_UNCONFIGURED;
15940 			} else {
15941 				ap_state->ap_ostate =
15942 				    AP_OSTATE_CONFIGURED;
15943 			}
15944 			mutex_exit(&(DEVI(tdip)->devi_lock));
15945 			ndi_devi_exit(dip, circ);
15946 		} else {
15947 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15948 			ap_state->ap_condition = AP_COND_UNKNOWN;
15949 		}
15950 		break;
15951 	}
15952 	default:
15953 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15954 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15955 		ap_state->ap_condition = AP_COND_UNKNOWN;
15956 		/*
15957 		 * This is actually internal error condition (non fatal),
15958 		 * because we have already checked all defined device types.
15959 		 */
15960 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15961 		    "sata_cfgadm_state: Internal error: "
15962 		    "unknown device type"));
15963 		break;
15964 	}
15965 }
15966 
15967 
15968 /*
15969  * Process ioctl get device path request.
15970  *
15971  * NOTE: Port multiplier has no target dip. Devices connected to port
15972  * multiplier have target node attached to the HBA node. The only difference
15973  * between them and the directly-attached device node is a target address.
15974  */
15975 static int
15976 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
15977     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
15978 {
15979 	char path[MAXPATHLEN];
15980 	uint32_t size;
15981 	dev_info_t *tdip;
15982 
15983 	(void) strcpy(path, "/devices");
15984 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15985 	    &sata_device->satadev_addr)) == NULL) {
15986 		/*
15987 		 * No such device. If this is a request for a size, do not
15988 		 * return EINVAL for non-existing target, because cfgadm
15989 		 * will then indicate a meaningless ioctl failure.
15990 		 * If this is a request for a path, indicate invalid
15991 		 * argument.
15992 		 */
15993 		if (ioc->get_size == 0)
15994 			return (EINVAL);
15995 	} else {
15996 		(void) ddi_pathname(tdip, path + strlen(path));
15997 	}
15998 	size = strlen(path) + 1;
15999 
16000 	if (ioc->get_size != 0) {
16001 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16002 		    mode) != 0)
16003 			return (EFAULT);
16004 	} else {
16005 		if (ioc->bufsiz != size)
16006 			return (EINVAL);
16007 
16008 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16009 		    mode) != 0)
16010 			return (EFAULT);
16011 	}
16012 	return (0);
16013 }
16014 
16015 /*
16016  * Process ioctl get attachment point type request.
16017  *
16018  * NOTE: Port multiplier is supported.
16019  */
16020 static	int
16021 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16022     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16023 {
16024 	uint32_t	type_len;
16025 	const char	*ap_type;
16026 	int		dev_type;
16027 
16028 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16029 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16030 		    sata_device->satadev_addr.cport);
16031 	else /* pmport */
16032 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16033 		    sata_device->satadev_addr.cport,
16034 		    sata_device->satadev_addr.pmport);
16035 
16036 	switch (dev_type) {
16037 	case SATA_DTYPE_NONE:
16038 		ap_type = "port";
16039 		break;
16040 
16041 	case SATA_DTYPE_ATADISK:
16042 	case SATA_DTYPE_ATAPIDISK:
16043 		ap_type = "disk";
16044 		break;
16045 
16046 	case SATA_DTYPE_ATAPICD:
16047 		ap_type = "cd/dvd";
16048 		break;
16049 
16050 	case SATA_DTYPE_ATAPITAPE:
16051 		ap_type = "tape";
16052 		break;
16053 
16054 	case SATA_DTYPE_PMULT:
16055 		ap_type = "sata-pmult";
16056 		break;
16057 
16058 	case SATA_DTYPE_UNKNOWN:
16059 		ap_type = "unknown";
16060 		break;
16061 
16062 	default:
16063 		ap_type = "unsupported";
16064 		break;
16065 
16066 	} /* end of dev_type switch */
16067 
16068 	type_len = strlen(ap_type) + 1;
16069 
16070 	if (ioc->get_size) {
16071 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16072 		    mode) != 0)
16073 			return (EFAULT);
16074 	} else {
16075 		if (ioc->bufsiz != type_len)
16076 			return (EINVAL);
16077 
16078 		if (ddi_copyout((void *)ap_type, ioc->buf,
16079 		    ioc->bufsiz, mode) != 0)
16080 			return (EFAULT);
16081 	}
16082 	return (0);
16083 
16084 }
16085 
16086 /*
16087  * Process ioctl get device model info request.
16088  * This operation should return to cfgadm the device model
16089  * information string
16090  *
16091  * NOTE: Port multiplier is supported.
16092  */
16093 static	int
16094 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16095     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16096 {
16097 	sata_drive_info_t *sdinfo;
16098 	uint32_t info_len;
16099 	char ap_info[SATA_ID_MODEL_LEN + 1];
16100 
16101 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16102 	    sata_device->satadev_addr.cport)->cport_mutex);
16103 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16104 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16105 		    sata_device->satadev_addr.cport);
16106 	else /* port multiplier */
16107 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16108 		    sata_device->satadev_addr.cport,
16109 		    sata_device->satadev_addr.pmport);
16110 	if (sdinfo == NULL) {
16111 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16112 		    sata_device->satadev_addr.cport)->cport_mutex);
16113 		return (EINVAL);
16114 	}
16115 
16116 #ifdef	_LITTLE_ENDIAN
16117 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16118 #else	/* _LITTLE_ENDIAN */
16119 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16120 #endif	/* _LITTLE_ENDIAN */
16121 
16122 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16123 	    sata_device->satadev_addr.cport)->cport_mutex);
16124 
16125 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16126 
16127 	info_len = strlen(ap_info) + 1;
16128 
16129 	if (ioc->get_size) {
16130 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16131 		    mode) != 0)
16132 			return (EFAULT);
16133 	} else {
16134 		if (ioc->bufsiz < info_len)
16135 			return (EINVAL);
16136 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16137 		    mode) != 0)
16138 			return (EFAULT);
16139 	}
16140 	return (0);
16141 }
16142 
16143 
16144 /*
16145  * Process ioctl get device firmware revision info request.
16146  * This operation should return to cfgadm the device firmware revision
16147  * information string
16148  *
16149  * Port multiplier is supported.
16150  */
16151 static	int
16152 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16153     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16154 {
16155 	sata_drive_info_t *sdinfo;
16156 	uint32_t info_len;
16157 	char ap_info[SATA_ID_FW_LEN + 1];
16158 
16159 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16160 	    sata_device->satadev_addr.cport)->cport_mutex);
16161 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16162 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16163 		    sata_device->satadev_addr.cport);
16164 	else /* port multiplier */
16165 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16166 		    sata_device->satadev_addr.cport,
16167 		    sata_device->satadev_addr.pmport);
16168 	if (sdinfo == NULL) {
16169 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16170 		    sata_device->satadev_addr.cport)->cport_mutex);
16171 		return (EINVAL);
16172 	}
16173 
16174 #ifdef	_LITTLE_ENDIAN
16175 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16176 #else	/* _LITTLE_ENDIAN */
16177 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16178 #endif	/* _LITTLE_ENDIAN */
16179 
16180 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16181 	    sata_device->satadev_addr.cport)->cport_mutex);
16182 
16183 	ap_info[SATA_ID_FW_LEN] = '\0';
16184 
16185 	info_len = strlen(ap_info) + 1;
16186 
16187 	if (ioc->get_size) {
16188 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16189 		    mode) != 0)
16190 			return (EFAULT);
16191 	} else {
16192 		if (ioc->bufsiz < info_len)
16193 			return (EINVAL);
16194 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16195 		    mode) != 0)
16196 			return (EFAULT);
16197 	}
16198 	return (0);
16199 }
16200 
16201 
16202 /*
16203  * Process ioctl get device serial number info request.
16204  * This operation should return to cfgadm the device serial number string.
16205  *
16206  * NOTE: Port multiplier is supported.
16207  */
16208 static	int
16209 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16210     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16211 {
16212 	sata_drive_info_t *sdinfo;
16213 	uint32_t info_len;
16214 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16215 
16216 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16217 	    sata_device->satadev_addr.cport)->cport_mutex);
16218 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16219 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16220 		    sata_device->satadev_addr.cport);
16221 	else /* port multiplier */
16222 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16223 		    sata_device->satadev_addr.cport,
16224 		    sata_device->satadev_addr.pmport);
16225 	if (sdinfo == NULL) {
16226 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16227 		    sata_device->satadev_addr.cport)->cport_mutex);
16228 		return (EINVAL);
16229 	}
16230 
16231 #ifdef	_LITTLE_ENDIAN
16232 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16233 #else	/* _LITTLE_ENDIAN */
16234 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16235 #endif	/* _LITTLE_ENDIAN */
16236 
16237 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16238 	    sata_device->satadev_addr.cport)->cport_mutex);
16239 
16240 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16241 
16242 	info_len = strlen(ap_info) + 1;
16243 
16244 	if (ioc->get_size) {
16245 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16246 		    mode) != 0)
16247 			return (EFAULT);
16248 	} else {
16249 		if (ioc->bufsiz < info_len)
16250 			return (EINVAL);
16251 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16252 		    mode) != 0)
16253 			return (EFAULT);
16254 	}
16255 	return (0);
16256 }
16257 
16258 
16259 /*
16260  * Preset scsi extended sense data (to NO SENSE)
16261  * First 18 bytes of the sense data are preset to current valid sense
16262  * with a key NO SENSE data.
16263  *
16264  * Returns void
16265  */
16266 static void
16267 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16268 {
16269 	sense->es_valid = 1;		/* Valid sense */
16270 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16271 	sense->es_key = KEY_NO_SENSE;
16272 	sense->es_info_1 = 0;
16273 	sense->es_info_2 = 0;
16274 	sense->es_info_3 = 0;
16275 	sense->es_info_4 = 0;
16276 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16277 	sense->es_cmd_info[0] = 0;
16278 	sense->es_cmd_info[1] = 0;
16279 	sense->es_cmd_info[2] = 0;
16280 	sense->es_cmd_info[3] = 0;
16281 	sense->es_add_code = 0;
16282 	sense->es_qual_code = 0;
16283 }
16284 
16285 /*
16286  * Register a legacy cmdk-style devid for the target (disk) device.
16287  *
16288  * Note: This function is called only when the HBA devinfo node has the
16289  * property "use-cmdk-devid-format" set. This property indicates that
16290  * devid compatible with old cmdk (target) driver is to be generated
16291  * for any target device attached to this controller. This will take
16292  * precedence over the devid generated by sd (target) driver.
16293  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16294  */
16295 static void
16296 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16297 {
16298 	char	*hwid;
16299 	int	modlen;
16300 	int	serlen;
16301 	int	rval;
16302 	ddi_devid_t	devid;
16303 
16304 	/*
16305 	 * device ID is a concatanation of model number, "=", serial number.
16306 	 */
16307 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16308 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16309 	    sizeof (sdinfo->satadrv_id.ai_model));
16310 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16311 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16312 	if (modlen == 0)
16313 		goto err;
16314 	hwid[modlen++] = '=';
16315 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16316 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16317 	swab(&hwid[modlen], &hwid[modlen],
16318 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16319 	serlen = sata_check_modser(&hwid[modlen],
16320 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16321 	if (serlen == 0)
16322 		goto err;
16323 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16324 
16325 	/* initialize/register devid */
16326 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16327 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16328 		rval = ddi_devid_register(dip, devid);
16329 		/*
16330 		 * Free up the allocated devid buffer.
16331 		 * NOTE: This doesn't mean unregistering devid.
16332 		 */
16333 		ddi_devid_free(devid);
16334 	}
16335 
16336 	if (rval != DDI_SUCCESS)
16337 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16338 		    " on port %d", sdinfo->satadrv_addr.cport);
16339 err:
16340 	kmem_free(hwid, LEGACY_HWID_LEN);
16341 }
16342 
16343 /*
16344  * valid model/serial string must contain a non-zero non-space characters.
16345  * trim trailing spaces/NULLs.
16346  */
16347 static int
16348 sata_check_modser(char *buf, int buf_len)
16349 {
16350 	boolean_t ret;
16351 	char *s;
16352 	int i;
16353 	int tb;
16354 	char ch;
16355 
16356 	ret = B_FALSE;
16357 	s = buf;
16358 	for (i = 0; i < buf_len; i++) {
16359 		ch = *s++;
16360 		if (ch != ' ' && ch != '\0')
16361 			tb = i + 1;
16362 		if (ch != ' ' && ch != '\0' && ch != '0')
16363 			ret = B_TRUE;
16364 	}
16365 
16366 	if (ret == B_FALSE)
16367 		return (0); /* invalid string */
16368 
16369 	return (tb); /* return length */
16370 }
16371 
16372 /*
16373  * sata_set_drive_features function compares current device features setting
16374  * with the saved device features settings and, if there is a difference,
16375  * it restores device features setting to the previously saved state.
16376  * It also arbitrarily tries to select the highest supported DMA mode.
16377  * Device Identify or Identify Packet Device data has to be current.
16378  * At the moment read ahead and write cache are considered for all devices.
16379  * For atapi devices, Removable Media Status Notification is set in addition
16380  * to common features.
16381  *
16382  * This function cannot be called in the interrupt context (it may sleep).
16383  *
16384  * The input argument sdinfo should point to the drive info structure
16385  * to be updated after features are set. Note, that only
16386  * device (packet) identify data is updated, not the flags indicating the
16387  * supported features.
16388  *
16389  * Returns SATA_SUCCESS if successful or there was nothing to do.
16390  * Device Identify data in the drive info structure pointed to by the sdinfo
16391  * arguments is updated even when no features were set or changed.
16392  *
16393  * Returns SATA_FAILURE if device features could not be set or DMA mode
16394  * for a disk cannot be set and device identify data cannot be fetched.
16395  *
16396  * Returns SATA_RETRY if device features could not be set (other than disk
16397  * DMA mode) but the device identify data was fetched successfully.
16398  *
16399  * Note: This function may fail the port, making it inaccessible.
16400  * In such case the explicit port disconnect/connect or physical device
16401  * detach/attach is required to re-evaluate port state again.
16402  */
16403 
16404 static int
16405 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16406     sata_drive_info_t *sdinfo, int restore)
16407 {
16408 	int rval = SATA_SUCCESS;
16409 	int rval_set;
16410 	sata_drive_info_t new_sdinfo;
16411 	char *finfo = "sata_set_drive_features: cannot";
16412 	char *finfox;
16413 	int cache_op;
16414 
16415 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16416 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16417 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16418 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16419 		/*
16420 		 * Cannot get device identification - caller may retry later
16421 		 */
16422 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16423 		    "%s fetch device identify data\n", finfo);
16424 		return (SATA_FAILURE);
16425 	}
16426 	finfox = (restore != 0) ? " restore device features" :
16427 	    " initialize device features\n";
16428 
16429 	switch (sdinfo->satadrv_type) {
16430 	case SATA_DTYPE_ATADISK:
16431 		/* Arbitrarily set UDMA mode */
16432 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16433 		    SATA_SUCCESS) {
16434 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16435 			    "%s set UDMA mode\n", finfo));
16436 			return (SATA_FAILURE);
16437 		}
16438 		break;
16439 	case SATA_DTYPE_ATAPICD:
16440 	case SATA_DTYPE_ATAPITAPE:
16441 	case SATA_DTYPE_ATAPIDISK:
16442 		/*  Set Removable Media Status Notification, if necessary */
16443 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16444 		    restore != 0) {
16445 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16446 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16447 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16448 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16449 				/* Current setting does not match saved one */
16450 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16451 				    sdinfo->satadrv_settings &
16452 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16453 					rval = SATA_FAILURE;
16454 			}
16455 		}
16456 		/*
16457 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16458 		 * we want to use DMA transfer mode whenever possible.
16459 		 * Some devices require explicit setting of the DMA mode.
16460 		 */
16461 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16462 			/* Set highest supported DMA mode */
16463 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16464 			    SATA_SUCCESS) {
16465 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16466 				    "%s set UDMA mode\n", finfo));
16467 				rval = SATA_FAILURE;
16468 			}
16469 		}
16470 		break;
16471 	}
16472 
16473 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16474 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16475 		/*
16476 		 * neither READ AHEAD nor WRITE CACHE is supported
16477 		 * - do nothing
16478 		 */
16479 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16480 		    "settable features not supported\n", NULL);
16481 		goto update_sdinfo;
16482 	}
16483 
16484 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16485 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16486 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16487 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16488 		/*
16489 		 * both READ AHEAD and WRITE CACHE are enabled
16490 		 * - Nothing to do
16491 		 */
16492 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16493 		    "no device features to set\n", NULL);
16494 		goto update_sdinfo;
16495 	}
16496 
16497 	cache_op = 0;
16498 
16499 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16500 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16501 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16502 			/* Enable read ahead / read cache */
16503 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16504 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16505 			    "enabling read cache\n", NULL);
16506 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16507 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16508 			/* Disable read ahead  / read cache */
16509 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16510 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16511 			    "disabling read cache\n", NULL);
16512 		}
16513 
16514 		if (cache_op != 0) {
16515 			/* Try to set read cache mode */
16516 			rval_set = sata_set_cache_mode(sata_hba_inst,
16517 			    &new_sdinfo, cache_op);
16518 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16519 				rval = rval_set;
16520 		}
16521 	}
16522 
16523 	cache_op = 0;
16524 
16525 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16526 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16527 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16528 			/* Enable write cache */
16529 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16530 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16531 			    "enabling write cache\n", NULL);
16532 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16533 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16534 			/* Disable write cache */
16535 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16536 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16537 			    "disabling write cache\n", NULL);
16538 		}
16539 
16540 		if (cache_op != 0) {
16541 			/* Try to set write cache mode */
16542 			rval_set = sata_set_cache_mode(sata_hba_inst,
16543 			    &new_sdinfo, cache_op);
16544 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16545 				rval = rval_set;
16546 		}
16547 	}
16548 	if (rval != SATA_SUCCESS)
16549 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16550 		    "%s %s", finfo, finfox));
16551 
16552 update_sdinfo:
16553 	/*
16554 	 * We need to fetch Device Identify data again
16555 	 */
16556 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16557 		/*
16558 		 * Cannot get device identification - retry later
16559 		 */
16560 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16561 		    "%s re-fetch device identify data\n", finfo));
16562 		rval = SATA_FAILURE;
16563 	}
16564 	/* Copy device sata info. */
16565 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16566 
16567 	return (rval);
16568 }
16569 
16570 
16571 /*
16572  *
16573  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16574  * unable to determine.
16575  *
16576  * Cannot be called in an interrupt context.
16577  *
16578  * Called by sata_build_lsense_page_2f()
16579  */
16580 
16581 static int
16582 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16583     sata_drive_info_t *sdinfo)
16584 {
16585 	sata_pkt_t *spkt;
16586 	sata_cmd_t *scmd;
16587 	sata_pkt_txlate_t *spx;
16588 	int rval;
16589 
16590 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16591 	spx->txlt_sata_hba_inst = sata_hba_inst;
16592 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16593 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16594 	if (spkt == NULL) {
16595 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16596 		return (-1);
16597 	}
16598 	/* address is needed now */
16599 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16600 
16601 
16602 	/* Fill sata_pkt */
16603 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16604 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16605 	/* Synchronous mode, no callback */
16606 	spkt->satapkt_comp = NULL;
16607 	/* Timeout 30s */
16608 	spkt->satapkt_time = sata_default_pkt_time;
16609 
16610 	scmd = &spkt->satapkt_cmd;
16611 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16612 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16613 
16614 	/* Set up which registers need to be returned */
16615 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16616 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16617 
16618 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16619 	scmd->satacmd_addr_type = 0;		/* N/A */
16620 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16621 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16622 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16623 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16624 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16625 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16626 	scmd->satacmd_cmd_reg = SATAC_SMART;
16627 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16628 	    sdinfo->satadrv_addr.cport)));
16629 
16630 
16631 	/* Send pkt to SATA HBA driver */
16632 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16633 	    SATA_TRAN_ACCEPTED ||
16634 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16635 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16636 		    sdinfo->satadrv_addr.cport)));
16637 		/*
16638 		 * Whoops, no SMART RETURN STATUS
16639 		 */
16640 		rval = -1;
16641 	} else {
16642 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16643 		    sdinfo->satadrv_addr.cport)));
16644 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16645 			rval = -1;
16646 			goto fail;
16647 		}
16648 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16649 			rval = -1;
16650 			goto fail;
16651 		}
16652 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16653 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16654 			rval = 0;
16655 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16656 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16657 			rval = 1;
16658 		else {
16659 			rval = -1;
16660 			goto fail;
16661 		}
16662 	}
16663 fail:
16664 	/* Free allocated resources */
16665 	sata_pkt_free(spx);
16666 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16667 
16668 	return (rval);
16669 }
16670 
16671 /*
16672  *
16673  * Returns 0 if succeeded, -1 otherwise
16674  *
16675  * Cannot be called in an interrupt context.
16676  *
16677  */
16678 static int
16679 sata_fetch_smart_data(
16680 	sata_hba_inst_t *sata_hba_inst,
16681 	sata_drive_info_t *sdinfo,
16682 	struct smart_data *smart_data)
16683 {
16684 	sata_pkt_t *spkt;
16685 	sata_cmd_t *scmd;
16686 	sata_pkt_txlate_t *spx;
16687 	int rval;
16688 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16689 
16690 #if ! defined(lint)
16691 	ASSERT(sizeof (struct smart_data) == 512);
16692 #endif
16693 
16694 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16695 	spx->txlt_sata_hba_inst = sata_hba_inst;
16696 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16697 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16698 	if (spkt == NULL) {
16699 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16700 		return (-1);
16701 	}
16702 	/* address is needed now */
16703 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16704 
16705 
16706 	/* Fill sata_pkt */
16707 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16708 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16709 	/* Synchronous mode, no callback */
16710 	spkt->satapkt_comp = NULL;
16711 	/* Timeout 30s */
16712 	spkt->satapkt_time = sata_default_pkt_time;
16713 
16714 	scmd = &spkt->satapkt_cmd;
16715 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16716 
16717 	/*
16718 	 * Allocate buffer for SMART data
16719 	 */
16720 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16721 	    sizeof (struct smart_data));
16722 	if (scmd->satacmd_bp == NULL) {
16723 		sata_pkt_free(spx);
16724 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16725 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16726 		    "sata_fetch_smart_data: "
16727 		    "cannot allocate buffer"));
16728 		return (-1);
16729 	}
16730 
16731 
16732 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16733 	scmd->satacmd_addr_type = 0;		/* N/A */
16734 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16735 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16736 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16737 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16738 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16739 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16740 	scmd->satacmd_cmd_reg = SATAC_SMART;
16741 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16742 	    sdinfo->satadrv_addr.cport)));
16743 
16744 	/* Send pkt to SATA HBA driver */
16745 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16746 	    SATA_TRAN_ACCEPTED ||
16747 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16748 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16749 		    sdinfo->satadrv_addr.cport)));
16750 		/*
16751 		 * Whoops, no SMART DATA available
16752 		 */
16753 		rval = -1;
16754 		goto fail;
16755 	} else {
16756 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16757 		    sdinfo->satadrv_addr.cport)));
16758 		if (spx->txlt_buf_dma_handle != NULL) {
16759 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16760 			    DDI_DMA_SYNC_FORKERNEL);
16761 			ASSERT(rval == DDI_SUCCESS);
16762 			if (sata_check_for_dma_error(dip, spx)) {
16763 				ddi_fm_service_impact(dip,
16764 				    DDI_SERVICE_UNAFFECTED);
16765 				rval = -1;
16766 				goto fail;
16767 			}
16768 		}
16769 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16770 		    sizeof (struct smart_data));
16771 	}
16772 
16773 fail:
16774 	/* Free allocated resources */
16775 	sata_free_local_buffer(spx);
16776 	sata_pkt_free(spx);
16777 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16778 
16779 	return (rval);
16780 }
16781 
16782 /*
16783  * Used by LOG SENSE page 0x10
16784  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16785  * Note: cannot be called in the interrupt context.
16786  *
16787  * return 0 for success, -1 otherwise
16788  *
16789  */
16790 static int
16791 sata_ext_smart_selftest_read_log(
16792 	sata_hba_inst_t *sata_hba_inst,
16793 	sata_drive_info_t *sdinfo,
16794 	struct smart_ext_selftest_log *ext_selftest_log,
16795 	uint16_t block_num)
16796 {
16797 	sata_pkt_txlate_t *spx;
16798 	sata_pkt_t *spkt;
16799 	sata_cmd_t *scmd;
16800 	int rval;
16801 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16802 
16803 #if ! defined(lint)
16804 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16805 #endif
16806 
16807 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16808 	spx->txlt_sata_hba_inst = sata_hba_inst;
16809 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16810 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16811 	if (spkt == NULL) {
16812 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16813 		return (-1);
16814 	}
16815 	/* address is needed now */
16816 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16817 
16818 
16819 	/* Fill sata_pkt */
16820 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16821 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16822 	/* Synchronous mode, no callback */
16823 	spkt->satapkt_comp = NULL;
16824 	/* Timeout 30s */
16825 	spkt->satapkt_time = sata_default_pkt_time;
16826 
16827 	scmd = &spkt->satapkt_cmd;
16828 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16829 
16830 	/*
16831 	 * Allocate buffer for SMART extended self-test log
16832 	 */
16833 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16834 	    sizeof (struct smart_ext_selftest_log));
16835 	if (scmd->satacmd_bp == NULL) {
16836 		sata_pkt_free(spx);
16837 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16838 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16839 		    "sata_ext_smart_selftest_log: "
16840 		    "cannot allocate buffer"));
16841 		return (-1);
16842 	}
16843 
16844 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16845 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16846 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16847 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16848 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16849 	scmd->satacmd_lba_low_msb = 0;
16850 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16851 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16852 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16853 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16854 
16855 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16856 	    sdinfo->satadrv_addr.cport)));
16857 
16858 	/* Send pkt to SATA HBA driver */
16859 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16860 	    SATA_TRAN_ACCEPTED ||
16861 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16862 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16863 		    sdinfo->satadrv_addr.cport)));
16864 
16865 		/*
16866 		 * Whoops, no SMART selftest log info available
16867 		 */
16868 		rval = -1;
16869 		goto fail;
16870 	} else {
16871 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16872 		    sdinfo->satadrv_addr.cport)));
16873 
16874 		if (spx->txlt_buf_dma_handle != NULL) {
16875 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16876 			    DDI_DMA_SYNC_FORKERNEL);
16877 			ASSERT(rval == DDI_SUCCESS);
16878 			if (sata_check_for_dma_error(dip, spx)) {
16879 				ddi_fm_service_impact(dip,
16880 				    DDI_SERVICE_UNAFFECTED);
16881 				rval = -1;
16882 				goto fail;
16883 			}
16884 		}
16885 		bcopy(scmd->satacmd_bp->b_un.b_addr,
16886 		    (uint8_t *)ext_selftest_log,
16887 		    sizeof (struct smart_ext_selftest_log));
16888 		rval = 0;
16889 	}
16890 
16891 fail:
16892 	/* Free allocated resources */
16893 	sata_free_local_buffer(spx);
16894 	sata_pkt_free(spx);
16895 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16896 
16897 	return (rval);
16898 }
16899 
16900 /*
16901  * Returns 0 for success, -1 otherwise
16902  *
16903  * SMART self-test log data is returned in buffer pointed to by selftest_log
16904  */
16905 static int
16906 sata_smart_selftest_log(
16907 	sata_hba_inst_t *sata_hba_inst,
16908 	sata_drive_info_t *sdinfo,
16909 	struct smart_selftest_log *selftest_log)
16910 {
16911 	sata_pkt_t *spkt;
16912 	sata_cmd_t *scmd;
16913 	sata_pkt_txlate_t *spx;
16914 	int rval;
16915 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16916 
16917 #if ! defined(lint)
16918 	ASSERT(sizeof (struct smart_selftest_log) == 512);
16919 #endif
16920 
16921 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16922 	spx->txlt_sata_hba_inst = sata_hba_inst;
16923 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16924 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16925 	if (spkt == NULL) {
16926 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16927 		return (-1);
16928 	}
16929 	/* address is needed now */
16930 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16931 
16932 
16933 	/* Fill sata_pkt */
16934 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16935 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16936 	/* Synchronous mode, no callback */
16937 	spkt->satapkt_comp = NULL;
16938 	/* Timeout 30s */
16939 	spkt->satapkt_time = sata_default_pkt_time;
16940 
16941 	scmd = &spkt->satapkt_cmd;
16942 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16943 
16944 	/*
16945 	 * Allocate buffer for SMART SELFTEST LOG
16946 	 */
16947 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16948 	    sizeof (struct smart_selftest_log));
16949 	if (scmd->satacmd_bp == NULL) {
16950 		sata_pkt_free(spx);
16951 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16952 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16953 		    "sata_smart_selftest_log: "
16954 		    "cannot allocate buffer"));
16955 		return (-1);
16956 	}
16957 
16958 	/* Build SMART_READ_LOG cmd in the sata_pkt */
16959 	scmd->satacmd_addr_type = 0;		/* N/A */
16960 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
16961 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
16962 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16963 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16964 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
16965 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16966 	scmd->satacmd_cmd_reg = SATAC_SMART;
16967 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16968 	    sdinfo->satadrv_addr.cport)));
16969 
16970 	/* Send pkt to SATA HBA driver */
16971 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16972 	    SATA_TRAN_ACCEPTED ||
16973 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16974 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16975 		    sdinfo->satadrv_addr.cport)));
16976 		/*
16977 		 * Whoops, no SMART DATA available
16978 		 */
16979 		rval = -1;
16980 		goto fail;
16981 	} else {
16982 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16983 		    sdinfo->satadrv_addr.cport)));
16984 		if (spx->txlt_buf_dma_handle != NULL) {
16985 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16986 			    DDI_DMA_SYNC_FORKERNEL);
16987 			ASSERT(rval == DDI_SUCCESS);
16988 			if (sata_check_for_dma_error(dip, spx)) {
16989 				ddi_fm_service_impact(dip,
16990 				    DDI_SERVICE_UNAFFECTED);
16991 				rval = -1;
16992 				goto fail;
16993 			}
16994 		}
16995 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
16996 		    sizeof (struct smart_selftest_log));
16997 		rval = 0;
16998 	}
16999 
17000 fail:
17001 	/* Free allocated resources */
17002 	sata_free_local_buffer(spx);
17003 	sata_pkt_free(spx);
17004 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17005 
17006 	return (rval);
17007 }
17008 
17009 
17010 /*
17011  * Returns 0 for success, -1 otherwise
17012  *
17013  * SMART READ LOG data is returned in buffer pointed to by smart_log
17014  */
17015 static int
17016 sata_smart_read_log(
17017 	sata_hba_inst_t *sata_hba_inst,
17018 	sata_drive_info_t *sdinfo,
17019 	uint8_t *smart_log,		/* where the data should be returned */
17020 	uint8_t which_log,		/* which log should be returned */
17021 	uint8_t log_size)		/* # of 512 bytes in log */
17022 {
17023 	sata_pkt_t *spkt;
17024 	sata_cmd_t *scmd;
17025 	sata_pkt_txlate_t *spx;
17026 	int rval;
17027 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17028 
17029 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17030 	spx->txlt_sata_hba_inst = sata_hba_inst;
17031 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17032 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17033 	if (spkt == NULL) {
17034 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17035 		return (-1);
17036 	}
17037 	/* address is needed now */
17038 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17039 
17040 
17041 	/* Fill sata_pkt */
17042 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17043 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17044 	/* Synchronous mode, no callback */
17045 	spkt->satapkt_comp = NULL;
17046 	/* Timeout 30s */
17047 	spkt->satapkt_time = sata_default_pkt_time;
17048 
17049 	scmd = &spkt->satapkt_cmd;
17050 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17051 
17052 	/*
17053 	 * Allocate buffer for SMART READ LOG
17054 	 */
17055 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17056 	if (scmd->satacmd_bp == NULL) {
17057 		sata_pkt_free(spx);
17058 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17059 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17060 		    "sata_smart_read_log: " "cannot allocate buffer"));
17061 		return (-1);
17062 	}
17063 
17064 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17065 	scmd->satacmd_addr_type = 0;		/* N/A */
17066 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17067 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17068 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17069 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17070 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17071 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17072 	scmd->satacmd_cmd_reg = SATAC_SMART;
17073 
17074 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17075 	    sdinfo->satadrv_addr.cport)));
17076 
17077 	/* Send pkt to SATA HBA driver */
17078 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17079 	    SATA_TRAN_ACCEPTED ||
17080 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17081 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17082 		    sdinfo->satadrv_addr.cport)));
17083 
17084 		/*
17085 		 * Whoops, no SMART DATA available
17086 		 */
17087 		rval = -1;
17088 		goto fail;
17089 	} else {
17090 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17091 		    sdinfo->satadrv_addr.cport)));
17092 
17093 		if (spx->txlt_buf_dma_handle != NULL) {
17094 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17095 			    DDI_DMA_SYNC_FORKERNEL);
17096 			ASSERT(rval == DDI_SUCCESS);
17097 			if (sata_check_for_dma_error(dip, spx)) {
17098 				ddi_fm_service_impact(dip,
17099 				    DDI_SERVICE_UNAFFECTED);
17100 				rval = -1;
17101 				goto fail;
17102 			}
17103 		}
17104 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17105 		rval = 0;
17106 	}
17107 
17108 fail:
17109 	/* Free allocated resources */
17110 	sata_free_local_buffer(spx);
17111 	sata_pkt_free(spx);
17112 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17113 
17114 	return (rval);
17115 }
17116 
17117 /*
17118  * Used by LOG SENSE page 0x10
17119  *
17120  * return 0 for success, -1 otherwise
17121  *
17122  */
17123 static int
17124 sata_read_log_ext_directory(
17125 	sata_hba_inst_t *sata_hba_inst,
17126 	sata_drive_info_t *sdinfo,
17127 	struct read_log_ext_directory *logdir)
17128 {
17129 	sata_pkt_txlate_t *spx;
17130 	sata_pkt_t *spkt;
17131 	sata_cmd_t *scmd;
17132 	int rval;
17133 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17134 
17135 #if ! defined(lint)
17136 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17137 #endif
17138 
17139 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17140 	spx->txlt_sata_hba_inst = sata_hba_inst;
17141 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17142 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17143 	if (spkt == NULL) {
17144 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17145 		return (-1);
17146 	}
17147 
17148 	/* Fill sata_pkt */
17149 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17150 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17151 	/* Synchronous mode, no callback */
17152 	spkt->satapkt_comp = NULL;
17153 	/* Timeout 30s */
17154 	spkt->satapkt_time = sata_default_pkt_time;
17155 
17156 	scmd = &spkt->satapkt_cmd;
17157 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17158 
17159 	/*
17160 	 * Allocate buffer for SMART READ LOG EXTENDED command
17161 	 */
17162 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17163 	    sizeof (struct read_log_ext_directory));
17164 	if (scmd->satacmd_bp == NULL) {
17165 		sata_pkt_free(spx);
17166 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17167 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17168 		    "sata_read_log_ext_directory: "
17169 		    "cannot allocate buffer"));
17170 		return (-1);
17171 	}
17172 
17173 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17174 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17175 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17176 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17177 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17178 	scmd->satacmd_lba_low_msb = 0;
17179 	scmd->satacmd_lba_mid_lsb = 0;
17180 	scmd->satacmd_lba_mid_msb = 0;
17181 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17182 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17183 
17184 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17185 	    sdinfo->satadrv_addr.cport)));
17186 
17187 	/* Send pkt to SATA HBA driver */
17188 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17189 	    SATA_TRAN_ACCEPTED ||
17190 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17191 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17192 		    sdinfo->satadrv_addr.cport)));
17193 		/*
17194 		 * Whoops, no SMART selftest log info available
17195 		 */
17196 		rval = -1;
17197 		goto fail;
17198 	} else {
17199 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17200 		    sdinfo->satadrv_addr.cport)));
17201 		if (spx->txlt_buf_dma_handle != NULL) {
17202 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17203 			    DDI_DMA_SYNC_FORKERNEL);
17204 			ASSERT(rval == DDI_SUCCESS);
17205 			if (sata_check_for_dma_error(dip, spx)) {
17206 				ddi_fm_service_impact(dip,
17207 				    DDI_SERVICE_UNAFFECTED);
17208 				rval = -1;
17209 				goto fail;
17210 			}
17211 		}
17212 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17213 		    sizeof (struct read_log_ext_directory));
17214 		rval = 0;
17215 	}
17216 
17217 fail:
17218 	/* Free allocated resources */
17219 	sata_free_local_buffer(spx);
17220 	sata_pkt_free(spx);
17221 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17222 
17223 	return (rval);
17224 }
17225 
17226 /*
17227  * Set up error retrieval sata command for NCQ command error data
17228  * recovery.
17229  *
17230  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17231  * returns SATA_FAILURE otherwise.
17232  */
17233 static int
17234 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17235 {
17236 #ifndef __lock_lint
17237 	_NOTE(ARGUNUSED(sdinfo))
17238 #endif
17239 
17240 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17241 	sata_cmd_t *scmd;
17242 	struct buf *bp;
17243 
17244 	/* Operation modes are up to the caller */
17245 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17246 
17247 	/* Synchronous mode, no callback - may be changed by the caller */
17248 	spkt->satapkt_comp = NULL;
17249 	spkt->satapkt_time = sata_default_pkt_time;
17250 
17251 	scmd = &spkt->satapkt_cmd;
17252 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17253 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17254 
17255 	/*
17256 	 * Allocate dma_able buffer error data.
17257 	 * Buffer allocation will take care of buffer alignment and other DMA
17258 	 * attributes.
17259 	 */
17260 	bp = sata_alloc_local_buffer(spx,
17261 	    sizeof (struct sata_ncq_error_recovery_page));
17262 	if (bp == NULL)
17263 		return (SATA_FAILURE);
17264 
17265 	bp_mapin(bp); /* make data buffer accessible */
17266 	scmd->satacmd_bp = bp;
17267 
17268 	/*
17269 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17270 	 * before accessing it. Handle is in usual place in translate struct.
17271 	 */
17272 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17273 
17274 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17275 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17276 
17277 	return (SATA_SUCCESS);
17278 }
17279 
17280 /*
17281  * sata_xlate_errors() is used to translate (S)ATA error
17282  * information to SCSI information returned in the SCSI
17283  * packet.
17284  */
17285 static void
17286 sata_xlate_errors(sata_pkt_txlate_t *spx)
17287 {
17288 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17289 	struct scsi_extended_sense *sense;
17290 
17291 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17292 	*scsipkt->pkt_scbp = STATUS_CHECK;
17293 	sense = sata_arq_sense(spx);
17294 
17295 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17296 	case SATA_PKT_PORT_ERROR:
17297 		/*
17298 		 * We have no device data. Assume no data transfered.
17299 		 */
17300 		sense->es_key = KEY_HARDWARE_ERROR;
17301 		break;
17302 
17303 	case SATA_PKT_DEV_ERROR:
17304 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17305 		    SATA_STATUS_ERR) {
17306 			/*
17307 			 * determine dev error reason from error
17308 			 * reg content
17309 			 */
17310 			sata_decode_device_error(spx, sense);
17311 			break;
17312 		}
17313 		/* No extended sense key - no info available */
17314 		break;
17315 
17316 	case SATA_PKT_TIMEOUT:
17317 		scsipkt->pkt_reason = CMD_TIMEOUT;
17318 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17319 		/* No extended sense key */
17320 		break;
17321 
17322 	case SATA_PKT_ABORTED:
17323 		scsipkt->pkt_reason = CMD_ABORTED;
17324 		scsipkt->pkt_statistics |= STAT_ABORTED;
17325 		/* No extended sense key */
17326 		break;
17327 
17328 	case SATA_PKT_RESET:
17329 		/*
17330 		 * pkt aborted either by an explicit reset request from
17331 		 * a host, or due to error recovery
17332 		 */
17333 		scsipkt->pkt_reason = CMD_RESET;
17334 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17335 		break;
17336 
17337 	default:
17338 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17339 		break;
17340 	}
17341 }
17342 
17343 
17344 
17345 
17346 /*
17347  * Log sata message
17348  * dev pathname msg line preceeds the logged message.
17349  */
17350 
17351 static	void
17352 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17353 {
17354 	char pathname[128];
17355 	dev_info_t *dip = NULL;
17356 	va_list ap;
17357 
17358 	mutex_enter(&sata_log_mutex);
17359 
17360 	va_start(ap, fmt);
17361 	(void) vsprintf(sata_log_buf, fmt, ap);
17362 	va_end(ap);
17363 
17364 	if (sata_hba_inst != NULL) {
17365 		dip = SATA_DIP(sata_hba_inst);
17366 		(void) ddi_pathname(dip, pathname);
17367 	} else {
17368 		pathname[0] = 0;
17369 	}
17370 	if (level == CE_CONT) {
17371 		if (sata_debug_flags == 0)
17372 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17373 		else
17374 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17375 	} else {
17376 		if (level != CE_NOTE) {
17377 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17378 		} else if (sata_msg) {
17379 			cmn_err(level, "%s:\n %s", pathname,
17380 			    sata_log_buf);
17381 		}
17382 	}
17383 
17384 	/* sata trace debug */
17385 	sata_trace_debug(dip, sata_log_buf);
17386 
17387 	mutex_exit(&sata_log_mutex);
17388 }
17389 
17390 
17391 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17392 
17393 /*
17394  * Start or terminate the thread, depending on flag arg and current state
17395  */
17396 static void
17397 sata_event_thread_control(int startstop)
17398 {
17399 	static 	int sata_event_thread_terminating = 0;
17400 	static 	int sata_event_thread_starting = 0;
17401 	int i;
17402 
17403 	mutex_enter(&sata_event_mutex);
17404 
17405 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17406 	    sata_event_thread_terminating == 1)) {
17407 		mutex_exit(&sata_event_mutex);
17408 		return;
17409 	}
17410 	if (startstop == 1 && sata_event_thread_starting == 1) {
17411 		mutex_exit(&sata_event_mutex);
17412 		return;
17413 	}
17414 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17415 		sata_event_thread_starting = 1;
17416 		/* wait til terminate operation completes */
17417 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17418 		while (sata_event_thread_terminating == 1) {
17419 			if (i-- <= 0) {
17420 				sata_event_thread_starting = 0;
17421 				mutex_exit(&sata_event_mutex);
17422 #ifdef SATA_DEBUG
17423 				cmn_err(CE_WARN, "sata_event_thread_control: "
17424 				    "timeout waiting for thread to terminate");
17425 #endif
17426 				return;
17427 			}
17428 			mutex_exit(&sata_event_mutex);
17429 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17430 			mutex_enter(&sata_event_mutex);
17431 		}
17432 	}
17433 	if (startstop == 1) {
17434 		if (sata_event_thread == NULL) {
17435 			sata_event_thread = thread_create(NULL, 0,
17436 			    (void (*)())sata_event_daemon,
17437 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17438 		}
17439 		sata_event_thread_starting = 0;
17440 		mutex_exit(&sata_event_mutex);
17441 		return;
17442 	}
17443 
17444 	/*
17445 	 * If we got here, thread may need to be terminated
17446 	 */
17447 	if (sata_event_thread != NULL) {
17448 		int i;
17449 		/* Signal event thread to go away */
17450 		sata_event_thread_terminating = 1;
17451 		sata_event_thread_terminate = 1;
17452 		cv_signal(&sata_event_cv);
17453 		/*
17454 		 * Wait til daemon terminates.
17455 		 */
17456 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17457 		while (sata_event_thread_terminate == 1) {
17458 			mutex_exit(&sata_event_mutex);
17459 			if (i-- <= 0) {
17460 				/* Daemon did not go away !!! */
17461 #ifdef SATA_DEBUG
17462 				cmn_err(CE_WARN, "sata_event_thread_control: "
17463 				    "cannot terminate event daemon thread");
17464 #endif
17465 				mutex_enter(&sata_event_mutex);
17466 				break;
17467 			}
17468 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17469 			mutex_enter(&sata_event_mutex);
17470 		}
17471 		sata_event_thread_terminating = 0;
17472 	}
17473 	ASSERT(sata_event_thread_terminating == 0);
17474 	ASSERT(sata_event_thread_starting == 0);
17475 	mutex_exit(&sata_event_mutex);
17476 }
17477 
17478 
17479 /*
17480  * SATA HBA event notification function.
17481  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17482  * a port and/or device state or a controller itself.
17483  * Events for different addresses/addr types cannot be combined.
17484  * A warning message is generated for each event type.
17485  * Events are not processed by this function, so only the
17486  * event flag(s)is set for an affected entity and the event thread is
17487  * waken up. Event daemon thread processes all events.
17488  *
17489  * NOTE: Since more than one event may be reported at the same time, one
17490  * cannot determine a sequence of events when opposite event are reported, eg.
17491  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17492  * is taking precedence over reported events, i.e. may cause ignoring some
17493  * events.
17494  */
17495 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17496 
17497 void
17498 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17499 {
17500 	sata_hba_inst_t *sata_hba_inst = NULL;
17501 	sata_address_t *saddr;
17502 	sata_pmult_info_t *pmultinfo;
17503 	sata_drive_info_t *sdinfo;
17504 	sata_port_stats_t *pstats;
17505 	sata_cport_info_t *cportinfo;
17506 	sata_pmport_info_t *pmportinfo;
17507 	int cport, pmport;
17508 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17509 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17510 	char *lcp;
17511 	static char *err_msg_evnt_1 =
17512 	    "sata_hba_event_notify: invalid port event 0x%x ";
17513 	static char *err_msg_evnt_2 =
17514 	    "sata_hba_event_notify: invalid device event 0x%x ";
17515 	int linkevent;
17516 
17517 	/*
17518 	 * There is a possibility that an event will be generated on HBA
17519 	 * that has not completed attachment or is detaching. We still want
17520 	 * to process events until HBA is detached.
17521 	 */
17522 	mutex_enter(&sata_mutex);
17523 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17524 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17525 		if (SATA_DIP(sata_hba_inst) == dip)
17526 			if (sata_hba_inst->satahba_attached == 1)
17527 				break;
17528 	}
17529 	mutex_exit(&sata_mutex);
17530 	if (sata_hba_inst == NULL)
17531 		/* HBA not attached */
17532 		return;
17533 
17534 	ASSERT(sata_device != NULL);
17535 
17536 	/*
17537 	 * Validate address before - do not proceed with invalid address.
17538 	 */
17539 	saddr = &sata_device->satadev_addr;
17540 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17541 		return;
17542 
17543 	cport = saddr->cport;
17544 	pmport = saddr->pmport;
17545 
17546 	buf1[0] = buf2[0] = '\0';
17547 
17548 	/*
17549 	 * If event relates to port or device, check port state.
17550 	 * Port has to be initialized, or we cannot accept an event.
17551 	 */
17552 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17553 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17554 		mutex_enter(&sata_hba_inst->satahba_mutex);
17555 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17556 		mutex_exit(&sata_hba_inst->satahba_mutex);
17557 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17558 			return;
17559 	}
17560 
17561 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17562 	    SATA_ADDR_DPMPORT)) != 0) {
17563 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17564 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17565 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17566 			    "is attached to port %d, ignore pmult/pmport "
17567 			    "event 0x%x", cportinfo->cport_dev_type,
17568 			    cport, event));
17569 			return;
17570 		}
17571 
17572 		mutex_enter(&cportinfo->cport_mutex);
17573 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17574 		mutex_exit(&cportinfo->cport_mutex);
17575 
17576 		/*
17577 		 * The daemon might be processing attachment of port
17578 		 * multiplier, in that case we should ignore events on its
17579 		 * sub-devices.
17580 		 *
17581 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17582 		 * The pmport_state is checked by sata daemon.
17583 		 */
17584 		if (pmultinfo == NULL ||
17585 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17586 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17587 			    "sata_hba_event_notify: pmult is not"
17588 			    "available at port %d:%d, ignore event 0x%x",
17589 			    cport, pmport, event));
17590 			return;
17591 		}
17592 	}
17593 
17594 	if ((saddr->qual &
17595 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17596 
17597 		mutex_enter(&cportinfo->cport_mutex);
17598 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17599 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17600 			    "sata_hba_event_notify: invalid/"
17601 			    "un-implemented port %d:%d (%d ports), "
17602 			    "ignore event 0x%x", cport, pmport,
17603 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17604 			mutex_exit(&cportinfo->cport_mutex);
17605 			return;
17606 		}
17607 		mutex_exit(&cportinfo->cport_mutex);
17608 
17609 		mutex_enter(&sata_hba_inst->satahba_mutex);
17610 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17611 		    cport, pmport);
17612 		mutex_exit(&sata_hba_inst->satahba_mutex);
17613 
17614 		/* pmport is implemented/valid? */
17615 		if (pmportinfo == NULL) {
17616 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17617 			    "sata_hba_event_notify: invalid/"
17618 			    "un-implemented port %d:%d, ignore "
17619 			    "event 0x%x", cport, pmport, event));
17620 			return;
17621 		}
17622 	}
17623 
17624 	/*
17625 	 * Events refer to devices, ports and controllers - each has
17626 	 * unique address. Events for different addresses cannot be combined.
17627 	 */
17628 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17629 
17630 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17631 
17632 		/* qualify this event(s) */
17633 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17634 			/* Invalid event for the device port */
17635 			(void) sprintf(buf2, err_msg_evnt_1,
17636 			    event & SATA_EVNT_PORT_EVENTS);
17637 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17638 			goto event_info;
17639 		}
17640 		if (saddr->qual == SATA_ADDR_CPORT) {
17641 			/* Controller's device port event */
17642 
17643 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17644 			    cport_event_flags |=
17645 			    event & SATA_EVNT_PORT_EVENTS;
17646 			pstats =
17647 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17648 			    cport_stats;
17649 		} else {
17650 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17651 			mutex_enter(&pmportinfo->pmport_mutex);
17652 			/* Port multiplier's device port event */
17653 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17654 			    pmport_event_flags |=
17655 			    event & SATA_EVNT_PORT_EVENTS;
17656 			pstats =
17657 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17658 			    pmport_stats;
17659 			mutex_exit(&pmportinfo->pmport_mutex);
17660 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17661 		}
17662 
17663 		/*
17664 		 * Add to statistics and log the message. We have to do it
17665 		 * here rather than in the event daemon, because there may be
17666 		 * multiple events occuring before they are processed.
17667 		 */
17668 		linkevent = event &
17669 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17670 		if (linkevent) {
17671 			if (linkevent == (SATA_EVNT_LINK_LOST |
17672 			    SATA_EVNT_LINK_ESTABLISHED)) {
17673 				/* This is likely event combination */
17674 				(void) strlcat(buf1, "link lost/established, ",
17675 				    SATA_EVENT_MAX_MSG_LENGTH);
17676 
17677 				if (pstats->link_lost < 0xffffffffffffffffULL)
17678 					pstats->link_lost++;
17679 				if (pstats->link_established <
17680 				    0xffffffffffffffffULL)
17681 					pstats->link_established++;
17682 				linkevent = 0;
17683 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17684 				(void) strlcat(buf1, "link lost, ",
17685 				    SATA_EVENT_MAX_MSG_LENGTH);
17686 
17687 				if (pstats->link_lost < 0xffffffffffffffffULL)
17688 					pstats->link_lost++;
17689 			} else {
17690 				(void) strlcat(buf1, "link established, ",
17691 				    SATA_EVENT_MAX_MSG_LENGTH);
17692 				if (pstats->link_established <
17693 				    0xffffffffffffffffULL)
17694 					pstats->link_established++;
17695 			}
17696 		}
17697 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17698 			(void) strlcat(buf1, "device attached, ",
17699 			    SATA_EVENT_MAX_MSG_LENGTH);
17700 			if (pstats->device_attached < 0xffffffffffffffffULL)
17701 				pstats->device_attached++;
17702 		}
17703 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17704 			(void) strlcat(buf1, "device detached, ",
17705 			    SATA_EVENT_MAX_MSG_LENGTH);
17706 			if (pstats->device_detached < 0xffffffffffffffffULL)
17707 				pstats->device_detached++;
17708 		}
17709 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17710 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17711 			    "port %d power level changed", cport);
17712 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17713 				pstats->port_pwr_changed++;
17714 		}
17715 
17716 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17717 			/* There should be no other events for this address */
17718 			(void) sprintf(buf2, err_msg_evnt_1,
17719 			    event & ~SATA_EVNT_PORT_EVENTS);
17720 		}
17721 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17722 
17723 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17724 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17725 
17726 		/* qualify this event */
17727 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17728 			/* Invalid event for a device */
17729 			(void) sprintf(buf2, err_msg_evnt_2,
17730 			    event & SATA_EVNT_DEVICE_RESET);
17731 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17732 			goto event_info;
17733 		}
17734 		/* drive event */
17735 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17736 		if (sdinfo != NULL) {
17737 			if (event & SATA_EVNT_DEVICE_RESET) {
17738 				(void) strlcat(buf1, "device reset, ",
17739 				    SATA_EVENT_MAX_MSG_LENGTH);
17740 				if (sdinfo->satadrv_stats.drive_reset <
17741 				    0xffffffffffffffffULL)
17742 					sdinfo->satadrv_stats.drive_reset++;
17743 				sdinfo->satadrv_event_flags |=
17744 				    SATA_EVNT_DEVICE_RESET;
17745 			}
17746 		}
17747 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17748 			/* Invalid event for a device */
17749 			(void) sprintf(buf2, err_msg_evnt_2,
17750 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17751 		}
17752 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17753 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17754 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17755 
17756 		/* qualify this event */
17757 		if ((event & (SATA_EVNT_DEVICE_RESET |
17758 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17759 			/* Invalid event for a port multiplier */
17760 			(void) sprintf(buf2, err_msg_evnt_2,
17761 			    event & SATA_EVNT_DEVICE_RESET);
17762 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17763 			goto event_info;
17764 		}
17765 
17766 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17767 
17768 		if (event & SATA_EVNT_DEVICE_RESET) {
17769 
17770 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17771 			    "[Reset] port-mult on cport %d", cport);
17772 			pmultinfo->pmult_event_flags |=
17773 			    SATA_EVNT_DEVICE_RESET;
17774 			(void) strlcat(buf1, "pmult reset, ",
17775 			    SATA_EVENT_MAX_MSG_LENGTH);
17776 		}
17777 
17778 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17779 
17780 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17781 			    "pmult link changed on cport %d", cport);
17782 			pmultinfo->pmult_event_flags |=
17783 			    SATA_EVNT_PMULT_LINK_CHANGED;
17784 			(void) strlcat(buf1, "pmult link changed, ",
17785 			    SATA_EVENT_MAX_MSG_LENGTH);
17786 		}
17787 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17788 
17789 	} else {
17790 		if (saddr->qual != SATA_ADDR_NULL) {
17791 			/* Wrong address qualifier */
17792 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17793 			    "sata_hba_event_notify: invalid address 0x%x",
17794 			    *(uint32_t *)saddr));
17795 			return;
17796 		}
17797 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17798 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17799 			/* Invalid event for the controller */
17800 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17801 			    "sata_hba_event_notify: invalid event 0x%x for "
17802 			    "controller",
17803 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17804 			return;
17805 		}
17806 		buf1[0] = '\0';
17807 		/* This may be a frequent and not interesting event */
17808 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17809 		    "controller power level changed\n", NULL);
17810 
17811 		mutex_enter(&sata_hba_inst->satahba_mutex);
17812 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17813 		    0xffffffffffffffffULL)
17814 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17815 
17816 		sata_hba_inst->satahba_event_flags |=
17817 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17818 		mutex_exit(&sata_hba_inst->satahba_mutex);
17819 	}
17820 	/*
17821 	 * If we got here, there is something to do with this HBA
17822 	 * instance.
17823 	 */
17824 	mutex_enter(&sata_hba_inst->satahba_mutex);
17825 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17826 	mutex_exit(&sata_hba_inst->satahba_mutex);
17827 	mutex_enter(&sata_mutex);
17828 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17829 	mutex_exit(&sata_mutex);
17830 
17831 	/* Tickle event thread */
17832 	mutex_enter(&sata_event_mutex);
17833 	if (sata_event_thread_active == 0)
17834 		cv_signal(&sata_event_cv);
17835 	mutex_exit(&sata_event_mutex);
17836 
17837 event_info:
17838 	if (buf1[0] != '\0') {
17839 		lcp = strrchr(buf1, ',');
17840 		if (lcp != NULL)
17841 			*lcp = '\0';
17842 	}
17843 	if (saddr->qual == SATA_ADDR_CPORT ||
17844 	    saddr->qual == SATA_ADDR_DCPORT) {
17845 		if (buf1[0] != '\0') {
17846 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17847 			    cport, buf1);
17848 		}
17849 		if (buf2[0] != '\0') {
17850 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17851 			    cport, buf2);
17852 		}
17853 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17854 	    saddr->qual == SATA_ADDR_DPMPORT) {
17855 		if (buf1[0] != '\0') {
17856 			sata_log(sata_hba_inst, CE_NOTE,
17857 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17858 		}
17859 		if (buf2[0] != '\0') {
17860 			sata_log(sata_hba_inst, CE_NOTE,
17861 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17862 		}
17863 	}
17864 }
17865 
17866 
17867 /*
17868  * Event processing thread.
17869  * Arg is a pointer to the sata_hba_list pointer.
17870  * It is not really needed, because sata_hba_list is global and static
17871  */
17872 static void
17873 sata_event_daemon(void *arg)
17874 {
17875 #ifndef __lock_lint
17876 	_NOTE(ARGUNUSED(arg))
17877 #endif
17878 	sata_hba_inst_t *sata_hba_inst;
17879 	clock_t delta;
17880 
17881 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17882 	    "SATA event daemon started\n", NULL);
17883 loop:
17884 	/*
17885 	 * Process events here. Walk through all registered HBAs
17886 	 */
17887 	mutex_enter(&sata_mutex);
17888 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17889 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17890 		ASSERT(sata_hba_inst != NULL);
17891 		mutex_enter(&sata_hba_inst->satahba_mutex);
17892 		if (sata_hba_inst->satahba_attached == 0 ||
17893 		    (sata_hba_inst->satahba_event_flags &
17894 		    SATA_EVNT_SKIP) != 0) {
17895 			mutex_exit(&sata_hba_inst->satahba_mutex);
17896 			continue;
17897 		}
17898 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
17899 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
17900 			mutex_exit(&sata_hba_inst->satahba_mutex);
17901 			mutex_exit(&sata_mutex);
17902 			/* Got the controller with pending event */
17903 			sata_process_controller_events(sata_hba_inst);
17904 			/*
17905 			 * Since global mutex was released, there is a
17906 			 * possibility that HBA list has changed, so start
17907 			 * over from the top. Just processed controller
17908 			 * will be passed-over because of the SKIP flag.
17909 			 */
17910 			goto loop;
17911 		}
17912 		mutex_exit(&sata_hba_inst->satahba_mutex);
17913 	}
17914 	/* Clear SKIP flag in all controllers */
17915 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17916 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17917 		mutex_enter(&sata_hba_inst->satahba_mutex);
17918 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
17919 		mutex_exit(&sata_hba_inst->satahba_mutex);
17920 	}
17921 	mutex_exit(&sata_mutex);
17922 
17923 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17924 	    "SATA EVENT DAEMON suspending itself", NULL);
17925 
17926 #ifdef SATA_DEBUG
17927 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
17928 		sata_log(sata_hba_inst, CE_WARN,
17929 		    "SATA EVENTS PROCESSING DISABLED\n");
17930 		thread_exit(); /* Daemon will not run again */
17931 	}
17932 #endif
17933 	mutex_enter(&sata_event_mutex);
17934 	sata_event_thread_active = 0;
17935 	mutex_exit(&sata_event_mutex);
17936 	/*
17937 	 * Go to sleep/suspend itself and wake up either because new event or
17938 	 * wait timeout. Exit if there is a termination request (driver
17939 	 * unload).
17940 	 */
17941 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
17942 	do {
17943 		mutex_enter(&sata_event_mutex);
17944 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
17945 		    delta, TR_CLOCK_TICK);
17946 
17947 		if (sata_event_thread_active != 0) {
17948 			mutex_exit(&sata_event_mutex);
17949 			continue;
17950 		}
17951 
17952 		/* Check if it is time to go away */
17953 		if (sata_event_thread_terminate == 1) {
17954 			/*
17955 			 * It is up to the thread setting above flag to make
17956 			 * sure that this thread is not killed prematurely.
17957 			 */
17958 			sata_event_thread_terminate = 0;
17959 			sata_event_thread = NULL;
17960 			mutex_exit(&sata_event_mutex);
17961 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17962 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
17963 			thread_exit();  { _NOTE(NOT_REACHED) }
17964 		}
17965 		mutex_exit(&sata_event_mutex);
17966 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
17967 
17968 	mutex_enter(&sata_event_mutex);
17969 	sata_event_thread_active = 1;
17970 	mutex_exit(&sata_event_mutex);
17971 
17972 	mutex_enter(&sata_mutex);
17973 	sata_event_pending &= ~SATA_EVNT_MAIN;
17974 	mutex_exit(&sata_mutex);
17975 
17976 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17977 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
17978 
17979 	goto loop;
17980 }
17981 
17982 /*
17983  * Specific HBA instance event processing.
17984  *
17985  * NOTE: At the moment, device event processing is limited to hard disks
17986  * only.
17987  * Port multiplier is supported now.
17988  */
17989 static void
17990 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
17991 {
17992 	int ncport;
17993 	uint32_t event_flags;
17994 	sata_address_t *saddr;
17995 	sata_cport_info_t *cportinfo;
17996 	sata_pmult_info_t *pmultinfo;
17997 
17998 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
17999 	    "Processing controller %d event(s)",
18000 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18001 
18002 	mutex_enter(&sata_hba_inst->satahba_mutex);
18003 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18004 	event_flags = sata_hba_inst->satahba_event_flags;
18005 	mutex_exit(&sata_hba_inst->satahba_mutex);
18006 	/*
18007 	 * Process controller power change first
18008 	 * HERE
18009 	 */
18010 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18011 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18012 
18013 	/*
18014 	 * Search through ports/devices to identify affected port/device.
18015 	 * We may have to process events for more than one port/device.
18016 	 */
18017 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18018 		/*
18019 		 * Not all ports may be processed in attach by the time we
18020 		 * get an event. Check if port info is initialized.
18021 		 */
18022 		mutex_enter(&sata_hba_inst->satahba_mutex);
18023 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18024 		mutex_exit(&sata_hba_inst->satahba_mutex);
18025 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18026 			continue;
18027 
18028 		/* We have initialized controller port info */
18029 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18030 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18031 		    cport_event_flags;
18032 		/* Check if port was locked by IOCTL processing */
18033 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18034 			/*
18035 			 * We ignore port events because port is busy
18036 			 * with AP control processing. Set again
18037 			 * controller and main event flag, so that
18038 			 * events may be processed by the next daemon
18039 			 * run.
18040 			 */
18041 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18042 			mutex_enter(&sata_hba_inst->satahba_mutex);
18043 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18044 			mutex_exit(&sata_hba_inst->satahba_mutex);
18045 			mutex_enter(&sata_mutex);
18046 			sata_event_pending |= SATA_EVNT_MAIN;
18047 			mutex_exit(&sata_mutex);
18048 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18049 			    "Event processing postponed until "
18050 			    "AP control processing completes",
18051 			    NULL);
18052 			/* Check other ports */
18053 			continue;
18054 		} else {
18055 			/*
18056 			 * Set BSY flag so that AP control would not
18057 			 * interfere with events processing for
18058 			 * this port.
18059 			 */
18060 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18061 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18062 		}
18063 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18064 
18065 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18066 
18067 		if ((event_flags &
18068 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18069 			/*
18070 			 * Got port event.
18071 			 * We need some hierarchy of event processing as they
18072 			 * are affecting each other:
18073 			 * 1. port failed
18074 			 * 2. device detached/attached
18075 			 * 3. link events - link events may trigger device
18076 			 *    detached or device attached events in some
18077 			 *    circumstances.
18078 			 * 4. port power level changed
18079 			 */
18080 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18081 				sata_process_port_failed_event(sata_hba_inst,
18082 				    saddr);
18083 			}
18084 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18085 				sata_process_device_detached(sata_hba_inst,
18086 				    saddr);
18087 			}
18088 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18089 				sata_process_device_attached(sata_hba_inst,
18090 				    saddr);
18091 			}
18092 			if (event_flags &
18093 			    (SATA_EVNT_LINK_ESTABLISHED |
18094 			    SATA_EVNT_LINK_LOST)) {
18095 				sata_process_port_link_events(sata_hba_inst,
18096 				    saddr);
18097 			}
18098 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18099 				sata_process_port_pwr_change(sata_hba_inst,
18100 				    saddr);
18101 			}
18102 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18103 				sata_process_target_node_cleanup(
18104 				    sata_hba_inst, saddr);
18105 			}
18106 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18107 				sata_process_device_autoonline(
18108 				    sata_hba_inst, saddr);
18109 			}
18110 		}
18111 
18112 
18113 		/*
18114 		 * Scan port multiplier and all its sub-ports event flags.
18115 		 * The events are marked by
18116 		 * (1) sata_pmult_info.pmult_event_flags
18117 		 * (2) sata_pmport_info.pmport_event_flags
18118 		 */
18119 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18120 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18121 			/*
18122 			 * There should be another extra check: this
18123 			 * port multiplier still exists?
18124 			 */
18125 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18126 			    ncport);
18127 
18128 			if (pmultinfo != NULL) {
18129 				mutex_exit(&(SATA_CPORT_MUTEX(
18130 				    sata_hba_inst, ncport)));
18131 				sata_process_pmult_events(
18132 				    sata_hba_inst, ncport);
18133 				mutex_enter(&(SATA_CPORT_MUTEX(
18134 				    sata_hba_inst, ncport)));
18135 			} else {
18136 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18137 				    "Port-multiplier is gone. "
18138 				    "Ignore all sub-device events "
18139 				    "at port %d.", ncport);
18140 			}
18141 		}
18142 
18143 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18144 		    SATA_DTYPE_NONE) &&
18145 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18146 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18147 			    satadrv_event_flags &
18148 			    (SATA_EVNT_DEVICE_RESET |
18149 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18150 				/* Have device event */
18151 				sata_process_device_reset(sata_hba_inst,
18152 				    saddr);
18153 			}
18154 		}
18155 		/* Release PORT_BUSY flag */
18156 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18157 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18158 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18159 
18160 	} /* End of loop through the controller SATA ports */
18161 }
18162 
18163 /*
18164  * Specific port multiplier instance event processing. At the moment, device
18165  * event processing is limited to link/attach event only.
18166  *
18167  * NOTE: power management event is not supported yet.
18168  */
18169 static void
18170 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18171 {
18172 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18173 	sata_pmult_info_t *pmultinfo;
18174 	sata_pmport_info_t *pmportinfo;
18175 	sata_address_t *saddr;
18176 	sata_device_t sata_device;
18177 	uint32_t event_flags;
18178 	int npmport;
18179 	int rval;
18180 
18181 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18182 	    "Processing pmult event(s) on cport %d of controller %d",
18183 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18184 
18185 	/* First process events on port multiplier */
18186 	mutex_enter(&cportinfo->cport_mutex);
18187 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18188 	event_flags = pmultinfo->pmult_event_flags;
18189 
18190 	/*
18191 	 * Reset event (of port multiplier) has higher priority because the
18192 	 * port multiplier itself might be failed or removed after reset.
18193 	 */
18194 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18195 		/*
18196 		 * The status of the sub-links are uncertain,
18197 		 * so mark all sub-ports as RESET
18198 		 */
18199 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18200 		    sata_hba_inst, cport); npmport ++) {
18201 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18202 			    cport, npmport);
18203 			if (pmportinfo == NULL) {
18204 				/* That's weird. */
18205 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18206 				    "sata_hba_event_notify: "
18207 				    "invalid/un-implemented "
18208 				    "port %d:%d (%d ports), ",
18209 				    cport, npmport, SATA_NUM_PMPORTS(
18210 				    sata_hba_inst, cport)));
18211 				continue;
18212 			}
18213 
18214 			mutex_enter(&pmportinfo->pmport_mutex);
18215 
18216 			/* Mark all pmport to unknow state. */
18217 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18218 			/* Mark all pmports with link events. */
18219 			pmportinfo->pmport_event_flags =
18220 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18221 			mutex_exit(&pmportinfo->pmport_mutex);
18222 		}
18223 
18224 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18225 		/*
18226 		 * We need probe the port multiplier to know what has
18227 		 * happened.
18228 		 */
18229 		bzero(&sata_device, sizeof (sata_device_t));
18230 		sata_device.satadev_rev = SATA_DEVICE_REV;
18231 		sata_device.satadev_addr.cport = cport;
18232 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18233 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18234 
18235 		mutex_exit(&cportinfo->cport_mutex);
18236 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18237 		    (SATA_DIP(sata_hba_inst), &sata_device);
18238 		mutex_enter(&cportinfo->cport_mutex);
18239 		if (rval != SATA_SUCCESS) {
18240 			/* Something went wrong? Fail the port */
18241 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18242 			mutex_exit(&cportinfo->cport_mutex);
18243 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18244 			    "SATA port %d probing failed", cport));
18245 
18246 			/* PMult structure must be released.  */
18247 			sata_free_pmult(sata_hba_inst, &sata_device);
18248 			return;
18249 		}
18250 
18251 		sata_update_port_info(sata_hba_inst, &sata_device);
18252 
18253 		/*
18254 		 * Sanity check - Port is active? Is the link active?
18255 		 * The device is still a port multiplier?
18256 		 */
18257 		if ((cportinfo->cport_state &
18258 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18259 		    ((cportinfo->cport_scr.sstatus &
18260 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18261 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18262 			mutex_exit(&cportinfo->cport_mutex);
18263 
18264 			/* PMult structure must be released.  */
18265 			sata_free_pmult(sata_hba_inst, &sata_device);
18266 			return;
18267 		}
18268 
18269 		/* Probed succeed, set port ready. */
18270 		cportinfo->cport_state |=
18271 		    SATA_STATE_PROBED | SATA_STATE_READY;
18272 	}
18273 
18274 	/* Release port multiplier event flags. */
18275 	pmultinfo->pmult_event_flags &=
18276 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18277 	mutex_exit(&cportinfo->cport_mutex);
18278 
18279 	/*
18280 	 * Check all sub-links.
18281 	 */
18282 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18283 	    npmport ++) {
18284 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18285 		mutex_enter(&pmportinfo->pmport_mutex);
18286 		event_flags = pmportinfo->pmport_event_flags;
18287 		mutex_exit(&pmportinfo->pmport_mutex);
18288 		saddr = &pmportinfo->pmport_addr;
18289 
18290 		if ((event_flags &
18291 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18292 			/*
18293 			 * Got port multiplier port event.
18294 			 * We need some hierarchy of event processing as they
18295 			 * are affecting each other:
18296 			 * 1. device detached/attached
18297 			 * 2. link events - link events may trigger device
18298 			 *    detached or device attached events in some
18299 			 *    circumstances.
18300 			 */
18301 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18302 				sata_process_pmdevice_detached(sata_hba_inst,
18303 				    saddr);
18304 			}
18305 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18306 				sata_process_pmdevice_attached(sata_hba_inst,
18307 				    saddr);
18308 			}
18309 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18310 			    event_flags & SATA_EVNT_LINK_LOST) {
18311 				sata_process_pmport_link_events(sata_hba_inst,
18312 				    saddr);
18313 			}
18314 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18315 				sata_process_target_node_cleanup(
18316 				    sata_hba_inst, saddr);
18317 			}
18318 		}
18319 
18320 		/* Checking drive event(s). */
18321 		mutex_enter(&pmportinfo->pmport_mutex);
18322 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18323 		    pmportinfo->pmport_sata_drive != NULL) {
18324 			event_flags = pmportinfo->pmport_sata_drive->
18325 			    satadrv_event_flags;
18326 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18327 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18328 
18329 				/* Have device event */
18330 				sata_process_pmdevice_reset(sata_hba_inst,
18331 				    saddr);
18332 			}
18333 		}
18334 		mutex_exit(&pmportinfo->pmport_mutex);
18335 
18336 		/* Release PORT_BUSY flag */
18337 		mutex_enter(&cportinfo->cport_mutex);
18338 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18339 		mutex_exit(&cportinfo->cport_mutex);
18340 	}
18341 
18342 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18343 	    "[DONE] pmult event(s) on cport %d of controller %d",
18344 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18345 }
18346 
18347 /*
18348  * Process HBA power level change reported by HBA driver.
18349  * Not implemented at this time - event is ignored.
18350  */
18351 static void
18352 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18353 {
18354 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18355 	    "Processing controller power level change", NULL);
18356 
18357 	/* Ignoring it for now */
18358 	mutex_enter(&sata_hba_inst->satahba_mutex);
18359 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18360 	mutex_exit(&sata_hba_inst->satahba_mutex);
18361 }
18362 
18363 /*
18364  * Process port power level change reported by HBA driver.
18365  * Not implemented at this time - event is ignored.
18366  */
18367 static void
18368 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18369     sata_address_t *saddr)
18370 {
18371 	sata_cport_info_t *cportinfo;
18372 
18373 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18374 	    "Processing port power level change", NULL);
18375 
18376 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18377 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18378 	/* Reset event flag */
18379 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18380 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18381 }
18382 
18383 /*
18384  * Process port failure reported by HBA driver.
18385  * cports support only - no pmports.
18386  */
18387 static void
18388 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18389     sata_address_t *saddr)
18390 {
18391 	sata_cport_info_t *cportinfo;
18392 
18393 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18394 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18395 	/* Reset event flag first */
18396 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18397 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18398 	if ((cportinfo->cport_state &
18399 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18400 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18401 		    cport_mutex);
18402 		return;
18403 	}
18404 	/* Fail the port */
18405 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18406 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18407 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18408 }
18409 
18410 /*
18411  * Device Reset Event processing.
18412  * The seqeunce is managed by 3 stage flags:
18413  * - reset event reported,
18414  * - reset event being processed,
18415  * - request to clear device reset state.
18416  *
18417  * NOTE: This function has to be entered with cport mutex held. It exits with
18418  * mutex held as well, but can release mutex during the processing.
18419  */
18420 static void
18421 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18422     sata_address_t *saddr)
18423 {
18424 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18425 	sata_drive_info_t *sdinfo;
18426 	sata_cport_info_t *cportinfo;
18427 	sata_device_t sata_device;
18428 	int rval_probe, rval_set;
18429 
18430 	/* We only care about host sata cport for now */
18431 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18432 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18433 	/*
18434 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18435 	 * state, ignore reset event.
18436 	 */
18437 	if (((cportinfo->cport_state &
18438 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18439 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18440 		sdinfo->satadrv_event_flags &=
18441 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18442 		return;
18443 	}
18444 
18445 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18446 	    SATA_DTYPE_PMULT)) {
18447 		/*
18448 		 * Should not happened: this is already handled in
18449 		 * sata_hba_event_notify()
18450 		 */
18451 		mutex_exit(&cportinfo->cport_mutex);
18452 		goto done;
18453 	}
18454 
18455 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18456 	    SATA_VALID_DEV_TYPE) == 0) {
18457 		/*
18458 		 * This should not happen - coding error.
18459 		 * But we can recover, so do not panic, just clean up
18460 		 * and if in debug mode, log the message.
18461 		 */
18462 #ifdef SATA_DEBUG
18463 		sata_log(sata_hba_inst, CE_WARN,
18464 		    "sata_process_device_reset: "
18465 		    "Invalid device type with sdinfo!", NULL);
18466 #endif
18467 		sdinfo->satadrv_event_flags = 0;
18468 		return;
18469 	}
18470 
18471 #ifdef SATA_DEBUG
18472 	if ((sdinfo->satadrv_event_flags &
18473 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18474 		/* Nothing to do */
18475 		/* Something is weird - why we are processing dev reset? */
18476 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18477 		    "No device reset event!!!!", NULL);
18478 
18479 		return;
18480 	}
18481 	if ((sdinfo->satadrv_event_flags &
18482 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18483 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18484 		/* Something is weird - new device reset event */
18485 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18486 		    "Overlapping device reset events!", NULL);
18487 	}
18488 #endif
18489 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18490 	    "Processing port %d device reset", saddr->cport);
18491 
18492 	/* Clear event flag */
18493 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18494 
18495 	/* It seems that we always need to check the port state first */
18496 	sata_device.satadev_rev = SATA_DEVICE_REV;
18497 	sata_device.satadev_addr = *saddr;
18498 	/*
18499 	 * We have to exit mutex, because the HBA probe port function may
18500 	 * block on its own mutex.
18501 	 */
18502 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18503 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18504 	    (SATA_DIP(sata_hba_inst), &sata_device);
18505 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18506 	sata_update_port_info(sata_hba_inst, &sata_device);
18507 	if (rval_probe != SATA_SUCCESS) {
18508 		/* Something went wrong? Fail the port */
18509 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18510 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18511 		if (sdinfo != NULL)
18512 			sdinfo->satadrv_event_flags = 0;
18513 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18514 		    cport_mutex);
18515 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18516 		    "SATA port %d probing failed",
18517 		    saddr->cport));
18518 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18519 		    saddr->cport)->cport_mutex);
18520 		return;
18521 	}
18522 	if ((sata_device.satadev_scr.sstatus  &
18523 	    SATA_PORT_DEVLINK_UP_MASK) !=
18524 	    SATA_PORT_DEVLINK_UP ||
18525 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18526 		/*
18527 		 * No device to process, anymore. Some other event processing
18528 		 * would or have already performed port info cleanup.
18529 		 * To be safe (HBA may need it), request clearing device
18530 		 * reset condition.
18531 		 */
18532 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18533 		if (sdinfo != NULL) {
18534 			sdinfo->satadrv_event_flags &=
18535 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18536 			sdinfo->satadrv_event_flags |=
18537 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18538 		}
18539 		return;
18540 	}
18541 
18542 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18543 	if (sdinfo == NULL) {
18544 		return;
18545 	}
18546 	if ((sdinfo->satadrv_event_flags &
18547 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18548 		/*
18549 		 * Start tracking time for device feature restoration and
18550 		 * identification. Save current time (lbolt value).
18551 		 */
18552 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18553 	}
18554 	/* Mark device reset processing as active */
18555 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18556 
18557 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18558 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18559 
18560 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18561 
18562 	if (rval_set  != SATA_SUCCESS) {
18563 		/*
18564 		 * Restoring drive setting failed.
18565 		 * Probe the port first, to check if the port state has changed
18566 		 */
18567 		sata_device.satadev_rev = SATA_DEVICE_REV;
18568 		sata_device.satadev_addr = *saddr;
18569 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18570 		/* probe port */
18571 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18572 		    (SATA_DIP(sata_hba_inst), &sata_device);
18573 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18574 		    cport_mutex);
18575 		if (rval_probe == SATA_SUCCESS &&
18576 		    (sata_device.satadev_state &
18577 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18578 		    (sata_device.satadev_scr.sstatus  &
18579 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18580 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18581 			/*
18582 			 * We may retry this a bit later - in-process reset
18583 			 * condition should be already set.
18584 			 * Track retry time for device identification.
18585 			 */
18586 			if ((cportinfo->cport_dev_type &
18587 			    SATA_VALID_DEV_TYPE) != 0 &&
18588 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18589 			    sdinfo->satadrv_reset_time != 0) {
18590 				clock_t cur_time = ddi_get_lbolt();
18591 				/*
18592 				 * If the retry time limit was not
18593 				 * exceeded, retry.
18594 				 */
18595 				if ((cur_time - sdinfo->satadrv_reset_time) <
18596 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18597 					mutex_enter(
18598 					    &sata_hba_inst->satahba_mutex);
18599 					sata_hba_inst->satahba_event_flags |=
18600 					    SATA_EVNT_MAIN;
18601 					mutex_exit(
18602 					    &sata_hba_inst->satahba_mutex);
18603 					mutex_enter(&sata_mutex);
18604 					sata_event_pending |= SATA_EVNT_MAIN;
18605 					mutex_exit(&sata_mutex);
18606 					return;
18607 				}
18608 				if (rval_set == SATA_RETRY) {
18609 					/*
18610 					 * Setting drive features failed, but
18611 					 * the drive is still accessible,
18612 					 * so emit a warning message before
18613 					 * return.
18614 					 */
18615 					mutex_exit(&SATA_CPORT_INFO(
18616 					    sata_hba_inst,
18617 					    saddr->cport)->cport_mutex);
18618 					goto done;
18619 				}
18620 			}
18621 			/* Fail the drive */
18622 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18623 
18624 			sata_log(sata_hba_inst, CE_WARN,
18625 			    "SATA device at port %d - device failed",
18626 			    saddr->cport);
18627 		}
18628 		/*
18629 		 * No point of retrying - device failed or some other event
18630 		 * processing or already did or will do port info cleanup.
18631 		 * To be safe (HBA may need it),
18632 		 * request clearing device reset condition.
18633 		 */
18634 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18635 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18636 		sdinfo->satadrv_reset_time = 0;
18637 		return;
18638 	}
18639 done:
18640 	/*
18641 	 * If setting of drive features failed, but the drive is still
18642 	 * accessible, emit a warning message.
18643 	 */
18644 	if (rval_set == SATA_RETRY) {
18645 		sata_log(sata_hba_inst, CE_WARN,
18646 		    "SATA device at port %d - desired setting could not be "
18647 		    "restored after reset. Device may not operate as expected.",
18648 		    saddr->cport);
18649 	}
18650 	/*
18651 	 * Raise the flag indicating that the next sata command could
18652 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18653 	 * reset is reported.
18654 	 */
18655 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18656 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18657 		sdinfo->satadrv_reset_time = 0;
18658 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18659 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18660 			sdinfo->satadrv_event_flags &=
18661 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18662 			sdinfo->satadrv_event_flags |=
18663 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18664 		}
18665 	}
18666 }
18667 
18668 
18669 /*
18670  * Port Multiplier Port Device Reset Event processing.
18671  *
18672  * NOTE: This function has to be entered with pmport mutex held. It exits with
18673  * mutex held as well, but can release mutex during the processing.
18674  */
18675 static void
18676 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18677     sata_address_t *saddr)
18678 {
18679 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18680 	sata_drive_info_t *sdinfo = NULL;
18681 	sata_cport_info_t *cportinfo = NULL;
18682 	sata_pmport_info_t *pmportinfo = NULL;
18683 	sata_pmult_info_t *pminfo = NULL;
18684 	sata_device_t sata_device;
18685 	uint8_t cport = saddr->cport;
18686 	uint8_t pmport = saddr->pmport;
18687 	int rval;
18688 
18689 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18690 	    "Processing drive reset at port %d:%d", cport, pmport);
18691 
18692 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18693 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18694 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18695 
18696 	/*
18697 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18698 	 * state, ignore reset event.
18699 	 */
18700 	if (((cportinfo->cport_state &
18701 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18702 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18703 		sdinfo->satadrv_event_flags &=
18704 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18705 		return;
18706 	}
18707 
18708 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18709 		/*
18710 		 * This should not happen - coding error.
18711 		 * But we can recover, so do not panic, just clean up
18712 		 * and if in debug mode, log the message.
18713 		 */
18714 #ifdef SATA_DEBUG
18715 		sata_log(sata_hba_inst, CE_WARN,
18716 		    "sata_process_pmdevice_reset: "
18717 		    "Invalid device type with sdinfo!", NULL);
18718 #endif
18719 		sdinfo->satadrv_event_flags = 0;
18720 		return;
18721 	}
18722 
18723 #ifdef SATA_DEBUG
18724 	if ((sdinfo->satadrv_event_flags &
18725 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18726 		/* Nothing to do */
18727 		/* Something is weird - why we are processing dev reset? */
18728 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18729 		    "No device reset event!!!!", NULL);
18730 
18731 		return;
18732 	}
18733 	if ((sdinfo->satadrv_event_flags &
18734 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18735 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18736 		/* Something is weird - new device reset event */
18737 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18738 		    "Overlapping device reset events!", NULL);
18739 	}
18740 #endif
18741 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18742 	    "Processing port %d:%d device reset", cport, pmport);
18743 
18744 	/* Clear event flag */
18745 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18746 
18747 	/* It seems that we always need to check the port state first */
18748 	sata_device.satadev_rev = SATA_DEVICE_REV;
18749 	sata_device.satadev_addr = *saddr;
18750 	/*
18751 	 * We have to exit mutex, because the HBA probe port function may
18752 	 * block on its own mutex.
18753 	 */
18754 	mutex_exit(&pmportinfo->pmport_mutex);
18755 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18756 	    (SATA_DIP(sata_hba_inst), &sata_device);
18757 	mutex_enter(&pmportinfo->pmport_mutex);
18758 
18759 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18760 	if (rval != SATA_SUCCESS) {
18761 		/* Something went wrong? Fail the port */
18762 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18763 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18764 		    saddr->pmport);
18765 		if (sdinfo != NULL)
18766 			sdinfo->satadrv_event_flags = 0;
18767 		mutex_exit(&pmportinfo->pmport_mutex);
18768 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18769 		    "SATA port %d:%d probing failed",
18770 		    saddr->cport, saddr->pmport));
18771 		mutex_enter(&pmportinfo->pmport_mutex);
18772 		return;
18773 	}
18774 	if ((sata_device.satadev_scr.sstatus  &
18775 	    SATA_PORT_DEVLINK_UP_MASK) !=
18776 	    SATA_PORT_DEVLINK_UP ||
18777 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18778 		/*
18779 		 * No device to process, anymore. Some other event processing
18780 		 * would or have already performed port info cleanup.
18781 		 * To be safe (HBA may need it), request clearing device
18782 		 * reset condition.
18783 		 */
18784 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18785 		    saddr->pmport);
18786 		if (sdinfo != NULL) {
18787 			sdinfo->satadrv_event_flags &=
18788 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18789 			/* must clear flags on cport */
18790 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18791 			    saddr->cport);
18792 			pminfo->pmult_event_flags |=
18793 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18794 		}
18795 		return;
18796 	}
18797 
18798 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18799 	    saddr->pmport);
18800 	if (sdinfo == NULL) {
18801 		return;
18802 	}
18803 	if ((sdinfo->satadrv_event_flags &
18804 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18805 		/*
18806 		 * Start tracking time for device feature restoration and
18807 		 * identification. Save current time (lbolt value).
18808 		 */
18809 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18810 	}
18811 	/* Mark device reset processing as active */
18812 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18813 
18814 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18815 	mutex_exit(&pmportinfo->pmport_mutex);
18816 
18817 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18818 	    SATA_FAILURE) {
18819 		/*
18820 		 * Restoring drive setting failed.
18821 		 * Probe the port first, to check if the port state has changed
18822 		 */
18823 		sata_device.satadev_rev = SATA_DEVICE_REV;
18824 		sata_device.satadev_addr = *saddr;
18825 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18826 
18827 		/* probe port */
18828 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18829 		    (SATA_DIP(sata_hba_inst), &sata_device);
18830 		mutex_enter(&pmportinfo->pmport_mutex);
18831 		if (rval == SATA_SUCCESS &&
18832 		    (sata_device.satadev_state &
18833 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18834 		    (sata_device.satadev_scr.sstatus  &
18835 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18836 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18837 			/*
18838 			 * We may retry this a bit later - in-process reset
18839 			 * condition should be already set.
18840 			 * Track retry time for device identification.
18841 			 */
18842 			if ((pmportinfo->pmport_dev_type &
18843 			    SATA_VALID_DEV_TYPE) != 0 &&
18844 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18845 			    sdinfo->satadrv_reset_time != 0) {
18846 				clock_t cur_time = ddi_get_lbolt();
18847 				/*
18848 				 * If the retry time limit was not
18849 				 * exceeded, retry.
18850 				 */
18851 				if ((cur_time - sdinfo->satadrv_reset_time) <
18852 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18853 					mutex_enter(
18854 					    &sata_hba_inst->satahba_mutex);
18855 					sata_hba_inst->satahba_event_flags |=
18856 					    SATA_EVNT_MAIN;
18857 					mutex_exit(
18858 					    &sata_hba_inst->satahba_mutex);
18859 					mutex_enter(&sata_mutex);
18860 					sata_event_pending |= SATA_EVNT_MAIN;
18861 					mutex_exit(&sata_mutex);
18862 					return;
18863 				}
18864 			}
18865 			/* Fail the drive */
18866 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18867 
18868 			sata_log(sata_hba_inst, CE_WARN,
18869 			    "SATA device at port %d:%d - device failed",
18870 			    saddr->cport, saddr->pmport);
18871 		} else {
18872 			/*
18873 			 * No point of retrying - some other event processing
18874 			 * would or already did port info cleanup.
18875 			 * To be safe (HBA may need it),
18876 			 * request clearing device reset condition.
18877 			 */
18878 			sdinfo->satadrv_event_flags |=
18879 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18880 		}
18881 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18882 		sdinfo->satadrv_reset_time = 0;
18883 		return;
18884 	}
18885 	/*
18886 	 * Raise the flag indicating that the next sata command could
18887 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18888 	 * reset is reported.
18889 	 */
18890 	mutex_enter(&pmportinfo->pmport_mutex);
18891 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18892 		sdinfo->satadrv_reset_time = 0;
18893 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
18894 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18895 			sdinfo->satadrv_event_flags &=
18896 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18897 			/* must clear flags on cport */
18898 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18899 			    saddr->cport);
18900 			pminfo->pmult_event_flags |=
18901 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18902 		}
18903 	}
18904 }
18905 
18906 /*
18907  * Port Link Events processing.
18908  * Every link established event may involve device reset (due to
18909  * COMRESET signal, equivalent of the hard reset) so arbitrarily
18910  * set device reset event for an attached device (if any).
18911  * If the port is in SHUTDOWN or FAILED state, ignore link events.
18912  *
18913  * The link established event processing varies, depending on the state
18914  * of the target node, HBA hotplugging capabilities, state of the port.
18915  * If the link is not active, the link established event is ignored.
18916  * If HBA cannot detect device attachment and there is no target node,
18917  * the link established event triggers device attach event processing.
18918  * Else, link established event triggers device reset event processing.
18919  *
18920  * The link lost event processing varies, depending on a HBA hotplugging
18921  * capability and the state of the port (link active or not active).
18922  * If the link is active, the lost link event is ignored.
18923  * If HBA cannot detect device removal, the lost link event triggers
18924  * device detached event processing after link lost timeout.
18925  * Else, the event is ignored.
18926  *
18927  * NOTE: Port multiplier ports events are handled by
18928  * sata_process_pmport_link_events();
18929  */
18930 static void
18931 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
18932     sata_address_t *saddr)
18933 {
18934 	sata_device_t sata_device;
18935 	sata_cport_info_t *cportinfo;
18936 	sata_drive_info_t *sdinfo;
18937 	uint32_t event_flags;
18938 	int rval;
18939 
18940 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18941 	    "Processing port %d link event(s)", saddr->cport);
18942 
18943 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18944 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18945 	event_flags = cportinfo->cport_event_flags;
18946 
18947 	/* Reset event flags first */
18948 	cportinfo->cport_event_flags &=
18949 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
18950 
18951 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
18952 	if ((cportinfo->cport_state &
18953 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18954 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18955 		    cport_mutex);
18956 		return;
18957 	}
18958 
18959 	/*
18960 	 * For the sanity sake get current port state.
18961 	 * Set device address only. Other sata_device fields should be
18962 	 * set by HBA driver.
18963 	 */
18964 	sata_device.satadev_rev = SATA_DEVICE_REV;
18965 	sata_device.satadev_addr = *saddr;
18966 	/*
18967 	 * We have to exit mutex, because the HBA probe port function may
18968 	 * block on its own mutex.
18969 	 */
18970 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18971 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18972 	    (SATA_DIP(sata_hba_inst), &sata_device);
18973 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18974 	sata_update_port_info(sata_hba_inst, &sata_device);
18975 	if (rval != SATA_SUCCESS) {
18976 		/* Something went wrong? Fail the port */
18977 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18978 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18979 		    cport_mutex);
18980 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18981 		    "SATA port %d probing failed",
18982 		    saddr->cport));
18983 		/*
18984 		 * We may want to release device info structure, but
18985 		 * it is not necessary.
18986 		 */
18987 		return;
18988 	} else {
18989 		/* port probed successfully */
18990 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
18991 	}
18992 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
18993 
18994 		if ((sata_device.satadev_scr.sstatus &
18995 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
18996 			/* Ignore event */
18997 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18998 			    "Ignoring port %d link established event - "
18999 			    "link down",
19000 			    saddr->cport);
19001 			goto linklost;
19002 		}
19003 
19004 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19005 		    "Processing port %d link established event",
19006 		    saddr->cport);
19007 
19008 		/*
19009 		 * For the sanity sake check if a device is attached - check
19010 		 * return state of a port probing.
19011 		 */
19012 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19013 			/*
19014 			 * HBA port probe indicated that there is a device
19015 			 * attached. Check if the framework had device info
19016 			 * structure attached for this device.
19017 			 */
19018 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19019 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19020 				    NULL);
19021 
19022 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19023 				if ((sdinfo->satadrv_type &
19024 				    SATA_VALID_DEV_TYPE) != 0) {
19025 					/*
19026 					 * Dev info structure is present.
19027 					 * If dev_type is set to known type in
19028 					 * the framework's drive info struct
19029 					 * then the device existed before and
19030 					 * the link was probably lost
19031 					 * momentarily - in such case
19032 					 * we may want to check device
19033 					 * identity.
19034 					 * Identity check is not supported now.
19035 					 *
19036 					 * Link established event
19037 					 * triggers device reset event.
19038 					 */
19039 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19040 					    satadrv_event_flags |=
19041 					    SATA_EVNT_DEVICE_RESET;
19042 				}
19043 			} else if (cportinfo->cport_dev_type ==
19044 			    SATA_DTYPE_NONE) {
19045 				/*
19046 				 * We got new device attached! If HBA does not
19047 				 * generate device attached events, trigger it
19048 				 * here.
19049 				 */
19050 				if (!(SATA_FEATURES(sata_hba_inst) &
19051 				    SATA_CTLF_HOTPLUG)) {
19052 					cportinfo->cport_event_flags |=
19053 					    SATA_EVNT_DEVICE_ATTACHED;
19054 				}
19055 			}
19056 			/* Reset link lost timeout */
19057 			cportinfo->cport_link_lost_time = 0;
19058 		}
19059 	}
19060 linklost:
19061 	if (event_flags & SATA_EVNT_LINK_LOST) {
19062 		if ((sata_device.satadev_scr.sstatus &
19063 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19064 			/* Ignore event */
19065 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19066 			    "Ignoring port %d link lost event - link is up",
19067 			    saddr->cport);
19068 			goto done;
19069 		}
19070 #ifdef SATA_DEBUG
19071 		if (cportinfo->cport_link_lost_time == 0) {
19072 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19073 			    "Processing port %d link lost event",
19074 			    saddr->cport);
19075 		}
19076 #endif
19077 		/*
19078 		 * When HBA cannot generate device attached/detached events,
19079 		 * we need to track link lost time and eventually generate
19080 		 * device detach event.
19081 		 */
19082 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19083 			/* We are tracking link lost time */
19084 			if (cportinfo->cport_link_lost_time == 0) {
19085 				/* save current time (lbolt value) */
19086 				cportinfo->cport_link_lost_time =
19087 				    ddi_get_lbolt();
19088 				/* just keep link lost event */
19089 				cportinfo->cport_event_flags |=
19090 				    SATA_EVNT_LINK_LOST;
19091 			} else {
19092 				clock_t cur_time = ddi_get_lbolt();
19093 				if ((cur_time -
19094 				    cportinfo->cport_link_lost_time) >=
19095 				    drv_usectohz(
19096 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19097 					/* trigger device detach event */
19098 					cportinfo->cport_event_flags |=
19099 					    SATA_EVNT_DEVICE_DETACHED;
19100 					cportinfo->cport_link_lost_time = 0;
19101 					SATADBG1(SATA_DBG_EVENTS,
19102 					    sata_hba_inst,
19103 					    "Triggering port %d "
19104 					    "device detached event",
19105 					    saddr->cport);
19106 				} else {
19107 					/* keep link lost event */
19108 					cportinfo->cport_event_flags |=
19109 					    SATA_EVNT_LINK_LOST;
19110 				}
19111 			}
19112 		}
19113 		/*
19114 		 * We could change port state to disable/delay access to
19115 		 * the attached device until the link is recovered.
19116 		 */
19117 	}
19118 done:
19119 	event_flags = cportinfo->cport_event_flags;
19120 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19121 	if (event_flags != 0) {
19122 		mutex_enter(&sata_hba_inst->satahba_mutex);
19123 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19124 		mutex_exit(&sata_hba_inst->satahba_mutex);
19125 		mutex_enter(&sata_mutex);
19126 		sata_event_pending |= SATA_EVNT_MAIN;
19127 		mutex_exit(&sata_mutex);
19128 	}
19129 }
19130 
19131 /*
19132  * Port Multiplier Port Link Events processing.
19133  */
19134 static void
19135 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19136     sata_address_t *saddr)
19137 {
19138 	sata_device_t sata_device;
19139 	sata_pmport_info_t *pmportinfo = NULL;
19140 	sata_drive_info_t *sdinfo = NULL;
19141 	uint32_t event_flags;
19142 	uint8_t cport = saddr->cport;
19143 	uint8_t pmport = saddr->pmport;
19144 	int rval;
19145 
19146 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19147 	    "Processing port %d:%d link event(s)",
19148 	    cport, pmport);
19149 
19150 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19151 	mutex_enter(&pmportinfo->pmport_mutex);
19152 	event_flags = pmportinfo->pmport_event_flags;
19153 
19154 	/* Reset event flags first */
19155 	pmportinfo->pmport_event_flags &=
19156 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19157 
19158 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19159 	if ((pmportinfo->pmport_state &
19160 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19161 		mutex_exit(&pmportinfo->pmport_mutex);
19162 		return;
19163 	}
19164 
19165 	/*
19166 	 * For the sanity sake get current port state.
19167 	 * Set device address only. Other sata_device fields should be
19168 	 * set by HBA driver.
19169 	 */
19170 	sata_device.satadev_rev = SATA_DEVICE_REV;
19171 	sata_device.satadev_addr = *saddr;
19172 	/*
19173 	 * We have to exit mutex, because the HBA probe port function may
19174 	 * block on its own mutex.
19175 	 */
19176 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19177 	    saddr->pmport));
19178 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19179 	    (SATA_DIP(sata_hba_inst), &sata_device);
19180 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19181 	    saddr->pmport));
19182 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19183 	if (rval != SATA_SUCCESS) {
19184 		/* Something went wrong? Fail the port */
19185 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19186 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19187 		    saddr->pmport));
19188 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19189 		    "SATA port %d:%d probing failed",
19190 		    saddr->cport, saddr->pmport));
19191 		/*
19192 		 * We may want to release device info structure, but
19193 		 * it is not necessary.
19194 		 */
19195 		return;
19196 	} else {
19197 		/* port probed successfully */
19198 		pmportinfo->pmport_state |=
19199 		    SATA_STATE_PROBED | SATA_STATE_READY;
19200 	}
19201 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19202 	    saddr->cport, saddr->pmport));
19203 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19204 	    saddr->cport, saddr->pmport));
19205 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19206 
19207 		if ((sata_device.satadev_scr.sstatus &
19208 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19209 			/* Ignore event */
19210 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19211 			    "Ignoring port %d:%d link established event - "
19212 			    "link down",
19213 			    saddr->cport, saddr->pmport);
19214 			goto linklost;
19215 		}
19216 
19217 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19218 		    "Processing port %d:%d link established event",
19219 		    cport, pmport);
19220 
19221 		/*
19222 		 * For the sanity sake check if a device is attached - check
19223 		 * return state of a port probing.
19224 		 */
19225 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19226 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19227 			/*
19228 			 * HBA port probe indicated that there is a device
19229 			 * attached. Check if the framework had device info
19230 			 * structure attached for this device.
19231 			 */
19232 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19233 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19234 				    NULL);
19235 
19236 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19237 				if ((sdinfo->satadrv_type &
19238 				    SATA_VALID_DEV_TYPE) != 0) {
19239 					/*
19240 					 * Dev info structure is present.
19241 					 * If dev_type is set to known type in
19242 					 * the framework's drive info struct
19243 					 * then the device existed before and
19244 					 * the link was probably lost
19245 					 * momentarily - in such case
19246 					 * we may want to check device
19247 					 * identity.
19248 					 * Identity check is not supported now.
19249 					 *
19250 					 * Link established event
19251 					 * triggers device reset event.
19252 					 */
19253 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19254 					    satadrv_event_flags |=
19255 					    SATA_EVNT_DEVICE_RESET;
19256 				}
19257 			} else if (pmportinfo->pmport_dev_type ==
19258 			    SATA_DTYPE_NONE) {
19259 				/*
19260 				 * We got new device attached! If HBA does not
19261 				 * generate device attached events, trigger it
19262 				 * here.
19263 				 */
19264 				if (!(SATA_FEATURES(sata_hba_inst) &
19265 				    SATA_CTLF_HOTPLUG)) {
19266 					pmportinfo->pmport_event_flags |=
19267 					    SATA_EVNT_DEVICE_ATTACHED;
19268 				}
19269 			}
19270 			/* Reset link lost timeout */
19271 			pmportinfo->pmport_link_lost_time = 0;
19272 		}
19273 	}
19274 linklost:
19275 	if (event_flags & SATA_EVNT_LINK_LOST) {
19276 #ifdef SATA_DEBUG
19277 		if (pmportinfo->pmport_link_lost_time == 0) {
19278 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19279 			    "Processing port %d:%d link lost event",
19280 			    saddr->cport, saddr->pmport);
19281 		}
19282 #endif
19283 		if ((sata_device.satadev_scr.sstatus &
19284 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19285 			/* Ignore event */
19286 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19287 			    "Ignoring port %d:%d link lost event - link is up",
19288 			    saddr->cport, saddr->pmport);
19289 			goto done;
19290 		}
19291 		/*
19292 		 * When HBA cannot generate device attached/detached events,
19293 		 * we need to track link lost time and eventually generate
19294 		 * device detach event.
19295 		 */
19296 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19297 			/* We are tracking link lost time */
19298 			if (pmportinfo->pmport_link_lost_time == 0) {
19299 				/* save current time (lbolt value) */
19300 				pmportinfo->pmport_link_lost_time =
19301 				    ddi_get_lbolt();
19302 				/* just keep link lost event */
19303 				pmportinfo->pmport_event_flags |=
19304 				    SATA_EVNT_LINK_LOST;
19305 			} else {
19306 				clock_t cur_time = ddi_get_lbolt();
19307 				if ((cur_time -
19308 				    pmportinfo->pmport_link_lost_time) >=
19309 				    drv_usectohz(
19310 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19311 					/* trigger device detach event */
19312 					pmportinfo->pmport_event_flags |=
19313 					    SATA_EVNT_DEVICE_DETACHED;
19314 					pmportinfo->pmport_link_lost_time = 0;
19315 					SATADBG2(SATA_DBG_EVENTS,
19316 					    sata_hba_inst,
19317 					    "Triggering port %d:%d "
19318 					    "device detached event",
19319 					    saddr->cport, saddr->pmport);
19320 				} else {
19321 					/* keep link lost event */
19322 					pmportinfo->pmport_event_flags |=
19323 					    SATA_EVNT_LINK_LOST;
19324 				}
19325 			}
19326 		}
19327 		/*
19328 		 * We could change port state to disable/delay access to
19329 		 * the attached device until the link is recovered.
19330 		 */
19331 	}
19332 done:
19333 	event_flags = pmportinfo->pmport_event_flags;
19334 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19335 	    saddr->pmport));
19336 	if (event_flags != 0) {
19337 		mutex_enter(&sata_hba_inst->satahba_mutex);
19338 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19339 		mutex_exit(&sata_hba_inst->satahba_mutex);
19340 		mutex_enter(&sata_mutex);
19341 		sata_event_pending |= SATA_EVNT_MAIN;
19342 		mutex_exit(&sata_mutex);
19343 	}
19344 }
19345 
19346 /*
19347  * Device Detached Event processing.
19348  * Port is probed to find if a device is really gone. If so,
19349  * the device info structure is detached from the SATA port info structure
19350  * and released.
19351  * Port status is updated.
19352  *
19353  * NOTE: Port multiplier ports events are handled by
19354  * sata_process_pmdevice_detached()
19355  */
19356 static void
19357 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19358     sata_address_t *saddr)
19359 {
19360 	sata_cport_info_t *cportinfo;
19361 	sata_pmport_info_t *pmportinfo;
19362 	sata_drive_info_t *sdevinfo;
19363 	sata_device_t sata_device;
19364 	sata_address_t pmport_addr;
19365 	char name[16];
19366 	uint8_t cport = saddr->cport;
19367 	int npmport;
19368 	int rval;
19369 
19370 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19371 	    "Processing port %d device detached", saddr->cport);
19372 
19373 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19374 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19375 	/* Clear event flag */
19376 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19377 
19378 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19379 	if ((cportinfo->cport_state &
19380 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19381 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19382 		    cport_mutex);
19383 		return;
19384 	}
19385 	/* For sanity, re-probe the port */
19386 	sata_device.satadev_rev = SATA_DEVICE_REV;
19387 	sata_device.satadev_addr = *saddr;
19388 
19389 	/*
19390 	 * We have to exit mutex, because the HBA probe port function may
19391 	 * block on its own mutex.
19392 	 */
19393 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19394 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19395 	    (SATA_DIP(sata_hba_inst), &sata_device);
19396 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19397 	sata_update_port_info(sata_hba_inst, &sata_device);
19398 	if (rval != SATA_SUCCESS) {
19399 		/* Something went wrong? Fail the port */
19400 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19401 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19402 		    cport_mutex);
19403 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19404 		    "SATA port %d probing failed",
19405 		    saddr->cport));
19406 		/*
19407 		 * We may want to release device info structure, but
19408 		 * it is not necessary.
19409 		 */
19410 		return;
19411 	} else {
19412 		/* port probed successfully */
19413 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19414 	}
19415 	/*
19416 	 * Check if a device is still attached. For sanity, check also
19417 	 * link status - if no link, there is no device.
19418 	 */
19419 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19420 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19421 	    SATA_DTYPE_NONE) {
19422 		/*
19423 		 * Device is still attached - ignore detach event.
19424 		 */
19425 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19426 		    cport_mutex);
19427 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19428 		    "Ignoring detach - device still attached to port %d",
19429 		    sata_device.satadev_addr.cport);
19430 		return;
19431 	}
19432 	/*
19433 	 * We need to detach and release device info structure here
19434 	 */
19435 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19436 		/*
19437 		 * A port-multiplier is removed.
19438 		 *
19439 		 * Calling sata_process_pmdevice_detached() does not work
19440 		 * here. The port multiplier is gone, so we cannot probe
19441 		 * sub-port any more and all pmult-related data structure must
19442 		 * be de-allocated immediately. Following structure of every
19443 		 * implemented sub-port behind the pmult are required to
19444 		 * released.
19445 		 *
19446 		 *   - attachment point
19447 		 *   - target node
19448 		 *   - sata_drive_info
19449 		 *   - sata_pmport_info
19450 		 */
19451 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19452 		    cport); npmport ++) {
19453 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19454 			    sata_hba_inst,
19455 			    "Detaching target node at port %d:%d",
19456 			    cport, npmport);
19457 
19458 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19459 
19460 			/* Remove attachment point. */
19461 			name[0] = '\0';
19462 			(void) sprintf(name, "%d.%d", cport, npmport);
19463 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19464 			sata_log(sata_hba_inst, CE_NOTE,
19465 			    "Remove attachment point of port %d:%d",
19466 			    cport, npmport);
19467 
19468 			/* Remove target node */
19469 			pmport_addr.cport = cport;
19470 			pmport_addr.pmport = (uint8_t)npmport;
19471 			pmport_addr.qual = SATA_ADDR_PMPORT;
19472 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19473 
19474 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19475 
19476 			/* Release sata_pmport_info & sata_drive_info. */
19477 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19478 			    cport, npmport);
19479 			ASSERT(pmportinfo != NULL);
19480 
19481 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19482 			if (sdevinfo != NULL) {
19483 				(void) kmem_free((void *) sdevinfo,
19484 				    sizeof (sata_drive_info_t));
19485 			}
19486 
19487 			/* Release sata_pmport_info at last */
19488 			(void) kmem_free((void *) pmportinfo,
19489 			    sizeof (sata_pmport_info_t));
19490 		}
19491 
19492 		/* Finally, release sata_pmult_info */
19493 		(void) kmem_free((void *)
19494 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19495 		    sizeof (sata_pmult_info_t));
19496 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19497 
19498 		sata_log(sata_hba_inst, CE_WARN,
19499 		    "SATA port-multiplier detached at port %d", cport);
19500 
19501 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19502 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19503 		    saddr->cport)->cport_mutex);
19504 	} else {
19505 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19506 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19507 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19508 			(void) kmem_free((void *)sdevinfo,
19509 			    sizeof (sata_drive_info_t));
19510 		}
19511 		sata_log(sata_hba_inst, CE_WARN,
19512 		    "SATA device detached at port %d", cport);
19513 
19514 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19515 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19516 		    saddr->cport)->cport_mutex);
19517 
19518 		/*
19519 		 * Try to offline a device and remove target node
19520 		 * if it still exists
19521 		 */
19522 		sata_remove_target_node(sata_hba_inst, saddr);
19523 	}
19524 
19525 
19526 	/*
19527 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19528 	 * with the hint: SE_HINT_REMOVE
19529 	 */
19530 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19531 }
19532 
19533 /*
19534  * Port Multiplier Port Device Deattached Event processing.
19535  *
19536  * NOTE: No Mutex should be hold.
19537  */
19538 static void
19539 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19540     sata_address_t *saddr)
19541 {
19542 	sata_pmport_info_t *pmportinfo;
19543 	sata_drive_info_t *sdevinfo;
19544 	sata_device_t sata_device;
19545 	int rval;
19546 	uint8_t cport, pmport;
19547 
19548 	cport = saddr->cport;
19549 	pmport = saddr->pmport;
19550 
19551 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19552 	    "Processing port %d:%d device detached",
19553 	    cport, pmport);
19554 
19555 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19556 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19557 
19558 	/* Clear event flag */
19559 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19560 
19561 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19562 	if ((pmportinfo->pmport_state &
19563 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19564 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19565 		return;
19566 	}
19567 	/* For sanity, re-probe the port */
19568 	sata_device.satadev_rev = SATA_DEVICE_REV;
19569 	sata_device.satadev_addr = *saddr;
19570 
19571 	/*
19572 	 * We have to exit mutex, because the HBA probe port function may
19573 	 * block on its own mutex.
19574 	 */
19575 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19576 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19577 	    (SATA_DIP(sata_hba_inst), &sata_device);
19578 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19579 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19580 	if (rval != SATA_SUCCESS) {
19581 		/* Something went wrong? Fail the port */
19582 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19583 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19584 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19585 		    "SATA port %d:%d probing failed",
19586 		    saddr->pmport));
19587 		/*
19588 		 * We may want to release device info structure, but
19589 		 * it is not necessary.
19590 		 */
19591 		return;
19592 	} else {
19593 		/* port probed successfully */
19594 		pmportinfo->pmport_state |=
19595 		    SATA_STATE_PROBED | SATA_STATE_READY;
19596 	}
19597 	/*
19598 	 * Check if a device is still attached. For sanity, check also
19599 	 * link status - if no link, there is no device.
19600 	 */
19601 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19602 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19603 	    SATA_DTYPE_NONE) {
19604 		/*
19605 		 * Device is still attached - ignore detach event.
19606 		 */
19607 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19608 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19609 		    "Ignoring detach - device still attached to port %d",
19610 		    sata_device.satadev_addr.pmport);
19611 		return;
19612 	}
19613 	/*
19614 	 * We need to detach and release device info structure here
19615 	 */
19616 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19617 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19618 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19619 		(void) kmem_free((void *)sdevinfo,
19620 		    sizeof (sata_drive_info_t));
19621 	}
19622 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19623 	/*
19624 	 * Device cannot be reached anymore, even if the target node may be
19625 	 * still present.
19626 	 */
19627 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19628 
19629 	/*
19630 	 * Try to offline a device and remove target node if it still exists
19631 	 */
19632 	sata_remove_target_node(sata_hba_inst, saddr);
19633 
19634 	/*
19635 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19636 	 * with the hint: SE_HINT_REMOVE
19637 	 */
19638 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19639 }
19640 
19641 
19642 /*
19643  * Device Attached Event processing.
19644  * Port state is checked to verify that a device is really attached. If so,
19645  * the device info structure is created and attached to the SATA port info
19646  * structure.
19647  *
19648  * If attached device cannot be identified or set-up, the retry for the
19649  * attach processing is set-up. Subsequent daemon run would try again to
19650  * identify the device, until the time limit is reached
19651  * (SATA_DEV_IDENTIFY_TIMEOUT).
19652  *
19653  * This function cannot be called in interrupt context (it may sleep).
19654  *
19655  * NOTE: Port multiplier ports events are handled by
19656  * sata_process_pmdevice_attached()
19657  */
19658 static void
19659 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19660     sata_address_t *saddr)
19661 {
19662 	sata_cport_info_t *cportinfo = NULL;
19663 	sata_drive_info_t *sdevinfo = NULL;
19664 	sata_pmult_info_t *pmultinfo = NULL;
19665 	sata_pmport_info_t *pmportinfo = NULL;
19666 	sata_device_t sata_device;
19667 	dev_info_t *tdip;
19668 	uint32_t event_flags = 0, pmult_event_flags = 0;
19669 	int rval;
19670 	int npmport;
19671 
19672 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19673 	    "Processing port %d device attached", saddr->cport);
19674 
19675 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19676 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19677 
19678 	/* Clear attach event flag first */
19679 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19680 
19681 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19682 	if ((cportinfo->cport_state &
19683 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19684 		cportinfo->cport_dev_attach_time = 0;
19685 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19686 		    cport_mutex);
19687 		return;
19688 	}
19689 
19690 	/*
19691 	 * If the sata_drive_info structure is found attached to the port info,
19692 	 * despite the fact the device was removed and now it is re-attached,
19693 	 * the old drive info structure was not removed.
19694 	 * Arbitrarily release device info structure.
19695 	 */
19696 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19697 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19698 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19699 		(void) kmem_free((void *)sdevinfo,
19700 		    sizeof (sata_drive_info_t));
19701 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19702 		    "Arbitrarily detaching old device info.", NULL);
19703 	}
19704 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19705 
19706 	/* For sanity, re-probe the port */
19707 	sata_device.satadev_rev = SATA_DEVICE_REV;
19708 	sata_device.satadev_addr = *saddr;
19709 
19710 	/*
19711 	 * We have to exit mutex, because the HBA probe port function may
19712 	 * block on its own mutex.
19713 	 */
19714 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19715 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19716 	    (SATA_DIP(sata_hba_inst), &sata_device);
19717 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19718 	sata_update_port_info(sata_hba_inst, &sata_device);
19719 	if (rval != SATA_SUCCESS) {
19720 		/* Something went wrong? Fail the port */
19721 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19722 		cportinfo->cport_dev_attach_time = 0;
19723 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19724 		    cport_mutex);
19725 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19726 		    "SATA port %d probing failed",
19727 		    saddr->cport));
19728 		return;
19729 	} else {
19730 		/* port probed successfully */
19731 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19732 	}
19733 	/*
19734 	 * Check if a device is still attached. For sanity, check also
19735 	 * link status - if no link, there is no device.
19736 	 */
19737 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19738 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19739 	    SATA_DTYPE_NONE) {
19740 		/*
19741 		 * No device - ignore attach event.
19742 		 */
19743 		cportinfo->cport_dev_attach_time = 0;
19744 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19745 		    cport_mutex);
19746 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19747 		    "Ignoring attach - no device connected to port %d",
19748 		    sata_device.satadev_addr.cport);
19749 		return;
19750 	}
19751 
19752 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19753 	/*
19754 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19755 	 * with the hint: SE_HINT_INSERT
19756 	 */
19757 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19758 
19759 	/*
19760 	 * Port reprobing will take care of the creation of the device
19761 	 * info structure and determination of the device type.
19762 	 */
19763 	sata_device.satadev_addr = *saddr;
19764 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19765 	    SATA_DEV_IDENTIFY_NORETRY);
19766 
19767 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19768 	    cport_mutex);
19769 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19770 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19771 		/* Some device is attached to the port */
19772 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19773 			/*
19774 			 * A device was not successfully attached.
19775 			 * Track retry time for device identification.
19776 			 */
19777 			if (cportinfo->cport_dev_attach_time != 0) {
19778 				clock_t cur_time = ddi_get_lbolt();
19779 				/*
19780 				 * If the retry time limit was not exceeded,
19781 				 * reinstate attach event.
19782 				 */
19783 				if ((cur_time -
19784 				    cportinfo->cport_dev_attach_time) <
19785 				    drv_usectohz(
19786 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19787 					/* OK, restore attach event */
19788 					cportinfo->cport_event_flags |=
19789 					    SATA_EVNT_DEVICE_ATTACHED;
19790 				} else {
19791 					/* Timeout - cannot identify device */
19792 					cportinfo->cport_dev_attach_time = 0;
19793 					sata_log(sata_hba_inst,
19794 					    CE_WARN,
19795 					    "Could not identify SATA device "
19796 					    "at port %d",
19797 					    saddr->cport);
19798 				}
19799 			} else {
19800 				/*
19801 				 * Start tracking time for device
19802 				 * identification.
19803 				 * Save current time (lbolt value).
19804 				 */
19805 				cportinfo->cport_dev_attach_time =
19806 				    ddi_get_lbolt();
19807 				/* Restore attach event */
19808 				cportinfo->cport_event_flags |=
19809 				    SATA_EVNT_DEVICE_ATTACHED;
19810 			}
19811 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19812 			cportinfo->cport_dev_attach_time = 0;
19813 			sata_log(sata_hba_inst, CE_NOTE,
19814 			    "SATA port-multiplier detected at port %d",
19815 			    saddr->cport);
19816 
19817 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19818 				/* Log the info of new port multiplier */
19819 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19820 				    saddr->cport)->cport_mutex);
19821 				sata_show_pmult_info(sata_hba_inst,
19822 				    &sata_device);
19823 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19824 				    saddr->cport)->cport_mutex);
19825 			}
19826 
19827 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19828 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19829 			for (npmport = 0; npmport <
19830 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19831 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19832 				    saddr->cport, npmport);
19833 				ASSERT(pmportinfo != NULL);
19834 
19835 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19836 				    saddr->cport)->cport_mutex);
19837 				mutex_enter(&pmportinfo->pmport_mutex);
19838 				/* Marked all pmports with link events. */
19839 				pmportinfo->pmport_event_flags =
19840 				    SATA_EVNT_LINK_ESTABLISHED;
19841 				pmult_event_flags |=
19842 				    pmportinfo->pmport_event_flags;
19843 				mutex_exit(&pmportinfo->pmport_mutex);
19844 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19845 				    saddr->cport)->cport_mutex);
19846 			}
19847 			/* Auto-online is not available for PMult now. */
19848 
19849 		} else {
19850 			/*
19851 			 * If device was successfully attached, the subsequent
19852 			 * action depends on a state of the
19853 			 * sata_auto_online variable. If it is set to zero.
19854 			 * an explicit 'configure' command will be needed to
19855 			 * configure it. If its value is non-zero, we will
19856 			 * attempt to online (configure) the device.
19857 			 * First, log the message indicating that a device
19858 			 * was attached.
19859 			 */
19860 			cportinfo->cport_dev_attach_time = 0;
19861 			sata_log(sata_hba_inst, CE_WARN,
19862 			    "SATA device detected at port %d", saddr->cport);
19863 
19864 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19865 				sata_drive_info_t new_sdinfo;
19866 
19867 				/* Log device info data */
19868 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19869 				    cportinfo));
19870 				sata_show_drive_info(sata_hba_inst,
19871 				    &new_sdinfo);
19872 			}
19873 
19874 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19875 			    saddr->cport)->cport_mutex);
19876 
19877 			/*
19878 			 * Make sure that there is no target node for that
19879 			 * device. If so, release it. It should not happen,
19880 			 * unless we had problem removing the node when
19881 			 * device was detached.
19882 			 */
19883 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19884 			    saddr->cport, saddr->pmport);
19885 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19886 			    saddr->cport)->cport_mutex);
19887 			if (tdip != NULL) {
19888 
19889 #ifdef SATA_DEBUG
19890 				if ((cportinfo->cport_event_flags &
19891 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19892 					sata_log(sata_hba_inst, CE_WARN,
19893 					    "sata_process_device_attached: "
19894 					    "old device target node exists!");
19895 #endif
19896 				/*
19897 				 * target node exists - try to unconfigure
19898 				 * device and remove the node.
19899 				 */
19900 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19901 				    saddr->cport)->cport_mutex);
19902 				rval = ndi_devi_offline(tdip,
19903 				    NDI_DEVI_REMOVE);
19904 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19905 				    saddr->cport)->cport_mutex);
19906 
19907 				if (rval == NDI_SUCCESS) {
19908 					cportinfo->cport_event_flags &=
19909 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19910 					cportinfo->cport_tgtnode_clean = B_TRUE;
19911 				} else {
19912 					/*
19913 					 * PROBLEM - the target node remained
19914 					 * and it belongs to a previously
19915 					 * attached device.
19916 					 * This happens when the file was open
19917 					 * or the node was waiting for
19918 					 * resources at the time the
19919 					 * associated device was removed.
19920 					 * Instruct event daemon to retry the
19921 					 * cleanup later.
19922 					 */
19923 					sata_log(sata_hba_inst,
19924 					    CE_WARN,
19925 					    "Application(s) accessing "
19926 					    "previously attached SATA "
19927 					    "device have to release "
19928 					    "it before newly inserted "
19929 					    "device can be made accessible.",
19930 					    saddr->cport);
19931 					cportinfo->cport_event_flags |=
19932 					    SATA_EVNT_TARGET_NODE_CLEANUP;
19933 					cportinfo->cport_tgtnode_clean =
19934 					    B_FALSE;
19935 				}
19936 			}
19937 			if (sata_auto_online != 0) {
19938 				cportinfo->cport_event_flags |=
19939 				    SATA_EVNT_AUTOONLINE_DEVICE;
19940 			}
19941 
19942 		}
19943 	} else {
19944 		cportinfo->cport_dev_attach_time = 0;
19945 	}
19946 
19947 	event_flags = cportinfo->cport_event_flags;
19948 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19949 	if (event_flags != 0 || pmult_event_flags != 0) {
19950 		mutex_enter(&sata_hba_inst->satahba_mutex);
19951 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19952 		mutex_exit(&sata_hba_inst->satahba_mutex);
19953 		mutex_enter(&sata_mutex);
19954 		sata_event_pending |= SATA_EVNT_MAIN;
19955 		mutex_exit(&sata_mutex);
19956 	}
19957 }
19958 
19959 /*
19960  * Port Multiplier Port Device Attached Event processing.
19961  *
19962  * NOTE: No Mutex should be hold.
19963  */
19964 static void
19965 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
19966     sata_address_t *saddr)
19967 {
19968 	sata_pmport_info_t *pmportinfo;
19969 	sata_drive_info_t *sdinfo;
19970 	sata_device_t sata_device;
19971 	dev_info_t *tdip;
19972 	uint32_t event_flags;
19973 	uint8_t cport = saddr->cport;
19974 	uint8_t pmport = saddr->pmport;
19975 	int rval;
19976 
19977 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19978 	    "Processing port %d:%d device attached", cport, pmport);
19979 
19980 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19981 
19982 	mutex_enter(&pmportinfo->pmport_mutex);
19983 
19984 	/* Clear attach event flag first */
19985 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19986 
19987 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19988 	if ((pmportinfo->pmport_state &
19989 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19990 		pmportinfo->pmport_dev_attach_time = 0;
19991 		mutex_exit(&pmportinfo->pmport_mutex);
19992 		return;
19993 	}
19994 
19995 	/*
19996 	 * If the sata_drive_info structure is found attached to the port info,
19997 	 * despite the fact the device was removed and now it is re-attached,
19998 	 * the old drive info structure was not removed.
19999 	 * Arbitrarily release device info structure.
20000 	 */
20001 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20002 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20003 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20004 		(void) kmem_free((void *)sdinfo,
20005 		    sizeof (sata_drive_info_t));
20006 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20007 		    "Arbitrarily detaching old device info.", NULL);
20008 	}
20009 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20010 
20011 	/* For sanity, re-probe the port */
20012 	sata_device.satadev_rev = SATA_DEVICE_REV;
20013 	sata_device.satadev_addr = *saddr;
20014 
20015 	/*
20016 	 * We have to exit mutex, because the HBA probe port function may
20017 	 * block on its own mutex.
20018 	 */
20019 	mutex_exit(&pmportinfo->pmport_mutex);
20020 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20021 	    (SATA_DIP(sata_hba_inst), &sata_device);
20022 	mutex_enter(&pmportinfo->pmport_mutex);
20023 
20024 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20025 	if (rval != SATA_SUCCESS) {
20026 		/* Something went wrong? Fail the port */
20027 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20028 		pmportinfo->pmport_dev_attach_time = 0;
20029 		mutex_exit(&pmportinfo->pmport_mutex);
20030 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20031 		    "SATA port %d:%d probing failed", cport, pmport));
20032 		return;
20033 	} else {
20034 		/* pmport probed successfully */
20035 		pmportinfo->pmport_state |=
20036 		    SATA_STATE_PROBED | SATA_STATE_READY;
20037 	}
20038 	/*
20039 	 * Check if a device is still attached. For sanity, check also
20040 	 * link status - if no link, there is no device.
20041 	 */
20042 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20043 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20044 	    SATA_DTYPE_NONE) {
20045 		/*
20046 		 * No device - ignore attach event.
20047 		 */
20048 		pmportinfo->pmport_dev_attach_time = 0;
20049 		mutex_exit(&pmportinfo->pmport_mutex);
20050 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20051 		    "Ignoring attach - no device connected to port %d:%d",
20052 		    cport, pmport);
20053 		return;
20054 	}
20055 
20056 	mutex_exit(&pmportinfo->pmport_mutex);
20057 	/*
20058 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20059 	 * with the hint: SE_HINT_INSERT
20060 	 */
20061 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20062 
20063 	/*
20064 	 * Port reprobing will take care of the creation of the device
20065 	 * info structure and determination of the device type.
20066 	 */
20067 	sata_device.satadev_addr = *saddr;
20068 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20069 	    SATA_DEV_IDENTIFY_NORETRY);
20070 
20071 	mutex_enter(&pmportinfo->pmport_mutex);
20072 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20073 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20074 		/* Some device is attached to the port */
20075 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20076 			/*
20077 			 * A device was not successfully attached.
20078 			 * Track retry time for device identification.
20079 			 */
20080 			if (pmportinfo->pmport_dev_attach_time != 0) {
20081 				clock_t cur_time = ddi_get_lbolt();
20082 				/*
20083 				 * If the retry time limit was not exceeded,
20084 				 * reinstate attach event.
20085 				 */
20086 				if ((cur_time -
20087 				    pmportinfo->pmport_dev_attach_time) <
20088 				    drv_usectohz(
20089 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20090 					/* OK, restore attach event */
20091 					pmportinfo->pmport_event_flags |=
20092 					    SATA_EVNT_DEVICE_ATTACHED;
20093 				} else {
20094 					/* Timeout - cannot identify device */
20095 					pmportinfo->pmport_dev_attach_time = 0;
20096 					sata_log(sata_hba_inst, CE_WARN,
20097 					    "Could not identify SATA device "
20098 					    "at port %d:%d",
20099 					    cport, pmport);
20100 				}
20101 			} else {
20102 				/*
20103 				 * Start tracking time for device
20104 				 * identification.
20105 				 * Save current time (lbolt value).
20106 				 */
20107 				pmportinfo->pmport_dev_attach_time =
20108 				    ddi_get_lbolt();
20109 				/* Restore attach event */
20110 				pmportinfo->pmport_event_flags |=
20111 				    SATA_EVNT_DEVICE_ATTACHED;
20112 			}
20113 		} else {
20114 			/*
20115 			 * If device was successfully attached, the subsequent
20116 			 * action depends on a state of the
20117 			 * sata_auto_online variable. If it is set to zero.
20118 			 * an explicit 'configure' command will be needed to
20119 			 * configure it. If its value is non-zero, we will
20120 			 * attempt to online (configure) the device.
20121 			 * First, log the message indicating that a device
20122 			 * was attached.
20123 			 */
20124 			pmportinfo->pmport_dev_attach_time = 0;
20125 			sata_log(sata_hba_inst, CE_WARN,
20126 			    "SATA device detected at port %d:%d",
20127 			    cport, pmport);
20128 
20129 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20130 				sata_drive_info_t new_sdinfo;
20131 
20132 				/* Log device info data */
20133 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20134 				    pmportinfo));
20135 				sata_show_drive_info(sata_hba_inst,
20136 				    &new_sdinfo);
20137 			}
20138 
20139 			mutex_exit(&pmportinfo->pmport_mutex);
20140 
20141 			/*
20142 			 * Make sure that there is no target node for that
20143 			 * device. If so, release it. It should not happen,
20144 			 * unless we had problem removing the node when
20145 			 * device was detached.
20146 			 */
20147 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20148 			    saddr->cport, saddr->pmport);
20149 			mutex_enter(&pmportinfo->pmport_mutex);
20150 			if (tdip != NULL) {
20151 
20152 #ifdef SATA_DEBUG
20153 				if ((pmportinfo->pmport_event_flags &
20154 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20155 					sata_log(sata_hba_inst, CE_WARN,
20156 					    "sata_process_device_attached: "
20157 					    "old device target node exists!");
20158 #endif
20159 				/*
20160 				 * target node exists - try to unconfigure
20161 				 * device and remove the node.
20162 				 */
20163 				mutex_exit(&pmportinfo->pmport_mutex);
20164 				rval = ndi_devi_offline(tdip,
20165 				    NDI_DEVI_REMOVE);
20166 				mutex_enter(&pmportinfo->pmport_mutex);
20167 
20168 				if (rval == NDI_SUCCESS) {
20169 					pmportinfo->pmport_event_flags &=
20170 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20171 					pmportinfo->pmport_tgtnode_clean =
20172 					    B_TRUE;
20173 				} else {
20174 					/*
20175 					 * PROBLEM - the target node remained
20176 					 * and it belongs to a previously
20177 					 * attached device.
20178 					 * This happens when the file was open
20179 					 * or the node was waiting for
20180 					 * resources at the time the
20181 					 * associated device was removed.
20182 					 * Instruct event daemon to retry the
20183 					 * cleanup later.
20184 					 */
20185 					sata_log(sata_hba_inst,
20186 					    CE_WARN,
20187 					    "Application(s) accessing "
20188 					    "previously attached SATA "
20189 					    "device have to release "
20190 					    "it before newly inserted "
20191 					    "device can be made accessible."
20192 					    "at port %d:%d",
20193 					    cport, pmport);
20194 					pmportinfo->pmport_event_flags |=
20195 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20196 					pmportinfo->pmport_tgtnode_clean =
20197 					    B_FALSE;
20198 				}
20199 			}
20200 			if (sata_auto_online != 0) {
20201 				pmportinfo->pmport_event_flags |=
20202 				    SATA_EVNT_AUTOONLINE_DEVICE;
20203 			}
20204 
20205 		}
20206 	} else {
20207 		pmportinfo->pmport_dev_attach_time = 0;
20208 	}
20209 
20210 	event_flags = pmportinfo->pmport_event_flags;
20211 	mutex_exit(&pmportinfo->pmport_mutex);
20212 	if (event_flags != 0) {
20213 		mutex_enter(&sata_hba_inst->satahba_mutex);
20214 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20215 		mutex_exit(&sata_hba_inst->satahba_mutex);
20216 		mutex_enter(&sata_mutex);
20217 		sata_event_pending |= SATA_EVNT_MAIN;
20218 		mutex_exit(&sata_mutex);
20219 	}
20220 
20221 	/* clear the reset_in_progress events */
20222 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20223 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20224 			/* must clear flags on cport */
20225 			sata_pmult_info_t *pminfo =
20226 			    SATA_PMULT_INFO(sata_hba_inst,
20227 			    saddr->cport);
20228 			pminfo->pmult_event_flags |=
20229 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20230 		}
20231 	}
20232 }
20233 
20234 /*
20235  * Device Target Node Cleanup Event processing.
20236  * If the target node associated with a sata port device is in
20237  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20238  * If the target node cannot be removed, the event flag is left intact,
20239  * so that event daemon may re-run this function later.
20240  *
20241  * This function cannot be called in interrupt context (it may sleep).
20242  *
20243  * NOTE: Processes cport events only, not port multiplier ports.
20244  */
20245 static void
20246 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20247     sata_address_t *saddr)
20248 {
20249 	sata_cport_info_t *cportinfo;
20250 	dev_info_t *tdip;
20251 
20252 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20253 	    "Processing port %d device target node cleanup", saddr->cport);
20254 
20255 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20256 
20257 	/*
20258 	 * Check if there is target node for that device and it is in the
20259 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20260 	 */
20261 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20262 	    saddr->pmport);
20263 	if (tdip != NULL) {
20264 		/*
20265 		 * target node exists - check if it is target node of
20266 		 * a removed device.
20267 		 */
20268 		if (sata_check_device_removed(tdip) == B_TRUE) {
20269 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20270 			    "sata_process_target_node_cleanup: "
20271 			    "old device target node exists!", NULL);
20272 			/*
20273 			 * Unconfigure and remove the target node
20274 			 */
20275 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20276 			    NDI_SUCCESS) {
20277 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20278 				    saddr->cport)->cport_mutex);
20279 				cportinfo->cport_event_flags &=
20280 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20281 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20282 				    saddr->cport)->cport_mutex);
20283 				return;
20284 			}
20285 			/*
20286 			 * Event daemon will retry the cleanup later.
20287 			 */
20288 			mutex_enter(&sata_hba_inst->satahba_mutex);
20289 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20290 			mutex_exit(&sata_hba_inst->satahba_mutex);
20291 			mutex_enter(&sata_mutex);
20292 			sata_event_pending |= SATA_EVNT_MAIN;
20293 			mutex_exit(&sata_mutex);
20294 		}
20295 	} else {
20296 		if (saddr->qual == SATA_ADDR_CPORT ||
20297 		    saddr->qual == SATA_ADDR_DCPORT) {
20298 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20299 			    saddr->cport)->cport_mutex);
20300 			cportinfo->cport_event_flags &=
20301 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20302 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20303 			    saddr->cport)->cport_mutex);
20304 		} else {
20305 			/* sanity check */
20306 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20307 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20308 			    saddr->cport) == NULL)
20309 				return;
20310 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20311 			    saddr->pmport) == NULL)
20312 				return;
20313 
20314 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20315 			    saddr->cport, saddr->pmport)->pmport_mutex);
20316 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20317 			    saddr->pmport)->pmport_event_flags &=
20318 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20319 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20320 			    saddr->cport, saddr->pmport)->pmport_mutex);
20321 		}
20322 	}
20323 }
20324 
20325 /*
20326  * Device AutoOnline Event processing.
20327  * If attached device is to be onlined, an attempt is made to online this
20328  * device, but only if there is no lingering (old) target node present.
20329  * If the device cannot be onlined, the event flag is left intact,
20330  * so that event daemon may re-run this function later.
20331  *
20332  * This function cannot be called in interrupt context (it may sleep).
20333  *
20334  * NOTE: Processes cport events only, not port multiplier ports.
20335  */
20336 static void
20337 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20338     sata_address_t *saddr)
20339 {
20340 	sata_cport_info_t *cportinfo;
20341 	sata_drive_info_t *sdinfo;
20342 	sata_device_t sata_device;
20343 	dev_info_t *tdip;
20344 
20345 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20346 	    "Processing port %d attached device auto-onlining", saddr->cport);
20347 
20348 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20349 
20350 	/*
20351 	 * Check if device is present and recognized. If not, reset event.
20352 	 */
20353 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20354 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20355 		/* Nothing to online */
20356 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20357 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20358 		    saddr->cport)->cport_mutex);
20359 		return;
20360 	}
20361 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20362 
20363 	/*
20364 	 * Check if there is target node for this device and if it is in the
20365 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20366 	 * the event for later processing.
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_device_autoonline: "
20378 			    "old device target node exists!", NULL);
20379 			/*
20380 			 * Event daemon will retry device onlining later.
20381 			 */
20382 			mutex_enter(&sata_hba_inst->satahba_mutex);
20383 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20384 			mutex_exit(&sata_hba_inst->satahba_mutex);
20385 			mutex_enter(&sata_mutex);
20386 			sata_event_pending |= SATA_EVNT_MAIN;
20387 			mutex_exit(&sata_mutex);
20388 			return;
20389 		}
20390 		/*
20391 		 * If the target node is not in the 'removed" state, assume
20392 		 * that it belongs to this device. There is nothing more to do,
20393 		 * but reset the event.
20394 		 */
20395 	} else {
20396 
20397 		/*
20398 		 * Try to online the device
20399 		 * If there is any reset-related event, remove it. We are
20400 		 * configuring the device and no state restoring is needed.
20401 		 */
20402 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20403 		    saddr->cport)->cport_mutex);
20404 		sata_device.satadev_addr = *saddr;
20405 		if (saddr->qual == SATA_ADDR_CPORT)
20406 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20407 		else
20408 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20409 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20410 		if (sdinfo != NULL) {
20411 			if (sdinfo->satadrv_event_flags &
20412 			    (SATA_EVNT_DEVICE_RESET |
20413 			    SATA_EVNT_INPROC_DEVICE_RESET))
20414 				sdinfo->satadrv_event_flags = 0;
20415 			sdinfo->satadrv_event_flags |=
20416 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20417 
20418 			/* Need to create a new target node. */
20419 			cportinfo->cport_tgtnode_clean = B_TRUE;
20420 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20421 			    saddr->cport)->cport_mutex);
20422 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20423 			    sata_hba_inst, &sata_device.satadev_addr);
20424 			if (tdip == NULL) {
20425 				/*
20426 				 * Configure (onlining) failed.
20427 				 * We will NOT retry
20428 				 */
20429 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20430 				    "sata_process_device_autoonline: "
20431 				    "configuring SATA device at port %d failed",
20432 				    saddr->cport));
20433 			}
20434 		} else {
20435 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20436 			    saddr->cport)->cport_mutex);
20437 		}
20438 
20439 	}
20440 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20441 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20442 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20443 	    saddr->cport)->cport_mutex);
20444 }
20445 
20446 
20447 static void
20448 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20449     int hint)
20450 {
20451 	char ap[MAXPATHLEN];
20452 	nvlist_t *ev_attr_list = NULL;
20453 	int err;
20454 
20455 	/* Allocate and build sysevent attribute list */
20456 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20457 	if (err != 0) {
20458 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20459 		    "sata_gen_sysevent: "
20460 		    "cannot allocate memory for sysevent attributes\n"));
20461 		return;
20462 	}
20463 	/* Add hint attribute */
20464 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20465 	if (err != 0) {
20466 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20467 		    "sata_gen_sysevent: "
20468 		    "failed to add DR_HINT attr for sysevent"));
20469 		nvlist_free(ev_attr_list);
20470 		return;
20471 	}
20472 	/*
20473 	 * Add AP attribute.
20474 	 * Get controller pathname and convert it into AP pathname by adding
20475 	 * a target number.
20476 	 */
20477 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20478 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20479 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20480 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20481 
20482 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20483 	if (err != 0) {
20484 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20485 		    "sata_gen_sysevent: "
20486 		    "failed to add DR_AP_ID attr for sysevent"));
20487 		nvlist_free(ev_attr_list);
20488 		return;
20489 	}
20490 
20491 	/* Generate/log sysevent */
20492 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20493 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20494 	if (err != DDI_SUCCESS) {
20495 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20496 		    "sata_gen_sysevent: "
20497 		    "cannot log sysevent, err code %x\n", err));
20498 	}
20499 
20500 	nvlist_free(ev_attr_list);
20501 }
20502 
20503 
20504 
20505 
20506 /*
20507  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20508  */
20509 static void
20510 sata_set_device_removed(dev_info_t *tdip)
20511 {
20512 	int circ;
20513 
20514 	ASSERT(tdip != NULL);
20515 
20516 	ndi_devi_enter(tdip, &circ);
20517 	mutex_enter(&DEVI(tdip)->devi_lock);
20518 	DEVI_SET_DEVICE_REMOVED(tdip);
20519 	mutex_exit(&DEVI(tdip)->devi_lock);
20520 	ndi_devi_exit(tdip, circ);
20521 }
20522 
20523 
20524 /*
20525  * Set internal event instructing event daemon to try
20526  * to perform the target node cleanup.
20527  */
20528 static void
20529 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20530     sata_address_t *saddr)
20531 {
20532 	if (saddr->qual == SATA_ADDR_CPORT ||
20533 	    saddr->qual == SATA_ADDR_DCPORT) {
20534 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20535 		    saddr->cport)->cport_mutex);
20536 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20537 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20538 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20539 		    cport_tgtnode_clean = B_FALSE;
20540 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20541 		    saddr->cport)->cport_mutex);
20542 	} else {
20543 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20544 		    saddr->cport, saddr->pmport)->pmport_mutex);
20545 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20546 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20547 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20548 		    pmport_tgtnode_clean = B_FALSE;
20549 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20550 		    saddr->cport, saddr->pmport)->pmport_mutex);
20551 	}
20552 	mutex_enter(&sata_hba_inst->satahba_mutex);
20553 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20554 	mutex_exit(&sata_hba_inst->satahba_mutex);
20555 	mutex_enter(&sata_mutex);
20556 	sata_event_pending |= SATA_EVNT_MAIN;
20557 	mutex_exit(&sata_mutex);
20558 }
20559 
20560 
20561 /*
20562  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20563  * i.e. check if the target node state indicates that it belongs to a removed
20564  * device.
20565  *
20566  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20567  * B_FALSE otherwise.
20568  */
20569 static boolean_t
20570 sata_check_device_removed(dev_info_t *tdip)
20571 {
20572 	ASSERT(tdip != NULL);
20573 
20574 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20575 		return (B_TRUE);
20576 	else
20577 		return (B_FALSE);
20578 }
20579 
20580 
20581 /*
20582  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20583  */
20584 static boolean_t
20585 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20586 {
20587 	int fm_capability = ddi_fm_capable(dip);
20588 	ddi_fm_error_t de;
20589 
20590 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20591 		if (spx->txlt_buf_dma_handle != NULL) {
20592 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20593 			    DDI_FME_VERSION);
20594 			if (de.fme_status != DDI_SUCCESS)
20595 				return (B_TRUE);
20596 		}
20597 	}
20598 	return (B_FALSE);
20599 }
20600 
20601 
20602 /* ************************ FAULT INJECTTION **************************** */
20603 
20604 #ifdef SATA_INJECT_FAULTS
20605 
20606 static	uint32_t sata_fault_count = 0;
20607 static	uint32_t sata_fault_suspend_count = 0;
20608 
20609 /*
20610  * Inject sata pkt fault
20611  * It modifies returned values of the sata packet.
20612  * It returns immediately if:
20613  * pkt fault injection is not enabled (via sata_inject_fault,
20614  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20615  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20616  * pkt is not directed to specified fault controller/device
20617  * (sata_fault_ctrl_dev and sata_fault_device).
20618  * If fault controller is not specified, fault injection applies to all
20619  * controllers and devices.
20620  *
20621  * First argument is the pointer to the executed sata packet.
20622  * Second argument is a pointer to a value returned by the HBA tran_start
20623  * function.
20624  * Third argument specifies injected error. Injected sata packet faults
20625  * are the satapkt_reason values.
20626  * SATA_PKT_BUSY		-1	Not completed, busy
20627  * SATA_PKT_DEV_ERROR		1	Device reported error
20628  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20629  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20630  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20631  * SATA_PKT_ABORTED		5	Aborted by request
20632  * SATA_PKT_TIMEOUT		6	Operation timeut
20633  * SATA_PKT_RESET		7	Aborted by reset request
20634  *
20635  * Additional global variables affecting the execution:
20636  *
20637  * sata_inject_fault_count variable specifies number of times in row the
20638  * error is injected. Value of -1 specifies permanent fault, ie. every time
20639  * the fault injection point is reached, the fault is injected and a pause
20640  * between fault injection specified by sata_inject_fault_pause_count is
20641  * ignored). Fault injection routine decrements sata_inject_fault_count
20642  * (if greater than zero) until it reaches 0. No fault is injected when
20643  * sata_inject_fault_count is 0 (zero).
20644  *
20645  * sata_inject_fault_pause_count variable specifies number of times a fault
20646  * injection is bypassed (pause between fault injections).
20647  * If set to 0, a fault is injected only a number of times specified by
20648  * sata_inject_fault_count.
20649  *
20650  * The fault counts are static, so for periodic errors they have to be manually
20651  * reset to start repetition sequence from scratch.
20652  * If the original value returned by the HBA tran_start function is not
20653  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20654  * is injected (to avoid masking real problems);
20655  *
20656  * NOTE: In its current incarnation, this function should be invoked only for
20657  * commands executed in SYNCHRONOUS mode.
20658  */
20659 
20660 
20661 static void
20662 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20663 {
20664 
20665 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20666 		return;
20667 
20668 	if (sata_inject_fault_count == 0)
20669 		return;
20670 
20671 	if (fault == 0)
20672 		return;
20673 
20674 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20675 		return;
20676 
20677 	if (sata_fault_ctrl != NULL) {
20678 		sata_pkt_txlate_t *spx =
20679 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20680 
20681 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20682 		    spx->txlt_sata_hba_inst->satahba_dip)
20683 			return;
20684 
20685 		if (sata_fault_device.satadev_addr.cport !=
20686 		    spkt->satapkt_device.satadev_addr.cport ||
20687 		    sata_fault_device.satadev_addr.pmport !=
20688 		    spkt->satapkt_device.satadev_addr.pmport ||
20689 		    sata_fault_device.satadev_addr.qual !=
20690 		    spkt->satapkt_device.satadev_addr.qual)
20691 			return;
20692 	}
20693 
20694 	/* Modify pkt return parameters */
20695 	if (*rval != SATA_TRAN_ACCEPTED ||
20696 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20697 		sata_fault_count = 0;
20698 		sata_fault_suspend_count = 0;
20699 		return;
20700 	}
20701 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20702 		/* Pause in the injection */
20703 		sata_fault_suspend_count -= 1;
20704 		return;
20705 	}
20706 
20707 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20708 		/*
20709 		 * Init inject fault cycle. If fault count is set to -1,
20710 		 * it is a permanent fault.
20711 		 */
20712 		if (sata_inject_fault_count != -1) {
20713 			sata_fault_count = sata_inject_fault_count;
20714 			sata_fault_suspend_count =
20715 			    sata_inject_fault_pause_count;
20716 			if (sata_fault_suspend_count == 0)
20717 				sata_inject_fault_count = 0;
20718 		}
20719 	}
20720 
20721 	if (sata_fault_count != 0)
20722 		sata_fault_count -= 1;
20723 
20724 	switch (fault) {
20725 	case SATA_PKT_BUSY:
20726 		*rval = SATA_TRAN_BUSY;
20727 		spkt->satapkt_reason = SATA_PKT_BUSY;
20728 		break;
20729 
20730 	case SATA_PKT_QUEUE_FULL:
20731 		*rval = SATA_TRAN_QUEUE_FULL;
20732 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20733 		break;
20734 
20735 	case SATA_PKT_CMD_UNSUPPORTED:
20736 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20737 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20738 		break;
20739 
20740 	case SATA_PKT_PORT_ERROR:
20741 		/* This is "rejected" command */
20742 		*rval = SATA_TRAN_PORT_ERROR;
20743 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20744 		/* Additional error setup could be done here - port state */
20745 		break;
20746 
20747 	case SATA_PKT_DEV_ERROR:
20748 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20749 		/*
20750 		 * Additional error setup could be done here
20751 		 */
20752 		break;
20753 
20754 	case SATA_PKT_ABORTED:
20755 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20756 		break;
20757 
20758 	case SATA_PKT_TIMEOUT:
20759 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20760 		/* Additional error setup could be done here */
20761 		break;
20762 
20763 	case SATA_PKT_RESET:
20764 		spkt->satapkt_reason = SATA_PKT_RESET;
20765 		/*
20766 		 * Additional error setup could be done here - device reset
20767 		 */
20768 		break;
20769 
20770 	default:
20771 		break;
20772 	}
20773 }
20774 
20775 #endif
20776 
20777 /*
20778  * SATA Trace Ring Buffer
20779  * ----------------------
20780  *
20781  * Overview
20782  *
20783  * The SATA trace ring buffer is a ring buffer created and managed by
20784  * the SATA framework module that can be used by any module or driver
20785  * within the SATA framework to store debug messages.
20786  *
20787  * Ring Buffer Interfaces:
20788  *
20789  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20790  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20791  *
20792  *	Note that the sata_trace_debug() interface was created to give
20793  *	consumers the flexibilty of sending debug messages to ring buffer
20794  *	as variable arguments.  Consumers can send type va_list debug
20795  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20796  *	and sata_vtrace_debug() relationship is similar to that of
20797  *	cmn_err(9F) and vcmn_err(9F).
20798  *
20799  * Below is a diagram of the SATA trace ring buffer interfaces and
20800  * sample consumers:
20801  *
20802  * +---------------------------------+
20803  * |    o  o  SATA Framework Module  |
20804  * | o  SATA  o     +------------------+      +------------------+
20805  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20806  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20807  * | o        o     +------------------+   |  +------------------+
20808  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20809  * |                        |        |        +------------------+
20810  * |           +------------------+  |
20811  * |           |SATA Debug Message|  |
20812  * |           +------------------+  |
20813  * +---------------------------------+
20814  *
20815  * Supporting Routines:
20816  *
20817  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20818  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20819  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20820  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20821  *
20822  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20823  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20824  * /etc/system to desired size in unit of bytes.
20825  *
20826  * The individual debug message size in the ring buffer is restricted
20827  * to DMSG_BUF_SIZE.
20828  */
20829 void
20830 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20831 {
20832 	sata_trace_dmsg_t *dmsg;
20833 
20834 	if (sata_debug_rbuf == NULL) {
20835 		return;
20836 	}
20837 
20838 	/*
20839 	 * If max size of ring buffer is smaller than size
20840 	 * required for one debug message then just return
20841 	 * since we have no room for the debug message.
20842 	 */
20843 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20844 		return;
20845 	}
20846 
20847 	mutex_enter(&sata_debug_rbuf->lock);
20848 
20849 	/* alloc or reuse on ring buffer */
20850 	dmsg = sata_trace_dmsg_alloc();
20851 
20852 	if (dmsg == NULL) {
20853 		/* resource allocation failed */
20854 		mutex_exit(&sata_debug_rbuf->lock);
20855 		return;
20856 	}
20857 
20858 	dmsg->dip = dip;
20859 	gethrestime(&dmsg->timestamp);
20860 
20861 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20862 
20863 	mutex_exit(&sata_debug_rbuf->lock);
20864 }
20865 
20866 void
20867 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20868 {
20869 	va_list ap;
20870 
20871 	va_start(ap, fmt);
20872 	sata_vtrace_debug(dip, fmt, ap);
20873 	va_end(ap);
20874 }
20875 
20876 /*
20877  * This routine is used to manage debug messages
20878  * on ring buffer.
20879  */
20880 static sata_trace_dmsg_t *
20881 sata_trace_dmsg_alloc(void)
20882 {
20883 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20884 
20885 	if (sata_debug_rbuf->looped == TRUE) {
20886 		sata_debug_rbuf->dmsgp = dmsg->next;
20887 		return (sata_debug_rbuf->dmsgp);
20888 	}
20889 
20890 	/*
20891 	 * If we're looping for the first time,
20892 	 * connect the ring.
20893 	 */
20894 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
20895 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
20896 		dmsg->next = sata_debug_rbuf->dmsgh;
20897 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
20898 		sata_debug_rbuf->looped = TRUE;
20899 		return (sata_debug_rbuf->dmsgp);
20900 	}
20901 
20902 	/* If we've gotten this far then memory allocation is needed */
20903 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
20904 	if (dmsg_alloc == NULL) {
20905 		sata_debug_rbuf->allocfailed++;
20906 		return (dmsg_alloc);
20907 	} else {
20908 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
20909 	}
20910 
20911 	if (sata_debug_rbuf->dmsgp != NULL) {
20912 		dmsg->next = dmsg_alloc;
20913 		sata_debug_rbuf->dmsgp = dmsg->next;
20914 		return (sata_debug_rbuf->dmsgp);
20915 	} else {
20916 		/*
20917 		 * We should only be here if we're initializing
20918 		 * the ring buffer.
20919 		 */
20920 		if (sata_debug_rbuf->dmsgh == NULL) {
20921 			sata_debug_rbuf->dmsgh = dmsg_alloc;
20922 		} else {
20923 			/* Something is wrong */
20924 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
20925 			return (NULL);
20926 		}
20927 
20928 		sata_debug_rbuf->dmsgp = dmsg_alloc;
20929 		return (sata_debug_rbuf->dmsgp);
20930 	}
20931 }
20932 
20933 
20934 /*
20935  * Free all messages on debug ring buffer.
20936  */
20937 static void
20938 sata_trace_dmsg_free(void)
20939 {
20940 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
20941 
20942 	while (dmsg != NULL) {
20943 		dmsg_next = dmsg->next;
20944 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
20945 
20946 		/*
20947 		 * If we've looped around the ring than we're done.
20948 		 */
20949 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
20950 			break;
20951 		} else {
20952 			dmsg = dmsg_next;
20953 		}
20954 	}
20955 }
20956 
20957 
20958 /*
20959  * This function can block
20960  */
20961 static void
20962 sata_trace_rbuf_alloc(void)
20963 {
20964 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
20965 
20966 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
20967 
20968 	if (dmsg_ring_size > 0) {
20969 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
20970 	}
20971 }
20972 
20973 
20974 static void
20975 sata_trace_rbuf_free(void)
20976 {
20977 	sata_trace_dmsg_free();
20978 	mutex_destroy(&sata_debug_rbuf->lock);
20979 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
20980 }
20981 
20982 /*
20983  * If SATA_DEBUG is not defined then this routine is called instead
20984  * of sata_log() via the SATA_LOG_D macro.
20985  */
20986 static void
20987 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
20988     const char *fmt, ...)
20989 {
20990 #ifndef __lock_lint
20991 	_NOTE(ARGUNUSED(level))
20992 #endif
20993 
20994 	dev_info_t *dip = NULL;
20995 	va_list ap;
20996 
20997 	if (sata_hba_inst != NULL) {
20998 		dip = SATA_DIP(sata_hba_inst);
20999 	}
21000 
21001 	va_start(ap, fmt);
21002 	sata_vtrace_debug(dip, fmt, ap);
21003 	va_end(ap);
21004 }
21005