xref: /titanic_44/usr/src/uts/common/io/sata/impl/sata.c (revision 5e01956f3000408c2a2c5a08c8d0acf2c2a9d8ee)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 
27 /*
28  * SATA Framework
29  * Generic SATA Host Adapter Implementation
30  */
31 
32 #include <sys/conf.h>
33 #include <sys/file.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/modctl.h>
37 #include <sys/cmn_err.h>
38 #include <sys/errno.h>
39 #include <sys/thread.h>
40 #include <sys/kstat.h>
41 #include <sys/note.h>
42 #include <sys/sysevent.h>
43 #include <sys/sysevent/eventdefs.h>
44 #include <sys/sysevent/dr.h>
45 #include <sys/taskq.h>
46 #include <sys/disp.h>
47 #include <sys/sdt.h>
48 
49 #include <sys/sata/impl/sata.h>
50 #include <sys/sata/sata_hba.h>
51 #include <sys/sata/sata_defs.h>
52 #include <sys/sata/sata_cfgadm.h>
53 #include <sys/sata/sata_blacklist.h>
54 #include <sys/sata/sata_satl.h>
55 
56 #include <sys/scsi/impl/spc3_types.h>
57 
58 /*
59  * FMA header files
60  */
61 #include <sys/ddifm.h>
62 #include <sys/fm/protocol.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/io/ddi.h>
65 
66 /* Debug flags - defined in sata.h */
67 int	sata_debug_flags = 0;
68 int	sata_msg = 0;
69 
70 /*
71  * Flags enabling selected SATA HBA framework functionality
72  */
73 #define	SATA_ENABLE_QUEUING		1
74 #define	SATA_ENABLE_NCQ			2
75 #define	SATA_ENABLE_PROCESS_EVENTS	4
76 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
77 int sata_func_enable =
78 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
79 
80 /*
81  * Global variable setting default maximum queue depth (NCQ or TCQ)
82  * Note:minimum queue depth is 1
83  */
84 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
85 
86 /*
87  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
88  * initialization, using value from sata_max_queue_depth
89  * It is adjusted to minimum supported by the controller and by the device,
90  * if queueing is enabled.
91  */
92 static	int sata_current_max_qdepth;
93 
94 /*
95  * Global variable determining the default behavior after device hotpluggin.
96  * If non-zero, the hotplugged device is onlined (if possible) without explicit
97  * IOCTL request (AP_CONFIGURE).
98  * If zero, hotplugged device is identified, but not onlined.
99  * Enabling (AP_CONNECT) device port with an attached device does not result
100  * in device onlining regardless of the flag setting
101  */
102 int sata_auto_online = 0;
103 
104 #ifdef SATA_DEBUG
105 
106 #define	SATA_LOG_D(args)	sata_log args
107 uint64_t mbuf_count = 0;
108 uint64_t mbuffail_count = 0;
109 
110 sata_atapi_cmd_t sata_atapi_trace[64];
111 uint32_t sata_atapi_trace_index = 0;
112 int sata_atapi_trace_save = 1;
113 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
114 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
115     sata_save_atapi_trace(spx, count);
116 
117 #else
118 #define	SATA_LOG_D(args)	sata_trace_log args
119 #define	SATAATAPITRACE(spx, count)
120 #endif
121 
122 #if 0
123 static void
124 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
125 #endif
126 
127 #ifdef SATA_INJECT_FAULTS
128 
129 #define		SATA_INJECT_PKT_FAULT	1
130 uint32_t	sata_inject_fault = 0;
131 
132 uint32_t	sata_inject_fault_count = 0;
133 uint32_t	sata_inject_fault_pause_count = 0;
134 uint32_t	sata_fault_type = 0;
135 uint32_t	sata_fault_cmd = 0;
136 dev_info_t	*sata_fault_ctrl = NULL;
137 sata_device_t	sata_fault_device;
138 
139 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
140 
141 #endif
142 
143 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
144 
145 static char sata_rev_tag[] = {"1.46"};
146 
147 /*
148  * SATA cb_ops functions
149  */
150 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
151 static 	int sata_hba_close(dev_t, int, int, cred_t *);
152 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
153 
154 /*
155  * SCSA required entry points
156  */
157 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
158     scsi_hba_tran_t *, struct scsi_device *);
159 static	int sata_scsi_tgt_probe(struct scsi_device *,
160     int (*callback)(void));
161 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
162     scsi_hba_tran_t *, struct scsi_device *);
163 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
164 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
165 static 	int sata_scsi_reset(struct scsi_address *, int);
166 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
167 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
168 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
169     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
170     caddr_t);
171 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
172 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
173 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
174 
175 /*
176  * SATA HBA interface functions are defined in sata_hba.h header file
177  */
178 
179 /* Event processing functions */
180 static	void sata_event_daemon(void *);
181 static	void sata_event_thread_control(int);
182 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
183 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
184 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
185 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
186 static	void sata_process_port_failed_event(sata_hba_inst_t *,
187     sata_address_t *);
188 static	void sata_process_port_link_events(sata_hba_inst_t *,
189     sata_address_t *);
190 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
191     sata_address_t *);
192 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
193 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
194     sata_address_t *);
195 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
196 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
197     sata_address_t *);
198 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
199 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
200 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
201     sata_address_t *);
202 static	void sata_process_device_autoonline(sata_hba_inst_t *,
203     sata_address_t *saddr);
204 
205 /*
206  * Local translation functions
207  */
208 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
209 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
210 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
211 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
212 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
213 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
214 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
215 static	int sata_txlt_read(sata_pkt_txlate_t *);
216 static	int sata_txlt_write(sata_pkt_txlate_t *);
217 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
218 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
219 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
220 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
221 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
222 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
223 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
224 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
225 
226 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
227 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
228 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
229 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
230 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
231 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
232 static	void sata_txlt_rw_completion(sata_pkt_t *);
233 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
234 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
235 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
236 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
237 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
238 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
239     uint8_t);
240 static	struct scsi_extended_sense *sata_immediate_error_response(
241     sata_pkt_txlate_t *, int);
242 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
243 
244 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
245 static	void sata_txlt_atapi_completion(sata_pkt_t *);
246 
247 /*
248  * Local functions for ioctl
249  */
250 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
251 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
252     devctl_ap_state_t *);
253 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
254 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
255 static	dev_info_t *sata_devt_to_devinfo(dev_t);
256 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
257 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
258 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
259 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
260 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
265 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
267     sata_ioctl_data_t *, int mode);
268 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
269     sata_ioctl_data_t *, int mode);
270 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
271     sata_ioctl_data_t *, int mode);
272 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
273     sata_ioctl_data_t *, int mode);
274 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
275     sata_device_t *, sata_ioctl_data_t *, int mode);
276 
277 /*
278  * Local functions
279  */
280 static 	void sata_remove_hba_instance(dev_info_t *);
281 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
282 static 	void sata_probe_ports(sata_hba_inst_t *);
283 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
284 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
285 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
286 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
287 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
288 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
289 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
290 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
291     sata_drive_info_t *);
292 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
293     sata_address_t *);
294 static 	void sata_remove_target_node(sata_hba_inst_t *,
295     sata_address_t *);
296 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
297     struct scsi_address *, sata_device_t *);
298 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
299 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
300 static	void sata_pkt_free(sata_pkt_txlate_t *);
301 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
302     caddr_t, ddi_dma_attr_t *);
303 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
304 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
305 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
306     sata_device_t *);
307 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
308 static	void sata_reidentify_device(sata_pkt_txlate_t *);
309 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
310 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
311 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
312 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
313     ddi_dma_attr_t *);
314 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
315     sata_drive_info_t *);
316 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
317 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
318 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
319 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
320 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
321 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
322 static	int sata_set_drive_features(sata_hba_inst_t *,
323     sata_drive_info_t *, int flag);
324 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
325 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
326 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
327     uint8_t *);
328 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
329     struct scsi_inquiry *);
330 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
331 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
332 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
333 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
334 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
336     struct mode_cache_scsi3 *, int, int *, int *, int *);
337 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
338     struct mode_info_power_cond *, int, int *, int *, int *);
339 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
340     struct mode_info_excpt_page *, int, int *, int *, int *);
341 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
342     struct mode_acoustic_management *, int, int *, int *, int *);
343 
344 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
345 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
346     sata_hba_inst_t *);
347 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
348     sata_hba_inst_t *);
349 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
350     sata_hba_inst_t *);
351 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
352     sata_pkt_txlate_t *);
353 
354 static	void sata_set_arq_data(sata_pkt_t *);
355 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
356 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
357 static	uint8_t sata_get_standby_timer(uint8_t *timer);
358 
359 static	void sata_save_drive_settings(sata_drive_info_t *);
360 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
361 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
362 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
363 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
364 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
365     sata_drive_info_t *);
366 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
367     struct smart_data *);
368 static	int sata_smart_selftest_log(sata_hba_inst_t *,
369     sata_drive_info_t *,
370     struct smart_selftest_log *);
371 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
372     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
373 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
374     uint8_t *, uint8_t, uint8_t);
375 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
376     struct read_log_ext_directory *);
377 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
378 static	void sata_xlate_errors(sata_pkt_txlate_t *);
379 static	void sata_decode_device_error(sata_pkt_txlate_t *,
380     struct scsi_extended_sense *);
381 static	void sata_set_device_removed(dev_info_t *);
382 static	boolean_t sata_check_device_removed(dev_info_t *);
383 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
384 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
385     sata_drive_info_t *);
386 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
387     sata_drive_info_t *);
388 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
389 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
390 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
391 static  int sata_check_modser(char *, int);
392 
393 /*
394  * FMA
395  */
396 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
397 
398 
399 /*
400  * SATA Framework will ignore SATA HBA driver cb_ops structure and
401  * register following one with SCSA framework.
402  * Open & close are provided, so scsi framework will not use its own
403  */
404 static struct cb_ops sata_cb_ops = {
405 	sata_hba_open,			/* open */
406 	sata_hba_close,			/* close */
407 	nodev,				/* strategy */
408 	nodev,				/* print */
409 	nodev,				/* dump */
410 	nodev,				/* read */
411 	nodev,				/* write */
412 	sata_hba_ioctl,			/* ioctl */
413 	nodev,				/* devmap */
414 	nodev,				/* mmap */
415 	nodev,				/* segmap */
416 	nochpoll,			/* chpoll */
417 	ddi_prop_op,			/* cb_prop_op */
418 	0,				/* streamtab */
419 	D_NEW | D_MP,			/* cb_flag */
420 	CB_REV,				/* rev */
421 	nodev,				/* aread */
422 	nodev				/* awrite */
423 };
424 
425 
426 extern struct mod_ops mod_miscops;
427 extern uchar_t	scsi_cdb_size[];
428 
429 static struct modlmisc modlmisc = {
430 	&mod_miscops,			/* Type of module */
431 	"SATA Module"			/* module name */
432 };
433 
434 
435 static struct modlinkage modlinkage = {
436 	MODREV_1,
437 	(void *)&modlmisc,
438 	NULL
439 };
440 
441 /*
442  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
443  * i.e. when scsi_pkt has not timeout specified.
444  */
445 static int sata_default_pkt_time = 60;	/* 60 seconds */
446 
447 /*
448  * Intermediate buffer device access attributes - they are required,
449  * but not necessarily used.
450  */
451 static ddi_device_acc_attr_t sata_acc_attr = {
452 	DDI_DEVICE_ATTR_V0,
453 	DDI_STRUCTURE_LE_ACC,
454 	DDI_STRICTORDER_ACC
455 };
456 
457 
458 /*
459  * Mutexes protecting structures in multithreaded operations.
460  * Because events are relatively rare, a single global mutex protecting
461  * data structures should be sufficient. To increase performance, add
462  * separate mutex per each sata port and use global mutex only to protect
463  * common data structures.
464  */
465 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
466 static	kmutex_t sata_log_mutex;	/* protects log */
467 
468 static 	char sata_log_buf[256];
469 
470 /*
471  * sata trace debug
472  */
473 static	sata_trace_rbuf_t *sata_debug_rbuf;
474 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
475 static	void sata_trace_dmsg_free(void);
476 static	void sata_trace_rbuf_alloc(void);
477 static	void sata_trace_rbuf_free(void);
478 
479 int	dmsg_ring_size = DMSG_RING_SIZE;
480 
481 /* Default write cache setting for SATA hard disks */
482 int	sata_write_cache = 1;		/* enabled */
483 
484 /* Default write cache setting for SATA ATAPI CD/DVD */
485 int	sata_atapicdvd_write_cache = 1; /* enabled */
486 
487 /* Default write cache setting for SATA ATAPI tape */
488 int	sata_atapitape_write_cache = 1; /* enabled */
489 
490 /* Default write cache setting for SATA ATAPI disk */
491 int	sata_atapidisk_write_cache = 1;	/* enabled */
492 
493 /*
494  * Linked list of HBA instances
495  */
496 static 	sata_hba_inst_t *sata_hba_list = NULL;
497 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
498 /*
499  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
500  * structure and in sata soft state.
501  */
502 
503 /*
504  * Event daemon related variables
505  */
506 static 	kmutex_t sata_event_mutex;
507 static 	kcondvar_t sata_event_cv;
508 static 	kthread_t *sata_event_thread = NULL;
509 static 	int sata_event_thread_terminate = 0;
510 static 	int sata_event_pending = 0;
511 static 	int sata_event_thread_active = 0;
512 extern 	pri_t minclsyspri;
513 
514 /*
515  * NCQ error recovery command
516  */
517 static const sata_cmd_t sata_rle_cmd = {
518 	SATA_CMD_REV,
519 	NULL,
520 	{
521 		SATA_DIR_READ
522 	},
523 	ATA_ADDR_LBA48,
524 	0,
525 	0,
526 	0,
527 	0,
528 	0,
529 	1,
530 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
531 	0,
532 	0,
533 	0,
534 	SATAC_READ_LOG_EXT,
535 	0,
536 	0,
537 	0,
538 };
539 
540 /*
541  * ATAPI error recovery CDB
542  */
543 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
544 	SCMD_REQUEST_SENSE,
545 	0,			/* Only fixed RQ format is supported */
546 	0,
547 	0,
548 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
549 	0
550 };
551 
552 
553 /* Warlock directives */
554 
555 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
556 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
564 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
565 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
566 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
567 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
568 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
569     sata_hba_inst::satahba_scsi_tran))
570 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
571 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
572 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
573 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
574 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
575     sata_hba_inst::satahba_event_flags))
576 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
577     sata_cport_info::cport_devp))
578 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
579 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
580 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
581     sata_cport_info::cport_dev_type))
582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
583 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
584     sata_cport_info::cport_state))
585 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
586 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
587     sata_pmport_info::pmport_state))
588 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
589 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
590     sata_pmport_info::pmport_dev_type))
591 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
592 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
593     sata_pmport_info::pmport_sata_drive))
594 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
595     sata_pmport_info::pmport_tgtnode_clean))
596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597     sata_pmport_info::pmport_event_flags))
598 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
599 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
600 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
601 #ifdef SATA_DEBUG
602 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
603 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
606 #endif
607 
608 /* End of warlock directives */
609 
610 /* ************** loadable module configuration functions ************** */
611 
612 int
613 _init()
614 {
615 	int rval;
616 
617 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
618 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
619 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
620 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
621 	sata_trace_rbuf_alloc();
622 	if ((rval = mod_install(&modlinkage)) != 0) {
623 #ifdef SATA_DEBUG
624 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
625 #endif
626 		sata_trace_rbuf_free();
627 		mutex_destroy(&sata_log_mutex);
628 		cv_destroy(&sata_event_cv);
629 		mutex_destroy(&sata_event_mutex);
630 		mutex_destroy(&sata_mutex);
631 	}
632 	return (rval);
633 }
634 
635 int
636 _fini()
637 {
638 	int rval;
639 
640 	if ((rval = mod_remove(&modlinkage)) != 0)
641 		return (rval);
642 
643 	sata_trace_rbuf_free();
644 	mutex_destroy(&sata_log_mutex);
645 	cv_destroy(&sata_event_cv);
646 	mutex_destroy(&sata_event_mutex);
647 	mutex_destroy(&sata_mutex);
648 	return (rval);
649 }
650 
651 int
652 _info(struct modinfo *modinfop)
653 {
654 	return (mod_info(&modlinkage, modinfop));
655 }
656 
657 
658 
659 /* ********************* SATA HBA entry points ********************* */
660 
661 
662 /*
663  * Called by SATA HBA from _init().
664  * Registers HBA driver instance/sata framework pair with scsi framework, by
665  * calling scsi_hba_init().
666  *
667  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
668  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
669  * cb_ops pointer in SATA HBA driver dev_ops structure.
670  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
671  *
672  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
673  * driver.
674  */
675 int
676 sata_hba_init(struct modlinkage *modlp)
677 {
678 	int rval;
679 	struct dev_ops *hba_ops;
680 
681 	SATADBG1(SATA_DBG_HBA_IF, NULL,
682 	    "sata_hba_init: name %s \n",
683 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
684 	/*
685 	 * Fill-up cb_ops and dev_ops when necessary
686 	 */
687 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
688 	/*
689 	 * Provide pointer to SATA dev_ops
690 	 */
691 	hba_ops->devo_cb_ops = &sata_cb_ops;
692 
693 	/*
694 	 * Register SATA HBA with SCSI framework
695 	 */
696 	if ((rval = scsi_hba_init(modlp)) != 0) {
697 		SATADBG1(SATA_DBG_HBA_IF, NULL,
698 		    "sata_hba_init: scsi hba init failed\n", NULL);
699 		return (rval);
700 	}
701 
702 	return (0);
703 }
704 
705 
706 /* HBA attach stages */
707 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
708 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
709 #define	HBA_ATTACH_STAGE_SETUP		4
710 #define	HBA_ATTACH_STAGE_LINKED		8
711 
712 
713 /*
714  *
715  * Called from SATA HBA driver's attach routine to attach an instance of
716  * the HBA.
717  *
718  * For DDI_ATTACH command:
719  * sata_hba_inst structure is allocated here and initialized with pointers to
720  * SATA framework implementation of required scsi tran functions.
721  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
722  * to the soft structure (sata_hba_inst) allocated by SATA framework for
723  * SATA HBA instance related data.
724  * The scsi_tran's tran_hba_private field is used by SATA framework to
725  * store a pointer to per-HBA-instance of sata_hba_inst structure.
726  * The sata_hba_inst structure is cross-linked to scsi tran structure.
727  * Among other info, a pointer to sata_hba_tran structure is stored in
728  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
729  * linked together into the list, pointed to by sata_hba_list.
730  * On the first HBA instance attach the sata event thread is initialized.
731  * Attachment points are created for all SATA ports of the HBA being attached.
732  * All HBA instance's SATA ports are probed and type of plugged devices is
733  * determined. For each device of a supported type, a target node is created.
734  *
735  * DDI_SUCCESS is returned when attachment process is successful,
736  * DDI_FAILURE is returned otherwise.
737  *
738  * For DDI_RESUME command:
739  * Not implemented at this time (postponed until phase 2 of the development).
740  */
741 int
742 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
743     ddi_attach_cmd_t cmd)
744 {
745 	sata_hba_inst_t	*sata_hba_inst;
746 	scsi_hba_tran_t *scsi_tran = NULL;
747 	int hba_attach_state = 0;
748 	char taskq_name[MAXPATHLEN];
749 
750 	SATADBG3(SATA_DBG_HBA_IF, NULL,
751 	    "sata_hba_attach: node %s (%s%d)\n",
752 	    ddi_node_name(dip), ddi_driver_name(dip),
753 	    ddi_get_instance(dip));
754 
755 	if (cmd == DDI_RESUME) {
756 		/*
757 		 * Postponed until phase 2 of the development
758 		 */
759 		return (DDI_FAILURE);
760 	}
761 
762 	if (cmd != DDI_ATTACH) {
763 		return (DDI_FAILURE);
764 	}
765 
766 	/* cmd == DDI_ATTACH */
767 
768 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
769 		SATA_LOG_D((NULL, CE_WARN,
770 		    "sata_hba_attach: invalid sata_hba_tran"));
771 		return (DDI_FAILURE);
772 	}
773 	/*
774 	 * Allocate and initialize SCSI tran structure.
775 	 * SATA copy of tran_bus_config is provided to create port nodes.
776 	 */
777 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
778 	if (scsi_tran == NULL)
779 		return (DDI_FAILURE);
780 	/*
781 	 * Allocate soft structure for SATA HBA instance.
782 	 * There is a separate softstate for each HBA instance.
783 	 */
784 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
785 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
786 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
787 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
788 
789 	/*
790 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
791 	 * soft structure allocated by SATA framework for
792 	 * SATA HBA instance related data.
793 	 */
794 	scsi_tran->tran_hba_private	= sata_hba_inst;
795 	scsi_tran->tran_tgt_private	= NULL;
796 
797 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
798 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
799 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
800 
801 	scsi_tran->tran_start		= sata_scsi_start;
802 	scsi_tran->tran_reset		= sata_scsi_reset;
803 	scsi_tran->tran_abort		= sata_scsi_abort;
804 	scsi_tran->tran_getcap		= sata_scsi_getcap;
805 	scsi_tran->tran_setcap		= sata_scsi_setcap;
806 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
807 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
808 
809 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
810 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
811 
812 	scsi_tran->tran_reset_notify	= NULL;
813 	scsi_tran->tran_get_bus_addr	= NULL;
814 	scsi_tran->tran_quiesce		= NULL;
815 	scsi_tran->tran_unquiesce	= NULL;
816 	scsi_tran->tran_bus_reset	= NULL;
817 
818 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
819 	    scsi_tran, 0) != DDI_SUCCESS) {
820 #ifdef SATA_DEBUG
821 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
822 		    ddi_driver_name(dip), ddi_get_instance(dip));
823 #endif
824 		goto fail;
825 	}
826 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
827 
828 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
829 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
830 		    "sata", 1) != DDI_PROP_SUCCESS) {
831 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
832 			    "failed to create hba sata prop"));
833 			goto fail;
834 		}
835 	}
836 
837 	/*
838 	 * Save pointers in hba instance soft state.
839 	 */
840 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
841 	sata_hba_inst->satahba_tran = sata_tran;
842 	sata_hba_inst->satahba_dip = dip;
843 
844 	/*
845 	 * Create a task queue to handle emulated commands completion
846 	 * Use node name, dash, instance number as the queue name.
847 	 */
848 	taskq_name[0] = '\0';
849 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
850 	    sizeof (taskq_name));
851 	(void) snprintf(taskq_name + strlen(taskq_name),
852 	    sizeof (taskq_name) - strlen(taskq_name),
853 	    "-%d", DEVI(dip)->devi_instance);
854 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
855 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
856 	    TASKQ_DYNAMIC);
857 
858 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
859 
860 	/*
861 	 * Create events thread if not created yet.
862 	 */
863 	sata_event_thread_control(1);
864 
865 	/*
866 	 * Link this hba instance into the list.
867 	 */
868 	mutex_enter(&sata_mutex);
869 
870 	if (sata_hba_list == NULL) {
871 		/*
872 		 * The first instance of HBA is attached.
873 		 * Set current/active default maximum NCQ/TCQ queue depth for
874 		 * all SATA devices. It is done here and now, to eliminate the
875 		 * possibility of the dynamic, programatic modification of the
876 		 * queue depth via global (and public) sata_max_queue_depth
877 		 * variable (this would require special handling in HBA drivers)
878 		 */
879 		sata_current_max_qdepth = sata_max_queue_depth;
880 		if (sata_current_max_qdepth > 32)
881 			sata_current_max_qdepth = 32;
882 		else if (sata_current_max_qdepth < 1)
883 			sata_current_max_qdepth = 1;
884 	}
885 
886 	sata_hba_inst->satahba_next = NULL;
887 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
888 	if (sata_hba_list == NULL) {
889 		sata_hba_list = sata_hba_inst;
890 	}
891 	if (sata_hba_list_tail != NULL) {
892 		sata_hba_list_tail->satahba_next = sata_hba_inst;
893 	}
894 	sata_hba_list_tail = sata_hba_inst;
895 	mutex_exit(&sata_mutex);
896 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
897 
898 	/*
899 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
900 	 * SATA HBA driver should not use its own open/close entry points.
901 	 *
902 	 * Make sure that instance number doesn't overflow
903 	 * when forming minor numbers.
904 	 */
905 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
906 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
907 	    INST2DEVCTL(ddi_get_instance(dip)),
908 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
909 #ifdef SATA_DEBUG
910 		cmn_err(CE_WARN, "sata_hba_attach: "
911 		    "cannot create devctl minor node");
912 #endif
913 		goto fail;
914 	}
915 
916 
917 	/*
918 	 * Set-up kstats here, if necessary.
919 	 * (postponed until future phase of the development).
920 	 */
921 
922 	/*
923 	 * Indicate that HBA is attached. This will enable events processing
924 	 * for this HBA.
925 	 */
926 	sata_hba_inst->satahba_attached = 1;
927 	/*
928 	 * Probe controller ports. This operation will describe a current
929 	 * controller/port/multipliers/device configuration and will create
930 	 * attachment points.
931 	 * We may end-up with just a controller with no devices attached.
932 	 * For the ports with a supported device attached, device target nodes
933 	 * are created and devices are initialized.
934 	 */
935 	sata_probe_ports(sata_hba_inst);
936 
937 	return (DDI_SUCCESS);
938 
939 fail:
940 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
941 		(void) sata_remove_hba_instance(dip);
942 		if (sata_hba_list == NULL)
943 			sata_event_thread_control(0);
944 	}
945 
946 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
947 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
948 		taskq_destroy(sata_hba_inst->satahba_taskq);
949 	}
950 
951 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
952 		(void) scsi_hba_detach(dip);
953 
954 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
955 		mutex_destroy(&sata_hba_inst->satahba_mutex);
956 		kmem_free((void *)sata_hba_inst,
957 		    sizeof (struct sata_hba_inst));
958 		scsi_hba_tran_free(scsi_tran);
959 	}
960 
961 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
962 	    ddi_driver_name(dip), ddi_get_instance(dip));
963 
964 	return (DDI_FAILURE);
965 }
966 
967 
968 /*
969  * Called by SATA HBA from to detach an instance of the driver.
970  *
971  * For DDI_DETACH command:
972  * Free local structures allocated for SATA HBA instance during
973  * sata_hba_attach processing.
974  *
975  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
976  *
977  * For DDI_SUSPEND command:
978  * Not implemented at this time (postponed until phase 2 of the development)
979  * Returnd DDI_SUCCESS.
980  *
981  * When the last HBA instance is detached, the event daemon is terminated.
982  *
983  * NOTE: Port multiplier is supported.
984  */
985 int
986 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
987 {
988 	dev_info_t	*tdip;
989 	sata_hba_inst_t	*sata_hba_inst;
990 	scsi_hba_tran_t *scsi_hba_tran;
991 	sata_cport_info_t *cportinfo;
992 	sata_pmult_info_t *pminfo;
993 	sata_drive_info_t *sdinfo;
994 	sata_device_t	sdevice;
995 	int ncport, npmport;
996 
997 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
998 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
999 
1000 	switch (cmd) {
1001 	case DDI_DETACH:
1002 
1003 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1004 			return (DDI_FAILURE);
1005 
1006 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1007 		if (sata_hba_inst == NULL)
1008 			return (DDI_FAILURE);
1009 
1010 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1011 			sata_hba_inst->satahba_attached = 1;
1012 			return (DDI_FAILURE);
1013 		}
1014 
1015 		/*
1016 		 * Free all target nodes - at this point
1017 		 * devices should be at least offlined
1018 		 * otherwise scsi_hba_detach() should not be called.
1019 		 */
1020 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1021 		    ncport++) {
1022 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1023 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1024 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1025 				if (sdinfo != NULL) {
1026 					tdip = sata_get_target_dip(dip,
1027 					    ncport, 0);
1028 					if (tdip != NULL) {
1029 						if (ndi_devi_offline(tdip,
1030 						    NDI_DEVI_REMOVE) !=
1031 						    NDI_SUCCESS) {
1032 							SATA_LOG_D((
1033 							    sata_hba_inst,
1034 							    CE_WARN,
1035 							    "sata_hba_detach: "
1036 							    "Target node not "
1037 							    "removed !"));
1038 							return (DDI_FAILURE);
1039 						}
1040 					}
1041 				}
1042 			} else { /* SATA_DTYPE_PMULT */
1043 				mutex_enter(&cportinfo->cport_mutex);
1044 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1045 
1046 				if (pminfo == NULL) {
1047 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1048 					    "sata_hba_detach: Port multiplier "
1049 					    "not ready yet!"));
1050 					mutex_exit(&cportinfo->cport_mutex);
1051 					return (DDI_FAILURE);
1052 				}
1053 
1054 				/*
1055 				 * Detach would fail if removal of any of the
1056 				 * target nodes is failed - albeit in that
1057 				 * case some of them may have been removed.
1058 				 */
1059 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1060 				    sata_hba_inst, ncport); npmport++) {
1061 					tdip = sata_get_target_dip(dip, ncport,
1062 					    npmport);
1063 					if (tdip != NULL) {
1064 						if (ndi_devi_offline(tdip,
1065 						    NDI_DEVI_REMOVE) !=
1066 						    NDI_SUCCESS) {
1067 							SATA_LOG_D((
1068 							    sata_hba_inst,
1069 							    CE_WARN,
1070 							    "sata_hba_detach: "
1071 							    "Target node not "
1072 							    "removed !"));
1073 							mutex_exit(&cportinfo->
1074 							    cport_mutex);
1075 							return (DDI_FAILURE);
1076 						}
1077 					}
1078 				}
1079 				mutex_exit(&cportinfo->cport_mutex);
1080 			}
1081 		}
1082 		/*
1083 		 * Disable sata event daemon processing for this HBA
1084 		 */
1085 		sata_hba_inst->satahba_attached = 0;
1086 
1087 		/*
1088 		 * Remove event daemon thread, if it is last HBA instance.
1089 		 */
1090 
1091 		mutex_enter(&sata_mutex);
1092 		if (sata_hba_list->satahba_next == NULL) {
1093 			mutex_exit(&sata_mutex);
1094 			sata_event_thread_control(0);
1095 			mutex_enter(&sata_mutex);
1096 		}
1097 		mutex_exit(&sata_mutex);
1098 
1099 		/* Remove this HBA instance from the HBA list */
1100 		sata_remove_hba_instance(dip);
1101 
1102 		/*
1103 		 * At this point there should be no target nodes attached.
1104 		 * Detach and destroy device and port info structures.
1105 		 */
1106 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1107 		    ncport++) {
1108 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1109 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1110 				sdinfo =
1111 				    cportinfo->cport_devp.cport_sata_drive;
1112 				if (sdinfo != NULL) {
1113 					/* Release device structure */
1114 					kmem_free(sdinfo,
1115 					    sizeof (sata_drive_info_t));
1116 				}
1117 				/* Release cport info */
1118 				mutex_destroy(&cportinfo->cport_mutex);
1119 				kmem_free(cportinfo,
1120 				    sizeof (sata_cport_info_t));
1121 			} else { /* SATA_DTYPE_PMULT */
1122 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1123 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1124 				sata_free_pmult(sata_hba_inst, &sdevice);
1125 			}
1126 		}
1127 
1128 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1129 
1130 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1131 
1132 		taskq_destroy(sata_hba_inst->satahba_taskq);
1133 
1134 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1135 		kmem_free((void *)sata_hba_inst,
1136 		    sizeof (struct sata_hba_inst));
1137 
1138 		return (DDI_SUCCESS);
1139 
1140 	case DDI_SUSPEND:
1141 		/*
1142 		 * Postponed until phase 2
1143 		 */
1144 		return (DDI_FAILURE);
1145 
1146 	default:
1147 		return (DDI_FAILURE);
1148 	}
1149 }
1150 
1151 
1152 /*
1153  * Called by an HBA drive from _fini() routine.
1154  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1155  */
1156 void
1157 sata_hba_fini(struct modlinkage *modlp)
1158 {
1159 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1160 	    "sata_hba_fini: name %s\n",
1161 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1162 
1163 	scsi_hba_fini(modlp);
1164 }
1165 
1166 
1167 /*
1168  * Default open and close routine for sata_hba framework.
1169  *
1170  */
1171 /*
1172  * Open devctl node.
1173  *
1174  * Returns:
1175  * 0 if node was open successfully, error code otherwise.
1176  *
1177  *
1178  */
1179 
1180 static int
1181 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1182 {
1183 #ifndef __lock_lint
1184 	_NOTE(ARGUNUSED(credp))
1185 #endif
1186 	int rv = 0;
1187 	dev_info_t *dip;
1188 	scsi_hba_tran_t *scsi_hba_tran;
1189 	sata_hba_inst_t	*sata_hba_inst;
1190 
1191 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1192 
1193 	if (otyp != OTYP_CHR)
1194 		return (EINVAL);
1195 
1196 	dip = sata_devt_to_devinfo(*devp);
1197 	if (dip == NULL)
1198 		return (ENXIO);
1199 
1200 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1201 		return (ENXIO);
1202 
1203 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1204 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1205 		return (ENXIO);
1206 
1207 	mutex_enter(&sata_mutex);
1208 	if (flags & FEXCL) {
1209 		if (sata_hba_inst->satahba_open_flag != 0) {
1210 			rv = EBUSY;
1211 		} else {
1212 			sata_hba_inst->satahba_open_flag =
1213 			    SATA_DEVCTL_EXOPENED;
1214 		}
1215 	} else {
1216 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1217 			rv = EBUSY;
1218 		} else {
1219 			sata_hba_inst->satahba_open_flag =
1220 			    SATA_DEVCTL_SOPENED;
1221 		}
1222 	}
1223 	mutex_exit(&sata_mutex);
1224 
1225 	return (rv);
1226 }
1227 
1228 
1229 /*
1230  * Close devctl node.
1231  * Returns:
1232  * 0 if node was closed successfully, error code otherwise.
1233  *
1234  */
1235 
1236 static int
1237 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1238 {
1239 #ifndef __lock_lint
1240 	_NOTE(ARGUNUSED(credp))
1241 	_NOTE(ARGUNUSED(flag))
1242 #endif
1243 	dev_info_t *dip;
1244 	scsi_hba_tran_t *scsi_hba_tran;
1245 	sata_hba_inst_t	*sata_hba_inst;
1246 
1247 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1248 
1249 	if (otyp != OTYP_CHR)
1250 		return (EINVAL);
1251 
1252 	dip = sata_devt_to_devinfo(dev);
1253 	if (dip == NULL)
1254 		return (ENXIO);
1255 
1256 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1257 		return (ENXIO);
1258 
1259 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1260 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1261 		return (ENXIO);
1262 
1263 	mutex_enter(&sata_mutex);
1264 	sata_hba_inst->satahba_open_flag = 0;
1265 	mutex_exit(&sata_mutex);
1266 	return (0);
1267 }
1268 
1269 
1270 
1271 /*
1272  * Standard IOCTL commands for SATA hotplugging.
1273  * Implemented DEVCTL_AP commands:
1274  * DEVCTL_AP_CONNECT
1275  * DEVCTL_AP_DISCONNECT
1276  * DEVCTL_AP_CONFIGURE
1277  * DEVCTL_UNCONFIGURE
1278  * DEVCTL_AP_CONTROL
1279  *
1280  * Commands passed to default ndi ioctl handler:
1281  * DEVCTL_DEVICE_GETSTATE
1282  * DEVCTL_DEVICE_ONLINE
1283  * DEVCTL_DEVICE_OFFLINE
1284  * DEVCTL_DEVICE_REMOVE
1285  * DEVCTL_DEVICE_INSERT
1286  * DEVCTL_BUS_GETSTATE
1287  *
1288  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1289  * if not.
1290  *
1291  * Returns:
1292  * 0 if successful,
1293  * error code if operation failed.
1294  *
1295  * Port Multiplier support is supported now.
1296  *
1297  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1298  */
1299 
1300 static int
1301 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1302     int *rvalp)
1303 {
1304 #ifndef __lock_lint
1305 	_NOTE(ARGUNUSED(credp))
1306 	_NOTE(ARGUNUSED(rvalp))
1307 #endif
1308 	int rv = 0;
1309 	int32_t	comp_port = -1;
1310 	dev_info_t *dip;
1311 	devctl_ap_state_t ap_state;
1312 	struct devctl_iocdata *dcp = NULL;
1313 	scsi_hba_tran_t *scsi_hba_tran;
1314 	sata_hba_inst_t *sata_hba_inst;
1315 	sata_device_t sata_device;
1316 	sata_cport_info_t *cportinfo;
1317 	int cport, pmport, qual;
1318 	int rval = SATA_SUCCESS;
1319 
1320 	dip = sata_devt_to_devinfo(dev);
1321 	if (dip == NULL)
1322 		return (ENXIO);
1323 
1324 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1325 		return (ENXIO);
1326 
1327 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1328 	if (sata_hba_inst == NULL)
1329 		return (ENXIO);
1330 
1331 	if (sata_hba_inst->satahba_tran == NULL)
1332 		return (ENXIO);
1333 
1334 	switch (cmd) {
1335 
1336 	case DEVCTL_DEVICE_GETSTATE:
1337 	case DEVCTL_DEVICE_ONLINE:
1338 	case DEVCTL_DEVICE_OFFLINE:
1339 	case DEVCTL_DEVICE_REMOVE:
1340 	case DEVCTL_BUS_GETSTATE:
1341 		/*
1342 		 * There may be more cases that we want to pass to default
1343 		 * handler rather than fail them.
1344 		 */
1345 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1346 	}
1347 
1348 	/* read devctl ioctl data */
1349 	if (cmd != DEVCTL_AP_CONTROL) {
1350 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1351 			return (EFAULT);
1352 
1353 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1354 		    -1) {
1355 			if (dcp)
1356 				ndi_dc_freehdl(dcp);
1357 			return (EINVAL);
1358 		}
1359 
1360 		/*
1361 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1362 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1363 		 */
1364 		cport = SCSI_TO_SATA_CPORT(comp_port);
1365 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1366 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1367 
1368 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1369 		    qual) != 0) {
1370 			ndi_dc_freehdl(dcp);
1371 			return (EINVAL);
1372 		}
1373 
1374 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1375 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1376 		    cport_mutex);
1377 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1378 			/*
1379 			 * Cannot process ioctl request now. Come back later.
1380 			 */
1381 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1382 			    cport_mutex);
1383 			ndi_dc_freehdl(dcp);
1384 			return (EBUSY);
1385 		}
1386 		/* Block event processing for this port */
1387 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1388 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1389 
1390 		sata_device.satadev_addr.cport = cport;
1391 		sata_device.satadev_addr.pmport = pmport;
1392 		sata_device.satadev_addr.qual = qual;
1393 		sata_device.satadev_rev = SATA_DEVICE_REV;
1394 	}
1395 
1396 	switch (cmd) {
1397 
1398 	case DEVCTL_AP_DISCONNECT:
1399 
1400 		/*
1401 		 * Normally, cfgadm sata plugin will try to offline
1402 		 * (unconfigure) device before this request. Nevertheless,
1403 		 * if a device is still configured, we need to
1404 		 * attempt to offline and unconfigure device first, and we will
1405 		 * deactivate the port regardless of the unconfigure
1406 		 * operation results.
1407 		 *
1408 		 */
1409 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1410 
1411 		break;
1412 
1413 	case DEVCTL_AP_UNCONFIGURE:
1414 
1415 		/*
1416 		 * The unconfigure operation uses generic nexus operation to
1417 		 * offline a device. It leaves a target device node attached.
1418 		 * and obviously sata_drive_info attached as well, because
1419 		 * from the hardware point of view nothing has changed.
1420 		 */
1421 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1422 		break;
1423 
1424 	case DEVCTL_AP_CONNECT:
1425 	{
1426 		/*
1427 		 * The sata cfgadm pluging will invoke this operation only if
1428 		 * port was found in the disconnect state (failed state
1429 		 * is also treated as the disconnected state).
1430 		 * If port activation is successful and a device is found
1431 		 * attached to the port, the initialization sequence is
1432 		 * executed to probe the port and attach
1433 		 * a device structure to a port structure. The device is not
1434 		 * set in configured state (system-wise) by this operation.
1435 		 */
1436 
1437 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1438 
1439 		break;
1440 	}
1441 
1442 	case DEVCTL_AP_CONFIGURE:
1443 	{
1444 		/*
1445 		 * A port may be in an active or shutdown state.
1446 		 * If port is in a failed state, operation is aborted.
1447 		 * If a port is in a shutdown state, sata_tran_port_activate()
1448 		 * is invoked prior to any other operation.
1449 		 *
1450 		 * Onlining the device involves creating a new target node.
1451 		 * If there is an old target node present (belonging to
1452 		 * previously removed device), the operation is aborted - the
1453 		 * old node has to be released and removed before configure
1454 		 * operation is attempted.
1455 		 */
1456 
1457 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1458 
1459 		break;
1460 	}
1461 
1462 	case DEVCTL_AP_GETSTATE:
1463 
1464 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1465 
1466 		ap_state.ap_last_change = (time_t)-1;
1467 		ap_state.ap_error_code = 0;
1468 		ap_state.ap_in_transition = 0;
1469 
1470 		/* Copy the return AP-state information to the user space */
1471 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1472 			rv = EFAULT;
1473 		}
1474 		break;
1475 
1476 	case DEVCTL_AP_CONTROL:
1477 	{
1478 		/*
1479 		 * Generic devctl for hardware specific functionality
1480 		 */
1481 		sata_ioctl_data_t	ioc;
1482 
1483 		ASSERT(dcp == NULL);
1484 
1485 		/* Copy in user ioctl data first */
1486 #ifdef _MULTI_DATAMODEL
1487 		if (ddi_model_convert_from(mode & FMODELS) ==
1488 		    DDI_MODEL_ILP32) {
1489 
1490 			sata_ioctl_data_32_t	ioc32;
1491 
1492 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1493 			    sizeof (ioc32), mode) != 0) {
1494 				rv = EFAULT;
1495 				break;
1496 			}
1497 			ioc.cmd 	= (uint_t)ioc32.cmd;
1498 			ioc.port	= (uint_t)ioc32.port;
1499 			ioc.get_size	= (uint_t)ioc32.get_size;
1500 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1501 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1502 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1503 		} else
1504 #endif /* _MULTI_DATAMODEL */
1505 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1506 		    mode) != 0) {
1507 			return (EFAULT);
1508 		}
1509 
1510 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1511 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1512 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1513 
1514 		/*
1515 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1516 		 * a 32-bit number.
1517 		 */
1518 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1519 			return (EINVAL);
1520 		}
1521 		/* validate address */
1522 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1523 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1524 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1525 
1526 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1527 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1528 		    cport, pmport, qual);
1529 
1530 		if (sata_validate_sata_address(sata_hba_inst, cport,
1531 		    pmport, qual) != 0)
1532 			return (EINVAL);
1533 
1534 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1535 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1536 		    cport_mutex);
1537 		/* Is the port locked by event processing daemon ? */
1538 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1539 			/*
1540 			 * Cannot process ioctl request now. Come back later
1541 			 */
1542 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1543 			    cport_mutex);
1544 			return (EBUSY);
1545 		}
1546 		/* Block event processing for this port */
1547 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1548 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1549 
1550 
1551 		sata_device.satadev_addr.cport = cport;
1552 		sata_device.satadev_addr.pmport = pmport;
1553 		sata_device.satadev_addr.qual = qual;
1554 		sata_device.satadev_rev = SATA_DEVICE_REV;
1555 
1556 		switch (ioc.cmd) {
1557 
1558 		case SATA_CFGA_RESET_PORT:
1559 			/*
1560 			 * There is no protection for configured device.
1561 			 */
1562 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1563 			break;
1564 
1565 		case SATA_CFGA_RESET_DEVICE:
1566 			/*
1567 			 * There is no protection for configured device.
1568 			 */
1569 			rv = sata_ioctl_reset_device(sata_hba_inst,
1570 			    &sata_device);
1571 			break;
1572 
1573 		case SATA_CFGA_RESET_ALL:
1574 			/*
1575 			 * There is no protection for configured devices.
1576 			 */
1577 			rv = sata_ioctl_reset_all(sata_hba_inst);
1578 			/*
1579 			 * We return here, because common return is for
1580 			 * a single port operation - we have already unlocked
1581 			 * all ports and no dc handle was allocated.
1582 			 */
1583 			return (rv);
1584 
1585 		case SATA_CFGA_PORT_DEACTIVATE:
1586 			/*
1587 			 * Arbitrarily unconfigure attached device, if any.
1588 			 * Even if the unconfigure fails, proceed with the
1589 			 * port deactivation.
1590 			 */
1591 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1592 
1593 			break;
1594 
1595 		case SATA_CFGA_PORT_ACTIVATE:
1596 
1597 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1598 			break;
1599 
1600 		case SATA_CFGA_PORT_SELF_TEST:
1601 
1602 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1603 			    &sata_device);
1604 			break;
1605 
1606 		case SATA_CFGA_GET_DEVICE_PATH:
1607 
1608 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1609 			    &sata_device, &ioc, mode);
1610 			break;
1611 
1612 		case SATA_CFGA_GET_AP_TYPE:
1613 
1614 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1615 			    &sata_device, &ioc, mode);
1616 			break;
1617 
1618 		case SATA_CFGA_GET_MODEL_INFO:
1619 
1620 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1621 			    &sata_device, &ioc, mode);
1622 			break;
1623 
1624 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1625 
1626 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1627 			    &sata_device, &ioc, mode);
1628 			break;
1629 
1630 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1631 
1632 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1633 			    &sata_device, &ioc, mode);
1634 			break;
1635 
1636 		default:
1637 			rv = EINVAL;
1638 			break;
1639 
1640 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1641 
1642 		break;
1643 	}
1644 
1645 	default:
1646 	{
1647 		/*
1648 		 * If we got here, we got an IOCTL that SATA HBA Framework
1649 		 * does not recognize. Pass ioctl to HBA driver, in case
1650 		 * it could process it.
1651 		 */
1652 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1653 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1654 
1655 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1656 		    "IOCTL 0x%2x not supported in SATA framework, "
1657 		    "passthrough to HBA", cmd);
1658 
1659 		if (sata_tran->sata_tran_ioctl == NULL) {
1660 			rv = EINVAL;
1661 			break;
1662 		}
1663 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1664 		if (rval != 0) {
1665 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1666 			    "IOCTL 0x%2x failed in HBA", cmd);
1667 			rv = rval;
1668 		}
1669 		break;
1670 	}
1671 
1672 	} /* End of main IOCTL switch */
1673 
1674 	if (dcp) {
1675 		ndi_dc_freehdl(dcp);
1676 	}
1677 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1678 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1679 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1680 
1681 	return (rv);
1682 }
1683 
1684 
1685 /*
1686  * Create error retrieval sata packet
1687  *
1688  * A sata packet is allocated and set-up to contain specified error retrieval
1689  * command and appropriate dma-able data buffer.
1690  * No association with any scsi packet is made and no callback routine is
1691  * specified.
1692  *
1693  * Returns a pointer to sata packet upon successful packet creation.
1694  * Returns NULL, if packet cannot be created.
1695  */
1696 sata_pkt_t *
1697 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1698     int pkt_type)
1699 {
1700 	sata_hba_inst_t	*sata_hba_inst;
1701 	sata_pkt_txlate_t *spx;
1702 	sata_pkt_t *spkt;
1703 	sata_drive_info_t *sdinfo;
1704 
1705 	mutex_enter(&sata_mutex);
1706 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1707 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1708 		if (SATA_DIP(sata_hba_inst) == dip)
1709 			break;
1710 	}
1711 	mutex_exit(&sata_mutex);
1712 	ASSERT(sata_hba_inst != NULL);
1713 
1714 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1715 	if (sdinfo == NULL) {
1716 		sata_log(sata_hba_inst, CE_WARN,
1717 		    "sata: error recovery request for non-attached device at "
1718 		    "cport %d", sata_device->satadev_addr.cport);
1719 		return (NULL);
1720 	}
1721 
1722 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1723 	spx->txlt_sata_hba_inst = sata_hba_inst;
1724 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1725 	spkt = sata_pkt_alloc(spx, NULL);
1726 	if (spkt == NULL) {
1727 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1728 		return (NULL);
1729 	}
1730 	/* address is needed now */
1731 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1732 
1733 	switch (pkt_type) {
1734 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1735 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1736 			if (sata_check_for_dma_error(dip, spx)) {
1737 				ddi_fm_service_impact(dip,
1738 				    DDI_SERVICE_UNAFFECTED);
1739 				break;
1740 			}
1741 			return (spkt);
1742 		}
1743 		break;
1744 
1745 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1746 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1747 			if (sata_check_for_dma_error(dip, spx)) {
1748 				ddi_fm_service_impact(dip,
1749 				    DDI_SERVICE_UNAFFECTED);
1750 				break;
1751 			}
1752 			return (spkt);
1753 		}
1754 		break;
1755 
1756 	default:
1757 		break;
1758 	}
1759 
1760 	sata_pkt_free(spx);
1761 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1762 	return (NULL);
1763 
1764 }
1765 
1766 
1767 /*
1768  * Free error retrieval sata packet
1769  *
1770  * Free sata packet and any associated resources allocated previously by
1771  * sata_get_error_retrieval_pkt().
1772  *
1773  * Void return.
1774  */
1775 void
1776 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1777 {
1778 	sata_pkt_txlate_t *spx =
1779 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1780 
1781 	ASSERT(sata_pkt != NULL);
1782 
1783 	sata_free_local_buffer(spx);
1784 	sata_pkt_free(spx);
1785 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1786 
1787 }
1788 
1789 /*
1790  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1791  *
1792  * No association with any scsi packet is made and no callback routine is
1793  * specified.
1794  *
1795  * Returns a pointer to sata packet upon successful packet creation.
1796  * Returns NULL, if packet cannot be created.
1797  *
1798  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1799  * only lower 32 bits are available currently.
1800  */
1801 sata_pkt_t *
1802 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1803     uint8_t regn, uint32_t regv, uint32_t type)
1804 {
1805 	sata_hba_inst_t	*sata_hba_inst;
1806 	sata_pkt_txlate_t *spx;
1807 	sata_pkt_t *spkt;
1808 	sata_cmd_t *scmd;
1809 
1810 	/* Only READ/WRITE commands are accepted. */
1811 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1812 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1813 
1814 	mutex_enter(&sata_mutex);
1815 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1816 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1817 		if (SATA_DIP(sata_hba_inst) == dip)
1818 			break;
1819 	}
1820 	mutex_exit(&sata_mutex);
1821 	ASSERT(sata_hba_inst != NULL);
1822 
1823 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1824 	spx->txlt_sata_hba_inst = sata_hba_inst;
1825 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1826 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1827 	if (spkt == NULL) {
1828 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1829 		return (NULL);
1830 	}
1831 
1832 	/*
1833 	 * NOTE: We need to send this command to the port multiplier,
1834 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1835 	 *
1836 	 * sata_device contains the address of actual target device, and the
1837 	 * pmport number in the command comes from the sata_device structure.
1838 	 */
1839 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1840 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1841 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1842 
1843 	/* Fill sata_pkt */
1844 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1845 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1846 	spkt->satapkt_time = 10; /* Timeout 10s */
1847 
1848 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1849 	scmd = &spkt->satapkt_cmd;
1850 	scmd->satacmd_features_reg = regn & 0xff;
1851 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1852 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1853 	scmd->satacmd_addr_type = 0;		/* N/A */
1854 
1855 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1856 
1857 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1858 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1859 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1860 		scmd->satacmd_flags.sata_special_regs = 1;
1861 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1862 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1863 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1864 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1865 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1866 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1867 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1868 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1869 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1870 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1871 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1872 	}
1873 
1874 	return (spkt);
1875 }
1876 
1877 /*
1878  * Free sata packet and any associated resources allocated previously by
1879  * sata_get_rdwr_pmult_pkt().
1880  *
1881  * Void return.
1882  */
1883 void
1884 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1885 {
1886 	sata_pkt_txlate_t *spx =
1887 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1888 
1889 	/* Free allocated resources */
1890 	sata_pkt_free(spx);
1891 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1892 }
1893 
1894 /*
1895  * Register a port multiplier to framework.
1896  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1897  * 2) Search in the blacklist and update the number of the device ports of the
1898  * port multiplier.
1899  *
1900  * Void return.
1901  */
1902 void
1903 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1904 {
1905 	sata_hba_inst_t *sata_hba_inst = NULL;
1906 	sata_pmult_info_t *pmultinfo;
1907 	sata_pmult_bl_t *blp;
1908 	int cport = sd->satadev_addr.cport;
1909 
1910 	mutex_enter(&sata_mutex);
1911 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1912 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1913 		if (SATA_DIP(sata_hba_inst) == dip)
1914 			if (sata_hba_inst->satahba_attached == 1)
1915 				break;
1916 	}
1917 	mutex_exit(&sata_mutex);
1918 	/* HBA not attached? */
1919 	if (sata_hba_inst == NULL)
1920 		return;
1921 
1922 	/* Number of pmports */
1923 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1924 
1925 	/* Check the blacklist */
1926 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1927 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1928 			continue;
1929 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1930 			continue;
1931 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1932 			continue;
1933 
1934 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1935 		sd->satadev_add_info = blp->bl_flags;
1936 		break;
1937 	}
1938 
1939 	/* Register the port multiplier GSCR */
1940 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1941 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1942 	if (pmultinfo != NULL) {
1943 		pmultinfo->pmult_gscr = *sg;
1944 		pmultinfo->pmult_num_dev_ports =
1945 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1946 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1947 		    "Port multiplier registered at port %d", cport);
1948 	}
1949 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1950 }
1951 
1952 /*
1953  * sata_name_child is for composing the name of the node
1954  * the format of the name is "target,0".
1955  */
1956 static int
1957 sata_name_child(dev_info_t *dip, char *name, int namelen)
1958 {
1959 	int target;
1960 
1961 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1962 	    DDI_PROP_DONTPASS, "target", -1);
1963 	if (target == -1)
1964 		return (DDI_FAILURE);
1965 	(void) snprintf(name, namelen, "%x,0", target);
1966 	return (DDI_SUCCESS);
1967 }
1968 
1969 
1970 
1971 /* ****************** SCSA required entry points *********************** */
1972 
1973 /*
1974  * Implementation of scsi tran_tgt_init.
1975  * sata_scsi_tgt_init() initializes scsi_device structure
1976  *
1977  * If successful, DDI_SUCCESS is returned.
1978  * DDI_FAILURE is returned if addressed device does not exist
1979  */
1980 
1981 static int
1982 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1983     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1984 {
1985 #ifndef __lock_lint
1986 	_NOTE(ARGUNUSED(hba_dip))
1987 	_NOTE(ARGUNUSED(tgt_dip))
1988 #endif
1989 	sata_device_t		sata_device;
1990 	sata_drive_info_t	*sdinfo;
1991 	struct sata_id		*sid;
1992 	sata_hba_inst_t		*sata_hba_inst;
1993 	char			model[SATA_ID_MODEL_LEN + 1];
1994 	char			fw[SATA_ID_FW_LEN + 1];
1995 	char			*vid, *pid;
1996 	int			i;
1997 
1998 	/*
1999 	 * Fail tran_tgt_init for .conf stub node
2000 	 */
2001 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2002 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2003 		ddi_set_name_addr(tgt_dip, NULL);
2004 		return (DDI_FAILURE);
2005 	}
2006 
2007 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2008 
2009 	/* Validate scsi device address */
2010 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2011 	    &sata_device) != 0)
2012 		return (DDI_FAILURE);
2013 
2014 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2015 	    sata_device.satadev_addr.cport)));
2016 
2017 	/* sata_device now contains a valid sata address */
2018 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2019 	if (sdinfo == NULL) {
2020 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2021 		    sata_device.satadev_addr.cport)));
2022 		return (DDI_FAILURE);
2023 	}
2024 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2025 	    sata_device.satadev_addr.cport)));
2026 
2027 	/*
2028 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2029 	 * the target disks.
2030 	 *
2031 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2032 	 * if we need to create cmdk-style devid for all the disk devices
2033 	 * attached to this controller. This property may have been set
2034 	 * from HBA driver's .conf file or by the HBA driver in its
2035 	 * attach(9F) function.
2036 	 */
2037 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2038 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2039 	    "use-cmdk-devid-format", 0) == 1)) {
2040 		/* register a legacy devid for this target node */
2041 		sata_target_devid_register(tgt_dip, sdinfo);
2042 	}
2043 
2044 
2045 	/*
2046 	 * 'Identify Device Data' does not always fit in standard SCSI
2047 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2048 	 * of information.
2049 	 */
2050 	sid = &sdinfo->satadrv_id;
2051 #ifdef	_LITTLE_ENDIAN
2052 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2053 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2054 #else	/* _LITTLE_ENDIAN */
2055 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2056 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2057 #endif	/* _LITTLE_ENDIAN */
2058 	model[SATA_ID_MODEL_LEN] = 0;
2059 	fw[SATA_ID_FW_LEN] = 0;
2060 
2061 	/* split model into into vid/pid */
2062 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2063 		if ((*pid == ' ') || (*pid == '\t'))
2064 			break;
2065 	if (i < SATA_ID_MODEL_LEN) {
2066 		vid = model;
2067 		*pid++ = 0;		/* terminate vid, establish pid */
2068 	} else {
2069 		vid = NULL;		/* vid will stay "ATA     " */
2070 		pid = model;		/* model is all pid */
2071 	}
2072 
2073 	if (vid)
2074 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2075 		    vid, strlen(vid));
2076 	if (pid)
2077 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2078 		    pid, strlen(pid));
2079 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2080 	    fw, strlen(fw));
2081 
2082 	return (DDI_SUCCESS);
2083 }
2084 
2085 /*
2086  * Implementation of scsi tran_tgt_probe.
2087  * Probe target, by calling default scsi routine scsi_hba_probe()
2088  */
2089 static int
2090 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2091 {
2092 	sata_hba_inst_t *sata_hba_inst =
2093 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2094 	int rval;
2095 	uint32_t pm_cap;
2096 
2097 	rval = scsi_hba_probe(sd, callback);
2098 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2099 	    SATA_CAP_LOG_SENSE;
2100 
2101 	if (rval == SCSIPROBE_EXISTS) {
2102 		/*
2103 		 * Set property "pm-capable" on the target device node, so that
2104 		 * the target driver will not try to fetch scsi cycle counters
2105 		 * before enabling device power-management.
2106 		 */
2107 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2108 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2109 			sata_log(sata_hba_inst, CE_WARN,
2110 			    "SATA device at port %d: "
2111 			    "will not be power-managed ",
2112 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2113 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2114 			    "failure updating pm-capable property"));
2115 		}
2116 	}
2117 	return (rval);
2118 }
2119 
2120 /*
2121  * Implementation of scsi tran_tgt_free.
2122  * Release all resources allocated for scsi_device
2123  */
2124 static void
2125 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2126     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2127 {
2128 #ifndef __lock_lint
2129 	_NOTE(ARGUNUSED(hba_dip))
2130 #endif
2131 	sata_device_t		sata_device;
2132 	sata_drive_info_t	*sdinfo;
2133 	sata_hba_inst_t		*sata_hba_inst;
2134 	ddi_devid_t		devid;
2135 
2136 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2137 
2138 	/* Validate scsi device address */
2139 	/*
2140 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2141 	 * was a device at this address, so even if the sata framework internal
2142 	 * resources were alredy released because a device was detached,
2143 	 * this function should be executed as long as its actions do
2144 	 * not require the internal sata view of a device and the address
2145 	 * refers to a valid sata address.
2146 	 * Validating the address here means that we do not trust SCSA...
2147 	 */
2148 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2149 	    &sata_device) == -1)
2150 		return;
2151 
2152 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2153 	    sata_device.satadev_addr.cport)));
2154 
2155 	/* sata_device now should contain a valid sata address */
2156 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2157 	if (sdinfo == NULL) {
2158 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2159 		    sata_device.satadev_addr.cport)));
2160 		return;
2161 	}
2162 	/*
2163 	 * We did not allocate any resources in sata_scsi_tgt_init()
2164 	 * other than few properties.
2165 	 * Free them.
2166 	 */
2167 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2168 	    sata_device.satadev_addr.cport)));
2169 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2170 
2171 	/*
2172 	 * If devid was previously created but not freed up from
2173 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2174 	 */
2175 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2176 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2177 	    "use-cmdk-devid-format", 0) == 1) &&
2178 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2179 		ddi_devid_unregister(tgt_dip);
2180 		ddi_devid_free(devid);
2181 	}
2182 }
2183 
2184 /*
2185  * Implementation of scsi tran_init_pkt
2186  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2187  *
2188  * It seems that we should always allocate pkt, even if the address is
2189  * for non-existing device - just use some default for dma_attr.
2190  * The reason is that there is no way to communicate this to a caller here.
2191  * Subsequent call to sata_scsi_start may fail appropriately.
2192  * Simply returning NULL does not seem to discourage a target driver...
2193  *
2194  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2195  */
2196 static struct scsi_pkt *
2197 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2198     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2199     int (*callback)(caddr_t), caddr_t arg)
2200 {
2201 	sata_hba_inst_t *sata_hba_inst =
2202 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2203 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2204 	sata_device_t sata_device;
2205 	sata_drive_info_t *sdinfo;
2206 	sata_pkt_txlate_t *spx;
2207 	ddi_dma_attr_t cur_dma_attr;
2208 	int rval;
2209 	boolean_t new_pkt = B_TRUE;
2210 
2211 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2212 
2213 	/*
2214 	 * We need to translate the address, even if it could be
2215 	 * a bogus one, for a non-existing device
2216 	 */
2217 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2218 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2219 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2220 	sata_device.satadev_rev = SATA_DEVICE_REV;
2221 
2222 	if (pkt == NULL) {
2223 		/*
2224 		 * Have to allocate a brand new scsi packet.
2225 		 * We need to operate with auto request sense enabled.
2226 		 */
2227 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2228 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2229 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2230 
2231 		if (pkt == NULL)
2232 			return (NULL);
2233 
2234 		/* Fill scsi packet structure */
2235 		pkt->pkt_comp		= (void (*)())NULL;
2236 		pkt->pkt_time		= 0;
2237 		pkt->pkt_resid		= 0;
2238 		pkt->pkt_statistics	= 0;
2239 		pkt->pkt_reason		= 0;
2240 
2241 		/*
2242 		 * pkt_hba_private will point to sata pkt txlate structure
2243 		 */
2244 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2245 		bzero(spx, sizeof (sata_pkt_txlate_t));
2246 
2247 		spx->txlt_scsi_pkt = pkt;
2248 		spx->txlt_sata_hba_inst = sata_hba_inst;
2249 
2250 		/* Allocate sata_pkt */
2251 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2252 		if (spx->txlt_sata_pkt == NULL) {
2253 			/* Could not allocate sata pkt */
2254 			scsi_hba_pkt_free(ap, pkt);
2255 			return (NULL);
2256 		}
2257 		/* Set sata address */
2258 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2259 		    sata_device.satadev_addr;
2260 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2261 		    sata_device.satadev_rev;
2262 
2263 		if ((bp == NULL) || (bp->b_bcount == 0))
2264 			return (pkt);
2265 
2266 		spx->txlt_total_residue = bp->b_bcount;
2267 	} else {
2268 		new_pkt = B_FALSE;
2269 		/*
2270 		 * Packet was preallocated/initialized by previous call
2271 		 */
2272 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2273 
2274 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2275 			return (pkt);
2276 		}
2277 
2278 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2279 	}
2280 
2281 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2282 
2283 	/*
2284 	 * We use an adjusted version of the dma_attr, to account
2285 	 * for device addressing limitations.
2286 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2287 	 * happen when a device is not yet configured.
2288 	 */
2289 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2290 	    sata_device.satadev_addr.cport)));
2291 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2292 	    &spx->txlt_sata_pkt->satapkt_device);
2293 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2294 	sata_adjust_dma_attr(sdinfo,
2295 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2296 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2297 	    sata_device.satadev_addr.cport)));
2298 	/*
2299 	 * Allocate necessary DMA resources for the packet's data buffer
2300 	 * NOTE:
2301 	 * In case of read/write commands, DMA resource allocation here is
2302 	 * based on the premise that the transfer length specified in
2303 	 * the read/write scsi cdb will match exactly DMA resources -
2304 	 * returning correct packet residue is crucial.
2305 	 */
2306 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2307 	    &cur_dma_attr)) != DDI_SUCCESS) {
2308 		/*
2309 		 * If a DMA allocation request fails with
2310 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2311 		 * bioerror(9F) with bp and an error code of EFAULT.
2312 		 * If a DMA allocation request fails with
2313 		 * DDI_DMA_TOOBIG, indicate the error by calling
2314 		 * bioerror(9F) with bp and an error code of EINVAL.
2315 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2316 		 * Request may be repeated later - there is no real error.
2317 		 */
2318 		switch (rval) {
2319 		case DDI_DMA_NORESOURCES:
2320 			bioerror(bp, 0);
2321 			break;
2322 		case DDI_DMA_NOMAPPING:
2323 		case DDI_DMA_BADATTR:
2324 			bioerror(bp, EFAULT);
2325 			break;
2326 		case DDI_DMA_TOOBIG:
2327 		default:
2328 			bioerror(bp, EINVAL);
2329 			break;
2330 		}
2331 		goto fail;
2332 	}
2333 
2334 	if (sata_check_for_dma_error(dip, spx)) {
2335 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2336 		bioerror(bp, EFAULT);
2337 		goto fail;
2338 	}
2339 
2340 success:
2341 	/* Set number of bytes that are not yet accounted for */
2342 	pkt->pkt_resid = spx->txlt_total_residue;
2343 	ASSERT(pkt->pkt_resid >= 0);
2344 
2345 	return (pkt);
2346 
2347 fail:
2348 	if (new_pkt == B_TRUE) {
2349 		/*
2350 		 * Since this is a new packet, we can clean-up
2351 		 * everything
2352 		 */
2353 		sata_scsi_destroy_pkt(ap, pkt);
2354 	} else {
2355 		/*
2356 		 * This is a re-used packet. It will be target driver's
2357 		 * responsibility to eventually destroy it (which
2358 		 * will free allocated resources).
2359 		 * Here, we just "complete" the request, leaving
2360 		 * allocated resources intact, so the request may
2361 		 * be retried.
2362 		 */
2363 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2364 		sata_pkt_free(spx);
2365 	}
2366 	return (NULL);
2367 }
2368 
2369 /*
2370  * Implementation of scsi tran_start.
2371  * Translate scsi cmd into sata operation and return status.
2372  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2373  * are supported.
2374  * For SATA hard disks, supported scsi commands:
2375  * SCMD_INQUIRY
2376  * SCMD_TEST_UNIT_READY
2377  * SCMD_START_STOP
2378  * SCMD_READ_CAPACITY
2379  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2380  * SCMD_REQUEST_SENSE
2381  * SCMD_LOG_SENSE_G1
2382  * SCMD_LOG_SELECT_G1
2383  * SCMD_MODE_SENSE	(specific pages)
2384  * SCMD_MODE_SENSE_G1	(specific pages)
2385  * SCMD_MODE_SELECT	(specific pages)
2386  * SCMD_MODE_SELECT_G1	(specific pages)
2387  * SCMD_SYNCHRONIZE_CACHE
2388  * SCMD_SYNCHRONIZE_CACHE_G1
2389  * SCMD_READ
2390  * SCMD_READ_G1
2391  * SCMD_READ_G4
2392  * SCMD_READ_G5
2393  * SCMD_WRITE
2394  * SCMD_WRITE_BUFFER
2395  * SCMD_WRITE_G1
2396  * SCMD_WRITE_G4
2397  * SCMD_WRITE_G5
2398  * SCMD_SEEK		(noop)
2399  * SCMD_SDIAG
2400  *
2401  * All other commands are rejected as unsupported.
2402  *
2403  * Returns:
2404  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2405  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2406  * a callback could be scheduled.
2407  * TRAN_BADPKT if cmd was directed to invalid address.
2408  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2409  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2410  * was removed and there was no callback specified in scsi pkt.
2411  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2412  * framework was busy performing some other operation(s).
2413  *
2414  */
2415 static int
2416 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2417 {
2418 	sata_hba_inst_t *sata_hba_inst =
2419 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2420 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2421 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2422 	sata_drive_info_t *sdinfo;
2423 	struct buf *bp;
2424 	uint8_t cport, pmport;
2425 	boolean_t dev_gone = B_FALSE;
2426 	int rval;
2427 
2428 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2429 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2430 
2431 	ASSERT(spx != NULL &&
2432 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2433 
2434 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2435 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2436 
2437 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2438 
2439 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2440 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2441 		if (sdinfo == NULL ||
2442 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2443 		    cport_tgtnode_clean == B_FALSE ||
2444 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2445 			dev_gone = B_TRUE;
2446 		}
2447 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2448 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2449 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2450 		    cport) == NULL) {
2451 			dev_gone = B_TRUE;
2452 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2453 		    pmport) == NULL) {
2454 			dev_gone = B_TRUE;
2455 		} else {
2456 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2457 			    cport, pmport)));
2458 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2459 			if (sdinfo == NULL ||
2460 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2461 			    pmport_tgtnode_clean == B_FALSE ||
2462 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2463 				dev_gone = B_TRUE;
2464 			}
2465 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2466 			    cport, pmport)));
2467 		}
2468 	}
2469 
2470 	if (dev_gone == B_TRUE) {
2471 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2472 		pkt->pkt_reason = CMD_DEV_GONE;
2473 		/*
2474 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2475 		 * only in callback function (for normal requests) and
2476 		 * in the dump code path.
2477 		 * So, if the callback is available, we need to do
2478 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2479 		 */
2480 		if (pkt->pkt_comp != NULL) {
2481 			/* scsi callback required */
2482 			if (servicing_interrupt()) {
2483 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2484 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2485 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2486 				    NULL) {
2487 					return (TRAN_BUSY);
2488 				}
2489 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2490 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2491 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2492 				/* Scheduling the callback failed */
2493 				return (TRAN_BUSY);
2494 			}
2495 			return (TRAN_ACCEPT);
2496 		}
2497 		/* No callback available */
2498 		return (TRAN_FATAL_ERROR);
2499 	}
2500 
2501 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2502 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2503 		rval = sata_txlt_atapi(spx);
2504 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2505 		    "sata_scsi_start atapi: rval %d\n", rval);
2506 		return (rval);
2507 	}
2508 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2509 
2510 	/*
2511 	 * Checking for power state, if it was on
2512 	 * STOPPED state, then the drive is not capable
2513 	 * of processing media access command.  And
2514 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2515 	 * in the function for different power state.
2516 	 */
2517 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2518 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2519 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2520 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2521 		    SD_SCSI_ASC_LU_NOT_READY));
2522 	}
2523 
2524 	/* ATA Disk commands processing starts here */
2525 
2526 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2527 
2528 	switch (pkt->pkt_cdbp[0]) {
2529 
2530 	case SCMD_INQUIRY:
2531 		/* Mapped to identify device */
2532 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2533 			bp_mapin(bp);
2534 		rval = sata_txlt_inquiry(spx);
2535 		break;
2536 
2537 	case SCMD_TEST_UNIT_READY:
2538 		/*
2539 		 * SAT "SATA to ATA Translation" doc specifies translation
2540 		 * to ATA CHECK POWER MODE.
2541 		 */
2542 		rval = sata_txlt_test_unit_ready(spx);
2543 		break;
2544 
2545 	case SCMD_START_STOP:
2546 		/* Mapping depends on the command */
2547 		rval = sata_txlt_start_stop_unit(spx);
2548 		break;
2549 
2550 	case SCMD_READ_CAPACITY:
2551 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2552 			bp_mapin(bp);
2553 		rval = sata_txlt_read_capacity(spx);
2554 		break;
2555 
2556 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2557 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2558 			bp_mapin(bp);
2559 		rval = sata_txlt_read_capacity16(spx);
2560 		break;
2561 
2562 	case SCMD_REQUEST_SENSE:
2563 		/*
2564 		 * Always No Sense, since we force ARQ
2565 		 */
2566 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2567 			bp_mapin(bp);
2568 		rval = sata_txlt_request_sense(spx);
2569 		break;
2570 
2571 	case SCMD_LOG_SENSE_G1:
2572 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2573 			bp_mapin(bp);
2574 		rval = sata_txlt_log_sense(spx);
2575 		break;
2576 
2577 	case SCMD_LOG_SELECT_G1:
2578 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2579 			bp_mapin(bp);
2580 		rval = sata_txlt_log_select(spx);
2581 		break;
2582 
2583 	case SCMD_MODE_SENSE:
2584 	case SCMD_MODE_SENSE_G1:
2585 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2586 			bp_mapin(bp);
2587 		rval = sata_txlt_mode_sense(spx);
2588 		break;
2589 
2590 
2591 	case SCMD_MODE_SELECT:
2592 	case SCMD_MODE_SELECT_G1:
2593 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2594 			bp_mapin(bp);
2595 		rval = sata_txlt_mode_select(spx);
2596 		break;
2597 
2598 	case SCMD_SYNCHRONIZE_CACHE:
2599 	case SCMD_SYNCHRONIZE_CACHE_G1:
2600 		rval = sata_txlt_synchronize_cache(spx);
2601 		break;
2602 
2603 	case SCMD_READ:
2604 	case SCMD_READ_G1:
2605 	case SCMD_READ_G4:
2606 	case SCMD_READ_G5:
2607 		rval = sata_txlt_read(spx);
2608 		break;
2609 	case SCMD_WRITE_BUFFER:
2610 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2611 			bp_mapin(bp);
2612 		rval = sata_txlt_write_buffer(spx);
2613 		break;
2614 
2615 	case SCMD_WRITE:
2616 	case SCMD_WRITE_G1:
2617 	case SCMD_WRITE_G4:
2618 	case SCMD_WRITE_G5:
2619 		rval = sata_txlt_write(spx);
2620 		break;
2621 
2622 	case SCMD_SEEK:
2623 		rval = sata_txlt_nodata_cmd_immediate(spx);
2624 		break;
2625 
2626 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2627 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2628 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2629 			bp_mapin(bp);
2630 		rval = sata_txlt_ata_pass_thru(spx);
2631 		break;
2632 
2633 		/* Other cases will be filed later */
2634 		/* postponed until phase 2 of the development */
2635 	case SPC3_CMD_UNMAP:
2636 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2637 			bp_mapin(bp);
2638 		rval = sata_txlt_unmap(spx);
2639 		break;
2640 	default:
2641 		rval = sata_txlt_invalid_command(spx);
2642 		break;
2643 	}
2644 
2645 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2646 	    "sata_scsi_start: rval %d\n", rval);
2647 
2648 	return (rval);
2649 }
2650 
2651 /*
2652  * Implementation of scsi tran_abort.
2653  * Abort specific pkt or all packets.
2654  *
2655  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2656  *
2657  * May be called from an interrupt level.
2658  */
2659 static int
2660 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2661 {
2662 	sata_hba_inst_t *sata_hba_inst =
2663 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2664 	sata_device_t	sata_device;
2665 	sata_pkt_t	*sata_pkt;
2666 
2667 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2668 	    "sata_scsi_abort: %s at target: 0x%x\n",
2669 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2670 
2671 	/* Validate address */
2672 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2673 		/* Invalid address */
2674 		return (0);
2675 
2676 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2677 	    sata_device.satadev_addr.cport)));
2678 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2679 		/* invalid address */
2680 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2681 		    sata_device.satadev_addr.cport)));
2682 		return (0);
2683 	}
2684 	if (scsi_pkt == NULL) {
2685 		/*
2686 		 * Abort all packets.
2687 		 * Although we do not have specific packet, we still need
2688 		 * dummy packet structure to pass device address to HBA.
2689 		 * Allocate one, without sleeping. Fail if pkt cannot be
2690 		 * allocated.
2691 		 */
2692 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2693 		if (sata_pkt == NULL) {
2694 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2695 			    sata_device.satadev_addr.cport)));
2696 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2697 			    "could not allocate sata_pkt"));
2698 			return (0);
2699 		}
2700 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2701 		sata_pkt->satapkt_device = sata_device;
2702 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2703 	} else {
2704 		if (scsi_pkt->pkt_ha_private == NULL) {
2705 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2706 			    sata_device.satadev_addr.cport)));
2707 			return (0); /* Bad scsi pkt */
2708 		}
2709 		/* extract pointer to sata pkt */
2710 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2711 		    txlt_sata_pkt;
2712 	}
2713 
2714 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2715 	    sata_device.satadev_addr.cport)));
2716 	/* Send abort request to HBA */
2717 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2718 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2719 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2720 	    SATA_SUCCESS) {
2721 		if (scsi_pkt == NULL)
2722 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2723 		/* Success */
2724 		return (1);
2725 	}
2726 	/* Else, something did not go right */
2727 	if (scsi_pkt == NULL)
2728 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2729 	/* Failure */
2730 	return (0);
2731 }
2732 
2733 
2734 /*
2735  * Implementation of scsi tran_reset.
2736  * RESET_ALL request is translated into port reset.
2737  * RESET_TARGET requests is translated into a device reset,
2738  * RESET_LUN request is accepted only for LUN 0 and translated into
2739  * device reset.
2740  * The target reset should cause all HBA active and queued packets to
2741  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2742  * the return. HBA should report reset event for the device.
2743  *
2744  * Returns 1 upon success, 0 upon failure.
2745  */
2746 static int
2747 sata_scsi_reset(struct scsi_address *ap, int level)
2748 {
2749 	sata_hba_inst_t	*sata_hba_inst =
2750 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2751 	sata_device_t	sata_device;
2752 	int		val;
2753 
2754 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2755 	    "sata_scsi_reset: level %d target: 0x%x\n",
2756 	    level, ap->a_target);
2757 
2758 	/* Validate address */
2759 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2760 	if (val == -1)
2761 		/* Invalid address */
2762 		return (0);
2763 
2764 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2765 	    sata_device.satadev_addr.cport)));
2766 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2767 		/* invalid address */
2768 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2769 		    sata_device.satadev_addr.cport)));
2770 		return (0);
2771 	}
2772 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2773 	    sata_device.satadev_addr.cport)));
2774 	if (level == RESET_ALL) {
2775 		/* port reset */
2776 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2777 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2778 		else
2779 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2780 
2781 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2782 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2783 			return (1);
2784 		else
2785 			return (0);
2786 
2787 	} else if (val == 0 &&
2788 	    (level == RESET_TARGET || level == RESET_LUN)) {
2789 		/* reset device (device attached) */
2790 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2791 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2792 			return (1);
2793 		else
2794 			return (0);
2795 	}
2796 	return (0);
2797 }
2798 
2799 
2800 /*
2801  * Implementation of scsi tran_getcap (get transport/device capabilities).
2802  * Supported capabilities for SATA hard disks:
2803  * auto-rqsense		(always supported)
2804  * tagged-qing		(supported if HBA supports it)
2805  * untagged-qing	(could be supported if disk supports it, but because
2806  *			 caching behavior allowing untagged queuing actually
2807  *			 results in reduced performance.  sd tries to throttle
2808  *			 back to only 3 outstanding commands, which may
2809  *			 work for real SCSI disks, but with read ahead
2810  *			 caching, having more than 1 outstanding command
2811  *			 results in cache thrashing.)
2812  * sector_size
2813  * dma_max
2814  * interconnect-type	(INTERCONNECT_SATA)
2815  *
2816  * Supported capabilities for ATAPI CD/DVD devices:
2817  * auto-rqsense		(always supported)
2818  * sector_size
2819  * dma_max
2820  * max-cdb-length
2821  * interconnect-type	(INTERCONNECT_SATA)
2822  *
2823  * Supported capabilities for ATAPI TAPE devices:
2824  * auto-rqsense		(always supported)
2825  * dma_max
2826  * max-cdb-length
2827  *
2828  * Supported capabilities for SATA ATAPI hard disks:
2829  * auto-rqsense		(always supported)
2830  * interconnect-type	(INTERCONNECT_SATA)
2831  * max-cdb-length
2832  *
2833  * Request for other capabilities is rejected as unsupported.
2834  *
2835  * Returns supported capability value, or -1 if capability is unsuppported or
2836  * the address is invalid - no device.
2837  */
2838 
2839 static int
2840 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2841 {
2842 
2843 	sata_hba_inst_t 	*sata_hba_inst =
2844 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2845 	sata_device_t		sata_device;
2846 	sata_drive_info_t	*sdinfo;
2847 	ddi_dma_attr_t		adj_dma_attr;
2848 	int 			rval;
2849 
2850 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2851 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2852 	    ap->a_target, cap);
2853 
2854 	/*
2855 	 * We want to process the capabilities on per port granularity.
2856 	 * So, we are specifically restricting ourselves to whom != 0
2857 	 * to exclude the controller wide handling.
2858 	 */
2859 	if (cap == NULL || whom == 0)
2860 		return (-1);
2861 
2862 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2863 		/* Invalid address */
2864 		return (-1);
2865 	}
2866 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2867 	    sata_device.satadev_addr.cport)));
2868 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2869 	    NULL) {
2870 		/* invalid address */
2871 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2872 		    sata_device.satadev_addr.cport)));
2873 		return (-1);
2874 	}
2875 
2876 	switch (scsi_hba_lookup_capstr(cap)) {
2877 	case SCSI_CAP_ARQ:
2878 		rval = 1;		/* ARQ supported, turned on */
2879 		break;
2880 
2881 	case SCSI_CAP_SECTOR_SIZE:
2882 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2883 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2884 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2885 			rval = SATA_ATAPI_SECTOR_SIZE;
2886 		else rval = -1;
2887 		break;
2888 
2889 	/*
2890 	 * untagged queuing cause a performance inversion because of
2891 	 * the way sd operates.  Because of this reason we do not
2892 	 * use it when available.
2893 	 */
2894 	case SCSI_CAP_UNTAGGED_QING:
2895 		if (sdinfo->satadrv_features_enabled &
2896 		    SATA_DEV_F_E_UNTAGGED_QING)
2897 			rval = 1;	/* Untagged queuing available */
2898 		else
2899 			rval = -1;	/* Untagged queuing not available */
2900 		break;
2901 
2902 	case SCSI_CAP_TAGGED_QING:
2903 		if ((sdinfo->satadrv_features_enabled &
2904 		    SATA_DEV_F_E_TAGGED_QING) &&
2905 		    (sdinfo->satadrv_max_queue_depth > 1))
2906 			rval = 1;	/* Tagged queuing available */
2907 		else
2908 			rval = -1;	/* Tagged queuing not available */
2909 		break;
2910 
2911 	case SCSI_CAP_DMA_MAX:
2912 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2913 		    &adj_dma_attr);
2914 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2915 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2916 		break;
2917 
2918 	case SCSI_CAP_INTERCONNECT_TYPE:
2919 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2920 		break;
2921 
2922 	case SCSI_CAP_CDB_LEN:
2923 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2924 			rval = sdinfo->satadrv_atapi_cdb_len;
2925 		else
2926 			rval = -1;
2927 		break;
2928 
2929 	default:
2930 		rval = -1;
2931 		break;
2932 	}
2933 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2934 	    sata_device.satadev_addr.cport)));
2935 	return (rval);
2936 }
2937 
2938 /*
2939  * Implementation of scsi tran_setcap
2940  *
2941  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2942  *
2943  */
2944 static int
2945 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2946 {
2947 	sata_hba_inst_t	*sata_hba_inst =
2948 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2949 	sata_device_t	sata_device;
2950 	sata_drive_info_t	*sdinfo;
2951 	int		rval;
2952 
2953 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2954 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2955 
2956 	/*
2957 	 * We want to process the capabilities on per port granularity.
2958 	 * So, we are specifically restricting ourselves to whom != 0
2959 	 * to exclude the controller wide handling.
2960 	 */
2961 	if (cap == NULL || whom == 0) {
2962 		return (-1);
2963 	}
2964 
2965 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2966 		/* Invalid address */
2967 		return (-1);
2968 	}
2969 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2970 	    sata_device.satadev_addr.cport)));
2971 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2972 	    &sata_device)) == NULL) {
2973 		/* invalid address */
2974 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2975 		    sata_device.satadev_addr.cport)));
2976 		return (-1);
2977 	}
2978 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2979 	    sata_device.satadev_addr.cport)));
2980 
2981 	switch (scsi_hba_lookup_capstr(cap)) {
2982 	case SCSI_CAP_ARQ:
2983 	case SCSI_CAP_SECTOR_SIZE:
2984 	case SCSI_CAP_DMA_MAX:
2985 	case SCSI_CAP_INTERCONNECT_TYPE:
2986 		rval = 0;
2987 		break;
2988 	case SCSI_CAP_UNTAGGED_QING:
2989 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2990 			rval = 1;
2991 			if (value == 1) {
2992 				sdinfo->satadrv_features_enabled |=
2993 				    SATA_DEV_F_E_UNTAGGED_QING;
2994 			} else if (value == 0) {
2995 				sdinfo->satadrv_features_enabled &=
2996 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2997 			} else {
2998 				rval = -1;
2999 			}
3000 		} else {
3001 			rval = 0;
3002 		}
3003 		break;
3004 	case SCSI_CAP_TAGGED_QING:
3005 		/* This can TCQ or NCQ */
3006 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3007 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3008 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3009 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3010 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3011 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3012 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3013 			rval = 1;
3014 			if (value == 1) {
3015 				sdinfo->satadrv_features_enabled |=
3016 				    SATA_DEV_F_E_TAGGED_QING;
3017 			} else if (value == 0) {
3018 				sdinfo->satadrv_features_enabled &=
3019 				    ~SATA_DEV_F_E_TAGGED_QING;
3020 			} else {
3021 				rval = -1;
3022 			}
3023 		} else {
3024 			rval = 0;
3025 		}
3026 		break;
3027 	default:
3028 		rval = -1;
3029 		break;
3030 	}
3031 	return (rval);
3032 }
3033 
3034 /*
3035  * Implementations of scsi tran_destroy_pkt.
3036  * Free resources allocated by sata_scsi_init_pkt()
3037  */
3038 static void
3039 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3040 {
3041 	sata_pkt_txlate_t *spx;
3042 
3043 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3044 
3045 	sata_common_free_dma_rsrcs(spx);
3046 
3047 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3048 	sata_pkt_free(spx);
3049 
3050 	scsi_hba_pkt_free(ap, pkt);
3051 }
3052 
3053 /*
3054  * Implementation of scsi tran_dmafree.
3055  * Free DMA resources allocated by sata_scsi_init_pkt()
3056  */
3057 
3058 static void
3059 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3060 {
3061 #ifndef __lock_lint
3062 	_NOTE(ARGUNUSED(ap))
3063 #endif
3064 	sata_pkt_txlate_t *spx;
3065 
3066 	ASSERT(pkt != NULL);
3067 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3068 
3069 	sata_common_free_dma_rsrcs(spx);
3070 }
3071 
3072 /*
3073  * Implementation of scsi tran_sync_pkt.
3074  *
3075  * The assumption below is that pkt is unique - there is no need to check ap
3076  *
3077  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3078  * into/from the real buffer.
3079  */
3080 static void
3081 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3082 {
3083 #ifndef __lock_lint
3084 	_NOTE(ARGUNUSED(ap))
3085 #endif
3086 	int rval;
3087 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3088 	struct buf *bp;
3089 	int direction;
3090 
3091 	ASSERT(spx != NULL);
3092 	if (spx->txlt_buf_dma_handle != NULL) {
3093 		direction = spx->txlt_sata_pkt->
3094 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3095 		if (spx->txlt_sata_pkt != NULL &&
3096 		    direction != SATA_DIR_NODATA_XFER) {
3097 			if (spx->txlt_tmp_buf != NULL) {
3098 				/* Intermediate DMA buffer used */
3099 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3100 
3101 				if (direction & SATA_DIR_WRITE) {
3102 					bcopy(bp->b_un.b_addr,
3103 					    spx->txlt_tmp_buf, bp->b_bcount);
3104 				}
3105 			}
3106 			/* Sync the buffer for device or for CPU */
3107 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3108 			    (direction & SATA_DIR_WRITE) ?
3109 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3110 			ASSERT(rval == DDI_SUCCESS);
3111 			if (spx->txlt_tmp_buf != NULL &&
3112 			    !(direction & SATA_DIR_WRITE)) {
3113 				/* Intermediate DMA buffer used for read */
3114 				bcopy(spx->txlt_tmp_buf,
3115 				    bp->b_un.b_addr, bp->b_bcount);
3116 			}
3117 
3118 		}
3119 	}
3120 }
3121 
3122 
3123 
3124 /* *******************  SATA - SCSI Translation functions **************** */
3125 /*
3126  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3127  * translation.
3128  */
3129 
3130 /*
3131  * Checks if a device exists and can be access and translates common
3132  * scsi_pkt data to sata_pkt data.
3133  *
3134  * Flag argument indicates that a non-read/write ATA command may be sent
3135  * to HBA in arbitrary SYNC mode to execute this packet.
3136  *
3137  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3138  * sata_pkt was set-up.
3139  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3140  * exist and pkt_comp callback was scheduled.
3141  * Returns other TRAN_XXXXX values when error occured and command should be
3142  * rejected with the returned TRAN_XXXXX value.
3143  *
3144  * This function should be called with port mutex held.
3145  */
3146 static int
3147 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3148 {
3149 	sata_drive_info_t *sdinfo;
3150 	sata_device_t sata_device;
3151 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3152 		SATA_DIR_NODATA_XFER,
3153 		/* all other values to 0/FALSE */
3154 	};
3155 	/*
3156 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3157 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3158 	 * indicates that the scsi packet was not accepted (the reason will not
3159 	 * be checked by the scsi target driver).
3160 	 * To make debugging easier, we set pkt_reason to know value here.
3161 	 * It may be changed later when different completion reason is
3162 	 * determined.
3163 	 */
3164 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3165 	*reason = CMD_TRAN_ERR;
3166 
3167 	/* Validate address */
3168 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3169 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3170 
3171 	case -1:
3172 		/* Invalid address or invalid device type */
3173 		return (TRAN_BADPKT);
3174 	case 2:
3175 		/*
3176 		 * Valid address but device type is unknown - Chack if it is
3177 		 * in the reset state and therefore in an indeterminate state.
3178 		 */
3179 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3180 		    &spx->txlt_sata_pkt->satapkt_device);
3181 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3182 		    (SATA_EVNT_DEVICE_RESET |
3183 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3184 			if (!ddi_in_panic()) {
3185 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3186 				*reason = CMD_INCOMPLETE;
3187 				SATADBG1(SATA_DBG_SCSI_IF,
3188 				    spx->txlt_sata_hba_inst,
3189 				    "sata_scsi_start: rejecting command "
3190 				    "because of device reset state\n", NULL);
3191 				return (TRAN_BUSY);
3192 			}
3193 		}
3194 		/* FALLTHROUGH */
3195 	case 1:
3196 		/* valid address but no valid device - it has disappeared */
3197 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3198 		*reason = CMD_DEV_GONE;
3199 		/*
3200 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3201 		 * only in callback function (for normal requests) and
3202 		 * in the dump code path.
3203 		 * So, if the callback is available, we need to do
3204 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3205 		 */
3206 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3207 			/* scsi callback required */
3208 			if (servicing_interrupt()) {
3209 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3210 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3211 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3212 				    NULL) {
3213 					return (TRAN_BUSY);
3214 				}
3215 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3216 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3217 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3218 				/* Scheduling the callback failed */
3219 				return (TRAN_BUSY);
3220 			}
3221 
3222 			return (TRAN_ACCEPT);
3223 		}
3224 		return (TRAN_FATAL_ERROR);
3225 	default:
3226 		/* all OK; pkt reason will be overwritten later */
3227 		break;
3228 	}
3229 	/*
3230 	 * If pkt is to be executed in polling mode and a command will not be
3231 	 * emulated in SATA module (requires sending a non-read/write ATA
3232 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3233 	 * interrupt context and not in the panic dump, then reject the packet
3234 	 * to avoid a possible interrupt stack overrun or hang caused by
3235 	 * a potentially blocked interrupt.
3236 	 */
3237 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3238 	    servicing_interrupt() && !ddi_in_panic()) {
3239 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3240 		    "sata_scsi_start: rejecting synchronous command because "
3241 		    "of interrupt context\n", NULL);
3242 		return (TRAN_BUSY);
3243 	}
3244 
3245 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3246 	    &spx->txlt_sata_pkt->satapkt_device);
3247 
3248 	/*
3249 	 * If device is in reset condition, reject the packet with
3250 	 * TRAN_BUSY, unless:
3251 	 * 1. system is panicking (dumping)
3252 	 * In such case only one thread is running and there is no way to
3253 	 * process reset.
3254 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3255 	 * Some cfgadm operations involve drive commands, so reset condition
3256 	 * needs to be ignored for IOCTL operations.
3257 	 */
3258 	if ((sdinfo->satadrv_event_flags &
3259 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3260 
3261 		if (!ddi_in_panic() &&
3262 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3263 		    sata_device.satadev_addr.cport) &
3264 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3265 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3266 			*reason = CMD_INCOMPLETE;
3267 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3268 			    "sata_scsi_start: rejecting command because "
3269 			    "of device reset state\n", NULL);
3270 			return (TRAN_BUSY);
3271 		}
3272 	}
3273 
3274 	/*
3275 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3276 	 * sata_scsi_pkt_init() because pkt init had to work also with
3277 	 * non-existing devices.
3278 	 * Now we know that the packet was set-up for a real device, so its
3279 	 * type is known.
3280 	 */
3281 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3282 
3283 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3284 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3285 	    sata_device.satadev_addr.cport)->cport_event_flags &
3286 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3287 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3288 		    sata_ignore_dev_reset = B_TRUE;
3289 	}
3290 	/*
3291 	 * At this point the generic translation routine determined that the
3292 	 * scsi packet should be accepted. Packet completion reason may be
3293 	 * changed later when a different completion reason is determined.
3294 	 */
3295 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3296 	*reason = CMD_CMPLT;
3297 
3298 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3299 		/* Synchronous execution */
3300 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3301 		    SATA_OPMODE_POLLING;
3302 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3303 		    sata_ignore_dev_reset = ddi_in_panic();
3304 	} else {
3305 		/* Asynchronous execution */
3306 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3307 		    SATA_OPMODE_INTERRUPTS;
3308 	}
3309 	/* Convert queuing information */
3310 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3311 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3312 		    B_TRUE;
3313 	else if (spx->txlt_scsi_pkt->pkt_flags &
3314 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3315 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3316 		    B_TRUE;
3317 
3318 	/* Always limit pkt time */
3319 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3320 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3321 	else
3322 		/* Pass on scsi_pkt time */
3323 		spx->txlt_sata_pkt->satapkt_time =
3324 		    spx->txlt_scsi_pkt->pkt_time;
3325 
3326 	return (TRAN_ACCEPT);
3327 }
3328 
3329 
3330 /*
3331  * Translate ATA Identify Device data to SCSI Inquiry data.
3332  * This function may be called only for ATA devices.
3333  * This function should not be called for ATAPI devices - they
3334  * respond directly to SCSI Inquiry command.
3335  *
3336  * SATA Identify Device data has to be valid in sata_drive_info.
3337  * Buffer has to accomodate the inquiry length (36 bytes).
3338  *
3339  * This function should be called with a port mutex held.
3340  */
3341 static	void
3342 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3343     sata_drive_info_t *sdinfo, uint8_t *buf)
3344 {
3345 
3346 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3347 	struct sata_id *sid = &sdinfo->satadrv_id;
3348 
3349 	/* Start with a nice clean slate */
3350 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3351 
3352 	/*
3353 	 * Rely on the dev_type for setting paripheral qualifier.
3354 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3355 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3356 	 * ATAPI Inquiry may provide more data to the target driver.
3357 	 */
3358 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3359 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3360 
3361 	/* CFA type device is not a removable media device */
3362 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3363 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3364 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3365 	inq->inq_iso = 0;	/* ISO version */
3366 	inq->inq_ecma = 0;	/* ECMA version */
3367 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3368 	inq->inq_aenc = 0;	/* Async event notification cap. */
3369 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3370 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3371 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3372 	inq->inq_len = 31;	/* Additional length */
3373 	inq->inq_dualp = 0;	/* dual port device - NO */
3374 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3375 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3376 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3377 				/*
3378 				 * Queuing support - controller has to
3379 				 * support some sort of command queuing.
3380 				 */
3381 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3382 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3383 	else
3384 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3385 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3386 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3387 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3388 
3389 #ifdef	_LITTLE_ENDIAN
3390 	/* Swap text fields to match SCSI format */
3391 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3392 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3393 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3394 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3395 	else
3396 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3397 #else	/* _LITTLE_ENDIAN */
3398 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3399 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3400 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3401 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3402 	else
3403 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3404 #endif	/* _LITTLE_ENDIAN */
3405 }
3406 
3407 
3408 /*
3409  * Scsi response set up for invalid command (command not supported)
3410  *
3411  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3412  */
3413 static int
3414 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3415 {
3416 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3417 	struct scsi_extended_sense *sense;
3418 
3419 	scsipkt->pkt_reason = CMD_CMPLT;
3420 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3421 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3422 
3423 	*scsipkt->pkt_scbp = STATUS_CHECK;
3424 
3425 	sense = sata_arq_sense(spx);
3426 	sense->es_key = KEY_ILLEGAL_REQUEST;
3427 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3428 
3429 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3430 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3431 
3432 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3433 	    scsipkt->pkt_comp != NULL) {
3434 		/* scsi callback required */
3435 		if (servicing_interrupt()) {
3436 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3437 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3438 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3439 				return (TRAN_BUSY);
3440 			}
3441 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3442 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3443 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3444 			/* Scheduling the callback failed */
3445 			return (TRAN_BUSY);
3446 		}
3447 	}
3448 	return (TRAN_ACCEPT);
3449 }
3450 
3451 /*
3452  * Scsi response set up for check condition with special sense key
3453  * and additional sense code.
3454  *
3455  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3456  */
3457 static int
3458 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3459 {
3460 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3461 	int cport = SATA_TXLT_CPORT(spx);
3462 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3463 	struct scsi_extended_sense *sense;
3464 
3465 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3466 	scsipkt->pkt_reason = CMD_CMPLT;
3467 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3468 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3469 
3470 	*scsipkt->pkt_scbp = STATUS_CHECK;
3471 
3472 	sense = sata_arq_sense(spx);
3473 	sense->es_key = key;
3474 	sense->es_add_code = code;
3475 
3476 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3477 
3478 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3479 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3480 
3481 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3482 	    scsipkt->pkt_comp != NULL) {
3483 		/* scsi callback required */
3484 		if (servicing_interrupt()) {
3485 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3486 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3487 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3488 				return (TRAN_BUSY);
3489 			}
3490 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3491 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3492 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3493 			/* Scheduling the callback failed */
3494 			return (TRAN_BUSY);
3495 		}
3496 	}
3497 	return (TRAN_ACCEPT);
3498 }
3499 
3500 /*
3501  * Scsi response setup for
3502  * emulated non-data command that requires no action/return data
3503  *
3504  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3505  */
3506 static	int
3507 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3508 {
3509 	int rval;
3510 	int reason;
3511 
3512 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3513 
3514 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3515 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3516 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3517 		return (rval);
3518 	}
3519 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3520 
3521 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3522 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3523 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3524 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3525 
3526 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3527 	    "Scsi_pkt completion reason %x\n",
3528 	    spx->txlt_scsi_pkt->pkt_reason);
3529 
3530 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3531 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3532 		/* scsi callback required */
3533 		if (servicing_interrupt()) {
3534 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3535 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3536 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3537 				return (TRAN_BUSY);
3538 			}
3539 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3540 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3541 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3542 			/* Scheduling the callback failed */
3543 			return (TRAN_BUSY);
3544 		}
3545 	}
3546 	return (TRAN_ACCEPT);
3547 }
3548 
3549 
3550 /*
3551  * SATA translate command: Inquiry / Identify Device
3552  * Use cached Identify Device data for now, rather than issuing actual
3553  * Device Identify cmd request. If device is detached and re-attached,
3554  * asynchronous event processing should fetch and refresh Identify Device
3555  * data.
3556  * VPD pages supported now:
3557  * Vital Product Data page
3558  * Unit Serial Number page
3559  * Block Device Characteristics Page
3560  * ATA Information Page
3561  *
3562  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3563  */
3564 
3565 #define	EVPD			1	/* Extended Vital Product Data flag */
3566 #define	CMDDT			2	/* Command Support Data - Obsolete */
3567 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3568 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3569 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3570 					/* Code */
3571 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3572 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3573 
3574 static int
3575 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3576 {
3577 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3578 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3579 	sata_drive_info_t *sdinfo;
3580 	struct scsi_extended_sense *sense;
3581 	int count;
3582 	uint8_t *p;
3583 	int i, j;
3584 	uint8_t page_buf[1024]; /* Max length */
3585 	int rval, reason;
3586 	ushort_t rate;
3587 
3588 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3589 
3590 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3591 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3592 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3593 		return (rval);
3594 	}
3595 
3596 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3597 	    &spx->txlt_sata_pkt->satapkt_device);
3598 
3599 	ASSERT(sdinfo != NULL);
3600 
3601 	scsipkt->pkt_reason = CMD_CMPLT;
3602 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3603 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3604 
3605 	/* Reject not supported request */
3606 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3607 		*scsipkt->pkt_scbp = STATUS_CHECK;
3608 		sense = sata_arq_sense(spx);
3609 		sense->es_key = KEY_ILLEGAL_REQUEST;
3610 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3611 		goto done;
3612 	}
3613 
3614 	/* Valid Inquiry request */
3615 	*scsipkt->pkt_scbp = STATUS_GOOD;
3616 
3617 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3618 
3619 		/*
3620 		 * Because it is fully emulated command storing data
3621 		 * programatically in the specified buffer, release
3622 		 * preallocated DMA resources before storing data in the buffer,
3623 		 * so no unwanted DMA sync would take place.
3624 		 */
3625 		sata_scsi_dmafree(NULL, scsipkt);
3626 
3627 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3628 			/* Standard Inquiry Data request */
3629 			struct scsi_inquiry inq;
3630 			unsigned int bufsize;
3631 
3632 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3633 			    sdinfo, (uint8_t *)&inq);
3634 			/* Copy no more than requested */
3635 			count = MIN(bp->b_bcount,
3636 			    sizeof (struct scsi_inquiry));
3637 			bufsize = scsipkt->pkt_cdbp[4];
3638 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3639 			count = MIN(count, bufsize);
3640 			bcopy(&inq, bp->b_un.b_addr, count);
3641 
3642 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3643 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3644 			    bufsize - count : 0;
3645 		} else {
3646 			/*
3647 			 * peripheral_qualifier = 0;
3648 			 *
3649 			 * We are dealing only with HD and will be
3650 			 * dealing with CD/DVD devices soon
3651 			 */
3652 			uint8_t peripheral_device_type =
3653 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3654 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3655 
3656 			bzero(page_buf, sizeof (page_buf));
3657 
3658 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3659 			case INQUIRY_SUP_VPD_PAGE:
3660 				/*
3661 				 * Request for supported Vital Product Data
3662 				 * pages.
3663 				 */
3664 				page_buf[0] = peripheral_device_type;
3665 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3666 				page_buf[2] = 0;
3667 				page_buf[3] = 4; /* page length */
3668 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3669 				page_buf[5] = INQUIRY_USN_PAGE;
3670 				page_buf[6] = INQUIRY_BDC_PAGE;
3671 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3672 				/* Copy no more than requested */
3673 				count = MIN(bp->b_bcount, 8);
3674 				bcopy(page_buf, bp->b_un.b_addr, count);
3675 				break;
3676 
3677 			case INQUIRY_USN_PAGE:
3678 				/*
3679 				 * Request for Unit Serial Number page.
3680 				 * Set-up the page.
3681 				 */
3682 				page_buf[0] = peripheral_device_type;
3683 				page_buf[1] = INQUIRY_USN_PAGE;
3684 				page_buf[2] = 0;
3685 				/* remaining page length */
3686 				page_buf[3] = SATA_ID_SERIAL_LEN;
3687 
3688 				/*
3689 				 * Copy serial number from Identify Device data
3690 				 * words into the inquiry page and swap bytes
3691 				 * when necessary.
3692 				 */
3693 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3694 #ifdef	_LITTLE_ENDIAN
3695 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3696 #else
3697 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3698 #endif
3699 				/*
3700 				 * Least significant character of the serial
3701 				 * number shall appear as the last byte,
3702 				 * according to SBC-3 spec.
3703 				 * Count trailing spaces to determine the
3704 				 * necessary shift length.
3705 				 */
3706 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3707 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3708 					if (*(p - j) != '\0' &&
3709 					    *(p - j) != '\040')
3710 						break;
3711 				}
3712 
3713 				/*
3714 				 * Shift SN string right, so that the last
3715 				 * non-blank character would appear in last
3716 				 * byte of SN field in the page.
3717 				 * 'j' is the shift length.
3718 				 */
3719 				for (i = 0;
3720 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3721 				    i++, p--)
3722 					*p = *(p - j);
3723 
3724 				/*
3725 				 * Add leading spaces - same number as the
3726 				 * shift size
3727 				 */
3728 				for (; j > 0; j--)
3729 					page_buf[4 + j - 1] = '\040';
3730 
3731 				count = MIN(bp->b_bcount,
3732 				    SATA_ID_SERIAL_LEN + 4);
3733 				bcopy(page_buf, bp->b_un.b_addr, count);
3734 				break;
3735 
3736 			case INQUIRY_BDC_PAGE:
3737 				/*
3738 				 * Request for Block Device Characteristics
3739 				 * page.  Set-up the page.
3740 				 */
3741 				page_buf[0] = peripheral_device_type;
3742 				page_buf[1] = INQUIRY_BDC_PAGE;
3743 				page_buf[2] = 0;
3744 				/* remaining page length */
3745 				page_buf[3] = SATA_ID_BDC_LEN;
3746 
3747 				rate = sdinfo->satadrv_id.ai_medrotrate;
3748 				page_buf[4] = (rate >> 8) & 0xff;
3749 				page_buf[5] = rate & 0xff;
3750 				page_buf[6] = 0;
3751 				page_buf[7] = sdinfo->satadrv_id.
3752 				    ai_nomformfactor & 0xf;
3753 
3754 				count = MIN(bp->b_bcount,
3755 				    SATA_ID_BDC_LEN + 4);
3756 				bcopy(page_buf, bp->b_un.b_addr, count);
3757 				break;
3758 
3759 			case INQUIRY_ATA_INFO_PAGE:
3760 				/*
3761 				 * Request for ATA Information page.
3762 				 */
3763 				page_buf[0] = peripheral_device_type;
3764 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3765 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3766 				    0xff;
3767 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3768 				/* page_buf[4-7] reserved */
3769 #ifdef  _LITTLE_ENDIAN
3770 				bcopy("ATA     ", &page_buf[8], 8);
3771 				swab(sdinfo->satadrv_id.ai_model,
3772 				    &page_buf[16], 16);
3773 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3774 				    "    ", 4) == 0) {
3775 					swab(sdinfo->satadrv_id.ai_fw,
3776 					    &page_buf[32], 4);
3777 				} else {
3778 					swab(&sdinfo->satadrv_id.ai_fw[4],
3779 					    &page_buf[32], 4);
3780 				}
3781 #else   /* _LITTLE_ENDIAN */
3782 				bcopy("ATA     ", &page_buf[8], 8);
3783 				bcopy(sdinfo->satadrv_id.ai_model,
3784 				    &page_buf[16], 16);
3785 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3786 				    "    ", 4) == 0) {
3787 					bcopy(sdinfo->satadrv_id.ai_fw,
3788 					    &page_buf[32], 4);
3789 				} else {
3790 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3791 					    &page_buf[32], 4);
3792 				}
3793 #endif  /* _LITTLE_ENDIAN */
3794 				/*
3795 				 * page_buf[36-55] which defines the device
3796 				 * signature is not defined at this
3797 				 * time.
3798 				 */
3799 
3800 				/* Set the command code */
3801 				if (sdinfo->satadrv_type ==
3802 				    SATA_DTYPE_ATADISK) {
3803 					page_buf[56] = SATAC_ID_DEVICE;
3804 				} else if (sdinfo->satadrv_type ==
3805 				    SATA_DTYPE_ATAPI) {
3806 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3807 				}
3808 				/*
3809 				 * If the command code, page_buf[56], is not
3810 				 * zero and if one of the identify commands
3811 				 * succeeds, return the identify data.
3812 				 */
3813 				if ((page_buf[56] != 0) &&
3814 				    (sata_fetch_device_identify_data(
3815 				    spx->txlt_sata_hba_inst, sdinfo) ==
3816 				    SATA_SUCCESS)) {
3817 					bcopy(&sdinfo->satadrv_id,
3818 					    &page_buf[60], sizeof (sata_id_t));
3819 				}
3820 
3821 				/* Need to copy out the page_buf to bp */
3822 				count = MIN(bp->b_bcount,
3823 				    SATA_ID_ATA_INFO_LEN + 4);
3824 				bcopy(page_buf, bp->b_un.b_addr, count);
3825 				break;
3826 
3827 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3828 				/*
3829 				 * We may want to implement this page, when
3830 				 * identifiers are common for SATA devices
3831 				 * But not now.
3832 				 */
3833 				/*FALLTHROUGH*/
3834 
3835 			default:
3836 				/* Request for unsupported VPD page */
3837 				*scsipkt->pkt_scbp = STATUS_CHECK;
3838 				sense = sata_arq_sense(spx);
3839 				sense->es_key = KEY_ILLEGAL_REQUEST;
3840 				sense->es_add_code =
3841 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3842 				goto done;
3843 			}
3844 		}
3845 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3846 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3847 		    scsipkt->pkt_cdbp[4] - count : 0;
3848 	}
3849 done:
3850 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3851 
3852 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3853 	    "Scsi_pkt completion reason %x\n",
3854 	    scsipkt->pkt_reason);
3855 
3856 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3857 	    scsipkt->pkt_comp != NULL) {
3858 		/* scsi callback required */
3859 		if (servicing_interrupt()) {
3860 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3861 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3862 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3863 				return (TRAN_BUSY);
3864 			}
3865 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3866 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3867 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3868 			/* Scheduling the callback failed */
3869 			return (TRAN_BUSY);
3870 		}
3871 	}
3872 	return (TRAN_ACCEPT);
3873 }
3874 
3875 /*
3876  * SATA translate command: Request Sense.
3877  *
3878  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3879  * At the moment this is an emulated command (ATA version for SATA hard disks).
3880  * May be translated into Check Power Mode command in the future.
3881  *
3882  * Note: There is a mismatch between already implemented Informational
3883  * Exception Mode Select page 0x1C and this function.
3884  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3885  * NO SENSE and set additional sense code to the exception code - this is not
3886  * implemented here.
3887  */
3888 static int
3889 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3890 {
3891 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3892 	struct scsi_extended_sense sense;
3893 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3894 	sata_drive_info_t *sdinfo;
3895 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3896 	int rval, reason, power_state = 0;
3897 
3898 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3899 
3900 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3901 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3902 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3903 		return (rval);
3904 	}
3905 
3906 	scsipkt->pkt_reason = CMD_CMPLT;
3907 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3908 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3909 	*scsipkt->pkt_scbp = STATUS_GOOD;
3910 
3911 	/*
3912 	 * when CONTROL field's NACA bit == 1
3913 	 * return ILLEGAL_REQUEST
3914 	 */
3915 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3916 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3917 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3918 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3919 	}
3920 
3921 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3922 	    &spx->txlt_sata_pkt->satapkt_device);
3923 	ASSERT(sdinfo != NULL);
3924 
3925 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3926 
3927 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3928 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3929 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3930 	if (sata_hba_start(spx, &rval) != 0) {
3931 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3932 		return (rval);
3933 	} else {
3934 		if (scmd->satacmd_error_reg != 0) {
3935 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3936 			return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3937 			    SD_SCSI_ASC_NO_ADD_SENSE));
3938 		}
3939 	}
3940 
3941 	switch (scmd->satacmd_sec_count_lsb) {
3942 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3943 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3944 			power_state = SATA_POWER_STOPPED;
3945 		else {
3946 			power_state = SATA_POWER_STANDBY;
3947 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3948 		}
3949 		break;
3950 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3951 		power_state = SATA_POWER_IDLE;
3952 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3953 		break;
3954 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3955 	default:		  /* 0x40, 0x41 active mode */
3956 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3957 			power_state = SATA_POWER_IDLE;
3958 		else {
3959 			power_state = SATA_POWER_ACTIVE;
3960 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3961 		}
3962 		break;
3963 	}
3964 
3965 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3966 
3967 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3968 		/*
3969 		 * Because it is fully emulated command storing data
3970 		 * programatically in the specified buffer, release
3971 		 * preallocated DMA resources before storing data in the buffer,
3972 		 * so no unwanted DMA sync would take place.
3973 		 */
3974 		int count = MIN(bp->b_bcount,
3975 		    sizeof (struct scsi_extended_sense));
3976 		sata_scsi_dmafree(NULL, scsipkt);
3977 		bzero(&sense, sizeof (struct scsi_extended_sense));
3978 		sense.es_valid = 0;	/* Valid LBA */
3979 		sense.es_class = 7;	/* Response code 0x70 - current err */
3980 		sense.es_key = KEY_NO_SENSE;
3981 		sense.es_add_len = 6;	/* Additional length */
3982 		/* Copy no more than requested */
3983 		bcopy(&sense, bp->b_un.b_addr, count);
3984 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3985 		scsipkt->pkt_resid = 0;
3986 		switch (power_state) {
3987 		case SATA_POWER_IDLE:
3988 		case SATA_POWER_STANDBY:
3989 			sense.es_add_code =
3990 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3991 			break;
3992 		case SATA_POWER_STOPPED:
3993 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3994 			break;
3995 		case SATA_POWER_ACTIVE:
3996 		default:
3997 			break;
3998 		}
3999 	}
4000 
4001 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4002 	    "Scsi_pkt completion reason %x\n",
4003 	    scsipkt->pkt_reason);
4004 
4005 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4006 	    scsipkt->pkt_comp != NULL) {
4007 		/* scsi callback required */
4008 		if (servicing_interrupt()) {
4009 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4010 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4011 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4012 				return (TRAN_BUSY);
4013 			}
4014 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4015 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4016 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4017 			/* Scheduling the callback failed */
4018 			return (TRAN_BUSY);
4019 		}
4020 	}
4021 	return (TRAN_ACCEPT);
4022 }
4023 
4024 /*
4025  * SATA translate command: Test Unit Ready
4026  * (ATA version for SATA hard disks).
4027  * It is translated into the Check Power Mode command.
4028  *
4029  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4030  */
4031 static int
4032 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4033 {
4034 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4035 	struct scsi_extended_sense *sense;
4036 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4037 	sata_drive_info_t *sdinfo;
4038 	int power_state;
4039 	int rval, reason;
4040 
4041 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4042 
4043 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4044 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4045 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4046 		return (rval);
4047 	}
4048 
4049 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4050 	    &spx->txlt_sata_pkt->satapkt_device);
4051 	ASSERT(sdinfo != NULL);
4052 
4053 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4054 
4055 	/* send CHECK POWER MODE command */
4056 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4057 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4058 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4059 	if (sata_hba_start(spx, &rval) != 0) {
4060 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4061 		return (rval);
4062 	} else {
4063 		if (scmd->satacmd_error_reg != 0) {
4064 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4065 			return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4066 			    SD_SCSI_ASC_LU_NOT_RESPONSE));
4067 		}
4068 	}
4069 
4070 	power_state = scmd->satacmd_sec_count_lsb;
4071 
4072 	/*
4073 	 * return NOT READY when device in STOPPED mode
4074 	 */
4075 	if (power_state == SATA_PWRMODE_STANDBY &&
4076 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4077 		*scsipkt->pkt_scbp = STATUS_CHECK;
4078 		sense = sata_arq_sense(spx);
4079 		sense->es_key = KEY_NOT_READY;
4080 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4081 	} else {
4082 		/*
4083 		 * For other power mode, return GOOD status
4084 		 */
4085 		*scsipkt->pkt_scbp = STATUS_GOOD;
4086 	}
4087 
4088 	scsipkt->pkt_reason = CMD_CMPLT;
4089 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4090 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4091 
4092 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4093 
4094 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4095 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4096 
4097 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4098 	    scsipkt->pkt_comp != NULL) {
4099 		/* scsi callback required */
4100 		if (servicing_interrupt()) {
4101 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4102 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4103 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4104 				return (TRAN_BUSY);
4105 			}
4106 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4107 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4108 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4109 			/* Scheduling the callback failed */
4110 			return (TRAN_BUSY);
4111 		}
4112 	}
4113 
4114 	return (TRAN_ACCEPT);
4115 }
4116 
4117 /*
4118  * SATA translate command: Start Stop Unit
4119  * Translation depends on a command:
4120  *
4121  * Power condition bits will be supported
4122  * and the power level should be maintained by SATL,
4123  * When SATL received a command, it will check the
4124  * power level firstly, and return the status according
4125  * to SAT2 v2.6 and SAT-2 Standby Modifications
4126  *
4127  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4128  * -----------------------------------------------------------------------
4129  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4130  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4131  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4132  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4133  *
4134  *	Unload Media / NOT SUPPORTED YET
4135  *	Load Media / NOT SUPPROTED YET
4136  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4137  *
4138  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4139  * appropriate values in scsi_pkt fields.
4140  */
4141 static int
4142 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4143 {
4144 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4145 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4146 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4147 	int cport = SATA_TXLT_CPORT(spx);
4148 	int rval, reason;
4149 	sata_drive_info_t *sdinfo;
4150 	sata_id_t *sata_id;
4151 
4152 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4153 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4154 
4155 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
4156 
4157 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4158 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4159 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4160 		return (rval);
4161 	}
4162 
4163 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4164 		/* IMMED bit - not supported */
4165 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4166 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4167 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4168 	}
4169 
4170 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4171 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4172 
4173 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4174 	    &spx->txlt_sata_pkt->satapkt_device);
4175 	ASSERT(sdinfo != NULL);
4176 	sata_id = &sdinfo->satadrv_id;
4177 
4178 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4179 	case 0:
4180 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4181 			/* Load/Unload Media - invalid request */
4182 			goto err_out;
4183 		}
4184 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4185 			/* Start Unit */
4186 			sata_build_read_verify_cmd(scmd, 1, 5);
4187 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4188 			/* Transfer command to HBA */
4189 			if (sata_hba_start(spx, &rval) != 0) {
4190 				/* Pkt not accepted for execution */
4191 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4192 				return (rval);
4193 			} else {
4194 				if (scmd->satacmd_error_reg != 0) {
4195 					goto err_out;
4196 				}
4197 			}
4198 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4199 		} else {
4200 			/* Stop Unit */
4201 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4202 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4203 			if (sata_hba_start(spx, &rval) != 0) {
4204 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4205 				return (rval);
4206 			} else {
4207 				if (scmd->satacmd_error_reg != 0) {
4208 					goto err_out;
4209 				}
4210 			}
4211 			/* ata standby immediate command */
4212 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4213 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4214 			if (sata_hba_start(spx, &rval) != 0) {
4215 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4216 				return (rval);
4217 			} else {
4218 				if (scmd->satacmd_error_reg != 0) {
4219 					goto err_out;
4220 				}
4221 			}
4222 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4223 		}
4224 		break;
4225 	case 0x1:
4226 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4227 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4228 		if (sata_hba_start(spx, &rval) != 0) {
4229 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4230 			return (rval);
4231 		} else {
4232 			if (scmd->satacmd_error_reg != 0) {
4233 				goto err_out;
4234 			}
4235 		}
4236 		sata_build_read_verify_cmd(scmd, 1, 5);
4237 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4238 		/* Transfer command to HBA */
4239 		if (sata_hba_start(spx, &rval) != 0) {
4240 			/* Pkt not accepted for execution */
4241 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4242 			return (rval);
4243 		} else {
4244 			if (scmd->satacmd_error_reg != 0) {
4245 				goto err_out;
4246 			}
4247 		}
4248 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4249 		break;
4250 	case 0x2:
4251 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4252 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4253 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4254 			if (sata_hba_start(spx, &rval) != 0) {
4255 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4256 				return (rval);
4257 			} else {
4258 				if (scmd->satacmd_error_reg != 0) {
4259 					goto err_out;
4260 				}
4261 			}
4262 		}
4263 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4264 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4265 		if (sata_hba_start(spx, &rval) != 0) {
4266 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4267 			return (rval);
4268 		} else {
4269 			if (scmd->satacmd_error_reg != 0) {
4270 				goto err_out;
4271 			}
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(&SATA_CPORT_MUTEX(shi, cport));
4292 				return (rval);
4293 			} else {
4294 				if (scmd->satacmd_error_reg != 0) {
4295 					goto err_out;
4296 				}
4297 			}
4298 		}
4299 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4300 		break;
4301 	case 0x3:
4302 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4303 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4304 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4305 			if (sata_hba_start(spx, &rval) != 0) {
4306 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4307 				return (rval);
4308 			} else {
4309 				if (scmd->satacmd_error_reg != 0) {
4310 					goto err_out;
4311 				}
4312 			}
4313 		}
4314 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4315 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4316 		if (sata_hba_start(spx, &rval) != 0) {
4317 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4318 			return (rval);
4319 		} else {
4320 			if (scmd->satacmd_error_reg != 0) {
4321 				goto err_out;
4322 			}
4323 		}
4324 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4325 		break;
4326 	case 0x7:
4327 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4328 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4329 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4330 		if (sata_hba_start(spx, &rval) != 0) {
4331 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4332 			return (rval);
4333 		} else {
4334 			if (scmd->satacmd_error_reg != 0) {
4335 				goto err_out;
4336 			}
4337 		}
4338 		switch (scmd->satacmd_sec_count_lsb) {
4339 		case SATA_PWRMODE_STANDBY:
4340 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4341 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4342 			    sdinfo->satadrv_standby_timer);
4343 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 			if (sata_hba_start(spx, &rval) != 0) {
4345 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4346 				return (rval);
4347 			} else {
4348 				if (scmd->satacmd_error_reg != 0) {
4349 					goto err_out;
4350 				}
4351 			}
4352 			break;
4353 		case SATA_PWRMODE_IDLE:
4354 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4355 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4356 			    sdinfo->satadrv_standby_timer);
4357 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4358 			if (sata_hba_start(spx, &rval) != 0) {
4359 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4360 				return (rval);
4361 			} else {
4362 				if (scmd->satacmd_error_reg != 0) {
4363 					goto err_out;
4364 				}
4365 			}
4366 			break;
4367 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4368 		case SATA_PWRMODE_ACTIVE_SPINUP:
4369 		case SATA_PWRMODE_ACTIVE:
4370 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4371 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4372 			    sdinfo->satadrv_standby_timer);
4373 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4374 			if (sata_hba_start(spx, &rval) != 0) {
4375 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4376 				return (rval);
4377 			} else {
4378 				if (scmd->satacmd_error_reg != 0) {
4379 					goto err_out;
4380 				}
4381 			}
4382 			sata_build_read_verify_cmd(scmd, 1, 5);
4383 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4384 			if (sata_hba_start(spx, &rval) != 0) {
4385 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4386 				return (rval);
4387 			} else {
4388 				if (scmd->satacmd_error_reg != 0) {
4389 					goto err_out;
4390 				}
4391 			}
4392 			break;
4393 		default:
4394 			goto err_out;
4395 		}
4396 		break;
4397 	case 0xb:
4398 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4399 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4400 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4401 			return (sata_txlt_check_condition(spx,
4402 			    KEY_ILLEGAL_REQUEST,
4403 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4404 		}
4405 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4406 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4407 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4408 			if (sata_hba_start(spx, &rval) != 0) {
4409 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4410 				return (rval);
4411 			} else {
4412 				if (scmd->satacmd_error_reg != 0) {
4413 					goto err_out;
4414 				}
4415 			}
4416 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4417 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4418 			if (sata_hba_start(spx, &rval) != 0) {
4419 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4420 				return (rval);
4421 			} else {
4422 				if (scmd->satacmd_error_reg != 0) {
4423 					goto err_out;
4424 				}
4425 			}
4426 		}
4427 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4428 		break;
4429 	default:
4430 err_out:
4431 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4432 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4433 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4434 	}
4435 
4436 	/*
4437 	 * Since it was a synchronous command,
4438 	 * a callback function will be called directly.
4439 	 */
4440 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4441 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4442 	    "synchronous execution status %x\n",
4443 	    spx->txlt_sata_pkt->satapkt_reason);
4444 
4445 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4446 	    scsipkt->pkt_comp != NULL) {
4447 		sata_set_arq_data(spx->txlt_sata_pkt);
4448 		if (servicing_interrupt()) {
4449 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4450 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4451 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4452 				return (TRAN_BUSY);
4453 			}
4454 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4455 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4456 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4457 			/* Scheduling the callback failed */
4458 			return (TRAN_BUSY);
4459 		}
4460 	}
4461 	else
4462 
4463 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4464 
4465 	return (TRAN_ACCEPT);
4466 
4467 }
4468 
4469 /*
4470  * SATA translate command:  Read Capacity.
4471  * Emulated command for SATA disks.
4472  * Capacity is retrieved from cached Idenifty Device data.
4473  * Identify Device data shows effective disk capacity, not the native
4474  * capacity, which may be limitted by Set Max Address command.
4475  * This is ATA version for SATA hard disks.
4476  *
4477  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4478  */
4479 static int
4480 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4481 {
4482 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4483 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4484 	sata_drive_info_t *sdinfo;
4485 	uint64_t val;
4486 	uchar_t *rbuf;
4487 	int rval, reason;
4488 
4489 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4490 	    "sata_txlt_read_capacity: ", NULL);
4491 
4492 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4493 
4494 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4495 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4496 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4497 		return (rval);
4498 	}
4499 
4500 	scsipkt->pkt_reason = CMD_CMPLT;
4501 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4502 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4503 	*scsipkt->pkt_scbp = STATUS_GOOD;
4504 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4505 		/*
4506 		 * Because it is fully emulated command storing data
4507 		 * programatically in the specified buffer, release
4508 		 * preallocated DMA resources before storing data in the buffer,
4509 		 * so no unwanted DMA sync would take place.
4510 		 */
4511 		sata_scsi_dmafree(NULL, scsipkt);
4512 
4513 		sdinfo = sata_get_device_info(
4514 		    spx->txlt_sata_hba_inst,
4515 		    &spx->txlt_sata_pkt->satapkt_device);
4516 
4517 		/*
4518 		 * As per SBC-3, the "returned LBA" is either the highest
4519 		 * addressable LBA or 0xffffffff, whichever is smaller.
4520 		 */
4521 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4522 
4523 		rbuf = (uchar_t *)bp->b_un.b_addr;
4524 		/* Need to swap endians to match scsi format */
4525 		rbuf[0] = (val >> 24) & 0xff;
4526 		rbuf[1] = (val >> 16) & 0xff;
4527 		rbuf[2] = (val >> 8) & 0xff;
4528 		rbuf[3] = val & 0xff;
4529 		/* block size - always 512 bytes, for now */
4530 		rbuf[4] = 0;
4531 		rbuf[5] = 0;
4532 		rbuf[6] = 0x02;
4533 		rbuf[7] = 0;
4534 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4535 		scsipkt->pkt_resid = 0;
4536 
4537 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4538 		    sdinfo->satadrv_capacity -1);
4539 	}
4540 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4541 	/*
4542 	 * If a callback was requested, do it now.
4543 	 */
4544 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4545 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4546 
4547 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4548 	    scsipkt->pkt_comp != NULL) {
4549 		/* scsi callback required */
4550 		if (servicing_interrupt()) {
4551 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4552 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4553 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4554 				return (TRAN_BUSY);
4555 			}
4556 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4557 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4558 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4559 			/* Scheduling the callback failed */
4560 			return (TRAN_BUSY);
4561 		}
4562 	}
4563 
4564 	return (TRAN_ACCEPT);
4565 }
4566 
4567 /*
4568  * SATA translate command:  Read Capacity (16).
4569  * Emulated command for SATA disks.
4570  * Info is retrieved from cached Identify Device data.
4571  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4572  *
4573  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4574  */
4575 static int
4576 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4577 {
4578 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4579 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4580 	sata_drive_info_t *sdinfo;
4581 	uint64_t val;
4582 	uint16_t l2p_exp;
4583 	uchar_t *rbuf;
4584 	int rval, reason;
4585 #define	TPE	0x80
4586 #define	TPRZ	0x40
4587 
4588 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4589 	    "sata_txlt_read_capacity: ", NULL);
4590 
4591 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4592 
4593 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4594 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4595 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4596 		return (rval);
4597 	}
4598 
4599 	scsipkt->pkt_reason = CMD_CMPLT;
4600 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4601 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4602 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4603 		/*
4604 		 * Because it is fully emulated command storing data
4605 		 * programatically in the specified buffer, release
4606 		 * preallocated DMA resources before storing data in the buffer,
4607 		 * so no unwanted DMA sync would take place.
4608 		 */
4609 		sata_scsi_dmafree(NULL, scsipkt);
4610 
4611 		/* Check SERVICE ACTION field */
4612 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4613 		    SSVC_ACTION_READ_CAPACITY_G4) {
4614 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4615 			return (sata_txlt_check_condition(spx,
4616 			    KEY_ILLEGAL_REQUEST,
4617 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4618 		}
4619 
4620 		/* Check LBA field */
4621 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4622 		    (scsipkt->pkt_cdbp[3] != 0) ||
4623 		    (scsipkt->pkt_cdbp[4] != 0) ||
4624 		    (scsipkt->pkt_cdbp[5] != 0) ||
4625 		    (scsipkt->pkt_cdbp[6] != 0) ||
4626 		    (scsipkt->pkt_cdbp[7] != 0) ||
4627 		    (scsipkt->pkt_cdbp[8] != 0) ||
4628 		    (scsipkt->pkt_cdbp[9] != 0)) {
4629 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4630 			return (sata_txlt_check_condition(spx,
4631 			    KEY_ILLEGAL_REQUEST,
4632 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4633 		}
4634 
4635 		/* Check PMI bit */
4636 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4637 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4638 			return (sata_txlt_check_condition(spx,
4639 			    KEY_ILLEGAL_REQUEST,
4640 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4641 		}
4642 
4643 		*scsipkt->pkt_scbp = STATUS_GOOD;
4644 
4645 		sdinfo = sata_get_device_info(
4646 		    spx->txlt_sata_hba_inst,
4647 		    &spx->txlt_sata_pkt->satapkt_device);
4648 
4649 		/* last logical block address */
4650 		val = MIN(sdinfo->satadrv_capacity - 1,
4651 		    SCSI_READ_CAPACITY16_MAX_LBA);
4652 
4653 		/* logical to physical block size exponent */
4654 		l2p_exp = 0;
4655 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4656 			/* physical/logical sector size word is valid */
4657 
4658 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4659 			    SATA_L2PS_HAS_MULT) {
4660 				/* multiple logical sectors per phys sectors */
4661 				l2p_exp =
4662 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4663 				    SATA_L2PS_EXP_MASK;
4664 			}
4665 		}
4666 
4667 		rbuf = (uchar_t *)bp->b_un.b_addr;
4668 		bzero(rbuf, bp->b_bcount);
4669 
4670 		/* returned logical block address */
4671 		rbuf[0] = (val >> 56) & 0xff;
4672 		rbuf[1] = (val >> 48) & 0xff;
4673 		rbuf[2] = (val >> 40) & 0xff;
4674 		rbuf[3] = (val >> 32) & 0xff;
4675 		rbuf[4] = (val >> 24) & 0xff;
4676 		rbuf[5] = (val >> 16) & 0xff;
4677 		rbuf[6] = (val >> 8) & 0xff;
4678 		rbuf[7] = val & 0xff;
4679 
4680 		/* logical block length in bytes = 512 (for now) */
4681 		/* rbuf[8] = 0; */
4682 		/* rbuf[9] = 0; */
4683 		rbuf[10] = 0x02;
4684 		/* rbuf[11] = 0; */
4685 
4686 		/* p_type, prot_en, unspecified by SAT-2 */
4687 		/* rbuf[12] = 0; */
4688 
4689 		/* p_i_exponent, undefined by SAT-2 */
4690 		/* logical blocks per physical block exponent */
4691 		rbuf[13] = l2p_exp;
4692 
4693 		/* lowest aligned logical block address = 0 (for now) */
4694 		/* tpe and tprz as defined in T10/10-079 r0 */
4695 		if (sdinfo->satadrv_id.ai_addsupported &
4696 		    SATA_DETERMINISTIC_READ) {
4697 			if (sdinfo->satadrv_id.ai_addsupported &
4698 			    SATA_READ_ZERO) {
4699 				rbuf[14] |= TPRZ;
4700 			} else {
4701 				rbuf[14] |= TPE;
4702 			}
4703 		}
4704 		/* rbuf[15] = 0; */
4705 
4706 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4707 		scsipkt->pkt_resid = 0;
4708 
4709 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4710 		    sdinfo->satadrv_capacity -1);
4711 	}
4712 
4713 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4714 
4715 	/*
4716 	 * If a callback was requested, do it now.
4717 	 */
4718 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4719 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4720 
4721 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4722 	    scsipkt->pkt_comp != NULL) {
4723 		/* scsi callback required */
4724 		if (servicing_interrupt()) {
4725 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4726 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4727 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4728 				return (TRAN_BUSY);
4729 			}
4730 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4731 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4732 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4733 			/* Scheduling the callback failed */
4734 			return (TRAN_BUSY);
4735 		}
4736 	}
4737 
4738 	return (TRAN_ACCEPT);
4739 }
4740 
4741 /*
4742  * Translate command: UNMAP
4743  *
4744  * The function cannot be called in interrupt context since it may sleep.
4745  */
4746 static int
4747 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4748 {
4749 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4750 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4751 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4752 	uint16_t count = 0;
4753 	int synch;
4754 	int rval, reason;
4755 	int i, x;
4756 	int bdlen = 0;
4757 	int ranges = 0;
4758 	int paramlen = 8;
4759 	uint8_t *data, *tmpbd;
4760 	sata_drive_info_t *sdinfo;
4761 #define	TRIM	0x1
4762 
4763 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4764 	    "sata_txlt_unmap: ", NULL);
4765 
4766 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4767 
4768 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4769 	    &spx->txlt_sata_pkt->satapkt_device);
4770 	if (sdinfo != NULL) {
4771 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4772 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4773 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4774 		    sdinfo->satadrv_id.ai_maxcount);
4775 	}
4776 
4777 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4778 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4779 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4780 		return (rval);
4781 	}
4782 
4783 	/*
4784 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4785 	 * Start by getting the block descriptor data length by subtracting
4786 	 * the 8 byte parameter list header from the parameter list length.
4787 	 * The block descriptor size has to be a multiple of 16 bytes.
4788 	 */
4789 	bdlen = scsipkt->pkt_cdbp[7];
4790 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4791 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4792 	    (bdlen > (bp->b_bcount - paramlen))) {
4793 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4794 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4795 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4796 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4797 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4798 	}
4799 	/*
4800 	 * If there are no parameter data or block descriptors, it is not
4801 	 * considered an error so just complete the command without sending
4802 	 * TRIM.
4803 	 */
4804 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4805 	    (bp->b_bcount == 0)) {
4806 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4807 		    "sata_txlt_unmap: no parameter data or block descriptors",
4808 		    NULL);
4809 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4810 		return (sata_txlt_unmap_nodata_cmd(spx));
4811 	}
4812 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4813 	data = kmem_zalloc(bdlen, KM_SLEEP);
4814 
4815 	/*
4816 	 * Loop through all the UNMAP block descriptors and convert the data
4817 	 * into TRIM format.
4818 	 */
4819 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4820 		/* get range length */
4821 		data[x] = tmpbd[i+7];
4822 		data[x+1] = tmpbd[i+6];
4823 		/* get LBA */
4824 		data[x+2] = tmpbd[i+5];
4825 		data[x+3] = tmpbd[i+4];
4826 		data[x+4] = tmpbd[i+3];
4827 		data[x+5] = tmpbd[i+2];
4828 		data[x+6] = tmpbd[i+11];
4829 		data[x+7] = tmpbd[i+10];
4830 
4831 		ranges++;
4832 	}
4833 
4834 	/*
4835 	 * The TRIM command expects the data buffer to be a multiple of
4836 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4837 	 * may be too small.  Free the original DMA resources and create a
4838 	 * local buffer.
4839 	 */
4840 	sata_common_free_dma_rsrcs(spx);
4841 
4842 	/*
4843 	 * Get count of 512-byte blocks of range entries.  The length
4844 	 * of a range entry is 8 bytes which means one count has 64 range
4845 	 * entries.
4846 	 */
4847 	count = (ranges + 63)/64;
4848 
4849 	/* Allocate a buffer that is a multiple of 512 bytes. */
4850 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4851 	bp = sata_alloc_local_buffer(spx, count * 512);
4852 	if (bp == NULL) {
4853 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4854 		    "sata_txlt_unmap: "
4855 		    "cannot allocate buffer for TRIM command", NULL);
4856 		kmem_free(data, bdlen);
4857 		return (TRAN_BUSY);
4858 	}
4859 	bp_mapin(bp); /* make data buffer accessible */
4860 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4861 
4862 	bzero(bp->b_un.b_addr, bp->b_bcount);
4863 	bcopy(data, bp->b_un.b_addr, x);
4864 	kmem_free(data, bdlen);
4865 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4866 	    DDI_DMA_SYNC_FORDEV);
4867 	ASSERT(rval == DDI_SUCCESS);
4868 
4869 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4870 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4871 	scmd->satacmd_cmd_reg = SATAC_DSM;
4872 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4873 	scmd->satacmd_sec_count_lsb = count & 0xff;
4874 	scmd->satacmd_features_reg = TRIM;
4875 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4876 	scmd->satacmd_status_reg = 0;
4877 	scmd->satacmd_error_reg = 0;
4878 
4879 	/* Start processing command */
4880 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4881 		spx->txlt_sata_pkt->satapkt_comp =
4882 		    sata_txlt_unmap_completion;
4883 		synch = FALSE;
4884 	} else {
4885 		synch = TRUE;
4886 	}
4887 
4888 	if (sata_hba_start(spx, &rval) != 0) {
4889 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4890 		return (rval);
4891 	}
4892 
4893 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4894 
4895 	if (synch) {
4896 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4897 	}
4898 
4899 	return (TRAN_ACCEPT);
4900 }
4901 
4902 /*
4903  * SATA translate command: Mode Sense.
4904  * Translated into appropriate SATA command or emulated.
4905  * Saved Values Page Control (03) are not supported.
4906  *
4907  * NOTE: only caching mode sense page is currently implemented.
4908  *
4909  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4910  */
4911 
4912 #define	LLBAA	0x10	/* Long LBA Accepted */
4913 
4914 static int
4915 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4916 {
4917 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4918 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4919 	sata_drive_info_t *sdinfo;
4920 	sata_id_t *sata_id;
4921 	struct scsi_extended_sense *sense;
4922 	int 		len, bdlen, count, alc_len;
4923 	int		pc;	/* Page Control code */
4924 	uint8_t		*buf;	/* mode sense buffer */
4925 	int		rval, reason;
4926 
4927 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4928 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4929 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4930 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4931 
4932 	if (servicing_interrupt()) {
4933 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4934 		if (buf == NULL) {
4935 			return (TRAN_BUSY);
4936 		}
4937 	} else {
4938 		buf = kmem_zalloc(1024, KM_SLEEP);
4939 	}
4940 
4941 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4942 
4943 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4944 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4945 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4946 		kmem_free(buf, 1024);
4947 		return (rval);
4948 	}
4949 
4950 	scsipkt->pkt_reason = CMD_CMPLT;
4951 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4952 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4953 
4954 	pc = scsipkt->pkt_cdbp[2] >> 6;
4955 
4956 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4957 		/*
4958 		 * Because it is fully emulated command storing data
4959 		 * programatically in the specified buffer, release
4960 		 * preallocated DMA resources before storing data in the buffer,
4961 		 * so no unwanted DMA sync would take place.
4962 		 */
4963 		sata_scsi_dmafree(NULL, scsipkt);
4964 
4965 		len = 0;
4966 		bdlen = 0;
4967 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4968 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4969 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4970 				bdlen = 16;
4971 			else
4972 				bdlen = 8;
4973 		}
4974 		/* Build mode parameter header */
4975 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4976 			/* 4-byte mode parameter header */
4977 			buf[len++] = 0;		/* mode data length */
4978 			buf[len++] = 0;		/* medium type */
4979 			buf[len++] = 0;		/* dev-specific param */
4980 			buf[len++] = bdlen;	/* Block Descriptor length */
4981 		} else {
4982 			/* 8-byte mode parameter header */
4983 			buf[len++] = 0;		/* mode data length */
4984 			buf[len++] = 0;
4985 			buf[len++] = 0;		/* medium type */
4986 			buf[len++] = 0;		/* dev-specific param */
4987 			if (bdlen == 16)
4988 				buf[len++] = 1;	/* long lba descriptor */
4989 			else
4990 				buf[len++] = 0;
4991 			buf[len++] = 0;
4992 			buf[len++] = 0;		/* Block Descriptor length */
4993 			buf[len++] = bdlen;
4994 		}
4995 
4996 		sdinfo = sata_get_device_info(
4997 		    spx->txlt_sata_hba_inst,
4998 		    &spx->txlt_sata_pkt->satapkt_device);
4999 
5000 		/* Build block descriptor only if not disabled (DBD) */
5001 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5002 			/* Block descriptor - direct-access device format */
5003 			if (bdlen == 8) {
5004 				/* build regular block descriptor */
5005 				buf[len++] =
5006 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5007 				buf[len++] =
5008 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5009 				buf[len++] =
5010 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5011 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5012 				buf[len++] = 0; /* density code */
5013 				buf[len++] = 0;
5014 				if (sdinfo->satadrv_type ==
5015 				    SATA_DTYPE_ATADISK)
5016 					buf[len++] = 2;
5017 				else
5018 					/* ATAPI */
5019 					buf[len++] = 8;
5020 				buf[len++] = 0;
5021 			} else if (bdlen == 16) {
5022 				/* Long LBA Accepted */
5023 				/* build long lba block descriptor */
5024 #ifndef __lock_lint
5025 				buf[len++] =
5026 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5027 				buf[len++] =
5028 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5029 				buf[len++] =
5030 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5031 				buf[len++] =
5032 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5033 #endif
5034 				buf[len++] =
5035 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5036 				buf[len++] =
5037 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5038 				buf[len++] =
5039 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5040 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5041 				buf[len++] = 0;
5042 				buf[len++] = 0; /* density code */
5043 				buf[len++] = 0;
5044 				buf[len++] = 0;
5045 				if (sdinfo->satadrv_type ==
5046 				    SATA_DTYPE_ATADISK)
5047 					buf[len++] = 2;
5048 				else
5049 					/* ATAPI */
5050 					buf[len++] = 8;
5051 				buf[len++] = 0;
5052 			}
5053 		}
5054 
5055 		sata_id = &sdinfo->satadrv_id;
5056 
5057 		/*
5058 		 * Add requested pages.
5059 		 * Page 3 and 4 are obsolete and we are not supporting them.
5060 		 * We deal now with:
5061 		 * caching (read/write cache control).
5062 		 * We should eventually deal with following mode pages:
5063 		 * error recovery  (0x01),
5064 		 * power condition (0x1a),
5065 		 * exception control page (enables SMART) (0x1c),
5066 		 * enclosure management (ses),
5067 		 * protocol-specific port mode (port control).
5068 		 */
5069 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5070 		case MODEPAGE_RW_ERRRECOV:
5071 			/* DAD_MODE_ERR_RECOV */
5072 			/* R/W recovery */
5073 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5074 			break;
5075 		case MODEPAGE_CACHING:
5076 			/* DAD_MODE_CACHE */
5077 			/* Reject not supported request for saved parameters */
5078 			if (pc == 3) {
5079 				*scsipkt->pkt_scbp = STATUS_CHECK;
5080 				sense = sata_arq_sense(spx);
5081 				sense->es_key = KEY_ILLEGAL_REQUEST;
5082 				sense->es_add_code =
5083 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5084 				goto done;
5085 			}
5086 
5087 			/* caching */
5088 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5089 			break;
5090 		case MODEPAGE_INFO_EXCPT:
5091 			/* exception cntrl */
5092 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5093 				len += sata_build_msense_page_1c(sdinfo, pc,
5094 				    buf+len);
5095 			}
5096 			else
5097 				goto err;
5098 			break;
5099 		case MODEPAGE_POWER_COND:
5100 			/* DAD_MODE_POWER_COND */
5101 			/* power condition */
5102 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5103 			break;
5104 
5105 		case MODEPAGE_ACOUSTIC_MANAG:
5106 			/* acoustic management */
5107 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5108 			break;
5109 		case MODEPAGE_ALLPAGES:
5110 			/* all pages */
5111 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5112 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5113 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5114 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5115 				len += sata_build_msense_page_1c(sdinfo, pc,
5116 				    buf+len);
5117 			}
5118 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5119 			break;
5120 		default:
5121 		err:
5122 			/* Invalid request */
5123 			*scsipkt->pkt_scbp = STATUS_CHECK;
5124 			sense = sata_arq_sense(spx);
5125 			sense->es_key = KEY_ILLEGAL_REQUEST;
5126 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5127 			goto done;
5128 		}
5129 
5130 		/* fix total mode data length */
5131 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5132 			/* 4-byte mode parameter header */
5133 			buf[0] = len - 1;	/* mode data length */
5134 		} else {
5135 			buf[0] = (len -2) >> 8;
5136 			buf[1] = (len -2) & 0xff;
5137 		}
5138 
5139 
5140 		/* Check allocation length */
5141 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5142 			alc_len = scsipkt->pkt_cdbp[4];
5143 		} else {
5144 			alc_len = scsipkt->pkt_cdbp[7];
5145 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5146 		}
5147 		/*
5148 		 * We do not check for possible parameters truncation
5149 		 * (alc_len < len) assuming that the target driver works
5150 		 * correctly. Just avoiding overrun.
5151 		 * Copy no more than requested and possible, buffer-wise.
5152 		 */
5153 		count = MIN(alc_len, len);
5154 		count = MIN(bp->b_bcount, count);
5155 		bcopy(buf, bp->b_un.b_addr, count);
5156 
5157 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5158 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5159 	}
5160 	*scsipkt->pkt_scbp = STATUS_GOOD;
5161 done:
5162 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5163 	(void) kmem_free(buf, 1024);
5164 
5165 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5166 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5167 
5168 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5169 	    scsipkt->pkt_comp != NULL) {
5170 		/* scsi callback required */
5171 		if (servicing_interrupt()) {
5172 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5173 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5174 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5175 				return (TRAN_BUSY);
5176 			}
5177 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5178 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5179 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5180 			/* Scheduling the callback failed */
5181 			return (TRAN_BUSY);
5182 		}
5183 	}
5184 
5185 	return (TRAN_ACCEPT);
5186 }
5187 
5188 
5189 /*
5190  * SATA translate command: Mode Select.
5191  * Translated into appropriate SATA command or emulated.
5192  * Saving parameters is not supported.
5193  * Changing device capacity is not supported (although theoretically
5194  * possible by executing SET FEATURES/SET MAX ADDRESS)
5195  *
5196  * Assumption is that the target driver is working correctly.
5197  *
5198  * More than one SATA command may be executed to perform operations specified
5199  * by mode select pages. The first error terminates further execution.
5200  * Operations performed successully are not backed-up in such case.
5201  *
5202  * NOTE: Implemented pages:
5203  * - caching page
5204  * - informational exception page
5205  * - acoustic management page
5206  * - power condition page
5207  * Caching setup is remembered so it could be re-stored in case of
5208  * an unexpected device reset.
5209  *
5210  * Returns TRAN_XXXX.
5211  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5212  */
5213 
5214 static int
5215 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5216 {
5217 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5218 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5219 	struct scsi_extended_sense *sense;
5220 	int len, pagelen, count, pllen;
5221 	uint8_t *buf;	/* mode select buffer */
5222 	int rval, stat, reason;
5223 	uint_t nointr_flag;
5224 	int dmod = 0;
5225 
5226 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5227 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5228 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5229 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5230 
5231 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5232 
5233 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5234 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5235 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5236 		return (rval);
5237 	}
5238 
5239 	rval = TRAN_ACCEPT;
5240 
5241 	scsipkt->pkt_reason = CMD_CMPLT;
5242 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5243 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5244 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5245 
5246 	/* Reject not supported request */
5247 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5248 		*scsipkt->pkt_scbp = STATUS_CHECK;
5249 		sense = sata_arq_sense(spx);
5250 		sense->es_key = KEY_ILLEGAL_REQUEST;
5251 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5252 		goto done;
5253 	}
5254 
5255 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5256 		pllen = scsipkt->pkt_cdbp[4];
5257 	} else {
5258 		pllen = scsipkt->pkt_cdbp[7];
5259 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5260 	}
5261 
5262 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5263 
5264 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5265 		buf = (uint8_t *)bp->b_un.b_addr;
5266 		count = MIN(bp->b_bcount, pllen);
5267 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5268 		scsipkt->pkt_resid = 0;
5269 		pllen = count;
5270 
5271 		/*
5272 		 * Check the header to skip the block descriptor(s) - we
5273 		 * do not support setting device capacity.
5274 		 * Existing macros do not recognize long LBA dscriptor,
5275 		 * hence manual calculation.
5276 		 */
5277 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5278 			/* 6-bytes CMD, 4 bytes header */
5279 			if (count <= 4)
5280 				goto done;		/* header only */
5281 			len = buf[3] + 4;
5282 		} else {
5283 			/* 10-bytes CMD, 8 bytes header */
5284 			if (count <= 8)
5285 				goto done;		/* header only */
5286 			len = buf[6];
5287 			len = (len << 8) + buf[7] + 8;
5288 		}
5289 		if (len >= count)
5290 			goto done;	/* header + descriptor(s) only */
5291 
5292 		pllen -= len;		/* remaining data length */
5293 
5294 		/*
5295 		 * We may be executing SATA command and want to execute it
5296 		 * in SYNCH mode, regardless of scsi_pkt setting.
5297 		 * Save scsi_pkt setting and indicate SYNCH mode
5298 		 */
5299 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5300 		    scsipkt->pkt_comp != NULL) {
5301 			scsipkt->pkt_flags |= FLAG_NOINTR;
5302 		}
5303 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5304 
5305 		/*
5306 		 * len is now the offset to a first mode select page
5307 		 * Process all pages
5308 		 */
5309 		while (pllen > 0) {
5310 			switch ((int)buf[len]) {
5311 			case MODEPAGE_CACHING:
5312 				/* No support for SP (saving) */
5313 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5314 					*scsipkt->pkt_scbp = STATUS_CHECK;
5315 					sense = sata_arq_sense(spx);
5316 					sense->es_key = KEY_ILLEGAL_REQUEST;
5317 					sense->es_add_code =
5318 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5319 					goto done;
5320 				}
5321 				stat = sata_mode_select_page_8(spx,
5322 				    (struct mode_cache_scsi3 *)&buf[len],
5323 				    pllen, &pagelen, &rval, &dmod);
5324 				/*
5325 				 * The pagelen value indicates the number of
5326 				 * parameter bytes already processed.
5327 				 * The rval is the return value from
5328 				 * sata_tran_start().
5329 				 * The stat indicates the overall status of
5330 				 * the operation(s).
5331 				 */
5332 				if (stat != SATA_SUCCESS)
5333 					/*
5334 					 * Page processing did not succeed -
5335 					 * all error info is already set-up,
5336 					 * just return
5337 					 */
5338 					pllen = 0; /* this breaks the loop */
5339 				else {
5340 					len += pagelen;
5341 					pllen -= pagelen;
5342 				}
5343 				break;
5344 
5345 			case MODEPAGE_INFO_EXCPT:
5346 				stat = sata_mode_select_page_1c(spx,
5347 				    (struct mode_info_excpt_page *)&buf[len],
5348 				    pllen, &pagelen, &rval, &dmod);
5349 				/*
5350 				 * The pagelen value indicates the number of
5351 				 * parameter bytes already processed.
5352 				 * The rval is the return value from
5353 				 * sata_tran_start().
5354 				 * The stat indicates the overall status of
5355 				 * the operation(s).
5356 				 */
5357 				if (stat != SATA_SUCCESS)
5358 					/*
5359 					 * Page processing did not succeed -
5360 					 * all error info is already set-up,
5361 					 * just return
5362 					 */
5363 					pllen = 0; /* this breaks the loop */
5364 				else {
5365 					len += pagelen;
5366 					pllen -= pagelen;
5367 				}
5368 				break;
5369 
5370 			case MODEPAGE_ACOUSTIC_MANAG:
5371 				stat = sata_mode_select_page_30(spx,
5372 				    (struct mode_acoustic_management *)
5373 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5374 				/*
5375 				 * The pagelen value indicates the number of
5376 				 * parameter bytes already processed.
5377 				 * The rval is the return value from
5378 				 * sata_tran_start().
5379 				 * The stat indicates the overall status of
5380 				 * the operation(s).
5381 				 */
5382 				if (stat != SATA_SUCCESS)
5383 					/*
5384 					 * Page processing did not succeed -
5385 					 * all error info is already set-up,
5386 					 * just return
5387 					 */
5388 					pllen = 0; /* this breaks the loop */
5389 				else {
5390 					len += pagelen;
5391 					pllen -= pagelen;
5392 				}
5393 
5394 				break;
5395 			case MODEPAGE_POWER_COND:
5396 				stat = sata_mode_select_page_1a(spx,
5397 				    (struct mode_info_power_cond *)&buf[len],
5398 				    pllen, &pagelen, &rval, &dmod);
5399 				/*
5400 				 * The pagelen value indicates the number of
5401 				 * parameter bytes already processed.
5402 				 * The rval is the return value from
5403 				 * sata_tran_start().
5404 				 * The stat indicates the overall status of
5405 				 * the operation(s).
5406 				 */
5407 				if (stat != SATA_SUCCESS)
5408 					/*
5409 					 * Page processing did not succeed -
5410 					 * all error info is already set-up,
5411 					 * just return
5412 					 */
5413 					pllen = 0; /* this breaks the loop */
5414 				else {
5415 					len += pagelen;
5416 					pllen -= pagelen;
5417 				}
5418 				break;
5419 			default:
5420 				*scsipkt->pkt_scbp = STATUS_CHECK;
5421 				sense = sata_arq_sense(spx);
5422 				sense->es_key = KEY_ILLEGAL_REQUEST;
5423 				sense->es_add_code =
5424 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5425 				goto done;
5426 			}
5427 		}
5428 	}
5429 done:
5430 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5431 	/*
5432 	 * If device parameters were modified, fetch and store the new
5433 	 * Identify Device data. Since port mutex could have been released
5434 	 * for accessing HBA driver, we need to re-check device existence.
5435 	 */
5436 	if (dmod != 0) {
5437 		sata_drive_info_t new_sdinfo, *sdinfo;
5438 		int rv = 0;
5439 
5440 		/*
5441 		 * Following statement has to be changed if this function is
5442 		 * used for devices other than SATA hard disks.
5443 		 */
5444 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5445 
5446 		new_sdinfo.satadrv_addr =
5447 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5448 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5449 		    &new_sdinfo);
5450 
5451 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5452 		/*
5453 		 * Since port mutex could have been released when
5454 		 * accessing HBA driver, we need to re-check that the
5455 		 * framework still holds the device info structure.
5456 		 */
5457 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5458 		    &spx->txlt_sata_pkt->satapkt_device);
5459 		if (sdinfo != NULL) {
5460 			/*
5461 			 * Device still has info structure in the
5462 			 * sata framework. Copy newly fetched info
5463 			 */
5464 			if (rv == 0) {
5465 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5466 				sata_save_drive_settings(sdinfo);
5467 			} else {
5468 				/*
5469 				 * Could not fetch new data - invalidate
5470 				 * sata_drive_info. That makes device
5471 				 * unusable.
5472 				 */
5473 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5474 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5475 			}
5476 		}
5477 		if (rv != 0 || sdinfo == NULL) {
5478 			/*
5479 			 * This changes the overall mode select completion
5480 			 * reason to a failed one !!!!!
5481 			 */
5482 			*scsipkt->pkt_scbp = STATUS_CHECK;
5483 			sense = sata_arq_sense(spx);
5484 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5485 			rval = TRAN_ACCEPT;
5486 		}
5487 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5488 	}
5489 	/* Restore the scsi pkt flags */
5490 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5491 	scsipkt->pkt_flags |= nointr_flag;
5492 
5493 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5494 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5495 
5496 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5497 	    scsipkt->pkt_comp != NULL) {
5498 		/* scsi callback required */
5499 		if (servicing_interrupt()) {
5500 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5501 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5502 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5503 				return (TRAN_BUSY);
5504 			}
5505 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5506 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5507 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5508 			/* Scheduling the callback failed */
5509 			return (TRAN_BUSY);
5510 		}
5511 	}
5512 
5513 	return (rval);
5514 }
5515 
5516 /*
5517  * Translate command: ATA Pass Through
5518  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5519  * PIO Data-Out protocols.  Also supports CK_COND bit.
5520  *
5521  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5522  * described in Table 111 of SAT-2 (Draft 9).
5523  */
5524 static  int
5525 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5526 {
5527 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5528 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5529 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5530 	int extend;
5531 	uint64_t lba;
5532 	uint16_t feature, sec_count;
5533 	int t_len, synch;
5534 	int rval, reason;
5535 
5536 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5537 
5538 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5539 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5540 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5541 		return (rval);
5542 	}
5543 
5544 	/* T_DIR bit */
5545 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5546 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5547 	else
5548 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5549 
5550 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5551 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5552 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5553 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5554 	}
5555 
5556 	/* OFFLINE field. If non-zero, invalid command (for now). */
5557 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5558 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5559 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5560 	}
5561 
5562 	/* PROTOCOL field */
5563 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5564 	case SATL_APT_P_HW_RESET:
5565 	case SATL_APT_P_SRST:
5566 	case SATL_APT_P_DMA:
5567 	case SATL_APT_P_DMA_QUEUED:
5568 	case SATL_APT_P_DEV_DIAG:
5569 	case SATL_APT_P_DEV_RESET:
5570 	case SATL_APT_P_UDMA_IN:
5571 	case SATL_APT_P_UDMA_OUT:
5572 	case SATL_APT_P_FPDMA:
5573 	case SATL_APT_P_RET_RESP:
5574 		/* Not yet implemented */
5575 	default:
5576 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5577 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5578 
5579 	case SATL_APT_P_NON_DATA:
5580 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5581 		break;
5582 
5583 	case SATL_APT_P_PIO_DATA_IN:
5584 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5585 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5586 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5587 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5588 		}
5589 
5590 		/* if there is a buffer, release its DMA resources */
5591 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5592 			sata_scsi_dmafree(NULL, scsipkt);
5593 		} else {
5594 			/* if there is no buffer, how do you PIO in? */
5595 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5596 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5597 		}
5598 
5599 		break;
5600 
5601 	case SATL_APT_P_PIO_DATA_OUT:
5602 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5603 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5604 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5605 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5606 		}
5607 
5608 		/* if there is a buffer, release its DMA resources */
5609 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5610 			sata_scsi_dmafree(NULL, scsipkt);
5611 		} else {
5612 			/* if there is no buffer, how do you PIO out? */
5613 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5614 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5615 		}
5616 
5617 		break;
5618 	}
5619 
5620 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5621 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5622 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5623 		feature = scsipkt->pkt_cdbp[3];
5624 
5625 		sec_count = scsipkt->pkt_cdbp[4];
5626 
5627 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5628 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5629 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5630 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5631 
5632 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5633 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5634 
5635 		break;
5636 
5637 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5638 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5639 			extend = 1;
5640 
5641 			feature = scsipkt->pkt_cdbp[3];
5642 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5643 
5644 			sec_count = scsipkt->pkt_cdbp[5];
5645 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5646 
5647 			lba = scsipkt->pkt_cdbp[11];
5648 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5649 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5650 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5651 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5652 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5653 
5654 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5655 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5656 		} else {
5657 			feature = scsipkt->pkt_cdbp[3];
5658 
5659 			sec_count = scsipkt->pkt_cdbp[5];
5660 
5661 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5662 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5663 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5664 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5665 
5666 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5667 			    0xf0;
5668 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5669 		}
5670 
5671 		break;
5672 	}
5673 
5674 	/* CK_COND bit */
5675 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5676 		if (extend) {
5677 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5678 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5679 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5680 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5681 		}
5682 
5683 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5684 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5685 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5686 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5687 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5688 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5689 	}
5690 
5691 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5692 	if (extend) {
5693 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5694 
5695 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5696 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5697 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5698 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5699 		scmd->satacmd_lba_high_msb = lba >> 40;
5700 	} else {
5701 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5702 
5703 		scmd->satacmd_features_reg_ext = 0;
5704 		scmd->satacmd_sec_count_msb = 0;
5705 		scmd->satacmd_lba_low_msb = 0;
5706 		scmd->satacmd_lba_mid_msb = 0;
5707 		scmd->satacmd_lba_high_msb = 0;
5708 	}
5709 
5710 	scmd->satacmd_features_reg = feature & 0xff;
5711 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5712 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5713 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5714 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5715 
5716 	/* Determine transfer length */
5717 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5718 	case 1:
5719 		t_len = feature;
5720 		break;
5721 	case 2:
5722 		t_len = sec_count;
5723 		break;
5724 	default:
5725 		t_len = 0;
5726 		break;
5727 	}
5728 
5729 	/* Adjust transfer length for the Byte Block bit */
5730 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5731 		t_len *= SATA_DISK_SECTOR_SIZE;
5732 
5733 	/* Start processing command */
5734 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5735 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5736 		synch = FALSE;
5737 	} else {
5738 		synch = TRUE;
5739 	}
5740 
5741 	if (sata_hba_start(spx, &rval) != 0) {
5742 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5743 		return (rval);
5744 	}
5745 
5746 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5747 
5748 	if (synch) {
5749 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5750 	}
5751 
5752 	return (TRAN_ACCEPT);
5753 }
5754 
5755 /*
5756  * Translate command: Log Sense
5757  */
5758 static 	int
5759 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5760 {
5761 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5762 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5763 	sata_drive_info_t *sdinfo;
5764 	struct scsi_extended_sense *sense;
5765 	int 		len, count, alc_len;
5766 	int		pc;	/* Page Control code */
5767 	int		page_code;	/* Page code */
5768 	uint8_t		*buf;	/* log sense buffer */
5769 	int		rval, reason;
5770 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5771 
5772 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5773 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5774 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5775 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5776 
5777 	if (servicing_interrupt()) {
5778 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5779 		if (buf == NULL) {
5780 			return (TRAN_BUSY);
5781 		}
5782 	} else {
5783 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5784 	}
5785 
5786 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5787 
5788 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5789 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5790 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5791 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5792 		return (rval);
5793 	}
5794 
5795 	scsipkt->pkt_reason = CMD_CMPLT;
5796 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5797 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5798 
5799 	pc = scsipkt->pkt_cdbp[2] >> 6;
5800 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5801 
5802 	/* Reject not supported request for all but cumulative values */
5803 	switch (pc) {
5804 	case PC_CUMULATIVE_VALUES:
5805 		break;
5806 	default:
5807 		*scsipkt->pkt_scbp = STATUS_CHECK;
5808 		sense = sata_arq_sense(spx);
5809 		sense->es_key = KEY_ILLEGAL_REQUEST;
5810 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5811 		goto done;
5812 	}
5813 
5814 	switch (page_code) {
5815 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5816 	case PAGE_CODE_SELF_TEST_RESULTS:
5817 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5818 	case PAGE_CODE_SMART_READ_DATA:
5819 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5820 		break;
5821 	default:
5822 		*scsipkt->pkt_scbp = STATUS_CHECK;
5823 		sense = sata_arq_sense(spx);
5824 		sense->es_key = KEY_ILLEGAL_REQUEST;
5825 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5826 		goto done;
5827 	}
5828 
5829 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5830 		/*
5831 		 * Because log sense uses local buffers for data retrieval from
5832 		 * the devices and sets the data programatically in the
5833 		 * original specified buffer, release preallocated DMA
5834 		 * resources before storing data in the original buffer,
5835 		 * so no unwanted DMA sync would take place.
5836 		 */
5837 		sata_id_t *sata_id;
5838 
5839 		sata_scsi_dmafree(NULL, scsipkt);
5840 
5841 		len = 0;
5842 
5843 		/* Build log parameter header */
5844 		buf[len++] = page_code;	/* page code as in the CDB */
5845 		buf[len++] = 0;		/* reserved */
5846 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5847 		buf[len++] = 0;		/* (LSB) */
5848 
5849 		sdinfo = sata_get_device_info(
5850 		    spx->txlt_sata_hba_inst,
5851 		    &spx->txlt_sata_pkt->satapkt_device);
5852 
5853 		/*
5854 		 * Add requested pages.
5855 		 */
5856 		switch (page_code) {
5857 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5858 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5859 			break;
5860 		case PAGE_CODE_SELF_TEST_RESULTS:
5861 			sata_id = &sdinfo->satadrv_id;
5862 			if ((! (sata_id->ai_cmdset84 &
5863 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5864 			    (! (sata_id->ai_features87 &
5865 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5866 				*scsipkt->pkt_scbp = STATUS_CHECK;
5867 				sense = sata_arq_sense(spx);
5868 				sense->es_key = KEY_ILLEGAL_REQUEST;
5869 				sense->es_add_code =
5870 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5871 
5872 				goto done;
5873 			}
5874 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5875 			    spx->txlt_sata_hba_inst);
5876 			break;
5877 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5878 			sata_id = &sdinfo->satadrv_id;
5879 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5880 				*scsipkt->pkt_scbp = STATUS_CHECK;
5881 				sense = sata_arq_sense(spx);
5882 				sense->es_key = KEY_ILLEGAL_REQUEST;
5883 				sense->es_add_code =
5884 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5885 
5886 				goto done;
5887 			}
5888 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5889 				*scsipkt->pkt_scbp = STATUS_CHECK;
5890 				sense = sata_arq_sense(spx);
5891 				sense->es_key = KEY_ABORTED_COMMAND;
5892 				sense->es_add_code =
5893 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5894 				sense->es_qual_code =
5895 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5896 
5897 				goto done;
5898 			}
5899 
5900 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5901 			    spx->txlt_sata_hba_inst);
5902 			break;
5903 		case PAGE_CODE_SMART_READ_DATA:
5904 			sata_id = &sdinfo->satadrv_id;
5905 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5906 				*scsipkt->pkt_scbp = STATUS_CHECK;
5907 				sense = sata_arq_sense(spx);
5908 				sense->es_key = KEY_ILLEGAL_REQUEST;
5909 				sense->es_add_code =
5910 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5911 
5912 				goto done;
5913 			}
5914 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5915 				*scsipkt->pkt_scbp = STATUS_CHECK;
5916 				sense = sata_arq_sense(spx);
5917 				sense->es_key = KEY_ABORTED_COMMAND;
5918 				sense->es_add_code =
5919 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5920 				sense->es_qual_code =
5921 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5922 
5923 				goto done;
5924 			}
5925 
5926 			/* This page doesn't include a page header */
5927 			len = sata_build_lsense_page_30(sdinfo, buf,
5928 			    spx->txlt_sata_hba_inst);
5929 			goto no_header;
5930 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5931 			sata_id = &sdinfo->satadrv_id;
5932 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5933 				*scsipkt->pkt_scbp = STATUS_CHECK;
5934 				sense = sata_arq_sense(spx);
5935 				sense->es_key = KEY_ILLEGAL_REQUEST;
5936 				sense->es_add_code =
5937 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5938 
5939 				goto done;
5940 			}
5941 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5942 				*scsipkt->pkt_scbp = STATUS_CHECK;
5943 				sense = sata_arq_sense(spx);
5944 				sense->es_key = KEY_ABORTED_COMMAND;
5945 				sense->es_add_code =
5946 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5947 				sense->es_qual_code =
5948 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5949 
5950 				goto done;
5951 			}
5952 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5953 			goto no_header;
5954 		default:
5955 			/* Invalid request */
5956 			*scsipkt->pkt_scbp = STATUS_CHECK;
5957 			sense = sata_arq_sense(spx);
5958 			sense->es_key = KEY_ILLEGAL_REQUEST;
5959 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5960 			goto done;
5961 		}
5962 
5963 		/* set parameter log sense data length */
5964 		buf[2] = len >> 8;	/* log sense length (MSB) */
5965 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5966 
5967 		len += SCSI_LOG_PAGE_HDR_LEN;
5968 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5969 
5970 no_header:
5971 		/* Check allocation length */
5972 		alc_len = scsipkt->pkt_cdbp[7];
5973 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5974 
5975 		/*
5976 		 * We do not check for possible parameters truncation
5977 		 * (alc_len < len) assuming that the target driver works
5978 		 * correctly. Just avoiding overrun.
5979 		 * Copy no more than requested and possible, buffer-wise.
5980 		 */
5981 		count = MIN(alc_len, len);
5982 		count = MIN(bp->b_bcount, count);
5983 		bcopy(buf, bp->b_un.b_addr, count);
5984 
5985 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5986 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5987 	}
5988 	*scsipkt->pkt_scbp = STATUS_GOOD;
5989 done:
5990 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5991 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5992 
5993 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5994 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5995 
5996 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5997 	    scsipkt->pkt_comp != NULL) {
5998 		/* scsi callback required */
5999 		if (servicing_interrupt()) {
6000 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6001 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6002 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6003 				return (TRAN_BUSY);
6004 			}
6005 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6006 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6007 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6008 			/* Scheduling the callback failed */
6009 			return (TRAN_BUSY);
6010 		}
6011 	}
6012 
6013 	return (TRAN_ACCEPT);
6014 }
6015 
6016 /*
6017  * Translate command: Log Select
6018  * Not implemented at this time - returns invalid command response.
6019  */
6020 static	int
6021 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6022 {
6023 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6024 	    "sata_txlt_log_select\n", NULL);
6025 
6026 	return (sata_txlt_invalid_command(spx));
6027 }
6028 
6029 
6030 /*
6031  * Translate command: Read (various types).
6032  * Translated into appropriate type of ATA READ command
6033  * for SATA hard disks.
6034  * Both the device capabilities and requested operation mode are
6035  * considered.
6036  *
6037  * Following scsi cdb fields are ignored:
6038  * rdprotect, dpo, fua, fua_nv, group_number.
6039  *
6040  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6041  * enable variable sata_func_enable), the capability of the controller and
6042  * capability of a device are checked and if both support queueing, read
6043  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6044  * command rather than plain READ_XXX command.
6045  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6046  * both the controller and device suport such functionality, the read
6047  * request will be translated to READ_FPDMA_QUEUED command.
6048  * In both cases the maximum queue depth is derived as minimum of:
6049  * HBA capability,device capability and sata_max_queue_depth variable setting.
6050  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6051  * used to pass max queue depth value, and the maximum possible queue depth
6052  * is 32.
6053  *
6054  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6055  * appropriate values in scsi_pkt fields.
6056  */
6057 static int
6058 sata_txlt_read(sata_pkt_txlate_t *spx)
6059 {
6060 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6061 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6062 	sata_drive_info_t *sdinfo;
6063 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6064 	int cport = SATA_TXLT_CPORT(spx);
6065 	uint16_t sec_count;
6066 	uint64_t lba;
6067 	int rval, reason;
6068 	int synch;
6069 
6070 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6071 
6072 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6073 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6074 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6075 		return (rval);
6076 	}
6077 
6078 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6079 	    &spx->txlt_sata_pkt->satapkt_device);
6080 
6081 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6082 	/*
6083 	 * Extract LBA and sector count from scsi CDB.
6084 	 */
6085 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6086 	case SCMD_READ:
6087 		/* 6-byte scsi read cmd : 0x08 */
6088 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6089 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6090 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6091 		sec_count = scsipkt->pkt_cdbp[4];
6092 		/* sec_count 0 will be interpreted as 256 by a device */
6093 		break;
6094 	case SCMD_READ_G1:
6095 		/* 10-bytes scsi read command : 0x28 */
6096 		lba = scsipkt->pkt_cdbp[2];
6097 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6098 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6099 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6100 		sec_count = scsipkt->pkt_cdbp[7];
6101 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6102 		break;
6103 	case SCMD_READ_G5:
6104 		/* 12-bytes scsi read command : 0xA8 */
6105 		lba = scsipkt->pkt_cdbp[2];
6106 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6107 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6108 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6109 		sec_count = scsipkt->pkt_cdbp[6];
6110 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6111 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6112 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6113 		break;
6114 	case SCMD_READ_G4:
6115 		/* 16-bytes scsi read command : 0x88 */
6116 		lba = scsipkt->pkt_cdbp[2];
6117 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6118 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6119 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6120 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6121 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6122 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6123 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6124 		sec_count = scsipkt->pkt_cdbp[10];
6125 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6126 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6127 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6128 		break;
6129 	default:
6130 		/* Unsupported command */
6131 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6132 		return (sata_txlt_invalid_command(spx));
6133 	}
6134 
6135 	/*
6136 	 * Check if specified address exceeds device capacity
6137 	 */
6138 	if ((lba >= sdinfo->satadrv_capacity) ||
6139 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6140 		/* LBA out of range */
6141 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6142 		return (sata_txlt_lba_out_of_range(spx));
6143 	}
6144 
6145 	/*
6146 	 * For zero-length transfer, emulate good completion of the command
6147 	 * (reasons for rejecting the command were already checked).
6148 	 * No DMA resources were allocated.
6149 	 */
6150 	if (spx->txlt_dma_cookie_list == NULL) {
6151 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6152 		return (sata_emul_rw_completion(spx));
6153 	}
6154 
6155 	/*
6156 	 * Build cmd block depending on the device capability and
6157 	 * requested operation mode.
6158 	 * Do not bother with non-dma mode - we are working only with
6159 	 * devices supporting DMA.
6160 	 */
6161 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6162 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6163 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6164 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6165 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6166 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6167 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6168 #ifndef __lock_lint
6169 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6170 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6171 		scmd->satacmd_lba_high_msb = lba >> 40;
6172 #endif
6173 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6174 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6175 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6176 	}
6177 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6178 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6179 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6180 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6181 	scmd->satacmd_features_reg = 0;
6182 	scmd->satacmd_status_reg = 0;
6183 	scmd->satacmd_error_reg = 0;
6184 
6185 	/*
6186 	 * Check if queueing commands should be used and switch
6187 	 * to appropriate command if possible
6188 	 */
6189 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6190 		boolean_t using_queuing;
6191 
6192 		/* Queuing supported by controller and device? */
6193 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6194 		    (sdinfo->satadrv_features_support &
6195 		    SATA_DEV_F_NCQ) &&
6196 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6197 		    SATA_CTLF_NCQ)) {
6198 			using_queuing = B_TRUE;
6199 
6200 			/* NCQ supported - use FPDMA READ */
6201 			scmd->satacmd_cmd_reg =
6202 			    SATAC_READ_FPDMA_QUEUED;
6203 			scmd->satacmd_features_reg_ext =
6204 			    scmd->satacmd_sec_count_msb;
6205 			scmd->satacmd_sec_count_msb = 0;
6206 		} else if ((sdinfo->satadrv_features_support &
6207 		    SATA_DEV_F_TCQ) &&
6208 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6209 		    SATA_CTLF_QCMD)) {
6210 			using_queuing = B_TRUE;
6211 
6212 			/* Legacy queueing */
6213 			if (sdinfo->satadrv_features_support &
6214 			    SATA_DEV_F_LBA48) {
6215 				scmd->satacmd_cmd_reg =
6216 				    SATAC_READ_DMA_QUEUED_EXT;
6217 				scmd->satacmd_features_reg_ext =
6218 				    scmd->satacmd_sec_count_msb;
6219 				scmd->satacmd_sec_count_msb = 0;
6220 			} else {
6221 				scmd->satacmd_cmd_reg =
6222 				    SATAC_READ_DMA_QUEUED;
6223 			}
6224 		} else	/* NCQ nor legacy queuing not supported */
6225 			using_queuing = B_FALSE;
6226 
6227 		/*
6228 		 * If queuing, the sector count goes in the features register
6229 		 * and the secount count will contain the tag.
6230 		 */
6231 		if (using_queuing) {
6232 			scmd->satacmd_features_reg =
6233 			    scmd->satacmd_sec_count_lsb;
6234 			scmd->satacmd_sec_count_lsb = 0;
6235 			scmd->satacmd_flags.sata_queued = B_TRUE;
6236 
6237 			/* Set-up maximum queue depth */
6238 			scmd->satacmd_flags.sata_max_queue_depth =
6239 			    sdinfo->satadrv_max_queue_depth - 1;
6240 		} else if (sdinfo->satadrv_features_enabled &
6241 		    SATA_DEV_F_E_UNTAGGED_QING) {
6242 			/*
6243 			 * Although NCQ/TCQ is not enabled, untagged queuing
6244 			 * may be still used.
6245 			 * Set-up the maximum untagged queue depth.
6246 			 * Use controller's queue depth from sata_hba_tran.
6247 			 * SATA HBA drivers may ignore this value and rely on
6248 			 * the internal limits.For drivers that do not
6249 			 * ignore untaged queue depth, limit the value to
6250 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6251 			 * largest value that can be passed via
6252 			 * satacmd_flags.sata_max_queue_depth.
6253 			 */
6254 			scmd->satacmd_flags.sata_max_queue_depth =
6255 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6256 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6257 
6258 		} else {
6259 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6260 		}
6261 	} else
6262 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6263 
6264 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6265 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6266 	    scmd->satacmd_cmd_reg, lba, sec_count);
6267 
6268 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6269 		/* Need callback function */
6270 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6271 		synch = FALSE;
6272 	} else
6273 		synch = TRUE;
6274 
6275 	/* Transfer command to HBA */
6276 	if (sata_hba_start(spx, &rval) != 0) {
6277 		/* Pkt not accepted for execution */
6278 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6279 		return (rval);
6280 	}
6281 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6282 	/*
6283 	 * If execution is non-synchronous,
6284 	 * a callback function will handle potential errors, translate
6285 	 * the response and will do a callback to a target driver.
6286 	 * If it was synchronous, check execution status using the same
6287 	 * framework callback.
6288 	 */
6289 	if (synch) {
6290 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6291 		    "synchronous execution status %x\n",
6292 		    spx->txlt_sata_pkt->satapkt_reason);
6293 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6294 	}
6295 	return (TRAN_ACCEPT);
6296 }
6297 
6298 
6299 /*
6300  * SATA translate command: Write (various types)
6301  * Translated into appropriate type of ATA WRITE command
6302  * for SATA hard disks.
6303  * Both the device capabilities and requested operation mode are
6304  * considered.
6305  *
6306  * Following scsi cdb fields are ignored:
6307  * rwprotect, dpo, fua, fua_nv, group_number.
6308  *
6309  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6310  * enable variable sata_func_enable), the capability of the controller and
6311  * capability of a device are checked and if both support queueing, write
6312  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6313  * command rather than plain WRITE_XXX command.
6314  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6315  * both the controller and device suport such functionality, the write
6316  * request will be translated to WRITE_FPDMA_QUEUED command.
6317  * In both cases the maximum queue depth is derived as minimum of:
6318  * HBA capability,device capability and sata_max_queue_depth variable setting.
6319  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6320  * used to pass max queue depth value, and the maximum possible queue depth
6321  * is 32.
6322  *
6323  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6324  * appropriate values in scsi_pkt fields.
6325  */
6326 static int
6327 sata_txlt_write(sata_pkt_txlate_t *spx)
6328 {
6329 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6330 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6331 	sata_drive_info_t *sdinfo;
6332 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6333 	int cport = SATA_TXLT_CPORT(spx);
6334 	uint16_t sec_count;
6335 	uint64_t lba;
6336 	int rval, reason;
6337 	int synch;
6338 
6339 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6340 
6341 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6342 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6343 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6344 		return (rval);
6345 	}
6346 
6347 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6348 	    &spx->txlt_sata_pkt->satapkt_device);
6349 
6350 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6351 	/*
6352 	 * Extract LBA and sector count from scsi CDB
6353 	 */
6354 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6355 	case SCMD_WRITE:
6356 		/* 6-byte scsi read cmd : 0x0A */
6357 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6358 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6359 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6360 		sec_count = scsipkt->pkt_cdbp[4];
6361 		/* sec_count 0 will be interpreted as 256 by a device */
6362 		break;
6363 	case SCMD_WRITE_G1:
6364 		/* 10-bytes scsi write command : 0x2A */
6365 		lba = scsipkt->pkt_cdbp[2];
6366 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6367 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6368 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6369 		sec_count = scsipkt->pkt_cdbp[7];
6370 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6371 		break;
6372 	case SCMD_WRITE_G5:
6373 		/* 12-bytes scsi read command : 0xAA */
6374 		lba = scsipkt->pkt_cdbp[2];
6375 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6376 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6377 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6378 		sec_count = scsipkt->pkt_cdbp[6];
6379 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6380 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6381 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6382 		break;
6383 	case SCMD_WRITE_G4:
6384 		/* 16-bytes scsi write command : 0x8A */
6385 		lba = scsipkt->pkt_cdbp[2];
6386 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6387 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6388 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6389 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6390 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6391 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6392 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6393 		sec_count = scsipkt->pkt_cdbp[10];
6394 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6395 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6396 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6397 		break;
6398 	default:
6399 		/* Unsupported command */
6400 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6401 		return (sata_txlt_invalid_command(spx));
6402 	}
6403 
6404 	/*
6405 	 * Check if specified address and length exceeds device capacity
6406 	 */
6407 	if ((lba >= sdinfo->satadrv_capacity) ||
6408 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6409 		/* LBA out of range */
6410 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6411 		return (sata_txlt_lba_out_of_range(spx));
6412 	}
6413 
6414 	/*
6415 	 * For zero-length transfer, emulate good completion of the command
6416 	 * (reasons for rejecting the command were already checked).
6417 	 * No DMA resources were allocated.
6418 	 */
6419 	if (spx->txlt_dma_cookie_list == NULL) {
6420 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6421 		return (sata_emul_rw_completion(spx));
6422 	}
6423 
6424 	/*
6425 	 * Build cmd block depending on the device capability and
6426 	 * requested operation mode.
6427 	 * Do not bother with non-dma mode- we are working only with
6428 	 * devices supporting DMA.
6429 	 */
6430 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6431 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6432 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6433 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6434 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6435 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6436 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6437 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6438 #ifndef __lock_lint
6439 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6440 		scmd->satacmd_lba_high_msb = lba >> 40;
6441 #endif
6442 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6443 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6444 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6445 	}
6446 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6447 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6448 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6449 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6450 	scmd->satacmd_features_reg = 0;
6451 	scmd->satacmd_status_reg = 0;
6452 	scmd->satacmd_error_reg = 0;
6453 
6454 	/*
6455 	 * Check if queueing commands should be used and switch
6456 	 * to appropriate command if possible
6457 	 */
6458 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6459 		boolean_t using_queuing;
6460 
6461 		/* Queuing supported by controller and device? */
6462 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6463 		    (sdinfo->satadrv_features_support &
6464 		    SATA_DEV_F_NCQ) &&
6465 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6466 		    SATA_CTLF_NCQ)) {
6467 			using_queuing = B_TRUE;
6468 
6469 			/* NCQ supported - use FPDMA WRITE */
6470 			scmd->satacmd_cmd_reg =
6471 			    SATAC_WRITE_FPDMA_QUEUED;
6472 			scmd->satacmd_features_reg_ext =
6473 			    scmd->satacmd_sec_count_msb;
6474 			scmd->satacmd_sec_count_msb = 0;
6475 		} else if ((sdinfo->satadrv_features_support &
6476 		    SATA_DEV_F_TCQ) &&
6477 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6478 		    SATA_CTLF_QCMD)) {
6479 			using_queuing = B_TRUE;
6480 
6481 			/* Legacy queueing */
6482 			if (sdinfo->satadrv_features_support &
6483 			    SATA_DEV_F_LBA48) {
6484 				scmd->satacmd_cmd_reg =
6485 				    SATAC_WRITE_DMA_QUEUED_EXT;
6486 				scmd->satacmd_features_reg_ext =
6487 				    scmd->satacmd_sec_count_msb;
6488 				scmd->satacmd_sec_count_msb = 0;
6489 			} else {
6490 				scmd->satacmd_cmd_reg =
6491 				    SATAC_WRITE_DMA_QUEUED;
6492 			}
6493 		} else	/*  NCQ nor legacy queuing not supported */
6494 			using_queuing = B_FALSE;
6495 
6496 		if (using_queuing) {
6497 			scmd->satacmd_features_reg =
6498 			    scmd->satacmd_sec_count_lsb;
6499 			scmd->satacmd_sec_count_lsb = 0;
6500 			scmd->satacmd_flags.sata_queued = B_TRUE;
6501 			/* Set-up maximum queue depth */
6502 			scmd->satacmd_flags.sata_max_queue_depth =
6503 			    sdinfo->satadrv_max_queue_depth - 1;
6504 		} else if (sdinfo->satadrv_features_enabled &
6505 		    SATA_DEV_F_E_UNTAGGED_QING) {
6506 			/*
6507 			 * Although NCQ/TCQ is not enabled, untagged queuing
6508 			 * may be still used.
6509 			 * Set-up the maximum untagged queue depth.
6510 			 * Use controller's queue depth from sata_hba_tran.
6511 			 * SATA HBA drivers may ignore this value and rely on
6512 			 * the internal limits. For drivera that do not
6513 			 * ignore untaged queue depth, limit the value to
6514 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6515 			 * largest value that can be passed via
6516 			 * satacmd_flags.sata_max_queue_depth.
6517 			 */
6518 			scmd->satacmd_flags.sata_max_queue_depth =
6519 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6520 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6521 
6522 		} else {
6523 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6524 		}
6525 	} else
6526 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6527 
6528 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6529 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6530 	    scmd->satacmd_cmd_reg, lba, sec_count);
6531 
6532 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6533 		/* Need callback function */
6534 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6535 		synch = FALSE;
6536 	} else
6537 		synch = TRUE;
6538 
6539 	/* Transfer command to HBA */
6540 	if (sata_hba_start(spx, &rval) != 0) {
6541 		/* Pkt not accepted for execution */
6542 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6543 		return (rval);
6544 	}
6545 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6546 
6547 	/*
6548 	 * If execution is non-synchronous,
6549 	 * a callback function will handle potential errors, translate
6550 	 * the response and will do a callback to a target driver.
6551 	 * If it was synchronous, check execution status using the same
6552 	 * framework callback.
6553 	 */
6554 	if (synch) {
6555 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6556 		    "synchronous execution status %x\n",
6557 		    spx->txlt_sata_pkt->satapkt_reason);
6558 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6559 	}
6560 	return (TRAN_ACCEPT);
6561 }
6562 
6563 
6564 /*
6565  * Implements SCSI SBC WRITE BUFFER command download microcode option
6566  */
6567 static int
6568 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6569 {
6570 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6571 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6572 
6573 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
6574 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6575 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6576 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6577 
6578 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6579 	struct scsi_extended_sense *sense;
6580 	int rval, mode, sector_count, reason;
6581 	int cport = SATA_TXLT_CPORT(spx);
6582 
6583 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6584 
6585 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6586 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6587 
6588 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6589 
6590 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6591 	    TRAN_ACCEPT) {
6592 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6593 		return (rval);
6594 	}
6595 
6596 	/* Use synchronous mode */
6597 	spx->txlt_sata_pkt->satapkt_op_mode
6598 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6599 
6600 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6601 
6602 	scsipkt->pkt_reason = CMD_CMPLT;
6603 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6604 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6605 
6606 	/*
6607 	 * The SCSI to ATA translation specification only calls
6608 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6609 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6610 	 * ATA 8 (draft) got rid of download microcode for temp
6611 	 * and it is even optional for ATA 7, so it may be aborted.
6612 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6613 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6614 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6615 	 * sectors.  Thus the offset really doesn't buy us anything.
6616 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6617 	 * is revised, this can be revisisted.
6618 	 */
6619 	/* Reject not supported request */
6620 	switch (mode) {
6621 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6622 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6623 		break;
6624 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6625 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6626 		break;
6627 	default:
6628 		goto bad_param;
6629 	}
6630 
6631 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6632 
6633 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6634 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6635 		goto bad_param;
6636 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6637 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6638 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6639 	scmd->satacmd_lba_mid_lsb = 0;
6640 	scmd->satacmd_lba_high_lsb = 0;
6641 	scmd->satacmd_device_reg = 0;
6642 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6643 	scmd->satacmd_addr_type = 0;
6644 
6645 	/* Transfer command to HBA */
6646 	if (sata_hba_start(spx, &rval) != 0) {
6647 		/* Pkt not accepted for execution */
6648 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
6649 		return (rval);
6650 	}
6651 
6652 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
6653 
6654 	/* Then we need synchronous check the status of the disk */
6655 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6656 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6657 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6658 		scsipkt->pkt_reason = CMD_CMPLT;
6659 
6660 		/* Download commmand succeed, so probe and identify device */
6661 		sata_reidentify_device(spx);
6662 	} else {
6663 		/* Something went wrong, microcode download command failed */
6664 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6665 		*scsipkt->pkt_scbp = STATUS_CHECK;
6666 		sense = sata_arq_sense(spx);
6667 		switch (sata_pkt->satapkt_reason) {
6668 		case SATA_PKT_PORT_ERROR:
6669 			/*
6670 			 * We have no device data. Assume no data transfered.
6671 			 */
6672 			sense->es_key = KEY_HARDWARE_ERROR;
6673 			break;
6674 
6675 		case SATA_PKT_DEV_ERROR:
6676 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6677 			    SATA_STATUS_ERR) {
6678 				/*
6679 				 * determine dev error reason from error
6680 				 * reg content
6681 				 */
6682 				sata_decode_device_error(spx, sense);
6683 				break;
6684 			}
6685 			/* No extended sense key - no info available */
6686 			break;
6687 
6688 		case SATA_PKT_TIMEOUT:
6689 			scsipkt->pkt_reason = CMD_TIMEOUT;
6690 			scsipkt->pkt_statistics |=
6691 			    STAT_TIMEOUT | STAT_DEV_RESET;
6692 			/* No extended sense key ? */
6693 			break;
6694 
6695 		case SATA_PKT_ABORTED:
6696 			scsipkt->pkt_reason = CMD_ABORTED;
6697 			scsipkt->pkt_statistics |= STAT_ABORTED;
6698 			/* No extended sense key ? */
6699 			break;
6700 
6701 		case SATA_PKT_RESET:
6702 			/* pkt aborted by an explicit reset from a host */
6703 			scsipkt->pkt_reason = CMD_RESET;
6704 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6705 			break;
6706 
6707 		default:
6708 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6709 			    "sata_txlt_nodata_cmd_completion: "
6710 			    "invalid packet completion reason %d",
6711 			    sata_pkt->satapkt_reason));
6712 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6713 			break;
6714 		}
6715 
6716 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6717 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6718 
6719 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6720 			/* scsi callback required */
6721 			scsi_hba_pkt_comp(scsipkt);
6722 	}
6723 	return (TRAN_ACCEPT);
6724 
6725 bad_param:
6726 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6727 	*scsipkt->pkt_scbp = STATUS_CHECK;
6728 	sense = sata_arq_sense(spx);
6729 	sense->es_key = KEY_ILLEGAL_REQUEST;
6730 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6731 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6732 	    scsipkt->pkt_comp != NULL) {
6733 		/* scsi callback required */
6734 		if (servicing_interrupt()) {
6735 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6736 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6737 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6738 				return (TRAN_BUSY);
6739 			}
6740 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6741 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6742 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6743 			/* Scheduling the callback failed */
6744 			return (TRAN_BUSY);
6745 		}
6746 	}
6747 	return (rval);
6748 }
6749 
6750 /*
6751  * Re-identify device after doing a firmware download.
6752  */
6753 static void
6754 sata_reidentify_device(sata_pkt_txlate_t *spx)
6755 {
6756 #define	DOWNLOAD_WAIT_TIME_SECS	60
6757 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6758 	int rval;
6759 	int retry_cnt;
6760 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6761 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6762 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6763 	sata_drive_info_t *sdinfo;
6764 
6765 	/*
6766 	 * Before returning good status, probe device.
6767 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6768 	 * The assumption is that the new microcode is applied by the
6769 	 * device. It is a caller responsibility to verify this.
6770 	 */
6771 	for (retry_cnt = 0;
6772 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6773 	    retry_cnt++) {
6774 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6775 
6776 		if (rval == SATA_SUCCESS) { /* Set default features */
6777 			sdinfo = sata_get_device_info(sata_hba_inst,
6778 			    &sata_device);
6779 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6780 			    SATA_SUCCESS) {
6781 				/* retry */
6782 				rval = sata_initialize_device(sata_hba_inst,
6783 				    sdinfo);
6784 				if (rval == SATA_RETRY)
6785 					sata_log(sata_hba_inst, CE_WARN,
6786 					    "SATA device at port %d pmport %d -"
6787 					    " default device features could not"
6788 					    " be set. Device may not operate "
6789 					    "as expected.",
6790 					    sata_device.satadev_addr.cport,
6791 					    sata_device.satadev_addr.pmport);
6792 			}
6793 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6794 				scsi_hba_pkt_comp(scsipkt);
6795 			return;
6796 		} else if (rval == SATA_RETRY) {
6797 			delay(drv_usectohz(1000000 *
6798 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6799 			continue;
6800 		} else	/* failed - no reason to retry */
6801 			break;
6802 	}
6803 
6804 	/*
6805 	 * Something went wrong, device probing failed.
6806 	 */
6807 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6808 	    "Cannot probe device after downloading microcode\n"));
6809 
6810 	/* Reset device to force retrying the probe. */
6811 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6812 	    (SATA_DIP(sata_hba_inst), &sata_device);
6813 
6814 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6815 		scsi_hba_pkt_comp(scsipkt);
6816 }
6817 
6818 
6819 /*
6820  * Translate command: Synchronize Cache.
6821  * Translates into Flush Cache command for SATA hard disks.
6822  *
6823  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6824  * appropriate values in scsi_pkt fields.
6825  */
6826 static 	int
6827 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6828 {
6829 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6830 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6831 	int cport = SATA_TXLT_CPORT(spx);
6832 	int rval, reason;
6833 	int synch;
6834 
6835 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6836 
6837 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6838 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6839 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6840 		return (rval);
6841 	}
6842 
6843 	scmd->satacmd_addr_type = 0;
6844 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6845 	scmd->satacmd_device_reg = 0;
6846 	scmd->satacmd_sec_count_lsb = 0;
6847 	scmd->satacmd_lba_low_lsb = 0;
6848 	scmd->satacmd_lba_mid_lsb = 0;
6849 	scmd->satacmd_lba_high_lsb = 0;
6850 	scmd->satacmd_features_reg = 0;
6851 	scmd->satacmd_status_reg = 0;
6852 	scmd->satacmd_error_reg = 0;
6853 
6854 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6855 	    "sata_txlt_synchronize_cache\n", NULL);
6856 
6857 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6858 		/* Need to set-up a callback function */
6859 		spx->txlt_sata_pkt->satapkt_comp =
6860 		    sata_txlt_nodata_cmd_completion;
6861 		synch = FALSE;
6862 	} else
6863 		synch = TRUE;
6864 
6865 	/* Transfer command to HBA */
6866 	if (sata_hba_start(spx, &rval) != 0) {
6867 		/* Pkt not accepted for execution */
6868 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6869 		return (rval);
6870 	}
6871 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6872 
6873 	/*
6874 	 * If execution non-synchronous, it had to be completed
6875 	 * a callback function will handle potential errors, translate
6876 	 * the response and will do a callback to a target driver.
6877 	 * If it was synchronous, check status, using the same
6878 	 * framework callback.
6879 	 */
6880 	if (synch) {
6881 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6882 		    "synchronous execution status %x\n",
6883 		    spx->txlt_sata_pkt->satapkt_reason);
6884 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6885 	}
6886 	return (TRAN_ACCEPT);
6887 }
6888 
6889 
6890 /*
6891  * Send pkt to SATA HBA driver
6892  *
6893  * This function may be called only if the operation is requested by scsi_pkt,
6894  * i.e. scsi_pkt is not NULL.
6895  *
6896  * This function has to be called with cport mutex held. It does release
6897  * the mutex when it calls HBA driver sata_tran_start function and
6898  * re-acquires it afterwards.
6899  *
6900  * If return value is 0, pkt was accepted, -1 otherwise
6901  * rval is set to appropriate sata_scsi_start return value.
6902  *
6903  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6904  * have called the sata_pkt callback function for this packet.
6905  *
6906  * The scsi callback has to be performed by the caller of this routine.
6907  */
6908 static int
6909 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6910 {
6911 	int stat;
6912 	uint8_t cport = SATA_TXLT_CPORT(spx);
6913 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6914 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6915 	sata_drive_info_t *sdinfo;
6916 	sata_pmult_info_t *pminfo;
6917 	sata_pmport_info_t *pmportinfo = NULL;
6918 	sata_device_t *sata_device = NULL;
6919 	uint8_t cmd;
6920 	struct sata_cmd_flags cmd_flags;
6921 
6922 	ASSERT(spx->txlt_sata_pkt != NULL);
6923 
6924 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6925 
6926 	sdinfo = sata_get_device_info(sata_hba_inst,
6927 	    &spx->txlt_sata_pkt->satapkt_device);
6928 	ASSERT(sdinfo != NULL);
6929 
6930 	/* Clear device reset state? */
6931 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6932 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6933 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6934 
6935 		/*
6936 		 * Get the pmult_info of the its parent port multiplier, all
6937 		 * sub-devices share a common device reset flags on in
6938 		 * pmult_info.
6939 		 */
6940 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6941 		pmportinfo = pminfo->pmult_dev_port[pmport];
6942 		ASSERT(pminfo != NULL);
6943 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6944 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6945 			    sata_clear_dev_reset = B_TRUE;
6946 			pminfo->pmult_event_flags &=
6947 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6948 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6949 			    "sata_hba_start: clearing device reset state"
6950 			    "on pmult.\n", NULL);
6951 		}
6952 	} else {
6953 		if (sdinfo->satadrv_event_flags &
6954 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6955 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6956 			    sata_clear_dev_reset = B_TRUE;
6957 			sdinfo->satadrv_event_flags &=
6958 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6959 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6960 			    "sata_hba_start: clearing device reset state\n",
6961 			    NULL);
6962 		}
6963 	}
6964 
6965 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6966 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6967 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6968 
6969 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6970 
6971 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6972 	    "Sata cmd 0x%2x\n", cmd);
6973 
6974 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6975 	    spx->txlt_sata_pkt);
6976 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6977 	/*
6978 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6979 	 * with the sata callback, the sata_pkt could be already destroyed
6980 	 * by the time we check ther return status from the hba_start()
6981 	 * function, because sata_scsi_destroy_pkt() could have been already
6982 	 * called (perhaps in the interrupt context). So, in such case, there
6983 	 * should be no references to it. In other cases, sata_pkt still
6984 	 * exists.
6985 	 */
6986 	if (stat == SATA_TRAN_ACCEPTED) {
6987 		/*
6988 		 * pkt accepted for execution.
6989 		 * If it was executed synchronously, it is already completed
6990 		 * and pkt completion_reason indicates completion status.
6991 		 */
6992 		*rval = TRAN_ACCEPT;
6993 		return (0);
6994 	}
6995 
6996 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6997 	switch (stat) {
6998 	case SATA_TRAN_QUEUE_FULL:
6999 		/*
7000 		 * Controller detected queue full condition.
7001 		 */
7002 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7003 		    "sata_hba_start: queue full\n", NULL);
7004 
7005 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7006 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7007 
7008 		*rval = TRAN_BUSY;
7009 		break;
7010 
7011 	case SATA_TRAN_PORT_ERROR:
7012 		/*
7013 		 * Communication/link with device or general port error
7014 		 * detected before pkt execution begun.
7015 		 */
7016 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7017 		    SATA_ADDR_CPORT ||
7018 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7019 		    SATA_ADDR_DCPORT)
7020 			sata_log(sata_hba_inst, CE_CONT,
7021 			    "SATA port %d error",
7022 			    sata_device->satadev_addr.cport);
7023 		else
7024 			sata_log(sata_hba_inst, CE_CONT,
7025 			    "SATA port %d:%d error\n",
7026 			    sata_device->satadev_addr.cport,
7027 			    sata_device->satadev_addr.pmport);
7028 
7029 		/*
7030 		 * Update the port/device structure.
7031 		 * sata_pkt should be still valid. Since port error is
7032 		 * returned, sata_device content should reflect port
7033 		 * state - it means, that sata address have been changed,
7034 		 * because original packet's sata address refered to a device
7035 		 * attached to some port.
7036 		 */
7037 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7038 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7039 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7040 			mutex_enter(&pmportinfo->pmport_mutex);
7041 			sata_update_pmport_info(sata_hba_inst, sata_device);
7042 			mutex_exit(&pmportinfo->pmport_mutex);
7043 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7044 		} else {
7045 			sata_update_port_info(sata_hba_inst, sata_device);
7046 		}
7047 
7048 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7049 		*rval = TRAN_FATAL_ERROR;
7050 		break;
7051 
7052 	case SATA_TRAN_CMD_UNSUPPORTED:
7053 		/*
7054 		 * Command rejected by HBA as unsupported. It was HBA driver
7055 		 * that rejected the command, command was not sent to
7056 		 * an attached device.
7057 		 */
7058 		if ((sdinfo != NULL) &&
7059 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7060 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7061 			    "sat_hba_start: cmd 0x%2x rejected "
7062 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7063 
7064 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7065 		(void) sata_txlt_invalid_command(spx);
7066 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7067 
7068 		*rval = TRAN_ACCEPT;
7069 		break;
7070 
7071 	case SATA_TRAN_BUSY:
7072 		/*
7073 		 * Command rejected by HBA because other operation prevents
7074 		 * accepting the packet, or device is in RESET condition.
7075 		 */
7076 		if (sdinfo != NULL) {
7077 			sdinfo->satadrv_state =
7078 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7079 
7080 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7081 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7082 				    "sata_hba_start: cmd 0x%2x rejected "
7083 				    "because of device reset condition\n",
7084 				    cmd);
7085 			} else {
7086 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7087 				    "sata_hba_start: cmd 0x%2x rejected "
7088 				    "with SATA_TRAN_BUSY status\n",
7089 				    cmd);
7090 			}
7091 		}
7092 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7093 		*rval = TRAN_BUSY;
7094 		break;
7095 
7096 	default:
7097 		/* Unrecognized HBA response */
7098 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7099 		    "sata_hba_start: unrecognized HBA response "
7100 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7101 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7102 		*rval = TRAN_FATAL_ERROR;
7103 		break;
7104 	}
7105 
7106 	/*
7107 	 * If we got here, the packet was rejected.
7108 	 * Check if we need to remember reset state clearing request
7109 	 */
7110 	if (cmd_flags.sata_clear_dev_reset) {
7111 		/*
7112 		 * Check if device is still configured - it may have
7113 		 * disapeared from the configuration
7114 		 */
7115 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7116 		if (sdinfo != NULL) {
7117 			/*
7118 			 * Restore the flag that requests clearing of
7119 			 * the device reset state,
7120 			 * so the next sata packet may carry it to HBA.
7121 			 */
7122 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7123 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7124 				pminfo->pmult_event_flags |=
7125 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7126 			} else {
7127 				sdinfo->satadrv_event_flags |=
7128 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7129 			}
7130 		}
7131 	}
7132 	return (-1);
7133 }
7134 
7135 /*
7136  * Scsi response setup for invalid LBA
7137  *
7138  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7139  */
7140 static int
7141 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7142 {
7143 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7144 	struct scsi_extended_sense *sense;
7145 
7146 	scsipkt->pkt_reason = CMD_CMPLT;
7147 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7148 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7149 	*scsipkt->pkt_scbp = STATUS_CHECK;
7150 
7151 	*scsipkt->pkt_scbp = STATUS_CHECK;
7152 	sense = sata_arq_sense(spx);
7153 	sense->es_key = KEY_ILLEGAL_REQUEST;
7154 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7155 
7156 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7157 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7158 
7159 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7160 	    scsipkt->pkt_comp != NULL) {
7161 		/* scsi callback required */
7162 		if (servicing_interrupt()) {
7163 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7164 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7165 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7166 				return (TRAN_BUSY);
7167 			}
7168 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7169 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7170 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7171 			/* Scheduling the callback failed */
7172 			return (TRAN_BUSY);
7173 		}
7174 	}
7175 	return (TRAN_ACCEPT);
7176 }
7177 
7178 
7179 /*
7180  * Analyze device status and error registers and translate them into
7181  * appropriate scsi sense codes.
7182  * NOTE: non-packet commands only for now
7183  */
7184 static void
7185 sata_decode_device_error(sata_pkt_txlate_t *spx,
7186     struct scsi_extended_sense *sense)
7187 {
7188 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7189 
7190 	ASSERT(sense != NULL);
7191 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7192 	    SATA_STATUS_ERR);
7193 
7194 
7195 	if (err_reg & SATA_ERROR_ICRC) {
7196 		sense->es_key = KEY_ABORTED_COMMAND;
7197 		sense->es_add_code = 0x08; /* Communication failure */
7198 		return;
7199 	}
7200 
7201 	if (err_reg & SATA_ERROR_UNC) {
7202 		sense->es_key = KEY_MEDIUM_ERROR;
7203 		/* Information bytes (LBA) need to be set by a caller */
7204 		return;
7205 	}
7206 
7207 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7208 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7209 		sense->es_key = KEY_UNIT_ATTENTION;
7210 		sense->es_add_code = 0x3a; /* No media present */
7211 		return;
7212 	}
7213 
7214 	if (err_reg & SATA_ERROR_IDNF) {
7215 		if (err_reg & SATA_ERROR_ABORT) {
7216 			sense->es_key = KEY_ABORTED_COMMAND;
7217 		} else {
7218 			sense->es_key = KEY_ILLEGAL_REQUEST;
7219 			sense->es_add_code = 0x21; /* LBA out of range */
7220 		}
7221 		return;
7222 	}
7223 
7224 	if (err_reg & SATA_ERROR_ABORT) {
7225 		ASSERT(spx->txlt_sata_pkt != NULL);
7226 		sense->es_key = KEY_ABORTED_COMMAND;
7227 		return;
7228 	}
7229 }
7230 
7231 /*
7232  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7233  */
7234 static void
7235 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7236 {
7237 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7238 
7239 	*lba = 0;
7240 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7241 		*lba = sata_cmd->satacmd_lba_high_msb;
7242 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7243 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7244 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7245 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7246 	}
7247 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7248 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7249 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7250 }
7251 
7252 /*
7253  * This is fixed sense format - if LBA exceeds the info field size,
7254  * no valid info will be returned (valid bit in extended sense will
7255  * be set to 0).
7256  */
7257 static struct scsi_extended_sense *
7258 sata_arq_sense(sata_pkt_txlate_t *spx)
7259 {
7260 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7261 	struct scsi_arq_status *arqs;
7262 	struct scsi_extended_sense *sense;
7263 
7264 	/* Fill ARQ sense data */
7265 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7266 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7267 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7268 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7269 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7270 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7271 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7272 	arqs->sts_rqpkt_resid = 0;
7273 	sense = &arqs->sts_sensedata;
7274 	bzero(sense, sizeof (struct scsi_extended_sense));
7275 	sata_fixed_sense_data_preset(sense);
7276 	return (sense);
7277 }
7278 
7279 /*
7280  * ATA Pass Through support
7281  * Sets flags indicating that an invalid value was found in some
7282  * field in the command.  It could be something illegal according to
7283  * the SAT-2 spec or it could be a feature that is not (yet?)
7284  * supported.
7285  */
7286 static int
7287 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7288 {
7289 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7290 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7291 
7292 	scsipkt->pkt_reason = CMD_CMPLT;
7293 	*scsipkt->pkt_scbp = STATUS_CHECK;
7294 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7295 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7296 
7297 	sense = sata_arq_sense(spx);
7298 	sense->es_key = KEY_ILLEGAL_REQUEST;
7299 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7300 
7301 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7302 	    scsipkt->pkt_comp != NULL) {
7303 		/* scsi callback required */
7304 		if (servicing_interrupt()) {
7305 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7306 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7307 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7308 				return (TRAN_BUSY);
7309 			}
7310 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7311 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7312 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7313 			/* Scheduling the callback failed */
7314 			return (TRAN_BUSY);
7315 		}
7316 	}
7317 
7318 	return (TRAN_ACCEPT);
7319 }
7320 
7321 /*
7322  * The UNMAP command considers it not to be an error if the parameter length
7323  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7324  * to do so just complete the command.
7325  */
7326 static int
7327 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7328 {
7329 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7330 
7331 	scsipkt->pkt_reason = CMD_CMPLT;
7332 	*scsipkt->pkt_scbp = STATUS_GOOD;
7333 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7334 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7335 
7336 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7337 	    scsipkt->pkt_comp != NULL) {
7338 		/* scsi callback required */
7339 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7340 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7341 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7342 			/* Scheduling the callback failed */
7343 			return (TRAN_BUSY);
7344 		}
7345 	}
7346 
7347 	return (TRAN_ACCEPT);
7348 }
7349 
7350 /*
7351  * Emulated SATA Read/Write command completion for zero-length requests.
7352  * This request always succedes, so in synchronous mode it always returns
7353  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7354  * callback cannot be scheduled.
7355  */
7356 static int
7357 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7358 {
7359 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7360 
7361 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7362 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7363 	scsipkt->pkt_reason = CMD_CMPLT;
7364 	*scsipkt->pkt_scbp = STATUS_GOOD;
7365 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7366 		/* scsi callback required - have to schedule it */
7367 		if (servicing_interrupt()) {
7368 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7369 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7370 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7371 				return (TRAN_BUSY);
7372 			}
7373 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7374 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7375 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7376 			/* Scheduling the callback failed */
7377 			return (TRAN_BUSY);
7378 		}
7379 	}
7380 	return (TRAN_ACCEPT);
7381 }
7382 
7383 
7384 /*
7385  * Translate completion status of SATA read/write commands into scsi response.
7386  * pkt completion_reason is checked to determine the completion status.
7387  * Do scsi callback if necessary.
7388  *
7389  * Note: this function may be called also for synchronously executed
7390  * commands.
7391  * This function may be used only if scsi_pkt is non-NULL.
7392  */
7393 static void
7394 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7395 {
7396 	sata_pkt_txlate_t *spx =
7397 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7398 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7399 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7400 	struct scsi_extended_sense *sense;
7401 	uint64_t lba;
7402 	struct buf *bp;
7403 	int rval;
7404 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7405 		/* Normal completion */
7406 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7407 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7408 		scsipkt->pkt_reason = CMD_CMPLT;
7409 		*scsipkt->pkt_scbp = STATUS_GOOD;
7410 		if (spx->txlt_tmp_buf != NULL) {
7411 			/* Temporary buffer was used */
7412 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7413 			if (bp->b_flags & B_READ) {
7414 				rval = ddi_dma_sync(
7415 				    spx->txlt_buf_dma_handle, 0, 0,
7416 				    DDI_DMA_SYNC_FORCPU);
7417 				ASSERT(rval == DDI_SUCCESS);
7418 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7419 				    bp->b_bcount);
7420 			}
7421 		}
7422 	} else {
7423 		/*
7424 		 * Something went wrong - analyze return
7425 		 */
7426 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7427 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7428 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7429 		*scsipkt->pkt_scbp = STATUS_CHECK;
7430 		sense = sata_arq_sense(spx);
7431 		ASSERT(sense != NULL);
7432 
7433 		/*
7434 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7435 		 * extract from device registers the failing LBA.
7436 		 */
7437 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7438 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7439 			    (scmd->satacmd_lba_mid_msb != 0 ||
7440 			    scmd->satacmd_lba_high_msb != 0)) {
7441 				/*
7442 				 * We have problem reporting this cmd LBA
7443 				 * in fixed sense data format, because of
7444 				 * the size of the scsi LBA fields.
7445 				 */
7446 				sense->es_valid = 0;
7447 			} else {
7448 				sata_extract_error_lba(spx, &lba);
7449 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7450 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7451 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7452 				sense->es_info_4 = lba & 0xFF;
7453 			}
7454 		} else {
7455 			/* Invalid extended sense info */
7456 			sense->es_valid = 0;
7457 		}
7458 
7459 		switch (sata_pkt->satapkt_reason) {
7460 		case SATA_PKT_PORT_ERROR:
7461 			/* We may want to handle DEV GONE state as well */
7462 			/*
7463 			 * We have no device data. Assume no data transfered.
7464 			 */
7465 			sense->es_key = KEY_HARDWARE_ERROR;
7466 			break;
7467 
7468 		case SATA_PKT_DEV_ERROR:
7469 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7470 			    SATA_STATUS_ERR) {
7471 				/*
7472 				 * determine dev error reason from error
7473 				 * reg content
7474 				 */
7475 				sata_decode_device_error(spx, sense);
7476 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7477 					switch (scmd->satacmd_cmd_reg) {
7478 					case SATAC_READ_DMA:
7479 					case SATAC_READ_DMA_EXT:
7480 					case SATAC_READ_DMA_QUEUED:
7481 					case SATAC_READ_DMA_QUEUED_EXT:
7482 					case SATAC_READ_FPDMA_QUEUED:
7483 						/* Unrecovered read error */
7484 						sense->es_add_code =
7485 						    SD_SCSI_ASC_UNREC_READ_ERR;
7486 						break;
7487 					case SATAC_WRITE_DMA:
7488 					case SATAC_WRITE_DMA_EXT:
7489 					case SATAC_WRITE_DMA_QUEUED:
7490 					case SATAC_WRITE_DMA_QUEUED_EXT:
7491 					case SATAC_WRITE_FPDMA_QUEUED:
7492 						/* Write error */
7493 						sense->es_add_code =
7494 						    SD_SCSI_ASC_WRITE_ERR;
7495 						break;
7496 					default:
7497 						/* Internal error */
7498 						SATA_LOG_D((
7499 						    spx->txlt_sata_hba_inst,
7500 						    CE_WARN,
7501 						    "sata_txlt_rw_completion :"
7502 						    "internal error - invalid "
7503 						    "command 0x%2x",
7504 						    scmd->satacmd_cmd_reg));
7505 						break;
7506 					}
7507 				}
7508 				break;
7509 			}
7510 			/* No extended sense key - no info available */
7511 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7512 			break;
7513 
7514 		case SATA_PKT_TIMEOUT:
7515 			scsipkt->pkt_reason = CMD_TIMEOUT;
7516 			scsipkt->pkt_statistics |=
7517 			    STAT_TIMEOUT | STAT_DEV_RESET;
7518 			sense->es_key = KEY_ABORTED_COMMAND;
7519 			break;
7520 
7521 		case SATA_PKT_ABORTED:
7522 			scsipkt->pkt_reason = CMD_ABORTED;
7523 			scsipkt->pkt_statistics |= STAT_ABORTED;
7524 			sense->es_key = KEY_ABORTED_COMMAND;
7525 			break;
7526 
7527 		case SATA_PKT_RESET:
7528 			scsipkt->pkt_reason = CMD_RESET;
7529 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7530 			sense->es_key = KEY_ABORTED_COMMAND;
7531 			break;
7532 
7533 		default:
7534 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7535 			    "sata_txlt_rw_completion: "
7536 			    "invalid packet completion reason"));
7537 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7538 			break;
7539 		}
7540 	}
7541 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7542 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7543 
7544 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7545 		/* scsi callback required */
7546 		scsi_hba_pkt_comp(scsipkt);
7547 }
7548 
7549 
7550 /*
7551  * Translate completion status of non-data commands (i.e. commands returning
7552  * no data).
7553  * pkt completion_reason is checked to determine the completion status.
7554  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7555  *
7556  * Note: this function may be called also for synchronously executed
7557  * commands.
7558  * This function may be used only if scsi_pkt is non-NULL.
7559  */
7560 
7561 static	void
7562 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7563 {
7564 	sata_pkt_txlate_t *spx =
7565 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7566 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7567 
7568 	sata_set_arq_data(sata_pkt);
7569 
7570 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7571 		/* scsi callback required */
7572 		scsi_hba_pkt_comp(scsipkt);
7573 }
7574 
7575 /*
7576  * Completion handler for ATA Pass Through command
7577  */
7578 static void
7579 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7580 {
7581 	sata_pkt_txlate_t *spx =
7582 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7583 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7584 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7585 	struct buf *bp;
7586 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7587 
7588 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7589 		/* Normal completion */
7590 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7591 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7592 		scsipkt->pkt_reason = CMD_CMPLT;
7593 		*scsipkt->pkt_scbp = STATUS_GOOD;
7594 
7595 		/*
7596 		 * If the command has CK_COND set
7597 		 */
7598 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7599 			*scsipkt->pkt_scbp = STATUS_CHECK;
7600 			sata_fill_ata_return_desc(sata_pkt,
7601 			    KEY_RECOVERABLE_ERROR,
7602 			    SD_SCSI_ASC_ATP_INFO_AVAIL, 0);
7603 		}
7604 
7605 		if (spx->txlt_tmp_buf != NULL) {
7606 			/* Temporary buffer was used */
7607 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7608 			if (bp->b_flags & B_READ) {
7609 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7610 				    bp->b_bcount);
7611 			}
7612 		}
7613 	} else {
7614 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7615 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7616 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7617 		*scsipkt->pkt_scbp = STATUS_CHECK;
7618 
7619 		/*
7620 		 * If DF or ERR was set, the HBA should have copied out the
7621 		 * status and error registers to the satacmd structure.
7622 		 */
7623 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7624 			sense_key = KEY_HARDWARE_ERROR;
7625 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7626 			addl_sense_qual = 0;
7627 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7628 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7629 				sense_key = KEY_NOT_READY;
7630 				addl_sense_code =
7631 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7632 				addl_sense_qual = 0;
7633 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7634 				sense_key = KEY_MEDIUM_ERROR;
7635 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7636 				addl_sense_qual = 0;
7637 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7638 				sense_key = KEY_DATA_PROTECT;
7639 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7640 				addl_sense_qual = 0;
7641 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7642 				sense_key = KEY_ILLEGAL_REQUEST;
7643 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7644 				addl_sense_qual = 0;
7645 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7646 				sense_key = KEY_ABORTED_COMMAND;
7647 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7648 				addl_sense_qual = 0;
7649 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7650 				sense_key = KEY_UNIT_ATTENTION;
7651 				addl_sense_code =
7652 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7653 				addl_sense_qual = 0;
7654 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7655 				sense_key = KEY_UNIT_ATTENTION;
7656 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7657 				addl_sense_qual = 0;
7658 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7659 				sense_key = KEY_ABORTED_COMMAND;
7660 				addl_sense_code =
7661 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7662 				addl_sense_qual = 0;
7663 			}
7664 		}
7665 
7666 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7667 		    addl_sense_qual);
7668 	}
7669 
7670 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7671 		/* scsi callback required */
7672 		scsi_hba_pkt_comp(scsipkt);
7673 }
7674 
7675 /*
7676  * Completion handler for unmap translation command
7677  */
7678 static void
7679 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7680 {
7681 	sata_pkt_txlate_t *spx =
7682 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7683 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7684 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7685 	struct buf *bp;
7686 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7687 
7688 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7689 		/* Normal completion */
7690 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7691 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7692 		scsipkt->pkt_reason = CMD_CMPLT;
7693 		*scsipkt->pkt_scbp = STATUS_GOOD;
7694 
7695 		if (spx->txlt_tmp_buf != NULL) {
7696 			/* Temporary buffer was used */
7697 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7698 			if (bp->b_flags & B_READ) {
7699 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7700 				    bp->b_bcount);
7701 			}
7702 		}
7703 	} else {
7704 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7705 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7706 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7707 		*scsipkt->pkt_scbp = STATUS_CHECK;
7708 
7709 		/*
7710 		 * If DF or ERR was set, the HBA should have copied out the
7711 		 * status and error registers to the satacmd structure.
7712 		 */
7713 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7714 			sense_key = KEY_HARDWARE_ERROR;
7715 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7716 			addl_sense_qual = 0;
7717 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7718 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7719 				sense_key = KEY_NOT_READY;
7720 				addl_sense_code =
7721 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7722 				addl_sense_qual = 0;
7723 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7724 				sense_key = KEY_MEDIUM_ERROR;
7725 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7726 				addl_sense_qual = 0;
7727 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7728 				sense_key = KEY_DATA_PROTECT;
7729 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7730 				addl_sense_qual = 0;
7731 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7732 				sense_key = KEY_ILLEGAL_REQUEST;
7733 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7734 				addl_sense_qual = 0;
7735 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7736 				sense_key = KEY_ABORTED_COMMAND;
7737 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7738 				addl_sense_qual = 0;
7739 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7740 				sense_key = KEY_UNIT_ATTENTION;
7741 				addl_sense_code =
7742 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7743 				addl_sense_qual = 0;
7744 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7745 				sense_key = KEY_UNIT_ATTENTION;
7746 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7747 				addl_sense_qual = 0;
7748 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7749 				sense_key = KEY_ABORTED_COMMAND;
7750 				addl_sense_code =
7751 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7752 				addl_sense_qual = 0;
7753 			}
7754 		}
7755 
7756 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7757 		    addl_sense_qual);
7758 	}
7759 
7760 	sata_free_local_buffer(spx);
7761 
7762 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7763 		/* scsi callback required */
7764 		scsi_hba_pkt_comp(scsipkt);
7765 }
7766 
7767 /*
7768  *
7769  */
7770 static void
7771 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7772     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7773 {
7774 	sata_pkt_txlate_t *spx =
7775 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7776 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7777 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7778 	struct sata_apt_sense_data *apt_sd =
7779 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7780 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7781 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7782 	    &(apt_sd->apt_sd_sense);
7783 	int extend = 0;
7784 
7785 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7786 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7787 		extend = 1;
7788 
7789 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7790 
7791 	/* update the residual count */
7792 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7793 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7794 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7795 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7796 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7797 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7798 	    sizeof (struct sata_apt_sense_data);
7799 
7800 	/*
7801 	 * Fill in the Descriptor sense header
7802 	 */
7803 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7804 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7805 	sds->ds_class = CLASS_EXTENDED_SENSE;
7806 	sds->ds_key = sense_key & 0xf;
7807 	sds->ds_add_code = addl_sense_code;
7808 	sds->ds_qual_code = addl_sense_qual;
7809 	sds->ds_addl_sense_length =
7810 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7811 
7812 	/*
7813 	 * Fill in the ATA Return descriptor sense data
7814 	 */
7815 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7816 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7817 	ata_ret_desc->ars_addl_length = 0xc;
7818 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7819 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7820 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7821 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7822 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7823 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7824 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7825 
7826 	if (extend == 1) {
7827 		ata_ret_desc->ars_extend = 1;
7828 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7829 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7830 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7831 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7832 	} else {
7833 		ata_ret_desc->ars_extend = 0;
7834 		ata_ret_desc->ars_sec_count_msb = 0;
7835 		ata_ret_desc->ars_lba_low_msb = 0;
7836 		ata_ret_desc->ars_lba_mid_msb = 0;
7837 		ata_ret_desc->ars_lba_high_msb = 0;
7838 	}
7839 }
7840 
7841 static	void
7842 sata_set_arq_data(sata_pkt_t *sata_pkt)
7843 {
7844 	sata_pkt_txlate_t *spx =
7845 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7846 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7847 	struct scsi_extended_sense *sense;
7848 
7849 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7850 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7851 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7852 		/* Normal completion */
7853 		scsipkt->pkt_reason = CMD_CMPLT;
7854 		*scsipkt->pkt_scbp = STATUS_GOOD;
7855 	} else {
7856 		/* Something went wrong */
7857 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7858 		*scsipkt->pkt_scbp = STATUS_CHECK;
7859 		sense = sata_arq_sense(spx);
7860 		switch (sata_pkt->satapkt_reason) {
7861 		case SATA_PKT_PORT_ERROR:
7862 			/*
7863 			 * We have no device data. Assume no data transfered.
7864 			 */
7865 			sense->es_key = KEY_HARDWARE_ERROR;
7866 			break;
7867 
7868 		case SATA_PKT_DEV_ERROR:
7869 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7870 			    SATA_STATUS_ERR) {
7871 				/*
7872 				 * determine dev error reason from error
7873 				 * reg content
7874 				 */
7875 				sata_decode_device_error(spx, sense);
7876 				break;
7877 			}
7878 			/* No extended sense key - no info available */
7879 			break;
7880 
7881 		case SATA_PKT_TIMEOUT:
7882 			scsipkt->pkt_reason = CMD_TIMEOUT;
7883 			scsipkt->pkt_statistics |=
7884 			    STAT_TIMEOUT | STAT_DEV_RESET;
7885 			/* No extended sense key ? */
7886 			break;
7887 
7888 		case SATA_PKT_ABORTED:
7889 			scsipkt->pkt_reason = CMD_ABORTED;
7890 			scsipkt->pkt_statistics |= STAT_ABORTED;
7891 			/* No extended sense key ? */
7892 			break;
7893 
7894 		case SATA_PKT_RESET:
7895 			/* pkt aborted by an explicit reset from a host */
7896 			scsipkt->pkt_reason = CMD_RESET;
7897 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7898 			break;
7899 
7900 		default:
7901 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7902 			    "sata_txlt_nodata_cmd_completion: "
7903 			    "invalid packet completion reason %d",
7904 			    sata_pkt->satapkt_reason));
7905 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7906 			break;
7907 		}
7908 
7909 	}
7910 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7911 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7912 }
7913 
7914 
7915 /*
7916  * Build Mode sense R/W recovery page
7917  * NOT IMPLEMENTED
7918  */
7919 
7920 static int
7921 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7922 {
7923 #ifndef __lock_lint
7924 	_NOTE(ARGUNUSED(sdinfo))
7925 	_NOTE(ARGUNUSED(pcntrl))
7926 	_NOTE(ARGUNUSED(buf))
7927 #endif
7928 	return (0);
7929 }
7930 
7931 /*
7932  * Build Mode sense caching page  -  scsi-3 implementation.
7933  * Page length distinguishes previous format from scsi-3 format.
7934  * buf must have space for 0x12 bytes.
7935  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7936  *
7937  */
7938 static int
7939 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7940 {
7941 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7942 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7943 
7944 	/*
7945 	 * Most of the fields are set to 0, being not supported and/or disabled
7946 	 */
7947 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7948 
7949 	/* Saved paramters not supported */
7950 	if (pcntrl == 3)
7951 		return (0);
7952 	if (pcntrl == 0 || pcntrl == 2) {
7953 		/*
7954 		 * For now treat current and default parameters as same
7955 		 * That may have to change, if target driver will complain
7956 		 */
7957 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
7958 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7959 
7960 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7961 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
7962 			page->dra = 1;		/* Read Ahead disabled */
7963 			page->rcd = 1;		/* Read Cache disabled */
7964 		}
7965 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
7966 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
7967 			page->wce = 1;		/* Write Cache enabled */
7968 	} else {
7969 		/* Changeable parameters */
7970 		page->mode_page.code = MODEPAGE_CACHING;
7971 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7972 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
7973 			page->dra = 1;
7974 			page->rcd = 1;
7975 		}
7976 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
7977 			page->wce = 1;
7978 	}
7979 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7980 	    sizeof (struct mode_page));
7981 }
7982 
7983 /*
7984  * Build Mode sense exception cntrl page
7985  */
7986 static int
7987 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7988 {
7989 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
7990 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7991 
7992 	/*
7993 	 * Most of the fields are set to 0, being not supported and/or disabled
7994 	 */
7995 	bzero(buf, PAGELENGTH_INFO_EXCPT);
7996 
7997 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
7998 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
7999 
8000 	/* Indicate that this is page is saveable */
8001 	page->mode_page.ps = 1;
8002 
8003 	/*
8004 	 * We will return the same data for default, current and saved page.
8005 	 * The only changeable bit is dexcpt and that bit is required
8006 	 * by the ATA specification to be preserved across power cycles.
8007 	 */
8008 	if (pcntrl != 1) {
8009 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8010 		page->mrie = MRIE_ONLY_ON_REQUEST;
8011 	}
8012 	else
8013 		page->dexcpt = 1;	/* Only changeable parameter */
8014 
8015 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8016 }
8017 
8018 
8019 static int
8020 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8021 {
8022 	struct mode_acoustic_management *page =
8023 	    (struct mode_acoustic_management *)buf;
8024 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8025 
8026 	/*
8027 	 * Most of the fields are set to 0, being not supported and/or disabled
8028 	 */
8029 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8030 
8031 	switch (pcntrl) {
8032 	case P_CNTRL_DEFAULT:
8033 		/*  default paramters not supported */
8034 		return (0);
8035 
8036 	case P_CNTRL_CURRENT:
8037 	case P_CNTRL_SAVED:
8038 		/* Saved and current are supported and are identical */
8039 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8040 		page->mode_page.length =
8041 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8042 		page->mode_page.ps = 1;
8043 
8044 		/* Word 83 indicates if feature is supported */
8045 		/* If feature is not supported */
8046 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8047 			page->acoustic_manag_enable =
8048 			    ACOUSTIC_DISABLED;
8049 		} else {
8050 			page->acoustic_manag_enable =
8051 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8052 			    != 0);
8053 			/* Word 94 inidicates the value */
8054 #ifdef	_LITTLE_ENDIAN
8055 			page->acoustic_manag_level =
8056 			    (uchar_t)sata_id->ai_acoustic;
8057 			page->vendor_recommended_value =
8058 			    sata_id->ai_acoustic >> 8;
8059 #else
8060 			page->acoustic_manag_level =
8061 			    sata_id->ai_acoustic >> 8;
8062 			page->vendor_recommended_value =
8063 			    (uchar_t)sata_id->ai_acoustic;
8064 #endif
8065 		}
8066 		break;
8067 
8068 	case P_CNTRL_CHANGEABLE:
8069 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8070 		page->mode_page.length =
8071 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8072 		page->mode_page.ps = 1;
8073 
8074 		/* Word 83 indicates if the feature is supported */
8075 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8076 			page->acoustic_manag_enable =
8077 			    ACOUSTIC_ENABLED;
8078 			page->acoustic_manag_level = 0xff;
8079 		}
8080 		break;
8081 	}
8082 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8083 	    sizeof (struct mode_page));
8084 }
8085 
8086 
8087 /*
8088  * Build Mode sense power condition page.
8089  */
8090 static int
8091 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8092 {
8093 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8094 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8095 
8096 	/*
8097 	 * Most of the fields are set to 0, being not supported and/or disabled
8098 	 * power condition page length was 0x0a
8099 	 */
8100 	bzero(buf, sizeof (struct mode_info_power_cond));
8101 
8102 	if (pcntrl == P_CNTRL_DEFAULT) {
8103 		/*  default paramters not supported */
8104 		return (0);
8105 	}
8106 
8107 	page->mode_page.code = MODEPAGE_POWER_COND;
8108 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8109 
8110 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
8111 		page->standby = 1;
8112 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8113 		    sizeof (uchar_t) * 4);
8114 	}
8115 
8116 	return (sizeof (struct mode_info_power_cond));
8117 }
8118 
8119 /*
8120  * Process mode select caching page 8 (scsi3 format only).
8121  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8122  * if these features are supported by the device. If these features are not
8123  * supported, the command will be terminated with STATUS_CHECK.
8124  * This function fails only if the SET FEATURE command sent to
8125  * the device fails. The page format is not verified, assuming that the
8126  * target driver operates correctly - if parameters length is too short,
8127  * we just drop the page.
8128  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8129  * setting have to be changed.
8130  * SET FEATURE command is executed synchronously, i.e. we wait here until
8131  * it is completed, regardless of the scsi pkt directives.
8132  *
8133  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8134  * changing DRA will change RCD.
8135  *
8136  * More than one SATA command may be executed to perform operations specified
8137  * by mode select pages. The first error terminates further execution.
8138  * Operations performed successully are not backed-up in such case.
8139  *
8140  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8141  * If operation resulted in changing device setup, dmod flag should be set to
8142  * one (1). If parameters were not changed, dmod flag should be set to 0.
8143  * Upon return, if operation required sending command to the device, the rval
8144  * should be set to the value returned by sata_hba_start. If operation
8145  * did not require device access, rval should be set to TRAN_ACCEPT.
8146  * The pagelen should be set to the length of the page.
8147  *
8148  * This function has to be called with a port mutex held.
8149  *
8150  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8151  */
8152 int
8153 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8154     int parmlen, int *pagelen, int *rval, int *dmod)
8155 {
8156 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8157 	sata_drive_info_t *sdinfo;
8158 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8159 	sata_id_t *sata_id;
8160 	struct scsi_extended_sense *sense;
8161 	int wce, dra;	/* Current settings */
8162 
8163 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8164 	    &spx->txlt_sata_pkt->satapkt_device);
8165 	sata_id = &sdinfo->satadrv_id;
8166 	*dmod = 0;
8167 
8168 	/* Verify parameters length. If too short, drop it */
8169 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8170 	    sizeof (struct mode_page)) > parmlen) {
8171 		*scsipkt->pkt_scbp = STATUS_CHECK;
8172 		sense = sata_arq_sense(spx);
8173 		sense->es_key = KEY_ILLEGAL_REQUEST;
8174 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8175 		*pagelen = parmlen;
8176 		*rval = TRAN_ACCEPT;
8177 		return (SATA_FAILURE);
8178 	}
8179 
8180 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8181 
8182 	/* Current setting of Read Ahead (and Read Cache) */
8183 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8184 		dra = 0;	/* 0 == not disabled */
8185 	else
8186 		dra = 1;
8187 	/* Current setting of Write Cache */
8188 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8189 		wce = 1;
8190 	else
8191 		wce = 0;
8192 
8193 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8194 		/* nothing to do */
8195 		*rval = TRAN_ACCEPT;
8196 		return (SATA_SUCCESS);
8197 	}
8198 
8199 	/*
8200 	 * Need to flip some setting
8201 	 * Set-up Internal SET FEATURES command(s)
8202 	 */
8203 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8204 	scmd->satacmd_addr_type = 0;
8205 	scmd->satacmd_device_reg = 0;
8206 	scmd->satacmd_status_reg = 0;
8207 	scmd->satacmd_error_reg = 0;
8208 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8209 	if (page->dra != dra || page->rcd != dra) {
8210 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8211 			/* Need to flip read ahead setting */
8212 			if (dra == 0)
8213 				/* Disable read ahead / read cache */
8214 				scmd->satacmd_features_reg =
8215 				    SATAC_SF_DISABLE_READ_AHEAD;
8216 			else
8217 				/* Enable read ahead  / read cache */
8218 				scmd->satacmd_features_reg =
8219 				    SATAC_SF_ENABLE_READ_AHEAD;
8220 
8221 			/* Transfer command to HBA */
8222 			if (sata_hba_start(spx, rval) != 0)
8223 				/*
8224 				 * Pkt not accepted for execution.
8225 				 */
8226 				return (SATA_FAILURE);
8227 
8228 			*dmod = 1;
8229 
8230 			/* Now process return */
8231 			if (spx->txlt_sata_pkt->satapkt_reason !=
8232 			    SATA_PKT_COMPLETED) {
8233 				goto failure;	/* Terminate */
8234 			}
8235 		} else {
8236 			*scsipkt->pkt_scbp = STATUS_CHECK;
8237 			sense = sata_arq_sense(spx);
8238 			sense->es_key = KEY_ILLEGAL_REQUEST;
8239 			sense->es_add_code =
8240 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8241 			*pagelen = parmlen;
8242 			*rval = TRAN_ACCEPT;
8243 			return (SATA_FAILURE);
8244 		}
8245 	}
8246 
8247 	/* Note that the packet is not removed, so it could be re-used */
8248 	if (page->wce != wce) {
8249 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8250 			/* Need to flip Write Cache setting */
8251 			if (page->wce == 1)
8252 				/* Enable write cache */
8253 				scmd->satacmd_features_reg =
8254 				    SATAC_SF_ENABLE_WRITE_CACHE;
8255 			else
8256 				/* Disable write cache */
8257 				scmd->satacmd_features_reg =
8258 				    SATAC_SF_DISABLE_WRITE_CACHE;
8259 
8260 			/* Transfer command to HBA */
8261 			if (sata_hba_start(spx, rval) != 0)
8262 				/*
8263 				 * Pkt not accepted for execution.
8264 				 */
8265 				return (SATA_FAILURE);
8266 
8267 			*dmod = 1;
8268 
8269 			/* Now process return */
8270 			if (spx->txlt_sata_pkt->satapkt_reason !=
8271 			    SATA_PKT_COMPLETED) {
8272 				goto failure;
8273 			}
8274 		} else {
8275 			*scsipkt->pkt_scbp = STATUS_CHECK;
8276 			sense = sata_arq_sense(spx);
8277 			sense->es_key = KEY_ILLEGAL_REQUEST;
8278 			sense->es_add_code =
8279 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8280 			*pagelen = parmlen;
8281 			*rval = TRAN_ACCEPT;
8282 			return (SATA_FAILURE);
8283 		}
8284 	}
8285 	return (SATA_SUCCESS);
8286 
8287 failure:
8288 	sata_xlate_errors(spx);
8289 
8290 	return (SATA_FAILURE);
8291 }
8292 
8293 /*
8294  * Process mode select informational exceptions control page 0x1c
8295  *
8296  * The only changeable bit is dexcpt (disable exceptions).
8297  * MRIE (method of reporting informational exceptions) must be
8298  * "only on request".
8299  * This page applies to informational exceptions that report
8300  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8301  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8302  * Informational exception conditions occur as the result of background scan
8303  * errors, background self-test errors, or vendor specific events within a
8304  * logical unit. An informational exception condition may occur asynchronous
8305  * to any commands.
8306  *
8307  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8308  * If operation resulted in changing device setup, dmod flag should be set to
8309  * one (1). If parameters were not changed, dmod flag should be set to 0.
8310  * Upon return, if operation required sending command to the device, the rval
8311  * should be set to the value returned by sata_hba_start. If operation
8312  * did not require device access, rval should be set to TRAN_ACCEPT.
8313  * The pagelen should be set to the length of the page.
8314  *
8315  * This function has to be called with a port mutex held.
8316  *
8317  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8318  *
8319  * Cannot be called in the interrupt context.
8320  */
8321 static	int
8322 sata_mode_select_page_1c(
8323 	sata_pkt_txlate_t *spx,
8324 	struct mode_info_excpt_page *page,
8325 	int parmlen,
8326 	int *pagelen,
8327 	int *rval,
8328 	int *dmod)
8329 {
8330 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8331 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8332 	sata_drive_info_t *sdinfo;
8333 	sata_id_t *sata_id;
8334 	struct scsi_extended_sense *sense;
8335 
8336 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8337 	    &spx->txlt_sata_pkt->satapkt_device);
8338 	sata_id = &sdinfo->satadrv_id;
8339 
8340 	*dmod = 0;
8341 
8342 	/* Verify parameters length. If too short, drop it */
8343 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8344 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8345 		*scsipkt->pkt_scbp = STATUS_CHECK;
8346 		sense = sata_arq_sense(spx);
8347 		sense->es_key = KEY_ILLEGAL_REQUEST;
8348 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8349 		*pagelen = parmlen;
8350 		*rval = TRAN_ACCEPT;
8351 		return (SATA_FAILURE);
8352 	}
8353 
8354 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8355 
8356 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8357 		*scsipkt->pkt_scbp = STATUS_CHECK;
8358 		sense = sata_arq_sense(spx);
8359 		sense->es_key = KEY_ILLEGAL_REQUEST;
8360 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8361 		*pagelen = parmlen;
8362 		*rval = TRAN_ACCEPT;
8363 		return (SATA_FAILURE);
8364 	}
8365 
8366 	/* If already in the state requested, we are done */
8367 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8368 		/* nothing to do */
8369 		*rval = TRAN_ACCEPT;
8370 		return (SATA_SUCCESS);
8371 	}
8372 
8373 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8374 
8375 	/* Build SMART_ENABLE or SMART_DISABLE command */
8376 	scmd->satacmd_addr_type = 0;		/* N/A */
8377 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8378 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8379 	scmd->satacmd_features_reg = page->dexcpt ?
8380 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8381 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8382 	scmd->satacmd_cmd_reg = SATAC_SMART;
8383 
8384 	/* Transfer command to HBA */
8385 	if (sata_hba_start(spx, rval) != 0)
8386 		/*
8387 		 * Pkt not accepted for execution.
8388 		 */
8389 		return (SATA_FAILURE);
8390 
8391 	*dmod = 1;	/* At least may have been modified */
8392 
8393 	/* Now process return */
8394 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8395 		return (SATA_SUCCESS);
8396 
8397 	/* Packet did not complete successfully */
8398 	sata_xlate_errors(spx);
8399 
8400 	return (SATA_FAILURE);
8401 }
8402 
8403 /*
8404  * Process mode select acoustic management control page 0x30
8405  *
8406  *
8407  * This function has to be called with a port mutex held.
8408  *
8409  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8410  *
8411  * Cannot be called in the interrupt context.
8412  */
8413 int
8414 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8415     mode_acoustic_management *page, int parmlen, int *pagelen,
8416     int *rval, int *dmod)
8417 {
8418 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8419 	sata_drive_info_t *sdinfo;
8420 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8421 	sata_id_t *sata_id;
8422 	struct scsi_extended_sense *sense;
8423 
8424 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8425 	    &spx->txlt_sata_pkt->satapkt_device);
8426 	sata_id = &sdinfo->satadrv_id;
8427 	*dmod = 0;
8428 
8429 	/* If parmlen is too short or the feature is not supported, drop it */
8430 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8431 	    sizeof (struct mode_page)) > parmlen) ||
8432 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8433 		*scsipkt->pkt_scbp = STATUS_CHECK;
8434 		sense = sata_arq_sense(spx);
8435 		sense->es_key = KEY_ILLEGAL_REQUEST;
8436 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8437 		*pagelen = parmlen;
8438 		*rval = TRAN_ACCEPT;
8439 		return (SATA_FAILURE);
8440 	}
8441 
8442 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8443 	    sizeof (struct mode_page);
8444 
8445 	/*
8446 	 * We can enable and disable acoustice management and
8447 	 * set the acoustic management level.
8448 	 */
8449 
8450 	/*
8451 	 * Set-up Internal SET FEATURES command(s)
8452 	 */
8453 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8454 	scmd->satacmd_addr_type = 0;
8455 	scmd->satacmd_device_reg = 0;
8456 	scmd->satacmd_status_reg = 0;
8457 	scmd->satacmd_error_reg = 0;
8458 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8459 	if (page->acoustic_manag_enable) {
8460 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8461 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8462 	} else {	/* disabling acoustic management */
8463 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8464 	}
8465 
8466 	/* Transfer command to HBA */
8467 	if (sata_hba_start(spx, rval) != 0)
8468 		/*
8469 		 * Pkt not accepted for execution.
8470 		 */
8471 		return (SATA_FAILURE);
8472 
8473 	/* Now process return */
8474 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8475 		sata_xlate_errors(spx);
8476 		return (SATA_FAILURE);
8477 	}
8478 
8479 	*dmod = 1;
8480 
8481 	return (SATA_SUCCESS);
8482 }
8483 
8484 /*
8485  * Process mode select power condition page 0x1a
8486  *
8487  * This function has to be called with a port mutex held.
8488  *
8489  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8490  *
8491  * Cannot be called in the interrupt context.
8492  */
8493 int
8494 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8495     mode_info_power_cond *page, int parmlen, int *pagelen,
8496     int *rval, int *dmod)
8497 {
8498 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8499 	sata_drive_info_t *sdinfo;
8500 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8501 	sata_id_t *sata_id;
8502 	struct scsi_extended_sense *sense;
8503 	uint8_t ata_count;
8504 	int i, len;
8505 
8506 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8507 	    &spx->txlt_sata_pkt->satapkt_device);
8508 	sata_id = &sdinfo->satadrv_id;
8509 	*dmod = 0;
8510 
8511 	len = sizeof (struct mode_info_power_cond);
8512 	len += sizeof (struct mode_page);
8513 
8514 	/* If parmlen is too short or the feature is not supported, drop it */
8515 	if ((len < parmlen) || (page->idle == 1) ||
8516 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
8517 		*scsipkt->pkt_scbp = STATUS_CHECK;
8518 		sense = sata_arq_sense(spx);
8519 		sense->es_key = KEY_ILLEGAL_REQUEST;
8520 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8521 		*pagelen = parmlen;
8522 		*rval = TRAN_ACCEPT;
8523 		return (SATA_FAILURE);
8524 	}
8525 
8526 	*pagelen = len;
8527 
8528 	/*
8529 	 * Set-up Internal STANDBY command(s)
8530 	 */
8531 	if (page->standby == 0)
8532 		goto out;
8533 
8534 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8535 
8536 	scmd->satacmd_addr_type = 0;
8537 	scmd->satacmd_sec_count_lsb = ata_count;
8538 	scmd->satacmd_lba_low_lsb = 0;
8539 	scmd->satacmd_lba_mid_lsb = 0;
8540 	scmd->satacmd_lba_high_lsb = 0;
8541 	scmd->satacmd_features_reg = 0;
8542 	scmd->satacmd_device_reg = 0;
8543 	scmd->satacmd_status_reg = 0;
8544 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8545 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8546 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8547 
8548 	/* Transfer command to HBA */
8549 	if (sata_hba_start(spx, rval) != 0) {
8550 		return (SATA_FAILURE);
8551 	} else {
8552 		if ((scmd->satacmd_error_reg != 0) ||
8553 		    (spx->txlt_sata_pkt->satapkt_reason !=
8554 		    SATA_PKT_COMPLETED)) {
8555 			sata_xlate_errors(spx);
8556 			return (SATA_FAILURE);
8557 		}
8558 	}
8559 
8560 	for (i = 0; i < 4; i++) {
8561 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8562 	}
8563 out:
8564 	*dmod = 1;
8565 	return (SATA_SUCCESS);
8566 }
8567 
8568 /*
8569  * sata_build_lsense_page0() is used to create the
8570  * SCSI LOG SENSE page 0 (supported log pages)
8571  *
8572  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8573  * (supported log pages, self-test results, informational exceptions
8574  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8575  *
8576  * Takes a sata_drive_info t * and the address of a buffer
8577  * in which to create the page information.
8578  *
8579  * Returns the number of bytes valid in the buffer.
8580  */
8581 static	int
8582 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8583 {
8584 	struct log_parameter *lpp = (struct log_parameter *)buf;
8585 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8586 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8587 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8588 
8589 	lpp->param_code[0] = 0;
8590 	lpp->param_code[1] = 0;
8591 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8592 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8593 
8594 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8595 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8596 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8597 			++num_pages_supported;
8598 		}
8599 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8600 		++num_pages_supported;
8601 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8602 		++num_pages_supported;
8603 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8604 		++num_pages_supported;
8605 	}
8606 
8607 	lpp->param_len = num_pages_supported;
8608 
8609 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8610 	    num_pages_supported);
8611 }
8612 
8613 /*
8614  * sata_build_lsense_page_10() is used to create the
8615  * SCSI LOG SENSE page 0x10 (self-test results)
8616  *
8617  * Takes a sata_drive_info t * and the address of a buffer
8618  * in which to create the page information as well as a sata_hba_inst_t *.
8619  *
8620  * Returns the number of bytes valid in the buffer.
8621  *
8622  * Note: Self test and SMART data is accessible in device log pages.
8623  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8624  * of data can be transferred by a single command), or by the General Purpose
8625  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8626  * - approximately 33MB - can be transferred by a single command.
8627  * The SCT Command response (either error or command) is the same for both
8628  * the SMART and GPL methods of issuing commands.
8629  * This function uses READ LOG EXT command when drive supports LBA48, and
8630  * SMART READ command otherwise.
8631  *
8632  * Since above commands are executed in a synchronous mode, this function
8633  * should not be called in an interrupt context.
8634  */
8635 static	int
8636 sata_build_lsense_page_10(
8637 	sata_drive_info_t *sdinfo,
8638 	uint8_t *buf,
8639 	sata_hba_inst_t *sata_hba_inst)
8640 {
8641 	struct log_parameter *lpp = (struct log_parameter *)buf;
8642 	int rval;
8643 
8644 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8645 		struct smart_ext_selftest_log *ext_selftest_log;
8646 
8647 		ext_selftest_log = kmem_zalloc(
8648 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8649 
8650 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8651 		    ext_selftest_log, 0);
8652 		if (rval == 0) {
8653 			int index, start_index;
8654 			struct smart_ext_selftest_log_entry *entry;
8655 			static const struct smart_ext_selftest_log_entry empty =
8656 			    {0};
8657 			uint16_t block_num;
8658 			int count;
8659 			boolean_t only_one_block = B_FALSE;
8660 
8661 			index = ext_selftest_log->
8662 			    smart_ext_selftest_log_index[0];
8663 			index |= ext_selftest_log->
8664 			    smart_ext_selftest_log_index[1] << 8;
8665 			if (index == 0)
8666 				goto out;
8667 
8668 			--index;	/* Correct for 0 origin */
8669 			start_index = index;	/* remember where we started */
8670 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8671 			if (block_num != 0) {
8672 				rval = sata_ext_smart_selftest_read_log(
8673 				    sata_hba_inst, sdinfo, ext_selftest_log,
8674 				    block_num);
8675 				if (rval != 0)
8676 					goto out;
8677 			}
8678 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8679 			entry =
8680 			    &ext_selftest_log->
8681 			    smart_ext_selftest_log_entries[index];
8682 
8683 			for (count = 1;
8684 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8685 			    ++count) {
8686 				uint8_t status;
8687 				uint8_t code;
8688 				uint8_t sense_key;
8689 				uint8_t add_sense_code;
8690 				uint8_t add_sense_code_qual;
8691 
8692 				/* If this is an unused entry, we are done */
8693 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8694 					/* Broken firmware on some disks */
8695 					if (index + 1 ==
8696 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8697 						--entry;
8698 						--index;
8699 						if (bcmp(entry, &empty,
8700 						    sizeof (empty)) == 0)
8701 							goto out;
8702 					} else
8703 						goto out;
8704 				}
8705 
8706 				if (only_one_block &&
8707 				    start_index == index)
8708 					goto out;
8709 
8710 				lpp->param_code[0] = 0;
8711 				lpp->param_code[1] = count;
8712 				lpp->param_ctrl_flags =
8713 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8714 				lpp->param_len =
8715 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8716 
8717 				status = entry->smart_ext_selftest_log_status;
8718 				status >>= 4;
8719 				switch (status) {
8720 				case 0:
8721 				default:
8722 					sense_key = KEY_NO_SENSE;
8723 					add_sense_code =
8724 					    SD_SCSI_ASC_NO_ADD_SENSE;
8725 					add_sense_code_qual = 0;
8726 					break;
8727 				case 1:
8728 					sense_key = KEY_ABORTED_COMMAND;
8729 					add_sense_code =
8730 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8731 					add_sense_code_qual = SCSI_COMPONENT_81;
8732 					break;
8733 				case 2:
8734 					sense_key = KEY_ABORTED_COMMAND;
8735 					add_sense_code =
8736 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8737 					add_sense_code_qual = SCSI_COMPONENT_82;
8738 					break;
8739 				case 3:
8740 					sense_key = KEY_ABORTED_COMMAND;
8741 					add_sense_code =
8742 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8743 					add_sense_code_qual = SCSI_COMPONENT_83;
8744 					break;
8745 				case 4:
8746 					sense_key = KEY_HARDWARE_ERROR;
8747 					add_sense_code =
8748 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8749 					add_sense_code_qual = SCSI_COMPONENT_84;
8750 					break;
8751 				case 5:
8752 					sense_key = KEY_HARDWARE_ERROR;
8753 					add_sense_code =
8754 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8755 					add_sense_code_qual = SCSI_COMPONENT_85;
8756 					break;
8757 				case 6:
8758 					sense_key = KEY_HARDWARE_ERROR;
8759 					add_sense_code =
8760 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8761 					add_sense_code_qual = SCSI_COMPONENT_86;
8762 					break;
8763 				case 7:
8764 					sense_key = KEY_MEDIUM_ERROR;
8765 					add_sense_code =
8766 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8767 					add_sense_code_qual = SCSI_COMPONENT_87;
8768 					break;
8769 				case 8:
8770 					sense_key = KEY_HARDWARE_ERROR;
8771 					add_sense_code =
8772 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8773 					add_sense_code_qual = SCSI_COMPONENT_88;
8774 					break;
8775 				}
8776 				code = 0;	/* unspecified */
8777 				status |= (code << 4);
8778 				lpp->param_values[0] = status;
8779 				lpp->param_values[1] = 0; /* unspecified */
8780 				lpp->param_values[2] = entry->
8781 				    smart_ext_selftest_log_timestamp[1];
8782 				lpp->param_values[3] = entry->
8783 				    smart_ext_selftest_log_timestamp[0];
8784 				if (status != 0) {
8785 					lpp->param_values[4] = 0;
8786 					lpp->param_values[5] = 0;
8787 					lpp->param_values[6] = entry->
8788 					    smart_ext_selftest_log_failing_lba
8789 					    [5];
8790 					lpp->param_values[7] = entry->
8791 					    smart_ext_selftest_log_failing_lba
8792 					    [4];
8793 					lpp->param_values[8] = entry->
8794 					    smart_ext_selftest_log_failing_lba
8795 					    [3];
8796 					lpp->param_values[9] = entry->
8797 					    smart_ext_selftest_log_failing_lba
8798 					    [2];
8799 					lpp->param_values[10] = entry->
8800 					    smart_ext_selftest_log_failing_lba
8801 					    [1];
8802 					lpp->param_values[11] = entry->
8803 					    smart_ext_selftest_log_failing_lba
8804 					    [0];
8805 				} else {	/* No bad block address */
8806 					lpp->param_values[4] = 0xff;
8807 					lpp->param_values[5] = 0xff;
8808 					lpp->param_values[6] = 0xff;
8809 					lpp->param_values[7] = 0xff;
8810 					lpp->param_values[8] = 0xff;
8811 					lpp->param_values[9] = 0xff;
8812 					lpp->param_values[10] = 0xff;
8813 					lpp->param_values[11] = 0xff;
8814 				}
8815 
8816 				lpp->param_values[12] = sense_key;
8817 				lpp->param_values[13] = add_sense_code;
8818 				lpp->param_values[14] = add_sense_code_qual;
8819 				lpp->param_values[15] = 0; /* undefined */
8820 
8821 				lpp = (struct log_parameter *)
8822 				    (((uint8_t *)lpp) +
8823 				    SCSI_LOG_PARAM_HDR_LEN +
8824 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8825 
8826 				--index;	/* Back up to previous entry */
8827 				if (index < 0) {
8828 					if (block_num > 0) {
8829 						--block_num;
8830 					} else {
8831 						struct read_log_ext_directory
8832 						    logdir;
8833 
8834 						rval =
8835 						    sata_read_log_ext_directory(
8836 						    sata_hba_inst, sdinfo,
8837 						    &logdir);
8838 						if (rval == -1)
8839 							goto out;
8840 						if ((logdir.read_log_ext_vers
8841 						    [0] == 0) &&
8842 						    (logdir.read_log_ext_vers
8843 						    [1] == 0))
8844 							goto out;
8845 						block_num =
8846 						    logdir.read_log_ext_nblks
8847 						    [EXT_SMART_SELFTEST_LOG_PAGE
8848 						    - 1][0];
8849 						block_num |= logdir.
8850 						    read_log_ext_nblks
8851 						    [EXT_SMART_SELFTEST_LOG_PAGE
8852 						    - 1][1] << 8;
8853 						--block_num;
8854 						only_one_block =
8855 						    (block_num == 0);
8856 					}
8857 					rval = sata_ext_smart_selftest_read_log(
8858 					    sata_hba_inst, sdinfo,
8859 					    ext_selftest_log, block_num);
8860 					if (rval != 0)
8861 						goto out;
8862 
8863 					index =
8864 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8865 					    1;
8866 				}
8867 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8868 				entry = &ext_selftest_log->
8869 				    smart_ext_selftest_log_entries[index];
8870 			}
8871 		}
8872 out:
8873 		kmem_free(ext_selftest_log,
8874 		    sizeof (struct smart_ext_selftest_log));
8875 	} else {
8876 		struct smart_selftest_log *selftest_log;
8877 
8878 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8879 		    KM_SLEEP);
8880 
8881 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8882 		    selftest_log);
8883 
8884 		if (rval == 0) {
8885 			int index;
8886 			int count;
8887 			struct smart_selftest_log_entry *entry;
8888 			static const struct smart_selftest_log_entry empty =
8889 			    { 0 };
8890 
8891 			index = selftest_log->smart_selftest_log_index;
8892 			if (index == 0)
8893 				goto done;
8894 			--index;	/* Correct for 0 origin */
8895 			entry = &selftest_log->
8896 			    smart_selftest_log_entries[index];
8897 			for (count = 1;
8898 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8899 			    ++count) {
8900 				uint8_t status;
8901 				uint8_t code;
8902 				uint8_t sense_key;
8903 				uint8_t add_sense_code;
8904 				uint8_t add_sense_code_qual;
8905 
8906 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8907 					goto done;
8908 
8909 				lpp->param_code[0] = 0;
8910 				lpp->param_code[1] = count;
8911 				lpp->param_ctrl_flags =
8912 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8913 				lpp->param_len =
8914 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8915 
8916 				status = entry->smart_selftest_log_status;
8917 				status >>= 4;
8918 				switch (status) {
8919 				case 0:
8920 				default:
8921 					sense_key = KEY_NO_SENSE;
8922 					add_sense_code =
8923 					    SD_SCSI_ASC_NO_ADD_SENSE;
8924 					break;
8925 				case 1:
8926 					sense_key = KEY_ABORTED_COMMAND;
8927 					add_sense_code =
8928 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8929 					add_sense_code_qual = SCSI_COMPONENT_81;
8930 					break;
8931 				case 2:
8932 					sense_key = KEY_ABORTED_COMMAND;
8933 					add_sense_code =
8934 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8935 					add_sense_code_qual = SCSI_COMPONENT_82;
8936 					break;
8937 				case 3:
8938 					sense_key = KEY_ABORTED_COMMAND;
8939 					add_sense_code =
8940 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8941 					add_sense_code_qual = SCSI_COMPONENT_83;
8942 					break;
8943 				case 4:
8944 					sense_key = KEY_HARDWARE_ERROR;
8945 					add_sense_code =
8946 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8947 					add_sense_code_qual = SCSI_COMPONENT_84;
8948 					break;
8949 				case 5:
8950 					sense_key = KEY_HARDWARE_ERROR;
8951 					add_sense_code =
8952 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8953 					add_sense_code_qual = SCSI_COMPONENT_85;
8954 					break;
8955 				case 6:
8956 					sense_key = KEY_HARDWARE_ERROR;
8957 					add_sense_code =
8958 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8959 					add_sense_code_qual = SCSI_COMPONENT_86;
8960 					break;
8961 				case 7:
8962 					sense_key = KEY_MEDIUM_ERROR;
8963 					add_sense_code =
8964 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8965 					add_sense_code_qual = SCSI_COMPONENT_87;
8966 					break;
8967 				case 8:
8968 					sense_key = KEY_HARDWARE_ERROR;
8969 					add_sense_code =
8970 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8971 					add_sense_code_qual = SCSI_COMPONENT_88;
8972 					break;
8973 				}
8974 				code = 0;	/* unspecified */
8975 				status |= (code << 4);
8976 				lpp->param_values[0] = status;
8977 				lpp->param_values[1] = 0; /* unspecified */
8978 				lpp->param_values[2] = entry->
8979 				    smart_selftest_log_timestamp[1];
8980 				lpp->param_values[3] = entry->
8981 				    smart_selftest_log_timestamp[0];
8982 				if (status != 0) {
8983 					lpp->param_values[4] = 0;
8984 					lpp->param_values[5] = 0;
8985 					lpp->param_values[6] = 0;
8986 					lpp->param_values[7] = 0;
8987 					lpp->param_values[8] = entry->
8988 					    smart_selftest_log_failing_lba[3];
8989 					lpp->param_values[9] = entry->
8990 					    smart_selftest_log_failing_lba[2];
8991 					lpp->param_values[10] = entry->
8992 					    smart_selftest_log_failing_lba[1];
8993 					lpp->param_values[11] = entry->
8994 					    smart_selftest_log_failing_lba[0];
8995 				} else {	/* No block address */
8996 					lpp->param_values[4] = 0xff;
8997 					lpp->param_values[5] = 0xff;
8998 					lpp->param_values[6] = 0xff;
8999 					lpp->param_values[7] = 0xff;
9000 					lpp->param_values[8] = 0xff;
9001 					lpp->param_values[9] = 0xff;
9002 					lpp->param_values[10] = 0xff;
9003 					lpp->param_values[11] = 0xff;
9004 				}
9005 				lpp->param_values[12] = sense_key;
9006 				lpp->param_values[13] = add_sense_code;
9007 				lpp->param_values[14] = add_sense_code_qual;
9008 				lpp->param_values[15] = 0; /* undefined */
9009 
9010 				lpp = (struct log_parameter *)
9011 				    (((uint8_t *)lpp) +
9012 				    SCSI_LOG_PARAM_HDR_LEN +
9013 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9014 				--index;	/* back up to previous entry */
9015 				if (index < 0) {
9016 					index =
9017 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9018 				}
9019 				entry = &selftest_log->
9020 				    smart_selftest_log_entries[index];
9021 			}
9022 		}
9023 done:
9024 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9025 	}
9026 
9027 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9028 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9029 }
9030 
9031 /*
9032  * sata_build_lsense_page_2f() is used to create the
9033  * SCSI LOG SENSE page 0x2f (informational exceptions)
9034  *
9035  * Takes a sata_drive_info t * and the address of a buffer
9036  * in which to create the page information as well as a sata_hba_inst_t *.
9037  *
9038  * Returns the number of bytes valid in the buffer.
9039  *
9040  * Because it invokes function(s) that send synchronously executed command
9041  * to the HBA, it cannot be called in the interrupt context.
9042  */
9043 static	int
9044 sata_build_lsense_page_2f(
9045 	sata_drive_info_t *sdinfo,
9046 	uint8_t *buf,
9047 	sata_hba_inst_t *sata_hba_inst)
9048 {
9049 	struct log_parameter *lpp = (struct log_parameter *)buf;
9050 	int rval;
9051 	uint8_t *smart_data;
9052 	uint8_t temp;
9053 	sata_id_t *sata_id;
9054 #define	SMART_NO_TEMP	0xff
9055 
9056 	lpp->param_code[0] = 0;
9057 	lpp->param_code[1] = 0;
9058 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9059 
9060 	/* Now get the SMART status w.r.t. threshold exceeded */
9061 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9062 	switch (rval) {
9063 	case 1:
9064 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9065 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9066 		break;
9067 	case 0:
9068 	case -1:	/* failed to get data */
9069 		lpp->param_values[0] = 0;	/* No failure predicted */
9070 		lpp->param_values[1] = 0;
9071 		break;
9072 #if defined(SATA_DEBUG)
9073 	default:
9074 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9075 		/* NOTREACHED */
9076 #endif
9077 	}
9078 
9079 	sata_id = &sdinfo->satadrv_id;
9080 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9081 		temp = SMART_NO_TEMP;
9082 	else {
9083 		/* Now get the temperature */
9084 		smart_data = kmem_zalloc(512, KM_SLEEP);
9085 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9086 		    SCT_STATUS_LOG_PAGE, 1);
9087 		if (rval == -1)
9088 			temp = SMART_NO_TEMP;
9089 		else {
9090 			temp = smart_data[200];
9091 			if (temp & 0x80) {
9092 				if (temp & 0x7f)
9093 					temp = 0;
9094 				else
9095 					temp = SMART_NO_TEMP;
9096 			}
9097 		}
9098 		kmem_free(smart_data, 512);
9099 	}
9100 
9101 	lpp->param_values[2] = temp;	/* most recent temperature */
9102 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9103 
9104 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9105 
9106 
9107 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9108 }
9109 
9110 /*
9111  * sata_build_lsense_page_30() is used to create the
9112  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9113  *
9114  * Takes a sata_drive_info t * and the address of a buffer
9115  * in which to create the page information as well as a sata_hba_inst_t *.
9116  *
9117  * Returns the number of bytes valid in the buffer.
9118  */
9119 static int
9120 sata_build_lsense_page_30(
9121 	sata_drive_info_t *sdinfo,
9122 	uint8_t *buf,
9123 	sata_hba_inst_t *sata_hba_inst)
9124 {
9125 	struct smart_data *smart_data = (struct smart_data *)buf;
9126 	int rval;
9127 
9128 	/* Now do the SMART READ DATA */
9129 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9130 	if (rval == -1)
9131 		return (0);
9132 
9133 	return (sizeof (struct smart_data));
9134 }
9135 
9136 /*
9137  * sata_build_lsense_page_0e() is used to create the
9138  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9139  *
9140  * Date of Manufacture (0x0001)
9141  *	YEAR = "0000"
9142  *	WEEK = "00"
9143  * Accounting Date (0x0002)
9144  *	6 ASCII space character(20h)
9145  * Specified cycle count over device lifetime
9146  *	VALUE - THRESH - the delta between max and min;
9147  * Accumulated start-stop cycles
9148  *	VALUE - WORST - the accumulated cycles;
9149  *
9150  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9151  *
9152  * Takes a sata_drive_info t * and the address of a buffer
9153  * in which to create the page information as well as a sata_hba_inst_t *.
9154  *
9155  * Returns the number of bytes valid in the buffer.
9156  */
9157 static	int
9158 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9159 	sata_pkt_txlate_t *spx)
9160 {
9161 	struct start_stop_cycle_counter_log *log_page;
9162 	int i, rval, index;
9163 	uint8_t smart_data[512], id, value, worst, thresh;
9164 	uint32_t max_count, cycles;
9165 
9166 	/* Now do the SMART READ DATA */
9167 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9168 	    (struct smart_data *)smart_data);
9169 	if (rval == -1)
9170 		return (0);
9171 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9172 		index = (i * 12) + 2;
9173 		id = smart_data[index];
9174 		if (id != SMART_START_STOP_COUNT_ID)
9175 			continue;
9176 		else {
9177 			thresh = smart_data[index + 2];
9178 			value = smart_data[index + 3];
9179 			worst = smart_data[index + 4];
9180 			break;
9181 		}
9182 	}
9183 	if (id != SMART_START_STOP_COUNT_ID)
9184 		return (0);
9185 	max_count = value - thresh;
9186 	cycles = value - worst;
9187 
9188 	log_page = (struct start_stop_cycle_counter_log *)buf;
9189 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9190 	log_page->code = 0x0e;
9191 	log_page->page_len_low = 0x24;
9192 
9193 	log_page->manufactor_date_low = 0x1;
9194 	log_page->param_1.fmt_link = 0x1; /* 01b */
9195 	log_page->param_len_1 = 0x06;
9196 	for (i = 0; i < 4; i++) {
9197 		log_page->year_manu[i] = 0x30;
9198 		if (i < 2)
9199 			log_page->week_manu[i] = 0x30;
9200 	}
9201 
9202 	log_page->account_date_low = 0x02;
9203 	log_page->param_2.fmt_link = 0x01; /* 01b */
9204 	log_page->param_len_2 = 0x06;
9205 	for (i = 0; i < 4; i++) {
9206 		log_page->year_account[i] = 0x20;
9207 		if (i < 2)
9208 			log_page->week_account[i] = 0x20;
9209 	}
9210 
9211 	log_page->lifetime_code_low = 0x03;
9212 	log_page->param_3.fmt_link = 0x03; /* 11b */
9213 	log_page->param_len_3 = 0x04;
9214 	/* VALUE - THRESH - the delta between max and min */
9215 	log_page->cycle_code_low = 0x04;
9216 	log_page->param_4.fmt_link = 0x03; /* 11b */
9217 	log_page->param_len_4 = 0x04;
9218 	/* WORST - THRESH - the distance from 'now' to min */
9219 
9220 	for (i = 0; i < 4; i++) {
9221 		log_page->cycle_lifetime[i] =
9222 		    (max_count >> (8 * (3 - i))) & 0xff;
9223 		log_page->cycle_accumulated[i] =
9224 		    (cycles >> (8 * (3 - i))) & 0xff;
9225 	}
9226 
9227 	return (sizeof (struct start_stop_cycle_counter_log));
9228 }
9229 
9230 /*
9231  * This function was used for build a ATA read verify sector command
9232  */
9233 static void
9234 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9235 {
9236 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9237 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9238 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9239 
9240 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9241 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9242 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9243 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9244 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9245 	scmd->satacmd_features_reg = 0;
9246 	scmd->satacmd_status_reg = 0;
9247 	scmd->satacmd_error_reg = 0;
9248 }
9249 
9250 /*
9251  * This function was used for building an ATA
9252  * command, and only command register need to
9253  * be defined, other register will be zero or na.
9254  */
9255 static void
9256 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9257 {
9258 	scmd->satacmd_addr_type = 0;
9259 	scmd->satacmd_cmd_reg = cmd;
9260 	scmd->satacmd_device_reg = 0;
9261 	scmd->satacmd_sec_count_lsb = 0;
9262 	scmd->satacmd_lba_low_lsb = 0;
9263 	scmd->satacmd_lba_mid_lsb = 0;
9264 	scmd->satacmd_lba_high_lsb = 0;
9265 	scmd->satacmd_features_reg = 0;
9266 	scmd->satacmd_status_reg = 0;
9267 	scmd->satacmd_error_reg = 0;
9268 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9269 }
9270 
9271 /*
9272  * This function was used for changing the standby
9273  * timer format from SCSI to ATA.
9274  */
9275 static uint8_t
9276 sata_get_standby_timer(uint8_t *timer)
9277 {
9278 	uint32_t i = 0, count = 0;
9279 	uint8_t ata_count;
9280 
9281 	for (i = 0; i < 4; i++) {
9282 		count = count << 8 | timer[i];
9283 	}
9284 
9285 	if (count == 0)
9286 		return (0);
9287 
9288 	if (count >= 1 && count <= 12000)
9289 		ata_count = (count -1) / 50 + 1;
9290 	else if (count > 12000 && count <= 12600)
9291 		ata_count = 0xfc;
9292 	else if (count > 12601 && count <= 12750)
9293 		ata_count = 0xff;
9294 	else if (count > 12750 && count <= 17999)
9295 		ata_count = 0xf1;
9296 	else if (count > 18000 && count <= 198000)
9297 		ata_count = count / 18000 + 240;
9298 	else
9299 		ata_count = 0xfd;
9300 	return (ata_count);
9301 }
9302 
9303 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9304 
9305 /*
9306  * Start command for ATAPI device.
9307  * This function processes scsi_pkt requests.
9308  * Now CD/DVD, tape and ATAPI disk devices are supported.
9309  * Most commands are packet without any translation into Packet Command.
9310  * Some may be trapped and executed as SATA commands (not clear which one).
9311  *
9312  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9313  * execution).
9314  * Returns other TRAN_XXXX codes if command is not accepted or completed
9315  * (see return values for sata_hba_start()).
9316  *
9317  * Note:
9318  * Inquiry cdb format differs between transport version 2 and 3.
9319  * However, the transport version 3 devices that were checked did not adhere
9320  * to the specification (ignored MSB of the allocation length). Therefore,
9321  * the transport version is not checked, but Inquiry allocation length is
9322  * truncated to 255 bytes if the original allocation length set-up by the
9323  * target driver is greater than 255 bytes.
9324  */
9325 static int
9326 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9327 {
9328 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9329 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9330 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9331 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9332 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9333 	    &spx->txlt_sata_pkt->satapkt_device);
9334 	int cport = SATA_TXLT_CPORT(spx);
9335 	int cdblen;
9336 	int rval, reason;
9337 	int synch;
9338 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9339 
9340 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
9341 
9342 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9343 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9344 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9345 		return (rval);
9346 	}
9347 
9348 	/*
9349 	 * ATAPI device executes some ATA commands in addition to those
9350 	 * commands sent via PACKET command. These ATA commands may be
9351 	 * executed by the regular SATA translation functions. None needs
9352 	 * to be captured now.
9353 	 *
9354 	 * Commands sent via PACKET command include:
9355 	 *	MMC command set for ATAPI CD/DVD device
9356 	 *	SSC command set for ATAPI TAPE device
9357 	 *	SBC command set for ATAPI disk device
9358 	 *
9359 	 */
9360 
9361 	/* Check the size of cdb */
9362 
9363 	switch (GETGROUP(cdbp)) {
9364 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9365 		/*
9366 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9367 		 * therefore require special handling.  Return failure, for now.
9368 		 */
9369 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9370 		return (TRAN_BADPKT);
9371 
9372 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9373 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9374 		/* obtain length from the scsi_pkt */
9375 		cdblen = scsipkt->pkt_cdblen;
9376 		break;
9377 
9378 	default:
9379 		/* CDB's length is statically known, per SPC-4 */
9380 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9381 		break;
9382 	}
9383 
9384 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9385 		sata_log(NULL, CE_WARN,
9386 		    "sata: invalid ATAPI cdb length %d",
9387 		    cdblen);
9388 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9389 		return (TRAN_BADPKT);
9390 	}
9391 
9392 	SATAATAPITRACE(spx, cdblen);
9393 
9394 	/*
9395 	 * For non-read/write commands we need to
9396 	 * map buffer
9397 	 */
9398 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9399 	case SCMD_READ:
9400 	case SCMD_READ_G1:
9401 	case SCMD_READ_G5:
9402 	case SCMD_READ_G4:
9403 	case SCMD_WRITE:
9404 	case SCMD_WRITE_G1:
9405 	case SCMD_WRITE_G5:
9406 	case SCMD_WRITE_G4:
9407 		break;
9408 	default:
9409 		if (bp != NULL) {
9410 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9411 				bp_mapin(bp);
9412 		}
9413 		break;
9414 	}
9415 	/*
9416 	 * scmd->satacmd_flags.sata_data_direction default -
9417 	 * SATA_DIR_NODATA_XFER - is set by
9418 	 * sata_txlt_generic_pkt_info().
9419 	 */
9420 	if (scmd->satacmd_bp) {
9421 		if (scmd->satacmd_bp->b_flags & B_READ) {
9422 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9423 		} else {
9424 			scmd->satacmd_flags.sata_data_direction =
9425 			    SATA_DIR_WRITE;
9426 		}
9427 	}
9428 
9429 	/*
9430 	 * Set up ATAPI packet command.
9431 	 */
9432 
9433 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9434 
9435 	/* Copy cdb into sata_cmd */
9436 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9437 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9438 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9439 
9440 	/* See note in the command header */
9441 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9442 		if (scmd->satacmd_acdb[3] != 0)
9443 			scmd->satacmd_acdb[4] = 255;
9444 	}
9445 
9446 #ifdef SATA_DEBUG
9447 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9448 		uint8_t *p = scmd->satacmd_acdb;
9449 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9450 
9451 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9452 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9453 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9454 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9455 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9456 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9457 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9458 	}
9459 #endif
9460 
9461 	/*
9462 	 * Preset request sense data to NO SENSE.
9463 	 * If there is no way to get error information via Request Sense,
9464 	 * the packet request sense data would not have to be modified by HBA,
9465 	 * but it could be returned as is.
9466 	 */
9467 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9468 	sata_fixed_sense_data_preset(
9469 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9470 
9471 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9472 		/* Need callback function */
9473 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9474 		synch = FALSE;
9475 	} else
9476 		synch = TRUE;
9477 
9478 	/* Transfer command to HBA */
9479 	if (sata_hba_start(spx, &rval) != 0) {
9480 		/* Pkt not accepted for execution */
9481 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
9482 		return (rval);
9483 	}
9484 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
9485 	/*
9486 	 * If execution is non-synchronous,
9487 	 * a callback function will handle potential errors, translate
9488 	 * the response and will do a callback to a target driver.
9489 	 * If it was synchronous, use the same framework callback to check
9490 	 * an execution status.
9491 	 */
9492 	if (synch) {
9493 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9494 		    "synchronous execution status %x\n",
9495 		    spx->txlt_sata_pkt->satapkt_reason);
9496 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9497 	}
9498 	return (TRAN_ACCEPT);
9499 }
9500 
9501 
9502 /*
9503  * ATAPI Packet command completion.
9504  *
9505  * Failure of the command passed via Packet command are considered device
9506  * error. SATA HBA driver would have to retrieve error data (via Request
9507  * Sense command delivered via error retrieval sata packet) and copy it
9508  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9509  */
9510 static void
9511 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9512 {
9513 	sata_pkt_txlate_t *spx =
9514 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9515 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9516 	struct scsi_extended_sense *sense;
9517 	struct buf *bp;
9518 	int rval;
9519 
9520 #ifdef SATA_DEBUG
9521 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9522 #endif
9523 
9524 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9525 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9526 
9527 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9528 		/* Normal completion */
9529 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9530 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9531 		scsipkt->pkt_reason = CMD_CMPLT;
9532 		*scsipkt->pkt_scbp = STATUS_GOOD;
9533 		if (spx->txlt_tmp_buf != NULL) {
9534 			/* Temporary buffer was used */
9535 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9536 			if (bp->b_flags & B_READ) {
9537 				rval = ddi_dma_sync(
9538 				    spx->txlt_buf_dma_handle, 0, 0,
9539 				    DDI_DMA_SYNC_FORCPU);
9540 				ASSERT(rval == DDI_SUCCESS);
9541 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9542 				    bp->b_bcount);
9543 			}
9544 		}
9545 	} else {
9546 		/*
9547 		 * Something went wrong - analyze return
9548 		 */
9549 		*scsipkt->pkt_scbp = STATUS_CHECK;
9550 		sense = sata_arq_sense(spx);
9551 
9552 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9553 			/*
9554 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9555 			 * Under this condition ERR bit is set for ATA command,
9556 			 * and CHK bit set for ATAPI command.
9557 			 *
9558 			 * Please check st_intr & sdintr about how pkt_reason
9559 			 * is used.
9560 			 */
9561 			scsipkt->pkt_reason = CMD_CMPLT;
9562 
9563 			/*
9564 			 * We may not have ARQ data if there was a double
9565 			 * error. But sense data in sata packet was pre-set
9566 			 * with NO SENSE so it is valid even if HBA could
9567 			 * not retrieve a real sense data.
9568 			 * Just copy this sense data into scsi pkt sense area.
9569 			 */
9570 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9571 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9572 #ifdef SATA_DEBUG
9573 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9574 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9575 				    "sata_txlt_atapi_completion: %02x\n"
9576 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9577 				    "          %02x %02x %02x %02x %02x %02x "
9578 				    "          %02x %02x %02x %02x %02x %02x\n",
9579 				    scsipkt->pkt_reason,
9580 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9581 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9582 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9583 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9584 				    rqsp[16], rqsp[17]);
9585 			}
9586 #endif
9587 		} else {
9588 			switch (sata_pkt->satapkt_reason) {
9589 			case SATA_PKT_PORT_ERROR:
9590 				/*
9591 				 * We have no device data.
9592 				 */
9593 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9594 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9595 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9596 				    STATE_GOT_STATUS);
9597 				sense->es_key = KEY_HARDWARE_ERROR;
9598 				break;
9599 
9600 			case SATA_PKT_TIMEOUT:
9601 				scsipkt->pkt_reason = CMD_TIMEOUT;
9602 				scsipkt->pkt_statistics |=
9603 				    STAT_TIMEOUT | STAT_DEV_RESET;
9604 				/*
9605 				 * Need to check if HARDWARE_ERROR/
9606 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9607 				 * appropriate.
9608 				 */
9609 				break;
9610 
9611 			case SATA_PKT_ABORTED:
9612 				scsipkt->pkt_reason = CMD_ABORTED;
9613 				scsipkt->pkt_statistics |= STAT_ABORTED;
9614 				/* Should we set key COMMAND_ABPRTED? */
9615 				break;
9616 
9617 			case SATA_PKT_RESET:
9618 				scsipkt->pkt_reason = CMD_RESET;
9619 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9620 				/*
9621 				 * May be we should set Unit Attention /
9622 				 * Reset. Perhaps the same should be
9623 				 * returned for disks....
9624 				 */
9625 				sense->es_key = KEY_UNIT_ATTENTION;
9626 				sense->es_add_code = SD_SCSI_ASC_RESET;
9627 				break;
9628 
9629 			default:
9630 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9631 				    "sata_txlt_atapi_completion: "
9632 				    "invalid packet completion reason"));
9633 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9634 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9635 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9636 				    STATE_GOT_STATUS);
9637 				break;
9638 			}
9639 		}
9640 	}
9641 
9642 	SATAATAPITRACE(spx, 0);
9643 
9644 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9645 	    scsipkt->pkt_comp != NULL) {
9646 		/* scsi callback required */
9647 		(*scsipkt->pkt_comp)(scsipkt);
9648 	}
9649 }
9650 
9651 /*
9652  * Set up error retrieval sata command for ATAPI Packet Command error data
9653  * recovery.
9654  *
9655  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9656  * returns SATA_FAILURE otherwise.
9657  */
9658 
9659 static int
9660 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9661 {
9662 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9663 	sata_cmd_t *scmd;
9664 	struct buf *bp;
9665 
9666 	/*
9667 	 * Allocate dma-able buffer error data.
9668 	 * Buffer allocation will take care of buffer alignment and other DMA
9669 	 * attributes.
9670 	 */
9671 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9672 	if (bp == NULL) {
9673 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9674 		    "sata_get_err_retrieval_pkt: "
9675 		    "cannot allocate buffer for error data", NULL);
9676 		return (SATA_FAILURE);
9677 	}
9678 	bp_mapin(bp); /* make data buffer accessible */
9679 
9680 	/* Operation modes are up to the caller */
9681 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9682 
9683 	/* Synchronous mode, no callback - may be changed by the caller */
9684 	spkt->satapkt_comp = NULL;
9685 	spkt->satapkt_time = sata_default_pkt_time;
9686 
9687 	scmd = &spkt->satapkt_cmd;
9688 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9689 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9690 
9691 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9692 
9693 	/*
9694 	 * Set-up acdb. Request Sense CDB (packet command content) is
9695 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9696 	 * it is transfered into packet FIS).
9697 	 */
9698 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9699 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9700 	/* Following zeroing of pad bytes may not be necessary */
9701 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9702 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9703 
9704 	/*
9705 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9706 	 * before accessing it. Handle is in usual place in translate struct.
9707 	 */
9708 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9709 
9710 	/*
9711 	 * Preset request sense data to NO SENSE.
9712 	 * Here it is redundant, only for a symetry with scsi-originated
9713 	 * packets. It should not be used for anything but debugging.
9714 	 */
9715 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9716 	sata_fixed_sense_data_preset(
9717 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9718 
9719 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9720 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9721 
9722 	return (SATA_SUCCESS);
9723 }
9724 
9725 /*
9726  * Set-up ATAPI packet command.
9727  * Data transfer direction has to be set-up in sata_cmd structure prior to
9728  * calling this function.
9729  *
9730  * Returns void
9731  */
9732 
9733 static void
9734 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9735 {
9736 	scmd->satacmd_addr_type = 0;		/* N/A */
9737 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9738 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9739 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9740 	scmd->satacmd_lba_high_lsb =
9741 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9742 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9743 
9744 	/*
9745 	 * We want all data to be transfered via DMA.
9746 	 * But specify it only if drive supports DMA and DMA mode is
9747 	 * selected - some drives are sensitive about it.
9748 	 * Hopefully it wil work for all drives....
9749 	 */
9750 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9751 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9752 
9753 	/*
9754 	 * Features register requires special care for devices that use
9755 	 * Serial ATA bridge - they need an explicit specification of
9756 	 * the data transfer direction for Packet DMA commands.
9757 	 * Setting this bit is harmless if DMA is not used.
9758 	 *
9759 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9760 	 * spec they follow.
9761 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9762 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9763 	 * ATA/ATAPI-7 support is explicitly indicated.
9764 	 */
9765 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9766 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9767 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9768 		/*
9769 		 * Specification of major version is valid and version 7
9770 		 * is supported. It does automatically imply that all
9771 		 * spec features are supported. For now, we assume that
9772 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9773 		 */
9774 		if ((sdinfo->satadrv_id.ai_dirdma &
9775 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9776 			if (scmd->satacmd_flags.sata_data_direction ==
9777 			    SATA_DIR_READ)
9778 			scmd->satacmd_features_reg |=
9779 			    SATA_ATAPI_F_DATA_DIR_READ;
9780 		}
9781 	}
9782 }
9783 
9784 
9785 #ifdef SATA_DEBUG
9786 
9787 /* Display 18 bytes of Inquiry data */
9788 static void
9789 sata_show_inqry_data(uint8_t *buf)
9790 {
9791 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9792 	uint8_t *p;
9793 
9794 	cmn_err(CE_NOTE, "Inquiry data:");
9795 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9796 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9797 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9798 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9799 	    SATA_ATAPI_TRANS_VERSION(inq));
9800 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9801 	    inq->inq_rdf, inq->inq_aenc);
9802 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9803 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9804 	p = (uint8_t *)inq->inq_vid;
9805 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9806 	    "%02x %02x %02x %02x",
9807 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9808 	p = (uint8_t *)inq->inq_vid;
9809 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9810 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9811 
9812 	p = (uint8_t *)inq->inq_pid;
9813 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9814 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9815 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9816 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9817 	p = (uint8_t *)inq->inq_pid;
9818 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9819 	    "%c %c %c %c %c %c %c %c",
9820 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9821 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9822 
9823 	p = (uint8_t *)inq->inq_revision;
9824 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9825 	    p[0], p[1], p[2], p[3]);
9826 	p = (uint8_t *)inq->inq_revision;
9827 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9828 	    p[0], p[1], p[2], p[3]);
9829 
9830 }
9831 
9832 
9833 static void
9834 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9835 {
9836 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9837 
9838 	if (scsi_pkt == NULL)
9839 		return;
9840 	if (count != 0) {
9841 		/* saving cdb */
9842 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9843 		    SATA_ATAPI_MAX_CDB_LEN);
9844 		bcopy(scsi_pkt->pkt_cdbp,
9845 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9846 	} else {
9847 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9848 		    sts_sensedata,
9849 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9850 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9851 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9852 		    scsi_pkt->pkt_reason;
9853 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9854 		    spx->txlt_sata_pkt->satapkt_reason;
9855 
9856 		if (++sata_atapi_trace_index >= 64)
9857 			sata_atapi_trace_index = 0;
9858 	}
9859 }
9860 
9861 #endif
9862 
9863 /*
9864  * Fetch inquiry data from ATAPI device
9865  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9866  *
9867  * Note:
9868  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9869  * where the caller expects to see the inquiry data.
9870  *
9871  */
9872 
9873 static int
9874 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9875     sata_address_t *saddr, struct scsi_inquiry *inq)
9876 {
9877 	sata_pkt_txlate_t *spx;
9878 	sata_pkt_t *spkt;
9879 	struct buf *bp;
9880 	sata_drive_info_t *sdinfo;
9881 	sata_cmd_t *scmd;
9882 	int rval;
9883 	uint8_t *rqsp;
9884 	dev_info_t *dip = SATA_DIP(sata_hba);
9885 #ifdef SATA_DEBUG
9886 	char msg_buf[MAXPATHLEN];
9887 #endif
9888 
9889 	ASSERT(sata_hba != NULL);
9890 
9891 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9892 	spx->txlt_sata_hba_inst = sata_hba;
9893 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9894 	spkt = sata_pkt_alloc(spx, NULL);
9895 	if (spkt == NULL) {
9896 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9897 		return (SATA_FAILURE);
9898 	}
9899 	/* address is needed now */
9900 	spkt->satapkt_device.satadev_addr = *saddr;
9901 
9902 	/* scsi_inquiry size buffer */
9903 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9904 	if (bp == NULL) {
9905 		sata_pkt_free(spx);
9906 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9907 		SATA_LOG_D((sata_hba, CE_WARN,
9908 		    "sata_get_atapi_inquiry_data: "
9909 		    "cannot allocate data buffer"));
9910 		return (SATA_FAILURE);
9911 	}
9912 	bp_mapin(bp); /* make data buffer accessible */
9913 
9914 	scmd = &spkt->satapkt_cmd;
9915 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9916 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9917 
9918 	/* Use synchronous mode */
9919 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9920 	spkt->satapkt_comp = NULL;
9921 	spkt->satapkt_time = sata_default_pkt_time;
9922 
9923 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9924 
9925 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9926 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9927 
9928 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
9929 	sdinfo = sata_get_device_info(sata_hba,
9930 	    &spx->txlt_sata_pkt->satapkt_device);
9931 	if (sdinfo == NULL) {
9932 		/* we have to be carefull about the disapearing device */
9933 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9934 		rval = SATA_FAILURE;
9935 		goto cleanup;
9936 	}
9937 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9938 
9939 	/*
9940 	 * Set-up acdb. This works for atapi transport version 2 and later.
9941 	 */
9942 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9943 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9944 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
9945 	scmd->satacmd_acdb[1] = 0x00;
9946 	scmd->satacmd_acdb[2] = 0x00;
9947 	scmd->satacmd_acdb[3] = 0x00;
9948 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9949 	scmd->satacmd_acdb[5] = 0x00;
9950 
9951 	sata_fixed_sense_data_preset(
9952 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9953 
9954 	/* Transfer command to HBA */
9955 	if (sata_hba_start(spx, &rval) != 0) {
9956 		/* Pkt not accepted for execution */
9957 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9958 		    "sata_get_atapi_inquiry_data: "
9959 		    "Packet not accepted for execution - ret: %02x", rval);
9960 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9961 		rval = SATA_FAILURE;
9962 		goto cleanup;
9963 	}
9964 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
9965 
9966 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9967 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
9968 		    "sata_get_atapi_inquiry_data: "
9969 		    "Packet completed successfully - ret: %02x", rval);
9970 		if (spx->txlt_buf_dma_handle != NULL) {
9971 			/*
9972 			 * Sync buffer. Handle is in usual place in translate
9973 			 * struct.
9974 			 */
9975 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
9976 			    DDI_DMA_SYNC_FORCPU);
9977 			ASSERT(rval == DDI_SUCCESS);
9978 		}
9979 
9980 		if (sata_check_for_dma_error(dip, spx)) {
9981 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
9982 			rval = SATA_FAILURE;
9983 		} else {
9984 			/*
9985 			 * Normal completion - copy data into caller's buffer
9986 			 */
9987 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
9988 			    sizeof (struct scsi_inquiry));
9989 #ifdef SATA_DEBUG
9990 			if (sata_debug_flags & SATA_DBG_ATAPI) {
9991 				sata_show_inqry_data((uint8_t *)inq);
9992 			}
9993 #endif
9994 			rval = SATA_SUCCESS;
9995 		}
9996 	} else {
9997 		/*
9998 		 * Something went wrong - analyze return - check rqsense data
9999 		 */
10000 		rval = SATA_FAILURE;
10001 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10002 			/*
10003 			 * ARQ data hopefull show something other than NO SENSE
10004 			 */
10005 			rqsp = scmd->satacmd_rqsense;
10006 #ifdef SATA_DEBUG
10007 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10008 				msg_buf[0] = '\0';
10009 				(void) snprintf(msg_buf, MAXPATHLEN,
10010 				    "ATAPI packet completion reason: %02x\n"
10011 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10012 				    "          %02x %02x %02x %02x %02x %02x\n"
10013 				    "          %02x %02x %02x %02x %02x %02x",
10014 				    spkt->satapkt_reason,
10015 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10016 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10017 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10018 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10019 				    rqsp[16], rqsp[17]);
10020 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10021 				    "%s", msg_buf);
10022 			}
10023 #endif
10024 		} else {
10025 			switch (spkt->satapkt_reason) {
10026 			case SATA_PKT_PORT_ERROR:
10027 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10028 				    "sata_get_atapi_inquiry_data: "
10029 				    "packet reason: port error", NULL);
10030 				break;
10031 
10032 			case SATA_PKT_TIMEOUT:
10033 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10034 				    "sata_get_atapi_inquiry_data: "
10035 				    "packet reason: timeout", NULL);
10036 				break;
10037 
10038 			case SATA_PKT_ABORTED:
10039 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10040 				    "sata_get_atapi_inquiry_data: "
10041 				    "packet reason: aborted", NULL);
10042 				break;
10043 
10044 			case SATA_PKT_RESET:
10045 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10046 				    "sata_get_atapi_inquiry_data: "
10047 				    "packet reason: reset\n", NULL);
10048 				break;
10049 			default:
10050 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10051 				    "sata_get_atapi_inquiry_data: "
10052 				    "invalid packet reason: %02x\n",
10053 				    spkt->satapkt_reason);
10054 				break;
10055 			}
10056 		}
10057 	}
10058 cleanup:
10059 	sata_free_local_buffer(spx);
10060 	sata_pkt_free(spx);
10061 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10062 	return (rval);
10063 }
10064 
10065 
10066 
10067 
10068 
10069 #if 0
10070 #ifdef SATA_DEBUG
10071 
10072 /*
10073  * Test ATAPI packet command.
10074  * Single threaded test: send packet command in synch mode, process completion
10075  *
10076  */
10077 static void
10078 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10079 {
10080 	sata_pkt_txlate_t *spx;
10081 	sata_pkt_t *spkt;
10082 	struct buf *bp;
10083 	sata_device_t sata_device;
10084 	sata_drive_info_t *sdinfo;
10085 	sata_cmd_t *scmd;
10086 	int rval;
10087 	uint8_t *rqsp;
10088 
10089 	ASSERT(sata_hba_inst != NULL);
10090 	sata_device.satadev_addr.cport = cport;
10091 	sata_device.satadev_addr.pmport = 0;
10092 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10093 	sata_device.satadev_rev = SATA_DEVICE_REV;
10094 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10095 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10096 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10097 	if (sdinfo == NULL) {
10098 		sata_log(sata_hba_inst, CE_WARN,
10099 		    "sata_test_atapi_packet_command: "
10100 		    "no device info for cport %d",
10101 		    sata_device.satadev_addr.cport);
10102 		return;
10103 	}
10104 
10105 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10106 	spx->txlt_sata_hba_inst = sata_hba_inst;
10107 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10108 	spkt = sata_pkt_alloc(spx, NULL);
10109 	if (spkt == NULL) {
10110 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10111 		return;
10112 	}
10113 	/* address is needed now */
10114 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10115 
10116 	/* 1024k buffer */
10117 	bp = sata_alloc_local_buffer(spx, 1024);
10118 	if (bp == NULL) {
10119 		sata_pkt_free(spx);
10120 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10121 		sata_log(sata_hba_inst, CE_WARN,
10122 		    "sata_test_atapi_packet_command: "
10123 		    "cannot allocate data buffer");
10124 		return;
10125 	}
10126 	bp_mapin(bp); /* make data buffer accessible */
10127 
10128 	scmd = &spkt->satapkt_cmd;
10129 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10130 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10131 
10132 	/* Use synchronous mode */
10133 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10134 
10135 	/* Synchronous mode, no callback - may be changed by the caller */
10136 	spkt->satapkt_comp = NULL;
10137 	spkt->satapkt_time = sata_default_pkt_time;
10138 
10139 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10140 
10141 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10142 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10143 
10144 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10145 
10146 	/* Set-up acdb. */
10147 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10148 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10149 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10150 	scmd->satacmd_acdb[1] = 0x00;
10151 	scmd->satacmd_acdb[2] = 0x00;
10152 	scmd->satacmd_acdb[3] = 0x00;
10153 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10154 	scmd->satacmd_acdb[5] = 0x00;
10155 
10156 	sata_fixed_sense_data_preset(
10157 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10158 
10159 	/* Transfer command to HBA */
10160 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10161 	if (sata_hba_start(spx, &rval) != 0) {
10162 		/* Pkt not accepted for execution */
10163 		sata_log(sata_hba_inst, CE_WARN,
10164 		    "sata_test_atapi_packet_command: "
10165 		    "Packet not accepted for execution - ret: %02x", rval);
10166 		mutex_exit(
10167 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10168 		goto cleanup;
10169 	}
10170 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10171 
10172 	if (spx->txlt_buf_dma_handle != NULL) {
10173 		/*
10174 		 * Sync buffer. Handle is in usual place in translate struct.
10175 		 */
10176 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10177 		    DDI_DMA_SYNC_FORCPU);
10178 		ASSERT(rval == DDI_SUCCESS);
10179 	}
10180 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10181 		sata_log(sata_hba_inst, CE_WARN,
10182 		    "sata_test_atapi_packet_command: "
10183 		    "Packet completed successfully");
10184 		/*
10185 		 * Normal completion - show inquiry data
10186 		 */
10187 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10188 	} else {
10189 		/*
10190 		 * Something went wrong - analyze return - check rqsense data
10191 		 */
10192 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10193 			/*
10194 			 * ARQ data hopefull show something other than NO SENSE
10195 			 */
10196 			rqsp = scmd->satacmd_rqsense;
10197 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10198 			    "ATAPI packet completion reason: %02x\n"
10199 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10200 			    "          %02x %02x %02x %02x %02x %02x "
10201 			    "          %02x %02x %02x %02x %02x %02x\n",
10202 			    spkt->satapkt_reason,
10203 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10204 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10205 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10206 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10207 			    rqsp[16], rqsp[17]);
10208 		} else {
10209 			switch (spkt->satapkt_reason) {
10210 			case SATA_PKT_PORT_ERROR:
10211 				sata_log(sata_hba_inst, CE_WARN,
10212 				    "sata_test_atapi_packet_command: "
10213 				    "packet reason: port error\n");
10214 				break;
10215 
10216 			case SATA_PKT_TIMEOUT:
10217 				sata_log(sata_hba_inst, CE_WARN,
10218 				    "sata_test_atapi_packet_command: "
10219 				    "packet reason: timeout\n");
10220 				break;
10221 
10222 			case SATA_PKT_ABORTED:
10223 				sata_log(sata_hba_inst, CE_WARN,
10224 				    "sata_test_atapi_packet_command: "
10225 				    "packet reason: aborted\n");
10226 				break;
10227 
10228 			case SATA_PKT_RESET:
10229 				sata_log(sata_hba_inst, CE_WARN,
10230 				    "sata_test_atapi_packet_command: "
10231 				    "packet reason: reset\n");
10232 				break;
10233 			default:
10234 				sata_log(sata_hba_inst, CE_WARN,
10235 				    "sata_test_atapi_packet_command: "
10236 				    "invalid packet reason: %02x\n",
10237 				    spkt->satapkt_reason);
10238 				break;
10239 			}
10240 		}
10241 	}
10242 cleanup:
10243 	sata_free_local_buffer(spx);
10244 	sata_pkt_free(spx);
10245 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10246 }
10247 
10248 #endif /* SATA_DEBUG */
10249 #endif /* 1 */
10250 
10251 
10252 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10253 
10254 /*
10255  * Validate sata_tran info
10256  * SATA_FAILURE returns if structure is inconsistent or structure revision
10257  * does not match one used by the framework.
10258  *
10259  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10260  * required function pointers.
10261  * Returns SATA_FAILURE otherwise.
10262  */
10263 static int
10264 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10265 {
10266 	/*
10267 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10268 	 * of the SATA interface.
10269 	 */
10270 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10271 		sata_log(NULL, CE_WARN,
10272 		    "sata: invalid sata_hba_tran version %d for driver %s",
10273 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10274 		return (SATA_FAILURE);
10275 	}
10276 
10277 	if (dip != sata_tran->sata_tran_hba_dip) {
10278 		SATA_LOG_D((NULL, CE_WARN,
10279 		    "sata: inconsistent sata_tran_hba_dip "
10280 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10281 		return (SATA_FAILURE);
10282 	}
10283 
10284 	if (sata_tran->sata_tran_probe_port == NULL ||
10285 	    sata_tran->sata_tran_start == NULL ||
10286 	    sata_tran->sata_tran_abort == NULL ||
10287 	    sata_tran->sata_tran_reset_dport == NULL ||
10288 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10289 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10290 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10291 	    NULL) {
10292 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10293 		    "required functions"));
10294 	}
10295 	return (SATA_SUCCESS);
10296 }
10297 
10298 /*
10299  * Remove HBA instance from sata_hba_list.
10300  */
10301 static void
10302 sata_remove_hba_instance(dev_info_t *dip)
10303 {
10304 	sata_hba_inst_t	*sata_hba_inst;
10305 
10306 	mutex_enter(&sata_mutex);
10307 	for (sata_hba_inst = sata_hba_list;
10308 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10309 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10310 		if (sata_hba_inst->satahba_dip == dip)
10311 			break;
10312 	}
10313 
10314 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10315 #ifdef SATA_DEBUG
10316 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10317 		    "unknown HBA instance\n");
10318 #endif
10319 		ASSERT(FALSE);
10320 	}
10321 	if (sata_hba_inst == sata_hba_list) {
10322 		sata_hba_list = sata_hba_inst->satahba_next;
10323 		if (sata_hba_list) {
10324 			sata_hba_list->satahba_prev =
10325 			    (struct sata_hba_inst *)NULL;
10326 		}
10327 		if (sata_hba_inst == sata_hba_list_tail) {
10328 			sata_hba_list_tail = NULL;
10329 		}
10330 	} else if (sata_hba_inst == sata_hba_list_tail) {
10331 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10332 		if (sata_hba_list_tail) {
10333 			sata_hba_list_tail->satahba_next =
10334 			    (struct sata_hba_inst *)NULL;
10335 		}
10336 	} else {
10337 		sata_hba_inst->satahba_prev->satahba_next =
10338 		    sata_hba_inst->satahba_next;
10339 		sata_hba_inst->satahba_next->satahba_prev =
10340 		    sata_hba_inst->satahba_prev;
10341 	}
10342 	mutex_exit(&sata_mutex);
10343 }
10344 
10345 /*
10346  * Probe all SATA ports of the specified HBA instance.
10347  * The assumption is that there are no target and attachment point minor nodes
10348  * created by the boot subsystems, so we do not need to prune device tree.
10349  *
10350  * This function is called only from sata_hba_attach(). It does not have to
10351  * be protected by controller mutex, because the hba_attached flag is not set
10352  * yet and no one would be touching this HBA instance other than this thread.
10353  * Determines if port is active and what type of the device is attached
10354  * (if any). Allocates necessary structures for each port.
10355  *
10356  * An AP (Attachement Point) node is created for each SATA device port even
10357  * when there is no device attached.
10358  */
10359 
10360 static 	void
10361 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10362 {
10363 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10364 	int			ncport;
10365 	sata_cport_info_t 	*cportinfo;
10366 	sata_drive_info_t	*drive;
10367 	sata_device_t		sata_device;
10368 	int			rval;
10369 	dev_t			minor_number;
10370 	char			name[16];
10371 	clock_t			start_time, cur_time;
10372 
10373 	/*
10374 	 * Probe controller ports first, to find port status and
10375 	 * any port multiplier attached.
10376 	 */
10377 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10378 		/* allocate cport structure */
10379 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10380 		ASSERT(cportinfo != NULL);
10381 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10382 
10383 		mutex_enter(&cportinfo->cport_mutex);
10384 
10385 		cportinfo->cport_addr.cport = ncport;
10386 		cportinfo->cport_addr.pmport = 0;
10387 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10388 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10389 		cportinfo->cport_state |= SATA_STATE_PROBING;
10390 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10391 
10392 		/*
10393 		 * Regardless if a port is usable or not, create
10394 		 * an attachment point
10395 		 */
10396 		mutex_exit(&cportinfo->cport_mutex);
10397 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10398 		    ncport, 0, SATA_ADDR_CPORT);
10399 		(void) sprintf(name, "%d", ncport);
10400 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10401 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10402 		    DDI_SUCCESS) {
10403 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10404 			    "cannot create SATA attachment point for port %d",
10405 			    ncport);
10406 		}
10407 
10408 		/* Probe port */
10409 		start_time = ddi_get_lbolt();
10410 	reprobe_cport:
10411 		sata_device.satadev_addr.cport = ncport;
10412 		sata_device.satadev_addr.pmport = 0;
10413 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10414 		sata_device.satadev_rev = SATA_DEVICE_REV;
10415 
10416 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10417 		    (dip, &sata_device);
10418 
10419 		mutex_enter(&cportinfo->cport_mutex);
10420 		cportinfo->cport_scr = sata_device.satadev_scr;
10421 		if (rval != SATA_SUCCESS) {
10422 			/* Something went wrong? Fail the port */
10423 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10424 			mutex_exit(&cportinfo->cport_mutex);
10425 			continue;
10426 		}
10427 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10428 		cportinfo->cport_state |= SATA_STATE_PROBED;
10429 		cportinfo->cport_dev_type = sata_device.satadev_type;
10430 
10431 		cportinfo->cport_state |= SATA_STATE_READY;
10432 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10433 			mutex_exit(&cportinfo->cport_mutex);
10434 			continue;
10435 		}
10436 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10437 			/*
10438 			 * There is some device attached.
10439 			 * Allocate device info structure
10440 			 */
10441 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10442 				mutex_exit(&cportinfo->cport_mutex);
10443 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10444 				    kmem_zalloc(sizeof (sata_drive_info_t),
10445 				    KM_SLEEP);
10446 				mutex_enter(&cportinfo->cport_mutex);
10447 			}
10448 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10449 			drive->satadrv_addr = cportinfo->cport_addr;
10450 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10451 			drive->satadrv_type = cportinfo->cport_dev_type;
10452 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10453 
10454 			mutex_exit(&cportinfo->cport_mutex);
10455 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10456 			    SATA_SUCCESS) {
10457 				/*
10458 				 * Plugged device was not correctly identified.
10459 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10460 				 */
10461 				cur_time = ddi_get_lbolt();
10462 				if ((cur_time - start_time) <
10463 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10464 					/* sleep for a while */
10465 					delay(drv_usectohz(
10466 					    SATA_DEV_RETRY_DLY));
10467 					goto reprobe_cport;
10468 				}
10469 			}
10470 		} else { /* SATA_DTYPE_PMULT */
10471 			mutex_exit(&cportinfo->cport_mutex);
10472 
10473 			/* Allocate sata_pmult_info and sata_pmport_info */
10474 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10475 			    SATA_SUCCESS)
10476 				continue;
10477 
10478 			/* Log the information of the port multiplier */
10479 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10480 
10481 			/* Probe its pmports */
10482 			sata_probe_pmports(sata_hba_inst, ncport);
10483 		}
10484 	}
10485 }
10486 
10487 /*
10488  * Probe all device ports behind a port multiplier.
10489  *
10490  * PMult-related structure should be allocated before by sata_alloc_pmult().
10491  *
10492  * NOTE1: Only called from sata_probe_ports()
10493  * NOTE2: No mutex should be hold.
10494  */
10495 static void
10496 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10497 {
10498 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10499 	sata_pmult_info_t	*pmultinfo = NULL;
10500 	sata_pmport_info_t 	*pmportinfo = NULL;
10501 	sata_drive_info_t	*drive = NULL;
10502 	sata_device_t		sata_device;
10503 
10504 	clock_t			start_time, cur_time;
10505 	int			npmport;
10506 	int			rval;
10507 
10508 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10509 
10510 	/* Probe Port Multiplier ports */
10511 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10512 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10513 		start_time = ddi_get_lbolt();
10514 reprobe_pmport:
10515 		sata_device.satadev_addr.cport = ncport;
10516 		sata_device.satadev_addr.pmport = npmport;
10517 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10518 		sata_device.satadev_rev = SATA_DEVICE_REV;
10519 
10520 		/* Let HBA driver probe it. */
10521 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10522 		    (dip, &sata_device);
10523 		mutex_enter(&pmportinfo->pmport_mutex);
10524 
10525 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10526 
10527 		if (rval != SATA_SUCCESS) {
10528 			pmportinfo->pmport_state =
10529 			    SATA_PSTATE_FAILED;
10530 			mutex_exit(&pmportinfo->pmport_mutex);
10531 			continue;
10532 		}
10533 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10534 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10535 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10536 
10537 		pmportinfo->pmport_state |= SATA_STATE_READY;
10538 		if (pmportinfo->pmport_dev_type ==
10539 		    SATA_DTYPE_NONE) {
10540 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10541 			    "no device found at port %d:%d", ncport, npmport);
10542 			mutex_exit(&pmportinfo->pmport_mutex);
10543 			continue;
10544 		}
10545 		/* Port multipliers cannot be chained */
10546 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10547 		/*
10548 		 * There is something attached to Port
10549 		 * Multiplier device port
10550 		 * Allocate device info structure
10551 		 */
10552 		if (pmportinfo->pmport_sata_drive == NULL) {
10553 			mutex_exit(&pmportinfo->pmport_mutex);
10554 			pmportinfo->pmport_sata_drive =
10555 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10556 			mutex_enter(&pmportinfo->pmport_mutex);
10557 		}
10558 		drive = pmportinfo->pmport_sata_drive;
10559 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10560 		drive->satadrv_addr.pmport = npmport;
10561 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10562 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10563 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10564 
10565 		mutex_exit(&pmportinfo->pmport_mutex);
10566 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10567 
10568 		if (rval != SATA_SUCCESS) {
10569 			/*
10570 			 * Plugged device was not correctly identified.
10571 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10572 			 */
10573 			cur_time = ddi_get_lbolt();
10574 			if ((cur_time - start_time) < drv_usectohz(
10575 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10576 				/* sleep for a while */
10577 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10578 				goto reprobe_pmport;
10579 			}
10580 		}
10581 	}
10582 }
10583 
10584 /*
10585  * Add SATA device for specified HBA instance & port (SCSI target
10586  * device nodes).
10587  * This function is called (indirectly) only from sata_hba_attach().
10588  * A target node is created when there is a supported type device attached,
10589  * but may be removed if it cannot be put online.
10590  *
10591  * This function cannot be called from an interrupt context.
10592  *
10593  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10594  *
10595  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10596  * device identification failed - adding a device could be retried.
10597  *
10598  */
10599 static 	int
10600 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10601     sata_device_t *sata_device)
10602 {
10603 	sata_cport_info_t 	*cportinfo;
10604 	sata_pmult_info_t	*pminfo;
10605 	sata_pmport_info_t	*pmportinfo;
10606 	dev_info_t		*cdip;		/* child dip */
10607 	sata_address_t		*saddr = &sata_device->satadev_addr;
10608 	uint8_t			cport, pmport;
10609 	int			rval;
10610 
10611 	cport = saddr->cport;
10612 	pmport = saddr->pmport;
10613 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10614 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10615 
10616 	/*
10617 	 * Some device is attached to a controller port.
10618 	 * We rely on controllers distinquishing between no-device,
10619 	 * attached port multiplier and other kind of attached device.
10620 	 * We need to get Identify Device data and determine
10621 	 * positively the dev type before trying to attach
10622 	 * the target driver.
10623 	 */
10624 	sata_device->satadev_rev = SATA_DEVICE_REV;
10625 	switch (saddr->qual) {
10626 	case SATA_ADDR_CPORT:
10627 		/*
10628 		 * Add a non-port-multiplier device at controller port.
10629 		 */
10630 		saddr->qual = SATA_ADDR_DCPORT;
10631 
10632 		rval = sata_probe_device(sata_hba_inst, sata_device);
10633 		if (rval != SATA_SUCCESS ||
10634 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10635 			return (SATA_FAILURE);
10636 
10637 		mutex_enter(&cportinfo->cport_mutex);
10638 		sata_show_drive_info(sata_hba_inst,
10639 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10640 
10641 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10642 			/*
10643 			 * Could not determine device type or
10644 			 * a device is not supported.
10645 			 * Degrade this device to unknown.
10646 			 */
10647 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10648 			mutex_exit(&cportinfo->cport_mutex);
10649 			return (SATA_SUCCESS);
10650 		}
10651 		cportinfo->cport_dev_type = sata_device->satadev_type;
10652 		cportinfo->cport_tgtnode_clean = B_TRUE;
10653 		mutex_exit(&cportinfo->cport_mutex);
10654 
10655 		/*
10656 		 * Initialize device to the desired state. Even if it
10657 		 * fails, the device will still attach but syslog
10658 		 * will show the warning.
10659 		 */
10660 		if (sata_initialize_device(sata_hba_inst,
10661 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10662 			/* Retry */
10663 			rval = sata_initialize_device(sata_hba_inst,
10664 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10665 
10666 			if (rval == SATA_RETRY)
10667 				sata_log(sata_hba_inst, CE_WARN,
10668 				    "SATA device at port %d - "
10669 				    "default device features could not be set."
10670 				    " Device may not operate as expected.",
10671 				    cport);
10672 		}
10673 
10674 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10675 		if (cdip == NULL) {
10676 			/*
10677 			 * Attaching target node failed.
10678 			 * We retain sata_drive_info structure...
10679 			 */
10680 			return (SATA_SUCCESS);
10681 		}
10682 
10683 		mutex_enter(&cportinfo->cport_mutex);
10684 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10685 		    satadrv_state = SATA_STATE_READY;
10686 		mutex_exit(&cportinfo->cport_mutex);
10687 
10688 		break;
10689 
10690 	case SATA_ADDR_PMPORT:
10691 		saddr->qual = SATA_ADDR_DPMPORT;
10692 
10693 		mutex_enter(&cportinfo->cport_mutex);
10694 		/* It must be a Port Multiplier at the controller port */
10695 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10696 
10697 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10698 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10699 		mutex_exit(&cportinfo->cport_mutex);
10700 
10701 		rval = sata_probe_device(sata_hba_inst, sata_device);
10702 		if (rval != SATA_SUCCESS ||
10703 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10704 			return (SATA_FAILURE);
10705 		}
10706 
10707 		mutex_enter(&pmportinfo->pmport_mutex);
10708 		sata_show_drive_info(sata_hba_inst,
10709 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10710 
10711 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10712 			/*
10713 			 * Could not determine device type.
10714 			 * Degrade this device to unknown.
10715 			 */
10716 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10717 			mutex_exit(&pmportinfo->pmport_mutex);
10718 			return (SATA_SUCCESS);
10719 		}
10720 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10721 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10722 		mutex_exit(&pmportinfo->pmport_mutex);
10723 
10724 		/*
10725 		 * Initialize device to the desired state.
10726 		 * Even if it fails, the device will still
10727 		 * attach but syslog will show the warning.
10728 		 */
10729 		if (sata_initialize_device(sata_hba_inst,
10730 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10731 			/* Retry */
10732 			rval = sata_initialize_device(sata_hba_inst,
10733 			    pmportinfo->pmport_sata_drive);
10734 
10735 			if (rval == SATA_RETRY)
10736 				sata_log(sata_hba_inst, CE_WARN,
10737 				    "SATA device at port %d:%d - "
10738 				    "default device features could not be set."
10739 				    " Device may not operate as expected.",
10740 				    cport, pmport);
10741 		}
10742 
10743 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10744 		if (cdip == NULL) {
10745 			/*
10746 			 * Attaching target node failed.
10747 			 * We retain sata_drive_info structure...
10748 			 */
10749 			return (SATA_SUCCESS);
10750 		}
10751 		mutex_enter(&pmportinfo->pmport_mutex);
10752 		pmportinfo->pmport_sata_drive->satadrv_state |=
10753 		    SATA_STATE_READY;
10754 		mutex_exit(&pmportinfo->pmport_mutex);
10755 
10756 		break;
10757 
10758 	default:
10759 		return (SATA_FAILURE);
10760 	}
10761 
10762 	return (SATA_SUCCESS);
10763 }
10764 
10765 /*
10766  * Clean up target node at specific address.
10767  *
10768  * NOTE: No Mutex should be hold.
10769  */
10770 static int
10771 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10772     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10773 {
10774 	uint8_t cport, pmport, qual;
10775 	dev_info_t *tdip;
10776 
10777 	cport = sata_device->satadev_addr.cport;
10778 	pmport = sata_device->satadev_addr.pmport;
10779 	qual = sata_device->satadev_addr.qual;
10780 
10781 	if (qual == SATA_ADDR_DCPORT) {
10782 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10783 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10784 	} else {
10785 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10786 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10787 		    cport, pmport));
10788 	}
10789 
10790 	/* We are addressing attached device, not a port */
10791 	sata_device->satadev_addr.qual =
10792 	    sdinfo->satadrv_addr.qual;
10793 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10794 	    &sata_device->satadev_addr);
10795 	if (tdip != NULL && ndi_devi_offline(tdip,
10796 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10797 		/*
10798 		 * Problem :
10799 		 * The target node remained attached.
10800 		 * This happens when the device file was open
10801 		 * or a node was waiting for resources.
10802 		 * Cannot do anything about it.
10803 		 */
10804 		if (qual == SATA_ADDR_DCPORT) {
10805 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10806 			    "sata_hba_ioctl: disconnect: could "
10807 			    "not unconfigure device before "
10808 			    "disconnecting the SATA port %d",
10809 			    cport));
10810 		} else {
10811 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10812 			    "sata_hba_ioctl: disconnect: could "
10813 			    "not unconfigure device before "
10814 			    "disconnecting the SATA port %d:%d",
10815 			    cport, pmport));
10816 		}
10817 		/*
10818 		 * Set DEVICE REMOVED state in the target
10819 		 * node. It will prevent access to the device
10820 		 * even when a new device is attached, until
10821 		 * the old target node is released, removed and
10822 		 * recreated for a new  device.
10823 		 */
10824 		sata_set_device_removed(tdip);
10825 
10826 		/*
10827 		 * Instruct event daemon to try the target
10828 		 * node cleanup later.
10829 		 */
10830 		sata_set_target_node_cleanup(
10831 		    sata_hba_inst, &sata_device->satadev_addr);
10832 	}
10833 
10834 
10835 	return (SATA_SUCCESS);
10836 }
10837 
10838 
10839 /*
10840  * Create scsi target node for attached device, create node properties and
10841  * attach the node.
10842  * The node could be removed if the device onlining fails.
10843  *
10844  * A dev_info_t pointer is returned if operation is successful, NULL is
10845  * returned otherwise.
10846  */
10847 
10848 static dev_info_t *
10849 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10850 			sata_address_t *sata_addr)
10851 {
10852 	dev_info_t *cdip = NULL;
10853 	int rval;
10854 	char *nname = NULL;
10855 	char **compatible = NULL;
10856 	int ncompatible;
10857 	struct scsi_inquiry inq;
10858 	sata_device_t sata_device;
10859 	sata_drive_info_t *sdinfo;
10860 	int target;
10861 	int i;
10862 
10863 	sata_device.satadev_rev = SATA_DEVICE_REV;
10864 	sata_device.satadev_addr = *sata_addr;
10865 
10866 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10867 
10868 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10869 
10870 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10871 	    sata_addr->pmport, sata_addr->qual);
10872 
10873 	if (sdinfo == NULL) {
10874 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10875 		    sata_addr->cport)));
10876 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10877 		    "sata_create_target_node: no sdinfo for target %x",
10878 		    target));
10879 		return (NULL);
10880 	}
10881 
10882 	/*
10883 	 * create or get scsi inquiry data, expected by
10884 	 * scsi_hba_nodename_compatible_get()
10885 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10886 	 * ATAPI devices respond directly to Inquiry request.
10887 	 */
10888 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10889 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10890 		    (uint8_t *)&inq);
10891 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10892 		    sata_addr->cport)));
10893 	} else { /* Assume supported ATAPI device */
10894 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10895 		    sata_addr->cport)));
10896 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10897 		    &inq) == SATA_FAILURE)
10898 			return (NULL);
10899 		/*
10900 		 * Save supported ATAPI transport version
10901 		 */
10902 		sdinfo->satadrv_atapi_trans_ver =
10903 		    SATA_ATAPI_TRANS_VERSION(&inq);
10904 	}
10905 
10906 	/* determine the node name and compatible */
10907 	scsi_hba_nodename_compatible_get(&inq, NULL,
10908 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10909 
10910 #ifdef SATA_DEBUG
10911 	if (sata_debug_flags & SATA_DBG_NODES) {
10912 		if (nname == NULL) {
10913 			cmn_err(CE_NOTE, "sata_create_target_node: "
10914 			    "cannot determine nodename for target %d\n",
10915 			    target);
10916 		} else {
10917 			cmn_err(CE_WARN, "sata_create_target_node: "
10918 			    "target %d nodename: %s\n", target, nname);
10919 		}
10920 		if (compatible == NULL) {
10921 			cmn_err(CE_WARN,
10922 			    "sata_create_target_node: no compatible name\n");
10923 		} else {
10924 			for (i = 0; i < ncompatible; i++) {
10925 				cmn_err(CE_WARN, "sata_create_target_node: "
10926 				    "compatible name: %s\n", compatible[i]);
10927 			}
10928 		}
10929 	}
10930 #endif
10931 
10932 	/* if nodename can't be determined, log error and exit */
10933 	if (nname == NULL) {
10934 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10935 		    "sata_create_target_node: cannot determine nodename "
10936 		    "for target %d\n", target));
10937 		scsi_hba_nodename_compatible_free(nname, compatible);
10938 		return (NULL);
10939 	}
10940 	/*
10941 	 * Create scsi target node
10942 	 */
10943 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10944 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10945 	    "device-type", "scsi");
10946 
10947 	if (rval != DDI_PROP_SUCCESS) {
10948 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10949 		    "updating device_type prop failed %d", rval));
10950 		goto fail;
10951 	}
10952 
10953 	/*
10954 	 * Create target node properties: target & lun
10955 	 */
10956 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10957 	if (rval != DDI_PROP_SUCCESS) {
10958 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10959 		    "updating target prop failed %d", rval));
10960 		goto fail;
10961 	}
10962 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
10963 	if (rval != DDI_PROP_SUCCESS) {
10964 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10965 		    "updating target prop failed %d", rval));
10966 		goto fail;
10967 	}
10968 
10969 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10970 		/*
10971 		 * Add "variant" property
10972 		 */
10973 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10974 		    "variant", "atapi");
10975 		if (rval != DDI_PROP_SUCCESS) {
10976 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10977 			    "sata_create_target_node: variant atapi "
10978 			    "property could not be created: %d", rval));
10979 			goto fail;
10980 		}
10981 	}
10982 	/* decorate the node with compatible */
10983 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
10984 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
10985 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10986 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
10987 		    (void *)cdip));
10988 		goto fail;
10989 	}
10990 
10991 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10992 		/*
10993 		 * Add "sata-phy" property
10994 		 */
10995 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
10996 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
10997 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10998 			    "sata_create_target_node: failed to create "
10999 			    "\"sata-phy\" property: port %d",
11000 			    sata_addr->cport));
11001 		}
11002 	}
11003 
11004 
11005 	/*
11006 	 * Now, try to attach the driver. If probing of the device fails,
11007 	 * the target node may be removed
11008 	 */
11009 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11010 
11011 	scsi_hba_nodename_compatible_free(nname, compatible);
11012 
11013 	if (rval == NDI_SUCCESS)
11014 		return (cdip);
11015 
11016 	/* target node was removed - are we sure? */
11017 	return (NULL);
11018 
11019 fail:
11020 	scsi_hba_nodename_compatible_free(nname, compatible);
11021 	ddi_prop_remove_all(cdip);
11022 	rval = ndi_devi_free(cdip);
11023 	if (rval != NDI_SUCCESS) {
11024 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11025 		    "node removal failed %d", rval));
11026 	}
11027 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11028 	    "cannot create target node for SATA device at port %d",
11029 	    sata_addr->cport);
11030 	return (NULL);
11031 }
11032 
11033 /*
11034  * Remove a target node.
11035  */
11036 static void
11037 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11038 			sata_address_t *sata_addr)
11039 {
11040 	dev_info_t *tdip;
11041 	uint8_t cport = sata_addr->cport;
11042 	uint8_t pmport = sata_addr->pmport;
11043 	uint8_t qual = sata_addr->qual;
11044 
11045 	/* Note the sata daemon uses the address of the port/pmport */
11046 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11047 
11048 	/* Remove target node */
11049 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11050 	if (tdip != NULL) {
11051 		/*
11052 		 * Target node exists.  Unconfigure device
11053 		 * then remove the target node (one ndi
11054 		 * operation).
11055 		 */
11056 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11057 			/*
11058 			 * PROBLEM - no device, but target node remained. This
11059 			 * happens when the file was open or node was waiting
11060 			 * for resources.
11061 			 */
11062 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11063 			    "sata_remove_target_node: "
11064 			    "Failed to remove target node for "
11065 			    "detached SATA device."));
11066 			/*
11067 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11068 			 * re-check first that the node still exists.
11069 			 */
11070 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11071 			    cport, pmport);
11072 			if (tdip != NULL) {
11073 				sata_set_device_removed(tdip);
11074 				/*
11075 				 * Instruct event daemon to retry the cleanup
11076 				 * later.
11077 				 */
11078 				sata_set_target_node_cleanup(sata_hba_inst,
11079 				    sata_addr);
11080 			}
11081 		}
11082 
11083 		if (qual == SATA_ADDR_CPORT)
11084 			sata_log(sata_hba_inst, CE_WARN,
11085 			    "SATA device detached at port %d", cport);
11086 		else
11087 			sata_log(sata_hba_inst, CE_WARN,
11088 			    "SATA device detached at port %d:%d",
11089 			    cport, pmport);
11090 	}
11091 #ifdef SATA_DEBUG
11092 	else {
11093 		if (qual == SATA_ADDR_CPORT)
11094 			sata_log(sata_hba_inst, CE_WARN,
11095 			    "target node not found at port %d", cport);
11096 		else
11097 			sata_log(sata_hba_inst, CE_WARN,
11098 			    "target node not found at port %d:%d",
11099 			    cport, pmport);
11100 	}
11101 #endif
11102 }
11103 
11104 
11105 /*
11106  * Re-probe sata port, check for a device and attach info
11107  * structures when necessary. Identify Device data is fetched, if possible.
11108  * Assumption: sata address is already validated.
11109  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11110  * the presence of a device and its type.
11111  *
11112  * flag arg specifies that the function should try multiple times to identify
11113  * device type and to initialize it, or it should return immediately on failure.
11114  * SATA_DEV_IDENTIFY_RETRY - retry
11115  * SATA_DEV_IDENTIFY_NORETRY - no retry
11116  *
11117  * SATA_FAILURE is returned if one of the operations failed.
11118  *
11119  * This function cannot be called in interrupt context - it may sleep.
11120  *
11121  * Note: Port multiplier is supported.
11122  */
11123 static int
11124 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11125     int flag)
11126 {
11127 	sata_cport_info_t *cportinfo;
11128 	sata_pmult_info_t *pmultinfo;
11129 	sata_drive_info_t *sdinfo, *osdinfo;
11130 	boolean_t init_device = B_FALSE;
11131 	int prev_device_type = SATA_DTYPE_NONE;
11132 	int prev_device_settings = 0;
11133 	int prev_device_state = 0;
11134 	clock_t start_time;
11135 	int retry = B_FALSE;
11136 	uint8_t cport = sata_device->satadev_addr.cport;
11137 	int rval_probe, rval_init;
11138 
11139 	/*
11140 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11141 	 */
11142 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11143 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11144 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11145 
11146 	/* We only care about host sata cport for now */
11147 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11148 	    sata_device->satadev_addr.cport);
11149 
11150 	/*
11151 	 * If a port multiplier was previously attached (we have no idea it
11152 	 * still there or not), sata_reprobe_pmult() will handle it.
11153 	 */
11154 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11155 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11156 
11157 	/* Store sata_drive_info when a non-pmult device was attached. */
11158 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11159 	if (osdinfo != NULL) {
11160 		/*
11161 		 * We are re-probing port with a previously attached device.
11162 		 * Save previous device type and settings.
11163 		 */
11164 		prev_device_type = cportinfo->cport_dev_type;
11165 		prev_device_settings = osdinfo->satadrv_settings;
11166 		prev_device_state = osdinfo->satadrv_state;
11167 	}
11168 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11169 		start_time = ddi_get_lbolt();
11170 		retry = B_TRUE;
11171 	}
11172 retry_probe:
11173 
11174 	/* probe port */
11175 	mutex_enter(&cportinfo->cport_mutex);
11176 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11177 	cportinfo->cport_state |= SATA_STATE_PROBING;
11178 	mutex_exit(&cportinfo->cport_mutex);
11179 
11180 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11181 	    (SATA_DIP(sata_hba_inst), sata_device);
11182 
11183 	mutex_enter(&cportinfo->cport_mutex);
11184 	if (rval_probe != SATA_SUCCESS) {
11185 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11186 		mutex_exit(&cportinfo->cport_mutex);
11187 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11188 		    "SATA port %d probing failed",
11189 		    cportinfo->cport_addr.cport));
11190 		return (SATA_FAILURE);
11191 	}
11192 
11193 	/*
11194 	 * update sata port state and set device type
11195 	 */
11196 	sata_update_port_info(sata_hba_inst, sata_device);
11197 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11198 
11199 	/*
11200 	 * Sanity check - Port is active? Is the link active?
11201 	 * Is there any device attached?
11202 	 */
11203 	if ((cportinfo->cport_state &
11204 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11205 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11206 	    SATA_PORT_DEVLINK_UP) {
11207 		/*
11208 		 * Port in non-usable state or no link active/no device.
11209 		 * Free info structure if necessary (direct attached drive
11210 		 * only, for now!
11211 		 */
11212 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11213 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11214 		/* Add here differentiation for device attached or not */
11215 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11216 		mutex_exit(&cportinfo->cport_mutex);
11217 		if (sdinfo != NULL)
11218 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11219 		return (SATA_SUCCESS);
11220 	}
11221 
11222 	cportinfo->cport_state |= SATA_STATE_READY;
11223 	cportinfo->cport_state |= SATA_STATE_PROBED;
11224 
11225 	cportinfo->cport_dev_type = sata_device->satadev_type;
11226 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11227 
11228 	/*
11229 	 * If we are re-probing the port, there may be
11230 	 * sata_drive_info structure attached
11231 	 */
11232 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11233 
11234 		/*
11235 		 * There is no device, so remove device info structure,
11236 		 * if necessary.
11237 		 */
11238 		/* Device change: Drive -> None */
11239 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11240 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11241 		if (sdinfo != NULL) {
11242 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11243 			sata_log(sata_hba_inst, CE_WARN,
11244 			    "SATA device detached "
11245 			    "from port %d", cportinfo->cport_addr.cport);
11246 		}
11247 		mutex_exit(&cportinfo->cport_mutex);
11248 		return (SATA_SUCCESS);
11249 
11250 	}
11251 
11252 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11253 
11254 		/* Device (may) change: Drive -> Drive */
11255 		if (sdinfo == NULL) {
11256 			/*
11257 			 * There is some device attached, but there is
11258 			 * no sata_drive_info structure - allocate one
11259 			 */
11260 			mutex_exit(&cportinfo->cport_mutex);
11261 			sdinfo = kmem_zalloc(
11262 			    sizeof (sata_drive_info_t), KM_SLEEP);
11263 			mutex_enter(&cportinfo->cport_mutex);
11264 			/*
11265 			 * Recheck, that the port state did not change when we
11266 			 * released mutex.
11267 			 */
11268 			if (cportinfo->cport_state & SATA_STATE_READY) {
11269 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11270 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11271 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11272 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11273 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11274 			} else {
11275 				/*
11276 				 * Port is not in ready state, we
11277 				 * cannot attach a device.
11278 				 */
11279 				mutex_exit(&cportinfo->cport_mutex);
11280 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11281 				return (SATA_SUCCESS);
11282 			}
11283 			/*
11284 			 * Since we are adding device, presumably new one,
11285 			 * indicate that it  should be initalized,
11286 			 * as well as some internal framework states).
11287 			 */
11288 			init_device = B_TRUE;
11289 		}
11290 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11291 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11292 	} else {
11293 		/* Device change: Drive -> PMult */
11294 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11295 		if (sdinfo != NULL) {
11296 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11297 			sata_log(sata_hba_inst, CE_WARN,
11298 			    "SATA device detached "
11299 			    "from port %d", cportinfo->cport_addr.cport);
11300 		}
11301 
11302 		sata_log(sata_hba_inst, CE_WARN,
11303 		    "SATA port multiplier detected at port %d",
11304 		    cportinfo->cport_addr.cport);
11305 
11306 		mutex_exit(&cportinfo->cport_mutex);
11307 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11308 		    SATA_SUCCESS)
11309 			return (SATA_FAILURE);
11310 		sata_show_pmult_info(sata_hba_inst, sata_device);
11311 		mutex_enter(&cportinfo->cport_mutex);
11312 
11313 		/*
11314 		 * Mark all the port multiplier port behind the port
11315 		 * multiplier behind with link events, so that the sata daemon
11316 		 * will update their status.
11317 		 */
11318 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11319 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11320 		mutex_exit(&cportinfo->cport_mutex);
11321 		return (SATA_SUCCESS);
11322 	}
11323 	mutex_exit(&cportinfo->cport_mutex);
11324 
11325 	/*
11326 	 * Figure out what kind of device we are really
11327 	 * dealing with. Failure of identifying device does not fail this
11328 	 * function.
11329 	 */
11330 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11331 	rval_init = SATA_FAILURE;
11332 	mutex_enter(&cportinfo->cport_mutex);
11333 	if (rval_probe == SATA_SUCCESS) {
11334 		/*
11335 		 * If we are dealing with the same type of a device as before,
11336 		 * restore its settings flags.
11337 		 */
11338 		if (osdinfo != NULL &&
11339 		    sata_device->satadev_type == prev_device_type)
11340 			sdinfo->satadrv_settings = prev_device_settings;
11341 
11342 		mutex_exit(&cportinfo->cport_mutex);
11343 		rval_init = SATA_SUCCESS;
11344 		/* Set initial device features, if necessary */
11345 		if (init_device == B_TRUE) {
11346 			rval_init = sata_initialize_device(sata_hba_inst,
11347 			    sdinfo);
11348 		}
11349 		if (rval_init == SATA_SUCCESS)
11350 			return (rval_init);
11351 		/* else we will retry if retry was asked for */
11352 
11353 	} else {
11354 		/*
11355 		 * If there was some device info before we probe the device,
11356 		 * restore previous device setting, so we can retry from scratch
11357 		 * later. Providing, of course, that device has not disapear
11358 		 * during probing process.
11359 		 */
11360 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11361 			if (osdinfo != NULL) {
11362 				cportinfo->cport_dev_type = prev_device_type;
11363 				sdinfo->satadrv_type = prev_device_type;
11364 				sdinfo->satadrv_state = prev_device_state;
11365 			}
11366 		} else {
11367 			/* device is gone */
11368 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11369 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11370 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11371 			mutex_exit(&cportinfo->cport_mutex);
11372 			return (SATA_SUCCESS);
11373 		}
11374 		mutex_exit(&cportinfo->cport_mutex);
11375 	}
11376 
11377 	if (retry) {
11378 		clock_t cur_time = ddi_get_lbolt();
11379 		/*
11380 		 * A device was not successfully identified or initialized.
11381 		 * Track retry time for device identification.
11382 		 */
11383 		if ((cur_time - start_time) <
11384 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11385 			/* sleep for a while */
11386 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11387 			goto retry_probe;
11388 		}
11389 		/* else no more retries */
11390 		mutex_enter(&cportinfo->cport_mutex);
11391 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11392 			if (rval_init == SATA_RETRY) {
11393 				/*
11394 				 * Setting drive features have failed, but
11395 				 * because the drive is still accessible,
11396 				 * keep it and emit a warning message.
11397 				 */
11398 				sata_log(sata_hba_inst, CE_WARN,
11399 				    "SATA device at port %d - desired "
11400 				    "drive features could not be set. "
11401 				    "Device may not operate as expected.",
11402 				    cportinfo->cport_addr.cport);
11403 			} else {
11404 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11405 				    satadrv_state = SATA_DSTATE_FAILED;
11406 			}
11407 		}
11408 		mutex_exit(&cportinfo->cport_mutex);
11409 	}
11410 	return (SATA_SUCCESS);
11411 }
11412 
11413 /*
11414  * Reprobe a controller port that connected to a port multiplier.
11415  *
11416  * NOTE: No Mutex should be hold.
11417  */
11418 static int
11419 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11420     int flag)
11421 {
11422 	_NOTE(ARGUNUSED(flag))
11423 	sata_cport_info_t *cportinfo;
11424 	sata_pmult_info_t *pmultinfo;
11425 	uint8_t cport = sata_device->satadev_addr.cport;
11426 	int rval_probe;
11427 
11428 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11429 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11430 
11431 	/* probe port */
11432 	mutex_enter(&cportinfo->cport_mutex);
11433 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11434 	cportinfo->cport_state |= SATA_STATE_PROBING;
11435 	mutex_exit(&cportinfo->cport_mutex);
11436 
11437 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11438 	    (SATA_DIP(sata_hba_inst), sata_device);
11439 
11440 	mutex_enter(&cportinfo->cport_mutex);
11441 	if (rval_probe != SATA_SUCCESS) {
11442 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11443 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11444 		    "SATA port %d probing failed", cport));
11445 		sata_log(sata_hba_inst, CE_WARN,
11446 		    "SATA port multiplier detached at port %d", cport);
11447 		mutex_exit(&cportinfo->cport_mutex);
11448 		sata_free_pmult(sata_hba_inst, sata_device);
11449 		return (SATA_FAILURE);
11450 	}
11451 
11452 	/*
11453 	 * update sata port state and set device type
11454 	 */
11455 	sata_update_port_info(sata_hba_inst, sata_device);
11456 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11457 	cportinfo->cport_state |= SATA_STATE_PROBED;
11458 
11459 	/*
11460 	 * Sanity check - Port is active? Is the link active?
11461 	 * Is there any device attached?
11462 	 */
11463 	if ((cportinfo->cport_state &
11464 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11465 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11466 	    SATA_PORT_DEVLINK_UP ||
11467 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11468 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11469 		mutex_exit(&cportinfo->cport_mutex);
11470 		sata_free_pmult(sata_hba_inst, sata_device);
11471 		sata_log(sata_hba_inst, CE_WARN,
11472 		    "SATA port multiplier detached at port %d", cport);
11473 		return (SATA_SUCCESS);
11474 	}
11475 
11476 	/*
11477 	 * Device changed: PMult -> Non-PMult
11478 	 *
11479 	 * This situation is uncommon, most possibly being caused by errors
11480 	 * after which the port multiplier is not correct initialized and
11481 	 * recognized. In that case the new device will be marked as unknown
11482 	 * and will not be automatically probed in this routine. Instead
11483 	 * system administrator could manually restart it via cfgadm(1M).
11484 	 */
11485 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11486 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11487 		mutex_exit(&cportinfo->cport_mutex);
11488 		sata_free_pmult(sata_hba_inst, sata_device);
11489 		sata_log(sata_hba_inst, CE_WARN,
11490 		    "SATA port multiplier detached at port %d", cport);
11491 		return (SATA_FAILURE);
11492 	}
11493 
11494 	/*
11495 	 * Now we know it is a port multiplier. However, if this is not the
11496 	 * previously attached port multiplier - they may have different
11497 	 * pmport numbers - we need to re-allocate data structures for every
11498 	 * pmport and drive.
11499 	 *
11500 	 * Port multipliers of the same model have identical values in these
11501 	 * registers, so it is still necessary to update the information of
11502 	 * all drives attached to the previous port multiplier afterwards.
11503 	 */
11504 	/* Device changed: PMult -> another PMult */
11505 	mutex_exit(&cportinfo->cport_mutex);
11506 	sata_free_pmult(sata_hba_inst, sata_device);
11507 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11508 		return (SATA_FAILURE);
11509 	mutex_enter(&cportinfo->cport_mutex);
11510 
11511 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11512 	    "SATA port multiplier [changed] at port %d", cport);
11513 	sata_log(sata_hba_inst, CE_WARN,
11514 	    "SATA port multiplier detected at port %d", cport);
11515 
11516 	/*
11517 	 * Mark all the port multiplier port behind the port
11518 	 * multiplier behind with link events, so that the sata daemon
11519 	 * will update their status.
11520 	 */
11521 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11522 	mutex_exit(&cportinfo->cport_mutex);
11523 
11524 	return (SATA_SUCCESS);
11525 }
11526 
11527 /*
11528  * Re-probe a port multiplier port, check for a device and attach info
11529  * structures when necessary. Identify Device data is fetched, if possible.
11530  * Assumption: sata address is already validated as port multiplier port.
11531  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11532  * the presence of a device and its type.
11533  *
11534  * flag arg specifies that the function should try multiple times to identify
11535  * device type and to initialize it, or it should return immediately on failure.
11536  * SATA_DEV_IDENTIFY_RETRY - retry
11537  * SATA_DEV_IDENTIFY_NORETRY - no retry
11538  *
11539  * SATA_FAILURE is returned if one of the operations failed.
11540  *
11541  * This function cannot be called in interrupt context - it may sleep.
11542  *
11543  * NOTE: Should be only called by sata_probe_port() in case target port is a
11544  *       port multiplier port.
11545  * NOTE: No Mutex should be hold.
11546  */
11547 static int
11548 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11549     int flag)
11550 {
11551 	sata_cport_info_t *cportinfo = NULL;
11552 	sata_pmport_info_t *pmportinfo = NULL;
11553 	sata_drive_info_t *sdinfo, *osdinfo;
11554 	sata_device_t sdevice;
11555 	boolean_t init_device = B_FALSE;
11556 	int prev_device_type = SATA_DTYPE_NONE;
11557 	int prev_device_settings = 0;
11558 	int prev_device_state = 0;
11559 	clock_t start_time;
11560 	uint8_t cport = sata_device->satadev_addr.cport;
11561 	uint8_t pmport = sata_device->satadev_addr.pmport;
11562 	int rval;
11563 
11564 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11565 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11566 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11567 
11568 	if (osdinfo != NULL) {
11569 		/*
11570 		 * We are re-probing port with a previously attached device.
11571 		 * Save previous device type and settings.
11572 		 */
11573 		prev_device_type = pmportinfo->pmport_dev_type;
11574 		prev_device_settings = osdinfo->satadrv_settings;
11575 		prev_device_state = osdinfo->satadrv_state;
11576 	}
11577 
11578 	start_time = ddi_get_lbolt();
11579 
11580 	/* check parent status */
11581 	mutex_enter(&cportinfo->cport_mutex);
11582 	if ((cportinfo->cport_state &
11583 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11584 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11585 	    SATA_PORT_DEVLINK_UP) {
11586 		mutex_exit(&cportinfo->cport_mutex);
11587 		return (SATA_FAILURE);
11588 	}
11589 	mutex_exit(&cportinfo->cport_mutex);
11590 
11591 retry_probe_pmport:
11592 
11593 	/* probe port */
11594 	mutex_enter(&pmportinfo->pmport_mutex);
11595 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11596 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11597 	mutex_exit(&pmportinfo->pmport_mutex);
11598 
11599 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11600 	    (SATA_DIP(sata_hba_inst), sata_device);
11601 
11602 	/* might need retry because we cannot touch registers. */
11603 	if (rval == SATA_FAILURE) {
11604 		mutex_enter(&pmportinfo->pmport_mutex);
11605 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11606 		mutex_exit(&pmportinfo->pmport_mutex);
11607 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11608 		    "SATA port %d:%d probing failed",
11609 		    cport, pmport));
11610 		return (SATA_FAILURE);
11611 	} else if (rval == SATA_RETRY) {
11612 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11613 		    "SATA port %d:%d probing failed, retrying...",
11614 		    cport, pmport));
11615 		clock_t cur_time = ddi_get_lbolt();
11616 		/*
11617 		 * A device was not successfully identified or initialized.
11618 		 * Track retry time for device identification.
11619 		 */
11620 		if ((cur_time - start_time) <
11621 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11622 			/* sleep for a while */
11623 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11624 			goto retry_probe_pmport;
11625 		} else {
11626 			mutex_enter(&pmportinfo->pmport_mutex);
11627 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11628 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11629 				    satadrv_state = SATA_DSTATE_FAILED;
11630 			mutex_exit(&pmportinfo->pmport_mutex);
11631 			return (SATA_SUCCESS);
11632 		}
11633 	}
11634 
11635 	/*
11636 	 * Sanity check - Controller port is active? Is the link active?
11637 	 * Is it still a port multiplier?
11638 	 */
11639 	if ((cportinfo->cport_state &
11640 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11641 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11642 	    SATA_PORT_DEVLINK_UP ||
11643 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11644 		/*
11645 		 * Port in non-usable state or no link active/no
11646 		 * device. Free info structure.
11647 		 */
11648 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11649 
11650 		sdevice.satadev_addr.cport = cport;
11651 		sdevice.satadev_addr.pmport = pmport;
11652 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11653 		mutex_exit(&cportinfo->cport_mutex);
11654 
11655 		sata_free_pmult(sata_hba_inst, &sdevice);
11656 		return (SATA_FAILURE);
11657 	}
11658 
11659 	/* SATA_SUCCESS NOW */
11660 	/*
11661 	 * update sata port state and set device type
11662 	 */
11663 	mutex_enter(&pmportinfo->pmport_mutex);
11664 	sata_update_pmport_info(sata_hba_inst, sata_device);
11665 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11666 
11667 	/*
11668 	 * Sanity check - Port is active? Is the link active?
11669 	 * Is there any device attached?
11670 	 */
11671 	if ((pmportinfo->pmport_state &
11672 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11673 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11674 	    SATA_PORT_DEVLINK_UP) {
11675 		/*
11676 		 * Port in non-usable state or no link active/no device.
11677 		 * Free info structure if necessary (direct attached drive
11678 		 * only, for now!
11679 		 */
11680 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11681 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11682 		/* Add here differentiation for device attached or not */
11683 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11684 		mutex_exit(&pmportinfo->pmport_mutex);
11685 		if (sdinfo != NULL)
11686 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11687 		return (SATA_SUCCESS);
11688 	}
11689 
11690 	pmportinfo->pmport_state |= SATA_STATE_READY;
11691 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11692 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11693 
11694 	/*
11695 	 * If we are re-probing the port, there may be
11696 	 * sata_drive_info structure attached
11697 	 * (or sata_pm_info, if PMult is supported).
11698 	 */
11699 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11700 		/*
11701 		 * There is no device, so remove device info structure,
11702 		 * if necessary.
11703 		 */
11704 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11705 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11706 		if (sdinfo != NULL) {
11707 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11708 			sata_log(sata_hba_inst, CE_WARN,
11709 			    "SATA device detached from port %d:%d",
11710 			    cport, pmport);
11711 		}
11712 		mutex_exit(&pmportinfo->pmport_mutex);
11713 		return (SATA_SUCCESS);
11714 	}
11715 
11716 	/* this should not be a pmult */
11717 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11718 	if (sdinfo == NULL) {
11719 		/*
11720 		 * There is some device attached, but there is
11721 		 * no sata_drive_info structure - allocate one
11722 		 */
11723 		mutex_exit(&pmportinfo->pmport_mutex);
11724 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11725 		    KM_SLEEP);
11726 		mutex_enter(&pmportinfo->pmport_mutex);
11727 		/*
11728 		 * Recheck, that the port state did not change when we
11729 		 * released mutex.
11730 		 */
11731 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11732 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11733 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11734 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11735 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11736 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11737 		} else {
11738 			/*
11739 			 * Port is not in ready state, we
11740 			 * cannot attach a device.
11741 			 */
11742 			mutex_exit(&pmportinfo->pmport_mutex);
11743 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11744 			return (SATA_SUCCESS);
11745 		}
11746 		/*
11747 		 * Since we are adding device, presumably new one,
11748 		 * indicate that it  should be initalized,
11749 		 * as well as some internal framework states).
11750 		 */
11751 		init_device = B_TRUE;
11752 	}
11753 
11754 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11755 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11756 
11757 	mutex_exit(&pmportinfo->pmport_mutex);
11758 	/*
11759 	 * Figure out what kind of device we are really
11760 	 * dealing with.
11761 	 */
11762 	rval = sata_probe_device(sata_hba_inst, sata_device);
11763 
11764 	mutex_enter(&pmportinfo->pmport_mutex);
11765 	if (rval == SATA_SUCCESS) {
11766 		/*
11767 		 * If we are dealing with the same type of a device as before,
11768 		 * restore its settings flags.
11769 		 */
11770 		if (osdinfo != NULL &&
11771 		    sata_device->satadev_type == prev_device_type)
11772 			sdinfo->satadrv_settings = prev_device_settings;
11773 
11774 		mutex_exit(&pmportinfo->pmport_mutex);
11775 		/* Set initial device features, if necessary */
11776 		if (init_device == B_TRUE) {
11777 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11778 		}
11779 		if (rval == SATA_SUCCESS)
11780 			return (rval);
11781 	} else {
11782 		/*
11783 		 * If there was some device info before we probe the device,
11784 		 * restore previous device setting, so we can retry from scratch
11785 		 * later. Providing, of course, that device has not disappeared
11786 		 * during probing process.
11787 		 */
11788 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11789 			if (osdinfo != NULL) {
11790 				pmportinfo->pmport_dev_type = prev_device_type;
11791 				sdinfo->satadrv_type = prev_device_type;
11792 				sdinfo->satadrv_state = prev_device_state;
11793 			}
11794 		} else {
11795 			/* device is gone */
11796 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11797 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11798 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11799 			mutex_exit(&pmportinfo->pmport_mutex);
11800 			return (SATA_SUCCESS);
11801 		}
11802 		mutex_exit(&pmportinfo->pmport_mutex);
11803 	}
11804 
11805 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11806 		clock_t cur_time = ddi_get_lbolt();
11807 		/*
11808 		 * A device was not successfully identified or initialized.
11809 		 * Track retry time for device identification.
11810 		 */
11811 		if ((cur_time - start_time) <
11812 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11813 			/* sleep for a while */
11814 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11815 			goto retry_probe_pmport;
11816 		} else {
11817 			mutex_enter(&pmportinfo->pmport_mutex);
11818 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11819 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11820 				    satadrv_state = SATA_DSTATE_FAILED;
11821 			mutex_exit(&pmportinfo->pmport_mutex);
11822 		}
11823 	}
11824 	return (SATA_SUCCESS);
11825 }
11826 
11827 /*
11828  * Allocated related structure for a port multiplier and its device ports
11829  *
11830  * Port multiplier should be ready and probed, and related information like
11831  * the number of the device ports should be store in sata_device_t.
11832  *
11833  * NOTE: No Mutex should be hold.
11834  */
11835 static int
11836 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11837 {
11838 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11839 	sata_cport_info_t *cportinfo = NULL;
11840 	sata_pmult_info_t *pmultinfo = NULL;
11841 	sata_pmport_info_t *pmportinfo = NULL;
11842 	sata_device_t sd;
11843 	dev_t minor_number;
11844 	char name[16];
11845 	uint8_t cport = sata_device->satadev_addr.cport;
11846 	int rval;
11847 	int npmport;
11848 
11849 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11850 
11851 	/* This function might be called while a port-mult is hot-plugged. */
11852 	mutex_enter(&cportinfo->cport_mutex);
11853 
11854 	/* dev_type's not updated when get called from sata_reprobe_port() */
11855 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11856 		/* Create a pmult_info structure */
11857 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11858 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11859 	}
11860 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11861 
11862 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11863 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11864 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11865 
11866 	/*
11867 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11868 	 * The HBA driver should initialize and register the port multiplier,
11869 	 * sata_register_pmult() will fill following fields,
11870 	 *   + sata_pmult_info.pmult_gscr
11871 	 *   + sata_pmult_info.pmult_num_dev_ports
11872 	 */
11873 	sd.satadev_addr = sata_device->satadev_addr;
11874 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11875 	mutex_exit(&cportinfo->cport_mutex);
11876 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11877 	    (SATA_DIP(sata_hba_inst), &sd);
11878 	mutex_enter(&cportinfo->cport_mutex);
11879 
11880 	if (rval != SATA_SUCCESS ||
11881 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11882 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11883 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11884 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11885 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11886 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11887 		mutex_exit(&cportinfo->cport_mutex);
11888 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11889 		    "sata_alloc_pmult: failed to initialize pmult "
11890 		    "at port %d.", cport)
11891 		return (SATA_FAILURE);
11892 	}
11893 
11894 	/* Initialize pmport_info structure */
11895 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11896 	    npmport++) {
11897 
11898 		/* if everything is allocated, skip */
11899 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11900 			continue;
11901 
11902 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11903 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11904 		mutex_exit(&cportinfo->cport_mutex);
11905 
11906 		mutex_enter(&pmportinfo->pmport_mutex);
11907 		pmportinfo->pmport_addr.cport = cport;
11908 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11909 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11910 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11911 		mutex_exit(&pmportinfo->pmport_mutex);
11912 
11913 		mutex_enter(&cportinfo->cport_mutex);
11914 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11915 
11916 		/* Create an attachment point */
11917 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11918 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11919 		(void) sprintf(name, "%d.%d", cport, npmport);
11920 
11921 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11922 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11923 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11924 			    "cannot create SATA attachment point for "
11925 			    "port %d:%d", cport, npmport);
11926 		}
11927 	}
11928 
11929 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11930 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11931 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11932 
11933 	mutex_exit(&cportinfo->cport_mutex);
11934 	return (SATA_SUCCESS);
11935 }
11936 
11937 /*
11938  * Free data structures when a port multiplier is removed.
11939  *
11940  * NOTE: No Mutex should be hold.
11941  */
11942 static void
11943 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11944 {
11945 	sata_cport_info_t *cportinfo;
11946 	sata_pmult_info_t *pmultinfo;
11947 	sata_pmport_info_t *pmportinfo;
11948 	sata_device_t pmport_device;
11949 	sata_drive_info_t *sdinfo;
11950 	dev_info_t *tdip;
11951 	char name[16];
11952 	uint8_t cport = sata_device->satadev_addr.cport;
11953 	int npmport;
11954 
11955 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11956 
11957 	/* This function might be called while port-mult is hot plugged. */
11958 	mutex_enter(&cportinfo->cport_mutex);
11959 
11960 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11961 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11962 	ASSERT(pmultinfo != NULL);
11963 
11964 	/* Free pmport_info structure */
11965 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11966 	    npmport++) {
11967 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
11968 		if (pmportinfo == NULL)
11969 			continue;
11970 		mutex_exit(&cportinfo->cport_mutex);
11971 
11972 		mutex_enter(&pmportinfo->pmport_mutex);
11973 		sdinfo = pmportinfo->pmport_sata_drive;
11974 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11975 		mutex_exit(&pmportinfo->pmport_mutex);
11976 
11977 		/* Remove attachment point. */
11978 		name[0] = '\0';
11979 		(void) sprintf(name, "%d.%d", cport, npmport);
11980 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
11981 		sata_log(sata_hba_inst, CE_NOTE,
11982 		    "Remove attachment point of port %d:%d",
11983 		    cport, npmport);
11984 
11985 		/*
11986 		 * Rumove target node
11987 		 */
11988 		bzero(&pmport_device, sizeof (sata_device_t));
11989 		pmport_device.satadev_rev = SATA_DEVICE_REV;
11990 		pmport_device.satadev_addr.cport = cport;
11991 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
11992 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
11993 
11994 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11995 		    &(pmport_device.satadev_addr));
11996 		if (tdip != NULL && ndi_devi_offline(tdip,
11997 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11998 			/*
11999 			 * Problem :
12000 			 * The target node remained attached.
12001 			 * This happens when the device file was open
12002 			 * or a node was waiting for resources.
12003 			 * Cannot do anything about it.
12004 			 */
12005 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12006 			    "sata_free_pmult: could not unconfigure device "
12007 			    "before disconnecting the SATA port %d:%d",
12008 			    cport, npmport));
12009 
12010 			/*
12011 			 * Set DEVICE REMOVED state in the target
12012 			 * node. It will prevent access to the device
12013 			 * even when a new device is attached, until
12014 			 * the old target node is released, removed and
12015 			 * recreated for a new  device.
12016 			 */
12017 			sata_set_device_removed(tdip);
12018 
12019 			/*
12020 			 * Instruct event daemon to try the target
12021 			 * node cleanup later.
12022 			 */
12023 			sata_set_target_node_cleanup(
12024 			    sata_hba_inst, &(pmport_device.satadev_addr));
12025 
12026 		}
12027 		mutex_enter(&cportinfo->cport_mutex);
12028 
12029 		/*
12030 		 * Add here differentiation for device attached or not
12031 		 */
12032 		if (sdinfo != NULL)  {
12033 			sata_log(sata_hba_inst, CE_WARN,
12034 			    "SATA device detached from port %d:%d",
12035 			    cport, npmport);
12036 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12037 		}
12038 
12039 		mutex_destroy(&pmportinfo->pmport_mutex);
12040 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12041 	}
12042 
12043 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12044 
12045 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12046 
12047 	sata_log(sata_hba_inst, CE_WARN,
12048 	    "SATA port multiplier detached at port %d", cport);
12049 
12050 	mutex_exit(&cportinfo->cport_mutex);
12051 }
12052 
12053 /*
12054  * Initialize device
12055  * Specified device is initialized to a default state.
12056  *
12057  * Returns SATA_SUCCESS if all device features are set successfully,
12058  * SATA_RETRY if device is accessible but device features were not set
12059  * successfully, and SATA_FAILURE otherwise.
12060  */
12061 static int
12062 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12063     sata_drive_info_t *sdinfo)
12064 {
12065 	int rval;
12066 
12067 	sata_save_drive_settings(sdinfo);
12068 
12069 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12070 
12071 	sata_init_write_cache_mode(sdinfo);
12072 
12073 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12074 
12075 	/* Determine current data transfer mode */
12076 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12077 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12078 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12079 	    SATA_VALIDINFO_88) != 0 &&
12080 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12081 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12082 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12083 	    SATA_MDMA_SEL_MASK) != 0) {
12084 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12085 	} else
12086 		/* DMA supported, not no DMA transfer mode is selected !? */
12087 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12088 
12089 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12090 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12091 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12092 	else
12093 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12094 
12095 	return (rval);
12096 }
12097 
12098 
12099 /*
12100  * Initialize write cache mode.
12101  *
12102  * The default write cache setting for SATA HDD is provided by sata_write_cache
12103  * static variable. ATAPI CD/DVDs devices have write cache default is
12104  * determined by sata_atapicdvd_write_cache static variable.
12105  * ATAPI tape devices have write cache default is determined by
12106  * sata_atapitape_write_cache static variable.
12107  * ATAPI disk devices have write cache default is determined by
12108  * sata_atapidisk_write_cache static variable.
12109  * 1 - enable
12110  * 0 - disable
12111  * any other value - current drive setting
12112  *
12113  * Although there is not reason to disable write cache on CD/DVD devices,
12114  * tape devices and ATAPI disk devices, the default setting control is provided
12115  * for the maximun flexibility.
12116  *
12117  * In the future, it may be overridden by the
12118  * disk-write-cache-enable property setting, if it is defined.
12119  * Returns SATA_SUCCESS if all device features are set successfully,
12120  * SATA_FAILURE otherwise.
12121  */
12122 static void
12123 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12124 {
12125 	switch (sdinfo->satadrv_type) {
12126 	case SATA_DTYPE_ATADISK:
12127 		if (sata_write_cache == 1)
12128 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12129 		else if (sata_write_cache == 0)
12130 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12131 		/*
12132 		 * When sata_write_cache value is not 0 or 1,
12133 		 * a current setting of the drive's write cache is used.
12134 		 */
12135 		break;
12136 	case SATA_DTYPE_ATAPICD:
12137 		if (sata_atapicdvd_write_cache == 1)
12138 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12139 		else if (sata_atapicdvd_write_cache == 0)
12140 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12141 		/*
12142 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12143 		 * a current setting of the drive's write cache is used.
12144 		 */
12145 		break;
12146 	case SATA_DTYPE_ATAPITAPE:
12147 		if (sata_atapitape_write_cache == 1)
12148 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12149 		else if (sata_atapitape_write_cache == 0)
12150 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12151 		/*
12152 		 * When sata_atapitape_write_cache value is not 0 or 1,
12153 		 * a current setting of the drive's write cache is used.
12154 		 */
12155 		break;
12156 	case SATA_DTYPE_ATAPIDISK:
12157 		if (sata_atapidisk_write_cache == 1)
12158 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12159 		else if (sata_atapidisk_write_cache == 0)
12160 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12161 		/*
12162 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12163 		 * a current setting of the drive's write cache is used.
12164 		 */
12165 		break;
12166 	}
12167 }
12168 
12169 
12170 /*
12171  * Validate sata address.
12172  * Specified cport, pmport and qualifier has to match
12173  * passed sata_scsi configuration info.
12174  * The presence of an attached device is not verified.
12175  *
12176  * Returns 0 when address is valid, -1 otherwise.
12177  */
12178 static int
12179 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12180 	int pmport, int qual)
12181 {
12182 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12183 		goto invalid_address;
12184 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12185 		goto invalid_address;
12186 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12187 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12188 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12189 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12190 		goto invalid_address;
12191 
12192 	return (0);
12193 
12194 invalid_address:
12195 	return (-1);
12196 
12197 }
12198 
12199 /*
12200  * Validate scsi address
12201  * SCSI target address is translated into SATA cport/pmport and compared
12202  * with a controller port/device configuration. LUN has to be 0.
12203  * Returns 0 if a scsi target refers to an attached device,
12204  * returns 1 if address is valid but no valid device is attached,
12205  * returns 2 if address is valid but device type is unknown (not valid device),
12206  * returns -1 if bad address or device is of an unsupported type.
12207  * Upon return sata_device argument is set.
12208  *
12209  * Port multiplier is supported now.
12210  */
12211 static int
12212 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12213 	struct scsi_address *ap, sata_device_t *sata_device)
12214 {
12215 	int cport, pmport, qual, rval;
12216 
12217 	rval = -1;	/* Invalid address */
12218 	if (ap->a_lun != 0)
12219 		goto out;
12220 
12221 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12222 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12223 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12224 
12225 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12226 		goto out;
12227 
12228 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12229 	    0) {
12230 
12231 		sata_cport_info_t *cportinfo;
12232 		sata_pmult_info_t *pmultinfo;
12233 		sata_drive_info_t *sdinfo = NULL;
12234 
12235 		sata_device->satadev_addr.qual = qual;
12236 		sata_device->satadev_addr.cport = cport;
12237 		sata_device->satadev_addr.pmport = pmport;
12238 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12239 
12240 		rval = 1;	/* Valid sata address */
12241 
12242 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12243 		if (qual == SATA_ADDR_DCPORT) {
12244 			if (cportinfo == NULL ||
12245 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12246 				goto out;
12247 
12248 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12249 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12250 			    sdinfo != NULL) {
12251 				rval = 2;
12252 				goto out;
12253 			}
12254 
12255 			if ((cportinfo->cport_dev_type &
12256 			    SATA_VALID_DEV_TYPE) == 0) {
12257 				rval = -1;
12258 				goto out;
12259 			}
12260 
12261 		} else if (qual == SATA_ADDR_DPMPORT) {
12262 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12263 			if (pmultinfo == NULL) {
12264 				rval = -1;
12265 				goto out;
12266 			}
12267 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12268 			    NULL ||
12269 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12270 			    pmport) == SATA_DTYPE_NONE)
12271 				goto out;
12272 
12273 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12274 			    pmport);
12275 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12276 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12277 				rval = 2;
12278 				goto out;
12279 			}
12280 
12281 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12282 			    pmport) && SATA_VALID_DEV_TYPE) == 0) {
12283 				rval = -1;
12284 				goto out;
12285 			}
12286 
12287 		} else {
12288 			rval = -1;
12289 			goto out;
12290 		}
12291 		if ((sdinfo == NULL) ||
12292 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12293 			goto out;
12294 
12295 		sata_device->satadev_type = sdinfo->satadrv_type;
12296 
12297 		return (0);
12298 	}
12299 out:
12300 	if (rval > 0) {
12301 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12302 		    "sata_validate_scsi_address: no valid target %x lun %x",
12303 		    ap->a_target, ap->a_lun);
12304 	}
12305 	return (rval);
12306 }
12307 
12308 /*
12309  * Find dip corresponding to passed device number
12310  *
12311  * Returns NULL if invalid device number is passed or device cannot be found,
12312  * Returns dip is device is found.
12313  */
12314 static dev_info_t *
12315 sata_devt_to_devinfo(dev_t dev)
12316 {
12317 	dev_info_t *dip;
12318 #ifndef __lock_lint
12319 	struct devnames *dnp;
12320 	major_t major = getmajor(dev);
12321 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12322 
12323 	if (major >= devcnt)
12324 		return (NULL);
12325 
12326 	dnp = &devnamesp[major];
12327 	LOCK_DEV_OPS(&(dnp->dn_lock));
12328 	dip = dnp->dn_head;
12329 	while (dip && (ddi_get_instance(dip) != instance)) {
12330 		dip = ddi_get_next(dip);
12331 	}
12332 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12333 #endif
12334 
12335 	return (dip);
12336 }
12337 
12338 
12339 /*
12340  * Probe device.
12341  * This function issues Identify Device command and initializes local
12342  * sata_drive_info structure if the device can be identified.
12343  * The device type is determined by examining Identify Device
12344  * command response.
12345  * If the sata_hba_inst has linked drive info structure for this
12346  * device address, the Identify Device data is stored into sata_drive_info
12347  * structure linked to the port info structure.
12348  *
12349  * sata_device has to refer to the valid sata port(s) for HBA described
12350  * by sata_hba_inst structure.
12351  *
12352  * Returns:
12353  *	SATA_SUCCESS if device type was successfully probed and port-linked
12354  *		drive info structure was updated;
12355  * 	SATA_FAILURE if there is no device, or device was not probed
12356  *		successully;
12357  *	SATA_RETRY if device probe can be retried later.
12358  * If a device cannot be identified, sata_device's dev_state and dev_type
12359  * fields are set to unknown.
12360  * There are no retries in this function. Any retries should be managed by
12361  * the caller.
12362  */
12363 
12364 
12365 static int
12366 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12367 {
12368 	sata_pmport_info_t *pmportinfo;
12369 	sata_drive_info_t *sdinfo;
12370 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12371 	int rval;
12372 
12373 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12374 	    sata_device->satadev_addr.cport) &
12375 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12376 
12377 	sata_device->satadev_type = SATA_DTYPE_NONE;
12378 
12379 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12380 	    sata_device->satadev_addr.cport)));
12381 
12382 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12383 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12384 		    sata_device->satadev_addr.cport,
12385 		    sata_device->satadev_addr.pmport);
12386 		ASSERT(pmportinfo != NULL);
12387 	}
12388 
12389 	/* Get pointer to port-linked sata device info structure */
12390 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12391 	if (sdinfo != NULL) {
12392 		sdinfo->satadrv_state &=
12393 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12394 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12395 	} else {
12396 		/* No device to probe */
12397 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12398 		    sata_device->satadev_addr.cport)));
12399 		sata_device->satadev_type = SATA_DTYPE_NONE;
12400 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12401 		return (SATA_FAILURE);
12402 	}
12403 	/*
12404 	 * Need to issue both types of identify device command and
12405 	 * determine device type by examining retreived data/status.
12406 	 * First, ATA Identify Device.
12407 	 */
12408 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12409 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12410 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12411 	    sata_device->satadev_addr.cport)));
12412 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12413 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12414 	if (rval == SATA_RETRY) {
12415 		/* We may try to check for ATAPI device */
12416 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12417 			/*
12418 			 * HBA supports ATAPI - try to issue Identify Packet
12419 			 * Device command.
12420 			 */
12421 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12422 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12423 		}
12424 	}
12425 	if (rval == SATA_SUCCESS) {
12426 		/*
12427 		 * Got something responding positively to ATA Identify Device
12428 		 * or to Identify Packet Device cmd.
12429 		 * Save last used device type.
12430 		 */
12431 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12432 
12433 		/* save device info, if possible */
12434 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12435 		    sata_device->satadev_addr.cport)));
12436 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12437 		if (sdinfo == NULL) {
12438 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12439 			    sata_device->satadev_addr.cport)));
12440 			return (SATA_FAILURE);
12441 		}
12442 		/*
12443 		 * Copy drive info into the port-linked drive info structure.
12444 		 */
12445 		*sdinfo = new_sdinfo;
12446 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12447 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12448 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12449 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12450 			    sata_device->satadev_addr.cport) =
12451 			    sdinfo->satadrv_type;
12452 		else { /* SATA_ADDR_DPMPORT */
12453 			mutex_enter(&pmportinfo->pmport_mutex);
12454 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12455 			    sata_device->satadev_addr.cport,
12456 			    sata_device->satadev_addr.pmport) =
12457 			    sdinfo->satadrv_type;
12458 			mutex_exit(&pmportinfo->pmport_mutex);
12459 		}
12460 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12461 		    sata_device->satadev_addr.cport)));
12462 		return (SATA_SUCCESS);
12463 	}
12464 
12465 	/*
12466 	 * It may be SATA_RETRY or SATA_FAILURE return.
12467 	 * Looks like we cannot determine the device type at this time.
12468 	 */
12469 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12470 	    sata_device->satadev_addr.cport)));
12471 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12472 	if (sdinfo != NULL) {
12473 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12474 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12475 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12476 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12477 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12478 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12479 			    sata_device->satadev_addr.cport) =
12480 			    SATA_DTYPE_UNKNOWN;
12481 		else {
12482 			/* SATA_ADDR_DPMPORT */
12483 			mutex_enter(&pmportinfo->pmport_mutex);
12484 			if ((SATA_PMULT_INFO(sata_hba_inst,
12485 			    sata_device->satadev_addr.cport) != NULL) &&
12486 			    (SATA_PMPORT_INFO(sata_hba_inst,
12487 			    sata_device->satadev_addr.cport,
12488 			    sata_device->satadev_addr.pmport) != NULL))
12489 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12490 				    sata_device->satadev_addr.cport,
12491 				    sata_device->satadev_addr.pmport) =
12492 				    SATA_DTYPE_UNKNOWN;
12493 			mutex_exit(&pmportinfo->pmport_mutex);
12494 		}
12495 	}
12496 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12497 	    sata_device->satadev_addr.cport)));
12498 	return (rval);
12499 }
12500 
12501 
12502 /*
12503  * Get pointer to sata_drive_info structure.
12504  *
12505  * The sata_device has to contain address (cport, pmport and qualifier) for
12506  * specified sata_scsi structure.
12507  *
12508  * Returns NULL if device address is not valid for this HBA configuration.
12509  * Otherwise, returns a pointer to sata_drive_info structure.
12510  *
12511  * This function should be called with a port mutex held.
12512  */
12513 static sata_drive_info_t *
12514 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12515     sata_device_t *sata_device)
12516 {
12517 	uint8_t cport = sata_device->satadev_addr.cport;
12518 	uint8_t pmport = sata_device->satadev_addr.pmport;
12519 	uint8_t qual = sata_device->satadev_addr.qual;
12520 
12521 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12522 		return (NULL);
12523 
12524 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12525 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12526 		/* Port not probed yet */
12527 		return (NULL);
12528 
12529 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12530 		return (NULL);
12531 
12532 	if (qual == SATA_ADDR_DCPORT) {
12533 		/* Request for a device on a controller port */
12534 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12535 		    SATA_DTYPE_PMULT)
12536 			/* Port multiplier attached */
12537 			return (NULL);
12538 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12539 	}
12540 	if (qual == SATA_ADDR_DPMPORT) {
12541 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12542 		    SATA_DTYPE_PMULT)
12543 			return (NULL);
12544 
12545 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12546 			return (NULL);
12547 
12548 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12549 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12550 			/* Port multiplier port not probed yet */
12551 			return (NULL);
12552 
12553 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12554 	}
12555 
12556 	/* we should not get here */
12557 	return (NULL);
12558 }
12559 
12560 
12561 /*
12562  * sata_identify_device.
12563  * Send Identify Device command to SATA HBA driver.
12564  * If command executes successfully, update sata_drive_info structure pointed
12565  * to by sdinfo argument, including Identify Device data.
12566  * If command fails, invalidate data in sata_drive_info.
12567  *
12568  * Cannot be called from interrupt level.
12569  *
12570  * Returns:
12571  * SATA_SUCCESS if the device was identified as a supported device,
12572  * SATA_RETRY if the device was not identified but could be retried,
12573  * SATA_FAILURE if the device was not identified and identify attempt
12574  *	should not be retried.
12575  */
12576 static int
12577 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12578     sata_drive_info_t *sdinfo)
12579 {
12580 	uint16_t cfg_word;
12581 	int rval;
12582 
12583 	/* fetch device identify data */
12584 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12585 	    sdinfo)) != SATA_SUCCESS)
12586 		goto fail_unknown;
12587 
12588 	cfg_word = sdinfo->satadrv_id.ai_config;
12589 
12590 	/* Set the correct device type */
12591 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12592 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12593 	} else if (cfg_word == SATA_CFA_TYPE) {
12594 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12595 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12596 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12597 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12598 		case SATA_ATAPI_CDROM_DEV:
12599 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12600 			break;
12601 		case SATA_ATAPI_SQACC_DEV:
12602 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12603 			break;
12604 		case SATA_ATAPI_DIRACC_DEV:
12605 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12606 			break;
12607 		default:
12608 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12609 		}
12610 	} else {
12611 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12612 	}
12613 
12614 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12615 		if (sdinfo->satadrv_capacity == 0) {
12616 			/* Non-LBA disk. Too bad... */
12617 			sata_log(sata_hba_inst, CE_WARN,
12618 			    "SATA disk device at port %d does not support LBA",
12619 			    sdinfo->satadrv_addr.cport);
12620 			rval = SATA_FAILURE;
12621 			goto fail_unknown;
12622 		}
12623 	}
12624 #if 0
12625 	/* Left for historical reason */
12626 	/*
12627 	 * Some initial version of SATA spec indicated that at least
12628 	 * UDMA mode 4 has to be supported. It is not metioned in
12629 	 * SerialATA 2.6, so this restriction is removed.
12630 	 */
12631 	/* Check for Ultra DMA modes 6 through 0 being supported */
12632 	for (i = 6; i >= 0; --i) {
12633 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12634 			break;
12635 	}
12636 
12637 	/*
12638 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12639 	 * higher are not supported by the device, fail this
12640 	 * device.
12641 	 */
12642 	if (i < 4) {
12643 		/* No required Ultra DMA mode supported */
12644 		sata_log(sata_hba_inst, CE_WARN,
12645 		    "SATA disk device at port %d does not support UDMA "
12646 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12647 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12648 		    "mode 4 or higher required, %d supported", i));
12649 		rval = SATA_FAILURE;
12650 		goto fail_unknown;
12651 	}
12652 #endif
12653 
12654 	/*
12655 	 * For Disk devices, if it doesn't support UDMA mode, we would
12656 	 * like to return failure directly.
12657 	 */
12658 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12659 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12660 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12661 		sata_log(sata_hba_inst, CE_WARN,
12662 		    "SATA disk device at port %d does not support UDMA",
12663 		    sdinfo->satadrv_addr.cport);
12664 		rval = SATA_FAILURE;
12665 		goto fail_unknown;
12666 	}
12667 
12668 	return (SATA_SUCCESS);
12669 
12670 fail_unknown:
12671 	/* Invalidate sata_drive_info ? */
12672 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12673 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12674 	return (rval);
12675 }
12676 
12677 /*
12678  * Log/display device information
12679  */
12680 static void
12681 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12682     sata_drive_info_t *sdinfo)
12683 {
12684 	int valid_version;
12685 	char msg_buf[MAXPATHLEN];
12686 	int i;
12687 
12688 	/* Show HBA path */
12689 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12690 
12691 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12692 
12693 	switch (sdinfo->satadrv_type) {
12694 	case SATA_DTYPE_ATADISK:
12695 		(void) sprintf(msg_buf, "SATA disk device at");
12696 		break;
12697 
12698 	case SATA_DTYPE_ATAPICD:
12699 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12700 		break;
12701 
12702 	case SATA_DTYPE_ATAPITAPE:
12703 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12704 		break;
12705 
12706 	case SATA_DTYPE_ATAPIDISK:
12707 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12708 		break;
12709 
12710 	case SATA_DTYPE_UNKNOWN:
12711 		(void) sprintf(msg_buf,
12712 		    "Unsupported SATA device type (cfg 0x%x) at ",
12713 		    sdinfo->satadrv_id.ai_config);
12714 		break;
12715 	}
12716 
12717 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12718 		cmn_err(CE_CONT, "?\t%s port %d\n",
12719 		    msg_buf, sdinfo->satadrv_addr.cport);
12720 	else
12721 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12722 		    msg_buf, sdinfo->satadrv_addr.cport,
12723 		    sdinfo->satadrv_addr.pmport);
12724 
12725 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12726 	    sizeof (sdinfo->satadrv_id.ai_model));
12727 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12728 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12729 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12730 
12731 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12732 	    sizeof (sdinfo->satadrv_id.ai_fw));
12733 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12734 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12735 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12736 
12737 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12738 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12739 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12740 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12741 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12742 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12743 	} else {
12744 		/*
12745 		 * Some drives do not implement serial number and may
12746 		 * violate the spec by providing spaces rather than zeros
12747 		 * in serial number field. Scan the buffer to detect it.
12748 		 */
12749 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12750 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12751 				break;
12752 		}
12753 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12754 			cmn_err(CE_CONT, "?\tserial number - none\n");
12755 		} else {
12756 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12757 		}
12758 	}
12759 
12760 #ifdef SATA_DEBUG
12761 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12762 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12763 		int i;
12764 		for (i = 14; i >= 2; i--) {
12765 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12766 				valid_version = i;
12767 				break;
12768 			}
12769 		}
12770 		cmn_err(CE_CONT,
12771 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12772 		    valid_version,
12773 		    sdinfo->satadrv_id.ai_majorversion,
12774 		    sdinfo->satadrv_id.ai_minorversion);
12775 	}
12776 #endif
12777 	/* Log some info */
12778 	cmn_err(CE_CONT, "?\tsupported features:\n");
12779 	msg_buf[0] = '\0';
12780 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12781 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12782 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12783 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12784 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12785 	}
12786 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12787 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12788 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12789 		(void) strlcat(msg_buf, ", Native Command Queueing",
12790 		    MAXPATHLEN);
12791 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12792 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12793 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12794 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12795 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12796 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12797 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12798 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12799 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12800 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12801 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12802 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12803 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12804 	if (sdinfo->satadrv_features_support &
12805 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12806 		msg_buf[0] = '\0';
12807 		(void) snprintf(msg_buf, MAXPATHLEN,
12808 		    "Supported queue depth %d",
12809 		    sdinfo->satadrv_queue_depth);
12810 		if (!(sata_func_enable &
12811 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12812 			(void) strlcat(msg_buf,
12813 			    " - queueing disabled globally", MAXPATHLEN);
12814 		else if (sdinfo->satadrv_queue_depth >
12815 		    sdinfo->satadrv_max_queue_depth) {
12816 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12817 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12818 			    (int)sdinfo->satadrv_max_queue_depth);
12819 		}
12820 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12821 	}
12822 
12823 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12824 #ifdef __i386
12825 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12826 		    sdinfo->satadrv_capacity);
12827 #else
12828 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12829 		    sdinfo->satadrv_capacity);
12830 #endif
12831 		cmn_err(CE_CONT, "?%s", msg_buf);
12832 	}
12833 }
12834 
12835 /*
12836  * Log/display port multiplier information
12837  * No Mutex should be hold.
12838  */
12839 static void
12840 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12841     sata_device_t *sata_device)
12842 {
12843 	_NOTE(ARGUNUSED(sata_hba_inst))
12844 
12845 	int cport = sata_device->satadev_addr.cport;
12846 	sata_pmult_info_t *pmultinfo;
12847 	char msg_buf[MAXPATHLEN];
12848 	uint32_t gscr0, gscr1, gscr2, gscr64;
12849 
12850 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12851 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12852 	if (pmultinfo == NULL) {
12853 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12854 		return;
12855 	}
12856 
12857 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12858 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12859 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12860 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12861 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12862 
12863 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12864 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12865 
12866 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12867 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12868 	cmn_err(CE_CONT, "?%s", msg_buf);
12869 
12870 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12871 	if (gscr1 & (1 << 3))
12872 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12873 	else if (gscr1 & (1 << 2))
12874 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12875 	else if (gscr1 & (1 << 1))
12876 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12877 	else
12878 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12879 	cmn_err(CE_CONT, "?%s", msg_buf);
12880 
12881 	(void) strcpy(msg_buf, "\tSupport ");
12882 	if (gscr64 & (1 << 3))
12883 		(void) strlcat(msg_buf, "Asy-Notif, ",
12884 		    MAXPATHLEN);
12885 	if (gscr64 & (1 << 2))
12886 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12887 	if (gscr64 & (1 << 1))
12888 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12889 	if (gscr64 & (1 << 0))
12890 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12891 	if ((gscr64 & 0xf) == 0)
12892 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12893 	cmn_err(CE_CONT, "?%s", msg_buf);
12894 
12895 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12896 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12897 	cmn_err(CE_CONT, "?%s", msg_buf);
12898 }
12899 
12900 /*
12901  * sata_save_drive_settings extracts current setting of the device and stores
12902  * it for future reference, in case the device setup would need to be restored
12903  * after the device reset.
12904  *
12905  * For all devices read ahead and write cache settings are saved, if the
12906  * device supports these features at all.
12907  * For ATAPI devices the Removable Media Status Notification setting is saved.
12908  */
12909 static void
12910 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12911 {
12912 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12913 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12914 
12915 		/* Current setting of Read Ahead (and Read Cache) */
12916 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12917 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12918 		else
12919 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12920 
12921 		/* Current setting of Write Cache */
12922 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12923 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12924 		else
12925 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12926 	}
12927 
12928 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12929 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12930 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12931 		else
12932 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12933 	}
12934 }
12935 
12936 
12937 /*
12938  * sata_check_capacity function determines a disk capacity
12939  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12940  *
12941  * NOTE: CHS mode is not supported! If a device does not support LBA,
12942  * this function is not called.
12943  *
12944  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12945  */
12946 static uint64_t
12947 sata_check_capacity(sata_drive_info_t *sdinfo)
12948 {
12949 	uint64_t capacity = 0;
12950 	int i;
12951 
12952 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
12953 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
12954 		/* Capacity valid only for LBA-addressable disk devices */
12955 		return (0);
12956 
12957 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
12958 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
12959 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
12960 		/* LBA48 mode supported and enabled */
12961 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
12962 		    SATA_DEV_F_LBA28;
12963 		for (i = 3;  i >= 0;  --i) {
12964 			capacity <<= 16;
12965 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
12966 		}
12967 	} else {
12968 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
12969 		capacity <<= 16;
12970 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
12971 		if (capacity >= 0x1000000)
12972 			/* LBA28 mode */
12973 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
12974 	}
12975 	return (capacity);
12976 }
12977 
12978 
12979 /*
12980  * Allocate consistent buffer for DMA transfer
12981  *
12982  * Cannot be called from interrupt level or with mutex held - it may sleep.
12983  *
12984  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
12985  */
12986 static struct buf *
12987 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
12988 {
12989 	struct scsi_address ap;
12990 	struct buf *bp;
12991 	ddi_dma_attr_t	cur_dma_attr;
12992 
12993 	ASSERT(spx->txlt_sata_pkt != NULL);
12994 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
12995 	ap.a_target = SATA_TO_SCSI_TARGET(
12996 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
12997 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
12998 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
12999 	ap.a_lun = 0;
13000 
13001 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13002 	    B_READ, SLEEP_FUNC, NULL);
13003 
13004 	if (bp != NULL) {
13005 		/* Allocate DMA resources for this buffer */
13006 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13007 		/*
13008 		 * We use a local version of the dma_attr, to account
13009 		 * for a device addressing limitations.
13010 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13011 		 * will cause dma attributes to be adjusted to a lowest
13012 		 * acceptable level.
13013 		 */
13014 		sata_adjust_dma_attr(NULL,
13015 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13016 
13017 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13018 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13019 			scsi_free_consistent_buf(bp);
13020 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13021 			bp = NULL;
13022 		}
13023 	}
13024 	return (bp);
13025 }
13026 
13027 /*
13028  * Release local buffer (consistent buffer for DMA transfer) allocated
13029  * via sata_alloc_local_buffer().
13030  */
13031 static void
13032 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13033 {
13034 	ASSERT(spx->txlt_sata_pkt != NULL);
13035 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13036 
13037 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13038 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13039 
13040 	sata_common_free_dma_rsrcs(spx);
13041 
13042 	/* Free buffer */
13043 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13044 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13045 }
13046 
13047 /*
13048  * Allocate sata_pkt
13049  * Pkt structure version and embedded strcutures version are initialized.
13050  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13051  *
13052  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13053  * callback argument determines if it can sleep or not.
13054  * Hence, it should not be called from interrupt context.
13055  *
13056  * If successful, non-NULL pointer to a sata pkt is returned.
13057  * Upon failure, NULL pointer is returned.
13058  */
13059 static sata_pkt_t *
13060 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13061 {
13062 	sata_pkt_t *spkt;
13063 	int kmsflag;
13064 
13065 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13066 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13067 	if (spkt == NULL) {
13068 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13069 		    "sata_pkt_alloc: failed"));
13070 		return (NULL);
13071 	}
13072 	spkt->satapkt_rev = SATA_PKT_REV;
13073 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13074 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13075 	spkt->satapkt_framework_private = spx;
13076 	spx->txlt_sata_pkt = spkt;
13077 	return (spkt);
13078 }
13079 
13080 /*
13081  * Free sata pkt allocated via sata_pkt_alloc()
13082  */
13083 static void
13084 sata_pkt_free(sata_pkt_txlate_t *spx)
13085 {
13086 	ASSERT(spx->txlt_sata_pkt != NULL);
13087 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13088 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13089 	spx->txlt_sata_pkt = NULL;
13090 }
13091 
13092 
13093 /*
13094  * Adjust DMA attributes.
13095  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13096  * from 8 bits to 16 bits, depending on a command being used.
13097  * Limiting max block count arbitrarily to 256 for all read/write
13098  * commands may affects performance, so check both the device and
13099  * controller capability before adjusting dma attributes.
13100  */
13101 void
13102 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13103     ddi_dma_attr_t *adj_dma_attr)
13104 {
13105 	uint32_t count_max;
13106 
13107 	/* Copy original attributes */
13108 	*adj_dma_attr = *dma_attr;
13109 	/*
13110 	 * Things to consider: device addressing capability,
13111 	 * "excessive" controller DMA capabilities.
13112 	 * If a device is being probed/initialized, there are
13113 	 * no device info - use default limits then.
13114 	 */
13115 	if (sdinfo == NULL) {
13116 		count_max = dma_attr->dma_attr_granular * 0x100;
13117 		if (dma_attr->dma_attr_count_max > count_max)
13118 			adj_dma_attr->dma_attr_count_max = count_max;
13119 		if (dma_attr->dma_attr_maxxfer > count_max)
13120 			adj_dma_attr->dma_attr_maxxfer = count_max;
13121 		return;
13122 	}
13123 
13124 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13125 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13126 			/*
13127 			 * 16-bit sector count may be used - we rely on
13128 			 * the assumption that only read and write cmds
13129 			 * will request more than 256 sectors worth of data
13130 			 */
13131 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13132 		} else {
13133 			/*
13134 			 * 8-bit sector count will be used - default limits
13135 			 * for dma attributes
13136 			 */
13137 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13138 		}
13139 		/*
13140 		 * Adjust controler dma attributes, if necessary
13141 		 */
13142 		if (dma_attr->dma_attr_count_max > count_max)
13143 			adj_dma_attr->dma_attr_count_max = count_max;
13144 		if (dma_attr->dma_attr_maxxfer > count_max)
13145 			adj_dma_attr->dma_attr_maxxfer = count_max;
13146 	}
13147 }
13148 
13149 
13150 /*
13151  * Allocate DMA resources for the buffer
13152  * This function handles initial DMA resource allocation as well as
13153  * DMA window shift and may be called repeatedly for the same DMA window
13154  * until all DMA cookies in the DMA window are processed.
13155  * To guarantee that there is always a coherent set of cookies to process
13156  * by SATA HBA driver (observing alignment, device granularity, etc.),
13157  * the number of slots for DMA cookies is equal to lesser of  a number of
13158  * cookies in a DMA window and a max number of scatter/gather entries.
13159  *
13160  * Returns DDI_SUCCESS upon successful operation.
13161  * Return failure code of a failing command or DDI_FAILURE when
13162  * internal cleanup failed.
13163  */
13164 static int
13165 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13166     int (*callback)(caddr_t), caddr_t arg,
13167     ddi_dma_attr_t *cur_dma_attr)
13168 {
13169 	int	rval;
13170 	off_t	offset;
13171 	size_t	size;
13172 	int	max_sg_len, req_len, i;
13173 	uint_t	dma_flags;
13174 	struct buf	*bp;
13175 	uint64_t	cur_txfer_len;
13176 
13177 
13178 	ASSERT(spx->txlt_sata_pkt != NULL);
13179 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13180 	ASSERT(bp != NULL);
13181 
13182 
13183 	if (spx->txlt_buf_dma_handle == NULL) {
13184 		/*
13185 		 * No DMA resources allocated so far - this is a first call
13186 		 * for this sata pkt.
13187 		 */
13188 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13189 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13190 
13191 		if (rval != DDI_SUCCESS) {
13192 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13193 			    "sata_dma_buf_setup: no buf DMA resources %x",
13194 			    rval));
13195 			return (rval);
13196 		}
13197 
13198 		if (bp->b_flags & B_READ)
13199 			dma_flags = DDI_DMA_READ;
13200 		else
13201 			dma_flags = DDI_DMA_WRITE;
13202 
13203 		if (flags & PKT_CONSISTENT)
13204 			dma_flags |= DDI_DMA_CONSISTENT;
13205 
13206 		if (flags & PKT_DMA_PARTIAL)
13207 			dma_flags |= DDI_DMA_PARTIAL;
13208 
13209 		/*
13210 		 * Check buffer alignment and size against dma attributes
13211 		 * Consider dma_attr_align only. There may be requests
13212 		 * with the size lower than device granularity, but they
13213 		 * will not read/write from/to the device, so no adjustment
13214 		 * is necessary. The dma_attr_minxfer theoretically should
13215 		 * be considered, but no HBA driver is checking it.
13216 		 */
13217 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13218 		    cur_dma_attr->dma_attr_align)) {
13219 			rval = ddi_dma_buf_bind_handle(
13220 			    spx->txlt_buf_dma_handle,
13221 			    bp, dma_flags, callback, arg,
13222 			    &spx->txlt_dma_cookie,
13223 			    &spx->txlt_curwin_num_dma_cookies);
13224 		} else { /* Buffer is not aligned */
13225 
13226 			int	(*ddicallback)(caddr_t);
13227 			size_t	bufsz;
13228 
13229 			/* Check id sleeping is allowed */
13230 			ddicallback = (callback == NULL_FUNC) ?
13231 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13232 
13233 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13234 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13235 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13236 
13237 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13238 				/*
13239 				 * CPU will need to access data in the buffer
13240 				 * (for copying) so map it.
13241 				 */
13242 				bp_mapin(bp);
13243 
13244 			ASSERT(spx->txlt_tmp_buf == NULL);
13245 
13246 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13247 			rval = ddi_dma_mem_alloc(
13248 			    spx->txlt_buf_dma_handle,
13249 			    bp->b_bcount,
13250 			    &sata_acc_attr,
13251 			    DDI_DMA_STREAMING,
13252 			    ddicallback, NULL,
13253 			    &spx->txlt_tmp_buf,
13254 			    &bufsz,
13255 			    &spx->txlt_tmp_buf_handle);
13256 
13257 			if (rval != DDI_SUCCESS) {
13258 				/* DMA mapping failed */
13259 				(void) ddi_dma_free_handle(
13260 				    &spx->txlt_buf_dma_handle);
13261 				spx->txlt_buf_dma_handle = NULL;
13262 #ifdef SATA_DEBUG
13263 				mbuffail_count++;
13264 #endif
13265 				SATADBG1(SATA_DBG_DMA_SETUP,
13266 				    spx->txlt_sata_hba_inst,
13267 				    "sata_dma_buf_setup: "
13268 				    "buf dma mem alloc failed %x\n", rval);
13269 				return (rval);
13270 			}
13271 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13272 			    cur_dma_attr->dma_attr_align));
13273 
13274 #ifdef SATA_DEBUG
13275 			mbuf_count++;
13276 
13277 			if (bp->b_bcount != bufsz)
13278 				/*
13279 				 * This will require special handling, because
13280 				 * DMA cookies will be based on the temporary
13281 				 * buffer size, not the original buffer
13282 				 * b_bcount, so the residue may have to
13283 				 * be counted differently.
13284 				 */
13285 				SATADBG2(SATA_DBG_DMA_SETUP,
13286 				    spx->txlt_sata_hba_inst,
13287 				    "sata_dma_buf_setup: bp size %x != "
13288 				    "bufsz %x\n", bp->b_bcount, bufsz);
13289 #endif
13290 			if (dma_flags & DDI_DMA_WRITE) {
13291 				/*
13292 				 * Write operation - copy data into
13293 				 * an aligned temporary buffer. Buffer will be
13294 				 * synced for device by ddi_dma_addr_bind_handle
13295 				 */
13296 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13297 				    bp->b_bcount);
13298 			}
13299 
13300 			rval = ddi_dma_addr_bind_handle(
13301 			    spx->txlt_buf_dma_handle,
13302 			    NULL,
13303 			    spx->txlt_tmp_buf,
13304 			    bufsz, dma_flags, ddicallback, 0,
13305 			    &spx->txlt_dma_cookie,
13306 			    &spx->txlt_curwin_num_dma_cookies);
13307 		}
13308 
13309 		switch (rval) {
13310 		case DDI_DMA_PARTIAL_MAP:
13311 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13312 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13313 			/*
13314 			 * Partial DMA mapping.
13315 			 * Retrieve number of DMA windows for this request.
13316 			 */
13317 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13318 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13319 				if (spx->txlt_tmp_buf != NULL) {
13320 					ddi_dma_mem_free(
13321 					    &spx->txlt_tmp_buf_handle);
13322 					spx->txlt_tmp_buf = NULL;
13323 				}
13324 				(void) ddi_dma_unbind_handle(
13325 				    spx->txlt_buf_dma_handle);
13326 				(void) ddi_dma_free_handle(
13327 				    &spx->txlt_buf_dma_handle);
13328 				spx->txlt_buf_dma_handle = NULL;
13329 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13330 				    "sata_dma_buf_setup: numwin failed\n"));
13331 				return (DDI_FAILURE);
13332 			}
13333 			SATADBG2(SATA_DBG_DMA_SETUP,
13334 			    spx->txlt_sata_hba_inst,
13335 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13336 			    spx->txlt_num_dma_win,
13337 			    spx->txlt_curwin_num_dma_cookies);
13338 			spx->txlt_cur_dma_win = 0;
13339 			break;
13340 
13341 		case DDI_DMA_MAPPED:
13342 			/* DMA fully mapped */
13343 			spx->txlt_num_dma_win = 1;
13344 			spx->txlt_cur_dma_win = 0;
13345 			SATADBG1(SATA_DBG_DMA_SETUP,
13346 			    spx->txlt_sata_hba_inst,
13347 			    "sata_dma_buf_setup: windows: 1 "
13348 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13349 			break;
13350 
13351 		default:
13352 			/* DMA mapping failed */
13353 			if (spx->txlt_tmp_buf != NULL) {
13354 				ddi_dma_mem_free(
13355 				    &spx->txlt_tmp_buf_handle);
13356 				spx->txlt_tmp_buf = NULL;
13357 			}
13358 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13359 			spx->txlt_buf_dma_handle = NULL;
13360 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13361 			    "sata_dma_buf_setup: buf dma handle binding "
13362 			    "failed %x\n", rval));
13363 			return (rval);
13364 		}
13365 		spx->txlt_curwin_processed_dma_cookies = 0;
13366 		spx->txlt_dma_cookie_list = NULL;
13367 	} else {
13368 		/*
13369 		 * DMA setup is reused. Check if we need to process more
13370 		 * cookies in current window, or to get next window, if any.
13371 		 */
13372 
13373 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13374 		    spx->txlt_curwin_num_dma_cookies);
13375 
13376 		if (spx->txlt_curwin_processed_dma_cookies ==
13377 		    spx->txlt_curwin_num_dma_cookies) {
13378 			/*
13379 			 * All cookies from current DMA window were processed.
13380 			 * Get next DMA window.
13381 			 */
13382 			spx->txlt_cur_dma_win++;
13383 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13384 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13385 				    spx->txlt_cur_dma_win, &offset, &size,
13386 				    &spx->txlt_dma_cookie,
13387 				    &spx->txlt_curwin_num_dma_cookies);
13388 				spx->txlt_curwin_processed_dma_cookies = 0;
13389 			} else {
13390 				/* No more windows! End of request! */
13391 				/* What to do? - panic for now */
13392 				ASSERT(spx->txlt_cur_dma_win >=
13393 				    spx->txlt_num_dma_win);
13394 
13395 				spx->txlt_curwin_num_dma_cookies = 0;
13396 				spx->txlt_curwin_processed_dma_cookies = 0;
13397 				spx->txlt_sata_pkt->
13398 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13399 				return (DDI_SUCCESS);
13400 			}
13401 		}
13402 	}
13403 	/* There better be at least one DMA cookie outstanding */
13404 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13405 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13406 
13407 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13408 		/* The default cookie slot was used in previous run */
13409 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13410 		spx->txlt_dma_cookie_list = NULL;
13411 		spx->txlt_dma_cookie_list_len = 0;
13412 	}
13413 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13414 		/*
13415 		 * Processing a new DMA window - set-up dma cookies list.
13416 		 * We may reuse previously allocated cookie array if it is
13417 		 * possible.
13418 		 */
13419 		if (spx->txlt_dma_cookie_list != NULL &&
13420 		    spx->txlt_dma_cookie_list_len <
13421 		    spx->txlt_curwin_num_dma_cookies) {
13422 			/*
13423 			 * New DMA window contains more cookies than
13424 			 * the previous one. We need larger cookie list - free
13425 			 * the old one.
13426 			 */
13427 			(void) kmem_free(spx->txlt_dma_cookie_list,
13428 			    spx->txlt_dma_cookie_list_len *
13429 			    sizeof (ddi_dma_cookie_t));
13430 			spx->txlt_dma_cookie_list = NULL;
13431 			spx->txlt_dma_cookie_list_len = 0;
13432 		}
13433 		if (spx->txlt_dma_cookie_list == NULL) {
13434 			/*
13435 			 * Calculate lesser of number of cookies in this
13436 			 * DMA window and number of s/g entries.
13437 			 */
13438 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13439 			req_len = MIN(max_sg_len,
13440 			    spx->txlt_curwin_num_dma_cookies);
13441 
13442 			/* Allocate new dma cookie array if necessary */
13443 			if (req_len == 1) {
13444 				/* Only one cookie - no need for a list */
13445 				spx->txlt_dma_cookie_list =
13446 				    &spx->txlt_dma_cookie;
13447 				spx->txlt_dma_cookie_list_len = 1;
13448 			} else {
13449 				/*
13450 				 * More than one cookie - try to allocate space.
13451 				 */
13452 				spx->txlt_dma_cookie_list = kmem_zalloc(
13453 				    sizeof (ddi_dma_cookie_t) * req_len,
13454 				    callback == NULL_FUNC ? KM_NOSLEEP :
13455 				    KM_SLEEP);
13456 				if (spx->txlt_dma_cookie_list == NULL) {
13457 					SATADBG1(SATA_DBG_DMA_SETUP,
13458 					    spx->txlt_sata_hba_inst,
13459 					    "sata_dma_buf_setup: cookie list "
13460 					    "allocation failed\n", NULL);
13461 					/*
13462 					 * We could not allocate space for
13463 					 * neccessary number of dma cookies in
13464 					 * this window, so we fail this request.
13465 					 * Next invocation would try again to
13466 					 * allocate space for cookie list.
13467 					 * Note:Packet residue was not modified.
13468 					 */
13469 					return (DDI_DMA_NORESOURCES);
13470 				} else {
13471 					spx->txlt_dma_cookie_list_len = req_len;
13472 				}
13473 			}
13474 		}
13475 		/*
13476 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13477 		 * First cookie was already fetched.
13478 		 */
13479 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13480 		cur_txfer_len =
13481 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13482 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13483 		spx->txlt_curwin_processed_dma_cookies++;
13484 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13485 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13486 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13487 			    &spx->txlt_dma_cookie_list[i]);
13488 			cur_txfer_len +=
13489 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13490 			spx->txlt_curwin_processed_dma_cookies++;
13491 			spx->txlt_sata_pkt->
13492 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13493 		}
13494 	} else {
13495 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13496 		    "sata_dma_buf_setup: sliding within DMA window, "
13497 		    "cur cookie %d, total cookies %d\n",
13498 		    spx->txlt_curwin_processed_dma_cookies,
13499 		    spx->txlt_curwin_num_dma_cookies);
13500 
13501 		/*
13502 		 * Not all cookies from the current dma window were used because
13503 		 * of s/g limitation.
13504 		 * There is no need to re-size the list - it was set at
13505 		 * optimal size, or only default entry is used (s/g = 1).
13506 		 */
13507 		if (spx->txlt_dma_cookie_list == NULL) {
13508 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13509 			spx->txlt_dma_cookie_list_len = 1;
13510 		}
13511 		/*
13512 		 * Since we are processing remaining cookies in a DMA window,
13513 		 * there may be less of them than the number of entries in the
13514 		 * current dma cookie list.
13515 		 */
13516 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13517 		    (spx->txlt_curwin_num_dma_cookies -
13518 		    spx->txlt_curwin_processed_dma_cookies));
13519 
13520 		/* Fetch the next batch of cookies */
13521 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13522 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13523 			    &spx->txlt_dma_cookie_list[i]);
13524 			cur_txfer_len +=
13525 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13526 			spx->txlt_sata_pkt->
13527 			    satapkt_cmd.satacmd_num_dma_cookies++;
13528 			spx->txlt_curwin_processed_dma_cookies++;
13529 		}
13530 	}
13531 
13532 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13533 
13534 	/* Point sata_cmd to the cookie list */
13535 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13536 	    &spx->txlt_dma_cookie_list[0];
13537 
13538 	/* Remember number of DMA cookies passed in sata packet */
13539 	spx->txlt_num_dma_cookies =
13540 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13541 
13542 	ASSERT(cur_txfer_len != 0);
13543 	if (cur_txfer_len <= bp->b_bcount)
13544 		spx->txlt_total_residue -= cur_txfer_len;
13545 	else {
13546 		/*
13547 		 * Temporary DMA buffer has been padded by
13548 		 * ddi_dma_mem_alloc()!
13549 		 * This requires special handling, because DMA cookies are
13550 		 * based on the temporary buffer size, not the b_bcount,
13551 		 * and we have extra bytes to transfer - but the packet
13552 		 * residue has to stay correct because we will copy only
13553 		 * the requested number of bytes.
13554 		 */
13555 		spx->txlt_total_residue -= bp->b_bcount;
13556 	}
13557 
13558 	return (DDI_SUCCESS);
13559 }
13560 
13561 /*
13562  * Common routine for releasing DMA resources
13563  */
13564 static void
13565 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13566 {
13567 	if (spx->txlt_buf_dma_handle != NULL) {
13568 		if (spx->txlt_tmp_buf != NULL)  {
13569 			/*
13570 			 * Intermediate DMA buffer was allocated.
13571 			 * Free allocated buffer and associated access handle.
13572 			 */
13573 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13574 			spx->txlt_tmp_buf = NULL;
13575 		}
13576 		/*
13577 		 * Free DMA resources - cookies and handles
13578 		 */
13579 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13580 		if (spx->txlt_dma_cookie_list != NULL) {
13581 			if (spx->txlt_dma_cookie_list !=
13582 			    &spx->txlt_dma_cookie) {
13583 				(void) kmem_free(spx->txlt_dma_cookie_list,
13584 				    spx->txlt_dma_cookie_list_len *
13585 				    sizeof (ddi_dma_cookie_t));
13586 				spx->txlt_dma_cookie_list = NULL;
13587 			}
13588 		}
13589 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13590 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13591 		spx->txlt_buf_dma_handle = NULL;
13592 	}
13593 }
13594 
13595 /*
13596  * Free DMA resources
13597  * Used by the HBA driver to release DMA resources that it does not use.
13598  *
13599  * Returns Void
13600  */
13601 void
13602 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13603 {
13604 	sata_pkt_txlate_t *spx;
13605 
13606 	if (sata_pkt == NULL)
13607 		return;
13608 
13609 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13610 
13611 	sata_common_free_dma_rsrcs(spx);
13612 }
13613 
13614 /*
13615  * Fetch Device Identify data.
13616  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13617  * command to a device and get the device identify data.
13618  * The device_info structure has to be set to device type (for selecting proper
13619  * device identify command).
13620  *
13621  * Returns:
13622  * SATA_SUCCESS if cmd succeeded
13623  * SATA_RETRY if cmd was rejected and could be retried,
13624  * SATA_FAILURE if cmd failed and should not be retried (port error)
13625  *
13626  * Cannot be called in an interrupt context.
13627  */
13628 
13629 static int
13630 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13631     sata_drive_info_t *sdinfo)
13632 {
13633 	struct buf *bp;
13634 	sata_pkt_t *spkt;
13635 	sata_cmd_t *scmd;
13636 	sata_pkt_txlate_t *spx;
13637 	int rval;
13638 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13639 
13640 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13641 	spx->txlt_sata_hba_inst = sata_hba_inst;
13642 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13643 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13644 	if (spkt == NULL) {
13645 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13646 		return (SATA_RETRY); /* may retry later */
13647 	}
13648 	/* address is needed now */
13649 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13650 
13651 	/*
13652 	 * Allocate buffer for Identify Data return data
13653 	 */
13654 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13655 	if (bp == NULL) {
13656 		sata_pkt_free(spx);
13657 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13658 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13659 		    "sata_fetch_device_identify_data: "
13660 		    "cannot allocate buffer for ID"));
13661 		return (SATA_RETRY); /* may retry later */
13662 	}
13663 
13664 	/* Fill sata_pkt */
13665 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13666 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13667 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13668 	/* Synchronous mode, no callback */
13669 	spkt->satapkt_comp = NULL;
13670 	/* Timeout 30s */
13671 	spkt->satapkt_time = sata_default_pkt_time;
13672 
13673 	scmd = &spkt->satapkt_cmd;
13674 	scmd->satacmd_bp = bp;
13675 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13676 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13677 
13678 	/* Build Identify Device cmd in the sata_pkt */
13679 	scmd->satacmd_addr_type = 0;		/* N/A */
13680 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13681 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13682 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13683 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13684 	scmd->satacmd_features_reg = 0;		/* N/A */
13685 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13686 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13687 		/* Identify Packet Device cmd */
13688 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13689 	} else {
13690 		/* Identify Device cmd - mandatory for all other devices */
13691 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13692 	}
13693 
13694 	/* Send pkt to SATA HBA driver */
13695 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13696 
13697 #ifdef SATA_INJECT_FAULTS
13698 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13699 #endif
13700 
13701 	if (rval == SATA_TRAN_ACCEPTED &&
13702 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13703 		if (spx->txlt_buf_dma_handle != NULL) {
13704 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13705 			    DDI_DMA_SYNC_FORKERNEL);
13706 			ASSERT(rval == DDI_SUCCESS);
13707 			if (sata_check_for_dma_error(dip, spx)) {
13708 				ddi_fm_service_impact(dip,
13709 				    DDI_SERVICE_UNAFFECTED);
13710 				rval = SATA_RETRY;
13711 				goto fail;
13712 			}
13713 
13714 		}
13715 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13716 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13717 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13718 			    "SATA disk device at port %d - "
13719 			    "partial Identify Data",
13720 			    sdinfo->satadrv_addr.cport));
13721 			rval = SATA_RETRY; /* may retry later */
13722 			goto fail;
13723 		}
13724 		/* Update sata_drive_info */
13725 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13726 		    sizeof (sata_id_t));
13727 
13728 		sdinfo->satadrv_features_support = 0;
13729 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13730 			/*
13731 			 * Retrieve capacity (disks only) and addressing mode
13732 			 */
13733 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13734 		} else {
13735 			/*
13736 			 * For ATAPI devices one would have to issue
13737 			 * Get Capacity cmd for media capacity. Not here.
13738 			 */
13739 			sdinfo->satadrv_capacity = 0;
13740 			/*
13741 			 * Check what cdb length is supported
13742 			 */
13743 			if ((sdinfo->satadrv_id.ai_config &
13744 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13745 				sdinfo->satadrv_atapi_cdb_len = 16;
13746 			else
13747 				sdinfo->satadrv_atapi_cdb_len = 12;
13748 		}
13749 		/* Setup supported features flags */
13750 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13751 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13752 
13753 		/* Check for SATA GEN and NCQ support */
13754 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13755 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13756 			/* SATA compliance */
13757 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13758 				sdinfo->satadrv_features_support |=
13759 				    SATA_DEV_F_NCQ;
13760 			if (sdinfo->satadrv_id.ai_satacap &
13761 			    (SATA_1_SPEED | SATA_2_SPEED)) {
13762 				if (sdinfo->satadrv_id.ai_satacap &
13763 				    SATA_2_SPEED)
13764 					sdinfo->satadrv_features_support |=
13765 					    SATA_DEV_F_SATA2;
13766 				if (sdinfo->satadrv_id.ai_satacap &
13767 				    SATA_1_SPEED)
13768 					sdinfo->satadrv_features_support |=
13769 					    SATA_DEV_F_SATA1;
13770 			} else {
13771 				sdinfo->satadrv_features_support |=
13772 				    SATA_DEV_F_SATA1;
13773 			}
13774 		}
13775 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13776 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13777 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13778 
13779 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13780 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13781 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13782 			++sdinfo->satadrv_queue_depth;
13783 			/* Adjust according to controller capabilities */
13784 			sdinfo->satadrv_max_queue_depth = MIN(
13785 			    sdinfo->satadrv_queue_depth,
13786 			    SATA_QDEPTH(sata_hba_inst));
13787 			/* Adjust according to global queue depth limit */
13788 			sdinfo->satadrv_max_queue_depth = MIN(
13789 			    sdinfo->satadrv_max_queue_depth,
13790 			    sata_current_max_qdepth);
13791 			if (sdinfo->satadrv_max_queue_depth == 0)
13792 				sdinfo->satadrv_max_queue_depth = 1;
13793 		} else
13794 			sdinfo->satadrv_max_queue_depth = 1;
13795 
13796 		rval = SATA_SUCCESS;
13797 	} else {
13798 		/*
13799 		 * Woops, no Identify Data.
13800 		 */
13801 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13802 			rval = SATA_RETRY; /* may retry later */
13803 		} else if (rval == SATA_TRAN_ACCEPTED) {
13804 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13805 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13806 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13807 			    spkt->satapkt_reason == SATA_PKT_RESET)
13808 				rval = SATA_RETRY; /* may retry later */
13809 			else
13810 				rval = SATA_FAILURE;
13811 		} else {
13812 			rval = SATA_FAILURE;
13813 		}
13814 	}
13815 fail:
13816 	/* Free allocated resources */
13817 	sata_free_local_buffer(spx);
13818 	sata_pkt_free(spx);
13819 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13820 
13821 	return (rval);
13822 }
13823 
13824 
13825 /*
13826  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13827  * UDMA mode is checked first, followed by MWDMA mode.
13828  * set correctly, so this function is setting it to the highest supported level.
13829  * Older SATA spec required that the device supports at least DMA 4 mode and
13830  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13831  * restriction has been removed.
13832  *
13833  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13834  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13835  *
13836  * NOTE: This function should be called only if DMA mode is supported.
13837  */
13838 static int
13839 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13840 {
13841 	sata_pkt_t *spkt;
13842 	sata_cmd_t *scmd;
13843 	sata_pkt_txlate_t *spx;
13844 	int i, mode;
13845 	uint8_t subcmd;
13846 	int rval = SATA_SUCCESS;
13847 
13848 	ASSERT(sdinfo != NULL);
13849 	ASSERT(sata_hba_inst != NULL);
13850 
13851 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13852 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13853 		/* Find highest Ultra DMA mode supported */
13854 		for (mode = 6; mode >= 0; --mode) {
13855 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13856 				break;
13857 		}
13858 #if 0
13859 		/* Left for historical reasons */
13860 		/*
13861 		 * Some initial version of SATA spec indicated that at least
13862 		 * UDMA mode 4 has to be supported. It is not mentioned in
13863 		 * SerialATA 2.6, so this restriction is removed.
13864 		 */
13865 		if (mode < 4)
13866 			return (SATA_FAILURE);
13867 #endif
13868 
13869 		/*
13870 		 * For disk, we're still going to set DMA mode whatever is
13871 		 * selected by default
13872 		 *
13873 		 * We saw an old maxtor sata drive will select Ultra DMA and
13874 		 * Multi-Word DMA simultaneouly by default, which is going
13875 		 * to cause DMA command timed out, so we need to select DMA
13876 		 * mode even when it's already done by default
13877 		 */
13878 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13879 
13880 			/* Find UDMA mode currently selected */
13881 			for (i = 6; i >= 0; --i) {
13882 				if (sdinfo->satadrv_id.ai_ultradma &
13883 				    (1 << (i + 8)))
13884 					break;
13885 			}
13886 			if (i >= mode)
13887 				/* Nothing to do */
13888 				return (SATA_SUCCESS);
13889 		}
13890 
13891 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13892 
13893 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13894 		/* Find highest MultiWord DMA mode supported */
13895 		for (mode = 2; mode >= 0; --mode) {
13896 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13897 				break;
13898 		}
13899 
13900 		/*
13901 		 * For disk, We're still going to set DMA mode whatever is
13902 		 * selected by default
13903 		 *
13904 		 * We saw an old maxtor sata drive will select Ultra DMA and
13905 		 * Multi-Word DMA simultaneouly by default, which is going
13906 		 * to cause DMA command timed out, so we need to select DMA
13907 		 * mode even when it's already done by default
13908 		 */
13909 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13910 
13911 			/* Find highest MultiWord DMA mode selected */
13912 			for (i = 2; i >= 0; --i) {
13913 				if (sdinfo->satadrv_id.ai_dworddma &
13914 				    (1 << (i + 8)))
13915 					break;
13916 			}
13917 			if (i >= mode)
13918 				/* Nothing to do */
13919 				return (SATA_SUCCESS);
13920 		}
13921 
13922 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13923 	} else
13924 		return (SATA_SUCCESS);
13925 
13926 	/*
13927 	 * Set DMA mode via SET FEATURES COMMAND.
13928 	 * Prepare packet for SET FEATURES COMMAND.
13929 	 */
13930 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13931 	spx->txlt_sata_hba_inst = sata_hba_inst;
13932 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
13933 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13934 	if (spkt == NULL) {
13935 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13936 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
13937 		rval = SATA_FAILURE;
13938 		goto done;
13939 	}
13940 	/* Fill sata_pkt */
13941 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13942 	/* Timeout 30s */
13943 	spkt->satapkt_time = sata_default_pkt_time;
13944 	/* Synchronous mode, no callback, interrupts */
13945 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13946 	spkt->satapkt_comp = NULL;
13947 	scmd = &spkt->satapkt_cmd;
13948 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13949 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13950 	scmd->satacmd_addr_type = 0;
13951 	scmd->satacmd_device_reg = 0;
13952 	scmd->satacmd_status_reg = 0;
13953 	scmd->satacmd_error_reg = 0;
13954 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
13955 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
13956 	scmd->satacmd_sec_count_lsb = subcmd | mode;
13957 
13958 	/* Transfer command to HBA */
13959 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
13960 	    spkt) != SATA_TRAN_ACCEPTED ||
13961 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13962 		/* Pkt execution failed */
13963 		rval = SATA_FAILURE;
13964 	}
13965 done:
13966 
13967 	/* Free allocated resources */
13968 	if (spkt != NULL)
13969 		sata_pkt_free(spx);
13970 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
13971 
13972 	return (rval);
13973 }
13974 
13975 
13976 /*
13977  * Set device caching mode.
13978  * One of the following operations should be specified:
13979  * SATAC_SF_ENABLE_READ_AHEAD
13980  * SATAC_SF_DISABLE_READ_AHEAD
13981  * SATAC_SF_ENABLE_WRITE_CACHE
13982  * SATAC_SF_DISABLE_WRITE_CACHE
13983  *
13984  * If operation fails, system log messgage is emitted.
13985  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
13986  * command was sent but did not succeed, and SATA_FAILURE otherwise.
13987  */
13988 
13989 static int
13990 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
13991     int cache_op)
13992 {
13993 	sata_pkt_t *spkt;
13994 	sata_cmd_t *scmd;
13995 	sata_pkt_txlate_t *spx;
13996 	int rval = SATA_SUCCESS;
13997 	int hba_rval;
13998 	char *infop;
13999 
14000 	ASSERT(sdinfo != NULL);
14001 	ASSERT(sata_hba_inst != NULL);
14002 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14003 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14004 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14005 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14006 
14007 
14008 	/* Prepare packet for SET FEATURES COMMAND */
14009 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14010 	spx->txlt_sata_hba_inst = sata_hba_inst;
14011 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14012 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14013 	if (spkt == NULL) {
14014 		rval = SATA_FAILURE;
14015 		goto failure;
14016 	}
14017 	/* Fill sata_pkt */
14018 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14019 	/* Timeout 30s */
14020 	spkt->satapkt_time = sata_default_pkt_time;
14021 	/* Synchronous mode, no callback, interrupts */
14022 	spkt->satapkt_op_mode =
14023 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14024 	spkt->satapkt_comp = NULL;
14025 	scmd = &spkt->satapkt_cmd;
14026 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14027 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14028 	scmd->satacmd_addr_type = 0;
14029 	scmd->satacmd_device_reg = 0;
14030 	scmd->satacmd_status_reg = 0;
14031 	scmd->satacmd_error_reg = 0;
14032 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14033 	scmd->satacmd_features_reg = cache_op;
14034 
14035 	/* Transfer command to HBA */
14036 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14037 	    SATA_DIP(sata_hba_inst), spkt);
14038 
14039 #ifdef SATA_INJECT_FAULTS
14040 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14041 #endif
14042 
14043 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14044 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14045 		/* Pkt execution failed */
14046 		switch (cache_op) {
14047 		case SATAC_SF_ENABLE_READ_AHEAD:
14048 			infop = "enabling read ahead failed";
14049 			break;
14050 		case SATAC_SF_DISABLE_READ_AHEAD:
14051 			infop = "disabling read ahead failed";
14052 			break;
14053 		case SATAC_SF_ENABLE_WRITE_CACHE:
14054 			infop = "enabling write cache failed";
14055 			break;
14056 		case SATAC_SF_DISABLE_WRITE_CACHE:
14057 			infop = "disabling write cache failed";
14058 			break;
14059 		}
14060 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14061 		rval = SATA_RETRY;
14062 	}
14063 failure:
14064 	/* Free allocated resources */
14065 	if (spkt != NULL)
14066 		sata_pkt_free(spx);
14067 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14068 	return (rval);
14069 }
14070 
14071 /*
14072  * Set Removable Media Status Notification (enable/disable)
14073  * state == 0 , disable
14074  * state != 0 , enable
14075  *
14076  * If operation fails, system log messgage is emitted.
14077  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14078  */
14079 
14080 static int
14081 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14082     int state)
14083 {
14084 	sata_pkt_t *spkt;
14085 	sata_cmd_t *scmd;
14086 	sata_pkt_txlate_t *spx;
14087 	int rval = SATA_SUCCESS;
14088 	char *infop;
14089 
14090 	ASSERT(sdinfo != NULL);
14091 	ASSERT(sata_hba_inst != NULL);
14092 
14093 	/* Prepare packet for SET FEATURES COMMAND */
14094 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14095 	spx->txlt_sata_hba_inst = sata_hba_inst;
14096 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14097 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14098 	if (spkt == NULL) {
14099 		rval = SATA_FAILURE;
14100 		goto failure;
14101 	}
14102 	/* Fill sata_pkt */
14103 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14104 	/* Timeout 30s */
14105 	spkt->satapkt_time = sata_default_pkt_time;
14106 	/* Synchronous mode, no callback, interrupts */
14107 	spkt->satapkt_op_mode =
14108 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14109 	spkt->satapkt_comp = NULL;
14110 	scmd = &spkt->satapkt_cmd;
14111 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14112 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14113 	scmd->satacmd_addr_type = 0;
14114 	scmd->satacmd_device_reg = 0;
14115 	scmd->satacmd_status_reg = 0;
14116 	scmd->satacmd_error_reg = 0;
14117 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14118 	if (state == 0)
14119 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14120 	else
14121 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14122 
14123 	/* Transfer command to HBA */
14124 	if (((*SATA_START_FUNC(sata_hba_inst))(
14125 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14126 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14127 		/* Pkt execution failed */
14128 		if (state == 0)
14129 			infop = "disabling Removable Media Status "
14130 			    "Notification failed";
14131 		else
14132 			infop = "enabling Removable Media Status "
14133 			    "Notification failed";
14134 
14135 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14136 		rval = SATA_FAILURE;
14137 	}
14138 failure:
14139 	/* Free allocated resources */
14140 	if (spkt != NULL)
14141 		sata_pkt_free(spx);
14142 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14143 	return (rval);
14144 }
14145 
14146 
14147 /*
14148  * Update state and copy port ss* values from passed sata_device structure.
14149  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14150  * configuration struct.
14151  *
14152  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14153  * regardless of the state in device argument.
14154  *
14155  * Port mutex should be held while calling this function.
14156  */
14157 static void
14158 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14159     sata_device_t *sata_device)
14160 {
14161 	sata_cport_info_t *cportinfo;
14162 
14163 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14164 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14165 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14166 		    sata_device->satadev_addr.cport)
14167 			return;
14168 
14169 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14170 		    sata_device->satadev_addr.cport);
14171 
14172 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14173 		cportinfo->cport_scr = sata_device->satadev_scr;
14174 
14175 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14176 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14177 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14178 		cportinfo->cport_state |=
14179 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14180 	}
14181 }
14182 
14183 void
14184 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14185     sata_device_t *sata_device)
14186 {
14187 	sata_pmport_info_t *pmportinfo;
14188 
14189 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14190 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14191 	    SATA_NUM_PMPORTS(sata_hba_inst,
14192 	    sata_device->satadev_addr.cport) <
14193 	    sata_device->satadev_addr.pmport) {
14194 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14195 		    "sata_update_port_info: error address %p.",
14196 		    &sata_device->satadev_addr);
14197 		return;
14198 	}
14199 
14200 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14201 	    sata_device->satadev_addr.cport,
14202 	    sata_device->satadev_addr.pmport);
14203 
14204 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14205 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14206 
14207 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14208 	pmportinfo->pmport_state &=
14209 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14210 	pmportinfo->pmport_state |=
14211 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14212 }
14213 
14214 /*
14215  * Extract SATA port specification from an IOCTL argument.
14216  *
14217  * This function return the port the user land send us as is, unless it
14218  * cannot retrieve port spec, then -1 is returned.
14219  *
14220  * Support port multiplier.
14221  */
14222 static int32_t
14223 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14224 {
14225 	int32_t port;
14226 
14227 	/* Extract port number from nvpair in dca structure  */
14228 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14229 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14230 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14231 		    port));
14232 		port = -1;
14233 	}
14234 
14235 	return (port);
14236 }
14237 
14238 /*
14239  * Get dev_info_t pointer to the device node pointed to by port argument.
14240  * NOTE: target argument is a value used in ioctls to identify
14241  * the AP - it is not a sata_address.
14242  * It is a combination of cport, pmport and address qualifier, encodded same
14243  * way as a scsi target number.
14244  * At this moment it carries only cport number.
14245  *
14246  * PMult hotplug is supported now.
14247  *
14248  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14249  */
14250 
14251 static dev_info_t *
14252 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14253 {
14254 	dev_info_t	*cdip = NULL;
14255 	int		target, tgt;
14256 	int 		circ;
14257 	uint8_t		qual;
14258 
14259 	sata_hba_inst_t	*sata_hba_inst;
14260 	scsi_hba_tran_t *scsi_hba_tran;
14261 
14262 	/* Get target id */
14263 	scsi_hba_tran = ddi_get_driver_private(dip);
14264 	if (scsi_hba_tran == NULL)
14265 		return (NULL);
14266 
14267 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14268 
14269 	if (sata_hba_inst == NULL)
14270 		return (NULL);
14271 
14272 	/* Identify a port-mult by cport_info.cport_dev_type */
14273 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14274 		qual = SATA_ADDR_DPMPORT;
14275 	else
14276 		qual = SATA_ADDR_DCPORT;
14277 
14278 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14279 
14280 	/* Retrieve target dip */
14281 	ndi_devi_enter(dip, &circ);
14282 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14283 		dev_info_t *next = ddi_get_next_sibling(cdip);
14284 
14285 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14286 		    DDI_PROP_DONTPASS, "target", -1);
14287 		if (tgt == -1) {
14288 			/*
14289 			 * This is actually an error condition, but not
14290 			 * a fatal one. Just continue the search.
14291 			 */
14292 			cdip = next;
14293 			continue;
14294 		}
14295 
14296 		if (tgt == target)
14297 			break;
14298 
14299 		cdip = next;
14300 	}
14301 	ndi_devi_exit(dip, circ);
14302 
14303 	return (cdip);
14304 }
14305 
14306 /*
14307  * Get dev_info_t pointer to the device node pointed to by port argument.
14308  * NOTE: target argument is a value used in ioctls to identify
14309  * the AP - it is not a sata_address.
14310  * It is a combination of cport, pmport and address qualifier, encoded same
14311  * way as a scsi target number.
14312  *
14313  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14314  */
14315 
14316 static dev_info_t *
14317 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14318 {
14319 	dev_info_t	*cdip = NULL;
14320 	int		target, tgt;
14321 	int 		circ;
14322 
14323 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14324 
14325 	ndi_devi_enter(dip, &circ);
14326 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14327 		dev_info_t *next = ddi_get_next_sibling(cdip);
14328 
14329 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14330 		    DDI_PROP_DONTPASS, "target", -1);
14331 		if (tgt == -1) {
14332 			/*
14333 			 * This is actually an error condition, but not
14334 			 * a fatal one. Just continue the search.
14335 			 */
14336 			cdip = next;
14337 			continue;
14338 		}
14339 
14340 		if (tgt == target)
14341 			break;
14342 
14343 		cdip = next;
14344 	}
14345 	ndi_devi_exit(dip, circ);
14346 
14347 	return (cdip);
14348 }
14349 
14350 /*
14351  * Process sata port disconnect request.
14352  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14353  * before this request. Nevertheless, if a device is still configured,
14354  * we need to attempt to offline and unconfigure device.
14355  * Regardless of the unconfigure operation results the port is marked as
14356  * deactivated and no access to the attached device is possible.
14357  * If the target node remains because unconfigure operation failed, its state
14358  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14359  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14360  * the device and remove old target node.
14361  *
14362  * This function invokes sata_hba_inst->satahba_tran->
14363  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14364  * If successful, the device structure (if any) attached to the specified port
14365  * is removed and state of the port marked appropriately.
14366  * Failure of the port_deactivate may keep port in the physically active state,
14367  * or may fail the port.
14368  *
14369  * NOTE: Port multiplier is supported.
14370  */
14371 
14372 static int
14373 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14374     sata_device_t *sata_device)
14375 {
14376 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14377 	sata_cport_info_t *cportinfo = NULL;
14378 	sata_pmport_info_t *pmportinfo = NULL;
14379 	sata_pmult_info_t *pmultinfo = NULL;
14380 	sata_device_t subsdevice;
14381 	int cport, pmport, qual;
14382 	int rval = SATA_SUCCESS;
14383 	int npmport = 0;
14384 	int rv = 0;
14385 
14386 	cport = sata_device->satadev_addr.cport;
14387 	pmport = sata_device->satadev_addr.pmport;
14388 	qual = sata_device->satadev_addr.qual;
14389 
14390 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14391 	if (qual == SATA_ADDR_DCPORT)
14392 		qual = SATA_ADDR_CPORT;
14393 	else
14394 		qual = SATA_ADDR_PMPORT;
14395 
14396 	/*
14397 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14398 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14399 	 * Do the sanity check.
14400 	 */
14401 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14402 		/* No physical port deactivation supported. */
14403 		return (EINVAL);
14404 	}
14405 
14406 	/* Check the current state of the port */
14407 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14408 	    (SATA_DIP(sata_hba_inst), sata_device);
14409 
14410 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14411 
14412 	/*
14413 	 * Processing port mulitiplier
14414 	 */
14415 	if (qual == SATA_ADDR_CPORT &&
14416 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14417 		mutex_enter(&cportinfo->cport_mutex);
14418 
14419 		/* Check controller port status */
14420 		sata_update_port_info(sata_hba_inst, sata_device);
14421 		if (rval != SATA_SUCCESS ||
14422 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14423 			/*
14424 			 * Device port status is unknown or it is in failed
14425 			 * state
14426 			 */
14427 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14428 			    SATA_PSTATE_FAILED;
14429 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14430 			    "sata_hba_ioctl: connect: failed to deactivate "
14431 			    "SATA port %d", cport);
14432 			mutex_exit(&cportinfo->cport_mutex);
14433 			return (EIO);
14434 		}
14435 
14436 		/* Disconnect all sub-devices. */
14437 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14438 		if (pmultinfo != NULL) {
14439 
14440 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14441 			    sata_hba_inst, cport); npmport ++) {
14442 				subsdinfo = SATA_PMPORT_DRV_INFO(
14443 				    sata_hba_inst, cport, npmport);
14444 				if (subsdinfo == NULL)
14445 					continue;
14446 
14447 				subsdevice.satadev_addr = subsdinfo->
14448 				    satadrv_addr;
14449 
14450 				mutex_exit(&cportinfo->cport_mutex);
14451 				if (sata_ioctl_disconnect(sata_hba_inst,
14452 				    &subsdevice) == SATA_SUCCESS) {
14453 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14454 					"[Remove] device at port %d:%d "
14455 					"successfully.", cport, npmport);
14456 				}
14457 				mutex_enter(&cportinfo->cport_mutex);
14458 			}
14459 		}
14460 
14461 		/* Disconnect the port multiplier */
14462 		cportinfo->cport_state &= ~SATA_STATE_READY;
14463 		mutex_exit(&cportinfo->cport_mutex);
14464 
14465 		sata_device->satadev_addr.qual = qual;
14466 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14467 		    (SATA_DIP(sata_hba_inst), sata_device);
14468 
14469 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14470 		    SE_NO_HINT);
14471 
14472 		mutex_enter(&cportinfo->cport_mutex);
14473 		sata_update_port_info(sata_hba_inst, sata_device);
14474 		if (rval != SATA_SUCCESS &&
14475 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14476 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14477 			rv = EIO;
14478 		} else {
14479 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14480 		}
14481 		mutex_exit(&cportinfo->cport_mutex);
14482 
14483 		return (rv);
14484 	}
14485 
14486 	/*
14487 	 * Process non-port-multiplier device - it could be a drive connected
14488 	 * to a port multiplier port or a controller port.
14489 	 */
14490 	if (qual == SATA_ADDR_PMPORT) {
14491 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14492 		mutex_enter(&pmportinfo->pmport_mutex);
14493 		sata_update_pmport_info(sata_hba_inst, sata_device);
14494 		if (rval != SATA_SUCCESS ||
14495 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14496 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14497 			    SATA_PSTATE_FAILED;
14498 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14499 			    "sata_hba_ioctl: connect: failed to deactivate "
14500 			    "SATA port %d:%d", cport, pmport);
14501 			mutex_exit(&pmportinfo->pmport_mutex);
14502 			return (EIO);
14503 		}
14504 
14505 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14506 			sdinfo = pmportinfo->pmport_sata_drive;
14507 			ASSERT(sdinfo != NULL);
14508 		}
14509 
14510 		/*
14511 		 * Set port's dev_state to not ready - this will disable
14512 		 * an access to a potentially attached device.
14513 		 */
14514 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14515 
14516 		/* Remove and release sata_drive info structure. */
14517 		if (sdinfo != NULL) {
14518 			if ((sdinfo->satadrv_type &
14519 			    SATA_VALID_DEV_TYPE) != 0) {
14520 				/*
14521 				 * If a target node exists, try to offline
14522 				 * a device and remove target node.
14523 				 */
14524 				mutex_exit(&pmportinfo->pmport_mutex);
14525 				(void) sata_offline_device(sata_hba_inst,
14526 				    sata_device, sdinfo);
14527 				mutex_enter(&pmportinfo->pmport_mutex);
14528 			}
14529 
14530 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14531 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14532 			(void) kmem_free((void *)sdinfo,
14533 			    sizeof (sata_drive_info_t));
14534 		}
14535 		mutex_exit(&pmportinfo->pmport_mutex);
14536 
14537 	} else if (qual == SATA_ADDR_CPORT) {
14538 		mutex_enter(&cportinfo->cport_mutex);
14539 		sata_update_port_info(sata_hba_inst, sata_device);
14540 		if (rval != SATA_SUCCESS ||
14541 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14542 			/*
14543 			 * Device port status is unknown or it is in failed
14544 			 * state
14545 			 */
14546 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14547 			    SATA_PSTATE_FAILED;
14548 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14549 			    "sata_hba_ioctl: connect: failed to deactivate "
14550 			    "SATA port %d", cport);
14551 			mutex_exit(&cportinfo->cport_mutex);
14552 			return (EIO);
14553 		}
14554 
14555 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14556 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14557 			ASSERT(pmultinfo != NULL);
14558 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14559 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14560 			ASSERT(sdinfo != NULL);
14561 		}
14562 		cportinfo->cport_state &= ~SATA_STATE_READY;
14563 
14564 		if (sdinfo != NULL) {
14565 			if ((sdinfo->satadrv_type &
14566 			    SATA_VALID_DEV_TYPE) != 0) {
14567 				/*
14568 				 * If a target node exists, try to offline
14569 				 * a device and remove target node.
14570 				 */
14571 				mutex_exit(&cportinfo->cport_mutex);
14572 				(void) sata_offline_device(sata_hba_inst,
14573 				    sata_device, sdinfo);
14574 				mutex_enter(&cportinfo->cport_mutex);
14575 			}
14576 
14577 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14578 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14579 			(void) kmem_free((void *)sdinfo,
14580 			    sizeof (sata_drive_info_t));
14581 		}
14582 		mutex_exit(&cportinfo->cport_mutex);
14583 	}
14584 
14585 	/* Just ask HBA driver to deactivate port */
14586 	sata_device->satadev_addr.qual = qual;
14587 
14588 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14589 	    (SATA_DIP(sata_hba_inst), sata_device);
14590 
14591 	/*
14592 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14593 	 * without the hint (to force listener to investivate the state).
14594 	 */
14595 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14596 	    SE_NO_HINT);
14597 
14598 	if (qual == SATA_ADDR_PMPORT) {
14599 		mutex_enter(&pmportinfo->pmport_mutex);
14600 		sata_update_pmport_info(sata_hba_inst, sata_device);
14601 
14602 		if (rval != SATA_SUCCESS &&
14603 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14604 			/*
14605 			 * Port deactivation failure - do not change port
14606 			 * state unless the state returned by HBA indicates a
14607 			 * port failure.
14608 			 *
14609 			 * NOTE: device structures were released, so devices
14610 			 * now are invisible! Port reset is needed to
14611 			 * re-enumerate devices.
14612 			 */
14613 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14614 			rv = EIO;
14615 		} else {
14616 			/*
14617 			 * Deactivation succeded. From now on the sata framework
14618 			 * will not care what is happening to the device, until
14619 			 * the port is activated again.
14620 			 */
14621 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14622 		}
14623 		mutex_exit(&pmportinfo->pmport_mutex);
14624 	} else if (qual == SATA_ADDR_CPORT) {
14625 		mutex_enter(&cportinfo->cport_mutex);
14626 		sata_update_port_info(sata_hba_inst, sata_device);
14627 
14628 		if (rval != SATA_SUCCESS &&
14629 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14630 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14631 			rv = EIO;
14632 		} else {
14633 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14634 		}
14635 		mutex_exit(&cportinfo->cport_mutex);
14636 	}
14637 
14638 	return (rv);
14639 }
14640 
14641 
14642 
14643 /*
14644  * Process sata port connect request
14645  * The sata cfgadm pluging will invoke this operation only if port was found
14646  * in the disconnect state (failed state is also treated as the disconnected
14647  * state).
14648  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14649  * sata_tran_hotplug_ops->sata_tran_port_activate().
14650  * If successful and a device is found attached to the port,
14651  * the initialization sequence is executed to attach a device structure to
14652  * a port structure. The state of the port and a device would be set
14653  * appropriately.
14654  * The device is not set in configured state (system-wise) by this operation.
14655  *
14656  * Note, that activating the port may generate link events,
14657  * so it is important that following processing and the
14658  * event processing does not interfere with each other!
14659  *
14660  * This operation may remove port failed state and will
14661  * try to make port active and in good standing.
14662  *
14663  * NOTE: Port multiplier is supported.
14664  */
14665 
14666 static int
14667 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14668     sata_device_t *sata_device)
14669 {
14670 	sata_pmport_info_t	*pmportinfo = NULL;
14671 	uint8_t cport, pmport, qual;
14672 	int rv = 0;
14673 
14674 	cport = sata_device->satadev_addr.cport;
14675 	pmport = sata_device->satadev_addr.pmport;
14676 	qual = sata_device->satadev_addr.qual;
14677 
14678 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14679 	if (qual == SATA_ADDR_DCPORT)
14680 		qual = SATA_ADDR_CPORT;
14681 	else
14682 		qual = SATA_ADDR_PMPORT;
14683 
14684 	if (qual == SATA_ADDR_PMPORT)
14685 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14686 
14687 	/*
14688 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14689 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14690 	 * Perform sanity check now.
14691 	 */
14692 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14693 		/* No physical port activation supported. */
14694 		return (EINVAL);
14695 	}
14696 
14697 	/* Just ask HBA driver to activate port */
14698 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14699 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14700 		/*
14701 		 * Port activation failure.
14702 		 */
14703 		if (qual == SATA_ADDR_CPORT) {
14704 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14705 			    cport)->cport_mutex);
14706 			sata_update_port_info(sata_hba_inst, sata_device);
14707 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14708 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14709 				    SATA_PSTATE_FAILED;
14710 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14711 				    "sata_hba_ioctl: connect: failed to "
14712 				    "activate SATA port %d", cport);
14713 			}
14714 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14715 			    cport)->cport_mutex);
14716 		} else { /* port multiplier device port */
14717 			mutex_enter(&pmportinfo->pmport_mutex);
14718 			sata_update_pmport_info(sata_hba_inst, sata_device);
14719 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14720 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14721 				    pmport) = SATA_PSTATE_FAILED;
14722 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14723 				    "sata_hba_ioctl: connect: failed to "
14724 				    "activate SATA port %d:%d", cport, pmport);
14725 			}
14726 			mutex_exit(&pmportinfo->pmport_mutex);
14727 		}
14728 		return (EIO);
14729 	}
14730 
14731 	/* Virgin port state - will be updated by the port re-probe. */
14732 	if (qual == SATA_ADDR_CPORT) {
14733 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14734 		    cport)->cport_mutex);
14735 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14736 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14737 		    cport)->cport_mutex);
14738 	} else { /* port multiplier device port */
14739 		mutex_enter(&pmportinfo->pmport_mutex);
14740 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14741 		mutex_exit(&pmportinfo->pmport_mutex);
14742 	}
14743 
14744 	/*
14745 	 * Probe the port to find its state and attached device.
14746 	 */
14747 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14748 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14749 		rv = EIO;
14750 
14751 	/*
14752 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14753 	 * without the hint
14754 	 */
14755 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14756 	    SE_NO_HINT);
14757 
14758 	/*
14759 	 * If there is a device attached to the port, emit
14760 	 * a message.
14761 	 */
14762 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14763 
14764 		if (qual == SATA_ADDR_CPORT) {
14765 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14766 				sata_log(sata_hba_inst, CE_WARN,
14767 				    "SATA port multiplier detected "
14768 				    "at port %d", cport);
14769 			} else {
14770 				sata_log(sata_hba_inst, CE_WARN,
14771 				    "SATA device detected at port %d", cport);
14772 				if (sata_device->satadev_type ==
14773 				    SATA_DTYPE_UNKNOWN) {
14774 				/*
14775 				 * A device was not successfully identified
14776 				 */
14777 				sata_log(sata_hba_inst, CE_WARN,
14778 				    "Could not identify SATA "
14779 				    "device at port %d", cport);
14780 				}
14781 			}
14782 		} else { /* port multiplier device port */
14783 			sata_log(sata_hba_inst, CE_WARN,
14784 			    "SATA device detected at port %d:%d",
14785 			    cport, pmport);
14786 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14787 				/*
14788 				 * A device was not successfully identified
14789 				 */
14790 				sata_log(sata_hba_inst, CE_WARN,
14791 				    "Could not identify SATA "
14792 				    "device at port %d:%d", cport, pmport);
14793 			}
14794 		}
14795 	}
14796 
14797 	return (rv);
14798 }
14799 
14800 
14801 /*
14802  * Process sata device unconfigure request.
14803  * The unconfigure operation uses generic nexus operation to
14804  * offline a device. It leaves a target device node attached.
14805  * and obviously sata_drive_info attached as well, because
14806  * from the hardware point of view nothing has changed.
14807  */
14808 static int
14809 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14810     sata_device_t *sata_device)
14811 {
14812 	int rv = 0;
14813 	dev_info_t *tdip;
14814 
14815 	/* We are addressing attached device, not a port */
14816 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14817 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14818 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14819 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14820 
14821 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14822 	    &sata_device->satadev_addr)) != NULL) {
14823 
14824 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14825 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14826 			    "sata_hba_ioctl: unconfigure: "
14827 			    "failed to unconfigure device at SATA port %d:%d",
14828 			    sata_device->satadev_addr.cport,
14829 			    sata_device->satadev_addr.pmport));
14830 			rv = EIO;
14831 		}
14832 		/*
14833 		 * The target node devi_state should be marked with
14834 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14835 		 * This would be the indication for cfgadm that
14836 		 * the AP node occupant state is 'unconfigured'.
14837 		 */
14838 
14839 	} else {
14840 		/*
14841 		 * This would indicate a failure on the part of cfgadm
14842 		 * to detect correct state of the node prior to this
14843 		 * call - one cannot unconfigure non-existing device.
14844 		 */
14845 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14846 		    "sata_hba_ioctl: unconfigure: "
14847 		    "attempt to unconfigure non-existing device "
14848 		    "at SATA port %d:%d",
14849 		    sata_device->satadev_addr.cport,
14850 		    sata_device->satadev_addr.pmport));
14851 		rv = ENXIO;
14852 	}
14853 	return (rv);
14854 }
14855 
14856 /*
14857  * Process sata device configure request
14858  * If port is in a failed state, operation is aborted - one has to use
14859  * an explicit connect or port activate request to try to get a port into
14860  * non-failed mode. Port reset wil also work in such situation.
14861  * If the port is in disconnected (shutdown) state, the connect operation is
14862  * attempted prior to any other action.
14863  * When port is in the active state, there is a device attached and the target
14864  * node exists, a device was most likely offlined.
14865  * If target node does not exist, a new target node is created. In both cases
14866  * an attempt is made to online (configure) the device.
14867  *
14868  * NOTE: Port multiplier is supported.
14869  */
14870 static int
14871 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14872     sata_device_t *sata_device)
14873 {
14874 	int cport, pmport, qual;
14875 	int rval;
14876 	boolean_t target = B_TRUE;
14877 	sata_cport_info_t *cportinfo;
14878 	sata_pmport_info_t *pmportinfo = NULL;
14879 	dev_info_t *tdip;
14880 	sata_drive_info_t *sdinfo;
14881 
14882 	cport = sata_device->satadev_addr.cport;
14883 	pmport = sata_device->satadev_addr.pmport;
14884 	qual = sata_device->satadev_addr.qual;
14885 
14886 	/* Get current port state */
14887 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14888 	    (SATA_DIP(sata_hba_inst), sata_device);
14889 
14890 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14891 	if (qual == SATA_ADDR_DPMPORT) {
14892 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14893 		mutex_enter(&pmportinfo->pmport_mutex);
14894 		sata_update_pmport_info(sata_hba_inst, sata_device);
14895 		if (rval != SATA_SUCCESS ||
14896 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14897 			/*
14898 			 * Obviously, device on a failed port is not visible
14899 			 */
14900 			mutex_exit(&pmportinfo->pmport_mutex);
14901 			return (ENXIO);
14902 		}
14903 		mutex_exit(&pmportinfo->pmport_mutex);
14904 	} else {
14905 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14906 		    cport)->cport_mutex);
14907 		sata_update_port_info(sata_hba_inst, sata_device);
14908 		if (rval != SATA_SUCCESS ||
14909 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14910 			/*
14911 			 * Obviously, device on a failed port is not visible
14912 			 */
14913 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14914 			    cport)->cport_mutex);
14915 			return (ENXIO);
14916 		}
14917 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14918 		    cport)->cport_mutex);
14919 	}
14920 
14921 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14922 		/* need to activate port */
14923 		target = B_FALSE;
14924 
14925 		/* Sanity check */
14926 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14927 			return (ENXIO);
14928 
14929 		/* Just let HBA driver to activate port */
14930 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14931 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14932 			/*
14933 			 * Port activation failure - do not change port state
14934 			 * unless the state returned by HBA indicates a port
14935 			 * failure.
14936 			 */
14937 			if (qual == SATA_ADDR_DPMPORT) {
14938 				mutex_enter(&pmportinfo->pmport_mutex);
14939 				sata_update_pmport_info(sata_hba_inst,
14940 				    sata_device);
14941 				if (sata_device->satadev_state &
14942 				    SATA_PSTATE_FAILED)
14943 					pmportinfo->pmport_state =
14944 					    SATA_PSTATE_FAILED;
14945 				mutex_exit(&pmportinfo->pmport_mutex);
14946 			} else {
14947 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14948 				    cport)->cport_mutex);
14949 				sata_update_port_info(sata_hba_inst,
14950 				    sata_device);
14951 				if (sata_device->satadev_state &
14952 				    SATA_PSTATE_FAILED)
14953 					cportinfo->cport_state =
14954 					    SATA_PSTATE_FAILED;
14955 				mutex_exit(&SATA_CPORT_INFO(
14956 				    sata_hba_inst, cport)->cport_mutex);
14957 			}
14958 		}
14959 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14960 		    "sata_hba_ioctl: configure: "
14961 		    "failed to activate SATA port %d:%d",
14962 		    cport, pmport));
14963 		return (EIO);
14964 	}
14965 	/*
14966 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14967 	 * without the hint.
14968 	 */
14969 	sata_gen_sysevent(sata_hba_inst,
14970 	    &sata_device->satadev_addr, SE_NO_HINT);
14971 
14972 	/* Virgin port state */
14973 	if (qual == SATA_ADDR_DPMPORT) {
14974 		mutex_enter(&pmportinfo->pmport_mutex);
14975 		pmportinfo->pmport_state = 0;
14976 		mutex_exit(&pmportinfo->pmport_mutex);
14977 	} else {
14978 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14979 		    cport)-> cport_mutex);
14980 		cportinfo->cport_state = 0;
14981 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14982 		    cport)->cport_mutex);
14983 	}
14984 	/*
14985 	 * Always reprobe port, to get current device info.
14986 	 */
14987 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14988 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14989 		return (EIO);
14990 
14991 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
14992 		if (qual == SATA_ADDR_DPMPORT) {
14993 			/*
14994 			 * That's the transition from "inactive" port
14995 			 * to active one with device attached.
14996 			 */
14997 			sata_log(sata_hba_inst, CE_WARN,
14998 			    "SATA device detected at port %d:%d",
14999 			    cport, pmport);
15000 		} else {
15001 			/*
15002 			 * When PM is attached to the cport and cport is
15003 			 * activated, every PM device port needs to be reprobed.
15004 			 * We need to emit message for all devices detected
15005 			 * at port multiplier's device ports.
15006 			 * Add such code here.
15007 			 * For now, just inform about device attached to
15008 			 * cport.
15009 			 */
15010 			sata_log(sata_hba_inst, CE_WARN,
15011 			    "SATA device detected at port %d", cport);
15012 		}
15013 	}
15014 
15015 	/*
15016 	 * This is where real configuration operation starts.
15017 	 *
15018 	 * When PM is attached to the cport and cport is activated,
15019 	 * devices attached PM device ports may have to be configured
15020 	 * explicitly. This may change when port multiplier is supported.
15021 	 * For now, configure only disks and other valid target devices.
15022 	 */
15023 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15024 		if (qual == SATA_ADDR_DCPORT) {
15025 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15026 				/*
15027 				 * A device was not successfully identified
15028 				 */
15029 				sata_log(sata_hba_inst, CE_WARN,
15030 				    "Could not identify SATA "
15031 				    "device at port %d", cport);
15032 			}
15033 		} else { /* port multiplier device port */
15034 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15035 				/*
15036 				 * A device was not successfully identified
15037 				 */
15038 				sata_log(sata_hba_inst, CE_WARN,
15039 				    "Could not identify SATA "
15040 				    "device at port %d:%d", cport, pmport);
15041 			}
15042 		}
15043 		return (ENXIO);		/* No device to configure */
15044 	}
15045 
15046 	/*
15047 	 * Here we may have a device in reset condition,
15048 	 * but because we are just configuring it, there is
15049 	 * no need to process the reset other than just
15050 	 * to clear device reset condition in the HBA driver.
15051 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15052 	 * cause a first command sent the HBA driver with the request
15053 	 * to clear device reset condition.
15054 	 */
15055 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15056 	if (qual == SATA_ADDR_DPMPORT)
15057 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15058 	else
15059 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15060 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15061 	if (sdinfo == NULL) {
15062 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15063 		return (ENXIO);
15064 	}
15065 	if (sdinfo->satadrv_event_flags &
15066 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15067 		sdinfo->satadrv_event_flags = 0;
15068 	}
15069 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15070 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15071 
15072 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15073 	    &sata_device->satadev_addr)) != NULL) {
15074 		/*
15075 		 * Target node exists. Verify, that it belongs
15076 		 * to existing, attached device and not to
15077 		 * a removed device.
15078 		 */
15079 		if (sata_check_device_removed(tdip) == B_TRUE) {
15080 			if (qual == SATA_ADDR_DPMPORT)
15081 				sata_log(sata_hba_inst, CE_WARN,
15082 				    "SATA device at port %d cannot be "
15083 				    "configured. "
15084 				    "Application(s) accessing "
15085 				    "previously attached device "
15086 				    "have to release it before newly "
15087 				    "inserted device can be made accessible.",
15088 				    cport);
15089 			else
15090 				sata_log(sata_hba_inst, CE_WARN,
15091 				    "SATA device at port %d:%d cannot be"
15092 				    "configured. "
15093 				    "Application(s) accessing "
15094 				    "previously attached device "
15095 				    "have to release it before newly "
15096 				    "inserted device can be made accessible.",
15097 				    cport, pmport);
15098 			return (EIO);
15099 		}
15100 		/*
15101 		 * Device was not removed and re-inserted.
15102 		 * Try to online it.
15103 		 */
15104 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15105 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15106 			    "sata_hba_ioctl: configure: "
15107 			    "onlining device at SATA port "
15108 			    "%d:%d failed", cport, pmport));
15109 			return (EIO);
15110 		}
15111 
15112 		if (qual == SATA_ADDR_DPMPORT) {
15113 			mutex_enter(&pmportinfo->pmport_mutex);
15114 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15115 			mutex_exit(&pmportinfo->pmport_mutex);
15116 		} else {
15117 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15118 			    cport)->cport_mutex);
15119 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15120 			mutex_exit(&SATA_CPORT_INFO(
15121 			    sata_hba_inst, cport)->cport_mutex);
15122 		}
15123 	} else {
15124 		/*
15125 		 * No target node - need to create a new target node.
15126 		 */
15127 		if (qual == SATA_ADDR_DPMPORT) {
15128 			mutex_enter(&pmportinfo->pmport_mutex);
15129 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15130 			mutex_exit(&pmportinfo->pmport_mutex);
15131 		} else {
15132 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15133 			    cport_mutex);
15134 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15135 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15136 			    cport_mutex);
15137 		}
15138 
15139 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15140 		    sata_hba_inst, &sata_device->satadev_addr);
15141 		if (tdip == NULL) {
15142 			/* Configure operation failed */
15143 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15144 			    "sata_hba_ioctl: configure: "
15145 			    "configuring SATA device at port %d:%d "
15146 			    "failed", cport, pmport));
15147 			return (EIO);
15148 		}
15149 	}
15150 	return (0);
15151 }
15152 
15153 
15154 /*
15155  * Process ioctl deactivate port request.
15156  * Arbitrarily unconfigure attached device, if any.
15157  * Even if the unconfigure fails, proceed with the
15158  * port deactivation.
15159  *
15160  * NOTE: Port Multiplier is supported now.
15161  */
15162 
15163 static int
15164 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15165     sata_device_t *sata_device)
15166 {
15167 	int cport, pmport, qual;
15168 	int rval, rv = 0;
15169 	int npmport;
15170 	sata_cport_info_t *cportinfo;
15171 	sata_pmport_info_t *pmportinfo;
15172 	sata_pmult_info_t *pmultinfo;
15173 	dev_info_t *tdip;
15174 	sata_drive_info_t *sdinfo = NULL;
15175 	sata_device_t subsdevice;
15176 
15177 	/* Sanity check */
15178 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15179 		return (ENOTSUP);
15180 
15181 	cport = sata_device->satadev_addr.cport;
15182 	pmport = sata_device->satadev_addr.pmport;
15183 	qual = sata_device->satadev_addr.qual;
15184 
15185 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15186 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15187 	if (qual == SATA_ADDR_DCPORT)
15188 		qual = SATA_ADDR_CPORT;
15189 	else
15190 		qual = SATA_ADDR_PMPORT;
15191 
15192 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15193 	if (qual == SATA_ADDR_PMPORT)
15194 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15195 
15196 	/*
15197 	 * Processing port multiplier
15198 	 */
15199 	if (qual == SATA_ADDR_CPORT &&
15200 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15201 		mutex_enter(&cportinfo->cport_mutex);
15202 
15203 		/* Deactivate all sub-deices */
15204 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15205 		if (pmultinfo != NULL) {
15206 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15207 			    sata_hba_inst, cport); npmport++) {
15208 
15209 				subsdevice.satadev_addr.cport = cport;
15210 				subsdevice.satadev_addr.pmport =
15211 				    (uint8_t)npmport;
15212 				subsdevice.satadev_addr.qual =
15213 				    SATA_ADDR_DPMPORT;
15214 
15215 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15216 				    "sata_hba_ioctl: deactivate: trying to "
15217 				    "deactivate SATA port %d:%d",
15218 				    cport, npmport);
15219 
15220 				mutex_exit(&cportinfo->cport_mutex);
15221 				if (sata_ioctl_deactivate(sata_hba_inst,
15222 				    &subsdevice) == SATA_SUCCESS) {
15223 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15224 					    "[Deactivate] device at port %d:%d "
15225 					    "successfully.", cport, npmport);
15226 				}
15227 				mutex_enter(&cportinfo->cport_mutex);
15228 			}
15229 		}
15230 
15231 		/* Deactivate the port multiplier now. */
15232 		cportinfo->cport_state &= ~SATA_STATE_READY;
15233 		mutex_exit(&cportinfo->cport_mutex);
15234 
15235 		sata_device->satadev_addr.qual = qual;
15236 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15237 		    (SATA_DIP(sata_hba_inst), sata_device);
15238 
15239 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15240 		    SE_NO_HINT);
15241 
15242 		mutex_enter(&cportinfo->cport_mutex);
15243 		sata_update_port_info(sata_hba_inst, sata_device);
15244 		if (rval != SATA_SUCCESS) {
15245 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15246 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15247 			}
15248 			rv = EIO;
15249 		} else {
15250 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15251 		}
15252 		mutex_exit(&cportinfo->cport_mutex);
15253 
15254 		return (rv);
15255 	}
15256 
15257 	/*
15258 	 * Process non-port-multiplier device - it could be a drive connected
15259 	 * to a port multiplier port or a controller port.
15260 	 */
15261 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15262 	if (qual == SATA_ADDR_CPORT) {
15263 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15264 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15265 			/* deal only with valid devices */
15266 			if ((cportinfo->cport_dev_type &
15267 			    SATA_VALID_DEV_TYPE) != 0)
15268 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15269 		}
15270 		cportinfo->cport_state &= ~SATA_STATE_READY;
15271 	} else {
15272 		/* Port multiplier device port */
15273 		mutex_enter(&pmportinfo->pmport_mutex);
15274 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15275 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15276 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15277 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15278 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15279 		mutex_exit(&pmportinfo->pmport_mutex);
15280 	}
15281 
15282 	if (sdinfo != NULL) {
15283 		/*
15284 		 * If a target node exists, try to offline a device and
15285 		 * to remove a target node.
15286 		 */
15287 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15288 		    cport_mutex);
15289 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15290 		    &sata_device->satadev_addr);
15291 		if (tdip != NULL) {
15292 			/* target node exist */
15293 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15294 			    "sata_hba_ioctl: port deactivate: "
15295 			    "target node exists.", NULL);
15296 
15297 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15298 			    NDI_SUCCESS) {
15299 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15300 				    "sata_hba_ioctl: port deactivate: "
15301 				    "failed to unconfigure device at port "
15302 				    "%d:%d before deactivating the port",
15303 				    cport, pmport));
15304 				/*
15305 				 * Set DEVICE REMOVED state in the target
15306 				 * node. It will prevent an access to
15307 				 * the device even when a new device is
15308 				 * attached, until the old target node is
15309 				 * released, removed and recreated for a new
15310 				 * device.
15311 				 */
15312 				sata_set_device_removed(tdip);
15313 
15314 				/*
15315 				 * Instruct the event daemon to try the
15316 				 * target node cleanup later.
15317 				 */
15318 				sata_set_target_node_cleanup(sata_hba_inst,
15319 				    &sata_device->satadev_addr);
15320 			}
15321 		}
15322 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15323 		    cport_mutex);
15324 		/*
15325 		 * In any case, remove and release sata_drive_info
15326 		 * structure.
15327 		 */
15328 		if (qual == SATA_ADDR_CPORT) {
15329 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15330 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15331 		} else { /* port multiplier device port */
15332 			mutex_enter(&pmportinfo->pmport_mutex);
15333 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15334 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15335 			mutex_exit(&pmportinfo->pmport_mutex);
15336 		}
15337 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15338 	}
15339 
15340 	if (qual == SATA_ADDR_CPORT) {
15341 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15342 		    SATA_STATE_PROBING);
15343 	} else if (qual == SATA_ADDR_PMPORT) {
15344 		mutex_enter(&pmportinfo->pmport_mutex);
15345 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15346 		    SATA_STATE_PROBING);
15347 		mutex_exit(&pmportinfo->pmport_mutex);
15348 	}
15349 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15350 
15351 	/* Just let HBA driver to deactivate port */
15352 	sata_device->satadev_addr.qual = qual;
15353 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15354 	    (SATA_DIP(sata_hba_inst), sata_device);
15355 
15356 	/*
15357 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15358 	 * without the hint
15359 	 */
15360 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15361 	    SE_NO_HINT);
15362 
15363 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15364 	sata_update_port_info(sata_hba_inst, sata_device);
15365 	if (qual == SATA_ADDR_CPORT) {
15366 		if (rval != SATA_SUCCESS) {
15367 			/*
15368 			 * Port deactivation failure - do not change port state
15369 			 * unless the state returned by HBA indicates a port
15370 			 * failure.
15371 			 */
15372 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15373 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15374 				    SATA_PSTATE_FAILED;
15375 			}
15376 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15377 			    "sata_hba_ioctl: port deactivate: "
15378 			    "cannot deactivate SATA port %d", cport));
15379 			rv = EIO;
15380 		} else {
15381 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15382 		}
15383 	} else {
15384 		mutex_enter(&pmportinfo->pmport_mutex);
15385 		if (rval != SATA_SUCCESS) {
15386 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15387 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15388 				    pmport) = SATA_PSTATE_FAILED;
15389 			}
15390 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15391 			    "sata_hba_ioctl: port deactivate: "
15392 			    "cannot deactivate SATA port %d:%d",
15393 			    cport, pmport));
15394 			rv = EIO;
15395 		} else {
15396 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15397 		}
15398 		mutex_exit(&pmportinfo->pmport_mutex);
15399 	}
15400 
15401 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15402 
15403 	return (rv);
15404 }
15405 
15406 /*
15407  * Process ioctl port activate request.
15408  *
15409  * NOTE: Port multiplier is supported now.
15410  */
15411 static int
15412 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15413     sata_device_t *sata_device)
15414 {
15415 	int cport, pmport, qual;
15416 	sata_cport_info_t *cportinfo;
15417 	sata_pmport_info_t *pmportinfo = NULL;
15418 	boolean_t dev_existed = B_TRUE;
15419 
15420 	/* Sanity check */
15421 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15422 		return (ENOTSUP);
15423 
15424 	cport = sata_device->satadev_addr.cport;
15425 	pmport = sata_device->satadev_addr.pmport;
15426 	qual = sata_device->satadev_addr.qual;
15427 
15428 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15429 
15430 	/*
15431 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15432 	 * is a device. But what we are dealing with is port/pmport.
15433 	 */
15434 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15435 	if (qual == SATA_ADDR_DCPORT)
15436 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15437 	else
15438 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15439 
15440 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15441 	if (qual == SATA_ADDR_PMPORT) {
15442 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15443 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15444 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15445 			dev_existed = B_FALSE;
15446 	} else { /* cport */
15447 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15448 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15449 			dev_existed = B_FALSE;
15450 	}
15451 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15452 
15453 	/* Just let HBA driver to activate port, if necessary */
15454 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15455 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15456 		/*
15457 		 * Port activation failure - do not change port state unless
15458 		 * the state returned by HBA indicates a port failure.
15459 		 */
15460 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15461 		    cport)->cport_mutex);
15462 		sata_update_port_info(sata_hba_inst, sata_device);
15463 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15464 			if (qual == SATA_ADDR_PMPORT) {
15465 				mutex_enter(&pmportinfo->pmport_mutex);
15466 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15467 				mutex_exit(&pmportinfo->pmport_mutex);
15468 			} else
15469 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15470 
15471 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15472 			    cport)->cport_mutex);
15473 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15474 			    "sata_hba_ioctl: port activate: cannot activate "
15475 			    "SATA port %d:%d", cport, pmport));
15476 			return (EIO);
15477 		}
15478 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15479 	}
15480 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15481 	if (qual == SATA_ADDR_PMPORT) {
15482 		mutex_enter(&pmportinfo->pmport_mutex);
15483 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15484 		mutex_exit(&pmportinfo->pmport_mutex);
15485 	} else
15486 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15487 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15488 
15489 	/*
15490 	 * Re-probe port to find its current state and possibly attached device.
15491 	 * Port re-probing may change the cportinfo device type if device is
15492 	 * found attached.
15493 	 * If port probing failed, the device type would be set to
15494 	 * SATA_DTYPE_NONE.
15495 	 */
15496 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15497 	    SATA_DEV_IDENTIFY_RETRY);
15498 
15499 	/*
15500 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15501 	 * without the hint.
15502 	 */
15503 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15504 	    SE_NO_HINT);
15505 
15506 	if (dev_existed == B_FALSE) {
15507 		if (qual == SATA_ADDR_PMPORT &&
15508 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15509 			/*
15510 			 * That's the transition from the "inactive" port state
15511 			 * or the active port without a device attached to the
15512 			 * active port state with a device attached.
15513 			 */
15514 			sata_log(sata_hba_inst, CE_WARN,
15515 			    "SATA device detected at port %d:%d",
15516 			    cport, pmport);
15517 		} else if (qual == SATA_ADDR_CPORT &&
15518 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15519 			/*
15520 			 * That's the transition from the "inactive" port state
15521 			 * or the active port without a device attached to the
15522 			 * active port state with a device attached.
15523 			 */
15524 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15525 				sata_log(sata_hba_inst, CE_WARN,
15526 				    "SATA device detected at port %d", cport);
15527 			} else {
15528 				sata_log(sata_hba_inst, CE_WARN,
15529 				    "SATA port multiplier detected at port %d",
15530 				    cport);
15531 			}
15532 		}
15533 	}
15534 	return (0);
15535 }
15536 
15537 
15538 
15539 /*
15540  * Process ioctl reset port request.
15541  *
15542  * NOTE: Port-Multiplier is supported.
15543  */
15544 static int
15545 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15546     sata_device_t *sata_device)
15547 {
15548 	int cport, pmport, qual;
15549 	int rv = 0;
15550 
15551 	cport = sata_device->satadev_addr.cport;
15552 	pmport = sata_device->satadev_addr.pmport;
15553 	qual = sata_device->satadev_addr.qual;
15554 
15555 	/*
15556 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15557 	 * is a device. But what we are dealing with is port/pmport.
15558 	 */
15559 	if (qual == SATA_ADDR_DCPORT)
15560 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15561 	else
15562 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15563 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15564 
15565 	/* Sanity check */
15566 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15567 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15568 		    "sata_hba_ioctl: sata_hba_tran missing required "
15569 		    "function sata_tran_reset_dport"));
15570 		return (ENOTSUP);
15571 	}
15572 
15573 	/* Ask HBA to reset port */
15574 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15575 	    sata_device) != SATA_SUCCESS) {
15576 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15577 		    "sata_hba_ioctl: reset port: failed %d:%d",
15578 		    cport, pmport));
15579 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15580 		    cport_mutex);
15581 		sata_update_port_info(sata_hba_inst, sata_device);
15582 		if (qual == SATA_ADDR_CPORT)
15583 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15584 			    SATA_PSTATE_FAILED;
15585 		else {
15586 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15587 			    pmport));
15588 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15589 			    SATA_PSTATE_FAILED;
15590 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15591 			    pmport));
15592 		}
15593 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15594 		    cport_mutex);
15595 		rv = EIO;
15596 	}
15597 
15598 	return (rv);
15599 }
15600 
15601 /*
15602  * Process ioctl reset device request.
15603  *
15604  * NOTE: Port multiplier is supported.
15605  */
15606 static int
15607 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15608     sata_device_t *sata_device)
15609 {
15610 	sata_drive_info_t *sdinfo = NULL;
15611 	sata_pmult_info_t *pmultinfo = NULL;
15612 	int cport, pmport;
15613 	int rv = 0;
15614 
15615 	/* Sanity check */
15616 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15617 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15618 		    "sata_hba_ioctl: sata_hba_tran missing required "
15619 		    "function sata_tran_reset_dport"));
15620 		return (ENOTSUP);
15621 	}
15622 
15623 	cport = sata_device->satadev_addr.cport;
15624 	pmport = sata_device->satadev_addr.pmport;
15625 
15626 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15627 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15628 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15629 		    SATA_DTYPE_PMULT)
15630 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15631 			    cport_devp.cport_sata_pmult;
15632 		else
15633 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15634 			    sata_device->satadev_addr.cport);
15635 	} else { /* port multiplier */
15636 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15637 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15638 		    sata_device->satadev_addr.cport,
15639 		    sata_device->satadev_addr.pmport);
15640 	}
15641 	if (sdinfo == NULL && pmultinfo == NULL) {
15642 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15643 		return (EINVAL);
15644 	}
15645 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15646 
15647 	/* Ask HBA to reset device */
15648 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15649 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15650 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15651 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15652 		    cport, pmport));
15653 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15654 		    cport_mutex);
15655 		sata_update_port_info(sata_hba_inst, sata_device);
15656 		/*
15657 		 * Device info structure remains attached. Another device reset
15658 		 * or port disconnect/connect and re-probing is
15659 		 * needed to change it's state
15660 		 */
15661 		if (sdinfo != NULL) {
15662 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15663 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15664 		} else if (pmultinfo != NULL) {
15665 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15666 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15667 		}
15668 
15669 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15670 		rv = EIO;
15671 	}
15672 	/*
15673 	 * If attached device was a port multiplier, some extra processing
15674 	 * may be needed to bring it back. SATA specification requies a
15675 	 * mandatory software reset on host port to reliably enumerate a port
15676 	 * multiplier, the HBA driver should handle that after reset
15677 	 * operation.
15678 	 */
15679 	return (rv);
15680 }
15681 
15682 
15683 /*
15684  * Process ioctl reset all request.
15685  */
15686 static int
15687 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15688 {
15689 	sata_device_t sata_device;
15690 	int rv = 0;
15691 	int tcport;
15692 
15693 	sata_device.satadev_rev = SATA_DEVICE_REV;
15694 
15695 	/*
15696 	 * There is no protection here for configured devices.
15697 	 */
15698 	/* Sanity check */
15699 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15700 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15701 		    "sata_hba_ioctl: sata_hba_tran missing required "
15702 		    "function sata_tran_reset_dport"));
15703 		return (ENOTSUP);
15704 	}
15705 
15706 	/*
15707 	 * Need to lock all ports, not just one.
15708 	 * If any port is locked by event processing, fail the whole operation.
15709 	 * One port is already locked, but for simplicity lock it again.
15710 	 */
15711 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15712 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15713 		    cport_mutex);
15714 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15715 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15716 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15717 			    cport_mutex);
15718 			rv = EBUSY;
15719 			break;
15720 		} else {
15721 			/*
15722 			 * It is enough to lock cport in command-based
15723 			 * switching mode.
15724 			 */
15725 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15726 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15727 		}
15728 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15729 		    cport_mutex);
15730 	}
15731 
15732 	if (rv == 0) {
15733 		/*
15734 		 * All cports were successfully locked.
15735 		 * Reset main SATA controller.
15736 		 * Set the device address to port 0, to have a valid device
15737 		 * address.
15738 		 */
15739 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15740 		sata_device.satadev_addr.cport = 0;
15741 		sata_device.satadev_addr.pmport = 0;
15742 
15743 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15744 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15745 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15746 			    "sata_hba_ioctl: reset controller failed"));
15747 			return (EIO);
15748 		}
15749 	}
15750 	/*
15751 	 * Unlock all ports
15752 	 */
15753 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15754 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15755 		    cport_mutex);
15756 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15757 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15758 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15759 		    cport_mutex);
15760 	}
15761 
15762 	/*
15763 	 * This operation returns EFAULT if either reset
15764 	 * controller failed or a re-probing of any port failed.
15765 	 */
15766 	return (rv);
15767 }
15768 
15769 
15770 /*
15771  * Process ioctl port self test request.
15772  *
15773  * NOTE: Port multiplier code is not completed nor tested.
15774  */
15775 static int
15776 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15777     sata_device_t *sata_device)
15778 {
15779 	int cport, pmport, qual;
15780 	int rv = 0;
15781 
15782 	/* Sanity check */
15783 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15784 		return (ENOTSUP);
15785 
15786 	cport = sata_device->satadev_addr.cport;
15787 	pmport = sata_device->satadev_addr.pmport;
15788 	qual = sata_device->satadev_addr.qual;
15789 
15790 	/*
15791 	 * There is no protection here for a configured
15792 	 * device attached to this port.
15793 	 */
15794 
15795 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15796 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15797 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15798 		    "sata_hba_ioctl: port selftest: "
15799 		    "failed port %d:%d", cport, pmport));
15800 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15801 		    cport_mutex);
15802 		sata_update_port_info(sata_hba_inst, sata_device);
15803 		if (qual == SATA_ADDR_CPORT)
15804 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15805 			    SATA_PSTATE_FAILED;
15806 		else { /* port multiplier device port */
15807 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15808 			    cport, pmport));
15809 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15810 			    SATA_PSTATE_FAILED;
15811 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15812 			    cport, pmport));
15813 		}
15814 
15815 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15816 		    cport_mutex);
15817 		return (EIO);
15818 	}
15819 	/*
15820 	 * Beacuse the port was reset in the course of testing, it should be
15821 	 * re-probed and attached device state should be restored. At this
15822 	 * point the port state is unknown - it's state is HBA-specific.
15823 	 * Force port re-probing to get it into a known state.
15824 	 */
15825 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15826 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15827 		rv = EIO;
15828 	return (rv);
15829 }
15830 
15831 
15832 /*
15833  * sata_cfgadm_state:
15834  * Use the sata port state and state of the target node to figure out
15835  * the cfgadm_state.
15836  *
15837  * The port argument is a value with encoded cport,
15838  * pmport and address qualifier, in the same manner as a scsi target number.
15839  * SCSI_TO_SATA_CPORT macro extracts cport number,
15840  * SCSI_TO_SATA_PMPORT extracts pmport number and
15841  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15842  *
15843  * Port multiplier is supported.
15844  */
15845 
15846 static void
15847 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15848     devctl_ap_state_t *ap_state)
15849 {
15850 	uint8_t		cport, pmport, qual;
15851 	uint32_t	port_state, pmult_state;
15852 	uint32_t	dev_type;
15853 	sata_drive_info_t *sdinfo;
15854 
15855 	cport = SCSI_TO_SATA_CPORT(port);
15856 	pmport = SCSI_TO_SATA_PMPORT(port);
15857 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15858 
15859 	/* Check cport state */
15860 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15861 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15862 	    port_state & SATA_PSTATE_FAILED) {
15863 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15864 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15865 		if (port_state & SATA_PSTATE_FAILED)
15866 			ap_state->ap_condition = AP_COND_FAILED;
15867 		else
15868 			ap_state->ap_condition = AP_COND_UNKNOWN;
15869 
15870 		return;
15871 	}
15872 
15873 	/* cport state is okay. Now check pmport state */
15874 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15875 		/* Sanity check */
15876 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15877 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15878 		    cport, pmport) == NULL)
15879 			return;
15880 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15881 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15882 		    port_state & SATA_PSTATE_FAILED) {
15883 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15884 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15885 			if (port_state & SATA_PSTATE_FAILED)
15886 				ap_state->ap_condition = AP_COND_FAILED;
15887 			else
15888 				ap_state->ap_condition = AP_COND_UNKNOWN;
15889 
15890 			return;
15891 		}
15892 	}
15893 
15894 	/* Port is enabled and ready */
15895 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15896 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15897 	else
15898 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15899 
15900 	switch (dev_type) {
15901 	case SATA_DTYPE_NONE:
15902 	{
15903 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15904 		ap_state->ap_condition = AP_COND_OK;
15905 		/* No device attached */
15906 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15907 		break;
15908 	}
15909 	case SATA_DTYPE_PMULT:
15910 	{
15911 		/* Need to check port multiplier state */
15912 		ASSERT(qual == SATA_ADDR_DCPORT);
15913 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15914 		    pmult_state;
15915 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15916 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15917 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15918 			if (pmult_state & SATA_PSTATE_FAILED)
15919 				ap_state->ap_condition = AP_COND_FAILED;
15920 			else
15921 				ap_state->ap_condition = AP_COND_UNKNOWN;
15922 
15923 			return;
15924 		}
15925 
15926 		/* Port multiplier is not configurable */
15927 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15928 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15929 		ap_state->ap_condition = AP_COND_OK;
15930 		break;
15931 	}
15932 
15933 	case SATA_DTYPE_ATADISK:
15934 	case SATA_DTYPE_ATAPICD:
15935 	case SATA_DTYPE_ATAPITAPE:
15936 	case SATA_DTYPE_ATAPIDISK:
15937 	{
15938 		dev_info_t *tdip = NULL;
15939 		dev_info_t *dip = NULL;
15940 		int circ;
15941 
15942 		dip = SATA_DIP(sata_hba_inst);
15943 		tdip = sata_get_target_dip(dip, cport, pmport);
15944 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15945 		if (tdip != NULL) {
15946 			ndi_devi_enter(dip, &circ);
15947 			mutex_enter(&(DEVI(tdip)->devi_lock));
15948 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
15949 				/*
15950 				 * There could be the case where previously
15951 				 * configured and opened device was removed
15952 				 * and unknown device was plugged.
15953 				 * In such case we want to show a device, and
15954 				 * its configured or unconfigured state but
15955 				 * indicate unusable condition untill the
15956 				 * old target node is released and removed.
15957 				 */
15958 				ap_state->ap_condition = AP_COND_UNUSABLE;
15959 			} else {
15960 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
15961 				    cport));
15962 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15963 				    cport);
15964 				if (sdinfo != NULL) {
15965 					if ((sdinfo->satadrv_state &
15966 					    SATA_DSTATE_FAILED) != 0)
15967 						ap_state->ap_condition =
15968 						    AP_COND_FAILED;
15969 					else
15970 						ap_state->ap_condition =
15971 						    AP_COND_OK;
15972 				} else {
15973 					ap_state->ap_condition =
15974 					    AP_COND_UNKNOWN;
15975 				}
15976 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
15977 				    cport));
15978 			}
15979 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
15980 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
15981 				ap_state->ap_ostate =
15982 				    AP_OSTATE_UNCONFIGURED;
15983 			} else {
15984 				ap_state->ap_ostate =
15985 				    AP_OSTATE_CONFIGURED;
15986 			}
15987 			mutex_exit(&(DEVI(tdip)->devi_lock));
15988 			ndi_devi_exit(dip, circ);
15989 		} else {
15990 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15991 			ap_state->ap_condition = AP_COND_UNKNOWN;
15992 		}
15993 		break;
15994 	}
15995 	default:
15996 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15997 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15998 		ap_state->ap_condition = AP_COND_UNKNOWN;
15999 		/*
16000 		 * This is actually internal error condition (non fatal),
16001 		 * because we have already checked all defined device types.
16002 		 */
16003 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16004 		    "sata_cfgadm_state: Internal error: "
16005 		    "unknown device type"));
16006 		break;
16007 	}
16008 }
16009 
16010 
16011 /*
16012  * Process ioctl get device path request.
16013  *
16014  * NOTE: Port multiplier has no target dip. Devices connected to port
16015  * multiplier have target node attached to the HBA node. The only difference
16016  * between them and the directly-attached device node is a target address.
16017  */
16018 static int
16019 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16020     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16021 {
16022 	char path[MAXPATHLEN];
16023 	uint32_t size;
16024 	dev_info_t *tdip;
16025 
16026 	(void) strcpy(path, "/devices");
16027 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16028 	    &sata_device->satadev_addr)) == NULL) {
16029 		/*
16030 		 * No such device. If this is a request for a size, do not
16031 		 * return EINVAL for non-existing target, because cfgadm
16032 		 * will then indicate a meaningless ioctl failure.
16033 		 * If this is a request for a path, indicate invalid
16034 		 * argument.
16035 		 */
16036 		if (ioc->get_size == 0)
16037 			return (EINVAL);
16038 	} else {
16039 		(void) ddi_pathname(tdip, path + strlen(path));
16040 	}
16041 	size = strlen(path) + 1;
16042 
16043 	if (ioc->get_size != 0) {
16044 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16045 		    mode) != 0)
16046 			return (EFAULT);
16047 	} else {
16048 		if (ioc->bufsiz != size)
16049 			return (EINVAL);
16050 
16051 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16052 		    mode) != 0)
16053 			return (EFAULT);
16054 	}
16055 	return (0);
16056 }
16057 
16058 /*
16059  * Process ioctl get attachment point type request.
16060  *
16061  * NOTE: Port multiplier is supported.
16062  */
16063 static	int
16064 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16065     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16066 {
16067 	uint32_t	type_len;
16068 	const char	*ap_type;
16069 	int		dev_type;
16070 
16071 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16072 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16073 		    sata_device->satadev_addr.cport);
16074 	else /* pmport */
16075 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16076 		    sata_device->satadev_addr.cport,
16077 		    sata_device->satadev_addr.pmport);
16078 
16079 	switch (dev_type) {
16080 	case SATA_DTYPE_NONE:
16081 		ap_type = "port";
16082 		break;
16083 
16084 	case SATA_DTYPE_ATADISK:
16085 	case SATA_DTYPE_ATAPIDISK:
16086 		ap_type = "disk";
16087 		break;
16088 
16089 	case SATA_DTYPE_ATAPICD:
16090 		ap_type = "cd/dvd";
16091 		break;
16092 
16093 	case SATA_DTYPE_ATAPITAPE:
16094 		ap_type = "tape";
16095 		break;
16096 
16097 	case SATA_DTYPE_PMULT:
16098 		ap_type = "sata-pmult";
16099 		break;
16100 
16101 	case SATA_DTYPE_UNKNOWN:
16102 		ap_type = "unknown";
16103 		break;
16104 
16105 	default:
16106 		ap_type = "unsupported";
16107 		break;
16108 
16109 	} /* end of dev_type switch */
16110 
16111 	type_len = strlen(ap_type) + 1;
16112 
16113 	if (ioc->get_size) {
16114 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16115 		    mode) != 0)
16116 			return (EFAULT);
16117 	} else {
16118 		if (ioc->bufsiz != type_len)
16119 			return (EINVAL);
16120 
16121 		if (ddi_copyout((void *)ap_type, ioc->buf,
16122 		    ioc->bufsiz, mode) != 0)
16123 			return (EFAULT);
16124 	}
16125 	return (0);
16126 
16127 }
16128 
16129 /*
16130  * Process ioctl get device model info request.
16131  * This operation should return to cfgadm the device model
16132  * information string
16133  *
16134  * NOTE: Port multiplier is supported.
16135  */
16136 static	int
16137 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16138     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16139 {
16140 	sata_drive_info_t *sdinfo;
16141 	uint32_t info_len;
16142 	char ap_info[SATA_ID_MODEL_LEN + 1];
16143 
16144 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16145 	    sata_device->satadev_addr.cport)->cport_mutex);
16146 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16147 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16148 		    sata_device->satadev_addr.cport);
16149 	else /* port multiplier */
16150 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16151 		    sata_device->satadev_addr.cport,
16152 		    sata_device->satadev_addr.pmport);
16153 	if (sdinfo == NULL) {
16154 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16155 		    sata_device->satadev_addr.cport)->cport_mutex);
16156 		return (EINVAL);
16157 	}
16158 
16159 #ifdef	_LITTLE_ENDIAN
16160 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16161 #else	/* _LITTLE_ENDIAN */
16162 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16163 #endif	/* _LITTLE_ENDIAN */
16164 
16165 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16166 	    sata_device->satadev_addr.cport)->cport_mutex);
16167 
16168 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16169 
16170 	info_len = strlen(ap_info) + 1;
16171 
16172 	if (ioc->get_size) {
16173 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16174 		    mode) != 0)
16175 			return (EFAULT);
16176 	} else {
16177 		if (ioc->bufsiz < info_len)
16178 			return (EINVAL);
16179 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16180 		    mode) != 0)
16181 			return (EFAULT);
16182 	}
16183 	return (0);
16184 }
16185 
16186 
16187 /*
16188  * Process ioctl get device firmware revision info request.
16189  * This operation should return to cfgadm the device firmware revision
16190  * information string
16191  *
16192  * Port multiplier is supported.
16193  */
16194 static	int
16195 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16196     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16197 {
16198 	sata_drive_info_t *sdinfo;
16199 	uint32_t info_len;
16200 	char ap_info[SATA_ID_FW_LEN + 1];
16201 
16202 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16203 	    sata_device->satadev_addr.cport)->cport_mutex);
16204 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16205 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16206 		    sata_device->satadev_addr.cport);
16207 	else /* port multiplier */
16208 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16209 		    sata_device->satadev_addr.cport,
16210 		    sata_device->satadev_addr.pmport);
16211 	if (sdinfo == NULL) {
16212 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16213 		    sata_device->satadev_addr.cport)->cport_mutex);
16214 		return (EINVAL);
16215 	}
16216 
16217 #ifdef	_LITTLE_ENDIAN
16218 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16219 #else	/* _LITTLE_ENDIAN */
16220 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16221 #endif	/* _LITTLE_ENDIAN */
16222 
16223 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16224 	    sata_device->satadev_addr.cport)->cport_mutex);
16225 
16226 	ap_info[SATA_ID_FW_LEN] = '\0';
16227 
16228 	info_len = strlen(ap_info) + 1;
16229 
16230 	if (ioc->get_size) {
16231 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16232 		    mode) != 0)
16233 			return (EFAULT);
16234 	} else {
16235 		if (ioc->bufsiz < info_len)
16236 			return (EINVAL);
16237 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16238 		    mode) != 0)
16239 			return (EFAULT);
16240 	}
16241 	return (0);
16242 }
16243 
16244 
16245 /*
16246  * Process ioctl get device serial number info request.
16247  * This operation should return to cfgadm the device serial number string.
16248  *
16249  * NOTE: Port multiplier is supported.
16250  */
16251 static	int
16252 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16253     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16254 {
16255 	sata_drive_info_t *sdinfo;
16256 	uint32_t info_len;
16257 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16258 
16259 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16260 	    sata_device->satadev_addr.cport)->cport_mutex);
16261 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16262 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16263 		    sata_device->satadev_addr.cport);
16264 	else /* port multiplier */
16265 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16266 		    sata_device->satadev_addr.cport,
16267 		    sata_device->satadev_addr.pmport);
16268 	if (sdinfo == NULL) {
16269 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16270 		    sata_device->satadev_addr.cport)->cport_mutex);
16271 		return (EINVAL);
16272 	}
16273 
16274 #ifdef	_LITTLE_ENDIAN
16275 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16276 #else	/* _LITTLE_ENDIAN */
16277 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16278 #endif	/* _LITTLE_ENDIAN */
16279 
16280 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16281 	    sata_device->satadev_addr.cport)->cport_mutex);
16282 
16283 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16284 
16285 	info_len = strlen(ap_info) + 1;
16286 
16287 	if (ioc->get_size) {
16288 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16289 		    mode) != 0)
16290 			return (EFAULT);
16291 	} else {
16292 		if (ioc->bufsiz < info_len)
16293 			return (EINVAL);
16294 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16295 		    mode) != 0)
16296 			return (EFAULT);
16297 	}
16298 	return (0);
16299 }
16300 
16301 
16302 /*
16303  * Preset scsi extended sense data (to NO SENSE)
16304  * First 18 bytes of the sense data are preset to current valid sense
16305  * with a key NO SENSE data.
16306  *
16307  * Returns void
16308  */
16309 static void
16310 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16311 {
16312 	sense->es_valid = 1;		/* Valid sense */
16313 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16314 	sense->es_key = KEY_NO_SENSE;
16315 	sense->es_info_1 = 0;
16316 	sense->es_info_2 = 0;
16317 	sense->es_info_3 = 0;
16318 	sense->es_info_4 = 0;
16319 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16320 	sense->es_cmd_info[0] = 0;
16321 	sense->es_cmd_info[1] = 0;
16322 	sense->es_cmd_info[2] = 0;
16323 	sense->es_cmd_info[3] = 0;
16324 	sense->es_add_code = 0;
16325 	sense->es_qual_code = 0;
16326 }
16327 
16328 /*
16329  * Register a legacy cmdk-style devid for the target (disk) device.
16330  *
16331  * Note: This function is called only when the HBA devinfo node has the
16332  * property "use-cmdk-devid-format" set. This property indicates that
16333  * devid compatible with old cmdk (target) driver is to be generated
16334  * for any target device attached to this controller. This will take
16335  * precedence over the devid generated by sd (target) driver.
16336  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16337  */
16338 static void
16339 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16340 {
16341 	char	*hwid;
16342 	int	modlen;
16343 	int	serlen;
16344 	int	rval;
16345 	ddi_devid_t	devid;
16346 
16347 	/*
16348 	 * device ID is a concatanation of model number, "=", serial number.
16349 	 */
16350 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16351 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16352 	    sizeof (sdinfo->satadrv_id.ai_model));
16353 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16354 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16355 	if (modlen == 0)
16356 		goto err;
16357 	hwid[modlen++] = '=';
16358 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16359 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16360 	swab(&hwid[modlen], &hwid[modlen],
16361 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16362 	serlen = sata_check_modser(&hwid[modlen],
16363 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16364 	if (serlen == 0)
16365 		goto err;
16366 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16367 
16368 	/* initialize/register devid */
16369 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16370 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16371 		rval = ddi_devid_register(dip, devid);
16372 		/*
16373 		 * Free up the allocated devid buffer.
16374 		 * NOTE: This doesn't mean unregistering devid.
16375 		 */
16376 		ddi_devid_free(devid);
16377 	}
16378 
16379 	if (rval != DDI_SUCCESS)
16380 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16381 		    " on port %d", sdinfo->satadrv_addr.cport);
16382 err:
16383 	kmem_free(hwid, LEGACY_HWID_LEN);
16384 }
16385 
16386 /*
16387  * valid model/serial string must contain a non-zero non-space characters.
16388  * trim trailing spaces/NULLs.
16389  */
16390 static int
16391 sata_check_modser(char *buf, int buf_len)
16392 {
16393 	boolean_t ret;
16394 	char *s;
16395 	int i;
16396 	int tb;
16397 	char ch;
16398 
16399 	ret = B_FALSE;
16400 	s = buf;
16401 	for (i = 0; i < buf_len; i++) {
16402 		ch = *s++;
16403 		if (ch != ' ' && ch != '\0')
16404 			tb = i + 1;
16405 		if (ch != ' ' && ch != '\0' && ch != '0')
16406 			ret = B_TRUE;
16407 	}
16408 
16409 	if (ret == B_FALSE)
16410 		return (0); /* invalid string */
16411 
16412 	return (tb); /* return length */
16413 }
16414 
16415 /*
16416  * sata_set_drive_features function compares current device features setting
16417  * with the saved device features settings and, if there is a difference,
16418  * it restores device features setting to the previously saved state.
16419  * It also arbitrarily tries to select the highest supported DMA mode.
16420  * Device Identify or Identify Packet Device data has to be current.
16421  * At the moment read ahead and write cache are considered for all devices.
16422  * For atapi devices, Removable Media Status Notification is set in addition
16423  * to common features.
16424  *
16425  * This function cannot be called in the interrupt context (it may sleep).
16426  *
16427  * The input argument sdinfo should point to the drive info structure
16428  * to be updated after features are set. Note, that only
16429  * device (packet) identify data is updated, not the flags indicating the
16430  * supported features.
16431  *
16432  * Returns SATA_SUCCESS if successful or there was nothing to do.
16433  * Device Identify data in the drive info structure pointed to by the sdinfo
16434  * arguments is updated even when no features were set or changed.
16435  *
16436  * Returns SATA_FAILURE if device features could not be set or DMA mode
16437  * for a disk cannot be set and device identify data cannot be fetched.
16438  *
16439  * Returns SATA_RETRY if device features could not be set (other than disk
16440  * DMA mode) but the device identify data was fetched successfully.
16441  *
16442  * Note: This function may fail the port, making it inaccessible.
16443  * In such case the explicit port disconnect/connect or physical device
16444  * detach/attach is required to re-evaluate port state again.
16445  */
16446 
16447 static int
16448 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16449     sata_drive_info_t *sdinfo, int restore)
16450 {
16451 	int rval = SATA_SUCCESS;
16452 	int rval_set;
16453 	sata_drive_info_t new_sdinfo;
16454 	char *finfo = "sata_set_drive_features: cannot";
16455 	char *finfox;
16456 	int cache_op;
16457 
16458 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16459 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16460 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16461 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16462 		/*
16463 		 * Cannot get device identification - caller may retry later
16464 		 */
16465 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16466 		    "%s fetch device identify data\n", finfo);
16467 		return (SATA_FAILURE);
16468 	}
16469 	finfox = (restore != 0) ? " restore device features" :
16470 	    " initialize device features\n";
16471 
16472 	switch (sdinfo->satadrv_type) {
16473 	case SATA_DTYPE_ATADISK:
16474 		/* Arbitrarily set UDMA mode */
16475 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16476 		    SATA_SUCCESS) {
16477 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16478 			    "%s set UDMA mode\n", finfo));
16479 			return (SATA_FAILURE);
16480 		}
16481 		break;
16482 	case SATA_DTYPE_ATAPICD:
16483 	case SATA_DTYPE_ATAPITAPE:
16484 	case SATA_DTYPE_ATAPIDISK:
16485 		/*  Set Removable Media Status Notification, if necessary */
16486 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16487 		    restore != 0) {
16488 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16489 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16490 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16491 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16492 				/* Current setting does not match saved one */
16493 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16494 				    sdinfo->satadrv_settings &
16495 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16496 					rval = SATA_FAILURE;
16497 			}
16498 		}
16499 		/*
16500 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16501 		 * we want to use DMA transfer mode whenever possible.
16502 		 * Some devices require explicit setting of the DMA mode.
16503 		 */
16504 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16505 			/* Set highest supported DMA mode */
16506 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16507 			    SATA_SUCCESS) {
16508 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16509 				    "%s set UDMA mode\n", finfo));
16510 				rval = SATA_FAILURE;
16511 			}
16512 		}
16513 		break;
16514 	}
16515 
16516 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16517 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16518 		/*
16519 		 * neither READ AHEAD nor WRITE CACHE is supported
16520 		 * - do nothing
16521 		 */
16522 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16523 		    "settable features not supported\n", NULL);
16524 		goto update_sdinfo;
16525 	}
16526 
16527 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16528 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16529 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16530 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16531 		/*
16532 		 * both READ AHEAD and WRITE CACHE are enabled
16533 		 * - Nothing to do
16534 		 */
16535 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16536 		    "no device features to set\n", NULL);
16537 		goto update_sdinfo;
16538 	}
16539 
16540 	cache_op = 0;
16541 
16542 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16543 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16544 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16545 			/* Enable read ahead / read cache */
16546 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16547 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16548 			    "enabling read cache\n", NULL);
16549 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16550 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16551 			/* Disable read ahead  / read cache */
16552 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16553 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16554 			    "disabling read cache\n", NULL);
16555 		}
16556 
16557 		if (cache_op != 0) {
16558 			/* Try to set read cache mode */
16559 			rval_set = sata_set_cache_mode(sata_hba_inst,
16560 			    &new_sdinfo, cache_op);
16561 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16562 				rval = rval_set;
16563 		}
16564 	}
16565 
16566 	cache_op = 0;
16567 
16568 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16569 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16570 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16571 			/* Enable write cache */
16572 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16573 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16574 			    "enabling write cache\n", NULL);
16575 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16576 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16577 			/* Disable write cache */
16578 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16579 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16580 			    "disabling write cache\n", NULL);
16581 		}
16582 
16583 		if (cache_op != 0) {
16584 			/* Try to set write cache mode */
16585 			rval_set = sata_set_cache_mode(sata_hba_inst,
16586 			    &new_sdinfo, cache_op);
16587 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16588 				rval = rval_set;
16589 		}
16590 	}
16591 	if (rval != SATA_SUCCESS)
16592 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16593 		    "%s %s", finfo, finfox));
16594 
16595 update_sdinfo:
16596 	/*
16597 	 * We need to fetch Device Identify data again
16598 	 */
16599 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16600 		/*
16601 		 * Cannot get device identification - retry later
16602 		 */
16603 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16604 		    "%s re-fetch device identify data\n", finfo));
16605 		rval = SATA_FAILURE;
16606 	}
16607 	/* Copy device sata info. */
16608 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16609 
16610 	return (rval);
16611 }
16612 
16613 
16614 /*
16615  *
16616  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16617  * unable to determine.
16618  *
16619  * Cannot be called in an interrupt context.
16620  *
16621  * Called by sata_build_lsense_page_2f()
16622  */
16623 
16624 static int
16625 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16626     sata_drive_info_t *sdinfo)
16627 {
16628 	sata_pkt_t *spkt;
16629 	sata_cmd_t *scmd;
16630 	sata_pkt_txlate_t *spx;
16631 	int rval;
16632 
16633 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16634 	spx->txlt_sata_hba_inst = sata_hba_inst;
16635 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16636 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16637 	if (spkt == NULL) {
16638 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16639 		return (-1);
16640 	}
16641 	/* address is needed now */
16642 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16643 
16644 
16645 	/* Fill sata_pkt */
16646 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16647 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16648 	/* Synchronous mode, no callback */
16649 	spkt->satapkt_comp = NULL;
16650 	/* Timeout 30s */
16651 	spkt->satapkt_time = sata_default_pkt_time;
16652 
16653 	scmd = &spkt->satapkt_cmd;
16654 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16655 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16656 
16657 	/* Set up which registers need to be returned */
16658 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16659 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16660 
16661 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16662 	scmd->satacmd_addr_type = 0;		/* N/A */
16663 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16664 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16665 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16666 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16667 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16668 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16669 	scmd->satacmd_cmd_reg = SATAC_SMART;
16670 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16671 	    sdinfo->satadrv_addr.cport)));
16672 
16673 
16674 	/* Send pkt to SATA HBA driver */
16675 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16676 	    SATA_TRAN_ACCEPTED ||
16677 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16678 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16679 		    sdinfo->satadrv_addr.cport)));
16680 		/*
16681 		 * Whoops, no SMART RETURN STATUS
16682 		 */
16683 		rval = -1;
16684 	} else {
16685 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16686 		    sdinfo->satadrv_addr.cport)));
16687 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16688 			rval = -1;
16689 			goto fail;
16690 		}
16691 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16692 			rval = -1;
16693 			goto fail;
16694 		}
16695 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16696 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16697 			rval = 0;
16698 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16699 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16700 			rval = 1;
16701 		else {
16702 			rval = -1;
16703 			goto fail;
16704 		}
16705 	}
16706 fail:
16707 	/* Free allocated resources */
16708 	sata_pkt_free(spx);
16709 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16710 
16711 	return (rval);
16712 }
16713 
16714 /*
16715  *
16716  * Returns 0 if succeeded, -1 otherwise
16717  *
16718  * Cannot be called in an interrupt context.
16719  *
16720  */
16721 static int
16722 sata_fetch_smart_data(
16723 	sata_hba_inst_t *sata_hba_inst,
16724 	sata_drive_info_t *sdinfo,
16725 	struct smart_data *smart_data)
16726 {
16727 	sata_pkt_t *spkt;
16728 	sata_cmd_t *scmd;
16729 	sata_pkt_txlate_t *spx;
16730 	int rval;
16731 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16732 
16733 #if ! defined(lint)
16734 	ASSERT(sizeof (struct smart_data) == 512);
16735 #endif
16736 
16737 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16738 	spx->txlt_sata_hba_inst = sata_hba_inst;
16739 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16740 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16741 	if (spkt == NULL) {
16742 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16743 		return (-1);
16744 	}
16745 	/* address is needed now */
16746 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16747 
16748 
16749 	/* Fill sata_pkt */
16750 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16751 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16752 	/* Synchronous mode, no callback */
16753 	spkt->satapkt_comp = NULL;
16754 	/* Timeout 30s */
16755 	spkt->satapkt_time = sata_default_pkt_time;
16756 
16757 	scmd = &spkt->satapkt_cmd;
16758 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16759 
16760 	/*
16761 	 * Allocate buffer for SMART data
16762 	 */
16763 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16764 	    sizeof (struct smart_data));
16765 	if (scmd->satacmd_bp == NULL) {
16766 		sata_pkt_free(spx);
16767 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16768 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16769 		    "sata_fetch_smart_data: "
16770 		    "cannot allocate buffer"));
16771 		return (-1);
16772 	}
16773 
16774 
16775 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16776 	scmd->satacmd_addr_type = 0;		/* N/A */
16777 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16778 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16779 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16780 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16781 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16782 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16783 	scmd->satacmd_cmd_reg = SATAC_SMART;
16784 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16785 	    sdinfo->satadrv_addr.cport)));
16786 
16787 	/* Send pkt to SATA HBA driver */
16788 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16789 	    SATA_TRAN_ACCEPTED ||
16790 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16791 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16792 		    sdinfo->satadrv_addr.cport)));
16793 		/*
16794 		 * Whoops, no SMART DATA available
16795 		 */
16796 		rval = -1;
16797 		goto fail;
16798 	} else {
16799 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16800 		    sdinfo->satadrv_addr.cport)));
16801 		if (spx->txlt_buf_dma_handle != NULL) {
16802 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16803 			    DDI_DMA_SYNC_FORKERNEL);
16804 			ASSERT(rval == DDI_SUCCESS);
16805 			if (sata_check_for_dma_error(dip, spx)) {
16806 				ddi_fm_service_impact(dip,
16807 				    DDI_SERVICE_UNAFFECTED);
16808 				rval = -1;
16809 				goto fail;
16810 			}
16811 		}
16812 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16813 		    sizeof (struct smart_data));
16814 	}
16815 
16816 fail:
16817 	/* Free allocated resources */
16818 	sata_free_local_buffer(spx);
16819 	sata_pkt_free(spx);
16820 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16821 
16822 	return (rval);
16823 }
16824 
16825 /*
16826  * Used by LOG SENSE page 0x10
16827  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16828  * Note: cannot be called in the interrupt context.
16829  *
16830  * return 0 for success, -1 otherwise
16831  *
16832  */
16833 static int
16834 sata_ext_smart_selftest_read_log(
16835 	sata_hba_inst_t *sata_hba_inst,
16836 	sata_drive_info_t *sdinfo,
16837 	struct smart_ext_selftest_log *ext_selftest_log,
16838 	uint16_t block_num)
16839 {
16840 	sata_pkt_txlate_t *spx;
16841 	sata_pkt_t *spkt;
16842 	sata_cmd_t *scmd;
16843 	int rval;
16844 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16845 
16846 #if ! defined(lint)
16847 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16848 #endif
16849 
16850 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16851 	spx->txlt_sata_hba_inst = sata_hba_inst;
16852 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16853 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16854 	if (spkt == NULL) {
16855 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16856 		return (-1);
16857 	}
16858 	/* address is needed now */
16859 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16860 
16861 
16862 	/* Fill sata_pkt */
16863 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16864 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16865 	/* Synchronous mode, no callback */
16866 	spkt->satapkt_comp = NULL;
16867 	/* Timeout 30s */
16868 	spkt->satapkt_time = sata_default_pkt_time;
16869 
16870 	scmd = &spkt->satapkt_cmd;
16871 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16872 
16873 	/*
16874 	 * Allocate buffer for SMART extended self-test log
16875 	 */
16876 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16877 	    sizeof (struct smart_ext_selftest_log));
16878 	if (scmd->satacmd_bp == NULL) {
16879 		sata_pkt_free(spx);
16880 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16881 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16882 		    "sata_ext_smart_selftest_log: "
16883 		    "cannot allocate buffer"));
16884 		return (-1);
16885 	}
16886 
16887 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16888 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16889 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16890 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16891 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16892 	scmd->satacmd_lba_low_msb = 0;
16893 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16894 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16895 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16896 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16897 
16898 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16899 	    sdinfo->satadrv_addr.cport)));
16900 
16901 	/* Send pkt to SATA HBA driver */
16902 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16903 	    SATA_TRAN_ACCEPTED ||
16904 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16905 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16906 		    sdinfo->satadrv_addr.cport)));
16907 
16908 		/*
16909 		 * Whoops, no SMART selftest log info available
16910 		 */
16911 		rval = -1;
16912 		goto fail;
16913 	} else {
16914 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16915 		    sdinfo->satadrv_addr.cport)));
16916 
16917 		if (spx->txlt_buf_dma_handle != NULL) {
16918 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16919 			    DDI_DMA_SYNC_FORKERNEL);
16920 			ASSERT(rval == DDI_SUCCESS);
16921 			if (sata_check_for_dma_error(dip, spx)) {
16922 				ddi_fm_service_impact(dip,
16923 				    DDI_SERVICE_UNAFFECTED);
16924 				rval = -1;
16925 				goto fail;
16926 			}
16927 		}
16928 		bcopy(scmd->satacmd_bp->b_un.b_addr,
16929 		    (uint8_t *)ext_selftest_log,
16930 		    sizeof (struct smart_ext_selftest_log));
16931 		rval = 0;
16932 	}
16933 
16934 fail:
16935 	/* Free allocated resources */
16936 	sata_free_local_buffer(spx);
16937 	sata_pkt_free(spx);
16938 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16939 
16940 	return (rval);
16941 }
16942 
16943 /*
16944  * Returns 0 for success, -1 otherwise
16945  *
16946  * SMART self-test log data is returned in buffer pointed to by selftest_log
16947  */
16948 static int
16949 sata_smart_selftest_log(
16950 	sata_hba_inst_t *sata_hba_inst,
16951 	sata_drive_info_t *sdinfo,
16952 	struct smart_selftest_log *selftest_log)
16953 {
16954 	sata_pkt_t *spkt;
16955 	sata_cmd_t *scmd;
16956 	sata_pkt_txlate_t *spx;
16957 	int rval;
16958 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16959 
16960 #if ! defined(lint)
16961 	ASSERT(sizeof (struct smart_selftest_log) == 512);
16962 #endif
16963 
16964 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16965 	spx->txlt_sata_hba_inst = sata_hba_inst;
16966 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16967 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16968 	if (spkt == NULL) {
16969 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16970 		return (-1);
16971 	}
16972 	/* address is needed now */
16973 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16974 
16975 
16976 	/* Fill sata_pkt */
16977 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16978 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16979 	/* Synchronous mode, no callback */
16980 	spkt->satapkt_comp = NULL;
16981 	/* Timeout 30s */
16982 	spkt->satapkt_time = sata_default_pkt_time;
16983 
16984 	scmd = &spkt->satapkt_cmd;
16985 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16986 
16987 	/*
16988 	 * Allocate buffer for SMART SELFTEST LOG
16989 	 */
16990 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16991 	    sizeof (struct smart_selftest_log));
16992 	if (scmd->satacmd_bp == NULL) {
16993 		sata_pkt_free(spx);
16994 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16995 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16996 		    "sata_smart_selftest_log: "
16997 		    "cannot allocate buffer"));
16998 		return (-1);
16999 	}
17000 
17001 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17002 	scmd->satacmd_addr_type = 0;		/* N/A */
17003 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17004 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17005 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17006 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17007 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17008 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17009 	scmd->satacmd_cmd_reg = SATAC_SMART;
17010 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17011 	    sdinfo->satadrv_addr.cport)));
17012 
17013 	/* Send pkt to SATA HBA driver */
17014 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17015 	    SATA_TRAN_ACCEPTED ||
17016 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17017 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17018 		    sdinfo->satadrv_addr.cport)));
17019 		/*
17020 		 * Whoops, no SMART DATA available
17021 		 */
17022 		rval = -1;
17023 		goto fail;
17024 	} else {
17025 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17026 		    sdinfo->satadrv_addr.cport)));
17027 		if (spx->txlt_buf_dma_handle != NULL) {
17028 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17029 			    DDI_DMA_SYNC_FORKERNEL);
17030 			ASSERT(rval == DDI_SUCCESS);
17031 			if (sata_check_for_dma_error(dip, spx)) {
17032 				ddi_fm_service_impact(dip,
17033 				    DDI_SERVICE_UNAFFECTED);
17034 				rval = -1;
17035 				goto fail;
17036 			}
17037 		}
17038 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17039 		    sizeof (struct smart_selftest_log));
17040 		rval = 0;
17041 	}
17042 
17043 fail:
17044 	/* Free allocated resources */
17045 	sata_free_local_buffer(spx);
17046 	sata_pkt_free(spx);
17047 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17048 
17049 	return (rval);
17050 }
17051 
17052 
17053 /*
17054  * Returns 0 for success, -1 otherwise
17055  *
17056  * SMART READ LOG data is returned in buffer pointed to by smart_log
17057  */
17058 static int
17059 sata_smart_read_log(
17060 	sata_hba_inst_t *sata_hba_inst,
17061 	sata_drive_info_t *sdinfo,
17062 	uint8_t *smart_log,		/* where the data should be returned */
17063 	uint8_t which_log,		/* which log should be returned */
17064 	uint8_t log_size)		/* # of 512 bytes in log */
17065 {
17066 	sata_pkt_t *spkt;
17067 	sata_cmd_t *scmd;
17068 	sata_pkt_txlate_t *spx;
17069 	int rval;
17070 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17071 
17072 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17073 	spx->txlt_sata_hba_inst = sata_hba_inst;
17074 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17075 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17076 	if (spkt == NULL) {
17077 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17078 		return (-1);
17079 	}
17080 	/* address is needed now */
17081 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17082 
17083 
17084 	/* Fill sata_pkt */
17085 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17086 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17087 	/* Synchronous mode, no callback */
17088 	spkt->satapkt_comp = NULL;
17089 	/* Timeout 30s */
17090 	spkt->satapkt_time = sata_default_pkt_time;
17091 
17092 	scmd = &spkt->satapkt_cmd;
17093 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17094 
17095 	/*
17096 	 * Allocate buffer for SMART READ LOG
17097 	 */
17098 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17099 	if (scmd->satacmd_bp == NULL) {
17100 		sata_pkt_free(spx);
17101 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17102 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17103 		    "sata_smart_read_log: " "cannot allocate buffer"));
17104 		return (-1);
17105 	}
17106 
17107 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17108 	scmd->satacmd_addr_type = 0;		/* N/A */
17109 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17110 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17111 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17112 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17113 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17114 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17115 	scmd->satacmd_cmd_reg = SATAC_SMART;
17116 
17117 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17118 	    sdinfo->satadrv_addr.cport)));
17119 
17120 	/* Send pkt to SATA HBA driver */
17121 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17122 	    SATA_TRAN_ACCEPTED ||
17123 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17124 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17125 		    sdinfo->satadrv_addr.cport)));
17126 
17127 		/*
17128 		 * Whoops, no SMART DATA available
17129 		 */
17130 		rval = -1;
17131 		goto fail;
17132 	} else {
17133 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17134 		    sdinfo->satadrv_addr.cport)));
17135 
17136 		if (spx->txlt_buf_dma_handle != NULL) {
17137 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17138 			    DDI_DMA_SYNC_FORKERNEL);
17139 			ASSERT(rval == DDI_SUCCESS);
17140 			if (sata_check_for_dma_error(dip, spx)) {
17141 				ddi_fm_service_impact(dip,
17142 				    DDI_SERVICE_UNAFFECTED);
17143 				rval = -1;
17144 				goto fail;
17145 			}
17146 		}
17147 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17148 		rval = 0;
17149 	}
17150 
17151 fail:
17152 	/* Free allocated resources */
17153 	sata_free_local_buffer(spx);
17154 	sata_pkt_free(spx);
17155 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17156 
17157 	return (rval);
17158 }
17159 
17160 /*
17161  * Used by LOG SENSE page 0x10
17162  *
17163  * return 0 for success, -1 otherwise
17164  *
17165  */
17166 static int
17167 sata_read_log_ext_directory(
17168 	sata_hba_inst_t *sata_hba_inst,
17169 	sata_drive_info_t *sdinfo,
17170 	struct read_log_ext_directory *logdir)
17171 {
17172 	sata_pkt_txlate_t *spx;
17173 	sata_pkt_t *spkt;
17174 	sata_cmd_t *scmd;
17175 	int rval;
17176 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17177 
17178 #if ! defined(lint)
17179 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17180 #endif
17181 
17182 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17183 	spx->txlt_sata_hba_inst = sata_hba_inst;
17184 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17185 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17186 	if (spkt == NULL) {
17187 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17188 		return (-1);
17189 	}
17190 
17191 	/* Fill sata_pkt */
17192 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17193 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17194 	/* Synchronous mode, no callback */
17195 	spkt->satapkt_comp = NULL;
17196 	/* Timeout 30s */
17197 	spkt->satapkt_time = sata_default_pkt_time;
17198 
17199 	scmd = &spkt->satapkt_cmd;
17200 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17201 
17202 	/*
17203 	 * Allocate buffer for SMART READ LOG EXTENDED command
17204 	 */
17205 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17206 	    sizeof (struct read_log_ext_directory));
17207 	if (scmd->satacmd_bp == NULL) {
17208 		sata_pkt_free(spx);
17209 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17210 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17211 		    "sata_read_log_ext_directory: "
17212 		    "cannot allocate buffer"));
17213 		return (-1);
17214 	}
17215 
17216 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17217 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17218 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17219 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17220 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17221 	scmd->satacmd_lba_low_msb = 0;
17222 	scmd->satacmd_lba_mid_lsb = 0;
17223 	scmd->satacmd_lba_mid_msb = 0;
17224 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17225 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17226 
17227 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17228 	    sdinfo->satadrv_addr.cport)));
17229 
17230 	/* Send pkt to SATA HBA driver */
17231 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17232 	    SATA_TRAN_ACCEPTED ||
17233 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17234 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17235 		    sdinfo->satadrv_addr.cport)));
17236 		/*
17237 		 * Whoops, no SMART selftest log info available
17238 		 */
17239 		rval = -1;
17240 		goto fail;
17241 	} else {
17242 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17243 		    sdinfo->satadrv_addr.cport)));
17244 		if (spx->txlt_buf_dma_handle != NULL) {
17245 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17246 			    DDI_DMA_SYNC_FORKERNEL);
17247 			ASSERT(rval == DDI_SUCCESS);
17248 			if (sata_check_for_dma_error(dip, spx)) {
17249 				ddi_fm_service_impact(dip,
17250 				    DDI_SERVICE_UNAFFECTED);
17251 				rval = -1;
17252 				goto fail;
17253 			}
17254 		}
17255 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17256 		    sizeof (struct read_log_ext_directory));
17257 		rval = 0;
17258 	}
17259 
17260 fail:
17261 	/* Free allocated resources */
17262 	sata_free_local_buffer(spx);
17263 	sata_pkt_free(spx);
17264 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17265 
17266 	return (rval);
17267 }
17268 
17269 /*
17270  * Set up error retrieval sata command for NCQ command error data
17271  * recovery.
17272  *
17273  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17274  * returns SATA_FAILURE otherwise.
17275  */
17276 static int
17277 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17278 {
17279 #ifndef __lock_lint
17280 	_NOTE(ARGUNUSED(sdinfo))
17281 #endif
17282 
17283 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17284 	sata_cmd_t *scmd;
17285 	struct buf *bp;
17286 
17287 	/* Operation modes are up to the caller */
17288 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17289 
17290 	/* Synchronous mode, no callback - may be changed by the caller */
17291 	spkt->satapkt_comp = NULL;
17292 	spkt->satapkt_time = sata_default_pkt_time;
17293 
17294 	scmd = &spkt->satapkt_cmd;
17295 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17296 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17297 
17298 	/*
17299 	 * Allocate dma_able buffer error data.
17300 	 * Buffer allocation will take care of buffer alignment and other DMA
17301 	 * attributes.
17302 	 */
17303 	bp = sata_alloc_local_buffer(spx,
17304 	    sizeof (struct sata_ncq_error_recovery_page));
17305 	if (bp == NULL)
17306 		return (SATA_FAILURE);
17307 
17308 	bp_mapin(bp); /* make data buffer accessible */
17309 	scmd->satacmd_bp = bp;
17310 
17311 	/*
17312 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17313 	 * before accessing it. Handle is in usual place in translate struct.
17314 	 */
17315 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17316 
17317 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17318 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17319 
17320 	return (SATA_SUCCESS);
17321 }
17322 
17323 /*
17324  * sata_xlate_errors() is used to translate (S)ATA error
17325  * information to SCSI information returned in the SCSI
17326  * packet.
17327  */
17328 static void
17329 sata_xlate_errors(sata_pkt_txlate_t *spx)
17330 {
17331 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17332 	struct scsi_extended_sense *sense;
17333 
17334 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17335 	*scsipkt->pkt_scbp = STATUS_CHECK;
17336 	sense = sata_arq_sense(spx);
17337 
17338 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17339 	case SATA_PKT_PORT_ERROR:
17340 		/*
17341 		 * We have no device data. Assume no data transfered.
17342 		 */
17343 		sense->es_key = KEY_HARDWARE_ERROR;
17344 		break;
17345 
17346 	case SATA_PKT_DEV_ERROR:
17347 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17348 		    SATA_STATUS_ERR) {
17349 			/*
17350 			 * determine dev error reason from error
17351 			 * reg content
17352 			 */
17353 			sata_decode_device_error(spx, sense);
17354 			break;
17355 		}
17356 		/* No extended sense key - no info available */
17357 		break;
17358 
17359 	case SATA_PKT_TIMEOUT:
17360 		scsipkt->pkt_reason = CMD_TIMEOUT;
17361 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17362 		/* No extended sense key */
17363 		break;
17364 
17365 	case SATA_PKT_ABORTED:
17366 		scsipkt->pkt_reason = CMD_ABORTED;
17367 		scsipkt->pkt_statistics |= STAT_ABORTED;
17368 		/* No extended sense key */
17369 		break;
17370 
17371 	case SATA_PKT_RESET:
17372 		/*
17373 		 * pkt aborted either by an explicit reset request from
17374 		 * a host, or due to error recovery
17375 		 */
17376 		scsipkt->pkt_reason = CMD_RESET;
17377 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17378 		break;
17379 
17380 	default:
17381 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17382 		break;
17383 	}
17384 }
17385 
17386 
17387 
17388 
17389 /*
17390  * Log sata message
17391  * dev pathname msg line preceeds the logged message.
17392  */
17393 
17394 static	void
17395 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17396 {
17397 	char pathname[128];
17398 	dev_info_t *dip = NULL;
17399 	va_list ap;
17400 
17401 	mutex_enter(&sata_log_mutex);
17402 
17403 	va_start(ap, fmt);
17404 	(void) vsprintf(sata_log_buf, fmt, ap);
17405 	va_end(ap);
17406 
17407 	if (sata_hba_inst != NULL) {
17408 		dip = SATA_DIP(sata_hba_inst);
17409 		(void) ddi_pathname(dip, pathname);
17410 	} else {
17411 		pathname[0] = 0;
17412 	}
17413 	if (level == CE_CONT) {
17414 		if (sata_debug_flags == 0)
17415 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17416 		else
17417 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17418 	} else {
17419 		if (level != CE_NOTE) {
17420 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17421 		} else if (sata_msg) {
17422 			cmn_err(level, "%s:\n %s", pathname,
17423 			    sata_log_buf);
17424 		}
17425 	}
17426 
17427 	/* sata trace debug */
17428 	sata_trace_debug(dip, sata_log_buf);
17429 
17430 	mutex_exit(&sata_log_mutex);
17431 }
17432 
17433 
17434 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17435 
17436 /*
17437  * Start or terminate the thread, depending on flag arg and current state
17438  */
17439 static void
17440 sata_event_thread_control(int startstop)
17441 {
17442 	static 	int sata_event_thread_terminating = 0;
17443 	static 	int sata_event_thread_starting = 0;
17444 	int i;
17445 
17446 	mutex_enter(&sata_event_mutex);
17447 
17448 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17449 	    sata_event_thread_terminating == 1)) {
17450 		mutex_exit(&sata_event_mutex);
17451 		return;
17452 	}
17453 	if (startstop == 1 && sata_event_thread_starting == 1) {
17454 		mutex_exit(&sata_event_mutex);
17455 		return;
17456 	}
17457 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17458 		sata_event_thread_starting = 1;
17459 		/* wait til terminate operation completes */
17460 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17461 		while (sata_event_thread_terminating == 1) {
17462 			if (i-- <= 0) {
17463 				sata_event_thread_starting = 0;
17464 				mutex_exit(&sata_event_mutex);
17465 #ifdef SATA_DEBUG
17466 				cmn_err(CE_WARN, "sata_event_thread_control: "
17467 				    "timeout waiting for thread to terminate");
17468 #endif
17469 				return;
17470 			}
17471 			mutex_exit(&sata_event_mutex);
17472 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17473 			mutex_enter(&sata_event_mutex);
17474 		}
17475 	}
17476 	if (startstop == 1) {
17477 		if (sata_event_thread == NULL) {
17478 			sata_event_thread = thread_create(NULL, 0,
17479 			    (void (*)())sata_event_daemon,
17480 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17481 		}
17482 		sata_event_thread_starting = 0;
17483 		mutex_exit(&sata_event_mutex);
17484 		return;
17485 	}
17486 
17487 	/*
17488 	 * If we got here, thread may need to be terminated
17489 	 */
17490 	if (sata_event_thread != NULL) {
17491 		int i;
17492 		/* Signal event thread to go away */
17493 		sata_event_thread_terminating = 1;
17494 		sata_event_thread_terminate = 1;
17495 		cv_signal(&sata_event_cv);
17496 		/*
17497 		 * Wait til daemon terminates.
17498 		 */
17499 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17500 		while (sata_event_thread_terminate == 1) {
17501 			mutex_exit(&sata_event_mutex);
17502 			if (i-- <= 0) {
17503 				/* Daemon did not go away !!! */
17504 #ifdef SATA_DEBUG
17505 				cmn_err(CE_WARN, "sata_event_thread_control: "
17506 				    "cannot terminate event daemon thread");
17507 #endif
17508 				mutex_enter(&sata_event_mutex);
17509 				break;
17510 			}
17511 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17512 			mutex_enter(&sata_event_mutex);
17513 		}
17514 		sata_event_thread_terminating = 0;
17515 	}
17516 	ASSERT(sata_event_thread_terminating == 0);
17517 	ASSERT(sata_event_thread_starting == 0);
17518 	mutex_exit(&sata_event_mutex);
17519 }
17520 
17521 
17522 /*
17523  * SATA HBA event notification function.
17524  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17525  * a port and/or device state or a controller itself.
17526  * Events for different addresses/addr types cannot be combined.
17527  * A warning message is generated for each event type.
17528  * Events are not processed by this function, so only the
17529  * event flag(s)is set for an affected entity and the event thread is
17530  * waken up. Event daemon thread processes all events.
17531  *
17532  * NOTE: Since more than one event may be reported at the same time, one
17533  * cannot determine a sequence of events when opposite event are reported, eg.
17534  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17535  * is taking precedence over reported events, i.e. may cause ignoring some
17536  * events.
17537  */
17538 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17539 
17540 void
17541 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17542 {
17543 	sata_hba_inst_t *sata_hba_inst = NULL;
17544 	sata_address_t *saddr;
17545 	sata_pmult_info_t *pmultinfo;
17546 	sata_drive_info_t *sdinfo;
17547 	sata_port_stats_t *pstats;
17548 	sata_cport_info_t *cportinfo;
17549 	sata_pmport_info_t *pmportinfo;
17550 	int cport, pmport;
17551 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17552 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17553 	char *lcp;
17554 	static char *err_msg_evnt_1 =
17555 	    "sata_hba_event_notify: invalid port event 0x%x ";
17556 	static char *err_msg_evnt_2 =
17557 	    "sata_hba_event_notify: invalid device event 0x%x ";
17558 	int linkevent;
17559 
17560 	/*
17561 	 * There is a possibility that an event will be generated on HBA
17562 	 * that has not completed attachment or is detaching. We still want
17563 	 * to process events until HBA is detached.
17564 	 */
17565 	mutex_enter(&sata_mutex);
17566 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17567 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17568 		if (SATA_DIP(sata_hba_inst) == dip)
17569 			if (sata_hba_inst->satahba_attached == 1)
17570 				break;
17571 	}
17572 	mutex_exit(&sata_mutex);
17573 	if (sata_hba_inst == NULL)
17574 		/* HBA not attached */
17575 		return;
17576 
17577 	ASSERT(sata_device != NULL);
17578 
17579 	/*
17580 	 * Validate address before - do not proceed with invalid address.
17581 	 */
17582 	saddr = &sata_device->satadev_addr;
17583 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17584 		return;
17585 
17586 	cport = saddr->cport;
17587 	pmport = saddr->pmport;
17588 
17589 	buf1[0] = buf2[0] = '\0';
17590 
17591 	/*
17592 	 * If event relates to port or device, check port state.
17593 	 * Port has to be initialized, or we cannot accept an event.
17594 	 */
17595 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17596 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17597 		mutex_enter(&sata_hba_inst->satahba_mutex);
17598 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17599 		mutex_exit(&sata_hba_inst->satahba_mutex);
17600 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17601 			return;
17602 	}
17603 
17604 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17605 	    SATA_ADDR_DPMPORT)) != 0) {
17606 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17607 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17608 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17609 			    "is attached to port %d, ignore pmult/pmport "
17610 			    "event 0x%x", cportinfo->cport_dev_type,
17611 			    cport, event));
17612 			return;
17613 		}
17614 
17615 		mutex_enter(&cportinfo->cport_mutex);
17616 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17617 		mutex_exit(&cportinfo->cport_mutex);
17618 
17619 		/*
17620 		 * The daemon might be processing attachment of port
17621 		 * multiplier, in that case we should ignore events on its
17622 		 * sub-devices.
17623 		 *
17624 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17625 		 * The pmport_state is checked by sata daemon.
17626 		 */
17627 		if (pmultinfo == NULL ||
17628 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17629 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17630 			    "sata_hba_event_notify: pmult is not"
17631 			    "available at port %d:%d, ignore event 0x%x",
17632 			    cport, pmport, event));
17633 			return;
17634 		}
17635 	}
17636 
17637 	if ((saddr->qual &
17638 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17639 
17640 		mutex_enter(&cportinfo->cport_mutex);
17641 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17642 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17643 			    "sata_hba_event_notify: invalid/"
17644 			    "un-implemented port %d:%d (%d ports), "
17645 			    "ignore event 0x%x", cport, pmport,
17646 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17647 			mutex_exit(&cportinfo->cport_mutex);
17648 			return;
17649 		}
17650 		mutex_exit(&cportinfo->cport_mutex);
17651 
17652 		mutex_enter(&sata_hba_inst->satahba_mutex);
17653 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17654 		    cport, pmport);
17655 		mutex_exit(&sata_hba_inst->satahba_mutex);
17656 
17657 		/* pmport is implemented/valid? */
17658 		if (pmportinfo == NULL) {
17659 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17660 			    "sata_hba_event_notify: invalid/"
17661 			    "un-implemented port %d:%d, ignore "
17662 			    "event 0x%x", cport, pmport, event));
17663 			return;
17664 		}
17665 	}
17666 
17667 	/*
17668 	 * Events refer to devices, ports and controllers - each has
17669 	 * unique address. Events for different addresses cannot be combined.
17670 	 */
17671 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17672 
17673 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17674 
17675 		/* qualify this event(s) */
17676 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17677 			/* Invalid event for the device port */
17678 			(void) sprintf(buf2, err_msg_evnt_1,
17679 			    event & SATA_EVNT_PORT_EVENTS);
17680 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17681 			goto event_info;
17682 		}
17683 		if (saddr->qual == SATA_ADDR_CPORT) {
17684 			/* Controller's device port event */
17685 
17686 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17687 			    cport_event_flags |=
17688 			    event & SATA_EVNT_PORT_EVENTS;
17689 			pstats =
17690 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17691 			    cport_stats;
17692 		} else {
17693 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17694 			mutex_enter(&pmportinfo->pmport_mutex);
17695 			/* Port multiplier's device port event */
17696 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17697 			    pmport_event_flags |=
17698 			    event & SATA_EVNT_PORT_EVENTS;
17699 			pstats =
17700 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17701 			    pmport_stats;
17702 			mutex_exit(&pmportinfo->pmport_mutex);
17703 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17704 		}
17705 
17706 		/*
17707 		 * Add to statistics and log the message. We have to do it
17708 		 * here rather than in the event daemon, because there may be
17709 		 * multiple events occuring before they are processed.
17710 		 */
17711 		linkevent = event &
17712 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17713 		if (linkevent) {
17714 			if (linkevent == (SATA_EVNT_LINK_LOST |
17715 			    SATA_EVNT_LINK_ESTABLISHED)) {
17716 				/* This is likely event combination */
17717 				(void) strlcat(buf1, "link lost/established, ",
17718 				    SATA_EVENT_MAX_MSG_LENGTH);
17719 
17720 				if (pstats->link_lost < 0xffffffffffffffffULL)
17721 					pstats->link_lost++;
17722 				if (pstats->link_established <
17723 				    0xffffffffffffffffULL)
17724 					pstats->link_established++;
17725 				linkevent = 0;
17726 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17727 				(void) strlcat(buf1, "link lost, ",
17728 				    SATA_EVENT_MAX_MSG_LENGTH);
17729 
17730 				if (pstats->link_lost < 0xffffffffffffffffULL)
17731 					pstats->link_lost++;
17732 			} else {
17733 				(void) strlcat(buf1, "link established, ",
17734 				    SATA_EVENT_MAX_MSG_LENGTH);
17735 				if (pstats->link_established <
17736 				    0xffffffffffffffffULL)
17737 					pstats->link_established++;
17738 			}
17739 		}
17740 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17741 			(void) strlcat(buf1, "device attached, ",
17742 			    SATA_EVENT_MAX_MSG_LENGTH);
17743 			if (pstats->device_attached < 0xffffffffffffffffULL)
17744 				pstats->device_attached++;
17745 		}
17746 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17747 			(void) strlcat(buf1, "device detached, ",
17748 			    SATA_EVENT_MAX_MSG_LENGTH);
17749 			if (pstats->device_detached < 0xffffffffffffffffULL)
17750 				pstats->device_detached++;
17751 		}
17752 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17753 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17754 			    "port %d power level changed", cport);
17755 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17756 				pstats->port_pwr_changed++;
17757 		}
17758 
17759 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17760 			/* There should be no other events for this address */
17761 			(void) sprintf(buf2, err_msg_evnt_1,
17762 			    event & ~SATA_EVNT_PORT_EVENTS);
17763 		}
17764 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17765 
17766 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17767 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17768 
17769 		/* qualify this event */
17770 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17771 			/* Invalid event for a device */
17772 			(void) sprintf(buf2, err_msg_evnt_2,
17773 			    event & SATA_EVNT_DEVICE_RESET);
17774 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17775 			goto event_info;
17776 		}
17777 		/* drive event */
17778 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17779 		if (sdinfo != NULL) {
17780 			if (event & SATA_EVNT_DEVICE_RESET) {
17781 				(void) strlcat(buf1, "device reset, ",
17782 				    SATA_EVENT_MAX_MSG_LENGTH);
17783 				if (sdinfo->satadrv_stats.drive_reset <
17784 				    0xffffffffffffffffULL)
17785 					sdinfo->satadrv_stats.drive_reset++;
17786 				sdinfo->satadrv_event_flags |=
17787 				    SATA_EVNT_DEVICE_RESET;
17788 			}
17789 		}
17790 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17791 			/* Invalid event for a device */
17792 			(void) sprintf(buf2, err_msg_evnt_2,
17793 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17794 		}
17795 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17796 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17797 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17798 
17799 		/* qualify this event */
17800 		if ((event & (SATA_EVNT_DEVICE_RESET |
17801 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17802 			/* Invalid event for a port multiplier */
17803 			(void) sprintf(buf2, err_msg_evnt_2,
17804 			    event & SATA_EVNT_DEVICE_RESET);
17805 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17806 			goto event_info;
17807 		}
17808 
17809 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17810 
17811 		if (event & SATA_EVNT_DEVICE_RESET) {
17812 
17813 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17814 			    "[Reset] port-mult on cport %d", cport);
17815 			pmultinfo->pmult_event_flags |=
17816 			    SATA_EVNT_DEVICE_RESET;
17817 			(void) strlcat(buf1, "pmult reset, ",
17818 			    SATA_EVENT_MAX_MSG_LENGTH);
17819 		}
17820 
17821 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17822 
17823 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17824 			    "pmult link changed on cport %d", cport);
17825 			pmultinfo->pmult_event_flags |=
17826 			    SATA_EVNT_PMULT_LINK_CHANGED;
17827 			(void) strlcat(buf1, "pmult link changed, ",
17828 			    SATA_EVENT_MAX_MSG_LENGTH);
17829 		}
17830 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17831 
17832 	} else {
17833 		if (saddr->qual != SATA_ADDR_NULL) {
17834 			/* Wrong address qualifier */
17835 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17836 			    "sata_hba_event_notify: invalid address 0x%x",
17837 			    *(uint32_t *)saddr));
17838 			return;
17839 		}
17840 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17841 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17842 			/* Invalid event for the controller */
17843 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17844 			    "sata_hba_event_notify: invalid event 0x%x for "
17845 			    "controller",
17846 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17847 			return;
17848 		}
17849 		buf1[0] = '\0';
17850 		/* This may be a frequent and not interesting event */
17851 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17852 		    "controller power level changed\n", NULL);
17853 
17854 		mutex_enter(&sata_hba_inst->satahba_mutex);
17855 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17856 		    0xffffffffffffffffULL)
17857 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17858 
17859 		sata_hba_inst->satahba_event_flags |=
17860 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17861 		mutex_exit(&sata_hba_inst->satahba_mutex);
17862 	}
17863 	/*
17864 	 * If we got here, there is something to do with this HBA
17865 	 * instance.
17866 	 */
17867 	mutex_enter(&sata_hba_inst->satahba_mutex);
17868 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17869 	mutex_exit(&sata_hba_inst->satahba_mutex);
17870 	mutex_enter(&sata_mutex);
17871 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17872 	mutex_exit(&sata_mutex);
17873 
17874 	/* Tickle event thread */
17875 	mutex_enter(&sata_event_mutex);
17876 	if (sata_event_thread_active == 0)
17877 		cv_signal(&sata_event_cv);
17878 	mutex_exit(&sata_event_mutex);
17879 
17880 event_info:
17881 	if (buf1[0] != '\0') {
17882 		lcp = strrchr(buf1, ',');
17883 		if (lcp != NULL)
17884 			*lcp = '\0';
17885 	}
17886 	if (saddr->qual == SATA_ADDR_CPORT ||
17887 	    saddr->qual == SATA_ADDR_DCPORT) {
17888 		if (buf1[0] != '\0') {
17889 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17890 			    cport, buf1);
17891 		}
17892 		if (buf2[0] != '\0') {
17893 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17894 			    cport, buf2);
17895 		}
17896 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17897 	    saddr->qual == SATA_ADDR_DPMPORT) {
17898 		if (buf1[0] != '\0') {
17899 			sata_log(sata_hba_inst, CE_NOTE,
17900 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17901 		}
17902 		if (buf2[0] != '\0') {
17903 			sata_log(sata_hba_inst, CE_NOTE,
17904 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17905 		}
17906 	}
17907 }
17908 
17909 
17910 /*
17911  * Event processing thread.
17912  * Arg is a pointer to the sata_hba_list pointer.
17913  * It is not really needed, because sata_hba_list is global and static
17914  */
17915 static void
17916 sata_event_daemon(void *arg)
17917 {
17918 #ifndef __lock_lint
17919 	_NOTE(ARGUNUSED(arg))
17920 #endif
17921 	sata_hba_inst_t *sata_hba_inst;
17922 	clock_t delta;
17923 
17924 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17925 	    "SATA event daemon started\n", NULL);
17926 loop:
17927 	/*
17928 	 * Process events here. Walk through all registered HBAs
17929 	 */
17930 	mutex_enter(&sata_mutex);
17931 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17932 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17933 		ASSERT(sata_hba_inst != NULL);
17934 		mutex_enter(&sata_hba_inst->satahba_mutex);
17935 		if (sata_hba_inst->satahba_attached == 0 ||
17936 		    (sata_hba_inst->satahba_event_flags &
17937 		    SATA_EVNT_SKIP) != 0) {
17938 			mutex_exit(&sata_hba_inst->satahba_mutex);
17939 			continue;
17940 		}
17941 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
17942 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
17943 			mutex_exit(&sata_hba_inst->satahba_mutex);
17944 			mutex_exit(&sata_mutex);
17945 			/* Got the controller with pending event */
17946 			sata_process_controller_events(sata_hba_inst);
17947 			/*
17948 			 * Since global mutex was released, there is a
17949 			 * possibility that HBA list has changed, so start
17950 			 * over from the top. Just processed controller
17951 			 * will be passed-over because of the SKIP flag.
17952 			 */
17953 			goto loop;
17954 		}
17955 		mutex_exit(&sata_hba_inst->satahba_mutex);
17956 	}
17957 	/* Clear SKIP flag in all controllers */
17958 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17959 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17960 		mutex_enter(&sata_hba_inst->satahba_mutex);
17961 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
17962 		mutex_exit(&sata_hba_inst->satahba_mutex);
17963 	}
17964 	mutex_exit(&sata_mutex);
17965 
17966 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17967 	    "SATA EVENT DAEMON suspending itself", NULL);
17968 
17969 #ifdef SATA_DEBUG
17970 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
17971 		sata_log(sata_hba_inst, CE_WARN,
17972 		    "SATA EVENTS PROCESSING DISABLED\n");
17973 		thread_exit(); /* Daemon will not run again */
17974 	}
17975 #endif
17976 	mutex_enter(&sata_event_mutex);
17977 	sata_event_thread_active = 0;
17978 	mutex_exit(&sata_event_mutex);
17979 	/*
17980 	 * Go to sleep/suspend itself and wake up either because new event or
17981 	 * wait timeout. Exit if there is a termination request (driver
17982 	 * unload).
17983 	 */
17984 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
17985 	do {
17986 		mutex_enter(&sata_event_mutex);
17987 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
17988 		    delta, TR_CLOCK_TICK);
17989 
17990 		if (sata_event_thread_active != 0) {
17991 			mutex_exit(&sata_event_mutex);
17992 			continue;
17993 		}
17994 
17995 		/* Check if it is time to go away */
17996 		if (sata_event_thread_terminate == 1) {
17997 			/*
17998 			 * It is up to the thread setting above flag to make
17999 			 * sure that this thread is not killed prematurely.
18000 			 */
18001 			sata_event_thread_terminate = 0;
18002 			sata_event_thread = NULL;
18003 			mutex_exit(&sata_event_mutex);
18004 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18005 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18006 			thread_exit();  { _NOTE(NOT_REACHED) }
18007 		}
18008 		mutex_exit(&sata_event_mutex);
18009 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18010 
18011 	mutex_enter(&sata_event_mutex);
18012 	sata_event_thread_active = 1;
18013 	mutex_exit(&sata_event_mutex);
18014 
18015 	mutex_enter(&sata_mutex);
18016 	sata_event_pending &= ~SATA_EVNT_MAIN;
18017 	mutex_exit(&sata_mutex);
18018 
18019 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18020 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18021 
18022 	goto loop;
18023 }
18024 
18025 /*
18026  * Specific HBA instance event processing.
18027  *
18028  * NOTE: At the moment, device event processing is limited to hard disks
18029  * only.
18030  * Port multiplier is supported now.
18031  */
18032 static void
18033 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18034 {
18035 	int ncport;
18036 	uint32_t event_flags;
18037 	sata_address_t *saddr;
18038 	sata_cport_info_t *cportinfo;
18039 	sata_pmult_info_t *pmultinfo;
18040 
18041 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18042 	    "Processing controller %d event(s)",
18043 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18044 
18045 	mutex_enter(&sata_hba_inst->satahba_mutex);
18046 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18047 	event_flags = sata_hba_inst->satahba_event_flags;
18048 	mutex_exit(&sata_hba_inst->satahba_mutex);
18049 	/*
18050 	 * Process controller power change first
18051 	 * HERE
18052 	 */
18053 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18054 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18055 
18056 	/*
18057 	 * Search through ports/devices to identify affected port/device.
18058 	 * We may have to process events for more than one port/device.
18059 	 */
18060 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18061 		/*
18062 		 * Not all ports may be processed in attach by the time we
18063 		 * get an event. Check if port info is initialized.
18064 		 */
18065 		mutex_enter(&sata_hba_inst->satahba_mutex);
18066 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18067 		mutex_exit(&sata_hba_inst->satahba_mutex);
18068 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18069 			continue;
18070 
18071 		/* We have initialized controller port info */
18072 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18073 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18074 		    cport_event_flags;
18075 		/* Check if port was locked by IOCTL processing */
18076 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18077 			/*
18078 			 * We ignore port events because port is busy
18079 			 * with AP control processing. Set again
18080 			 * controller and main event flag, so that
18081 			 * events may be processed by the next daemon
18082 			 * run.
18083 			 */
18084 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18085 			mutex_enter(&sata_hba_inst->satahba_mutex);
18086 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18087 			mutex_exit(&sata_hba_inst->satahba_mutex);
18088 			mutex_enter(&sata_mutex);
18089 			sata_event_pending |= SATA_EVNT_MAIN;
18090 			mutex_exit(&sata_mutex);
18091 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18092 			    "Event processing postponed until "
18093 			    "AP control processing completes",
18094 			    NULL);
18095 			/* Check other ports */
18096 			continue;
18097 		} else {
18098 			/*
18099 			 * Set BSY flag so that AP control would not
18100 			 * interfere with events processing for
18101 			 * this port.
18102 			 */
18103 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18104 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18105 		}
18106 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18107 
18108 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18109 
18110 		if ((event_flags &
18111 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18112 			/*
18113 			 * Got port event.
18114 			 * We need some hierarchy of event processing as they
18115 			 * are affecting each other:
18116 			 * 1. port failed
18117 			 * 2. device detached/attached
18118 			 * 3. link events - link events may trigger device
18119 			 *    detached or device attached events in some
18120 			 *    circumstances.
18121 			 * 4. port power level changed
18122 			 */
18123 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18124 				sata_process_port_failed_event(sata_hba_inst,
18125 				    saddr);
18126 			}
18127 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18128 				sata_process_device_detached(sata_hba_inst,
18129 				    saddr);
18130 			}
18131 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18132 				sata_process_device_attached(sata_hba_inst,
18133 				    saddr);
18134 			}
18135 			if (event_flags &
18136 			    (SATA_EVNT_LINK_ESTABLISHED |
18137 			    SATA_EVNT_LINK_LOST)) {
18138 				sata_process_port_link_events(sata_hba_inst,
18139 				    saddr);
18140 			}
18141 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18142 				sata_process_port_pwr_change(sata_hba_inst,
18143 				    saddr);
18144 			}
18145 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18146 				sata_process_target_node_cleanup(
18147 				    sata_hba_inst, saddr);
18148 			}
18149 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18150 				sata_process_device_autoonline(
18151 				    sata_hba_inst, saddr);
18152 			}
18153 		}
18154 
18155 
18156 		/*
18157 		 * Scan port multiplier and all its sub-ports event flags.
18158 		 * The events are marked by
18159 		 * (1) sata_pmult_info.pmult_event_flags
18160 		 * (2) sata_pmport_info.pmport_event_flags
18161 		 */
18162 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18163 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18164 			/*
18165 			 * There should be another extra check: this
18166 			 * port multiplier still exists?
18167 			 */
18168 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18169 			    ncport);
18170 
18171 			if (pmultinfo != NULL) {
18172 				mutex_exit(&(SATA_CPORT_MUTEX(
18173 				    sata_hba_inst, ncport)));
18174 				sata_process_pmult_events(
18175 				    sata_hba_inst, ncport);
18176 				mutex_enter(&(SATA_CPORT_MUTEX(
18177 				    sata_hba_inst, ncport)));
18178 			} else {
18179 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18180 				    "Port-multiplier is gone. "
18181 				    "Ignore all sub-device events "
18182 				    "at port %d.", ncport);
18183 			}
18184 		}
18185 
18186 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18187 		    SATA_DTYPE_NONE) &&
18188 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18189 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18190 			    satadrv_event_flags &
18191 			    (SATA_EVNT_DEVICE_RESET |
18192 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18193 				/* Have device event */
18194 				sata_process_device_reset(sata_hba_inst,
18195 				    saddr);
18196 			}
18197 		}
18198 		/* Release PORT_BUSY flag */
18199 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18200 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18201 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18202 
18203 	} /* End of loop through the controller SATA ports */
18204 }
18205 
18206 /*
18207  * Specific port multiplier instance event processing. At the moment, device
18208  * event processing is limited to link/attach event only.
18209  *
18210  * NOTE: power management event is not supported yet.
18211  */
18212 static void
18213 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18214 {
18215 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18216 	sata_pmult_info_t *pmultinfo;
18217 	sata_pmport_info_t *pmportinfo;
18218 	sata_address_t *saddr;
18219 	sata_device_t sata_device;
18220 	uint32_t event_flags;
18221 	int npmport;
18222 	int rval;
18223 
18224 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18225 	    "Processing pmult event(s) on cport %d of controller %d",
18226 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18227 
18228 	/* First process events on port multiplier */
18229 	mutex_enter(&cportinfo->cport_mutex);
18230 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18231 	event_flags = pmultinfo->pmult_event_flags;
18232 
18233 	/*
18234 	 * Reset event (of port multiplier) has higher priority because the
18235 	 * port multiplier itself might be failed or removed after reset.
18236 	 */
18237 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18238 		/*
18239 		 * The status of the sub-links are uncertain,
18240 		 * so mark all sub-ports as RESET
18241 		 */
18242 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18243 		    sata_hba_inst, cport); npmport ++) {
18244 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18245 			    cport, npmport);
18246 			if (pmportinfo == NULL) {
18247 				/* That's weird. */
18248 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18249 				    "sata_hba_event_notify: "
18250 				    "invalid/un-implemented "
18251 				    "port %d:%d (%d ports), ",
18252 				    cport, npmport, SATA_NUM_PMPORTS(
18253 				    sata_hba_inst, cport)));
18254 				continue;
18255 			}
18256 
18257 			mutex_enter(&pmportinfo->pmport_mutex);
18258 
18259 			/* Mark all pmport to unknow state. */
18260 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18261 			/* Mark all pmports with link events. */
18262 			pmportinfo->pmport_event_flags =
18263 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18264 			mutex_exit(&pmportinfo->pmport_mutex);
18265 		}
18266 
18267 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18268 		/*
18269 		 * We need probe the port multiplier to know what has
18270 		 * happened.
18271 		 */
18272 		bzero(&sata_device, sizeof (sata_device_t));
18273 		sata_device.satadev_rev = SATA_DEVICE_REV;
18274 		sata_device.satadev_addr.cport = cport;
18275 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18276 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18277 
18278 		mutex_exit(&cportinfo->cport_mutex);
18279 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18280 		    (SATA_DIP(sata_hba_inst), &sata_device);
18281 		mutex_enter(&cportinfo->cport_mutex);
18282 		if (rval != SATA_SUCCESS) {
18283 			/* Something went wrong? Fail the port */
18284 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18285 			mutex_exit(&cportinfo->cport_mutex);
18286 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18287 			    "SATA port %d probing failed", cport));
18288 
18289 			/* PMult structure must be released.  */
18290 			sata_free_pmult(sata_hba_inst, &sata_device);
18291 			return;
18292 		}
18293 
18294 		sata_update_port_info(sata_hba_inst, &sata_device);
18295 
18296 		/*
18297 		 * Sanity check - Port is active? Is the link active?
18298 		 * The device is still a port multiplier?
18299 		 */
18300 		if ((cportinfo->cport_state &
18301 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18302 		    ((cportinfo->cport_scr.sstatus &
18303 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18304 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18305 			mutex_exit(&cportinfo->cport_mutex);
18306 
18307 			/* PMult structure must be released.  */
18308 			sata_free_pmult(sata_hba_inst, &sata_device);
18309 			return;
18310 		}
18311 
18312 		/* Probed succeed, set port ready. */
18313 		cportinfo->cport_state |=
18314 		    SATA_STATE_PROBED | SATA_STATE_READY;
18315 	}
18316 
18317 	/* Release port multiplier event flags. */
18318 	pmultinfo->pmult_event_flags &=
18319 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18320 	mutex_exit(&cportinfo->cport_mutex);
18321 
18322 	/*
18323 	 * Check all sub-links.
18324 	 */
18325 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18326 	    npmport ++) {
18327 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18328 		mutex_enter(&pmportinfo->pmport_mutex);
18329 		event_flags = pmportinfo->pmport_event_flags;
18330 		mutex_exit(&pmportinfo->pmport_mutex);
18331 		saddr = &pmportinfo->pmport_addr;
18332 
18333 		if ((event_flags &
18334 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18335 			/*
18336 			 * Got port multiplier port event.
18337 			 * We need some hierarchy of event processing as they
18338 			 * are affecting each other:
18339 			 * 1. device detached/attached
18340 			 * 2. link events - link events may trigger device
18341 			 *    detached or device attached events in some
18342 			 *    circumstances.
18343 			 */
18344 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18345 				sata_process_pmdevice_detached(sata_hba_inst,
18346 				    saddr);
18347 			}
18348 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18349 				sata_process_pmdevice_attached(sata_hba_inst,
18350 				    saddr);
18351 			}
18352 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18353 			    event_flags & SATA_EVNT_LINK_LOST) {
18354 				sata_process_pmport_link_events(sata_hba_inst,
18355 				    saddr);
18356 			}
18357 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18358 				sata_process_target_node_cleanup(
18359 				    sata_hba_inst, saddr);
18360 			}
18361 		}
18362 
18363 		/* Checking drive event(s). */
18364 		mutex_enter(&pmportinfo->pmport_mutex);
18365 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18366 		    pmportinfo->pmport_sata_drive != NULL) {
18367 			event_flags = pmportinfo->pmport_sata_drive->
18368 			    satadrv_event_flags;
18369 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18370 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18371 
18372 				/* Have device event */
18373 				sata_process_pmdevice_reset(sata_hba_inst,
18374 				    saddr);
18375 			}
18376 		}
18377 		mutex_exit(&pmportinfo->pmport_mutex);
18378 
18379 		/* Release PORT_BUSY flag */
18380 		mutex_enter(&cportinfo->cport_mutex);
18381 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18382 		mutex_exit(&cportinfo->cport_mutex);
18383 	}
18384 
18385 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18386 	    "[DONE] pmult event(s) on cport %d of controller %d",
18387 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18388 }
18389 
18390 /*
18391  * Process HBA power level change reported by HBA driver.
18392  * Not implemented at this time - event is ignored.
18393  */
18394 static void
18395 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18396 {
18397 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18398 	    "Processing controller power level change", NULL);
18399 
18400 	/* Ignoring it for now */
18401 	mutex_enter(&sata_hba_inst->satahba_mutex);
18402 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18403 	mutex_exit(&sata_hba_inst->satahba_mutex);
18404 }
18405 
18406 /*
18407  * Process port power level change reported by HBA driver.
18408  * Not implemented at this time - event is ignored.
18409  */
18410 static void
18411 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18412     sata_address_t *saddr)
18413 {
18414 	sata_cport_info_t *cportinfo;
18415 
18416 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18417 	    "Processing port power level change", NULL);
18418 
18419 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18420 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18421 	/* Reset event flag */
18422 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18423 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18424 }
18425 
18426 /*
18427  * Process port failure reported by HBA driver.
18428  * cports support only - no pmports.
18429  */
18430 static void
18431 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18432     sata_address_t *saddr)
18433 {
18434 	sata_cport_info_t *cportinfo;
18435 
18436 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18437 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18438 	/* Reset event flag first */
18439 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18440 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18441 	if ((cportinfo->cport_state &
18442 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18443 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18444 		    cport_mutex);
18445 		return;
18446 	}
18447 	/* Fail the port */
18448 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18449 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18450 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18451 }
18452 
18453 /*
18454  * Device Reset Event processing.
18455  * The seqeunce is managed by 3 stage flags:
18456  * - reset event reported,
18457  * - reset event being processed,
18458  * - request to clear device reset state.
18459  *
18460  * NOTE: This function has to be entered with cport mutex held. It exits with
18461  * mutex held as well, but can release mutex during the processing.
18462  */
18463 static void
18464 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18465     sata_address_t *saddr)
18466 {
18467 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18468 	sata_drive_info_t *sdinfo;
18469 	sata_cport_info_t *cportinfo;
18470 	sata_device_t sata_device;
18471 	int rval_probe, rval_set;
18472 
18473 	/* We only care about host sata cport for now */
18474 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18475 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18476 	/*
18477 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18478 	 * state, ignore reset event.
18479 	 */
18480 	if (((cportinfo->cport_state &
18481 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18482 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18483 		sdinfo->satadrv_event_flags &=
18484 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18485 		return;
18486 	}
18487 
18488 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18489 	    SATA_DTYPE_PMULT)) {
18490 		/*
18491 		 * Should not happened: this is already handled in
18492 		 * sata_hba_event_notify()
18493 		 */
18494 		mutex_exit(&cportinfo->cport_mutex);
18495 		goto done;
18496 	}
18497 
18498 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18499 	    SATA_VALID_DEV_TYPE) == 0) {
18500 		/*
18501 		 * This should not happen - coding error.
18502 		 * But we can recover, so do not panic, just clean up
18503 		 * and if in debug mode, log the message.
18504 		 */
18505 #ifdef SATA_DEBUG
18506 		sata_log(sata_hba_inst, CE_WARN,
18507 		    "sata_process_device_reset: "
18508 		    "Invalid device type with sdinfo!", NULL);
18509 #endif
18510 		sdinfo->satadrv_event_flags = 0;
18511 		return;
18512 	}
18513 
18514 #ifdef SATA_DEBUG
18515 	if ((sdinfo->satadrv_event_flags &
18516 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18517 		/* Nothing to do */
18518 		/* Something is weird - why we are processing dev reset? */
18519 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18520 		    "No device reset event!!!!", NULL);
18521 
18522 		return;
18523 	}
18524 	if ((sdinfo->satadrv_event_flags &
18525 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18526 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18527 		/* Something is weird - new device reset event */
18528 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18529 		    "Overlapping device reset events!", NULL);
18530 	}
18531 #endif
18532 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18533 	    "Processing port %d device reset", saddr->cport);
18534 
18535 	/* Clear event flag */
18536 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18537 
18538 	/* It seems that we always need to check the port state first */
18539 	sata_device.satadev_rev = SATA_DEVICE_REV;
18540 	sata_device.satadev_addr = *saddr;
18541 	/*
18542 	 * We have to exit mutex, because the HBA probe port function may
18543 	 * block on its own mutex.
18544 	 */
18545 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18546 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18547 	    (SATA_DIP(sata_hba_inst), &sata_device);
18548 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18549 	sata_update_port_info(sata_hba_inst, &sata_device);
18550 	if (rval_probe != SATA_SUCCESS) {
18551 		/* Something went wrong? Fail the port */
18552 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18553 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18554 		if (sdinfo != NULL)
18555 			sdinfo->satadrv_event_flags = 0;
18556 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18557 		    cport_mutex);
18558 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18559 		    "SATA port %d probing failed",
18560 		    saddr->cport));
18561 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18562 		    saddr->cport)->cport_mutex);
18563 		return;
18564 	}
18565 	if ((sata_device.satadev_scr.sstatus  &
18566 	    SATA_PORT_DEVLINK_UP_MASK) !=
18567 	    SATA_PORT_DEVLINK_UP ||
18568 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18569 		/*
18570 		 * No device to process, anymore. Some other event processing
18571 		 * would or have already performed port info cleanup.
18572 		 * To be safe (HBA may need it), request clearing device
18573 		 * reset condition.
18574 		 */
18575 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18576 		if (sdinfo != NULL) {
18577 			sdinfo->satadrv_event_flags &=
18578 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18579 			sdinfo->satadrv_event_flags |=
18580 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18581 		}
18582 		return;
18583 	}
18584 
18585 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18586 	if (sdinfo == NULL) {
18587 		return;
18588 	}
18589 	if ((sdinfo->satadrv_event_flags &
18590 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18591 		/*
18592 		 * Start tracking time for device feature restoration and
18593 		 * identification. Save current time (lbolt value).
18594 		 */
18595 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18596 	}
18597 	/* Mark device reset processing as active */
18598 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18599 
18600 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18601 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18602 
18603 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18604 
18605 	if (rval_set  != SATA_SUCCESS) {
18606 		/*
18607 		 * Restoring drive setting failed.
18608 		 * Probe the port first, to check if the port state has changed
18609 		 */
18610 		sata_device.satadev_rev = SATA_DEVICE_REV;
18611 		sata_device.satadev_addr = *saddr;
18612 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18613 		/* probe port */
18614 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18615 		    (SATA_DIP(sata_hba_inst), &sata_device);
18616 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18617 		    cport_mutex);
18618 		if (rval_probe == SATA_SUCCESS &&
18619 		    (sata_device.satadev_state &
18620 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18621 		    (sata_device.satadev_scr.sstatus  &
18622 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18623 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18624 			/*
18625 			 * We may retry this a bit later - in-process reset
18626 			 * condition should be already set.
18627 			 * Track retry time for device identification.
18628 			 */
18629 			if ((cportinfo->cport_dev_type &
18630 			    SATA_VALID_DEV_TYPE) != 0 &&
18631 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18632 			    sdinfo->satadrv_reset_time != 0) {
18633 				clock_t cur_time = ddi_get_lbolt();
18634 				/*
18635 				 * If the retry time limit was not
18636 				 * exceeded, retry.
18637 				 */
18638 				if ((cur_time - sdinfo->satadrv_reset_time) <
18639 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18640 					mutex_enter(
18641 					    &sata_hba_inst->satahba_mutex);
18642 					sata_hba_inst->satahba_event_flags |=
18643 					    SATA_EVNT_MAIN;
18644 					mutex_exit(
18645 					    &sata_hba_inst->satahba_mutex);
18646 					mutex_enter(&sata_mutex);
18647 					sata_event_pending |= SATA_EVNT_MAIN;
18648 					mutex_exit(&sata_mutex);
18649 					return;
18650 				}
18651 				if (rval_set == SATA_RETRY) {
18652 					/*
18653 					 * Setting drive features failed, but
18654 					 * the drive is still accessible,
18655 					 * so emit a warning message before
18656 					 * return.
18657 					 */
18658 					mutex_exit(&SATA_CPORT_INFO(
18659 					    sata_hba_inst,
18660 					    saddr->cport)->cport_mutex);
18661 					goto done;
18662 				}
18663 			}
18664 			/* Fail the drive */
18665 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18666 
18667 			sata_log(sata_hba_inst, CE_WARN,
18668 			    "SATA device at port %d - device failed",
18669 			    saddr->cport);
18670 
18671 			DTRACE_PROBE(port_failed_f);
18672 		}
18673 		/*
18674 		 * No point of retrying - device failed or some other event
18675 		 * processing or already did or will do port info cleanup.
18676 		 * To be safe (HBA may need it),
18677 		 * request clearing device reset condition.
18678 		 */
18679 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18680 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18681 		sdinfo->satadrv_reset_time = 0;
18682 		return;
18683 	}
18684 done:
18685 	/*
18686 	 * If setting of drive features failed, but the drive is still
18687 	 * accessible, emit a warning message.
18688 	 */
18689 	if (rval_set == SATA_RETRY) {
18690 		sata_log(sata_hba_inst, CE_WARN,
18691 		    "SATA device at port %d - desired setting could not be "
18692 		    "restored after reset. Device may not operate as expected.",
18693 		    saddr->cport);
18694 	}
18695 	/*
18696 	 * Raise the flag indicating that the next sata command could
18697 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18698 	 * reset is reported.
18699 	 */
18700 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18701 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18702 		sdinfo->satadrv_reset_time = 0;
18703 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18704 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18705 			sdinfo->satadrv_event_flags &=
18706 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18707 			sdinfo->satadrv_event_flags |=
18708 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18709 		}
18710 	}
18711 }
18712 
18713 
18714 /*
18715  * Port Multiplier Port Device Reset Event processing.
18716  *
18717  * NOTE: This function has to be entered with pmport mutex held. It exits with
18718  * mutex held as well, but can release mutex during the processing.
18719  */
18720 static void
18721 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18722     sata_address_t *saddr)
18723 {
18724 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18725 	sata_drive_info_t *sdinfo = NULL;
18726 	sata_cport_info_t *cportinfo = NULL;
18727 	sata_pmport_info_t *pmportinfo = NULL;
18728 	sata_pmult_info_t *pminfo = NULL;
18729 	sata_device_t sata_device;
18730 	uint8_t cport = saddr->cport;
18731 	uint8_t pmport = saddr->pmport;
18732 	int rval;
18733 
18734 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18735 	    "Processing drive reset at port %d:%d", cport, pmport);
18736 
18737 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18738 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18739 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18740 
18741 	/*
18742 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18743 	 * state, ignore reset event.
18744 	 */
18745 	if (((cportinfo->cport_state &
18746 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18747 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18748 		sdinfo->satadrv_event_flags &=
18749 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18750 		return;
18751 	}
18752 
18753 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18754 		/*
18755 		 * This should not happen - coding error.
18756 		 * But we can recover, so do not panic, just clean up
18757 		 * and if in debug mode, log the message.
18758 		 */
18759 #ifdef SATA_DEBUG
18760 		sata_log(sata_hba_inst, CE_WARN,
18761 		    "sata_process_pmdevice_reset: "
18762 		    "Invalid device type with sdinfo!", NULL);
18763 #endif
18764 		sdinfo->satadrv_event_flags = 0;
18765 		return;
18766 	}
18767 
18768 #ifdef SATA_DEBUG
18769 	if ((sdinfo->satadrv_event_flags &
18770 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18771 		/* Nothing to do */
18772 		/* Something is weird - why we are processing dev reset? */
18773 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18774 		    "No device reset event!!!!", NULL);
18775 
18776 		return;
18777 	}
18778 	if ((sdinfo->satadrv_event_flags &
18779 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18780 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18781 		/* Something is weird - new device reset event */
18782 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18783 		    "Overlapping device reset events!", NULL);
18784 	}
18785 #endif
18786 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18787 	    "Processing port %d:%d device reset", cport, pmport);
18788 
18789 	/* Clear event flag */
18790 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18791 
18792 	/* It seems that we always need to check the port state first */
18793 	sata_device.satadev_rev = SATA_DEVICE_REV;
18794 	sata_device.satadev_addr = *saddr;
18795 	/*
18796 	 * We have to exit mutex, because the HBA probe port function may
18797 	 * block on its own mutex.
18798 	 */
18799 	mutex_exit(&pmportinfo->pmport_mutex);
18800 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18801 	    (SATA_DIP(sata_hba_inst), &sata_device);
18802 	mutex_enter(&pmportinfo->pmport_mutex);
18803 
18804 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18805 	if (rval != SATA_SUCCESS) {
18806 		/* Something went wrong? Fail the port */
18807 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18808 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18809 		    saddr->pmport);
18810 		if (sdinfo != NULL)
18811 			sdinfo->satadrv_event_flags = 0;
18812 		mutex_exit(&pmportinfo->pmport_mutex);
18813 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18814 		    "SATA port %d:%d probing failed",
18815 		    saddr->cport, saddr->pmport));
18816 		mutex_enter(&pmportinfo->pmport_mutex);
18817 		return;
18818 	}
18819 	if ((sata_device.satadev_scr.sstatus  &
18820 	    SATA_PORT_DEVLINK_UP_MASK) !=
18821 	    SATA_PORT_DEVLINK_UP ||
18822 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18823 		/*
18824 		 * No device to process, anymore. Some other event processing
18825 		 * would or have already performed port info cleanup.
18826 		 * To be safe (HBA may need it), request clearing device
18827 		 * reset condition.
18828 		 */
18829 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18830 		    saddr->pmport);
18831 		if (sdinfo != NULL) {
18832 			sdinfo->satadrv_event_flags &=
18833 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18834 			/* must clear flags on cport */
18835 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18836 			    saddr->cport);
18837 			pminfo->pmult_event_flags |=
18838 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18839 		}
18840 		return;
18841 	}
18842 
18843 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18844 	    saddr->pmport);
18845 	if (sdinfo == NULL) {
18846 		return;
18847 	}
18848 	if ((sdinfo->satadrv_event_flags &
18849 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18850 		/*
18851 		 * Start tracking time for device feature restoration and
18852 		 * identification. Save current time (lbolt value).
18853 		 */
18854 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18855 	}
18856 	/* Mark device reset processing as active */
18857 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18858 
18859 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18860 	mutex_exit(&pmportinfo->pmport_mutex);
18861 
18862 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18863 	    SATA_FAILURE) {
18864 		/*
18865 		 * Restoring drive setting failed.
18866 		 * Probe the port first, to check if the port state has changed
18867 		 */
18868 		sata_device.satadev_rev = SATA_DEVICE_REV;
18869 		sata_device.satadev_addr = *saddr;
18870 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18871 
18872 		/* probe port */
18873 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18874 		    (SATA_DIP(sata_hba_inst), &sata_device);
18875 		mutex_enter(&pmportinfo->pmport_mutex);
18876 		if (rval == SATA_SUCCESS &&
18877 		    (sata_device.satadev_state &
18878 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18879 		    (sata_device.satadev_scr.sstatus  &
18880 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18881 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18882 			/*
18883 			 * We may retry this a bit later - in-process reset
18884 			 * condition should be already set.
18885 			 * Track retry time for device identification.
18886 			 */
18887 			if ((pmportinfo->pmport_dev_type &
18888 			    SATA_VALID_DEV_TYPE) != 0 &&
18889 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18890 			    sdinfo->satadrv_reset_time != 0) {
18891 				clock_t cur_time = ddi_get_lbolt();
18892 				/*
18893 				 * If the retry time limit was not
18894 				 * exceeded, retry.
18895 				 */
18896 				if ((cur_time - sdinfo->satadrv_reset_time) <
18897 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18898 					mutex_enter(
18899 					    &sata_hba_inst->satahba_mutex);
18900 					sata_hba_inst->satahba_event_flags |=
18901 					    SATA_EVNT_MAIN;
18902 					mutex_exit(
18903 					    &sata_hba_inst->satahba_mutex);
18904 					mutex_enter(&sata_mutex);
18905 					sata_event_pending |= SATA_EVNT_MAIN;
18906 					mutex_exit(&sata_mutex);
18907 					return;
18908 				}
18909 			}
18910 			/* Fail the drive */
18911 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18912 
18913 			sata_log(sata_hba_inst, CE_WARN,
18914 			    "SATA device at port %d:%d - device failed",
18915 			    saddr->cport, saddr->pmport);
18916 		} else {
18917 			/*
18918 			 * No point of retrying - some other event processing
18919 			 * would or already did port info cleanup.
18920 			 * To be safe (HBA may need it),
18921 			 * request clearing device reset condition.
18922 			 */
18923 			sdinfo->satadrv_event_flags |=
18924 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18925 		}
18926 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18927 		sdinfo->satadrv_reset_time = 0;
18928 		return;
18929 	}
18930 	/*
18931 	 * Raise the flag indicating that the next sata command could
18932 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18933 	 * reset is reported.
18934 	 */
18935 	mutex_enter(&pmportinfo->pmport_mutex);
18936 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18937 		sdinfo->satadrv_reset_time = 0;
18938 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
18939 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18940 			sdinfo->satadrv_event_flags &=
18941 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18942 			/* must clear flags on cport */
18943 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18944 			    saddr->cport);
18945 			pminfo->pmult_event_flags |=
18946 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18947 		}
18948 	}
18949 }
18950 
18951 /*
18952  * Port Link Events processing.
18953  * Every link established event may involve device reset (due to
18954  * COMRESET signal, equivalent of the hard reset) so arbitrarily
18955  * set device reset event for an attached device (if any).
18956  * If the port is in SHUTDOWN or FAILED state, ignore link events.
18957  *
18958  * The link established event processing varies, depending on the state
18959  * of the target node, HBA hotplugging capabilities, state of the port.
18960  * If the link is not active, the link established event is ignored.
18961  * If HBA cannot detect device attachment and there is no target node,
18962  * the link established event triggers device attach event processing.
18963  * Else, link established event triggers device reset event processing.
18964  *
18965  * The link lost event processing varies, depending on a HBA hotplugging
18966  * capability and the state of the port (link active or not active).
18967  * If the link is active, the lost link event is ignored.
18968  * If HBA cannot detect device removal, the lost link event triggers
18969  * device detached event processing after link lost timeout.
18970  * Else, the event is ignored.
18971  *
18972  * NOTE: Port multiplier ports events are handled by
18973  * sata_process_pmport_link_events();
18974  */
18975 static void
18976 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
18977     sata_address_t *saddr)
18978 {
18979 	sata_device_t sata_device;
18980 	sata_cport_info_t *cportinfo;
18981 	sata_drive_info_t *sdinfo;
18982 	uint32_t event_flags;
18983 	int rval;
18984 
18985 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18986 	    "Processing port %d link event(s)", saddr->cport);
18987 
18988 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18989 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18990 	event_flags = cportinfo->cport_event_flags;
18991 
18992 	/* Reset event flags first */
18993 	cportinfo->cport_event_flags &=
18994 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
18995 
18996 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
18997 	if ((cportinfo->cport_state &
18998 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18999 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19000 		    cport_mutex);
19001 		return;
19002 	}
19003 
19004 	/*
19005 	 * For the sanity sake get current port state.
19006 	 * Set device address only. Other sata_device fields should be
19007 	 * set by HBA driver.
19008 	 */
19009 	sata_device.satadev_rev = SATA_DEVICE_REV;
19010 	sata_device.satadev_addr = *saddr;
19011 	/*
19012 	 * We have to exit mutex, because the HBA probe port function may
19013 	 * block on its own mutex.
19014 	 */
19015 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19016 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19017 	    (SATA_DIP(sata_hba_inst), &sata_device);
19018 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19019 	sata_update_port_info(sata_hba_inst, &sata_device);
19020 	if (rval != SATA_SUCCESS) {
19021 		/* Something went wrong? Fail the port */
19022 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19023 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19024 		    cport_mutex);
19025 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19026 		    "SATA port %d probing failed",
19027 		    saddr->cport));
19028 		/*
19029 		 * We may want to release device info structure, but
19030 		 * it is not necessary.
19031 		 */
19032 		return;
19033 	} else {
19034 		/* port probed successfully */
19035 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19036 	}
19037 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19038 
19039 		if ((sata_device.satadev_scr.sstatus &
19040 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19041 			/* Ignore event */
19042 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19043 			    "Ignoring port %d link established event - "
19044 			    "link down",
19045 			    saddr->cport);
19046 			goto linklost;
19047 		}
19048 
19049 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19050 		    "Processing port %d link established event",
19051 		    saddr->cport);
19052 
19053 		/*
19054 		 * For the sanity sake check if a device is attached - check
19055 		 * return state of a port probing.
19056 		 */
19057 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19058 			/*
19059 			 * HBA port probe indicated that there is a device
19060 			 * attached. Check if the framework had device info
19061 			 * structure attached for this device.
19062 			 */
19063 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19064 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19065 				    NULL);
19066 
19067 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19068 				if ((sdinfo->satadrv_type &
19069 				    SATA_VALID_DEV_TYPE) != 0) {
19070 					/*
19071 					 * Dev info structure is present.
19072 					 * If dev_type is set to known type in
19073 					 * the framework's drive info struct
19074 					 * then the device existed before and
19075 					 * the link was probably lost
19076 					 * momentarily - in such case
19077 					 * we may want to check device
19078 					 * identity.
19079 					 * Identity check is not supported now.
19080 					 *
19081 					 * Link established event
19082 					 * triggers device reset event.
19083 					 */
19084 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19085 					    satadrv_event_flags |=
19086 					    SATA_EVNT_DEVICE_RESET;
19087 				}
19088 			} else if (cportinfo->cport_dev_type ==
19089 			    SATA_DTYPE_NONE) {
19090 				/*
19091 				 * We got new device attached! If HBA does not
19092 				 * generate device attached events, trigger it
19093 				 * here.
19094 				 */
19095 				if (!(SATA_FEATURES(sata_hba_inst) &
19096 				    SATA_CTLF_HOTPLUG)) {
19097 					cportinfo->cport_event_flags |=
19098 					    SATA_EVNT_DEVICE_ATTACHED;
19099 				}
19100 			}
19101 			/* Reset link lost timeout */
19102 			cportinfo->cport_link_lost_time = 0;
19103 		}
19104 	}
19105 linklost:
19106 	if (event_flags & SATA_EVNT_LINK_LOST) {
19107 		if ((sata_device.satadev_scr.sstatus &
19108 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19109 			/* Ignore event */
19110 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19111 			    "Ignoring port %d link lost event - link is up",
19112 			    saddr->cport);
19113 			goto done;
19114 		}
19115 #ifdef SATA_DEBUG
19116 		if (cportinfo->cport_link_lost_time == 0) {
19117 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19118 			    "Processing port %d link lost event",
19119 			    saddr->cport);
19120 		}
19121 #endif
19122 		/*
19123 		 * When HBA cannot generate device attached/detached events,
19124 		 * we need to track link lost time and eventually generate
19125 		 * device detach event.
19126 		 */
19127 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19128 			/* We are tracking link lost time */
19129 			if (cportinfo->cport_link_lost_time == 0) {
19130 				/* save current time (lbolt value) */
19131 				cportinfo->cport_link_lost_time =
19132 				    ddi_get_lbolt();
19133 				/* just keep link lost event */
19134 				cportinfo->cport_event_flags |=
19135 				    SATA_EVNT_LINK_LOST;
19136 			} else {
19137 				clock_t cur_time = ddi_get_lbolt();
19138 				if ((cur_time -
19139 				    cportinfo->cport_link_lost_time) >=
19140 				    drv_usectohz(
19141 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19142 					/* trigger device detach event */
19143 					cportinfo->cport_event_flags |=
19144 					    SATA_EVNT_DEVICE_DETACHED;
19145 					cportinfo->cport_link_lost_time = 0;
19146 					SATADBG1(SATA_DBG_EVENTS,
19147 					    sata_hba_inst,
19148 					    "Triggering port %d "
19149 					    "device detached event",
19150 					    saddr->cport);
19151 				} else {
19152 					/* keep link lost event */
19153 					cportinfo->cport_event_flags |=
19154 					    SATA_EVNT_LINK_LOST;
19155 				}
19156 			}
19157 		}
19158 		/*
19159 		 * We could change port state to disable/delay access to
19160 		 * the attached device until the link is recovered.
19161 		 */
19162 	}
19163 done:
19164 	event_flags = cportinfo->cport_event_flags;
19165 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19166 	if (event_flags != 0) {
19167 		mutex_enter(&sata_hba_inst->satahba_mutex);
19168 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19169 		mutex_exit(&sata_hba_inst->satahba_mutex);
19170 		mutex_enter(&sata_mutex);
19171 		sata_event_pending |= SATA_EVNT_MAIN;
19172 		mutex_exit(&sata_mutex);
19173 	}
19174 }
19175 
19176 /*
19177  * Port Multiplier Port Link Events processing.
19178  */
19179 static void
19180 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19181     sata_address_t *saddr)
19182 {
19183 	sata_device_t sata_device;
19184 	sata_pmport_info_t *pmportinfo = NULL;
19185 	sata_drive_info_t *sdinfo = NULL;
19186 	uint32_t event_flags;
19187 	uint8_t cport = saddr->cport;
19188 	uint8_t pmport = saddr->pmport;
19189 	int rval;
19190 
19191 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19192 	    "Processing port %d:%d link event(s)",
19193 	    cport, pmport);
19194 
19195 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19196 	mutex_enter(&pmportinfo->pmport_mutex);
19197 	event_flags = pmportinfo->pmport_event_flags;
19198 
19199 	/* Reset event flags first */
19200 	pmportinfo->pmport_event_flags &=
19201 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19202 
19203 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19204 	if ((pmportinfo->pmport_state &
19205 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19206 		mutex_exit(&pmportinfo->pmport_mutex);
19207 		return;
19208 	}
19209 
19210 	/*
19211 	 * For the sanity sake get current port state.
19212 	 * Set device address only. Other sata_device fields should be
19213 	 * set by HBA driver.
19214 	 */
19215 	sata_device.satadev_rev = SATA_DEVICE_REV;
19216 	sata_device.satadev_addr = *saddr;
19217 	/*
19218 	 * We have to exit mutex, because the HBA probe port function may
19219 	 * block on its own mutex.
19220 	 */
19221 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19222 	    saddr->pmport));
19223 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19224 	    (SATA_DIP(sata_hba_inst), &sata_device);
19225 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19226 	    saddr->pmport));
19227 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19228 	if (rval != SATA_SUCCESS) {
19229 		/* Something went wrong? Fail the port */
19230 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19231 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19232 		    saddr->pmport));
19233 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19234 		    "SATA port %d:%d probing failed",
19235 		    saddr->cport, saddr->pmport));
19236 		/*
19237 		 * We may want to release device info structure, but
19238 		 * it is not necessary.
19239 		 */
19240 		return;
19241 	} else {
19242 		/* port probed successfully */
19243 		pmportinfo->pmport_state |=
19244 		    SATA_STATE_PROBED | SATA_STATE_READY;
19245 	}
19246 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19247 	    saddr->cport, saddr->pmport));
19248 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19249 	    saddr->cport, saddr->pmport));
19250 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19251 
19252 		if ((sata_device.satadev_scr.sstatus &
19253 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19254 			/* Ignore event */
19255 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19256 			    "Ignoring port %d:%d link established event - "
19257 			    "link down",
19258 			    saddr->cport, saddr->pmport);
19259 			goto linklost;
19260 		}
19261 
19262 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19263 		    "Processing port %d:%d link established event",
19264 		    cport, pmport);
19265 
19266 		/*
19267 		 * For the sanity sake check if a device is attached - check
19268 		 * return state of a port probing.
19269 		 */
19270 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19271 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19272 			/*
19273 			 * HBA port probe indicated that there is a device
19274 			 * attached. Check if the framework had device info
19275 			 * structure attached for this device.
19276 			 */
19277 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19278 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19279 				    NULL);
19280 
19281 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19282 				if ((sdinfo->satadrv_type &
19283 				    SATA_VALID_DEV_TYPE) != 0) {
19284 					/*
19285 					 * Dev info structure is present.
19286 					 * If dev_type is set to known type in
19287 					 * the framework's drive info struct
19288 					 * then the device existed before and
19289 					 * the link was probably lost
19290 					 * momentarily - in such case
19291 					 * we may want to check device
19292 					 * identity.
19293 					 * Identity check is not supported now.
19294 					 *
19295 					 * Link established event
19296 					 * triggers device reset event.
19297 					 */
19298 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19299 					    satadrv_event_flags |=
19300 					    SATA_EVNT_DEVICE_RESET;
19301 				}
19302 			} else if (pmportinfo->pmport_dev_type ==
19303 			    SATA_DTYPE_NONE) {
19304 				/*
19305 				 * We got new device attached! If HBA does not
19306 				 * generate device attached events, trigger it
19307 				 * here.
19308 				 */
19309 				if (!(SATA_FEATURES(sata_hba_inst) &
19310 				    SATA_CTLF_HOTPLUG)) {
19311 					pmportinfo->pmport_event_flags |=
19312 					    SATA_EVNT_DEVICE_ATTACHED;
19313 				}
19314 			}
19315 			/* Reset link lost timeout */
19316 			pmportinfo->pmport_link_lost_time = 0;
19317 		}
19318 	}
19319 linklost:
19320 	if (event_flags & SATA_EVNT_LINK_LOST) {
19321 #ifdef SATA_DEBUG
19322 		if (pmportinfo->pmport_link_lost_time == 0) {
19323 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19324 			    "Processing port %d:%d link lost event",
19325 			    saddr->cport, saddr->pmport);
19326 		}
19327 #endif
19328 		if ((sata_device.satadev_scr.sstatus &
19329 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19330 			/* Ignore event */
19331 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19332 			    "Ignoring port %d:%d link lost event - link is up",
19333 			    saddr->cport, saddr->pmport);
19334 			goto done;
19335 		}
19336 		/*
19337 		 * When HBA cannot generate device attached/detached events,
19338 		 * we need to track link lost time and eventually generate
19339 		 * device detach event.
19340 		 */
19341 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19342 			/* We are tracking link lost time */
19343 			if (pmportinfo->pmport_link_lost_time == 0) {
19344 				/* save current time (lbolt value) */
19345 				pmportinfo->pmport_link_lost_time =
19346 				    ddi_get_lbolt();
19347 				/* just keep link lost event */
19348 				pmportinfo->pmport_event_flags |=
19349 				    SATA_EVNT_LINK_LOST;
19350 			} else {
19351 				clock_t cur_time = ddi_get_lbolt();
19352 				if ((cur_time -
19353 				    pmportinfo->pmport_link_lost_time) >=
19354 				    drv_usectohz(
19355 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19356 					/* trigger device detach event */
19357 					pmportinfo->pmport_event_flags |=
19358 					    SATA_EVNT_DEVICE_DETACHED;
19359 					pmportinfo->pmport_link_lost_time = 0;
19360 					SATADBG2(SATA_DBG_EVENTS,
19361 					    sata_hba_inst,
19362 					    "Triggering port %d:%d "
19363 					    "device detached event",
19364 					    saddr->cport, saddr->pmport);
19365 				} else {
19366 					/* keep link lost event */
19367 					pmportinfo->pmport_event_flags |=
19368 					    SATA_EVNT_LINK_LOST;
19369 				}
19370 			}
19371 		}
19372 		/*
19373 		 * We could change port state to disable/delay access to
19374 		 * the attached device until the link is recovered.
19375 		 */
19376 	}
19377 done:
19378 	event_flags = pmportinfo->pmport_event_flags;
19379 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19380 	    saddr->pmport));
19381 	if (event_flags != 0) {
19382 		mutex_enter(&sata_hba_inst->satahba_mutex);
19383 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19384 		mutex_exit(&sata_hba_inst->satahba_mutex);
19385 		mutex_enter(&sata_mutex);
19386 		sata_event_pending |= SATA_EVNT_MAIN;
19387 		mutex_exit(&sata_mutex);
19388 	}
19389 }
19390 
19391 /*
19392  * Device Detached Event processing.
19393  * Port is probed to find if a device is really gone. If so,
19394  * the device info structure is detached from the SATA port info structure
19395  * and released.
19396  * Port status is updated.
19397  *
19398  * NOTE: Port multiplier ports events are handled by
19399  * sata_process_pmdevice_detached()
19400  */
19401 static void
19402 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19403     sata_address_t *saddr)
19404 {
19405 	sata_cport_info_t *cportinfo;
19406 	sata_pmport_info_t *pmportinfo;
19407 	sata_drive_info_t *sdevinfo;
19408 	sata_device_t sata_device;
19409 	sata_address_t pmport_addr;
19410 	char name[16];
19411 	uint8_t cport = saddr->cport;
19412 	int npmport;
19413 	int rval;
19414 
19415 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19416 	    "Processing port %d device detached", saddr->cport);
19417 
19418 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19419 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19420 	/* Clear event flag */
19421 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19422 
19423 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19424 	if ((cportinfo->cport_state &
19425 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19426 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19427 		    cport_mutex);
19428 		return;
19429 	}
19430 	/* For sanity, re-probe the port */
19431 	sata_device.satadev_rev = SATA_DEVICE_REV;
19432 	sata_device.satadev_addr = *saddr;
19433 
19434 	/*
19435 	 * We have to exit mutex, because the HBA probe port function may
19436 	 * block on its own mutex.
19437 	 */
19438 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19439 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19440 	    (SATA_DIP(sata_hba_inst), &sata_device);
19441 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19442 	sata_update_port_info(sata_hba_inst, &sata_device);
19443 	if (rval != SATA_SUCCESS) {
19444 		/* Something went wrong? Fail the port */
19445 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19446 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19447 		    cport_mutex);
19448 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19449 		    "SATA port %d probing failed",
19450 		    saddr->cport));
19451 		/*
19452 		 * We may want to release device info structure, but
19453 		 * it is not necessary.
19454 		 */
19455 		return;
19456 	} else {
19457 		/* port probed successfully */
19458 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19459 	}
19460 	/*
19461 	 * Check if a device is still attached. For sanity, check also
19462 	 * link status - if no link, there is no device.
19463 	 */
19464 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19465 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19466 	    SATA_DTYPE_NONE) {
19467 		/*
19468 		 * Device is still attached - ignore detach event.
19469 		 */
19470 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19471 		    cport_mutex);
19472 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19473 		    "Ignoring detach - device still attached to port %d",
19474 		    sata_device.satadev_addr.cport);
19475 		return;
19476 	}
19477 	/*
19478 	 * We need to detach and release device info structure here
19479 	 */
19480 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19481 		/*
19482 		 * A port-multiplier is removed.
19483 		 *
19484 		 * Calling sata_process_pmdevice_detached() does not work
19485 		 * here. The port multiplier is gone, so we cannot probe
19486 		 * sub-port any more and all pmult-related data structure must
19487 		 * be de-allocated immediately. Following structure of every
19488 		 * implemented sub-port behind the pmult are required to
19489 		 * released.
19490 		 *
19491 		 *   - attachment point
19492 		 *   - target node
19493 		 *   - sata_drive_info
19494 		 *   - sata_pmport_info
19495 		 */
19496 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19497 		    cport); npmport ++) {
19498 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19499 			    sata_hba_inst,
19500 			    "Detaching target node at port %d:%d",
19501 			    cport, npmport);
19502 
19503 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19504 
19505 			/* Remove attachment point. */
19506 			name[0] = '\0';
19507 			(void) sprintf(name, "%d.%d", cport, npmport);
19508 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19509 			sata_log(sata_hba_inst, CE_NOTE,
19510 			    "Remove attachment point of port %d:%d",
19511 			    cport, npmport);
19512 
19513 			/* Remove target node */
19514 			pmport_addr.cport = cport;
19515 			pmport_addr.pmport = (uint8_t)npmport;
19516 			pmport_addr.qual = SATA_ADDR_PMPORT;
19517 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19518 
19519 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19520 
19521 			/* Release sata_pmport_info & sata_drive_info. */
19522 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19523 			    cport, npmport);
19524 			ASSERT(pmportinfo != NULL);
19525 
19526 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19527 			if (sdevinfo != NULL) {
19528 				(void) kmem_free((void *) sdevinfo,
19529 				    sizeof (sata_drive_info_t));
19530 			}
19531 
19532 			/* Release sata_pmport_info at last */
19533 			(void) kmem_free((void *) pmportinfo,
19534 			    sizeof (sata_pmport_info_t));
19535 		}
19536 
19537 		/* Finally, release sata_pmult_info */
19538 		(void) kmem_free((void *)
19539 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19540 		    sizeof (sata_pmult_info_t));
19541 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19542 
19543 		sata_log(sata_hba_inst, CE_WARN,
19544 		    "SATA port-multiplier detached at port %d", cport);
19545 
19546 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19547 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19548 		    saddr->cport)->cport_mutex);
19549 	} else {
19550 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19551 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19552 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19553 			(void) kmem_free((void *)sdevinfo,
19554 			    sizeof (sata_drive_info_t));
19555 		}
19556 		sata_log(sata_hba_inst, CE_WARN,
19557 		    "SATA device detached at port %d", cport);
19558 
19559 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19560 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19561 		    saddr->cport)->cport_mutex);
19562 
19563 		/*
19564 		 * Try to offline a device and remove target node
19565 		 * if it still exists
19566 		 */
19567 		sata_remove_target_node(sata_hba_inst, saddr);
19568 	}
19569 
19570 
19571 	/*
19572 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19573 	 * with the hint: SE_HINT_REMOVE
19574 	 */
19575 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19576 }
19577 
19578 /*
19579  * Port Multiplier Port Device Deattached Event processing.
19580  *
19581  * NOTE: No Mutex should be hold.
19582  */
19583 static void
19584 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19585     sata_address_t *saddr)
19586 {
19587 	sata_pmport_info_t *pmportinfo;
19588 	sata_drive_info_t *sdevinfo;
19589 	sata_device_t sata_device;
19590 	int rval;
19591 	uint8_t cport, pmport;
19592 
19593 	cport = saddr->cport;
19594 	pmport = saddr->pmport;
19595 
19596 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19597 	    "Processing port %d:%d device detached",
19598 	    cport, pmport);
19599 
19600 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19601 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19602 
19603 	/* Clear event flag */
19604 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19605 
19606 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19607 	if ((pmportinfo->pmport_state &
19608 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19609 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19610 		return;
19611 	}
19612 	/* For sanity, re-probe the port */
19613 	sata_device.satadev_rev = SATA_DEVICE_REV;
19614 	sata_device.satadev_addr = *saddr;
19615 
19616 	/*
19617 	 * We have to exit mutex, because the HBA probe port function may
19618 	 * block on its own mutex.
19619 	 */
19620 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19621 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19622 	    (SATA_DIP(sata_hba_inst), &sata_device);
19623 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19624 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19625 	if (rval != SATA_SUCCESS) {
19626 		/* Something went wrong? Fail the port */
19627 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19628 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19629 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19630 		    "SATA port %d:%d probing failed",
19631 		    saddr->pmport));
19632 		/*
19633 		 * We may want to release device info structure, but
19634 		 * it is not necessary.
19635 		 */
19636 		return;
19637 	} else {
19638 		/* port probed successfully */
19639 		pmportinfo->pmport_state |=
19640 		    SATA_STATE_PROBED | SATA_STATE_READY;
19641 	}
19642 	/*
19643 	 * Check if a device is still attached. For sanity, check also
19644 	 * link status - if no link, there is no device.
19645 	 */
19646 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19647 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19648 	    SATA_DTYPE_NONE) {
19649 		/*
19650 		 * Device is still attached - ignore detach event.
19651 		 */
19652 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19653 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19654 		    "Ignoring detach - device still attached to port %d",
19655 		    sata_device.satadev_addr.pmport);
19656 		return;
19657 	}
19658 	/*
19659 	 * We need to detach and release device info structure here
19660 	 */
19661 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19662 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19663 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19664 		(void) kmem_free((void *)sdevinfo,
19665 		    sizeof (sata_drive_info_t));
19666 	}
19667 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19668 	/*
19669 	 * Device cannot be reached anymore, even if the target node may be
19670 	 * still present.
19671 	 */
19672 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19673 
19674 	/*
19675 	 * Try to offline a device and remove target node if it still exists
19676 	 */
19677 	sata_remove_target_node(sata_hba_inst, saddr);
19678 
19679 	/*
19680 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19681 	 * with the hint: SE_HINT_REMOVE
19682 	 */
19683 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19684 }
19685 
19686 
19687 /*
19688  * Device Attached Event processing.
19689  * Port state is checked to verify that a device is really attached. If so,
19690  * the device info structure is created and attached to the SATA port info
19691  * structure.
19692  *
19693  * If attached device cannot be identified or set-up, the retry for the
19694  * attach processing is set-up. Subsequent daemon run would try again to
19695  * identify the device, until the time limit is reached
19696  * (SATA_DEV_IDENTIFY_TIMEOUT).
19697  *
19698  * This function cannot be called in interrupt context (it may sleep).
19699  *
19700  * NOTE: Port multiplier ports events are handled by
19701  * sata_process_pmdevice_attached()
19702  */
19703 static void
19704 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19705     sata_address_t *saddr)
19706 {
19707 	sata_cport_info_t *cportinfo = NULL;
19708 	sata_drive_info_t *sdevinfo = NULL;
19709 	sata_pmult_info_t *pmultinfo = NULL;
19710 	sata_pmport_info_t *pmportinfo = NULL;
19711 	sata_device_t sata_device;
19712 	dev_info_t *tdip;
19713 	uint32_t event_flags = 0, pmult_event_flags = 0;
19714 	int rval;
19715 	int npmport;
19716 
19717 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19718 	    "Processing port %d device attached", saddr->cport);
19719 
19720 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19721 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19722 
19723 	/* Clear attach event flag first */
19724 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19725 
19726 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19727 	if ((cportinfo->cport_state &
19728 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19729 		cportinfo->cport_dev_attach_time = 0;
19730 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19731 		    cport_mutex);
19732 		return;
19733 	}
19734 
19735 	/*
19736 	 * If the sata_drive_info structure is found attached to the port info,
19737 	 * despite the fact the device was removed and now it is re-attached,
19738 	 * the old drive info structure was not removed.
19739 	 * Arbitrarily release device info structure.
19740 	 */
19741 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19742 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19743 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19744 		(void) kmem_free((void *)sdevinfo,
19745 		    sizeof (sata_drive_info_t));
19746 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19747 		    "Arbitrarily detaching old device info.", NULL);
19748 	}
19749 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19750 
19751 	/* For sanity, re-probe the port */
19752 	sata_device.satadev_rev = SATA_DEVICE_REV;
19753 	sata_device.satadev_addr = *saddr;
19754 
19755 	/*
19756 	 * We have to exit mutex, because the HBA probe port function may
19757 	 * block on its own mutex.
19758 	 */
19759 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19760 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19761 	    (SATA_DIP(sata_hba_inst), &sata_device);
19762 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19763 	sata_update_port_info(sata_hba_inst, &sata_device);
19764 	if (rval != SATA_SUCCESS) {
19765 		/* Something went wrong? Fail the port */
19766 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19767 		cportinfo->cport_dev_attach_time = 0;
19768 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19769 		    cport_mutex);
19770 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19771 		    "SATA port %d probing failed",
19772 		    saddr->cport));
19773 		return;
19774 	} else {
19775 		/* port probed successfully */
19776 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19777 	}
19778 	/*
19779 	 * Check if a device is still attached. For sanity, check also
19780 	 * link status - if no link, there is no device.
19781 	 */
19782 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19783 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19784 	    SATA_DTYPE_NONE) {
19785 		/*
19786 		 * No device - ignore attach event.
19787 		 */
19788 		cportinfo->cport_dev_attach_time = 0;
19789 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19790 		    cport_mutex);
19791 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19792 		    "Ignoring attach - no device connected to port %d",
19793 		    sata_device.satadev_addr.cport);
19794 		return;
19795 	}
19796 
19797 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19798 	/*
19799 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19800 	 * with the hint: SE_HINT_INSERT
19801 	 */
19802 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19803 
19804 	/*
19805 	 * Port reprobing will take care of the creation of the device
19806 	 * info structure and determination of the device type.
19807 	 */
19808 	sata_device.satadev_addr = *saddr;
19809 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19810 	    SATA_DEV_IDENTIFY_NORETRY);
19811 
19812 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19813 	    cport_mutex);
19814 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19815 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19816 		/* Some device is attached to the port */
19817 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19818 			/*
19819 			 * A device was not successfully attached.
19820 			 * Track retry time for device identification.
19821 			 */
19822 			if (cportinfo->cport_dev_attach_time != 0) {
19823 				clock_t cur_time = ddi_get_lbolt();
19824 				/*
19825 				 * If the retry time limit was not exceeded,
19826 				 * reinstate attach event.
19827 				 */
19828 				if ((cur_time -
19829 				    cportinfo->cport_dev_attach_time) <
19830 				    drv_usectohz(
19831 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19832 					/* OK, restore attach event */
19833 					cportinfo->cport_event_flags |=
19834 					    SATA_EVNT_DEVICE_ATTACHED;
19835 				} else {
19836 					/* Timeout - cannot identify device */
19837 					cportinfo->cport_dev_attach_time = 0;
19838 					sata_log(sata_hba_inst,
19839 					    CE_WARN,
19840 					    "Could not identify SATA device "
19841 					    "at port %d",
19842 					    saddr->cport);
19843 				}
19844 			} else {
19845 				/*
19846 				 * Start tracking time for device
19847 				 * identification.
19848 				 * Save current time (lbolt value).
19849 				 */
19850 				cportinfo->cport_dev_attach_time =
19851 				    ddi_get_lbolt();
19852 				/* Restore attach event */
19853 				cportinfo->cport_event_flags |=
19854 				    SATA_EVNT_DEVICE_ATTACHED;
19855 			}
19856 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19857 			cportinfo->cport_dev_attach_time = 0;
19858 			sata_log(sata_hba_inst, CE_NOTE,
19859 			    "SATA port-multiplier detected at port %d",
19860 			    saddr->cport);
19861 
19862 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19863 				/* Log the info of new port multiplier */
19864 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19865 				    saddr->cport)->cport_mutex);
19866 				sata_show_pmult_info(sata_hba_inst,
19867 				    &sata_device);
19868 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19869 				    saddr->cport)->cport_mutex);
19870 			}
19871 
19872 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19873 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19874 			for (npmport = 0; npmport <
19875 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19876 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19877 				    saddr->cport, npmport);
19878 				ASSERT(pmportinfo != NULL);
19879 
19880 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19881 				    saddr->cport)->cport_mutex);
19882 				mutex_enter(&pmportinfo->pmport_mutex);
19883 				/* Marked all pmports with link events. */
19884 				pmportinfo->pmport_event_flags =
19885 				    SATA_EVNT_LINK_ESTABLISHED;
19886 				pmult_event_flags |=
19887 				    pmportinfo->pmport_event_flags;
19888 				mutex_exit(&pmportinfo->pmport_mutex);
19889 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19890 				    saddr->cport)->cport_mutex);
19891 			}
19892 			/* Auto-online is not available for PMult now. */
19893 
19894 		} else {
19895 			/*
19896 			 * If device was successfully attached, the subsequent
19897 			 * action depends on a state of the
19898 			 * sata_auto_online variable. If it is set to zero.
19899 			 * an explicit 'configure' command will be needed to
19900 			 * configure it. If its value is non-zero, we will
19901 			 * attempt to online (configure) the device.
19902 			 * First, log the message indicating that a device
19903 			 * was attached.
19904 			 */
19905 			cportinfo->cport_dev_attach_time = 0;
19906 			sata_log(sata_hba_inst, CE_WARN,
19907 			    "SATA device detected at port %d", saddr->cport);
19908 
19909 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19910 				sata_drive_info_t new_sdinfo;
19911 
19912 				/* Log device info data */
19913 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19914 				    cportinfo));
19915 				sata_show_drive_info(sata_hba_inst,
19916 				    &new_sdinfo);
19917 			}
19918 
19919 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19920 			    saddr->cport)->cport_mutex);
19921 
19922 			/*
19923 			 * Make sure that there is no target node for that
19924 			 * device. If so, release it. It should not happen,
19925 			 * unless we had problem removing the node when
19926 			 * device was detached.
19927 			 */
19928 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19929 			    saddr->cport, saddr->pmport);
19930 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19931 			    saddr->cport)->cport_mutex);
19932 			if (tdip != NULL) {
19933 
19934 #ifdef SATA_DEBUG
19935 				if ((cportinfo->cport_event_flags &
19936 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19937 					sata_log(sata_hba_inst, CE_WARN,
19938 					    "sata_process_device_attached: "
19939 					    "old device target node exists!");
19940 #endif
19941 				/*
19942 				 * target node exists - try to unconfigure
19943 				 * device and remove the node.
19944 				 */
19945 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19946 				    saddr->cport)->cport_mutex);
19947 				rval = ndi_devi_offline(tdip,
19948 				    NDI_DEVI_REMOVE);
19949 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19950 				    saddr->cport)->cport_mutex);
19951 
19952 				if (rval == NDI_SUCCESS) {
19953 					cportinfo->cport_event_flags &=
19954 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19955 					cportinfo->cport_tgtnode_clean = B_TRUE;
19956 				} else {
19957 					/*
19958 					 * PROBLEM - the target node remained
19959 					 * and it belongs to a previously
19960 					 * attached device.
19961 					 * This happens when the file was open
19962 					 * or the node was waiting for
19963 					 * resources at the time the
19964 					 * associated device was removed.
19965 					 * Instruct event daemon to retry the
19966 					 * cleanup later.
19967 					 */
19968 					sata_log(sata_hba_inst,
19969 					    CE_WARN,
19970 					    "Application(s) accessing "
19971 					    "previously attached SATA "
19972 					    "device have to release "
19973 					    "it before newly inserted "
19974 					    "device can be made accessible.",
19975 					    saddr->cport);
19976 					cportinfo->cport_event_flags |=
19977 					    SATA_EVNT_TARGET_NODE_CLEANUP;
19978 					cportinfo->cport_tgtnode_clean =
19979 					    B_FALSE;
19980 				}
19981 			}
19982 			if (sata_auto_online != 0) {
19983 				cportinfo->cport_event_flags |=
19984 				    SATA_EVNT_AUTOONLINE_DEVICE;
19985 			}
19986 
19987 		}
19988 	} else {
19989 		cportinfo->cport_dev_attach_time = 0;
19990 	}
19991 
19992 	event_flags = cportinfo->cport_event_flags;
19993 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19994 	if (event_flags != 0 || pmult_event_flags != 0) {
19995 		mutex_enter(&sata_hba_inst->satahba_mutex);
19996 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19997 		mutex_exit(&sata_hba_inst->satahba_mutex);
19998 		mutex_enter(&sata_mutex);
19999 		sata_event_pending |= SATA_EVNT_MAIN;
20000 		mutex_exit(&sata_mutex);
20001 	}
20002 }
20003 
20004 /*
20005  * Port Multiplier Port Device Attached Event processing.
20006  *
20007  * NOTE: No Mutex should be hold.
20008  */
20009 static void
20010 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20011     sata_address_t *saddr)
20012 {
20013 	sata_pmport_info_t *pmportinfo;
20014 	sata_drive_info_t *sdinfo;
20015 	sata_device_t sata_device;
20016 	dev_info_t *tdip;
20017 	uint32_t event_flags;
20018 	uint8_t cport = saddr->cport;
20019 	uint8_t pmport = saddr->pmport;
20020 	int rval;
20021 
20022 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20023 	    "Processing port %d:%d device attached", cport, pmport);
20024 
20025 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20026 
20027 	mutex_enter(&pmportinfo->pmport_mutex);
20028 
20029 	/* Clear attach event flag first */
20030 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20031 
20032 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20033 	if ((pmportinfo->pmport_state &
20034 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20035 		pmportinfo->pmport_dev_attach_time = 0;
20036 		mutex_exit(&pmportinfo->pmport_mutex);
20037 		return;
20038 	}
20039 
20040 	/*
20041 	 * If the sata_drive_info structure is found attached to the port info,
20042 	 * despite the fact the device was removed and now it is re-attached,
20043 	 * the old drive info structure was not removed.
20044 	 * Arbitrarily release device info structure.
20045 	 */
20046 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20047 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20048 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20049 		(void) kmem_free((void *)sdinfo,
20050 		    sizeof (sata_drive_info_t));
20051 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20052 		    "Arbitrarily detaching old device info.", NULL);
20053 	}
20054 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20055 
20056 	/* For sanity, re-probe the port */
20057 	sata_device.satadev_rev = SATA_DEVICE_REV;
20058 	sata_device.satadev_addr = *saddr;
20059 
20060 	/*
20061 	 * We have to exit mutex, because the HBA probe port function may
20062 	 * block on its own mutex.
20063 	 */
20064 	mutex_exit(&pmportinfo->pmport_mutex);
20065 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20066 	    (SATA_DIP(sata_hba_inst), &sata_device);
20067 	mutex_enter(&pmportinfo->pmport_mutex);
20068 
20069 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20070 	if (rval != SATA_SUCCESS) {
20071 		/* Something went wrong? Fail the port */
20072 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20073 		pmportinfo->pmport_dev_attach_time = 0;
20074 		mutex_exit(&pmportinfo->pmport_mutex);
20075 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20076 		    "SATA port %d:%d probing failed", cport, pmport));
20077 		return;
20078 	} else {
20079 		/* pmport probed successfully */
20080 		pmportinfo->pmport_state |=
20081 		    SATA_STATE_PROBED | SATA_STATE_READY;
20082 	}
20083 	/*
20084 	 * Check if a device is still attached. For sanity, check also
20085 	 * link status - if no link, there is no device.
20086 	 */
20087 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20088 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20089 	    SATA_DTYPE_NONE) {
20090 		/*
20091 		 * No device - ignore attach event.
20092 		 */
20093 		pmportinfo->pmport_dev_attach_time = 0;
20094 		mutex_exit(&pmportinfo->pmport_mutex);
20095 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20096 		    "Ignoring attach - no device connected to port %d:%d",
20097 		    cport, pmport);
20098 		return;
20099 	}
20100 
20101 	mutex_exit(&pmportinfo->pmport_mutex);
20102 	/*
20103 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20104 	 * with the hint: SE_HINT_INSERT
20105 	 */
20106 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20107 
20108 	/*
20109 	 * Port reprobing will take care of the creation of the device
20110 	 * info structure and determination of the device type.
20111 	 */
20112 	sata_device.satadev_addr = *saddr;
20113 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20114 	    SATA_DEV_IDENTIFY_NORETRY);
20115 
20116 	mutex_enter(&pmportinfo->pmport_mutex);
20117 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20118 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20119 		/* Some device is attached to the port */
20120 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20121 			/*
20122 			 * A device was not successfully attached.
20123 			 * Track retry time for device identification.
20124 			 */
20125 			if (pmportinfo->pmport_dev_attach_time != 0) {
20126 				clock_t cur_time = ddi_get_lbolt();
20127 				/*
20128 				 * If the retry time limit was not exceeded,
20129 				 * reinstate attach event.
20130 				 */
20131 				if ((cur_time -
20132 				    pmportinfo->pmport_dev_attach_time) <
20133 				    drv_usectohz(
20134 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20135 					/* OK, restore attach event */
20136 					pmportinfo->pmport_event_flags |=
20137 					    SATA_EVNT_DEVICE_ATTACHED;
20138 				} else {
20139 					/* Timeout - cannot identify device */
20140 					pmportinfo->pmport_dev_attach_time = 0;
20141 					sata_log(sata_hba_inst, CE_WARN,
20142 					    "Could not identify SATA device "
20143 					    "at port %d:%d",
20144 					    cport, pmport);
20145 				}
20146 			} else {
20147 				/*
20148 				 * Start tracking time for device
20149 				 * identification.
20150 				 * Save current time (lbolt value).
20151 				 */
20152 				pmportinfo->pmport_dev_attach_time =
20153 				    ddi_get_lbolt();
20154 				/* Restore attach event */
20155 				pmportinfo->pmport_event_flags |=
20156 				    SATA_EVNT_DEVICE_ATTACHED;
20157 			}
20158 		} else {
20159 			/*
20160 			 * If device was successfully attached, the subsequent
20161 			 * action depends on a state of the
20162 			 * sata_auto_online variable. If it is set to zero.
20163 			 * an explicit 'configure' command will be needed to
20164 			 * configure it. If its value is non-zero, we will
20165 			 * attempt to online (configure) the device.
20166 			 * First, log the message indicating that a device
20167 			 * was attached.
20168 			 */
20169 			pmportinfo->pmport_dev_attach_time = 0;
20170 			sata_log(sata_hba_inst, CE_WARN,
20171 			    "SATA device detected at port %d:%d",
20172 			    cport, pmport);
20173 
20174 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20175 				sata_drive_info_t new_sdinfo;
20176 
20177 				/* Log device info data */
20178 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20179 				    pmportinfo));
20180 				sata_show_drive_info(sata_hba_inst,
20181 				    &new_sdinfo);
20182 			}
20183 
20184 			mutex_exit(&pmportinfo->pmport_mutex);
20185 
20186 			/*
20187 			 * Make sure that there is no target node for that
20188 			 * device. If so, release it. It should not happen,
20189 			 * unless we had problem removing the node when
20190 			 * device was detached.
20191 			 */
20192 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20193 			    saddr->cport, saddr->pmport);
20194 			mutex_enter(&pmportinfo->pmport_mutex);
20195 			if (tdip != NULL) {
20196 
20197 #ifdef SATA_DEBUG
20198 				if ((pmportinfo->pmport_event_flags &
20199 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20200 					sata_log(sata_hba_inst, CE_WARN,
20201 					    "sata_process_device_attached: "
20202 					    "old device target node exists!");
20203 #endif
20204 				/*
20205 				 * target node exists - try to unconfigure
20206 				 * device and remove the node.
20207 				 */
20208 				mutex_exit(&pmportinfo->pmport_mutex);
20209 				rval = ndi_devi_offline(tdip,
20210 				    NDI_DEVI_REMOVE);
20211 				mutex_enter(&pmportinfo->pmport_mutex);
20212 
20213 				if (rval == NDI_SUCCESS) {
20214 					pmportinfo->pmport_event_flags &=
20215 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20216 					pmportinfo->pmport_tgtnode_clean =
20217 					    B_TRUE;
20218 				} else {
20219 					/*
20220 					 * PROBLEM - the target node remained
20221 					 * and it belongs to a previously
20222 					 * attached device.
20223 					 * This happens when the file was open
20224 					 * or the node was waiting for
20225 					 * resources at the time the
20226 					 * associated device was removed.
20227 					 * Instruct event daemon to retry the
20228 					 * cleanup later.
20229 					 */
20230 					sata_log(sata_hba_inst,
20231 					    CE_WARN,
20232 					    "Application(s) accessing "
20233 					    "previously attached SATA "
20234 					    "device have to release "
20235 					    "it before newly inserted "
20236 					    "device can be made accessible."
20237 					    "at port %d:%d",
20238 					    cport, pmport);
20239 					pmportinfo->pmport_event_flags |=
20240 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20241 					pmportinfo->pmport_tgtnode_clean =
20242 					    B_FALSE;
20243 				}
20244 			}
20245 			if (sata_auto_online != 0) {
20246 				pmportinfo->pmport_event_flags |=
20247 				    SATA_EVNT_AUTOONLINE_DEVICE;
20248 			}
20249 
20250 		}
20251 	} else {
20252 		pmportinfo->pmport_dev_attach_time = 0;
20253 	}
20254 
20255 	event_flags = pmportinfo->pmport_event_flags;
20256 	mutex_exit(&pmportinfo->pmport_mutex);
20257 	if (event_flags != 0) {
20258 		mutex_enter(&sata_hba_inst->satahba_mutex);
20259 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20260 		mutex_exit(&sata_hba_inst->satahba_mutex);
20261 		mutex_enter(&sata_mutex);
20262 		sata_event_pending |= SATA_EVNT_MAIN;
20263 		mutex_exit(&sata_mutex);
20264 	}
20265 
20266 	/* clear the reset_in_progress events */
20267 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20268 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20269 			/* must clear flags on cport */
20270 			sata_pmult_info_t *pminfo =
20271 			    SATA_PMULT_INFO(sata_hba_inst,
20272 			    saddr->cport);
20273 			pminfo->pmult_event_flags |=
20274 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20275 		}
20276 	}
20277 }
20278 
20279 /*
20280  * Device Target Node Cleanup Event processing.
20281  * If the target node associated with a sata port device is in
20282  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20283  * If the target node cannot be removed, the event flag is left intact,
20284  * so that event daemon may re-run this function later.
20285  *
20286  * This function cannot be called in interrupt context (it may sleep).
20287  *
20288  * NOTE: Processes cport events only, not port multiplier ports.
20289  */
20290 static void
20291 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20292     sata_address_t *saddr)
20293 {
20294 	sata_cport_info_t *cportinfo;
20295 	dev_info_t *tdip;
20296 
20297 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20298 	    "Processing port %d device target node cleanup", saddr->cport);
20299 
20300 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20301 
20302 	/*
20303 	 * Check if there is target node for that device and it is in the
20304 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20305 	 */
20306 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20307 	    saddr->pmport);
20308 	if (tdip != NULL) {
20309 		/*
20310 		 * target node exists - check if it is target node of
20311 		 * a removed device.
20312 		 */
20313 		if (sata_check_device_removed(tdip) == B_TRUE) {
20314 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20315 			    "sata_process_target_node_cleanup: "
20316 			    "old device target node exists!", NULL);
20317 			/*
20318 			 * Unconfigure and remove the target node
20319 			 */
20320 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20321 			    NDI_SUCCESS) {
20322 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20323 				    saddr->cport)->cport_mutex);
20324 				cportinfo->cport_event_flags &=
20325 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20326 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20327 				    saddr->cport)->cport_mutex);
20328 				return;
20329 			}
20330 			/*
20331 			 * Event daemon will retry the cleanup later.
20332 			 */
20333 			mutex_enter(&sata_hba_inst->satahba_mutex);
20334 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20335 			mutex_exit(&sata_hba_inst->satahba_mutex);
20336 			mutex_enter(&sata_mutex);
20337 			sata_event_pending |= SATA_EVNT_MAIN;
20338 			mutex_exit(&sata_mutex);
20339 		}
20340 	} else {
20341 		if (saddr->qual == SATA_ADDR_CPORT ||
20342 		    saddr->qual == SATA_ADDR_DCPORT) {
20343 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20344 			    saddr->cport)->cport_mutex);
20345 			cportinfo->cport_event_flags &=
20346 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20347 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20348 			    saddr->cport)->cport_mutex);
20349 		} else {
20350 			/* sanity check */
20351 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20352 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20353 			    saddr->cport) == NULL)
20354 				return;
20355 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20356 			    saddr->pmport) == NULL)
20357 				return;
20358 
20359 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20360 			    saddr->cport, saddr->pmport)->pmport_mutex);
20361 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20362 			    saddr->pmport)->pmport_event_flags &=
20363 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20364 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20365 			    saddr->cport, saddr->pmport)->pmport_mutex);
20366 		}
20367 	}
20368 }
20369 
20370 /*
20371  * Device AutoOnline Event processing.
20372  * If attached device is to be onlined, an attempt is made to online this
20373  * device, but only if there is no lingering (old) target node present.
20374  * If the device cannot be onlined, the event flag is left intact,
20375  * so that event daemon may re-run this function later.
20376  *
20377  * This function cannot be called in interrupt context (it may sleep).
20378  *
20379  * NOTE: Processes cport events only, not port multiplier ports.
20380  */
20381 static void
20382 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20383     sata_address_t *saddr)
20384 {
20385 	sata_cport_info_t *cportinfo;
20386 	sata_drive_info_t *sdinfo;
20387 	sata_device_t sata_device;
20388 	dev_info_t *tdip;
20389 
20390 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20391 	    "Processing port %d attached device auto-onlining", saddr->cport);
20392 
20393 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20394 
20395 	/*
20396 	 * Check if device is present and recognized. If not, reset event.
20397 	 */
20398 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20399 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20400 		/* Nothing to online */
20401 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20402 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20403 		    saddr->cport)->cport_mutex);
20404 		return;
20405 	}
20406 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20407 
20408 	/*
20409 	 * Check if there is target node for this device and if it is in the
20410 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20411 	 * the event for later processing.
20412 	 */
20413 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20414 	    saddr->pmport);
20415 	if (tdip != NULL) {
20416 		/*
20417 		 * target node exists - check if it is target node of
20418 		 * a removed device.
20419 		 */
20420 		if (sata_check_device_removed(tdip) == B_TRUE) {
20421 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20422 			    "sata_process_device_autoonline: "
20423 			    "old device target node exists!", NULL);
20424 			/*
20425 			 * Event daemon will retry device onlining later.
20426 			 */
20427 			mutex_enter(&sata_hba_inst->satahba_mutex);
20428 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20429 			mutex_exit(&sata_hba_inst->satahba_mutex);
20430 			mutex_enter(&sata_mutex);
20431 			sata_event_pending |= SATA_EVNT_MAIN;
20432 			mutex_exit(&sata_mutex);
20433 			return;
20434 		}
20435 		/*
20436 		 * If the target node is not in the 'removed" state, assume
20437 		 * that it belongs to this device. There is nothing more to do,
20438 		 * but reset the event.
20439 		 */
20440 	} else {
20441 
20442 		/*
20443 		 * Try to online the device
20444 		 * If there is any reset-related event, remove it. We are
20445 		 * configuring the device and no state restoring is needed.
20446 		 */
20447 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20448 		    saddr->cport)->cport_mutex);
20449 		sata_device.satadev_addr = *saddr;
20450 		if (saddr->qual == SATA_ADDR_CPORT)
20451 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20452 		else
20453 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20454 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20455 		if (sdinfo != NULL) {
20456 			if (sdinfo->satadrv_event_flags &
20457 			    (SATA_EVNT_DEVICE_RESET |
20458 			    SATA_EVNT_INPROC_DEVICE_RESET))
20459 				sdinfo->satadrv_event_flags = 0;
20460 			sdinfo->satadrv_event_flags |=
20461 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20462 
20463 			/* Need to create a new target node. */
20464 			cportinfo->cport_tgtnode_clean = B_TRUE;
20465 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20466 			    saddr->cport)->cport_mutex);
20467 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20468 			    sata_hba_inst, &sata_device.satadev_addr);
20469 			if (tdip == NULL) {
20470 				/*
20471 				 * Configure (onlining) failed.
20472 				 * We will NOT retry
20473 				 */
20474 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20475 				    "sata_process_device_autoonline: "
20476 				    "configuring SATA device at port %d failed",
20477 				    saddr->cport));
20478 			}
20479 		} else {
20480 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20481 			    saddr->cport)->cport_mutex);
20482 		}
20483 
20484 	}
20485 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20486 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20487 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20488 	    saddr->cport)->cport_mutex);
20489 }
20490 
20491 
20492 static void
20493 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20494     int hint)
20495 {
20496 	char ap[MAXPATHLEN];
20497 	nvlist_t *ev_attr_list = NULL;
20498 	int err;
20499 
20500 	/* Allocate and build sysevent attribute list */
20501 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20502 	if (err != 0) {
20503 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20504 		    "sata_gen_sysevent: "
20505 		    "cannot allocate memory for sysevent attributes\n"));
20506 		return;
20507 	}
20508 	/* Add hint attribute */
20509 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20510 	if (err != 0) {
20511 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20512 		    "sata_gen_sysevent: "
20513 		    "failed to add DR_HINT attr for sysevent"));
20514 		nvlist_free(ev_attr_list);
20515 		return;
20516 	}
20517 	/*
20518 	 * Add AP attribute.
20519 	 * Get controller pathname and convert it into AP pathname by adding
20520 	 * a target number.
20521 	 */
20522 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20523 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20524 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20525 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20526 
20527 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20528 	if (err != 0) {
20529 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20530 		    "sata_gen_sysevent: "
20531 		    "failed to add DR_AP_ID attr for sysevent"));
20532 		nvlist_free(ev_attr_list);
20533 		return;
20534 	}
20535 
20536 	/* Generate/log sysevent */
20537 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20538 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20539 	if (err != DDI_SUCCESS) {
20540 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20541 		    "sata_gen_sysevent: "
20542 		    "cannot log sysevent, err code %x\n", err));
20543 	}
20544 
20545 	nvlist_free(ev_attr_list);
20546 }
20547 
20548 
20549 
20550 
20551 /*
20552  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20553  */
20554 static void
20555 sata_set_device_removed(dev_info_t *tdip)
20556 {
20557 	int circ;
20558 
20559 	ASSERT(tdip != NULL);
20560 
20561 	ndi_devi_enter(tdip, &circ);
20562 	mutex_enter(&DEVI(tdip)->devi_lock);
20563 	DEVI_SET_DEVICE_REMOVED(tdip);
20564 	mutex_exit(&DEVI(tdip)->devi_lock);
20565 	ndi_devi_exit(tdip, circ);
20566 }
20567 
20568 
20569 /*
20570  * Set internal event instructing event daemon to try
20571  * to perform the target node cleanup.
20572  */
20573 static void
20574 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20575     sata_address_t *saddr)
20576 {
20577 	if (saddr->qual == SATA_ADDR_CPORT ||
20578 	    saddr->qual == SATA_ADDR_DCPORT) {
20579 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20580 		    saddr->cport)->cport_mutex);
20581 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20582 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20583 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20584 		    cport_tgtnode_clean = B_FALSE;
20585 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20586 		    saddr->cport)->cport_mutex);
20587 	} else {
20588 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20589 		    saddr->cport, saddr->pmport)->pmport_mutex);
20590 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20591 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20592 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20593 		    pmport_tgtnode_clean = B_FALSE;
20594 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20595 		    saddr->cport, saddr->pmport)->pmport_mutex);
20596 	}
20597 	mutex_enter(&sata_hba_inst->satahba_mutex);
20598 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20599 	mutex_exit(&sata_hba_inst->satahba_mutex);
20600 	mutex_enter(&sata_mutex);
20601 	sata_event_pending |= SATA_EVNT_MAIN;
20602 	mutex_exit(&sata_mutex);
20603 }
20604 
20605 
20606 /*
20607  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20608  * i.e. check if the target node state indicates that it belongs to a removed
20609  * device.
20610  *
20611  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20612  * B_FALSE otherwise.
20613  */
20614 static boolean_t
20615 sata_check_device_removed(dev_info_t *tdip)
20616 {
20617 	ASSERT(tdip != NULL);
20618 
20619 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20620 		return (B_TRUE);
20621 	else
20622 		return (B_FALSE);
20623 }
20624 
20625 
20626 /*
20627  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20628  */
20629 static boolean_t
20630 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20631 {
20632 	int fm_capability = ddi_fm_capable(dip);
20633 	ddi_fm_error_t de;
20634 
20635 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20636 		if (spx->txlt_buf_dma_handle != NULL) {
20637 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20638 			    DDI_FME_VERSION);
20639 			if (de.fme_status != DDI_SUCCESS)
20640 				return (B_TRUE);
20641 		}
20642 	}
20643 	return (B_FALSE);
20644 }
20645 
20646 
20647 /* ************************ FAULT INJECTTION **************************** */
20648 
20649 #ifdef SATA_INJECT_FAULTS
20650 
20651 static	uint32_t sata_fault_count = 0;
20652 static	uint32_t sata_fault_suspend_count = 0;
20653 
20654 /*
20655  * Inject sata pkt fault
20656  * It modifies returned values of the sata packet.
20657  * It returns immediately if:
20658  * pkt fault injection is not enabled (via sata_inject_fault,
20659  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20660  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20661  * pkt is not directed to specified fault controller/device
20662  * (sata_fault_ctrl_dev and sata_fault_device).
20663  * If fault controller is not specified, fault injection applies to all
20664  * controllers and devices.
20665  *
20666  * First argument is the pointer to the executed sata packet.
20667  * Second argument is a pointer to a value returned by the HBA tran_start
20668  * function.
20669  * Third argument specifies injected error. Injected sata packet faults
20670  * are the satapkt_reason values.
20671  * SATA_PKT_BUSY		-1	Not completed, busy
20672  * SATA_PKT_DEV_ERROR		1	Device reported error
20673  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20674  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20675  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20676  * SATA_PKT_ABORTED		5	Aborted by request
20677  * SATA_PKT_TIMEOUT		6	Operation timeut
20678  * SATA_PKT_RESET		7	Aborted by reset request
20679  *
20680  * Additional global variables affecting the execution:
20681  *
20682  * sata_inject_fault_count variable specifies number of times in row the
20683  * error is injected. Value of -1 specifies permanent fault, ie. every time
20684  * the fault injection point is reached, the fault is injected and a pause
20685  * between fault injection specified by sata_inject_fault_pause_count is
20686  * ignored). Fault injection routine decrements sata_inject_fault_count
20687  * (if greater than zero) until it reaches 0. No fault is injected when
20688  * sata_inject_fault_count is 0 (zero).
20689  *
20690  * sata_inject_fault_pause_count variable specifies number of times a fault
20691  * injection is bypassed (pause between fault injections).
20692  * If set to 0, a fault is injected only a number of times specified by
20693  * sata_inject_fault_count.
20694  *
20695  * The fault counts are static, so for periodic errors they have to be manually
20696  * reset to start repetition sequence from scratch.
20697  * If the original value returned by the HBA tran_start function is not
20698  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20699  * is injected (to avoid masking real problems);
20700  *
20701  * NOTE: In its current incarnation, this function should be invoked only for
20702  * commands executed in SYNCHRONOUS mode.
20703  */
20704 
20705 
20706 static void
20707 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20708 {
20709 
20710 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20711 		return;
20712 
20713 	if (sata_inject_fault_count == 0)
20714 		return;
20715 
20716 	if (fault == 0)
20717 		return;
20718 
20719 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20720 		return;
20721 
20722 	if (sata_fault_ctrl != NULL) {
20723 		sata_pkt_txlate_t *spx =
20724 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20725 
20726 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20727 		    spx->txlt_sata_hba_inst->satahba_dip)
20728 			return;
20729 
20730 		if (sata_fault_device.satadev_addr.cport !=
20731 		    spkt->satapkt_device.satadev_addr.cport ||
20732 		    sata_fault_device.satadev_addr.pmport !=
20733 		    spkt->satapkt_device.satadev_addr.pmport ||
20734 		    sata_fault_device.satadev_addr.qual !=
20735 		    spkt->satapkt_device.satadev_addr.qual)
20736 			return;
20737 	}
20738 
20739 	/* Modify pkt return parameters */
20740 	if (*rval != SATA_TRAN_ACCEPTED ||
20741 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20742 		sata_fault_count = 0;
20743 		sata_fault_suspend_count = 0;
20744 		return;
20745 	}
20746 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20747 		/* Pause in the injection */
20748 		sata_fault_suspend_count -= 1;
20749 		return;
20750 	}
20751 
20752 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20753 		/*
20754 		 * Init inject fault cycle. If fault count is set to -1,
20755 		 * it is a permanent fault.
20756 		 */
20757 		if (sata_inject_fault_count != -1) {
20758 			sata_fault_count = sata_inject_fault_count;
20759 			sata_fault_suspend_count =
20760 			    sata_inject_fault_pause_count;
20761 			if (sata_fault_suspend_count == 0)
20762 				sata_inject_fault_count = 0;
20763 		}
20764 	}
20765 
20766 	if (sata_fault_count != 0)
20767 		sata_fault_count -= 1;
20768 
20769 	switch (fault) {
20770 	case SATA_PKT_BUSY:
20771 		*rval = SATA_TRAN_BUSY;
20772 		spkt->satapkt_reason = SATA_PKT_BUSY;
20773 		break;
20774 
20775 	case SATA_PKT_QUEUE_FULL:
20776 		*rval = SATA_TRAN_QUEUE_FULL;
20777 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20778 		break;
20779 
20780 	case SATA_PKT_CMD_UNSUPPORTED:
20781 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20782 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20783 		break;
20784 
20785 	case SATA_PKT_PORT_ERROR:
20786 		/* This is "rejected" command */
20787 		*rval = SATA_TRAN_PORT_ERROR;
20788 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20789 		/* Additional error setup could be done here - port state */
20790 		break;
20791 
20792 	case SATA_PKT_DEV_ERROR:
20793 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20794 		/*
20795 		 * Additional error setup could be done here
20796 		 */
20797 		break;
20798 
20799 	case SATA_PKT_ABORTED:
20800 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20801 		break;
20802 
20803 	case SATA_PKT_TIMEOUT:
20804 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20805 		/* Additional error setup could be done here */
20806 		break;
20807 
20808 	case SATA_PKT_RESET:
20809 		spkt->satapkt_reason = SATA_PKT_RESET;
20810 		/*
20811 		 * Additional error setup could be done here - device reset
20812 		 */
20813 		break;
20814 
20815 	default:
20816 		break;
20817 	}
20818 }
20819 
20820 #endif
20821 
20822 /*
20823  * SATA Trace Ring Buffer
20824  * ----------------------
20825  *
20826  * Overview
20827  *
20828  * The SATA trace ring buffer is a ring buffer created and managed by
20829  * the SATA framework module that can be used by any module or driver
20830  * within the SATA framework to store debug messages.
20831  *
20832  * Ring Buffer Interfaces:
20833  *
20834  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20835  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20836  *
20837  *	Note that the sata_trace_debug() interface was created to give
20838  *	consumers the flexibilty of sending debug messages to ring buffer
20839  *	as variable arguments.  Consumers can send type va_list debug
20840  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20841  *	and sata_vtrace_debug() relationship is similar to that of
20842  *	cmn_err(9F) and vcmn_err(9F).
20843  *
20844  * Below is a diagram of the SATA trace ring buffer interfaces and
20845  * sample consumers:
20846  *
20847  * +---------------------------------+
20848  * |    o  o  SATA Framework Module  |
20849  * | o  SATA  o     +------------------+      +------------------+
20850  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20851  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20852  * | o        o     +------------------+   |  +------------------+
20853  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20854  * |                        |        |        +------------------+
20855  * |           +------------------+  |
20856  * |           |SATA Debug Message|  |
20857  * |           +------------------+  |
20858  * +---------------------------------+
20859  *
20860  * Supporting Routines:
20861  *
20862  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20863  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20864  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20865  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20866  *
20867  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20868  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20869  * /etc/system to desired size in unit of bytes.
20870  *
20871  * The individual debug message size in the ring buffer is restricted
20872  * to DMSG_BUF_SIZE.
20873  */
20874 void
20875 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20876 {
20877 	sata_trace_dmsg_t *dmsg;
20878 
20879 	if (sata_debug_rbuf == NULL) {
20880 		return;
20881 	}
20882 
20883 	/*
20884 	 * If max size of ring buffer is smaller than size
20885 	 * required for one debug message then just return
20886 	 * since we have no room for the debug message.
20887 	 */
20888 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20889 		return;
20890 	}
20891 
20892 	mutex_enter(&sata_debug_rbuf->lock);
20893 
20894 	/* alloc or reuse on ring buffer */
20895 	dmsg = sata_trace_dmsg_alloc();
20896 
20897 	if (dmsg == NULL) {
20898 		/* resource allocation failed */
20899 		mutex_exit(&sata_debug_rbuf->lock);
20900 		return;
20901 	}
20902 
20903 	dmsg->dip = dip;
20904 	gethrestime(&dmsg->timestamp);
20905 
20906 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20907 
20908 	mutex_exit(&sata_debug_rbuf->lock);
20909 }
20910 
20911 void
20912 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20913 {
20914 	va_list ap;
20915 
20916 	va_start(ap, fmt);
20917 	sata_vtrace_debug(dip, fmt, ap);
20918 	va_end(ap);
20919 }
20920 
20921 /*
20922  * This routine is used to manage debug messages
20923  * on ring buffer.
20924  */
20925 static sata_trace_dmsg_t *
20926 sata_trace_dmsg_alloc(void)
20927 {
20928 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20929 
20930 	if (sata_debug_rbuf->looped == TRUE) {
20931 		sata_debug_rbuf->dmsgp = dmsg->next;
20932 		return (sata_debug_rbuf->dmsgp);
20933 	}
20934 
20935 	/*
20936 	 * If we're looping for the first time,
20937 	 * connect the ring.
20938 	 */
20939 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
20940 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
20941 		dmsg->next = sata_debug_rbuf->dmsgh;
20942 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
20943 		sata_debug_rbuf->looped = TRUE;
20944 		return (sata_debug_rbuf->dmsgp);
20945 	}
20946 
20947 	/* If we've gotten this far then memory allocation is needed */
20948 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
20949 	if (dmsg_alloc == NULL) {
20950 		sata_debug_rbuf->allocfailed++;
20951 		return (dmsg_alloc);
20952 	} else {
20953 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
20954 	}
20955 
20956 	if (sata_debug_rbuf->dmsgp != NULL) {
20957 		dmsg->next = dmsg_alloc;
20958 		sata_debug_rbuf->dmsgp = dmsg->next;
20959 		return (sata_debug_rbuf->dmsgp);
20960 	} else {
20961 		/*
20962 		 * We should only be here if we're initializing
20963 		 * the ring buffer.
20964 		 */
20965 		if (sata_debug_rbuf->dmsgh == NULL) {
20966 			sata_debug_rbuf->dmsgh = dmsg_alloc;
20967 		} else {
20968 			/* Something is wrong */
20969 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
20970 			return (NULL);
20971 		}
20972 
20973 		sata_debug_rbuf->dmsgp = dmsg_alloc;
20974 		return (sata_debug_rbuf->dmsgp);
20975 	}
20976 }
20977 
20978 
20979 /*
20980  * Free all messages on debug ring buffer.
20981  */
20982 static void
20983 sata_trace_dmsg_free(void)
20984 {
20985 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
20986 
20987 	while (dmsg != NULL) {
20988 		dmsg_next = dmsg->next;
20989 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
20990 
20991 		/*
20992 		 * If we've looped around the ring than we're done.
20993 		 */
20994 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
20995 			break;
20996 		} else {
20997 			dmsg = dmsg_next;
20998 		}
20999 	}
21000 }
21001 
21002 
21003 /*
21004  * This function can block
21005  */
21006 static void
21007 sata_trace_rbuf_alloc(void)
21008 {
21009 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21010 
21011 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21012 
21013 	if (dmsg_ring_size > 0) {
21014 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21015 	}
21016 }
21017 
21018 
21019 static void
21020 sata_trace_rbuf_free(void)
21021 {
21022 	sata_trace_dmsg_free();
21023 	mutex_destroy(&sata_debug_rbuf->lock);
21024 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21025 }
21026 
21027 /*
21028  * If SATA_DEBUG is not defined then this routine is called instead
21029  * of sata_log() via the SATA_LOG_D macro.
21030  */
21031 static void
21032 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21033     const char *fmt, ...)
21034 {
21035 #ifndef __lock_lint
21036 	_NOTE(ARGUNUSED(level))
21037 #endif
21038 
21039 	dev_info_t *dip = NULL;
21040 	va_list ap;
21041 
21042 	if (sata_hba_inst != NULL) {
21043 		dip = SATA_DIP(sata_hba_inst);
21044 	}
21045 
21046 	va_start(ap, fmt);
21047 	sata_vtrace_debug(dip, fmt, ap);
21048 	va_end(ap);
21049 }
21050