xref: /titanic_51/usr/src/uts/common/io/sata/impl/sata.c (revision 45e662eb8429b38c18931ebeed30f2e5287ae51b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27  */
28 
29 /*
30  * SATA Framework
31  * Generic SATA Host Adapter Implementation
32  */
33 
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/modctl.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/thread.h>
42 #include <sys/kstat.h>
43 #include <sys/note.h>
44 #include <sys/sysevent.h>
45 #include <sys/sysevent/eventdefs.h>
46 #include <sys/sysevent/dr.h>
47 #include <sys/taskq.h>
48 #include <sys/disp.h>
49 #include <sys/sdt.h>
50 
51 #include <sys/sata/impl/sata.h>
52 #include <sys/sata/sata_hba.h>
53 #include <sys/sata/sata_defs.h>
54 #include <sys/sata/sata_cfgadm.h>
55 #include <sys/sata/sata_blacklist.h>
56 #include <sys/sata/sata_satl.h>
57 
58 #include <sys/scsi/impl/spc3_types.h>
59 
60 /*
61  * FMA header files
62  */
63 #include <sys/ddifm.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/fm/util.h>
66 #include <sys/fm/io/ddi.h>
67 
68 /* Debug flags - defined in sata.h */
69 int	sata_debug_flags = 0;
70 int	sata_msg = 0;
71 
72 /*
73  * Flags enabling selected SATA HBA framework functionality
74  */
75 #define	SATA_ENABLE_QUEUING		1
76 #define	SATA_ENABLE_NCQ			2
77 #define	SATA_ENABLE_PROCESS_EVENTS	4
78 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
79 int sata_func_enable =
80 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
81 
82 /*
83  * Global variable setting default maximum queue depth (NCQ or TCQ)
84  * Note:minimum queue depth is 1
85  */
86 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
87 
88 /*
89  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
90  * initialization, using value from sata_max_queue_depth
91  * It is adjusted to minimum supported by the controller and by the device,
92  * if queueing is enabled.
93  */
94 static	int sata_current_max_qdepth;
95 
96 /*
97  * Global variable determining the default behavior after device hotpluggin.
98  * If non-zero, the hotplugged device is onlined (if possible) without explicit
99  * IOCTL request (AP_CONFIGURE).
100  * If zero, hotplugged device is identified, but not onlined.
101  * Enabling (AP_CONNECT) device port with an attached device does not result
102  * in device onlining regardless of the flag setting
103  */
104 int sata_auto_online = 0;
105 
106 #ifdef SATA_DEBUG
107 
108 #define	SATA_LOG_D(args)	sata_log args
109 uint64_t mbuf_count = 0;
110 uint64_t mbuffail_count = 0;
111 
112 sata_atapi_cmd_t sata_atapi_trace[64];
113 uint32_t sata_atapi_trace_index = 0;
114 int sata_atapi_trace_save = 1;
115 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
116 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
117     sata_save_atapi_trace(spx, count);
118 
119 #else
120 #define	SATA_LOG_D(args)	sata_trace_log args
121 #define	SATAATAPITRACE(spx, count)
122 #endif
123 
124 #if 0
125 static void
126 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
127 #endif
128 
129 #ifdef SATA_INJECT_FAULTS
130 
131 #define		SATA_INJECT_PKT_FAULT	1
132 uint32_t	sata_inject_fault = 0;
133 
134 uint32_t	sata_inject_fault_count = 0;
135 uint32_t	sata_inject_fault_pause_count = 0;
136 uint32_t	sata_fault_type = 0;
137 uint32_t	sata_fault_cmd = 0;
138 dev_info_t	*sata_fault_ctrl = NULL;
139 sata_device_t	sata_fault_device;
140 
141 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
142 
143 #endif
144 
145 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
146 
147 static char sata_rev_tag[] = {"1.46"};
148 
149 /*
150  * SATA cb_ops functions
151  */
152 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
153 static 	int sata_hba_close(dev_t, int, int, cred_t *);
154 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
155 
156 /*
157  * SCSA required entry points
158  */
159 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
160     scsi_hba_tran_t *, struct scsi_device *);
161 static	int sata_scsi_tgt_probe(struct scsi_device *,
162     int (*callback)(void));
163 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
164     scsi_hba_tran_t *, struct scsi_device *);
165 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
166 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
167 static 	int sata_scsi_reset(struct scsi_address *, int);
168 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
169 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
170 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
171     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
172     caddr_t);
173 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
174 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
175 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
176 
177 /*
178  * SATA HBA interface functions are defined in sata_hba.h header file
179  */
180 
181 /* Event processing functions */
182 static	void sata_event_daemon(void *);
183 static	void sata_event_thread_control(int);
184 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
185 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
186 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
187 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
188 static	void sata_process_port_failed_event(sata_hba_inst_t *,
189     sata_address_t *);
190 static	void sata_process_port_link_events(sata_hba_inst_t *,
191     sata_address_t *);
192 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
193     sata_address_t *);
194 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
195 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
196     sata_address_t *);
197 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
198 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
199     sata_address_t *);
200 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
201 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
202 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
203     sata_address_t *);
204 static	void sata_process_device_autoonline(sata_hba_inst_t *,
205     sata_address_t *saddr);
206 
207 /*
208  * Local translation functions
209  */
210 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
211 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
212 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
213 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
214 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
215 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
216 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
217 static	int sata_txlt_read(sata_pkt_txlate_t *);
218 static	int sata_txlt_write(sata_pkt_txlate_t *);
219 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
220 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
221 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
222 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
223 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
224 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
225 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
226 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
227 
228 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
229 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
230 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
231 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
232 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
233 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
234 static	void sata_txlt_rw_completion(sata_pkt_t *);
235 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
236 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
237 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
238 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
239 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
240 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
241     uint8_t);
242 static	struct scsi_extended_sense *sata_immediate_error_response(
243     sata_pkt_txlate_t *, int);
244 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
245 
246 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
247 static	void sata_txlt_atapi_completion(sata_pkt_t *);
248 
249 /*
250  * Local functions for ioctl
251  */
252 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
253 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
254     devctl_ap_state_t *);
255 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
256 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
257 static	dev_info_t *sata_devt_to_devinfo(dev_t);
258 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
259 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
260 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
267 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
268 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
269     sata_ioctl_data_t *, int mode);
270 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
271     sata_ioctl_data_t *, int mode);
272 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
273     sata_ioctl_data_t *, int mode);
274 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
275     sata_ioctl_data_t *, int mode);
276 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
277     sata_device_t *, sata_ioctl_data_t *, int mode);
278 
279 /*
280  * Local functions
281  */
282 static 	void sata_remove_hba_instance(dev_info_t *);
283 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
284 static 	void sata_probe_ports(sata_hba_inst_t *);
285 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
286 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
287 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
288 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
289 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
290 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
291 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
292 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
293     sata_drive_info_t *);
294 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
295     sata_address_t *);
296 static 	void sata_remove_target_node(sata_hba_inst_t *,
297     sata_address_t *);
298 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
299     struct scsi_address *, sata_device_t *);
300 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
301 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
302 static	void sata_pkt_free(sata_pkt_txlate_t *);
303 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
304     caddr_t, ddi_dma_attr_t *);
305 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
306 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
307 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
308     sata_device_t *);
309 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
310 static	void sata_reidentify_device(sata_pkt_txlate_t *);
311 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
312 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
313 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
314 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
315     ddi_dma_attr_t *);
316 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
317     sata_drive_info_t *);
318 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
319 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
320 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
321 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
322 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
323 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
324 static	int sata_set_drive_features(sata_hba_inst_t *,
325     sata_drive_info_t *, int flag);
326 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
327 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
328 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
329     uint8_t *);
330 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
331     struct scsi_inquiry *);
332 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
333 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
334 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
338     struct mode_cache_scsi3 *, int, int *, int *, int *);
339 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
340     struct mode_info_power_cond *, int, int *, int *, int *);
341 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
342     struct mode_info_excpt_page *, int, int *, int *, int *);
343 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
344     struct mode_acoustic_management *, int, int *, int *, int *);
345 
346 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
347 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
348     sata_hba_inst_t *);
349 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
350     sata_hba_inst_t *);
351 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
352     sata_hba_inst_t *);
353 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
354     sata_pkt_txlate_t *);
355 
356 static	void sata_set_arq_data(sata_pkt_t *);
357 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
358 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
359 static	uint8_t sata_get_standby_timer(uint8_t *timer);
360 
361 static	void sata_save_drive_settings(sata_drive_info_t *);
362 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
363 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
364 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
365 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
366 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
367     sata_drive_info_t *);
368 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
369     struct smart_data *);
370 static	int sata_smart_selftest_log(sata_hba_inst_t *,
371     sata_drive_info_t *,
372     struct smart_selftest_log *);
373 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
374     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
375 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
376     uint8_t *, uint8_t, uint8_t);
377 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
378     struct read_log_ext_directory *);
379 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
380 static	void sata_xlate_errors(sata_pkt_txlate_t *);
381 static	void sata_decode_device_error(sata_pkt_txlate_t *,
382     struct scsi_extended_sense *);
383 static	void sata_set_device_removed(dev_info_t *);
384 static	boolean_t sata_check_device_removed(dev_info_t *);
385 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
386 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
387     sata_drive_info_t *);
388 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
389     sata_drive_info_t *);
390 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
391 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
392 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
393 static  int sata_check_modser(char *, int);
394 
395 /*
396  * FMA
397  */
398 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
399 
400 
401 /*
402  * SATA Framework will ignore SATA HBA driver cb_ops structure and
403  * register following one with SCSA framework.
404  * Open & close are provided, so scsi framework will not use its own
405  */
406 static struct cb_ops sata_cb_ops = {
407 	sata_hba_open,			/* open */
408 	sata_hba_close,			/* close */
409 	nodev,				/* strategy */
410 	nodev,				/* print */
411 	nodev,				/* dump */
412 	nodev,				/* read */
413 	nodev,				/* write */
414 	sata_hba_ioctl,			/* ioctl */
415 	nodev,				/* devmap */
416 	nodev,				/* mmap */
417 	nodev,				/* segmap */
418 	nochpoll,			/* chpoll */
419 	ddi_prop_op,			/* cb_prop_op */
420 	0,				/* streamtab */
421 	D_NEW | D_MP,			/* cb_flag */
422 	CB_REV,				/* rev */
423 	nodev,				/* aread */
424 	nodev				/* awrite */
425 };
426 
427 
428 extern struct mod_ops mod_miscops;
429 extern uchar_t	scsi_cdb_size[];
430 
431 static struct modlmisc modlmisc = {
432 	&mod_miscops,			/* Type of module */
433 	"SATA Module"			/* module name */
434 };
435 
436 
437 static struct modlinkage modlinkage = {
438 	MODREV_1,
439 	(void *)&modlmisc,
440 	NULL
441 };
442 
443 /*
444  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
445  * i.e. when scsi_pkt has not timeout specified.
446  */
447 static int sata_default_pkt_time = 60;	/* 60 seconds */
448 
449 /*
450  * Intermediate buffer device access attributes - they are required,
451  * but not necessarily used.
452  */
453 static ddi_device_acc_attr_t sata_acc_attr = {
454 	DDI_DEVICE_ATTR_V0,
455 	DDI_STRUCTURE_LE_ACC,
456 	DDI_STRICTORDER_ACC
457 };
458 
459 
460 /*
461  * Mutexes protecting structures in multithreaded operations.
462  * Because events are relatively rare, a single global mutex protecting
463  * data structures should be sufficient. To increase performance, add
464  * separate mutex per each sata port and use global mutex only to protect
465  * common data structures.
466  */
467 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
468 static	kmutex_t sata_log_mutex;	/* protects log */
469 
470 static 	char sata_log_buf[256];
471 
472 /*
473  * sata trace debug
474  */
475 static	sata_trace_rbuf_t *sata_debug_rbuf;
476 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
477 static	void sata_trace_dmsg_free(void);
478 static	void sata_trace_rbuf_alloc(void);
479 static	void sata_trace_rbuf_free(void);
480 
481 int	dmsg_ring_size = DMSG_RING_SIZE;
482 
483 /* Default write cache setting for SATA hard disks */
484 int	sata_write_cache = 1;		/* enabled */
485 
486 /* Default write cache setting for SATA ATAPI CD/DVD */
487 int	sata_atapicdvd_write_cache = 1; /* enabled */
488 
489 /* Default write cache setting for SATA ATAPI tape */
490 int	sata_atapitape_write_cache = 1; /* enabled */
491 
492 /* Default write cache setting for SATA ATAPI disk */
493 int	sata_atapidisk_write_cache = 1;	/* enabled */
494 
495 /*
496  * Linked list of HBA instances
497  */
498 static 	sata_hba_inst_t *sata_hba_list = NULL;
499 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
500 /*
501  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
502  * structure and in sata soft state.
503  */
504 
505 /*
506  * Event daemon related variables
507  */
508 static 	kmutex_t sata_event_mutex;
509 static 	kcondvar_t sata_event_cv;
510 static 	kthread_t *sata_event_thread = NULL;
511 static 	int sata_event_thread_terminate = 0;
512 static 	int sata_event_pending = 0;
513 static 	int sata_event_thread_active = 0;
514 extern 	pri_t minclsyspri;
515 
516 /*
517  * NCQ error recovery command
518  */
519 static const sata_cmd_t sata_rle_cmd = {
520 	SATA_CMD_REV,
521 	NULL,
522 	{
523 		SATA_DIR_READ
524 	},
525 	ATA_ADDR_LBA48,
526 	0,
527 	0,
528 	0,
529 	0,
530 	0,
531 	1,
532 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
533 	0,
534 	0,
535 	0,
536 	SATAC_READ_LOG_EXT,
537 	0,
538 	0,
539 	0,
540 };
541 
542 /*
543  * ATAPI error recovery CDB
544  */
545 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
546 	SCMD_REQUEST_SENSE,
547 	0,			/* Only fixed RQ format is supported */
548 	0,
549 	0,
550 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
551 	0
552 };
553 
554 
555 /* Warlock directives */
556 
557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
566 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
567 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
569 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
570 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
571     sata_hba_inst::satahba_scsi_tran))
572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
574 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
575 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
576 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
577     sata_hba_inst::satahba_event_flags))
578 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
579     sata_cport_info::cport_devp))
580 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
581 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
582 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
583     sata_cport_info::cport_dev_type))
584 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
585 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
586     sata_cport_info::cport_state))
587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
588 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
589     sata_pmport_info::pmport_state))
590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
591 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
592     sata_pmport_info::pmport_dev_type))
593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
594 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
595     sata_pmport_info::pmport_sata_drive))
596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597     sata_pmport_info::pmport_tgtnode_clean))
598 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
599     sata_pmport_info::pmport_event_flags))
600 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
601 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
603 #ifdef SATA_DEBUG
604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
608 #endif
609 
610 /* End of warlock directives */
611 
612 /* ************** loadable module configuration functions ************** */
613 
614 int
615 _init()
616 {
617 	int rval;
618 
619 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
620 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
621 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
622 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
623 	sata_trace_rbuf_alloc();
624 	if ((rval = mod_install(&modlinkage)) != 0) {
625 #ifdef SATA_DEBUG
626 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
627 #endif
628 		sata_trace_rbuf_free();
629 		mutex_destroy(&sata_log_mutex);
630 		cv_destroy(&sata_event_cv);
631 		mutex_destroy(&sata_event_mutex);
632 		mutex_destroy(&sata_mutex);
633 	}
634 	return (rval);
635 }
636 
637 int
638 _fini()
639 {
640 	int rval;
641 
642 	if ((rval = mod_remove(&modlinkage)) != 0)
643 		return (rval);
644 
645 	sata_trace_rbuf_free();
646 	mutex_destroy(&sata_log_mutex);
647 	cv_destroy(&sata_event_cv);
648 	mutex_destroy(&sata_event_mutex);
649 	mutex_destroy(&sata_mutex);
650 	return (rval);
651 }
652 
653 int
654 _info(struct modinfo *modinfop)
655 {
656 	return (mod_info(&modlinkage, modinfop));
657 }
658 
659 
660 
661 /* ********************* SATA HBA entry points ********************* */
662 
663 
664 /*
665  * Called by SATA HBA from _init().
666  * Registers HBA driver instance/sata framework pair with scsi framework, by
667  * calling scsi_hba_init().
668  *
669  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
670  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
671  * cb_ops pointer in SATA HBA driver dev_ops structure.
672  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
673  *
674  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
675  * driver.
676  */
677 int
678 sata_hba_init(struct modlinkage *modlp)
679 {
680 	int rval;
681 	struct dev_ops *hba_ops;
682 
683 	SATADBG1(SATA_DBG_HBA_IF, NULL,
684 	    "sata_hba_init: name %s \n",
685 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
686 	/*
687 	 * Fill-up cb_ops and dev_ops when necessary
688 	 */
689 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
690 	/*
691 	 * Provide pointer to SATA dev_ops
692 	 */
693 	hba_ops->devo_cb_ops = &sata_cb_ops;
694 
695 	/*
696 	 * Register SATA HBA with SCSI framework
697 	 */
698 	if ((rval = scsi_hba_init(modlp)) != 0) {
699 		SATADBG1(SATA_DBG_HBA_IF, NULL,
700 		    "sata_hba_init: scsi hba init failed\n", NULL);
701 		return (rval);
702 	}
703 
704 	return (0);
705 }
706 
707 
708 /* HBA attach stages */
709 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
710 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
711 #define	HBA_ATTACH_STAGE_SETUP		4
712 #define	HBA_ATTACH_STAGE_LINKED		8
713 
714 
715 /*
716  *
717  * Called from SATA HBA driver's attach routine to attach an instance of
718  * the HBA.
719  *
720  * For DDI_ATTACH command:
721  * sata_hba_inst structure is allocated here and initialized with pointers to
722  * SATA framework implementation of required scsi tran functions.
723  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
724  * to the soft structure (sata_hba_inst) allocated by SATA framework for
725  * SATA HBA instance related data.
726  * The scsi_tran's tran_hba_private field is used by SATA framework to
727  * store a pointer to per-HBA-instance of sata_hba_inst structure.
728  * The sata_hba_inst structure is cross-linked to scsi tran structure.
729  * Among other info, a pointer to sata_hba_tran structure is stored in
730  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
731  * linked together into the list, pointed to by sata_hba_list.
732  * On the first HBA instance attach the sata event thread is initialized.
733  * Attachment points are created for all SATA ports of the HBA being attached.
734  * All HBA instance's SATA ports are probed and type of plugged devices is
735  * determined. For each device of a supported type, a target node is created.
736  *
737  * DDI_SUCCESS is returned when attachment process is successful,
738  * DDI_FAILURE is returned otherwise.
739  *
740  * For DDI_RESUME command:
741  * Not implemented at this time (postponed until phase 2 of the development).
742  */
743 int
744 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
745     ddi_attach_cmd_t cmd)
746 {
747 	sata_hba_inst_t	*sata_hba_inst;
748 	scsi_hba_tran_t *scsi_tran = NULL;
749 	int hba_attach_state = 0;
750 	char taskq_name[MAXPATHLEN];
751 
752 	SATADBG3(SATA_DBG_HBA_IF, NULL,
753 	    "sata_hba_attach: node %s (%s%d)\n",
754 	    ddi_node_name(dip), ddi_driver_name(dip),
755 	    ddi_get_instance(dip));
756 
757 	if (cmd == DDI_RESUME) {
758 		/*
759 		 * Postponed until phase 2 of the development
760 		 */
761 		return (DDI_FAILURE);
762 	}
763 
764 	if (cmd != DDI_ATTACH) {
765 		return (DDI_FAILURE);
766 	}
767 
768 	/* cmd == DDI_ATTACH */
769 
770 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
771 		SATA_LOG_D((NULL, CE_WARN,
772 		    "sata_hba_attach: invalid sata_hba_tran"));
773 		return (DDI_FAILURE);
774 	}
775 	/*
776 	 * Allocate and initialize SCSI tran structure.
777 	 * SATA copy of tran_bus_config is provided to create port nodes.
778 	 */
779 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
780 	if (scsi_tran == NULL)
781 		return (DDI_FAILURE);
782 	/*
783 	 * Allocate soft structure for SATA HBA instance.
784 	 * There is a separate softstate for each HBA instance.
785 	 */
786 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
787 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
788 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
789 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
790 
791 	/*
792 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
793 	 * soft structure allocated by SATA framework for
794 	 * SATA HBA instance related data.
795 	 */
796 	scsi_tran->tran_hba_private	= sata_hba_inst;
797 	scsi_tran->tran_tgt_private	= NULL;
798 
799 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
800 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
801 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
802 
803 	scsi_tran->tran_start		= sata_scsi_start;
804 	scsi_tran->tran_reset		= sata_scsi_reset;
805 	scsi_tran->tran_abort		= sata_scsi_abort;
806 	scsi_tran->tran_getcap		= sata_scsi_getcap;
807 	scsi_tran->tran_setcap		= sata_scsi_setcap;
808 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
809 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
810 
811 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
812 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
813 
814 	scsi_tran->tran_reset_notify	= NULL;
815 	scsi_tran->tran_get_bus_addr	= NULL;
816 	scsi_tran->tran_quiesce		= NULL;
817 	scsi_tran->tran_unquiesce	= NULL;
818 	scsi_tran->tran_bus_reset	= NULL;
819 
820 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
821 	    scsi_tran, 0) != DDI_SUCCESS) {
822 #ifdef SATA_DEBUG
823 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
824 		    ddi_driver_name(dip), ddi_get_instance(dip));
825 #endif
826 		goto fail;
827 	}
828 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
829 
830 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
831 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
832 		    "sata", 1) != DDI_PROP_SUCCESS) {
833 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
834 			    "failed to create hba sata prop"));
835 			goto fail;
836 		}
837 	}
838 
839 	/*
840 	 * Save pointers in hba instance soft state.
841 	 */
842 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
843 	sata_hba_inst->satahba_tran = sata_tran;
844 	sata_hba_inst->satahba_dip = dip;
845 
846 	/*
847 	 * Create a task queue to handle emulated commands completion
848 	 * Use node name, dash, instance number as the queue name.
849 	 */
850 	taskq_name[0] = '\0';
851 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
852 	    sizeof (taskq_name));
853 	(void) snprintf(taskq_name + strlen(taskq_name),
854 	    sizeof (taskq_name) - strlen(taskq_name),
855 	    "-%d", DEVI(dip)->devi_instance);
856 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
857 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
858 	    TASKQ_DYNAMIC);
859 
860 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
861 
862 	/*
863 	 * Create events thread if not created yet.
864 	 */
865 	sata_event_thread_control(1);
866 
867 	/*
868 	 * Link this hba instance into the list.
869 	 */
870 	mutex_enter(&sata_mutex);
871 
872 	if (sata_hba_list == NULL) {
873 		/*
874 		 * The first instance of HBA is attached.
875 		 * Set current/active default maximum NCQ/TCQ queue depth for
876 		 * all SATA devices. It is done here and now, to eliminate the
877 		 * possibility of the dynamic, programatic modification of the
878 		 * queue depth via global (and public) sata_max_queue_depth
879 		 * variable (this would require special handling in HBA drivers)
880 		 */
881 		sata_current_max_qdepth = sata_max_queue_depth;
882 		if (sata_current_max_qdepth > 32)
883 			sata_current_max_qdepth = 32;
884 		else if (sata_current_max_qdepth < 1)
885 			sata_current_max_qdepth = 1;
886 	}
887 
888 	sata_hba_inst->satahba_next = NULL;
889 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
890 	if (sata_hba_list == NULL) {
891 		sata_hba_list = sata_hba_inst;
892 	}
893 	if (sata_hba_list_tail != NULL) {
894 		sata_hba_list_tail->satahba_next = sata_hba_inst;
895 	}
896 	sata_hba_list_tail = sata_hba_inst;
897 	mutex_exit(&sata_mutex);
898 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
899 
900 	/*
901 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
902 	 * SATA HBA driver should not use its own open/close entry points.
903 	 *
904 	 * Make sure that instance number doesn't overflow
905 	 * when forming minor numbers.
906 	 */
907 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
908 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
909 	    INST2DEVCTL(ddi_get_instance(dip)),
910 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
911 #ifdef SATA_DEBUG
912 		cmn_err(CE_WARN, "sata_hba_attach: "
913 		    "cannot create devctl minor node");
914 #endif
915 		goto fail;
916 	}
917 
918 
919 	/*
920 	 * Set-up kstats here, if necessary.
921 	 * (postponed until future phase of the development).
922 	 */
923 
924 	/*
925 	 * Indicate that HBA is attached. This will enable events processing
926 	 * for this HBA.
927 	 */
928 	sata_hba_inst->satahba_attached = 1;
929 	/*
930 	 * Probe controller ports. This operation will describe a current
931 	 * controller/port/multipliers/device configuration and will create
932 	 * attachment points.
933 	 * We may end-up with just a controller with no devices attached.
934 	 * For the ports with a supported device attached, device target nodes
935 	 * are created and devices are initialized.
936 	 */
937 	sata_probe_ports(sata_hba_inst);
938 
939 	return (DDI_SUCCESS);
940 
941 fail:
942 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
943 		(void) sata_remove_hba_instance(dip);
944 		if (sata_hba_list == NULL)
945 			sata_event_thread_control(0);
946 	}
947 
948 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
949 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
950 		taskq_destroy(sata_hba_inst->satahba_taskq);
951 	}
952 
953 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
954 		(void) scsi_hba_detach(dip);
955 
956 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
957 		mutex_destroy(&sata_hba_inst->satahba_mutex);
958 		kmem_free((void *)sata_hba_inst,
959 		    sizeof (struct sata_hba_inst));
960 		scsi_hba_tran_free(scsi_tran);
961 	}
962 
963 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
964 	    ddi_driver_name(dip), ddi_get_instance(dip));
965 
966 	return (DDI_FAILURE);
967 }
968 
969 
970 /*
971  * Called by SATA HBA from to detach an instance of the driver.
972  *
973  * For DDI_DETACH command:
974  * Free local structures allocated for SATA HBA instance during
975  * sata_hba_attach processing.
976  *
977  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
978  *
979  * For DDI_SUSPEND command:
980  * Not implemented at this time (postponed until phase 2 of the development)
981  * Returnd DDI_SUCCESS.
982  *
983  * When the last HBA instance is detached, the event daemon is terminated.
984  *
985  * NOTE: Port multiplier is supported.
986  */
987 int
988 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
989 {
990 	dev_info_t	*tdip;
991 	sata_hba_inst_t	*sata_hba_inst;
992 	scsi_hba_tran_t *scsi_hba_tran;
993 	sata_cport_info_t *cportinfo;
994 	sata_pmult_info_t *pminfo;
995 	sata_drive_info_t *sdinfo;
996 	sata_device_t	sdevice;
997 	int ncport, npmport;
998 
999 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1000 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1001 
1002 	switch (cmd) {
1003 	case DDI_DETACH:
1004 
1005 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1006 			return (DDI_FAILURE);
1007 
1008 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1009 		if (sata_hba_inst == NULL)
1010 			return (DDI_FAILURE);
1011 
1012 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1013 			sata_hba_inst->satahba_attached = 1;
1014 			return (DDI_FAILURE);
1015 		}
1016 
1017 		/*
1018 		 * Free all target nodes - at this point
1019 		 * devices should be at least offlined
1020 		 * otherwise scsi_hba_detach() should not be called.
1021 		 */
1022 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1023 		    ncport++) {
1024 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1025 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1026 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1027 				if (sdinfo != NULL) {
1028 					tdip = sata_get_target_dip(dip,
1029 					    ncport, 0);
1030 					if (tdip != NULL) {
1031 						if (ndi_devi_offline(tdip,
1032 						    NDI_DEVI_REMOVE) !=
1033 						    NDI_SUCCESS) {
1034 							SATA_LOG_D((
1035 							    sata_hba_inst,
1036 							    CE_WARN,
1037 							    "sata_hba_detach: "
1038 							    "Target node not "
1039 							    "removed !"));
1040 							return (DDI_FAILURE);
1041 						}
1042 					}
1043 				}
1044 			} else { /* SATA_DTYPE_PMULT */
1045 				mutex_enter(&cportinfo->cport_mutex);
1046 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1047 
1048 				if (pminfo == NULL) {
1049 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1050 					    "sata_hba_detach: Port multiplier "
1051 					    "not ready yet!"));
1052 					mutex_exit(&cportinfo->cport_mutex);
1053 					return (DDI_FAILURE);
1054 				}
1055 
1056 				/*
1057 				 * Detach would fail if removal of any of the
1058 				 * target nodes is failed - albeit in that
1059 				 * case some of them may have been removed.
1060 				 */
1061 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1062 				    sata_hba_inst, ncport); npmport++) {
1063 					tdip = sata_get_target_dip(dip, ncport,
1064 					    npmport);
1065 					if (tdip != NULL) {
1066 						if (ndi_devi_offline(tdip,
1067 						    NDI_DEVI_REMOVE) !=
1068 						    NDI_SUCCESS) {
1069 							SATA_LOG_D((
1070 							    sata_hba_inst,
1071 							    CE_WARN,
1072 							    "sata_hba_detach: "
1073 							    "Target node not "
1074 							    "removed !"));
1075 							mutex_exit(&cportinfo->
1076 							    cport_mutex);
1077 							return (DDI_FAILURE);
1078 						}
1079 					}
1080 				}
1081 				mutex_exit(&cportinfo->cport_mutex);
1082 			}
1083 		}
1084 		/*
1085 		 * Disable sata event daemon processing for this HBA
1086 		 */
1087 		sata_hba_inst->satahba_attached = 0;
1088 
1089 		/*
1090 		 * Remove event daemon thread, if it is last HBA instance.
1091 		 */
1092 
1093 		mutex_enter(&sata_mutex);
1094 		if (sata_hba_list->satahba_next == NULL) {
1095 			mutex_exit(&sata_mutex);
1096 			sata_event_thread_control(0);
1097 			mutex_enter(&sata_mutex);
1098 		}
1099 		mutex_exit(&sata_mutex);
1100 
1101 		/* Remove this HBA instance from the HBA list */
1102 		sata_remove_hba_instance(dip);
1103 
1104 		/*
1105 		 * At this point there should be no target nodes attached.
1106 		 * Detach and destroy device and port info structures.
1107 		 */
1108 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1109 		    ncport++) {
1110 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1111 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1112 				sdinfo =
1113 				    cportinfo->cport_devp.cport_sata_drive;
1114 				if (sdinfo != NULL) {
1115 					/* Release device structure */
1116 					kmem_free(sdinfo,
1117 					    sizeof (sata_drive_info_t));
1118 				}
1119 				/* Release cport info */
1120 				mutex_destroy(&cportinfo->cport_mutex);
1121 				kmem_free(cportinfo,
1122 				    sizeof (sata_cport_info_t));
1123 			} else { /* SATA_DTYPE_PMULT */
1124 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1125 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1126 				sata_free_pmult(sata_hba_inst, &sdevice);
1127 			}
1128 		}
1129 
1130 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1131 
1132 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1133 
1134 		taskq_destroy(sata_hba_inst->satahba_taskq);
1135 
1136 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1137 		kmem_free((void *)sata_hba_inst,
1138 		    sizeof (struct sata_hba_inst));
1139 
1140 		return (DDI_SUCCESS);
1141 
1142 	case DDI_SUSPEND:
1143 		/*
1144 		 * Postponed until phase 2
1145 		 */
1146 		return (DDI_FAILURE);
1147 
1148 	default:
1149 		return (DDI_FAILURE);
1150 	}
1151 }
1152 
1153 
1154 /*
1155  * Called by an HBA drive from _fini() routine.
1156  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1157  */
1158 void
1159 sata_hba_fini(struct modlinkage *modlp)
1160 {
1161 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1162 	    "sata_hba_fini: name %s\n",
1163 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1164 
1165 	scsi_hba_fini(modlp);
1166 }
1167 
1168 
1169 /*
1170  * Default open and close routine for sata_hba framework.
1171  *
1172  */
1173 /*
1174  * Open devctl node.
1175  *
1176  * Returns:
1177  * 0 if node was open successfully, error code otherwise.
1178  *
1179  *
1180  */
1181 
1182 static int
1183 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1184 {
1185 #ifndef __lock_lint
1186 	_NOTE(ARGUNUSED(credp))
1187 #endif
1188 	int rv = 0;
1189 	dev_info_t *dip;
1190 	scsi_hba_tran_t *scsi_hba_tran;
1191 	sata_hba_inst_t	*sata_hba_inst;
1192 
1193 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1194 
1195 	if (otyp != OTYP_CHR)
1196 		return (EINVAL);
1197 
1198 	dip = sata_devt_to_devinfo(*devp);
1199 	if (dip == NULL)
1200 		return (ENXIO);
1201 
1202 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1203 		return (ENXIO);
1204 
1205 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1206 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1207 		return (ENXIO);
1208 
1209 	mutex_enter(&sata_mutex);
1210 	if (flags & FEXCL) {
1211 		if (sata_hba_inst->satahba_open_flag != 0) {
1212 			rv = EBUSY;
1213 		} else {
1214 			sata_hba_inst->satahba_open_flag =
1215 			    SATA_DEVCTL_EXOPENED;
1216 		}
1217 	} else {
1218 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1219 			rv = EBUSY;
1220 		} else {
1221 			sata_hba_inst->satahba_open_flag =
1222 			    SATA_DEVCTL_SOPENED;
1223 		}
1224 	}
1225 	mutex_exit(&sata_mutex);
1226 
1227 	return (rv);
1228 }
1229 
1230 
1231 /*
1232  * Close devctl node.
1233  * Returns:
1234  * 0 if node was closed successfully, error code otherwise.
1235  *
1236  */
1237 
1238 static int
1239 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1240 {
1241 #ifndef __lock_lint
1242 	_NOTE(ARGUNUSED(credp))
1243 	_NOTE(ARGUNUSED(flag))
1244 #endif
1245 	dev_info_t *dip;
1246 	scsi_hba_tran_t *scsi_hba_tran;
1247 	sata_hba_inst_t	*sata_hba_inst;
1248 
1249 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1250 
1251 	if (otyp != OTYP_CHR)
1252 		return (EINVAL);
1253 
1254 	dip = sata_devt_to_devinfo(dev);
1255 	if (dip == NULL)
1256 		return (ENXIO);
1257 
1258 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1259 		return (ENXIO);
1260 
1261 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1262 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1263 		return (ENXIO);
1264 
1265 	mutex_enter(&sata_mutex);
1266 	sata_hba_inst->satahba_open_flag = 0;
1267 	mutex_exit(&sata_mutex);
1268 	return (0);
1269 }
1270 
1271 
1272 
1273 /*
1274  * Standard IOCTL commands for SATA hotplugging.
1275  * Implemented DEVCTL_AP commands:
1276  * DEVCTL_AP_CONNECT
1277  * DEVCTL_AP_DISCONNECT
1278  * DEVCTL_AP_CONFIGURE
1279  * DEVCTL_UNCONFIGURE
1280  * DEVCTL_AP_CONTROL
1281  *
1282  * Commands passed to default ndi ioctl handler:
1283  * DEVCTL_DEVICE_GETSTATE
1284  * DEVCTL_DEVICE_ONLINE
1285  * DEVCTL_DEVICE_OFFLINE
1286  * DEVCTL_DEVICE_REMOVE
1287  * DEVCTL_DEVICE_INSERT
1288  * DEVCTL_BUS_GETSTATE
1289  *
1290  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1291  * if not.
1292  *
1293  * Returns:
1294  * 0 if successful,
1295  * error code if operation failed.
1296  *
1297  * Port Multiplier support is supported now.
1298  *
1299  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1300  */
1301 
1302 static int
1303 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1304     int *rvalp)
1305 {
1306 #ifndef __lock_lint
1307 	_NOTE(ARGUNUSED(credp))
1308 	_NOTE(ARGUNUSED(rvalp))
1309 #endif
1310 	int rv = 0;
1311 	int32_t	comp_port = -1;
1312 	dev_info_t *dip;
1313 	devctl_ap_state_t ap_state;
1314 	struct devctl_iocdata *dcp = NULL;
1315 	scsi_hba_tran_t *scsi_hba_tran;
1316 	sata_hba_inst_t *sata_hba_inst;
1317 	sata_device_t sata_device;
1318 	sata_cport_info_t *cportinfo;
1319 	int cport, pmport, qual;
1320 	int rval = SATA_SUCCESS;
1321 
1322 	dip = sata_devt_to_devinfo(dev);
1323 	if (dip == NULL)
1324 		return (ENXIO);
1325 
1326 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1327 		return (ENXIO);
1328 
1329 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1330 	if (sata_hba_inst == NULL)
1331 		return (ENXIO);
1332 
1333 	if (sata_hba_inst->satahba_tran == NULL)
1334 		return (ENXIO);
1335 
1336 	switch (cmd) {
1337 
1338 	case DEVCTL_DEVICE_GETSTATE:
1339 	case DEVCTL_DEVICE_ONLINE:
1340 	case DEVCTL_DEVICE_OFFLINE:
1341 	case DEVCTL_DEVICE_REMOVE:
1342 	case DEVCTL_BUS_GETSTATE:
1343 		/*
1344 		 * There may be more cases that we want to pass to default
1345 		 * handler rather than fail them.
1346 		 */
1347 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1348 	}
1349 
1350 	/* read devctl ioctl data */
1351 	if (cmd != DEVCTL_AP_CONTROL) {
1352 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1353 			return (EFAULT);
1354 
1355 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1356 		    -1) {
1357 			if (dcp)
1358 				ndi_dc_freehdl(dcp);
1359 			return (EINVAL);
1360 		}
1361 
1362 		/*
1363 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1364 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1365 		 */
1366 		cport = SCSI_TO_SATA_CPORT(comp_port);
1367 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1368 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1369 
1370 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1371 		    qual) != 0) {
1372 			ndi_dc_freehdl(dcp);
1373 			return (EINVAL);
1374 		}
1375 
1376 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1377 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1378 		    cport_mutex);
1379 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1380 			/*
1381 			 * Cannot process ioctl request now. Come back later.
1382 			 */
1383 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1384 			    cport_mutex);
1385 			ndi_dc_freehdl(dcp);
1386 			return (EBUSY);
1387 		}
1388 		/* Block event processing for this port */
1389 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1390 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1391 
1392 		sata_device.satadev_addr.cport = cport;
1393 		sata_device.satadev_addr.pmport = pmport;
1394 		sata_device.satadev_addr.qual = qual;
1395 		sata_device.satadev_rev = SATA_DEVICE_REV;
1396 	}
1397 
1398 	switch (cmd) {
1399 
1400 	case DEVCTL_AP_DISCONNECT:
1401 
1402 		/*
1403 		 * Normally, cfgadm sata plugin will try to offline
1404 		 * (unconfigure) device before this request. Nevertheless,
1405 		 * if a device is still configured, we need to
1406 		 * attempt to offline and unconfigure device first, and we will
1407 		 * deactivate the port regardless of the unconfigure
1408 		 * operation results.
1409 		 *
1410 		 */
1411 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1412 
1413 		break;
1414 
1415 	case DEVCTL_AP_UNCONFIGURE:
1416 
1417 		/*
1418 		 * The unconfigure operation uses generic nexus operation to
1419 		 * offline a device. It leaves a target device node attached.
1420 		 * and obviously sata_drive_info attached as well, because
1421 		 * from the hardware point of view nothing has changed.
1422 		 */
1423 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1424 		break;
1425 
1426 	case DEVCTL_AP_CONNECT:
1427 	{
1428 		/*
1429 		 * The sata cfgadm pluging will invoke this operation only if
1430 		 * port was found in the disconnect state (failed state
1431 		 * is also treated as the disconnected state).
1432 		 * If port activation is successful and a device is found
1433 		 * attached to the port, the initialization sequence is
1434 		 * executed to probe the port and attach
1435 		 * a device structure to a port structure. The device is not
1436 		 * set in configured state (system-wise) by this operation.
1437 		 */
1438 
1439 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1440 
1441 		break;
1442 	}
1443 
1444 	case DEVCTL_AP_CONFIGURE:
1445 	{
1446 		/*
1447 		 * A port may be in an active or shutdown state.
1448 		 * If port is in a failed state, operation is aborted.
1449 		 * If a port is in a shutdown state, sata_tran_port_activate()
1450 		 * is invoked prior to any other operation.
1451 		 *
1452 		 * Onlining the device involves creating a new target node.
1453 		 * If there is an old target node present (belonging to
1454 		 * previously removed device), the operation is aborted - the
1455 		 * old node has to be released and removed before configure
1456 		 * operation is attempted.
1457 		 */
1458 
1459 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1460 
1461 		break;
1462 	}
1463 
1464 	case DEVCTL_AP_GETSTATE:
1465 
1466 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1467 
1468 		ap_state.ap_last_change = (time_t)-1;
1469 		ap_state.ap_error_code = 0;
1470 		ap_state.ap_in_transition = 0;
1471 
1472 		/* Copy the return AP-state information to the user space */
1473 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1474 			rv = EFAULT;
1475 		}
1476 		break;
1477 
1478 	case DEVCTL_AP_CONTROL:
1479 	{
1480 		/*
1481 		 * Generic devctl for hardware specific functionality
1482 		 */
1483 		sata_ioctl_data_t	ioc;
1484 
1485 		ASSERT(dcp == NULL);
1486 
1487 		/* Copy in user ioctl data first */
1488 #ifdef _MULTI_DATAMODEL
1489 		if (ddi_model_convert_from(mode & FMODELS) ==
1490 		    DDI_MODEL_ILP32) {
1491 
1492 			sata_ioctl_data_32_t	ioc32;
1493 
1494 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1495 			    sizeof (ioc32), mode) != 0) {
1496 				rv = EFAULT;
1497 				break;
1498 			}
1499 			ioc.cmd 	= (uint_t)ioc32.cmd;
1500 			ioc.port	= (uint_t)ioc32.port;
1501 			ioc.get_size	= (uint_t)ioc32.get_size;
1502 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1503 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1504 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1505 		} else
1506 #endif /* _MULTI_DATAMODEL */
1507 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1508 		    mode) != 0) {
1509 			return (EFAULT);
1510 		}
1511 
1512 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1513 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1514 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1515 
1516 		/*
1517 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1518 		 * a 32-bit number.
1519 		 */
1520 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1521 			return (EINVAL);
1522 		}
1523 		/* validate address */
1524 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1525 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1526 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1527 
1528 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1529 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1530 		    cport, pmport, qual);
1531 
1532 		if (sata_validate_sata_address(sata_hba_inst, cport,
1533 		    pmport, qual) != 0)
1534 			return (EINVAL);
1535 
1536 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1537 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1538 		    cport_mutex);
1539 		/* Is the port locked by event processing daemon ? */
1540 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1541 			/*
1542 			 * Cannot process ioctl request now. Come back later
1543 			 */
1544 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1545 			    cport_mutex);
1546 			return (EBUSY);
1547 		}
1548 		/* Block event processing for this port */
1549 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1550 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1551 
1552 
1553 		sata_device.satadev_addr.cport = cport;
1554 		sata_device.satadev_addr.pmport = pmport;
1555 		sata_device.satadev_addr.qual = qual;
1556 		sata_device.satadev_rev = SATA_DEVICE_REV;
1557 
1558 		switch (ioc.cmd) {
1559 
1560 		case SATA_CFGA_RESET_PORT:
1561 			/*
1562 			 * There is no protection for configured device.
1563 			 */
1564 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1565 			break;
1566 
1567 		case SATA_CFGA_RESET_DEVICE:
1568 			/*
1569 			 * There is no protection for configured device.
1570 			 */
1571 			rv = sata_ioctl_reset_device(sata_hba_inst,
1572 			    &sata_device);
1573 			break;
1574 
1575 		case SATA_CFGA_RESET_ALL:
1576 			/*
1577 			 * There is no protection for configured devices.
1578 			 */
1579 			rv = sata_ioctl_reset_all(sata_hba_inst);
1580 			/*
1581 			 * We return here, because common return is for
1582 			 * a single port operation - we have already unlocked
1583 			 * all ports and no dc handle was allocated.
1584 			 */
1585 			return (rv);
1586 
1587 		case SATA_CFGA_PORT_DEACTIVATE:
1588 			/*
1589 			 * Arbitrarily unconfigure attached device, if any.
1590 			 * Even if the unconfigure fails, proceed with the
1591 			 * port deactivation.
1592 			 */
1593 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1594 
1595 			break;
1596 
1597 		case SATA_CFGA_PORT_ACTIVATE:
1598 
1599 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1600 			break;
1601 
1602 		case SATA_CFGA_PORT_SELF_TEST:
1603 
1604 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1605 			    &sata_device);
1606 			break;
1607 
1608 		case SATA_CFGA_GET_DEVICE_PATH:
1609 
1610 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1611 			    &sata_device, &ioc, mode);
1612 			break;
1613 
1614 		case SATA_CFGA_GET_AP_TYPE:
1615 
1616 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1617 			    &sata_device, &ioc, mode);
1618 			break;
1619 
1620 		case SATA_CFGA_GET_MODEL_INFO:
1621 
1622 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1623 			    &sata_device, &ioc, mode);
1624 			break;
1625 
1626 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1627 
1628 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1629 			    &sata_device, &ioc, mode);
1630 			break;
1631 
1632 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1633 
1634 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1635 			    &sata_device, &ioc, mode);
1636 			break;
1637 
1638 		default:
1639 			rv = EINVAL;
1640 			break;
1641 
1642 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1643 
1644 		break;
1645 	}
1646 
1647 	default:
1648 	{
1649 		/*
1650 		 * If we got here, we got an IOCTL that SATA HBA Framework
1651 		 * does not recognize. Pass ioctl to HBA driver, in case
1652 		 * it could process it.
1653 		 */
1654 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1655 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1656 
1657 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1658 		    "IOCTL 0x%2x not supported in SATA framework, "
1659 		    "passthrough to HBA", cmd);
1660 
1661 		if (sata_tran->sata_tran_ioctl == NULL) {
1662 			rv = EINVAL;
1663 			break;
1664 		}
1665 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1666 		if (rval != 0) {
1667 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1668 			    "IOCTL 0x%2x failed in HBA", cmd);
1669 			rv = rval;
1670 		}
1671 		break;
1672 	}
1673 
1674 	} /* End of main IOCTL switch */
1675 
1676 	if (dcp) {
1677 		ndi_dc_freehdl(dcp);
1678 	}
1679 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1680 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1681 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1682 
1683 	return (rv);
1684 }
1685 
1686 
1687 /*
1688  * Create error retrieval sata packet
1689  *
1690  * A sata packet is allocated and set-up to contain specified error retrieval
1691  * command and appropriate dma-able data buffer.
1692  * No association with any scsi packet is made and no callback routine is
1693  * specified.
1694  *
1695  * Returns a pointer to sata packet upon successful packet creation.
1696  * Returns NULL, if packet cannot be created.
1697  */
1698 sata_pkt_t *
1699 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1700     int pkt_type)
1701 {
1702 	sata_hba_inst_t	*sata_hba_inst;
1703 	sata_pkt_txlate_t *spx;
1704 	sata_pkt_t *spkt;
1705 	sata_drive_info_t *sdinfo;
1706 
1707 	mutex_enter(&sata_mutex);
1708 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1709 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1710 		if (SATA_DIP(sata_hba_inst) == dip)
1711 			break;
1712 	}
1713 	mutex_exit(&sata_mutex);
1714 	ASSERT(sata_hba_inst != NULL);
1715 
1716 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1717 	if (sdinfo == NULL) {
1718 		sata_log(sata_hba_inst, CE_WARN,
1719 		    "sata: error recovery request for non-attached device at "
1720 		    "cport %d", sata_device->satadev_addr.cport);
1721 		return (NULL);
1722 	}
1723 
1724 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1725 	spx->txlt_sata_hba_inst = sata_hba_inst;
1726 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1727 	spkt = sata_pkt_alloc(spx, NULL);
1728 	if (spkt == NULL) {
1729 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1730 		return (NULL);
1731 	}
1732 	/* address is needed now */
1733 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1734 
1735 	switch (pkt_type) {
1736 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1737 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1738 			if (sata_check_for_dma_error(dip, spx)) {
1739 				ddi_fm_service_impact(dip,
1740 				    DDI_SERVICE_UNAFFECTED);
1741 				break;
1742 			}
1743 			return (spkt);
1744 		}
1745 		break;
1746 
1747 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1748 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1749 			if (sata_check_for_dma_error(dip, spx)) {
1750 				ddi_fm_service_impact(dip,
1751 				    DDI_SERVICE_UNAFFECTED);
1752 				break;
1753 			}
1754 			return (spkt);
1755 		}
1756 		break;
1757 
1758 	default:
1759 		break;
1760 	}
1761 
1762 	sata_pkt_free(spx);
1763 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1764 	return (NULL);
1765 
1766 }
1767 
1768 
1769 /*
1770  * Free error retrieval sata packet
1771  *
1772  * Free sata packet and any associated resources allocated previously by
1773  * sata_get_error_retrieval_pkt().
1774  *
1775  * Void return.
1776  */
1777 void
1778 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1779 {
1780 	sata_pkt_txlate_t *spx =
1781 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1782 
1783 	ASSERT(sata_pkt != NULL);
1784 
1785 	sata_free_local_buffer(spx);
1786 	sata_pkt_free(spx);
1787 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1788 
1789 }
1790 
1791 /*
1792  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1793  *
1794  * No association with any scsi packet is made and no callback routine is
1795  * specified.
1796  *
1797  * Returns a pointer to sata packet upon successful packet creation.
1798  * Returns NULL, if packet cannot be created.
1799  *
1800  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1801  * only lower 32 bits are available currently.
1802  */
1803 sata_pkt_t *
1804 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1805     uint8_t regn, uint32_t regv, uint32_t type)
1806 {
1807 	sata_hba_inst_t	*sata_hba_inst;
1808 	sata_pkt_txlate_t *spx;
1809 	sata_pkt_t *spkt;
1810 	sata_cmd_t *scmd;
1811 
1812 	/* Only READ/WRITE commands are accepted. */
1813 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1814 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1815 
1816 	mutex_enter(&sata_mutex);
1817 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1818 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1819 		if (SATA_DIP(sata_hba_inst) == dip)
1820 			break;
1821 	}
1822 	mutex_exit(&sata_mutex);
1823 	ASSERT(sata_hba_inst != NULL);
1824 
1825 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1826 	spx->txlt_sata_hba_inst = sata_hba_inst;
1827 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1828 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1829 	if (spkt == NULL) {
1830 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1831 		return (NULL);
1832 	}
1833 
1834 	/*
1835 	 * NOTE: We need to send this command to the port multiplier,
1836 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1837 	 *
1838 	 * sata_device contains the address of actual target device, and the
1839 	 * pmport number in the command comes from the sata_device structure.
1840 	 */
1841 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1842 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1843 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1844 
1845 	/* Fill sata_pkt */
1846 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1847 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1848 	spkt->satapkt_time = 10; /* Timeout 10s */
1849 
1850 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1851 	scmd = &spkt->satapkt_cmd;
1852 	scmd->satacmd_features_reg = regn & 0xff;
1853 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1854 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1855 	scmd->satacmd_addr_type = 0;		/* N/A */
1856 
1857 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1858 
1859 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1860 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1861 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1862 		scmd->satacmd_flags.sata_special_regs = 1;
1863 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1864 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1865 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1866 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1867 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1868 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1869 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1870 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1871 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1872 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1873 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1874 	}
1875 
1876 	return (spkt);
1877 }
1878 
1879 /*
1880  * Free sata packet and any associated resources allocated previously by
1881  * sata_get_rdwr_pmult_pkt().
1882  *
1883  * Void return.
1884  */
1885 void
1886 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1887 {
1888 	sata_pkt_txlate_t *spx =
1889 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1890 
1891 	/* Free allocated resources */
1892 	sata_pkt_free(spx);
1893 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1894 }
1895 
1896 /*
1897  * Register a port multiplier to framework.
1898  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1899  * 2) Search in the blacklist and update the number of the device ports of the
1900  * port multiplier.
1901  *
1902  * Void return.
1903  */
1904 void
1905 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1906 {
1907 	sata_hba_inst_t *sata_hba_inst = NULL;
1908 	sata_pmult_info_t *pmultinfo;
1909 	sata_pmult_bl_t *blp;
1910 	int cport = sd->satadev_addr.cport;
1911 
1912 	mutex_enter(&sata_mutex);
1913 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1914 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1915 		if (SATA_DIP(sata_hba_inst) == dip)
1916 			if (sata_hba_inst->satahba_attached == 1)
1917 				break;
1918 	}
1919 	mutex_exit(&sata_mutex);
1920 	/* HBA not attached? */
1921 	if (sata_hba_inst == NULL)
1922 		return;
1923 
1924 	/* Number of pmports */
1925 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1926 
1927 	/* Check the blacklist */
1928 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1929 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1930 			continue;
1931 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1932 			continue;
1933 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1934 			continue;
1935 
1936 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1937 		sd->satadev_add_info = blp->bl_flags;
1938 		break;
1939 	}
1940 
1941 	/* Register the port multiplier GSCR */
1942 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1943 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1944 	if (pmultinfo != NULL) {
1945 		pmultinfo->pmult_gscr = *sg;
1946 		pmultinfo->pmult_num_dev_ports =
1947 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1948 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1949 		    "Port multiplier registered at port %d", cport);
1950 	}
1951 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1952 }
1953 
1954 /*
1955  * sata_name_child is for composing the name of the node
1956  * the format of the name is "target,0".
1957  */
1958 static int
1959 sata_name_child(dev_info_t *dip, char *name, int namelen)
1960 {
1961 	int target;
1962 
1963 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1964 	    DDI_PROP_DONTPASS, "target", -1);
1965 	if (target == -1)
1966 		return (DDI_FAILURE);
1967 	(void) snprintf(name, namelen, "%x,0", target);
1968 	return (DDI_SUCCESS);
1969 }
1970 
1971 
1972 
1973 /* ****************** SCSA required entry points *********************** */
1974 
1975 /*
1976  * Implementation of scsi tran_tgt_init.
1977  * sata_scsi_tgt_init() initializes scsi_device structure
1978  *
1979  * If successful, DDI_SUCCESS is returned.
1980  * DDI_FAILURE is returned if addressed device does not exist
1981  */
1982 
1983 static int
1984 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1985     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1986 {
1987 #ifndef __lock_lint
1988 	_NOTE(ARGUNUSED(hba_dip))
1989 	_NOTE(ARGUNUSED(tgt_dip))
1990 #endif
1991 	sata_device_t		sata_device;
1992 	sata_drive_info_t	*sdinfo;
1993 	struct sata_id		*sid;
1994 	sata_hba_inst_t		*sata_hba_inst;
1995 	char			model[SATA_ID_MODEL_LEN + 1];
1996 	char			fw[SATA_ID_FW_LEN + 1];
1997 	char			*vid, *pid;
1998 	int			i;
1999 
2000 	/*
2001 	 * Fail tran_tgt_init for .conf stub node
2002 	 */
2003 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2004 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2005 		ddi_set_name_addr(tgt_dip, NULL);
2006 		return (DDI_FAILURE);
2007 	}
2008 
2009 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2010 
2011 	/* Validate scsi device address */
2012 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2013 	    &sata_device) != 0)
2014 		return (DDI_FAILURE);
2015 
2016 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2017 	    sata_device.satadev_addr.cport)));
2018 
2019 	/* sata_device now contains a valid sata address */
2020 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2021 	if (sdinfo == NULL) {
2022 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2023 		    sata_device.satadev_addr.cport)));
2024 		return (DDI_FAILURE);
2025 	}
2026 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2027 	    sata_device.satadev_addr.cport)));
2028 
2029 	/*
2030 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2031 	 * the target disks.
2032 	 *
2033 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2034 	 * if we need to create cmdk-style devid for all the disk devices
2035 	 * attached to this controller. This property may have been set
2036 	 * from HBA driver's .conf file or by the HBA driver in its
2037 	 * attach(9F) function.
2038 	 */
2039 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2040 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2041 	    "use-cmdk-devid-format", 0) == 1)) {
2042 		/* register a legacy devid for this target node */
2043 		sata_target_devid_register(tgt_dip, sdinfo);
2044 	}
2045 
2046 
2047 	/*
2048 	 * 'Identify Device Data' does not always fit in standard SCSI
2049 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2050 	 * of information.
2051 	 */
2052 	sid = &sdinfo->satadrv_id;
2053 #ifdef	_LITTLE_ENDIAN
2054 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2055 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2056 #else	/* _LITTLE_ENDIAN */
2057 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2058 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2059 #endif	/* _LITTLE_ENDIAN */
2060 	model[SATA_ID_MODEL_LEN] = 0;
2061 	fw[SATA_ID_FW_LEN] = 0;
2062 
2063 	/* split model into into vid/pid */
2064 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2065 		if ((*pid == ' ') || (*pid == '\t'))
2066 			break;
2067 	if (i < SATA_ID_MODEL_LEN) {
2068 		vid = model;
2069 		*pid++ = 0;		/* terminate vid, establish pid */
2070 	} else {
2071 		vid = NULL;		/* vid will stay "ATA     " */
2072 		pid = model;		/* model is all pid */
2073 	}
2074 
2075 	if (vid)
2076 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2077 		    vid, strlen(vid));
2078 	if (pid)
2079 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2080 		    pid, strlen(pid));
2081 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2082 	    fw, strlen(fw));
2083 
2084 	return (DDI_SUCCESS);
2085 }
2086 
2087 /*
2088  * Implementation of scsi tran_tgt_probe.
2089  * Probe target, by calling default scsi routine scsi_hba_probe()
2090  */
2091 static int
2092 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2093 {
2094 	sata_hba_inst_t *sata_hba_inst =
2095 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2096 	int rval;
2097 	uint32_t pm_cap;
2098 
2099 	rval = scsi_hba_probe(sd, callback);
2100 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2101 	    SATA_CAP_LOG_SENSE;
2102 
2103 	if (rval == SCSIPROBE_EXISTS) {
2104 		/*
2105 		 * Set property "pm-capable" on the target device node, so that
2106 		 * the target driver will not try to fetch scsi cycle counters
2107 		 * before enabling device power-management.
2108 		 */
2109 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2110 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2111 			sata_log(sata_hba_inst, CE_WARN,
2112 			    "SATA device at port %d: "
2113 			    "will not be power-managed ",
2114 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2115 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2116 			    "failure updating pm-capable property"));
2117 		}
2118 	}
2119 	return (rval);
2120 }
2121 
2122 /*
2123  * Implementation of scsi tran_tgt_free.
2124  * Release all resources allocated for scsi_device
2125  */
2126 static void
2127 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2128     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2129 {
2130 #ifndef __lock_lint
2131 	_NOTE(ARGUNUSED(hba_dip))
2132 #endif
2133 	sata_device_t		sata_device;
2134 	sata_drive_info_t	*sdinfo;
2135 	sata_hba_inst_t		*sata_hba_inst;
2136 	ddi_devid_t		devid;
2137 
2138 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2139 
2140 	/* Validate scsi device address */
2141 	/*
2142 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2143 	 * was a device at this address, so even if the sata framework internal
2144 	 * resources were alredy released because a device was detached,
2145 	 * this function should be executed as long as its actions do
2146 	 * not require the internal sata view of a device and the address
2147 	 * refers to a valid sata address.
2148 	 * Validating the address here means that we do not trust SCSA...
2149 	 */
2150 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2151 	    &sata_device) == -1)
2152 		return;
2153 
2154 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2155 	    sata_device.satadev_addr.cport)));
2156 
2157 	/* sata_device now should contain a valid sata address */
2158 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2159 	if (sdinfo == NULL) {
2160 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2161 		    sata_device.satadev_addr.cport)));
2162 		return;
2163 	}
2164 	/*
2165 	 * We did not allocate any resources in sata_scsi_tgt_init()
2166 	 * other than few properties.
2167 	 * Free them.
2168 	 */
2169 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2170 	    sata_device.satadev_addr.cport)));
2171 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2172 
2173 	/*
2174 	 * If devid was previously created but not freed up from
2175 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2176 	 */
2177 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2178 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2179 	    "use-cmdk-devid-format", 0) == 1) &&
2180 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2181 		ddi_devid_unregister(tgt_dip);
2182 		ddi_devid_free(devid);
2183 	}
2184 }
2185 
2186 /*
2187  * Implementation of scsi tran_init_pkt
2188  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2189  *
2190  * It seems that we should always allocate pkt, even if the address is
2191  * for non-existing device - just use some default for dma_attr.
2192  * The reason is that there is no way to communicate this to a caller here.
2193  * Subsequent call to sata_scsi_start may fail appropriately.
2194  * Simply returning NULL does not seem to discourage a target driver...
2195  *
2196  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2197  */
2198 static struct scsi_pkt *
2199 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2200     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2201     int (*callback)(caddr_t), caddr_t arg)
2202 {
2203 	sata_hba_inst_t *sata_hba_inst =
2204 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2205 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2206 	sata_device_t sata_device;
2207 	sata_drive_info_t *sdinfo;
2208 	sata_pkt_txlate_t *spx;
2209 	ddi_dma_attr_t cur_dma_attr;
2210 	int rval;
2211 	boolean_t new_pkt = B_TRUE;
2212 
2213 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2214 
2215 	/*
2216 	 * We need to translate the address, even if it could be
2217 	 * a bogus one, for a non-existing device
2218 	 */
2219 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2220 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2221 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2222 	sata_device.satadev_rev = SATA_DEVICE_REV;
2223 
2224 	if (pkt == NULL) {
2225 		/*
2226 		 * Have to allocate a brand new scsi packet.
2227 		 * We need to operate with auto request sense enabled.
2228 		 */
2229 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2230 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2231 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2232 
2233 		if (pkt == NULL)
2234 			return (NULL);
2235 
2236 		/* Fill scsi packet structure */
2237 		pkt->pkt_comp		= (void (*)())NULL;
2238 		pkt->pkt_time		= 0;
2239 		pkt->pkt_resid		= 0;
2240 		pkt->pkt_statistics	= 0;
2241 		pkt->pkt_reason		= 0;
2242 
2243 		/*
2244 		 * pkt_hba_private will point to sata pkt txlate structure
2245 		 */
2246 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2247 		bzero(spx, sizeof (sata_pkt_txlate_t));
2248 
2249 		spx->txlt_scsi_pkt = pkt;
2250 		spx->txlt_sata_hba_inst = sata_hba_inst;
2251 
2252 		/* Allocate sata_pkt */
2253 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2254 		if (spx->txlt_sata_pkt == NULL) {
2255 			/* Could not allocate sata pkt */
2256 			scsi_hba_pkt_free(ap, pkt);
2257 			return (NULL);
2258 		}
2259 		/* Set sata address */
2260 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2261 		    sata_device.satadev_addr;
2262 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2263 		    sata_device.satadev_rev;
2264 
2265 		if ((bp == NULL) || (bp->b_bcount == 0))
2266 			return (pkt);
2267 
2268 		spx->txlt_total_residue = bp->b_bcount;
2269 	} else {
2270 		new_pkt = B_FALSE;
2271 		/*
2272 		 * Packet was preallocated/initialized by previous call
2273 		 */
2274 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2275 
2276 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2277 			return (pkt);
2278 		}
2279 
2280 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2281 	}
2282 
2283 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2284 
2285 	/*
2286 	 * We use an adjusted version of the dma_attr, to account
2287 	 * for device addressing limitations.
2288 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2289 	 * happen when a device is not yet configured.
2290 	 */
2291 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2292 	    sata_device.satadev_addr.cport)));
2293 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2294 	    &spx->txlt_sata_pkt->satapkt_device);
2295 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2296 	sata_adjust_dma_attr(sdinfo,
2297 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2298 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2299 	    sata_device.satadev_addr.cport)));
2300 	/*
2301 	 * Allocate necessary DMA resources for the packet's data buffer
2302 	 * NOTE:
2303 	 * In case of read/write commands, DMA resource allocation here is
2304 	 * based on the premise that the transfer length specified in
2305 	 * the read/write scsi cdb will match exactly DMA resources -
2306 	 * returning correct packet residue is crucial.
2307 	 */
2308 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2309 	    &cur_dma_attr)) != DDI_SUCCESS) {
2310 		/*
2311 		 * If a DMA allocation request fails with
2312 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2313 		 * bioerror(9F) with bp and an error code of EFAULT.
2314 		 * If a DMA allocation request fails with
2315 		 * DDI_DMA_TOOBIG, indicate the error by calling
2316 		 * bioerror(9F) with bp and an error code of EINVAL.
2317 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2318 		 * Request may be repeated later - there is no real error.
2319 		 */
2320 		switch (rval) {
2321 		case DDI_DMA_NORESOURCES:
2322 			bioerror(bp, 0);
2323 			break;
2324 		case DDI_DMA_NOMAPPING:
2325 		case DDI_DMA_BADATTR:
2326 			bioerror(bp, EFAULT);
2327 			break;
2328 		case DDI_DMA_TOOBIG:
2329 		default:
2330 			bioerror(bp, EINVAL);
2331 			break;
2332 		}
2333 		goto fail;
2334 	}
2335 
2336 	if (sata_check_for_dma_error(dip, spx)) {
2337 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2338 		bioerror(bp, EFAULT);
2339 		goto fail;
2340 	}
2341 
2342 success:
2343 	/* Set number of bytes that are not yet accounted for */
2344 	pkt->pkt_resid = spx->txlt_total_residue;
2345 	ASSERT(pkt->pkt_resid >= 0);
2346 
2347 	return (pkt);
2348 
2349 fail:
2350 	if (new_pkt == B_TRUE) {
2351 		/*
2352 		 * Since this is a new packet, we can clean-up
2353 		 * everything
2354 		 */
2355 		sata_scsi_destroy_pkt(ap, pkt);
2356 	} else {
2357 		/*
2358 		 * This is a re-used packet. It will be target driver's
2359 		 * responsibility to eventually destroy it (which
2360 		 * will free allocated resources).
2361 		 * Here, we just "complete" the request, leaving
2362 		 * allocated resources intact, so the request may
2363 		 * be retried.
2364 		 */
2365 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2366 		sata_pkt_free(spx);
2367 	}
2368 	return (NULL);
2369 }
2370 
2371 /*
2372  * Implementation of scsi tran_start.
2373  * Translate scsi cmd into sata operation and return status.
2374  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2375  * are supported.
2376  * For SATA hard disks, supported scsi commands:
2377  * SCMD_INQUIRY
2378  * SCMD_TEST_UNIT_READY
2379  * SCMD_START_STOP
2380  * SCMD_READ_CAPACITY
2381  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2382  * SCMD_REQUEST_SENSE
2383  * SCMD_LOG_SENSE_G1
2384  * SCMD_LOG_SELECT_G1
2385  * SCMD_MODE_SENSE	(specific pages)
2386  * SCMD_MODE_SENSE_G1	(specific pages)
2387  * SCMD_MODE_SELECT	(specific pages)
2388  * SCMD_MODE_SELECT_G1	(specific pages)
2389  * SCMD_SYNCHRONIZE_CACHE
2390  * SCMD_SYNCHRONIZE_CACHE_G1
2391  * SCMD_READ
2392  * SCMD_READ_G1
2393  * SCMD_READ_G4
2394  * SCMD_READ_G5
2395  * SCMD_WRITE
2396  * SCMD_WRITE_BUFFER
2397  * SCMD_WRITE_G1
2398  * SCMD_WRITE_G4
2399  * SCMD_WRITE_G5
2400  * SCMD_SEEK		(noop)
2401  * SCMD_SDIAG
2402  *
2403  * All other commands are rejected as unsupported.
2404  *
2405  * Returns:
2406  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2407  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2408  * a callback could be scheduled.
2409  * TRAN_BADPKT if cmd was directed to invalid address.
2410  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2411  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2412  * was removed and there was no callback specified in scsi pkt.
2413  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2414  * framework was busy performing some other operation(s).
2415  *
2416  */
2417 static int
2418 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2419 {
2420 	sata_hba_inst_t *sata_hba_inst =
2421 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2422 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2423 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2424 	sata_drive_info_t *sdinfo;
2425 	struct buf *bp;
2426 	uint8_t cport, pmport;
2427 	boolean_t dev_gone = B_FALSE;
2428 	int rval;
2429 
2430 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2431 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2432 
2433 	ASSERT(spx != NULL &&
2434 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2435 
2436 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2437 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2438 
2439 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2440 
2441 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2442 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2443 		if (sdinfo == NULL ||
2444 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2445 		    cport_tgtnode_clean == B_FALSE ||
2446 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2447 			dev_gone = B_TRUE;
2448 		}
2449 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2450 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2451 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2452 		    cport) == NULL) {
2453 			dev_gone = B_TRUE;
2454 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2455 		    pmport) == NULL) {
2456 			dev_gone = B_TRUE;
2457 		} else {
2458 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2459 			    cport, pmport)));
2460 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2461 			if (sdinfo == NULL ||
2462 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2463 			    pmport_tgtnode_clean == B_FALSE ||
2464 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2465 				dev_gone = B_TRUE;
2466 			}
2467 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2468 			    cport, pmport)));
2469 		}
2470 	}
2471 
2472 	if (dev_gone == B_TRUE) {
2473 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2474 		pkt->pkt_reason = CMD_DEV_GONE;
2475 		/*
2476 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2477 		 * only in callback function (for normal requests) and
2478 		 * in the dump code path.
2479 		 * So, if the callback is available, we need to do
2480 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2481 		 */
2482 		if (pkt->pkt_comp != NULL) {
2483 			/* scsi callback required */
2484 			if (servicing_interrupt()) {
2485 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2486 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2487 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2488 				    NULL) {
2489 					return (TRAN_BUSY);
2490 				}
2491 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2492 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2493 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2494 				/* Scheduling the callback failed */
2495 				return (TRAN_BUSY);
2496 			}
2497 			return (TRAN_ACCEPT);
2498 		}
2499 		/* No callback available */
2500 		return (TRAN_FATAL_ERROR);
2501 	}
2502 
2503 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2504 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2505 		rval = sata_txlt_atapi(spx);
2506 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2507 		    "sata_scsi_start atapi: rval %d\n", rval);
2508 		return (rval);
2509 	}
2510 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2511 
2512 	/*
2513 	 * Checking for power state, if it was on
2514 	 * STOPPED state, then the drive is not capable
2515 	 * of processing media access command.  And
2516 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2517 	 * in the function for different power state.
2518 	 */
2519 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2520 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2521 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2522 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2523 		    SD_SCSI_ASC_LU_NOT_READY));
2524 	}
2525 
2526 	/* ATA Disk commands processing starts here */
2527 
2528 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2529 
2530 	switch (pkt->pkt_cdbp[0]) {
2531 
2532 	case SCMD_INQUIRY:
2533 		/* Mapped to identify device */
2534 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2535 			bp_mapin(bp);
2536 		rval = sata_txlt_inquiry(spx);
2537 		break;
2538 
2539 	case SCMD_TEST_UNIT_READY:
2540 		/*
2541 		 * SAT "SATA to ATA Translation" doc specifies translation
2542 		 * to ATA CHECK POWER MODE.
2543 		 */
2544 		rval = sata_txlt_test_unit_ready(spx);
2545 		break;
2546 
2547 	case SCMD_START_STOP:
2548 		/* Mapping depends on the command */
2549 		rval = sata_txlt_start_stop_unit(spx);
2550 		break;
2551 
2552 	case SCMD_READ_CAPACITY:
2553 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2554 			bp_mapin(bp);
2555 		rval = sata_txlt_read_capacity(spx);
2556 		break;
2557 
2558 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2559 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2560 			bp_mapin(bp);
2561 		rval = sata_txlt_read_capacity16(spx);
2562 		break;
2563 
2564 	case SCMD_REQUEST_SENSE:
2565 		/*
2566 		 * Always No Sense, since we force ARQ
2567 		 */
2568 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2569 			bp_mapin(bp);
2570 		rval = sata_txlt_request_sense(spx);
2571 		break;
2572 
2573 	case SCMD_LOG_SENSE_G1:
2574 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2575 			bp_mapin(bp);
2576 		rval = sata_txlt_log_sense(spx);
2577 		break;
2578 
2579 	case SCMD_LOG_SELECT_G1:
2580 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2581 			bp_mapin(bp);
2582 		rval = sata_txlt_log_select(spx);
2583 		break;
2584 
2585 	case SCMD_MODE_SENSE:
2586 	case SCMD_MODE_SENSE_G1:
2587 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2588 			bp_mapin(bp);
2589 		rval = sata_txlt_mode_sense(spx);
2590 		break;
2591 
2592 
2593 	case SCMD_MODE_SELECT:
2594 	case SCMD_MODE_SELECT_G1:
2595 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2596 			bp_mapin(bp);
2597 		rval = sata_txlt_mode_select(spx);
2598 		break;
2599 
2600 	case SCMD_SYNCHRONIZE_CACHE:
2601 	case SCMD_SYNCHRONIZE_CACHE_G1:
2602 		rval = sata_txlt_synchronize_cache(spx);
2603 		break;
2604 
2605 	case SCMD_READ:
2606 	case SCMD_READ_G1:
2607 	case SCMD_READ_G4:
2608 	case SCMD_READ_G5:
2609 		rval = sata_txlt_read(spx);
2610 		break;
2611 	case SCMD_WRITE_BUFFER:
2612 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2613 			bp_mapin(bp);
2614 		rval = sata_txlt_write_buffer(spx);
2615 		break;
2616 
2617 	case SCMD_WRITE:
2618 	case SCMD_WRITE_G1:
2619 	case SCMD_WRITE_G4:
2620 	case SCMD_WRITE_G5:
2621 		rval = sata_txlt_write(spx);
2622 		break;
2623 
2624 	case SCMD_SEEK:
2625 		rval = sata_txlt_nodata_cmd_immediate(spx);
2626 		break;
2627 
2628 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2629 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2630 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2631 			bp_mapin(bp);
2632 		rval = sata_txlt_ata_pass_thru(spx);
2633 		break;
2634 
2635 		/* Other cases will be filed later */
2636 		/* postponed until phase 2 of the development */
2637 	case SPC3_CMD_UNMAP:
2638 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2639 			bp_mapin(bp);
2640 		rval = sata_txlt_unmap(spx);
2641 		break;
2642 	default:
2643 		rval = sata_txlt_invalid_command(spx);
2644 		break;
2645 	}
2646 
2647 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2648 	    "sata_scsi_start: rval %d\n", rval);
2649 
2650 	return (rval);
2651 }
2652 
2653 /*
2654  * Implementation of scsi tran_abort.
2655  * Abort specific pkt or all packets.
2656  *
2657  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2658  *
2659  * May be called from an interrupt level.
2660  */
2661 static int
2662 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2663 {
2664 	sata_hba_inst_t *sata_hba_inst =
2665 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2666 	sata_device_t	sata_device;
2667 	sata_pkt_t	*sata_pkt;
2668 
2669 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2670 	    "sata_scsi_abort: %s at target: 0x%x\n",
2671 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2672 
2673 	/* Validate address */
2674 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2675 		/* Invalid address */
2676 		return (0);
2677 
2678 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2679 	    sata_device.satadev_addr.cport)));
2680 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2681 		/* invalid address */
2682 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2683 		    sata_device.satadev_addr.cport)));
2684 		return (0);
2685 	}
2686 	if (scsi_pkt == NULL) {
2687 		/*
2688 		 * Abort all packets.
2689 		 * Although we do not have specific packet, we still need
2690 		 * dummy packet structure to pass device address to HBA.
2691 		 * Allocate one, without sleeping. Fail if pkt cannot be
2692 		 * allocated.
2693 		 */
2694 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2695 		if (sata_pkt == NULL) {
2696 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2697 			    sata_device.satadev_addr.cport)));
2698 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2699 			    "could not allocate sata_pkt"));
2700 			return (0);
2701 		}
2702 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2703 		sata_pkt->satapkt_device = sata_device;
2704 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2705 	} else {
2706 		if (scsi_pkt->pkt_ha_private == NULL) {
2707 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2708 			    sata_device.satadev_addr.cport)));
2709 			return (0); /* Bad scsi pkt */
2710 		}
2711 		/* extract pointer to sata pkt */
2712 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2713 		    txlt_sata_pkt;
2714 	}
2715 
2716 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2717 	    sata_device.satadev_addr.cport)));
2718 	/* Send abort request to HBA */
2719 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2720 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2721 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2722 	    SATA_SUCCESS) {
2723 		if (scsi_pkt == NULL)
2724 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2725 		/* Success */
2726 		return (1);
2727 	}
2728 	/* Else, something did not go right */
2729 	if (scsi_pkt == NULL)
2730 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2731 	/* Failure */
2732 	return (0);
2733 }
2734 
2735 
2736 /*
2737  * Implementation of scsi tran_reset.
2738  * RESET_ALL request is translated into port reset.
2739  * RESET_TARGET requests is translated into a device reset,
2740  * RESET_LUN request is accepted only for LUN 0 and translated into
2741  * device reset.
2742  * The target reset should cause all HBA active and queued packets to
2743  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2744  * the return. HBA should report reset event for the device.
2745  *
2746  * Returns 1 upon success, 0 upon failure.
2747  */
2748 static int
2749 sata_scsi_reset(struct scsi_address *ap, int level)
2750 {
2751 	sata_hba_inst_t	*sata_hba_inst =
2752 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2753 	sata_device_t	sata_device;
2754 	int		val;
2755 
2756 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2757 	    "sata_scsi_reset: level %d target: 0x%x\n",
2758 	    level, ap->a_target);
2759 
2760 	/* Validate address */
2761 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2762 	if (val == -1)
2763 		/* Invalid address */
2764 		return (0);
2765 
2766 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2767 	    sata_device.satadev_addr.cport)));
2768 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2769 		/* invalid address */
2770 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2771 		    sata_device.satadev_addr.cport)));
2772 		return (0);
2773 	}
2774 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2775 	    sata_device.satadev_addr.cport)));
2776 	if (level == RESET_ALL) {
2777 		/* port reset */
2778 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2779 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2780 		else
2781 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2782 
2783 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2784 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2785 			return (1);
2786 		else
2787 			return (0);
2788 
2789 	} else if (val == 0 &&
2790 	    (level == RESET_TARGET || level == RESET_LUN)) {
2791 		/* reset device (device attached) */
2792 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2793 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2794 			return (1);
2795 		else
2796 			return (0);
2797 	}
2798 	return (0);
2799 }
2800 
2801 
2802 /*
2803  * Implementation of scsi tran_getcap (get transport/device capabilities).
2804  * Supported capabilities for SATA hard disks:
2805  * auto-rqsense		(always supported)
2806  * tagged-qing		(supported if HBA supports it)
2807  * untagged-qing	(could be supported if disk supports it, but because
2808  *			 caching behavior allowing untagged queuing actually
2809  *			 results in reduced performance.  sd tries to throttle
2810  *			 back to only 3 outstanding commands, which may
2811  *			 work for real SCSI disks, but with read ahead
2812  *			 caching, having more than 1 outstanding command
2813  *			 results in cache thrashing.)
2814  * sector_size
2815  * dma_max
2816  * interconnect-type	(INTERCONNECT_SATA)
2817  *
2818  * Supported capabilities for ATAPI CD/DVD devices:
2819  * auto-rqsense		(always supported)
2820  * sector_size
2821  * dma_max
2822  * max-cdb-length
2823  * interconnect-type	(INTERCONNECT_SATA)
2824  *
2825  * Supported capabilities for ATAPI TAPE devices:
2826  * auto-rqsense		(always supported)
2827  * dma_max
2828  * max-cdb-length
2829  *
2830  * Supported capabilities for SATA ATAPI hard disks:
2831  * auto-rqsense		(always supported)
2832  * interconnect-type	(INTERCONNECT_SATA)
2833  * max-cdb-length
2834  *
2835  * Request for other capabilities is rejected as unsupported.
2836  *
2837  * Returns supported capability value, or -1 if capability is unsuppported or
2838  * the address is invalid - no device.
2839  */
2840 
2841 static int
2842 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2843 {
2844 
2845 	sata_hba_inst_t 	*sata_hba_inst =
2846 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2847 	sata_device_t		sata_device;
2848 	sata_drive_info_t	*sdinfo;
2849 	ddi_dma_attr_t		adj_dma_attr;
2850 	int 			rval;
2851 
2852 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2853 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2854 	    ap->a_target, cap);
2855 
2856 	/*
2857 	 * We want to process the capabilities on per port granularity.
2858 	 * So, we are specifically restricting ourselves to whom != 0
2859 	 * to exclude the controller wide handling.
2860 	 */
2861 	if (cap == NULL || whom == 0)
2862 		return (-1);
2863 
2864 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2865 		/* Invalid address */
2866 		return (-1);
2867 	}
2868 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2869 	    sata_device.satadev_addr.cport)));
2870 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2871 	    NULL) {
2872 		/* invalid address */
2873 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2874 		    sata_device.satadev_addr.cport)));
2875 		return (-1);
2876 	}
2877 
2878 	switch (scsi_hba_lookup_capstr(cap)) {
2879 	case SCSI_CAP_ARQ:
2880 		rval = 1;		/* ARQ supported, turned on */
2881 		break;
2882 
2883 	case SCSI_CAP_SECTOR_SIZE:
2884 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2885 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2886 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2887 			rval = SATA_ATAPI_SECTOR_SIZE;
2888 		else rval = -1;
2889 		break;
2890 
2891 	/*
2892 	 * untagged queuing cause a performance inversion because of
2893 	 * the way sd operates.  Because of this reason we do not
2894 	 * use it when available.
2895 	 */
2896 	case SCSI_CAP_UNTAGGED_QING:
2897 		if (sdinfo->satadrv_features_enabled &
2898 		    SATA_DEV_F_E_UNTAGGED_QING)
2899 			rval = 1;	/* Untagged queuing available */
2900 		else
2901 			rval = -1;	/* Untagged queuing not available */
2902 		break;
2903 
2904 	case SCSI_CAP_TAGGED_QING:
2905 		if ((sdinfo->satadrv_features_enabled &
2906 		    SATA_DEV_F_E_TAGGED_QING) &&
2907 		    (sdinfo->satadrv_max_queue_depth > 1))
2908 			rval = 1;	/* Tagged queuing available */
2909 		else
2910 			rval = -1;	/* Tagged queuing not available */
2911 		break;
2912 
2913 	case SCSI_CAP_DMA_MAX:
2914 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2915 		    &adj_dma_attr);
2916 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2917 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2918 		break;
2919 
2920 	case SCSI_CAP_INTERCONNECT_TYPE:
2921 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2922 		break;
2923 
2924 	case SCSI_CAP_CDB_LEN:
2925 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2926 			rval = sdinfo->satadrv_atapi_cdb_len;
2927 		else
2928 			rval = -1;
2929 		break;
2930 
2931 	default:
2932 		rval = -1;
2933 		break;
2934 	}
2935 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2936 	    sata_device.satadev_addr.cport)));
2937 	return (rval);
2938 }
2939 
2940 /*
2941  * Implementation of scsi tran_setcap
2942  *
2943  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2944  *
2945  */
2946 static int
2947 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2948 {
2949 	sata_hba_inst_t	*sata_hba_inst =
2950 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2951 	sata_device_t	sata_device;
2952 	sata_drive_info_t	*sdinfo;
2953 	int		rval;
2954 
2955 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2956 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2957 
2958 	/*
2959 	 * We want to process the capabilities on per port granularity.
2960 	 * So, we are specifically restricting ourselves to whom != 0
2961 	 * to exclude the controller wide handling.
2962 	 */
2963 	if (cap == NULL || whom == 0) {
2964 		return (-1);
2965 	}
2966 
2967 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2968 		/* Invalid address */
2969 		return (-1);
2970 	}
2971 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2972 	    sata_device.satadev_addr.cport)));
2973 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2974 	    &sata_device)) == NULL) {
2975 		/* invalid address */
2976 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2977 		    sata_device.satadev_addr.cport)));
2978 		return (-1);
2979 	}
2980 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2981 	    sata_device.satadev_addr.cport)));
2982 
2983 	switch (scsi_hba_lookup_capstr(cap)) {
2984 	case SCSI_CAP_ARQ:
2985 	case SCSI_CAP_SECTOR_SIZE:
2986 	case SCSI_CAP_DMA_MAX:
2987 	case SCSI_CAP_INTERCONNECT_TYPE:
2988 		rval = 0;
2989 		break;
2990 	case SCSI_CAP_UNTAGGED_QING:
2991 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2992 			rval = 1;
2993 			if (value == 1) {
2994 				sdinfo->satadrv_features_enabled |=
2995 				    SATA_DEV_F_E_UNTAGGED_QING;
2996 			} else if (value == 0) {
2997 				sdinfo->satadrv_features_enabled &=
2998 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2999 			} else {
3000 				rval = -1;
3001 			}
3002 		} else {
3003 			rval = 0;
3004 		}
3005 		break;
3006 	case SCSI_CAP_TAGGED_QING:
3007 		/* This can TCQ or NCQ */
3008 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3009 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3010 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3011 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3012 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3013 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3014 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3015 			rval = 1;
3016 			if (value == 1) {
3017 				sdinfo->satadrv_features_enabled |=
3018 				    SATA_DEV_F_E_TAGGED_QING;
3019 			} else if (value == 0) {
3020 				sdinfo->satadrv_features_enabled &=
3021 				    ~SATA_DEV_F_E_TAGGED_QING;
3022 			} else {
3023 				rval = -1;
3024 			}
3025 		} else {
3026 			rval = 0;
3027 		}
3028 		break;
3029 	default:
3030 		rval = -1;
3031 		break;
3032 	}
3033 	return (rval);
3034 }
3035 
3036 /*
3037  * Implementations of scsi tran_destroy_pkt.
3038  * Free resources allocated by sata_scsi_init_pkt()
3039  */
3040 static void
3041 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3042 {
3043 	sata_pkt_txlate_t *spx;
3044 
3045 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3046 
3047 	sata_common_free_dma_rsrcs(spx);
3048 
3049 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3050 	sata_pkt_free(spx);
3051 
3052 	scsi_hba_pkt_free(ap, pkt);
3053 }
3054 
3055 /*
3056  * Implementation of scsi tran_dmafree.
3057  * Free DMA resources allocated by sata_scsi_init_pkt()
3058  */
3059 
3060 static void
3061 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3062 {
3063 #ifndef __lock_lint
3064 	_NOTE(ARGUNUSED(ap))
3065 #endif
3066 	sata_pkt_txlate_t *spx;
3067 
3068 	ASSERT(pkt != NULL);
3069 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3070 
3071 	sata_common_free_dma_rsrcs(spx);
3072 }
3073 
3074 /*
3075  * Implementation of scsi tran_sync_pkt.
3076  *
3077  * The assumption below is that pkt is unique - there is no need to check ap
3078  *
3079  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3080  * into/from the real buffer.
3081  */
3082 static void
3083 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3084 {
3085 #ifndef __lock_lint
3086 	_NOTE(ARGUNUSED(ap))
3087 #endif
3088 	int rval;
3089 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3090 	struct buf *bp;
3091 	int direction;
3092 
3093 	ASSERT(spx != NULL);
3094 	if (spx->txlt_buf_dma_handle != NULL) {
3095 		direction = spx->txlt_sata_pkt->
3096 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3097 		if (spx->txlt_sata_pkt != NULL &&
3098 		    direction != SATA_DIR_NODATA_XFER) {
3099 			if (spx->txlt_tmp_buf != NULL) {
3100 				/* Intermediate DMA buffer used */
3101 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3102 
3103 				if (direction & SATA_DIR_WRITE) {
3104 					bcopy(bp->b_un.b_addr,
3105 					    spx->txlt_tmp_buf, bp->b_bcount);
3106 				}
3107 			}
3108 			/* Sync the buffer for device or for CPU */
3109 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3110 			    (direction & SATA_DIR_WRITE) ?
3111 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3112 			ASSERT(rval == DDI_SUCCESS);
3113 			if (spx->txlt_tmp_buf != NULL &&
3114 			    !(direction & SATA_DIR_WRITE)) {
3115 				/* Intermediate DMA buffer used for read */
3116 				bcopy(spx->txlt_tmp_buf,
3117 				    bp->b_un.b_addr, bp->b_bcount);
3118 			}
3119 
3120 		}
3121 	}
3122 }
3123 
3124 
3125 
3126 /* *******************  SATA - SCSI Translation functions **************** */
3127 /*
3128  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3129  * translation.
3130  */
3131 
3132 /*
3133  * Checks if a device exists and can be access and translates common
3134  * scsi_pkt data to sata_pkt data.
3135  *
3136  * Flag argument indicates that a non-read/write ATA command may be sent
3137  * to HBA in arbitrary SYNC mode to execute this packet.
3138  *
3139  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3140  * sata_pkt was set-up.
3141  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3142  * exist and pkt_comp callback was scheduled.
3143  * Returns other TRAN_XXXXX values when error occured and command should be
3144  * rejected with the returned TRAN_XXXXX value.
3145  *
3146  * This function should be called with port mutex held.
3147  */
3148 static int
3149 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3150 {
3151 	sata_drive_info_t *sdinfo;
3152 	sata_device_t sata_device;
3153 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3154 		SATA_DIR_NODATA_XFER,
3155 		/* all other values to 0/FALSE */
3156 	};
3157 	/*
3158 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3159 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3160 	 * indicates that the scsi packet was not accepted (the reason will not
3161 	 * be checked by the scsi target driver).
3162 	 * To make debugging easier, we set pkt_reason to know value here.
3163 	 * It may be changed later when different completion reason is
3164 	 * determined.
3165 	 */
3166 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3167 	*reason = CMD_TRAN_ERR;
3168 
3169 	/* Validate address */
3170 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3171 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3172 
3173 	case -1:
3174 		/* Invalid address or invalid device type */
3175 		return (TRAN_BADPKT);
3176 	case 2:
3177 		/*
3178 		 * Valid address but device type is unknown - Chack if it is
3179 		 * in the reset state and therefore in an indeterminate state.
3180 		 */
3181 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3182 		    &spx->txlt_sata_pkt->satapkt_device);
3183 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3184 		    (SATA_EVNT_DEVICE_RESET |
3185 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3186 			if (!ddi_in_panic()) {
3187 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3188 				*reason = CMD_INCOMPLETE;
3189 				SATADBG1(SATA_DBG_SCSI_IF,
3190 				    spx->txlt_sata_hba_inst,
3191 				    "sata_scsi_start: rejecting command "
3192 				    "because of device reset state\n", NULL);
3193 				return (TRAN_BUSY);
3194 			}
3195 		}
3196 		/* FALLTHROUGH */
3197 	case 1:
3198 		/* valid address but no valid device - it has disappeared */
3199 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3200 		*reason = CMD_DEV_GONE;
3201 		/*
3202 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3203 		 * only in callback function (for normal requests) and
3204 		 * in the dump code path.
3205 		 * So, if the callback is available, we need to do
3206 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3207 		 */
3208 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3209 			/* scsi callback required */
3210 			if (servicing_interrupt()) {
3211 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3212 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3213 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3214 				    NULL) {
3215 					return (TRAN_BUSY);
3216 				}
3217 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3218 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3219 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3220 				/* Scheduling the callback failed */
3221 				return (TRAN_BUSY);
3222 			}
3223 
3224 			return (TRAN_ACCEPT);
3225 		}
3226 		return (TRAN_FATAL_ERROR);
3227 	default:
3228 		/* all OK; pkt reason will be overwritten later */
3229 		break;
3230 	}
3231 	/*
3232 	 * If pkt is to be executed in polling mode and a command will not be
3233 	 * emulated in SATA module (requires sending a non-read/write ATA
3234 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3235 	 * interrupt context and not in the panic dump, then reject the packet
3236 	 * to avoid a possible interrupt stack overrun or hang caused by
3237 	 * a potentially blocked interrupt.
3238 	 */
3239 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3240 	    servicing_interrupt() && !ddi_in_panic()) {
3241 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3242 		    "sata_scsi_start: rejecting synchronous command because "
3243 		    "of interrupt context\n", NULL);
3244 		return (TRAN_BUSY);
3245 	}
3246 
3247 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3248 	    &spx->txlt_sata_pkt->satapkt_device);
3249 
3250 	/*
3251 	 * If device is in reset condition, reject the packet with
3252 	 * TRAN_BUSY, unless:
3253 	 * 1. system is panicking (dumping)
3254 	 * In such case only one thread is running and there is no way to
3255 	 * process reset.
3256 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3257 	 * Some cfgadm operations involve drive commands, so reset condition
3258 	 * needs to be ignored for IOCTL operations.
3259 	 */
3260 	if ((sdinfo->satadrv_event_flags &
3261 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3262 
3263 		if (!ddi_in_panic() &&
3264 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3265 		    sata_device.satadev_addr.cport) &
3266 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3267 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3268 			*reason = CMD_INCOMPLETE;
3269 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3270 			    "sata_scsi_start: rejecting command because "
3271 			    "of device reset state\n", NULL);
3272 			return (TRAN_BUSY);
3273 		}
3274 	}
3275 
3276 	/*
3277 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3278 	 * sata_scsi_pkt_init() because pkt init had to work also with
3279 	 * non-existing devices.
3280 	 * Now we know that the packet was set-up for a real device, so its
3281 	 * type is known.
3282 	 */
3283 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3284 
3285 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3286 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3287 	    sata_device.satadev_addr.cport)->cport_event_flags &
3288 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3289 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3290 		    sata_ignore_dev_reset = B_TRUE;
3291 	}
3292 	/*
3293 	 * At this point the generic translation routine determined that the
3294 	 * scsi packet should be accepted. Packet completion reason may be
3295 	 * changed later when a different completion reason is determined.
3296 	 */
3297 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3298 	*reason = CMD_CMPLT;
3299 
3300 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3301 		/* Synchronous execution */
3302 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3303 		    SATA_OPMODE_POLLING;
3304 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3305 		    sata_ignore_dev_reset = ddi_in_panic();
3306 	} else {
3307 		/* Asynchronous execution */
3308 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3309 		    SATA_OPMODE_INTERRUPTS;
3310 	}
3311 	/* Convert queuing information */
3312 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3313 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3314 		    B_TRUE;
3315 	else if (spx->txlt_scsi_pkt->pkt_flags &
3316 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3317 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3318 		    B_TRUE;
3319 
3320 	/* Always limit pkt time */
3321 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3322 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3323 	else
3324 		/* Pass on scsi_pkt time */
3325 		spx->txlt_sata_pkt->satapkt_time =
3326 		    spx->txlt_scsi_pkt->pkt_time;
3327 
3328 	return (TRAN_ACCEPT);
3329 }
3330 
3331 
3332 /*
3333  * Translate ATA Identify Device data to SCSI Inquiry data.
3334  * This function may be called only for ATA devices.
3335  * This function should not be called for ATAPI devices - they
3336  * respond directly to SCSI Inquiry command.
3337  *
3338  * SATA Identify Device data has to be valid in sata_drive_info.
3339  * Buffer has to accomodate the inquiry length (36 bytes).
3340  *
3341  * This function should be called with a port mutex held.
3342  */
3343 static	void
3344 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3345     sata_drive_info_t *sdinfo, uint8_t *buf)
3346 {
3347 
3348 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3349 	struct sata_id *sid = &sdinfo->satadrv_id;
3350 
3351 	/* Start with a nice clean slate */
3352 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3353 
3354 	/*
3355 	 * Rely on the dev_type for setting paripheral qualifier.
3356 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3357 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3358 	 * ATAPI Inquiry may provide more data to the target driver.
3359 	 */
3360 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3361 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3362 
3363 	/* CFA type device is not a removable media device */
3364 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3365 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3366 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3367 	inq->inq_iso = 0;	/* ISO version */
3368 	inq->inq_ecma = 0;	/* ECMA version */
3369 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3370 	inq->inq_aenc = 0;	/* Async event notification cap. */
3371 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3372 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3373 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3374 	inq->inq_len = 31;	/* Additional length */
3375 	inq->inq_dualp = 0;	/* dual port device - NO */
3376 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3377 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3378 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3379 				/*
3380 				 * Queuing support - controller has to
3381 				 * support some sort of command queuing.
3382 				 */
3383 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3384 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3385 	else
3386 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3387 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3388 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3389 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3390 
3391 #ifdef	_LITTLE_ENDIAN
3392 	/* Swap text fields to match SCSI format */
3393 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3394 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3395 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3396 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3397 	else
3398 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3399 #else	/* _LITTLE_ENDIAN */
3400 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3401 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3402 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3403 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3404 	else
3405 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3406 #endif	/* _LITTLE_ENDIAN */
3407 }
3408 
3409 
3410 /*
3411  * Scsi response set up for invalid command (command not supported)
3412  *
3413  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3414  */
3415 static int
3416 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3417 {
3418 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3419 	struct scsi_extended_sense *sense;
3420 
3421 	scsipkt->pkt_reason = CMD_CMPLT;
3422 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3423 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3424 
3425 	*scsipkt->pkt_scbp = STATUS_CHECK;
3426 
3427 	sense = sata_arq_sense(spx);
3428 	sense->es_key = KEY_ILLEGAL_REQUEST;
3429 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3430 
3431 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3432 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3433 
3434 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3435 	    scsipkt->pkt_comp != NULL) {
3436 		/* scsi callback required */
3437 		if (servicing_interrupt()) {
3438 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3439 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3440 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3441 				return (TRAN_BUSY);
3442 			}
3443 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3444 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3445 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3446 			/* Scheduling the callback failed */
3447 			return (TRAN_BUSY);
3448 		}
3449 	}
3450 	return (TRAN_ACCEPT);
3451 }
3452 
3453 /*
3454  * Scsi response set up for check condition with special sense key
3455  * and additional sense code.
3456  *
3457  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3458  */
3459 static int
3460 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3461 {
3462 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3463 	int cport = SATA_TXLT_CPORT(spx);
3464 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3465 	struct scsi_extended_sense *sense;
3466 
3467 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3468 	scsipkt->pkt_reason = CMD_CMPLT;
3469 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3470 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3471 
3472 	*scsipkt->pkt_scbp = STATUS_CHECK;
3473 
3474 	sense = sata_arq_sense(spx);
3475 	sense->es_key = key;
3476 	sense->es_add_code = code;
3477 
3478 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3479 
3480 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3481 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3482 
3483 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3484 	    scsipkt->pkt_comp != NULL) {
3485 		/* scsi callback required */
3486 		if (servicing_interrupt()) {
3487 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3488 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3489 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3490 				return (TRAN_BUSY);
3491 			}
3492 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3493 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3494 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3495 			/* Scheduling the callback failed */
3496 			return (TRAN_BUSY);
3497 		}
3498 	}
3499 	return (TRAN_ACCEPT);
3500 }
3501 
3502 /*
3503  * Scsi response setup for
3504  * emulated non-data command that requires no action/return data
3505  *
3506  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3507  */
3508 static	int
3509 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3510 {
3511 	int rval;
3512 	int reason;
3513 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3514 
3515 	mutex_enter(cport_mutex);
3516 
3517 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3518 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3519 		mutex_exit(cport_mutex);
3520 		return (rval);
3521 	}
3522 	mutex_exit(cport_mutex);
3523 
3524 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3525 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3526 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3527 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3528 
3529 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3530 	    "Scsi_pkt completion reason %x\n",
3531 	    spx->txlt_scsi_pkt->pkt_reason);
3532 
3533 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3534 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3535 		/* scsi callback required */
3536 		if (servicing_interrupt()) {
3537 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3538 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3539 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3540 				return (TRAN_BUSY);
3541 			}
3542 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3543 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3544 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3545 			/* Scheduling the callback failed */
3546 			return (TRAN_BUSY);
3547 		}
3548 	}
3549 	return (TRAN_ACCEPT);
3550 }
3551 
3552 
3553 /*
3554  * SATA translate command: Inquiry / Identify Device
3555  * Use cached Identify Device data for now, rather than issuing actual
3556  * Device Identify cmd request. If device is detached and re-attached,
3557  * asynchronous event processing should fetch and refresh Identify Device
3558  * data.
3559  * VPD pages supported now:
3560  * Vital Product Data page
3561  * Unit Serial Number page
3562  * Block Device Characteristics Page
3563  * ATA Information Page
3564  *
3565  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3566  */
3567 
3568 #define	EVPD			1	/* Extended Vital Product Data flag */
3569 #define	CMDDT			2	/* Command Support Data - Obsolete */
3570 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3571 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3572 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3573 					/* Code */
3574 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3575 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3576 
3577 static int
3578 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3579 {
3580 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3581 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3582 	sata_drive_info_t *sdinfo;
3583 	struct scsi_extended_sense *sense;
3584 	int count;
3585 	uint8_t *p;
3586 	int i, j;
3587 	uint8_t page_buf[1024]; /* Max length */
3588 	int rval, reason;
3589 	ushort_t rate;
3590 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3591 
3592 	mutex_enter(cport_mutex);
3593 
3594 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3595 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3596 		mutex_exit(cport_mutex);
3597 		return (rval);
3598 	}
3599 
3600 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3601 	    &spx->txlt_sata_pkt->satapkt_device);
3602 
3603 	ASSERT(sdinfo != NULL);
3604 
3605 	scsipkt->pkt_reason = CMD_CMPLT;
3606 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3607 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3608 
3609 	/* Reject not supported request */
3610 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3611 		*scsipkt->pkt_scbp = STATUS_CHECK;
3612 		sense = sata_arq_sense(spx);
3613 		sense->es_key = KEY_ILLEGAL_REQUEST;
3614 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3615 		goto done;
3616 	}
3617 
3618 	/* Valid Inquiry request */
3619 	*scsipkt->pkt_scbp = STATUS_GOOD;
3620 
3621 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3622 
3623 		/*
3624 		 * Because it is fully emulated command storing data
3625 		 * programatically in the specified buffer, release
3626 		 * preallocated DMA resources before storing data in the buffer,
3627 		 * so no unwanted DMA sync would take place.
3628 		 */
3629 		sata_scsi_dmafree(NULL, scsipkt);
3630 
3631 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3632 			/* Standard Inquiry Data request */
3633 			struct scsi_inquiry inq;
3634 			unsigned int bufsize;
3635 
3636 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3637 			    sdinfo, (uint8_t *)&inq);
3638 			/* Copy no more than requested */
3639 			count = MIN(bp->b_bcount,
3640 			    sizeof (struct scsi_inquiry));
3641 			bufsize = scsipkt->pkt_cdbp[4];
3642 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3643 			count = MIN(count, bufsize);
3644 			bcopy(&inq, bp->b_un.b_addr, count);
3645 
3646 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3647 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3648 			    bufsize - count : 0;
3649 		} else {
3650 			/*
3651 			 * peripheral_qualifier = 0;
3652 			 *
3653 			 * We are dealing only with HD and will be
3654 			 * dealing with CD/DVD devices soon
3655 			 */
3656 			uint8_t peripheral_device_type =
3657 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3658 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3659 
3660 			bzero(page_buf, sizeof (page_buf));
3661 
3662 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3663 			case INQUIRY_SUP_VPD_PAGE:
3664 				/*
3665 				 * Request for supported Vital Product Data
3666 				 * pages.
3667 				 */
3668 				page_buf[0] = peripheral_device_type;
3669 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3670 				page_buf[2] = 0;
3671 				page_buf[3] = 4; /* page length */
3672 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3673 				page_buf[5] = INQUIRY_USN_PAGE;
3674 				page_buf[6] = INQUIRY_BDC_PAGE;
3675 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3676 				/* Copy no more than requested */
3677 				count = MIN(bp->b_bcount, 8);
3678 				bcopy(page_buf, bp->b_un.b_addr, count);
3679 				break;
3680 
3681 			case INQUIRY_USN_PAGE:
3682 				/*
3683 				 * Request for Unit Serial Number page.
3684 				 * Set-up the page.
3685 				 */
3686 				page_buf[0] = peripheral_device_type;
3687 				page_buf[1] = INQUIRY_USN_PAGE;
3688 				page_buf[2] = 0;
3689 				/* remaining page length */
3690 				page_buf[3] = SATA_ID_SERIAL_LEN;
3691 
3692 				/*
3693 				 * Copy serial number from Identify Device data
3694 				 * words into the inquiry page and swap bytes
3695 				 * when necessary.
3696 				 */
3697 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3698 #ifdef	_LITTLE_ENDIAN
3699 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3700 #else
3701 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3702 #endif
3703 				/*
3704 				 * Least significant character of the serial
3705 				 * number shall appear as the last byte,
3706 				 * according to SBC-3 spec.
3707 				 * Count trailing spaces to determine the
3708 				 * necessary shift length.
3709 				 */
3710 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3711 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3712 					if (*(p - j) != '\0' &&
3713 					    *(p - j) != '\040')
3714 						break;
3715 				}
3716 
3717 				/*
3718 				 * Shift SN string right, so that the last
3719 				 * non-blank character would appear in last
3720 				 * byte of SN field in the page.
3721 				 * 'j' is the shift length.
3722 				 */
3723 				for (i = 0;
3724 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3725 				    i++, p--)
3726 					*p = *(p - j);
3727 
3728 				/*
3729 				 * Add leading spaces - same number as the
3730 				 * shift size
3731 				 */
3732 				for (; j > 0; j--)
3733 					page_buf[4 + j - 1] = '\040';
3734 
3735 				count = MIN(bp->b_bcount,
3736 				    SATA_ID_SERIAL_LEN + 4);
3737 				bcopy(page_buf, bp->b_un.b_addr, count);
3738 				break;
3739 
3740 			case INQUIRY_BDC_PAGE:
3741 				/*
3742 				 * Request for Block Device Characteristics
3743 				 * page.  Set-up the page.
3744 				 */
3745 				page_buf[0] = peripheral_device_type;
3746 				page_buf[1] = INQUIRY_BDC_PAGE;
3747 				page_buf[2] = 0;
3748 				/* remaining page length */
3749 				page_buf[3] = SATA_ID_BDC_LEN;
3750 
3751 				rate = sdinfo->satadrv_id.ai_medrotrate;
3752 				page_buf[4] = (rate >> 8) & 0xff;
3753 				page_buf[5] = rate & 0xff;
3754 				page_buf[6] = 0;
3755 				page_buf[7] = sdinfo->satadrv_id.
3756 				    ai_nomformfactor & 0xf;
3757 
3758 				count = MIN(bp->b_bcount,
3759 				    SATA_ID_BDC_LEN + 4);
3760 				bcopy(page_buf, bp->b_un.b_addr, count);
3761 				break;
3762 
3763 			case INQUIRY_ATA_INFO_PAGE:
3764 				/*
3765 				 * Request for ATA Information page.
3766 				 */
3767 				page_buf[0] = peripheral_device_type;
3768 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3769 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3770 				    0xff;
3771 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3772 				/* page_buf[4-7] reserved */
3773 #ifdef  _LITTLE_ENDIAN
3774 				bcopy("ATA     ", &page_buf[8], 8);
3775 				swab(sdinfo->satadrv_id.ai_model,
3776 				    &page_buf[16], 16);
3777 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3778 				    "    ", 4) == 0) {
3779 					swab(sdinfo->satadrv_id.ai_fw,
3780 					    &page_buf[32], 4);
3781 				} else {
3782 					swab(&sdinfo->satadrv_id.ai_fw[4],
3783 					    &page_buf[32], 4);
3784 				}
3785 #else   /* _LITTLE_ENDIAN */
3786 				bcopy("ATA     ", &page_buf[8], 8);
3787 				bcopy(sdinfo->satadrv_id.ai_model,
3788 				    &page_buf[16], 16);
3789 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3790 				    "    ", 4) == 0) {
3791 					bcopy(sdinfo->satadrv_id.ai_fw,
3792 					    &page_buf[32], 4);
3793 				} else {
3794 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3795 					    &page_buf[32], 4);
3796 				}
3797 #endif  /* _LITTLE_ENDIAN */
3798 				/*
3799 				 * page_buf[36-55] which defines the device
3800 				 * signature is not defined at this
3801 				 * time.
3802 				 */
3803 
3804 				/* Set the command code */
3805 				if (sdinfo->satadrv_type ==
3806 				    SATA_DTYPE_ATADISK) {
3807 					page_buf[56] = SATAC_ID_DEVICE;
3808 				} else if (sdinfo->satadrv_type ==
3809 				    SATA_DTYPE_ATAPI) {
3810 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3811 				}
3812 				/*
3813 				 * If the command code, page_buf[56], is not
3814 				 * zero and if one of the identify commands
3815 				 * succeeds, return the identify data.
3816 				 */
3817 				if ((page_buf[56] != 0) &&
3818 				    (sata_fetch_device_identify_data(
3819 				    spx->txlt_sata_hba_inst, sdinfo) ==
3820 				    SATA_SUCCESS)) {
3821 					bcopy(&sdinfo->satadrv_id,
3822 					    &page_buf[60], sizeof (sata_id_t));
3823 				}
3824 
3825 				/* Need to copy out the page_buf to bp */
3826 				count = MIN(bp->b_bcount,
3827 				    SATA_ID_ATA_INFO_LEN + 4);
3828 				bcopy(page_buf, bp->b_un.b_addr, count);
3829 				break;
3830 
3831 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3832 				/*
3833 				 * We may want to implement this page, when
3834 				 * identifiers are common for SATA devices
3835 				 * But not now.
3836 				 */
3837 				/*FALLTHROUGH*/
3838 
3839 			default:
3840 				/* Request for unsupported VPD page */
3841 				*scsipkt->pkt_scbp = STATUS_CHECK;
3842 				sense = sata_arq_sense(spx);
3843 				sense->es_key = KEY_ILLEGAL_REQUEST;
3844 				sense->es_add_code =
3845 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3846 				goto done;
3847 			}
3848 		}
3849 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3850 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3851 		    scsipkt->pkt_cdbp[4] - count : 0;
3852 	}
3853 done:
3854 	mutex_exit(cport_mutex);
3855 
3856 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3857 	    "Scsi_pkt completion reason %x\n",
3858 	    scsipkt->pkt_reason);
3859 
3860 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3861 	    scsipkt->pkt_comp != NULL) {
3862 		/* scsi callback required */
3863 		if (servicing_interrupt()) {
3864 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3865 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3866 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3867 				return (TRAN_BUSY);
3868 			}
3869 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3870 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3871 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3872 			/* Scheduling the callback failed */
3873 			return (TRAN_BUSY);
3874 		}
3875 	}
3876 	return (TRAN_ACCEPT);
3877 }
3878 
3879 /*
3880  * SATA translate command: Request Sense.
3881  *
3882  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3883  * At the moment this is an emulated command (ATA version for SATA hard disks).
3884  * May be translated into Check Power Mode command in the future.
3885  *
3886  * Note: There is a mismatch between already implemented Informational
3887  * Exception Mode Select page 0x1C and this function.
3888  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3889  * NO SENSE and set additional sense code to the exception code - this is not
3890  * implemented here.
3891  */
3892 static int
3893 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3894 {
3895 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3896 	struct scsi_extended_sense sense;
3897 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3898 	sata_drive_info_t *sdinfo;
3899 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3900 	int rval, reason, power_state = 0;
3901 	kmutex_t *cport_mutex;
3902 
3903 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3904 	mutex_enter(cport_mutex);
3905 
3906 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3907 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3908 		mutex_exit(cport_mutex);
3909 		return (rval);
3910 	}
3911 
3912 	scsipkt->pkt_reason = CMD_CMPLT;
3913 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3914 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3915 	*scsipkt->pkt_scbp = STATUS_GOOD;
3916 
3917 	/*
3918 	 * when CONTROL field's NACA bit == 1
3919 	 * return ILLEGAL_REQUEST
3920 	 */
3921 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3922 		mutex_exit(cport_mutex);
3923 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3924 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3925 	}
3926 
3927 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3928 	    &spx->txlt_sata_pkt->satapkt_device);
3929 	ASSERT(sdinfo != NULL);
3930 
3931 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3932 
3933 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3934 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3935 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3936 	if (sata_hba_start(spx, &rval) != 0) {
3937 		mutex_exit(cport_mutex);
3938 		return (rval);
3939 	}
3940 	if (scmd->satacmd_error_reg != 0) {
3941 		mutex_exit(cport_mutex);
3942 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3943 		    SD_SCSI_ASC_NO_ADD_SENSE));
3944 	}
3945 
3946 	switch (scmd->satacmd_sec_count_lsb) {
3947 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3948 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3949 			power_state = SATA_POWER_STOPPED;
3950 		else {
3951 			power_state = SATA_POWER_STANDBY;
3952 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3953 		}
3954 		break;
3955 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3956 		power_state = SATA_POWER_IDLE;
3957 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3958 		break;
3959 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3960 	default:		  /* 0x40, 0x41 active mode */
3961 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3962 			power_state = SATA_POWER_IDLE;
3963 		else {
3964 			power_state = SATA_POWER_ACTIVE;
3965 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3966 		}
3967 		break;
3968 	}
3969 
3970 	mutex_exit(cport_mutex);
3971 
3972 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3973 		/*
3974 		 * Because it is fully emulated command storing data
3975 		 * programatically in the specified buffer, release
3976 		 * preallocated DMA resources before storing data in the buffer,
3977 		 * so no unwanted DMA sync would take place.
3978 		 */
3979 		int count = MIN(bp->b_bcount,
3980 		    sizeof (struct scsi_extended_sense));
3981 		sata_scsi_dmafree(NULL, scsipkt);
3982 		bzero(&sense, sizeof (struct scsi_extended_sense));
3983 		sense.es_valid = 0;	/* Valid LBA */
3984 		sense.es_class = 7;	/* Response code 0x70 - current err */
3985 		sense.es_key = KEY_NO_SENSE;
3986 		sense.es_add_len = 6;	/* Additional length */
3987 		/* Copy no more than requested */
3988 		bcopy(&sense, bp->b_un.b_addr, count);
3989 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3990 		scsipkt->pkt_resid = 0;
3991 		switch (power_state) {
3992 		case SATA_POWER_IDLE:
3993 		case SATA_POWER_STANDBY:
3994 			sense.es_add_code =
3995 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3996 			break;
3997 		case SATA_POWER_STOPPED:
3998 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3999 			break;
4000 		case SATA_POWER_ACTIVE:
4001 		default:
4002 			break;
4003 		}
4004 	}
4005 
4006 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4007 	    "Scsi_pkt completion reason %x\n",
4008 	    scsipkt->pkt_reason);
4009 
4010 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4011 	    scsipkt->pkt_comp != NULL) {
4012 		/* scsi callback required */
4013 		if (servicing_interrupt()) {
4014 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4015 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4016 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4017 				return (TRAN_BUSY);
4018 			}
4019 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4020 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4021 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4022 			/* Scheduling the callback failed */
4023 			return (TRAN_BUSY);
4024 		}
4025 	}
4026 	return (TRAN_ACCEPT);
4027 }
4028 
4029 /*
4030  * SATA translate command: Test Unit Ready
4031  * (ATA version for SATA hard disks).
4032  * It is translated into the Check Power Mode command.
4033  *
4034  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4035  */
4036 static int
4037 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4038 {
4039 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4040 	struct scsi_extended_sense *sense;
4041 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4042 	sata_drive_info_t *sdinfo;
4043 	int power_state;
4044 	int rval, reason;
4045 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4046 
4047 	mutex_enter(cport_mutex);
4048 
4049 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4050 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4051 		mutex_exit(cport_mutex);
4052 		return (rval);
4053 	}
4054 
4055 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4056 	    &spx->txlt_sata_pkt->satapkt_device);
4057 	ASSERT(sdinfo != NULL);
4058 
4059 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4060 
4061 	/* send CHECK POWER MODE command */
4062 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4063 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4064 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4065 	if (sata_hba_start(spx, &rval) != 0) {
4066 		mutex_exit(cport_mutex);
4067 		return (rval);
4068 	}
4069 
4070 	if (scmd->satacmd_error_reg != 0) {
4071 		mutex_exit(cport_mutex);
4072 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4073 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4074 	}
4075 
4076 	power_state = scmd->satacmd_sec_count_lsb;
4077 
4078 	/*
4079 	 * return NOT READY when device in STOPPED mode
4080 	 */
4081 	if (power_state == SATA_PWRMODE_STANDBY &&
4082 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4083 		*scsipkt->pkt_scbp = STATUS_CHECK;
4084 		sense = sata_arq_sense(spx);
4085 		sense->es_key = KEY_NOT_READY;
4086 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4087 	} else {
4088 		/*
4089 		 * For other power mode, return GOOD status
4090 		 */
4091 		*scsipkt->pkt_scbp = STATUS_GOOD;
4092 	}
4093 
4094 	scsipkt->pkt_reason = CMD_CMPLT;
4095 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4096 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4097 
4098 	mutex_exit(cport_mutex);
4099 
4100 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4101 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4102 
4103 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4104 	    scsipkt->pkt_comp != NULL) {
4105 		/* scsi callback required */
4106 		if (servicing_interrupt()) {
4107 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4108 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4109 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4110 				return (TRAN_BUSY);
4111 			}
4112 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4113 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4114 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4115 			/* Scheduling the callback failed */
4116 			return (TRAN_BUSY);
4117 		}
4118 	}
4119 
4120 	return (TRAN_ACCEPT);
4121 }
4122 
4123 /*
4124  * SATA translate command: Start Stop Unit
4125  * Translation depends on a command:
4126  *
4127  * Power condition bits will be supported
4128  * and the power level should be maintained by SATL,
4129  * When SATL received a command, it will check the
4130  * power level firstly, and return the status according
4131  * to SAT2 v2.6 and SAT-2 Standby Modifications
4132  *
4133  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4134  * -----------------------------------------------------------------------
4135  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4136  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4137  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4138  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4139  *
4140  *	Unload Media / NOT SUPPORTED YET
4141  *	Load Media / NOT SUPPROTED YET
4142  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4143  *
4144  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4145  * appropriate values in scsi_pkt fields.
4146  */
4147 static int
4148 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4149 {
4150 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4151 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4152 	int rval, reason;
4153 	sata_drive_info_t *sdinfo;
4154 	sata_id_t *sata_id;
4155 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4156 
4157 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4158 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4159 
4160 	mutex_enter(cport_mutex);
4161 
4162 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4163 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4164 		mutex_exit(cport_mutex);
4165 		return (rval);
4166 	}
4167 
4168 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4169 		/* IMMED bit - not supported */
4170 		mutex_exit(cport_mutex);
4171 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4172 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4173 	}
4174 
4175 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4176 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4177 
4178 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4179 	    &spx->txlt_sata_pkt->satapkt_device);
4180 	ASSERT(sdinfo != NULL);
4181 	sata_id = &sdinfo->satadrv_id;
4182 
4183 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4184 	case 0:
4185 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4186 			/* Load/Unload Media - invalid request */
4187 			goto err_out;
4188 		}
4189 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4190 			/* Start Unit */
4191 			sata_build_read_verify_cmd(scmd, 1, 5);
4192 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4193 			/* Transfer command to HBA */
4194 			if (sata_hba_start(spx, &rval) != 0) {
4195 				/* Pkt not accepted for execution */
4196 				mutex_exit(cport_mutex);
4197 				return (rval);
4198 			}
4199 			if (scmd->satacmd_error_reg != 0) {
4200 				goto err_out;
4201 			}
4202 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4203 		} else {
4204 			/* Stop Unit */
4205 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4206 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4207 			if (sata_hba_start(spx, &rval) != 0) {
4208 				mutex_exit(cport_mutex);
4209 				return (rval);
4210 			} else {
4211 				if (scmd->satacmd_error_reg != 0) {
4212 					goto err_out;
4213 				}
4214 			}
4215 			/* ata standby immediate command */
4216 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4217 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4218 			if (sata_hba_start(spx, &rval) != 0) {
4219 				mutex_exit(cport_mutex);
4220 				return (rval);
4221 			}
4222 			if (scmd->satacmd_error_reg != 0) {
4223 				goto err_out;
4224 			}
4225 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4226 		}
4227 		break;
4228 	case 0x1:
4229 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4230 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4231 		if (sata_hba_start(spx, &rval) != 0) {
4232 			mutex_exit(cport_mutex);
4233 			return (rval);
4234 		}
4235 		if (scmd->satacmd_error_reg != 0) {
4236 			goto err_out;
4237 		}
4238 		sata_build_read_verify_cmd(scmd, 1, 5);
4239 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4240 		/* Transfer command to HBA */
4241 		if (sata_hba_start(spx, &rval) != 0) {
4242 			/* Pkt not accepted for execution */
4243 			mutex_exit(cport_mutex);
4244 			return (rval);
4245 		} else {
4246 			if (scmd->satacmd_error_reg != 0) {
4247 				goto err_out;
4248 			}
4249 		}
4250 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4251 		break;
4252 	case 0x2:
4253 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4254 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4255 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4256 			if (sata_hba_start(spx, &rval) != 0) {
4257 				mutex_exit(cport_mutex);
4258 				return (rval);
4259 			}
4260 			if (scmd->satacmd_error_reg != 0) {
4261 				goto err_out;
4262 			}
4263 		}
4264 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4265 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4266 		if (sata_hba_start(spx, &rval) != 0) {
4267 			mutex_exit(cport_mutex);
4268 			return (rval);
4269 		}
4270 		if (scmd->satacmd_error_reg != 0) {
4271 			goto err_out;
4272 		}
4273 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4274 			/*
4275 			 *  POWER CONDITION MODIFIER bit set
4276 			 *  to 0x1 or larger it will be handled
4277 			 *  on the same way as bit = 0x1
4278 			 */
4279 			if (!(sata_id->ai_cmdset84 &
4280 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4281 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4282 				break;
4283 			}
4284 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4285 			scmd->satacmd_features_reg = 0x44;
4286 			scmd->satacmd_lba_low_lsb = 0x4c;
4287 			scmd->satacmd_lba_mid_lsb = 0x4e;
4288 			scmd->satacmd_lba_high_lsb = 0x55;
4289 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4290 			if (sata_hba_start(spx, &rval) != 0) {
4291 				mutex_exit(cport_mutex);
4292 				return (rval);
4293 			}
4294 			if (scmd->satacmd_error_reg != 0) {
4295 				goto err_out;
4296 			}
4297 		}
4298 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4299 		break;
4300 	case 0x3:
4301 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4302 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4303 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4304 			if (sata_hba_start(spx, &rval) != 0) {
4305 				mutex_exit(cport_mutex);
4306 				return (rval);
4307 			}
4308 			if (scmd->satacmd_error_reg != 0) {
4309 				goto err_out;
4310 			}
4311 		}
4312 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4313 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4314 		if (sata_hba_start(spx, &rval) != 0) {
4315 			mutex_exit(cport_mutex);
4316 			return (rval);
4317 		}
4318 		if (scmd->satacmd_error_reg != 0) {
4319 			goto err_out;
4320 		}
4321 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4322 		break;
4323 	case 0x7:
4324 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4325 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4326 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4327 		if (sata_hba_start(spx, &rval) != 0) {
4328 			mutex_exit(cport_mutex);
4329 			return (rval);
4330 		}
4331 		if (scmd->satacmd_error_reg != 0) {
4332 			goto err_out;
4333 		}
4334 		switch (scmd->satacmd_sec_count_lsb) {
4335 		case SATA_PWRMODE_STANDBY:
4336 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4337 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4338 			    sdinfo->satadrv_standby_timer);
4339 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4340 			if (sata_hba_start(spx, &rval) != 0) {
4341 				mutex_exit(cport_mutex);
4342 				return (rval);
4343 			} else {
4344 				if (scmd->satacmd_error_reg != 0) {
4345 					goto err_out;
4346 				}
4347 			}
4348 			break;
4349 		case SATA_PWRMODE_IDLE:
4350 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4351 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4352 			    sdinfo->satadrv_standby_timer);
4353 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4354 			if (sata_hba_start(spx, &rval) != 0) {
4355 				mutex_exit(cport_mutex);
4356 				return (rval);
4357 			} else {
4358 				if (scmd->satacmd_error_reg != 0) {
4359 					goto err_out;
4360 				}
4361 			}
4362 			break;
4363 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4364 		case SATA_PWRMODE_ACTIVE_SPINUP:
4365 		case SATA_PWRMODE_ACTIVE:
4366 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4367 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4368 			    sdinfo->satadrv_standby_timer);
4369 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4370 			if (sata_hba_start(spx, &rval) != 0) {
4371 				mutex_exit(cport_mutex);
4372 				return (rval);
4373 			}
4374 			if (scmd->satacmd_error_reg != 0) {
4375 				goto err_out;
4376 			}
4377 			sata_build_read_verify_cmd(scmd, 1, 5);
4378 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4379 			if (sata_hba_start(spx, &rval) != 0) {
4380 				mutex_exit(cport_mutex);
4381 				return (rval);
4382 			}
4383 			if (scmd->satacmd_error_reg != 0) {
4384 				goto err_out;
4385 			}
4386 			break;
4387 		default:
4388 			goto err_out;
4389 		}
4390 		break;
4391 	case 0xb:
4392 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4393 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4394 			mutex_exit(cport_mutex);
4395 			return (sata_txlt_check_condition(spx,
4396 			    KEY_ILLEGAL_REQUEST,
4397 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4398 		}
4399 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4400 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4401 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4402 			if (sata_hba_start(spx, &rval) != 0) {
4403 				mutex_exit(cport_mutex);
4404 				return (rval);
4405 			}
4406 			if (scmd->satacmd_error_reg != 0) {
4407 				goto err_out;
4408 			}
4409 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4410 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4411 			if (sata_hba_start(spx, &rval) != 0) {
4412 				mutex_exit(cport_mutex);
4413 				return (rval);
4414 			}
4415 			if (scmd->satacmd_error_reg != 0) {
4416 				goto err_out;
4417 			}
4418 		}
4419 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4420 		break;
4421 	default:
4422 err_out:
4423 		mutex_exit(cport_mutex);
4424 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4425 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4426 	}
4427 
4428 	/*
4429 	 * Since it was a synchronous command,
4430 	 * a callback function will be called directly.
4431 	 */
4432 	mutex_exit(cport_mutex);
4433 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4434 	    "synchronous execution status %x\n",
4435 	    spx->txlt_sata_pkt->satapkt_reason);
4436 
4437 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4438 	    scsipkt->pkt_comp != NULL) {
4439 		sata_set_arq_data(spx->txlt_sata_pkt);
4440 		if (servicing_interrupt()) {
4441 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4442 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4443 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4444 				return (TRAN_BUSY);
4445 			}
4446 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4447 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4448 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4449 			/* Scheduling the callback failed */
4450 			return (TRAN_BUSY);
4451 		}
4452 	}
4453 	else
4454 
4455 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4456 
4457 	return (TRAN_ACCEPT);
4458 
4459 }
4460 
4461 /*
4462  * SATA translate command:  Read Capacity.
4463  * Emulated command for SATA disks.
4464  * Capacity is retrieved from cached Idenifty Device data.
4465  * Identify Device data shows effective disk capacity, not the native
4466  * capacity, which may be limitted by Set Max Address command.
4467  * This is ATA version for SATA hard disks.
4468  *
4469  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4470  */
4471 static int
4472 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4473 {
4474 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4475 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4476 	sata_drive_info_t *sdinfo;
4477 	uint64_t val;
4478 	uchar_t *rbuf;
4479 	int rval, reason;
4480 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4481 
4482 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4483 	    "sata_txlt_read_capacity: ", NULL);
4484 
4485 	mutex_enter(cport_mutex);
4486 
4487 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4488 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4489 		mutex_exit(cport_mutex);
4490 		return (rval);
4491 	}
4492 
4493 	scsipkt->pkt_reason = CMD_CMPLT;
4494 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4495 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4496 	*scsipkt->pkt_scbp = STATUS_GOOD;
4497 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4498 		/*
4499 		 * Because it is fully emulated command storing data
4500 		 * programatically in the specified buffer, release
4501 		 * preallocated DMA resources before storing data in the buffer,
4502 		 * so no unwanted DMA sync would take place.
4503 		 */
4504 		sata_scsi_dmafree(NULL, scsipkt);
4505 
4506 		sdinfo = sata_get_device_info(
4507 		    spx->txlt_sata_hba_inst,
4508 		    &spx->txlt_sata_pkt->satapkt_device);
4509 
4510 		/*
4511 		 * As per SBC-3, the "returned LBA" is either the highest
4512 		 * addressable LBA or 0xffffffff, whichever is smaller.
4513 		 */
4514 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4515 
4516 		rbuf = (uchar_t *)bp->b_un.b_addr;
4517 		/* Need to swap endians to match scsi format */
4518 		rbuf[0] = (val >> 24) & 0xff;
4519 		rbuf[1] = (val >> 16) & 0xff;
4520 		rbuf[2] = (val >> 8) & 0xff;
4521 		rbuf[3] = val & 0xff;
4522 		/* block size - always 512 bytes, for now */
4523 		rbuf[4] = 0;
4524 		rbuf[5] = 0;
4525 		rbuf[6] = 0x02;
4526 		rbuf[7] = 0;
4527 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4528 		scsipkt->pkt_resid = 0;
4529 
4530 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4531 		    sdinfo->satadrv_capacity -1);
4532 	}
4533 	mutex_exit(cport_mutex);
4534 	/*
4535 	 * If a callback was requested, do it now.
4536 	 */
4537 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4538 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4539 
4540 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4541 	    scsipkt->pkt_comp != NULL) {
4542 		/* scsi callback required */
4543 		if (servicing_interrupt()) {
4544 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4545 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4546 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4547 				return (TRAN_BUSY);
4548 			}
4549 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4550 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4551 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4552 			/* Scheduling the callback failed */
4553 			return (TRAN_BUSY);
4554 		}
4555 	}
4556 
4557 	return (TRAN_ACCEPT);
4558 }
4559 
4560 /*
4561  * SATA translate command:  Read Capacity (16).
4562  * Emulated command for SATA disks.
4563  * Info is retrieved from cached Identify Device data.
4564  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4565  *
4566  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4567  */
4568 static int
4569 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4570 {
4571 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4572 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4573 	sata_drive_info_t *sdinfo;
4574 	uint64_t val;
4575 	uint16_t l2p_exp;
4576 	uchar_t *rbuf;
4577 	int rval, reason;
4578 #define	TPE	0x80
4579 #define	TPRZ	0x40
4580 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4581 
4582 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4583 	    "sata_txlt_read_capacity: ", NULL);
4584 
4585 	mutex_enter(cport_mutex);
4586 
4587 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4588 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4589 		mutex_exit(cport_mutex);
4590 		return (rval);
4591 	}
4592 
4593 	scsipkt->pkt_reason = CMD_CMPLT;
4594 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4595 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4596 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4597 		/*
4598 		 * Because it is fully emulated command storing data
4599 		 * programatically in the specified buffer, release
4600 		 * preallocated DMA resources before storing data in the buffer,
4601 		 * so no unwanted DMA sync would take place.
4602 		 */
4603 		sata_scsi_dmafree(NULL, scsipkt);
4604 
4605 		/* Check SERVICE ACTION field */
4606 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4607 		    SSVC_ACTION_READ_CAPACITY_G4) {
4608 			mutex_exit(cport_mutex);
4609 			return (sata_txlt_check_condition(spx,
4610 			    KEY_ILLEGAL_REQUEST,
4611 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4612 		}
4613 
4614 		/* Check LBA field */
4615 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4616 		    (scsipkt->pkt_cdbp[3] != 0) ||
4617 		    (scsipkt->pkt_cdbp[4] != 0) ||
4618 		    (scsipkt->pkt_cdbp[5] != 0) ||
4619 		    (scsipkt->pkt_cdbp[6] != 0) ||
4620 		    (scsipkt->pkt_cdbp[7] != 0) ||
4621 		    (scsipkt->pkt_cdbp[8] != 0) ||
4622 		    (scsipkt->pkt_cdbp[9] != 0)) {
4623 			mutex_exit(cport_mutex);
4624 			return (sata_txlt_check_condition(spx,
4625 			    KEY_ILLEGAL_REQUEST,
4626 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4627 		}
4628 
4629 		/* Check PMI bit */
4630 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4631 			mutex_exit(cport_mutex);
4632 			return (sata_txlt_check_condition(spx,
4633 			    KEY_ILLEGAL_REQUEST,
4634 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4635 		}
4636 
4637 		*scsipkt->pkt_scbp = STATUS_GOOD;
4638 
4639 		sdinfo = sata_get_device_info(
4640 		    spx->txlt_sata_hba_inst,
4641 		    &spx->txlt_sata_pkt->satapkt_device);
4642 
4643 		/* last logical block address */
4644 		val = MIN(sdinfo->satadrv_capacity - 1,
4645 		    SCSI_READ_CAPACITY16_MAX_LBA);
4646 
4647 		/* logical to physical block size exponent */
4648 		l2p_exp = 0;
4649 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4650 			/* physical/logical sector size word is valid */
4651 
4652 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4653 			    SATA_L2PS_HAS_MULT) {
4654 				/* multiple logical sectors per phys sectors */
4655 				l2p_exp =
4656 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4657 				    SATA_L2PS_EXP_MASK;
4658 			}
4659 		}
4660 
4661 		rbuf = (uchar_t *)bp->b_un.b_addr;
4662 		bzero(rbuf, bp->b_bcount);
4663 
4664 		/* returned logical block address */
4665 		rbuf[0] = (val >> 56) & 0xff;
4666 		rbuf[1] = (val >> 48) & 0xff;
4667 		rbuf[2] = (val >> 40) & 0xff;
4668 		rbuf[3] = (val >> 32) & 0xff;
4669 		rbuf[4] = (val >> 24) & 0xff;
4670 		rbuf[5] = (val >> 16) & 0xff;
4671 		rbuf[6] = (val >> 8) & 0xff;
4672 		rbuf[7] = val & 0xff;
4673 
4674 		/* logical block length in bytes = 512 (for now) */
4675 		/* rbuf[8] = 0; */
4676 		/* rbuf[9] = 0; */
4677 		rbuf[10] = 0x02;
4678 		/* rbuf[11] = 0; */
4679 
4680 		/* p_type, prot_en, unspecified by SAT-2 */
4681 		/* rbuf[12] = 0; */
4682 
4683 		/* p_i_exponent, undefined by SAT-2 */
4684 		/* logical blocks per physical block exponent */
4685 		rbuf[13] = l2p_exp;
4686 
4687 		/* lowest aligned logical block address = 0 (for now) */
4688 		/* tpe and tprz as defined in T10/10-079 r0 */
4689 		if (sdinfo->satadrv_id.ai_addsupported &
4690 		    SATA_DETERMINISTIC_READ) {
4691 			if (sdinfo->satadrv_id.ai_addsupported &
4692 			    SATA_READ_ZERO) {
4693 				rbuf[14] |= TPRZ;
4694 			} else {
4695 				rbuf[14] |= TPE;
4696 			}
4697 		}
4698 		/* rbuf[15] = 0; */
4699 
4700 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4701 		scsipkt->pkt_resid = 0;
4702 
4703 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4704 		    sdinfo->satadrv_capacity -1);
4705 	}
4706 
4707 	mutex_exit(cport_mutex);
4708 
4709 	/*
4710 	 * If a callback was requested, do it now.
4711 	 */
4712 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4713 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4714 
4715 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4716 	    scsipkt->pkt_comp != NULL) {
4717 		/* scsi callback required */
4718 		if (servicing_interrupt()) {
4719 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4720 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4721 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4722 				return (TRAN_BUSY);
4723 			}
4724 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4725 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4726 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4727 			/* Scheduling the callback failed */
4728 			return (TRAN_BUSY);
4729 		}
4730 	}
4731 
4732 	return (TRAN_ACCEPT);
4733 }
4734 
4735 /*
4736  * Translate command: UNMAP
4737  *
4738  * The function cannot be called in interrupt context since it may sleep.
4739  */
4740 static int
4741 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4742 {
4743 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4744 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4745 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4746 	uint16_t count = 0;
4747 	int synch;
4748 	int rval, reason;
4749 	int i, x;
4750 	int bdlen = 0;
4751 	int ranges = 0;
4752 	int paramlen = 8;
4753 	uint8_t *data, *tmpbd;
4754 	sata_drive_info_t *sdinfo;
4755 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4756 #define	TRIM	0x1
4757 
4758 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4759 	    "sata_txlt_unmap: ", NULL);
4760 
4761 	mutex_enter(cport_mutex);
4762 
4763 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4764 	    &spx->txlt_sata_pkt->satapkt_device);
4765 	if (sdinfo != NULL) {
4766 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4767 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4768 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4769 		    sdinfo->satadrv_id.ai_maxcount);
4770 	}
4771 
4772 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4773 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4774 		mutex_exit(cport_mutex);
4775 		return (rval);
4776 	}
4777 
4778 	/*
4779 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4780 	 * Start by getting the block descriptor data length by subtracting
4781 	 * the 8 byte parameter list header from the parameter list length.
4782 	 * The block descriptor size has to be a multiple of 16 bytes.
4783 	 */
4784 	bdlen = scsipkt->pkt_cdbp[7];
4785 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4786 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4787 	    (bdlen > (bp->b_bcount - paramlen))) {
4788 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4789 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4790 		mutex_exit(cport_mutex);
4791 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4792 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4793 	}
4794 	/*
4795 	 * If there are no parameter data or block descriptors, it is not
4796 	 * considered an error so just complete the command without sending
4797 	 * TRIM.
4798 	 */
4799 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4800 	    (bp->b_bcount == 0)) {
4801 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4802 		    "sata_txlt_unmap: no parameter data or block descriptors",
4803 		    NULL);
4804 		mutex_exit(cport_mutex);
4805 		return (sata_txlt_unmap_nodata_cmd(spx));
4806 	}
4807 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4808 	data = kmem_zalloc(bdlen, KM_SLEEP);
4809 
4810 	/*
4811 	 * Loop through all the UNMAP block descriptors and convert the data
4812 	 * into TRIM format.
4813 	 */
4814 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4815 		/* get range length */
4816 		data[x] = tmpbd[i+7];
4817 		data[x+1] = tmpbd[i+6];
4818 		/* get LBA */
4819 		data[x+2] = tmpbd[i+5];
4820 		data[x+3] = tmpbd[i+4];
4821 		data[x+4] = tmpbd[i+3];
4822 		data[x+5] = tmpbd[i+2];
4823 		data[x+6] = tmpbd[i+11];
4824 		data[x+7] = tmpbd[i+10];
4825 
4826 		ranges++;
4827 	}
4828 
4829 	/*
4830 	 * The TRIM command expects the data buffer to be a multiple of
4831 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4832 	 * may be too small.  Free the original DMA resources and create a
4833 	 * local buffer.
4834 	 */
4835 	sata_common_free_dma_rsrcs(spx);
4836 
4837 	/*
4838 	 * Get count of 512-byte blocks of range entries.  The length
4839 	 * of a range entry is 8 bytes which means one count has 64 range
4840 	 * entries.
4841 	 */
4842 	count = (ranges + 63)/64;
4843 
4844 	/* Allocate a buffer that is a multiple of 512 bytes. */
4845 	mutex_exit(cport_mutex);
4846 	bp = sata_alloc_local_buffer(spx, count * 512);
4847 	if (bp == NULL) {
4848 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4849 		    "sata_txlt_unmap: "
4850 		    "cannot allocate buffer for TRIM command", NULL);
4851 		kmem_free(data, bdlen);
4852 		return (TRAN_BUSY);
4853 	}
4854 	bp_mapin(bp); /* make data buffer accessible */
4855 	mutex_enter(cport_mutex);
4856 
4857 	bzero(bp->b_un.b_addr, bp->b_bcount);
4858 	bcopy(data, bp->b_un.b_addr, x);
4859 	kmem_free(data, bdlen);
4860 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4861 	    DDI_DMA_SYNC_FORDEV);
4862 	ASSERT(rval == DDI_SUCCESS);
4863 
4864 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4865 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4866 	scmd->satacmd_cmd_reg = SATAC_DSM;
4867 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4868 	scmd->satacmd_sec_count_lsb = count & 0xff;
4869 	scmd->satacmd_features_reg = TRIM;
4870 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4871 	scmd->satacmd_status_reg = 0;
4872 	scmd->satacmd_error_reg = 0;
4873 
4874 	/* Start processing command */
4875 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4876 		spx->txlt_sata_pkt->satapkt_comp =
4877 		    sata_txlt_unmap_completion;
4878 		synch = FALSE;
4879 	} else {
4880 		synch = TRUE;
4881 	}
4882 
4883 	if (sata_hba_start(spx, &rval) != 0) {
4884 		mutex_exit(cport_mutex);
4885 		return (rval);
4886 	}
4887 
4888 	mutex_exit(cport_mutex);
4889 
4890 	if (synch) {
4891 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4892 	}
4893 
4894 	return (TRAN_ACCEPT);
4895 }
4896 
4897 /*
4898  * SATA translate command: Mode Sense.
4899  * Translated into appropriate SATA command or emulated.
4900  * Saved Values Page Control (03) are not supported.
4901  *
4902  * NOTE: only caching mode sense page is currently implemented.
4903  *
4904  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4905  */
4906 
4907 #define	LLBAA	0x10	/* Long LBA Accepted */
4908 
4909 static int
4910 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4911 {
4912 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4913 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4914 	sata_drive_info_t *sdinfo;
4915 	sata_id_t *sata_id;
4916 	struct scsi_extended_sense *sense;
4917 	int 		len, bdlen, count, alc_len;
4918 	int		pc;	/* Page Control code */
4919 	uint8_t		*buf;	/* mode sense buffer */
4920 	int		rval, reason;
4921 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4922 
4923 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4924 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4925 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4926 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4927 
4928 	if (servicing_interrupt()) {
4929 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4930 		if (buf == NULL) {
4931 			return (TRAN_BUSY);
4932 		}
4933 	} else {
4934 		buf = kmem_zalloc(1024, KM_SLEEP);
4935 	}
4936 
4937 	mutex_enter(cport_mutex);
4938 
4939 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4940 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4941 		mutex_exit(cport_mutex);
4942 		kmem_free(buf, 1024);
4943 		return (rval);
4944 	}
4945 
4946 	scsipkt->pkt_reason = CMD_CMPLT;
4947 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4948 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4949 
4950 	pc = scsipkt->pkt_cdbp[2] >> 6;
4951 
4952 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4953 		/*
4954 		 * Because it is fully emulated command storing data
4955 		 * programatically in the specified buffer, release
4956 		 * preallocated DMA resources before storing data in the buffer,
4957 		 * so no unwanted DMA sync would take place.
4958 		 */
4959 		sata_scsi_dmafree(NULL, scsipkt);
4960 
4961 		len = 0;
4962 		bdlen = 0;
4963 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4964 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4965 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4966 				bdlen = 16;
4967 			else
4968 				bdlen = 8;
4969 		}
4970 		/* Build mode parameter header */
4971 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4972 			/* 4-byte mode parameter header */
4973 			buf[len++] = 0;		/* mode data length */
4974 			buf[len++] = 0;		/* medium type */
4975 			buf[len++] = 0;		/* dev-specific param */
4976 			buf[len++] = bdlen;	/* Block Descriptor length */
4977 		} else {
4978 			/* 8-byte mode parameter header */
4979 			buf[len++] = 0;		/* mode data length */
4980 			buf[len++] = 0;
4981 			buf[len++] = 0;		/* medium type */
4982 			buf[len++] = 0;		/* dev-specific param */
4983 			if (bdlen == 16)
4984 				buf[len++] = 1;	/* long lba descriptor */
4985 			else
4986 				buf[len++] = 0;
4987 			buf[len++] = 0;
4988 			buf[len++] = 0;		/* Block Descriptor length */
4989 			buf[len++] = bdlen;
4990 		}
4991 
4992 		sdinfo = sata_get_device_info(
4993 		    spx->txlt_sata_hba_inst,
4994 		    &spx->txlt_sata_pkt->satapkt_device);
4995 
4996 		/* Build block descriptor only if not disabled (DBD) */
4997 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4998 			/* Block descriptor - direct-access device format */
4999 			if (bdlen == 8) {
5000 				/* build regular block descriptor */
5001 				buf[len++] =
5002 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5003 				buf[len++] =
5004 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5005 				buf[len++] =
5006 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5007 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5008 				buf[len++] = 0; /* density code */
5009 				buf[len++] = 0;
5010 				if (sdinfo->satadrv_type ==
5011 				    SATA_DTYPE_ATADISK)
5012 					buf[len++] = 2;
5013 				else
5014 					/* ATAPI */
5015 					buf[len++] = 8;
5016 				buf[len++] = 0;
5017 			} else if (bdlen == 16) {
5018 				/* Long LBA Accepted */
5019 				/* build long lba block descriptor */
5020 #ifndef __lock_lint
5021 				buf[len++] =
5022 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5023 				buf[len++] =
5024 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5025 				buf[len++] =
5026 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5027 				buf[len++] =
5028 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5029 #endif
5030 				buf[len++] =
5031 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5032 				buf[len++] =
5033 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5034 				buf[len++] =
5035 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5036 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5037 				buf[len++] = 0;
5038 				buf[len++] = 0; /* density code */
5039 				buf[len++] = 0;
5040 				buf[len++] = 0;
5041 				if (sdinfo->satadrv_type ==
5042 				    SATA_DTYPE_ATADISK)
5043 					buf[len++] = 2;
5044 				else
5045 					/* ATAPI */
5046 					buf[len++] = 8;
5047 				buf[len++] = 0;
5048 			}
5049 		}
5050 
5051 		sata_id = &sdinfo->satadrv_id;
5052 
5053 		/*
5054 		 * Add requested pages.
5055 		 * Page 3 and 4 are obsolete and we are not supporting them.
5056 		 * We deal now with:
5057 		 * caching (read/write cache control).
5058 		 * We should eventually deal with following mode pages:
5059 		 * error recovery  (0x01),
5060 		 * power condition (0x1a),
5061 		 * exception control page (enables SMART) (0x1c),
5062 		 * enclosure management (ses),
5063 		 * protocol-specific port mode (port control).
5064 		 */
5065 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5066 		case MODEPAGE_RW_ERRRECOV:
5067 			/* DAD_MODE_ERR_RECOV */
5068 			/* R/W recovery */
5069 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5070 			break;
5071 		case MODEPAGE_CACHING:
5072 			/* DAD_MODE_CACHE */
5073 			/* Reject not supported request for saved parameters */
5074 			if (pc == 3) {
5075 				*scsipkt->pkt_scbp = STATUS_CHECK;
5076 				sense = sata_arq_sense(spx);
5077 				sense->es_key = KEY_ILLEGAL_REQUEST;
5078 				sense->es_add_code =
5079 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5080 				goto done;
5081 			}
5082 
5083 			/* caching */
5084 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5085 			break;
5086 		case MODEPAGE_INFO_EXCPT:
5087 			/* exception cntrl */
5088 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5089 				len += sata_build_msense_page_1c(sdinfo, pc,
5090 				    buf+len);
5091 			}
5092 			else
5093 				goto err;
5094 			break;
5095 		case MODEPAGE_POWER_COND:
5096 			/* DAD_MODE_POWER_COND */
5097 			/* power condition */
5098 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5099 			break;
5100 
5101 		case MODEPAGE_ACOUSTIC_MANAG:
5102 			/* acoustic management */
5103 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5104 			break;
5105 		case MODEPAGE_ALLPAGES:
5106 			/* all pages */
5107 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5108 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5109 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5110 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5111 				len += sata_build_msense_page_1c(sdinfo, pc,
5112 				    buf+len);
5113 			}
5114 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5115 			break;
5116 		default:
5117 		err:
5118 			/* Invalid request */
5119 			*scsipkt->pkt_scbp = STATUS_CHECK;
5120 			sense = sata_arq_sense(spx);
5121 			sense->es_key = KEY_ILLEGAL_REQUEST;
5122 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5123 			goto done;
5124 		}
5125 
5126 		/* fix total mode data length */
5127 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5128 			/* 4-byte mode parameter header */
5129 			buf[0] = len - 1;	/* mode data length */
5130 		} else {
5131 			buf[0] = (len -2) >> 8;
5132 			buf[1] = (len -2) & 0xff;
5133 		}
5134 
5135 
5136 		/* Check allocation length */
5137 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5138 			alc_len = scsipkt->pkt_cdbp[4];
5139 		} else {
5140 			alc_len = scsipkt->pkt_cdbp[7];
5141 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5142 		}
5143 		/*
5144 		 * We do not check for possible parameters truncation
5145 		 * (alc_len < len) assuming that the target driver works
5146 		 * correctly. Just avoiding overrun.
5147 		 * Copy no more than requested and possible, buffer-wise.
5148 		 */
5149 		count = MIN(alc_len, len);
5150 		count = MIN(bp->b_bcount, count);
5151 		bcopy(buf, bp->b_un.b_addr, count);
5152 
5153 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5154 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5155 	}
5156 	*scsipkt->pkt_scbp = STATUS_GOOD;
5157 done:
5158 	mutex_exit(cport_mutex);
5159 	(void) kmem_free(buf, 1024);
5160 
5161 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5162 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5163 
5164 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5165 	    scsipkt->pkt_comp != NULL) {
5166 		/* scsi callback required */
5167 		if (servicing_interrupt()) {
5168 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5169 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5170 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5171 				return (TRAN_BUSY);
5172 			}
5173 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5174 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5175 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5176 			/* Scheduling the callback failed */
5177 			return (TRAN_BUSY);
5178 		}
5179 	}
5180 
5181 	return (TRAN_ACCEPT);
5182 }
5183 
5184 
5185 /*
5186  * SATA translate command: Mode Select.
5187  * Translated into appropriate SATA command or emulated.
5188  * Saving parameters is not supported.
5189  * Changing device capacity is not supported (although theoretically
5190  * possible by executing SET FEATURES/SET MAX ADDRESS)
5191  *
5192  * Assumption is that the target driver is working correctly.
5193  *
5194  * More than one SATA command may be executed to perform operations specified
5195  * by mode select pages. The first error terminates further execution.
5196  * Operations performed successully are not backed-up in such case.
5197  *
5198  * NOTE: Implemented pages:
5199  * - caching page
5200  * - informational exception page
5201  * - acoustic management page
5202  * - power condition page
5203  * Caching setup is remembered so it could be re-stored in case of
5204  * an unexpected device reset.
5205  *
5206  * Returns TRAN_XXXX.
5207  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5208  */
5209 
5210 static int
5211 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5212 {
5213 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5214 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5215 	struct scsi_extended_sense *sense;
5216 	int len, pagelen, count, pllen;
5217 	uint8_t *buf;	/* mode select buffer */
5218 	int rval, stat, reason;
5219 	uint_t nointr_flag;
5220 	int dmod = 0;
5221 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5222 
5223 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5224 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5225 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5226 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5227 
5228 	mutex_enter(cport_mutex);
5229 
5230 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5231 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5232 		mutex_exit(cport_mutex);
5233 		return (rval);
5234 	}
5235 
5236 	rval = TRAN_ACCEPT;
5237 
5238 	scsipkt->pkt_reason = CMD_CMPLT;
5239 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5240 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5241 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5242 
5243 	/* Reject not supported request */
5244 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5245 		*scsipkt->pkt_scbp = STATUS_CHECK;
5246 		sense = sata_arq_sense(spx);
5247 		sense->es_key = KEY_ILLEGAL_REQUEST;
5248 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5249 		goto done;
5250 	}
5251 
5252 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5253 		pllen = scsipkt->pkt_cdbp[4];
5254 	} else {
5255 		pllen = scsipkt->pkt_cdbp[7];
5256 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5257 	}
5258 
5259 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5260 
5261 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5262 		buf = (uint8_t *)bp->b_un.b_addr;
5263 		count = MIN(bp->b_bcount, pllen);
5264 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5265 		scsipkt->pkt_resid = 0;
5266 		pllen = count;
5267 
5268 		/*
5269 		 * Check the header to skip the block descriptor(s) - we
5270 		 * do not support setting device capacity.
5271 		 * Existing macros do not recognize long LBA dscriptor,
5272 		 * hence manual calculation.
5273 		 */
5274 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5275 			/* 6-bytes CMD, 4 bytes header */
5276 			if (count <= 4)
5277 				goto done;		/* header only */
5278 			len = buf[3] + 4;
5279 		} else {
5280 			/* 10-bytes CMD, 8 bytes header */
5281 			if (count <= 8)
5282 				goto done;		/* header only */
5283 			len = buf[6];
5284 			len = (len << 8) + buf[7] + 8;
5285 		}
5286 		if (len >= count)
5287 			goto done;	/* header + descriptor(s) only */
5288 
5289 		pllen -= len;		/* remaining data length */
5290 
5291 		/*
5292 		 * We may be executing SATA command and want to execute it
5293 		 * in SYNCH mode, regardless of scsi_pkt setting.
5294 		 * Save scsi_pkt setting and indicate SYNCH mode
5295 		 */
5296 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5297 		    scsipkt->pkt_comp != NULL) {
5298 			scsipkt->pkt_flags |= FLAG_NOINTR;
5299 		}
5300 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5301 
5302 		/*
5303 		 * len is now the offset to a first mode select page
5304 		 * Process all pages
5305 		 */
5306 		while (pllen > 0) {
5307 			switch ((int)buf[len]) {
5308 			case MODEPAGE_CACHING:
5309 				/* No support for SP (saving) */
5310 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5311 					*scsipkt->pkt_scbp = STATUS_CHECK;
5312 					sense = sata_arq_sense(spx);
5313 					sense->es_key = KEY_ILLEGAL_REQUEST;
5314 					sense->es_add_code =
5315 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5316 					goto done;
5317 				}
5318 				stat = sata_mode_select_page_8(spx,
5319 				    (struct mode_cache_scsi3 *)&buf[len],
5320 				    pllen, &pagelen, &rval, &dmod);
5321 				/*
5322 				 * The pagelen value indicates the number of
5323 				 * parameter bytes already processed.
5324 				 * The rval is the return value from
5325 				 * sata_tran_start().
5326 				 * The stat indicates the overall status of
5327 				 * the operation(s).
5328 				 */
5329 				if (stat != SATA_SUCCESS)
5330 					/*
5331 					 * Page processing did not succeed -
5332 					 * all error info is already set-up,
5333 					 * just return
5334 					 */
5335 					pllen = 0; /* this breaks the loop */
5336 				else {
5337 					len += pagelen;
5338 					pllen -= pagelen;
5339 				}
5340 				break;
5341 
5342 			case MODEPAGE_INFO_EXCPT:
5343 				stat = sata_mode_select_page_1c(spx,
5344 				    (struct mode_info_excpt_page *)&buf[len],
5345 				    pllen, &pagelen, &rval, &dmod);
5346 				/*
5347 				 * The pagelen value indicates the number of
5348 				 * parameter bytes already processed.
5349 				 * The rval is the return value from
5350 				 * sata_tran_start().
5351 				 * The stat indicates the overall status of
5352 				 * the operation(s).
5353 				 */
5354 				if (stat != SATA_SUCCESS)
5355 					/*
5356 					 * Page processing did not succeed -
5357 					 * all error info is already set-up,
5358 					 * just return
5359 					 */
5360 					pllen = 0; /* this breaks the loop */
5361 				else {
5362 					len += pagelen;
5363 					pllen -= pagelen;
5364 				}
5365 				break;
5366 
5367 			case MODEPAGE_ACOUSTIC_MANAG:
5368 				stat = sata_mode_select_page_30(spx,
5369 				    (struct mode_acoustic_management *)
5370 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5371 				/*
5372 				 * The pagelen value indicates the number of
5373 				 * parameter bytes already processed.
5374 				 * The rval is the return value from
5375 				 * sata_tran_start().
5376 				 * The stat indicates the overall status of
5377 				 * the operation(s).
5378 				 */
5379 				if (stat != SATA_SUCCESS)
5380 					/*
5381 					 * Page processing did not succeed -
5382 					 * all error info is already set-up,
5383 					 * just return
5384 					 */
5385 					pllen = 0; /* this breaks the loop */
5386 				else {
5387 					len += pagelen;
5388 					pllen -= pagelen;
5389 				}
5390 
5391 				break;
5392 			case MODEPAGE_POWER_COND:
5393 				stat = sata_mode_select_page_1a(spx,
5394 				    (struct mode_info_power_cond *)&buf[len],
5395 				    pllen, &pagelen, &rval, &dmod);
5396 				/*
5397 				 * The pagelen value indicates the number of
5398 				 * parameter bytes already processed.
5399 				 * The rval is the return value from
5400 				 * sata_tran_start().
5401 				 * The stat indicates the overall status of
5402 				 * the operation(s).
5403 				 */
5404 				if (stat != SATA_SUCCESS)
5405 					/*
5406 					 * Page processing did not succeed -
5407 					 * all error info is already set-up,
5408 					 * just return
5409 					 */
5410 					pllen = 0; /* this breaks the loop */
5411 				else {
5412 					len += pagelen;
5413 					pllen -= pagelen;
5414 				}
5415 				break;
5416 			default:
5417 				*scsipkt->pkt_scbp = STATUS_CHECK;
5418 				sense = sata_arq_sense(spx);
5419 				sense->es_key = KEY_ILLEGAL_REQUEST;
5420 				sense->es_add_code =
5421 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5422 				goto done;
5423 			}
5424 		}
5425 	}
5426 done:
5427 	mutex_exit(cport_mutex);
5428 	/*
5429 	 * If device parameters were modified, fetch and store the new
5430 	 * Identify Device data. Since port mutex could have been released
5431 	 * for accessing HBA driver, we need to re-check device existence.
5432 	 */
5433 	if (dmod != 0) {
5434 		sata_drive_info_t new_sdinfo, *sdinfo;
5435 		int rv = 0;
5436 
5437 		/*
5438 		 * Following statement has to be changed if this function is
5439 		 * used for devices other than SATA hard disks.
5440 		 */
5441 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5442 
5443 		new_sdinfo.satadrv_addr =
5444 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5445 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5446 		    &new_sdinfo);
5447 
5448 		mutex_enter(cport_mutex);
5449 		/*
5450 		 * Since port mutex could have been released when
5451 		 * accessing HBA driver, we need to re-check that the
5452 		 * framework still holds the device info structure.
5453 		 */
5454 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5455 		    &spx->txlt_sata_pkt->satapkt_device);
5456 		if (sdinfo != NULL) {
5457 			/*
5458 			 * Device still has info structure in the
5459 			 * sata framework. Copy newly fetched info
5460 			 */
5461 			if (rv == 0) {
5462 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5463 				sata_save_drive_settings(sdinfo);
5464 			} else {
5465 				/*
5466 				 * Could not fetch new data - invalidate
5467 				 * sata_drive_info. That makes device
5468 				 * unusable.
5469 				 */
5470 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5471 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5472 			}
5473 		}
5474 		if (rv != 0 || sdinfo == NULL) {
5475 			/*
5476 			 * This changes the overall mode select completion
5477 			 * reason to a failed one !!!!!
5478 			 */
5479 			*scsipkt->pkt_scbp = STATUS_CHECK;
5480 			sense = sata_arq_sense(spx);
5481 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5482 			rval = TRAN_ACCEPT;
5483 		}
5484 		mutex_exit(cport_mutex);
5485 	}
5486 	/* Restore the scsi pkt flags */
5487 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5488 	scsipkt->pkt_flags |= nointr_flag;
5489 
5490 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5491 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5492 
5493 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5494 	    scsipkt->pkt_comp != NULL) {
5495 		/* scsi callback required */
5496 		if (servicing_interrupt()) {
5497 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5498 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5499 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5500 				return (TRAN_BUSY);
5501 			}
5502 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5503 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5504 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5505 			/* Scheduling the callback failed */
5506 			return (TRAN_BUSY);
5507 		}
5508 	}
5509 
5510 	return (rval);
5511 }
5512 
5513 /*
5514  * Translate command: ATA Pass Through
5515  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5516  * PIO Data-Out protocols.  Also supports CK_COND bit.
5517  *
5518  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5519  * described in Table 111 of SAT-2 (Draft 9).
5520  */
5521 static  int
5522 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5523 {
5524 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5525 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5526 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5527 	int extend;
5528 	uint64_t lba;
5529 	uint16_t feature, sec_count;
5530 	int t_len, synch;
5531 	int rval, reason;
5532 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5533 
5534 	mutex_enter(cport_mutex);
5535 
5536 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5537 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5538 		mutex_exit(cport_mutex);
5539 		return (rval);
5540 	}
5541 
5542 	/* T_DIR bit */
5543 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5544 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5545 	else
5546 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5547 
5548 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5549 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5550 		mutex_exit(cport_mutex);
5551 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5552 	}
5553 
5554 	/* OFFLINE field. If non-zero, invalid command (for now). */
5555 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5556 		mutex_exit(cport_mutex);
5557 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5558 	}
5559 
5560 	/* PROTOCOL field */
5561 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5562 	case SATL_APT_P_HW_RESET:
5563 	case SATL_APT_P_SRST:
5564 	case SATL_APT_P_DMA:
5565 	case SATL_APT_P_DMA_QUEUED:
5566 	case SATL_APT_P_DEV_DIAG:
5567 	case SATL_APT_P_DEV_RESET:
5568 	case SATL_APT_P_UDMA_IN:
5569 	case SATL_APT_P_UDMA_OUT:
5570 	case SATL_APT_P_FPDMA:
5571 	case SATL_APT_P_RET_RESP:
5572 		/* Not yet implemented */
5573 	default:
5574 		mutex_exit(cport_mutex);
5575 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5576 
5577 	case SATL_APT_P_NON_DATA:
5578 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5579 		break;
5580 
5581 	case SATL_APT_P_PIO_DATA_IN:
5582 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5583 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5584 			mutex_exit(cport_mutex);
5585 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5586 		}
5587 
5588 		/* if there is a buffer, release its DMA resources */
5589 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5590 			sata_scsi_dmafree(NULL, scsipkt);
5591 		} else {
5592 			/* if there is no buffer, how do you PIO in? */
5593 			mutex_exit(cport_mutex);
5594 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5595 		}
5596 
5597 		break;
5598 
5599 	case SATL_APT_P_PIO_DATA_OUT:
5600 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5601 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5602 			mutex_exit(cport_mutex);
5603 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5604 		}
5605 
5606 		/* if there is a buffer, release its DMA resources */
5607 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5608 			sata_scsi_dmafree(NULL, scsipkt);
5609 		} else {
5610 			/* if there is no buffer, how do you PIO out? */
5611 			mutex_exit(cport_mutex);
5612 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5613 		}
5614 
5615 		break;
5616 	}
5617 
5618 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5619 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5620 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5621 		feature = scsipkt->pkt_cdbp[3];
5622 
5623 		sec_count = scsipkt->pkt_cdbp[4];
5624 
5625 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5626 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5627 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5628 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5629 
5630 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5631 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5632 
5633 		break;
5634 
5635 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5636 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5637 			extend = 1;
5638 
5639 			feature = scsipkt->pkt_cdbp[3];
5640 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5641 
5642 			sec_count = scsipkt->pkt_cdbp[5];
5643 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5644 
5645 			lba = scsipkt->pkt_cdbp[11];
5646 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5647 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5648 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5649 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5650 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5651 
5652 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5653 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5654 		} else {
5655 			feature = scsipkt->pkt_cdbp[3];
5656 
5657 			sec_count = scsipkt->pkt_cdbp[5];
5658 
5659 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5660 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5661 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5662 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5663 
5664 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5665 			    0xf0;
5666 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5667 		}
5668 
5669 		break;
5670 	}
5671 
5672 	/* CK_COND bit */
5673 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5674 		if (extend) {
5675 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5676 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5677 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5678 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5679 		}
5680 
5681 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5682 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5683 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5684 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5685 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5686 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5687 	}
5688 
5689 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5690 	if (extend) {
5691 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5692 
5693 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5694 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5695 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5696 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5697 		scmd->satacmd_lba_high_msb = lba >> 40;
5698 	} else {
5699 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5700 
5701 		scmd->satacmd_features_reg_ext = 0;
5702 		scmd->satacmd_sec_count_msb = 0;
5703 		scmd->satacmd_lba_low_msb = 0;
5704 		scmd->satacmd_lba_mid_msb = 0;
5705 		scmd->satacmd_lba_high_msb = 0;
5706 	}
5707 
5708 	scmd->satacmd_features_reg = feature & 0xff;
5709 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5710 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5711 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5712 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5713 
5714 	/* Determine transfer length */
5715 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5716 	case 1:
5717 		t_len = feature;
5718 		break;
5719 	case 2:
5720 		t_len = sec_count;
5721 		break;
5722 	default:
5723 		t_len = 0;
5724 		break;
5725 	}
5726 
5727 	/* Adjust transfer length for the Byte Block bit */
5728 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5729 		t_len *= SATA_DISK_SECTOR_SIZE;
5730 
5731 	/* Start processing command */
5732 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5733 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5734 		synch = FALSE;
5735 	} else {
5736 		synch = TRUE;
5737 	}
5738 
5739 	if (sata_hba_start(spx, &rval) != 0) {
5740 		mutex_exit(cport_mutex);
5741 		return (rval);
5742 	}
5743 
5744 	mutex_exit(cport_mutex);
5745 
5746 	if (synch) {
5747 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5748 	}
5749 
5750 	return (TRAN_ACCEPT);
5751 }
5752 
5753 /*
5754  * Translate command: Log Sense
5755  */
5756 static 	int
5757 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5758 {
5759 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5760 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5761 	sata_drive_info_t *sdinfo;
5762 	struct scsi_extended_sense *sense;
5763 	int 		len, count, alc_len;
5764 	int		pc;	/* Page Control code */
5765 	int		page_code;	/* Page code */
5766 	uint8_t		*buf;	/* log sense buffer */
5767 	int		rval, reason;
5768 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5769 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5770 
5771 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5772 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5773 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5774 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5775 
5776 	if (servicing_interrupt()) {
5777 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5778 		if (buf == NULL) {
5779 			return (TRAN_BUSY);
5780 		}
5781 	} else {
5782 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5783 	}
5784 
5785 	mutex_enter(cport_mutex);
5786 
5787 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5788 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5789 		mutex_exit(cport_mutex);
5790 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5791 		return (rval);
5792 	}
5793 
5794 	scsipkt->pkt_reason = CMD_CMPLT;
5795 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5796 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5797 
5798 	pc = scsipkt->pkt_cdbp[2] >> 6;
5799 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5800 
5801 	/* Reject not supported request for all but cumulative values */
5802 	switch (pc) {
5803 	case PC_CUMULATIVE_VALUES:
5804 		break;
5805 	default:
5806 		*scsipkt->pkt_scbp = STATUS_CHECK;
5807 		sense = sata_arq_sense(spx);
5808 		sense->es_key = KEY_ILLEGAL_REQUEST;
5809 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5810 		goto done;
5811 	}
5812 
5813 	switch (page_code) {
5814 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5815 	case PAGE_CODE_SELF_TEST_RESULTS:
5816 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5817 	case PAGE_CODE_SMART_READ_DATA:
5818 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5819 		break;
5820 	default:
5821 		*scsipkt->pkt_scbp = STATUS_CHECK;
5822 		sense = sata_arq_sense(spx);
5823 		sense->es_key = KEY_ILLEGAL_REQUEST;
5824 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5825 		goto done;
5826 	}
5827 
5828 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5829 		/*
5830 		 * Because log sense uses local buffers for data retrieval from
5831 		 * the devices and sets the data programatically in the
5832 		 * original specified buffer, release preallocated DMA
5833 		 * resources before storing data in the original buffer,
5834 		 * so no unwanted DMA sync would take place.
5835 		 */
5836 		sata_id_t *sata_id;
5837 
5838 		sata_scsi_dmafree(NULL, scsipkt);
5839 
5840 		len = 0;
5841 
5842 		/* Build log parameter header */
5843 		buf[len++] = page_code;	/* page code as in the CDB */
5844 		buf[len++] = 0;		/* reserved */
5845 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5846 		buf[len++] = 0;		/* (LSB) */
5847 
5848 		sdinfo = sata_get_device_info(
5849 		    spx->txlt_sata_hba_inst,
5850 		    &spx->txlt_sata_pkt->satapkt_device);
5851 
5852 		/*
5853 		 * Add requested pages.
5854 		 */
5855 		switch (page_code) {
5856 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5857 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5858 			break;
5859 		case PAGE_CODE_SELF_TEST_RESULTS:
5860 			sata_id = &sdinfo->satadrv_id;
5861 			if ((! (sata_id->ai_cmdset84 &
5862 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5863 			    (! (sata_id->ai_features87 &
5864 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5865 				*scsipkt->pkt_scbp = STATUS_CHECK;
5866 				sense = sata_arq_sense(spx);
5867 				sense->es_key = KEY_ILLEGAL_REQUEST;
5868 				sense->es_add_code =
5869 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5870 
5871 				goto done;
5872 			}
5873 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5874 			    spx->txlt_sata_hba_inst);
5875 			break;
5876 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5877 			sata_id = &sdinfo->satadrv_id;
5878 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5879 				*scsipkt->pkt_scbp = STATUS_CHECK;
5880 				sense = sata_arq_sense(spx);
5881 				sense->es_key = KEY_ILLEGAL_REQUEST;
5882 				sense->es_add_code =
5883 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5884 
5885 				goto done;
5886 			}
5887 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5888 				*scsipkt->pkt_scbp = STATUS_CHECK;
5889 				sense = sata_arq_sense(spx);
5890 				sense->es_key = KEY_ABORTED_COMMAND;
5891 				sense->es_add_code =
5892 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5893 				sense->es_qual_code =
5894 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5895 
5896 				goto done;
5897 			}
5898 
5899 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5900 			    spx->txlt_sata_hba_inst);
5901 			break;
5902 		case PAGE_CODE_SMART_READ_DATA:
5903 			sata_id = &sdinfo->satadrv_id;
5904 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5905 				*scsipkt->pkt_scbp = STATUS_CHECK;
5906 				sense = sata_arq_sense(spx);
5907 				sense->es_key = KEY_ILLEGAL_REQUEST;
5908 				sense->es_add_code =
5909 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5910 
5911 				goto done;
5912 			}
5913 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5914 				*scsipkt->pkt_scbp = STATUS_CHECK;
5915 				sense = sata_arq_sense(spx);
5916 				sense->es_key = KEY_ABORTED_COMMAND;
5917 				sense->es_add_code =
5918 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5919 				sense->es_qual_code =
5920 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5921 
5922 				goto done;
5923 			}
5924 
5925 			/* This page doesn't include a page header */
5926 			len = sata_build_lsense_page_30(sdinfo, buf,
5927 			    spx->txlt_sata_hba_inst);
5928 			goto no_header;
5929 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5930 			sata_id = &sdinfo->satadrv_id;
5931 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5932 				*scsipkt->pkt_scbp = STATUS_CHECK;
5933 				sense = sata_arq_sense(spx);
5934 				sense->es_key = KEY_ILLEGAL_REQUEST;
5935 				sense->es_add_code =
5936 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5937 
5938 				goto done;
5939 			}
5940 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5941 				*scsipkt->pkt_scbp = STATUS_CHECK;
5942 				sense = sata_arq_sense(spx);
5943 				sense->es_key = KEY_ABORTED_COMMAND;
5944 				sense->es_add_code =
5945 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5946 				sense->es_qual_code =
5947 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5948 
5949 				goto done;
5950 			}
5951 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5952 			goto no_header;
5953 		default:
5954 			/* Invalid request */
5955 			*scsipkt->pkt_scbp = STATUS_CHECK;
5956 			sense = sata_arq_sense(spx);
5957 			sense->es_key = KEY_ILLEGAL_REQUEST;
5958 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5959 			goto done;
5960 		}
5961 
5962 		/* set parameter log sense data length */
5963 		buf[2] = len >> 8;	/* log sense length (MSB) */
5964 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5965 
5966 		len += SCSI_LOG_PAGE_HDR_LEN;
5967 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5968 
5969 no_header:
5970 		/* Check allocation length */
5971 		alc_len = scsipkt->pkt_cdbp[7];
5972 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5973 
5974 		/*
5975 		 * We do not check for possible parameters truncation
5976 		 * (alc_len < len) assuming that the target driver works
5977 		 * correctly. Just avoiding overrun.
5978 		 * Copy no more than requested and possible, buffer-wise.
5979 		 */
5980 		count = MIN(alc_len, len);
5981 		count = MIN(bp->b_bcount, count);
5982 		bcopy(buf, bp->b_un.b_addr, count);
5983 
5984 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5985 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5986 	}
5987 	*scsipkt->pkt_scbp = STATUS_GOOD;
5988 done:
5989 	mutex_exit(cport_mutex);
5990 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5991 
5992 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5993 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5994 
5995 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5996 	    scsipkt->pkt_comp != NULL) {
5997 		/* scsi callback required */
5998 		if (servicing_interrupt()) {
5999 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6000 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6001 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6002 				return (TRAN_BUSY);
6003 			}
6004 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6005 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6006 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6007 			/* Scheduling the callback failed */
6008 			return (TRAN_BUSY);
6009 		}
6010 	}
6011 
6012 	return (TRAN_ACCEPT);
6013 }
6014 
6015 /*
6016  * Translate command: Log Select
6017  * Not implemented at this time - returns invalid command response.
6018  */
6019 static	int
6020 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6021 {
6022 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6023 	    "sata_txlt_log_select\n", NULL);
6024 
6025 	return (sata_txlt_invalid_command(spx));
6026 }
6027 
6028 
6029 /*
6030  * Translate command: Read (various types).
6031  * Translated into appropriate type of ATA READ command
6032  * for SATA hard disks.
6033  * Both the device capabilities and requested operation mode are
6034  * considered.
6035  *
6036  * Following scsi cdb fields are ignored:
6037  * rdprotect, dpo, fua, fua_nv, group_number.
6038  *
6039  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6040  * enable variable sata_func_enable), the capability of the controller and
6041  * capability of a device are checked and if both support queueing, read
6042  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6043  * command rather than plain READ_XXX command.
6044  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6045  * both the controller and device suport such functionality, the read
6046  * request will be translated to READ_FPDMA_QUEUED command.
6047  * In both cases the maximum queue depth is derived as minimum of:
6048  * HBA capability,device capability and sata_max_queue_depth variable setting.
6049  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6050  * used to pass max queue depth value, and the maximum possible queue depth
6051  * is 32.
6052  *
6053  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6054  * appropriate values in scsi_pkt fields.
6055  */
6056 static int
6057 sata_txlt_read(sata_pkt_txlate_t *spx)
6058 {
6059 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6060 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6061 	sata_drive_info_t *sdinfo;
6062 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6063 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6064 	uint16_t sec_count;
6065 	uint64_t lba;
6066 	int rval, reason;
6067 	int synch;
6068 
6069 	mutex_enter(cport_mutex);
6070 
6071 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6072 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6073 		mutex_exit(cport_mutex);
6074 		return (rval);
6075 	}
6076 
6077 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6078 	    &spx->txlt_sata_pkt->satapkt_device);
6079 
6080 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6081 	/*
6082 	 * Extract LBA and sector count from scsi CDB.
6083 	 */
6084 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6085 	case SCMD_READ:
6086 		/* 6-byte scsi read cmd : 0x08 */
6087 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6088 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6089 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6090 		sec_count = scsipkt->pkt_cdbp[4];
6091 		/* sec_count 0 will be interpreted as 256 by a device */
6092 		break;
6093 	case SCMD_READ_G1:
6094 		/* 10-bytes scsi read command : 0x28 */
6095 		lba = scsipkt->pkt_cdbp[2];
6096 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6097 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6098 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6099 		sec_count = scsipkt->pkt_cdbp[7];
6100 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6101 		break;
6102 	case SCMD_READ_G5:
6103 		/* 12-bytes scsi read command : 0xA8 */
6104 		lba = scsipkt->pkt_cdbp[2];
6105 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6106 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6107 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6108 		sec_count = scsipkt->pkt_cdbp[6];
6109 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6110 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6111 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6112 		break;
6113 	case SCMD_READ_G4:
6114 		/* 16-bytes scsi read command : 0x88 */
6115 		lba = scsipkt->pkt_cdbp[2];
6116 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6117 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6118 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6119 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6120 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6121 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6122 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6123 		sec_count = scsipkt->pkt_cdbp[10];
6124 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6125 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6126 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6127 		break;
6128 	default:
6129 		/* Unsupported command */
6130 		mutex_exit(cport_mutex);
6131 		return (sata_txlt_invalid_command(spx));
6132 	}
6133 
6134 	/*
6135 	 * Check if specified address exceeds device capacity
6136 	 */
6137 	if ((lba >= sdinfo->satadrv_capacity) ||
6138 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6139 		/* LBA out of range */
6140 		mutex_exit(cport_mutex);
6141 		return (sata_txlt_lba_out_of_range(spx));
6142 	}
6143 
6144 	/*
6145 	 * For zero-length transfer, emulate good completion of the command
6146 	 * (reasons for rejecting the command were already checked).
6147 	 * No DMA resources were allocated.
6148 	 */
6149 	if (spx->txlt_dma_cookie_list == NULL) {
6150 		mutex_exit(cport_mutex);
6151 		return (sata_emul_rw_completion(spx));
6152 	}
6153 
6154 	/*
6155 	 * Build cmd block depending on the device capability and
6156 	 * requested operation mode.
6157 	 * Do not bother with non-dma mode - we are working only with
6158 	 * devices supporting DMA.
6159 	 */
6160 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6161 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6162 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6163 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6164 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6165 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6166 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6167 #ifndef __lock_lint
6168 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6169 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6170 		scmd->satacmd_lba_high_msb = lba >> 40;
6171 #endif
6172 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6173 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6174 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6175 	}
6176 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6177 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6178 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6179 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6180 	scmd->satacmd_features_reg = 0;
6181 	scmd->satacmd_status_reg = 0;
6182 	scmd->satacmd_error_reg = 0;
6183 
6184 	/*
6185 	 * Check if queueing commands should be used and switch
6186 	 * to appropriate command if possible
6187 	 */
6188 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6189 		boolean_t using_queuing;
6190 
6191 		/* Queuing supported by controller and device? */
6192 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6193 		    (sdinfo->satadrv_features_support &
6194 		    SATA_DEV_F_NCQ) &&
6195 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6196 		    SATA_CTLF_NCQ)) {
6197 			using_queuing = B_TRUE;
6198 
6199 			/* NCQ supported - use FPDMA READ */
6200 			scmd->satacmd_cmd_reg =
6201 			    SATAC_READ_FPDMA_QUEUED;
6202 			scmd->satacmd_features_reg_ext =
6203 			    scmd->satacmd_sec_count_msb;
6204 			scmd->satacmd_sec_count_msb = 0;
6205 		} else if ((sdinfo->satadrv_features_support &
6206 		    SATA_DEV_F_TCQ) &&
6207 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6208 		    SATA_CTLF_QCMD)) {
6209 			using_queuing = B_TRUE;
6210 
6211 			/* Legacy queueing */
6212 			if (sdinfo->satadrv_features_support &
6213 			    SATA_DEV_F_LBA48) {
6214 				scmd->satacmd_cmd_reg =
6215 				    SATAC_READ_DMA_QUEUED_EXT;
6216 				scmd->satacmd_features_reg_ext =
6217 				    scmd->satacmd_sec_count_msb;
6218 				scmd->satacmd_sec_count_msb = 0;
6219 			} else {
6220 				scmd->satacmd_cmd_reg =
6221 				    SATAC_READ_DMA_QUEUED;
6222 			}
6223 		} else	/* NCQ nor legacy queuing not supported */
6224 			using_queuing = B_FALSE;
6225 
6226 		/*
6227 		 * If queuing, the sector count goes in the features register
6228 		 * and the secount count will contain the tag.
6229 		 */
6230 		if (using_queuing) {
6231 			scmd->satacmd_features_reg =
6232 			    scmd->satacmd_sec_count_lsb;
6233 			scmd->satacmd_sec_count_lsb = 0;
6234 			scmd->satacmd_flags.sata_queued = B_TRUE;
6235 
6236 			/* Set-up maximum queue depth */
6237 			scmd->satacmd_flags.sata_max_queue_depth =
6238 			    sdinfo->satadrv_max_queue_depth - 1;
6239 		} else if (sdinfo->satadrv_features_enabled &
6240 		    SATA_DEV_F_E_UNTAGGED_QING) {
6241 			/*
6242 			 * Although NCQ/TCQ is not enabled, untagged queuing
6243 			 * may be still used.
6244 			 * Set-up the maximum untagged queue depth.
6245 			 * Use controller's queue depth from sata_hba_tran.
6246 			 * SATA HBA drivers may ignore this value and rely on
6247 			 * the internal limits.For drivers that do not
6248 			 * ignore untaged queue depth, limit the value to
6249 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6250 			 * largest value that can be passed via
6251 			 * satacmd_flags.sata_max_queue_depth.
6252 			 */
6253 			scmd->satacmd_flags.sata_max_queue_depth =
6254 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6255 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6256 
6257 		} else {
6258 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6259 		}
6260 	} else
6261 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6262 
6263 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6264 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6265 	    scmd->satacmd_cmd_reg, lba, sec_count);
6266 
6267 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6268 		/* Need callback function */
6269 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6270 		synch = FALSE;
6271 	} else
6272 		synch = TRUE;
6273 
6274 	/* Transfer command to HBA */
6275 	if (sata_hba_start(spx, &rval) != 0) {
6276 		/* Pkt not accepted for execution */
6277 		mutex_exit(cport_mutex);
6278 		return (rval);
6279 	}
6280 	mutex_exit(cport_mutex);
6281 	/*
6282 	 * If execution is non-synchronous,
6283 	 * a callback function will handle potential errors, translate
6284 	 * the response and will do a callback to a target driver.
6285 	 * If it was synchronous, check execution status using the same
6286 	 * framework callback.
6287 	 */
6288 	if (synch) {
6289 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6290 		    "synchronous execution status %x\n",
6291 		    spx->txlt_sata_pkt->satapkt_reason);
6292 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6293 	}
6294 	return (TRAN_ACCEPT);
6295 }
6296 
6297 
6298 /*
6299  * SATA translate command: Write (various types)
6300  * Translated into appropriate type of ATA WRITE command
6301  * for SATA hard disks.
6302  * Both the device capabilities and requested operation mode are
6303  * considered.
6304  *
6305  * Following scsi cdb fields are ignored:
6306  * rwprotect, dpo, fua, fua_nv, group_number.
6307  *
6308  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6309  * enable variable sata_func_enable), the capability of the controller and
6310  * capability of a device are checked and if both support queueing, write
6311  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6312  * command rather than plain WRITE_XXX command.
6313  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6314  * both the controller and device suport such functionality, the write
6315  * request will be translated to WRITE_FPDMA_QUEUED command.
6316  * In both cases the maximum queue depth is derived as minimum of:
6317  * HBA capability,device capability and sata_max_queue_depth variable setting.
6318  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6319  * used to pass max queue depth value, and the maximum possible queue depth
6320  * is 32.
6321  *
6322  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6323  * appropriate values in scsi_pkt fields.
6324  */
6325 static int
6326 sata_txlt_write(sata_pkt_txlate_t *spx)
6327 {
6328 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6329 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6330 	sata_drive_info_t *sdinfo;
6331 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6332 	uint16_t sec_count;
6333 	uint64_t lba;
6334 	int rval, reason;
6335 	int synch;
6336 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6337 
6338 	mutex_enter(cport_mutex);
6339 
6340 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6341 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6342 		mutex_exit(cport_mutex);
6343 		return (rval);
6344 	}
6345 
6346 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6347 	    &spx->txlt_sata_pkt->satapkt_device);
6348 
6349 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6350 	/*
6351 	 * Extract LBA and sector count from scsi CDB
6352 	 */
6353 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6354 	case SCMD_WRITE:
6355 		/* 6-byte scsi read cmd : 0x0A */
6356 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6357 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6358 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6359 		sec_count = scsipkt->pkt_cdbp[4];
6360 		/* sec_count 0 will be interpreted as 256 by a device */
6361 		break;
6362 	case SCMD_WRITE_G1:
6363 		/* 10-bytes scsi write command : 0x2A */
6364 		lba = scsipkt->pkt_cdbp[2];
6365 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6366 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6367 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6368 		sec_count = scsipkt->pkt_cdbp[7];
6369 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6370 		break;
6371 	case SCMD_WRITE_G5:
6372 		/* 12-bytes scsi read command : 0xAA */
6373 		lba = scsipkt->pkt_cdbp[2];
6374 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6375 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6376 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6377 		sec_count = scsipkt->pkt_cdbp[6];
6378 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6379 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6380 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6381 		break;
6382 	case SCMD_WRITE_G4:
6383 		/* 16-bytes scsi write command : 0x8A */
6384 		lba = scsipkt->pkt_cdbp[2];
6385 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6386 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6387 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6388 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6389 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6390 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6391 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6392 		sec_count = scsipkt->pkt_cdbp[10];
6393 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6394 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6395 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6396 		break;
6397 	default:
6398 		/* Unsupported command */
6399 		mutex_exit(cport_mutex);
6400 		return (sata_txlt_invalid_command(spx));
6401 	}
6402 
6403 	/*
6404 	 * Check if specified address and length exceeds device capacity
6405 	 */
6406 	if ((lba >= sdinfo->satadrv_capacity) ||
6407 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6408 		/* LBA out of range */
6409 		mutex_exit(cport_mutex);
6410 		return (sata_txlt_lba_out_of_range(spx));
6411 	}
6412 
6413 	/*
6414 	 * For zero-length transfer, emulate good completion of the command
6415 	 * (reasons for rejecting the command were already checked).
6416 	 * No DMA resources were allocated.
6417 	 */
6418 	if (spx->txlt_dma_cookie_list == NULL) {
6419 		mutex_exit(cport_mutex);
6420 		return (sata_emul_rw_completion(spx));
6421 	}
6422 
6423 	/*
6424 	 * Build cmd block depending on the device capability and
6425 	 * requested operation mode.
6426 	 * Do not bother with non-dma mode- we are working only with
6427 	 * devices supporting DMA.
6428 	 */
6429 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6430 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6431 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6432 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6433 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6434 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6435 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6436 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6437 #ifndef __lock_lint
6438 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6439 		scmd->satacmd_lba_high_msb = lba >> 40;
6440 #endif
6441 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6442 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6443 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6444 	}
6445 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6446 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6447 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6448 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6449 	scmd->satacmd_features_reg = 0;
6450 	scmd->satacmd_status_reg = 0;
6451 	scmd->satacmd_error_reg = 0;
6452 
6453 	/*
6454 	 * Check if queueing commands should be used and switch
6455 	 * to appropriate command if possible
6456 	 */
6457 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6458 		boolean_t using_queuing;
6459 
6460 		/* Queuing supported by controller and device? */
6461 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6462 		    (sdinfo->satadrv_features_support &
6463 		    SATA_DEV_F_NCQ) &&
6464 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6465 		    SATA_CTLF_NCQ)) {
6466 			using_queuing = B_TRUE;
6467 
6468 			/* NCQ supported - use FPDMA WRITE */
6469 			scmd->satacmd_cmd_reg =
6470 			    SATAC_WRITE_FPDMA_QUEUED;
6471 			scmd->satacmd_features_reg_ext =
6472 			    scmd->satacmd_sec_count_msb;
6473 			scmd->satacmd_sec_count_msb = 0;
6474 		} else if ((sdinfo->satadrv_features_support &
6475 		    SATA_DEV_F_TCQ) &&
6476 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6477 		    SATA_CTLF_QCMD)) {
6478 			using_queuing = B_TRUE;
6479 
6480 			/* Legacy queueing */
6481 			if (sdinfo->satadrv_features_support &
6482 			    SATA_DEV_F_LBA48) {
6483 				scmd->satacmd_cmd_reg =
6484 				    SATAC_WRITE_DMA_QUEUED_EXT;
6485 				scmd->satacmd_features_reg_ext =
6486 				    scmd->satacmd_sec_count_msb;
6487 				scmd->satacmd_sec_count_msb = 0;
6488 			} else {
6489 				scmd->satacmd_cmd_reg =
6490 				    SATAC_WRITE_DMA_QUEUED;
6491 			}
6492 		} else	/*  NCQ nor legacy queuing not supported */
6493 			using_queuing = B_FALSE;
6494 
6495 		if (using_queuing) {
6496 			scmd->satacmd_features_reg =
6497 			    scmd->satacmd_sec_count_lsb;
6498 			scmd->satacmd_sec_count_lsb = 0;
6499 			scmd->satacmd_flags.sata_queued = B_TRUE;
6500 			/* Set-up maximum queue depth */
6501 			scmd->satacmd_flags.sata_max_queue_depth =
6502 			    sdinfo->satadrv_max_queue_depth - 1;
6503 		} else if (sdinfo->satadrv_features_enabled &
6504 		    SATA_DEV_F_E_UNTAGGED_QING) {
6505 			/*
6506 			 * Although NCQ/TCQ is not enabled, untagged queuing
6507 			 * may be still used.
6508 			 * Set-up the maximum untagged queue depth.
6509 			 * Use controller's queue depth from sata_hba_tran.
6510 			 * SATA HBA drivers may ignore this value and rely on
6511 			 * the internal limits. For drivera that do not
6512 			 * ignore untaged queue depth, limit the value to
6513 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6514 			 * largest value that can be passed via
6515 			 * satacmd_flags.sata_max_queue_depth.
6516 			 */
6517 			scmd->satacmd_flags.sata_max_queue_depth =
6518 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6519 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6520 
6521 		} else {
6522 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6523 		}
6524 	} else
6525 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6526 
6527 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6528 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6529 	    scmd->satacmd_cmd_reg, lba, sec_count);
6530 
6531 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6532 		/* Need callback function */
6533 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6534 		synch = FALSE;
6535 	} else
6536 		synch = TRUE;
6537 
6538 	/* Transfer command to HBA */
6539 	if (sata_hba_start(spx, &rval) != 0) {
6540 		/* Pkt not accepted for execution */
6541 		mutex_exit(cport_mutex);
6542 		return (rval);
6543 	}
6544 	mutex_exit(cport_mutex);
6545 
6546 	/*
6547 	 * If execution is non-synchronous,
6548 	 * a callback function will handle potential errors, translate
6549 	 * the response and will do a callback to a target driver.
6550 	 * If it was synchronous, check execution status using the same
6551 	 * framework callback.
6552 	 */
6553 	if (synch) {
6554 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6555 		    "synchronous execution status %x\n",
6556 		    spx->txlt_sata_pkt->satapkt_reason);
6557 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6558 	}
6559 	return (TRAN_ACCEPT);
6560 }
6561 
6562 
6563 /*
6564  * Implements SCSI SBC WRITE BUFFER command download microcode option
6565  */
6566 static int
6567 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6568 {
6569 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6570 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6571 
6572 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6573 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6574 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6575 
6576 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6577 	struct scsi_extended_sense *sense;
6578 	int rval, mode, sector_count, reason;
6579 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6580 
6581 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6582 
6583 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6584 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6585 
6586 	mutex_enter(cport_mutex);
6587 
6588 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6589 	    TRAN_ACCEPT) {
6590 		mutex_exit(cport_mutex);
6591 		return (rval);
6592 	}
6593 
6594 	/* Use synchronous mode */
6595 	spx->txlt_sata_pkt->satapkt_op_mode
6596 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6597 
6598 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6599 
6600 	scsipkt->pkt_reason = CMD_CMPLT;
6601 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6602 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6603 
6604 	/*
6605 	 * The SCSI to ATA translation specification only calls
6606 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6607 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6608 	 * ATA 8 (draft) got rid of download microcode for temp
6609 	 * and it is even optional for ATA 7, so it may be aborted.
6610 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6611 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6612 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6613 	 * sectors.  Thus the offset really doesn't buy us anything.
6614 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6615 	 * is revised, this can be revisisted.
6616 	 */
6617 	/* Reject not supported request */
6618 	switch (mode) {
6619 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6620 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6621 		break;
6622 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6623 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6624 		break;
6625 	default:
6626 		goto bad_param;
6627 	}
6628 
6629 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6630 
6631 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6632 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6633 		goto bad_param;
6634 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6635 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6636 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6637 	scmd->satacmd_lba_mid_lsb = 0;
6638 	scmd->satacmd_lba_high_lsb = 0;
6639 	scmd->satacmd_device_reg = 0;
6640 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6641 	scmd->satacmd_addr_type = 0;
6642 
6643 	/* Transfer command to HBA */
6644 	if (sata_hba_start(spx, &rval) != 0) {
6645 		/* Pkt not accepted for execution */
6646 		mutex_exit(cport_mutex);
6647 		return (rval);
6648 	}
6649 
6650 	mutex_exit(cport_mutex);
6651 
6652 	/* Then we need synchronous check the status of the disk */
6653 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6654 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6655 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6656 		scsipkt->pkt_reason = CMD_CMPLT;
6657 
6658 		/* Download commmand succeed, so probe and identify device */
6659 		sata_reidentify_device(spx);
6660 	} else {
6661 		/* Something went wrong, microcode download command failed */
6662 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6663 		*scsipkt->pkt_scbp = STATUS_CHECK;
6664 		sense = sata_arq_sense(spx);
6665 		switch (sata_pkt->satapkt_reason) {
6666 		case SATA_PKT_PORT_ERROR:
6667 			/*
6668 			 * We have no device data. Assume no data transfered.
6669 			 */
6670 			sense->es_key = KEY_HARDWARE_ERROR;
6671 			break;
6672 
6673 		case SATA_PKT_DEV_ERROR:
6674 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6675 			    SATA_STATUS_ERR) {
6676 				/*
6677 				 * determine dev error reason from error
6678 				 * reg content
6679 				 */
6680 				sata_decode_device_error(spx, sense);
6681 				break;
6682 			}
6683 			/* No extended sense key - no info available */
6684 			break;
6685 
6686 		case SATA_PKT_TIMEOUT:
6687 			scsipkt->pkt_reason = CMD_TIMEOUT;
6688 			scsipkt->pkt_statistics |=
6689 			    STAT_TIMEOUT | STAT_DEV_RESET;
6690 			/* No extended sense key ? */
6691 			break;
6692 
6693 		case SATA_PKT_ABORTED:
6694 			scsipkt->pkt_reason = CMD_ABORTED;
6695 			scsipkt->pkt_statistics |= STAT_ABORTED;
6696 			/* No extended sense key ? */
6697 			break;
6698 
6699 		case SATA_PKT_RESET:
6700 			/* pkt aborted by an explicit reset from a host */
6701 			scsipkt->pkt_reason = CMD_RESET;
6702 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6703 			break;
6704 
6705 		default:
6706 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6707 			    "sata_txlt_nodata_cmd_completion: "
6708 			    "invalid packet completion reason %d",
6709 			    sata_pkt->satapkt_reason));
6710 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6711 			break;
6712 		}
6713 
6714 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6715 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6716 
6717 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6718 			/* scsi callback required */
6719 			scsi_hba_pkt_comp(scsipkt);
6720 	}
6721 	return (TRAN_ACCEPT);
6722 
6723 bad_param:
6724 	mutex_exit(cport_mutex);
6725 	*scsipkt->pkt_scbp = STATUS_CHECK;
6726 	sense = sata_arq_sense(spx);
6727 	sense->es_key = KEY_ILLEGAL_REQUEST;
6728 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6729 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6730 	    scsipkt->pkt_comp != NULL) {
6731 		/* scsi callback required */
6732 		if (servicing_interrupt()) {
6733 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6734 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6735 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6736 				return (TRAN_BUSY);
6737 			}
6738 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6739 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6740 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6741 			/* Scheduling the callback failed */
6742 			return (TRAN_BUSY);
6743 		}
6744 	}
6745 	return (rval);
6746 }
6747 
6748 /*
6749  * Re-identify device after doing a firmware download.
6750  */
6751 static void
6752 sata_reidentify_device(sata_pkt_txlate_t *spx)
6753 {
6754 #define	DOWNLOAD_WAIT_TIME_SECS	60
6755 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6756 	int rval;
6757 	int retry_cnt;
6758 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6759 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6760 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6761 	sata_drive_info_t *sdinfo;
6762 
6763 	/*
6764 	 * Before returning good status, probe device.
6765 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6766 	 * The assumption is that the new microcode is applied by the
6767 	 * device. It is a caller responsibility to verify this.
6768 	 */
6769 	for (retry_cnt = 0;
6770 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6771 	    retry_cnt++) {
6772 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6773 
6774 		if (rval == SATA_SUCCESS) { /* Set default features */
6775 			sdinfo = sata_get_device_info(sata_hba_inst,
6776 			    &sata_device);
6777 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6778 			    SATA_SUCCESS) {
6779 				/* retry */
6780 				rval = sata_initialize_device(sata_hba_inst,
6781 				    sdinfo);
6782 				if (rval == SATA_RETRY)
6783 					sata_log(sata_hba_inst, CE_WARN,
6784 					    "SATA device at port %d pmport %d -"
6785 					    " default device features could not"
6786 					    " be set. Device may not operate "
6787 					    "as expected.",
6788 					    sata_device.satadev_addr.cport,
6789 					    sata_device.satadev_addr.pmport);
6790 			}
6791 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6792 				scsi_hba_pkt_comp(scsipkt);
6793 			return;
6794 		} else if (rval == SATA_RETRY) {
6795 			delay(drv_usectohz(1000000 *
6796 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6797 			continue;
6798 		} else	/* failed - no reason to retry */
6799 			break;
6800 	}
6801 
6802 	/*
6803 	 * Something went wrong, device probing failed.
6804 	 */
6805 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6806 	    "Cannot probe device after downloading microcode\n"));
6807 
6808 	/* Reset device to force retrying the probe. */
6809 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6810 	    (SATA_DIP(sata_hba_inst), &sata_device);
6811 
6812 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6813 		scsi_hba_pkt_comp(scsipkt);
6814 }
6815 
6816 
6817 /*
6818  * Translate command: Synchronize Cache.
6819  * Translates into Flush Cache command for SATA hard disks.
6820  *
6821  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6822  * appropriate values in scsi_pkt fields.
6823  */
6824 static 	int
6825 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6826 {
6827 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6828 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6829 	int rval, reason;
6830 	int synch;
6831 
6832 	mutex_enter(cport_mutex);
6833 
6834 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6835 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6836 		mutex_exit(cport_mutex);
6837 		return (rval);
6838 	}
6839 
6840 	scmd->satacmd_addr_type = 0;
6841 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6842 	scmd->satacmd_device_reg = 0;
6843 	scmd->satacmd_sec_count_lsb = 0;
6844 	scmd->satacmd_lba_low_lsb = 0;
6845 	scmd->satacmd_lba_mid_lsb = 0;
6846 	scmd->satacmd_lba_high_lsb = 0;
6847 	scmd->satacmd_features_reg = 0;
6848 	scmd->satacmd_status_reg = 0;
6849 	scmd->satacmd_error_reg = 0;
6850 
6851 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6852 	    "sata_txlt_synchronize_cache\n", NULL);
6853 
6854 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6855 		/* Need to set-up a callback function */
6856 		spx->txlt_sata_pkt->satapkt_comp =
6857 		    sata_txlt_nodata_cmd_completion;
6858 		synch = FALSE;
6859 	} else
6860 		synch = TRUE;
6861 
6862 	/* Transfer command to HBA */
6863 	if (sata_hba_start(spx, &rval) != 0) {
6864 		/* Pkt not accepted for execution */
6865 		mutex_exit(cport_mutex);
6866 		return (rval);
6867 	}
6868 	mutex_exit(cport_mutex);
6869 
6870 	/*
6871 	 * If execution non-synchronous, it had to be completed
6872 	 * a callback function will handle potential errors, translate
6873 	 * the response and will do a callback to a target driver.
6874 	 * If it was synchronous, check status, using the same
6875 	 * framework callback.
6876 	 */
6877 	if (synch) {
6878 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6879 		    "synchronous execution status %x\n",
6880 		    spx->txlt_sata_pkt->satapkt_reason);
6881 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6882 	}
6883 	return (TRAN_ACCEPT);
6884 }
6885 
6886 
6887 /*
6888  * Send pkt to SATA HBA driver
6889  *
6890  * This function may be called only if the operation is requested by scsi_pkt,
6891  * i.e. scsi_pkt is not NULL.
6892  *
6893  * This function has to be called with cport mutex held. It does release
6894  * the mutex when it calls HBA driver sata_tran_start function and
6895  * re-acquires it afterwards.
6896  *
6897  * If return value is 0, pkt was accepted, -1 otherwise
6898  * rval is set to appropriate sata_scsi_start return value.
6899  *
6900  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6901  * have called the sata_pkt callback function for this packet.
6902  *
6903  * The scsi callback has to be performed by the caller of this routine.
6904  */
6905 static int
6906 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6907 {
6908 	int stat;
6909 	uint8_t cport = SATA_TXLT_CPORT(spx);
6910 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6911 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6912 	sata_drive_info_t *sdinfo;
6913 	sata_pmult_info_t *pminfo;
6914 	sata_pmport_info_t *pmportinfo = NULL;
6915 	sata_device_t *sata_device = NULL;
6916 	uint8_t cmd;
6917 	struct sata_cmd_flags cmd_flags;
6918 
6919 	ASSERT(spx->txlt_sata_pkt != NULL);
6920 
6921 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6922 
6923 	sdinfo = sata_get_device_info(sata_hba_inst,
6924 	    &spx->txlt_sata_pkt->satapkt_device);
6925 	ASSERT(sdinfo != NULL);
6926 
6927 	/* Clear device reset state? */
6928 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6929 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6930 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6931 
6932 		/*
6933 		 * Get the pmult_info of the its parent port multiplier, all
6934 		 * sub-devices share a common device reset flags on in
6935 		 * pmult_info.
6936 		 */
6937 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6938 		pmportinfo = pminfo->pmult_dev_port[pmport];
6939 		ASSERT(pminfo != NULL);
6940 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6941 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6942 			    sata_clear_dev_reset = B_TRUE;
6943 			pminfo->pmult_event_flags &=
6944 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6945 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6946 			    "sata_hba_start: clearing device reset state"
6947 			    "on pmult.\n", NULL);
6948 		}
6949 	} else {
6950 		if (sdinfo->satadrv_event_flags &
6951 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6952 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6953 			    sata_clear_dev_reset = B_TRUE;
6954 			sdinfo->satadrv_event_flags &=
6955 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6956 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6957 			    "sata_hba_start: clearing device reset state\n",
6958 			    NULL);
6959 		}
6960 	}
6961 
6962 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6963 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6964 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6965 
6966 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6967 
6968 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6969 	    "Sata cmd 0x%2x\n", cmd);
6970 
6971 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6972 	    spx->txlt_sata_pkt);
6973 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6974 	/*
6975 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6976 	 * with the sata callback, the sata_pkt could be already destroyed
6977 	 * by the time we check ther return status from the hba_start()
6978 	 * function, because sata_scsi_destroy_pkt() could have been already
6979 	 * called (perhaps in the interrupt context). So, in such case, there
6980 	 * should be no references to it. In other cases, sata_pkt still
6981 	 * exists.
6982 	 */
6983 	if (stat == SATA_TRAN_ACCEPTED) {
6984 		/*
6985 		 * pkt accepted for execution.
6986 		 * If it was executed synchronously, it is already completed
6987 		 * and pkt completion_reason indicates completion status.
6988 		 */
6989 		*rval = TRAN_ACCEPT;
6990 		return (0);
6991 	}
6992 
6993 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6994 	switch (stat) {
6995 	case SATA_TRAN_QUEUE_FULL:
6996 		/*
6997 		 * Controller detected queue full condition.
6998 		 */
6999 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7000 		    "sata_hba_start: queue full\n", NULL);
7001 
7002 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7003 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7004 
7005 		*rval = TRAN_BUSY;
7006 		break;
7007 
7008 	case SATA_TRAN_PORT_ERROR:
7009 		/*
7010 		 * Communication/link with device or general port error
7011 		 * detected before pkt execution begun.
7012 		 */
7013 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7014 		    SATA_ADDR_CPORT ||
7015 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7016 		    SATA_ADDR_DCPORT)
7017 			sata_log(sata_hba_inst, CE_CONT,
7018 			    "SATA port %d error",
7019 			    sata_device->satadev_addr.cport);
7020 		else
7021 			sata_log(sata_hba_inst, CE_CONT,
7022 			    "SATA port %d:%d error\n",
7023 			    sata_device->satadev_addr.cport,
7024 			    sata_device->satadev_addr.pmport);
7025 
7026 		/*
7027 		 * Update the port/device structure.
7028 		 * sata_pkt should be still valid. Since port error is
7029 		 * returned, sata_device content should reflect port
7030 		 * state - it means, that sata address have been changed,
7031 		 * because original packet's sata address refered to a device
7032 		 * attached to some port.
7033 		 */
7034 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7035 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7036 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7037 			mutex_enter(&pmportinfo->pmport_mutex);
7038 			sata_update_pmport_info(sata_hba_inst, sata_device);
7039 			mutex_exit(&pmportinfo->pmport_mutex);
7040 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7041 		} else {
7042 			sata_update_port_info(sata_hba_inst, sata_device);
7043 		}
7044 
7045 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7046 		*rval = TRAN_FATAL_ERROR;
7047 		break;
7048 
7049 	case SATA_TRAN_CMD_UNSUPPORTED:
7050 		/*
7051 		 * Command rejected by HBA as unsupported. It was HBA driver
7052 		 * that rejected the command, command was not sent to
7053 		 * an attached device.
7054 		 */
7055 		if ((sdinfo != NULL) &&
7056 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7057 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7058 			    "sat_hba_start: cmd 0x%2x rejected "
7059 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7060 
7061 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7062 		(void) sata_txlt_invalid_command(spx);
7063 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7064 
7065 		*rval = TRAN_ACCEPT;
7066 		break;
7067 
7068 	case SATA_TRAN_BUSY:
7069 		/*
7070 		 * Command rejected by HBA because other operation prevents
7071 		 * accepting the packet, or device is in RESET condition.
7072 		 */
7073 		if (sdinfo != NULL) {
7074 			sdinfo->satadrv_state =
7075 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7076 
7077 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7078 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7079 				    "sata_hba_start: cmd 0x%2x rejected "
7080 				    "because of device reset condition\n",
7081 				    cmd);
7082 			} else {
7083 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7084 				    "sata_hba_start: cmd 0x%2x rejected "
7085 				    "with SATA_TRAN_BUSY status\n",
7086 				    cmd);
7087 			}
7088 		}
7089 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7090 		*rval = TRAN_BUSY;
7091 		break;
7092 
7093 	default:
7094 		/* Unrecognized HBA response */
7095 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7096 		    "sata_hba_start: unrecognized HBA response "
7097 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7098 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7099 		*rval = TRAN_FATAL_ERROR;
7100 		break;
7101 	}
7102 
7103 	/*
7104 	 * If we got here, the packet was rejected.
7105 	 * Check if we need to remember reset state clearing request
7106 	 */
7107 	if (cmd_flags.sata_clear_dev_reset) {
7108 		/*
7109 		 * Check if device is still configured - it may have
7110 		 * disapeared from the configuration
7111 		 */
7112 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7113 		if (sdinfo != NULL) {
7114 			/*
7115 			 * Restore the flag that requests clearing of
7116 			 * the device reset state,
7117 			 * so the next sata packet may carry it to HBA.
7118 			 */
7119 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7120 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7121 				pminfo->pmult_event_flags |=
7122 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7123 			} else {
7124 				sdinfo->satadrv_event_flags |=
7125 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7126 			}
7127 		}
7128 	}
7129 	return (-1);
7130 }
7131 
7132 /*
7133  * Scsi response setup for invalid LBA
7134  *
7135  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7136  */
7137 static int
7138 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7139 {
7140 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7141 	struct scsi_extended_sense *sense;
7142 
7143 	scsipkt->pkt_reason = CMD_CMPLT;
7144 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7145 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7146 	*scsipkt->pkt_scbp = STATUS_CHECK;
7147 
7148 	*scsipkt->pkt_scbp = STATUS_CHECK;
7149 	sense = sata_arq_sense(spx);
7150 	sense->es_key = KEY_ILLEGAL_REQUEST;
7151 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7152 
7153 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7154 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7155 
7156 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7157 	    scsipkt->pkt_comp != NULL) {
7158 		/* scsi callback required */
7159 		if (servicing_interrupt()) {
7160 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7161 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7162 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7163 				return (TRAN_BUSY);
7164 			}
7165 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7166 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7167 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7168 			/* Scheduling the callback failed */
7169 			return (TRAN_BUSY);
7170 		}
7171 	}
7172 	return (TRAN_ACCEPT);
7173 }
7174 
7175 
7176 /*
7177  * Analyze device status and error registers and translate them into
7178  * appropriate scsi sense codes.
7179  * NOTE: non-packet commands only for now
7180  */
7181 static void
7182 sata_decode_device_error(sata_pkt_txlate_t *spx,
7183     struct scsi_extended_sense *sense)
7184 {
7185 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7186 
7187 	ASSERT(sense != NULL);
7188 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7189 	    SATA_STATUS_ERR);
7190 
7191 
7192 	if (err_reg & SATA_ERROR_ICRC) {
7193 		sense->es_key = KEY_ABORTED_COMMAND;
7194 		sense->es_add_code = 0x08; /* Communication failure */
7195 		return;
7196 	}
7197 
7198 	if (err_reg & SATA_ERROR_UNC) {
7199 		sense->es_key = KEY_MEDIUM_ERROR;
7200 		/* Information bytes (LBA) need to be set by a caller */
7201 		return;
7202 	}
7203 
7204 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7205 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7206 		sense->es_key = KEY_UNIT_ATTENTION;
7207 		sense->es_add_code = 0x3a; /* No media present */
7208 		return;
7209 	}
7210 
7211 	if (err_reg & SATA_ERROR_IDNF) {
7212 		if (err_reg & SATA_ERROR_ABORT) {
7213 			sense->es_key = KEY_ABORTED_COMMAND;
7214 		} else {
7215 			sense->es_key = KEY_ILLEGAL_REQUEST;
7216 			sense->es_add_code = 0x21; /* LBA out of range */
7217 		}
7218 		return;
7219 	}
7220 
7221 	if (err_reg & SATA_ERROR_ABORT) {
7222 		ASSERT(spx->txlt_sata_pkt != NULL);
7223 		sense->es_key = KEY_ABORTED_COMMAND;
7224 		return;
7225 	}
7226 }
7227 
7228 /*
7229  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7230  */
7231 static void
7232 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7233 {
7234 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7235 
7236 	*lba = 0;
7237 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7238 		*lba = sata_cmd->satacmd_lba_high_msb;
7239 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7240 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7241 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7242 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7243 	}
7244 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7245 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7246 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7247 }
7248 
7249 /*
7250  * This is fixed sense format - if LBA exceeds the info field size,
7251  * no valid info will be returned (valid bit in extended sense will
7252  * be set to 0).
7253  */
7254 static struct scsi_extended_sense *
7255 sata_arq_sense(sata_pkt_txlate_t *spx)
7256 {
7257 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7258 	struct scsi_arq_status *arqs;
7259 	struct scsi_extended_sense *sense;
7260 
7261 	/* Fill ARQ sense data */
7262 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7263 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7264 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7265 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7266 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7267 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7268 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7269 	arqs->sts_rqpkt_resid = 0;
7270 	sense = &arqs->sts_sensedata;
7271 	bzero(sense, sizeof (struct scsi_extended_sense));
7272 	sata_fixed_sense_data_preset(sense);
7273 	return (sense);
7274 }
7275 
7276 /*
7277  * ATA Pass Through support
7278  * Sets flags indicating that an invalid value was found in some
7279  * field in the command.  It could be something illegal according to
7280  * the SAT-2 spec or it could be a feature that is not (yet?)
7281  * supported.
7282  */
7283 static int
7284 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7285 {
7286 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7287 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7288 
7289 	scsipkt->pkt_reason = CMD_CMPLT;
7290 	*scsipkt->pkt_scbp = STATUS_CHECK;
7291 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7292 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7293 
7294 	sense = sata_arq_sense(spx);
7295 	sense->es_key = KEY_ILLEGAL_REQUEST;
7296 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7297 
7298 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7299 	    scsipkt->pkt_comp != NULL) {
7300 		/* scsi callback required */
7301 		if (servicing_interrupt()) {
7302 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7303 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7304 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7305 				return (TRAN_BUSY);
7306 			}
7307 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7308 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7309 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7310 			/* Scheduling the callback failed */
7311 			return (TRAN_BUSY);
7312 		}
7313 	}
7314 
7315 	return (TRAN_ACCEPT);
7316 }
7317 
7318 /*
7319  * The UNMAP command considers it not to be an error if the parameter length
7320  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7321  * to do so just complete the command.
7322  */
7323 static int
7324 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7325 {
7326 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7327 
7328 	scsipkt->pkt_reason = CMD_CMPLT;
7329 	*scsipkt->pkt_scbp = STATUS_GOOD;
7330 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7331 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7332 
7333 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7334 	    scsipkt->pkt_comp != NULL) {
7335 		/* scsi callback required */
7336 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7337 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7338 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7339 			/* Scheduling the callback failed */
7340 			return (TRAN_BUSY);
7341 		}
7342 	}
7343 
7344 	return (TRAN_ACCEPT);
7345 }
7346 
7347 /*
7348  * Emulated SATA Read/Write command completion for zero-length requests.
7349  * This request always succedes, so in synchronous mode it always returns
7350  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7351  * callback cannot be scheduled.
7352  */
7353 static int
7354 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7355 {
7356 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7357 
7358 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7359 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7360 	scsipkt->pkt_reason = CMD_CMPLT;
7361 	*scsipkt->pkt_scbp = STATUS_GOOD;
7362 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7363 		/* scsi callback required - have to schedule it */
7364 		if (servicing_interrupt()) {
7365 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7366 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7367 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7368 				return (TRAN_BUSY);
7369 			}
7370 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7371 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7372 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7373 			/* Scheduling the callback failed */
7374 			return (TRAN_BUSY);
7375 		}
7376 	}
7377 	return (TRAN_ACCEPT);
7378 }
7379 
7380 
7381 /*
7382  * Translate completion status of SATA read/write commands into scsi response.
7383  * pkt completion_reason is checked to determine the completion status.
7384  * Do scsi callback if necessary.
7385  *
7386  * Note: this function may be called also for synchronously executed
7387  * commands.
7388  * This function may be used only if scsi_pkt is non-NULL.
7389  */
7390 static void
7391 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7392 {
7393 	sata_pkt_txlate_t *spx =
7394 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7395 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7396 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7397 	struct scsi_extended_sense *sense;
7398 	uint64_t lba;
7399 	struct buf *bp;
7400 	int rval;
7401 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7402 		/* Normal completion */
7403 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7404 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7405 		scsipkt->pkt_reason = CMD_CMPLT;
7406 		*scsipkt->pkt_scbp = STATUS_GOOD;
7407 		if (spx->txlt_tmp_buf != NULL) {
7408 			/* Temporary buffer was used */
7409 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7410 			if (bp->b_flags & B_READ) {
7411 				rval = ddi_dma_sync(
7412 				    spx->txlt_buf_dma_handle, 0, 0,
7413 				    DDI_DMA_SYNC_FORCPU);
7414 				ASSERT(rval == DDI_SUCCESS);
7415 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7416 				    bp->b_bcount);
7417 			}
7418 		}
7419 	} else {
7420 		/*
7421 		 * Something went wrong - analyze return
7422 		 */
7423 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7424 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7425 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7426 		*scsipkt->pkt_scbp = STATUS_CHECK;
7427 		sense = sata_arq_sense(spx);
7428 		ASSERT(sense != NULL);
7429 
7430 		/*
7431 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7432 		 * extract from device registers the failing LBA.
7433 		 */
7434 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7435 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7436 			    (scmd->satacmd_lba_mid_msb != 0 ||
7437 			    scmd->satacmd_lba_high_msb != 0)) {
7438 				/*
7439 				 * We have problem reporting this cmd LBA
7440 				 * in fixed sense data format, because of
7441 				 * the size of the scsi LBA fields.
7442 				 */
7443 				sense->es_valid = 0;
7444 			} else {
7445 				sata_extract_error_lba(spx, &lba);
7446 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7447 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7448 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7449 				sense->es_info_4 = lba & 0xFF;
7450 			}
7451 		} else {
7452 			/* Invalid extended sense info */
7453 			sense->es_valid = 0;
7454 		}
7455 
7456 		switch (sata_pkt->satapkt_reason) {
7457 		case SATA_PKT_PORT_ERROR:
7458 			/* We may want to handle DEV GONE state as well */
7459 			/*
7460 			 * We have no device data. Assume no data transfered.
7461 			 */
7462 			sense->es_key = KEY_HARDWARE_ERROR;
7463 			break;
7464 
7465 		case SATA_PKT_DEV_ERROR:
7466 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7467 			    SATA_STATUS_ERR) {
7468 				/*
7469 				 * determine dev error reason from error
7470 				 * reg content
7471 				 */
7472 				sata_decode_device_error(spx, sense);
7473 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7474 					switch (scmd->satacmd_cmd_reg) {
7475 					case SATAC_READ_DMA:
7476 					case SATAC_READ_DMA_EXT:
7477 					case SATAC_READ_DMA_QUEUED:
7478 					case SATAC_READ_DMA_QUEUED_EXT:
7479 					case SATAC_READ_FPDMA_QUEUED:
7480 						/* Unrecovered read error */
7481 						sense->es_add_code =
7482 						    SD_SCSI_ASC_UNREC_READ_ERR;
7483 						break;
7484 					case SATAC_WRITE_DMA:
7485 					case SATAC_WRITE_DMA_EXT:
7486 					case SATAC_WRITE_DMA_QUEUED:
7487 					case SATAC_WRITE_DMA_QUEUED_EXT:
7488 					case SATAC_WRITE_FPDMA_QUEUED:
7489 						/* Write error */
7490 						sense->es_add_code =
7491 						    SD_SCSI_ASC_WRITE_ERR;
7492 						break;
7493 					default:
7494 						/* Internal error */
7495 						SATA_LOG_D((
7496 						    spx->txlt_sata_hba_inst,
7497 						    CE_WARN,
7498 						    "sata_txlt_rw_completion :"
7499 						    "internal error - invalid "
7500 						    "command 0x%2x",
7501 						    scmd->satacmd_cmd_reg));
7502 						break;
7503 					}
7504 				}
7505 				break;
7506 			}
7507 			/* No extended sense key - no info available */
7508 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7509 			break;
7510 
7511 		case SATA_PKT_TIMEOUT:
7512 			scsipkt->pkt_reason = CMD_TIMEOUT;
7513 			scsipkt->pkt_statistics |=
7514 			    STAT_TIMEOUT | STAT_DEV_RESET;
7515 			sense->es_key = KEY_ABORTED_COMMAND;
7516 			break;
7517 
7518 		case SATA_PKT_ABORTED:
7519 			scsipkt->pkt_reason = CMD_ABORTED;
7520 			scsipkt->pkt_statistics |= STAT_ABORTED;
7521 			sense->es_key = KEY_ABORTED_COMMAND;
7522 			break;
7523 
7524 		case SATA_PKT_RESET:
7525 			scsipkt->pkt_reason = CMD_RESET;
7526 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7527 			sense->es_key = KEY_ABORTED_COMMAND;
7528 			break;
7529 
7530 		default:
7531 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7532 			    "sata_txlt_rw_completion: "
7533 			    "invalid packet completion reason"));
7534 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7535 			break;
7536 		}
7537 	}
7538 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7539 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7540 
7541 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7542 		/* scsi callback required */
7543 		scsi_hba_pkt_comp(scsipkt);
7544 }
7545 
7546 
7547 /*
7548  * Translate completion status of non-data commands (i.e. commands returning
7549  * no data).
7550  * pkt completion_reason is checked to determine the completion status.
7551  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7552  *
7553  * Note: this function may be called also for synchronously executed
7554  * commands.
7555  * This function may be used only if scsi_pkt is non-NULL.
7556  */
7557 
7558 static	void
7559 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7560 {
7561 	sata_pkt_txlate_t *spx =
7562 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7563 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7564 
7565 	sata_set_arq_data(sata_pkt);
7566 
7567 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7568 		/* scsi callback required */
7569 		scsi_hba_pkt_comp(scsipkt);
7570 }
7571 
7572 /*
7573  * Completion handler for ATA Pass Through command
7574  */
7575 static void
7576 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7577 {
7578 	sata_pkt_txlate_t *spx =
7579 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7580 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7581 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7582 	struct buf *bp;
7583 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7584 
7585 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7586 		/* Normal completion */
7587 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7588 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7589 		scsipkt->pkt_reason = CMD_CMPLT;
7590 		*scsipkt->pkt_scbp = STATUS_GOOD;
7591 
7592 		/*
7593 		 * If the command has CK_COND set
7594 		 */
7595 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7596 			*scsipkt->pkt_scbp = STATUS_CHECK;
7597 			sata_fill_ata_return_desc(sata_pkt,
7598 			    KEY_RECOVERABLE_ERROR,
7599 			    SD_SCSI_ASC_ATP_INFO_AVAIL, 0);
7600 		}
7601 
7602 		if (spx->txlt_tmp_buf != NULL) {
7603 			/* Temporary buffer was used */
7604 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7605 			if (bp->b_flags & B_READ) {
7606 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7607 				    bp->b_bcount);
7608 			}
7609 		}
7610 	} else {
7611 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7612 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7613 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7614 		*scsipkt->pkt_scbp = STATUS_CHECK;
7615 
7616 		/*
7617 		 * If DF or ERR was set, the HBA should have copied out the
7618 		 * status and error registers to the satacmd structure.
7619 		 */
7620 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7621 			sense_key = KEY_HARDWARE_ERROR;
7622 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7623 			addl_sense_qual = 0;
7624 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7625 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7626 				sense_key = KEY_NOT_READY;
7627 				addl_sense_code =
7628 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7629 				addl_sense_qual = 0;
7630 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7631 				sense_key = KEY_MEDIUM_ERROR;
7632 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7633 				addl_sense_qual = 0;
7634 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7635 				sense_key = KEY_DATA_PROTECT;
7636 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7637 				addl_sense_qual = 0;
7638 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7639 				sense_key = KEY_ILLEGAL_REQUEST;
7640 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7641 				addl_sense_qual = 0;
7642 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7643 				sense_key = KEY_ABORTED_COMMAND;
7644 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7645 				addl_sense_qual = 0;
7646 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7647 				sense_key = KEY_UNIT_ATTENTION;
7648 				addl_sense_code =
7649 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7650 				addl_sense_qual = 0;
7651 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7652 				sense_key = KEY_UNIT_ATTENTION;
7653 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7654 				addl_sense_qual = 0;
7655 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7656 				sense_key = KEY_ABORTED_COMMAND;
7657 				addl_sense_code =
7658 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7659 				addl_sense_qual = 0;
7660 			}
7661 		}
7662 
7663 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7664 		    addl_sense_qual);
7665 	}
7666 
7667 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7668 		/* scsi callback required */
7669 		scsi_hba_pkt_comp(scsipkt);
7670 }
7671 
7672 /*
7673  * Completion handler for unmap translation command
7674  */
7675 static void
7676 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7677 {
7678 	sata_pkt_txlate_t *spx =
7679 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7680 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7681 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7682 	struct buf *bp;
7683 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7684 
7685 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7686 		/* Normal completion */
7687 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7688 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7689 		scsipkt->pkt_reason = CMD_CMPLT;
7690 		*scsipkt->pkt_scbp = STATUS_GOOD;
7691 
7692 		if (spx->txlt_tmp_buf != NULL) {
7693 			/* Temporary buffer was used */
7694 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7695 			if (bp->b_flags & B_READ) {
7696 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7697 				    bp->b_bcount);
7698 			}
7699 		}
7700 	} else {
7701 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7702 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7703 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7704 		*scsipkt->pkt_scbp = STATUS_CHECK;
7705 
7706 		/*
7707 		 * If DF or ERR was set, the HBA should have copied out the
7708 		 * status and error registers to the satacmd structure.
7709 		 */
7710 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7711 			sense_key = KEY_HARDWARE_ERROR;
7712 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7713 			addl_sense_qual = 0;
7714 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7715 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7716 				sense_key = KEY_NOT_READY;
7717 				addl_sense_code =
7718 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7719 				addl_sense_qual = 0;
7720 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7721 				sense_key = KEY_MEDIUM_ERROR;
7722 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7723 				addl_sense_qual = 0;
7724 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7725 				sense_key = KEY_DATA_PROTECT;
7726 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7727 				addl_sense_qual = 0;
7728 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7729 				sense_key = KEY_ILLEGAL_REQUEST;
7730 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7731 				addl_sense_qual = 0;
7732 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7733 				sense_key = KEY_ABORTED_COMMAND;
7734 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7735 				addl_sense_qual = 0;
7736 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7737 				sense_key = KEY_UNIT_ATTENTION;
7738 				addl_sense_code =
7739 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7740 				addl_sense_qual = 0;
7741 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7742 				sense_key = KEY_UNIT_ATTENTION;
7743 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7744 				addl_sense_qual = 0;
7745 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7746 				sense_key = KEY_ABORTED_COMMAND;
7747 				addl_sense_code =
7748 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7749 				addl_sense_qual = 0;
7750 			}
7751 		}
7752 
7753 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7754 		    addl_sense_qual);
7755 	}
7756 
7757 	sata_free_local_buffer(spx);
7758 
7759 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7760 		/* scsi callback required */
7761 		scsi_hba_pkt_comp(scsipkt);
7762 }
7763 
7764 /*
7765  *
7766  */
7767 static void
7768 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7769     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7770 {
7771 	sata_pkt_txlate_t *spx =
7772 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7773 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7774 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7775 	struct sata_apt_sense_data *apt_sd =
7776 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7777 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7778 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7779 	    &(apt_sd->apt_sd_sense);
7780 	int extend = 0;
7781 
7782 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7783 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7784 		extend = 1;
7785 
7786 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7787 
7788 	/* update the residual count */
7789 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7790 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7791 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7792 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7793 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7794 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7795 	    sizeof (struct sata_apt_sense_data);
7796 
7797 	/*
7798 	 * Fill in the Descriptor sense header
7799 	 */
7800 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7801 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7802 	sds->ds_class = CLASS_EXTENDED_SENSE;
7803 	sds->ds_key = sense_key & 0xf;
7804 	sds->ds_add_code = addl_sense_code;
7805 	sds->ds_qual_code = addl_sense_qual;
7806 	sds->ds_addl_sense_length =
7807 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7808 
7809 	/*
7810 	 * Fill in the ATA Return descriptor sense data
7811 	 */
7812 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7813 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7814 	ata_ret_desc->ars_addl_length = 0xc;
7815 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7816 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7817 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7818 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7819 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7820 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7821 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7822 
7823 	if (extend == 1) {
7824 		ata_ret_desc->ars_extend = 1;
7825 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7826 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7827 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7828 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7829 	} else {
7830 		ata_ret_desc->ars_extend = 0;
7831 		ata_ret_desc->ars_sec_count_msb = 0;
7832 		ata_ret_desc->ars_lba_low_msb = 0;
7833 		ata_ret_desc->ars_lba_mid_msb = 0;
7834 		ata_ret_desc->ars_lba_high_msb = 0;
7835 	}
7836 }
7837 
7838 static	void
7839 sata_set_arq_data(sata_pkt_t *sata_pkt)
7840 {
7841 	sata_pkt_txlate_t *spx =
7842 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7843 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7844 	struct scsi_extended_sense *sense;
7845 
7846 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7847 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7848 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7849 		/* Normal completion */
7850 		scsipkt->pkt_reason = CMD_CMPLT;
7851 		*scsipkt->pkt_scbp = STATUS_GOOD;
7852 	} else {
7853 		/* Something went wrong */
7854 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7855 		*scsipkt->pkt_scbp = STATUS_CHECK;
7856 		sense = sata_arq_sense(spx);
7857 		switch (sata_pkt->satapkt_reason) {
7858 		case SATA_PKT_PORT_ERROR:
7859 			/*
7860 			 * We have no device data. Assume no data transfered.
7861 			 */
7862 			sense->es_key = KEY_HARDWARE_ERROR;
7863 			break;
7864 
7865 		case SATA_PKT_DEV_ERROR:
7866 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7867 			    SATA_STATUS_ERR) {
7868 				/*
7869 				 * determine dev error reason from error
7870 				 * reg content
7871 				 */
7872 				sata_decode_device_error(spx, sense);
7873 				break;
7874 			}
7875 			/* No extended sense key - no info available */
7876 			break;
7877 
7878 		case SATA_PKT_TIMEOUT:
7879 			scsipkt->pkt_reason = CMD_TIMEOUT;
7880 			scsipkt->pkt_statistics |=
7881 			    STAT_TIMEOUT | STAT_DEV_RESET;
7882 			/* No extended sense key ? */
7883 			break;
7884 
7885 		case SATA_PKT_ABORTED:
7886 			scsipkt->pkt_reason = CMD_ABORTED;
7887 			scsipkt->pkt_statistics |= STAT_ABORTED;
7888 			/* No extended sense key ? */
7889 			break;
7890 
7891 		case SATA_PKT_RESET:
7892 			/* pkt aborted by an explicit reset from a host */
7893 			scsipkt->pkt_reason = CMD_RESET;
7894 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7895 			break;
7896 
7897 		default:
7898 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7899 			    "sata_txlt_nodata_cmd_completion: "
7900 			    "invalid packet completion reason %d",
7901 			    sata_pkt->satapkt_reason));
7902 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7903 			break;
7904 		}
7905 
7906 	}
7907 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7908 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7909 }
7910 
7911 
7912 /*
7913  * Build Mode sense R/W recovery page
7914  * NOT IMPLEMENTED
7915  */
7916 
7917 static int
7918 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7919 {
7920 #ifndef __lock_lint
7921 	_NOTE(ARGUNUSED(sdinfo))
7922 	_NOTE(ARGUNUSED(pcntrl))
7923 	_NOTE(ARGUNUSED(buf))
7924 #endif
7925 	return (0);
7926 }
7927 
7928 /*
7929  * Build Mode sense caching page  -  scsi-3 implementation.
7930  * Page length distinguishes previous format from scsi-3 format.
7931  * buf must have space for 0x12 bytes.
7932  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7933  *
7934  */
7935 static int
7936 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7937 {
7938 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7939 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7940 
7941 	/*
7942 	 * Most of the fields are set to 0, being not supported and/or disabled
7943 	 */
7944 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7945 
7946 	/* Saved paramters not supported */
7947 	if (pcntrl == 3)
7948 		return (0);
7949 	if (pcntrl == 0 || pcntrl == 2) {
7950 		/*
7951 		 * For now treat current and default parameters as same
7952 		 * That may have to change, if target driver will complain
7953 		 */
7954 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
7955 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7956 
7957 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7958 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
7959 			page->dra = 1;		/* Read Ahead disabled */
7960 			page->rcd = 1;		/* Read Cache disabled */
7961 		}
7962 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
7963 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
7964 			page->wce = 1;		/* Write Cache enabled */
7965 	} else {
7966 		/* Changeable parameters */
7967 		page->mode_page.code = MODEPAGE_CACHING;
7968 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7969 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
7970 			page->dra = 1;
7971 			page->rcd = 1;
7972 		}
7973 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
7974 			page->wce = 1;
7975 	}
7976 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7977 	    sizeof (struct mode_page));
7978 }
7979 
7980 /*
7981  * Build Mode sense exception cntrl page
7982  */
7983 static int
7984 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7985 {
7986 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
7987 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7988 
7989 	/*
7990 	 * Most of the fields are set to 0, being not supported and/or disabled
7991 	 */
7992 	bzero(buf, PAGELENGTH_INFO_EXCPT);
7993 
7994 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
7995 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
7996 
7997 	/* Indicate that this is page is saveable */
7998 	page->mode_page.ps = 1;
7999 
8000 	/*
8001 	 * We will return the same data for default, current and saved page.
8002 	 * The only changeable bit is dexcpt and that bit is required
8003 	 * by the ATA specification to be preserved across power cycles.
8004 	 */
8005 	if (pcntrl != 1) {
8006 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8007 		page->mrie = MRIE_ONLY_ON_REQUEST;
8008 	}
8009 	else
8010 		page->dexcpt = 1;	/* Only changeable parameter */
8011 
8012 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8013 }
8014 
8015 
8016 static int
8017 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8018 {
8019 	struct mode_acoustic_management *page =
8020 	    (struct mode_acoustic_management *)buf;
8021 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8022 
8023 	/*
8024 	 * Most of the fields are set to 0, being not supported and/or disabled
8025 	 */
8026 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8027 
8028 	switch (pcntrl) {
8029 	case P_CNTRL_DEFAULT:
8030 		/*  default paramters not supported */
8031 		return (0);
8032 
8033 	case P_CNTRL_CURRENT:
8034 	case P_CNTRL_SAVED:
8035 		/* Saved and current are supported and are identical */
8036 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8037 		page->mode_page.length =
8038 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8039 		page->mode_page.ps = 1;
8040 
8041 		/* Word 83 indicates if feature is supported */
8042 		/* If feature is not supported */
8043 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8044 			page->acoustic_manag_enable =
8045 			    ACOUSTIC_DISABLED;
8046 		} else {
8047 			page->acoustic_manag_enable =
8048 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8049 			    != 0);
8050 			/* Word 94 inidicates the value */
8051 #ifdef	_LITTLE_ENDIAN
8052 			page->acoustic_manag_level =
8053 			    (uchar_t)sata_id->ai_acoustic;
8054 			page->vendor_recommended_value =
8055 			    sata_id->ai_acoustic >> 8;
8056 #else
8057 			page->acoustic_manag_level =
8058 			    sata_id->ai_acoustic >> 8;
8059 			page->vendor_recommended_value =
8060 			    (uchar_t)sata_id->ai_acoustic;
8061 #endif
8062 		}
8063 		break;
8064 
8065 	case P_CNTRL_CHANGEABLE:
8066 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8067 		page->mode_page.length =
8068 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8069 		page->mode_page.ps = 1;
8070 
8071 		/* Word 83 indicates if the feature is supported */
8072 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8073 			page->acoustic_manag_enable =
8074 			    ACOUSTIC_ENABLED;
8075 			page->acoustic_manag_level = 0xff;
8076 		}
8077 		break;
8078 	}
8079 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8080 	    sizeof (struct mode_page));
8081 }
8082 
8083 
8084 /*
8085  * Build Mode sense power condition page.
8086  */
8087 static int
8088 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8089 {
8090 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8091 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8092 
8093 	/*
8094 	 * Most of the fields are set to 0, being not supported and/or disabled
8095 	 * power condition page length was 0x0a
8096 	 */
8097 	bzero(buf, sizeof (struct mode_info_power_cond));
8098 
8099 	if (pcntrl == P_CNTRL_DEFAULT) {
8100 		/*  default paramters not supported */
8101 		return (0);
8102 	}
8103 
8104 	page->mode_page.code = MODEPAGE_POWER_COND;
8105 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8106 
8107 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8108 		page->standby = 1;
8109 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8110 		    sizeof (uchar_t) * 4);
8111 	}
8112 
8113 	return (sizeof (struct mode_info_power_cond));
8114 }
8115 
8116 /*
8117  * Process mode select caching page 8 (scsi3 format only).
8118  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8119  * if these features are supported by the device. If these features are not
8120  * supported, the command will be terminated with STATUS_CHECK.
8121  * This function fails only if the SET FEATURE command sent to
8122  * the device fails. The page format is not verified, assuming that the
8123  * target driver operates correctly - if parameters length is too short,
8124  * we just drop the page.
8125  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8126  * setting have to be changed.
8127  * SET FEATURE command is executed synchronously, i.e. we wait here until
8128  * it is completed, regardless of the scsi pkt directives.
8129  *
8130  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8131  * changing DRA will change RCD.
8132  *
8133  * More than one SATA command may be executed to perform operations specified
8134  * by mode select pages. The first error terminates further execution.
8135  * Operations performed successully are not backed-up in such case.
8136  *
8137  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8138  * If operation resulted in changing device setup, dmod flag should be set to
8139  * one (1). If parameters were not changed, dmod flag should be set to 0.
8140  * Upon return, if operation required sending command to the device, the rval
8141  * should be set to the value returned by sata_hba_start. If operation
8142  * did not require device access, rval should be set to TRAN_ACCEPT.
8143  * The pagelen should be set to the length of the page.
8144  *
8145  * This function has to be called with a port mutex held.
8146  *
8147  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8148  */
8149 int
8150 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8151     int parmlen, int *pagelen, int *rval, int *dmod)
8152 {
8153 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8154 	sata_drive_info_t *sdinfo;
8155 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8156 	sata_id_t *sata_id;
8157 	struct scsi_extended_sense *sense;
8158 	int wce, dra;	/* Current settings */
8159 
8160 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8161 	    &spx->txlt_sata_pkt->satapkt_device);
8162 	sata_id = &sdinfo->satadrv_id;
8163 	*dmod = 0;
8164 
8165 	/* Verify parameters length. If too short, drop it */
8166 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8167 	    sizeof (struct mode_page)) > parmlen) {
8168 		*scsipkt->pkt_scbp = STATUS_CHECK;
8169 		sense = sata_arq_sense(spx);
8170 		sense->es_key = KEY_ILLEGAL_REQUEST;
8171 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8172 		*pagelen = parmlen;
8173 		*rval = TRAN_ACCEPT;
8174 		return (SATA_FAILURE);
8175 	}
8176 
8177 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8178 
8179 	/* Current setting of Read Ahead (and Read Cache) */
8180 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8181 		dra = 0;	/* 0 == not disabled */
8182 	else
8183 		dra = 1;
8184 	/* Current setting of Write Cache */
8185 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8186 		wce = 1;
8187 	else
8188 		wce = 0;
8189 
8190 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8191 		/* nothing to do */
8192 		*rval = TRAN_ACCEPT;
8193 		return (SATA_SUCCESS);
8194 	}
8195 
8196 	/*
8197 	 * Need to flip some setting
8198 	 * Set-up Internal SET FEATURES command(s)
8199 	 */
8200 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8201 	scmd->satacmd_addr_type = 0;
8202 	scmd->satacmd_device_reg = 0;
8203 	scmd->satacmd_status_reg = 0;
8204 	scmd->satacmd_error_reg = 0;
8205 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8206 	if (page->dra != dra || page->rcd != dra) {
8207 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8208 			/* Need to flip read ahead setting */
8209 			if (dra == 0)
8210 				/* Disable read ahead / read cache */
8211 				scmd->satacmd_features_reg =
8212 				    SATAC_SF_DISABLE_READ_AHEAD;
8213 			else
8214 				/* Enable read ahead  / read cache */
8215 				scmd->satacmd_features_reg =
8216 				    SATAC_SF_ENABLE_READ_AHEAD;
8217 
8218 			/* Transfer command to HBA */
8219 			if (sata_hba_start(spx, rval) != 0)
8220 				/*
8221 				 * Pkt not accepted for execution.
8222 				 */
8223 				return (SATA_FAILURE);
8224 
8225 			*dmod = 1;
8226 
8227 			/* Now process return */
8228 			if (spx->txlt_sata_pkt->satapkt_reason !=
8229 			    SATA_PKT_COMPLETED) {
8230 				goto failure;	/* Terminate */
8231 			}
8232 		} else {
8233 			*scsipkt->pkt_scbp = STATUS_CHECK;
8234 			sense = sata_arq_sense(spx);
8235 			sense->es_key = KEY_ILLEGAL_REQUEST;
8236 			sense->es_add_code =
8237 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8238 			*pagelen = parmlen;
8239 			*rval = TRAN_ACCEPT;
8240 			return (SATA_FAILURE);
8241 		}
8242 	}
8243 
8244 	/* Note that the packet is not removed, so it could be re-used */
8245 	if (page->wce != wce) {
8246 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8247 			/* Need to flip Write Cache setting */
8248 			if (page->wce == 1)
8249 				/* Enable write cache */
8250 				scmd->satacmd_features_reg =
8251 				    SATAC_SF_ENABLE_WRITE_CACHE;
8252 			else
8253 				/* Disable write cache */
8254 				scmd->satacmd_features_reg =
8255 				    SATAC_SF_DISABLE_WRITE_CACHE;
8256 
8257 			/* Transfer command to HBA */
8258 			if (sata_hba_start(spx, rval) != 0)
8259 				/*
8260 				 * Pkt not accepted for execution.
8261 				 */
8262 				return (SATA_FAILURE);
8263 
8264 			*dmod = 1;
8265 
8266 			/* Now process return */
8267 			if (spx->txlt_sata_pkt->satapkt_reason !=
8268 			    SATA_PKT_COMPLETED) {
8269 				goto failure;
8270 			}
8271 		} else {
8272 			*scsipkt->pkt_scbp = STATUS_CHECK;
8273 			sense = sata_arq_sense(spx);
8274 			sense->es_key = KEY_ILLEGAL_REQUEST;
8275 			sense->es_add_code =
8276 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8277 			*pagelen = parmlen;
8278 			*rval = TRAN_ACCEPT;
8279 			return (SATA_FAILURE);
8280 		}
8281 	}
8282 	return (SATA_SUCCESS);
8283 
8284 failure:
8285 	sata_xlate_errors(spx);
8286 
8287 	return (SATA_FAILURE);
8288 }
8289 
8290 /*
8291  * Process mode select informational exceptions control page 0x1c
8292  *
8293  * The only changeable bit is dexcpt (disable exceptions).
8294  * MRIE (method of reporting informational exceptions) must be
8295  * "only on request".
8296  * This page applies to informational exceptions that report
8297  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8298  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8299  * Informational exception conditions occur as the result of background scan
8300  * errors, background self-test errors, or vendor specific events within a
8301  * logical unit. An informational exception condition may occur asynchronous
8302  * to any commands.
8303  *
8304  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8305  * If operation resulted in changing device setup, dmod flag should be set to
8306  * one (1). If parameters were not changed, dmod flag should be set to 0.
8307  * Upon return, if operation required sending command to the device, the rval
8308  * should be set to the value returned by sata_hba_start. If operation
8309  * did not require device access, rval should be set to TRAN_ACCEPT.
8310  * The pagelen should be set to the length of the page.
8311  *
8312  * This function has to be called with a port mutex held.
8313  *
8314  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8315  *
8316  * Cannot be called in the interrupt context.
8317  */
8318 static	int
8319 sata_mode_select_page_1c(
8320 	sata_pkt_txlate_t *spx,
8321 	struct mode_info_excpt_page *page,
8322 	int parmlen,
8323 	int *pagelen,
8324 	int *rval,
8325 	int *dmod)
8326 {
8327 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8328 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8329 	sata_drive_info_t *sdinfo;
8330 	sata_id_t *sata_id;
8331 	struct scsi_extended_sense *sense;
8332 
8333 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8334 	    &spx->txlt_sata_pkt->satapkt_device);
8335 	sata_id = &sdinfo->satadrv_id;
8336 
8337 	*dmod = 0;
8338 
8339 	/* Verify parameters length. If too short, drop it */
8340 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8341 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8342 		*scsipkt->pkt_scbp = STATUS_CHECK;
8343 		sense = sata_arq_sense(spx);
8344 		sense->es_key = KEY_ILLEGAL_REQUEST;
8345 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8346 		*pagelen = parmlen;
8347 		*rval = TRAN_ACCEPT;
8348 		return (SATA_FAILURE);
8349 	}
8350 
8351 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8352 
8353 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8354 		*scsipkt->pkt_scbp = STATUS_CHECK;
8355 		sense = sata_arq_sense(spx);
8356 		sense->es_key = KEY_ILLEGAL_REQUEST;
8357 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8358 		*pagelen = parmlen;
8359 		*rval = TRAN_ACCEPT;
8360 		return (SATA_FAILURE);
8361 	}
8362 
8363 	/* If already in the state requested, we are done */
8364 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8365 		/* nothing to do */
8366 		*rval = TRAN_ACCEPT;
8367 		return (SATA_SUCCESS);
8368 	}
8369 
8370 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8371 
8372 	/* Build SMART_ENABLE or SMART_DISABLE command */
8373 	scmd->satacmd_addr_type = 0;		/* N/A */
8374 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8375 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8376 	scmd->satacmd_features_reg = page->dexcpt ?
8377 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8378 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8379 	scmd->satacmd_cmd_reg = SATAC_SMART;
8380 
8381 	/* Transfer command to HBA */
8382 	if (sata_hba_start(spx, rval) != 0)
8383 		/*
8384 		 * Pkt not accepted for execution.
8385 		 */
8386 		return (SATA_FAILURE);
8387 
8388 	*dmod = 1;	/* At least may have been modified */
8389 
8390 	/* Now process return */
8391 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8392 		return (SATA_SUCCESS);
8393 
8394 	/* Packet did not complete successfully */
8395 	sata_xlate_errors(spx);
8396 
8397 	return (SATA_FAILURE);
8398 }
8399 
8400 /*
8401  * Process mode select acoustic management control page 0x30
8402  *
8403  *
8404  * This function has to be called with a port mutex held.
8405  *
8406  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8407  *
8408  * Cannot be called in the interrupt context.
8409  */
8410 int
8411 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8412     mode_acoustic_management *page, int parmlen, int *pagelen,
8413     int *rval, int *dmod)
8414 {
8415 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8416 	sata_drive_info_t *sdinfo;
8417 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8418 	sata_id_t *sata_id;
8419 	struct scsi_extended_sense *sense;
8420 
8421 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8422 	    &spx->txlt_sata_pkt->satapkt_device);
8423 	sata_id = &sdinfo->satadrv_id;
8424 	*dmod = 0;
8425 
8426 	/* If parmlen is too short or the feature is not supported, drop it */
8427 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8428 	    sizeof (struct mode_page)) > parmlen) ||
8429 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8430 		*scsipkt->pkt_scbp = STATUS_CHECK;
8431 		sense = sata_arq_sense(spx);
8432 		sense->es_key = KEY_ILLEGAL_REQUEST;
8433 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8434 		*pagelen = parmlen;
8435 		*rval = TRAN_ACCEPT;
8436 		return (SATA_FAILURE);
8437 	}
8438 
8439 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8440 	    sizeof (struct mode_page);
8441 
8442 	/*
8443 	 * We can enable and disable acoustice management and
8444 	 * set the acoustic management level.
8445 	 */
8446 
8447 	/*
8448 	 * Set-up Internal SET FEATURES command(s)
8449 	 */
8450 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8451 	scmd->satacmd_addr_type = 0;
8452 	scmd->satacmd_device_reg = 0;
8453 	scmd->satacmd_status_reg = 0;
8454 	scmd->satacmd_error_reg = 0;
8455 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8456 	if (page->acoustic_manag_enable) {
8457 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8458 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8459 	} else {	/* disabling acoustic management */
8460 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8461 	}
8462 
8463 	/* Transfer command to HBA */
8464 	if (sata_hba_start(spx, rval) != 0)
8465 		/*
8466 		 * Pkt not accepted for execution.
8467 		 */
8468 		return (SATA_FAILURE);
8469 
8470 	/* Now process return */
8471 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8472 		sata_xlate_errors(spx);
8473 		return (SATA_FAILURE);
8474 	}
8475 
8476 	*dmod = 1;
8477 
8478 	return (SATA_SUCCESS);
8479 }
8480 
8481 /*
8482  * Process mode select power condition page 0x1a
8483  *
8484  * This function has to be called with a port mutex held.
8485  *
8486  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8487  *
8488  * Cannot be called in the interrupt context.
8489  */
8490 int
8491 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8492     mode_info_power_cond *page, int parmlen, int *pagelen,
8493     int *rval, int *dmod)
8494 {
8495 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8496 	sata_drive_info_t *sdinfo;
8497 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8498 	sata_id_t *sata_id;
8499 	struct scsi_extended_sense *sense;
8500 	uint8_t ata_count;
8501 	int i, len;
8502 
8503 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8504 	    &spx->txlt_sata_pkt->satapkt_device);
8505 	sata_id = &sdinfo->satadrv_id;
8506 	*dmod = 0;
8507 
8508 	len = sizeof (struct mode_info_power_cond);
8509 	len += sizeof (struct mode_page);
8510 
8511 	/* If parmlen is too short or the feature is not supported, drop it */
8512 	if ((len < parmlen) || (page->idle == 1) ||
8513 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8514 		*scsipkt->pkt_scbp = STATUS_CHECK;
8515 		sense = sata_arq_sense(spx);
8516 		sense->es_key = KEY_ILLEGAL_REQUEST;
8517 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8518 		*pagelen = parmlen;
8519 		*rval = TRAN_ACCEPT;
8520 		return (SATA_FAILURE);
8521 	}
8522 
8523 	*pagelen = len;
8524 
8525 	/*
8526 	 * Set-up Internal STANDBY command(s)
8527 	 */
8528 	if (page->standby == 0)
8529 		goto out;
8530 
8531 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8532 
8533 	scmd->satacmd_addr_type = 0;
8534 	scmd->satacmd_sec_count_lsb = ata_count;
8535 	scmd->satacmd_lba_low_lsb = 0;
8536 	scmd->satacmd_lba_mid_lsb = 0;
8537 	scmd->satacmd_lba_high_lsb = 0;
8538 	scmd->satacmd_features_reg = 0;
8539 	scmd->satacmd_device_reg = 0;
8540 	scmd->satacmd_status_reg = 0;
8541 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8542 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8543 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8544 
8545 	/* Transfer command to HBA */
8546 	if (sata_hba_start(spx, rval) != 0) {
8547 		return (SATA_FAILURE);
8548 	} else {
8549 		if ((scmd->satacmd_error_reg != 0) ||
8550 		    (spx->txlt_sata_pkt->satapkt_reason !=
8551 		    SATA_PKT_COMPLETED)) {
8552 			sata_xlate_errors(spx);
8553 			return (SATA_FAILURE);
8554 		}
8555 	}
8556 
8557 	for (i = 0; i < 4; i++) {
8558 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8559 	}
8560 out:
8561 	*dmod = 1;
8562 	return (SATA_SUCCESS);
8563 }
8564 
8565 /*
8566  * sata_build_lsense_page0() is used to create the
8567  * SCSI LOG SENSE page 0 (supported log pages)
8568  *
8569  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8570  * (supported log pages, self-test results, informational exceptions
8571  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8572  *
8573  * Takes a sata_drive_info t * and the address of a buffer
8574  * in which to create the page information.
8575  *
8576  * Returns the number of bytes valid in the buffer.
8577  */
8578 static	int
8579 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8580 {
8581 	struct log_parameter *lpp = (struct log_parameter *)buf;
8582 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8583 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8584 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8585 
8586 	lpp->param_code[0] = 0;
8587 	lpp->param_code[1] = 0;
8588 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8589 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8590 
8591 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8592 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8593 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8594 			++num_pages_supported;
8595 		}
8596 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8597 		++num_pages_supported;
8598 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8599 		++num_pages_supported;
8600 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8601 		++num_pages_supported;
8602 	}
8603 
8604 	lpp->param_len = num_pages_supported;
8605 
8606 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8607 	    num_pages_supported);
8608 }
8609 
8610 /*
8611  * sata_build_lsense_page_10() is used to create the
8612  * SCSI LOG SENSE page 0x10 (self-test results)
8613  *
8614  * Takes a sata_drive_info t * and the address of a buffer
8615  * in which to create the page information as well as a sata_hba_inst_t *.
8616  *
8617  * Returns the number of bytes valid in the buffer.
8618  *
8619  * Note: Self test and SMART data is accessible in device log pages.
8620  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8621  * of data can be transferred by a single command), or by the General Purpose
8622  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8623  * - approximately 33MB - can be transferred by a single command.
8624  * The SCT Command response (either error or command) is the same for both
8625  * the SMART and GPL methods of issuing commands.
8626  * This function uses READ LOG EXT command when drive supports LBA48, and
8627  * SMART READ command otherwise.
8628  *
8629  * Since above commands are executed in a synchronous mode, this function
8630  * should not be called in an interrupt context.
8631  */
8632 static	int
8633 sata_build_lsense_page_10(
8634 	sata_drive_info_t *sdinfo,
8635 	uint8_t *buf,
8636 	sata_hba_inst_t *sata_hba_inst)
8637 {
8638 	struct log_parameter *lpp = (struct log_parameter *)buf;
8639 	int rval;
8640 
8641 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8642 		struct smart_ext_selftest_log *ext_selftest_log;
8643 
8644 		ext_selftest_log = kmem_zalloc(
8645 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8646 
8647 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8648 		    ext_selftest_log, 0);
8649 		if (rval == 0) {
8650 			int index, start_index;
8651 			struct smart_ext_selftest_log_entry *entry;
8652 			static const struct smart_ext_selftest_log_entry empty =
8653 			    {0};
8654 			uint16_t block_num;
8655 			int count;
8656 			boolean_t only_one_block = B_FALSE;
8657 
8658 			index = ext_selftest_log->
8659 			    smart_ext_selftest_log_index[0];
8660 			index |= ext_selftest_log->
8661 			    smart_ext_selftest_log_index[1] << 8;
8662 			if (index == 0)
8663 				goto out;
8664 
8665 			--index;	/* Correct for 0 origin */
8666 			start_index = index;	/* remember where we started */
8667 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8668 			if (block_num != 0) {
8669 				rval = sata_ext_smart_selftest_read_log(
8670 				    sata_hba_inst, sdinfo, ext_selftest_log,
8671 				    block_num);
8672 				if (rval != 0)
8673 					goto out;
8674 			}
8675 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8676 			entry =
8677 			    &ext_selftest_log->
8678 			    smart_ext_selftest_log_entries[index];
8679 
8680 			for (count = 1;
8681 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8682 			    ++count) {
8683 				uint8_t status;
8684 				uint8_t code;
8685 				uint8_t sense_key;
8686 				uint8_t add_sense_code;
8687 				uint8_t add_sense_code_qual;
8688 
8689 				/* If this is an unused entry, we are done */
8690 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8691 					/* Broken firmware on some disks */
8692 					if (index + 1 ==
8693 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8694 						--entry;
8695 						--index;
8696 						if (bcmp(entry, &empty,
8697 						    sizeof (empty)) == 0)
8698 							goto out;
8699 					} else
8700 						goto out;
8701 				}
8702 
8703 				if (only_one_block &&
8704 				    start_index == index)
8705 					goto out;
8706 
8707 				lpp->param_code[0] = 0;
8708 				lpp->param_code[1] = count;
8709 				lpp->param_ctrl_flags =
8710 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8711 				lpp->param_len =
8712 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8713 
8714 				status = entry->smart_ext_selftest_log_status;
8715 				status >>= 4;
8716 				switch (status) {
8717 				case 0:
8718 				default:
8719 					sense_key = KEY_NO_SENSE;
8720 					add_sense_code =
8721 					    SD_SCSI_ASC_NO_ADD_SENSE;
8722 					add_sense_code_qual = 0;
8723 					break;
8724 				case 1:
8725 					sense_key = KEY_ABORTED_COMMAND;
8726 					add_sense_code =
8727 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8728 					add_sense_code_qual = SCSI_COMPONENT_81;
8729 					break;
8730 				case 2:
8731 					sense_key = KEY_ABORTED_COMMAND;
8732 					add_sense_code =
8733 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8734 					add_sense_code_qual = SCSI_COMPONENT_82;
8735 					break;
8736 				case 3:
8737 					sense_key = KEY_ABORTED_COMMAND;
8738 					add_sense_code =
8739 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8740 					add_sense_code_qual = SCSI_COMPONENT_83;
8741 					break;
8742 				case 4:
8743 					sense_key = KEY_HARDWARE_ERROR;
8744 					add_sense_code =
8745 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8746 					add_sense_code_qual = SCSI_COMPONENT_84;
8747 					break;
8748 				case 5:
8749 					sense_key = KEY_HARDWARE_ERROR;
8750 					add_sense_code =
8751 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8752 					add_sense_code_qual = SCSI_COMPONENT_85;
8753 					break;
8754 				case 6:
8755 					sense_key = KEY_HARDWARE_ERROR;
8756 					add_sense_code =
8757 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8758 					add_sense_code_qual = SCSI_COMPONENT_86;
8759 					break;
8760 				case 7:
8761 					sense_key = KEY_MEDIUM_ERROR;
8762 					add_sense_code =
8763 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8764 					add_sense_code_qual = SCSI_COMPONENT_87;
8765 					break;
8766 				case 8:
8767 					sense_key = KEY_HARDWARE_ERROR;
8768 					add_sense_code =
8769 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8770 					add_sense_code_qual = SCSI_COMPONENT_88;
8771 					break;
8772 				}
8773 				code = 0;	/* unspecified */
8774 				status |= (code << 4);
8775 				lpp->param_values[0] = status;
8776 				lpp->param_values[1] = 0; /* unspecified */
8777 				lpp->param_values[2] = entry->
8778 				    smart_ext_selftest_log_timestamp[1];
8779 				lpp->param_values[3] = entry->
8780 				    smart_ext_selftest_log_timestamp[0];
8781 				if (status != 0) {
8782 					lpp->param_values[4] = 0;
8783 					lpp->param_values[5] = 0;
8784 					lpp->param_values[6] = entry->
8785 					    smart_ext_selftest_log_failing_lba
8786 					    [5];
8787 					lpp->param_values[7] = entry->
8788 					    smart_ext_selftest_log_failing_lba
8789 					    [4];
8790 					lpp->param_values[8] = entry->
8791 					    smart_ext_selftest_log_failing_lba
8792 					    [3];
8793 					lpp->param_values[9] = entry->
8794 					    smart_ext_selftest_log_failing_lba
8795 					    [2];
8796 					lpp->param_values[10] = entry->
8797 					    smart_ext_selftest_log_failing_lba
8798 					    [1];
8799 					lpp->param_values[11] = entry->
8800 					    smart_ext_selftest_log_failing_lba
8801 					    [0];
8802 				} else {	/* No bad block address */
8803 					lpp->param_values[4] = 0xff;
8804 					lpp->param_values[5] = 0xff;
8805 					lpp->param_values[6] = 0xff;
8806 					lpp->param_values[7] = 0xff;
8807 					lpp->param_values[8] = 0xff;
8808 					lpp->param_values[9] = 0xff;
8809 					lpp->param_values[10] = 0xff;
8810 					lpp->param_values[11] = 0xff;
8811 				}
8812 
8813 				lpp->param_values[12] = sense_key;
8814 				lpp->param_values[13] = add_sense_code;
8815 				lpp->param_values[14] = add_sense_code_qual;
8816 				lpp->param_values[15] = 0; /* undefined */
8817 
8818 				lpp = (struct log_parameter *)
8819 				    (((uint8_t *)lpp) +
8820 				    SCSI_LOG_PARAM_HDR_LEN +
8821 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8822 
8823 				--index;	/* Back up to previous entry */
8824 				if (index < 0) {
8825 					if (block_num > 0) {
8826 						--block_num;
8827 					} else {
8828 						struct read_log_ext_directory
8829 						    logdir;
8830 
8831 						rval =
8832 						    sata_read_log_ext_directory(
8833 						    sata_hba_inst, sdinfo,
8834 						    &logdir);
8835 						if (rval == -1)
8836 							goto out;
8837 						if ((logdir.read_log_ext_vers
8838 						    [0] == 0) &&
8839 						    (logdir.read_log_ext_vers
8840 						    [1] == 0))
8841 							goto out;
8842 						block_num =
8843 						    logdir.read_log_ext_nblks
8844 						    [EXT_SMART_SELFTEST_LOG_PAGE
8845 						    - 1][0];
8846 						block_num |= logdir.
8847 						    read_log_ext_nblks
8848 						    [EXT_SMART_SELFTEST_LOG_PAGE
8849 						    - 1][1] << 8;
8850 						--block_num;
8851 						only_one_block =
8852 						    (block_num == 0);
8853 					}
8854 					rval = sata_ext_smart_selftest_read_log(
8855 					    sata_hba_inst, sdinfo,
8856 					    ext_selftest_log, block_num);
8857 					if (rval != 0)
8858 						goto out;
8859 
8860 					index =
8861 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8862 					    1;
8863 				}
8864 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8865 				entry = &ext_selftest_log->
8866 				    smart_ext_selftest_log_entries[index];
8867 			}
8868 		}
8869 out:
8870 		kmem_free(ext_selftest_log,
8871 		    sizeof (struct smart_ext_selftest_log));
8872 	} else {
8873 		struct smart_selftest_log *selftest_log;
8874 
8875 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8876 		    KM_SLEEP);
8877 
8878 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8879 		    selftest_log);
8880 
8881 		if (rval == 0) {
8882 			int index;
8883 			int count;
8884 			struct smart_selftest_log_entry *entry;
8885 			static const struct smart_selftest_log_entry empty =
8886 			    { 0 };
8887 
8888 			index = selftest_log->smart_selftest_log_index;
8889 			if (index == 0)
8890 				goto done;
8891 			--index;	/* Correct for 0 origin */
8892 			entry = &selftest_log->
8893 			    smart_selftest_log_entries[index];
8894 			for (count = 1;
8895 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8896 			    ++count) {
8897 				uint8_t status;
8898 				uint8_t code;
8899 				uint8_t sense_key;
8900 				uint8_t add_sense_code;
8901 				uint8_t add_sense_code_qual;
8902 
8903 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8904 					goto done;
8905 
8906 				lpp->param_code[0] = 0;
8907 				lpp->param_code[1] = count;
8908 				lpp->param_ctrl_flags =
8909 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8910 				lpp->param_len =
8911 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8912 
8913 				status = entry->smart_selftest_log_status;
8914 				status >>= 4;
8915 				switch (status) {
8916 				case 0:
8917 				default:
8918 					sense_key = KEY_NO_SENSE;
8919 					add_sense_code =
8920 					    SD_SCSI_ASC_NO_ADD_SENSE;
8921 					break;
8922 				case 1:
8923 					sense_key = KEY_ABORTED_COMMAND;
8924 					add_sense_code =
8925 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8926 					add_sense_code_qual = SCSI_COMPONENT_81;
8927 					break;
8928 				case 2:
8929 					sense_key = KEY_ABORTED_COMMAND;
8930 					add_sense_code =
8931 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8932 					add_sense_code_qual = SCSI_COMPONENT_82;
8933 					break;
8934 				case 3:
8935 					sense_key = KEY_ABORTED_COMMAND;
8936 					add_sense_code =
8937 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8938 					add_sense_code_qual = SCSI_COMPONENT_83;
8939 					break;
8940 				case 4:
8941 					sense_key = KEY_HARDWARE_ERROR;
8942 					add_sense_code =
8943 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8944 					add_sense_code_qual = SCSI_COMPONENT_84;
8945 					break;
8946 				case 5:
8947 					sense_key = KEY_HARDWARE_ERROR;
8948 					add_sense_code =
8949 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8950 					add_sense_code_qual = SCSI_COMPONENT_85;
8951 					break;
8952 				case 6:
8953 					sense_key = KEY_HARDWARE_ERROR;
8954 					add_sense_code =
8955 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8956 					add_sense_code_qual = SCSI_COMPONENT_86;
8957 					break;
8958 				case 7:
8959 					sense_key = KEY_MEDIUM_ERROR;
8960 					add_sense_code =
8961 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8962 					add_sense_code_qual = SCSI_COMPONENT_87;
8963 					break;
8964 				case 8:
8965 					sense_key = KEY_HARDWARE_ERROR;
8966 					add_sense_code =
8967 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8968 					add_sense_code_qual = SCSI_COMPONENT_88;
8969 					break;
8970 				}
8971 				code = 0;	/* unspecified */
8972 				status |= (code << 4);
8973 				lpp->param_values[0] = status;
8974 				lpp->param_values[1] = 0; /* unspecified */
8975 				lpp->param_values[2] = entry->
8976 				    smart_selftest_log_timestamp[1];
8977 				lpp->param_values[3] = entry->
8978 				    smart_selftest_log_timestamp[0];
8979 				if (status != 0) {
8980 					lpp->param_values[4] = 0;
8981 					lpp->param_values[5] = 0;
8982 					lpp->param_values[6] = 0;
8983 					lpp->param_values[7] = 0;
8984 					lpp->param_values[8] = entry->
8985 					    smart_selftest_log_failing_lba[3];
8986 					lpp->param_values[9] = entry->
8987 					    smart_selftest_log_failing_lba[2];
8988 					lpp->param_values[10] = entry->
8989 					    smart_selftest_log_failing_lba[1];
8990 					lpp->param_values[11] = entry->
8991 					    smart_selftest_log_failing_lba[0];
8992 				} else {	/* No block address */
8993 					lpp->param_values[4] = 0xff;
8994 					lpp->param_values[5] = 0xff;
8995 					lpp->param_values[6] = 0xff;
8996 					lpp->param_values[7] = 0xff;
8997 					lpp->param_values[8] = 0xff;
8998 					lpp->param_values[9] = 0xff;
8999 					lpp->param_values[10] = 0xff;
9000 					lpp->param_values[11] = 0xff;
9001 				}
9002 				lpp->param_values[12] = sense_key;
9003 				lpp->param_values[13] = add_sense_code;
9004 				lpp->param_values[14] = add_sense_code_qual;
9005 				lpp->param_values[15] = 0; /* undefined */
9006 
9007 				lpp = (struct log_parameter *)
9008 				    (((uint8_t *)lpp) +
9009 				    SCSI_LOG_PARAM_HDR_LEN +
9010 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9011 				--index;	/* back up to previous entry */
9012 				if (index < 0) {
9013 					index =
9014 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9015 				}
9016 				entry = &selftest_log->
9017 				    smart_selftest_log_entries[index];
9018 			}
9019 		}
9020 done:
9021 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9022 	}
9023 
9024 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9025 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9026 }
9027 
9028 /*
9029  * sata_build_lsense_page_2f() is used to create the
9030  * SCSI LOG SENSE page 0x2f (informational exceptions)
9031  *
9032  * Takes a sata_drive_info t * and the address of a buffer
9033  * in which to create the page information as well as a sata_hba_inst_t *.
9034  *
9035  * Returns the number of bytes valid in the buffer.
9036  *
9037  * Because it invokes function(s) that send synchronously executed command
9038  * to the HBA, it cannot be called in the interrupt context.
9039  */
9040 static	int
9041 sata_build_lsense_page_2f(
9042 	sata_drive_info_t *sdinfo,
9043 	uint8_t *buf,
9044 	sata_hba_inst_t *sata_hba_inst)
9045 {
9046 	struct log_parameter *lpp = (struct log_parameter *)buf;
9047 	int rval;
9048 	uint8_t *smart_data;
9049 	uint8_t temp;
9050 	sata_id_t *sata_id;
9051 #define	SMART_NO_TEMP	0xff
9052 
9053 	lpp->param_code[0] = 0;
9054 	lpp->param_code[1] = 0;
9055 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9056 
9057 	/* Now get the SMART status w.r.t. threshold exceeded */
9058 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9059 	switch (rval) {
9060 	case 1:
9061 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9062 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9063 		break;
9064 	case 0:
9065 	case -1:	/* failed to get data */
9066 		lpp->param_values[0] = 0;	/* No failure predicted */
9067 		lpp->param_values[1] = 0;
9068 		break;
9069 #if defined(SATA_DEBUG)
9070 	default:
9071 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9072 		/* NOTREACHED */
9073 #endif
9074 	}
9075 
9076 	sata_id = &sdinfo->satadrv_id;
9077 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9078 		temp = SMART_NO_TEMP;
9079 	else {
9080 		/* Now get the temperature */
9081 		smart_data = kmem_zalloc(512, KM_SLEEP);
9082 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9083 		    SCT_STATUS_LOG_PAGE, 1);
9084 		if (rval == -1)
9085 			temp = SMART_NO_TEMP;
9086 		else {
9087 			temp = smart_data[200];
9088 			if (temp & 0x80) {
9089 				if (temp & 0x7f)
9090 					temp = 0;
9091 				else
9092 					temp = SMART_NO_TEMP;
9093 			}
9094 		}
9095 		kmem_free(smart_data, 512);
9096 	}
9097 
9098 	lpp->param_values[2] = temp;	/* most recent temperature */
9099 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9100 
9101 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9102 
9103 
9104 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9105 }
9106 
9107 /*
9108  * sata_build_lsense_page_30() is used to create the
9109  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9110  *
9111  * Takes a sata_drive_info t * and the address of a buffer
9112  * in which to create the page information as well as a sata_hba_inst_t *.
9113  *
9114  * Returns the number of bytes valid in the buffer.
9115  */
9116 static int
9117 sata_build_lsense_page_30(
9118 	sata_drive_info_t *sdinfo,
9119 	uint8_t *buf,
9120 	sata_hba_inst_t *sata_hba_inst)
9121 {
9122 	struct smart_data *smart_data = (struct smart_data *)buf;
9123 	int rval;
9124 
9125 	/* Now do the SMART READ DATA */
9126 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9127 	if (rval == -1)
9128 		return (0);
9129 
9130 	return (sizeof (struct smart_data));
9131 }
9132 
9133 /*
9134  * sata_build_lsense_page_0e() is used to create the
9135  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9136  *
9137  * Date of Manufacture (0x0001)
9138  *	YEAR = "0000"
9139  *	WEEK = "00"
9140  * Accounting Date (0x0002)
9141  *	6 ASCII space character(20h)
9142  * Specified cycle count over device lifetime
9143  *	VALUE - THRESH - the delta between max and min;
9144  * Accumulated start-stop cycles
9145  *	VALUE - WORST - the accumulated cycles;
9146  *
9147  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9148  *
9149  * Takes a sata_drive_info t * and the address of a buffer
9150  * in which to create the page information as well as a sata_hba_inst_t *.
9151  *
9152  * Returns the number of bytes valid in the buffer.
9153  */
9154 static	int
9155 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9156 	sata_pkt_txlate_t *spx)
9157 {
9158 	struct start_stop_cycle_counter_log *log_page;
9159 	int i, rval, index;
9160 	uint8_t smart_data[512], id, value, worst, thresh;
9161 	uint32_t max_count, cycles;
9162 
9163 	/* Now do the SMART READ DATA */
9164 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9165 	    (struct smart_data *)smart_data);
9166 	if (rval == -1)
9167 		return (0);
9168 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9169 		index = (i * 12) + 2;
9170 		id = smart_data[index];
9171 		if (id != SMART_START_STOP_COUNT_ID)
9172 			continue;
9173 		else {
9174 			thresh = smart_data[index + 2];
9175 			value = smart_data[index + 3];
9176 			worst = smart_data[index + 4];
9177 			break;
9178 		}
9179 	}
9180 	if (id != SMART_START_STOP_COUNT_ID)
9181 		return (0);
9182 	max_count = value - thresh;
9183 	cycles = value - worst;
9184 
9185 	log_page = (struct start_stop_cycle_counter_log *)buf;
9186 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9187 	log_page->code = 0x0e;
9188 	log_page->page_len_low = 0x24;
9189 
9190 	log_page->manufactor_date_low = 0x1;
9191 	log_page->param_1.fmt_link = 0x1; /* 01b */
9192 	log_page->param_len_1 = 0x06;
9193 	for (i = 0; i < 4; i++) {
9194 		log_page->year_manu[i] = 0x30;
9195 		if (i < 2)
9196 			log_page->week_manu[i] = 0x30;
9197 	}
9198 
9199 	log_page->account_date_low = 0x02;
9200 	log_page->param_2.fmt_link = 0x01; /* 01b */
9201 	log_page->param_len_2 = 0x06;
9202 	for (i = 0; i < 4; i++) {
9203 		log_page->year_account[i] = 0x20;
9204 		if (i < 2)
9205 			log_page->week_account[i] = 0x20;
9206 	}
9207 
9208 	log_page->lifetime_code_low = 0x03;
9209 	log_page->param_3.fmt_link = 0x03; /* 11b */
9210 	log_page->param_len_3 = 0x04;
9211 	/* VALUE - THRESH - the delta between max and min */
9212 	log_page->cycle_code_low = 0x04;
9213 	log_page->param_4.fmt_link = 0x03; /* 11b */
9214 	log_page->param_len_4 = 0x04;
9215 	/* WORST - THRESH - the distance from 'now' to min */
9216 
9217 	for (i = 0; i < 4; i++) {
9218 		log_page->cycle_lifetime[i] =
9219 		    (max_count >> (8 * (3 - i))) & 0xff;
9220 		log_page->cycle_accumulated[i] =
9221 		    (cycles >> (8 * (3 - i))) & 0xff;
9222 	}
9223 
9224 	return (sizeof (struct start_stop_cycle_counter_log));
9225 }
9226 
9227 /*
9228  * This function was used for build a ATA read verify sector command
9229  */
9230 static void
9231 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9232 {
9233 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9234 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9235 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9236 
9237 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9238 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9239 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9240 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9241 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9242 	scmd->satacmd_features_reg = 0;
9243 	scmd->satacmd_status_reg = 0;
9244 	scmd->satacmd_error_reg = 0;
9245 }
9246 
9247 /*
9248  * This function was used for building an ATA
9249  * command, and only command register need to
9250  * be defined, other register will be zero or na.
9251  */
9252 static void
9253 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9254 {
9255 	scmd->satacmd_addr_type = 0;
9256 	scmd->satacmd_cmd_reg = cmd;
9257 	scmd->satacmd_device_reg = 0;
9258 	scmd->satacmd_sec_count_lsb = 0;
9259 	scmd->satacmd_lba_low_lsb = 0;
9260 	scmd->satacmd_lba_mid_lsb = 0;
9261 	scmd->satacmd_lba_high_lsb = 0;
9262 	scmd->satacmd_features_reg = 0;
9263 	scmd->satacmd_status_reg = 0;
9264 	scmd->satacmd_error_reg = 0;
9265 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9266 }
9267 
9268 /*
9269  * This function was used for changing the standby
9270  * timer format from SCSI to ATA.
9271  */
9272 static uint8_t
9273 sata_get_standby_timer(uint8_t *timer)
9274 {
9275 	uint32_t i = 0, count = 0;
9276 	uint8_t ata_count;
9277 
9278 	for (i = 0; i < 4; i++) {
9279 		count = count << 8 | timer[i];
9280 	}
9281 
9282 	if (count == 0)
9283 		return (0);
9284 
9285 	if (count >= 1 && count <= 12000)
9286 		ata_count = (count -1) / 50 + 1;
9287 	else if (count > 12000 && count <= 12600)
9288 		ata_count = 0xfc;
9289 	else if (count > 12601 && count <= 12750)
9290 		ata_count = 0xff;
9291 	else if (count > 12750 && count <= 17999)
9292 		ata_count = 0xf1;
9293 	else if (count > 18000 && count <= 198000)
9294 		ata_count = count / 18000 + 240;
9295 	else
9296 		ata_count = 0xfd;
9297 	return (ata_count);
9298 }
9299 
9300 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9301 
9302 /*
9303  * Start command for ATAPI device.
9304  * This function processes scsi_pkt requests.
9305  * Now CD/DVD, tape and ATAPI disk devices are supported.
9306  * Most commands are packet without any translation into Packet Command.
9307  * Some may be trapped and executed as SATA commands (not clear which one).
9308  *
9309  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9310  * execution).
9311  * Returns other TRAN_XXXX codes if command is not accepted or completed
9312  * (see return values for sata_hba_start()).
9313  *
9314  * Note:
9315  * Inquiry cdb format differs between transport version 2 and 3.
9316  * However, the transport version 3 devices that were checked did not adhere
9317  * to the specification (ignored MSB of the allocation length). Therefore,
9318  * the transport version is not checked, but Inquiry allocation length is
9319  * truncated to 255 bytes if the original allocation length set-up by the
9320  * target driver is greater than 255 bytes.
9321  */
9322 static int
9323 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9324 {
9325 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9326 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9327 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9328 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9329 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9330 	    &spx->txlt_sata_pkt->satapkt_device);
9331 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9332 	int cdblen;
9333 	int rval, reason;
9334 	int synch;
9335 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9336 
9337 	mutex_enter(cport_mutex);
9338 
9339 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9340 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9341 		mutex_exit(cport_mutex);
9342 		return (rval);
9343 	}
9344 
9345 	/*
9346 	 * ATAPI device executes some ATA commands in addition to those
9347 	 * commands sent via PACKET command. These ATA commands may be
9348 	 * executed by the regular SATA translation functions. None needs
9349 	 * to be captured now.
9350 	 *
9351 	 * Commands sent via PACKET command include:
9352 	 *	MMC command set for ATAPI CD/DVD device
9353 	 *	SSC command set for ATAPI TAPE device
9354 	 *	SBC command set for ATAPI disk device
9355 	 *
9356 	 */
9357 
9358 	/* Check the size of cdb */
9359 
9360 	switch (GETGROUP(cdbp)) {
9361 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9362 		/*
9363 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9364 		 * therefore require special handling.  Return failure, for now.
9365 		 */
9366 		mutex_exit(cport_mutex);
9367 		return (TRAN_BADPKT);
9368 
9369 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9370 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9371 		/* obtain length from the scsi_pkt */
9372 		cdblen = scsipkt->pkt_cdblen;
9373 		break;
9374 
9375 	default:
9376 		/* CDB's length is statically known, per SPC-4 */
9377 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9378 		break;
9379 	}
9380 
9381 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9382 		sata_log(NULL, CE_WARN,
9383 		    "sata: invalid ATAPI cdb length %d",
9384 		    cdblen);
9385 		mutex_exit(cport_mutex);
9386 		return (TRAN_BADPKT);
9387 	}
9388 
9389 	SATAATAPITRACE(spx, cdblen);
9390 
9391 	/*
9392 	 * For non-read/write commands we need to
9393 	 * map buffer
9394 	 */
9395 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9396 	case SCMD_READ:
9397 	case SCMD_READ_G1:
9398 	case SCMD_READ_G5:
9399 	case SCMD_READ_G4:
9400 	case SCMD_WRITE:
9401 	case SCMD_WRITE_G1:
9402 	case SCMD_WRITE_G5:
9403 	case SCMD_WRITE_G4:
9404 		break;
9405 	default:
9406 		if (bp != NULL) {
9407 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9408 				bp_mapin(bp);
9409 		}
9410 		break;
9411 	}
9412 	/*
9413 	 * scmd->satacmd_flags.sata_data_direction default -
9414 	 * SATA_DIR_NODATA_XFER - is set by
9415 	 * sata_txlt_generic_pkt_info().
9416 	 */
9417 	if (scmd->satacmd_bp) {
9418 		if (scmd->satacmd_bp->b_flags & B_READ) {
9419 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9420 		} else {
9421 			scmd->satacmd_flags.sata_data_direction =
9422 			    SATA_DIR_WRITE;
9423 		}
9424 	}
9425 
9426 	/*
9427 	 * Set up ATAPI packet command.
9428 	 */
9429 
9430 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9431 
9432 	/* Copy cdb into sata_cmd */
9433 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9434 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9435 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9436 
9437 	/* See note in the command header */
9438 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9439 		if (scmd->satacmd_acdb[3] != 0)
9440 			scmd->satacmd_acdb[4] = 255;
9441 	}
9442 
9443 #ifdef SATA_DEBUG
9444 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9445 		uint8_t *p = scmd->satacmd_acdb;
9446 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9447 
9448 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9449 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9450 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9451 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9452 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9453 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9454 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9455 	}
9456 #endif
9457 
9458 	/*
9459 	 * Preset request sense data to NO SENSE.
9460 	 * If there is no way to get error information via Request Sense,
9461 	 * the packet request sense data would not have to be modified by HBA,
9462 	 * but it could be returned as is.
9463 	 */
9464 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9465 	sata_fixed_sense_data_preset(
9466 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9467 
9468 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9469 		/* Need callback function */
9470 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9471 		synch = FALSE;
9472 	} else
9473 		synch = TRUE;
9474 
9475 	/* Transfer command to HBA */
9476 	if (sata_hba_start(spx, &rval) != 0) {
9477 		/* Pkt not accepted for execution */
9478 		mutex_exit(cport_mutex);
9479 		return (rval);
9480 	}
9481 	mutex_exit(cport_mutex);
9482 	/*
9483 	 * If execution is non-synchronous,
9484 	 * a callback function will handle potential errors, translate
9485 	 * the response and will do a callback to a target driver.
9486 	 * If it was synchronous, use the same framework callback to check
9487 	 * an execution status.
9488 	 */
9489 	if (synch) {
9490 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9491 		    "synchronous execution status %x\n",
9492 		    spx->txlt_sata_pkt->satapkt_reason);
9493 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9494 	}
9495 	return (TRAN_ACCEPT);
9496 }
9497 
9498 
9499 /*
9500  * ATAPI Packet command completion.
9501  *
9502  * Failure of the command passed via Packet command are considered device
9503  * error. SATA HBA driver would have to retrieve error data (via Request
9504  * Sense command delivered via error retrieval sata packet) and copy it
9505  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9506  */
9507 static void
9508 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9509 {
9510 	sata_pkt_txlate_t *spx =
9511 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9512 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9513 	struct scsi_extended_sense *sense;
9514 	struct buf *bp;
9515 	int rval;
9516 
9517 #ifdef SATA_DEBUG
9518 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9519 #endif
9520 
9521 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9522 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9523 
9524 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9525 		/* Normal completion */
9526 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9527 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9528 		scsipkt->pkt_reason = CMD_CMPLT;
9529 		*scsipkt->pkt_scbp = STATUS_GOOD;
9530 		if (spx->txlt_tmp_buf != NULL) {
9531 			/* Temporary buffer was used */
9532 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9533 			if (bp->b_flags & B_READ) {
9534 				rval = ddi_dma_sync(
9535 				    spx->txlt_buf_dma_handle, 0, 0,
9536 				    DDI_DMA_SYNC_FORCPU);
9537 				ASSERT(rval == DDI_SUCCESS);
9538 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9539 				    bp->b_bcount);
9540 			}
9541 		}
9542 	} else {
9543 		/*
9544 		 * Something went wrong - analyze return
9545 		 */
9546 		*scsipkt->pkt_scbp = STATUS_CHECK;
9547 		sense = sata_arq_sense(spx);
9548 
9549 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9550 			/*
9551 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9552 			 * Under this condition ERR bit is set for ATA command,
9553 			 * and CHK bit set for ATAPI command.
9554 			 *
9555 			 * Please check st_intr & sdintr about how pkt_reason
9556 			 * is used.
9557 			 */
9558 			scsipkt->pkt_reason = CMD_CMPLT;
9559 
9560 			/*
9561 			 * We may not have ARQ data if there was a double
9562 			 * error. But sense data in sata packet was pre-set
9563 			 * with NO SENSE so it is valid even if HBA could
9564 			 * not retrieve a real sense data.
9565 			 * Just copy this sense data into scsi pkt sense area.
9566 			 */
9567 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9568 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9569 #ifdef SATA_DEBUG
9570 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9571 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9572 				    "sata_txlt_atapi_completion: %02x\n"
9573 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9574 				    "          %02x %02x %02x %02x %02x %02x "
9575 				    "          %02x %02x %02x %02x %02x %02x\n",
9576 				    scsipkt->pkt_reason,
9577 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9578 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9579 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9580 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9581 				    rqsp[16], rqsp[17]);
9582 			}
9583 #endif
9584 		} else {
9585 			switch (sata_pkt->satapkt_reason) {
9586 			case SATA_PKT_PORT_ERROR:
9587 				/*
9588 				 * We have no device data.
9589 				 */
9590 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9591 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9592 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9593 				    STATE_GOT_STATUS);
9594 				sense->es_key = KEY_HARDWARE_ERROR;
9595 				break;
9596 
9597 			case SATA_PKT_TIMEOUT:
9598 				scsipkt->pkt_reason = CMD_TIMEOUT;
9599 				scsipkt->pkt_statistics |=
9600 				    STAT_TIMEOUT | STAT_DEV_RESET;
9601 				/*
9602 				 * Need to check if HARDWARE_ERROR/
9603 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9604 				 * appropriate.
9605 				 */
9606 				break;
9607 
9608 			case SATA_PKT_ABORTED:
9609 				scsipkt->pkt_reason = CMD_ABORTED;
9610 				scsipkt->pkt_statistics |= STAT_ABORTED;
9611 				/* Should we set key COMMAND_ABPRTED? */
9612 				break;
9613 
9614 			case SATA_PKT_RESET:
9615 				scsipkt->pkt_reason = CMD_RESET;
9616 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9617 				/*
9618 				 * May be we should set Unit Attention /
9619 				 * Reset. Perhaps the same should be
9620 				 * returned for disks....
9621 				 */
9622 				sense->es_key = KEY_UNIT_ATTENTION;
9623 				sense->es_add_code = SD_SCSI_ASC_RESET;
9624 				break;
9625 
9626 			default:
9627 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9628 				    "sata_txlt_atapi_completion: "
9629 				    "invalid packet completion reason"));
9630 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9631 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9632 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9633 				    STATE_GOT_STATUS);
9634 				break;
9635 			}
9636 		}
9637 	}
9638 
9639 	SATAATAPITRACE(spx, 0);
9640 
9641 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9642 	    scsipkt->pkt_comp != NULL) {
9643 		/* scsi callback required */
9644 		(*scsipkt->pkt_comp)(scsipkt);
9645 	}
9646 }
9647 
9648 /*
9649  * Set up error retrieval sata command for ATAPI Packet Command error data
9650  * recovery.
9651  *
9652  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9653  * returns SATA_FAILURE otherwise.
9654  */
9655 
9656 static int
9657 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9658 {
9659 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9660 	sata_cmd_t *scmd;
9661 	struct buf *bp;
9662 
9663 	/*
9664 	 * Allocate dma-able buffer error data.
9665 	 * Buffer allocation will take care of buffer alignment and other DMA
9666 	 * attributes.
9667 	 */
9668 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9669 	if (bp == NULL) {
9670 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9671 		    "sata_get_err_retrieval_pkt: "
9672 		    "cannot allocate buffer for error data", NULL);
9673 		return (SATA_FAILURE);
9674 	}
9675 	bp_mapin(bp); /* make data buffer accessible */
9676 
9677 	/* Operation modes are up to the caller */
9678 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9679 
9680 	/* Synchronous mode, no callback - may be changed by the caller */
9681 	spkt->satapkt_comp = NULL;
9682 	spkt->satapkt_time = sata_default_pkt_time;
9683 
9684 	scmd = &spkt->satapkt_cmd;
9685 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9686 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9687 
9688 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9689 
9690 	/*
9691 	 * Set-up acdb. Request Sense CDB (packet command content) is
9692 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9693 	 * it is transfered into packet FIS).
9694 	 */
9695 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9696 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9697 	/* Following zeroing of pad bytes may not be necessary */
9698 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9699 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9700 
9701 	/*
9702 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9703 	 * before accessing it. Handle is in usual place in translate struct.
9704 	 */
9705 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9706 
9707 	/*
9708 	 * Preset request sense data to NO SENSE.
9709 	 * Here it is redundant, only for a symetry with scsi-originated
9710 	 * packets. It should not be used for anything but debugging.
9711 	 */
9712 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9713 	sata_fixed_sense_data_preset(
9714 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9715 
9716 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9717 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9718 
9719 	return (SATA_SUCCESS);
9720 }
9721 
9722 /*
9723  * Set-up ATAPI packet command.
9724  * Data transfer direction has to be set-up in sata_cmd structure prior to
9725  * calling this function.
9726  *
9727  * Returns void
9728  */
9729 
9730 static void
9731 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9732 {
9733 	scmd->satacmd_addr_type = 0;		/* N/A */
9734 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9735 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9736 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9737 	scmd->satacmd_lba_high_lsb =
9738 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9739 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9740 
9741 	/*
9742 	 * We want all data to be transfered via DMA.
9743 	 * But specify it only if drive supports DMA and DMA mode is
9744 	 * selected - some drives are sensitive about it.
9745 	 * Hopefully it wil work for all drives....
9746 	 */
9747 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9748 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9749 
9750 	/*
9751 	 * Features register requires special care for devices that use
9752 	 * Serial ATA bridge - they need an explicit specification of
9753 	 * the data transfer direction for Packet DMA commands.
9754 	 * Setting this bit is harmless if DMA is not used.
9755 	 *
9756 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9757 	 * spec they follow.
9758 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9759 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9760 	 * ATA/ATAPI-7 support is explicitly indicated.
9761 	 */
9762 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9763 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9764 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9765 		/*
9766 		 * Specification of major version is valid and version 7
9767 		 * is supported. It does automatically imply that all
9768 		 * spec features are supported. For now, we assume that
9769 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9770 		 */
9771 		if ((sdinfo->satadrv_id.ai_dirdma &
9772 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9773 			if (scmd->satacmd_flags.sata_data_direction ==
9774 			    SATA_DIR_READ)
9775 			scmd->satacmd_features_reg |=
9776 			    SATA_ATAPI_F_DATA_DIR_READ;
9777 		}
9778 	}
9779 }
9780 
9781 
9782 #ifdef SATA_DEBUG
9783 
9784 /* Display 18 bytes of Inquiry data */
9785 static void
9786 sata_show_inqry_data(uint8_t *buf)
9787 {
9788 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9789 	uint8_t *p;
9790 
9791 	cmn_err(CE_NOTE, "Inquiry data:");
9792 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9793 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9794 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9795 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9796 	    SATA_ATAPI_TRANS_VERSION(inq));
9797 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9798 	    inq->inq_rdf, inq->inq_aenc);
9799 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9800 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9801 	p = (uint8_t *)inq->inq_vid;
9802 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9803 	    "%02x %02x %02x %02x",
9804 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9805 	p = (uint8_t *)inq->inq_vid;
9806 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9807 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9808 
9809 	p = (uint8_t *)inq->inq_pid;
9810 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9811 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9812 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9813 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9814 	p = (uint8_t *)inq->inq_pid;
9815 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9816 	    "%c %c %c %c %c %c %c %c",
9817 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9818 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9819 
9820 	p = (uint8_t *)inq->inq_revision;
9821 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9822 	    p[0], p[1], p[2], p[3]);
9823 	p = (uint8_t *)inq->inq_revision;
9824 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9825 	    p[0], p[1], p[2], p[3]);
9826 
9827 }
9828 
9829 
9830 static void
9831 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9832 {
9833 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9834 
9835 	if (scsi_pkt == NULL)
9836 		return;
9837 	if (count != 0) {
9838 		/* saving cdb */
9839 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9840 		    SATA_ATAPI_MAX_CDB_LEN);
9841 		bcopy(scsi_pkt->pkt_cdbp,
9842 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9843 	} else {
9844 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9845 		    sts_sensedata,
9846 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9847 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9848 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9849 		    scsi_pkt->pkt_reason;
9850 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9851 		    spx->txlt_sata_pkt->satapkt_reason;
9852 
9853 		if (++sata_atapi_trace_index >= 64)
9854 			sata_atapi_trace_index = 0;
9855 	}
9856 }
9857 
9858 #endif
9859 
9860 /*
9861  * Fetch inquiry data from ATAPI device
9862  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9863  *
9864  * Note:
9865  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9866  * where the caller expects to see the inquiry data.
9867  *
9868  */
9869 
9870 static int
9871 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9872     sata_address_t *saddr, struct scsi_inquiry *inq)
9873 {
9874 	sata_pkt_txlate_t *spx;
9875 	sata_pkt_t *spkt;
9876 	struct buf *bp;
9877 	sata_drive_info_t *sdinfo;
9878 	sata_cmd_t *scmd;
9879 	int rval;
9880 	uint8_t *rqsp;
9881 	dev_info_t *dip = SATA_DIP(sata_hba);
9882 #ifdef SATA_DEBUG
9883 	char msg_buf[MAXPATHLEN];
9884 #endif
9885 	kmutex_t *cport_mutex;
9886 
9887 	ASSERT(sata_hba != NULL);
9888 
9889 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9890 	spx->txlt_sata_hba_inst = sata_hba;
9891 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9892 	spkt = sata_pkt_alloc(spx, NULL);
9893 	if (spkt == NULL) {
9894 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9895 		return (SATA_FAILURE);
9896 	}
9897 	/* address is needed now */
9898 	spkt->satapkt_device.satadev_addr = *saddr;
9899 
9900 	/* scsi_inquiry size buffer */
9901 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9902 	if (bp == NULL) {
9903 		sata_pkt_free(spx);
9904 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9905 		SATA_LOG_D((sata_hba, CE_WARN,
9906 		    "sata_get_atapi_inquiry_data: "
9907 		    "cannot allocate data buffer"));
9908 		return (SATA_FAILURE);
9909 	}
9910 	bp_mapin(bp); /* make data buffer accessible */
9911 
9912 	scmd = &spkt->satapkt_cmd;
9913 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9914 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9915 
9916 	/* Use synchronous mode */
9917 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9918 	spkt->satapkt_comp = NULL;
9919 	spkt->satapkt_time = sata_default_pkt_time;
9920 
9921 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9922 
9923 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9924 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9925 
9926 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9927 	mutex_enter(cport_mutex);
9928 	sdinfo = sata_get_device_info(sata_hba,
9929 	    &spx->txlt_sata_pkt->satapkt_device);
9930 	if (sdinfo == NULL) {
9931 		/* we have to be carefull about the disapearing device */
9932 		mutex_exit(cport_mutex);
9933 		rval = SATA_FAILURE;
9934 		goto cleanup;
9935 	}
9936 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9937 
9938 	/*
9939 	 * Set-up acdb. This works for atapi transport version 2 and later.
9940 	 */
9941 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9942 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9943 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
9944 	scmd->satacmd_acdb[1] = 0x00;
9945 	scmd->satacmd_acdb[2] = 0x00;
9946 	scmd->satacmd_acdb[3] = 0x00;
9947 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9948 	scmd->satacmd_acdb[5] = 0x00;
9949 
9950 	sata_fixed_sense_data_preset(
9951 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9952 
9953 	/* Transfer command to HBA */
9954 	if (sata_hba_start(spx, &rval) != 0) {
9955 		/* Pkt not accepted for execution */
9956 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9957 		    "sata_get_atapi_inquiry_data: "
9958 		    "Packet not accepted for execution - ret: %02x", rval);
9959 		mutex_exit(cport_mutex);
9960 		rval = SATA_FAILURE;
9961 		goto cleanup;
9962 	}
9963 	mutex_exit(cport_mutex);
9964 
9965 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9966 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9967 		    "sata_get_atapi_inquiry_data: "
9968 		    "Packet completed successfully - ret: %02x", rval);
9969 		if (spx->txlt_buf_dma_handle != NULL) {
9970 			/*
9971 			 * Sync buffer. Handle is in usual place in translate
9972 			 * struct.
9973 			 */
9974 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
9975 			    DDI_DMA_SYNC_FORCPU);
9976 			ASSERT(rval == DDI_SUCCESS);
9977 		}
9978 
9979 		if (sata_check_for_dma_error(dip, spx)) {
9980 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
9981 			rval = SATA_FAILURE;
9982 		} else {
9983 			/*
9984 			 * Normal completion - copy data into caller's buffer
9985 			 */
9986 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
9987 			    sizeof (struct scsi_inquiry));
9988 #ifdef SATA_DEBUG
9989 			if (sata_debug_flags & SATA_DBG_ATAPI) {
9990 				sata_show_inqry_data((uint8_t *)inq);
9991 			}
9992 #endif
9993 			rval = SATA_SUCCESS;
9994 		}
9995 	} else {
9996 		/*
9997 		 * Something went wrong - analyze return - check rqsense data
9998 		 */
9999 		rval = SATA_FAILURE;
10000 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10001 			/*
10002 			 * ARQ data hopefull show something other than NO SENSE
10003 			 */
10004 			rqsp = scmd->satacmd_rqsense;
10005 #ifdef SATA_DEBUG
10006 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10007 				msg_buf[0] = '\0';
10008 				(void) snprintf(msg_buf, MAXPATHLEN,
10009 				    "ATAPI packet completion reason: %02x\n"
10010 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10011 				    "          %02x %02x %02x %02x %02x %02x\n"
10012 				    "          %02x %02x %02x %02x %02x %02x",
10013 				    spkt->satapkt_reason,
10014 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10015 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10016 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10017 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10018 				    rqsp[16], rqsp[17]);
10019 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10020 				    "%s", msg_buf);
10021 			}
10022 #endif
10023 		} else {
10024 			switch (spkt->satapkt_reason) {
10025 			case SATA_PKT_PORT_ERROR:
10026 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10027 				    "sata_get_atapi_inquiry_data: "
10028 				    "packet reason: port error", NULL);
10029 				break;
10030 
10031 			case SATA_PKT_TIMEOUT:
10032 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10033 				    "sata_get_atapi_inquiry_data: "
10034 				    "packet reason: timeout", NULL);
10035 				break;
10036 
10037 			case SATA_PKT_ABORTED:
10038 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10039 				    "sata_get_atapi_inquiry_data: "
10040 				    "packet reason: aborted", NULL);
10041 				break;
10042 
10043 			case SATA_PKT_RESET:
10044 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10045 				    "sata_get_atapi_inquiry_data: "
10046 				    "packet reason: reset\n", NULL);
10047 				break;
10048 			default:
10049 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10050 				    "sata_get_atapi_inquiry_data: "
10051 				    "invalid packet reason: %02x\n",
10052 				    spkt->satapkt_reason);
10053 				break;
10054 			}
10055 		}
10056 	}
10057 cleanup:
10058 	sata_free_local_buffer(spx);
10059 	sata_pkt_free(spx);
10060 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10061 	return (rval);
10062 }
10063 
10064 
10065 
10066 
10067 
10068 #if 0
10069 #ifdef SATA_DEBUG
10070 
10071 /*
10072  * Test ATAPI packet command.
10073  * Single threaded test: send packet command in synch mode, process completion
10074  *
10075  */
10076 static void
10077 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10078 {
10079 	sata_pkt_txlate_t *spx;
10080 	sata_pkt_t *spkt;
10081 	struct buf *bp;
10082 	sata_device_t sata_device;
10083 	sata_drive_info_t *sdinfo;
10084 	sata_cmd_t *scmd;
10085 	int rval;
10086 	uint8_t *rqsp;
10087 
10088 	ASSERT(sata_hba_inst != NULL);
10089 	sata_device.satadev_addr.cport = cport;
10090 	sata_device.satadev_addr.pmport = 0;
10091 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10092 	sata_device.satadev_rev = SATA_DEVICE_REV;
10093 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10094 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10095 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10096 	if (sdinfo == NULL) {
10097 		sata_log(sata_hba_inst, CE_WARN,
10098 		    "sata_test_atapi_packet_command: "
10099 		    "no device info for cport %d",
10100 		    sata_device.satadev_addr.cport);
10101 		return;
10102 	}
10103 
10104 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10105 	spx->txlt_sata_hba_inst = sata_hba_inst;
10106 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10107 	spkt = sata_pkt_alloc(spx, NULL);
10108 	if (spkt == NULL) {
10109 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10110 		return;
10111 	}
10112 	/* address is needed now */
10113 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10114 
10115 	/* 1024k buffer */
10116 	bp = sata_alloc_local_buffer(spx, 1024);
10117 	if (bp == NULL) {
10118 		sata_pkt_free(spx);
10119 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10120 		sata_log(sata_hba_inst, CE_WARN,
10121 		    "sata_test_atapi_packet_command: "
10122 		    "cannot allocate data buffer");
10123 		return;
10124 	}
10125 	bp_mapin(bp); /* make data buffer accessible */
10126 
10127 	scmd = &spkt->satapkt_cmd;
10128 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10129 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10130 
10131 	/* Use synchronous mode */
10132 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10133 
10134 	/* Synchronous mode, no callback - may be changed by the caller */
10135 	spkt->satapkt_comp = NULL;
10136 	spkt->satapkt_time = sata_default_pkt_time;
10137 
10138 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10139 
10140 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10141 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10142 
10143 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10144 
10145 	/* Set-up acdb. */
10146 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10147 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10148 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10149 	scmd->satacmd_acdb[1] = 0x00;
10150 	scmd->satacmd_acdb[2] = 0x00;
10151 	scmd->satacmd_acdb[3] = 0x00;
10152 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10153 	scmd->satacmd_acdb[5] = 0x00;
10154 
10155 	sata_fixed_sense_data_preset(
10156 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10157 
10158 	/* Transfer command to HBA */
10159 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10160 	if (sata_hba_start(spx, &rval) != 0) {
10161 		/* Pkt not accepted for execution */
10162 		sata_log(sata_hba_inst, CE_WARN,
10163 		    "sata_test_atapi_packet_command: "
10164 		    "Packet not accepted for execution - ret: %02x", rval);
10165 		mutex_exit(
10166 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10167 		goto cleanup;
10168 	}
10169 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10170 
10171 	if (spx->txlt_buf_dma_handle != NULL) {
10172 		/*
10173 		 * Sync buffer. Handle is in usual place in translate struct.
10174 		 */
10175 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10176 		    DDI_DMA_SYNC_FORCPU);
10177 		ASSERT(rval == DDI_SUCCESS);
10178 	}
10179 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10180 		sata_log(sata_hba_inst, CE_WARN,
10181 		    "sata_test_atapi_packet_command: "
10182 		    "Packet completed successfully");
10183 		/*
10184 		 * Normal completion - show inquiry data
10185 		 */
10186 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10187 	} else {
10188 		/*
10189 		 * Something went wrong - analyze return - check rqsense data
10190 		 */
10191 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10192 			/*
10193 			 * ARQ data hopefull show something other than NO SENSE
10194 			 */
10195 			rqsp = scmd->satacmd_rqsense;
10196 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10197 			    "ATAPI packet completion reason: %02x\n"
10198 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10199 			    "          %02x %02x %02x %02x %02x %02x "
10200 			    "          %02x %02x %02x %02x %02x %02x\n",
10201 			    spkt->satapkt_reason,
10202 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10203 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10204 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10205 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10206 			    rqsp[16], rqsp[17]);
10207 		} else {
10208 			switch (spkt->satapkt_reason) {
10209 			case SATA_PKT_PORT_ERROR:
10210 				sata_log(sata_hba_inst, CE_WARN,
10211 				    "sata_test_atapi_packet_command: "
10212 				    "packet reason: port error\n");
10213 				break;
10214 
10215 			case SATA_PKT_TIMEOUT:
10216 				sata_log(sata_hba_inst, CE_WARN,
10217 				    "sata_test_atapi_packet_command: "
10218 				    "packet reason: timeout\n");
10219 				break;
10220 
10221 			case SATA_PKT_ABORTED:
10222 				sata_log(sata_hba_inst, CE_WARN,
10223 				    "sata_test_atapi_packet_command: "
10224 				    "packet reason: aborted\n");
10225 				break;
10226 
10227 			case SATA_PKT_RESET:
10228 				sata_log(sata_hba_inst, CE_WARN,
10229 				    "sata_test_atapi_packet_command: "
10230 				    "packet reason: reset\n");
10231 				break;
10232 			default:
10233 				sata_log(sata_hba_inst, CE_WARN,
10234 				    "sata_test_atapi_packet_command: "
10235 				    "invalid packet reason: %02x\n",
10236 				    spkt->satapkt_reason);
10237 				break;
10238 			}
10239 		}
10240 	}
10241 cleanup:
10242 	sata_free_local_buffer(spx);
10243 	sata_pkt_free(spx);
10244 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10245 }
10246 
10247 #endif /* SATA_DEBUG */
10248 #endif /* 1 */
10249 
10250 
10251 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10252 
10253 /*
10254  * Validate sata_tran info
10255  * SATA_FAILURE returns if structure is inconsistent or structure revision
10256  * does not match one used by the framework.
10257  *
10258  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10259  * required function pointers.
10260  * Returns SATA_FAILURE otherwise.
10261  */
10262 static int
10263 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10264 {
10265 	/*
10266 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10267 	 * of the SATA interface.
10268 	 */
10269 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10270 		sata_log(NULL, CE_WARN,
10271 		    "sata: invalid sata_hba_tran version %d for driver %s",
10272 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10273 		return (SATA_FAILURE);
10274 	}
10275 
10276 	if (dip != sata_tran->sata_tran_hba_dip) {
10277 		SATA_LOG_D((NULL, CE_WARN,
10278 		    "sata: inconsistent sata_tran_hba_dip "
10279 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10280 		return (SATA_FAILURE);
10281 	}
10282 
10283 	if (sata_tran->sata_tran_probe_port == NULL ||
10284 	    sata_tran->sata_tran_start == NULL ||
10285 	    sata_tran->sata_tran_abort == NULL ||
10286 	    sata_tran->sata_tran_reset_dport == NULL ||
10287 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10288 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10289 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10290 	    NULL) {
10291 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10292 		    "required functions"));
10293 	}
10294 	return (SATA_SUCCESS);
10295 }
10296 
10297 /*
10298  * Remove HBA instance from sata_hba_list.
10299  */
10300 static void
10301 sata_remove_hba_instance(dev_info_t *dip)
10302 {
10303 	sata_hba_inst_t	*sata_hba_inst;
10304 
10305 	mutex_enter(&sata_mutex);
10306 	for (sata_hba_inst = sata_hba_list;
10307 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10308 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10309 		if (sata_hba_inst->satahba_dip == dip)
10310 			break;
10311 	}
10312 
10313 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10314 #ifdef SATA_DEBUG
10315 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10316 		    "unknown HBA instance\n");
10317 #endif
10318 		ASSERT(FALSE);
10319 	}
10320 	if (sata_hba_inst == sata_hba_list) {
10321 		sata_hba_list = sata_hba_inst->satahba_next;
10322 		if (sata_hba_list) {
10323 			sata_hba_list->satahba_prev =
10324 			    (struct sata_hba_inst *)NULL;
10325 		}
10326 		if (sata_hba_inst == sata_hba_list_tail) {
10327 			sata_hba_list_tail = NULL;
10328 		}
10329 	} else if (sata_hba_inst == sata_hba_list_tail) {
10330 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10331 		if (sata_hba_list_tail) {
10332 			sata_hba_list_tail->satahba_next =
10333 			    (struct sata_hba_inst *)NULL;
10334 		}
10335 	} else {
10336 		sata_hba_inst->satahba_prev->satahba_next =
10337 		    sata_hba_inst->satahba_next;
10338 		sata_hba_inst->satahba_next->satahba_prev =
10339 		    sata_hba_inst->satahba_prev;
10340 	}
10341 	mutex_exit(&sata_mutex);
10342 }
10343 
10344 /*
10345  * Probe all SATA ports of the specified HBA instance.
10346  * The assumption is that there are no target and attachment point minor nodes
10347  * created by the boot subsystems, so we do not need to prune device tree.
10348  *
10349  * This function is called only from sata_hba_attach(). It does not have to
10350  * be protected by controller mutex, because the hba_attached flag is not set
10351  * yet and no one would be touching this HBA instance other than this thread.
10352  * Determines if port is active and what type of the device is attached
10353  * (if any). Allocates necessary structures for each port.
10354  *
10355  * An AP (Attachement Point) node is created for each SATA device port even
10356  * when there is no device attached.
10357  */
10358 
10359 static 	void
10360 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10361 {
10362 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10363 	int			ncport;
10364 	sata_cport_info_t 	*cportinfo;
10365 	sata_drive_info_t	*drive;
10366 	sata_device_t		sata_device;
10367 	int			rval;
10368 	dev_t			minor_number;
10369 	char			name[16];
10370 	clock_t			start_time, cur_time;
10371 
10372 	/*
10373 	 * Probe controller ports first, to find port status and
10374 	 * any port multiplier attached.
10375 	 */
10376 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10377 		/* allocate cport structure */
10378 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10379 		ASSERT(cportinfo != NULL);
10380 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10381 
10382 		mutex_enter(&cportinfo->cport_mutex);
10383 
10384 		cportinfo->cport_addr.cport = ncport;
10385 		cportinfo->cport_addr.pmport = 0;
10386 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10387 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10388 		cportinfo->cport_state |= SATA_STATE_PROBING;
10389 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10390 
10391 		/*
10392 		 * Regardless if a port is usable or not, create
10393 		 * an attachment point
10394 		 */
10395 		mutex_exit(&cportinfo->cport_mutex);
10396 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10397 		    ncport, 0, SATA_ADDR_CPORT);
10398 		(void) sprintf(name, "%d", ncport);
10399 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10400 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10401 		    DDI_SUCCESS) {
10402 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10403 			    "cannot create SATA attachment point for port %d",
10404 			    ncport);
10405 		}
10406 
10407 		/* Probe port */
10408 		start_time = ddi_get_lbolt();
10409 	reprobe_cport:
10410 		sata_device.satadev_addr.cport = ncport;
10411 		sata_device.satadev_addr.pmport = 0;
10412 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10413 		sata_device.satadev_rev = SATA_DEVICE_REV;
10414 
10415 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10416 		    (dip, &sata_device);
10417 
10418 		mutex_enter(&cportinfo->cport_mutex);
10419 		cportinfo->cport_scr = sata_device.satadev_scr;
10420 		if (rval != SATA_SUCCESS) {
10421 			/* Something went wrong? Fail the port */
10422 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10423 			mutex_exit(&cportinfo->cport_mutex);
10424 			continue;
10425 		}
10426 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10427 		cportinfo->cport_state |= SATA_STATE_PROBED;
10428 		cportinfo->cport_dev_type = sata_device.satadev_type;
10429 
10430 		cportinfo->cport_state |= SATA_STATE_READY;
10431 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10432 			mutex_exit(&cportinfo->cport_mutex);
10433 			continue;
10434 		}
10435 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10436 			/*
10437 			 * There is some device attached.
10438 			 * Allocate device info structure
10439 			 */
10440 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10441 				mutex_exit(&cportinfo->cport_mutex);
10442 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10443 				    kmem_zalloc(sizeof (sata_drive_info_t),
10444 				    KM_SLEEP);
10445 				mutex_enter(&cportinfo->cport_mutex);
10446 			}
10447 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10448 			drive->satadrv_addr = cportinfo->cport_addr;
10449 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10450 			drive->satadrv_type = cportinfo->cport_dev_type;
10451 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10452 
10453 			mutex_exit(&cportinfo->cport_mutex);
10454 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10455 			    SATA_SUCCESS) {
10456 				/*
10457 				 * Plugged device was not correctly identified.
10458 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10459 				 */
10460 				cur_time = ddi_get_lbolt();
10461 				if ((cur_time - start_time) <
10462 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10463 					/* sleep for a while */
10464 					delay(drv_usectohz(
10465 					    SATA_DEV_RETRY_DLY));
10466 					goto reprobe_cport;
10467 				}
10468 			}
10469 		} else { /* SATA_DTYPE_PMULT */
10470 			mutex_exit(&cportinfo->cport_mutex);
10471 
10472 			/* Allocate sata_pmult_info and sata_pmport_info */
10473 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10474 			    SATA_SUCCESS)
10475 				continue;
10476 
10477 			/* Log the information of the port multiplier */
10478 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10479 
10480 			/* Probe its pmports */
10481 			sata_probe_pmports(sata_hba_inst, ncport);
10482 		}
10483 	}
10484 }
10485 
10486 /*
10487  * Probe all device ports behind a port multiplier.
10488  *
10489  * PMult-related structure should be allocated before by sata_alloc_pmult().
10490  *
10491  * NOTE1: Only called from sata_probe_ports()
10492  * NOTE2: No mutex should be hold.
10493  */
10494 static void
10495 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10496 {
10497 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10498 	sata_pmult_info_t	*pmultinfo = NULL;
10499 	sata_pmport_info_t 	*pmportinfo = NULL;
10500 	sata_drive_info_t	*drive = NULL;
10501 	sata_device_t		sata_device;
10502 
10503 	clock_t			start_time, cur_time;
10504 	int			npmport;
10505 	int			rval;
10506 
10507 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10508 
10509 	/* Probe Port Multiplier ports */
10510 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10511 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10512 		start_time = ddi_get_lbolt();
10513 reprobe_pmport:
10514 		sata_device.satadev_addr.cport = ncport;
10515 		sata_device.satadev_addr.pmport = npmport;
10516 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10517 		sata_device.satadev_rev = SATA_DEVICE_REV;
10518 
10519 		/* Let HBA driver probe it. */
10520 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10521 		    (dip, &sata_device);
10522 		mutex_enter(&pmportinfo->pmport_mutex);
10523 
10524 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10525 
10526 		if (rval != SATA_SUCCESS) {
10527 			pmportinfo->pmport_state =
10528 			    SATA_PSTATE_FAILED;
10529 			mutex_exit(&pmportinfo->pmport_mutex);
10530 			continue;
10531 		}
10532 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10533 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10534 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10535 
10536 		pmportinfo->pmport_state |= SATA_STATE_READY;
10537 		if (pmportinfo->pmport_dev_type ==
10538 		    SATA_DTYPE_NONE) {
10539 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10540 			    "no device found at port %d:%d", ncport, npmport);
10541 			mutex_exit(&pmportinfo->pmport_mutex);
10542 			continue;
10543 		}
10544 		/* Port multipliers cannot be chained */
10545 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10546 		/*
10547 		 * There is something attached to Port
10548 		 * Multiplier device port
10549 		 * Allocate device info structure
10550 		 */
10551 		if (pmportinfo->pmport_sata_drive == NULL) {
10552 			mutex_exit(&pmportinfo->pmport_mutex);
10553 			pmportinfo->pmport_sata_drive =
10554 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10555 			mutex_enter(&pmportinfo->pmport_mutex);
10556 		}
10557 		drive = pmportinfo->pmport_sata_drive;
10558 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10559 		drive->satadrv_addr.pmport = npmport;
10560 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10561 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10562 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10563 
10564 		mutex_exit(&pmportinfo->pmport_mutex);
10565 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10566 
10567 		if (rval != SATA_SUCCESS) {
10568 			/*
10569 			 * Plugged device was not correctly identified.
10570 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10571 			 */
10572 			cur_time = ddi_get_lbolt();
10573 			if ((cur_time - start_time) < drv_usectohz(
10574 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10575 				/* sleep for a while */
10576 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10577 				goto reprobe_pmport;
10578 			}
10579 		}
10580 	}
10581 }
10582 
10583 /*
10584  * Add SATA device for specified HBA instance & port (SCSI target
10585  * device nodes).
10586  * This function is called (indirectly) only from sata_hba_attach().
10587  * A target node is created when there is a supported type device attached,
10588  * but may be removed if it cannot be put online.
10589  *
10590  * This function cannot be called from an interrupt context.
10591  *
10592  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10593  *
10594  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10595  * device identification failed - adding a device could be retried.
10596  *
10597  */
10598 static 	int
10599 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10600     sata_device_t *sata_device)
10601 {
10602 	sata_cport_info_t 	*cportinfo;
10603 	sata_pmult_info_t	*pminfo;
10604 	sata_pmport_info_t	*pmportinfo;
10605 	dev_info_t		*cdip;		/* child dip */
10606 	sata_address_t		*saddr = &sata_device->satadev_addr;
10607 	uint8_t			cport, pmport;
10608 	int			rval;
10609 
10610 	cport = saddr->cport;
10611 	pmport = saddr->pmport;
10612 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10613 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10614 
10615 	/*
10616 	 * Some device is attached to a controller port.
10617 	 * We rely on controllers distinquishing between no-device,
10618 	 * attached port multiplier and other kind of attached device.
10619 	 * We need to get Identify Device data and determine
10620 	 * positively the dev type before trying to attach
10621 	 * the target driver.
10622 	 */
10623 	sata_device->satadev_rev = SATA_DEVICE_REV;
10624 	switch (saddr->qual) {
10625 	case SATA_ADDR_CPORT:
10626 		/*
10627 		 * Add a non-port-multiplier device at controller port.
10628 		 */
10629 		saddr->qual = SATA_ADDR_DCPORT;
10630 
10631 		rval = sata_probe_device(sata_hba_inst, sata_device);
10632 		if (rval != SATA_SUCCESS ||
10633 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10634 			return (SATA_FAILURE);
10635 
10636 		mutex_enter(&cportinfo->cport_mutex);
10637 		sata_show_drive_info(sata_hba_inst,
10638 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10639 
10640 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10641 			/*
10642 			 * Could not determine device type or
10643 			 * a device is not supported.
10644 			 * Degrade this device to unknown.
10645 			 */
10646 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10647 			mutex_exit(&cportinfo->cport_mutex);
10648 			return (SATA_SUCCESS);
10649 		}
10650 		cportinfo->cport_dev_type = sata_device->satadev_type;
10651 		cportinfo->cport_tgtnode_clean = B_TRUE;
10652 		mutex_exit(&cportinfo->cport_mutex);
10653 
10654 		/*
10655 		 * Initialize device to the desired state. Even if it
10656 		 * fails, the device will still attach but syslog
10657 		 * will show the warning.
10658 		 */
10659 		if (sata_initialize_device(sata_hba_inst,
10660 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10661 			/* Retry */
10662 			rval = sata_initialize_device(sata_hba_inst,
10663 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10664 
10665 			if (rval == SATA_RETRY)
10666 				sata_log(sata_hba_inst, CE_WARN,
10667 				    "SATA device at port %d - "
10668 				    "default device features could not be set."
10669 				    " Device may not operate as expected.",
10670 				    cport);
10671 		}
10672 
10673 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10674 		if (cdip == NULL) {
10675 			/*
10676 			 * Attaching target node failed.
10677 			 * We retain sata_drive_info structure...
10678 			 */
10679 			return (SATA_SUCCESS);
10680 		}
10681 
10682 		mutex_enter(&cportinfo->cport_mutex);
10683 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10684 		    satadrv_state = SATA_STATE_READY;
10685 		mutex_exit(&cportinfo->cport_mutex);
10686 
10687 		break;
10688 
10689 	case SATA_ADDR_PMPORT:
10690 		saddr->qual = SATA_ADDR_DPMPORT;
10691 
10692 		mutex_enter(&cportinfo->cport_mutex);
10693 		/* It must be a Port Multiplier at the controller port */
10694 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10695 
10696 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10697 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10698 		mutex_exit(&cportinfo->cport_mutex);
10699 
10700 		rval = sata_probe_device(sata_hba_inst, sata_device);
10701 		if (rval != SATA_SUCCESS ||
10702 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10703 			return (SATA_FAILURE);
10704 		}
10705 
10706 		mutex_enter(&pmportinfo->pmport_mutex);
10707 		sata_show_drive_info(sata_hba_inst,
10708 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10709 
10710 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10711 			/*
10712 			 * Could not determine device type.
10713 			 * Degrade this device to unknown.
10714 			 */
10715 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10716 			mutex_exit(&pmportinfo->pmport_mutex);
10717 			return (SATA_SUCCESS);
10718 		}
10719 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10720 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10721 		mutex_exit(&pmportinfo->pmport_mutex);
10722 
10723 		/*
10724 		 * Initialize device to the desired state.
10725 		 * Even if it fails, the device will still
10726 		 * attach but syslog will show the warning.
10727 		 */
10728 		if (sata_initialize_device(sata_hba_inst,
10729 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10730 			/* Retry */
10731 			rval = sata_initialize_device(sata_hba_inst,
10732 			    pmportinfo->pmport_sata_drive);
10733 
10734 			if (rval == SATA_RETRY)
10735 				sata_log(sata_hba_inst, CE_WARN,
10736 				    "SATA device at port %d:%d - "
10737 				    "default device features could not be set."
10738 				    " Device may not operate as expected.",
10739 				    cport, pmport);
10740 		}
10741 
10742 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10743 		if (cdip == NULL) {
10744 			/*
10745 			 * Attaching target node failed.
10746 			 * We retain sata_drive_info structure...
10747 			 */
10748 			return (SATA_SUCCESS);
10749 		}
10750 		mutex_enter(&pmportinfo->pmport_mutex);
10751 		pmportinfo->pmport_sata_drive->satadrv_state |=
10752 		    SATA_STATE_READY;
10753 		mutex_exit(&pmportinfo->pmport_mutex);
10754 
10755 		break;
10756 
10757 	default:
10758 		return (SATA_FAILURE);
10759 	}
10760 
10761 	return (SATA_SUCCESS);
10762 }
10763 
10764 /*
10765  * Clean up target node at specific address.
10766  *
10767  * NOTE: No Mutex should be hold.
10768  */
10769 static int
10770 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10771     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10772 {
10773 	uint8_t cport, pmport, qual;
10774 	dev_info_t *tdip;
10775 
10776 	cport = sata_device->satadev_addr.cport;
10777 	pmport = sata_device->satadev_addr.pmport;
10778 	qual = sata_device->satadev_addr.qual;
10779 
10780 	if (qual == SATA_ADDR_DCPORT) {
10781 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10782 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10783 	} else {
10784 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10785 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10786 		    cport, pmport));
10787 	}
10788 
10789 	/* We are addressing attached device, not a port */
10790 	sata_device->satadev_addr.qual =
10791 	    sdinfo->satadrv_addr.qual;
10792 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10793 	    &sata_device->satadev_addr);
10794 	if (tdip != NULL && ndi_devi_offline(tdip,
10795 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10796 		/*
10797 		 * Problem :
10798 		 * The target node remained attached.
10799 		 * This happens when the device file was open
10800 		 * or a node was waiting for resources.
10801 		 * Cannot do anything about it.
10802 		 */
10803 		if (qual == SATA_ADDR_DCPORT) {
10804 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10805 			    "sata_hba_ioctl: disconnect: could "
10806 			    "not unconfigure device before "
10807 			    "disconnecting the SATA port %d",
10808 			    cport));
10809 		} else {
10810 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10811 			    "sata_hba_ioctl: disconnect: could "
10812 			    "not unconfigure device before "
10813 			    "disconnecting the SATA port %d:%d",
10814 			    cport, pmport));
10815 		}
10816 		/*
10817 		 * Set DEVICE REMOVED state in the target
10818 		 * node. It will prevent access to the device
10819 		 * even when a new device is attached, until
10820 		 * the old target node is released, removed and
10821 		 * recreated for a new  device.
10822 		 */
10823 		sata_set_device_removed(tdip);
10824 
10825 		/*
10826 		 * Instruct event daemon to try the target
10827 		 * node cleanup later.
10828 		 */
10829 		sata_set_target_node_cleanup(
10830 		    sata_hba_inst, &sata_device->satadev_addr);
10831 	}
10832 
10833 
10834 	return (SATA_SUCCESS);
10835 }
10836 
10837 
10838 /*
10839  * Create scsi target node for attached device, create node properties and
10840  * attach the node.
10841  * The node could be removed if the device onlining fails.
10842  *
10843  * A dev_info_t pointer is returned if operation is successful, NULL is
10844  * returned otherwise.
10845  */
10846 
10847 static dev_info_t *
10848 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10849 			sata_address_t *sata_addr)
10850 {
10851 	dev_info_t *cdip = NULL;
10852 	int rval;
10853 	char *nname = NULL;
10854 	char **compatible = NULL;
10855 	int ncompatible;
10856 	struct scsi_inquiry inq;
10857 	sata_device_t sata_device;
10858 	sata_drive_info_t *sdinfo;
10859 	int target;
10860 	int i;
10861 
10862 	sata_device.satadev_rev = SATA_DEVICE_REV;
10863 	sata_device.satadev_addr = *sata_addr;
10864 
10865 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10866 
10867 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10868 
10869 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10870 	    sata_addr->pmport, sata_addr->qual);
10871 
10872 	if (sdinfo == NULL) {
10873 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10874 		    sata_addr->cport)));
10875 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10876 		    "sata_create_target_node: no sdinfo for target %x",
10877 		    target));
10878 		return (NULL);
10879 	}
10880 
10881 	/*
10882 	 * create or get scsi inquiry data, expected by
10883 	 * scsi_hba_nodename_compatible_get()
10884 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10885 	 * ATAPI devices respond directly to Inquiry request.
10886 	 */
10887 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10888 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10889 		    (uint8_t *)&inq);
10890 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10891 		    sata_addr->cport)));
10892 	} else { /* Assume supported ATAPI device */
10893 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10894 		    sata_addr->cport)));
10895 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10896 		    &inq) == SATA_FAILURE)
10897 			return (NULL);
10898 		/*
10899 		 * Save supported ATAPI transport version
10900 		 */
10901 		sdinfo->satadrv_atapi_trans_ver =
10902 		    SATA_ATAPI_TRANS_VERSION(&inq);
10903 	}
10904 
10905 	/* determine the node name and compatible */
10906 	scsi_hba_nodename_compatible_get(&inq, NULL,
10907 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10908 
10909 #ifdef SATA_DEBUG
10910 	if (sata_debug_flags & SATA_DBG_NODES) {
10911 		if (nname == NULL) {
10912 			cmn_err(CE_NOTE, "sata_create_target_node: "
10913 			    "cannot determine nodename for target %d\n",
10914 			    target);
10915 		} else {
10916 			cmn_err(CE_WARN, "sata_create_target_node: "
10917 			    "target %d nodename: %s\n", target, nname);
10918 		}
10919 		if (compatible == NULL) {
10920 			cmn_err(CE_WARN,
10921 			    "sata_create_target_node: no compatible name\n");
10922 		} else {
10923 			for (i = 0; i < ncompatible; i++) {
10924 				cmn_err(CE_WARN, "sata_create_target_node: "
10925 				    "compatible name: %s\n", compatible[i]);
10926 			}
10927 		}
10928 	}
10929 #endif
10930 
10931 	/* if nodename can't be determined, log error and exit */
10932 	if (nname == NULL) {
10933 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10934 		    "sata_create_target_node: cannot determine nodename "
10935 		    "for target %d\n", target));
10936 		scsi_hba_nodename_compatible_free(nname, compatible);
10937 		return (NULL);
10938 	}
10939 	/*
10940 	 * Create scsi target node
10941 	 */
10942 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10943 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10944 	    "device-type", "scsi");
10945 
10946 	if (rval != DDI_PROP_SUCCESS) {
10947 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10948 		    "updating device_type prop failed %d", rval));
10949 		goto fail;
10950 	}
10951 
10952 	/*
10953 	 * Create target node properties: target & lun
10954 	 */
10955 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10956 	if (rval != DDI_PROP_SUCCESS) {
10957 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10958 		    "updating target prop failed %d", rval));
10959 		goto fail;
10960 	}
10961 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
10962 	if (rval != DDI_PROP_SUCCESS) {
10963 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10964 		    "updating target prop failed %d", rval));
10965 		goto fail;
10966 	}
10967 
10968 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10969 		/*
10970 		 * Add "variant" property
10971 		 */
10972 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10973 		    "variant", "atapi");
10974 		if (rval != DDI_PROP_SUCCESS) {
10975 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10976 			    "sata_create_target_node: variant atapi "
10977 			    "property could not be created: %d", rval));
10978 			goto fail;
10979 		}
10980 	}
10981 	/* decorate the node with compatible */
10982 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
10983 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
10984 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10985 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
10986 		    (void *)cdip));
10987 		goto fail;
10988 	}
10989 
10990 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10991 		/*
10992 		 * Add "sata-phy" property
10993 		 */
10994 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
10995 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
10996 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10997 			    "sata_create_target_node: failed to create "
10998 			    "\"sata-phy\" property: port %d",
10999 			    sata_addr->cport));
11000 		}
11001 	}
11002 
11003 
11004 	/*
11005 	 * Now, try to attach the driver. If probing of the device fails,
11006 	 * the target node may be removed
11007 	 */
11008 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11009 
11010 	scsi_hba_nodename_compatible_free(nname, compatible);
11011 
11012 	if (rval == NDI_SUCCESS)
11013 		return (cdip);
11014 
11015 	/* target node was removed - are we sure? */
11016 	return (NULL);
11017 
11018 fail:
11019 	scsi_hba_nodename_compatible_free(nname, compatible);
11020 	ddi_prop_remove_all(cdip);
11021 	rval = ndi_devi_free(cdip);
11022 	if (rval != NDI_SUCCESS) {
11023 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11024 		    "node removal failed %d", rval));
11025 	}
11026 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11027 	    "cannot create target node for SATA device at port %d",
11028 	    sata_addr->cport);
11029 	return (NULL);
11030 }
11031 
11032 /*
11033  * Remove a target node.
11034  */
11035 static void
11036 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11037 			sata_address_t *sata_addr)
11038 {
11039 	dev_info_t *tdip;
11040 	uint8_t cport = sata_addr->cport;
11041 	uint8_t pmport = sata_addr->pmport;
11042 	uint8_t qual = sata_addr->qual;
11043 
11044 	/* Note the sata daemon uses the address of the port/pmport */
11045 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11046 
11047 	/* Remove target node */
11048 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11049 	if (tdip != NULL) {
11050 		/*
11051 		 * Target node exists.  Unconfigure device
11052 		 * then remove the target node (one ndi
11053 		 * operation).
11054 		 */
11055 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11056 			/*
11057 			 * PROBLEM - no device, but target node remained. This
11058 			 * happens when the file was open or node was waiting
11059 			 * for resources.
11060 			 */
11061 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11062 			    "sata_remove_target_node: "
11063 			    "Failed to remove target node for "
11064 			    "detached SATA device."));
11065 			/*
11066 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11067 			 * re-check first that the node still exists.
11068 			 */
11069 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11070 			    cport, pmport);
11071 			if (tdip != NULL) {
11072 				sata_set_device_removed(tdip);
11073 				/*
11074 				 * Instruct event daemon to retry the cleanup
11075 				 * later.
11076 				 */
11077 				sata_set_target_node_cleanup(sata_hba_inst,
11078 				    sata_addr);
11079 			}
11080 		}
11081 
11082 		if (qual == SATA_ADDR_CPORT)
11083 			sata_log(sata_hba_inst, CE_WARN,
11084 			    "SATA device detached at port %d", cport);
11085 		else
11086 			sata_log(sata_hba_inst, CE_WARN,
11087 			    "SATA device detached at port %d:%d",
11088 			    cport, pmport);
11089 	}
11090 #ifdef SATA_DEBUG
11091 	else {
11092 		if (qual == SATA_ADDR_CPORT)
11093 			sata_log(sata_hba_inst, CE_WARN,
11094 			    "target node not found at port %d", cport);
11095 		else
11096 			sata_log(sata_hba_inst, CE_WARN,
11097 			    "target node not found at port %d:%d",
11098 			    cport, pmport);
11099 	}
11100 #endif
11101 }
11102 
11103 
11104 /*
11105  * Re-probe sata port, check for a device and attach info
11106  * structures when necessary. Identify Device data is fetched, if possible.
11107  * Assumption: sata address is already validated.
11108  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11109  * the presence of a device and its type.
11110  *
11111  * flag arg specifies that the function should try multiple times to identify
11112  * device type and to initialize it, or it should return immediately on failure.
11113  * SATA_DEV_IDENTIFY_RETRY - retry
11114  * SATA_DEV_IDENTIFY_NORETRY - no retry
11115  *
11116  * SATA_FAILURE is returned if one of the operations failed.
11117  *
11118  * This function cannot be called in interrupt context - it may sleep.
11119  *
11120  * Note: Port multiplier is supported.
11121  */
11122 static int
11123 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11124     int flag)
11125 {
11126 	sata_cport_info_t *cportinfo;
11127 	sata_pmult_info_t *pmultinfo;
11128 	sata_drive_info_t *sdinfo, *osdinfo;
11129 	boolean_t init_device = B_FALSE;
11130 	int prev_device_type = SATA_DTYPE_NONE;
11131 	int prev_device_settings = 0;
11132 	int prev_device_state = 0;
11133 	clock_t start_time;
11134 	int retry = B_FALSE;
11135 	uint8_t cport = sata_device->satadev_addr.cport;
11136 	int rval_probe, rval_init;
11137 
11138 	/*
11139 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11140 	 */
11141 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11142 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11143 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11144 
11145 	/* We only care about host sata cport for now */
11146 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11147 	    sata_device->satadev_addr.cport);
11148 
11149 	/*
11150 	 * If a port multiplier was previously attached (we have no idea it
11151 	 * still there or not), sata_reprobe_pmult() will handle it.
11152 	 */
11153 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11154 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11155 
11156 	/* Store sata_drive_info when a non-pmult device was attached. */
11157 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11158 	if (osdinfo != NULL) {
11159 		/*
11160 		 * We are re-probing port with a previously attached device.
11161 		 * Save previous device type and settings.
11162 		 */
11163 		prev_device_type = cportinfo->cport_dev_type;
11164 		prev_device_settings = osdinfo->satadrv_settings;
11165 		prev_device_state = osdinfo->satadrv_state;
11166 	}
11167 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11168 		start_time = ddi_get_lbolt();
11169 		retry = B_TRUE;
11170 	}
11171 retry_probe:
11172 
11173 	/* probe port */
11174 	mutex_enter(&cportinfo->cport_mutex);
11175 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11176 	cportinfo->cport_state |= SATA_STATE_PROBING;
11177 	mutex_exit(&cportinfo->cport_mutex);
11178 
11179 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11180 	    (SATA_DIP(sata_hba_inst), sata_device);
11181 
11182 	mutex_enter(&cportinfo->cport_mutex);
11183 	if (rval_probe != SATA_SUCCESS) {
11184 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11185 		mutex_exit(&cportinfo->cport_mutex);
11186 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11187 		    "SATA port %d probing failed",
11188 		    cportinfo->cport_addr.cport));
11189 		return (SATA_FAILURE);
11190 	}
11191 
11192 	/*
11193 	 * update sata port state and set device type
11194 	 */
11195 	sata_update_port_info(sata_hba_inst, sata_device);
11196 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11197 
11198 	/*
11199 	 * Sanity check - Port is active? Is the link active?
11200 	 * Is there any device attached?
11201 	 */
11202 	if ((cportinfo->cport_state &
11203 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11204 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11205 	    SATA_PORT_DEVLINK_UP) {
11206 		/*
11207 		 * Port in non-usable state or no link active/no device.
11208 		 * Free info structure if necessary (direct attached drive
11209 		 * only, for now!
11210 		 */
11211 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11212 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11213 		/* Add here differentiation for device attached or not */
11214 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11215 		mutex_exit(&cportinfo->cport_mutex);
11216 		if (sdinfo != NULL)
11217 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11218 		return (SATA_SUCCESS);
11219 	}
11220 
11221 	cportinfo->cport_state |= SATA_STATE_READY;
11222 	cportinfo->cport_state |= SATA_STATE_PROBED;
11223 
11224 	cportinfo->cport_dev_type = sata_device->satadev_type;
11225 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11226 
11227 	/*
11228 	 * If we are re-probing the port, there may be
11229 	 * sata_drive_info structure attached
11230 	 */
11231 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11232 
11233 		/*
11234 		 * There is no device, so remove device info structure,
11235 		 * if necessary.
11236 		 */
11237 		/* Device change: Drive -> None */
11238 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11239 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11240 		if (sdinfo != NULL) {
11241 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11242 			sata_log(sata_hba_inst, CE_WARN,
11243 			    "SATA device detached "
11244 			    "from port %d", cportinfo->cport_addr.cport);
11245 		}
11246 		mutex_exit(&cportinfo->cport_mutex);
11247 		return (SATA_SUCCESS);
11248 
11249 	}
11250 
11251 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11252 
11253 		/* Device (may) change: Drive -> Drive */
11254 		if (sdinfo == NULL) {
11255 			/*
11256 			 * There is some device attached, but there is
11257 			 * no sata_drive_info structure - allocate one
11258 			 */
11259 			mutex_exit(&cportinfo->cport_mutex);
11260 			sdinfo = kmem_zalloc(
11261 			    sizeof (sata_drive_info_t), KM_SLEEP);
11262 			mutex_enter(&cportinfo->cport_mutex);
11263 			/*
11264 			 * Recheck, that the port state did not change when we
11265 			 * released mutex.
11266 			 */
11267 			if (cportinfo->cport_state & SATA_STATE_READY) {
11268 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11269 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11270 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11271 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11272 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11273 			} else {
11274 				/*
11275 				 * Port is not in ready state, we
11276 				 * cannot attach a device.
11277 				 */
11278 				mutex_exit(&cportinfo->cport_mutex);
11279 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11280 				return (SATA_SUCCESS);
11281 			}
11282 			/*
11283 			 * Since we are adding device, presumably new one,
11284 			 * indicate that it  should be initalized,
11285 			 * as well as some internal framework states).
11286 			 */
11287 			init_device = B_TRUE;
11288 		}
11289 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11290 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11291 	} else {
11292 		/* Device change: Drive -> PMult */
11293 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11294 		if (sdinfo != NULL) {
11295 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11296 			sata_log(sata_hba_inst, CE_WARN,
11297 			    "SATA device detached "
11298 			    "from port %d", cportinfo->cport_addr.cport);
11299 		}
11300 
11301 		sata_log(sata_hba_inst, CE_WARN,
11302 		    "SATA port multiplier detected at port %d",
11303 		    cportinfo->cport_addr.cport);
11304 
11305 		mutex_exit(&cportinfo->cport_mutex);
11306 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11307 		    SATA_SUCCESS)
11308 			return (SATA_FAILURE);
11309 		sata_show_pmult_info(sata_hba_inst, sata_device);
11310 		mutex_enter(&cportinfo->cport_mutex);
11311 
11312 		/*
11313 		 * Mark all the port multiplier port behind the port
11314 		 * multiplier behind with link events, so that the sata daemon
11315 		 * will update their status.
11316 		 */
11317 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11318 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11319 		mutex_exit(&cportinfo->cport_mutex);
11320 		return (SATA_SUCCESS);
11321 	}
11322 	mutex_exit(&cportinfo->cport_mutex);
11323 
11324 	/*
11325 	 * Figure out what kind of device we are really
11326 	 * dealing with. Failure of identifying device does not fail this
11327 	 * function.
11328 	 */
11329 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11330 	rval_init = SATA_FAILURE;
11331 	mutex_enter(&cportinfo->cport_mutex);
11332 	if (rval_probe == SATA_SUCCESS) {
11333 		/*
11334 		 * If we are dealing with the same type of a device as before,
11335 		 * restore its settings flags.
11336 		 */
11337 		if (osdinfo != NULL &&
11338 		    sata_device->satadev_type == prev_device_type)
11339 			sdinfo->satadrv_settings = prev_device_settings;
11340 
11341 		mutex_exit(&cportinfo->cport_mutex);
11342 		rval_init = SATA_SUCCESS;
11343 		/* Set initial device features, if necessary */
11344 		if (init_device == B_TRUE) {
11345 			rval_init = sata_initialize_device(sata_hba_inst,
11346 			    sdinfo);
11347 		}
11348 		if (rval_init == SATA_SUCCESS)
11349 			return (rval_init);
11350 		/* else we will retry if retry was asked for */
11351 
11352 	} else {
11353 		/*
11354 		 * If there was some device info before we probe the device,
11355 		 * restore previous device setting, so we can retry from scratch
11356 		 * later. Providing, of course, that device has not disapear
11357 		 * during probing process.
11358 		 */
11359 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11360 			if (osdinfo != NULL) {
11361 				cportinfo->cport_dev_type = prev_device_type;
11362 				sdinfo->satadrv_type = prev_device_type;
11363 				sdinfo->satadrv_state = prev_device_state;
11364 			}
11365 		} else {
11366 			/* device is gone */
11367 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11368 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11369 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11370 			mutex_exit(&cportinfo->cport_mutex);
11371 			return (SATA_SUCCESS);
11372 		}
11373 		mutex_exit(&cportinfo->cport_mutex);
11374 	}
11375 
11376 	if (retry) {
11377 		clock_t cur_time = ddi_get_lbolt();
11378 		/*
11379 		 * A device was not successfully identified or initialized.
11380 		 * Track retry time for device identification.
11381 		 */
11382 		if ((cur_time - start_time) <
11383 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11384 			/* sleep for a while */
11385 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11386 			goto retry_probe;
11387 		}
11388 		/* else no more retries */
11389 		mutex_enter(&cportinfo->cport_mutex);
11390 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11391 			if (rval_init == SATA_RETRY) {
11392 				/*
11393 				 * Setting drive features have failed, but
11394 				 * because the drive is still accessible,
11395 				 * keep it and emit a warning message.
11396 				 */
11397 				sata_log(sata_hba_inst, CE_WARN,
11398 				    "SATA device at port %d - desired "
11399 				    "drive features could not be set. "
11400 				    "Device may not operate as expected.",
11401 				    cportinfo->cport_addr.cport);
11402 			} else {
11403 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11404 				    satadrv_state = SATA_DSTATE_FAILED;
11405 			}
11406 		}
11407 		mutex_exit(&cportinfo->cport_mutex);
11408 	}
11409 	return (SATA_SUCCESS);
11410 }
11411 
11412 /*
11413  * Reprobe a controller port that connected to a port multiplier.
11414  *
11415  * NOTE: No Mutex should be hold.
11416  */
11417 static int
11418 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11419     int flag)
11420 {
11421 	_NOTE(ARGUNUSED(flag))
11422 	sata_cport_info_t *cportinfo;
11423 	sata_pmult_info_t *pmultinfo;
11424 	uint8_t cport = sata_device->satadev_addr.cport;
11425 	int rval_probe;
11426 
11427 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11428 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11429 
11430 	/* probe port */
11431 	mutex_enter(&cportinfo->cport_mutex);
11432 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11433 	cportinfo->cport_state |= SATA_STATE_PROBING;
11434 	mutex_exit(&cportinfo->cport_mutex);
11435 
11436 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11437 	    (SATA_DIP(sata_hba_inst), sata_device);
11438 
11439 	mutex_enter(&cportinfo->cport_mutex);
11440 	if (rval_probe != SATA_SUCCESS) {
11441 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11442 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11443 		    "SATA port %d probing failed", cport));
11444 		sata_log(sata_hba_inst, CE_WARN,
11445 		    "SATA port multiplier detached at port %d", cport);
11446 		mutex_exit(&cportinfo->cport_mutex);
11447 		sata_free_pmult(sata_hba_inst, sata_device);
11448 		return (SATA_FAILURE);
11449 	}
11450 
11451 	/*
11452 	 * update sata port state and set device type
11453 	 */
11454 	sata_update_port_info(sata_hba_inst, sata_device);
11455 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11456 	cportinfo->cport_state |= SATA_STATE_PROBED;
11457 
11458 	/*
11459 	 * Sanity check - Port is active? Is the link active?
11460 	 * Is there any device attached?
11461 	 */
11462 	if ((cportinfo->cport_state &
11463 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11464 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11465 	    SATA_PORT_DEVLINK_UP ||
11466 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11467 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11468 		mutex_exit(&cportinfo->cport_mutex);
11469 		sata_free_pmult(sata_hba_inst, sata_device);
11470 		sata_log(sata_hba_inst, CE_WARN,
11471 		    "SATA port multiplier detached at port %d", cport);
11472 		return (SATA_SUCCESS);
11473 	}
11474 
11475 	/*
11476 	 * Device changed: PMult -> Non-PMult
11477 	 *
11478 	 * This situation is uncommon, most possibly being caused by errors
11479 	 * after which the port multiplier is not correct initialized and
11480 	 * recognized. In that case the new device will be marked as unknown
11481 	 * and will not be automatically probed in this routine. Instead
11482 	 * system administrator could manually restart it via cfgadm(1M).
11483 	 */
11484 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11485 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11486 		mutex_exit(&cportinfo->cport_mutex);
11487 		sata_free_pmult(sata_hba_inst, sata_device);
11488 		sata_log(sata_hba_inst, CE_WARN,
11489 		    "SATA port multiplier detached at port %d", cport);
11490 		return (SATA_FAILURE);
11491 	}
11492 
11493 	/*
11494 	 * Now we know it is a port multiplier. However, if this is not the
11495 	 * previously attached port multiplier - they may have different
11496 	 * pmport numbers - we need to re-allocate data structures for every
11497 	 * pmport and drive.
11498 	 *
11499 	 * Port multipliers of the same model have identical values in these
11500 	 * registers, so it is still necessary to update the information of
11501 	 * all drives attached to the previous port multiplier afterwards.
11502 	 */
11503 	/* Device changed: PMult -> another PMult */
11504 	mutex_exit(&cportinfo->cport_mutex);
11505 	sata_free_pmult(sata_hba_inst, sata_device);
11506 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11507 		return (SATA_FAILURE);
11508 	mutex_enter(&cportinfo->cport_mutex);
11509 
11510 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11511 	    "SATA port multiplier [changed] at port %d", cport);
11512 	sata_log(sata_hba_inst, CE_WARN,
11513 	    "SATA port multiplier detected at port %d", cport);
11514 
11515 	/*
11516 	 * Mark all the port multiplier port behind the port
11517 	 * multiplier behind with link events, so that the sata daemon
11518 	 * will update their status.
11519 	 */
11520 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11521 	mutex_exit(&cportinfo->cport_mutex);
11522 
11523 	return (SATA_SUCCESS);
11524 }
11525 
11526 /*
11527  * Re-probe a port multiplier port, check for a device and attach info
11528  * structures when necessary. Identify Device data is fetched, if possible.
11529  * Assumption: sata address is already validated as port multiplier port.
11530  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11531  * the presence of a device and its type.
11532  *
11533  * flag arg specifies that the function should try multiple times to identify
11534  * device type and to initialize it, or it should return immediately on failure.
11535  * SATA_DEV_IDENTIFY_RETRY - retry
11536  * SATA_DEV_IDENTIFY_NORETRY - no retry
11537  *
11538  * SATA_FAILURE is returned if one of the operations failed.
11539  *
11540  * This function cannot be called in interrupt context - it may sleep.
11541  *
11542  * NOTE: Should be only called by sata_probe_port() in case target port is a
11543  *       port multiplier port.
11544  * NOTE: No Mutex should be hold.
11545  */
11546 static int
11547 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11548     int flag)
11549 {
11550 	sata_cport_info_t *cportinfo = NULL;
11551 	sata_pmport_info_t *pmportinfo = NULL;
11552 	sata_drive_info_t *sdinfo, *osdinfo;
11553 	sata_device_t sdevice;
11554 	boolean_t init_device = B_FALSE;
11555 	int prev_device_type = SATA_DTYPE_NONE;
11556 	int prev_device_settings = 0;
11557 	int prev_device_state = 0;
11558 	clock_t start_time;
11559 	uint8_t cport = sata_device->satadev_addr.cport;
11560 	uint8_t pmport = sata_device->satadev_addr.pmport;
11561 	int rval;
11562 
11563 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11564 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11565 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11566 
11567 	if (osdinfo != NULL) {
11568 		/*
11569 		 * We are re-probing port with a previously attached device.
11570 		 * Save previous device type and settings.
11571 		 */
11572 		prev_device_type = pmportinfo->pmport_dev_type;
11573 		prev_device_settings = osdinfo->satadrv_settings;
11574 		prev_device_state = osdinfo->satadrv_state;
11575 	}
11576 
11577 	start_time = ddi_get_lbolt();
11578 
11579 	/* check parent status */
11580 	mutex_enter(&cportinfo->cport_mutex);
11581 	if ((cportinfo->cport_state &
11582 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11583 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11584 	    SATA_PORT_DEVLINK_UP) {
11585 		mutex_exit(&cportinfo->cport_mutex);
11586 		return (SATA_FAILURE);
11587 	}
11588 	mutex_exit(&cportinfo->cport_mutex);
11589 
11590 retry_probe_pmport:
11591 
11592 	/* probe port */
11593 	mutex_enter(&pmportinfo->pmport_mutex);
11594 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11595 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11596 	mutex_exit(&pmportinfo->pmport_mutex);
11597 
11598 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11599 	    (SATA_DIP(sata_hba_inst), sata_device);
11600 
11601 	/* might need retry because we cannot touch registers. */
11602 	if (rval == SATA_FAILURE) {
11603 		mutex_enter(&pmportinfo->pmport_mutex);
11604 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11605 		mutex_exit(&pmportinfo->pmport_mutex);
11606 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11607 		    "SATA port %d:%d probing failed",
11608 		    cport, pmport));
11609 		return (SATA_FAILURE);
11610 	} else if (rval == SATA_RETRY) {
11611 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11612 		    "SATA port %d:%d probing failed, retrying...",
11613 		    cport, pmport));
11614 		clock_t cur_time = ddi_get_lbolt();
11615 		/*
11616 		 * A device was not successfully identified or initialized.
11617 		 * Track retry time for device identification.
11618 		 */
11619 		if ((cur_time - start_time) <
11620 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11621 			/* sleep for a while */
11622 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11623 			goto retry_probe_pmport;
11624 		} else {
11625 			mutex_enter(&pmportinfo->pmport_mutex);
11626 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11627 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11628 				    satadrv_state = SATA_DSTATE_FAILED;
11629 			mutex_exit(&pmportinfo->pmport_mutex);
11630 			return (SATA_SUCCESS);
11631 		}
11632 	}
11633 
11634 	/*
11635 	 * Sanity check - Controller port is active? Is the link active?
11636 	 * Is it still a port multiplier?
11637 	 */
11638 	if ((cportinfo->cport_state &
11639 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11640 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11641 	    SATA_PORT_DEVLINK_UP ||
11642 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11643 		/*
11644 		 * Port in non-usable state or no link active/no
11645 		 * device. Free info structure.
11646 		 */
11647 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11648 
11649 		sdevice.satadev_addr.cport = cport;
11650 		sdevice.satadev_addr.pmport = pmport;
11651 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11652 		mutex_exit(&cportinfo->cport_mutex);
11653 
11654 		sata_free_pmult(sata_hba_inst, &sdevice);
11655 		return (SATA_FAILURE);
11656 	}
11657 
11658 	/* SATA_SUCCESS NOW */
11659 	/*
11660 	 * update sata port state and set device type
11661 	 */
11662 	mutex_enter(&pmportinfo->pmport_mutex);
11663 	sata_update_pmport_info(sata_hba_inst, sata_device);
11664 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11665 
11666 	/*
11667 	 * Sanity check - Port is active? Is the link active?
11668 	 * Is there any device attached?
11669 	 */
11670 	if ((pmportinfo->pmport_state &
11671 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11672 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11673 	    SATA_PORT_DEVLINK_UP) {
11674 		/*
11675 		 * Port in non-usable state or no link active/no device.
11676 		 * Free info structure if necessary (direct attached drive
11677 		 * only, for now!
11678 		 */
11679 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11680 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11681 		/* Add here differentiation for device attached or not */
11682 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11683 		mutex_exit(&pmportinfo->pmport_mutex);
11684 		if (sdinfo != NULL)
11685 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11686 		return (SATA_SUCCESS);
11687 	}
11688 
11689 	pmportinfo->pmport_state |= SATA_STATE_READY;
11690 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11691 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11692 
11693 	/*
11694 	 * If we are re-probing the port, there may be
11695 	 * sata_drive_info structure attached
11696 	 * (or sata_pm_info, if PMult is supported).
11697 	 */
11698 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11699 		/*
11700 		 * There is no device, so remove device info structure,
11701 		 * if necessary.
11702 		 */
11703 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11704 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11705 		if (sdinfo != NULL) {
11706 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11707 			sata_log(sata_hba_inst, CE_WARN,
11708 			    "SATA device detached from port %d:%d",
11709 			    cport, pmport);
11710 		}
11711 		mutex_exit(&pmportinfo->pmport_mutex);
11712 		return (SATA_SUCCESS);
11713 	}
11714 
11715 	/* this should not be a pmult */
11716 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11717 	if (sdinfo == NULL) {
11718 		/*
11719 		 * There is some device attached, but there is
11720 		 * no sata_drive_info structure - allocate one
11721 		 */
11722 		mutex_exit(&pmportinfo->pmport_mutex);
11723 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11724 		    KM_SLEEP);
11725 		mutex_enter(&pmportinfo->pmport_mutex);
11726 		/*
11727 		 * Recheck, that the port state did not change when we
11728 		 * released mutex.
11729 		 */
11730 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11731 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11732 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11733 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11734 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11735 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11736 		} else {
11737 			/*
11738 			 * Port is not in ready state, we
11739 			 * cannot attach a device.
11740 			 */
11741 			mutex_exit(&pmportinfo->pmport_mutex);
11742 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11743 			return (SATA_SUCCESS);
11744 		}
11745 		/*
11746 		 * Since we are adding device, presumably new one,
11747 		 * indicate that it  should be initalized,
11748 		 * as well as some internal framework states).
11749 		 */
11750 		init_device = B_TRUE;
11751 	}
11752 
11753 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11754 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11755 
11756 	mutex_exit(&pmportinfo->pmport_mutex);
11757 	/*
11758 	 * Figure out what kind of device we are really
11759 	 * dealing with.
11760 	 */
11761 	rval = sata_probe_device(sata_hba_inst, sata_device);
11762 
11763 	mutex_enter(&pmportinfo->pmport_mutex);
11764 	if (rval == SATA_SUCCESS) {
11765 		/*
11766 		 * If we are dealing with the same type of a device as before,
11767 		 * restore its settings flags.
11768 		 */
11769 		if (osdinfo != NULL &&
11770 		    sata_device->satadev_type == prev_device_type)
11771 			sdinfo->satadrv_settings = prev_device_settings;
11772 
11773 		mutex_exit(&pmportinfo->pmport_mutex);
11774 		/* Set initial device features, if necessary */
11775 		if (init_device == B_TRUE) {
11776 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11777 		}
11778 		if (rval == SATA_SUCCESS)
11779 			return (rval);
11780 	} else {
11781 		/*
11782 		 * If there was some device info before we probe the device,
11783 		 * restore previous device setting, so we can retry from scratch
11784 		 * later. Providing, of course, that device has not disappeared
11785 		 * during probing process.
11786 		 */
11787 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11788 			if (osdinfo != NULL) {
11789 				pmportinfo->pmport_dev_type = prev_device_type;
11790 				sdinfo->satadrv_type = prev_device_type;
11791 				sdinfo->satadrv_state = prev_device_state;
11792 			}
11793 		} else {
11794 			/* device is gone */
11795 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11796 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11797 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11798 			mutex_exit(&pmportinfo->pmport_mutex);
11799 			return (SATA_SUCCESS);
11800 		}
11801 		mutex_exit(&pmportinfo->pmport_mutex);
11802 	}
11803 
11804 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11805 		clock_t cur_time = ddi_get_lbolt();
11806 		/*
11807 		 * A device was not successfully identified or initialized.
11808 		 * Track retry time for device identification.
11809 		 */
11810 		if ((cur_time - start_time) <
11811 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11812 			/* sleep for a while */
11813 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11814 			goto retry_probe_pmport;
11815 		} else {
11816 			mutex_enter(&pmportinfo->pmport_mutex);
11817 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11818 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11819 				    satadrv_state = SATA_DSTATE_FAILED;
11820 			mutex_exit(&pmportinfo->pmport_mutex);
11821 		}
11822 	}
11823 	return (SATA_SUCCESS);
11824 }
11825 
11826 /*
11827  * Allocated related structure for a port multiplier and its device ports
11828  *
11829  * Port multiplier should be ready and probed, and related information like
11830  * the number of the device ports should be store in sata_device_t.
11831  *
11832  * NOTE: No Mutex should be hold.
11833  */
11834 static int
11835 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11836 {
11837 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11838 	sata_cport_info_t *cportinfo = NULL;
11839 	sata_pmult_info_t *pmultinfo = NULL;
11840 	sata_pmport_info_t *pmportinfo = NULL;
11841 	sata_device_t sd;
11842 	dev_t minor_number;
11843 	char name[16];
11844 	uint8_t cport = sata_device->satadev_addr.cport;
11845 	int rval;
11846 	int npmport;
11847 
11848 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11849 
11850 	/* This function might be called while a port-mult is hot-plugged. */
11851 	mutex_enter(&cportinfo->cport_mutex);
11852 
11853 	/* dev_type's not updated when get called from sata_reprobe_port() */
11854 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11855 		/* Create a pmult_info structure */
11856 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11857 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11858 	}
11859 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11860 
11861 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11862 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11863 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11864 
11865 	/*
11866 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11867 	 * The HBA driver should initialize and register the port multiplier,
11868 	 * sata_register_pmult() will fill following fields,
11869 	 *   + sata_pmult_info.pmult_gscr
11870 	 *   + sata_pmult_info.pmult_num_dev_ports
11871 	 */
11872 	sd.satadev_addr = sata_device->satadev_addr;
11873 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11874 	mutex_exit(&cportinfo->cport_mutex);
11875 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11876 	    (SATA_DIP(sata_hba_inst), &sd);
11877 	mutex_enter(&cportinfo->cport_mutex);
11878 
11879 	if (rval != SATA_SUCCESS ||
11880 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11881 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11882 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11883 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11884 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11885 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11886 		mutex_exit(&cportinfo->cport_mutex);
11887 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11888 		    "sata_alloc_pmult: failed to initialize pmult "
11889 		    "at port %d.", cport)
11890 		return (SATA_FAILURE);
11891 	}
11892 
11893 	/* Initialize pmport_info structure */
11894 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11895 	    npmport++) {
11896 
11897 		/* if everything is allocated, skip */
11898 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11899 			continue;
11900 
11901 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11902 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11903 		mutex_exit(&cportinfo->cport_mutex);
11904 
11905 		mutex_enter(&pmportinfo->pmport_mutex);
11906 		pmportinfo->pmport_addr.cport = cport;
11907 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11908 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11909 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11910 		mutex_exit(&pmportinfo->pmport_mutex);
11911 
11912 		mutex_enter(&cportinfo->cport_mutex);
11913 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11914 
11915 		/* Create an attachment point */
11916 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11917 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11918 		(void) sprintf(name, "%d.%d", cport, npmport);
11919 
11920 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11921 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11922 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11923 			    "cannot create SATA attachment point for "
11924 			    "port %d:%d", cport, npmport);
11925 		}
11926 	}
11927 
11928 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11929 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11930 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11931 
11932 	mutex_exit(&cportinfo->cport_mutex);
11933 	return (SATA_SUCCESS);
11934 }
11935 
11936 /*
11937  * Free data structures when a port multiplier is removed.
11938  *
11939  * NOTE: No Mutex should be hold.
11940  */
11941 static void
11942 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11943 {
11944 	sata_cport_info_t *cportinfo;
11945 	sata_pmult_info_t *pmultinfo;
11946 	sata_pmport_info_t *pmportinfo;
11947 	sata_device_t pmport_device;
11948 	sata_drive_info_t *sdinfo;
11949 	dev_info_t *tdip;
11950 	char name[16];
11951 	uint8_t cport = sata_device->satadev_addr.cport;
11952 	int npmport;
11953 
11954 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11955 
11956 	/* This function might be called while port-mult is hot plugged. */
11957 	mutex_enter(&cportinfo->cport_mutex);
11958 
11959 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11960 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11961 	ASSERT(pmultinfo != NULL);
11962 
11963 	/* Free pmport_info structure */
11964 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11965 	    npmport++) {
11966 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
11967 		if (pmportinfo == NULL)
11968 			continue;
11969 		mutex_exit(&cportinfo->cport_mutex);
11970 
11971 		mutex_enter(&pmportinfo->pmport_mutex);
11972 		sdinfo = pmportinfo->pmport_sata_drive;
11973 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11974 		mutex_exit(&pmportinfo->pmport_mutex);
11975 
11976 		/* Remove attachment point. */
11977 		name[0] = '\0';
11978 		(void) sprintf(name, "%d.%d", cport, npmport);
11979 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
11980 		sata_log(sata_hba_inst, CE_NOTE,
11981 		    "Remove attachment point of port %d:%d",
11982 		    cport, npmport);
11983 
11984 		/*
11985 		 * Rumove target node
11986 		 */
11987 		bzero(&pmport_device, sizeof (sata_device_t));
11988 		pmport_device.satadev_rev = SATA_DEVICE_REV;
11989 		pmport_device.satadev_addr.cport = cport;
11990 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
11991 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
11992 
11993 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11994 		    &(pmport_device.satadev_addr));
11995 		if (tdip != NULL && ndi_devi_offline(tdip,
11996 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11997 			/*
11998 			 * Problem :
11999 			 * The target node remained attached.
12000 			 * This happens when the device file was open
12001 			 * or a node was waiting for resources.
12002 			 * Cannot do anything about it.
12003 			 */
12004 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12005 			    "sata_free_pmult: could not unconfigure device "
12006 			    "before disconnecting the SATA port %d:%d",
12007 			    cport, npmport));
12008 
12009 			/*
12010 			 * Set DEVICE REMOVED state in the target
12011 			 * node. It will prevent access to the device
12012 			 * even when a new device is attached, until
12013 			 * the old target node is released, removed and
12014 			 * recreated for a new  device.
12015 			 */
12016 			sata_set_device_removed(tdip);
12017 
12018 			/*
12019 			 * Instruct event daemon to try the target
12020 			 * node cleanup later.
12021 			 */
12022 			sata_set_target_node_cleanup(
12023 			    sata_hba_inst, &(pmport_device.satadev_addr));
12024 
12025 		}
12026 		mutex_enter(&cportinfo->cport_mutex);
12027 
12028 		/*
12029 		 * Add here differentiation for device attached or not
12030 		 */
12031 		if (sdinfo != NULL)  {
12032 			sata_log(sata_hba_inst, CE_WARN,
12033 			    "SATA device detached from port %d:%d",
12034 			    cport, npmport);
12035 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12036 		}
12037 
12038 		mutex_destroy(&pmportinfo->pmport_mutex);
12039 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12040 	}
12041 
12042 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12043 
12044 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12045 
12046 	sata_log(sata_hba_inst, CE_WARN,
12047 	    "SATA port multiplier detached at port %d", cport);
12048 
12049 	mutex_exit(&cportinfo->cport_mutex);
12050 }
12051 
12052 /*
12053  * Initialize device
12054  * Specified device is initialized to a default state.
12055  *
12056  * Returns SATA_SUCCESS if all device features are set successfully,
12057  * SATA_RETRY if device is accessible but device features were not set
12058  * successfully, and SATA_FAILURE otherwise.
12059  */
12060 static int
12061 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12062     sata_drive_info_t *sdinfo)
12063 {
12064 	int rval;
12065 
12066 	sata_save_drive_settings(sdinfo);
12067 
12068 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12069 
12070 	sata_init_write_cache_mode(sdinfo);
12071 
12072 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12073 
12074 	/* Determine current data transfer mode */
12075 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12076 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12077 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12078 	    SATA_VALIDINFO_88) != 0 &&
12079 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12080 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12081 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12082 	    SATA_MDMA_SEL_MASK) != 0) {
12083 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12084 	} else
12085 		/* DMA supported, not no DMA transfer mode is selected !? */
12086 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12087 
12088 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12089 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12090 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12091 	else
12092 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12093 
12094 	return (rval);
12095 }
12096 
12097 
12098 /*
12099  * Initialize write cache mode.
12100  *
12101  * The default write cache setting for SATA HDD is provided by sata_write_cache
12102  * static variable. ATAPI CD/DVDs devices have write cache default is
12103  * determined by sata_atapicdvd_write_cache static variable.
12104  * ATAPI tape devices have write cache default is determined by
12105  * sata_atapitape_write_cache static variable.
12106  * ATAPI disk devices have write cache default is determined by
12107  * sata_atapidisk_write_cache static variable.
12108  * 1 - enable
12109  * 0 - disable
12110  * any other value - current drive setting
12111  *
12112  * Although there is not reason to disable write cache on CD/DVD devices,
12113  * tape devices and ATAPI disk devices, the default setting control is provided
12114  * for the maximun flexibility.
12115  *
12116  * In the future, it may be overridden by the
12117  * disk-write-cache-enable property setting, if it is defined.
12118  * Returns SATA_SUCCESS if all device features are set successfully,
12119  * SATA_FAILURE otherwise.
12120  */
12121 static void
12122 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12123 {
12124 	switch (sdinfo->satadrv_type) {
12125 	case SATA_DTYPE_ATADISK:
12126 		if (sata_write_cache == 1)
12127 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12128 		else if (sata_write_cache == 0)
12129 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12130 		/*
12131 		 * When sata_write_cache value is not 0 or 1,
12132 		 * a current setting of the drive's write cache is used.
12133 		 */
12134 		break;
12135 	case SATA_DTYPE_ATAPICD:
12136 		if (sata_atapicdvd_write_cache == 1)
12137 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12138 		else if (sata_atapicdvd_write_cache == 0)
12139 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12140 		/*
12141 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12142 		 * a current setting of the drive's write cache is used.
12143 		 */
12144 		break;
12145 	case SATA_DTYPE_ATAPITAPE:
12146 		if (sata_atapitape_write_cache == 1)
12147 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12148 		else if (sata_atapitape_write_cache == 0)
12149 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12150 		/*
12151 		 * When sata_atapitape_write_cache value is not 0 or 1,
12152 		 * a current setting of the drive's write cache is used.
12153 		 */
12154 		break;
12155 	case SATA_DTYPE_ATAPIDISK:
12156 		if (sata_atapidisk_write_cache == 1)
12157 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12158 		else if (sata_atapidisk_write_cache == 0)
12159 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12160 		/*
12161 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12162 		 * a current setting of the drive's write cache is used.
12163 		 */
12164 		break;
12165 	}
12166 }
12167 
12168 
12169 /*
12170  * Validate sata address.
12171  * Specified cport, pmport and qualifier has to match
12172  * passed sata_scsi configuration info.
12173  * The presence of an attached device is not verified.
12174  *
12175  * Returns 0 when address is valid, -1 otherwise.
12176  */
12177 static int
12178 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12179 	int pmport, int qual)
12180 {
12181 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12182 		goto invalid_address;
12183 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12184 		goto invalid_address;
12185 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12186 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12187 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12188 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12189 		goto invalid_address;
12190 
12191 	return (0);
12192 
12193 invalid_address:
12194 	return (-1);
12195 
12196 }
12197 
12198 /*
12199  * Validate scsi address
12200  * SCSI target address is translated into SATA cport/pmport and compared
12201  * with a controller port/device configuration. LUN has to be 0.
12202  * Returns 0 if a scsi target refers to an attached device,
12203  * returns 1 if address is valid but no valid device is attached,
12204  * returns 2 if address is valid but device type is unknown (not valid device),
12205  * returns -1 if bad address or device is of an unsupported type.
12206  * Upon return sata_device argument is set.
12207  *
12208  * Port multiplier is supported now.
12209  */
12210 static int
12211 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12212 	struct scsi_address *ap, sata_device_t *sata_device)
12213 {
12214 	int cport, pmport, qual, rval;
12215 
12216 	rval = -1;	/* Invalid address */
12217 	if (ap->a_lun != 0)
12218 		goto out;
12219 
12220 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12221 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12222 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12223 
12224 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12225 		goto out;
12226 
12227 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12228 	    0) {
12229 
12230 		sata_cport_info_t *cportinfo;
12231 		sata_pmult_info_t *pmultinfo;
12232 		sata_drive_info_t *sdinfo = NULL;
12233 
12234 		sata_device->satadev_addr.qual = qual;
12235 		sata_device->satadev_addr.cport = cport;
12236 		sata_device->satadev_addr.pmport = pmport;
12237 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12238 
12239 		rval = 1;	/* Valid sata address */
12240 
12241 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12242 		if (qual == SATA_ADDR_DCPORT) {
12243 			if (cportinfo == NULL ||
12244 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12245 				goto out;
12246 
12247 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12248 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12249 			    sdinfo != NULL) {
12250 				rval = 2;
12251 				goto out;
12252 			}
12253 
12254 			if ((cportinfo->cport_dev_type &
12255 			    SATA_VALID_DEV_TYPE) == 0) {
12256 				rval = -1;
12257 				goto out;
12258 			}
12259 
12260 		} else if (qual == SATA_ADDR_DPMPORT) {
12261 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12262 			if (pmultinfo == NULL) {
12263 				rval = -1;
12264 				goto out;
12265 			}
12266 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12267 			    NULL ||
12268 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12269 			    pmport) == SATA_DTYPE_NONE)
12270 				goto out;
12271 
12272 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12273 			    pmport);
12274 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12275 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12276 				rval = 2;
12277 				goto out;
12278 			}
12279 
12280 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12281 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12282 				rval = -1;
12283 				goto out;
12284 			}
12285 
12286 		} else {
12287 			rval = -1;
12288 			goto out;
12289 		}
12290 		if ((sdinfo == NULL) ||
12291 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12292 			goto out;
12293 
12294 		sata_device->satadev_type = sdinfo->satadrv_type;
12295 
12296 		return (0);
12297 	}
12298 out:
12299 	if (rval > 0) {
12300 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12301 		    "sata_validate_scsi_address: no valid target %x lun %x",
12302 		    ap->a_target, ap->a_lun);
12303 	}
12304 	return (rval);
12305 }
12306 
12307 /*
12308  * Find dip corresponding to passed device number
12309  *
12310  * Returns NULL if invalid device number is passed or device cannot be found,
12311  * Returns dip is device is found.
12312  */
12313 static dev_info_t *
12314 sata_devt_to_devinfo(dev_t dev)
12315 {
12316 	dev_info_t *dip;
12317 #ifndef __lock_lint
12318 	struct devnames *dnp;
12319 	major_t major = getmajor(dev);
12320 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12321 
12322 	if (major >= devcnt)
12323 		return (NULL);
12324 
12325 	dnp = &devnamesp[major];
12326 	LOCK_DEV_OPS(&(dnp->dn_lock));
12327 	dip = dnp->dn_head;
12328 	while (dip && (ddi_get_instance(dip) != instance)) {
12329 		dip = ddi_get_next(dip);
12330 	}
12331 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12332 #endif
12333 
12334 	return (dip);
12335 }
12336 
12337 
12338 /*
12339  * Probe device.
12340  * This function issues Identify Device command and initializes local
12341  * sata_drive_info structure if the device can be identified.
12342  * The device type is determined by examining Identify Device
12343  * command response.
12344  * If the sata_hba_inst has linked drive info structure for this
12345  * device address, the Identify Device data is stored into sata_drive_info
12346  * structure linked to the port info structure.
12347  *
12348  * sata_device has to refer to the valid sata port(s) for HBA described
12349  * by sata_hba_inst structure.
12350  *
12351  * Returns:
12352  *	SATA_SUCCESS if device type was successfully probed and port-linked
12353  *		drive info structure was updated;
12354  * 	SATA_FAILURE if there is no device, or device was not probed
12355  *		successully;
12356  *	SATA_RETRY if device probe can be retried later.
12357  * If a device cannot be identified, sata_device's dev_state and dev_type
12358  * fields are set to unknown.
12359  * There are no retries in this function. Any retries should be managed by
12360  * the caller.
12361  */
12362 
12363 
12364 static int
12365 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12366 {
12367 	sata_pmport_info_t *pmportinfo;
12368 	sata_drive_info_t *sdinfo;
12369 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12370 	int rval;
12371 
12372 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12373 	    sata_device->satadev_addr.cport) &
12374 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12375 
12376 	sata_device->satadev_type = SATA_DTYPE_NONE;
12377 
12378 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12379 	    sata_device->satadev_addr.cport)));
12380 
12381 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12382 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12383 		    sata_device->satadev_addr.cport,
12384 		    sata_device->satadev_addr.pmport);
12385 		ASSERT(pmportinfo != NULL);
12386 	}
12387 
12388 	/* Get pointer to port-linked sata device info structure */
12389 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12390 	if (sdinfo != NULL) {
12391 		sdinfo->satadrv_state &=
12392 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12393 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12394 	} else {
12395 		/* No device to probe */
12396 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12397 		    sata_device->satadev_addr.cport)));
12398 		sata_device->satadev_type = SATA_DTYPE_NONE;
12399 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12400 		return (SATA_FAILURE);
12401 	}
12402 	/*
12403 	 * Need to issue both types of identify device command and
12404 	 * determine device type by examining retreived data/status.
12405 	 * First, ATA Identify Device.
12406 	 */
12407 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12408 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12409 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12410 	    sata_device->satadev_addr.cport)));
12411 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12412 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12413 	if (rval == SATA_RETRY) {
12414 		/* We may try to check for ATAPI device */
12415 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12416 			/*
12417 			 * HBA supports ATAPI - try to issue Identify Packet
12418 			 * Device command.
12419 			 */
12420 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12421 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12422 		}
12423 	}
12424 	if (rval == SATA_SUCCESS) {
12425 		/*
12426 		 * Got something responding positively to ATA Identify Device
12427 		 * or to Identify Packet Device cmd.
12428 		 * Save last used device type.
12429 		 */
12430 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12431 
12432 		/* save device info, if possible */
12433 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12434 		    sata_device->satadev_addr.cport)));
12435 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12436 		if (sdinfo == NULL) {
12437 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12438 			    sata_device->satadev_addr.cport)));
12439 			return (SATA_FAILURE);
12440 		}
12441 		/*
12442 		 * Copy drive info into the port-linked drive info structure.
12443 		 */
12444 		*sdinfo = new_sdinfo;
12445 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12446 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12447 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12448 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12449 			    sata_device->satadev_addr.cport) =
12450 			    sdinfo->satadrv_type;
12451 		else { /* SATA_ADDR_DPMPORT */
12452 			mutex_enter(&pmportinfo->pmport_mutex);
12453 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12454 			    sata_device->satadev_addr.cport,
12455 			    sata_device->satadev_addr.pmport) =
12456 			    sdinfo->satadrv_type;
12457 			mutex_exit(&pmportinfo->pmport_mutex);
12458 		}
12459 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12460 		    sata_device->satadev_addr.cport)));
12461 		return (SATA_SUCCESS);
12462 	}
12463 
12464 	/*
12465 	 * It may be SATA_RETRY or SATA_FAILURE return.
12466 	 * Looks like we cannot determine the device type at this time.
12467 	 */
12468 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12469 	    sata_device->satadev_addr.cport)));
12470 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12471 	if (sdinfo != NULL) {
12472 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12473 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12474 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12475 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12476 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12477 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12478 			    sata_device->satadev_addr.cport) =
12479 			    SATA_DTYPE_UNKNOWN;
12480 		else {
12481 			/* SATA_ADDR_DPMPORT */
12482 			mutex_enter(&pmportinfo->pmport_mutex);
12483 			if ((SATA_PMULT_INFO(sata_hba_inst,
12484 			    sata_device->satadev_addr.cport) != NULL) &&
12485 			    (SATA_PMPORT_INFO(sata_hba_inst,
12486 			    sata_device->satadev_addr.cport,
12487 			    sata_device->satadev_addr.pmport) != NULL))
12488 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12489 				    sata_device->satadev_addr.cport,
12490 				    sata_device->satadev_addr.pmport) =
12491 				    SATA_DTYPE_UNKNOWN;
12492 			mutex_exit(&pmportinfo->pmport_mutex);
12493 		}
12494 	}
12495 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12496 	    sata_device->satadev_addr.cport)));
12497 	return (rval);
12498 }
12499 
12500 
12501 /*
12502  * Get pointer to sata_drive_info structure.
12503  *
12504  * The sata_device has to contain address (cport, pmport and qualifier) for
12505  * specified sata_scsi structure.
12506  *
12507  * Returns NULL if device address is not valid for this HBA configuration.
12508  * Otherwise, returns a pointer to sata_drive_info structure.
12509  *
12510  * This function should be called with a port mutex held.
12511  */
12512 static sata_drive_info_t *
12513 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12514     sata_device_t *sata_device)
12515 {
12516 	uint8_t cport = sata_device->satadev_addr.cport;
12517 	uint8_t pmport = sata_device->satadev_addr.pmport;
12518 	uint8_t qual = sata_device->satadev_addr.qual;
12519 
12520 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12521 		return (NULL);
12522 
12523 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12524 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12525 		/* Port not probed yet */
12526 		return (NULL);
12527 
12528 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12529 		return (NULL);
12530 
12531 	if (qual == SATA_ADDR_DCPORT) {
12532 		/* Request for a device on a controller port */
12533 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12534 		    SATA_DTYPE_PMULT)
12535 			/* Port multiplier attached */
12536 			return (NULL);
12537 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12538 	}
12539 	if (qual == SATA_ADDR_DPMPORT) {
12540 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12541 		    SATA_DTYPE_PMULT)
12542 			return (NULL);
12543 
12544 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12545 			return (NULL);
12546 
12547 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12548 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12549 			/* Port multiplier port not probed yet */
12550 			return (NULL);
12551 
12552 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12553 	}
12554 
12555 	/* we should not get here */
12556 	return (NULL);
12557 }
12558 
12559 
12560 /*
12561  * sata_identify_device.
12562  * Send Identify Device command to SATA HBA driver.
12563  * If command executes successfully, update sata_drive_info structure pointed
12564  * to by sdinfo argument, including Identify Device data.
12565  * If command fails, invalidate data in sata_drive_info.
12566  *
12567  * Cannot be called from interrupt level.
12568  *
12569  * Returns:
12570  * SATA_SUCCESS if the device was identified as a supported device,
12571  * SATA_RETRY if the device was not identified but could be retried,
12572  * SATA_FAILURE if the device was not identified and identify attempt
12573  *	should not be retried.
12574  */
12575 static int
12576 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12577     sata_drive_info_t *sdinfo)
12578 {
12579 	uint16_t cfg_word;
12580 	int rval;
12581 
12582 	/* fetch device identify data */
12583 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12584 	    sdinfo)) != SATA_SUCCESS)
12585 		goto fail_unknown;
12586 
12587 	cfg_word = sdinfo->satadrv_id.ai_config;
12588 
12589 	/* Set the correct device type */
12590 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12591 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12592 	} else if (cfg_word == SATA_CFA_TYPE) {
12593 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12594 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12595 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12596 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12597 		case SATA_ATAPI_CDROM_DEV:
12598 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12599 			break;
12600 		case SATA_ATAPI_SQACC_DEV:
12601 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12602 			break;
12603 		case SATA_ATAPI_DIRACC_DEV:
12604 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12605 			break;
12606 		default:
12607 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12608 		}
12609 	} else {
12610 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12611 	}
12612 
12613 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12614 		if (sdinfo->satadrv_capacity == 0) {
12615 			/* Non-LBA disk. Too bad... */
12616 			sata_log(sata_hba_inst, CE_WARN,
12617 			    "SATA disk device at port %d does not support LBA",
12618 			    sdinfo->satadrv_addr.cport);
12619 			rval = SATA_FAILURE;
12620 			goto fail_unknown;
12621 		}
12622 	}
12623 #if 0
12624 	/* Left for historical reason */
12625 	/*
12626 	 * Some initial version of SATA spec indicated that at least
12627 	 * UDMA mode 4 has to be supported. It is not metioned in
12628 	 * SerialATA 2.6, so this restriction is removed.
12629 	 */
12630 	/* Check for Ultra DMA modes 6 through 0 being supported */
12631 	for (i = 6; i >= 0; --i) {
12632 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12633 			break;
12634 	}
12635 
12636 	/*
12637 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12638 	 * higher are not supported by the device, fail this
12639 	 * device.
12640 	 */
12641 	if (i < 4) {
12642 		/* No required Ultra DMA mode supported */
12643 		sata_log(sata_hba_inst, CE_WARN,
12644 		    "SATA disk device at port %d does not support UDMA "
12645 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12646 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12647 		    "mode 4 or higher required, %d supported", i));
12648 		rval = SATA_FAILURE;
12649 		goto fail_unknown;
12650 	}
12651 #endif
12652 
12653 	/*
12654 	 * For Disk devices, if it doesn't support UDMA mode, we would
12655 	 * like to return failure directly.
12656 	 */
12657 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12658 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12659 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12660 		sata_log(sata_hba_inst, CE_WARN,
12661 		    "SATA disk device at port %d does not support UDMA",
12662 		    sdinfo->satadrv_addr.cport);
12663 		rval = SATA_FAILURE;
12664 		goto fail_unknown;
12665 	}
12666 
12667 	return (SATA_SUCCESS);
12668 
12669 fail_unknown:
12670 	/* Invalidate sata_drive_info ? */
12671 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12672 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12673 	return (rval);
12674 }
12675 
12676 /*
12677  * Log/display device information
12678  */
12679 static void
12680 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12681     sata_drive_info_t *sdinfo)
12682 {
12683 	int valid_version;
12684 	char msg_buf[MAXPATHLEN];
12685 	int i;
12686 
12687 	/* Show HBA path */
12688 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12689 
12690 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12691 
12692 	switch (sdinfo->satadrv_type) {
12693 	case SATA_DTYPE_ATADISK:
12694 		(void) sprintf(msg_buf, "SATA disk device at");
12695 		break;
12696 
12697 	case SATA_DTYPE_ATAPICD:
12698 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12699 		break;
12700 
12701 	case SATA_DTYPE_ATAPITAPE:
12702 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12703 		break;
12704 
12705 	case SATA_DTYPE_ATAPIDISK:
12706 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12707 		break;
12708 
12709 	case SATA_DTYPE_UNKNOWN:
12710 		(void) sprintf(msg_buf,
12711 		    "Unsupported SATA device type (cfg 0x%x) at ",
12712 		    sdinfo->satadrv_id.ai_config);
12713 		break;
12714 	}
12715 
12716 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12717 		cmn_err(CE_CONT, "?\t%s port %d\n",
12718 		    msg_buf, sdinfo->satadrv_addr.cport);
12719 	else
12720 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12721 		    msg_buf, sdinfo->satadrv_addr.cport,
12722 		    sdinfo->satadrv_addr.pmport);
12723 
12724 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12725 	    sizeof (sdinfo->satadrv_id.ai_model));
12726 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12727 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12728 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12729 
12730 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12731 	    sizeof (sdinfo->satadrv_id.ai_fw));
12732 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12733 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12734 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12735 
12736 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12737 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12738 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12739 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12740 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12741 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12742 	} else {
12743 		/*
12744 		 * Some drives do not implement serial number and may
12745 		 * violate the spec by providing spaces rather than zeros
12746 		 * in serial number field. Scan the buffer to detect it.
12747 		 */
12748 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12749 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12750 				break;
12751 		}
12752 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12753 			cmn_err(CE_CONT, "?\tserial number - none\n");
12754 		} else {
12755 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12756 		}
12757 	}
12758 
12759 #ifdef SATA_DEBUG
12760 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12761 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12762 		int i;
12763 		for (i = 14; i >= 2; i--) {
12764 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12765 				valid_version = i;
12766 				break;
12767 			}
12768 		}
12769 		cmn_err(CE_CONT,
12770 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12771 		    valid_version,
12772 		    sdinfo->satadrv_id.ai_majorversion,
12773 		    sdinfo->satadrv_id.ai_minorversion);
12774 	}
12775 #endif
12776 	/* Log some info */
12777 	cmn_err(CE_CONT, "?\tsupported features:\n");
12778 	msg_buf[0] = '\0';
12779 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12780 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12781 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12782 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12783 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12784 	}
12785 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12786 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12787 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12788 		(void) strlcat(msg_buf, ", Native Command Queueing",
12789 		    MAXPATHLEN);
12790 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12791 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12792 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12793 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12794 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12795 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12796 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12797 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12798 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12799 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12800 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12801 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12802 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12803 	if (sdinfo->satadrv_features_support &
12804 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12805 		msg_buf[0] = '\0';
12806 		(void) snprintf(msg_buf, MAXPATHLEN,
12807 		    "Supported queue depth %d",
12808 		    sdinfo->satadrv_queue_depth);
12809 		if (!(sata_func_enable &
12810 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12811 			(void) strlcat(msg_buf,
12812 			    " - queueing disabled globally", MAXPATHLEN);
12813 		else if (sdinfo->satadrv_queue_depth >
12814 		    sdinfo->satadrv_max_queue_depth) {
12815 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12816 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12817 			    (int)sdinfo->satadrv_max_queue_depth);
12818 		}
12819 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12820 	}
12821 
12822 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12823 #ifdef __i386
12824 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12825 		    sdinfo->satadrv_capacity);
12826 #else
12827 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12828 		    sdinfo->satadrv_capacity);
12829 #endif
12830 		cmn_err(CE_CONT, "?%s", msg_buf);
12831 	}
12832 }
12833 
12834 /*
12835  * Log/display port multiplier information
12836  * No Mutex should be hold.
12837  */
12838 static void
12839 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12840     sata_device_t *sata_device)
12841 {
12842 	_NOTE(ARGUNUSED(sata_hba_inst))
12843 
12844 	int cport = sata_device->satadev_addr.cport;
12845 	sata_pmult_info_t *pmultinfo;
12846 	char msg_buf[MAXPATHLEN];
12847 	uint32_t gscr0, gscr1, gscr2, gscr64;
12848 
12849 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12850 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12851 	if (pmultinfo == NULL) {
12852 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12853 		return;
12854 	}
12855 
12856 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12857 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12858 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12859 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12860 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12861 
12862 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12863 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12864 
12865 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12866 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12867 	cmn_err(CE_CONT, "?%s", msg_buf);
12868 
12869 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12870 	if (gscr1 & (1 << 3))
12871 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12872 	else if (gscr1 & (1 << 2))
12873 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12874 	else if (gscr1 & (1 << 1))
12875 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12876 	else
12877 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12878 	cmn_err(CE_CONT, "?%s", msg_buf);
12879 
12880 	(void) strcpy(msg_buf, "\tSupport ");
12881 	if (gscr64 & (1 << 3))
12882 		(void) strlcat(msg_buf, "Asy-Notif, ",
12883 		    MAXPATHLEN);
12884 	if (gscr64 & (1 << 2))
12885 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12886 	if (gscr64 & (1 << 1))
12887 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12888 	if (gscr64 & (1 << 0))
12889 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12890 	if ((gscr64 & 0xf) == 0)
12891 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12892 	cmn_err(CE_CONT, "?%s", msg_buf);
12893 
12894 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12895 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12896 	cmn_err(CE_CONT, "?%s", msg_buf);
12897 }
12898 
12899 /*
12900  * sata_save_drive_settings extracts current setting of the device and stores
12901  * it for future reference, in case the device setup would need to be restored
12902  * after the device reset.
12903  *
12904  * For all devices read ahead and write cache settings are saved, if the
12905  * device supports these features at all.
12906  * For ATAPI devices the Removable Media Status Notification setting is saved.
12907  */
12908 static void
12909 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12910 {
12911 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12912 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12913 
12914 		/* Current setting of Read Ahead (and Read Cache) */
12915 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12916 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12917 		else
12918 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12919 
12920 		/* Current setting of Write Cache */
12921 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12922 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12923 		else
12924 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12925 	}
12926 
12927 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12928 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12929 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12930 		else
12931 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12932 	}
12933 }
12934 
12935 
12936 /*
12937  * sata_check_capacity function determines a disk capacity
12938  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12939  *
12940  * NOTE: CHS mode is not supported! If a device does not support LBA,
12941  * this function is not called.
12942  *
12943  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12944  */
12945 static uint64_t
12946 sata_check_capacity(sata_drive_info_t *sdinfo)
12947 {
12948 	uint64_t capacity = 0;
12949 	int i;
12950 
12951 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
12952 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
12953 		/* Capacity valid only for LBA-addressable disk devices */
12954 		return (0);
12955 
12956 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
12957 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
12958 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
12959 		/* LBA48 mode supported and enabled */
12960 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
12961 		    SATA_DEV_F_LBA28;
12962 		for (i = 3;  i >= 0;  --i) {
12963 			capacity <<= 16;
12964 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
12965 		}
12966 	} else {
12967 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
12968 		capacity <<= 16;
12969 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
12970 		if (capacity >= 0x1000000)
12971 			/* LBA28 mode */
12972 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
12973 	}
12974 	return (capacity);
12975 }
12976 
12977 
12978 /*
12979  * Allocate consistent buffer for DMA transfer
12980  *
12981  * Cannot be called from interrupt level or with mutex held - it may sleep.
12982  *
12983  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
12984  */
12985 static struct buf *
12986 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
12987 {
12988 	struct scsi_address ap;
12989 	struct buf *bp;
12990 	ddi_dma_attr_t	cur_dma_attr;
12991 
12992 	ASSERT(spx->txlt_sata_pkt != NULL);
12993 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
12994 	ap.a_target = SATA_TO_SCSI_TARGET(
12995 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
12996 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
12997 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
12998 	ap.a_lun = 0;
12999 
13000 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13001 	    B_READ, SLEEP_FUNC, NULL);
13002 
13003 	if (bp != NULL) {
13004 		/* Allocate DMA resources for this buffer */
13005 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13006 		/*
13007 		 * We use a local version of the dma_attr, to account
13008 		 * for a device addressing limitations.
13009 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13010 		 * will cause dma attributes to be adjusted to a lowest
13011 		 * acceptable level.
13012 		 */
13013 		sata_adjust_dma_attr(NULL,
13014 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13015 
13016 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13017 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13018 			scsi_free_consistent_buf(bp);
13019 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13020 			bp = NULL;
13021 		}
13022 	}
13023 	return (bp);
13024 }
13025 
13026 /*
13027  * Release local buffer (consistent buffer for DMA transfer) allocated
13028  * via sata_alloc_local_buffer().
13029  */
13030 static void
13031 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13032 {
13033 	ASSERT(spx->txlt_sata_pkt != NULL);
13034 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13035 
13036 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13037 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13038 
13039 	sata_common_free_dma_rsrcs(spx);
13040 
13041 	/* Free buffer */
13042 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13043 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13044 }
13045 
13046 /*
13047  * Allocate sata_pkt
13048  * Pkt structure version and embedded strcutures version are initialized.
13049  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13050  *
13051  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13052  * callback argument determines if it can sleep or not.
13053  * Hence, it should not be called from interrupt context.
13054  *
13055  * If successful, non-NULL pointer to a sata pkt is returned.
13056  * Upon failure, NULL pointer is returned.
13057  */
13058 static sata_pkt_t *
13059 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13060 {
13061 	sata_pkt_t *spkt;
13062 	int kmsflag;
13063 
13064 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13065 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13066 	if (spkt == NULL) {
13067 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13068 		    "sata_pkt_alloc: failed"));
13069 		return (NULL);
13070 	}
13071 	spkt->satapkt_rev = SATA_PKT_REV;
13072 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13073 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13074 	spkt->satapkt_framework_private = spx;
13075 	spx->txlt_sata_pkt = spkt;
13076 	return (spkt);
13077 }
13078 
13079 /*
13080  * Free sata pkt allocated via sata_pkt_alloc()
13081  */
13082 static void
13083 sata_pkt_free(sata_pkt_txlate_t *spx)
13084 {
13085 	ASSERT(spx->txlt_sata_pkt != NULL);
13086 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13087 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13088 	spx->txlt_sata_pkt = NULL;
13089 }
13090 
13091 
13092 /*
13093  * Adjust DMA attributes.
13094  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13095  * from 8 bits to 16 bits, depending on a command being used.
13096  * Limiting max block count arbitrarily to 256 for all read/write
13097  * commands may affects performance, so check both the device and
13098  * controller capability before adjusting dma attributes.
13099  */
13100 void
13101 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13102     ddi_dma_attr_t *adj_dma_attr)
13103 {
13104 	uint32_t count_max;
13105 
13106 	/* Copy original attributes */
13107 	*adj_dma_attr = *dma_attr;
13108 	/*
13109 	 * Things to consider: device addressing capability,
13110 	 * "excessive" controller DMA capabilities.
13111 	 * If a device is being probed/initialized, there are
13112 	 * no device info - use default limits then.
13113 	 */
13114 	if (sdinfo == NULL) {
13115 		count_max = dma_attr->dma_attr_granular * 0x100;
13116 		if (dma_attr->dma_attr_count_max > count_max)
13117 			adj_dma_attr->dma_attr_count_max = count_max;
13118 		if (dma_attr->dma_attr_maxxfer > count_max)
13119 			adj_dma_attr->dma_attr_maxxfer = count_max;
13120 		return;
13121 	}
13122 
13123 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13124 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13125 			/*
13126 			 * 16-bit sector count may be used - we rely on
13127 			 * the assumption that only read and write cmds
13128 			 * will request more than 256 sectors worth of data
13129 			 */
13130 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13131 		} else {
13132 			/*
13133 			 * 8-bit sector count will be used - default limits
13134 			 * for dma attributes
13135 			 */
13136 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13137 		}
13138 		/*
13139 		 * Adjust controler dma attributes, if necessary
13140 		 */
13141 		if (dma_attr->dma_attr_count_max > count_max)
13142 			adj_dma_attr->dma_attr_count_max = count_max;
13143 		if (dma_attr->dma_attr_maxxfer > count_max)
13144 			adj_dma_attr->dma_attr_maxxfer = count_max;
13145 	}
13146 }
13147 
13148 
13149 /*
13150  * Allocate DMA resources for the buffer
13151  * This function handles initial DMA resource allocation as well as
13152  * DMA window shift and may be called repeatedly for the same DMA window
13153  * until all DMA cookies in the DMA window are processed.
13154  * To guarantee that there is always a coherent set of cookies to process
13155  * by SATA HBA driver (observing alignment, device granularity, etc.),
13156  * the number of slots for DMA cookies is equal to lesser of  a number of
13157  * cookies in a DMA window and a max number of scatter/gather entries.
13158  *
13159  * Returns DDI_SUCCESS upon successful operation.
13160  * Return failure code of a failing command or DDI_FAILURE when
13161  * internal cleanup failed.
13162  */
13163 static int
13164 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13165     int (*callback)(caddr_t), caddr_t arg,
13166     ddi_dma_attr_t *cur_dma_attr)
13167 {
13168 	int	rval;
13169 	off_t	offset;
13170 	size_t	size;
13171 	int	max_sg_len, req_len, i;
13172 	uint_t	dma_flags;
13173 	struct buf	*bp;
13174 	uint64_t	cur_txfer_len;
13175 
13176 
13177 	ASSERT(spx->txlt_sata_pkt != NULL);
13178 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13179 	ASSERT(bp != NULL);
13180 
13181 
13182 	if (spx->txlt_buf_dma_handle == NULL) {
13183 		/*
13184 		 * No DMA resources allocated so far - this is a first call
13185 		 * for this sata pkt.
13186 		 */
13187 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13188 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13189 
13190 		if (rval != DDI_SUCCESS) {
13191 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13192 			    "sata_dma_buf_setup: no buf DMA resources %x",
13193 			    rval));
13194 			return (rval);
13195 		}
13196 
13197 		if (bp->b_flags & B_READ)
13198 			dma_flags = DDI_DMA_READ;
13199 		else
13200 			dma_flags = DDI_DMA_WRITE;
13201 
13202 		if (flags & PKT_CONSISTENT)
13203 			dma_flags |= DDI_DMA_CONSISTENT;
13204 
13205 		if (flags & PKT_DMA_PARTIAL)
13206 			dma_flags |= DDI_DMA_PARTIAL;
13207 
13208 		/*
13209 		 * Check buffer alignment and size against dma attributes
13210 		 * Consider dma_attr_align only. There may be requests
13211 		 * with the size lower than device granularity, but they
13212 		 * will not read/write from/to the device, so no adjustment
13213 		 * is necessary. The dma_attr_minxfer theoretically should
13214 		 * be considered, but no HBA driver is checking it.
13215 		 */
13216 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13217 		    cur_dma_attr->dma_attr_align)) {
13218 			rval = ddi_dma_buf_bind_handle(
13219 			    spx->txlt_buf_dma_handle,
13220 			    bp, dma_flags, callback, arg,
13221 			    &spx->txlt_dma_cookie,
13222 			    &spx->txlt_curwin_num_dma_cookies);
13223 		} else { /* Buffer is not aligned */
13224 
13225 			int	(*ddicallback)(caddr_t);
13226 			size_t	bufsz;
13227 
13228 			/* Check id sleeping is allowed */
13229 			ddicallback = (callback == NULL_FUNC) ?
13230 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13231 
13232 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13233 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13234 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13235 
13236 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13237 				/*
13238 				 * CPU will need to access data in the buffer
13239 				 * (for copying) so map it.
13240 				 */
13241 				bp_mapin(bp);
13242 
13243 			ASSERT(spx->txlt_tmp_buf == NULL);
13244 
13245 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13246 			rval = ddi_dma_mem_alloc(
13247 			    spx->txlt_buf_dma_handle,
13248 			    bp->b_bcount,
13249 			    &sata_acc_attr,
13250 			    DDI_DMA_STREAMING,
13251 			    ddicallback, NULL,
13252 			    &spx->txlt_tmp_buf,
13253 			    &bufsz,
13254 			    &spx->txlt_tmp_buf_handle);
13255 
13256 			if (rval != DDI_SUCCESS) {
13257 				/* DMA mapping failed */
13258 				(void) ddi_dma_free_handle(
13259 				    &spx->txlt_buf_dma_handle);
13260 				spx->txlt_buf_dma_handle = NULL;
13261 #ifdef SATA_DEBUG
13262 				mbuffail_count++;
13263 #endif
13264 				SATADBG1(SATA_DBG_DMA_SETUP,
13265 				    spx->txlt_sata_hba_inst,
13266 				    "sata_dma_buf_setup: "
13267 				    "buf dma mem alloc failed %x\n", rval);
13268 				return (rval);
13269 			}
13270 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13271 			    cur_dma_attr->dma_attr_align));
13272 
13273 #ifdef SATA_DEBUG
13274 			mbuf_count++;
13275 
13276 			if (bp->b_bcount != bufsz)
13277 				/*
13278 				 * This will require special handling, because
13279 				 * DMA cookies will be based on the temporary
13280 				 * buffer size, not the original buffer
13281 				 * b_bcount, so the residue may have to
13282 				 * be counted differently.
13283 				 */
13284 				SATADBG2(SATA_DBG_DMA_SETUP,
13285 				    spx->txlt_sata_hba_inst,
13286 				    "sata_dma_buf_setup: bp size %x != "
13287 				    "bufsz %x\n", bp->b_bcount, bufsz);
13288 #endif
13289 			if (dma_flags & DDI_DMA_WRITE) {
13290 				/*
13291 				 * Write operation - copy data into
13292 				 * an aligned temporary buffer. Buffer will be
13293 				 * synced for device by ddi_dma_addr_bind_handle
13294 				 */
13295 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13296 				    bp->b_bcount);
13297 			}
13298 
13299 			rval = ddi_dma_addr_bind_handle(
13300 			    spx->txlt_buf_dma_handle,
13301 			    NULL,
13302 			    spx->txlt_tmp_buf,
13303 			    bufsz, dma_flags, ddicallback, 0,
13304 			    &spx->txlt_dma_cookie,
13305 			    &spx->txlt_curwin_num_dma_cookies);
13306 		}
13307 
13308 		switch (rval) {
13309 		case DDI_DMA_PARTIAL_MAP:
13310 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13311 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13312 			/*
13313 			 * Partial DMA mapping.
13314 			 * Retrieve number of DMA windows for this request.
13315 			 */
13316 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13317 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13318 				if (spx->txlt_tmp_buf != NULL) {
13319 					ddi_dma_mem_free(
13320 					    &spx->txlt_tmp_buf_handle);
13321 					spx->txlt_tmp_buf = NULL;
13322 				}
13323 				(void) ddi_dma_unbind_handle(
13324 				    spx->txlt_buf_dma_handle);
13325 				(void) ddi_dma_free_handle(
13326 				    &spx->txlt_buf_dma_handle);
13327 				spx->txlt_buf_dma_handle = NULL;
13328 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13329 				    "sata_dma_buf_setup: numwin failed\n"));
13330 				return (DDI_FAILURE);
13331 			}
13332 			SATADBG2(SATA_DBG_DMA_SETUP,
13333 			    spx->txlt_sata_hba_inst,
13334 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13335 			    spx->txlt_num_dma_win,
13336 			    spx->txlt_curwin_num_dma_cookies);
13337 			spx->txlt_cur_dma_win = 0;
13338 			break;
13339 
13340 		case DDI_DMA_MAPPED:
13341 			/* DMA fully mapped */
13342 			spx->txlt_num_dma_win = 1;
13343 			spx->txlt_cur_dma_win = 0;
13344 			SATADBG1(SATA_DBG_DMA_SETUP,
13345 			    spx->txlt_sata_hba_inst,
13346 			    "sata_dma_buf_setup: windows: 1 "
13347 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13348 			break;
13349 
13350 		default:
13351 			/* DMA mapping failed */
13352 			if (spx->txlt_tmp_buf != NULL) {
13353 				ddi_dma_mem_free(
13354 				    &spx->txlt_tmp_buf_handle);
13355 				spx->txlt_tmp_buf = NULL;
13356 			}
13357 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13358 			spx->txlt_buf_dma_handle = NULL;
13359 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13360 			    "sata_dma_buf_setup: buf dma handle binding "
13361 			    "failed %x\n", rval));
13362 			return (rval);
13363 		}
13364 		spx->txlt_curwin_processed_dma_cookies = 0;
13365 		spx->txlt_dma_cookie_list = NULL;
13366 	} else {
13367 		/*
13368 		 * DMA setup is reused. Check if we need to process more
13369 		 * cookies in current window, or to get next window, if any.
13370 		 */
13371 
13372 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13373 		    spx->txlt_curwin_num_dma_cookies);
13374 
13375 		if (spx->txlt_curwin_processed_dma_cookies ==
13376 		    spx->txlt_curwin_num_dma_cookies) {
13377 			/*
13378 			 * All cookies from current DMA window were processed.
13379 			 * Get next DMA window.
13380 			 */
13381 			spx->txlt_cur_dma_win++;
13382 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13383 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13384 				    spx->txlt_cur_dma_win, &offset, &size,
13385 				    &spx->txlt_dma_cookie,
13386 				    &spx->txlt_curwin_num_dma_cookies);
13387 				spx->txlt_curwin_processed_dma_cookies = 0;
13388 			} else {
13389 				/* No more windows! End of request! */
13390 				/* What to do? - panic for now */
13391 				ASSERT(spx->txlt_cur_dma_win >=
13392 				    spx->txlt_num_dma_win);
13393 
13394 				spx->txlt_curwin_num_dma_cookies = 0;
13395 				spx->txlt_curwin_processed_dma_cookies = 0;
13396 				spx->txlt_sata_pkt->
13397 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13398 				return (DDI_SUCCESS);
13399 			}
13400 		}
13401 	}
13402 	/* There better be at least one DMA cookie outstanding */
13403 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13404 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13405 
13406 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13407 		/* The default cookie slot was used in previous run */
13408 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13409 		spx->txlt_dma_cookie_list = NULL;
13410 		spx->txlt_dma_cookie_list_len = 0;
13411 	}
13412 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13413 		/*
13414 		 * Processing a new DMA window - set-up dma cookies list.
13415 		 * We may reuse previously allocated cookie array if it is
13416 		 * possible.
13417 		 */
13418 		if (spx->txlt_dma_cookie_list != NULL &&
13419 		    spx->txlt_dma_cookie_list_len <
13420 		    spx->txlt_curwin_num_dma_cookies) {
13421 			/*
13422 			 * New DMA window contains more cookies than
13423 			 * the previous one. We need larger cookie list - free
13424 			 * the old one.
13425 			 */
13426 			(void) kmem_free(spx->txlt_dma_cookie_list,
13427 			    spx->txlt_dma_cookie_list_len *
13428 			    sizeof (ddi_dma_cookie_t));
13429 			spx->txlt_dma_cookie_list = NULL;
13430 			spx->txlt_dma_cookie_list_len = 0;
13431 		}
13432 		if (spx->txlt_dma_cookie_list == NULL) {
13433 			/*
13434 			 * Calculate lesser of number of cookies in this
13435 			 * DMA window and number of s/g entries.
13436 			 */
13437 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13438 			req_len = MIN(max_sg_len,
13439 			    spx->txlt_curwin_num_dma_cookies);
13440 
13441 			/* Allocate new dma cookie array if necessary */
13442 			if (req_len == 1) {
13443 				/* Only one cookie - no need for a list */
13444 				spx->txlt_dma_cookie_list =
13445 				    &spx->txlt_dma_cookie;
13446 				spx->txlt_dma_cookie_list_len = 1;
13447 			} else {
13448 				/*
13449 				 * More than one cookie - try to allocate space.
13450 				 */
13451 				spx->txlt_dma_cookie_list = kmem_zalloc(
13452 				    sizeof (ddi_dma_cookie_t) * req_len,
13453 				    callback == NULL_FUNC ? KM_NOSLEEP :
13454 				    KM_SLEEP);
13455 				if (spx->txlt_dma_cookie_list == NULL) {
13456 					SATADBG1(SATA_DBG_DMA_SETUP,
13457 					    spx->txlt_sata_hba_inst,
13458 					    "sata_dma_buf_setup: cookie list "
13459 					    "allocation failed\n", NULL);
13460 					/*
13461 					 * We could not allocate space for
13462 					 * neccessary number of dma cookies in
13463 					 * this window, so we fail this request.
13464 					 * Next invocation would try again to
13465 					 * allocate space for cookie list.
13466 					 * Note:Packet residue was not modified.
13467 					 */
13468 					return (DDI_DMA_NORESOURCES);
13469 				} else {
13470 					spx->txlt_dma_cookie_list_len = req_len;
13471 				}
13472 			}
13473 		}
13474 		/*
13475 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13476 		 * First cookie was already fetched.
13477 		 */
13478 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13479 		cur_txfer_len =
13480 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13481 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13482 		spx->txlt_curwin_processed_dma_cookies++;
13483 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13484 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13485 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13486 			    &spx->txlt_dma_cookie_list[i]);
13487 			cur_txfer_len +=
13488 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13489 			spx->txlt_curwin_processed_dma_cookies++;
13490 			spx->txlt_sata_pkt->
13491 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13492 		}
13493 	} else {
13494 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13495 		    "sata_dma_buf_setup: sliding within DMA window, "
13496 		    "cur cookie %d, total cookies %d\n",
13497 		    spx->txlt_curwin_processed_dma_cookies,
13498 		    spx->txlt_curwin_num_dma_cookies);
13499 
13500 		/*
13501 		 * Not all cookies from the current dma window were used because
13502 		 * of s/g limitation.
13503 		 * There is no need to re-size the list - it was set at
13504 		 * optimal size, or only default entry is used (s/g = 1).
13505 		 */
13506 		if (spx->txlt_dma_cookie_list == NULL) {
13507 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13508 			spx->txlt_dma_cookie_list_len = 1;
13509 		}
13510 		/*
13511 		 * Since we are processing remaining cookies in a DMA window,
13512 		 * there may be less of them than the number of entries in the
13513 		 * current dma cookie list.
13514 		 */
13515 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13516 		    (spx->txlt_curwin_num_dma_cookies -
13517 		    spx->txlt_curwin_processed_dma_cookies));
13518 
13519 		/* Fetch the next batch of cookies */
13520 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13521 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13522 			    &spx->txlt_dma_cookie_list[i]);
13523 			cur_txfer_len +=
13524 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13525 			spx->txlt_sata_pkt->
13526 			    satapkt_cmd.satacmd_num_dma_cookies++;
13527 			spx->txlt_curwin_processed_dma_cookies++;
13528 		}
13529 	}
13530 
13531 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13532 
13533 	/* Point sata_cmd to the cookie list */
13534 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13535 	    &spx->txlt_dma_cookie_list[0];
13536 
13537 	/* Remember number of DMA cookies passed in sata packet */
13538 	spx->txlt_num_dma_cookies =
13539 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13540 
13541 	ASSERT(cur_txfer_len != 0);
13542 	if (cur_txfer_len <= bp->b_bcount)
13543 		spx->txlt_total_residue -= cur_txfer_len;
13544 	else {
13545 		/*
13546 		 * Temporary DMA buffer has been padded by
13547 		 * ddi_dma_mem_alloc()!
13548 		 * This requires special handling, because DMA cookies are
13549 		 * based on the temporary buffer size, not the b_bcount,
13550 		 * and we have extra bytes to transfer - but the packet
13551 		 * residue has to stay correct because we will copy only
13552 		 * the requested number of bytes.
13553 		 */
13554 		spx->txlt_total_residue -= bp->b_bcount;
13555 	}
13556 
13557 	return (DDI_SUCCESS);
13558 }
13559 
13560 /*
13561  * Common routine for releasing DMA resources
13562  */
13563 static void
13564 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13565 {
13566 	if (spx->txlt_buf_dma_handle != NULL) {
13567 		if (spx->txlt_tmp_buf != NULL)  {
13568 			/*
13569 			 * Intermediate DMA buffer was allocated.
13570 			 * Free allocated buffer and associated access handle.
13571 			 */
13572 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13573 			spx->txlt_tmp_buf = NULL;
13574 		}
13575 		/*
13576 		 * Free DMA resources - cookies and handles
13577 		 */
13578 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13579 		if (spx->txlt_dma_cookie_list != NULL) {
13580 			if (spx->txlt_dma_cookie_list !=
13581 			    &spx->txlt_dma_cookie) {
13582 				(void) kmem_free(spx->txlt_dma_cookie_list,
13583 				    spx->txlt_dma_cookie_list_len *
13584 				    sizeof (ddi_dma_cookie_t));
13585 				spx->txlt_dma_cookie_list = NULL;
13586 			}
13587 		}
13588 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13589 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13590 		spx->txlt_buf_dma_handle = NULL;
13591 	}
13592 }
13593 
13594 /*
13595  * Free DMA resources
13596  * Used by the HBA driver to release DMA resources that it does not use.
13597  *
13598  * Returns Void
13599  */
13600 void
13601 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13602 {
13603 	sata_pkt_txlate_t *spx;
13604 
13605 	if (sata_pkt == NULL)
13606 		return;
13607 
13608 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13609 
13610 	sata_common_free_dma_rsrcs(spx);
13611 }
13612 
13613 /*
13614  * Fetch Device Identify data.
13615  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13616  * command to a device and get the device identify data.
13617  * The device_info structure has to be set to device type (for selecting proper
13618  * device identify command).
13619  *
13620  * Returns:
13621  * SATA_SUCCESS if cmd succeeded
13622  * SATA_RETRY if cmd was rejected and could be retried,
13623  * SATA_FAILURE if cmd failed and should not be retried (port error)
13624  *
13625  * Cannot be called in an interrupt context.
13626  */
13627 
13628 static int
13629 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13630     sata_drive_info_t *sdinfo)
13631 {
13632 	struct buf *bp;
13633 	sata_pkt_t *spkt;
13634 	sata_cmd_t *scmd;
13635 	sata_pkt_txlate_t *spx;
13636 	int rval;
13637 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13638 
13639 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13640 	spx->txlt_sata_hba_inst = sata_hba_inst;
13641 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13642 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13643 	if (spkt == NULL) {
13644 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13645 		return (SATA_RETRY); /* may retry later */
13646 	}
13647 	/* address is needed now */
13648 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13649 
13650 	/*
13651 	 * Allocate buffer for Identify Data return data
13652 	 */
13653 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13654 	if (bp == NULL) {
13655 		sata_pkt_free(spx);
13656 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13657 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13658 		    "sata_fetch_device_identify_data: "
13659 		    "cannot allocate buffer for ID"));
13660 		return (SATA_RETRY); /* may retry later */
13661 	}
13662 
13663 	/* Fill sata_pkt */
13664 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13665 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13666 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13667 	/* Synchronous mode, no callback */
13668 	spkt->satapkt_comp = NULL;
13669 	/* Timeout 30s */
13670 	spkt->satapkt_time = sata_default_pkt_time;
13671 
13672 	scmd = &spkt->satapkt_cmd;
13673 	scmd->satacmd_bp = bp;
13674 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13675 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13676 
13677 	/* Build Identify Device cmd in the sata_pkt */
13678 	scmd->satacmd_addr_type = 0;		/* N/A */
13679 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13680 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13681 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13682 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13683 	scmd->satacmd_features_reg = 0;		/* N/A */
13684 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13685 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13686 		/* Identify Packet Device cmd */
13687 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13688 	} else {
13689 		/* Identify Device cmd - mandatory for all other devices */
13690 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13691 	}
13692 
13693 	/* Send pkt to SATA HBA driver */
13694 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13695 
13696 #ifdef SATA_INJECT_FAULTS
13697 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13698 #endif
13699 
13700 	if (rval == SATA_TRAN_ACCEPTED &&
13701 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13702 		if (spx->txlt_buf_dma_handle != NULL) {
13703 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13704 			    DDI_DMA_SYNC_FORKERNEL);
13705 			ASSERT(rval == DDI_SUCCESS);
13706 			if (sata_check_for_dma_error(dip, spx)) {
13707 				ddi_fm_service_impact(dip,
13708 				    DDI_SERVICE_UNAFFECTED);
13709 				rval = SATA_RETRY;
13710 				goto fail;
13711 			}
13712 
13713 		}
13714 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13715 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13716 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13717 			    "SATA disk device at port %d - "
13718 			    "partial Identify Data",
13719 			    sdinfo->satadrv_addr.cport));
13720 			rval = SATA_RETRY; /* may retry later */
13721 			goto fail;
13722 		}
13723 		/* Update sata_drive_info */
13724 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13725 		    sizeof (sata_id_t));
13726 
13727 		sdinfo->satadrv_features_support = 0;
13728 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13729 			/*
13730 			 * Retrieve capacity (disks only) and addressing mode
13731 			 */
13732 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13733 		} else {
13734 			/*
13735 			 * For ATAPI devices one would have to issue
13736 			 * Get Capacity cmd for media capacity. Not here.
13737 			 */
13738 			sdinfo->satadrv_capacity = 0;
13739 			/*
13740 			 * Check what cdb length is supported
13741 			 */
13742 			if ((sdinfo->satadrv_id.ai_config &
13743 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13744 				sdinfo->satadrv_atapi_cdb_len = 16;
13745 			else
13746 				sdinfo->satadrv_atapi_cdb_len = 12;
13747 		}
13748 		/* Setup supported features flags */
13749 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13750 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13751 
13752 		/* Check for SATA GEN and NCQ support */
13753 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13754 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13755 			/* SATA compliance */
13756 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13757 				sdinfo->satadrv_features_support |=
13758 				    SATA_DEV_F_NCQ;
13759 			if (sdinfo->satadrv_id.ai_satacap &
13760 			    (SATA_1_SPEED | SATA_2_SPEED)) {
13761 				if (sdinfo->satadrv_id.ai_satacap &
13762 				    SATA_2_SPEED)
13763 					sdinfo->satadrv_features_support |=
13764 					    SATA_DEV_F_SATA2;
13765 				if (sdinfo->satadrv_id.ai_satacap &
13766 				    SATA_1_SPEED)
13767 					sdinfo->satadrv_features_support |=
13768 					    SATA_DEV_F_SATA1;
13769 			} else {
13770 				sdinfo->satadrv_features_support |=
13771 				    SATA_DEV_F_SATA1;
13772 			}
13773 		}
13774 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13775 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13776 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13777 
13778 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13779 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13780 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13781 			++sdinfo->satadrv_queue_depth;
13782 			/* Adjust according to controller capabilities */
13783 			sdinfo->satadrv_max_queue_depth = MIN(
13784 			    sdinfo->satadrv_queue_depth,
13785 			    SATA_QDEPTH(sata_hba_inst));
13786 			/* Adjust according to global queue depth limit */
13787 			sdinfo->satadrv_max_queue_depth = MIN(
13788 			    sdinfo->satadrv_max_queue_depth,
13789 			    sata_current_max_qdepth);
13790 			if (sdinfo->satadrv_max_queue_depth == 0)
13791 				sdinfo->satadrv_max_queue_depth = 1;
13792 		} else
13793 			sdinfo->satadrv_max_queue_depth = 1;
13794 
13795 		rval = SATA_SUCCESS;
13796 	} else {
13797 		/*
13798 		 * Woops, no Identify Data.
13799 		 */
13800 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13801 			rval = SATA_RETRY; /* may retry later */
13802 		} else if (rval == SATA_TRAN_ACCEPTED) {
13803 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13804 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13805 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13806 			    spkt->satapkt_reason == SATA_PKT_RESET)
13807 				rval = SATA_RETRY; /* may retry later */
13808 			else
13809 				rval = SATA_FAILURE;
13810 		} else {
13811 			rval = SATA_FAILURE;
13812 		}
13813 	}
13814 fail:
13815 	/* Free allocated resources */
13816 	sata_free_local_buffer(spx);
13817 	sata_pkt_free(spx);
13818 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13819 
13820 	return (rval);
13821 }
13822 
13823 
13824 /*
13825  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13826  * UDMA mode is checked first, followed by MWDMA mode.
13827  * set correctly, so this function is setting it to the highest supported level.
13828  * Older SATA spec required that the device supports at least DMA 4 mode and
13829  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13830  * restriction has been removed.
13831  *
13832  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13833  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13834  *
13835  * NOTE: This function should be called only if DMA mode is supported.
13836  */
13837 static int
13838 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13839 {
13840 	sata_pkt_t *spkt;
13841 	sata_cmd_t *scmd;
13842 	sata_pkt_txlate_t *spx;
13843 	int i, mode;
13844 	uint8_t subcmd;
13845 	int rval = SATA_SUCCESS;
13846 
13847 	ASSERT(sdinfo != NULL);
13848 	ASSERT(sata_hba_inst != NULL);
13849 
13850 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13851 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13852 		/* Find highest Ultra DMA mode supported */
13853 		for (mode = 6; mode >= 0; --mode) {
13854 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13855 				break;
13856 		}
13857 #if 0
13858 		/* Left for historical reasons */
13859 		/*
13860 		 * Some initial version of SATA spec indicated that at least
13861 		 * UDMA mode 4 has to be supported. It is not mentioned in
13862 		 * SerialATA 2.6, so this restriction is removed.
13863 		 */
13864 		if (mode < 4)
13865 			return (SATA_FAILURE);
13866 #endif
13867 
13868 		/*
13869 		 * For disk, we're still going to set DMA mode whatever is
13870 		 * selected by default
13871 		 *
13872 		 * We saw an old maxtor sata drive will select Ultra DMA and
13873 		 * Multi-Word DMA simultaneouly by default, which is going
13874 		 * to cause DMA command timed out, so we need to select DMA
13875 		 * mode even when it's already done by default
13876 		 */
13877 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13878 
13879 			/* Find UDMA mode currently selected */
13880 			for (i = 6; i >= 0; --i) {
13881 				if (sdinfo->satadrv_id.ai_ultradma &
13882 				    (1 << (i + 8)))
13883 					break;
13884 			}
13885 			if (i >= mode)
13886 				/* Nothing to do */
13887 				return (SATA_SUCCESS);
13888 		}
13889 
13890 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13891 
13892 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13893 		/* Find highest MultiWord DMA mode supported */
13894 		for (mode = 2; mode >= 0; --mode) {
13895 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13896 				break;
13897 		}
13898 
13899 		/*
13900 		 * For disk, We're still going to set DMA mode whatever is
13901 		 * selected by default
13902 		 *
13903 		 * We saw an old maxtor sata drive will select Ultra DMA and
13904 		 * Multi-Word DMA simultaneouly by default, which is going
13905 		 * to cause DMA command timed out, so we need to select DMA
13906 		 * mode even when it's already done by default
13907 		 */
13908 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13909 
13910 			/* Find highest MultiWord DMA mode selected */
13911 			for (i = 2; i >= 0; --i) {
13912 				if (sdinfo->satadrv_id.ai_dworddma &
13913 				    (1 << (i + 8)))
13914 					break;
13915 			}
13916 			if (i >= mode)
13917 				/* Nothing to do */
13918 				return (SATA_SUCCESS);
13919 		}
13920 
13921 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13922 	} else
13923 		return (SATA_SUCCESS);
13924 
13925 	/*
13926 	 * Set DMA mode via SET FEATURES COMMAND.
13927 	 * Prepare packet for SET FEATURES COMMAND.
13928 	 */
13929 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13930 	spx->txlt_sata_hba_inst = sata_hba_inst;
13931 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13932 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13933 	if (spkt == NULL) {
13934 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13935 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
13936 		rval = SATA_FAILURE;
13937 		goto done;
13938 	}
13939 	/* Fill sata_pkt */
13940 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13941 	/* Timeout 30s */
13942 	spkt->satapkt_time = sata_default_pkt_time;
13943 	/* Synchronous mode, no callback, interrupts */
13944 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13945 	spkt->satapkt_comp = NULL;
13946 	scmd = &spkt->satapkt_cmd;
13947 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13948 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13949 	scmd->satacmd_addr_type = 0;
13950 	scmd->satacmd_device_reg = 0;
13951 	scmd->satacmd_status_reg = 0;
13952 	scmd->satacmd_error_reg = 0;
13953 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
13954 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
13955 	scmd->satacmd_sec_count_lsb = subcmd | mode;
13956 
13957 	/* Transfer command to HBA */
13958 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
13959 	    spkt) != SATA_TRAN_ACCEPTED ||
13960 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13961 		/* Pkt execution failed */
13962 		rval = SATA_FAILURE;
13963 	}
13964 done:
13965 
13966 	/* Free allocated resources */
13967 	if (spkt != NULL)
13968 		sata_pkt_free(spx);
13969 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
13970 
13971 	return (rval);
13972 }
13973 
13974 
13975 /*
13976  * Set device caching mode.
13977  * One of the following operations should be specified:
13978  * SATAC_SF_ENABLE_READ_AHEAD
13979  * SATAC_SF_DISABLE_READ_AHEAD
13980  * SATAC_SF_ENABLE_WRITE_CACHE
13981  * SATAC_SF_DISABLE_WRITE_CACHE
13982  *
13983  * If operation fails, system log messgage is emitted.
13984  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
13985  * command was sent but did not succeed, and SATA_FAILURE otherwise.
13986  */
13987 
13988 static int
13989 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
13990     int cache_op)
13991 {
13992 	sata_pkt_t *spkt;
13993 	sata_cmd_t *scmd;
13994 	sata_pkt_txlate_t *spx;
13995 	int rval = SATA_SUCCESS;
13996 	int hba_rval;
13997 	char *infop;
13998 
13999 	ASSERT(sdinfo != NULL);
14000 	ASSERT(sata_hba_inst != NULL);
14001 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14002 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14003 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14004 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14005 
14006 
14007 	/* Prepare packet for SET FEATURES COMMAND */
14008 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14009 	spx->txlt_sata_hba_inst = sata_hba_inst;
14010 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14011 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14012 	if (spkt == NULL) {
14013 		rval = SATA_FAILURE;
14014 		goto failure;
14015 	}
14016 	/* Fill sata_pkt */
14017 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14018 	/* Timeout 30s */
14019 	spkt->satapkt_time = sata_default_pkt_time;
14020 	/* Synchronous mode, no callback, interrupts */
14021 	spkt->satapkt_op_mode =
14022 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14023 	spkt->satapkt_comp = NULL;
14024 	scmd = &spkt->satapkt_cmd;
14025 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14026 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14027 	scmd->satacmd_addr_type = 0;
14028 	scmd->satacmd_device_reg = 0;
14029 	scmd->satacmd_status_reg = 0;
14030 	scmd->satacmd_error_reg = 0;
14031 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14032 	scmd->satacmd_features_reg = cache_op;
14033 
14034 	/* Transfer command to HBA */
14035 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14036 	    SATA_DIP(sata_hba_inst), spkt);
14037 
14038 #ifdef SATA_INJECT_FAULTS
14039 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14040 #endif
14041 
14042 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14043 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14044 		/* Pkt execution failed */
14045 		switch (cache_op) {
14046 		case SATAC_SF_ENABLE_READ_AHEAD:
14047 			infop = "enabling read ahead failed";
14048 			break;
14049 		case SATAC_SF_DISABLE_READ_AHEAD:
14050 			infop = "disabling read ahead failed";
14051 			break;
14052 		case SATAC_SF_ENABLE_WRITE_CACHE:
14053 			infop = "enabling write cache failed";
14054 			break;
14055 		case SATAC_SF_DISABLE_WRITE_CACHE:
14056 			infop = "disabling write cache failed";
14057 			break;
14058 		}
14059 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14060 		rval = SATA_RETRY;
14061 	}
14062 failure:
14063 	/* Free allocated resources */
14064 	if (spkt != NULL)
14065 		sata_pkt_free(spx);
14066 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14067 	return (rval);
14068 }
14069 
14070 /*
14071  * Set Removable Media Status Notification (enable/disable)
14072  * state == 0 , disable
14073  * state != 0 , enable
14074  *
14075  * If operation fails, system log messgage is emitted.
14076  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14077  */
14078 
14079 static int
14080 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14081     int state)
14082 {
14083 	sata_pkt_t *spkt;
14084 	sata_cmd_t *scmd;
14085 	sata_pkt_txlate_t *spx;
14086 	int rval = SATA_SUCCESS;
14087 	char *infop;
14088 
14089 	ASSERT(sdinfo != NULL);
14090 	ASSERT(sata_hba_inst != NULL);
14091 
14092 	/* Prepare packet for SET FEATURES COMMAND */
14093 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14094 	spx->txlt_sata_hba_inst = sata_hba_inst;
14095 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14096 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14097 	if (spkt == NULL) {
14098 		rval = SATA_FAILURE;
14099 		goto failure;
14100 	}
14101 	/* Fill sata_pkt */
14102 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14103 	/* Timeout 30s */
14104 	spkt->satapkt_time = sata_default_pkt_time;
14105 	/* Synchronous mode, no callback, interrupts */
14106 	spkt->satapkt_op_mode =
14107 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14108 	spkt->satapkt_comp = NULL;
14109 	scmd = &spkt->satapkt_cmd;
14110 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14111 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14112 	scmd->satacmd_addr_type = 0;
14113 	scmd->satacmd_device_reg = 0;
14114 	scmd->satacmd_status_reg = 0;
14115 	scmd->satacmd_error_reg = 0;
14116 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14117 	if (state == 0)
14118 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14119 	else
14120 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14121 
14122 	/* Transfer command to HBA */
14123 	if (((*SATA_START_FUNC(sata_hba_inst))(
14124 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14125 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14126 		/* Pkt execution failed */
14127 		if (state == 0)
14128 			infop = "disabling Removable Media Status "
14129 			    "Notification failed";
14130 		else
14131 			infop = "enabling Removable Media Status "
14132 			    "Notification failed";
14133 
14134 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14135 		rval = SATA_FAILURE;
14136 	}
14137 failure:
14138 	/* Free allocated resources */
14139 	if (spkt != NULL)
14140 		sata_pkt_free(spx);
14141 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14142 	return (rval);
14143 }
14144 
14145 
14146 /*
14147  * Update state and copy port ss* values from passed sata_device structure.
14148  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14149  * configuration struct.
14150  *
14151  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14152  * regardless of the state in device argument.
14153  *
14154  * Port mutex should be held while calling this function.
14155  */
14156 static void
14157 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14158     sata_device_t *sata_device)
14159 {
14160 	sata_cport_info_t *cportinfo;
14161 
14162 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14163 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14164 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14165 		    sata_device->satadev_addr.cport)
14166 			return;
14167 
14168 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14169 		    sata_device->satadev_addr.cport);
14170 
14171 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14172 		cportinfo->cport_scr = sata_device->satadev_scr;
14173 
14174 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14175 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14176 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14177 		cportinfo->cport_state |=
14178 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14179 	}
14180 }
14181 
14182 void
14183 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14184     sata_device_t *sata_device)
14185 {
14186 	sata_pmport_info_t *pmportinfo;
14187 
14188 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14189 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14190 	    SATA_NUM_PMPORTS(sata_hba_inst,
14191 	    sata_device->satadev_addr.cport) <
14192 	    sata_device->satadev_addr.pmport) {
14193 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14194 		    "sata_update_port_info: error address %p.",
14195 		    &sata_device->satadev_addr);
14196 		return;
14197 	}
14198 
14199 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14200 	    sata_device->satadev_addr.cport,
14201 	    sata_device->satadev_addr.pmport);
14202 
14203 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14204 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14205 
14206 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14207 	pmportinfo->pmport_state &=
14208 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14209 	pmportinfo->pmport_state |=
14210 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14211 }
14212 
14213 /*
14214  * Extract SATA port specification from an IOCTL argument.
14215  *
14216  * This function return the port the user land send us as is, unless it
14217  * cannot retrieve port spec, then -1 is returned.
14218  *
14219  * Support port multiplier.
14220  */
14221 static int32_t
14222 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14223 {
14224 	int32_t port;
14225 
14226 	/* Extract port number from nvpair in dca structure  */
14227 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14228 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14229 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14230 		    port));
14231 		port = -1;
14232 	}
14233 
14234 	return (port);
14235 }
14236 
14237 /*
14238  * Get dev_info_t pointer to the device node pointed to by port argument.
14239  * NOTE: target argument is a value used in ioctls to identify
14240  * the AP - it is not a sata_address.
14241  * It is a combination of cport, pmport and address qualifier, encodded same
14242  * way as a scsi target number.
14243  * At this moment it carries only cport number.
14244  *
14245  * PMult hotplug is supported now.
14246  *
14247  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14248  */
14249 
14250 static dev_info_t *
14251 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14252 {
14253 	dev_info_t	*cdip = NULL;
14254 	int		target, tgt;
14255 	int 		circ;
14256 	uint8_t		qual;
14257 
14258 	sata_hba_inst_t	*sata_hba_inst;
14259 	scsi_hba_tran_t *scsi_hba_tran;
14260 
14261 	/* Get target id */
14262 	scsi_hba_tran = ddi_get_driver_private(dip);
14263 	if (scsi_hba_tran == NULL)
14264 		return (NULL);
14265 
14266 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14267 
14268 	if (sata_hba_inst == NULL)
14269 		return (NULL);
14270 
14271 	/* Identify a port-mult by cport_info.cport_dev_type */
14272 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14273 		qual = SATA_ADDR_DPMPORT;
14274 	else
14275 		qual = SATA_ADDR_DCPORT;
14276 
14277 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14278 
14279 	/* Retrieve target dip */
14280 	ndi_devi_enter(dip, &circ);
14281 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14282 		dev_info_t *next = ddi_get_next_sibling(cdip);
14283 
14284 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14285 		    DDI_PROP_DONTPASS, "target", -1);
14286 		if (tgt == -1) {
14287 			/*
14288 			 * This is actually an error condition, but not
14289 			 * a fatal one. Just continue the search.
14290 			 */
14291 			cdip = next;
14292 			continue;
14293 		}
14294 
14295 		if (tgt == target)
14296 			break;
14297 
14298 		cdip = next;
14299 	}
14300 	ndi_devi_exit(dip, circ);
14301 
14302 	return (cdip);
14303 }
14304 
14305 /*
14306  * Get dev_info_t pointer to the device node pointed to by port argument.
14307  * NOTE: target argument is a value used in ioctls to identify
14308  * the AP - it is not a sata_address.
14309  * It is a combination of cport, pmport and address qualifier, encoded same
14310  * way as a scsi target number.
14311  *
14312  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14313  */
14314 
14315 static dev_info_t *
14316 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14317 {
14318 	dev_info_t	*cdip = NULL;
14319 	int		target, tgt;
14320 	int 		circ;
14321 
14322 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14323 
14324 	ndi_devi_enter(dip, &circ);
14325 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14326 		dev_info_t *next = ddi_get_next_sibling(cdip);
14327 
14328 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14329 		    DDI_PROP_DONTPASS, "target", -1);
14330 		if (tgt == -1) {
14331 			/*
14332 			 * This is actually an error condition, but not
14333 			 * a fatal one. Just continue the search.
14334 			 */
14335 			cdip = next;
14336 			continue;
14337 		}
14338 
14339 		if (tgt == target)
14340 			break;
14341 
14342 		cdip = next;
14343 	}
14344 	ndi_devi_exit(dip, circ);
14345 
14346 	return (cdip);
14347 }
14348 
14349 /*
14350  * Process sata port disconnect request.
14351  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14352  * before this request. Nevertheless, if a device is still configured,
14353  * we need to attempt to offline and unconfigure device.
14354  * Regardless of the unconfigure operation results the port is marked as
14355  * deactivated and no access to the attached device is possible.
14356  * If the target node remains because unconfigure operation failed, its state
14357  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14358  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14359  * the device and remove old target node.
14360  *
14361  * This function invokes sata_hba_inst->satahba_tran->
14362  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14363  * If successful, the device structure (if any) attached to the specified port
14364  * is removed and state of the port marked appropriately.
14365  * Failure of the port_deactivate may keep port in the physically active state,
14366  * or may fail the port.
14367  *
14368  * NOTE: Port multiplier is supported.
14369  */
14370 
14371 static int
14372 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14373     sata_device_t *sata_device)
14374 {
14375 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14376 	sata_cport_info_t *cportinfo = NULL;
14377 	sata_pmport_info_t *pmportinfo = NULL;
14378 	sata_pmult_info_t *pmultinfo = NULL;
14379 	sata_device_t subsdevice;
14380 	int cport, pmport, qual;
14381 	int rval = SATA_SUCCESS;
14382 	int npmport = 0;
14383 	int rv = 0;
14384 
14385 	cport = sata_device->satadev_addr.cport;
14386 	pmport = sata_device->satadev_addr.pmport;
14387 	qual = sata_device->satadev_addr.qual;
14388 
14389 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14390 	if (qual == SATA_ADDR_DCPORT)
14391 		qual = SATA_ADDR_CPORT;
14392 	else
14393 		qual = SATA_ADDR_PMPORT;
14394 
14395 	/*
14396 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14397 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14398 	 * Do the sanity check.
14399 	 */
14400 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14401 		/* No physical port deactivation supported. */
14402 		return (EINVAL);
14403 	}
14404 
14405 	/* Check the current state of the port */
14406 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14407 	    (SATA_DIP(sata_hba_inst), sata_device);
14408 
14409 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14410 
14411 	/*
14412 	 * Processing port mulitiplier
14413 	 */
14414 	if (qual == SATA_ADDR_CPORT &&
14415 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14416 		mutex_enter(&cportinfo->cport_mutex);
14417 
14418 		/* Check controller port status */
14419 		sata_update_port_info(sata_hba_inst, sata_device);
14420 		if (rval != SATA_SUCCESS ||
14421 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14422 			/*
14423 			 * Device port status is unknown or it is in failed
14424 			 * state
14425 			 */
14426 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14427 			    SATA_PSTATE_FAILED;
14428 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14429 			    "sata_hba_ioctl: connect: failed to deactivate "
14430 			    "SATA port %d", cport);
14431 			mutex_exit(&cportinfo->cport_mutex);
14432 			return (EIO);
14433 		}
14434 
14435 		/* Disconnect all sub-devices. */
14436 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14437 		if (pmultinfo != NULL) {
14438 
14439 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14440 			    sata_hba_inst, cport); npmport ++) {
14441 				subsdinfo = SATA_PMPORT_DRV_INFO(
14442 				    sata_hba_inst, cport, npmport);
14443 				if (subsdinfo == NULL)
14444 					continue;
14445 
14446 				subsdevice.satadev_addr = subsdinfo->
14447 				    satadrv_addr;
14448 
14449 				mutex_exit(&cportinfo->cport_mutex);
14450 				if (sata_ioctl_disconnect(sata_hba_inst,
14451 				    &subsdevice) == SATA_SUCCESS) {
14452 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14453 					"[Remove] device at port %d:%d "
14454 					"successfully.", cport, npmport);
14455 				}
14456 				mutex_enter(&cportinfo->cport_mutex);
14457 			}
14458 		}
14459 
14460 		/* Disconnect the port multiplier */
14461 		cportinfo->cport_state &= ~SATA_STATE_READY;
14462 		mutex_exit(&cportinfo->cport_mutex);
14463 
14464 		sata_device->satadev_addr.qual = qual;
14465 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14466 		    (SATA_DIP(sata_hba_inst), sata_device);
14467 
14468 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14469 		    SE_NO_HINT);
14470 
14471 		mutex_enter(&cportinfo->cport_mutex);
14472 		sata_update_port_info(sata_hba_inst, sata_device);
14473 		if (rval != SATA_SUCCESS &&
14474 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14475 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14476 			rv = EIO;
14477 		} else {
14478 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14479 		}
14480 		mutex_exit(&cportinfo->cport_mutex);
14481 
14482 		return (rv);
14483 	}
14484 
14485 	/*
14486 	 * Process non-port-multiplier device - it could be a drive connected
14487 	 * to a port multiplier port or a controller port.
14488 	 */
14489 	if (qual == SATA_ADDR_PMPORT) {
14490 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14491 		mutex_enter(&pmportinfo->pmport_mutex);
14492 		sata_update_pmport_info(sata_hba_inst, sata_device);
14493 		if (rval != SATA_SUCCESS ||
14494 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14495 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14496 			    SATA_PSTATE_FAILED;
14497 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14498 			    "sata_hba_ioctl: connect: failed to deactivate "
14499 			    "SATA port %d:%d", cport, pmport);
14500 			mutex_exit(&pmportinfo->pmport_mutex);
14501 			return (EIO);
14502 		}
14503 
14504 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14505 			sdinfo = pmportinfo->pmport_sata_drive;
14506 			ASSERT(sdinfo != NULL);
14507 		}
14508 
14509 		/*
14510 		 * Set port's dev_state to not ready - this will disable
14511 		 * an access to a potentially attached device.
14512 		 */
14513 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14514 
14515 		/* Remove and release sata_drive info structure. */
14516 		if (sdinfo != NULL) {
14517 			if ((sdinfo->satadrv_type &
14518 			    SATA_VALID_DEV_TYPE) != 0) {
14519 				/*
14520 				 * If a target node exists, try to offline
14521 				 * a device and remove target node.
14522 				 */
14523 				mutex_exit(&pmportinfo->pmport_mutex);
14524 				(void) sata_offline_device(sata_hba_inst,
14525 				    sata_device, sdinfo);
14526 				mutex_enter(&pmportinfo->pmport_mutex);
14527 			}
14528 
14529 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14530 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14531 			(void) kmem_free((void *)sdinfo,
14532 			    sizeof (sata_drive_info_t));
14533 		}
14534 		mutex_exit(&pmportinfo->pmport_mutex);
14535 
14536 	} else if (qual == SATA_ADDR_CPORT) {
14537 		mutex_enter(&cportinfo->cport_mutex);
14538 		sata_update_port_info(sata_hba_inst, sata_device);
14539 		if (rval != SATA_SUCCESS ||
14540 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14541 			/*
14542 			 * Device port status is unknown or it is in failed
14543 			 * state
14544 			 */
14545 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14546 			    SATA_PSTATE_FAILED;
14547 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14548 			    "sata_hba_ioctl: connect: failed to deactivate "
14549 			    "SATA port %d", cport);
14550 			mutex_exit(&cportinfo->cport_mutex);
14551 			return (EIO);
14552 		}
14553 
14554 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14555 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14556 			ASSERT(pmultinfo != NULL);
14557 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14558 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14559 			ASSERT(sdinfo != NULL);
14560 		}
14561 		cportinfo->cport_state &= ~SATA_STATE_READY;
14562 
14563 		if (sdinfo != NULL) {
14564 			if ((sdinfo->satadrv_type &
14565 			    SATA_VALID_DEV_TYPE) != 0) {
14566 				/*
14567 				 * If a target node exists, try to offline
14568 				 * a device and remove target node.
14569 				 */
14570 				mutex_exit(&cportinfo->cport_mutex);
14571 				(void) sata_offline_device(sata_hba_inst,
14572 				    sata_device, sdinfo);
14573 				mutex_enter(&cportinfo->cport_mutex);
14574 			}
14575 
14576 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14577 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14578 			(void) kmem_free((void *)sdinfo,
14579 			    sizeof (sata_drive_info_t));
14580 		}
14581 		mutex_exit(&cportinfo->cport_mutex);
14582 	}
14583 
14584 	/* Just ask HBA driver to deactivate port */
14585 	sata_device->satadev_addr.qual = qual;
14586 
14587 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14588 	    (SATA_DIP(sata_hba_inst), sata_device);
14589 
14590 	/*
14591 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14592 	 * without the hint (to force listener to investivate the state).
14593 	 */
14594 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14595 	    SE_NO_HINT);
14596 
14597 	if (qual == SATA_ADDR_PMPORT) {
14598 		mutex_enter(&pmportinfo->pmport_mutex);
14599 		sata_update_pmport_info(sata_hba_inst, sata_device);
14600 
14601 		if (rval != SATA_SUCCESS &&
14602 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14603 			/*
14604 			 * Port deactivation failure - do not change port
14605 			 * state unless the state returned by HBA indicates a
14606 			 * port failure.
14607 			 *
14608 			 * NOTE: device structures were released, so devices
14609 			 * now are invisible! Port reset is needed to
14610 			 * re-enumerate devices.
14611 			 */
14612 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14613 			rv = EIO;
14614 		} else {
14615 			/*
14616 			 * Deactivation succeded. From now on the sata framework
14617 			 * will not care what is happening to the device, until
14618 			 * the port is activated again.
14619 			 */
14620 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14621 		}
14622 		mutex_exit(&pmportinfo->pmport_mutex);
14623 	} else if (qual == SATA_ADDR_CPORT) {
14624 		mutex_enter(&cportinfo->cport_mutex);
14625 		sata_update_port_info(sata_hba_inst, sata_device);
14626 
14627 		if (rval != SATA_SUCCESS &&
14628 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14629 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14630 			rv = EIO;
14631 		} else {
14632 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14633 		}
14634 		mutex_exit(&cportinfo->cport_mutex);
14635 	}
14636 
14637 	return (rv);
14638 }
14639 
14640 
14641 
14642 /*
14643  * Process sata port connect request
14644  * The sata cfgadm pluging will invoke this operation only if port was found
14645  * in the disconnect state (failed state is also treated as the disconnected
14646  * state).
14647  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14648  * sata_tran_hotplug_ops->sata_tran_port_activate().
14649  * If successful and a device is found attached to the port,
14650  * the initialization sequence is executed to attach a device structure to
14651  * a port structure. The state of the port and a device would be set
14652  * appropriately.
14653  * The device is not set in configured state (system-wise) by this operation.
14654  *
14655  * Note, that activating the port may generate link events,
14656  * so it is important that following processing and the
14657  * event processing does not interfere with each other!
14658  *
14659  * This operation may remove port failed state and will
14660  * try to make port active and in good standing.
14661  *
14662  * NOTE: Port multiplier is supported.
14663  */
14664 
14665 static int
14666 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14667     sata_device_t *sata_device)
14668 {
14669 	sata_pmport_info_t	*pmportinfo = NULL;
14670 	uint8_t cport, pmport, qual;
14671 	int rv = 0;
14672 
14673 	cport = sata_device->satadev_addr.cport;
14674 	pmport = sata_device->satadev_addr.pmport;
14675 	qual = sata_device->satadev_addr.qual;
14676 
14677 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14678 	if (qual == SATA_ADDR_DCPORT)
14679 		qual = SATA_ADDR_CPORT;
14680 	else
14681 		qual = SATA_ADDR_PMPORT;
14682 
14683 	if (qual == SATA_ADDR_PMPORT)
14684 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14685 
14686 	/*
14687 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14688 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14689 	 * Perform sanity check now.
14690 	 */
14691 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14692 		/* No physical port activation supported. */
14693 		return (EINVAL);
14694 	}
14695 
14696 	/* Just ask HBA driver to activate port */
14697 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14698 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14699 		/*
14700 		 * Port activation failure.
14701 		 */
14702 		if (qual == SATA_ADDR_CPORT) {
14703 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14704 			    cport)->cport_mutex);
14705 			sata_update_port_info(sata_hba_inst, sata_device);
14706 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14707 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14708 				    SATA_PSTATE_FAILED;
14709 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14710 				    "sata_hba_ioctl: connect: failed to "
14711 				    "activate SATA port %d", cport);
14712 			}
14713 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14714 			    cport)->cport_mutex);
14715 		} else { /* port multiplier device port */
14716 			mutex_enter(&pmportinfo->pmport_mutex);
14717 			sata_update_pmport_info(sata_hba_inst, sata_device);
14718 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14719 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14720 				    pmport) = SATA_PSTATE_FAILED;
14721 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14722 				    "sata_hba_ioctl: connect: failed to "
14723 				    "activate SATA port %d:%d", cport, pmport);
14724 			}
14725 			mutex_exit(&pmportinfo->pmport_mutex);
14726 		}
14727 		return (EIO);
14728 	}
14729 
14730 	/* Virgin port state - will be updated by the port re-probe. */
14731 	if (qual == SATA_ADDR_CPORT) {
14732 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14733 		    cport)->cport_mutex);
14734 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14735 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14736 		    cport)->cport_mutex);
14737 	} else { /* port multiplier device port */
14738 		mutex_enter(&pmportinfo->pmport_mutex);
14739 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14740 		mutex_exit(&pmportinfo->pmport_mutex);
14741 	}
14742 
14743 	/*
14744 	 * Probe the port to find its state and attached device.
14745 	 */
14746 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14747 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14748 		rv = EIO;
14749 
14750 	/*
14751 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14752 	 * without the hint
14753 	 */
14754 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14755 	    SE_NO_HINT);
14756 
14757 	/*
14758 	 * If there is a device attached to the port, emit
14759 	 * a message.
14760 	 */
14761 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14762 
14763 		if (qual == SATA_ADDR_CPORT) {
14764 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14765 				sata_log(sata_hba_inst, CE_WARN,
14766 				    "SATA port multiplier detected "
14767 				    "at port %d", cport);
14768 			} else {
14769 				sata_log(sata_hba_inst, CE_WARN,
14770 				    "SATA device detected at port %d", cport);
14771 				if (sata_device->satadev_type ==
14772 				    SATA_DTYPE_UNKNOWN) {
14773 				/*
14774 				 * A device was not successfully identified
14775 				 */
14776 				sata_log(sata_hba_inst, CE_WARN,
14777 				    "Could not identify SATA "
14778 				    "device at port %d", cport);
14779 				}
14780 			}
14781 		} else { /* port multiplier device port */
14782 			sata_log(sata_hba_inst, CE_WARN,
14783 			    "SATA device detected at port %d:%d",
14784 			    cport, pmport);
14785 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14786 				/*
14787 				 * A device was not successfully identified
14788 				 */
14789 				sata_log(sata_hba_inst, CE_WARN,
14790 				    "Could not identify SATA "
14791 				    "device at port %d:%d", cport, pmport);
14792 			}
14793 		}
14794 	}
14795 
14796 	return (rv);
14797 }
14798 
14799 
14800 /*
14801  * Process sata device unconfigure request.
14802  * The unconfigure operation uses generic nexus operation to
14803  * offline a device. It leaves a target device node attached.
14804  * and obviously sata_drive_info attached as well, because
14805  * from the hardware point of view nothing has changed.
14806  */
14807 static int
14808 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14809     sata_device_t *sata_device)
14810 {
14811 	int rv = 0;
14812 	dev_info_t *tdip;
14813 
14814 	/* We are addressing attached device, not a port */
14815 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14816 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14817 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14818 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14819 
14820 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14821 	    &sata_device->satadev_addr)) != NULL) {
14822 
14823 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14824 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14825 			    "sata_hba_ioctl: unconfigure: "
14826 			    "failed to unconfigure device at SATA port %d:%d",
14827 			    sata_device->satadev_addr.cport,
14828 			    sata_device->satadev_addr.pmport));
14829 			rv = EIO;
14830 		}
14831 		/*
14832 		 * The target node devi_state should be marked with
14833 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14834 		 * This would be the indication for cfgadm that
14835 		 * the AP node occupant state is 'unconfigured'.
14836 		 */
14837 
14838 	} else {
14839 		/*
14840 		 * This would indicate a failure on the part of cfgadm
14841 		 * to detect correct state of the node prior to this
14842 		 * call - one cannot unconfigure non-existing device.
14843 		 */
14844 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14845 		    "sata_hba_ioctl: unconfigure: "
14846 		    "attempt to unconfigure non-existing device "
14847 		    "at SATA port %d:%d",
14848 		    sata_device->satadev_addr.cport,
14849 		    sata_device->satadev_addr.pmport));
14850 		rv = ENXIO;
14851 	}
14852 	return (rv);
14853 }
14854 
14855 /*
14856  * Process sata device configure request
14857  * If port is in a failed state, operation is aborted - one has to use
14858  * an explicit connect or port activate request to try to get a port into
14859  * non-failed mode. Port reset wil also work in such situation.
14860  * If the port is in disconnected (shutdown) state, the connect operation is
14861  * attempted prior to any other action.
14862  * When port is in the active state, there is a device attached and the target
14863  * node exists, a device was most likely offlined.
14864  * If target node does not exist, a new target node is created. In both cases
14865  * an attempt is made to online (configure) the device.
14866  *
14867  * NOTE: Port multiplier is supported.
14868  */
14869 static int
14870 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14871     sata_device_t *sata_device)
14872 {
14873 	int cport, pmport, qual;
14874 	int rval;
14875 	boolean_t target = B_TRUE;
14876 	sata_cport_info_t *cportinfo;
14877 	sata_pmport_info_t *pmportinfo = NULL;
14878 	dev_info_t *tdip;
14879 	sata_drive_info_t *sdinfo;
14880 
14881 	cport = sata_device->satadev_addr.cport;
14882 	pmport = sata_device->satadev_addr.pmport;
14883 	qual = sata_device->satadev_addr.qual;
14884 
14885 	/* Get current port state */
14886 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14887 	    (SATA_DIP(sata_hba_inst), sata_device);
14888 
14889 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14890 	if (qual == SATA_ADDR_DPMPORT) {
14891 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14892 		mutex_enter(&pmportinfo->pmport_mutex);
14893 		sata_update_pmport_info(sata_hba_inst, sata_device);
14894 		if (rval != SATA_SUCCESS ||
14895 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14896 			/*
14897 			 * Obviously, device on a failed port is not visible
14898 			 */
14899 			mutex_exit(&pmportinfo->pmport_mutex);
14900 			return (ENXIO);
14901 		}
14902 		mutex_exit(&pmportinfo->pmport_mutex);
14903 	} else {
14904 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14905 		    cport)->cport_mutex);
14906 		sata_update_port_info(sata_hba_inst, sata_device);
14907 		if (rval != SATA_SUCCESS ||
14908 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14909 			/*
14910 			 * Obviously, device on a failed port is not visible
14911 			 */
14912 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14913 			    cport)->cport_mutex);
14914 			return (ENXIO);
14915 		}
14916 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14917 		    cport)->cport_mutex);
14918 	}
14919 
14920 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14921 		/* need to activate port */
14922 		target = B_FALSE;
14923 
14924 		/* Sanity check */
14925 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14926 			return (ENXIO);
14927 
14928 		/* Just let HBA driver to activate port */
14929 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14930 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14931 			/*
14932 			 * Port activation failure - do not change port state
14933 			 * unless the state returned by HBA indicates a port
14934 			 * failure.
14935 			 */
14936 			if (qual == SATA_ADDR_DPMPORT) {
14937 				mutex_enter(&pmportinfo->pmport_mutex);
14938 				sata_update_pmport_info(sata_hba_inst,
14939 				    sata_device);
14940 				if (sata_device->satadev_state &
14941 				    SATA_PSTATE_FAILED)
14942 					pmportinfo->pmport_state =
14943 					    SATA_PSTATE_FAILED;
14944 				mutex_exit(&pmportinfo->pmport_mutex);
14945 			} else {
14946 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14947 				    cport)->cport_mutex);
14948 				sata_update_port_info(sata_hba_inst,
14949 				    sata_device);
14950 				if (sata_device->satadev_state &
14951 				    SATA_PSTATE_FAILED)
14952 					cportinfo->cport_state =
14953 					    SATA_PSTATE_FAILED;
14954 				mutex_exit(&SATA_CPORT_INFO(
14955 				    sata_hba_inst, cport)->cport_mutex);
14956 			}
14957 		}
14958 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14959 		    "sata_hba_ioctl: configure: "
14960 		    "failed to activate SATA port %d:%d",
14961 		    cport, pmport));
14962 		return (EIO);
14963 	}
14964 	/*
14965 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14966 	 * without the hint.
14967 	 */
14968 	sata_gen_sysevent(sata_hba_inst,
14969 	    &sata_device->satadev_addr, SE_NO_HINT);
14970 
14971 	/* Virgin port state */
14972 	if (qual == SATA_ADDR_DPMPORT) {
14973 		mutex_enter(&pmportinfo->pmport_mutex);
14974 		pmportinfo->pmport_state = 0;
14975 		mutex_exit(&pmportinfo->pmport_mutex);
14976 	} else {
14977 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14978 		    cport)-> cport_mutex);
14979 		cportinfo->cport_state = 0;
14980 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14981 		    cport)->cport_mutex);
14982 	}
14983 	/*
14984 	 * Always reprobe port, to get current device info.
14985 	 */
14986 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14987 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14988 		return (EIO);
14989 
14990 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
14991 		if (qual == SATA_ADDR_DPMPORT) {
14992 			/*
14993 			 * That's the transition from "inactive" port
14994 			 * to active one with device attached.
14995 			 */
14996 			sata_log(sata_hba_inst, CE_WARN,
14997 			    "SATA device detected at port %d:%d",
14998 			    cport, pmport);
14999 		} else {
15000 			/*
15001 			 * When PM is attached to the cport and cport is
15002 			 * activated, every PM device port needs to be reprobed.
15003 			 * We need to emit message for all devices detected
15004 			 * at port multiplier's device ports.
15005 			 * Add such code here.
15006 			 * For now, just inform about device attached to
15007 			 * cport.
15008 			 */
15009 			sata_log(sata_hba_inst, CE_WARN,
15010 			    "SATA device detected at port %d", cport);
15011 		}
15012 	}
15013 
15014 	/*
15015 	 * This is where real configuration operation starts.
15016 	 *
15017 	 * When PM is attached to the cport and cport is activated,
15018 	 * devices attached PM device ports may have to be configured
15019 	 * explicitly. This may change when port multiplier is supported.
15020 	 * For now, configure only disks and other valid target devices.
15021 	 */
15022 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15023 		if (qual == SATA_ADDR_DCPORT) {
15024 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15025 				/*
15026 				 * A device was not successfully identified
15027 				 */
15028 				sata_log(sata_hba_inst, CE_WARN,
15029 				    "Could not identify SATA "
15030 				    "device at port %d", cport);
15031 			}
15032 		} else { /* port multiplier device port */
15033 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15034 				/*
15035 				 * A device was not successfully identified
15036 				 */
15037 				sata_log(sata_hba_inst, CE_WARN,
15038 				    "Could not identify SATA "
15039 				    "device at port %d:%d", cport, pmport);
15040 			}
15041 		}
15042 		return (ENXIO);		/* No device to configure */
15043 	}
15044 
15045 	/*
15046 	 * Here we may have a device in reset condition,
15047 	 * but because we are just configuring it, there is
15048 	 * no need to process the reset other than just
15049 	 * to clear device reset condition in the HBA driver.
15050 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15051 	 * cause a first command sent the HBA driver with the request
15052 	 * to clear device reset condition.
15053 	 */
15054 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15055 	if (qual == SATA_ADDR_DPMPORT)
15056 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15057 	else
15058 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15059 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15060 	if (sdinfo == NULL) {
15061 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15062 		return (ENXIO);
15063 	}
15064 	if (sdinfo->satadrv_event_flags &
15065 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15066 		sdinfo->satadrv_event_flags = 0;
15067 	}
15068 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15069 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15070 
15071 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15072 	    &sata_device->satadev_addr)) != NULL) {
15073 		/*
15074 		 * Target node exists. Verify, that it belongs
15075 		 * to existing, attached device and not to
15076 		 * a removed device.
15077 		 */
15078 		if (sata_check_device_removed(tdip) == B_TRUE) {
15079 			if (qual == SATA_ADDR_DPMPORT)
15080 				sata_log(sata_hba_inst, CE_WARN,
15081 				    "SATA device at port %d cannot be "
15082 				    "configured. "
15083 				    "Application(s) accessing "
15084 				    "previously attached device "
15085 				    "have to release it before newly "
15086 				    "inserted device can be made accessible.",
15087 				    cport);
15088 			else
15089 				sata_log(sata_hba_inst, CE_WARN,
15090 				    "SATA device at port %d:%d cannot be"
15091 				    "configured. "
15092 				    "Application(s) accessing "
15093 				    "previously attached device "
15094 				    "have to release it before newly "
15095 				    "inserted device can be made accessible.",
15096 				    cport, pmport);
15097 			return (EIO);
15098 		}
15099 		/*
15100 		 * Device was not removed and re-inserted.
15101 		 * Try to online it.
15102 		 */
15103 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15104 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15105 			    "sata_hba_ioctl: configure: "
15106 			    "onlining device at SATA port "
15107 			    "%d:%d failed", cport, pmport));
15108 			return (EIO);
15109 		}
15110 
15111 		if (qual == SATA_ADDR_DPMPORT) {
15112 			mutex_enter(&pmportinfo->pmport_mutex);
15113 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15114 			mutex_exit(&pmportinfo->pmport_mutex);
15115 		} else {
15116 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15117 			    cport)->cport_mutex);
15118 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15119 			mutex_exit(&SATA_CPORT_INFO(
15120 			    sata_hba_inst, cport)->cport_mutex);
15121 		}
15122 	} else {
15123 		/*
15124 		 * No target node - need to create a new target node.
15125 		 */
15126 		if (qual == SATA_ADDR_DPMPORT) {
15127 			mutex_enter(&pmportinfo->pmport_mutex);
15128 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15129 			mutex_exit(&pmportinfo->pmport_mutex);
15130 		} else {
15131 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15132 			    cport_mutex);
15133 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15134 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15135 			    cport_mutex);
15136 		}
15137 
15138 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15139 		    sata_hba_inst, &sata_device->satadev_addr);
15140 		if (tdip == NULL) {
15141 			/* Configure operation failed */
15142 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15143 			    "sata_hba_ioctl: configure: "
15144 			    "configuring SATA device at port %d:%d "
15145 			    "failed", cport, pmport));
15146 			return (EIO);
15147 		}
15148 	}
15149 	return (0);
15150 }
15151 
15152 
15153 /*
15154  * Process ioctl deactivate port request.
15155  * Arbitrarily unconfigure attached device, if any.
15156  * Even if the unconfigure fails, proceed with the
15157  * port deactivation.
15158  *
15159  * NOTE: Port Multiplier is supported now.
15160  */
15161 
15162 static int
15163 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15164     sata_device_t *sata_device)
15165 {
15166 	int cport, pmport, qual;
15167 	int rval, rv = 0;
15168 	int npmport;
15169 	sata_cport_info_t *cportinfo;
15170 	sata_pmport_info_t *pmportinfo;
15171 	sata_pmult_info_t *pmultinfo;
15172 	dev_info_t *tdip;
15173 	sata_drive_info_t *sdinfo = NULL;
15174 	sata_device_t subsdevice;
15175 
15176 	/* Sanity check */
15177 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15178 		return (ENOTSUP);
15179 
15180 	cport = sata_device->satadev_addr.cport;
15181 	pmport = sata_device->satadev_addr.pmport;
15182 	qual = sata_device->satadev_addr.qual;
15183 
15184 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15185 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15186 	if (qual == SATA_ADDR_DCPORT)
15187 		qual = SATA_ADDR_CPORT;
15188 	else
15189 		qual = SATA_ADDR_PMPORT;
15190 
15191 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15192 	if (qual == SATA_ADDR_PMPORT)
15193 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15194 
15195 	/*
15196 	 * Processing port multiplier
15197 	 */
15198 	if (qual == SATA_ADDR_CPORT &&
15199 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15200 		mutex_enter(&cportinfo->cport_mutex);
15201 
15202 		/* Deactivate all sub-deices */
15203 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15204 		if (pmultinfo != NULL) {
15205 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15206 			    sata_hba_inst, cport); npmport++) {
15207 
15208 				subsdevice.satadev_addr.cport = cport;
15209 				subsdevice.satadev_addr.pmport =
15210 				    (uint8_t)npmport;
15211 				subsdevice.satadev_addr.qual =
15212 				    SATA_ADDR_DPMPORT;
15213 
15214 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15215 				    "sata_hba_ioctl: deactivate: trying to "
15216 				    "deactivate SATA port %d:%d",
15217 				    cport, npmport);
15218 
15219 				mutex_exit(&cportinfo->cport_mutex);
15220 				if (sata_ioctl_deactivate(sata_hba_inst,
15221 				    &subsdevice) == SATA_SUCCESS) {
15222 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15223 					    "[Deactivate] device at port %d:%d "
15224 					    "successfully.", cport, npmport);
15225 				}
15226 				mutex_enter(&cportinfo->cport_mutex);
15227 			}
15228 		}
15229 
15230 		/* Deactivate the port multiplier now. */
15231 		cportinfo->cport_state &= ~SATA_STATE_READY;
15232 		mutex_exit(&cportinfo->cport_mutex);
15233 
15234 		sata_device->satadev_addr.qual = qual;
15235 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15236 		    (SATA_DIP(sata_hba_inst), sata_device);
15237 
15238 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15239 		    SE_NO_HINT);
15240 
15241 		mutex_enter(&cportinfo->cport_mutex);
15242 		sata_update_port_info(sata_hba_inst, sata_device);
15243 		if (rval != SATA_SUCCESS) {
15244 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15245 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15246 			}
15247 			rv = EIO;
15248 		} else {
15249 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15250 		}
15251 		mutex_exit(&cportinfo->cport_mutex);
15252 
15253 		return (rv);
15254 	}
15255 
15256 	/*
15257 	 * Process non-port-multiplier device - it could be a drive connected
15258 	 * to a port multiplier port or a controller port.
15259 	 */
15260 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15261 	if (qual == SATA_ADDR_CPORT) {
15262 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15263 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15264 			/* deal only with valid devices */
15265 			if ((cportinfo->cport_dev_type &
15266 			    SATA_VALID_DEV_TYPE) != 0)
15267 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15268 		}
15269 		cportinfo->cport_state &= ~SATA_STATE_READY;
15270 	} else {
15271 		/* Port multiplier device port */
15272 		mutex_enter(&pmportinfo->pmport_mutex);
15273 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15274 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15275 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15276 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15277 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15278 		mutex_exit(&pmportinfo->pmport_mutex);
15279 	}
15280 
15281 	if (sdinfo != NULL) {
15282 		/*
15283 		 * If a target node exists, try to offline a device and
15284 		 * to remove a target node.
15285 		 */
15286 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15287 		    cport_mutex);
15288 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15289 		    &sata_device->satadev_addr);
15290 		if (tdip != NULL) {
15291 			/* target node exist */
15292 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15293 			    "sata_hba_ioctl: port deactivate: "
15294 			    "target node exists.", NULL);
15295 
15296 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15297 			    NDI_SUCCESS) {
15298 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15299 				    "sata_hba_ioctl: port deactivate: "
15300 				    "failed to unconfigure device at port "
15301 				    "%d:%d before deactivating the port",
15302 				    cport, pmport));
15303 				/*
15304 				 * Set DEVICE REMOVED state in the target
15305 				 * node. It will prevent an access to
15306 				 * the device even when a new device is
15307 				 * attached, until the old target node is
15308 				 * released, removed and recreated for a new
15309 				 * device.
15310 				 */
15311 				sata_set_device_removed(tdip);
15312 
15313 				/*
15314 				 * Instruct the event daemon to try the
15315 				 * target node cleanup later.
15316 				 */
15317 				sata_set_target_node_cleanup(sata_hba_inst,
15318 				    &sata_device->satadev_addr);
15319 			}
15320 		}
15321 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15322 		    cport_mutex);
15323 		/*
15324 		 * In any case, remove and release sata_drive_info
15325 		 * structure.
15326 		 */
15327 		if (qual == SATA_ADDR_CPORT) {
15328 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15329 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15330 		} else { /* port multiplier device port */
15331 			mutex_enter(&pmportinfo->pmport_mutex);
15332 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15333 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15334 			mutex_exit(&pmportinfo->pmport_mutex);
15335 		}
15336 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15337 	}
15338 
15339 	if (qual == SATA_ADDR_CPORT) {
15340 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15341 		    SATA_STATE_PROBING);
15342 	} else if (qual == SATA_ADDR_PMPORT) {
15343 		mutex_enter(&pmportinfo->pmport_mutex);
15344 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15345 		    SATA_STATE_PROBING);
15346 		mutex_exit(&pmportinfo->pmport_mutex);
15347 	}
15348 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15349 
15350 	/* Just let HBA driver to deactivate port */
15351 	sata_device->satadev_addr.qual = qual;
15352 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15353 	    (SATA_DIP(sata_hba_inst), sata_device);
15354 
15355 	/*
15356 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15357 	 * without the hint
15358 	 */
15359 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15360 	    SE_NO_HINT);
15361 
15362 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15363 	sata_update_port_info(sata_hba_inst, sata_device);
15364 	if (qual == SATA_ADDR_CPORT) {
15365 		if (rval != SATA_SUCCESS) {
15366 			/*
15367 			 * Port deactivation failure - do not change port state
15368 			 * unless the state returned by HBA indicates a port
15369 			 * failure.
15370 			 */
15371 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15372 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15373 				    SATA_PSTATE_FAILED;
15374 			}
15375 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15376 			    "sata_hba_ioctl: port deactivate: "
15377 			    "cannot deactivate SATA port %d", cport));
15378 			rv = EIO;
15379 		} else {
15380 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15381 		}
15382 	} else {
15383 		mutex_enter(&pmportinfo->pmport_mutex);
15384 		if (rval != SATA_SUCCESS) {
15385 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15386 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15387 				    pmport) = SATA_PSTATE_FAILED;
15388 			}
15389 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15390 			    "sata_hba_ioctl: port deactivate: "
15391 			    "cannot deactivate SATA port %d:%d",
15392 			    cport, pmport));
15393 			rv = EIO;
15394 		} else {
15395 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15396 		}
15397 		mutex_exit(&pmportinfo->pmport_mutex);
15398 	}
15399 
15400 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15401 
15402 	return (rv);
15403 }
15404 
15405 /*
15406  * Process ioctl port activate request.
15407  *
15408  * NOTE: Port multiplier is supported now.
15409  */
15410 static int
15411 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15412     sata_device_t *sata_device)
15413 {
15414 	int cport, pmport, qual;
15415 	sata_cport_info_t *cportinfo;
15416 	sata_pmport_info_t *pmportinfo = NULL;
15417 	boolean_t dev_existed = B_TRUE;
15418 
15419 	/* Sanity check */
15420 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15421 		return (ENOTSUP);
15422 
15423 	cport = sata_device->satadev_addr.cport;
15424 	pmport = sata_device->satadev_addr.pmport;
15425 	qual = sata_device->satadev_addr.qual;
15426 
15427 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15428 
15429 	/*
15430 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15431 	 * is a device. But what we are dealing with is port/pmport.
15432 	 */
15433 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15434 	if (qual == SATA_ADDR_DCPORT)
15435 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15436 	else
15437 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15438 
15439 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15440 	if (qual == SATA_ADDR_PMPORT) {
15441 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15442 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15443 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15444 			dev_existed = B_FALSE;
15445 	} else { /* cport */
15446 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15447 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15448 			dev_existed = B_FALSE;
15449 	}
15450 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15451 
15452 	/* Just let HBA driver to activate port, if necessary */
15453 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15454 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15455 		/*
15456 		 * Port activation failure - do not change port state unless
15457 		 * the state returned by HBA indicates a port failure.
15458 		 */
15459 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15460 		    cport)->cport_mutex);
15461 		sata_update_port_info(sata_hba_inst, sata_device);
15462 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15463 			if (qual == SATA_ADDR_PMPORT) {
15464 				mutex_enter(&pmportinfo->pmport_mutex);
15465 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15466 				mutex_exit(&pmportinfo->pmport_mutex);
15467 			} else
15468 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15469 
15470 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15471 			    cport)->cport_mutex);
15472 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15473 			    "sata_hba_ioctl: port activate: cannot activate "
15474 			    "SATA port %d:%d", cport, pmport));
15475 			return (EIO);
15476 		}
15477 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15478 	}
15479 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15480 	if (qual == SATA_ADDR_PMPORT) {
15481 		mutex_enter(&pmportinfo->pmport_mutex);
15482 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15483 		mutex_exit(&pmportinfo->pmport_mutex);
15484 	} else
15485 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15486 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15487 
15488 	/*
15489 	 * Re-probe port to find its current state and possibly attached device.
15490 	 * Port re-probing may change the cportinfo device type if device is
15491 	 * found attached.
15492 	 * If port probing failed, the device type would be set to
15493 	 * SATA_DTYPE_NONE.
15494 	 */
15495 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15496 	    SATA_DEV_IDENTIFY_RETRY);
15497 
15498 	/*
15499 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15500 	 * without the hint.
15501 	 */
15502 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15503 	    SE_NO_HINT);
15504 
15505 	if (dev_existed == B_FALSE) {
15506 		if (qual == SATA_ADDR_PMPORT &&
15507 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15508 			/*
15509 			 * That's the transition from the "inactive" port state
15510 			 * or the active port without a device attached to the
15511 			 * active port state with a device attached.
15512 			 */
15513 			sata_log(sata_hba_inst, CE_WARN,
15514 			    "SATA device detected at port %d:%d",
15515 			    cport, pmport);
15516 		} else if (qual == SATA_ADDR_CPORT &&
15517 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15518 			/*
15519 			 * That's the transition from the "inactive" port state
15520 			 * or the active port without a device attached to the
15521 			 * active port state with a device attached.
15522 			 */
15523 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15524 				sata_log(sata_hba_inst, CE_WARN,
15525 				    "SATA device detected at port %d", cport);
15526 			} else {
15527 				sata_log(sata_hba_inst, CE_WARN,
15528 				    "SATA port multiplier detected at port %d",
15529 				    cport);
15530 			}
15531 		}
15532 	}
15533 	return (0);
15534 }
15535 
15536 
15537 
15538 /*
15539  * Process ioctl reset port request.
15540  *
15541  * NOTE: Port-Multiplier is supported.
15542  */
15543 static int
15544 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15545     sata_device_t *sata_device)
15546 {
15547 	int cport, pmport, qual;
15548 	int rv = 0;
15549 
15550 	cport = sata_device->satadev_addr.cport;
15551 	pmport = sata_device->satadev_addr.pmport;
15552 	qual = sata_device->satadev_addr.qual;
15553 
15554 	/*
15555 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15556 	 * is a device. But what we are dealing with is port/pmport.
15557 	 */
15558 	if (qual == SATA_ADDR_DCPORT)
15559 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15560 	else
15561 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15562 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15563 
15564 	/* Sanity check */
15565 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15566 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15567 		    "sata_hba_ioctl: sata_hba_tran missing required "
15568 		    "function sata_tran_reset_dport"));
15569 		return (ENOTSUP);
15570 	}
15571 
15572 	/* Ask HBA to reset port */
15573 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15574 	    sata_device) != SATA_SUCCESS) {
15575 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15576 		    "sata_hba_ioctl: reset port: failed %d:%d",
15577 		    cport, pmport));
15578 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15579 		    cport_mutex);
15580 		sata_update_port_info(sata_hba_inst, sata_device);
15581 		if (qual == SATA_ADDR_CPORT)
15582 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15583 			    SATA_PSTATE_FAILED;
15584 		else {
15585 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15586 			    pmport));
15587 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15588 			    SATA_PSTATE_FAILED;
15589 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15590 			    pmport));
15591 		}
15592 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15593 		    cport_mutex);
15594 		rv = EIO;
15595 	}
15596 
15597 	return (rv);
15598 }
15599 
15600 /*
15601  * Process ioctl reset device request.
15602  *
15603  * NOTE: Port multiplier is supported.
15604  */
15605 static int
15606 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15607     sata_device_t *sata_device)
15608 {
15609 	sata_drive_info_t *sdinfo = NULL;
15610 	sata_pmult_info_t *pmultinfo = NULL;
15611 	int cport, pmport;
15612 	int rv = 0;
15613 
15614 	/* Sanity check */
15615 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15616 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15617 		    "sata_hba_ioctl: sata_hba_tran missing required "
15618 		    "function sata_tran_reset_dport"));
15619 		return (ENOTSUP);
15620 	}
15621 
15622 	cport = sata_device->satadev_addr.cport;
15623 	pmport = sata_device->satadev_addr.pmport;
15624 
15625 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15626 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15627 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15628 		    SATA_DTYPE_PMULT)
15629 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15630 			    cport_devp.cport_sata_pmult;
15631 		else
15632 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15633 			    sata_device->satadev_addr.cport);
15634 	} else { /* port multiplier */
15635 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15636 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15637 		    sata_device->satadev_addr.cport,
15638 		    sata_device->satadev_addr.pmport);
15639 	}
15640 	if (sdinfo == NULL && pmultinfo == NULL) {
15641 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15642 		return (EINVAL);
15643 	}
15644 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15645 
15646 	/* Ask HBA to reset device */
15647 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15648 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15649 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15650 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15651 		    cport, pmport));
15652 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15653 		    cport_mutex);
15654 		sata_update_port_info(sata_hba_inst, sata_device);
15655 		/*
15656 		 * Device info structure remains attached. Another device reset
15657 		 * or port disconnect/connect and re-probing is
15658 		 * needed to change it's state
15659 		 */
15660 		if (sdinfo != NULL) {
15661 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15662 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15663 		} else if (pmultinfo != NULL) {
15664 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15665 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15666 		}
15667 
15668 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15669 		rv = EIO;
15670 	}
15671 	/*
15672 	 * If attached device was a port multiplier, some extra processing
15673 	 * may be needed to bring it back. SATA specification requies a
15674 	 * mandatory software reset on host port to reliably enumerate a port
15675 	 * multiplier, the HBA driver should handle that after reset
15676 	 * operation.
15677 	 */
15678 	return (rv);
15679 }
15680 
15681 
15682 /*
15683  * Process ioctl reset all request.
15684  */
15685 static int
15686 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15687 {
15688 	sata_device_t sata_device;
15689 	int rv = 0;
15690 	int tcport;
15691 
15692 	sata_device.satadev_rev = SATA_DEVICE_REV;
15693 
15694 	/*
15695 	 * There is no protection here for configured devices.
15696 	 */
15697 	/* Sanity check */
15698 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15699 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15700 		    "sata_hba_ioctl: sata_hba_tran missing required "
15701 		    "function sata_tran_reset_dport"));
15702 		return (ENOTSUP);
15703 	}
15704 
15705 	/*
15706 	 * Need to lock all ports, not just one.
15707 	 * If any port is locked by event processing, fail the whole operation.
15708 	 * One port is already locked, but for simplicity lock it again.
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 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15714 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15715 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15716 			    cport_mutex);
15717 			rv = EBUSY;
15718 			break;
15719 		} else {
15720 			/*
15721 			 * It is enough to lock cport in command-based
15722 			 * switching mode.
15723 			 */
15724 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15725 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15726 		}
15727 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15728 		    cport_mutex);
15729 	}
15730 
15731 	if (rv == 0) {
15732 		/*
15733 		 * All cports were successfully locked.
15734 		 * Reset main SATA controller.
15735 		 * Set the device address to port 0, to have a valid device
15736 		 * address.
15737 		 */
15738 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15739 		sata_device.satadev_addr.cport = 0;
15740 		sata_device.satadev_addr.pmport = 0;
15741 
15742 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15743 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15744 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15745 			    "sata_hba_ioctl: reset controller failed"));
15746 			return (EIO);
15747 		}
15748 	}
15749 	/*
15750 	 * Unlock all ports
15751 	 */
15752 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15753 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15754 		    cport_mutex);
15755 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15756 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15757 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15758 		    cport_mutex);
15759 	}
15760 
15761 	/*
15762 	 * This operation returns EFAULT if either reset
15763 	 * controller failed or a re-probing of any port failed.
15764 	 */
15765 	return (rv);
15766 }
15767 
15768 
15769 /*
15770  * Process ioctl port self test request.
15771  *
15772  * NOTE: Port multiplier code is not completed nor tested.
15773  */
15774 static int
15775 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15776     sata_device_t *sata_device)
15777 {
15778 	int cport, pmport, qual;
15779 	int rv = 0;
15780 
15781 	/* Sanity check */
15782 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15783 		return (ENOTSUP);
15784 
15785 	cport = sata_device->satadev_addr.cport;
15786 	pmport = sata_device->satadev_addr.pmport;
15787 	qual = sata_device->satadev_addr.qual;
15788 
15789 	/*
15790 	 * There is no protection here for a configured
15791 	 * device attached to this port.
15792 	 */
15793 
15794 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15795 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15796 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15797 		    "sata_hba_ioctl: port selftest: "
15798 		    "failed port %d:%d", cport, pmport));
15799 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15800 		    cport_mutex);
15801 		sata_update_port_info(sata_hba_inst, sata_device);
15802 		if (qual == SATA_ADDR_CPORT)
15803 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15804 			    SATA_PSTATE_FAILED;
15805 		else { /* port multiplier device port */
15806 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15807 			    cport, pmport));
15808 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15809 			    SATA_PSTATE_FAILED;
15810 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15811 			    cport, pmport));
15812 		}
15813 
15814 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15815 		    cport_mutex);
15816 		return (EIO);
15817 	}
15818 	/*
15819 	 * Beacuse the port was reset in the course of testing, it should be
15820 	 * re-probed and attached device state should be restored. At this
15821 	 * point the port state is unknown - it's state is HBA-specific.
15822 	 * Force port re-probing to get it into a known state.
15823 	 */
15824 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15825 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15826 		rv = EIO;
15827 	return (rv);
15828 }
15829 
15830 
15831 /*
15832  * sata_cfgadm_state:
15833  * Use the sata port state and state of the target node to figure out
15834  * the cfgadm_state.
15835  *
15836  * The port argument is a value with encoded cport,
15837  * pmport and address qualifier, in the same manner as a scsi target number.
15838  * SCSI_TO_SATA_CPORT macro extracts cport number,
15839  * SCSI_TO_SATA_PMPORT extracts pmport number and
15840  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15841  *
15842  * Port multiplier is supported.
15843  */
15844 
15845 static void
15846 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15847     devctl_ap_state_t *ap_state)
15848 {
15849 	uint8_t		cport, pmport, qual;
15850 	uint32_t	port_state, pmult_state;
15851 	uint32_t	dev_type;
15852 	sata_drive_info_t *sdinfo;
15853 
15854 	cport = SCSI_TO_SATA_CPORT(port);
15855 	pmport = SCSI_TO_SATA_PMPORT(port);
15856 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15857 
15858 	/* Check cport state */
15859 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15860 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15861 	    port_state & SATA_PSTATE_FAILED) {
15862 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15863 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15864 		if (port_state & SATA_PSTATE_FAILED)
15865 			ap_state->ap_condition = AP_COND_FAILED;
15866 		else
15867 			ap_state->ap_condition = AP_COND_UNKNOWN;
15868 
15869 		return;
15870 	}
15871 
15872 	/* cport state is okay. Now check pmport state */
15873 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15874 		/* Sanity check */
15875 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15876 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15877 		    cport, pmport) == NULL)
15878 			return;
15879 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15880 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15881 		    port_state & SATA_PSTATE_FAILED) {
15882 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15883 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15884 			if (port_state & SATA_PSTATE_FAILED)
15885 				ap_state->ap_condition = AP_COND_FAILED;
15886 			else
15887 				ap_state->ap_condition = AP_COND_UNKNOWN;
15888 
15889 			return;
15890 		}
15891 	}
15892 
15893 	/* Port is enabled and ready */
15894 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15895 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15896 	else
15897 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15898 
15899 	switch (dev_type) {
15900 	case SATA_DTYPE_NONE:
15901 	{
15902 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15903 		ap_state->ap_condition = AP_COND_OK;
15904 		/* No device attached */
15905 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15906 		break;
15907 	}
15908 	case SATA_DTYPE_PMULT:
15909 	{
15910 		/* Need to check port multiplier state */
15911 		ASSERT(qual == SATA_ADDR_DCPORT);
15912 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15913 		    pmult_state;
15914 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15915 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15916 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15917 			if (pmult_state & SATA_PSTATE_FAILED)
15918 				ap_state->ap_condition = AP_COND_FAILED;
15919 			else
15920 				ap_state->ap_condition = AP_COND_UNKNOWN;
15921 
15922 			return;
15923 		}
15924 
15925 		/* Port multiplier is not configurable */
15926 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15927 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15928 		ap_state->ap_condition = AP_COND_OK;
15929 		break;
15930 	}
15931 
15932 	case SATA_DTYPE_ATADISK:
15933 	case SATA_DTYPE_ATAPICD:
15934 	case SATA_DTYPE_ATAPITAPE:
15935 	case SATA_DTYPE_ATAPIDISK:
15936 	{
15937 		dev_info_t *tdip = NULL;
15938 		dev_info_t *dip = NULL;
15939 		int circ;
15940 
15941 		dip = SATA_DIP(sata_hba_inst);
15942 		tdip = sata_get_target_dip(dip, cport, pmport);
15943 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15944 		if (tdip != NULL) {
15945 			ndi_devi_enter(dip, &circ);
15946 			mutex_enter(&(DEVI(tdip)->devi_lock));
15947 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
15948 				/*
15949 				 * There could be the case where previously
15950 				 * configured and opened device was removed
15951 				 * and unknown device was plugged.
15952 				 * In such case we want to show a device, and
15953 				 * its configured or unconfigured state but
15954 				 * indicate unusable condition untill the
15955 				 * old target node is released and removed.
15956 				 */
15957 				ap_state->ap_condition = AP_COND_UNUSABLE;
15958 			} else {
15959 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
15960 				    cport));
15961 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15962 				    cport);
15963 				if (sdinfo != NULL) {
15964 					if ((sdinfo->satadrv_state &
15965 					    SATA_DSTATE_FAILED) != 0)
15966 						ap_state->ap_condition =
15967 						    AP_COND_FAILED;
15968 					else
15969 						ap_state->ap_condition =
15970 						    AP_COND_OK;
15971 				} else {
15972 					ap_state->ap_condition =
15973 					    AP_COND_UNKNOWN;
15974 				}
15975 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
15976 				    cport));
15977 			}
15978 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
15979 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
15980 				ap_state->ap_ostate =
15981 				    AP_OSTATE_UNCONFIGURED;
15982 			} else {
15983 				ap_state->ap_ostate =
15984 				    AP_OSTATE_CONFIGURED;
15985 			}
15986 			mutex_exit(&(DEVI(tdip)->devi_lock));
15987 			ndi_devi_exit(dip, circ);
15988 		} else {
15989 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15990 			ap_state->ap_condition = AP_COND_UNKNOWN;
15991 		}
15992 		break;
15993 	}
15994 	default:
15995 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15996 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15997 		ap_state->ap_condition = AP_COND_UNKNOWN;
15998 		/*
15999 		 * This is actually internal error condition (non fatal),
16000 		 * because we have already checked all defined device types.
16001 		 */
16002 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16003 		    "sata_cfgadm_state: Internal error: "
16004 		    "unknown device type"));
16005 		break;
16006 	}
16007 }
16008 
16009 
16010 /*
16011  * Process ioctl get device path request.
16012  *
16013  * NOTE: Port multiplier has no target dip. Devices connected to port
16014  * multiplier have target node attached to the HBA node. The only difference
16015  * between them and the directly-attached device node is a target address.
16016  */
16017 static int
16018 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16019     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16020 {
16021 	char path[MAXPATHLEN];
16022 	uint32_t size;
16023 	dev_info_t *tdip;
16024 
16025 	(void) strcpy(path, "/devices");
16026 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16027 	    &sata_device->satadev_addr)) == NULL) {
16028 		/*
16029 		 * No such device. If this is a request for a size, do not
16030 		 * return EINVAL for non-existing target, because cfgadm
16031 		 * will then indicate a meaningless ioctl failure.
16032 		 * If this is a request for a path, indicate invalid
16033 		 * argument.
16034 		 */
16035 		if (ioc->get_size == 0)
16036 			return (EINVAL);
16037 	} else {
16038 		(void) ddi_pathname(tdip, path + strlen(path));
16039 	}
16040 	size = strlen(path) + 1;
16041 
16042 	if (ioc->get_size != 0) {
16043 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16044 		    mode) != 0)
16045 			return (EFAULT);
16046 	} else {
16047 		if (ioc->bufsiz != size)
16048 			return (EINVAL);
16049 
16050 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16051 		    mode) != 0)
16052 			return (EFAULT);
16053 	}
16054 	return (0);
16055 }
16056 
16057 /*
16058  * Process ioctl get attachment point type request.
16059  *
16060  * NOTE: Port multiplier is supported.
16061  */
16062 static	int
16063 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16064     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16065 {
16066 	uint32_t	type_len;
16067 	const char	*ap_type;
16068 	int		dev_type;
16069 
16070 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16071 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16072 		    sata_device->satadev_addr.cport);
16073 	else /* pmport */
16074 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16075 		    sata_device->satadev_addr.cport,
16076 		    sata_device->satadev_addr.pmport);
16077 
16078 	switch (dev_type) {
16079 	case SATA_DTYPE_NONE:
16080 		ap_type = "port";
16081 		break;
16082 
16083 	case SATA_DTYPE_ATADISK:
16084 	case SATA_DTYPE_ATAPIDISK:
16085 		ap_type = "disk";
16086 		break;
16087 
16088 	case SATA_DTYPE_ATAPICD:
16089 		ap_type = "cd/dvd";
16090 		break;
16091 
16092 	case SATA_DTYPE_ATAPITAPE:
16093 		ap_type = "tape";
16094 		break;
16095 
16096 	case SATA_DTYPE_PMULT:
16097 		ap_type = "sata-pmult";
16098 		break;
16099 
16100 	case SATA_DTYPE_UNKNOWN:
16101 		ap_type = "unknown";
16102 		break;
16103 
16104 	default:
16105 		ap_type = "unsupported";
16106 		break;
16107 
16108 	} /* end of dev_type switch */
16109 
16110 	type_len = strlen(ap_type) + 1;
16111 
16112 	if (ioc->get_size) {
16113 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16114 		    mode) != 0)
16115 			return (EFAULT);
16116 	} else {
16117 		if (ioc->bufsiz != type_len)
16118 			return (EINVAL);
16119 
16120 		if (ddi_copyout((void *)ap_type, ioc->buf,
16121 		    ioc->bufsiz, mode) != 0)
16122 			return (EFAULT);
16123 	}
16124 	return (0);
16125 
16126 }
16127 
16128 /*
16129  * Process ioctl get device model info request.
16130  * This operation should return to cfgadm the device model
16131  * information string
16132  *
16133  * NOTE: Port multiplier is supported.
16134  */
16135 static	int
16136 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16137     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16138 {
16139 	sata_drive_info_t *sdinfo;
16140 	uint32_t info_len;
16141 	char ap_info[SATA_ID_MODEL_LEN + 1];
16142 
16143 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16144 	    sata_device->satadev_addr.cport)->cport_mutex);
16145 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16146 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16147 		    sata_device->satadev_addr.cport);
16148 	else /* port multiplier */
16149 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16150 		    sata_device->satadev_addr.cport,
16151 		    sata_device->satadev_addr.pmport);
16152 	if (sdinfo == NULL) {
16153 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16154 		    sata_device->satadev_addr.cport)->cport_mutex);
16155 		return (EINVAL);
16156 	}
16157 
16158 #ifdef	_LITTLE_ENDIAN
16159 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16160 #else	/* _LITTLE_ENDIAN */
16161 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16162 #endif	/* _LITTLE_ENDIAN */
16163 
16164 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16165 	    sata_device->satadev_addr.cport)->cport_mutex);
16166 
16167 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16168 
16169 	info_len = strlen(ap_info) + 1;
16170 
16171 	if (ioc->get_size) {
16172 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16173 		    mode) != 0)
16174 			return (EFAULT);
16175 	} else {
16176 		if (ioc->bufsiz < info_len)
16177 			return (EINVAL);
16178 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16179 		    mode) != 0)
16180 			return (EFAULT);
16181 	}
16182 	return (0);
16183 }
16184 
16185 
16186 /*
16187  * Process ioctl get device firmware revision info request.
16188  * This operation should return to cfgadm the device firmware revision
16189  * information string
16190  *
16191  * Port multiplier is supported.
16192  */
16193 static	int
16194 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16195     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16196 {
16197 	sata_drive_info_t *sdinfo;
16198 	uint32_t info_len;
16199 	char ap_info[SATA_ID_FW_LEN + 1];
16200 
16201 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16202 	    sata_device->satadev_addr.cport)->cport_mutex);
16203 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16204 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16205 		    sata_device->satadev_addr.cport);
16206 	else /* port multiplier */
16207 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16208 		    sata_device->satadev_addr.cport,
16209 		    sata_device->satadev_addr.pmport);
16210 	if (sdinfo == NULL) {
16211 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16212 		    sata_device->satadev_addr.cport)->cport_mutex);
16213 		return (EINVAL);
16214 	}
16215 
16216 #ifdef	_LITTLE_ENDIAN
16217 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16218 #else	/* _LITTLE_ENDIAN */
16219 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16220 #endif	/* _LITTLE_ENDIAN */
16221 
16222 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16223 	    sata_device->satadev_addr.cport)->cport_mutex);
16224 
16225 	ap_info[SATA_ID_FW_LEN] = '\0';
16226 
16227 	info_len = strlen(ap_info) + 1;
16228 
16229 	if (ioc->get_size) {
16230 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16231 		    mode) != 0)
16232 			return (EFAULT);
16233 	} else {
16234 		if (ioc->bufsiz < info_len)
16235 			return (EINVAL);
16236 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16237 		    mode) != 0)
16238 			return (EFAULT);
16239 	}
16240 	return (0);
16241 }
16242 
16243 
16244 /*
16245  * Process ioctl get device serial number info request.
16246  * This operation should return to cfgadm the device serial number string.
16247  *
16248  * NOTE: Port multiplier is supported.
16249  */
16250 static	int
16251 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16252     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16253 {
16254 	sata_drive_info_t *sdinfo;
16255 	uint32_t info_len;
16256 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16257 
16258 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16259 	    sata_device->satadev_addr.cport)->cport_mutex);
16260 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16261 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16262 		    sata_device->satadev_addr.cport);
16263 	else /* port multiplier */
16264 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16265 		    sata_device->satadev_addr.cport,
16266 		    sata_device->satadev_addr.pmport);
16267 	if (sdinfo == NULL) {
16268 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16269 		    sata_device->satadev_addr.cport)->cport_mutex);
16270 		return (EINVAL);
16271 	}
16272 
16273 #ifdef	_LITTLE_ENDIAN
16274 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16275 #else	/* _LITTLE_ENDIAN */
16276 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16277 #endif	/* _LITTLE_ENDIAN */
16278 
16279 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16280 	    sata_device->satadev_addr.cport)->cport_mutex);
16281 
16282 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16283 
16284 	info_len = strlen(ap_info) + 1;
16285 
16286 	if (ioc->get_size) {
16287 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16288 		    mode) != 0)
16289 			return (EFAULT);
16290 	} else {
16291 		if (ioc->bufsiz < info_len)
16292 			return (EINVAL);
16293 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16294 		    mode) != 0)
16295 			return (EFAULT);
16296 	}
16297 	return (0);
16298 }
16299 
16300 
16301 /*
16302  * Preset scsi extended sense data (to NO SENSE)
16303  * First 18 bytes of the sense data are preset to current valid sense
16304  * with a key NO SENSE data.
16305  *
16306  * Returns void
16307  */
16308 static void
16309 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16310 {
16311 	sense->es_valid = 1;		/* Valid sense */
16312 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16313 	sense->es_key = KEY_NO_SENSE;
16314 	sense->es_info_1 = 0;
16315 	sense->es_info_2 = 0;
16316 	sense->es_info_3 = 0;
16317 	sense->es_info_4 = 0;
16318 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16319 	sense->es_cmd_info[0] = 0;
16320 	sense->es_cmd_info[1] = 0;
16321 	sense->es_cmd_info[2] = 0;
16322 	sense->es_cmd_info[3] = 0;
16323 	sense->es_add_code = 0;
16324 	sense->es_qual_code = 0;
16325 }
16326 
16327 /*
16328  * Register a legacy cmdk-style devid for the target (disk) device.
16329  *
16330  * Note: This function is called only when the HBA devinfo node has the
16331  * property "use-cmdk-devid-format" set. This property indicates that
16332  * devid compatible with old cmdk (target) driver is to be generated
16333  * for any target device attached to this controller. This will take
16334  * precedence over the devid generated by sd (target) driver.
16335  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16336  */
16337 static void
16338 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16339 {
16340 	char	*hwid;
16341 	int	modlen;
16342 	int	serlen;
16343 	int	rval;
16344 	ddi_devid_t	devid;
16345 
16346 	/*
16347 	 * device ID is a concatanation of model number, "=", serial number.
16348 	 */
16349 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16350 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16351 	    sizeof (sdinfo->satadrv_id.ai_model));
16352 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16353 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16354 	if (modlen == 0)
16355 		goto err;
16356 	hwid[modlen++] = '=';
16357 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16358 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16359 	swab(&hwid[modlen], &hwid[modlen],
16360 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16361 	serlen = sata_check_modser(&hwid[modlen],
16362 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16363 	if (serlen == 0)
16364 		goto err;
16365 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16366 
16367 	/* initialize/register devid */
16368 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16369 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16370 		rval = ddi_devid_register(dip, devid);
16371 		/*
16372 		 * Free up the allocated devid buffer.
16373 		 * NOTE: This doesn't mean unregistering devid.
16374 		 */
16375 		ddi_devid_free(devid);
16376 	}
16377 
16378 	if (rval != DDI_SUCCESS)
16379 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16380 		    " on port %d", sdinfo->satadrv_addr.cport);
16381 err:
16382 	kmem_free(hwid, LEGACY_HWID_LEN);
16383 }
16384 
16385 /*
16386  * valid model/serial string must contain a non-zero non-space characters.
16387  * trim trailing spaces/NULLs.
16388  */
16389 static int
16390 sata_check_modser(char *buf, int buf_len)
16391 {
16392 	boolean_t ret;
16393 	char *s;
16394 	int i;
16395 	int tb;
16396 	char ch;
16397 
16398 	ret = B_FALSE;
16399 	s = buf;
16400 	for (i = 0; i < buf_len; i++) {
16401 		ch = *s++;
16402 		if (ch != ' ' && ch != '\0')
16403 			tb = i + 1;
16404 		if (ch != ' ' && ch != '\0' && ch != '0')
16405 			ret = B_TRUE;
16406 	}
16407 
16408 	if (ret == B_FALSE)
16409 		return (0); /* invalid string */
16410 
16411 	return (tb); /* return length */
16412 }
16413 
16414 /*
16415  * sata_set_drive_features function compares current device features setting
16416  * with the saved device features settings and, if there is a difference,
16417  * it restores device features setting to the previously saved state.
16418  * It also arbitrarily tries to select the highest supported DMA mode.
16419  * Device Identify or Identify Packet Device data has to be current.
16420  * At the moment read ahead and write cache are considered for all devices.
16421  * For atapi devices, Removable Media Status Notification is set in addition
16422  * to common features.
16423  *
16424  * This function cannot be called in the interrupt context (it may sleep).
16425  *
16426  * The input argument sdinfo should point to the drive info structure
16427  * to be updated after features are set. Note, that only
16428  * device (packet) identify data is updated, not the flags indicating the
16429  * supported features.
16430  *
16431  * Returns SATA_SUCCESS if successful or there was nothing to do.
16432  * Device Identify data in the drive info structure pointed to by the sdinfo
16433  * arguments is updated even when no features were set or changed.
16434  *
16435  * Returns SATA_FAILURE if device features could not be set or DMA mode
16436  * for a disk cannot be set and device identify data cannot be fetched.
16437  *
16438  * Returns SATA_RETRY if device features could not be set (other than disk
16439  * DMA mode) but the device identify data was fetched successfully.
16440  *
16441  * Note: This function may fail the port, making it inaccessible.
16442  * In such case the explicit port disconnect/connect or physical device
16443  * detach/attach is required to re-evaluate port state again.
16444  */
16445 
16446 static int
16447 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16448     sata_drive_info_t *sdinfo, int restore)
16449 {
16450 	int rval = SATA_SUCCESS;
16451 	int rval_set;
16452 	sata_drive_info_t new_sdinfo;
16453 	char *finfo = "sata_set_drive_features: cannot";
16454 	char *finfox;
16455 	int cache_op;
16456 
16457 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16458 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16459 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16460 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16461 		/*
16462 		 * Cannot get device identification - caller may retry later
16463 		 */
16464 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16465 		    "%s fetch device identify data\n", finfo);
16466 		return (SATA_FAILURE);
16467 	}
16468 	finfox = (restore != 0) ? " restore device features" :
16469 	    " initialize device features\n";
16470 
16471 	switch (sdinfo->satadrv_type) {
16472 	case SATA_DTYPE_ATADISK:
16473 		/* Arbitrarily set UDMA mode */
16474 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16475 		    SATA_SUCCESS) {
16476 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16477 			    "%s set UDMA mode\n", finfo));
16478 			return (SATA_FAILURE);
16479 		}
16480 		break;
16481 	case SATA_DTYPE_ATAPICD:
16482 	case SATA_DTYPE_ATAPITAPE:
16483 	case SATA_DTYPE_ATAPIDISK:
16484 		/*  Set Removable Media Status Notification, if necessary */
16485 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16486 		    restore != 0) {
16487 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16488 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16489 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16490 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16491 				/* Current setting does not match saved one */
16492 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16493 				    sdinfo->satadrv_settings &
16494 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16495 					rval = SATA_FAILURE;
16496 			}
16497 		}
16498 		/*
16499 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16500 		 * we want to use DMA transfer mode whenever possible.
16501 		 * Some devices require explicit setting of the DMA mode.
16502 		 */
16503 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16504 			/* Set highest supported DMA mode */
16505 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16506 			    SATA_SUCCESS) {
16507 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16508 				    "%s set UDMA mode\n", finfo));
16509 				rval = SATA_FAILURE;
16510 			}
16511 		}
16512 		break;
16513 	}
16514 
16515 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16516 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16517 		/*
16518 		 * neither READ AHEAD nor WRITE CACHE is supported
16519 		 * - do nothing
16520 		 */
16521 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16522 		    "settable features not supported\n", NULL);
16523 		goto update_sdinfo;
16524 	}
16525 
16526 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16527 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16528 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16529 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16530 		/*
16531 		 * both READ AHEAD and WRITE CACHE are enabled
16532 		 * - Nothing to do
16533 		 */
16534 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16535 		    "no device features to set\n", NULL);
16536 		goto update_sdinfo;
16537 	}
16538 
16539 	cache_op = 0;
16540 
16541 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16542 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16543 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16544 			/* Enable read ahead / read cache */
16545 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16546 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16547 			    "enabling read cache\n", NULL);
16548 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16549 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16550 			/* Disable read ahead  / read cache */
16551 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16552 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16553 			    "disabling read cache\n", NULL);
16554 		}
16555 
16556 		if (cache_op != 0) {
16557 			/* Try to set read cache mode */
16558 			rval_set = sata_set_cache_mode(sata_hba_inst,
16559 			    &new_sdinfo, cache_op);
16560 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16561 				rval = rval_set;
16562 		}
16563 	}
16564 
16565 	cache_op = 0;
16566 
16567 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16568 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16569 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16570 			/* Enable write cache */
16571 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16572 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16573 			    "enabling write cache\n", NULL);
16574 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16575 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16576 			/* Disable write cache */
16577 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16578 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16579 			    "disabling write cache\n", NULL);
16580 		}
16581 
16582 		if (cache_op != 0) {
16583 			/* Try to set write cache mode */
16584 			rval_set = sata_set_cache_mode(sata_hba_inst,
16585 			    &new_sdinfo, cache_op);
16586 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16587 				rval = rval_set;
16588 		}
16589 	}
16590 	if (rval != SATA_SUCCESS)
16591 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16592 		    "%s %s", finfo, finfox));
16593 
16594 update_sdinfo:
16595 	/*
16596 	 * We need to fetch Device Identify data again
16597 	 */
16598 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16599 		/*
16600 		 * Cannot get device identification - retry later
16601 		 */
16602 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16603 		    "%s re-fetch device identify data\n", finfo));
16604 		rval = SATA_FAILURE;
16605 	}
16606 	/* Copy device sata info. */
16607 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16608 
16609 	return (rval);
16610 }
16611 
16612 
16613 /*
16614  *
16615  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16616  * unable to determine.
16617  *
16618  * Cannot be called in an interrupt context.
16619  *
16620  * Called by sata_build_lsense_page_2f()
16621  */
16622 
16623 static int
16624 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16625     sata_drive_info_t *sdinfo)
16626 {
16627 	sata_pkt_t *spkt;
16628 	sata_cmd_t *scmd;
16629 	sata_pkt_txlate_t *spx;
16630 	int rval;
16631 
16632 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16633 	spx->txlt_sata_hba_inst = sata_hba_inst;
16634 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16635 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16636 	if (spkt == NULL) {
16637 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16638 		return (-1);
16639 	}
16640 	/* address is needed now */
16641 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16642 
16643 
16644 	/* Fill sata_pkt */
16645 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16646 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16647 	/* Synchronous mode, no callback */
16648 	spkt->satapkt_comp = NULL;
16649 	/* Timeout 30s */
16650 	spkt->satapkt_time = sata_default_pkt_time;
16651 
16652 	scmd = &spkt->satapkt_cmd;
16653 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16654 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16655 
16656 	/* Set up which registers need to be returned */
16657 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16658 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16659 
16660 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16661 	scmd->satacmd_addr_type = 0;		/* N/A */
16662 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16663 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16664 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16665 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16666 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16667 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16668 	scmd->satacmd_cmd_reg = SATAC_SMART;
16669 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16670 	    sdinfo->satadrv_addr.cport)));
16671 
16672 
16673 	/* Send pkt to SATA HBA driver */
16674 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16675 	    SATA_TRAN_ACCEPTED ||
16676 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16677 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16678 		    sdinfo->satadrv_addr.cport)));
16679 		/*
16680 		 * Whoops, no SMART RETURN STATUS
16681 		 */
16682 		rval = -1;
16683 	} else {
16684 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16685 		    sdinfo->satadrv_addr.cport)));
16686 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16687 			rval = -1;
16688 			goto fail;
16689 		}
16690 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16691 			rval = -1;
16692 			goto fail;
16693 		}
16694 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16695 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16696 			rval = 0;
16697 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16698 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16699 			rval = 1;
16700 		else {
16701 			rval = -1;
16702 			goto fail;
16703 		}
16704 	}
16705 fail:
16706 	/* Free allocated resources */
16707 	sata_pkt_free(spx);
16708 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16709 
16710 	return (rval);
16711 }
16712 
16713 /*
16714  *
16715  * Returns 0 if succeeded, -1 otherwise
16716  *
16717  * Cannot be called in an interrupt context.
16718  *
16719  */
16720 static int
16721 sata_fetch_smart_data(
16722 	sata_hba_inst_t *sata_hba_inst,
16723 	sata_drive_info_t *sdinfo,
16724 	struct smart_data *smart_data)
16725 {
16726 	sata_pkt_t *spkt;
16727 	sata_cmd_t *scmd;
16728 	sata_pkt_txlate_t *spx;
16729 	int rval;
16730 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16731 
16732 #if ! defined(lint)
16733 	ASSERT(sizeof (struct smart_data) == 512);
16734 #endif
16735 
16736 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16737 	spx->txlt_sata_hba_inst = sata_hba_inst;
16738 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16739 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16740 	if (spkt == NULL) {
16741 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16742 		return (-1);
16743 	}
16744 	/* address is needed now */
16745 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16746 
16747 
16748 	/* Fill sata_pkt */
16749 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16750 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16751 	/* Synchronous mode, no callback */
16752 	spkt->satapkt_comp = NULL;
16753 	/* Timeout 30s */
16754 	spkt->satapkt_time = sata_default_pkt_time;
16755 
16756 	scmd = &spkt->satapkt_cmd;
16757 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16758 
16759 	/*
16760 	 * Allocate buffer for SMART data
16761 	 */
16762 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16763 	    sizeof (struct smart_data));
16764 	if (scmd->satacmd_bp == NULL) {
16765 		sata_pkt_free(spx);
16766 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16767 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16768 		    "sata_fetch_smart_data: "
16769 		    "cannot allocate buffer"));
16770 		return (-1);
16771 	}
16772 
16773 
16774 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16775 	scmd->satacmd_addr_type = 0;		/* N/A */
16776 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16777 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16778 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16779 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16780 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16781 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16782 	scmd->satacmd_cmd_reg = SATAC_SMART;
16783 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16784 	    sdinfo->satadrv_addr.cport)));
16785 
16786 	/* Send pkt to SATA HBA driver */
16787 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16788 	    SATA_TRAN_ACCEPTED ||
16789 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16790 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16791 		    sdinfo->satadrv_addr.cport)));
16792 		/*
16793 		 * Whoops, no SMART DATA available
16794 		 */
16795 		rval = -1;
16796 		goto fail;
16797 	} else {
16798 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16799 		    sdinfo->satadrv_addr.cport)));
16800 		if (spx->txlt_buf_dma_handle != NULL) {
16801 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16802 			    DDI_DMA_SYNC_FORKERNEL);
16803 			ASSERT(rval == DDI_SUCCESS);
16804 			if (sata_check_for_dma_error(dip, spx)) {
16805 				ddi_fm_service_impact(dip,
16806 				    DDI_SERVICE_UNAFFECTED);
16807 				rval = -1;
16808 				goto fail;
16809 			}
16810 		}
16811 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16812 		    sizeof (struct smart_data));
16813 	}
16814 
16815 fail:
16816 	/* Free allocated resources */
16817 	sata_free_local_buffer(spx);
16818 	sata_pkt_free(spx);
16819 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16820 
16821 	return (rval);
16822 }
16823 
16824 /*
16825  * Used by LOG SENSE page 0x10
16826  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16827  * Note: cannot be called in the interrupt context.
16828  *
16829  * return 0 for success, -1 otherwise
16830  *
16831  */
16832 static int
16833 sata_ext_smart_selftest_read_log(
16834 	sata_hba_inst_t *sata_hba_inst,
16835 	sata_drive_info_t *sdinfo,
16836 	struct smart_ext_selftest_log *ext_selftest_log,
16837 	uint16_t block_num)
16838 {
16839 	sata_pkt_txlate_t *spx;
16840 	sata_pkt_t *spkt;
16841 	sata_cmd_t *scmd;
16842 	int rval;
16843 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16844 
16845 #if ! defined(lint)
16846 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16847 #endif
16848 
16849 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16850 	spx->txlt_sata_hba_inst = sata_hba_inst;
16851 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16852 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16853 	if (spkt == NULL) {
16854 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16855 		return (-1);
16856 	}
16857 	/* address is needed now */
16858 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16859 
16860 
16861 	/* Fill sata_pkt */
16862 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16863 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16864 	/* Synchronous mode, no callback */
16865 	spkt->satapkt_comp = NULL;
16866 	/* Timeout 30s */
16867 	spkt->satapkt_time = sata_default_pkt_time;
16868 
16869 	scmd = &spkt->satapkt_cmd;
16870 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16871 
16872 	/*
16873 	 * Allocate buffer for SMART extended self-test log
16874 	 */
16875 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16876 	    sizeof (struct smart_ext_selftest_log));
16877 	if (scmd->satacmd_bp == NULL) {
16878 		sata_pkt_free(spx);
16879 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16880 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16881 		    "sata_ext_smart_selftest_log: "
16882 		    "cannot allocate buffer"));
16883 		return (-1);
16884 	}
16885 
16886 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16887 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16888 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16889 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16890 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16891 	scmd->satacmd_lba_low_msb = 0;
16892 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16893 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16894 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16895 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16896 
16897 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16898 	    sdinfo->satadrv_addr.cport)));
16899 
16900 	/* Send pkt to SATA HBA driver */
16901 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16902 	    SATA_TRAN_ACCEPTED ||
16903 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16904 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16905 		    sdinfo->satadrv_addr.cport)));
16906 
16907 		/*
16908 		 * Whoops, no SMART selftest log info available
16909 		 */
16910 		rval = -1;
16911 		goto fail;
16912 	} else {
16913 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16914 		    sdinfo->satadrv_addr.cport)));
16915 
16916 		if (spx->txlt_buf_dma_handle != NULL) {
16917 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16918 			    DDI_DMA_SYNC_FORKERNEL);
16919 			ASSERT(rval == DDI_SUCCESS);
16920 			if (sata_check_for_dma_error(dip, spx)) {
16921 				ddi_fm_service_impact(dip,
16922 				    DDI_SERVICE_UNAFFECTED);
16923 				rval = -1;
16924 				goto fail;
16925 			}
16926 		}
16927 		bcopy(scmd->satacmd_bp->b_un.b_addr,
16928 		    (uint8_t *)ext_selftest_log,
16929 		    sizeof (struct smart_ext_selftest_log));
16930 		rval = 0;
16931 	}
16932 
16933 fail:
16934 	/* Free allocated resources */
16935 	sata_free_local_buffer(spx);
16936 	sata_pkt_free(spx);
16937 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16938 
16939 	return (rval);
16940 }
16941 
16942 /*
16943  * Returns 0 for success, -1 otherwise
16944  *
16945  * SMART self-test log data is returned in buffer pointed to by selftest_log
16946  */
16947 static int
16948 sata_smart_selftest_log(
16949 	sata_hba_inst_t *sata_hba_inst,
16950 	sata_drive_info_t *sdinfo,
16951 	struct smart_selftest_log *selftest_log)
16952 {
16953 	sata_pkt_t *spkt;
16954 	sata_cmd_t *scmd;
16955 	sata_pkt_txlate_t *spx;
16956 	int rval;
16957 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16958 
16959 #if ! defined(lint)
16960 	ASSERT(sizeof (struct smart_selftest_log) == 512);
16961 #endif
16962 
16963 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16964 	spx->txlt_sata_hba_inst = sata_hba_inst;
16965 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16966 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16967 	if (spkt == NULL) {
16968 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16969 		return (-1);
16970 	}
16971 	/* address is needed now */
16972 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16973 
16974 
16975 	/* Fill sata_pkt */
16976 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16977 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16978 	/* Synchronous mode, no callback */
16979 	spkt->satapkt_comp = NULL;
16980 	/* Timeout 30s */
16981 	spkt->satapkt_time = sata_default_pkt_time;
16982 
16983 	scmd = &spkt->satapkt_cmd;
16984 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16985 
16986 	/*
16987 	 * Allocate buffer for SMART SELFTEST LOG
16988 	 */
16989 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16990 	    sizeof (struct smart_selftest_log));
16991 	if (scmd->satacmd_bp == NULL) {
16992 		sata_pkt_free(spx);
16993 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16994 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16995 		    "sata_smart_selftest_log: "
16996 		    "cannot allocate buffer"));
16997 		return (-1);
16998 	}
16999 
17000 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17001 	scmd->satacmd_addr_type = 0;		/* N/A */
17002 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17003 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17004 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17005 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17006 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17007 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17008 	scmd->satacmd_cmd_reg = SATAC_SMART;
17009 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17010 	    sdinfo->satadrv_addr.cport)));
17011 
17012 	/* Send pkt to SATA HBA driver */
17013 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17014 	    SATA_TRAN_ACCEPTED ||
17015 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17016 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17017 		    sdinfo->satadrv_addr.cport)));
17018 		/*
17019 		 * Whoops, no SMART DATA available
17020 		 */
17021 		rval = -1;
17022 		goto fail;
17023 	} else {
17024 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17025 		    sdinfo->satadrv_addr.cport)));
17026 		if (spx->txlt_buf_dma_handle != NULL) {
17027 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17028 			    DDI_DMA_SYNC_FORKERNEL);
17029 			ASSERT(rval == DDI_SUCCESS);
17030 			if (sata_check_for_dma_error(dip, spx)) {
17031 				ddi_fm_service_impact(dip,
17032 				    DDI_SERVICE_UNAFFECTED);
17033 				rval = -1;
17034 				goto fail;
17035 			}
17036 		}
17037 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17038 		    sizeof (struct smart_selftest_log));
17039 		rval = 0;
17040 	}
17041 
17042 fail:
17043 	/* Free allocated resources */
17044 	sata_free_local_buffer(spx);
17045 	sata_pkt_free(spx);
17046 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17047 
17048 	return (rval);
17049 }
17050 
17051 
17052 /*
17053  * Returns 0 for success, -1 otherwise
17054  *
17055  * SMART READ LOG data is returned in buffer pointed to by smart_log
17056  */
17057 static int
17058 sata_smart_read_log(
17059 	sata_hba_inst_t *sata_hba_inst,
17060 	sata_drive_info_t *sdinfo,
17061 	uint8_t *smart_log,		/* where the data should be returned */
17062 	uint8_t which_log,		/* which log should be returned */
17063 	uint8_t log_size)		/* # of 512 bytes in log */
17064 {
17065 	sata_pkt_t *spkt;
17066 	sata_cmd_t *scmd;
17067 	sata_pkt_txlate_t *spx;
17068 	int rval;
17069 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17070 
17071 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17072 	spx->txlt_sata_hba_inst = sata_hba_inst;
17073 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17074 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17075 	if (spkt == NULL) {
17076 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17077 		return (-1);
17078 	}
17079 	/* address is needed now */
17080 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17081 
17082 
17083 	/* Fill sata_pkt */
17084 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17085 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17086 	/* Synchronous mode, no callback */
17087 	spkt->satapkt_comp = NULL;
17088 	/* Timeout 30s */
17089 	spkt->satapkt_time = sata_default_pkt_time;
17090 
17091 	scmd = &spkt->satapkt_cmd;
17092 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17093 
17094 	/*
17095 	 * Allocate buffer for SMART READ LOG
17096 	 */
17097 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17098 	if (scmd->satacmd_bp == NULL) {
17099 		sata_pkt_free(spx);
17100 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17101 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17102 		    "sata_smart_read_log: " "cannot allocate buffer"));
17103 		return (-1);
17104 	}
17105 
17106 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17107 	scmd->satacmd_addr_type = 0;		/* N/A */
17108 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17109 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17110 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17111 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17112 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17113 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17114 	scmd->satacmd_cmd_reg = SATAC_SMART;
17115 
17116 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17117 	    sdinfo->satadrv_addr.cport)));
17118 
17119 	/* Send pkt to SATA HBA driver */
17120 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17121 	    SATA_TRAN_ACCEPTED ||
17122 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17123 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17124 		    sdinfo->satadrv_addr.cport)));
17125 
17126 		/*
17127 		 * Whoops, no SMART DATA available
17128 		 */
17129 		rval = -1;
17130 		goto fail;
17131 	} else {
17132 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17133 		    sdinfo->satadrv_addr.cport)));
17134 
17135 		if (spx->txlt_buf_dma_handle != NULL) {
17136 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17137 			    DDI_DMA_SYNC_FORKERNEL);
17138 			ASSERT(rval == DDI_SUCCESS);
17139 			if (sata_check_for_dma_error(dip, spx)) {
17140 				ddi_fm_service_impact(dip,
17141 				    DDI_SERVICE_UNAFFECTED);
17142 				rval = -1;
17143 				goto fail;
17144 			}
17145 		}
17146 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17147 		rval = 0;
17148 	}
17149 
17150 fail:
17151 	/* Free allocated resources */
17152 	sata_free_local_buffer(spx);
17153 	sata_pkt_free(spx);
17154 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17155 
17156 	return (rval);
17157 }
17158 
17159 /*
17160  * Used by LOG SENSE page 0x10
17161  *
17162  * return 0 for success, -1 otherwise
17163  *
17164  */
17165 static int
17166 sata_read_log_ext_directory(
17167 	sata_hba_inst_t *sata_hba_inst,
17168 	sata_drive_info_t *sdinfo,
17169 	struct read_log_ext_directory *logdir)
17170 {
17171 	sata_pkt_txlate_t *spx;
17172 	sata_pkt_t *spkt;
17173 	sata_cmd_t *scmd;
17174 	int rval;
17175 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17176 
17177 #if ! defined(lint)
17178 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17179 #endif
17180 
17181 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17182 	spx->txlt_sata_hba_inst = sata_hba_inst;
17183 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17184 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17185 	if (spkt == NULL) {
17186 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17187 		return (-1);
17188 	}
17189 
17190 	/* Fill sata_pkt */
17191 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17192 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17193 	/* Synchronous mode, no callback */
17194 	spkt->satapkt_comp = NULL;
17195 	/* Timeout 30s */
17196 	spkt->satapkt_time = sata_default_pkt_time;
17197 
17198 	scmd = &spkt->satapkt_cmd;
17199 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17200 
17201 	/*
17202 	 * Allocate buffer for SMART READ LOG EXTENDED command
17203 	 */
17204 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17205 	    sizeof (struct read_log_ext_directory));
17206 	if (scmd->satacmd_bp == NULL) {
17207 		sata_pkt_free(spx);
17208 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17209 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17210 		    "sata_read_log_ext_directory: "
17211 		    "cannot allocate buffer"));
17212 		return (-1);
17213 	}
17214 
17215 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17216 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17217 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17218 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17219 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17220 	scmd->satacmd_lba_low_msb = 0;
17221 	scmd->satacmd_lba_mid_lsb = 0;
17222 	scmd->satacmd_lba_mid_msb = 0;
17223 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17224 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17225 
17226 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17227 	    sdinfo->satadrv_addr.cport)));
17228 
17229 	/* Send pkt to SATA HBA driver */
17230 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17231 	    SATA_TRAN_ACCEPTED ||
17232 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17233 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17234 		    sdinfo->satadrv_addr.cport)));
17235 		/*
17236 		 * Whoops, no SMART selftest log info available
17237 		 */
17238 		rval = -1;
17239 		goto fail;
17240 	} else {
17241 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17242 		    sdinfo->satadrv_addr.cport)));
17243 		if (spx->txlt_buf_dma_handle != NULL) {
17244 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17245 			    DDI_DMA_SYNC_FORKERNEL);
17246 			ASSERT(rval == DDI_SUCCESS);
17247 			if (sata_check_for_dma_error(dip, spx)) {
17248 				ddi_fm_service_impact(dip,
17249 				    DDI_SERVICE_UNAFFECTED);
17250 				rval = -1;
17251 				goto fail;
17252 			}
17253 		}
17254 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17255 		    sizeof (struct read_log_ext_directory));
17256 		rval = 0;
17257 	}
17258 
17259 fail:
17260 	/* Free allocated resources */
17261 	sata_free_local_buffer(spx);
17262 	sata_pkt_free(spx);
17263 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17264 
17265 	return (rval);
17266 }
17267 
17268 /*
17269  * Set up error retrieval sata command for NCQ command error data
17270  * recovery.
17271  *
17272  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17273  * returns SATA_FAILURE otherwise.
17274  */
17275 static int
17276 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17277 {
17278 #ifndef __lock_lint
17279 	_NOTE(ARGUNUSED(sdinfo))
17280 #endif
17281 
17282 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17283 	sata_cmd_t *scmd;
17284 	struct buf *bp;
17285 
17286 	/* Operation modes are up to the caller */
17287 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17288 
17289 	/* Synchronous mode, no callback - may be changed by the caller */
17290 	spkt->satapkt_comp = NULL;
17291 	spkt->satapkt_time = sata_default_pkt_time;
17292 
17293 	scmd = &spkt->satapkt_cmd;
17294 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17295 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17296 
17297 	/*
17298 	 * Allocate dma_able buffer error data.
17299 	 * Buffer allocation will take care of buffer alignment and other DMA
17300 	 * attributes.
17301 	 */
17302 	bp = sata_alloc_local_buffer(spx,
17303 	    sizeof (struct sata_ncq_error_recovery_page));
17304 	if (bp == NULL)
17305 		return (SATA_FAILURE);
17306 
17307 	bp_mapin(bp); /* make data buffer accessible */
17308 	scmd->satacmd_bp = bp;
17309 
17310 	/*
17311 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17312 	 * before accessing it. Handle is in usual place in translate struct.
17313 	 */
17314 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17315 
17316 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17317 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17318 
17319 	return (SATA_SUCCESS);
17320 }
17321 
17322 /*
17323  * sata_xlate_errors() is used to translate (S)ATA error
17324  * information to SCSI information returned in the SCSI
17325  * packet.
17326  */
17327 static void
17328 sata_xlate_errors(sata_pkt_txlate_t *spx)
17329 {
17330 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17331 	struct scsi_extended_sense *sense;
17332 
17333 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17334 	*scsipkt->pkt_scbp = STATUS_CHECK;
17335 	sense = sata_arq_sense(spx);
17336 
17337 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17338 	case SATA_PKT_PORT_ERROR:
17339 		/*
17340 		 * We have no device data. Assume no data transfered.
17341 		 */
17342 		sense->es_key = KEY_HARDWARE_ERROR;
17343 		break;
17344 
17345 	case SATA_PKT_DEV_ERROR:
17346 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17347 		    SATA_STATUS_ERR) {
17348 			/*
17349 			 * determine dev error reason from error
17350 			 * reg content
17351 			 */
17352 			sata_decode_device_error(spx, sense);
17353 			break;
17354 		}
17355 		/* No extended sense key - no info available */
17356 		break;
17357 
17358 	case SATA_PKT_TIMEOUT:
17359 		scsipkt->pkt_reason = CMD_TIMEOUT;
17360 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17361 		/* No extended sense key */
17362 		break;
17363 
17364 	case SATA_PKT_ABORTED:
17365 		scsipkt->pkt_reason = CMD_ABORTED;
17366 		scsipkt->pkt_statistics |= STAT_ABORTED;
17367 		/* No extended sense key */
17368 		break;
17369 
17370 	case SATA_PKT_RESET:
17371 		/*
17372 		 * pkt aborted either by an explicit reset request from
17373 		 * a host, or due to error recovery
17374 		 */
17375 		scsipkt->pkt_reason = CMD_RESET;
17376 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17377 		break;
17378 
17379 	default:
17380 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17381 		break;
17382 	}
17383 }
17384 
17385 
17386 
17387 
17388 /*
17389  * Log sata message
17390  * dev pathname msg line preceeds the logged message.
17391  */
17392 
17393 static	void
17394 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17395 {
17396 	char pathname[128];
17397 	dev_info_t *dip = NULL;
17398 	va_list ap;
17399 
17400 	mutex_enter(&sata_log_mutex);
17401 
17402 	va_start(ap, fmt);
17403 	(void) vsprintf(sata_log_buf, fmt, ap);
17404 	va_end(ap);
17405 
17406 	if (sata_hba_inst != NULL) {
17407 		dip = SATA_DIP(sata_hba_inst);
17408 		(void) ddi_pathname(dip, pathname);
17409 	} else {
17410 		pathname[0] = 0;
17411 	}
17412 	if (level == CE_CONT) {
17413 		if (sata_debug_flags == 0)
17414 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17415 		else
17416 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17417 	} else {
17418 		if (level != CE_NOTE) {
17419 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17420 		} else if (sata_msg) {
17421 			cmn_err(level, "%s:\n %s", pathname,
17422 			    sata_log_buf);
17423 		}
17424 	}
17425 
17426 	/* sata trace debug */
17427 	sata_trace_debug(dip, sata_log_buf);
17428 
17429 	mutex_exit(&sata_log_mutex);
17430 }
17431 
17432 
17433 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17434 
17435 /*
17436  * Start or terminate the thread, depending on flag arg and current state
17437  */
17438 static void
17439 sata_event_thread_control(int startstop)
17440 {
17441 	static 	int sata_event_thread_terminating = 0;
17442 	static 	int sata_event_thread_starting = 0;
17443 	int i;
17444 
17445 	mutex_enter(&sata_event_mutex);
17446 
17447 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17448 	    sata_event_thread_terminating == 1)) {
17449 		mutex_exit(&sata_event_mutex);
17450 		return;
17451 	}
17452 	if (startstop == 1 && sata_event_thread_starting == 1) {
17453 		mutex_exit(&sata_event_mutex);
17454 		return;
17455 	}
17456 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17457 		sata_event_thread_starting = 1;
17458 		/* wait til terminate operation completes */
17459 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17460 		while (sata_event_thread_terminating == 1) {
17461 			if (i-- <= 0) {
17462 				sata_event_thread_starting = 0;
17463 				mutex_exit(&sata_event_mutex);
17464 #ifdef SATA_DEBUG
17465 				cmn_err(CE_WARN, "sata_event_thread_control: "
17466 				    "timeout waiting for thread to terminate");
17467 #endif
17468 				return;
17469 			}
17470 			mutex_exit(&sata_event_mutex);
17471 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17472 			mutex_enter(&sata_event_mutex);
17473 		}
17474 	}
17475 	if (startstop == 1) {
17476 		if (sata_event_thread == NULL) {
17477 			sata_event_thread = thread_create(NULL, 0,
17478 			    (void (*)())sata_event_daemon,
17479 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17480 		}
17481 		sata_event_thread_starting = 0;
17482 		mutex_exit(&sata_event_mutex);
17483 		return;
17484 	}
17485 
17486 	/*
17487 	 * If we got here, thread may need to be terminated
17488 	 */
17489 	if (sata_event_thread != NULL) {
17490 		int i;
17491 		/* Signal event thread to go away */
17492 		sata_event_thread_terminating = 1;
17493 		sata_event_thread_terminate = 1;
17494 		cv_signal(&sata_event_cv);
17495 		/*
17496 		 * Wait til daemon terminates.
17497 		 */
17498 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17499 		while (sata_event_thread_terminate == 1) {
17500 			mutex_exit(&sata_event_mutex);
17501 			if (i-- <= 0) {
17502 				/* Daemon did not go away !!! */
17503 #ifdef SATA_DEBUG
17504 				cmn_err(CE_WARN, "sata_event_thread_control: "
17505 				    "cannot terminate event daemon thread");
17506 #endif
17507 				mutex_enter(&sata_event_mutex);
17508 				break;
17509 			}
17510 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17511 			mutex_enter(&sata_event_mutex);
17512 		}
17513 		sata_event_thread_terminating = 0;
17514 	}
17515 	ASSERT(sata_event_thread_terminating == 0);
17516 	ASSERT(sata_event_thread_starting == 0);
17517 	mutex_exit(&sata_event_mutex);
17518 }
17519 
17520 
17521 /*
17522  * SATA HBA event notification function.
17523  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17524  * a port and/or device state or a controller itself.
17525  * Events for different addresses/addr types cannot be combined.
17526  * A warning message is generated for each event type.
17527  * Events are not processed by this function, so only the
17528  * event flag(s)is set for an affected entity and the event thread is
17529  * waken up. Event daemon thread processes all events.
17530  *
17531  * NOTE: Since more than one event may be reported at the same time, one
17532  * cannot determine a sequence of events when opposite event are reported, eg.
17533  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17534  * is taking precedence over reported events, i.e. may cause ignoring some
17535  * events.
17536  */
17537 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17538 
17539 void
17540 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17541 {
17542 	sata_hba_inst_t *sata_hba_inst = NULL;
17543 	sata_address_t *saddr;
17544 	sata_pmult_info_t *pmultinfo;
17545 	sata_drive_info_t *sdinfo;
17546 	sata_port_stats_t *pstats;
17547 	sata_cport_info_t *cportinfo;
17548 	sata_pmport_info_t *pmportinfo;
17549 	int cport, pmport;
17550 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17551 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17552 	char *lcp;
17553 	static char *err_msg_evnt_1 =
17554 	    "sata_hba_event_notify: invalid port event 0x%x ";
17555 	static char *err_msg_evnt_2 =
17556 	    "sata_hba_event_notify: invalid device event 0x%x ";
17557 	int linkevent;
17558 
17559 	/*
17560 	 * There is a possibility that an event will be generated on HBA
17561 	 * that has not completed attachment or is detaching. We still want
17562 	 * to process events until HBA is detached.
17563 	 */
17564 	mutex_enter(&sata_mutex);
17565 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17566 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17567 		if (SATA_DIP(sata_hba_inst) == dip)
17568 			if (sata_hba_inst->satahba_attached == 1)
17569 				break;
17570 	}
17571 	mutex_exit(&sata_mutex);
17572 	if (sata_hba_inst == NULL)
17573 		/* HBA not attached */
17574 		return;
17575 
17576 	ASSERT(sata_device != NULL);
17577 
17578 	/*
17579 	 * Validate address before - do not proceed with invalid address.
17580 	 */
17581 	saddr = &sata_device->satadev_addr;
17582 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17583 		return;
17584 
17585 	cport = saddr->cport;
17586 	pmport = saddr->pmport;
17587 
17588 	buf1[0] = buf2[0] = '\0';
17589 
17590 	/*
17591 	 * If event relates to port or device, check port state.
17592 	 * Port has to be initialized, or we cannot accept an event.
17593 	 */
17594 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17595 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17596 		mutex_enter(&sata_hba_inst->satahba_mutex);
17597 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17598 		mutex_exit(&sata_hba_inst->satahba_mutex);
17599 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17600 			return;
17601 	}
17602 
17603 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17604 	    SATA_ADDR_DPMPORT)) != 0) {
17605 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17606 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17607 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17608 			    "is attached to port %d, ignore pmult/pmport "
17609 			    "event 0x%x", cportinfo->cport_dev_type,
17610 			    cport, event));
17611 			return;
17612 		}
17613 
17614 		mutex_enter(&cportinfo->cport_mutex);
17615 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17616 		mutex_exit(&cportinfo->cport_mutex);
17617 
17618 		/*
17619 		 * The daemon might be processing attachment of port
17620 		 * multiplier, in that case we should ignore events on its
17621 		 * sub-devices.
17622 		 *
17623 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17624 		 * The pmport_state is checked by sata daemon.
17625 		 */
17626 		if (pmultinfo == NULL ||
17627 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17628 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17629 			    "sata_hba_event_notify: pmult is not"
17630 			    "available at port %d:%d, ignore event 0x%x",
17631 			    cport, pmport, event));
17632 			return;
17633 		}
17634 	}
17635 
17636 	if ((saddr->qual &
17637 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17638 
17639 		mutex_enter(&cportinfo->cport_mutex);
17640 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17641 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17642 			    "sata_hba_event_notify: invalid/"
17643 			    "un-implemented port %d:%d (%d ports), "
17644 			    "ignore event 0x%x", cport, pmport,
17645 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17646 			mutex_exit(&cportinfo->cport_mutex);
17647 			return;
17648 		}
17649 		mutex_exit(&cportinfo->cport_mutex);
17650 
17651 		mutex_enter(&sata_hba_inst->satahba_mutex);
17652 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17653 		    cport, pmport);
17654 		mutex_exit(&sata_hba_inst->satahba_mutex);
17655 
17656 		/* pmport is implemented/valid? */
17657 		if (pmportinfo == NULL) {
17658 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17659 			    "sata_hba_event_notify: invalid/"
17660 			    "un-implemented port %d:%d, ignore "
17661 			    "event 0x%x", cport, pmport, event));
17662 			return;
17663 		}
17664 	}
17665 
17666 	/*
17667 	 * Events refer to devices, ports and controllers - each has
17668 	 * unique address. Events for different addresses cannot be combined.
17669 	 */
17670 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17671 
17672 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17673 
17674 		/* qualify this event(s) */
17675 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17676 			/* Invalid event for the device port */
17677 			(void) sprintf(buf2, err_msg_evnt_1,
17678 			    event & SATA_EVNT_PORT_EVENTS);
17679 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17680 			goto event_info;
17681 		}
17682 		if (saddr->qual == SATA_ADDR_CPORT) {
17683 			/* Controller's device port event */
17684 
17685 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17686 			    cport_event_flags |=
17687 			    event & SATA_EVNT_PORT_EVENTS;
17688 			pstats =
17689 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17690 			    cport_stats;
17691 		} else {
17692 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17693 			mutex_enter(&pmportinfo->pmport_mutex);
17694 			/* Port multiplier's device port event */
17695 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17696 			    pmport_event_flags |=
17697 			    event & SATA_EVNT_PORT_EVENTS;
17698 			pstats =
17699 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17700 			    pmport_stats;
17701 			mutex_exit(&pmportinfo->pmport_mutex);
17702 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17703 		}
17704 
17705 		/*
17706 		 * Add to statistics and log the message. We have to do it
17707 		 * here rather than in the event daemon, because there may be
17708 		 * multiple events occuring before they are processed.
17709 		 */
17710 		linkevent = event &
17711 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17712 		if (linkevent) {
17713 			if (linkevent == (SATA_EVNT_LINK_LOST |
17714 			    SATA_EVNT_LINK_ESTABLISHED)) {
17715 				/* This is likely event combination */
17716 				(void) strlcat(buf1, "link lost/established, ",
17717 				    SATA_EVENT_MAX_MSG_LENGTH);
17718 
17719 				if (pstats->link_lost < 0xffffffffffffffffULL)
17720 					pstats->link_lost++;
17721 				if (pstats->link_established <
17722 				    0xffffffffffffffffULL)
17723 					pstats->link_established++;
17724 				linkevent = 0;
17725 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17726 				(void) strlcat(buf1, "link lost, ",
17727 				    SATA_EVENT_MAX_MSG_LENGTH);
17728 
17729 				if (pstats->link_lost < 0xffffffffffffffffULL)
17730 					pstats->link_lost++;
17731 			} else {
17732 				(void) strlcat(buf1, "link established, ",
17733 				    SATA_EVENT_MAX_MSG_LENGTH);
17734 				if (pstats->link_established <
17735 				    0xffffffffffffffffULL)
17736 					pstats->link_established++;
17737 			}
17738 		}
17739 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17740 			(void) strlcat(buf1, "device attached, ",
17741 			    SATA_EVENT_MAX_MSG_LENGTH);
17742 			if (pstats->device_attached < 0xffffffffffffffffULL)
17743 				pstats->device_attached++;
17744 		}
17745 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17746 			(void) strlcat(buf1, "device detached, ",
17747 			    SATA_EVENT_MAX_MSG_LENGTH);
17748 			if (pstats->device_detached < 0xffffffffffffffffULL)
17749 				pstats->device_detached++;
17750 		}
17751 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17752 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17753 			    "port %d power level changed", cport);
17754 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17755 				pstats->port_pwr_changed++;
17756 		}
17757 
17758 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17759 			/* There should be no other events for this address */
17760 			(void) sprintf(buf2, err_msg_evnt_1,
17761 			    event & ~SATA_EVNT_PORT_EVENTS);
17762 		}
17763 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17764 
17765 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17766 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17767 
17768 		/* qualify this event */
17769 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17770 			/* Invalid event for a device */
17771 			(void) sprintf(buf2, err_msg_evnt_2,
17772 			    event & SATA_EVNT_DEVICE_RESET);
17773 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17774 			goto event_info;
17775 		}
17776 		/* drive event */
17777 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17778 		if (sdinfo != NULL) {
17779 			if (event & SATA_EVNT_DEVICE_RESET) {
17780 				(void) strlcat(buf1, "device reset, ",
17781 				    SATA_EVENT_MAX_MSG_LENGTH);
17782 				if (sdinfo->satadrv_stats.drive_reset <
17783 				    0xffffffffffffffffULL)
17784 					sdinfo->satadrv_stats.drive_reset++;
17785 				sdinfo->satadrv_event_flags |=
17786 				    SATA_EVNT_DEVICE_RESET;
17787 			}
17788 		}
17789 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17790 			/* Invalid event for a device */
17791 			(void) sprintf(buf2, err_msg_evnt_2,
17792 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17793 		}
17794 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17795 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17796 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17797 
17798 		/* qualify this event */
17799 		if ((event & (SATA_EVNT_DEVICE_RESET |
17800 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17801 			/* Invalid event for a port multiplier */
17802 			(void) sprintf(buf2, err_msg_evnt_2,
17803 			    event & SATA_EVNT_DEVICE_RESET);
17804 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17805 			goto event_info;
17806 		}
17807 
17808 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17809 
17810 		if (event & SATA_EVNT_DEVICE_RESET) {
17811 
17812 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17813 			    "[Reset] port-mult on cport %d", cport);
17814 			pmultinfo->pmult_event_flags |=
17815 			    SATA_EVNT_DEVICE_RESET;
17816 			(void) strlcat(buf1, "pmult reset, ",
17817 			    SATA_EVENT_MAX_MSG_LENGTH);
17818 		}
17819 
17820 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17821 
17822 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17823 			    "pmult link changed on cport %d", cport);
17824 			pmultinfo->pmult_event_flags |=
17825 			    SATA_EVNT_PMULT_LINK_CHANGED;
17826 			(void) strlcat(buf1, "pmult link changed, ",
17827 			    SATA_EVENT_MAX_MSG_LENGTH);
17828 		}
17829 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17830 
17831 	} else {
17832 		if (saddr->qual != SATA_ADDR_NULL) {
17833 			/* Wrong address qualifier */
17834 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17835 			    "sata_hba_event_notify: invalid address 0x%x",
17836 			    *(uint32_t *)saddr));
17837 			return;
17838 		}
17839 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17840 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17841 			/* Invalid event for the controller */
17842 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17843 			    "sata_hba_event_notify: invalid event 0x%x for "
17844 			    "controller",
17845 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17846 			return;
17847 		}
17848 		buf1[0] = '\0';
17849 		/* This may be a frequent and not interesting event */
17850 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17851 		    "controller power level changed\n", NULL);
17852 
17853 		mutex_enter(&sata_hba_inst->satahba_mutex);
17854 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17855 		    0xffffffffffffffffULL)
17856 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17857 
17858 		sata_hba_inst->satahba_event_flags |=
17859 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17860 		mutex_exit(&sata_hba_inst->satahba_mutex);
17861 	}
17862 	/*
17863 	 * If we got here, there is something to do with this HBA
17864 	 * instance.
17865 	 */
17866 	mutex_enter(&sata_hba_inst->satahba_mutex);
17867 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17868 	mutex_exit(&sata_hba_inst->satahba_mutex);
17869 	mutex_enter(&sata_mutex);
17870 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17871 	mutex_exit(&sata_mutex);
17872 
17873 	/* Tickle event thread */
17874 	mutex_enter(&sata_event_mutex);
17875 	if (sata_event_thread_active == 0)
17876 		cv_signal(&sata_event_cv);
17877 	mutex_exit(&sata_event_mutex);
17878 
17879 event_info:
17880 	if (buf1[0] != '\0') {
17881 		lcp = strrchr(buf1, ',');
17882 		if (lcp != NULL)
17883 			*lcp = '\0';
17884 	}
17885 	if (saddr->qual == SATA_ADDR_CPORT ||
17886 	    saddr->qual == SATA_ADDR_DCPORT) {
17887 		if (buf1[0] != '\0') {
17888 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17889 			    cport, buf1);
17890 		}
17891 		if (buf2[0] != '\0') {
17892 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17893 			    cport, buf2);
17894 		}
17895 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17896 	    saddr->qual == SATA_ADDR_DPMPORT) {
17897 		if (buf1[0] != '\0') {
17898 			sata_log(sata_hba_inst, CE_NOTE,
17899 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17900 		}
17901 		if (buf2[0] != '\0') {
17902 			sata_log(sata_hba_inst, CE_NOTE,
17903 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17904 		}
17905 	}
17906 }
17907 
17908 
17909 /*
17910  * Event processing thread.
17911  * Arg is a pointer to the sata_hba_list pointer.
17912  * It is not really needed, because sata_hba_list is global and static
17913  */
17914 static void
17915 sata_event_daemon(void *arg)
17916 {
17917 #ifndef __lock_lint
17918 	_NOTE(ARGUNUSED(arg))
17919 #endif
17920 	sata_hba_inst_t *sata_hba_inst;
17921 	clock_t delta;
17922 
17923 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17924 	    "SATA event daemon started\n", NULL);
17925 loop:
17926 	/*
17927 	 * Process events here. Walk through all registered HBAs
17928 	 */
17929 	mutex_enter(&sata_mutex);
17930 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17931 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17932 		ASSERT(sata_hba_inst != NULL);
17933 		mutex_enter(&sata_hba_inst->satahba_mutex);
17934 		if (sata_hba_inst->satahba_attached == 0 ||
17935 		    (sata_hba_inst->satahba_event_flags &
17936 		    SATA_EVNT_SKIP) != 0) {
17937 			mutex_exit(&sata_hba_inst->satahba_mutex);
17938 			continue;
17939 		}
17940 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
17941 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
17942 			mutex_exit(&sata_hba_inst->satahba_mutex);
17943 			mutex_exit(&sata_mutex);
17944 			/* Got the controller with pending event */
17945 			sata_process_controller_events(sata_hba_inst);
17946 			/*
17947 			 * Since global mutex was released, there is a
17948 			 * possibility that HBA list has changed, so start
17949 			 * over from the top. Just processed controller
17950 			 * will be passed-over because of the SKIP flag.
17951 			 */
17952 			goto loop;
17953 		}
17954 		mutex_exit(&sata_hba_inst->satahba_mutex);
17955 	}
17956 	/* Clear SKIP flag in all controllers */
17957 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17958 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17959 		mutex_enter(&sata_hba_inst->satahba_mutex);
17960 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
17961 		mutex_exit(&sata_hba_inst->satahba_mutex);
17962 	}
17963 	mutex_exit(&sata_mutex);
17964 
17965 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17966 	    "SATA EVENT DAEMON suspending itself", NULL);
17967 
17968 #ifdef SATA_DEBUG
17969 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
17970 		sata_log(sata_hba_inst, CE_WARN,
17971 		    "SATA EVENTS PROCESSING DISABLED\n");
17972 		thread_exit(); /* Daemon will not run again */
17973 	}
17974 #endif
17975 	mutex_enter(&sata_event_mutex);
17976 	sata_event_thread_active = 0;
17977 	mutex_exit(&sata_event_mutex);
17978 	/*
17979 	 * Go to sleep/suspend itself and wake up either because new event or
17980 	 * wait timeout. Exit if there is a termination request (driver
17981 	 * unload).
17982 	 */
17983 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
17984 	do {
17985 		mutex_enter(&sata_event_mutex);
17986 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
17987 		    delta, TR_CLOCK_TICK);
17988 
17989 		if (sata_event_thread_active != 0) {
17990 			mutex_exit(&sata_event_mutex);
17991 			continue;
17992 		}
17993 
17994 		/* Check if it is time to go away */
17995 		if (sata_event_thread_terminate == 1) {
17996 			/*
17997 			 * It is up to the thread setting above flag to make
17998 			 * sure that this thread is not killed prematurely.
17999 			 */
18000 			sata_event_thread_terminate = 0;
18001 			sata_event_thread = NULL;
18002 			mutex_exit(&sata_event_mutex);
18003 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18004 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18005 			thread_exit();  { _NOTE(NOT_REACHED) }
18006 		}
18007 		mutex_exit(&sata_event_mutex);
18008 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18009 
18010 	mutex_enter(&sata_event_mutex);
18011 	sata_event_thread_active = 1;
18012 	mutex_exit(&sata_event_mutex);
18013 
18014 	mutex_enter(&sata_mutex);
18015 	sata_event_pending &= ~SATA_EVNT_MAIN;
18016 	mutex_exit(&sata_mutex);
18017 
18018 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18019 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18020 
18021 	goto loop;
18022 }
18023 
18024 /*
18025  * Specific HBA instance event processing.
18026  *
18027  * NOTE: At the moment, device event processing is limited to hard disks
18028  * only.
18029  * Port multiplier is supported now.
18030  */
18031 static void
18032 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18033 {
18034 	int ncport;
18035 	uint32_t event_flags;
18036 	sata_address_t *saddr;
18037 	sata_cport_info_t *cportinfo;
18038 	sata_pmult_info_t *pmultinfo;
18039 
18040 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18041 	    "Processing controller %d event(s)",
18042 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18043 
18044 	mutex_enter(&sata_hba_inst->satahba_mutex);
18045 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18046 	event_flags = sata_hba_inst->satahba_event_flags;
18047 	mutex_exit(&sata_hba_inst->satahba_mutex);
18048 	/*
18049 	 * Process controller power change first
18050 	 * HERE
18051 	 */
18052 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18053 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18054 
18055 	/*
18056 	 * Search through ports/devices to identify affected port/device.
18057 	 * We may have to process events for more than one port/device.
18058 	 */
18059 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18060 		/*
18061 		 * Not all ports may be processed in attach by the time we
18062 		 * get an event. Check if port info is initialized.
18063 		 */
18064 		mutex_enter(&sata_hba_inst->satahba_mutex);
18065 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18066 		mutex_exit(&sata_hba_inst->satahba_mutex);
18067 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18068 			continue;
18069 
18070 		/* We have initialized controller port info */
18071 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18072 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18073 		    cport_event_flags;
18074 		/* Check if port was locked by IOCTL processing */
18075 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18076 			/*
18077 			 * We ignore port events because port is busy
18078 			 * with AP control processing. Set again
18079 			 * controller and main event flag, so that
18080 			 * events may be processed by the next daemon
18081 			 * run.
18082 			 */
18083 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18084 			mutex_enter(&sata_hba_inst->satahba_mutex);
18085 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18086 			mutex_exit(&sata_hba_inst->satahba_mutex);
18087 			mutex_enter(&sata_mutex);
18088 			sata_event_pending |= SATA_EVNT_MAIN;
18089 			mutex_exit(&sata_mutex);
18090 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18091 			    "Event processing postponed until "
18092 			    "AP control processing completes",
18093 			    NULL);
18094 			/* Check other ports */
18095 			continue;
18096 		} else {
18097 			/*
18098 			 * Set BSY flag so that AP control would not
18099 			 * interfere with events processing for
18100 			 * this port.
18101 			 */
18102 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18103 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18104 		}
18105 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18106 
18107 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18108 
18109 		if ((event_flags &
18110 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18111 			/*
18112 			 * Got port event.
18113 			 * We need some hierarchy of event processing as they
18114 			 * are affecting each other:
18115 			 * 1. port failed
18116 			 * 2. device detached/attached
18117 			 * 3. link events - link events may trigger device
18118 			 *    detached or device attached events in some
18119 			 *    circumstances.
18120 			 * 4. port power level changed
18121 			 */
18122 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18123 				sata_process_port_failed_event(sata_hba_inst,
18124 				    saddr);
18125 			}
18126 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18127 				sata_process_device_detached(sata_hba_inst,
18128 				    saddr);
18129 			}
18130 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18131 				sata_process_device_attached(sata_hba_inst,
18132 				    saddr);
18133 			}
18134 			if (event_flags &
18135 			    (SATA_EVNT_LINK_ESTABLISHED |
18136 			    SATA_EVNT_LINK_LOST)) {
18137 				sata_process_port_link_events(sata_hba_inst,
18138 				    saddr);
18139 			}
18140 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18141 				sata_process_port_pwr_change(sata_hba_inst,
18142 				    saddr);
18143 			}
18144 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18145 				sata_process_target_node_cleanup(
18146 				    sata_hba_inst, saddr);
18147 			}
18148 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18149 				sata_process_device_autoonline(
18150 				    sata_hba_inst, saddr);
18151 			}
18152 		}
18153 
18154 
18155 		/*
18156 		 * Scan port multiplier and all its sub-ports event flags.
18157 		 * The events are marked by
18158 		 * (1) sata_pmult_info.pmult_event_flags
18159 		 * (2) sata_pmport_info.pmport_event_flags
18160 		 */
18161 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18162 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18163 			/*
18164 			 * There should be another extra check: this
18165 			 * port multiplier still exists?
18166 			 */
18167 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18168 			    ncport);
18169 
18170 			if (pmultinfo != NULL) {
18171 				mutex_exit(&(SATA_CPORT_MUTEX(
18172 				    sata_hba_inst, ncport)));
18173 				sata_process_pmult_events(
18174 				    sata_hba_inst, ncport);
18175 				mutex_enter(&(SATA_CPORT_MUTEX(
18176 				    sata_hba_inst, ncport)));
18177 			} else {
18178 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18179 				    "Port-multiplier is gone. "
18180 				    "Ignore all sub-device events "
18181 				    "at port %d.", ncport);
18182 			}
18183 		}
18184 
18185 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18186 		    SATA_DTYPE_NONE) &&
18187 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18188 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18189 			    satadrv_event_flags &
18190 			    (SATA_EVNT_DEVICE_RESET |
18191 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18192 				/* Have device event */
18193 				sata_process_device_reset(sata_hba_inst,
18194 				    saddr);
18195 			}
18196 		}
18197 		/* Release PORT_BUSY flag */
18198 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18199 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18200 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18201 
18202 	} /* End of loop through the controller SATA ports */
18203 }
18204 
18205 /*
18206  * Specific port multiplier instance event processing. At the moment, device
18207  * event processing is limited to link/attach event only.
18208  *
18209  * NOTE: power management event is not supported yet.
18210  */
18211 static void
18212 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18213 {
18214 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18215 	sata_pmult_info_t *pmultinfo;
18216 	sata_pmport_info_t *pmportinfo;
18217 	sata_address_t *saddr;
18218 	sata_device_t sata_device;
18219 	uint32_t event_flags;
18220 	int npmport;
18221 	int rval;
18222 
18223 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18224 	    "Processing pmult event(s) on cport %d of controller %d",
18225 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18226 
18227 	/* First process events on port multiplier */
18228 	mutex_enter(&cportinfo->cport_mutex);
18229 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18230 	event_flags = pmultinfo->pmult_event_flags;
18231 
18232 	/*
18233 	 * Reset event (of port multiplier) has higher priority because the
18234 	 * port multiplier itself might be failed or removed after reset.
18235 	 */
18236 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18237 		/*
18238 		 * The status of the sub-links are uncertain,
18239 		 * so mark all sub-ports as RESET
18240 		 */
18241 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18242 		    sata_hba_inst, cport); npmport ++) {
18243 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18244 			    cport, npmport);
18245 			if (pmportinfo == NULL) {
18246 				/* That's weird. */
18247 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18248 				    "sata_hba_event_notify: "
18249 				    "invalid/un-implemented "
18250 				    "port %d:%d (%d ports), ",
18251 				    cport, npmport, SATA_NUM_PMPORTS(
18252 				    sata_hba_inst, cport)));
18253 				continue;
18254 			}
18255 
18256 			mutex_enter(&pmportinfo->pmport_mutex);
18257 
18258 			/* Mark all pmport to unknow state. */
18259 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18260 			/* Mark all pmports with link events. */
18261 			pmportinfo->pmport_event_flags =
18262 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18263 			mutex_exit(&pmportinfo->pmport_mutex);
18264 		}
18265 
18266 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18267 		/*
18268 		 * We need probe the port multiplier to know what has
18269 		 * happened.
18270 		 */
18271 		bzero(&sata_device, sizeof (sata_device_t));
18272 		sata_device.satadev_rev = SATA_DEVICE_REV;
18273 		sata_device.satadev_addr.cport = cport;
18274 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18275 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18276 
18277 		mutex_exit(&cportinfo->cport_mutex);
18278 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18279 		    (SATA_DIP(sata_hba_inst), &sata_device);
18280 		mutex_enter(&cportinfo->cport_mutex);
18281 		if (rval != SATA_SUCCESS) {
18282 			/* Something went wrong? Fail the port */
18283 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18284 			mutex_exit(&cportinfo->cport_mutex);
18285 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18286 			    "SATA port %d probing failed", cport));
18287 
18288 			/* PMult structure must be released.  */
18289 			sata_free_pmult(sata_hba_inst, &sata_device);
18290 			return;
18291 		}
18292 
18293 		sata_update_port_info(sata_hba_inst, &sata_device);
18294 
18295 		/*
18296 		 * Sanity check - Port is active? Is the link active?
18297 		 * The device is still a port multiplier?
18298 		 */
18299 		if ((cportinfo->cport_state &
18300 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18301 		    ((cportinfo->cport_scr.sstatus &
18302 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18303 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18304 			mutex_exit(&cportinfo->cport_mutex);
18305 
18306 			/* PMult structure must be released.  */
18307 			sata_free_pmult(sata_hba_inst, &sata_device);
18308 			return;
18309 		}
18310 
18311 		/* Probed succeed, set port ready. */
18312 		cportinfo->cport_state |=
18313 		    SATA_STATE_PROBED | SATA_STATE_READY;
18314 	}
18315 
18316 	/* Release port multiplier event flags. */
18317 	pmultinfo->pmult_event_flags &=
18318 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18319 	mutex_exit(&cportinfo->cport_mutex);
18320 
18321 	/*
18322 	 * Check all sub-links.
18323 	 */
18324 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18325 	    npmport ++) {
18326 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18327 		mutex_enter(&pmportinfo->pmport_mutex);
18328 		event_flags = pmportinfo->pmport_event_flags;
18329 		mutex_exit(&pmportinfo->pmport_mutex);
18330 		saddr = &pmportinfo->pmport_addr;
18331 
18332 		if ((event_flags &
18333 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18334 			/*
18335 			 * Got port multiplier port event.
18336 			 * We need some hierarchy of event processing as they
18337 			 * are affecting each other:
18338 			 * 1. device detached/attached
18339 			 * 2. link events - link events may trigger device
18340 			 *    detached or device attached events in some
18341 			 *    circumstances.
18342 			 */
18343 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18344 				sata_process_pmdevice_detached(sata_hba_inst,
18345 				    saddr);
18346 			}
18347 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18348 				sata_process_pmdevice_attached(sata_hba_inst,
18349 				    saddr);
18350 			}
18351 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18352 			    event_flags & SATA_EVNT_LINK_LOST) {
18353 				sata_process_pmport_link_events(sata_hba_inst,
18354 				    saddr);
18355 			}
18356 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18357 				sata_process_target_node_cleanup(
18358 				    sata_hba_inst, saddr);
18359 			}
18360 		}
18361 
18362 		/* Checking drive event(s). */
18363 		mutex_enter(&pmportinfo->pmport_mutex);
18364 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18365 		    pmportinfo->pmport_sata_drive != NULL) {
18366 			event_flags = pmportinfo->pmport_sata_drive->
18367 			    satadrv_event_flags;
18368 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18369 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18370 
18371 				/* Have device event */
18372 				sata_process_pmdevice_reset(sata_hba_inst,
18373 				    saddr);
18374 			}
18375 		}
18376 		mutex_exit(&pmportinfo->pmport_mutex);
18377 
18378 		/* Release PORT_BUSY flag */
18379 		mutex_enter(&cportinfo->cport_mutex);
18380 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18381 		mutex_exit(&cportinfo->cport_mutex);
18382 	}
18383 
18384 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18385 	    "[DONE] pmult event(s) on cport %d of controller %d",
18386 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18387 }
18388 
18389 /*
18390  * Process HBA power level change reported by HBA driver.
18391  * Not implemented at this time - event is ignored.
18392  */
18393 static void
18394 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18395 {
18396 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18397 	    "Processing controller power level change", NULL);
18398 
18399 	/* Ignoring it for now */
18400 	mutex_enter(&sata_hba_inst->satahba_mutex);
18401 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18402 	mutex_exit(&sata_hba_inst->satahba_mutex);
18403 }
18404 
18405 /*
18406  * Process port power level change reported by HBA driver.
18407  * Not implemented at this time - event is ignored.
18408  */
18409 static void
18410 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18411     sata_address_t *saddr)
18412 {
18413 	sata_cport_info_t *cportinfo;
18414 
18415 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18416 	    "Processing port power level change", NULL);
18417 
18418 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18419 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18420 	/* Reset event flag */
18421 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18422 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18423 }
18424 
18425 /*
18426  * Process port failure reported by HBA driver.
18427  * cports support only - no pmports.
18428  */
18429 static void
18430 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18431     sata_address_t *saddr)
18432 {
18433 	sata_cport_info_t *cportinfo;
18434 
18435 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18436 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18437 	/* Reset event flag first */
18438 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18439 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18440 	if ((cportinfo->cport_state &
18441 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18442 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18443 		    cport_mutex);
18444 		return;
18445 	}
18446 	/* Fail the port */
18447 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18448 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18449 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18450 }
18451 
18452 /*
18453  * Device Reset Event processing.
18454  * The sequence is managed by 3 stage flags:
18455  * - reset event reported,
18456  * - reset event being processed,
18457  * - request to clear device reset state.
18458  *
18459  * NOTE: This function has to be entered with cport mutex held. It exits with
18460  * mutex held as well, but can release mutex during the processing.
18461  */
18462 static void
18463 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18464     sata_address_t *saddr)
18465 {
18466 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18467 	sata_drive_info_t *sdinfo;
18468 	sata_cport_info_t *cportinfo;
18469 	sata_device_t sata_device;
18470 	int rval_probe, rval_set;
18471 
18472 	/* We only care about host sata cport for now */
18473 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18474 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18475 	/*
18476 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18477 	 * state, ignore reset event.
18478 	 */
18479 	if (((cportinfo->cport_state &
18480 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18481 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18482 		sdinfo->satadrv_event_flags &=
18483 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18484 		return;
18485 	}
18486 
18487 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18488 	    SATA_DTYPE_PMULT)) {
18489 		/*
18490 		 * Should not happened: this is already handled in
18491 		 * sata_hba_event_notify()
18492 		 */
18493 		mutex_exit(&cportinfo->cport_mutex);
18494 		goto done;
18495 	}
18496 
18497 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18498 	    SATA_VALID_DEV_TYPE) == 0) {
18499 		/*
18500 		 * This should not happen - coding error.
18501 		 * But we can recover, so do not panic, just clean up
18502 		 * and if in debug mode, log the message.
18503 		 */
18504 #ifdef SATA_DEBUG
18505 		sata_log(sata_hba_inst, CE_WARN,
18506 		    "sata_process_device_reset: "
18507 		    "Invalid device type with sdinfo!", NULL);
18508 #endif
18509 		sdinfo->satadrv_event_flags = 0;
18510 		return;
18511 	}
18512 
18513 #ifdef SATA_DEBUG
18514 	if ((sdinfo->satadrv_event_flags &
18515 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18516 		/* Nothing to do */
18517 		/* Something is weird - why we are processing dev reset? */
18518 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18519 		    "No device reset event!!!!", NULL);
18520 
18521 		return;
18522 	}
18523 	if ((sdinfo->satadrv_event_flags &
18524 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18525 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18526 		/* Something is weird - new device reset event */
18527 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18528 		    "Overlapping device reset events!", NULL);
18529 	}
18530 #endif
18531 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18532 	    "Processing port %d device reset", saddr->cport);
18533 
18534 	/* Clear event flag */
18535 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18536 
18537 	/* It seems that we always need to check the port state first */
18538 	sata_device.satadev_rev = SATA_DEVICE_REV;
18539 	sata_device.satadev_addr = *saddr;
18540 	/*
18541 	 * We have to exit mutex, because the HBA probe port function may
18542 	 * block on its own mutex.
18543 	 */
18544 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18545 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18546 	    (SATA_DIP(sata_hba_inst), &sata_device);
18547 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18548 	sata_update_port_info(sata_hba_inst, &sata_device);
18549 	if (rval_probe != SATA_SUCCESS) {
18550 		/* Something went wrong? Fail the port */
18551 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18552 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18553 		if (sdinfo != NULL)
18554 			sdinfo->satadrv_event_flags = 0;
18555 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18556 		    cport_mutex);
18557 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18558 		    "SATA port %d probing failed",
18559 		    saddr->cport));
18560 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18561 		    saddr->cport)->cport_mutex);
18562 		return;
18563 	}
18564 	if ((sata_device.satadev_scr.sstatus  &
18565 	    SATA_PORT_DEVLINK_UP_MASK) !=
18566 	    SATA_PORT_DEVLINK_UP ||
18567 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18568 		/*
18569 		 * No device to process, anymore. Some other event processing
18570 		 * would or have already performed port info cleanup.
18571 		 * To be safe (HBA may need it), request clearing device
18572 		 * reset condition.
18573 		 */
18574 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18575 		if (sdinfo != NULL) {
18576 			sdinfo->satadrv_event_flags &=
18577 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18578 			sdinfo->satadrv_event_flags |=
18579 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18580 		}
18581 		return;
18582 	}
18583 
18584 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18585 	if (sdinfo == NULL) {
18586 		return;
18587 	}
18588 	if ((sdinfo->satadrv_event_flags &
18589 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18590 		/*
18591 		 * Start tracking time for device feature restoration and
18592 		 * identification. Save current time (lbolt value).
18593 		 */
18594 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18595 	}
18596 	/* Mark device reset processing as active */
18597 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18598 
18599 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18600 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18601 
18602 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18603 
18604 	if (rval_set  != SATA_SUCCESS) {
18605 		/*
18606 		 * Restoring drive setting failed.
18607 		 * Probe the port first, to check if the port state has changed
18608 		 */
18609 		sata_device.satadev_rev = SATA_DEVICE_REV;
18610 		sata_device.satadev_addr = *saddr;
18611 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18612 		/* probe port */
18613 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18614 		    (SATA_DIP(sata_hba_inst), &sata_device);
18615 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18616 		    cport_mutex);
18617 		if (rval_probe == SATA_SUCCESS &&
18618 		    (sata_device.satadev_state &
18619 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18620 		    (sata_device.satadev_scr.sstatus  &
18621 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18622 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18623 			/*
18624 			 * We may retry this a bit later - in-process reset
18625 			 * condition should be already set.
18626 			 * Track retry time for device identification.
18627 			 */
18628 			if ((cportinfo->cport_dev_type &
18629 			    SATA_VALID_DEV_TYPE) != 0 &&
18630 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18631 			    sdinfo->satadrv_reset_time != 0) {
18632 				clock_t cur_time = ddi_get_lbolt();
18633 				/*
18634 				 * If the retry time limit was not
18635 				 * exceeded, retry.
18636 				 */
18637 				if ((cur_time - sdinfo->satadrv_reset_time) <
18638 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18639 					mutex_enter(
18640 					    &sata_hba_inst->satahba_mutex);
18641 					sata_hba_inst->satahba_event_flags |=
18642 					    SATA_EVNT_MAIN;
18643 					mutex_exit(
18644 					    &sata_hba_inst->satahba_mutex);
18645 					mutex_enter(&sata_mutex);
18646 					sata_event_pending |= SATA_EVNT_MAIN;
18647 					mutex_exit(&sata_mutex);
18648 					return;
18649 				}
18650 				if (rval_set == SATA_RETRY) {
18651 					/*
18652 					 * Setting drive features failed, but
18653 					 * the drive is still accessible,
18654 					 * so emit a warning message before
18655 					 * return.
18656 					 */
18657 					mutex_exit(&SATA_CPORT_INFO(
18658 					    sata_hba_inst,
18659 					    saddr->cport)->cport_mutex);
18660 					goto done;
18661 				}
18662 			}
18663 			/* Fail the drive */
18664 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18665 
18666 			sata_log(sata_hba_inst, CE_WARN,
18667 			    "SATA device at port %d - device failed",
18668 			    saddr->cport);
18669 
18670 			DTRACE_PROBE(port_failed_f);
18671 		}
18672 		/*
18673 		 * No point of retrying - device failed or some other event
18674 		 * processing or already did or will do port info cleanup.
18675 		 * To be safe (HBA may need it),
18676 		 * request clearing device reset condition.
18677 		 */
18678 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18679 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18680 		sdinfo->satadrv_reset_time = 0;
18681 		return;
18682 	}
18683 done:
18684 	/*
18685 	 * If setting of drive features failed, but the drive is still
18686 	 * accessible, emit a warning message.
18687 	 */
18688 	if (rval_set == SATA_RETRY) {
18689 		sata_log(sata_hba_inst, CE_WARN,
18690 		    "SATA device at port %d - desired setting could not be "
18691 		    "restored after reset. Device may not operate as expected.",
18692 		    saddr->cport);
18693 	}
18694 	/*
18695 	 * Raise the flag indicating that the next sata command could
18696 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18697 	 * reset is reported.
18698 	 */
18699 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18700 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18701 		sdinfo->satadrv_reset_time = 0;
18702 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18703 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18704 			sdinfo->satadrv_event_flags &=
18705 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18706 			sdinfo->satadrv_event_flags |=
18707 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18708 		}
18709 	}
18710 }
18711 
18712 
18713 /*
18714  * Port Multiplier Port Device Reset Event processing.
18715  *
18716  * NOTE: This function has to be entered with pmport mutex held. It exits with
18717  * mutex held as well, but can release mutex during the processing.
18718  */
18719 static void
18720 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18721     sata_address_t *saddr)
18722 {
18723 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18724 	sata_drive_info_t *sdinfo = NULL;
18725 	sata_cport_info_t *cportinfo = NULL;
18726 	sata_pmport_info_t *pmportinfo = NULL;
18727 	sata_pmult_info_t *pminfo = NULL;
18728 	sata_device_t sata_device;
18729 	uint8_t cport = saddr->cport;
18730 	uint8_t pmport = saddr->pmport;
18731 	int rval;
18732 
18733 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18734 	    "Processing drive reset at port %d:%d", cport, pmport);
18735 
18736 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18737 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18738 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18739 
18740 	/*
18741 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18742 	 * state, ignore reset event.
18743 	 */
18744 	if (((cportinfo->cport_state &
18745 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18746 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18747 		sdinfo->satadrv_event_flags &=
18748 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18749 		return;
18750 	}
18751 
18752 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18753 		/*
18754 		 * This should not happen - coding error.
18755 		 * But we can recover, so do not panic, just clean up
18756 		 * and if in debug mode, log the message.
18757 		 */
18758 #ifdef SATA_DEBUG
18759 		sata_log(sata_hba_inst, CE_WARN,
18760 		    "sata_process_pmdevice_reset: "
18761 		    "Invalid device type with sdinfo!", NULL);
18762 #endif
18763 		sdinfo->satadrv_event_flags = 0;
18764 		return;
18765 	}
18766 
18767 #ifdef SATA_DEBUG
18768 	if ((sdinfo->satadrv_event_flags &
18769 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18770 		/* Nothing to do */
18771 		/* Something is weird - why we are processing dev reset? */
18772 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18773 		    "No device reset event!!!!", NULL);
18774 
18775 		return;
18776 	}
18777 	if ((sdinfo->satadrv_event_flags &
18778 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18779 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18780 		/* Something is weird - new device reset event */
18781 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18782 		    "Overlapping device reset events!", NULL);
18783 	}
18784 #endif
18785 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18786 	    "Processing port %d:%d device reset", cport, pmport);
18787 
18788 	/* Clear event flag */
18789 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18790 
18791 	/* It seems that we always need to check the port state first */
18792 	sata_device.satadev_rev = SATA_DEVICE_REV;
18793 	sata_device.satadev_addr = *saddr;
18794 	/*
18795 	 * We have to exit mutex, because the HBA probe port function may
18796 	 * block on its own mutex.
18797 	 */
18798 	mutex_exit(&pmportinfo->pmport_mutex);
18799 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18800 	    (SATA_DIP(sata_hba_inst), &sata_device);
18801 	mutex_enter(&pmportinfo->pmport_mutex);
18802 
18803 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18804 	if (rval != SATA_SUCCESS) {
18805 		/* Something went wrong? Fail the port */
18806 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18807 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18808 		    saddr->pmport);
18809 		if (sdinfo != NULL)
18810 			sdinfo->satadrv_event_flags = 0;
18811 		mutex_exit(&pmportinfo->pmport_mutex);
18812 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18813 		    "SATA port %d:%d probing failed",
18814 		    saddr->cport, saddr->pmport));
18815 		mutex_enter(&pmportinfo->pmport_mutex);
18816 		return;
18817 	}
18818 	if ((sata_device.satadev_scr.sstatus  &
18819 	    SATA_PORT_DEVLINK_UP_MASK) !=
18820 	    SATA_PORT_DEVLINK_UP ||
18821 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18822 		/*
18823 		 * No device to process, anymore. Some other event processing
18824 		 * would or have already performed port info cleanup.
18825 		 * To be safe (HBA may need it), request clearing device
18826 		 * reset condition.
18827 		 */
18828 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18829 		    saddr->pmport);
18830 		if (sdinfo != NULL) {
18831 			sdinfo->satadrv_event_flags &=
18832 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18833 			/* must clear flags on cport */
18834 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18835 			    saddr->cport);
18836 			pminfo->pmult_event_flags |=
18837 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18838 		}
18839 		return;
18840 	}
18841 
18842 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18843 	    saddr->pmport);
18844 	if (sdinfo == NULL) {
18845 		return;
18846 	}
18847 	if ((sdinfo->satadrv_event_flags &
18848 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18849 		/*
18850 		 * Start tracking time for device feature restoration and
18851 		 * identification. Save current time (lbolt value).
18852 		 */
18853 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18854 	}
18855 	/* Mark device reset processing as active */
18856 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18857 
18858 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18859 	mutex_exit(&pmportinfo->pmport_mutex);
18860 
18861 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18862 	    SATA_FAILURE) {
18863 		/*
18864 		 * Restoring drive setting failed.
18865 		 * Probe the port first, to check if the port state has changed
18866 		 */
18867 		sata_device.satadev_rev = SATA_DEVICE_REV;
18868 		sata_device.satadev_addr = *saddr;
18869 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18870 
18871 		/* probe port */
18872 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18873 		    (SATA_DIP(sata_hba_inst), &sata_device);
18874 		mutex_enter(&pmportinfo->pmport_mutex);
18875 		if (rval == SATA_SUCCESS &&
18876 		    (sata_device.satadev_state &
18877 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18878 		    (sata_device.satadev_scr.sstatus  &
18879 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18880 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18881 			/*
18882 			 * We may retry this a bit later - in-process reset
18883 			 * condition should be already set.
18884 			 * Track retry time for device identification.
18885 			 */
18886 			if ((pmportinfo->pmport_dev_type &
18887 			    SATA_VALID_DEV_TYPE) != 0 &&
18888 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18889 			    sdinfo->satadrv_reset_time != 0) {
18890 				clock_t cur_time = ddi_get_lbolt();
18891 				/*
18892 				 * If the retry time limit was not
18893 				 * exceeded, retry.
18894 				 */
18895 				if ((cur_time - sdinfo->satadrv_reset_time) <
18896 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18897 					mutex_enter(
18898 					    &sata_hba_inst->satahba_mutex);
18899 					sata_hba_inst->satahba_event_flags |=
18900 					    SATA_EVNT_MAIN;
18901 					mutex_exit(
18902 					    &sata_hba_inst->satahba_mutex);
18903 					mutex_enter(&sata_mutex);
18904 					sata_event_pending |= SATA_EVNT_MAIN;
18905 					mutex_exit(&sata_mutex);
18906 					return;
18907 				}
18908 			}
18909 			/* Fail the drive */
18910 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18911 
18912 			sata_log(sata_hba_inst, CE_WARN,
18913 			    "SATA device at port %d:%d - device failed",
18914 			    saddr->cport, saddr->pmport);
18915 		} else {
18916 			/*
18917 			 * No point of retrying - some other event processing
18918 			 * would or already did port info cleanup.
18919 			 * To be safe (HBA may need it),
18920 			 * request clearing device reset condition.
18921 			 */
18922 			sdinfo->satadrv_event_flags |=
18923 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18924 		}
18925 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18926 		sdinfo->satadrv_reset_time = 0;
18927 		return;
18928 	}
18929 	/*
18930 	 * Raise the flag indicating that the next sata command could
18931 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18932 	 * reset is reported.
18933 	 */
18934 	mutex_enter(&pmportinfo->pmport_mutex);
18935 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18936 		sdinfo->satadrv_reset_time = 0;
18937 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
18938 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18939 			sdinfo->satadrv_event_flags &=
18940 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18941 			/* must clear flags on cport */
18942 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18943 			    saddr->cport);
18944 			pminfo->pmult_event_flags |=
18945 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18946 		}
18947 	}
18948 }
18949 
18950 /*
18951  * Port Link Events processing.
18952  * Every link established event may involve device reset (due to
18953  * COMRESET signal, equivalent of the hard reset) so arbitrarily
18954  * set device reset event for an attached device (if any).
18955  * If the port is in SHUTDOWN or FAILED state, ignore link events.
18956  *
18957  * The link established event processing varies, depending on the state
18958  * of the target node, HBA hotplugging capabilities, state of the port.
18959  * If the link is not active, the link established event is ignored.
18960  * If HBA cannot detect device attachment and there is no target node,
18961  * the link established event triggers device attach event processing.
18962  * Else, link established event triggers device reset event processing.
18963  *
18964  * The link lost event processing varies, depending on a HBA hotplugging
18965  * capability and the state of the port (link active or not active).
18966  * If the link is active, the lost link event is ignored.
18967  * If HBA cannot detect device removal, the lost link event triggers
18968  * device detached event processing after link lost timeout.
18969  * Else, the event is ignored.
18970  *
18971  * NOTE: Port multiplier ports events are handled by
18972  * sata_process_pmport_link_events();
18973  */
18974 static void
18975 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
18976     sata_address_t *saddr)
18977 {
18978 	sata_device_t sata_device;
18979 	sata_cport_info_t *cportinfo;
18980 	sata_drive_info_t *sdinfo;
18981 	uint32_t event_flags;
18982 	int rval;
18983 
18984 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18985 	    "Processing port %d link event(s)", saddr->cport);
18986 
18987 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18988 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18989 	event_flags = cportinfo->cport_event_flags;
18990 
18991 	/* Reset event flags first */
18992 	cportinfo->cport_event_flags &=
18993 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
18994 
18995 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
18996 	if ((cportinfo->cport_state &
18997 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18998 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18999 		    cport_mutex);
19000 		return;
19001 	}
19002 
19003 	/*
19004 	 * For the sanity sake get current port state.
19005 	 * Set device address only. Other sata_device fields should be
19006 	 * set by HBA driver.
19007 	 */
19008 	sata_device.satadev_rev = SATA_DEVICE_REV;
19009 	sata_device.satadev_addr = *saddr;
19010 	/*
19011 	 * We have to exit mutex, because the HBA probe port function may
19012 	 * block on its own mutex.
19013 	 */
19014 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19015 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19016 	    (SATA_DIP(sata_hba_inst), &sata_device);
19017 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19018 	sata_update_port_info(sata_hba_inst, &sata_device);
19019 	if (rval != SATA_SUCCESS) {
19020 		/* Something went wrong? Fail the port */
19021 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19022 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19023 		    cport_mutex);
19024 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19025 		    "SATA port %d probing failed",
19026 		    saddr->cport));
19027 		/*
19028 		 * We may want to release device info structure, but
19029 		 * it is not necessary.
19030 		 */
19031 		return;
19032 	} else {
19033 		/* port probed successfully */
19034 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19035 	}
19036 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19037 
19038 		if ((sata_device.satadev_scr.sstatus &
19039 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19040 			/* Ignore event */
19041 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19042 			    "Ignoring port %d link established event - "
19043 			    "link down",
19044 			    saddr->cport);
19045 			goto linklost;
19046 		}
19047 
19048 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19049 		    "Processing port %d link established event",
19050 		    saddr->cport);
19051 
19052 		/*
19053 		 * For the sanity sake check if a device is attached - check
19054 		 * return state of a port probing.
19055 		 */
19056 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19057 			/*
19058 			 * HBA port probe indicated that there is a device
19059 			 * attached. Check if the framework had device info
19060 			 * structure attached for this device.
19061 			 */
19062 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19063 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19064 				    NULL);
19065 
19066 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19067 				if ((sdinfo->satadrv_type &
19068 				    SATA_VALID_DEV_TYPE) != 0) {
19069 					/*
19070 					 * Dev info structure is present.
19071 					 * If dev_type is set to known type in
19072 					 * the framework's drive info struct
19073 					 * then the device existed before and
19074 					 * the link was probably lost
19075 					 * momentarily - in such case
19076 					 * we may want to check device
19077 					 * identity.
19078 					 * Identity check is not supported now.
19079 					 *
19080 					 * Link established event
19081 					 * triggers device reset event.
19082 					 */
19083 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19084 					    satadrv_event_flags |=
19085 					    SATA_EVNT_DEVICE_RESET;
19086 				}
19087 			} else if (cportinfo->cport_dev_type ==
19088 			    SATA_DTYPE_NONE) {
19089 				/*
19090 				 * We got new device attached! If HBA does not
19091 				 * generate device attached events, trigger it
19092 				 * here.
19093 				 */
19094 				if (!(SATA_FEATURES(sata_hba_inst) &
19095 				    SATA_CTLF_HOTPLUG)) {
19096 					cportinfo->cport_event_flags |=
19097 					    SATA_EVNT_DEVICE_ATTACHED;
19098 				}
19099 			}
19100 			/* Reset link lost timeout */
19101 			cportinfo->cport_link_lost_time = 0;
19102 		}
19103 	}
19104 linklost:
19105 	if (event_flags & SATA_EVNT_LINK_LOST) {
19106 		if ((sata_device.satadev_scr.sstatus &
19107 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19108 			/* Ignore event */
19109 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19110 			    "Ignoring port %d link lost event - link is up",
19111 			    saddr->cport);
19112 			goto done;
19113 		}
19114 #ifdef SATA_DEBUG
19115 		if (cportinfo->cport_link_lost_time == 0) {
19116 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19117 			    "Processing port %d link lost event",
19118 			    saddr->cport);
19119 		}
19120 #endif
19121 		/*
19122 		 * When HBA cannot generate device attached/detached events,
19123 		 * we need to track link lost time and eventually generate
19124 		 * device detach event.
19125 		 */
19126 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19127 			/* We are tracking link lost time */
19128 			if (cportinfo->cport_link_lost_time == 0) {
19129 				/* save current time (lbolt value) */
19130 				cportinfo->cport_link_lost_time =
19131 				    ddi_get_lbolt();
19132 				/* just keep link lost event */
19133 				cportinfo->cport_event_flags |=
19134 				    SATA_EVNT_LINK_LOST;
19135 			} else {
19136 				clock_t cur_time = ddi_get_lbolt();
19137 				if ((cur_time -
19138 				    cportinfo->cport_link_lost_time) >=
19139 				    drv_usectohz(
19140 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19141 					/* trigger device detach event */
19142 					cportinfo->cport_event_flags |=
19143 					    SATA_EVNT_DEVICE_DETACHED;
19144 					cportinfo->cport_link_lost_time = 0;
19145 					SATADBG1(SATA_DBG_EVENTS,
19146 					    sata_hba_inst,
19147 					    "Triggering port %d "
19148 					    "device detached event",
19149 					    saddr->cport);
19150 				} else {
19151 					/* keep link lost event */
19152 					cportinfo->cport_event_flags |=
19153 					    SATA_EVNT_LINK_LOST;
19154 				}
19155 			}
19156 		}
19157 		/*
19158 		 * We could change port state to disable/delay access to
19159 		 * the attached device until the link is recovered.
19160 		 */
19161 	}
19162 done:
19163 	event_flags = cportinfo->cport_event_flags;
19164 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19165 	if (event_flags != 0) {
19166 		mutex_enter(&sata_hba_inst->satahba_mutex);
19167 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19168 		mutex_exit(&sata_hba_inst->satahba_mutex);
19169 		mutex_enter(&sata_mutex);
19170 		sata_event_pending |= SATA_EVNT_MAIN;
19171 		mutex_exit(&sata_mutex);
19172 	}
19173 }
19174 
19175 /*
19176  * Port Multiplier Port Link Events processing.
19177  */
19178 static void
19179 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19180     sata_address_t *saddr)
19181 {
19182 	sata_device_t sata_device;
19183 	sata_pmport_info_t *pmportinfo = NULL;
19184 	sata_drive_info_t *sdinfo = NULL;
19185 	uint32_t event_flags;
19186 	uint8_t cport = saddr->cport;
19187 	uint8_t pmport = saddr->pmport;
19188 	int rval;
19189 
19190 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19191 	    "Processing port %d:%d link event(s)",
19192 	    cport, pmport);
19193 
19194 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19195 	mutex_enter(&pmportinfo->pmport_mutex);
19196 	event_flags = pmportinfo->pmport_event_flags;
19197 
19198 	/* Reset event flags first */
19199 	pmportinfo->pmport_event_flags &=
19200 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19201 
19202 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19203 	if ((pmportinfo->pmport_state &
19204 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19205 		mutex_exit(&pmportinfo->pmport_mutex);
19206 		return;
19207 	}
19208 
19209 	/*
19210 	 * For the sanity sake get current port state.
19211 	 * Set device address only. Other sata_device fields should be
19212 	 * set by HBA driver.
19213 	 */
19214 	sata_device.satadev_rev = SATA_DEVICE_REV;
19215 	sata_device.satadev_addr = *saddr;
19216 	/*
19217 	 * We have to exit mutex, because the HBA probe port function may
19218 	 * block on its own mutex.
19219 	 */
19220 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19221 	    saddr->pmport));
19222 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19223 	    (SATA_DIP(sata_hba_inst), &sata_device);
19224 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19225 	    saddr->pmport));
19226 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19227 	if (rval != SATA_SUCCESS) {
19228 		/* Something went wrong? Fail the port */
19229 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19230 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19231 		    saddr->pmport));
19232 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19233 		    "SATA port %d:%d probing failed",
19234 		    saddr->cport, saddr->pmport));
19235 		/*
19236 		 * We may want to release device info structure, but
19237 		 * it is not necessary.
19238 		 */
19239 		return;
19240 	} else {
19241 		/* port probed successfully */
19242 		pmportinfo->pmport_state |=
19243 		    SATA_STATE_PROBED | SATA_STATE_READY;
19244 	}
19245 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19246 	    saddr->cport, saddr->pmport));
19247 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19248 	    saddr->cport, saddr->pmport));
19249 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19250 
19251 		if ((sata_device.satadev_scr.sstatus &
19252 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19253 			/* Ignore event */
19254 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19255 			    "Ignoring port %d:%d link established event - "
19256 			    "link down",
19257 			    saddr->cport, saddr->pmport);
19258 			goto linklost;
19259 		}
19260 
19261 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19262 		    "Processing port %d:%d link established event",
19263 		    cport, pmport);
19264 
19265 		/*
19266 		 * For the sanity sake check if a device is attached - check
19267 		 * return state of a port probing.
19268 		 */
19269 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19270 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19271 			/*
19272 			 * HBA port probe indicated that there is a device
19273 			 * attached. Check if the framework had device info
19274 			 * structure attached for this device.
19275 			 */
19276 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19277 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19278 				    NULL);
19279 
19280 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19281 				if ((sdinfo->satadrv_type &
19282 				    SATA_VALID_DEV_TYPE) != 0) {
19283 					/*
19284 					 * Dev info structure is present.
19285 					 * If dev_type is set to known type in
19286 					 * the framework's drive info struct
19287 					 * then the device existed before and
19288 					 * the link was probably lost
19289 					 * momentarily - in such case
19290 					 * we may want to check device
19291 					 * identity.
19292 					 * Identity check is not supported now.
19293 					 *
19294 					 * Link established event
19295 					 * triggers device reset event.
19296 					 */
19297 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19298 					    satadrv_event_flags |=
19299 					    SATA_EVNT_DEVICE_RESET;
19300 				}
19301 			} else if (pmportinfo->pmport_dev_type ==
19302 			    SATA_DTYPE_NONE) {
19303 				/*
19304 				 * We got new device attached! If HBA does not
19305 				 * generate device attached events, trigger it
19306 				 * here.
19307 				 */
19308 				if (!(SATA_FEATURES(sata_hba_inst) &
19309 				    SATA_CTLF_HOTPLUG)) {
19310 					pmportinfo->pmport_event_flags |=
19311 					    SATA_EVNT_DEVICE_ATTACHED;
19312 				}
19313 			}
19314 			/* Reset link lost timeout */
19315 			pmportinfo->pmport_link_lost_time = 0;
19316 		}
19317 	}
19318 linklost:
19319 	if (event_flags & SATA_EVNT_LINK_LOST) {
19320 #ifdef SATA_DEBUG
19321 		if (pmportinfo->pmport_link_lost_time == 0) {
19322 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19323 			    "Processing port %d:%d link lost event",
19324 			    saddr->cport, saddr->pmport);
19325 		}
19326 #endif
19327 		if ((sata_device.satadev_scr.sstatus &
19328 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19329 			/* Ignore event */
19330 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19331 			    "Ignoring port %d:%d link lost event - link is up",
19332 			    saddr->cport, saddr->pmport);
19333 			goto done;
19334 		}
19335 		/*
19336 		 * When HBA cannot generate device attached/detached events,
19337 		 * we need to track link lost time and eventually generate
19338 		 * device detach event.
19339 		 */
19340 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19341 			/* We are tracking link lost time */
19342 			if (pmportinfo->pmport_link_lost_time == 0) {
19343 				/* save current time (lbolt value) */
19344 				pmportinfo->pmport_link_lost_time =
19345 				    ddi_get_lbolt();
19346 				/* just keep link lost event */
19347 				pmportinfo->pmport_event_flags |=
19348 				    SATA_EVNT_LINK_LOST;
19349 			} else {
19350 				clock_t cur_time = ddi_get_lbolt();
19351 				if ((cur_time -
19352 				    pmportinfo->pmport_link_lost_time) >=
19353 				    drv_usectohz(
19354 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19355 					/* trigger device detach event */
19356 					pmportinfo->pmport_event_flags |=
19357 					    SATA_EVNT_DEVICE_DETACHED;
19358 					pmportinfo->pmport_link_lost_time = 0;
19359 					SATADBG2(SATA_DBG_EVENTS,
19360 					    sata_hba_inst,
19361 					    "Triggering port %d:%d "
19362 					    "device detached event",
19363 					    saddr->cport, saddr->pmport);
19364 				} else {
19365 					/* keep link lost event */
19366 					pmportinfo->pmport_event_flags |=
19367 					    SATA_EVNT_LINK_LOST;
19368 				}
19369 			}
19370 		}
19371 		/*
19372 		 * We could change port state to disable/delay access to
19373 		 * the attached device until the link is recovered.
19374 		 */
19375 	}
19376 done:
19377 	event_flags = pmportinfo->pmport_event_flags;
19378 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19379 	    saddr->pmport));
19380 	if (event_flags != 0) {
19381 		mutex_enter(&sata_hba_inst->satahba_mutex);
19382 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19383 		mutex_exit(&sata_hba_inst->satahba_mutex);
19384 		mutex_enter(&sata_mutex);
19385 		sata_event_pending |= SATA_EVNT_MAIN;
19386 		mutex_exit(&sata_mutex);
19387 	}
19388 }
19389 
19390 /*
19391  * Device Detached Event processing.
19392  * Port is probed to find if a device is really gone. If so,
19393  * the device info structure is detached from the SATA port info structure
19394  * and released.
19395  * Port status is updated.
19396  *
19397  * NOTE: Port multiplier ports events are handled by
19398  * sata_process_pmdevice_detached()
19399  */
19400 static void
19401 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19402     sata_address_t *saddr)
19403 {
19404 	sata_cport_info_t *cportinfo;
19405 	sata_pmport_info_t *pmportinfo;
19406 	sata_drive_info_t *sdevinfo;
19407 	sata_device_t sata_device;
19408 	sata_address_t pmport_addr;
19409 	char name[16];
19410 	uint8_t cport = saddr->cport;
19411 	int npmport;
19412 	int rval;
19413 
19414 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19415 	    "Processing port %d device detached", saddr->cport);
19416 
19417 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19418 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19419 	/* Clear event flag */
19420 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19421 
19422 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19423 	if ((cportinfo->cport_state &
19424 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19425 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19426 		    cport_mutex);
19427 		return;
19428 	}
19429 	/* For sanity, re-probe the port */
19430 	sata_device.satadev_rev = SATA_DEVICE_REV;
19431 	sata_device.satadev_addr = *saddr;
19432 
19433 	/*
19434 	 * We have to exit mutex, because the HBA probe port function may
19435 	 * block on its own mutex.
19436 	 */
19437 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19438 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19439 	    (SATA_DIP(sata_hba_inst), &sata_device);
19440 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19441 	sata_update_port_info(sata_hba_inst, &sata_device);
19442 	if (rval != SATA_SUCCESS) {
19443 		/* Something went wrong? Fail the port */
19444 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19445 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19446 		    cport_mutex);
19447 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19448 		    "SATA port %d probing failed",
19449 		    saddr->cport));
19450 		/*
19451 		 * We may want to release device info structure, but
19452 		 * it is not necessary.
19453 		 */
19454 		return;
19455 	} else {
19456 		/* port probed successfully */
19457 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19458 	}
19459 	/*
19460 	 * Check if a device is still attached. For sanity, check also
19461 	 * link status - if no link, there is no device.
19462 	 */
19463 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19464 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19465 	    SATA_DTYPE_NONE) {
19466 		/*
19467 		 * Device is still attached - ignore detach event.
19468 		 */
19469 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19470 		    cport_mutex);
19471 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19472 		    "Ignoring detach - device still attached to port %d",
19473 		    sata_device.satadev_addr.cport);
19474 		return;
19475 	}
19476 	/*
19477 	 * We need to detach and release device info structure here
19478 	 */
19479 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19480 		/*
19481 		 * A port-multiplier is removed.
19482 		 *
19483 		 * Calling sata_process_pmdevice_detached() does not work
19484 		 * here. The port multiplier is gone, so we cannot probe
19485 		 * sub-port any more and all pmult-related data structure must
19486 		 * be de-allocated immediately. Following structure of every
19487 		 * implemented sub-port behind the pmult are required to
19488 		 * released.
19489 		 *
19490 		 *   - attachment point
19491 		 *   - target node
19492 		 *   - sata_drive_info
19493 		 *   - sata_pmport_info
19494 		 */
19495 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19496 		    cport); npmport ++) {
19497 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19498 			    sata_hba_inst,
19499 			    "Detaching target node at port %d:%d",
19500 			    cport, npmport);
19501 
19502 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19503 
19504 			/* Remove attachment point. */
19505 			name[0] = '\0';
19506 			(void) sprintf(name, "%d.%d", cport, npmport);
19507 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19508 			sata_log(sata_hba_inst, CE_NOTE,
19509 			    "Remove attachment point of port %d:%d",
19510 			    cport, npmport);
19511 
19512 			/* Remove target node */
19513 			pmport_addr.cport = cport;
19514 			pmport_addr.pmport = (uint8_t)npmport;
19515 			pmport_addr.qual = SATA_ADDR_PMPORT;
19516 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19517 
19518 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19519 
19520 			/* Release sata_pmport_info & sata_drive_info. */
19521 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19522 			    cport, npmport);
19523 			ASSERT(pmportinfo != NULL);
19524 
19525 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19526 			if (sdevinfo != NULL) {
19527 				(void) kmem_free((void *) sdevinfo,
19528 				    sizeof (sata_drive_info_t));
19529 			}
19530 
19531 			/* Release sata_pmport_info at last */
19532 			(void) kmem_free((void *) pmportinfo,
19533 			    sizeof (sata_pmport_info_t));
19534 		}
19535 
19536 		/* Finally, release sata_pmult_info */
19537 		(void) kmem_free((void *)
19538 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19539 		    sizeof (sata_pmult_info_t));
19540 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19541 
19542 		sata_log(sata_hba_inst, CE_WARN,
19543 		    "SATA port-multiplier detached at port %d", cport);
19544 
19545 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19546 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19547 		    saddr->cport)->cport_mutex);
19548 	} else {
19549 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19550 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19551 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19552 			(void) kmem_free((void *)sdevinfo,
19553 			    sizeof (sata_drive_info_t));
19554 		}
19555 		sata_log(sata_hba_inst, CE_WARN,
19556 		    "SATA device detached at port %d", cport);
19557 
19558 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19559 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19560 		    saddr->cport)->cport_mutex);
19561 
19562 		/*
19563 		 * Try to offline a device and remove target node
19564 		 * if it still exists
19565 		 */
19566 		sata_remove_target_node(sata_hba_inst, saddr);
19567 	}
19568 
19569 
19570 	/*
19571 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19572 	 * with the hint: SE_HINT_REMOVE
19573 	 */
19574 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19575 }
19576 
19577 /*
19578  * Port Multiplier Port Device Deattached Event processing.
19579  *
19580  * NOTE: No Mutex should be hold.
19581  */
19582 static void
19583 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19584     sata_address_t *saddr)
19585 {
19586 	sata_pmport_info_t *pmportinfo;
19587 	sata_drive_info_t *sdevinfo;
19588 	sata_device_t sata_device;
19589 	int rval;
19590 	uint8_t cport, pmport;
19591 
19592 	cport = saddr->cport;
19593 	pmport = saddr->pmport;
19594 
19595 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19596 	    "Processing port %d:%d device detached",
19597 	    cport, pmport);
19598 
19599 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19600 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19601 
19602 	/* Clear event flag */
19603 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19604 
19605 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19606 	if ((pmportinfo->pmport_state &
19607 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19608 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19609 		return;
19610 	}
19611 	/* For sanity, re-probe the port */
19612 	sata_device.satadev_rev = SATA_DEVICE_REV;
19613 	sata_device.satadev_addr = *saddr;
19614 
19615 	/*
19616 	 * We have to exit mutex, because the HBA probe port function may
19617 	 * block on its own mutex.
19618 	 */
19619 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19620 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19621 	    (SATA_DIP(sata_hba_inst), &sata_device);
19622 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19623 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19624 	if (rval != SATA_SUCCESS) {
19625 		/* Something went wrong? Fail the port */
19626 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19627 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19628 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19629 		    "SATA port %d:%d probing failed",
19630 		    saddr->pmport));
19631 		/*
19632 		 * We may want to release device info structure, but
19633 		 * it is not necessary.
19634 		 */
19635 		return;
19636 	} else {
19637 		/* port probed successfully */
19638 		pmportinfo->pmport_state |=
19639 		    SATA_STATE_PROBED | SATA_STATE_READY;
19640 	}
19641 	/*
19642 	 * Check if a device is still attached. For sanity, check also
19643 	 * link status - if no link, there is no device.
19644 	 */
19645 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19646 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19647 	    SATA_DTYPE_NONE) {
19648 		/*
19649 		 * Device is still attached - ignore detach event.
19650 		 */
19651 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19652 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19653 		    "Ignoring detach - device still attached to port %d",
19654 		    sata_device.satadev_addr.pmport);
19655 		return;
19656 	}
19657 	/*
19658 	 * We need to detach and release device info structure here
19659 	 */
19660 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19661 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19662 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19663 		(void) kmem_free((void *)sdevinfo,
19664 		    sizeof (sata_drive_info_t));
19665 	}
19666 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19667 	/*
19668 	 * Device cannot be reached anymore, even if the target node may be
19669 	 * still present.
19670 	 */
19671 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19672 
19673 	/*
19674 	 * Try to offline a device and remove target node if it still exists
19675 	 */
19676 	sata_remove_target_node(sata_hba_inst, saddr);
19677 
19678 	/*
19679 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19680 	 * with the hint: SE_HINT_REMOVE
19681 	 */
19682 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19683 }
19684 
19685 
19686 /*
19687  * Device Attached Event processing.
19688  * Port state is checked to verify that a device is really attached. If so,
19689  * the device info structure is created and attached to the SATA port info
19690  * structure.
19691  *
19692  * If attached device cannot be identified or set-up, the retry for the
19693  * attach processing is set-up. Subsequent daemon run would try again to
19694  * identify the device, until the time limit is reached
19695  * (SATA_DEV_IDENTIFY_TIMEOUT).
19696  *
19697  * This function cannot be called in interrupt context (it may sleep).
19698  *
19699  * NOTE: Port multiplier ports events are handled by
19700  * sata_process_pmdevice_attached()
19701  */
19702 static void
19703 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19704     sata_address_t *saddr)
19705 {
19706 	sata_cport_info_t *cportinfo = NULL;
19707 	sata_drive_info_t *sdevinfo = NULL;
19708 	sata_pmult_info_t *pmultinfo = NULL;
19709 	sata_pmport_info_t *pmportinfo = NULL;
19710 	sata_device_t sata_device;
19711 	dev_info_t *tdip;
19712 	uint32_t event_flags = 0, pmult_event_flags = 0;
19713 	int rval;
19714 	int npmport;
19715 
19716 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19717 	    "Processing port %d device attached", saddr->cport);
19718 
19719 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19720 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19721 
19722 	/* Clear attach event flag first */
19723 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19724 
19725 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19726 	if ((cportinfo->cport_state &
19727 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19728 		cportinfo->cport_dev_attach_time = 0;
19729 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19730 		    cport_mutex);
19731 		return;
19732 	}
19733 
19734 	/*
19735 	 * If the sata_drive_info structure is found attached to the port info,
19736 	 * despite the fact the device was removed and now it is re-attached,
19737 	 * the old drive info structure was not removed.
19738 	 * Arbitrarily release device info structure.
19739 	 */
19740 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19741 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19742 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19743 		(void) kmem_free((void *)sdevinfo,
19744 		    sizeof (sata_drive_info_t));
19745 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19746 		    "Arbitrarily detaching old device info.", NULL);
19747 	}
19748 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19749 
19750 	/* For sanity, re-probe the port */
19751 	sata_device.satadev_rev = SATA_DEVICE_REV;
19752 	sata_device.satadev_addr = *saddr;
19753 
19754 	/*
19755 	 * We have to exit mutex, because the HBA probe port function may
19756 	 * block on its own mutex.
19757 	 */
19758 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19759 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19760 	    (SATA_DIP(sata_hba_inst), &sata_device);
19761 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19762 	sata_update_port_info(sata_hba_inst, &sata_device);
19763 	if (rval != SATA_SUCCESS) {
19764 		/* Something went wrong? Fail the port */
19765 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19766 		cportinfo->cport_dev_attach_time = 0;
19767 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19768 		    cport_mutex);
19769 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19770 		    "SATA port %d probing failed",
19771 		    saddr->cport));
19772 		return;
19773 	} else {
19774 		/* port probed successfully */
19775 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19776 	}
19777 	/*
19778 	 * Check if a device is still attached. For sanity, check also
19779 	 * link status - if no link, there is no device.
19780 	 */
19781 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19782 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19783 	    SATA_DTYPE_NONE) {
19784 		/*
19785 		 * No device - ignore attach event.
19786 		 */
19787 		cportinfo->cport_dev_attach_time = 0;
19788 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19789 		    cport_mutex);
19790 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19791 		    "Ignoring attach - no device connected to port %d",
19792 		    sata_device.satadev_addr.cport);
19793 		return;
19794 	}
19795 
19796 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19797 	/*
19798 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19799 	 * with the hint: SE_HINT_INSERT
19800 	 */
19801 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19802 
19803 	/*
19804 	 * Port reprobing will take care of the creation of the device
19805 	 * info structure and determination of the device type.
19806 	 */
19807 	sata_device.satadev_addr = *saddr;
19808 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19809 	    SATA_DEV_IDENTIFY_NORETRY);
19810 
19811 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19812 	    cport_mutex);
19813 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19814 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19815 		/* Some device is attached to the port */
19816 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19817 			/*
19818 			 * A device was not successfully attached.
19819 			 * Track retry time for device identification.
19820 			 */
19821 			if (cportinfo->cport_dev_attach_time != 0) {
19822 				clock_t cur_time = ddi_get_lbolt();
19823 				/*
19824 				 * If the retry time limit was not exceeded,
19825 				 * reinstate attach event.
19826 				 */
19827 				if ((cur_time -
19828 				    cportinfo->cport_dev_attach_time) <
19829 				    drv_usectohz(
19830 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19831 					/* OK, restore attach event */
19832 					cportinfo->cport_event_flags |=
19833 					    SATA_EVNT_DEVICE_ATTACHED;
19834 				} else {
19835 					/* Timeout - cannot identify device */
19836 					cportinfo->cport_dev_attach_time = 0;
19837 					sata_log(sata_hba_inst,
19838 					    CE_WARN,
19839 					    "Could not identify SATA device "
19840 					    "at port %d",
19841 					    saddr->cport);
19842 				}
19843 			} else {
19844 				/*
19845 				 * Start tracking time for device
19846 				 * identification.
19847 				 * Save current time (lbolt value).
19848 				 */
19849 				cportinfo->cport_dev_attach_time =
19850 				    ddi_get_lbolt();
19851 				/* Restore attach event */
19852 				cportinfo->cport_event_flags |=
19853 				    SATA_EVNT_DEVICE_ATTACHED;
19854 			}
19855 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19856 			cportinfo->cport_dev_attach_time = 0;
19857 			sata_log(sata_hba_inst, CE_NOTE,
19858 			    "SATA port-multiplier detected at port %d",
19859 			    saddr->cport);
19860 
19861 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19862 				/* Log the info of new port multiplier */
19863 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19864 				    saddr->cport)->cport_mutex);
19865 				sata_show_pmult_info(sata_hba_inst,
19866 				    &sata_device);
19867 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19868 				    saddr->cport)->cport_mutex);
19869 			}
19870 
19871 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19872 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19873 			for (npmport = 0; npmport <
19874 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19875 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19876 				    saddr->cport, npmport);
19877 				ASSERT(pmportinfo != NULL);
19878 
19879 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19880 				    saddr->cport)->cport_mutex);
19881 				mutex_enter(&pmportinfo->pmport_mutex);
19882 				/* Marked all pmports with link events. */
19883 				pmportinfo->pmport_event_flags =
19884 				    SATA_EVNT_LINK_ESTABLISHED;
19885 				pmult_event_flags |=
19886 				    pmportinfo->pmport_event_flags;
19887 				mutex_exit(&pmportinfo->pmport_mutex);
19888 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19889 				    saddr->cport)->cport_mutex);
19890 			}
19891 			/* Auto-online is not available for PMult now. */
19892 
19893 		} else {
19894 			/*
19895 			 * If device was successfully attached, the subsequent
19896 			 * action depends on a state of the
19897 			 * sata_auto_online variable. If it is set to zero.
19898 			 * an explicit 'configure' command will be needed to
19899 			 * configure it. If its value is non-zero, we will
19900 			 * attempt to online (configure) the device.
19901 			 * First, log the message indicating that a device
19902 			 * was attached.
19903 			 */
19904 			cportinfo->cport_dev_attach_time = 0;
19905 			sata_log(sata_hba_inst, CE_WARN,
19906 			    "SATA device detected at port %d", saddr->cport);
19907 
19908 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19909 				sata_drive_info_t new_sdinfo;
19910 
19911 				/* Log device info data */
19912 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19913 				    cportinfo));
19914 				sata_show_drive_info(sata_hba_inst,
19915 				    &new_sdinfo);
19916 			}
19917 
19918 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19919 			    saddr->cport)->cport_mutex);
19920 
19921 			/*
19922 			 * Make sure that there is no target node for that
19923 			 * device. If so, release it. It should not happen,
19924 			 * unless we had problem removing the node when
19925 			 * device was detached.
19926 			 */
19927 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19928 			    saddr->cport, saddr->pmport);
19929 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19930 			    saddr->cport)->cport_mutex);
19931 			if (tdip != NULL) {
19932 
19933 #ifdef SATA_DEBUG
19934 				if ((cportinfo->cport_event_flags &
19935 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19936 					sata_log(sata_hba_inst, CE_WARN,
19937 					    "sata_process_device_attached: "
19938 					    "old device target node exists!");
19939 #endif
19940 				/*
19941 				 * target node exists - try to unconfigure
19942 				 * device and remove the node.
19943 				 */
19944 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19945 				    saddr->cport)->cport_mutex);
19946 				rval = ndi_devi_offline(tdip,
19947 				    NDI_DEVI_REMOVE);
19948 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19949 				    saddr->cport)->cport_mutex);
19950 
19951 				if (rval == NDI_SUCCESS) {
19952 					cportinfo->cport_event_flags &=
19953 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19954 					cportinfo->cport_tgtnode_clean = B_TRUE;
19955 				} else {
19956 					/*
19957 					 * PROBLEM - the target node remained
19958 					 * and it belongs to a previously
19959 					 * attached device.
19960 					 * This happens when the file was open
19961 					 * or the node was waiting for
19962 					 * resources at the time the
19963 					 * associated device was removed.
19964 					 * Instruct event daemon to retry the
19965 					 * cleanup later.
19966 					 */
19967 					sata_log(sata_hba_inst,
19968 					    CE_WARN,
19969 					    "Application(s) accessing "
19970 					    "previously attached SATA "
19971 					    "device have to release "
19972 					    "it before newly inserted "
19973 					    "device can be made accessible.",
19974 					    saddr->cport);
19975 					cportinfo->cport_event_flags |=
19976 					    SATA_EVNT_TARGET_NODE_CLEANUP;
19977 					cportinfo->cport_tgtnode_clean =
19978 					    B_FALSE;
19979 				}
19980 			}
19981 			if (sata_auto_online != 0) {
19982 				cportinfo->cport_event_flags |=
19983 				    SATA_EVNT_AUTOONLINE_DEVICE;
19984 			}
19985 
19986 		}
19987 	} else {
19988 		cportinfo->cport_dev_attach_time = 0;
19989 	}
19990 
19991 	event_flags = cportinfo->cport_event_flags;
19992 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19993 	if (event_flags != 0 || pmult_event_flags != 0) {
19994 		mutex_enter(&sata_hba_inst->satahba_mutex);
19995 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19996 		mutex_exit(&sata_hba_inst->satahba_mutex);
19997 		mutex_enter(&sata_mutex);
19998 		sata_event_pending |= SATA_EVNT_MAIN;
19999 		mutex_exit(&sata_mutex);
20000 	}
20001 }
20002 
20003 /*
20004  * Port Multiplier Port Device Attached Event processing.
20005  *
20006  * NOTE: No Mutex should be hold.
20007  */
20008 static void
20009 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20010     sata_address_t *saddr)
20011 {
20012 	sata_pmport_info_t *pmportinfo;
20013 	sata_drive_info_t *sdinfo;
20014 	sata_device_t sata_device;
20015 	dev_info_t *tdip;
20016 	uint32_t event_flags;
20017 	uint8_t cport = saddr->cport;
20018 	uint8_t pmport = saddr->pmport;
20019 	int rval;
20020 
20021 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20022 	    "Processing port %d:%d device attached", cport, pmport);
20023 
20024 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20025 
20026 	mutex_enter(&pmportinfo->pmport_mutex);
20027 
20028 	/* Clear attach event flag first */
20029 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20030 
20031 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20032 	if ((pmportinfo->pmport_state &
20033 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20034 		pmportinfo->pmport_dev_attach_time = 0;
20035 		mutex_exit(&pmportinfo->pmport_mutex);
20036 		return;
20037 	}
20038 
20039 	/*
20040 	 * If the sata_drive_info structure is found attached to the port info,
20041 	 * despite the fact the device was removed and now it is re-attached,
20042 	 * the old drive info structure was not removed.
20043 	 * Arbitrarily release device info structure.
20044 	 */
20045 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20046 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20047 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20048 		(void) kmem_free((void *)sdinfo,
20049 		    sizeof (sata_drive_info_t));
20050 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20051 		    "Arbitrarily detaching old device info.", NULL);
20052 	}
20053 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20054 
20055 	/* For sanity, re-probe the port */
20056 	sata_device.satadev_rev = SATA_DEVICE_REV;
20057 	sata_device.satadev_addr = *saddr;
20058 
20059 	/*
20060 	 * We have to exit mutex, because the HBA probe port function may
20061 	 * block on its own mutex.
20062 	 */
20063 	mutex_exit(&pmportinfo->pmport_mutex);
20064 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20065 	    (SATA_DIP(sata_hba_inst), &sata_device);
20066 	mutex_enter(&pmportinfo->pmport_mutex);
20067 
20068 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20069 	if (rval != SATA_SUCCESS) {
20070 		/* Something went wrong? Fail the port */
20071 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20072 		pmportinfo->pmport_dev_attach_time = 0;
20073 		mutex_exit(&pmportinfo->pmport_mutex);
20074 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20075 		    "SATA port %d:%d probing failed", cport, pmport));
20076 		return;
20077 	} else {
20078 		/* pmport probed successfully */
20079 		pmportinfo->pmport_state |=
20080 		    SATA_STATE_PROBED | SATA_STATE_READY;
20081 	}
20082 	/*
20083 	 * Check if a device is still attached. For sanity, check also
20084 	 * link status - if no link, there is no device.
20085 	 */
20086 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20087 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20088 	    SATA_DTYPE_NONE) {
20089 		/*
20090 		 * No device - ignore attach event.
20091 		 */
20092 		pmportinfo->pmport_dev_attach_time = 0;
20093 		mutex_exit(&pmportinfo->pmport_mutex);
20094 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20095 		    "Ignoring attach - no device connected to port %d:%d",
20096 		    cport, pmport);
20097 		return;
20098 	}
20099 
20100 	mutex_exit(&pmportinfo->pmport_mutex);
20101 	/*
20102 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20103 	 * with the hint: SE_HINT_INSERT
20104 	 */
20105 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20106 
20107 	/*
20108 	 * Port reprobing will take care of the creation of the device
20109 	 * info structure and determination of the device type.
20110 	 */
20111 	sata_device.satadev_addr = *saddr;
20112 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20113 	    SATA_DEV_IDENTIFY_NORETRY);
20114 
20115 	mutex_enter(&pmportinfo->pmport_mutex);
20116 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20117 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20118 		/* Some device is attached to the port */
20119 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20120 			/*
20121 			 * A device was not successfully attached.
20122 			 * Track retry time for device identification.
20123 			 */
20124 			if (pmportinfo->pmport_dev_attach_time != 0) {
20125 				clock_t cur_time = ddi_get_lbolt();
20126 				/*
20127 				 * If the retry time limit was not exceeded,
20128 				 * reinstate attach event.
20129 				 */
20130 				if ((cur_time -
20131 				    pmportinfo->pmport_dev_attach_time) <
20132 				    drv_usectohz(
20133 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20134 					/* OK, restore attach event */
20135 					pmportinfo->pmport_event_flags |=
20136 					    SATA_EVNT_DEVICE_ATTACHED;
20137 				} else {
20138 					/* Timeout - cannot identify device */
20139 					pmportinfo->pmport_dev_attach_time = 0;
20140 					sata_log(sata_hba_inst, CE_WARN,
20141 					    "Could not identify SATA device "
20142 					    "at port %d:%d",
20143 					    cport, pmport);
20144 				}
20145 			} else {
20146 				/*
20147 				 * Start tracking time for device
20148 				 * identification.
20149 				 * Save current time (lbolt value).
20150 				 */
20151 				pmportinfo->pmport_dev_attach_time =
20152 				    ddi_get_lbolt();
20153 				/* Restore attach event */
20154 				pmportinfo->pmport_event_flags |=
20155 				    SATA_EVNT_DEVICE_ATTACHED;
20156 			}
20157 		} else {
20158 			/*
20159 			 * If device was successfully attached, the subsequent
20160 			 * action depends on a state of the
20161 			 * sata_auto_online variable. If it is set to zero.
20162 			 * an explicit 'configure' command will be needed to
20163 			 * configure it. If its value is non-zero, we will
20164 			 * attempt to online (configure) the device.
20165 			 * First, log the message indicating that a device
20166 			 * was attached.
20167 			 */
20168 			pmportinfo->pmport_dev_attach_time = 0;
20169 			sata_log(sata_hba_inst, CE_WARN,
20170 			    "SATA device detected at port %d:%d",
20171 			    cport, pmport);
20172 
20173 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20174 				sata_drive_info_t new_sdinfo;
20175 
20176 				/* Log device info data */
20177 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20178 				    pmportinfo));
20179 				sata_show_drive_info(sata_hba_inst,
20180 				    &new_sdinfo);
20181 			}
20182 
20183 			mutex_exit(&pmportinfo->pmport_mutex);
20184 
20185 			/*
20186 			 * Make sure that there is no target node for that
20187 			 * device. If so, release it. It should not happen,
20188 			 * unless we had problem removing the node when
20189 			 * device was detached.
20190 			 */
20191 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20192 			    saddr->cport, saddr->pmport);
20193 			mutex_enter(&pmportinfo->pmport_mutex);
20194 			if (tdip != NULL) {
20195 
20196 #ifdef SATA_DEBUG
20197 				if ((pmportinfo->pmport_event_flags &
20198 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20199 					sata_log(sata_hba_inst, CE_WARN,
20200 					    "sata_process_device_attached: "
20201 					    "old device target node exists!");
20202 #endif
20203 				/*
20204 				 * target node exists - try to unconfigure
20205 				 * device and remove the node.
20206 				 */
20207 				mutex_exit(&pmportinfo->pmport_mutex);
20208 				rval = ndi_devi_offline(tdip,
20209 				    NDI_DEVI_REMOVE);
20210 				mutex_enter(&pmportinfo->pmport_mutex);
20211 
20212 				if (rval == NDI_SUCCESS) {
20213 					pmportinfo->pmport_event_flags &=
20214 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20215 					pmportinfo->pmport_tgtnode_clean =
20216 					    B_TRUE;
20217 				} else {
20218 					/*
20219 					 * PROBLEM - the target node remained
20220 					 * and it belongs to a previously
20221 					 * attached device.
20222 					 * This happens when the file was open
20223 					 * or the node was waiting for
20224 					 * resources at the time the
20225 					 * associated device was removed.
20226 					 * Instruct event daemon to retry the
20227 					 * cleanup later.
20228 					 */
20229 					sata_log(sata_hba_inst,
20230 					    CE_WARN,
20231 					    "Application(s) accessing "
20232 					    "previously attached SATA "
20233 					    "device have to release "
20234 					    "it before newly inserted "
20235 					    "device can be made accessible."
20236 					    "at port %d:%d",
20237 					    cport, pmport);
20238 					pmportinfo->pmport_event_flags |=
20239 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20240 					pmportinfo->pmport_tgtnode_clean =
20241 					    B_FALSE;
20242 				}
20243 			}
20244 			if (sata_auto_online != 0) {
20245 				pmportinfo->pmport_event_flags |=
20246 				    SATA_EVNT_AUTOONLINE_DEVICE;
20247 			}
20248 
20249 		}
20250 	} else {
20251 		pmportinfo->pmport_dev_attach_time = 0;
20252 	}
20253 
20254 	event_flags = pmportinfo->pmport_event_flags;
20255 	mutex_exit(&pmportinfo->pmport_mutex);
20256 	if (event_flags != 0) {
20257 		mutex_enter(&sata_hba_inst->satahba_mutex);
20258 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20259 		mutex_exit(&sata_hba_inst->satahba_mutex);
20260 		mutex_enter(&sata_mutex);
20261 		sata_event_pending |= SATA_EVNT_MAIN;
20262 		mutex_exit(&sata_mutex);
20263 	}
20264 
20265 	/* clear the reset_in_progress events */
20266 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20267 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20268 			/* must clear flags on cport */
20269 			sata_pmult_info_t *pminfo =
20270 			    SATA_PMULT_INFO(sata_hba_inst,
20271 			    saddr->cport);
20272 			pminfo->pmult_event_flags |=
20273 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20274 		}
20275 	}
20276 }
20277 
20278 /*
20279  * Device Target Node Cleanup Event processing.
20280  * If the target node associated with a sata port device is in
20281  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20282  * If the target node cannot be removed, the event flag is left intact,
20283  * so that event daemon may re-run this function later.
20284  *
20285  * This function cannot be called in interrupt context (it may sleep).
20286  *
20287  * NOTE: Processes cport events only, not port multiplier ports.
20288  */
20289 static void
20290 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20291     sata_address_t *saddr)
20292 {
20293 	sata_cport_info_t *cportinfo;
20294 	dev_info_t *tdip;
20295 
20296 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20297 	    "Processing port %d device target node cleanup", saddr->cport);
20298 
20299 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20300 
20301 	/*
20302 	 * Check if there is target node for that device and it is in the
20303 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20304 	 */
20305 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20306 	    saddr->pmport);
20307 	if (tdip != NULL) {
20308 		/*
20309 		 * target node exists - check if it is target node of
20310 		 * a removed device.
20311 		 */
20312 		if (sata_check_device_removed(tdip) == B_TRUE) {
20313 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20314 			    "sata_process_target_node_cleanup: "
20315 			    "old device target node exists!", NULL);
20316 			/*
20317 			 * Unconfigure and remove the target node
20318 			 */
20319 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20320 			    NDI_SUCCESS) {
20321 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20322 				    saddr->cport)->cport_mutex);
20323 				cportinfo->cport_event_flags &=
20324 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20325 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20326 				    saddr->cport)->cport_mutex);
20327 				return;
20328 			}
20329 			/*
20330 			 * Event daemon will retry the cleanup later.
20331 			 */
20332 			mutex_enter(&sata_hba_inst->satahba_mutex);
20333 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20334 			mutex_exit(&sata_hba_inst->satahba_mutex);
20335 			mutex_enter(&sata_mutex);
20336 			sata_event_pending |= SATA_EVNT_MAIN;
20337 			mutex_exit(&sata_mutex);
20338 		}
20339 	} else {
20340 		if (saddr->qual == SATA_ADDR_CPORT ||
20341 		    saddr->qual == SATA_ADDR_DCPORT) {
20342 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20343 			    saddr->cport)->cport_mutex);
20344 			cportinfo->cport_event_flags &=
20345 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20346 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20347 			    saddr->cport)->cport_mutex);
20348 		} else {
20349 			/* sanity check */
20350 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20351 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20352 			    saddr->cport) == NULL)
20353 				return;
20354 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20355 			    saddr->pmport) == NULL)
20356 				return;
20357 
20358 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20359 			    saddr->cport, saddr->pmport)->pmport_mutex);
20360 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20361 			    saddr->pmport)->pmport_event_flags &=
20362 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20363 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20364 			    saddr->cport, saddr->pmport)->pmport_mutex);
20365 		}
20366 	}
20367 }
20368 
20369 /*
20370  * Device AutoOnline Event processing.
20371  * If attached device is to be onlined, an attempt is made to online this
20372  * device, but only if there is no lingering (old) target node present.
20373  * If the device cannot be onlined, the event flag is left intact,
20374  * so that event daemon may re-run this function later.
20375  *
20376  * This function cannot be called in interrupt context (it may sleep).
20377  *
20378  * NOTE: Processes cport events only, not port multiplier ports.
20379  */
20380 static void
20381 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20382     sata_address_t *saddr)
20383 {
20384 	sata_cport_info_t *cportinfo;
20385 	sata_drive_info_t *sdinfo;
20386 	sata_device_t sata_device;
20387 	dev_info_t *tdip;
20388 
20389 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20390 	    "Processing port %d attached device auto-onlining", saddr->cport);
20391 
20392 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20393 
20394 	/*
20395 	 * Check if device is present and recognized. If not, reset event.
20396 	 */
20397 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20398 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20399 		/* Nothing to online */
20400 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20401 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20402 		    saddr->cport)->cport_mutex);
20403 		return;
20404 	}
20405 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20406 
20407 	/*
20408 	 * Check if there is target node for this device and if it is in the
20409 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20410 	 * the event for later processing.
20411 	 */
20412 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20413 	    saddr->pmport);
20414 	if (tdip != NULL) {
20415 		/*
20416 		 * target node exists - check if it is target node of
20417 		 * a removed device.
20418 		 */
20419 		if (sata_check_device_removed(tdip) == B_TRUE) {
20420 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20421 			    "sata_process_device_autoonline: "
20422 			    "old device target node exists!", NULL);
20423 			/*
20424 			 * Event daemon will retry device onlining later.
20425 			 */
20426 			mutex_enter(&sata_hba_inst->satahba_mutex);
20427 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20428 			mutex_exit(&sata_hba_inst->satahba_mutex);
20429 			mutex_enter(&sata_mutex);
20430 			sata_event_pending |= SATA_EVNT_MAIN;
20431 			mutex_exit(&sata_mutex);
20432 			return;
20433 		}
20434 		/*
20435 		 * If the target node is not in the 'removed" state, assume
20436 		 * that it belongs to this device. There is nothing more to do,
20437 		 * but reset the event.
20438 		 */
20439 	} else {
20440 
20441 		/*
20442 		 * Try to online the device
20443 		 * If there is any reset-related event, remove it. We are
20444 		 * configuring the device and no state restoring is needed.
20445 		 */
20446 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20447 		    saddr->cport)->cport_mutex);
20448 		sata_device.satadev_addr = *saddr;
20449 		if (saddr->qual == SATA_ADDR_CPORT)
20450 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20451 		else
20452 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20453 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20454 		if (sdinfo != NULL) {
20455 			if (sdinfo->satadrv_event_flags &
20456 			    (SATA_EVNT_DEVICE_RESET |
20457 			    SATA_EVNT_INPROC_DEVICE_RESET))
20458 				sdinfo->satadrv_event_flags = 0;
20459 			sdinfo->satadrv_event_flags |=
20460 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20461 
20462 			/* Need to create a new target node. */
20463 			cportinfo->cport_tgtnode_clean = B_TRUE;
20464 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20465 			    saddr->cport)->cport_mutex);
20466 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20467 			    sata_hba_inst, &sata_device.satadev_addr);
20468 			if (tdip == NULL) {
20469 				/*
20470 				 * Configure (onlining) failed.
20471 				 * We will NOT retry
20472 				 */
20473 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20474 				    "sata_process_device_autoonline: "
20475 				    "configuring SATA device at port %d failed",
20476 				    saddr->cport));
20477 			}
20478 		} else {
20479 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20480 			    saddr->cport)->cport_mutex);
20481 		}
20482 
20483 	}
20484 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20485 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20486 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20487 	    saddr->cport)->cport_mutex);
20488 }
20489 
20490 
20491 static void
20492 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20493     int hint)
20494 {
20495 	char ap[MAXPATHLEN];
20496 	nvlist_t *ev_attr_list = NULL;
20497 	int err;
20498 
20499 	/* Allocate and build sysevent attribute list */
20500 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20501 	if (err != 0) {
20502 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20503 		    "sata_gen_sysevent: "
20504 		    "cannot allocate memory for sysevent attributes\n"));
20505 		return;
20506 	}
20507 	/* Add hint attribute */
20508 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20509 	if (err != 0) {
20510 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20511 		    "sata_gen_sysevent: "
20512 		    "failed to add DR_HINT attr for sysevent"));
20513 		nvlist_free(ev_attr_list);
20514 		return;
20515 	}
20516 	/*
20517 	 * Add AP attribute.
20518 	 * Get controller pathname and convert it into AP pathname by adding
20519 	 * a target number.
20520 	 */
20521 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20522 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20523 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20524 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20525 
20526 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20527 	if (err != 0) {
20528 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20529 		    "sata_gen_sysevent: "
20530 		    "failed to add DR_AP_ID attr for sysevent"));
20531 		nvlist_free(ev_attr_list);
20532 		return;
20533 	}
20534 
20535 	/* Generate/log sysevent */
20536 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20537 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20538 	if (err != DDI_SUCCESS) {
20539 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20540 		    "sata_gen_sysevent: "
20541 		    "cannot log sysevent, err code %x\n", err));
20542 	}
20543 
20544 	nvlist_free(ev_attr_list);
20545 }
20546 
20547 
20548 
20549 
20550 /*
20551  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20552  */
20553 static void
20554 sata_set_device_removed(dev_info_t *tdip)
20555 {
20556 	int circ;
20557 
20558 	ASSERT(tdip != NULL);
20559 
20560 	ndi_devi_enter(tdip, &circ);
20561 	mutex_enter(&DEVI(tdip)->devi_lock);
20562 	DEVI_SET_DEVICE_REMOVED(tdip);
20563 	mutex_exit(&DEVI(tdip)->devi_lock);
20564 	ndi_devi_exit(tdip, circ);
20565 }
20566 
20567 
20568 /*
20569  * Set internal event instructing event daemon to try
20570  * to perform the target node cleanup.
20571  */
20572 static void
20573 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20574     sata_address_t *saddr)
20575 {
20576 	if (saddr->qual == SATA_ADDR_CPORT ||
20577 	    saddr->qual == SATA_ADDR_DCPORT) {
20578 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20579 		    saddr->cport)->cport_mutex);
20580 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20581 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20582 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20583 		    cport_tgtnode_clean = B_FALSE;
20584 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20585 		    saddr->cport)->cport_mutex);
20586 	} else {
20587 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20588 		    saddr->cport, saddr->pmport)->pmport_mutex);
20589 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20590 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20591 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20592 		    pmport_tgtnode_clean = B_FALSE;
20593 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20594 		    saddr->cport, saddr->pmport)->pmport_mutex);
20595 	}
20596 	mutex_enter(&sata_hba_inst->satahba_mutex);
20597 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20598 	mutex_exit(&sata_hba_inst->satahba_mutex);
20599 	mutex_enter(&sata_mutex);
20600 	sata_event_pending |= SATA_EVNT_MAIN;
20601 	mutex_exit(&sata_mutex);
20602 }
20603 
20604 
20605 /*
20606  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20607  * i.e. check if the target node state indicates that it belongs to a removed
20608  * device.
20609  *
20610  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20611  * B_FALSE otherwise.
20612  */
20613 static boolean_t
20614 sata_check_device_removed(dev_info_t *tdip)
20615 {
20616 	ASSERT(tdip != NULL);
20617 
20618 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20619 		return (B_TRUE);
20620 	else
20621 		return (B_FALSE);
20622 }
20623 
20624 
20625 /*
20626  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20627  */
20628 static boolean_t
20629 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20630 {
20631 	int fm_capability = ddi_fm_capable(dip);
20632 	ddi_fm_error_t de;
20633 
20634 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20635 		if (spx->txlt_buf_dma_handle != NULL) {
20636 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20637 			    DDI_FME_VERSION);
20638 			if (de.fme_status != DDI_SUCCESS)
20639 				return (B_TRUE);
20640 		}
20641 	}
20642 	return (B_FALSE);
20643 }
20644 
20645 
20646 /* ************************ FAULT INJECTTION **************************** */
20647 
20648 #ifdef SATA_INJECT_FAULTS
20649 
20650 static	uint32_t sata_fault_count = 0;
20651 static	uint32_t sata_fault_suspend_count = 0;
20652 
20653 /*
20654  * Inject sata pkt fault
20655  * It modifies returned values of the sata packet.
20656  * It returns immediately if:
20657  * pkt fault injection is not enabled (via sata_inject_fault,
20658  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20659  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20660  * pkt is not directed to specified fault controller/device
20661  * (sata_fault_ctrl_dev and sata_fault_device).
20662  * If fault controller is not specified, fault injection applies to all
20663  * controllers and devices.
20664  *
20665  * First argument is the pointer to the executed sata packet.
20666  * Second argument is a pointer to a value returned by the HBA tran_start
20667  * function.
20668  * Third argument specifies injected error. Injected sata packet faults
20669  * are the satapkt_reason values.
20670  * SATA_PKT_BUSY		-1	Not completed, busy
20671  * SATA_PKT_DEV_ERROR		1	Device reported error
20672  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20673  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20674  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20675  * SATA_PKT_ABORTED		5	Aborted by request
20676  * SATA_PKT_TIMEOUT		6	Operation timeut
20677  * SATA_PKT_RESET		7	Aborted by reset request
20678  *
20679  * Additional global variables affecting the execution:
20680  *
20681  * sata_inject_fault_count variable specifies number of times in row the
20682  * error is injected. Value of -1 specifies permanent fault, ie. every time
20683  * the fault injection point is reached, the fault is injected and a pause
20684  * between fault injection specified by sata_inject_fault_pause_count is
20685  * ignored). Fault injection routine decrements sata_inject_fault_count
20686  * (if greater than zero) until it reaches 0. No fault is injected when
20687  * sata_inject_fault_count is 0 (zero).
20688  *
20689  * sata_inject_fault_pause_count variable specifies number of times a fault
20690  * injection is bypassed (pause between fault injections).
20691  * If set to 0, a fault is injected only a number of times specified by
20692  * sata_inject_fault_count.
20693  *
20694  * The fault counts are static, so for periodic errors they have to be manually
20695  * reset to start repetition sequence from scratch.
20696  * If the original value returned by the HBA tran_start function is not
20697  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20698  * is injected (to avoid masking real problems);
20699  *
20700  * NOTE: In its current incarnation, this function should be invoked only for
20701  * commands executed in SYNCHRONOUS mode.
20702  */
20703 
20704 
20705 static void
20706 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20707 {
20708 
20709 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20710 		return;
20711 
20712 	if (sata_inject_fault_count == 0)
20713 		return;
20714 
20715 	if (fault == 0)
20716 		return;
20717 
20718 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20719 		return;
20720 
20721 	if (sata_fault_ctrl != NULL) {
20722 		sata_pkt_txlate_t *spx =
20723 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20724 
20725 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20726 		    spx->txlt_sata_hba_inst->satahba_dip)
20727 			return;
20728 
20729 		if (sata_fault_device.satadev_addr.cport !=
20730 		    spkt->satapkt_device.satadev_addr.cport ||
20731 		    sata_fault_device.satadev_addr.pmport !=
20732 		    spkt->satapkt_device.satadev_addr.pmport ||
20733 		    sata_fault_device.satadev_addr.qual !=
20734 		    spkt->satapkt_device.satadev_addr.qual)
20735 			return;
20736 	}
20737 
20738 	/* Modify pkt return parameters */
20739 	if (*rval != SATA_TRAN_ACCEPTED ||
20740 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20741 		sata_fault_count = 0;
20742 		sata_fault_suspend_count = 0;
20743 		return;
20744 	}
20745 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20746 		/* Pause in the injection */
20747 		sata_fault_suspend_count -= 1;
20748 		return;
20749 	}
20750 
20751 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20752 		/*
20753 		 * Init inject fault cycle. If fault count is set to -1,
20754 		 * it is a permanent fault.
20755 		 */
20756 		if (sata_inject_fault_count != -1) {
20757 			sata_fault_count = sata_inject_fault_count;
20758 			sata_fault_suspend_count =
20759 			    sata_inject_fault_pause_count;
20760 			if (sata_fault_suspend_count == 0)
20761 				sata_inject_fault_count = 0;
20762 		}
20763 	}
20764 
20765 	if (sata_fault_count != 0)
20766 		sata_fault_count -= 1;
20767 
20768 	switch (fault) {
20769 	case SATA_PKT_BUSY:
20770 		*rval = SATA_TRAN_BUSY;
20771 		spkt->satapkt_reason = SATA_PKT_BUSY;
20772 		break;
20773 
20774 	case SATA_PKT_QUEUE_FULL:
20775 		*rval = SATA_TRAN_QUEUE_FULL;
20776 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20777 		break;
20778 
20779 	case SATA_PKT_CMD_UNSUPPORTED:
20780 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20781 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20782 		break;
20783 
20784 	case SATA_PKT_PORT_ERROR:
20785 		/* This is "rejected" command */
20786 		*rval = SATA_TRAN_PORT_ERROR;
20787 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20788 		/* Additional error setup could be done here - port state */
20789 		break;
20790 
20791 	case SATA_PKT_DEV_ERROR:
20792 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20793 		/*
20794 		 * Additional error setup could be done here
20795 		 */
20796 		break;
20797 
20798 	case SATA_PKT_ABORTED:
20799 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20800 		break;
20801 
20802 	case SATA_PKT_TIMEOUT:
20803 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20804 		/* Additional error setup could be done here */
20805 		break;
20806 
20807 	case SATA_PKT_RESET:
20808 		spkt->satapkt_reason = SATA_PKT_RESET;
20809 		/*
20810 		 * Additional error setup could be done here - device reset
20811 		 */
20812 		break;
20813 
20814 	default:
20815 		break;
20816 	}
20817 }
20818 
20819 #endif
20820 
20821 /*
20822  * SATA Trace Ring Buffer
20823  * ----------------------
20824  *
20825  * Overview
20826  *
20827  * The SATA trace ring buffer is a ring buffer created and managed by
20828  * the SATA framework module that can be used by any module or driver
20829  * within the SATA framework to store debug messages.
20830  *
20831  * Ring Buffer Interfaces:
20832  *
20833  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20834  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20835  *
20836  *	Note that the sata_trace_debug() interface was created to give
20837  *	consumers the flexibilty of sending debug messages to ring buffer
20838  *	as variable arguments.  Consumers can send type va_list debug
20839  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20840  *	and sata_vtrace_debug() relationship is similar to that of
20841  *	cmn_err(9F) and vcmn_err(9F).
20842  *
20843  * Below is a diagram of the SATA trace ring buffer interfaces and
20844  * sample consumers:
20845  *
20846  * +---------------------------------+
20847  * |    o  o  SATA Framework Module  |
20848  * | o  SATA  o     +------------------+      +------------------+
20849  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20850  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20851  * | o        o     +------------------+   |  +------------------+
20852  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20853  * |                        |        |        +------------------+
20854  * |           +------------------+  |
20855  * |           |SATA Debug Message|  |
20856  * |           +------------------+  |
20857  * +---------------------------------+
20858  *
20859  * Supporting Routines:
20860  *
20861  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20862  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20863  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20864  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20865  *
20866  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20867  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20868  * /etc/system to desired size in unit of bytes.
20869  *
20870  * The individual debug message size in the ring buffer is restricted
20871  * to DMSG_BUF_SIZE.
20872  */
20873 void
20874 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20875 {
20876 	sata_trace_dmsg_t *dmsg;
20877 
20878 	if (sata_debug_rbuf == NULL) {
20879 		return;
20880 	}
20881 
20882 	/*
20883 	 * If max size of ring buffer is smaller than size
20884 	 * required for one debug message then just return
20885 	 * since we have no room for the debug message.
20886 	 */
20887 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20888 		return;
20889 	}
20890 
20891 	mutex_enter(&sata_debug_rbuf->lock);
20892 
20893 	/* alloc or reuse on ring buffer */
20894 	dmsg = sata_trace_dmsg_alloc();
20895 
20896 	if (dmsg == NULL) {
20897 		/* resource allocation failed */
20898 		mutex_exit(&sata_debug_rbuf->lock);
20899 		return;
20900 	}
20901 
20902 	dmsg->dip = dip;
20903 	gethrestime(&dmsg->timestamp);
20904 
20905 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20906 
20907 	mutex_exit(&sata_debug_rbuf->lock);
20908 }
20909 
20910 void
20911 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20912 {
20913 	va_list ap;
20914 
20915 	va_start(ap, fmt);
20916 	sata_vtrace_debug(dip, fmt, ap);
20917 	va_end(ap);
20918 }
20919 
20920 /*
20921  * This routine is used to manage debug messages
20922  * on ring buffer.
20923  */
20924 static sata_trace_dmsg_t *
20925 sata_trace_dmsg_alloc(void)
20926 {
20927 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20928 
20929 	if (sata_debug_rbuf->looped == TRUE) {
20930 		sata_debug_rbuf->dmsgp = dmsg->next;
20931 		return (sata_debug_rbuf->dmsgp);
20932 	}
20933 
20934 	/*
20935 	 * If we're looping for the first time,
20936 	 * connect the ring.
20937 	 */
20938 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
20939 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
20940 		dmsg->next = sata_debug_rbuf->dmsgh;
20941 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
20942 		sata_debug_rbuf->looped = TRUE;
20943 		return (sata_debug_rbuf->dmsgp);
20944 	}
20945 
20946 	/* If we've gotten this far then memory allocation is needed */
20947 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
20948 	if (dmsg_alloc == NULL) {
20949 		sata_debug_rbuf->allocfailed++;
20950 		return (dmsg_alloc);
20951 	} else {
20952 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
20953 	}
20954 
20955 	if (sata_debug_rbuf->dmsgp != NULL) {
20956 		dmsg->next = dmsg_alloc;
20957 		sata_debug_rbuf->dmsgp = dmsg->next;
20958 		return (sata_debug_rbuf->dmsgp);
20959 	} else {
20960 		/*
20961 		 * We should only be here if we're initializing
20962 		 * the ring buffer.
20963 		 */
20964 		if (sata_debug_rbuf->dmsgh == NULL) {
20965 			sata_debug_rbuf->dmsgh = dmsg_alloc;
20966 		} else {
20967 			/* Something is wrong */
20968 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
20969 			return (NULL);
20970 		}
20971 
20972 		sata_debug_rbuf->dmsgp = dmsg_alloc;
20973 		return (sata_debug_rbuf->dmsgp);
20974 	}
20975 }
20976 
20977 
20978 /*
20979  * Free all messages on debug ring buffer.
20980  */
20981 static void
20982 sata_trace_dmsg_free(void)
20983 {
20984 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
20985 
20986 	while (dmsg != NULL) {
20987 		dmsg_next = dmsg->next;
20988 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
20989 
20990 		/*
20991 		 * If we've looped around the ring than we're done.
20992 		 */
20993 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
20994 			break;
20995 		} else {
20996 			dmsg = dmsg_next;
20997 		}
20998 	}
20999 }
21000 
21001 
21002 /*
21003  * This function can block
21004  */
21005 static void
21006 sata_trace_rbuf_alloc(void)
21007 {
21008 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21009 
21010 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21011 
21012 	if (dmsg_ring_size > 0) {
21013 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21014 	}
21015 }
21016 
21017 
21018 static void
21019 sata_trace_rbuf_free(void)
21020 {
21021 	sata_trace_dmsg_free();
21022 	mutex_destroy(&sata_debug_rbuf->lock);
21023 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21024 }
21025 
21026 /*
21027  * If SATA_DEBUG is not defined then this routine is called instead
21028  * of sata_log() via the SATA_LOG_D macro.
21029  */
21030 static void
21031 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21032     const char *fmt, ...)
21033 {
21034 #ifndef __lock_lint
21035 	_NOTE(ARGUNUSED(level))
21036 #endif
21037 
21038 	dev_info_t *dip = NULL;
21039 	va_list ap;
21040 
21041 	if (sata_hba_inst != NULL) {
21042 		dip = SATA_DIP(sata_hba_inst);
21043 	}
21044 
21045 	va_start(ap, fmt);
21046 	sata_vtrace_debug(dip, fmt, ap);
21047 	va_end(ap);
21048 }
21049